2016-02-16 22:15:18 -05:00
|
|
|
package ipvlan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-03-10 17:09:48 -05:00
|
|
|
"github.com/docker/docker/pkg/parsers/kernel"
|
2016-02-16 22:15:18 -05:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/docker/libnetwork/driverapi"
|
|
|
|
"github.com/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/libnetwork/options"
|
2016-03-10 16:23:04 -05:00
|
|
|
"github.com/docker/libnetwork/osl"
|
2016-02-16 22:15:18 -05:00
|
|
|
"github.com/docker/libnetwork/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateNetwork the network for the specified driver type
|
2016-03-02 20:13:15 -05:00
|
|
|
func (d *driver) CreateNetwork(nid string, option map[string]interface{}, ipV4Data, ipV6Data []driverapi.IPAMData) error {
|
2016-03-10 16:23:04 -05:00
|
|
|
defer osl.InitOSContext()()
|
2016-03-10 17:09:48 -05:00
|
|
|
kv, err := kernel.GetKernelVersion()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to check kernel version for %s driver support: %v", ipvlanType, err)
|
|
|
|
}
|
2016-03-11 14:28:32 -05:00
|
|
|
// ensure Kernel version is >= v4.2 for ipvlan support
|
|
|
|
if kv.Kernel < ipvlanKernelVer || (kv.Kernel == ipvlanKernelVer && kv.Major < ipvlanMajorVer) {
|
2016-03-10 17:09:48 -05:00
|
|
|
return fmt.Errorf("kernel version failed to meet the minimum ipvlan kernel requirement of %d.%d, found %d.%d.%d",
|
|
|
|
ipvlanKernelVer, ipvlanMajorVer, kv.Kernel, kv.Major, kv.Minor)
|
|
|
|
}
|
2016-03-13 00:42:00 -05:00
|
|
|
// reject a null v4 network
|
|
|
|
if len(ipV4Data) == 0 || ipV4Data[0].Pool.String() == "0.0.0.0/0" {
|
|
|
|
return fmt.Errorf("ipv4 pool is empty")
|
|
|
|
}
|
2016-02-16 22:15:18 -05:00
|
|
|
// parse and validate the config and bind to networkConfiguration
|
2016-03-02 20:13:15 -05:00
|
|
|
config, err := parseNetworkOptions(nid, option)
|
2016-02-16 22:15:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-02 20:13:15 -05:00
|
|
|
config.ID = nid
|
|
|
|
err = config.processIPAM(nid, ipV4Data, ipV6Data)
|
2016-02-16 22:15:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// verify the ipvlan mode from -o ipvlan_mode option
|
|
|
|
switch config.IpvlanMode {
|
|
|
|
case "", modeL2:
|
|
|
|
// default to ipvlan L2 mode if -o ipvlan_mode is empty
|
|
|
|
config.IpvlanMode = modeL2
|
|
|
|
case modeL3:
|
|
|
|
config.IpvlanMode = modeL3
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("requested ipvlan mode '%s' is not valid, 'l2' mode is the ipvlan driver default", config.IpvlanMode)
|
|
|
|
}
|
|
|
|
// loopback is not a valid parent link
|
2016-03-02 20:13:15 -05:00
|
|
|
if config.Parent == "lo" {
|
2016-02-16 22:15:18 -05:00
|
|
|
return fmt.Errorf("loopback interface is not a valid %s parent link", ipvlanType)
|
|
|
|
}
|
2016-03-02 20:13:15 -05:00
|
|
|
// 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
|
|
|
|
}
|
2016-02-16 22:15:18 -05:00
|
|
|
err = d.createNetwork(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// update persistent db, rollback on fail
|
|
|
|
err = d.storeUpdate(config)
|
|
|
|
if err != nil {
|
|
|
|
d.deleteNetwork(config.ID)
|
2016-03-02 20:13:15 -05:00
|
|
|
logrus.Debugf("encoutered an error rolling back a network create for %s : %v", config.ID, err)
|
2016-02-16 22:15:18 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createNetwork is used by new network callbacks and persistent network cache
|
|
|
|
func (d *driver) createNetwork(config *configuration) error {
|
|
|
|
networkList := d.getNetworks()
|
|
|
|
for _, nw := range networkList {
|
2016-03-02 20:13:15 -05:00
|
|
|
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)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
}
|
2016-03-02 20:13:15 -05:00
|
|
|
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
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
n := &network{
|
|
|
|
id: config.ID,
|
|
|
|
driver: d,
|
|
|
|
endpoints: endpointTable{},
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
// add the *network
|
|
|
|
d.addNetwork(n)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteNetwork the network for the specified driver type
|
|
|
|
func (d *driver) DeleteNetwork(nid string) error {
|
2016-03-10 16:23:04 -05:00
|
|
|
defer osl.InitOSContext()()
|
2016-02-16 22:15:18 -05:00
|
|
|
n := d.network(nid)
|
2016-03-02 20:13:15 -05:00
|
|
|
if n == nil {
|
|
|
|
return fmt.Errorf("network id %s not found", nid)
|
|
|
|
}
|
2016-02-16 22:15:18 -05:00
|
|
|
// if the driver created the slave interface, delete it, otherwise leave it
|
|
|
|
if ok := n.config.CreatedSlaveLink; ok {
|
2016-03-02 20:13:15 -05:00
|
|
|
// 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)
|
|
|
|
}
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// delete the *network
|
|
|
|
d.deleteNetwork(nid)
|
|
|
|
// delete the network record from persistent cache
|
2016-03-02 20:13:15 -05:00
|
|
|
err := d.storeDelete(n.config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error deleting deleting id %s from datastore: %v", nid, err)
|
|
|
|
}
|
|
|
|
return nil
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseNetworkOptions parse docker network options
|
|
|
|
func parseNetworkOptions(id string, option options.Generic) (*configuration, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
config = &configuration{}
|
|
|
|
)
|
|
|
|
// parse generic labels first
|
|
|
|
if genData, ok := option[netlabel.GenericData]; ok && genData != nil {
|
|
|
|
if config, err = parseNetworkGenericOptions(genData); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2016-03-02 20:13:15 -05:00
|
|
|
// setting the parent to "" will trigger an isolated network dummy parent link
|
2016-02-16 22:15:18 -05:00
|
|
|
if _, ok := option[netlabel.Internal]; ok {
|
2016-03-02 20:13:15 -05:00
|
|
|
config.Internal = true
|
|
|
|
// empty --parent= and --internal are handled the same.
|
|
|
|
config.Parent = ""
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseNetworkGenericOptions parse generic driver docker network options
|
|
|
|
func parseNetworkGenericOptions(data interface{}) (*configuration, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
config *configuration
|
|
|
|
)
|
|
|
|
switch opt := data.(type) {
|
|
|
|
case *configuration:
|
|
|
|
config = opt
|
|
|
|
case map[string]string:
|
|
|
|
config = &configuration{}
|
|
|
|
err = config.fromOptions(opt)
|
|
|
|
case options.Generic:
|
|
|
|
var opaqueConfig interface{}
|
|
|
|
if opaqueConfig, err = options.GenerateFromModel(opt, config); err == nil {
|
|
|
|
config = opaqueConfig.(*configuration)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
err = types.BadRequestErrorf("unrecognized network configuration format: %v", opt)
|
|
|
|
}
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// fromOptions binds the generic options to networkConfiguration to cache
|
|
|
|
func (config *configuration) fromOptions(labels map[string]string) error {
|
|
|
|
for label, value := range labels {
|
|
|
|
switch label {
|
2016-03-02 20:13:15 -05:00
|
|
|
case parentOpt:
|
|
|
|
// parse driver option '-o parent'
|
|
|
|
config.Parent = value
|
2016-02-16 22:15:18 -05:00
|
|
|
case driverModeOpt:
|
|
|
|
// parse driver option '-o ipvlan_mode'
|
|
|
|
config.IpvlanMode = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// processIPAM parses v4 and v6 IP information and binds it to the network configuration
|
|
|
|
func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error {
|
|
|
|
if len(ipamV4Data) > 0 {
|
|
|
|
for _, ipd := range ipamV4Data {
|
|
|
|
s := &ipv4Subnet{
|
|
|
|
SubnetIP: ipd.Pool.String(),
|
|
|
|
GwIP: ipd.Gateway.String(),
|
|
|
|
}
|
|
|
|
config.Ipv4Subnets = append(config.Ipv4Subnets, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(ipamV6Data) > 0 {
|
|
|
|
for _, ipd := range ipamV6Data {
|
|
|
|
s := &ipv6Subnet{
|
|
|
|
SubnetIP: ipd.Pool.String(),
|
|
|
|
GwIP: ipd.Gateway.String(),
|
|
|
|
}
|
|
|
|
config.Ipv6Subnets = append(config.Ipv6Subnets, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|