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

Make macvlan and ipvlan drivers as experimental

Signed-off-by: Madhu Venugopal <madhu@docker.com>
This commit is contained in:
Madhu Venugopal 2016-03-02 17:13:15 -08:00 committed by Brent Salisbury
parent ea30113303
commit ed9601e4c6
20 changed files with 473 additions and 225 deletions

View file

@ -23,7 +23,7 @@ build: ${build_image}.created
build-local: build-local:
@mkdir -p "bin" @mkdir -p "bin"
$(shell which godep) go build -o "bin/dnet" ./cmd/dnet $(shell which godep) go build -tags experimental -o "bin/dnet" ./cmd/dnet
clean: clean:
@if [ -d bin ]; then \ @if [ -d bin ]; then \

View file

@ -4,7 +4,6 @@ import (
"net" "net"
"sync" "sync"
"github.com/Sirupsen/logrus"
"github.com/docker/libnetwork/datastore" "github.com/docker/libnetwork/datastore"
"github.com/docker/libnetwork/discoverapi" "github.com/docker/libnetwork/discoverapi"
"github.com/docker/libnetwork/driverapi" "github.com/docker/libnetwork/driverapi"
@ -15,14 +14,14 @@ const (
vethLen = 7 vethLen = 7
containerVethPrefix = "eth" containerVethPrefix = "eth"
vethPrefix = "veth" vethPrefix = "veth"
ipvlanType = "ipvlan" // driver type name ipvlanType = "ipvlan" // driver type name
modeL2 = "l2" // ipvlan mode l2 is the default modeL2 = "l2" // ipvlan mode l2 is the default
modeL3 = "l3" // ipvlan L3 mode modeL3 = "l3" // ipvlan L3 mode
hostIfaceOpt = "host_iface" // host interface -o host_iface parentOpt = "parent" // parent interface -o parent
modeOpt = "_mode" // ipvlan mode ux opt suffix modeOpt = "_mode" // ipvlan mode ux opt suffix
) )
var driverModeOpt = ipvlanType + modeOpt // mode --option ipvlan_mode var driverModeOpt = ipvlanType + modeOpt // mode -o ipvlan_mode
type endpointTable map[string]*endpoint type endpointTable map[string]*endpoint
@ -54,9 +53,6 @@ type network struct {
// Init initializes and registers the libnetwork ipvlan driver // Init initializes and registers the libnetwork ipvlan driver
func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
if err := kernelSupport(ipvlanType); err != nil {
logrus.Warnf("encountered issues loading the ipvlan kernel module: %v", err)
}
c := driverapi.Capability{ c := driverapi.Capability{
DataScope: datastore.LocalScope, DataScope: datastore.LocalScope,
} }
@ -64,6 +60,7 @@ func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
networks: networkTable{}, networks: networkTable{},
} }
d.initStore(config) d.initStore(config)
return dc.RegisterDriver(ipvlanType, d, c) return dc.RegisterDriver(ipvlanType, d, c)
} }

View file

@ -3,6 +3,7 @@ package ipvlan
import ( import (
"fmt" "fmt"
"github.com/Sirupsen/logrus"
"github.com/docker/libnetwork/driverapi" "github.com/docker/libnetwork/driverapi"
"github.com/docker/libnetwork/netlabel" "github.com/docker/libnetwork/netlabel"
"github.com/docker/libnetwork/netutils" "github.com/docker/libnetwork/netutils"
@ -43,7 +44,7 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
if opt, ok := epOptions[netlabel.PortMap]; ok { if opt, ok := epOptions[netlabel.PortMap]; ok {
if _, ok := opt.([]types.PortBinding); ok { if _, ok := opt.([]types.PortBinding); ok {
if len(opt.([]types.PortBinding)) > 0 { if len(opt.([]types.PortBinding)) > 0 {
return fmt.Errorf("%s driver does not support port mappings", ipvlanType) logrus.Warnf("%s driver does not support port mappings", ipvlanType)
} }
} }
} }
@ -51,7 +52,7 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
if opt, ok := epOptions[netlabel.ExposedPorts]; ok { if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
if _, ok := opt.([]types.TransportPort); ok { if _, ok := opt.([]types.TransportPort); ok {
if len(opt.([]types.TransportPort)) > 0 { if len(opt.([]types.TransportPort)) > 0 {
return fmt.Errorf("%s driver does not support port exposures", ipvlanType) logrus.Warnf("%s driver does not support port exposures", ipvlanType)
} }
} }
} }
@ -76,5 +77,6 @@ func (d *driver) DeleteEndpoint(nid, eid string) error {
if link, err := netlink.LinkByName(ep.srcName); err == nil { if link, err := netlink.LinkByName(ep.srcName); err == nil {
netlink.LinkDel(link) netlink.LinkDel(link)
} }
return nil return nil
} }

View file

@ -39,7 +39,7 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
return fmt.Errorf("error generating an interface name: %v", err) return fmt.Errorf("error generating an interface name: %v", err)
} }
// create the netlink ipvlan interface // create the netlink ipvlan interface
vethName, err := createIPVlan(containerIfName, n.config.HostIface, n.config.IpvlanMode) vethName, err := createIPVlan(containerIfName, n.config.Parent, n.config.IpvlanMode)
if err != nil { if err != nil {
return err return err
} }
@ -59,8 +59,8 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
if err := jinfo.AddStaticRoute(defaultRoute.Destination, defaultRoute.RouteType, defaultRoute.NextHop); err != nil { if err := jinfo.AddStaticRoute(defaultRoute.Destination, defaultRoute.RouteType, defaultRoute.NextHop); err != nil {
return fmt.Errorf("failed to set an ipvlan l3 mode ipv4 default gateway: %v", err) return fmt.Errorf("failed to set an ipvlan l3 mode ipv4 default gateway: %v", err)
} }
logrus.Debugf("Ipvlan Endpoint Joined with IPv4_Addr: %s, Ipvlan_Mode: %s, Host_Iface: %s", logrus.Debugf("Ipvlan Endpoint Joined with IPv4_Addr: %s, Ipvlan_Mode: %s, Parent: %s",
ep.addr.IP.String(), n.config.IpvlanMode, n.config.HostIface) ep.addr.IP.String(), n.config.IpvlanMode, n.config.Parent)
// If the endpoint has a v6 address, set a v6 default route // If the endpoint has a v6 address, set a v6 default route
if ep.addrv6 != nil { if ep.addrv6 != nil {
default6Route, err := ifaceGateway(defaultV6RouteCidr) default6Route, err := ifaceGateway(defaultV6RouteCidr)
@ -70,8 +70,8 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
if err = jinfo.AddStaticRoute(default6Route.Destination, default6Route.RouteType, default6Route.NextHop); err != nil { if err = jinfo.AddStaticRoute(default6Route.Destination, default6Route.RouteType, default6Route.NextHop); err != nil {
return fmt.Errorf("failed to set an ipvlan l3 mode ipv6 default gateway: %v", err) return fmt.Errorf("failed to set an ipvlan l3 mode ipv6 default gateway: %v", err)
} }
logrus.Debugf("Ipvlan Endpoint Joined with IPv6_Addr: %s, Ipvlan_Mode: %s, Host_Iface: %s", logrus.Debugf("Ipvlan Endpoint Joined with IPv6_Addr: %s, Ipvlan_Mode: %s, Parent: %s",
ep.addrv6.IP.String(), n.config.IpvlanMode, n.config.HostIface) ep.addrv6.IP.String(), n.config.IpvlanMode, n.config.Parent)
} }
} }
if n.config.IpvlanMode == modeL2 { if n.config.IpvlanMode == modeL2 {
@ -89,8 +89,8 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
if err != nil { if err != nil {
return err return err
} }
logrus.Debugf("Ipvlan Endpoint Joined with IPv4_Addr: %s, Gateway: %s, Ipvlan_Mode: %s, Host_Iface: %s", logrus.Debugf("Ipvlan Endpoint Joined with IPv4_Addr: %s, Gateway: %s, Ipvlan_Mode: %s, Parent: %s",
ep.addr.IP.String(), v4gw.String(), n.config.IpvlanMode, n.config.HostIface) ep.addr.IP.String(), v4gw.String(), n.config.IpvlanMode, n.config.Parent)
} }
// parse and correlate the endpoint v6 address with the available v6 subnets // parse and correlate the endpoint v6 address with the available v6 subnets
if len(n.config.Ipv6Subnets) > 0 { if len(n.config.Ipv6Subnets) > 0 {
@ -106,8 +106,8 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
if err != nil { if err != nil {
return err return err
} }
logrus.Debugf("Ipvlan Endpoint Joined with IPv6_Addr: %s, Gateway: %s, Ipvlan_Mode: %s, Host_Iface: %s", logrus.Debugf("Ipvlan Endpoint Joined with IPv6_Addr: %s, Gateway: %s, Ipvlan_Mode: %s, Parent: %s",
ep.addrv6.IP.String(), v6gw.String(), n.config.IpvlanMode, n.config.HostIface) ep.addrv6.IP.String(), v6gw.String(), n.config.IpvlanMode, n.config.Parent)
} }
} }
iNames := jinfo.InterfaceName() iNames := jinfo.InterfaceName()
@ -115,6 +115,7 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
@ -131,6 +132,7 @@ func (d *driver) Leave(nid, eid string) error {
if endpoint == nil { if endpoint == nil {
return fmt.Errorf("could not find endpoint with id %s", eid) return fmt.Errorf("could not find endpoint with id %s", eid)
} }
return nil return nil
} }
@ -145,6 +147,7 @@ func ifaceGateway(dfNet string) (*staticRoute, error) {
RouteType: types.CONNECTED, RouteType: types.CONNECTED,
NextHop: nh, NextHop: nh,
} }
return defaultRoute, nil return defaultRoute, nil
} }
@ -165,6 +168,7 @@ func (n *network) getSubnetforIPv4(ip *net.IPNet) *ipv4Subnet {
return s return s
} }
} }
return nil return nil
} }
@ -185,5 +189,6 @@ func (n *network) getSubnetforIPv6(ip *net.IPNet) *ipv6Subnet {
return s return s
} }
} }
return nil return nil
} }

