From 268446bb4710d2d91637639203fc8b948db06e10 Mon Sep 17 00:00:00 2001 From: Ilya Dmitrichenko Date: Thu, 2 Apr 2020 11:45:38 +0100 Subject: [PATCH] pkg/datapath/linux/route: ensure the package compiles on macOS This package already has `route_darwin.go` and `route_linux.go`, but some functions in `route.go` did not respected this. Signed-off-by: Ilya Dmitrichenko --- pkg/datapath/linux/route/route.go | 53 ----------------------- pkg/datapath/linux/route/route_darwin.go | 20 ++++++--- pkg/datapath/linux/route/route_linux.go | 54 ++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 58 deletions(-) diff --git a/pkg/datapath/linux/route/route.go b/pkg/datapath/linux/route/route.go index 794a6ca0404d..1e09fee2c059 100644 --- a/pkg/datapath/linux/route/route.go +++ b/pkg/datapath/linux/route/route.go @@ -19,7 +19,6 @@ import ( "net" "github.com/cilium/cilium/pkg/logging/logfields" - "github.com/cilium/cilium/pkg/option" "github.com/sirupsen/logrus" "github.com/vishvananda/netlink" @@ -80,55 +79,3 @@ func (r *Route) ToIPCommand(dev string) []string { res = append(res, "dev", dev) return res } - -func lookupDefaultRoute(family int) (netlink.Route, error) { - linkIndex := 0 - - routes, err := netlink.RouteListFiltered(family, &netlink.Route{Dst: nil}, netlink.RT_FILTER_DST) - if err != nil { - return netlink.Route{}, fmt.Errorf("Unable to list direct routes: %s", err) - } - - if len(routes) == 0 { - return netlink.Route{}, fmt.Errorf("Default route not found for family %d", family) - } - - for _, route := range routes { - if linkIndex != 0 && linkIndex != route.LinkIndex { - return netlink.Route{}, fmt.Errorf("Found default routes with different netdev ifindices: %v vs %v", - linkIndex, route.LinkIndex) - } - linkIndex = route.LinkIndex - } - - log.Debugf("Found default route on node %v", routes[0]) - return routes[0], nil -} - -// NodeDeviceWithDefaultRoute returns the node's device which handles the -// default route in the current namespace -func NodeDeviceWithDefaultRoute() (netlink.Link, error) { - linkIndex := 0 - if option.Config.EnableIPv4 { - route, err := lookupDefaultRoute(netlink.FAMILY_V4) - if err != nil { - return nil, err - } - linkIndex = route.LinkIndex - } - if option.Config.EnableIPv6 { - route, err := lookupDefaultRoute(netlink.FAMILY_V6) - if err != nil { - return nil, err - } - if linkIndex != 0 && linkIndex != route.LinkIndex { - return nil, fmt.Errorf("IPv4/IPv6 have different link indices") - } - linkIndex = route.LinkIndex - } - link, err := netlink.LinkByIndex(linkIndex) - if err != nil { - return nil, err - } - return link, nil -} diff --git a/pkg/datapath/linux/route/route_darwin.go b/pkg/datapath/linux/route/route_darwin.go index 5b28e0ab9669..92c2da59e0b5 100644 --- a/pkg/datapath/linux/route/route_darwin.go +++ b/pkg/datapath/linux/route/route_darwin.go @@ -20,15 +20,25 @@ import ( "fmt" "github.com/cilium/cilium/pkg/mtu" + + "github.com/vishvananda/netlink" ) -// Replace adds or replaces the specified route if necessary. Does nothing -// for Darwin-based builds. +// errUnsupportedOp is a common error +var errUnsupportedOp = fmt.Errorf("Route operations not supported on Darwin") + +// Replace is not supported on Darwin and will return an error at runtime. func Replace(route Route, mtuConfig mtu.Configuration) error { - return fmt.Errorf("Operation not supported ") + return errUnsupportedOp } -// Delete removes a route. Does nothing for Darwin-based builds. +// Delete is not supported on Darwin and will return an error at runtime. func Delete(route Route) error { - return fmt.Errorf("Operation not supported ") + return errUnsupportedOp +} + +// NodeDeviceWithDefaultRoute is not supported on Darwin and will return +// an error at runtime. +func NodeDeviceWithDefaultRoute() (netlink.Link, error) { + return nil, errUnsupportedOp } diff --git a/pkg/datapath/linux/route/route_linux.go b/pkg/datapath/linux/route/route_linux.go index ddaa4b7f6b39..a59729d8dcfe 100644 --- a/pkg/datapath/linux/route/route_linux.go +++ b/pkg/datapath/linux/route/route_linux.go @@ -21,6 +21,8 @@ import ( "net" "time" + "github.com/cilium/cilium/pkg/option" + "github.com/vishvananda/netlink" ) @@ -409,3 +411,55 @@ func deleteRule(spec Rule, family int) error { rule.Family = family return netlink.RuleDel(rule) } + +func lookupDefaultRoute(family int) (netlink.Route, error) { + linkIndex := 0 + + routes, err := netlink.RouteListFiltered(family, &netlink.Route{Dst: nil}, netlink.RT_FILTER_DST) + if err != nil { + return netlink.Route{}, fmt.Errorf("Unable to list direct routes: %s", err) + } + + if len(routes) == 0 { + return netlink.Route{}, fmt.Errorf("Default route not found for family %d", family) + } + + for _, route := range routes { + if linkIndex != 0 && linkIndex != route.LinkIndex { + return netlink.Route{}, fmt.Errorf("Found default routes with different netdev ifindices: %v vs %v", + linkIndex, route.LinkIndex) + } + linkIndex = route.LinkIndex + } + + log.Debugf("Found default route on node %v", routes[0]) + return routes[0], nil +} + +// NodeDeviceWithDefaultRoute returns the node's device which handles the +// default route in the current namespace +func NodeDeviceWithDefaultRoute() (netlink.Link, error) { + linkIndex := 0 + if option.Config.EnableIPv4 { + route, err := lookupDefaultRoute(netlink.FAMILY_V4) + if err != nil { + return nil, err + } + linkIndex = route.LinkIndex + } + if option.Config.EnableIPv6 { + route, err := lookupDefaultRoute(netlink.FAMILY_V6) + if err != nil { + return nil, err + } + if linkIndex != 0 && linkIndex != route.LinkIndex { + return nil, fmt.Errorf("IPv4/IPv6 have different link indices") + } + linkIndex = route.LinkIndex + } + link, err := netlink.LinkByIndex(linkIndex) + if err != nil { + return nil, err + } + return link, nil +}