Skip to content

Commit

Permalink
Use primary interface to add iptables for connmark entry
Browse files Browse the repository at this point in the history
Currently iptables for CONNMARK entry always uses `eth0`, even though
AWS EC2 instance does not always use eth0.

This fix uses primary interface name which was discovered dynamically.
  • Loading branch information
nak3 committed Jan 30, 2019
1 parent 2cb11cb commit 6be0029
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 3 additions & 2 deletions pkg/networkutils/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,10 @@ func (n *linuxNetwork) SetupHostNetwork(vpcCIDR *net.IPNet, vpcCIDRs []*string,
return errors.Wrapf(err, "host network setup: failed to delete old host rule")
}

primaryIntf := "eth0"
if n.nodePortSupportEnabled {

primaryIntf, err := findPrimaryInterfaceName(primaryMAC)
primaryIntf, err = findPrimaryInterfaceName(primaryMAC)

if err != nil {
return errors.Wrapf(err, "failed to SetupHostNetwork")
Expand Down Expand Up @@ -350,7 +351,7 @@ func (n *linuxNetwork) SetupHostNetwork(vpcCIDR *net.IPNet, vpcCIDRs []*string,
chain: "PREROUTING",
rule: []string{
"-m", "comment", "--comment", "AWS, primary ENI",
"-i", "eth0",
"-i", primaryIntf,
"-m", "addrtype", "--dst-type", "LOCAL", "--limit-iface-in",
"-j", "CONNMARK", "--set-mark", fmt.Sprintf("%#x/%#x", n.mainENIMark, n.mainENIMark),
},
Expand Down
9 changes: 7 additions & 2 deletions pkg/networkutils/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,20 @@ func TestSetupHostNetworkNodePortEnabled(t *testing.T) {
mockNetLink.EXPECT().RuleAdd(&mainENIRule)

var vpcCIDRs []*string
err := ln.SetupHostNetwork(testENINetIPNet, vpcCIDRs, "", &testENINetIP)

// loopback for primary device is a little bit hacky. But the test is stable and it should be
// OK for test purpose.
LoopBackMac := ""

err := ln.SetupHostNetwork(testENINetIPNet, vpcCIDRs, LoopBackMac, &testENINetIP)
assert.NoError(t, err)

assert.Equal(t, map[string]map[string][][]string{
"mangle": {
"PREROUTING": [][]string{
{
"-m", "comment", "--comment", "AWS, primary ENI",
"-i", "eth0",
"-i", "lo",
"-m", "addrtype", "--dst-type", "LOCAL", "--limit-iface-in",
"-j", "CONNMARK", "--set-mark", "0x80/0x80",
},
Expand Down

0 comments on commit 6be0029

Please sign in to comment.