View file

@ -12,14 +12,14 @@ import (
) )
// CreateNetwork the network for the specified driver type // CreateNetwork the network for the specified driver type
func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Data, ipV6Data []driverapi.IPAMData) error { func (d *driver) CreateNetwork(nid string, option map[string]interface{}, ipV4Data, ipV6Data []driverapi.IPAMData) error {
// parse and validate the config and bind to networkConfiguration // parse and validate the config and bind to networkConfiguration
config, err := parseNetworkOptions(id, option) config, err := parseNetworkOptions(nid, option)
if err != nil { if err != nil {
return err return err
} }
config.ID = id config.ID = nid
err = config.processIPAM(id, ipV4Data, ipV6Data) err = config.processIPAM(nid, ipV4Data, ipV6Data)
if err != nil { if err != nil {
return err return err
} }
@ -33,14 +33,16 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Dat
default: default:
return fmt.Errorf("requested ipvlan mode '%s' is not valid, 'l2' mode is the ipvlan driver default", config.IpvlanMode) return fmt.Errorf("requested ipvlan mode '%s' is not valid, 'l2' mode is the ipvlan driver default", config.IpvlanMode)
} }
// user must specify a parent interface on the host to attach the container vif -o host_iface=eth0
if config.HostIface == "" {
return fmt.Errorf("%s requires an interface from the docker host to be specified (usage: -o host_iface=eth)", ipvlanType)
}
// loopback is not a valid parent link // loopback is not a valid parent link
if config.HostIface == "lo" { if config.Parent == "lo" {
return fmt.Errorf("loopback interface is not a valid %s parent link", ipvlanType) return fmt.Errorf("loopback interface is not a valid %s parent link", ipvlanType)
} }
// if parent interface not specified, create a dummy type link to use named dummy+net_id
if config.Parent == "" {
config.Parent = getDummyName(stringid.TruncateID(config.ID))
// empty parent and --internal are handled the same. Set here to update k/v
config.Internal = true
}
err = d.createNetwork(config) err = d.createNetwork(config)
if err != nil { if err != nil {
return err return err
@ -49,6 +51,7 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Dat
err = d.storeUpdate(config) err = d.storeUpdate(config)
if err != nil { if err != nil {
d.deleteNetwork(config.ID) d.deleteNetwork(config.ID)
logrus.Debugf("encoutered an error rolling back a network create for %s : %v", config.ID, err)
return err return err
} }
@ -57,23 +60,40 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Dat
// createNetwork is used by new network callbacks and persistent network cache // createNetwork is used by new network callbacks and persistent network cache
func (d *driver) createNetwork(config *configuration) error { func (d *driver) createNetwork(config *configuration) error {
// fail the network create if the ipvlan kernel module is unavailable
if err := kernelSupport(ipvlanType); err != nil {
return err
}
networkList := d.getNetworks() networkList := d.getNetworks()
for _, nw := range networkList { for _, nw := range networkList {
if config.HostIface == nw.config.HostIface { if config.Parent == nw.config.Parent {
return fmt.Errorf("network %s is already using host interface %s", return fmt.Errorf("network %s is already using parent interface %s",
stringid.TruncateID(nw.config.ID), config.HostIface) getDummyName(stringid.TruncateID(nw.config.ID)), config.Parent)
} }
} }
// if the -o host_iface does not exist, attempt to parse a parent_iface.vlan_id if !parentExists(config.Parent) {
if ok := hostIfaceExists(config.HostIface); !ok { // if the --internal flag is set, create a dummy link
// if the subinterface parent_iface.vlan_id checks do not pass, return err. if config.Internal {
// a valid example is eth0.10 for a parent iface: eth0 with a vlan id: 10 err := createDummyLink(config.Parent, getDummyName(stringid.TruncateID(config.ID)))
err := createVlanLink(config.HostIface) if err != nil {
if err != nil { return err
return err }
config.CreatedSlaveLink = true
// notify the user in logs they have limited comunicatins
if config.Parent == getDummyName(stringid.TruncateID(config.ID)) {
logrus.Debugf("Empty -o parent= and --internal flags limit communications to other containers inside of network: %s",
config.Parent)
}
} else {
// if the subinterface parent_iface.vlan_id checks do not pass, return err.
// a valid example is 'eth0.10' for a parent iface 'eth0' with a vlan id '10'
err := createVlanLink(config.Parent)
if err != nil {
return err
}
// if driver created the networks slave link, record it for future deletion
config.CreatedSlaveLink = true
} }
// if driver created the networks slave link, record it for future deletion
config.CreatedSlaveLink = true
} }
n := &network{ n := &network{
id: config.ID, id: config.ID,
@ -90,20 +110,38 @@ func (d *driver) createNetwork(config *configuration) error {
// DeleteNetwork the network for the specified driver type // DeleteNetwork the network for the specified driver type
func (d *driver) DeleteNetwork(nid string) error { func (d *driver) DeleteNetwork(nid string) error {
n := d.network(nid) n := d.network(nid)
if n == nil {
return fmt.Errorf("network id %s not found", nid)
}
// if the driver created the slave interface, delete it, otherwise leave it // if the driver created the slave interface, delete it, otherwise leave it
if ok := n.config.CreatedSlaveLink; ok { if ok := n.config.CreatedSlaveLink; ok {
// if the interface exists, only delete if it matches iface.vlan naming // if the interface exists, only delete if it matches iface.vlan or dummy.net_id naming
if ok := hostIfaceExists(n.config.HostIface); ok { if ok := parentExists(n.config.Parent); ok {
err := delVlanLink(n.config.HostIface) // only delete the link if it is named the net_id
if err != nil { if n.config.Parent == getDummyName(stringid.TruncateID(nid)) {
logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v", n.config.HostIface, err) err := delDummyLink(n.config.Parent)
if err != nil {
logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v",
n.config.Parent, err)
}
} else {
// only delete the link if it matches iface.vlan naming
err := delVlanLink(n.config.Parent)
if err != nil {
logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v",
n.config.Parent, err)
}
} }
} }
} }
// delete the *network // delete the *network
d.deleteNetwork(nid) d.deleteNetwork(nid)
// delete the network record from persistent cache // delete the network record from persistent cache
return d.storeDelete(n.config) err := d.storeDelete(n.config)
if err != nil {
return fmt.Errorf("error deleting deleting id %s from datastore: %v", nid, err)
}
return nil
} }
// parseNetworkOptions parse docker network options // parseNetworkOptions parse docker network options
@ -118,9 +156,11 @@ func parseNetworkOptions(id string, option options.Generic) (*configuration, err
return nil, err return nil, err
} }
} }
// return an error if the unsupported --internal is passed // setting the parent to "" will trigger an isolated network dummy parent link
if _, ok := option[netlabel.Internal]; ok { if _, ok := option[netlabel.Internal]; ok {
return nil, fmt.Errorf("--internal option is not supported with the ipvlan driver") config.Internal = true
// empty --parent= and --internal are handled the same.
config.Parent = ""
} }
return config, nil return config, nil
} }
@ -152,9 +192,9 @@ func parseNetworkGenericOptions(data interface{}) (*configuration, error) {
func (config *configuration) fromOptions(labels map[string]string) error { func (config *configuration) fromOptions(labels map[string]string) error {
for label, value := range labels { for label, value := range labels {
switch label { switch label {
case hostIfaceOpt: case parentOpt:
// parse driver option '-o host_iface' // parse driver option '-o parent'
config.HostIface = value config.Parent = value
case driverModeOpt: case driverModeOpt:
// parse driver option '-o ipvlan_mode' // parse driver option '-o ipvlan_mode'
config.IpvlanMode = value config.IpvlanMode = value

View file

@ -13,32 +13,42 @@ import (
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
) )
// Create the ipvlan netlink interface specifying the source name const (
func createIPVlan(containerIfName, hostIface, ipVlanMode string) (string, error) { dummyPrefix = "di-" // ipvlan prefix for dummy parent interface
ipvlanKernelVer = "3.19" // minimum ipvlan kernel version support
)
// createIPVlan Create the ipvlan slave specifying the source name
func createIPVlan(containerIfName, parent, ipvlanMode string) (string, error) {
defer osl.InitOSContext()() defer osl.InitOSContext()()
// Set the ipvlan mode. Default is L2 mode
mode, err := setIPVlanMode(ipVlanMode) // Set the ipvlan mode. Default is bridge mode
mode, err := setIPVlanMode(ipvlanMode)
if err != nil { if err != nil {
return "", fmt.Errorf("unsupported %s ipvlan mode: %v", ipVlanMode, err) return "", fmt.Errorf("Unsupported %s ipvlan mode: %v", ipvlanMode, err)
}
// verify the Docker host interface acting as the macvlan parent iface exists
if !parentExists(parent) {
return "", fmt.Errorf("the requested parent interface %s was not found on the Docker host", parent)
} }
// Get the link for the master index (Example: the docker host eth iface) // Get the link for the master index (Example: the docker host eth iface)
hostEth, err := netlink.LinkByName(hostIface) parentLink, err := netlink.LinkByName(parent)
if err != nil { if err != nil {
return "", fmt.Errorf("the requested host interface %s was not found on the Docker host", hostIface) return "", fmt.Errorf("error occoured looking up the %s parent iface %s error: %s", ipvlanType, parent, err)
} }
// Create a ipvlan link // Create a ipvlan link
ipvlan := &netlink.IPVlan{ ipvlan := &netlink.IPVlan{
LinkAttrs: netlink.LinkAttrs{ LinkAttrs: netlink.LinkAttrs{
Name: containerIfName, Name: containerIfName,
ParentIndex: hostEth.Attrs().Index, ParentIndex: parentLink.Attrs().Index,
}, },
Mode: mode, Mode: mode,
} }
if err := netlink.LinkAdd(ipvlan); err != nil { if err := netlink.LinkAdd(ipvlan); err != nil {
// verbose but will be an issue if you create a macvlan and ipvlan using the same parent iface // If a user creates a macvlan and ipvlan on same parent, only one slave iface can be active at a time.
logrus.Warn("Ensure there are no macvlan networks using the same `-o host_iface` as the ipvlan network.") return "", fmt.Errorf("failed to create the %s port: %v", ipvlanType, err)
return "", fmt.Errorf("Failed to create ipvlan link: %s with the error: %v", ipvlan.Name, err)
} }
return ipvlan.Attrs().Name, nil return ipvlan.Attrs().Name, nil
} }
@ -54,12 +64,13 @@ func setIPVlanMode(mode string) (netlink.IPVlanMode, error) {
} }
} }
// validateHostIface check if the specified interface exists in the default namespace // parentExists check if the specified interface exists in the default namespace
func hostIfaceExists(ifaceStr string) bool { func parentExists(ifaceStr string) bool {
_, err := netlink.LinkByName(ifaceStr) _, err := netlink.LinkByName(ifaceStr)
if err != nil { if err != nil {
return false return false
} }
return true return true
} }
@ -78,13 +89,14 @@ func kernelSupport(networkTpe string) error {
return nil return nil
} }
} }
return fmt.Errorf("required kernel module '%s' was not found in /proc/modules, ipvlan requires kernel version >= 3.19", ipvlanType)
return fmt.Errorf("unable to load the Linux kernel module %s", ipvlanType)
} }
// createVlanLink parses sub-interfaces and vlan id for creation // createVlanLink parses sub-interfaces and vlan id for creation
func createVlanLink(ifaceName string) error { func createVlanLink(parentName string) error {
if strings.Contains(ifaceName, ".") { if strings.Contains(parentName, ".") {
parentIface, vidInt, err := parseVlan(ifaceName) parent, vidInt, err := parseVlan(parentName)
if err != nil { if err != nil {
return err return err
} }
@ -93,74 +105,124 @@ func createVlanLink(ifaceName string) error {
return fmt.Errorf("vlan id must be between 1-4094, received: %d", vidInt) return fmt.Errorf("vlan id must be between 1-4094, received: %d", vidInt)
} }
// get the parent link to attach a vlan subinterface // get the parent link to attach a vlan subinterface
hostIface, err := netlink.LinkByName(parentIface) parentLink, err := netlink.LinkByName(parent)
if err != nil { if err != nil {
return fmt.Errorf("failed to find master interface %s on the Docker host: %v", parentIface, err) return fmt.Errorf("failed to find master interface %s on the Docker host: %v", parent, err)
} }
vlanIface := &netlink.Vlan{ vlanLink := &netlink.Vlan{
LinkAttrs: netlink.LinkAttrs{ LinkAttrs: netlink.LinkAttrs{
Name: ifaceName, Name: parentName,
ParentIndex: hostIface.Attrs().Index, ParentIndex: parentLink.Attrs().Index,
}, },
VlanId: vidInt, VlanId: vidInt,
} }
// create the subinterface // create the subinterface
if err := netlink.LinkAdd(vlanIface); err != nil { if err := netlink.LinkAdd(vlanLink); err != nil {
return fmt.Errorf("failed to create %s vlan link: %v", vlanIface.Name, err) return fmt.Errorf("failed to create %s vlan link: %v", vlanLink.Name, err)
} }
// Bring the new netlink iface up // Bring the new netlink iface up
if err := netlink.LinkSetUp(vlanIface); err != nil { if err := netlink.LinkSetUp(vlanLink); err != nil {
return fmt.Errorf("failed to enable %s the ipvlan netlink link: %v", vlanIface.Name, err) return fmt.Errorf("failed to enable %s the ipvlan parent link %v", vlanLink.Name, err)
} }
logrus.Debugf("Added a vlan tagged netlink subinterface: %s with a vlan id: %d", ifaceName, vidInt) logrus.Debugf("Added a vlan tagged netlink subinterface: %s with a vlan id: %d", parentName, vidInt)
return nil return nil
} }
return fmt.Errorf("invalid subinterface vlan name %s, example formatting is eth0.10", ifaceName)
return fmt.Errorf("invalid subinterface vlan name %s, example formatting is eth0.10", parentName)
} }
// verifyVlanDel verifies only sub-interfaces with a vlan id get deleted // delVlanLink verifies only sub-interfaces with a vlan id get deleted
func delVlanLink(ifaceName string) error { func delVlanLink(linkName string) error {
if strings.Contains(ifaceName, ".") { if strings.Contains(linkName, ".") {
_, _, err := parseVlan(ifaceName) _, _, err := parseVlan(linkName)
if err != nil { if err != nil {
return err return err
} }
// delete the vlan subinterface // delete the vlan subinterface
vlanIface, err := netlink.LinkByName(ifaceName) vlanLink, err := netlink.LinkByName(linkName)
if err != nil { if err != nil {
return fmt.Errorf("failed to find interface %s on the Docker host : %v", ifaceName, err) return fmt.Errorf("failed to find interface %s on the Docker host : %v", linkName, err)
} }
// verify a parent interface isn't being deleted // verify a parent interface isn't being deleted
if vlanIface.Attrs().ParentIndex == 0 { if vlanLink.Attrs().ParentIndex == 0 {
return fmt.Errorf("interface %s does not appear to be a slave device: %v", ifaceName, err) return fmt.Errorf("interface %s does not appear to be a slave device: %v", linkName, err)
} }
// delete the ipvlan slave device // delete the ipvlan slave device
if err := netlink.LinkDel(vlanIface); err != nil { if err := netlink.LinkDel(vlanLink); err != nil {
return fmt.Errorf("failed to delete %s link: %v", ifaceName, err) return fmt.Errorf("failed to delete %s link: %v", linkName, err)
} }
logrus.Debugf("Deleted a vlan tagged netlink subinterface: %s", ifaceName) logrus.Debugf("Deleted a vlan tagged netlink subinterface: %s", linkName)
} }
// if the subinterface doesn't parse to iface.vlan_id leave the interface in // if the subinterface doesn't parse to iface.vlan_id leave the interface in
// place since it could be a user specified name not created by the driver. // place since it could be a user specified name not created by the driver.
return nil return nil
} }
// parseVlan parses and verifies a slave interface name: -o host_iface=eth0.10 // parseVlan parses and verifies a slave interface name: -o parent=eth0.10
func parseVlan(ifaceName string) (string, int, error) { func parseVlan(linkName string) (string, int, error) {
// parse -o host_iface=eth0.10 // parse -o parent=eth0.10
splitIface := strings.Split(ifaceName, ".") splitName := strings.Split(linkName, ".")
if len(splitIface) != 2 { if len(splitName) != 2 {
return "", 0, fmt.Errorf("required interface name format is: name.vlan_id, ex. eth0.10 for vlan 10, instead received %s", ifaceName) return "", 0, fmt.Errorf("required interface name format is: name.vlan_id, ex. eth0.10 for vlan 10, instead received %s", linkName)
} }
parentIface, vidStr := splitIface[0], splitIface[1] parent, vidStr := splitName[0], splitName[1]
// validate type and convert vlan id to int // validate type and convert vlan id to int
vidInt, err := strconv.Atoi(vidStr) vidInt, err := strconv.Atoi(vidStr)
if err != nil { if err != nil {
return "", 0, fmt.Errorf("unable to parse a valid vlan id from: %s (ex. eth0.10 for vlan 10)", vidStr) return "", 0, fmt.Errorf("unable to parse a valid vlan id from: %s (ex. eth0.10 for vlan 10)", vidStr)
} }
// Check if the interface exists // Check if the interface exists
if ok := hostIfaceExists(parentIface); !ok { if !parentExists(parent) {
return "", 0, fmt.Errorf("-o host_iface parent interface does was not found on the host: %s", parentIface) return "", 0, fmt.Errorf("-o parent interface does was not found on the host: %s", parent)
} }
return parentIface, vidInt, nil
return parent, vidInt, nil
}
// createDummyLink creates a dummy0 parent link
func createDummyLink(dummyName, truncNetID string) error {
// create a parent interface since one was not specified
parent := &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Name: dummyName,
},
}
if err := netlink.LinkAdd(parent); err != nil {
return err
}
parentDummyLink, err := netlink.LinkByName(dummyName)
if err != nil {
return fmt.Errorf("error occoured looking up the %s parent iface %s error: %s", ipvlanType, dummyName, err)
}
// bring the new netlink iface up
if err := netlink.LinkSetUp(parentDummyLink); err != nil {
return fmt.Errorf("failed to enable %s the ipvlan parent link: %v", dummyName, err)
}
return nil
}
// delDummyLink deletes the link type dummy used when -o parent is not passed
func delDummyLink(linkName string) error {
// delete the vlan subinterface
dummyLink, err := netlink.LinkByName(linkName)
if err != nil {
return fmt.Errorf("failed to find link %s on the Docker host : %v", linkName, err)
}
// verify a parent interface is being deleted
if dummyLink.Attrs().ParentIndex != 0 {
return fmt.Errorf("link %s is not a parent dummy interface", linkName)
}
// delete the ipvlan dummy device
if err := netlink.LinkDel(dummyLink); err != nil {
return fmt.Errorf("failed to delete the dummy %s link: %v", linkName, err)
}
logrus.Debugf("Deleted a dummy parent link: %s", linkName)
return nil
}
// getDummyName returns the name of a dummy parent with truncated net ID and driver prefix
func getDummyName(netID string) string {
return fmt.Sprintf("%s%s", dummyPrefix, netID)
} }

