Skip to content

Commit e3a93b1

Browse files
chapsukpchaigno
authored andcommitted
datapath/linux/routing: Fix replace rule to 0.0.0.0/0 for ENI
Allows to use `ipv4-native-routing-cidr: 0.0.0.0/0` with ENI IPAM mode Signed-off-by: Maxim Krasilnikov <[email protected]>
1 parent eeb248d commit e3a93b1

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

pkg/datapath/linux/routing/routing.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,13 @@ func (info *RoutingInfo) Configure(ip net.IP, mtu int, compat bool, host bool) e
8888
if info.Masquerade && info.IpamMode == ipamOption.IPAMENI {
8989
// Lookup a VPC specific table for all traffic from an endpoint to the
9090
// CIDR configured for the VPC on which the endpoint has the IP on.
91+
// ReplaceRule function doesn't handle all zeros cidr and return `file exists` error,
92+
// so we need to normalize the rule to cidr here and in Delete
9193
for _, cidr := range info.IPv4CIDRs {
9294
if err := route.ReplaceRule(route.Rule{
9395
Priority: egressPriority,
9496
From: &ipWithMask,
95-
To: &cidr,
97+
To: normalizeRuleToCIDR(&cidr),
9698
Table: tableID,
9799
Protocol: linux_defaults.RTProto,
98100
}); err != nil {
@@ -206,7 +208,7 @@ func Delete(ip netip.Addr, compat bool) error {
206208
egress := route.Rule{
207209
Priority: priority,
208210
From: ipWithMask,
209-
To: cidr,
211+
To: normalizeRuleToCIDR(cidr),
210212
}
211213
if err := deleteRule(egress); err != nil {
212214
return fmt.Errorf("unable to delete egress rule with ip %s: %w", ipWithMask.String(), err)
@@ -314,3 +316,11 @@ func retrieveIfIndexFromMAC(mac mac.MAC, mtu int) (int, error) {
314316
func computeTableIDFromIfaceNumber(num int) int {
315317
return linux_defaults.RouteTableInterfacesOffset + num
316318
}
319+
320+
// normalizeRuleToCIDR returns nil when passed cidr is zeroes only cidr
321+
func normalizeRuleToCIDR(cidr *net.IPNet) *net.IPNet {
322+
if cidr.IP.IsUnspecified() {
323+
return nil
324+
}
325+
return cidr
326+
}

pkg/datapath/linux/routing/routing_test.go

+26-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestConfigure(t *testing.T) {
3030

3131
ns1 := netns.NewNetNS(t)
3232
ns1.Do(func() error {
33-
ip, ri := getFakes(t, true)
33+
ip, ri := getFakes(t, true, false)
3434
masterMAC := ri.MasterIfMAC
3535
ifaceCleanup := createDummyDevice(t, masterMAC)
3636
defer ifaceCleanup()
@@ -41,7 +41,22 @@ func TestConfigure(t *testing.T) {
4141

4242
ns2 := netns.NewNetNS(t)
4343
ns2.Do(func() error {
44-
ip, ri := getFakes(t, false)
44+
ip, ri := getFakes(t, false, false)
45+
masterMAC := ri.MasterIfMAC
46+
ifaceCleanup := createDummyDevice(t, masterMAC)
47+
defer ifaceCleanup()
48+
49+
runConfigureThenDelete(t, ri, ip, 1500)
50+
return nil
51+
})
52+
}
53+
54+
func TestConfigureZeros(t *testing.T) {
55+
setupLinuxRoutingSuite(t)
56+
57+
ns1 := netns.NewNetNS(t)
58+
ns1.Do(func() error {
59+
ip, ri := getFakes(t, true, true)
4560
masterMAC := ri.MasterIfMAC
4661
ifaceCleanup := createDummyDevice(t, masterMAC)
4762
defer ifaceCleanup()
@@ -54,7 +69,7 @@ func TestConfigure(t *testing.T) {
5469
func TestConfigureRouteWithIncompatibleIP(t *testing.T) {
5570
setupLinuxRoutingSuite(t)
5671

57-
_, ri := getFakes(t, true)
72+
_, ri := getFakes(t, true, false)
5873
ipv6 := netip.MustParseAddr("fd00::2").AsSlice()
5974
err := ri.Configure(ipv6, 1500, false, false)
6075
require.Error(t, err)
@@ -73,7 +88,7 @@ func TestDeleteRouteWithIncompatibleIP(t *testing.T) {
7388
func TestDelete(t *testing.T) {
7489
setupLinuxRoutingSuite(t)
7590

76-
fakeIP, fakeRoutingInfo := getFakes(t, true)
91+
fakeIP, fakeRoutingInfo := getFakes(t, true, false)
7792
masterMAC := fakeRoutingInfo.MasterIfMAC
7893

7994
tests := []struct {
@@ -235,7 +250,8 @@ func createDummyDevice(t *testing.T, macAddr mac.MAC) func() {
235250

236251
// getFakes returns a fake IP simulating an Endpoint IP and RoutingInfo as test harnesses.
237252
// To create routing info with a list of CIDRs which the interface has access to, set withCIDR parameter to true
238-
func getFakes(t *testing.T, withCIDR bool) (netip.Addr, RoutingInfo) {
253+
// If withZeroCIDR is also set to true, the function will use the "0.0.0.0/0" CIDR block instead of other CIDR blocks.
254+
func getFakes(t *testing.T, withCIDR bool, withZeroCIDR bool) (netip.Addr, RoutingInfo) {
239255
fakeGateway := netip.MustParseAddr("192.168.2.1")
240256
fakeSubnet1CIDR := netip.MustParsePrefix("192.168.0.0/16")
241257
fakeSubnet2CIDR := netip.MustParsePrefix("192.170.0.0/16")
@@ -245,9 +261,13 @@ func getFakes(t *testing.T, withCIDR bool) (netip.Addr, RoutingInfo) {
245261

246262
var fakeRoutingInfo *RoutingInfo
247263
if withCIDR {
264+
cidrs := []string{fakeSubnet1CIDR.String(), fakeSubnet2CIDR.String()}
265+
if withZeroCIDR {
266+
cidrs = []string{"0.0.0.0/0"}
267+
}
248268
fakeRoutingInfo, err = parse(
249269
fakeGateway.String(),
250-
[]string{fakeSubnet1CIDR.String(), fakeSubnet2CIDR.String()},
270+
cidrs,
251271
fakeMAC.String(),
252272
"1",
253273
ipamOption.IPAMENI,

0 commit comments

Comments
 (0)