From 46864de1d7da8f8592dbb2cc6ae454cb036ac4ec Mon Sep 17 00:00:00 2001 From: Arnaud Porterie Date: Sun, 22 Feb 2015 21:32:48 -0800 Subject: [PATCH] WIP - Fixed CIDR v4 and v6 Signed-off-by: Arnaud Porterie --- libnetwork/bridge/bridge.go | 8 ++++---- libnetwork/bridge/interface.go | 16 ++++++++++++++++ libnetwork/bridge/setup.go | 8 -------- libnetwork/bridge/setup_fixedcidrv4.go | 16 ++++++++++++++++ libnetwork/bridge/setup_fixedcidrv6.go | 11 +++++++++++ libnetwork/bridge/setup_ipv4.go | 3 +-- libnetwork/bridge/setup_verify.go | 24 ++---------------------- 7 files changed, 50 insertions(+), 36 deletions(-) create mode 100644 libnetwork/bridge/setup_fixedcidrv4.go create mode 100644 libnetwork/bridge/setup_fixedcidrv6.go diff --git a/libnetwork/bridge/bridge.go b/libnetwork/bridge/bridge.go index a7c5215b76..ab3e2bd202 100644 --- a/libnetwork/bridge/bridge.go +++ b/libnetwork/bridge/bridge.go @@ -14,8 +14,8 @@ const ( type Configuration struct { BridgeName string AddressIPv4 *net.IPNet - FixedCIDR string - FixedCIDRv6 string + FixedCIDR *net.IPNet + FixedCIDRv6 *net.IPNet EnableIPv6 bool EnableIPTables bool EnableIPForwarding bool @@ -75,11 +75,11 @@ func Create(config *Configuration) (libnetwork.Network, error) { // Setup the bridge to allocate containers IPv4 addresses in the // specified subnet. - {config.FixedCIDR != "", SetupFixedCIDRv4}, + {config.FixedCIDR != nil, SetupFixedCIDRv4}, // Setup the bridge to allocate containers global IPv6 addresses in the // specified subnet. - {config.FixedCIDRv6 != "", SetupFixedCIDRv6}, + {config.FixedCIDRv6 != nil, SetupFixedCIDRv6}, // Setup IPTables. {config.EnableIPTables, SetupIPTables}, diff --git a/libnetwork/bridge/interface.go b/libnetwork/bridge/interface.go index cd336367ba..414f42ea9f 100644 --- a/libnetwork/bridge/interface.go +++ b/libnetwork/bridge/interface.go @@ -30,3 +30,19 @@ func NewInterface(config *Configuration) *Interface { func (i *Interface) Exists() bool { return i.Link != nil } + +// Addresses returns a single IPv4 address and all IPv6 addresses for the +// bridge interface. +func (i *Interface) Addresses() (netlink.Addr, []netlink.Addr, error) { + v4addr, err := netlink.AddrList(i.Link, netlink.FAMILY_V4) + if err != nil { + return netlink.Addr{}, nil, err + } + + v6addr, err := netlink.AddrList(i.Link, netlink.FAMILY_V6) + if err != nil { + return netlink.Addr{}, nil, err + } + + return v4addr[0], v6addr, nil +} diff --git a/libnetwork/bridge/setup.go b/libnetwork/bridge/setup.go index 2d17f7a7f2..c0cbbe98ae 100644 --- a/libnetwork/bridge/setup.go +++ b/libnetwork/bridge/setup.go @@ -26,14 +26,6 @@ func (b *BridgeSetup) QueueStep(step SetupStep) { //---------------------------------------------------------------------------// -func SetupFixedCIDRv4(i *Interface) error { - return nil -} - -func SetupFixedCIDRv6(i *Interface) error { - return nil -} - func SetupIPTables(i *Interface) error { return nil } diff --git a/libnetwork/bridge/setup_fixedcidrv4.go b/libnetwork/bridge/setup_fixedcidrv4.go new file mode 100644 index 0000000000..bf5dc2701a --- /dev/null +++ b/libnetwork/bridge/setup_fixedcidrv4.go @@ -0,0 +1,16 @@ +package bridge + +import ( + log "github.com/Sirupsen/logrus" + "github.com/docker/docker/daemon/networkdriver/ipallocator" +) + +func SetupFixedCIDRv4(i *Interface) error { + addrv4, _, err := i.Addresses() + if err != nil { + return err + } + + log.Debugf("Using IPv4 subnet: %v", i.Config.FixedCIDR) + return ipallocator.RegisterSubnet(addrv4.IPNet, i.Config.FixedCIDR) +} diff --git a/libnetwork/bridge/setup_fixedcidrv6.go b/libnetwork/bridge/setup_fixedcidrv6.go new file mode 100644 index 0000000000..bb15cf5880 --- /dev/null +++ b/libnetwork/bridge/setup_fixedcidrv6.go @@ -0,0 +1,11 @@ +package bridge + +import ( + log "github.com/Sirupsen/logrus" + "github.com/docker/docker/daemon/networkdriver/ipallocator" +) + +func SetupFixedCIDRv6(i *Interface) error { + log.Debugf("Using IPv6 subnet: %v", i.Config.FixedCIDRv6) + return ipallocator.RegisterSubnet(i.Config.FixedCIDRv6, i.Config.FixedCIDRv6) +} diff --git a/libnetwork/bridge/setup_ipv4.go b/libnetwork/bridge/setup_ipv4.go index 974e7e2032..8068eea748 100644 --- a/libnetwork/bridge/setup_ipv4.go +++ b/libnetwork/bridge/setup_ipv4.go @@ -52,7 +52,7 @@ func SetupBridgeIPv4(i *Interface) error { } func electBridgeIPv4(config *Configuration) (*net.IPNet, error) { - // Use the requested IPv4 IP and mark when available. + // Use the requested IPv4 CIDR when available. if config.AddressIPv4 != nil { return config.AddressIPv4, nil } @@ -128,7 +128,6 @@ func networkRange(network *net.IPNet) (net.IP, net.IP) { } lastIP := make([]byte, len(netIP), len(netIP)) - for i := 0; i < len(netIP); i++ { lastIP[i] = netIP[i] | ^network.Mask[i] } diff --git a/libnetwork/bridge/setup_verify.go b/libnetwork/bridge/setup_verify.go index b825592ee5..7baf8cf3e2 100644 --- a/libnetwork/bridge/setup_verify.go +++ b/libnetwork/bridge/setup_verify.go @@ -1,14 +1,10 @@ package bridge -import ( - "fmt" - - "github.com/vishvananda/netlink" -) +import "fmt" func SetupVerifyConfiguredAddresses(i *Interface) error { // Fetch a single IPv4 and a slice of IPv6 addresses from the bridge. - addrv4, addrsv6, err := getInterfaceAddresses(i.Link) + addrv4, addrsv6, err := i.Addresses() if err != nil { return err } @@ -28,19 +24,3 @@ func SetupVerifyConfiguredAddresses(i *Interface) error { return fmt.Errorf("Bridge IPv6 addresses do not match the expected bridge configuration %s", BridgeIPv6) } - -func getInterfaceAddresses(iface netlink.Link) (netlink.Addr, []netlink.Addr, error) { - v4addr, err := netlink.AddrList(iface, netlink.FAMILY_V4) - if err != nil { - return netlink.Addr{}, nil, err - } - - v6addr, err := netlink.AddrList(iface, netlink.FAMILY_V6) - if err != nil { - return netlink.Addr{}, nil, err - } - - // We only return the first IPv4 address, and the complete slice of IPv6 - // addresses. - return v4addr[0], v6addr, nil -}