View file

@ -6,17 +6,17 @@ import (
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
) )
// TestValidateLink tests the validateHostIface function // TestValidateLink tests the parentExists function
func TestValidateLink(t *testing.T) { func TestValidateLink(t *testing.T) {
validIface := "lo" validIface := "lo"
invalidIface := "foo12345" invalidIface := "foo12345"
// test a valid parent interface validation // test a valid parent interface validation
if ok := hostIfaceExists(validIface); !ok { if ok := parentExists(validIface); !ok {
t.Fatalf("failed validating loopback %s", validIface) t.Fatalf("failed validating loopback %s", validIface)
} }
// test a invalid parent interface validation // test a invalid parent interface validation
if ok := hostIfaceExists(invalidIface); ok { if ok := parentExists(invalidIface); ok {
t.Fatalf("failed to invalidate interface %s", invalidIface) t.Fatalf("failed to invalidate interface %s", invalidIface)
} }
} }

View file

@ -15,6 +15,7 @@ func (d *driver) network(nid string) *network {
if !ok { if !ok {
logrus.Errorf("network id %s not found", nid) logrus.Errorf("network id %s not found", nid)
} }
return n return n
} }
@ -39,6 +40,7 @@ func (d *driver) getNetworks() []*network {
for _, nw := range d.networks { for _, nw := range d.networks {
ls = append(ls, nw) ls = append(ls, nw)
} }
return ls return ls
} }
@ -70,6 +72,7 @@ func (n *network) getEndpoint(eid string) (*endpoint, error) {
if ep, ok := n.endpoints[eid]; ok { if ep, ok := n.endpoints[eid]; ok {
return ep, nil return ep, nil
} }
return nil, nil return nil, nil
} }
@ -80,12 +83,14 @@ func validateID(nid, eid string) error {
if eid == "" { if eid == "" {
return fmt.Errorf("invalid endpoint id") return fmt.Errorf("invalid endpoint id")
} }
return nil return nil
} }
func (n *network) sandbox() osl.Sandbox { func (n *network) sandbox() osl.Sandbox {
n.Lock() n.Lock()
defer n.Unlock() defer n.Unlock()
return n.sbox return n.sbox
} }
@ -105,5 +110,6 @@ func (d *driver) getNetwork(id string) (*network, error) {
if nw, ok := d.networks[id]; ok { if nw, ok := d.networks[id]; ok {
return nw, nil return nw, nil
} }
return nil, types.NotFoundErrorf("network not found: %s", id) return nil, types.NotFoundErrorf("network not found: %s", id)
} }

