From 26726dc9ff3ac8ccc7f40f7672e6494d0e77611d Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Thu, 16 Jan 2014 23:41:29 -0800 Subject: [PATCH] netlink: add default Route to NetworkGetRoutes Docker-DCO-1.1-Signed-off-by: Johan Euphrosine (github: proppy) --- network.go | 4 ++-- network_test.go | 6 ++++-- pkg/netlink/netlink_linux.go | 27 ++++++++++++++++----------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/network.go b/network.go index 22ea8ba757..8f0170a710 100644 --- a/network.go +++ b/network.go @@ -71,9 +71,9 @@ func networkSize(mask net.IPMask) int32 { return int32(binary.BigEndian.Uint32(m)) + 1 } -func checkRouteOverlaps(networks []*net.IPNet, dockerNetwork *net.IPNet) error { +func checkRouteOverlaps(networks []netlink.Route, dockerNetwork *net.IPNet) error { for _, network := range networks { - if networkOverlaps(dockerNetwork, network) { + if networkOverlaps(dockerNetwork, network.IPNet) { return fmt.Errorf("Network %s is already routed: '%s'", dockerNetwork, network) } } diff --git a/network_test.go b/network_test.go index 69fcba01a2..0b6857ba76 100644 --- a/network_test.go +++ b/network_test.go @@ -2,7 +2,9 @@ package docker import ( "github.com/dotcloud/docker/pkg/iptables" + "github.com/dotcloud/docker/pkg/netlink" "github.com/dotcloud/docker/proxy" + "net" "testing" ) @@ -289,10 +291,10 @@ func TestNetworkOverlaps(t *testing.T) { func TestCheckRouteOverlaps(t *testing.T) { routesData := []string{"10.0.2.0/32", "10.0.3.0/24", "10.0.42.0/24", "172.16.42.0/24", "192.168.142.0/24"} - routes := []*net.IPNet{} + routes := []netlink.Route{} for _, addr := range routesData { _, netX, _ := net.ParseCIDR(addr) - routes = append(routes, netX) + routes = append(routes, netlink.Route{IPNet: netX}) } _, netX, _ := net.ParseCIDR("172.16.0.1/24") diff --git a/pkg/netlink/netlink_linux.go b/pkg/netlink/netlink_linux.go index 9a937d1218..a65bdaf5e9 100644 --- a/pkg/netlink/netlink_linux.go +++ b/pkg/netlink/netlink_linux.go @@ -471,9 +471,16 @@ func NetworkLinkAdd(name string, linkType string) error { return s.HandleAck(wb.Seq) } +// A Route is a subnet associated with the interface to reach it. +type Route struct { + *net.IPNet + Iface *net.Interface + Default bool +} + // Returns an array of IPNet for all the currently routed subnets on ipv4 // This is similar to the first column of "ip route" output -func NetworkGetRoutes() ([]*net.IPNet, error) { +func NetworkGetRoutes() ([]Route, error) { native := nativeEndian() s, err := getNetlinkSocket() @@ -496,7 +503,7 @@ func NetworkGetRoutes() ([]*net.IPNet, error) { return nil, err } - res := make([]*net.IPNet, 0) + res := make([]Route, 0) done: for { @@ -525,8 +532,7 @@ done: continue } - var iface *net.Interface = nil - var ipNet *net.IPNet = nil + var r Route msg := (*RtMsg)(unsafe.Pointer(&m.Data[0:syscall.SizeofRtMsg][0])) @@ -546,8 +552,8 @@ done: } if msg.Dst_len == 0 { - // Ignore default routes - continue + // Default routes + r.Default = true } attrs, err := syscall.ParseNetlinkRouteAttr(&m) @@ -558,18 +564,17 @@ done: switch attr.Attr.Type { case syscall.RTA_DST: ip := attr.Value - ipNet = &net.IPNet{ + r.IPNet = &net.IPNet{ IP: ip, Mask: net.CIDRMask(int(msg.Dst_len), 8*len(ip)), } case syscall.RTA_OIF: index := int(native.Uint32(attr.Value[0:4])) - iface, _ = net.InterfaceByIndex(index) - _ = iface + r.Iface, _ = net.InterfaceByIndex(index) } } - if ipNet != nil { - res = append(res, ipNet) + if r.Default || r.IPNet != nil { + res = append(res, r) } } }