1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #3640 from proppy/networkgetroutes-add-default

netlink: add default Route to NetworkGetRoutes
This commit is contained in:
Victor Vieux 2014-01-21 15:20:16 -08:00
commit be6aeda1e4
5 changed files with 31 additions and 16 deletions

View file

@ -71,9 +71,9 @@ func networkSize(mask net.IPMask) int32 {
return int32(binary.BigEndian.Uint32(m)) + 1 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 { for _, network := range networks {
if networkOverlaps(dockerNetwork, network) { if network.IPNet != nil && networkOverlaps(dockerNetwork, network.IPNet) {
return fmt.Errorf("Network %s is already routed: '%s'", dockerNetwork, network) return fmt.Errorf("Network %s is already routed: '%s'", dockerNetwork, network)
} }
} }

View file

@ -2,7 +2,9 @@ package docker
import ( import (
"github.com/dotcloud/docker/pkg/iptables" "github.com/dotcloud/docker/pkg/iptables"
"github.com/dotcloud/docker/pkg/netlink"
"github.com/dotcloud/docker/proxy" "github.com/dotcloud/docker/proxy"
"net" "net"
"testing" "testing"
) )
@ -289,10 +291,10 @@ func TestNetworkOverlaps(t *testing.T) {
func TestCheckRouteOverlaps(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"} 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 { for _, addr := range routesData {
_, netX, _ := net.ParseCIDR(addr) _, netX, _ := net.ParseCIDR(addr)
routes = append(routes, netX) routes = append(routes, netlink.Route{IPNet: netX})
} }
_, netX, _ := net.ParseCIDR("172.16.0.1/24") _, netX, _ := net.ParseCIDR("172.16.0.1/24")

15
pkg/netlink/netlink.go Normal file
View file

@ -0,0 +1,15 @@
// Packet netlink provide access to low level Netlink sockets and messages.
//
// Actual implementations are in:
// netlink_linux.go
// netlink_darwin.go
package netlink
import "net"
// A Route is a subnet associated with the interface to reach it.
type Route struct {
*net.IPNet
Iface *net.Interface
Default bool
}

View file

@ -5,7 +5,7 @@ import (
"net" "net"
) )
func NetworkGetRoutes() ([]*net.IPNet, error) { func NetworkGetRoutes() ([]Route, error) {
return nil, fmt.Errorf("Not implemented") return nil, fmt.Errorf("Not implemented")
} }

View file

@ -473,7 +473,7 @@ func NetworkLinkAdd(name string, linkType string) error {
// Returns an array of IPNet for all the currently routed subnets on ipv4 // Returns an array of IPNet for all the currently routed subnets on ipv4
// This is similar to the first column of "ip route" output // This is similar to the first column of "ip route" output
func NetworkGetRoutes() ([]*net.IPNet, error) { func NetworkGetRoutes() ([]Route, error) {
native := nativeEndian() native := nativeEndian()
s, err := getNetlinkSocket() s, err := getNetlinkSocket()
@ -496,7 +496,7 @@ func NetworkGetRoutes() ([]*net.IPNet, error) {
return nil, err return nil, err
} }
res := make([]*net.IPNet, 0) res := make([]Route, 0)
done: done:
for { for {
@ -525,8 +525,7 @@ done:
continue continue
} }
var iface *net.Interface = nil var r Route
var ipNet *net.IPNet = nil
msg := (*RtMsg)(unsafe.Pointer(&m.Data[0:syscall.SizeofRtMsg][0])) msg := (*RtMsg)(unsafe.Pointer(&m.Data[0:syscall.SizeofRtMsg][0]))
@ -546,8 +545,8 @@ done:
} }
if msg.Dst_len == 0 { if msg.Dst_len == 0 {
// Ignore default routes // Default routes
continue r.Default = true
} }
attrs, err := syscall.ParseNetlinkRouteAttr(&m) attrs, err := syscall.ParseNetlinkRouteAttr(&m)
@ -558,18 +557,17 @@ done:
switch attr.Attr.Type { switch attr.Attr.Type {
case syscall.RTA_DST: case syscall.RTA_DST:
ip := attr.Value ip := attr.Value
ipNet = &net.IPNet{ r.IPNet = &net.IPNet{
IP: ip, IP: ip,
Mask: net.CIDRMask(int(msg.Dst_len), 8*len(ip)), Mask: net.CIDRMask(int(msg.Dst_len), 8*len(ip)),
} }
case syscall.RTA_OIF: case syscall.RTA_OIF:
index := int(native.Uint32(attr.Value[0:4])) index := int(native.Uint32(attr.Value[0:4]))
iface, _ = net.InterfaceByIndex(index) r.Iface, _ = net.InterfaceByIndex(index)
_ = iface
} }
} }
if ipNet != nil { if r.Default || r.IPNet != nil {
res = append(res, ipNet) res = append(res, r)
} }
} }
} }