From ed9601e4c65554ab7e88a0d0dd3b8954cf0dbade Mon Sep 17 00:00:00 2001 From: Madhu Venugopal Date: Wed, 2 Mar 2016 17:13:15 -0800 Subject: [PATCH] Make macvlan and ipvlan drivers as experimental Signed-off-by: Madhu Venugopal --- libnetwork/Makefile | 2 +- libnetwork/drivers/ipvlan/ipvlan.go | 17 +- libnetwork/drivers/ipvlan/ipvlan_endpoint.go | 6 +- libnetwork/drivers/ipvlan/ipvlan_joinleave.go | 23 ++- libnetwork/drivers/ipvlan/ipvlan_network.go | 104 ++++++++---- libnetwork/drivers/ipvlan/ipvlan_setup.go | 160 ++++++++++++------ .../drivers/ipvlan/ipvlan_setup_test.go | 6 +- libnetwork/drivers/ipvlan/ipvlan_state.go | 6 + libnetwork/drivers/ipvlan/ipvlan_store.go | 10 +- libnetwork/drivers/macvlan/macvlan.go | 19 +-- .../drivers/macvlan/macvlan_endpoint.go | 9 +- .../drivers/macvlan/macvlan_joinleave.go | 14 +- libnetwork/drivers/macvlan/macvlan_network.go | 115 +++++++++---- libnetwork/drivers/macvlan/macvlan_setup.go | 153 +++++++++++------ .../drivers/macvlan/macvlan_setup_test.go | 6 +- libnetwork/drivers/macvlan/macvlan_state.go | 5 + libnetwork/drivers/macvlan/macvlan_store.go | 12 +- libnetwork/drivers_experimental_linux.go | 15 ++ libnetwork/drivers_linux.go | 9 +- libnetwork/drivers_stub_linux.go | 7 + 20 files changed, 473 insertions(+), 225 deletions(-) create mode 100644 libnetwork/drivers_experimental_linux.go create mode 100644 libnetwork/drivers_stub_linux.go diff --git a/libnetwork/Makefile b/libnetwork/Makefile index 096d7fcc21..ec40b694f2 100644 --- a/libnetwork/Makefile +++ b/libnetwork/Makefile @@ -23,7 +23,7 @@ build: ${build_image}.created build-local: @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: @if [ -d bin ]; then \ diff --git a/libnetwork/drivers/ipvlan/ipvlan.go b/libnetwork/drivers/ipvlan/ipvlan.go index 6bb3d689f3..047c531ee8 100644 --- a/libnetwork/drivers/ipvlan/ipvlan.go +++ b/libnetwork/drivers/ipvlan/ipvlan.go @@ -4,7 +4,6 @@ import ( "net" "sync" - "github.com/Sirupsen/logrus" "github.com/docker/libnetwork/datastore" "github.com/docker/libnetwork/discoverapi" "github.com/docker/libnetwork/driverapi" @@ -15,14 +14,14 @@ const ( vethLen = 7 containerVethPrefix = "eth" vethPrefix = "veth" - ipvlanType = "ipvlan" // driver type name - modeL2 = "l2" // ipvlan mode l2 is the default - modeL3 = "l3" // ipvlan L3 mode - hostIfaceOpt = "host_iface" // host interface -o host_iface - modeOpt = "_mode" // ipvlan mode ux opt suffix + ipvlanType = "ipvlan" // driver type name + modeL2 = "l2" // ipvlan mode l2 is the default + modeL3 = "l3" // ipvlan L3 mode + parentOpt = "parent" // parent interface -o parent + 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 @@ -54,9 +53,6 @@ type network struct { // Init initializes and registers the libnetwork ipvlan driver 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{ DataScope: datastore.LocalScope, } @@ -64,6 +60,7 @@ func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { networks: networkTable{}, } d.initStore(config) + return dc.RegisterDriver(ipvlanType, d, c) } diff --git a/libnetwork/drivers/ipvlan/ipvlan_endpoint.go b/libnetwork/drivers/ipvlan/ipvlan_endpoint.go index 6a049803a0..e3d724a3e5 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_endpoint.go +++ b/libnetwork/drivers/ipvlan/ipvlan_endpoint.go @@ -3,6 +3,7 @@ package ipvlan import ( "fmt" + "github.com/Sirupsen/logrus" "github.com/docker/libnetwork/driverapi" "github.com/docker/libnetwork/netlabel" "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 _, ok := opt.([]types.PortBinding); ok { 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 _, ok := opt.([]types.TransportPort); ok { 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 { netlink.LinkDel(link) } + return nil } diff --git a/libnetwork/drivers/ipvlan/ipvlan_joinleave.go b/libnetwork/drivers/ipvlan/ipvlan_joinleave.go index 1d17a76648..ee24d37bc6 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_joinleave.go +++ b/libnetwork/drivers/ipvlan/ipvlan_joinleave.go @@ -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) } // 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 { 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 { 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", - ep.addr.IP.String(), n.config.IpvlanMode, n.config.HostIface) + logrus.Debugf("Ipvlan Endpoint Joined with IPv4_Addr: %s, Ipvlan_Mode: %s, Parent: %s", + ep.addr.IP.String(), n.config.IpvlanMode, n.config.Parent) // If the endpoint has a v6 address, set a v6 default route if ep.addrv6 != nil { 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 { 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", - ep.addrv6.IP.String(), n.config.IpvlanMode, n.config.HostIface) + logrus.Debugf("Ipvlan Endpoint Joined with IPv6_Addr: %s, Ipvlan_Mode: %s, Parent: %s", + ep.addrv6.IP.String(), n.config.IpvlanMode, n.config.Parent) } } if n.config.IpvlanMode == modeL2 { @@ -89,8 +89,8 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, if err != nil { return err } - logrus.Debugf("Ipvlan Endpoint Joined with IPv4_Addr: %s, Gateway: %s, Ipvlan_Mode: %s, Host_Iface: %s", - ep.addr.IP.String(), v4gw.String(), n.config.IpvlanMode, n.config.HostIface) + 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.Parent) } // parse and correlate the endpoint v6 address with the available v6 subnets 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 { return err } - logrus.Debugf("Ipvlan Endpoint Joined with IPv6_Addr: %s, Gateway: %s, Ipvlan_Mode: %s, Host_Iface: %s", - ep.addrv6.IP.String(), v6gw.String(), n.config.IpvlanMode, n.config.HostIface) + 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.Parent) } } iNames := jinfo.InterfaceName() @@ -115,6 +115,7 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, if err != nil { return err } + return nil } @@ -131,6 +132,7 @@ func (d *driver) Leave(nid, eid string) error { if endpoint == nil { return fmt.Errorf("could not find endpoint with id %s", eid) } + return nil } @@ -145,6 +147,7 @@ func ifaceGateway(dfNet string) (*staticRoute, error) { RouteType: types.CONNECTED, NextHop: nh, } + return defaultRoute, nil } @@ -165,6 +168,7 @@ func (n *network) getSubnetforIPv4(ip *net.IPNet) *ipv4Subnet { return s } } + return nil } @@ -185,5 +189,6 @@ func (n *network) getSubnetforIPv6(ip *net.IPNet) *ipv6Subnet { return s } } + return nil } diff --git a/libnetwork/drivers/ipvlan/ipvlan_network.go b/libnetwork/drivers/ipvlan/ipvlan_network.go index 3ee9c9f63a..c0fa276190 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_network.go +++ b/libnetwork/drivers/ipvlan/ipvlan_network.go @@ -12,14 +12,14 @@ import ( ) // 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 - config, err := parseNetworkOptions(id, option) + config, err := parseNetworkOptions(nid, option) if err != nil { return err } - config.ID = id - err = config.processIPAM(id, ipV4Data, ipV6Data) + config.ID = nid + err = config.processIPAM(nid, ipV4Data, ipV6Data) if err != nil { return err } @@ -33,14 +33,16 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Dat default: 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 - if config.HostIface == "lo" { + if config.Parent == "lo" { 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) if err != nil { return err @@ -49,6 +51,7 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Dat err = d.storeUpdate(config) if err != nil { d.deleteNetwork(config.ID) + logrus.Debugf("encoutered an error rolling back a network create for %s : %v", config.ID, 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 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() for _, nw := range networkList { - if config.HostIface == nw.config.HostIface { - return fmt.Errorf("network %s is already using host interface %s", - stringid.TruncateID(nw.config.ID), config.HostIface) + if config.Parent == nw.config.Parent { + return fmt.Errorf("network %s is already using parent interface %s", + 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 ok := hostIfaceExists(config.HostIface); !ok { - // 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.HostIface) - if err != nil { - return err + if !parentExists(config.Parent) { + // if the --internal flag is set, create a dummy link + if config.Internal { + err := createDummyLink(config.Parent, getDummyName(stringid.TruncateID(config.ID))) + if err != nil { + 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{ id: config.ID, @@ -90,20 +110,38 @@ func (d *driver) createNetwork(config *configuration) error { // DeleteNetwork the network for the specified driver type func (d *driver) DeleteNetwork(nid string) error { 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 ok := n.config.CreatedSlaveLink; ok { - // if the interface exists, only delete if it matches iface.vlan naming - if ok := hostIfaceExists(n.config.HostIface); ok { - err := delVlanLink(n.config.HostIface) - if err != nil { - logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v", n.config.HostIface, err) + // if the interface exists, only delete if it matches iface.vlan or dummy.net_id naming + if ok := parentExists(n.config.Parent); ok { + // only delete the link if it is named the net_id + if n.config.Parent == getDummyName(stringid.TruncateID(nid)) { + 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 d.deleteNetwork(nid) // 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 @@ -118,9 +156,11 @@ func parseNetworkOptions(id string, option options.Generic) (*configuration, 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 { - 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 } @@ -152,9 +192,9 @@ func parseNetworkGenericOptions(data interface{}) (*configuration, error) { func (config *configuration) fromOptions(labels map[string]string) error { for label, value := range labels { switch label { - case hostIfaceOpt: - // parse driver option '-o host_iface' - config.HostIface = value + case parentOpt: + // parse driver option '-o parent' + config.Parent = value case driverModeOpt: // parse driver option '-o ipvlan_mode' config.IpvlanMode = value diff --git a/libnetwork/drivers/ipvlan/ipvlan_setup.go b/libnetwork/drivers/ipvlan/ipvlan_setup.go index 2f4063e8a5..3f8f4c2757 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_setup.go +++ b/libnetwork/drivers/ipvlan/ipvlan_setup.go @@ -13,32 +13,42 @@ import ( "github.com/vishvananda/netlink" ) -// Create the ipvlan netlink interface specifying the source name -func createIPVlan(containerIfName, hostIface, ipVlanMode string) (string, error) { +const ( + 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()() - // 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 { - 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) - hostEth, err := netlink.LinkByName(hostIface) + parentLink, err := netlink.LinkByName(parent) 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 ipvlan := &netlink.IPVlan{ LinkAttrs: netlink.LinkAttrs{ Name: containerIfName, - ParentIndex: hostEth.Attrs().Index, + ParentIndex: parentLink.Attrs().Index, }, Mode: mode, } 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 - logrus.Warn("Ensure there are no macvlan networks using the same `-o host_iface` as the ipvlan network.") - return "", fmt.Errorf("Failed to create ipvlan link: %s with the error: %v", ipvlan.Name, err) + // If a user creates a macvlan and ipvlan on same parent, only one slave iface can be active at a time. + return "", fmt.Errorf("failed to create the %s port: %v", ipvlanType, err) } + 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 -func hostIfaceExists(ifaceStr string) bool { +// parentExists check if the specified interface exists in the default namespace +func parentExists(ifaceStr string) bool { _, err := netlink.LinkByName(ifaceStr) if err != nil { return false } + return true } @@ -78,13 +89,14 @@ func kernelSupport(networkTpe string) error { 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 -func createVlanLink(ifaceName string) error { - if strings.Contains(ifaceName, ".") { - parentIface, vidInt, err := parseVlan(ifaceName) +func createVlanLink(parentName string) error { + if strings.Contains(parentName, ".") { + parent, vidInt, err := parseVlan(parentName) if err != nil { return err } @@ -93,74 +105,124 @@ func createVlanLink(ifaceName string) error { return fmt.Errorf("vlan id must be between 1-4094, received: %d", vidInt) } // get the parent link to attach a vlan subinterface - hostIface, err := netlink.LinkByName(parentIface) + parentLink, err := netlink.LinkByName(parent) 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{ - Name: ifaceName, - ParentIndex: hostIface.Attrs().Index, + Name: parentName, + ParentIndex: parentLink.Attrs().Index, }, VlanId: vidInt, } // create the subinterface - if err := netlink.LinkAdd(vlanIface); err != nil { - return fmt.Errorf("failed to create %s vlan link: %v", vlanIface.Name, err) + if err := netlink.LinkAdd(vlanLink); err != nil { + return fmt.Errorf("failed to create %s vlan link: %v", vlanLink.Name, err) } // Bring the new netlink iface up - if err := netlink.LinkSetUp(vlanIface); err != nil { - return fmt.Errorf("failed to enable %s the ipvlan netlink link: %v", vlanIface.Name, err) + if err := netlink.LinkSetUp(vlanLink); err != nil { + 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 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 -func delVlanLink(ifaceName string) error { - if strings.Contains(ifaceName, ".") { - _, _, err := parseVlan(ifaceName) +// delVlanLink verifies only sub-interfaces with a vlan id get deleted +func delVlanLink(linkName string) error { + if strings.Contains(linkName, ".") { + _, _, err := parseVlan(linkName) if err != nil { return err } // delete the vlan subinterface - vlanIface, err := netlink.LinkByName(ifaceName) + vlanLink, err := netlink.LinkByName(linkName) 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 - if vlanIface.Attrs().ParentIndex == 0 { - return fmt.Errorf("interface %s does not appear to be a slave device: %v", ifaceName, err) + if vlanLink.Attrs().ParentIndex == 0 { + return fmt.Errorf("interface %s does not appear to be a slave device: %v", linkName, err) } // delete the ipvlan slave device - if err := netlink.LinkDel(vlanIface); err != nil { - return fmt.Errorf("failed to delete %s link: %v", ifaceName, err) + if err := netlink.LinkDel(vlanLink); err != nil { + 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 // place since it could be a user specified name not created by the driver. return nil } -// parseVlan parses and verifies a slave interface name: -o host_iface=eth0.10 -func parseVlan(ifaceName string) (string, int, error) { - // parse -o host_iface=eth0.10 - splitIface := strings.Split(ifaceName, ".") - if len(splitIface) != 2 { - return "", 0, fmt.Errorf("required interface name format is: name.vlan_id, ex. eth0.10 for vlan 10, instead received %s", ifaceName) +// parseVlan parses and verifies a slave interface name: -o parent=eth0.10 +func parseVlan(linkName string) (string, int, error) { + // parse -o parent=eth0.10 + splitName := strings.Split(linkName, ".") + 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", linkName) } - parentIface, vidStr := splitIface[0], splitIface[1] + parent, vidStr := splitName[0], splitName[1] // validate type and convert vlan id to int vidInt, err := strconv.Atoi(vidStr) if err != nil { 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 - if ok := hostIfaceExists(parentIface); !ok { - return "", 0, fmt.Errorf("-o host_iface parent interface does was not found on the host: %s", parentIface) + if !parentExists(parent) { + 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) } diff --git a/libnetwork/drivers/ipvlan/ipvlan_setup_test.go b/libnetwork/drivers/ipvlan/ipvlan_setup_test.go index 35ea289030..098a6ecd73 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_setup_test.go +++ b/libnetwork/drivers/ipvlan/ipvlan_setup_test.go @@ -6,17 +6,17 @@ import ( "github.com/vishvananda/netlink" ) -// TestValidateLink tests the validateHostIface function +// TestValidateLink tests the parentExists function func TestValidateLink(t *testing.T) { validIface := "lo" invalidIface := "foo12345" // test a valid parent interface validation - if ok := hostIfaceExists(validIface); !ok { + if ok := parentExists(validIface); !ok { t.Fatalf("failed validating loopback %s", validIface) } // test a invalid parent interface validation - if ok := hostIfaceExists(invalidIface); ok { + if ok := parentExists(invalidIface); ok { t.Fatalf("failed to invalidate interface %s", invalidIface) } } diff --git a/libnetwork/drivers/ipvlan/ipvlan_state.go b/libnetwork/drivers/ipvlan/ipvlan_state.go index d9798c0e1b..2d8cb2d8f6 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_state.go +++ b/libnetwork/drivers/ipvlan/ipvlan_state.go @@ -15,6 +15,7 @@ func (d *driver) network(nid string) *network { if !ok { logrus.Errorf("network id %s not found", nid) } + return n } @@ -39,6 +40,7 @@ func (d *driver) getNetworks() []*network { for _, nw := range d.networks { ls = append(ls, nw) } + return ls } @@ -70,6 +72,7 @@ func (n *network) getEndpoint(eid string) (*endpoint, error) { if ep, ok := n.endpoints[eid]; ok { return ep, nil } + return nil, nil } @@ -80,12 +83,14 @@ func validateID(nid, eid string) error { if eid == "" { return fmt.Errorf("invalid endpoint id") } + return nil } func (n *network) sandbox() osl.Sandbox { n.Lock() defer n.Unlock() + return n.sbox } @@ -105,5 +110,6 @@ func (d *driver) getNetwork(id string) (*network, error) { if nw, ok := d.networks[id]; ok { return nw, nil } + return nil, types.NotFoundErrorf("network not found: %s", id) } diff --git a/libnetwork/drivers/ipvlan/ipvlan_store.go b/libnetwork/drivers/ipvlan/ipvlan_store.go index e21b280a88..f6746da670 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_store.go +++ b/libnetwork/drivers/ipvlan/ipvlan_store.go @@ -21,7 +21,7 @@ type configuration struct { dbIndex uint64 dbExists bool Internal bool - HostIface string + Parent string IpvlanMode string CreatedSlaveLink bool Ipv4Subnets []*ipv4Subnet @@ -114,8 +114,9 @@ func (config *configuration) MarshalJSON() ([]byte, error) { nMap := make(map[string]interface{}) nMap["ID"] = config.ID nMap["Mtu"] = config.Mtu - nMap["HostIface"] = config.HostIface + nMap["Parent"] = config.Parent nMap["IpvlanMode"] = config.IpvlanMode + nMap["Internal"] = config.Internal nMap["CreatedSubIface"] = config.CreatedSlaveLink if len(config.Ipv4Subnets) > 0 { iis, err := json.Marshal(config.Ipv4Subnets) @@ -131,6 +132,7 @@ func (config *configuration) MarshalJSON() ([]byte, error) { } nMap["Ipv6Subnets"] = string(iis) } + return json.Marshal(nMap) } @@ -145,8 +147,9 @@ func (config *configuration) UnmarshalJSON(b []byte) error { } config.ID = nMap["ID"].(string) config.Mtu = int(nMap["Mtu"].(float64)) - config.HostIface = nMap["HostIface"].(string) + config.Parent = nMap["Parent"].(string) config.IpvlanMode = nMap["IpvlanMode"].(string) + config.Internal = nMap["Internal"].(bool) config.CreatedSlaveLink = nMap["CreatedSubIface"].(bool) if v, ok := nMap["Ipv4Subnets"]; ok { 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 nil } diff --git a/libnetwork/drivers/macvlan/macvlan.go b/libnetwork/drivers/macvlan/macvlan.go index f45a5cbea3..502f166fd1 100644 --- a/libnetwork/drivers/macvlan/macvlan.go +++ b/libnetwork/drivers/macvlan/macvlan.go @@ -4,7 +4,6 @@ import ( "net" "sync" - "github.com/Sirupsen/logrus" "github.com/docker/libnetwork/datastore" "github.com/docker/libnetwork/discoverapi" "github.com/docker/libnetwork/driverapi" @@ -15,13 +14,13 @@ const ( vethLen = 7 containerVethPrefix = "eth" vethPrefix = "veth" - macvlanType = "macvlan" // driver type name - modePrivate = "private" // macvlan mode private - modeVepa = "vepa" // macvlan mode vepa - modeBridge = "bridge" // macvlan mode bridge - modePassthru = "passthru" // macvlan mode passthrough - hostIfaceOpt = "host_iface" // host interface -o host_iface - modeOpt = "_mode" // macvlan mode ux opt suffix + macvlanType = "macvlan" // driver type name + modePrivate = "private" // macvlan mode private + modeVepa = "vepa" // macvlan mode vepa + modeBridge = "bridge" // macvlan mode bridge + modePassthru = "passthru" // macvlan mode passthrough + parentOpt = "parent" // parent interface -o parent + modeOpt = "_mode" // macvlan mode ux opt suffix ) var driverModeOpt = macvlanType + modeOpt // mode --option macvlan_mode @@ -56,9 +55,6 @@ type network struct { // Init initializes and registers the libnetwork macvlan driver 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{ DataScope: datastore.LocalScope, } @@ -66,6 +62,7 @@ func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { networks: networkTable{}, } d.initStore(config) + return dc.RegisterDriver(macvlanType, d, c) } diff --git a/libnetwork/drivers/macvlan/macvlan_endpoint.go b/libnetwork/drivers/macvlan/macvlan_endpoint.go index b78cbf96c5..7f99092222 100644 --- a/libnetwork/drivers/macvlan/macvlan_endpoint.go +++ b/libnetwork/drivers/macvlan/macvlan_endpoint.go @@ -3,6 +3,7 @@ package macvlan import ( "fmt" + "github.com/Sirupsen/logrus" "github.com/docker/libnetwork/driverapi" "github.com/docker/libnetwork/netlabel" "github.com/docker/libnetwork/netutils" @@ -21,9 +22,6 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo, if err != nil { 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{ id: eid, 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 _, ok := opt.([]types.PortBinding); ok { 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 _, ok := opt.([]types.TransportPort); ok { 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 { netlink.LinkDel(link) } + return nil } diff --git a/libnetwork/drivers/macvlan/macvlan_joinleave.go b/libnetwork/drivers/macvlan/macvlan_joinleave.go index 97a3586b79..d7eeda81d1 100644 --- a/libnetwork/drivers/macvlan/macvlan_joinleave.go +++ b/libnetwork/drivers/macvlan/macvlan_joinleave.go @@ -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) } // 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 { return err } @@ -51,8 +51,8 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, if err != nil { return err } - logrus.Debugf("Macvlan Endpoint Joined with IPv4_Addr: %s, Gateway: %s, MacVlan_Mode: %s, Host_Iface: %s", - ep.addr.IP.String(), v4gw.String(), n.config.MacvlanMode, n.config.HostIface) + 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.Parent) } // parse and match the endpoint address with the available v6 subnets 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 { return err } - logrus.Debugf("Macvlan Endpoint Joined with IPv6_Addr: %s Gateway: %s MacVlan_Mode: %s, Host_Iface: %s", - ep.addrv6.IP.String(), v6gw.String(), n.config.MacvlanMode, n.config.HostIface) + 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.Parent) } iNames := jinfo.InterfaceName() err = iNames.SetNames(vethName, containerVethPrefix) if err != nil { return err } + return nil } @@ -92,6 +93,7 @@ func (d *driver) Leave(nid, eid string) error { if endpoint == nil { return fmt.Errorf("could not find endpoint with id %s", eid) } + return nil } @@ -112,6 +114,7 @@ func (n *network) getSubnetforIPv4(ip *net.IPNet) *ipv4Subnet { return s } } + return nil } @@ -132,5 +135,6 @@ func (n *network) getSubnetforIPv6(ip *net.IPNet) *ipv6Subnet { return s } } + return nil } diff --git a/libnetwork/drivers/macvlan/macvlan_network.go b/libnetwork/drivers/macvlan/macvlan_network.go index d26b058057..3b46a335f8 100644 --- a/libnetwork/drivers/macvlan/macvlan_network.go +++ b/libnetwork/drivers/macvlan/macvlan_network.go @@ -12,25 +12,17 @@ import ( ) // 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 - config, err := parseNetworkOptions(id, option) + config, err := parseNetworkOptions(nid, option) if err != nil { return err } - config.ID = id - err = config.processIPAM(id, ipV4Data, ipV6Data) + config.ID = nid + err = config.processIPAM(nid, ipV4Data, ipV6Data) if err != nil { 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 switch config.MacvlanMode { case "", modeBridge: @@ -45,6 +37,16 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Dat default: 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) if err != nil { return err @@ -53,6 +55,7 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Dat err = d.storeUpdate(config) if err != nil { d.deleteNetwork(config.ID) + logrus.Debugf("encoutered an error rolling back a network create for %s : %v", config.ID, 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 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() for _, nw := range networkList { - if config.HostIface == nw.config.HostIface { - return fmt.Errorf("network %s is already using host interface %s", - stringid.TruncateID(nw.config.ID), config.HostIface) + if config.Parent == nw.config.Parent { + return fmt.Errorf("network %s is already using parent interface %s", + 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 ok := hostIfaceExists(config.HostIface); !ok { - // 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.HostIface) - if err != nil { - return err + if !parentExists(config.Parent) { + // if the --internal flag is set, create a dummy link + if config.Internal { + err := createDummyLink(config.Parent, getDummyName(stringid.TruncateID(config.ID))) + if err != nil { + 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{ id: config.ID, @@ -95,20 +114,38 @@ func (d *driver) createNetwork(config *configuration) error { // DeleteNetwork the network for the specified driver type func (d *driver) DeleteNetwork(nid string) error { 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 ok := n.config.CreatedSlaveLink; ok { - // if the interface exists, only delete if it matches iface.vlan naming - if ok := hostIfaceExists(n.config.HostIface); ok { - err := delVlanLink(n.config.HostIface) - if err != nil { - logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v", n.config.HostIface, err) + // if the interface exists, only delete if it matches iface.vlan or dummy.net_id naming + if ok := parentExists(n.config.Parent); ok { + // only delete the link if it is named the net_id + if n.config.Parent == getDummyName(stringid.TruncateID(nid)) { + 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 d.deleteNetwork(nid) // 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 @@ -123,10 +160,13 @@ func parseNetworkOptions(id string, option options.Generic) (*configuration, 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 { - 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 } @@ -150,6 +190,7 @@ func parseNetworkGenericOptions(data interface{}) (*configuration, error) { default: err = types.BadRequestErrorf("unrecognized network configuration format: %v", opt) } + return config, err } @@ -157,14 +198,15 @@ func parseNetworkGenericOptions(data interface{}) (*configuration, error) { func (config *configuration) fromOptions(labels map[string]string) error { for label, value := range labels { switch label { - case hostIfaceOpt: - // parse driver option '-o host_iface' - config.HostIface = value + case parentOpt: + // parse driver option '-o parent' + config.Parent = value case driverModeOpt: // parse driver option '-o macvlan_mode' config.MacvlanMode = value } } + return nil } @@ -188,5 +230,6 @@ func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []dri config.Ipv6Subnets = append(config.Ipv6Subnets, s) } } + return nil } diff --git a/libnetwork/drivers/macvlan/macvlan_setup.go b/libnetwork/drivers/macvlan/macvlan_setup.go index 76d4e8c6b7..d6d97a1b3a 100644 --- a/libnetwork/drivers/macvlan/macvlan_setup.go +++ b/libnetwork/drivers/macvlan/macvlan_setup.go @@ -13,8 +13,13 @@ import ( "github.com/vishvananda/netlink" ) -// Create the netlink interface specifying the source name -func createMacVlan(containerIfName, hostIface, macvlanMode string) (string, error) { +const ( + 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()() // 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) } // verify the Docker host interface acting as the macvlan parent iface exists - if ok := hostIfaceExists(hostIface); !ok { - return "", fmt.Errorf("the requested host interface %s was not found on the Docker host", hostIface) + 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) - hostEth, err := netlink.LinkByName(hostIface) + parentLink, err := netlink.LinkByName(parent) 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 macvlan := &netlink.Macvlan{ LinkAttrs: netlink.LinkAttrs{ Name: containerIfName, - ParentIndex: hostEth.Attrs().Index, + ParentIndex: parentLink.Attrs().Index, }, Mode: mode, } 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 - logrus.Warn("Ensure there are no ipvlan networks using the same `-o host_iface` as the macvlan network.") - return "", fmt.Errorf("Failed to create macvlan link: %s with the error: %v", macvlan.Name, err) + // If a user creates a macvlan and ipvlan on same parent, only one slave iface can be active at a time. + return "", fmt.Errorf("failed to create the %s port: %v", macvlanType, err) } + 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 -func hostIfaceExists(ifaceStr string) bool { +// parentExists check if the specified interface exists in the default namespace +func parentExists(ifaceStr string) bool { _, err := netlink.LinkByName(ifaceStr) if err != nil { return false } + return true } @@ -87,13 +93,14 @@ func kernelSupport(networkTpe string) error { 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 -func createVlanLink(ifaceName string) error { - if strings.Contains(ifaceName, ".") { - parentIface, vidInt, err := parseVlan(ifaceName) +func createVlanLink(parentName string) error { + if strings.Contains(parentName, ".") { + parent, vidInt, err := parseVlan(parentName) if err != nil { return err } @@ -102,74 +109,124 @@ func createVlanLink(ifaceName string) error { return fmt.Errorf("vlan id must be between 1-4094, received: %d", vidInt) } // get the parent link to attach a vlan subinterface - hostIface, err := netlink.LinkByName(parentIface) + parentLink, err := netlink.LinkByName(parent) 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{ - Name: ifaceName, - ParentIndex: hostIface.Attrs().Index, + Name: parentName, + ParentIndex: parentLink.Attrs().Index, }, VlanId: vidInt, } // create the subinterface - if err := netlink.LinkAdd(vlanIface); err != nil { - return fmt.Errorf("failed to create %s vlan link: %v", vlanIface.Name, err) + if err := netlink.LinkAdd(vlanLink); err != nil { + return fmt.Errorf("failed to create %s vlan link: %v", vlanLink.Name, err) } // Bring the new netlink iface up - if err := netlink.LinkSetUp(vlanIface); err != nil { - return fmt.Errorf("failed to enable %s the macvlan netlink link: %v", vlanIface.Name, err) + if err := netlink.LinkSetUp(vlanLink); err != nil { + 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 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 -func delVlanLink(ifaceName string) error { - if strings.Contains(ifaceName, ".") { - _, _, err := parseVlan(ifaceName) +// delVlanLink verifies only sub-interfaces with a vlan id get deleted +func delVlanLink(linkName string) error { + if strings.Contains(linkName, ".") { + _, _, err := parseVlan(linkName) if err != nil { return err } // delete the vlan subinterface - vlanIface, err := netlink.LinkByName(ifaceName) + vlanLink, err := netlink.LinkByName(linkName) 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 - if vlanIface.Attrs().ParentIndex == 0 { - return fmt.Errorf("interface %s does not appear to be a slave device: %v", ifaceName, err) + if vlanLink.Attrs().ParentIndex == 0 { + return fmt.Errorf("interface %s does not appear to be a slave device: %v", linkName, err) } // delete the macvlan slave device - if err := netlink.LinkDel(vlanIface); err != nil { - return fmt.Errorf("failed to delete %s link: %v", ifaceName, err) + if err := netlink.LinkDel(vlanLink); err != nil { + 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 // place since it could be a user specified name not created by the driver. return nil } -// parseVlan parses and verifies a slave interface name: -o host_iface=eth0.10 -func parseVlan(ifaceName string) (string, int, error) { - // parse -o host_iface=eth0.10 - splitIface := strings.Split(ifaceName, ".") - if len(splitIface) != 2 { - return "", 0, fmt.Errorf("required interface name format is: name.vlan_id, ex. eth0.10 for vlan 10, instead received %s", ifaceName) +// parseVlan parses and verifies a slave interface name: -o parent=eth0.10 +func parseVlan(linkName string) (string, int, error) { + // parse -o parent=eth0.10 + splitName := strings.Split(linkName, ".") + 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", linkName) } - parentIface, vidStr := splitIface[0], splitIface[1] + parent, vidStr := splitName[0], splitName[1] // validate type and convert vlan id to int vidInt, err := strconv.Atoi(vidStr) if err != nil { 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 - if ok := hostIfaceExists(parentIface); !ok { - return "", 0, fmt.Errorf("-o host_iface parent interface does was not found on the host: %s", parentIface) + if !parentExists(parent) { + 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) } diff --git a/libnetwork/drivers/macvlan/macvlan_setup_test.go b/libnetwork/drivers/macvlan/macvlan_setup_test.go index afb96cad0d..49a8780c88 100644 --- a/libnetwork/drivers/macvlan/macvlan_setup_test.go +++ b/libnetwork/drivers/macvlan/macvlan_setup_test.go @@ -6,17 +6,17 @@ import ( "github.com/vishvananda/netlink" ) -// TestValidateLink tests the validateHostIface function +// TestValidateLink tests the parentExists function func TestValidateLink(t *testing.T) { validIface := "lo" invalidIface := "foo12345" // test a valid parent interface validation - if ok := hostIfaceExists(validIface); !ok { + if ok := parentExists(validIface); !ok { t.Fatalf("failed validating loopback %s", validIface) } // test a invalid parent interface validation - if ok := hostIfaceExists(invalidIface); ok { + if ok := parentExists(invalidIface); ok { t.Fatalf("failed to invalidate interface %s", invalidIface) } } diff --git a/libnetwork/drivers/macvlan/macvlan_state.go b/libnetwork/drivers/macvlan/macvlan_state.go index 726099a7ce..dd2a60ce4b 100644 --- a/libnetwork/drivers/macvlan/macvlan_state.go +++ b/libnetwork/drivers/macvlan/macvlan_state.go @@ -15,6 +15,7 @@ func (d *driver) network(nid string) *network { if !ok { logrus.Errorf("network id %s not found", nid) } + return n } @@ -39,6 +40,7 @@ func (d *driver) getNetworks() []*network { for _, nw := range d.networks { ls = append(ls, nw) } + return ls } @@ -70,6 +72,7 @@ func (n *network) getEndpoint(eid string) (*endpoint, error) { if ep, ok := n.endpoints[eid]; ok { return ep, nil } + return nil, nil } @@ -86,6 +89,7 @@ func validateID(nid, eid string) error { func (n *network) sandbox() osl.Sandbox { n.Lock() defer n.Unlock() + return n.sbox } @@ -104,5 +108,6 @@ func (d *driver) getNetwork(id string) (*network, error) { if nw, ok := d.networks[id]; ok { return nw, nil } + return nil, types.NotFoundErrorf("network not found: %s", id) } diff --git a/libnetwork/drivers/macvlan/macvlan_store.go b/libnetwork/drivers/macvlan/macvlan_store.go index c34a7cb0fc..492ea93f71 100644 --- a/libnetwork/drivers/macvlan/macvlan_store.go +++ b/libnetwork/drivers/macvlan/macvlan_store.go @@ -21,7 +21,7 @@ type configuration struct { dbIndex uint64 dbExists bool Internal bool - HostIface string + Parent string MacvlanMode string CreatedSlaveLink bool Ipv4Subnets []*ipv4Subnet @@ -114,8 +114,9 @@ func (config *configuration) MarshalJSON() ([]byte, error) { nMap := make(map[string]interface{}) nMap["ID"] = config.ID nMap["Mtu"] = config.Mtu - nMap["HostIface"] = config.HostIface + nMap["Parent"] = config.Parent nMap["MacvlanMode"] = config.MacvlanMode + nMap["Internal"] = config.Internal nMap["CreatedSubIface"] = config.CreatedSlaveLink if len(config.Ipv4Subnets) > 0 { iis, err := json.Marshal(config.Ipv4Subnets) @@ -131,6 +132,7 @@ func (config *configuration) MarshalJSON() ([]byte, error) { } nMap["Ipv6Subnets"] = string(iis) } + return json.Marshal(nMap) } @@ -145,8 +147,9 @@ func (config *configuration) UnmarshalJSON(b []byte) error { } config.ID = nMap["ID"].(string) config.Mtu = int(nMap["Mtu"].(float64)) - config.HostIface = nMap["HostIface"].(string) + config.Parent = nMap["Parent"].(string) config.MacvlanMode = nMap["MacvlanMode"].(string) + config.Internal = nMap["Internal"].(bool) config.CreatedSlaveLink = nMap["CreatedSubIface"].(bool) if v, ok := nMap["Ipv4Subnets"]; ok { 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 nil } @@ -174,6 +178,7 @@ func (config *configuration) Value() []byte { if err != nil { return nil } + return b } @@ -205,6 +210,7 @@ func (config *configuration) New() datastore.KVObject { func (config *configuration) CopyTo(o datastore.KVObject) error { dstNcfg := o.(*configuration) *dstNcfg = *config + return nil } diff --git a/libnetwork/drivers_experimental_linux.go b/libnetwork/drivers_experimental_linux.go new file mode 100644 index 0000000000..49f7b9bb36 --- /dev/null +++ b/libnetwork/drivers_experimental_linux.go @@ -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"}, + } +} diff --git a/libnetwork/drivers_linux.go b/libnetwork/drivers_linux.go index 91964519b6..df8b4d734b 100644 --- a/libnetwork/drivers_linux.go +++ b/libnetwork/drivers_linux.go @@ -3,21 +3,20 @@ package libnetwork import ( "github.com/docker/libnetwork/drivers/bridge" "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/overlay" "github.com/docker/libnetwork/drivers/remote" ) func getInitializers() []initializer { - return []initializer{ + in := []initializer{ {bridge.Init, "bridge"}, {host.Init, "host"}, {null.Init, "null"}, {remote.Init, "remote"}, {overlay.Init, "overlay"}, - {macvlan.Init, "macvlan"}, - {ipvlan.Init, "ipvlan"}, } + + in = append(in, additionalDrivers()...) + return in } diff --git a/libnetwork/drivers_stub_linux.go b/libnetwork/drivers_stub_linux.go new file mode 100644 index 0000000000..e20428c72f --- /dev/null +++ b/libnetwork/drivers_stub_linux.go @@ -0,0 +1,7 @@ +// +build !experimental + +package libnetwork + +func additionalDrivers() []initializer { + return nil +}