View file

@ -21,7 +21,7 @@ type configuration struct {
dbIndex uint64 dbIndex uint64
dbExists bool dbExists bool
Internal bool Internal bool
HostIface string Parent string
IpvlanMode string IpvlanMode string
CreatedSlaveLink bool CreatedSlaveLink bool
Ipv4Subnets []*ipv4Subnet Ipv4Subnets []*ipv4Subnet
@ -114,8 +114,9 @@ func (config *configuration) MarshalJSON() ([]byte, error) {
nMap := make(map[string]interface{}) nMap := make(map[string]interface{})
nMap["ID"] = config.ID nMap["ID"] = config.ID
nMap["Mtu"] = config.Mtu nMap["Mtu"] = config.Mtu
nMap["HostIface"] = config.HostIface nMap["Parent"] = config.Parent
nMap["IpvlanMode"] = config.IpvlanMode nMap["IpvlanMode"] = config.IpvlanMode
nMap["Internal"] = config.Internal
nMap["CreatedSubIface"] = config.CreatedSlaveLink nMap["CreatedSubIface"] = config.CreatedSlaveLink
if len(config.Ipv4Subnets) > 0 { if len(config.Ipv4Subnets) > 0 {
iis, err := json.Marshal(config.Ipv4Subnets) iis, err := json.Marshal(config.Ipv4Subnets)
@ -131,6 +132,7 @@ func (config *configuration) MarshalJSON() ([]byte, error) {
} }
nMap["Ipv6Subnets"] = string(iis) nMap["Ipv6Subnets"] = string(iis)
} }
return json.Marshal(nMap) return json.Marshal(nMap)
} }
@ -145,8 +147,9 @@ func (config *configuration) UnmarshalJSON(b []byte) error {
} }
config.ID = nMap["ID"].(string) config.ID = nMap["ID"].(string)
config.Mtu = int(nMap["Mtu"].(float64)) config.Mtu = int(nMap["Mtu"].(float64))
config.HostIface = nMap["HostIface"].(string) config.Parent = nMap["Parent"].(string)
config.IpvlanMode = nMap["IpvlanMode"].(string) config.IpvlanMode = nMap["IpvlanMode"].(string)
config.Internal = nMap["Internal"].(bool)
config.CreatedSlaveLink = nMap["CreatedSubIface"].(bool) config.CreatedSlaveLink = nMap["CreatedSubIface"].(bool)
if v, ok := nMap["Ipv4Subnets"]; ok { if v, ok := nMap["Ipv4Subnets"]; ok {
if err := json.Unmarshal([]byte(v.(string)), &config.Ipv4Subnets); err != nil { if err := json.Unmarshal([]byte(v.(string)), &config.Ipv4Subnets); err != nil {
@ -158,6 +161,7 @@ func (config *configuration) UnmarshalJSON(b []byte) error {
return err return err
} }
} }
return nil return nil
} }

View file

