Skip to content

Commit

Permalink
bypass netlink.LinkByIndex
Browse files Browse the repository at this point in the history
since we are seeing some cases where it returns "Link not found" when
the link is in fact there.
  • Loading branch information
rade committed Jan 19, 2016
1 parent b5256f7 commit 4341981
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions net/route.go
Expand Up @@ -10,7 +10,7 @@ import (
// A network is considered free if it does not overlap any existing
// routes on this host. This is the same approach taken by Docker.
func CheckNetworkFree(subnet *net.IPNet, ignoreIfaceNames map[string]struct{}) error {
return forEachRoute(ignoreIfaceNames, func(name string, route netlink.Route) error {
return forEachRoute(ignoreIfaceNames, func(route netlink.Route) error {
if route.Dst != nil && overlaps(route.Dst, subnet) {
return fmt.Errorf("Network %s overlaps with existing route %s on host.", subnet, route.Dst)
}
Expand All @@ -26,29 +26,30 @@ func overlaps(n1, n2 *net.IPNet) bool {
// For a specific address, we only care if it is actually *inside* an
// existing route, because weave-local traffic never hits IP routing.
func CheckAddressOverlap(addr net.IP, ignoreIfaceNames map[string]struct{}) error {
return forEachRoute(ignoreIfaceNames, func(name string, route netlink.Route) error {
return forEachRoute(ignoreIfaceNames, func(route netlink.Route) error {
if route.Dst != nil && route.Dst.Contains(addr) {
return fmt.Errorf("Address %s overlaps with existing route %s on host.", addr, route.Dst)
}
return nil
})
}

func forEachRoute(ignoreIfaceNames map[string]struct{}, check func(name string, r netlink.Route) error) error {
func forEachRoute(ignoreIfaceNames map[string]struct{}, check func(r netlink.Route) error) error {
ignoreIfaceIndices := make(map[int]struct{})
for ifaceName := range ignoreIfaceNames {
if iface, err := net.InterfaceByName(ifaceName); err == nil {
ignoreIfaceIndices[iface.Index] = struct{}{}
}
}
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
if err != nil {
return err
}
for _, route := range routes {
link, err := netlink.LinkByIndex(route.LinkIndex)
if err != nil {
continue
}
ifaceName := link.Attrs().Name
if _, found := ignoreIfaceNames[ifaceName]; found {
if _, found := ignoreIfaceIndices[route.LinkIndex]; found {
continue
}
if err := check(ifaceName, route); err != nil {
if err := check(route); err != nil {
return err
}
}
Expand Down

0 comments on commit 4341981

Please sign in to comment.