2016-02-16 22:15:18 -05:00
|
|
|
package ipvlan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-05-16 14:51:40 -04:00
|
|
|
"github.com/docker/libnetwork/ns"
|
2016-02-16 22:15:18 -05:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
2016-03-02 20:13:15 -05:00
|
|
|
const (
|
2016-03-10 17:09:48 -05:00
|
|
|
dummyPrefix = "di-" // ipvlan prefix for dummy parent interface
|
|
|
|
ipvlanKernelVer = 4 // minimum ipvlan kernel support
|
2016-03-11 14:28:32 -05:00
|
|
|
ipvlanMajorVer = 2 // minimum ipvlan major kernel support
|
2016-03-02 20:13:15 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// createIPVlan Create the ipvlan slave specifying the source name
|
|
|
|
func createIPVlan(containerIfName, parent, ipvlanMode string) (string, error) {
|
|
|
|
// Set the ipvlan mode. Default is bridge mode
|
|
|
|
mode, err := setIPVlanMode(ipvlanMode)
|
2016-02-16 22:15:18 -05:00
|
|
|
if err != nil {
|
2016-03-02 20:13:15 -05:00
|
|
|
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)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
// Get the link for the master index (Example: the docker host eth iface)
|
2016-05-16 14:51:40 -04:00
|
|
|
parentLink, err := ns.NlHandle().LinkByName(parent)
|
2016-02-16 22:15:18 -05:00
|
|
|
if err != nil {
|
2016-03-02 20:13:15 -05:00
|
|
|
return "", fmt.Errorf("error occoured looking up the %s parent iface %s error: %s", ipvlanType, parent, err)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
2016-06-02 01:37:39 -04:00
|
|
|
// Create an ipvlan link
|
2016-02-16 22:15:18 -05:00
|
|
|
ipvlan := &netlink.IPVlan{
|
|
|
|
LinkAttrs: netlink.LinkAttrs{
|
|
|
|
Name: containerIfName,
|
2016-03-02 20:13:15 -05:00
|
|
|
ParentIndex: parentLink.Attrs().Index,
|
2016-02-16 22:15:18 -05:00
|
|
|
},
|
|
|
|
Mode: mode,
|
|
|
|
}
|
2016-05-16 14:51:40 -04:00
|
|
|
if err := ns.NlHandle().LinkAdd(ipvlan); err != nil {
|
2016-03-02 20:13:15 -05:00
|
|
|
// 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)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
2016-03-02 20:13:15 -05:00
|
|
|
|
2016-02-16 22:15:18 -05:00
|
|
|
return ipvlan.Attrs().Name, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setIPVlanMode setter for one of the two ipvlan port types
|
|
|
|
func setIPVlanMode(mode string) (netlink.IPVlanMode, error) {
|
|
|
|
switch mode {
|
|
|
|
case modeL2:
|
|
|
|
return netlink.IPVLAN_MODE_L2, nil
|
|
|
|
case modeL3:
|
|
|
|
return netlink.IPVLAN_MODE_L3, nil
|
|
|
|
default:
|
|
|
|
return 0, fmt.Errorf("Unknown ipvlan mode: %s", mode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 20:13:15 -05:00
|
|
|
// parentExists check if the specified interface exists in the default namespace
|
|
|
|
func parentExists(ifaceStr string) bool {
|
2016-05-16 14:51:40 -04:00
|
|
|
_, err := ns.NlHandle().LinkByName(ifaceStr)
|
2016-02-16 22:15:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2016-03-02 20:13:15 -05:00
|
|
|
|
2016-02-16 22:15:18 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// createVlanLink parses sub-interfaces and vlan id for creation
|
2016-03-02 20:13:15 -05:00
|
|
|
func createVlanLink(parentName string) error {
|
|
|
|
if strings.Contains(parentName, ".") {
|
|
|
|
parent, vidInt, err := parseVlan(parentName)
|
2016-02-16 22:15:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// VLAN identifier or VID is a 12-bit field specifying the VLAN to which the frame belongs
|
|
|
|
if vidInt > 4094 || vidInt < 1 {
|
|
|
|
return fmt.Errorf("vlan id must be between 1-4094, received: %d", vidInt)
|
|
|
|
}
|
|
|
|
// get the parent link to attach a vlan subinterface
|
2016-05-16 14:51:40 -04:00
|
|
|
parentLink, err := ns.NlHandle().LinkByName(parent)
|
2016-02-16 22:15:18 -05:00
|
|
|
if err != nil {
|
2016-03-02 20:13:15 -05:00
|
|
|
return fmt.Errorf("failed to find master interface %s on the Docker host: %v", parent, err)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
2016-03-02 20:13:15 -05:00
|
|
|
vlanLink := &netlink.Vlan{
|
2016-02-16 22:15:18 -05:00
|
|
|
LinkAttrs: netlink.LinkAttrs{
|
2016-03-02 20:13:15 -05:00
|
|
|
Name: parentName,
|
|
|
|
ParentIndex: parentLink.Attrs().Index,
|
2016-02-16 22:15:18 -05:00
|
|
|
},
|
|
|
|
VlanId: vidInt,
|
|
|
|
}
|
|
|
|
// create the subinterface
|
2016-05-16 14:51:40 -04:00
|
|
|
if err := ns.NlHandle().LinkAdd(vlanLink); err != nil {
|
2016-03-02 20:13:15 -05:00
|
|
|
return fmt.Errorf("failed to create %s vlan link: %v", vlanLink.Name, err)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
// Bring the new netlink iface up
|
2016-05-16 14:51:40 -04:00
|
|
|
if err := ns.NlHandle().LinkSetUp(vlanLink); err != nil {
|
2016-03-02 20:13:15 -05:00
|
|
|
return fmt.Errorf("failed to enable %s the ipvlan parent link %v", vlanLink.Name, err)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
2016-03-02 20:13:15 -05:00
|
|
|
logrus.Debugf("Added a vlan tagged netlink subinterface: %s with a vlan id: %d", parentName, vidInt)
|
2016-02-16 22:15:18 -05:00
|
|
|
return nil
|
|
|
|
}
|
2016-03-02 20:13:15 -05:00
|
|
|
|
|
|
|
return fmt.Errorf("invalid subinterface vlan name %s, example formatting is eth0.10", parentName)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
|
2016-03-02 20:13:15 -05:00
|
|
|
// delVlanLink verifies only sub-interfaces with a vlan id get deleted
|
|
|
|
func delVlanLink(linkName string) error {
|
|
|
|
if strings.Contains(linkName, ".") {
|
|
|
|
_, _, err := parseVlan(linkName)
|
2016-02-16 22:15:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// delete the vlan subinterface
|
2016-05-16 14:51:40 -04:00
|
|
|
vlanLink, err := ns.NlHandle().LinkByName(linkName)
|
2016-02-16 22:15:18 -05:00
|
|
|
if err != nil {
|
2016-03-02 20:13:15 -05:00
|
|
|
return fmt.Errorf("failed to find interface %s on the Docker host : %v", linkName, err)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
// verify a parent interface isn't being deleted
|
2016-03-02 20:13:15 -05:00
|
|
|
if vlanLink.Attrs().ParentIndex == 0 {
|
|
|
|
return fmt.Errorf("interface %s does not appear to be a slave device: %v", linkName, err)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
// delete the ipvlan slave device
|
2016-05-16 14:51:40 -04:00
|
|
|
if err := ns.NlHandle().LinkDel(vlanLink); err != nil {
|
2016-03-02 20:13:15 -05:00
|
|
|
return fmt.Errorf("failed to delete %s link: %v", linkName, err)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
2016-03-02 20:13:15 -05:00
|
|
|
logrus.Debugf("Deleted a vlan tagged netlink subinterface: %s", linkName)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2016-03-02 20:13:15 -05:00
|
|
|
// 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)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
2016-03-02 20:13:15 -05:00
|
|
|
parent, vidStr := splitName[0], splitName[1]
|
2016-02-16 22:15:18 -05:00
|
|
|
// 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
|
2016-03-02 20:13:15 -05:00
|
|
|
if !parentExists(parent) {
|
|
|
|
return "", 0, fmt.Errorf("-o parent interface does was not found on the host: %s", parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
2016-05-16 14:51:40 -04:00
|
|
|
if err := ns.NlHandle().LinkAdd(parent); err != nil {
|
2016-03-02 20:13:15 -05:00
|
|
|
return err
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
2016-05-16 14:51:40 -04:00
|
|
|
parentDummyLink, err := ns.NlHandle().LinkByName(dummyName)
|
2016-03-02 20:13:15 -05:00
|
|
|
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
|
2016-05-16 14:51:40 -04:00
|
|
|
if err := ns.NlHandle().LinkSetUp(parentDummyLink); err != nil {
|
2016-03-02 20:13:15 -05:00
|
|
|
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
|
2016-05-16 14:51:40 -04:00
|
|
|
dummyLink, err := ns.NlHandle().LinkByName(linkName)
|
2016-03-02 20:13:15 -05:00
|
|
|
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
|
2016-05-16 14:51:40 -04:00
|
|
|
if err := ns.NlHandle().LinkDel(dummyLink); err != nil {
|
2016-03-02 20:13:15 -05:00
|
|
|
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)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|