@ -4,7 +4,6 @@ import (
"net" "net"
"sync" "sync"
"github.com/Sirupsen/logrus"
"github.com/docker/libnetwork/datastore" "github.com/docker/libnetwork/datastore"
"github.com/docker/libnetwork/discoverapi" "github.com/docker/libnetwork/discoverapi"
"github.com/docker/libnetwork/driverapi" "github.com/docker/libnetwork/driverapi"
@ -15,13 +14,13 @@ const (
vethLen = 7 vethLen = 7
containerVethPrefix = "eth" containerVethPrefix = "eth"
vethPrefix = "veth" vethPrefix = "veth"
macvlanType = "macvlan" // driver type name macvlanType = "macvlan" // driver type name
modePrivate = "private" // macvlan mode private modePrivate = "private" // macvlan mode private
modeVepa = "vepa" // macvlan mode vepa modeVepa = "vepa" // macvlan mode vepa
modeBridge = "bridge" // macvlan mode bridge modeBridge = "bridge" // macvlan mode bridge
modePassthru = "passthru" // macvlan mode passthrough modePassthru = "passthru" // macvlan mode passthrough
hostIfaceOpt = "host_iface" // host interface -o host_iface parentOpt = "parent" // parent interface -o parent
modeOpt = "_mode" // macvlan mode ux opt suffix modeOpt = "_mode" // macvlan mode ux opt suffix
) )
var driverModeOpt = macvlanType + modeOpt // mode --option macvlan_mode var driverModeOpt = macvlanType + modeOpt // mode --option macvlan_mode
@ -56,9 +55,6 @@ type network struct {
// Init initializes and registers the libnetwork macvlan driver // Init initializes and registers the libnetwork macvlan driver
func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
if err := kernelSupport(macvlanType); err != nil {
logrus.Warnf("encountered errors loading the macvlan kernel module: %v", err)
}
c := driverapi.Capability{ c := driverapi.Capability{
DataScope: datastore.LocalScope, DataScope: datastore.LocalScope,
} }
@ -66,6 +62,7 @@ func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
networks: networkTable{}, networks: networkTable{},
} }
d.initStore(config) d.initStore(config)
return dc.RegisterDriver(macvlanType, d, c) return dc.RegisterDriver(macvlanType, d, c)
} }

View file

@ -3,6 +3,7 @@ package macvlan
import ( import (
"fmt" "fmt"
"github.com/Sirupsen/logrus"
"github.com/docker/libnetwork/driverapi" "github.com/docker/libnetwork/driverapi"
"github.com/docker/libnetwork/netlabel" "github.com/docker/libnetwork/netlabel"
"github.com/docker/libnetwork/netutils" "github.com/docker/libnetwork/netutils"
@ -21,9 +22,6 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
if err != nil { if err != nil {
return fmt.Errorf("network id %q not found", nid) return fmt.Errorf("network id %q not found", nid)
} }
if ifInfo.MacAddress() != nil {
return fmt.Errorf("%s interfaces do not support custom mac address assigment", macvlanType)
}
ep := &endpoint{ ep := &endpoint{
id: eid, id: eid,
addr: ifInfo.Address(), addr: ifInfo.Address(),
@ -43,7 +41,7 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
if opt, ok := epOptions[netlabel.PortMap]; ok { if opt, ok := epOptions[netlabel.PortMap]; ok {
if _, ok := opt.([]types.PortBinding); ok { if _, ok := opt.([]types.PortBinding); ok {
if len(opt.([]types.PortBinding)) > 0 { if len(opt.([]types.PortBinding)) > 0 {
return fmt.Errorf("%s driver does not support port mappings", macvlanType) logrus.Warnf("%s driver does not support port mappings", macvlanType)
} }
} }
} }
@ -51,7 +49,7 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
if opt, ok := epOptions[netlabel.ExposedPorts]; ok { if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
if _, ok := opt.([]types.TransportPort); ok { if _, ok := opt.([]types.TransportPort); ok {
if len(opt.([]types.TransportPort)) > 0 { if len(opt.([]types.TransportPort)) > 0 {
return fmt.Errorf("%s driver does not support port exposures", macvlanType) logrus.Warnf("%s driver does not support port exposures", macvlanType)
} }
} }
} }
@ -76,5 +74,6 @@ func (d *driver) DeleteEndpoint(nid, eid string) error {
if link, err := netlink.LinkByName(ep.srcName); err == nil { if link, err := netlink.LinkByName(ep.srcName); err == nil {
netlink.LinkDel(link) netlink.LinkDel(link)
} }
return nil return nil
} }

View file

@ -27,7 +27,7 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
return fmt.Errorf("error generating an interface name: %s", err) return fmt.Errorf("error generating an interface name: %s", err)
} }
// create the netlink macvlan interface // create the netlink macvlan interface
vethName, err := createMacVlan(containerIfName, n.config.HostIface, n.config.MacvlanMode) vethName, err := createMacVlan(containerIfName, n.config.Parent, n.config.MacvlanMode)
if err != nil { if err != nil {
return err return err
} }
@ -51,8 +51,8 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
if err != nil { if err != nil {
return err return err
} }
logrus.Debugf("Macvlan Endpoint Joined with IPv4_Addr: %s, Gateway: %s, MacVlan_Mode: %s, Host_Iface: %s", logrus.Debugf("Macvlan Endpoint Joined with IPv4_Addr: %s, Gateway: %s, MacVlan_Mode: %s, Parent: %s",
ep.addr.IP.String(), v4gw.String(), n.config.MacvlanMode, n.config.HostIface) ep.addr.IP.String(), v4gw.String(), n.config.MacvlanMode, n.config.Parent)
} }
// parse and match the endpoint address with the available v6 subnets // parse and match the endpoint address with the available v6 subnets
if len(n.config.Ipv6Subnets) > 0 { if len(n.config.Ipv6Subnets) > 0 {
@ -68,14 +68,15 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
if err != nil { if err != nil {
return err return err
} }
logrus.Debugf("Macvlan Endpoint Joined with IPv6_Addr: %s Gateway: %s MacVlan_Mode: %s, Host_Iface: %s", logrus.Debugf("Macvlan Endpoint Joined with IPv6_Addr: %s Gateway: %s MacVlan_Mode: %s, Parent: %s",
ep.addrv6.IP.String(), v6gw.String(), n.config.MacvlanMode, n.config.HostIface) ep.addrv6.IP.String(), v6gw.String(), n.config.MacvlanMode, n.config.Parent)
} }
iNames := jinfo.InterfaceName() iNames := jinfo.InterfaceName()
err = iNames.SetNames(vethName, containerVethPrefix) err = iNames.SetNames(vethName, containerVethPrefix)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
@ -92,6 +93,7 @@ func (d *driver) Leave(nid, eid string) error {
if endpoint == nil { if endpoint == nil {
return fmt.Errorf("could not find endpoint with id %s", eid) return fmt.Errorf("could not find endpoint with id %s", eid)
} }
return nil return nil
} }
@ -112,6 +114,7 @@ func (n *network) getSubnetforIPv4(ip *net.IPNet) *ipv4Subnet {
return s return s
} }
} }
return nil return nil
} }
@ -132,5 +135,6 @@ func (n *network) getSubnetforIPv6(ip *net.IPNet) *ipv6Subnet {
return s return s
} }
} }
return nil return nil
} }

View file

