Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use unix.RT_TABLE_MAIN for main routing table number #269

Merged
merged 1 commit into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions pkg/networkutils/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const (
// 1025 - 1535 can be used priority lower than fromPodRulePriority but higher than default nonVPC CIDR rule
fromPodRulePriority = 1536

mainRoutingTable = 254
mainRoutingTable = unix.RT_TABLE_MAIN

// This environment is used to specify whether an external NAT gateway will be used to provide SNAT of
// secondary ENI IP addresses. If set to "true", the SNAT iptables rule and off-VPC ip rule will not
Expand Down Expand Up @@ -218,7 +218,6 @@ func (n *linuxNetwork) SetupHostNetwork(vpcCIDR *net.IPNet, vpcCIDRs []*string,
// reversed so, to the routing table, it looks like the traffic is pod traffic instead of NodePort traffic.
mainENIRule := n.netLink.NewRule()
mainENIRule.Mark = int(n.mainENIMark)
mainENIRule.Mask = int(n.mainENIMark)
mainENIRule.Table = mainRoutingTable
mainENIRule.Priority = hostRulePriority
// If this is a restart, cleanup previous rule first
Expand Down Expand Up @@ -258,7 +257,7 @@ func (n *linuxNetwork) SetupHostNetwork(vpcCIDR *net.IPNet, vpcCIDRs []*string,
log.Debugf("Setup Host Network: iptables -N %s -t nat", lastChainName)

if err = ipt.NewChain("nat", lastChainName); err != nil && !containChainExistErr(err) {
log.Errorf("TODO: ipt.NewChain chain [%s] error %v", lastChainName, err)
log.Errorf("Setup Host Network: ipt.NewChain chain [%s] error %v", lastChainName, err)
return errors.Wrapf(err, "host network setup: failed to add chain")
}

Expand Down Expand Up @@ -670,7 +669,7 @@ func (n *linuxNetwork) GetRuleListBySrc(ruleList []netlink.Rule, src net.IPNet)
// DeleteRuleListBySrc deletes IP rules who has matcing source IP
func (n *linuxNetwork) DeleteRuleListBySrc(src net.IPNet) error {

log.Infof("Delete Rule List By Src [%v", src)
log.Infof("Delete Rule List By Src [%v]", src)

ruleList, err := n.GetRuleList()
if err != nil {
Expand Down Expand Up @@ -709,7 +708,6 @@ func (n *linuxNetwork) UpdateRuleListBySrc(ruleList []netlink.Rule, src net.IPNe
log.Infof("Update Rule List[%v] for source[%v] with toCIDRs[%v], toFlag[%v]", ruleList, src, toCIDRs, toFlag)

srcRuleList, err := n.GetRuleListBySrc(ruleList, src)

if err != nil {
log.Errorf("UpdateRuleListBySrc: failed to retrieve rule list %v", err)
return err
Expand Down Expand Up @@ -758,7 +756,6 @@ func (n *linuxNetwork) UpdateRuleListBySrc(ruleList []netlink.Rule, src net.IPNe
}

log.Infof("UpdateRuleListBySrc: Successfully added pod rule[%v] to %s", podRule, toDst)

}
} else {
podRule := n.netLink.NewRule()
Expand Down
72 changes: 72 additions & 0 deletions pkg/networkutils/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,78 @@ func TestSetupHostNetworkNodePortDisabled(t *testing.T) {

}

func TestUpdateRuleListBySrc(t *testing.T) {
ctrl, mockNetLink, _, _, _ := setup(t)
defer ctrl.Finish()

ln := &linuxNetwork{netLink: mockNetLink}

origRule := netlink.Rule{
Src: testENINetIPNet,
Table: testTable,
}
testCases := []struct {
name string
oldRule netlink.Rule
toFlag bool
toCIDRs []string
ruleList []netlink.Rule
newRules []netlink.Rule
expDst []*net.IPNet
expTable []int
}{
{
"multiple desitinations",
origRule,
true,
[]string{"10.10.0.0/16", "10.11.0.0/16"},
[]netlink.Rule{origRule},
make([]netlink.Rule, 2),
make([]*net.IPNet, 2),
[]int{origRule.Table, origRule.Table},
},
{
"single desitination",
origRule,
false,
[]string{""},
[]netlink.Rule{origRule},
make([]netlink.Rule, 1),
make([]*net.IPNet, 1),
[]int{origRule.Table},
},
}

for _, tc := range testCases {
var newRuleSize int
if tc.toFlag {
newRuleSize = len(tc.toCIDRs)
} else {
newRuleSize = 1
}

for i := 0; i < newRuleSize; i += 1 {
_, tc.expDst[i], _ = net.ParseCIDR(tc.toCIDRs[i])
}

mockNetLink.EXPECT().RuleDel(&tc.oldRule)

for i := 0; i < newRuleSize; i += 1 {
mockNetLink.EXPECT().NewRule().Return(&tc.newRules[i])
mockNetLink.EXPECT().RuleAdd(&tc.newRules[i])
}

err := ln.UpdateRuleListBySrc(tc.ruleList, *testENINetIPNet, tc.toCIDRs, tc.toFlag)
assert.NoError(t, err)

for i := 0; i < newRuleSize; i += 1 {
assert.Equal(t, tc.oldRule.Src, tc.newRules[i].Src, tc.name)
assert.Equal(t, tc.expDst[i], tc.newRules[i].Dst, tc.name)
assert.Equal(t, tc.expTable[i], tc.newRules[i].Table, tc.name)
}
}
}

func TestSetupHostNetworkNodePortEnabled(t *testing.T) {
ctrl, mockNetLink, _, mockNS, mockIptables := setup(t)
defer ctrl.Finish()
Expand Down