From 40bae11aa7cdbe99dabbedba672f80fc0dfb6643 Mon Sep 17 00:00:00 2001 From: Wolfgang Nagele Date: Mon, 27 Feb 2017 23:23:12 +0100 Subject: [PATCH] Support for com.docker.network.bridge.container_interface_prefix label Signed-off-by: Wolfgang Nagele --- drivers/bridge/bridge.go | 33 ++++++++++++++++++++------------- drivers/bridge/bridge_store.go | 5 +++++ netlabel/labels.go | 3 +++ osl/interface_linux.go | 4 ++-- osl/namespace_linux.go | 10 +++++----- 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/drivers/bridge/bridge.go b/drivers/bridge/bridge.go index fa051bde5a..a32e627e13 100644 --- a/drivers/bridge/bridge.go +++ b/drivers/bridge/bridge.go @@ -28,11 +28,11 @@ import ( ) const ( - networkType = "bridge" - vethPrefix = "veth" - vethLen = 7 - containerVethPrefix = "eth" - maxAllocatePortAttempts = 10 + networkType = "bridge" + vethPrefix = "veth" + vethLen = 7 + defaultContainerVethPrefix = "eth" + maxAllocatePortAttempts = 10 ) const ( @@ -55,14 +55,15 @@ type configuration struct { // networkConfiguration for network specific configuration type networkConfiguration struct { - ID string - BridgeName string - EnableIPv6 bool - EnableIPMasquerade bool - EnableICC bool - Mtu int - DefaultBindingIP net.IP - DefaultBridge bool + ID string + BridgeName string + EnableIPv6 bool + EnableIPMasquerade bool + EnableICC bool + Mtu int + DefaultBindingIP net.IP + DefaultBridge bool + ContainerIfacePrefix string // Internal fields set after ipam data parsing AddressIPv4 *net.IPNet AddressIPv6 *net.IPNet @@ -239,6 +240,8 @@ func (c *networkConfiguration) fromLabels(labels map[string]string) error { if c.DefaultBindingIP = net.ParseIP(value); c.DefaultBindingIP == nil { return parseErr(label, value, "nil ip") } + case netlabel.ContainerIfacePrefix: + c.ContainerIfacePrefix = value } } @@ -1217,6 +1220,10 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, } iNames := jinfo.InterfaceName() + containerVethPrefix := defaultContainerVethPrefix + if network.config.ContainerIfacePrefix != "" { + containerVethPrefix = network.config.ContainerIfacePrefix + } err = iNames.SetNames(endpoint.srcName, containerVethPrefix) if err != nil { return err diff --git a/drivers/bridge/bridge_store.go b/drivers/bridge/bridge_store.go index 2ed60c7510..c7c83d8369 100644 --- a/drivers/bridge/bridge_store.go +++ b/drivers/bridge/bridge_store.go @@ -143,6 +143,7 @@ func (ncfg *networkConfiguration) MarshalJSON() ([]byte, error) { nMap["DefaultBindingIP"] = ncfg.DefaultBindingIP.String() nMap["DefaultGatewayIPv4"] = ncfg.DefaultGatewayIPv4.String() nMap["DefaultGatewayIPv6"] = ncfg.DefaultGatewayIPv6.String() + nMap["ContainerIfacePrefix"] = ncfg.ContainerIfacePrefix nMap["BridgeIfaceCreator"] = ncfg.BridgeIfaceCreator if ncfg.AddressIPv4 != nil { @@ -178,6 +179,10 @@ func (ncfg *networkConfiguration) UnmarshalJSON(b []byte) error { } } + if v, ok := nMap["ContainerIfacePrefix"]; ok { + ncfg.ContainerIfacePrefix = v.(string) + } + ncfg.DefaultBridge = nMap["DefaultBridge"].(bool) ncfg.DefaultBindingIP = net.ParseIP(nMap["DefaultBindingIP"].(string)) ncfg.DefaultGatewayIPv4 = net.ParseIP(nMap["DefaultGatewayIPv4"].(string)) diff --git a/netlabel/labels.go b/netlabel/labels.go index 49ba3081d3..1594556ec7 100644 --- a/netlabel/labels.go +++ b/netlabel/labels.go @@ -50,6 +50,9 @@ const ( // Internal constant represents that the network is internal which disables default gateway service Internal = Prefix + ".internal" + + // ContainerIfacePrefix can be used to override the interface prefix used inside the container + ContainerIfacePrefix = Prefix + ".container_iface_prefix" ) var ( diff --git a/osl/interface_linux.go b/osl/interface_linux.go index d76966200a..8e8a830ce9 100644 --- a/osl/interface_linux.go +++ b/osl/interface_linux.go @@ -241,8 +241,8 @@ func (n *networkNamespace) AddInterface(srcName, dstPrefix string, options ...If if n.isDefault { i.dstName = i.srcName } else { - i.dstName = fmt.Sprintf("%s%d", i.dstName, n.nextIfIndex) - n.nextIfIndex++ + i.dstName = fmt.Sprintf("%s%d", dstPrefix, n.nextIfIndex[dstPrefix]) + n.nextIfIndex[dstPrefix]++ } path := n.path diff --git a/osl/namespace_linux.go b/osl/namespace_linux.go index b714ccf9fa..478e3c48d8 100644 --- a/osl/namespace_linux.go +++ b/osl/namespace_linux.go @@ -48,7 +48,7 @@ type networkNamespace struct { gwv6 net.IP staticRoutes []*types.StaticRoute neighbors []*neigh - nextIfIndex int + nextIfIndex map[string]int isDefault bool nlHandle *netlink.Handle loV6Enabled bool @@ -203,7 +203,7 @@ func NewSandbox(key string, osCreate, isRestore bool) (Sandbox, error) { once.Do(createBasePath) } - n := &networkNamespace{path: key, isDefault: !osCreate} + n := &networkNamespace{path: key, isDefault: !osCreate, nextIfIndex: make(map[string]int)} sboxNs, err := netns.GetFromPath(n.path) if err != nil { @@ -256,7 +256,7 @@ func GetSandboxForExternalKey(basePath string, key string) (Sandbox, error) { if err := mountNetworkNamespace(basePath, key); err != nil { return nil, err } - n := &networkNamespace{path: key} + n := &networkNamespace{path: key, nextIfIndex: make(map[string]int)} sboxNs, err := netns.GetFromPath(n.path) if err != nil { @@ -495,8 +495,8 @@ func (n *networkNamespace) Restore(ifsopt map[string][]IfaceOption, routes []*ty } index++ n.Lock() - if index > n.nextIfIndex { - n.nextIfIndex = index + if index > n.nextIfIndex[dstPrefix] { + n.nextIfIndex[dstPrefix] = index } n.iFaces = append(n.iFaces, i) n.Unlock()