Skip to content

Commit

Permalink
pkg/datapath/linux/route: ensure the package compiles on macOS
Browse files Browse the repository at this point in the history
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 <errordeveloper@gmail.com>
  • Loading branch information
errordeveloper committed Apr 3, 2020
1 parent b9ef15f commit 268446b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 58 deletions.
53 changes: 0 additions & 53 deletions pkg/datapath/linux/route/route.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
20 changes: 15 additions & 5 deletions pkg/datapath/linux/route/route_darwin.go
Expand Up @@ -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
}
54 changes: 54 additions & 0 deletions pkg/datapath/linux/route/route_linux.go
Expand Up @@ -21,6 +21,8 @@ import (
"net"
"time"

"github.com/cilium/cilium/pkg/option"

"github.com/vishvananda/netlink"
)

Expand Down Expand Up @@ -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
}

0 comments on commit 268446b

Please sign in to comment.