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

egressgw: improve reconciliation for IP rules #26736

Merged
merged 2 commits into from
Jul 17, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 45 additions & 27 deletions pkg/egressgateway/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,45 +541,63 @@ func (manager *Manager) addMissingIpRulesAndRoutes(isRetry bool) (shouldRetry bo
return false
}

addIPRulesForConfig := func(endpointIP net.IP, dstCIDR *net.IPNet, gwc *gatewayConfig) {
logger := log.WithFields(logrus.Fields{
logfields.SourceIP: endpointIP,
logfields.DestinationCIDR: dstCIDR.String(),
logfields.EgressIP: gwc.egressIP.IP,
logfields.LinkIndex: gwc.ifaceIndex,
})
for _, policyConfig := range manager.policyConfigs {
gwc := &policyConfig.gatewayConfig

if err := addEgressIpRule(endpointIP, dstCIDR, gwc.egressIP.IP, gwc.ifaceIndex); err != nil {
if isRetry {
logger.WithError(err).Warn("Can't add IP rule")
} else {
logger.WithError(err).Debug("Can't add IP rule, will retry")
shouldRetry = true
}
} else {
logger.Debug("Added IP rule")
if !gwc.localNodeConfiguredAsGateway || len(policyConfig.matchedEndpoints) == 0 {
continue
}
}

for _, policyConfig := range manager.policyConfigs {
gwc := &policyConfig.gatewayConfig
logger := log.WithFields(logrus.Fields{
logfields.EgressIP: gwc.egressIP.IP,
logfields.LinkIndex: gwc.ifaceIndex,
})

if gwc.localNodeConfiguredAsGateway &&
len(policyConfig.matchedEndpoints) > 0 {
routingTableIdx := egressGatewayRoutingTableIdx(gwc.ifaceIndex)

policyConfig.forEachEndpointAndDestination(addIPRulesForConfig)
ipRules, err := listEgressIpRulesForRoutingTable(routingTableIdx)
if err != nil {
logger.WithError(err).Warn("Can't fetch IP rules")
continue
}

addIPRulesForConfig := func(endpointIP net.IP, dstCIDR *net.IPNet, gwc *gatewayConfig) {
logger := log.WithFields(logrus.Fields{
logfields.EgressIP: gwc.egressIP.IP,
logfields.LinkIndex: gwc.ifaceIndex,
logfields.SourceIP: endpointIP,
logfields.DestinationCIDR: dstCIDR.String(),
logfields.EgressIP: gwc.egressIP.IP,
logfields.LinkIndex: gwc.ifaceIndex,
})

if err := addEgressIpRoutes(gwc.egressIP, gwc.ifaceIndex); err != nil {
logger.WithError(err).Warn("Can't add IP routes")
// check if the corresponding IP rule already exists
for _, ipRule := range ipRules {
if egressIPRuleMatches(&ipRule, endpointIP, dstCIDR) {
return
}
}

// insert the missing rule
newRule := newEgressIpRule(endpointIP, dstCIDR, routingTableIdx)

if err := netlink.RuleAdd(newRule); err != nil {
if isRetry {
logger.WithError(err).Warn("Can't add IP rule")
} else {
logger.WithError(err).Debug("Can't add IP rule, will retry")
shouldRetry = true
}
} else {
logger.Debug("Added IP routes")
logger.Debug("Added IP rule")
}
}

policyConfig.forEachEndpointAndDestination(addIPRulesForConfig)

if err := addEgressIpRoutes(gwc.egressIP, gwc.ifaceIndex); err != nil {
logger.WithError(err).Warn("Can't add IP routes")
} else {
logger.Debug("Added IP routes")
}
}

return
Expand Down
46 changes: 34 additions & 12 deletions pkg/egressgateway/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,20 @@ func listEgressIpRules() ([]netlink.Rule, error) {
Priority: linux_defaults.RulePriorityEgressGateway,
}

rules, err := route.ListRules(netlink.FAMILY_V4, &filter)
return listFilteredEgressIpRules(&filter)
}

func listEgressIpRulesForRoutingTable(RoutingTableIdx int) ([]netlink.Rule, error) {
filter := route.Rule{
Priority: linux_defaults.RulePriorityEgressGateway,
Table: RoutingTableIdx,
}

return listFilteredEgressIpRules(&filter)
}

func listFilteredEgressIpRules(filter *route.Rule) ([]netlink.Rule, error) {
rules, err := route.ListRules(netlink.FAMILY_V4, filter)
if err != nil {
return nil, err
}
Expand All @@ -90,18 +103,27 @@ func listEgressIpRules() ([]netlink.Rule, error) {
return rules, nil
}

func addEgressIpRule(endpointIP net.IP, dstCIDR *net.IPNet, egressIP net.IP, ifaceIndex int) error {
routingTableIdx := egressGatewayRoutingTableIdx(ifaceIndex)
func newEgressIpRule(endpointIP net.IP, dstCIDR *net.IPNet, routingTableIdx int) *netlink.Rule {
rule := netlink.NewRule()

ipRule := route.Rule{
Priority: linux_defaults.RulePriorityEgressGateway,
From: &net.IPNet{IP: endpointIP, Mask: net.CIDRMask(32, 32)},
To: dstCIDR,
Table: routingTableIdx,
Protocol: linux_defaults.RTProto,
}
rule.Family = netlink.FAMILY_V4
rule.Table = routingTableIdx
rule.Priority = linux_defaults.RulePriorityEgressGateway
rule.Src = &net.IPNet{IP: endpointIP, Mask: net.CIDRMask(32, 32)}
rule.Dst = dstCIDR
rule.Protocol = linux_defaults.RTProto

return route.ReplaceRule(ipRule)
return rule
}

func egressIPRuleMatches(ipRule *netlink.Rule, endpointIP net.IP, dstCIDR *net.IPNet) bool {
ipRuleMaskSize, _ := ipRule.Dst.Mask.Size()
dstCIDRMaskSize, _ := dstCIDR.Mask.Size()

// We already filtered for .Family, .Priority and .Table
return ipRule.Src.IP.Equal(endpointIP) &&
ipRuleMaskSize == dstCIDRMaskSize &&
ipRule.Dst.IP.Equal(dstCIDR.IP)
}

func getFirstIPInHostRange(ip net.IPNet) net.IP {
Expand Down Expand Up @@ -139,7 +161,7 @@ func addEgressIpRoutes(egressIP net.IPNet, ifaceIndex int) error {
Gw: eniGatewayIP,
Protocol: linux_defaults.RTProto,
}); err != nil {
return fmt.Errorf("unable to add L2 nexthop route: %w", err)
return fmt.Errorf("unable to add default route: %w", err)
}

return nil
Expand Down