@ -12,25 +12,17 @@ import (
) )
// CreateNetwork the network for the specified driver type // CreateNetwork the network for the specified driver type
func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Data, ipV6Data []driverapi.IPAMData) error { func (d *driver) CreateNetwork(nid string, option map[string]interface{}, ipV4Data, ipV6Data []driverapi.IPAMData) error {
// parse and validate the config and bind to networkConfiguration // parse and validate the config and bind to networkConfiguration
config, err := parseNetworkOptions(id, option) config, err := parseNetworkOptions(nid, option)
if err != nil { if err != nil {
return err return err
} }
config.ID = id config.ID = nid
err = config.processIPAM(id, ipV4Data, ipV6Data) err = config.processIPAM(nid, ipV4Data, ipV6Data)
if err != nil { if err != nil {
return err return err
} }
// user must specify a parent interface on the host to attach the container vif -o host_iface=eth0
if config.HostIface == "" {
return fmt.Errorf("%s requires an interface from the docker host to be specified (usage: -o host_iface=eth)", macvlanType)
}
// loopback is not a valid parent link
if config.HostIface == "lo" {
return fmt.Errorf("loopback interface is not a valid %s parent link", macvlanType)
}
// verify the macvlan mode from -o macvlan_mode option // verify the macvlan mode from -o macvlan_mode option
switch config.MacvlanMode { switch config.MacvlanMode {
case "", modeBridge: case "", modeBridge:
@ -45,6 +37,16 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Dat
default: default:
return fmt.Errorf("requested macvlan mode '%s' is not valid, 'bridge' mode is the macvlan driver default", config.MacvlanMode) return fmt.Errorf("requested macvlan mode '%s' is not valid, 'bridge' mode is the macvlan driver default", config.MacvlanMode)
} }
// loopback is not a valid parent link
if config.Parent == "lo" {
return fmt.Errorf("loopback interface is not a valid %s parent link", macvlanType)
}
// if parent interface not specified, create a dummy type link to use named dummy+net_id
if config.Parent == "" {
config.Parent = getDummyName(stringid.TruncateID(config.ID))
// empty parent and --internal are handled the same. Set here to update k/v
config.Internal = true
}
err = d.createNetwork(config) err = d.createNetwork(config)
if err != nil { if err != nil {
return err return err
@ -53,6 +55,7 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Dat
err = d.storeUpdate(config) err = d.storeUpdate(config)
if err != nil { if err != nil {
d.deleteNetwork(config.ID) d.deleteNetwork(config.ID)
logrus.Debugf("encoutered an error rolling back a network create for %s : %v", config.ID, err)
return err return err
} }
@ -61,24 +64,40 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Dat
// createNetwork is used by new network callbacks and persistent network cache // createNetwork is used by new network callbacks and persistent network cache
func (d *driver) createNetwork(config *configuration) error { func (d *driver) createNetwork(config *configuration) error {
// if the -o host_iface does not exist, attempt to parse a parent_iface.vlan_id // fail the network create if the macvlan kernel module is unavailable
if err := kernelSupport(macvlanType); err != nil {
return err
}
networkList := d.getNetworks() networkList := d.getNetworks()
for _, nw := range networkList { for _, nw := range networkList {
if config.HostIface == nw.config.HostIface { if config.Parent == nw.config.Parent {
return fmt.Errorf("network %s is already using host interface %s", return fmt.Errorf("network %s is already using parent interface %s",
stringid.TruncateID(nw.config.ID), config.HostIface) getDummyName(stringid.TruncateID(nw.config.ID)), config.Parent)
} }
} }
// if the -o host_iface does not exist, attempt to parse a parent_iface.vlan_id if !parentExists(config.Parent) {
if ok := hostIfaceExists(config.HostIface); !ok { // if the --internal flag is set, create a dummy link
// if the subinterface parent_iface.vlan_id checks do not pass, return err. if config.Internal {
// a valid example is eth0.10 for a parent iface: eth0 with a vlan id: 10 err := createDummyLink(config.Parent, getDummyName(stringid.TruncateID(config.ID)))
err := createVlanLink(config.HostIface) if err != nil {
if err != nil { return err
return err }
config.CreatedSlaveLink = true
// notify the user in logs they have limited comunicatins
if config.Parent == getDummyName(stringid.TruncateID(config.ID)) {
logrus.Debugf("Empty -o parent= and --internal flags limit communications to other containers inside of network: %s",
config.Parent)
}
} else {
// if the subinterface parent_iface.vlan_id checks do not pass, return err.
// a valid example is 'eth0.10' for a parent iface 'eth0' with a vlan id '10'
err := createVlanLink(config.Parent)
if err != nil {
return err
}
// if driver created the networks slave link, record it for future deletion
config.CreatedSlaveLink = true
} }
// if driver created the networks slave link, record it for future deletion
config.CreatedSlaveLink = true
} }
n := &network{ n := &network{
id: config.ID, id: config.ID,
@ -95,20 +114,38 @@ func (d *driver) createNetwork(config *configuration) error {
// DeleteNetwork the network for the specified driver type // DeleteNetwork the network for the specified driver type
func (d *driver) DeleteNetwork(nid string) error { func (d *driver) DeleteNetwork(nid string) error {
n := d.network(nid) n := d.network(nid)
if n == nil {
return fmt.Errorf("network id %s not found", nid)
}
// if the driver created the slave interface, delete it, otherwise leave it // if the driver created the slave interface, delete it, otherwise leave it
if ok := n.config.CreatedSlaveLink; ok { if ok := n.config.CreatedSlaveLink; ok {
// if the interface exists, only delete if it matches iface.vlan naming // if the interface exists, only delete if it matches iface.vlan or dummy.net_id naming
if ok := hostIfaceExists(n.config.HostIface); ok { if ok := parentExists(n.config.Parent); ok {
err := delVlanLink(n.config.HostIface) // only delete the link if it is named the net_id
if err != nil { if n.config.Parent == getDummyName(stringid.TruncateID(nid)) {
logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v", n.config.HostIface, err) err := delDummyLink(n.config.Parent)
if err != nil {
logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v",
n.config.Parent, err)
}
} else {
// only delete the link if it matches iface.vlan naming
err := delVlanLink(n.config.Parent)
if err != nil {
logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v",
n.config.Parent, err)
}
} }
} }
} }
// delete the *network // delete the *network
d.deleteNetwork(nid) d.deleteNetwork(nid)
// delete the network record from persistent cache // delete the network record from persistent cache
return d.storeDelete(n.config) err := d.storeDelete(n.config)
if err != nil {
return fmt.Errorf("error deleting deleting id %s from datastore: %v", nid, err)
}
return nil
} }
// parseNetworkOptions parse docker network options // parseNetworkOptions parse docker network options
@ -123,10 +160,13 @@ func parseNetworkOptions(id string, option options.Generic) (*configuration, err
return nil, err return nil, err
} }
} }
// return an error if the unsupported --internal is passed // setting the parent to "" will trigger an isolated network dummy parent link
if _, ok := option[netlabel.Internal]; ok { if _, ok := option[netlabel.Internal]; ok {
return nil, fmt.Errorf("--internal option is not supported with the macvlan driver") config.Internal = true
// empty --parent= and --internal are handled the same.
config.Parent = ""
} }
return config, nil return config, nil
} }
@ -150,6 +190,7 @@ func parseNetworkGenericOptions(data interface{}) (*configuration, error) {
default: default:
err = types.BadRequestErrorf("unrecognized network configuration format: %v", opt) err = types.BadRequestErrorf("unrecognized network configuration format: %v", opt)
} }
return config, err return config, err
} }
@ -157,14 +198,15 @@ func parseNetworkGenericOptions(data interface{}) (*configuration, error) {
func (config *configuration) fromOptions(labels map[string]string) error { func (config *configuration) fromOptions(labels map[string]string) error {
for label, value := range labels { for label, value := range labels {
switch label { switch label {
case hostIfaceOpt: case parentOpt:
// parse driver option '-o host_iface' // parse driver option '-o parent'
config.HostIface = value config.Parent = value
case driverModeOpt: case driverModeOpt:
// parse driver option '-o macvlan_mode' // parse driver option '-o macvlan_mode'
config.MacvlanMode = value config.MacvlanMode = value
} }
} }
return nil return nil
} }
@ -188,5 +230,6 @@ func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []dri
config.Ipv6Subnets = append(config.Ipv6Subnets, s) config.Ipv6Subnets = append(config.Ipv6Subnets, s)
} }
} }
return nil return nil
} }

View file

