From 55b3bfe8f66242ae0fc228c9e5048b72e4048a8c Mon Sep 17 00:00:00 2001 From: Michael Cambria Date: Thu, 11 Mar 2021 13:49:00 -0500 Subject: [PATCH] Allow multiple routes to be added for the same prefix. Enables ECMP Fixes: https://github.com/containernetworking/plugins/issues/601 Signed-off-by: Michael Cambria --- pkg/ipam/ipam_linux.go | 13 ++++++++----- plugins/main/bridge/bridge.go | 2 +- .../github.com/vishvananda/netlink/route_linux.go | 12 ++++++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/pkg/ipam/ipam_linux.go b/pkg/ipam/ipam_linux.go index bf16cb0ff..8c8b8e0b0 100644 --- a/pkg/ipam/ipam_linux.go +++ b/pkg/ipam/ipam_linux.go @@ -113,11 +113,14 @@ func ConfigureIface(ifName string, res *current.Result) error { gw = v6gw } } - if err = ip.AddRoute(&r.Dst, gw, link); err != nil { - // we skip over duplicate routes as we assume the first one wins - if !os.IsExist(err) { - return fmt.Errorf("failed to add route '%v via %v dev %v': %v", r.Dst, gw, ifName, err) - } + route := netlink.Route{ + Dst: &r.Dst, + LinkIndex: link.Attrs().Index, + Gw: gw, + } + + if err = netlink.RouteAddEcmp(&route); err != nil { + return fmt.Errorf("failed to add route '%v via %v dev %v': %v", r.Dst, gw, ifName, err) } } diff --git a/plugins/main/bridge/bridge.go b/plugins/main/bridge/bridge.go index 69ffa98f1..638b70922 100644 --- a/plugins/main/bridge/bridge.go +++ b/plugins/main/bridge/bridge.go @@ -122,7 +122,7 @@ func calcGateways(result *current.Result, n *NetConf) (*gwInfo, *gwInfo, error) // Add a default route for this family using the current // gateway address if necessary. - if n.IsDefaultGW && !gws.defaultRouteFound { + if n.IsDefaultGW { for _, route := range result.Routes { if route.GW != nil && defaultNet.String() == route.Dst.String() { gws.defaultRouteFound = true diff --git a/vendor/github.com/vishvananda/netlink/route_linux.go b/vendor/github.com/vishvananda/netlink/route_linux.go index 4e778a417..5dec39a38 100644 --- a/vendor/github.com/vishvananda/netlink/route_linux.go +++ b/vendor/github.com/vishvananda/netlink/route_linux.go @@ -466,6 +466,18 @@ func RouteReplace(route *Route) error { return pkgHandle.RouteReplace(route) } +// RouteAddEcmp will add a route to the system. +func RouteAddEcmp(route *Route) error { + return pkgHandle.RouteAddEcmp(route) +} + +// RouteAddEcmp will add a route to the system. +func (h *Handle) RouteAddEcmp(route *Route) error { + flags := unix.NLM_F_CREATE | unix.NLM_F_ACK + req := h.newNetlinkRequest(unix.RTM_NEWROUTE, flags) + return h.routeHandle(route, req, nl.NewRtMsg()) +} + // RouteReplace will add a route to the system. // Equivalent to: `ip route replace $route` func (h *Handle) RouteReplace(route *Route) error {