From 25787126235087eda3f7e8deec4620acd54cf353 Mon Sep 17 00:00:00 2001 From: Vladimir Lavor Date: Wed, 15 Dec 2021 12:07:29 +0100 Subject: [PATCH] Make route scope relevant for IPv4 only Signed-off-by: Vladimir Lavor --- examples/localclient_linux/route/main.go | 5 +-- plugins/linux/l3plugin/descriptor/route.go | 37 ++++++++++++++-------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/examples/localclient_linux/route/main.go b/examples/localclient_linux/route/main.go index d2f8add484..bcce3291f1 100644 --- a/examples/localclient_linux/route/main.go +++ b/examples/localclient_linux/route/main.go @@ -178,7 +178,7 @@ func (p *RouteExamplePlugin) resync(ctx context.Context, timeout int) { LinuxInterface(linuxTap()). LinuxInterface(linuxTapIpv6()). LinuxRoute(routeResync()). // recreate - LinuxRoute(routeResyncIPv6()). // update + LinuxRoute(routeResyncIPv6()). // no action Send().ReceiveReply() if err != nil { p.Log.Errorf("Resync failed: %v", err) @@ -321,7 +321,6 @@ func routeIPv6() *linux_l3.Route { return &linux_l3.Route{ OutgoingInterface: "linux-tap2", DstNetwork: "aaa1::/64", - Scope: linux_l3.Route_GLOBAL, Metric: 0, } } @@ -330,7 +329,6 @@ func routeResyncIPv6() *linux_l3.Route { return &linux_l3.Route{ OutgoingInterface: "linux-tap2", DstNetwork: "aaa1::/64", - Scope: linux_l3.Route_LINK, Metric: 1024, } } @@ -339,7 +337,6 @@ func routeModifiedIPv6() *linux_l3.Route { return &linux_l3.Route{ OutgoingInterface: "linux-tap2", DstNetwork: "aaa1::/64", - Scope: linux_l3.Route_LINK, Metric: 500, } } diff --git a/plugins/linux/l3plugin/descriptor/route.go b/plugins/linux/l3plugin/descriptor/route.go index cb492f2dd3..88b7a1d551 100644 --- a/plugins/linux/l3plugin/descriptor/route.go +++ b/plugins/linux/l3plugin/descriptor/route.go @@ -122,10 +122,15 @@ func NewRouteDescriptor( // EquivalentRoutes is case-insensitive comparison function for l3.LinuxRoute. func (d *RouteDescriptor) EquivalentRoutes(key string, oldRoute, newRoute *linux_l3.Route) bool { // attributes compared as usually: - if oldRoute.OutgoingInterface != newRoute.OutgoingInterface || - oldRoute.Scope != newRoute.Scope { + if oldRoute.OutgoingInterface != newRoute.OutgoingInterface { return false } + // compare scopes for IPv4 routes + if ip := net.ParseIP(newRoute.DstNetwork); ip != nil && ip.To4() != nil { + if oldRoute.Scope != newRoute.Scope { + return false + } + } // compare metrics if !isRouteMetricEqual(oldRoute, newRoute) { return false @@ -230,13 +235,15 @@ func (d *RouteDescriptor) updateRoute(route *linux_l3.Route, actionName string, netlinkRoute.Gw = gwAddr.IP } - // set route scope - scope, err := rtScopeFromNBToNetlink(route.Scope) - if err != nil { - d.log.Error(err) - return err + // set route scope for IPv4 + if ip := net.ParseIP(route.DstNetwork); ip != nil && ip.To4() != nil { + scope, err := rtScopeFromNBToNetlink(route.Scope) + if err != nil { + d.log.Error(err) + return err + } + netlinkRoute.Scope = scope } - netlinkRoute.Scope = scope // set route metric netlinkRoute.Priority = int(route.Metric) @@ -397,11 +404,15 @@ func (d *RouteDescriptor) Retrieve(correlate []adapter.RouteKVWithMetadata) ([]a // correlate with the expected configuration for _, routeDetails := range routeDetails { - // Convert to key-value object with metadata - scope, err := rtScopeFromNetlinkToNB(routeDetails.Meta.NetlinkScope) - if err != nil { - // route not configured by the agent - continue + // convert to key-value object with metadata + // resolve scope for IPv6. Note that IPv6 route scope always returns zero value. + var scope linux_l3.Route_Scope + if ip := net.ParseIP(routeDetails.Route.DstNetwork); ip != nil && ip.To4() != nil { + scope, err = rtScopeFromNetlinkToNB(routeDetails.Meta.NetlinkScope) + if err != nil { + // route not configured by the agent + continue + } } route := adapter.RouteKVWithMetadata{ Key: linux_l3.RouteKey(routeDetails.Route.DstNetwork, routeDetails.Route.OutgoingInterface),