@ -13,8 +13,13 @@ import (
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
) )
// Create the netlink interface specifying the source name const (
func createMacVlan(containerIfName, hostIface, macvlanMode string) (string, error) { dummyPrefix = "dm-" // macvlan prefix for dummy parent interface
macvlanKernelVer = "3.9" // minimum ipvlan kernel version support
)
// Create the macvlan slave specifying the source name
func createMacVlan(containerIfName, parent, macvlanMode string) (string, error) {
defer osl.InitOSContext()() defer osl.InitOSContext()()
// Set the macvlan mode. Default is bridge mode // Set the macvlan mode. Default is bridge mode
@ -23,27 +28,27 @@ func createMacVlan(containerIfName, hostIface, macvlanMode string) (string, erro
return "", fmt.Errorf("Unsupported %s macvlan mode: %v", macvlanMode, err) return "", fmt.Errorf("Unsupported %s macvlan mode: %v", macvlanMode, err)
} }
// verify the Docker host interface acting as the macvlan parent iface exists // verify the Docker host interface acting as the macvlan parent iface exists
if ok := hostIfaceExists(hostIface); !ok { if !parentExists(parent) {
return "", fmt.Errorf("the requested host interface %s was not found on the Docker host", hostIface) return "", fmt.Errorf("the requested parent interface %s was not found on the Docker host", parent)
} }
// Get the link for the master index (Example: the docker host eth iface) // Get the link for the master index (Example: the docker host eth iface)
hostEth, err := netlink.LinkByName(hostIface) parentLink, err := netlink.LinkByName(parent)
if err != nil { if err != nil {
logrus.Errorf("error occoured looking up the parent iface %s mode: %s error: %s", hostIface, macvlanMode, err) return "", fmt.Errorf("error occoured looking up the %s parent iface %s error: %s", macvlanType, parent, err)
} }
// Create a macvlan link // Create a macvlan link
macvlan := &netlink.Macvlan{ macvlan := &netlink.Macvlan{
LinkAttrs: netlink.LinkAttrs{ LinkAttrs: netlink.LinkAttrs{
Name: containerIfName, Name: containerIfName,
ParentIndex: hostEth.Attrs().Index, ParentIndex: parentLink.Attrs().Index,
}, },
Mode: mode, Mode: mode,
} }
if err := netlink.LinkAdd(macvlan); err != nil { if err := netlink.LinkAdd(macvlan); err != nil {
// verbose but will be an issue if user creates a macvlan and ipvlan on same parent.Netlink msg is uninformative // If a user creates a macvlan and ipvlan on same parent, only one slave iface can be active at a time.
logrus.Warn("Ensure there are no ipvlan networks using the same `-o host_iface` as the macvlan network.") return "", fmt.Errorf("failed to create the %s port: %v", macvlanType, err)
return "", fmt.Errorf("Failed to create macvlan link: %s with the error: %v", macvlan.Name, err)
} }
return macvlan.Attrs().Name, nil return macvlan.Attrs().Name, nil
} }
@ -63,12 +68,13 @@ func setMacVlanMode(mode string) (netlink.MacvlanMode, error) {
} }
} }
// validateHostIface check if the specified interface exists in the default namespace // parentExists check if the specified interface exists in the default namespace
func hostIfaceExists(ifaceStr string) bool { func parentExists(ifaceStr string) bool {
_, err := netlink.LinkByName(ifaceStr) _, err := netlink.LinkByName(ifaceStr)
if err != nil { if err != nil {
return false return false
} }
return true return true
} }
@ -87,13 +93,14 @@ func kernelSupport(networkTpe string) error {
return nil return nil
} }
} }
return fmt.Errorf("required kernel module '%s' was not found in /proc/modules, kernel version >= 3.19 is recommended", macvlanType)
return fmt.Errorf("unable to load the Linux kernel module %s", macvlanType)
} }
// createVlanLink parses sub-interfaces and vlan id for creation // createVlanLink parses sub-interfaces and vlan id for creation
func createVlanLink(ifaceName string) error { func createVlanLink(parentName string) error {
if strings.Contains(ifaceName, ".") { if strings.Contains(parentName, ".") {
parentIface, vidInt, err := parseVlan(ifaceName) parent, vidInt, err := parseVlan(parentName)
if err != nil { if err != nil {
return err return err
} }
@ -102,74 +109,124 @@ func createVlanLink(ifaceName string) error {
return fmt.Errorf("vlan id must be between 1-4094, received: %d", vidInt) return fmt.Errorf("vlan id must be between 1-4094, received: %d", vidInt)
} }
// get the parent link to attach a vlan subinterface // get the parent link to attach a vlan subinterface
hostIface, err := netlink.LinkByName(parentIface) parentLink, err := netlink.LinkByName(parent)
if err != nil { if err != nil {
return fmt.Errorf("failed to find master interface %s on the Docker host: %v", parentIface, err) return fmt.Errorf("failed to find master interface %s on the Docker host: %v", parent, err)
} }
vlanIface := &netlink.Vlan{ vlanLink := &netlink.Vlan{
LinkAttrs: netlink.LinkAttrs{ LinkAttrs: netlink.LinkAttrs{
Name: ifaceName, Name: parentName,
ParentIndex: hostIface.Attrs().Index, ParentIndex: parentLink.Attrs().Index,
}, },
VlanId: vidInt, VlanId: vidInt,
} }
// create the subinterface // create the subinterface
if err := netlink.LinkAdd(vlanIface); err != nil { if err := netlink.LinkAdd(vlanLink); err != nil {
return fmt.Errorf("failed to create %s vlan link: %v", vlanIface.Name, err) return fmt.Errorf("failed to create %s vlan link: %v", vlanLink.Name, err)
} }
// Bring the new netlink iface up // Bring the new netlink iface up
if err := netlink.LinkSetUp(vlanIface); err != nil { if err := netlink.LinkSetUp(vlanLink); err != nil {
return fmt.Errorf("failed to enable %s the macvlan netlink link: %v", vlanIface.Name, err) return fmt.Errorf("failed to enable %s the macvlan parent link %v", vlanLink.Name, err)
} }
logrus.Debugf("Added a vlan tagged netlink subinterface: %s with a vlan id: %d", ifaceName, vidInt) logrus.Debugf("Added a vlan tagged netlink subinterface: %s with a vlan id: %d", parentName, vidInt)
return nil return nil
} }
return fmt.Errorf("invalid subinterface vlan name %s, example formatting is eth0.10", ifaceName)
return fmt.Errorf("invalid subinterface vlan name %s, example formatting is eth0.10", parentName)
} }
// verifyVlanDel verifies only sub-interfaces with a vlan id get deleted // delVlanLink verifies only sub-interfaces with a vlan id get deleted
func delVlanLink(ifaceName string) error { func delVlanLink(linkName string) error {
if strings.Contains(ifaceName, ".") { if strings.Contains(linkName, ".") {
_, _, err := parseVlan(ifaceName) _, _, err := parseVlan(linkName)
if err != nil { if err != nil {
return err return err
} }
// delete the vlan subinterface // delete the vlan subinterface
vlanIface, err := netlink.LinkByName(ifaceName) vlanLink, err := netlink.LinkByName(linkName)
if err != nil { if err != nil {
return fmt.Errorf("failed to find interface %s on the Docker host : %v", ifaceName, err) return fmt.Errorf("failed to find interface %s on the Docker host : %v", linkName, err)
} }
// verify a parent interface isn't being deleted // verify a parent interface isn't being deleted
if vlanIface.Attrs().ParentIndex == 0 { if vlanLink.Attrs().ParentIndex == 0 {
return fmt.Errorf("interface %s does not appear to be a slave device: %v", ifaceName, err) return fmt.Errorf("interface %s does not appear to be a slave device: %v", linkName, err)
} }
// delete the macvlan slave device // delete the macvlan slave device
if err := netlink.LinkDel(vlanIface); err != nil { if err := netlink.LinkDel(vlanLink); err != nil {
return fmt.Errorf("failed to delete %s link: %v", ifaceName, err) return fmt.Errorf("failed to delete %s link: %v", linkName, err)
} }
logrus.Debugf("Deleted a vlan tagged netlink subinterface: %s", ifaceName) logrus.Debugf("Deleted a vlan tagged netlink subinterface: %s", linkName)
} }
// if the subinterface doesn't parse to iface.vlan_id leave the interface in // if the subinterface doesn't parse to iface.vlan_id leave the interface in
// place since it could be a user specified name not created by the driver. // place since it could be a user specified name not created by the driver.
return nil return nil
} }
// parseVlan parses and verifies a slave interface name: -o host_iface=eth0.10 // parseVlan parses and verifies a slave interface name: -o parent=eth0.10
func parseVlan(ifaceName string) (string, int, error) { func parseVlan(linkName string) (string, int, error) {
// parse -o host_iface=eth0.10 // parse -o parent=eth0.10
splitIface := strings.Split(ifaceName, ".") splitName := strings.Split(linkName, ".")
if len(splitIface) != 2 { if len(splitName) != 2 {
return "", 0, fmt.Errorf("required interface name format is: name.vlan_id, ex. eth0.10 for vlan 10, instead received %s", ifaceName) return "", 0, fmt.Errorf("required interface name format is: name.vlan_id, ex. eth0.10 for vlan 10, instead received %s", linkName)
} }
parentIface, vidStr := splitIface[0], splitIface[1] parent, vidStr := splitName[0], splitName[1]
// validate type and convert vlan id to int // validate type and convert vlan id to int
vidInt, err := strconv.Atoi(vidStr) vidInt, err := strconv.Atoi(vidStr)
if err != nil { if err != nil {
return "", 0, fmt.Errorf("unable to parse a valid vlan id from: %s (ex. eth0.10 for vlan 10)", vidStr) return "", 0, fmt.Errorf("unable to parse a valid vlan id from: %s (ex. eth0.10 for vlan 10)", vidStr)
} }
// Check if the interface exists // Check if the interface exists
if ok := hostIfaceExists(parentIface); !ok { if !parentExists(parent) {
return "", 0, fmt.Errorf("-o host_iface parent interface does was not found on the host: %s", parentIface) return "", 0, fmt.Errorf("-o parent interface does was not found on the host: %s", parent)
} }
return parentIface, vidInt, nil
return parent, vidInt, nil
}
// createDummyLink creates a dummy0 parent link
func createDummyLink(dummyName, truncNetID string) error {
// create a parent interface since one was not specified
parent := &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Name: dummyName,
},
}
if err := netlink.LinkAdd(parent); err != nil {
return err
}
parentDummyLink, err := netlink.LinkByName(dummyName)
if err != nil {
return fmt.Errorf("error occoured looking up the %s parent iface %s error: %s", macvlanType, dummyName, err)
}
// bring the new netlink iface up
if err := netlink.LinkSetUp(parentDummyLink); err != nil {
return fmt.Errorf("failed to enable %s the macvlan parent link: %v", dummyName, err)
}
return nil
}
// delDummyLink deletes the link type dummy used when -o parent is not passed
func delDummyLink(linkName string) error {
// delete the vlan subinterface
dummyLink, err := netlink.LinkByName(linkName)
if err != nil {
return fmt.Errorf("failed to find link %s on the Docker host : %v", linkName, err)
}
// verify a parent interface is being deleted
if dummyLink.Attrs().ParentIndex != 0 {
return fmt.Errorf("link %s is not a parent dummy interface", linkName)
}
// delete the macvlan dummy device
if err := netlink.LinkDel(dummyLink); err != nil {
return fmt.Errorf("failed to delete the dummy %s link: %v", linkName, err)
}
logrus.Debugf("Deleted a dummy parent link: %s", linkName)
return nil
}
// getDummyName returns the name of a dummy parent with truncated net ID and driver prefix
func getDummyName(netID string) string {
return fmt.Sprintf("%s%s", dummyPrefix, netID)
} }

View file

@ -6,17 +6,17 @@ import (
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
) )
// TestValidateLink tests the validateHostIface function // TestValidateLink tests the parentExists function
func TestValidateLink(t *testing.T) { func TestValidateLink(t *testing.T) {
validIface := "lo" validIface := "lo"
invalidIface := "foo12345" invalidIface := "foo12345"
// test a valid parent interface validation // test a valid parent interface validation
if ok := hostIfaceExists(validIface); !ok { if ok := parentExists(validIface); !ok {
t.Fatalf("failed validating loopback %s", validIface) t.Fatalf("failed validating loopback %s", validIface)
} }
// test a invalid parent interface validation // test a invalid parent interface validation
if ok := hostIfaceExists(invalidIface); ok { if ok := parentExists(invalidIface); ok {
t.Fatalf("failed to invalidate interface %s", invalidIface) t.Fatalf("failed to invalidate interface %s", invalidIface)
} }
} }

View file

@ -15,6 +15,7 @@ func (d *driver) network(nid string) *network {
if !ok { if !ok {
logrus.Errorf("network id %s not found", nid) logrus.Errorf("network id %s not found", nid)
} }
return n return n
} }
@ -39,6 +40,7 @@ func (d *driver) getNetworks() []*network {
for _, nw := range d.networks { for _, nw := range d.networks {
ls = append(ls, nw) ls = append(ls, nw)
} }
return ls return ls
} }
@ -70,6 +72,7 @@ func (n *network) getEndpoint(eid string) (*endpoint, error) {
if ep, ok := n.endpoints[eid]; ok { if ep, ok := n.endpoints[eid]; ok {
return ep, nil return ep, nil
} }
return nil, nil return nil, nil
} }
@ -86,6 +89,7 @@ func validateID(nid, eid string) error {
func (n *network) sandbox() osl.Sandbox { func (n *network) sandbox() osl.Sandbox {
n.Lock() n.Lock()
defer n.Unlock() defer n.Unlock()
return n.sbox return n.sbox
} }
@ -104,5 +108,6 @@ func (d *driver) getNetwork(id string) (*network, error) {
if nw, ok := d.networks[id]; ok { if nw, ok := d.networks[id]; ok {
return nw, nil return nw, nil
} }
return nil, types.NotFoundErrorf("network not found: %s", id) return nil, types.NotFoundErrorf("network not found: %s", id)
} }

View file

@ -21,7 +21,7 @@ type configuration struct {
dbIndex uint64 dbIndex uint64
dbExists bool dbExists bool
Internal bool Internal bool
HostIface string Parent string
MacvlanMode string MacvlanMode string
CreatedSlaveLink bool CreatedSlaveLink bool
Ipv4Subnets []*ipv4Subnet Ipv4Subnets []*ipv4Subnet
@ -114,8 +114,9 @@ func (config *configuration) MarshalJSON() ([]byte, error) {
nMap := make(map[string]interface{}) nMap := make(map[string]interface{})
nMap["ID"] = config.ID nMap["ID"] = config.ID
nMap["Mtu"] = config.Mtu nMap["Mtu"] = config.Mtu
nMap["HostIface"] = config.HostIface nMap["Parent"] = config.Parent
nMap["MacvlanMode"] = config.MacvlanMode nMap["MacvlanMode"] = config.MacvlanMode
nMap["Internal"] = config.Internal
nMap["CreatedSubIface"] = config.CreatedSlaveLink nMap["CreatedSubIface"] = config.CreatedSlaveLink
if len(config.Ipv4Subnets) > 0 { if len(config.Ipv4Subnets) > 0 {
iis, err := json.Marshal(config.Ipv4Subnets) iis, err := json.Marshal(config.Ipv4Subnets)
@ -131,6 +132,7 @@ func (config *configuration) MarshalJSON() ([]byte, error) {
} }
nMap["Ipv6Subnets"] = string(iis) nMap["Ipv6Subnets"] = string(iis)
} }
return json.Marshal(nMap) return json.Marshal(nMap)
} }
@ -145,8 +147,9 @@ func (config *configuration) UnmarshalJSON(b []byte) error {
} }
config.ID = nMap["ID"].(string) config.ID = nMap["ID"].(string)
config.Mtu = int(nMap["Mtu"].(float64)) config.Mtu = int(nMap["Mtu"].(float64))
config.HostIface = nMap["HostIface"].(string) config.Parent = nMap["Parent"].(string)
config.MacvlanMode = nMap["MacvlanMode"].(string) config.MacvlanMode = nMap["MacvlanMode"].(string)
config.Internal = nMap["Internal"].(bool)
config.CreatedSlaveLink = nMap["CreatedSubIface"].(bool) config.CreatedSlaveLink = nMap["CreatedSubIface"].(bool)
if v, ok := nMap["Ipv4Subnets"]; ok { if v, ok := nMap["Ipv4Subnets"]; ok {
if err := json.Unmarshal([]byte(v.(string)), &config.Ipv4Subnets); err != nil { if err := json.Unmarshal([]byte(v.(string)), &config.Ipv4Subnets); err != nil {
@ -158,6 +161,7 @@ func (config *configuration) UnmarshalJSON(b []byte) error {
return err return err
} }
} }
return nil return nil
} }
@ -174,6 +178,7 @@ func (config *configuration) Value() []byte {
if err != nil { if err != nil {
return nil return nil
} }
return b return b
} }
@ -205,6 +210,7 @@ func (config *configuration) New() datastore.KVObject {
func (config *configuration) CopyTo(o datastore.KVObject) error { func (config *configuration) CopyTo(o datastore.KVObject) error {
dstNcfg := o.(*configuration) dstNcfg := o.(*configuration)
*dstNcfg = *config *dstNcfg = *config
return nil return nil
} }

View file

@ -0,0 +1,15 @@
// +build experimental
package libnetwork
import (
"github.com/docker/libnetwork/drivers/ipvlan"
"github.com/docker/libnetwork/drivers/macvlan"
)
func additionalDrivers() []initializer {
return []initializer{
{macvlan.Init, "macvlan"},
{ipvlan.Init, "ipvlan"},
}
}

View file

@ -3,21 +3,20 @@ package libnetwork
import ( import (
"github.com/docker/libnetwork/drivers/bridge" "github.com/docker/libnetwork/drivers/bridge"
"github.com/docker/libnetwork/drivers/host" "github.com/docker/libnetwork/drivers/host"
"github.com/docker/libnetwork/drivers/ipvlan"
"github.com/docker/libnetwork/drivers/macvlan"
"github.com/docker/libnetwork/drivers/null" "github.com/docker/libnetwork/drivers/null"
"github.com/docker/libnetwork/drivers/overlay" "github.com/docker/libnetwork/drivers/overlay"
"github.com/docker/libnetwork/drivers/remote" "github.com/docker/libnetwork/drivers/remote"
) )
func getInitializers() []initializer { func getInitializers() []initializer {
return []initializer{ in := []initializer{
{bridge.Init, "bridge"}, {bridge.Init, "bridge"},
{host.Init, "host"}, {host.Init, "host"},
{null.Init, "null"}, {null.Init, "null"},
{remote.Init, "remote"}, {remote.Init, "remote"},
{overlay.Init, "overlay"}, {overlay.Init, "overlay"},
{macvlan.Init, "macvlan"},
{ipvlan.Init, "ipvlan"},
} }
in = append(in, additionalDrivers()...)
return in
} }

View file

@ -0,0 +1,7 @@
// +build !experimental
package libnetwork
func additionalDrivers() []initializer {
return nil
}