2021-08-23 15:14:53 +02:00
|
|
|
//go:build linux
|
2021-05-25 23:48:54 +00:00
|
|
|
// +build linux
|
|
|
|
|
2016-02-16 22:15:18 -05:00
|
|
|
package ipvlan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/driverapi"
|
|
|
|
"github.com/docker/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/docker/libnetwork/ns"
|
|
|
|
"github.com/docker/docker/libnetwork/options"
|
|
|
|
"github.com/docker/docker/libnetwork/osl"
|
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2021-05-28 00:15:56 +00:00
|
|
|
"github.com/docker/docker/pkg/parsers/kernel"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2017-07-26 14:18:31 -07:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-02-16 22:15:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateNetwork the network for the specified driver type
|
2016-04-18 19:55:39 -07:00
|
|
|
func (d *driver) CreateNetwork(nid string, option map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
|
2016-03-10 13:23:04 -08:00
|
|
|
defer osl.InitOSContext()()
|
2016-03-10 17:09:48 -05:00
|
|
|
kv, err := kernel.GetKernelVersion()
|
|
|
|
if err != nil {
|
2022-07-01 11:30:46 +02:00
|
|
|
return fmt.Errorf("failed to check kernel version for ipvlan driver support: %v", err)
|
2016-03-10 17:09:48 -05:00
|
|
|
}
|
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 17:13:15 -08:00
|
|
|
config, err := parseNetworkOptions(nid, option)
|
2016-02-16 22:15:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-01 10:30:57 +02:00
|
|
|
config.processIPAM(ipV4Data, ipV6Data)
|
|
|
|
|
2016-03-02 17:13:15 -08: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))
|
|
|
|
}
|
2019-07-08 13:52:31 -07:00
|
|
|
foundExisting, err := d.createNetwork(config)
|
2016-02-16 22:15:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-08 13:52:31 -07:00
|
|
|
|
|
|
|
if foundExisting {
|
|
|
|
return types.InternalMaskableErrorf("restoring existing network %s", config.ID)
|
|
|
|
}
|
2022-07-01 11:30:46 +02:00
|
|
|
|
2016-02-16 22:15:18 -05:00
|
|
|
// update persistent db, rollback on fail
|
|
|
|
err = d.storeUpdate(config)
|
|
|
|
if err != nil {
|
|
|
|
d.deleteNetwork(config.ID)
|
2017-05-22 02:25:52 +00:00
|
|
|
logrus.Debugf("encountered 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
|
2019-07-08 13:52:31 -07:00
|
|
|
func (d *driver) createNetwork(config *configuration) (bool, error) {
|
|
|
|
foundExisting := false
|
2016-02-16 22:15:18 -05:00
|
|
|
networkList := d.getNetworks()
|
|
|
|
for _, nw := range networkList {
|
2016-03-02 17:13:15 -08:00
|
|
|
if config.Parent == nw.config.Parent {
|
2019-07-08 13:52:31 -07:00
|
|
|
if config.ID != nw.config.ID {
|
|
|
|
return false, fmt.Errorf("network %s is already using parent interface %s",
|
|
|
|
getDummyName(stringid.TruncateID(nw.config.ID)), config.Parent)
|
|
|
|
}
|
|
|
|
logrus.Debugf("Create Network for the same ID %s\n", config.ID)
|
|
|
|
foundExisting = true
|
|
|
|
break
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
}
|
2016-03-02 17:13:15 -08:00
|
|
|
if !parentExists(config.Parent) {
|
2020-03-03 14:42:42 -08:00
|
|
|
// Create a dummy link if a dummy name is set for parent
|
|
|
|
if dummyName := getDummyName(stringid.TruncateID(config.ID)); dummyName == config.Parent {
|
|
|
|
err := createDummyLink(config.Parent, dummyName)
|
2016-03-02 17:13:15 -08:00
|
|
|
if err != nil {
|
2019-07-24 17:06:04 -07:00
|
|
|
return false, err
|
2016-03-02 17:13:15 -08:00
|
|
|
}
|
|
|
|
config.CreatedSlaveLink = true
|
2019-07-08 13:52:31 -07:00
|
|
|
|
2022-07-01 11:30:46 +02:00
|
|
|
// notify the user in logs that they have limited communications
|
2020-03-03 14:42:42 -08:00
|
|
|
logrus.Debugf("Empty -o parent= flags limit communications to other containers inside of network: %s",
|
|
|
|
config.Parent)
|
2016-03-02 17:13:15 -08:00
|
|
|
} 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 {
|
2019-07-24 17:06:04 -07:00
|
|
|
return false, err
|
2016-03-02 17:13:15 -08:00
|
|
|
}
|
|
|
|
// if driver created the networks slave link, record it for future deletion
|
|
|
|
config.CreatedSlaveLink = true
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
}
|
2019-07-08 13:52:31 -07:00
|
|
|
if !foundExisting {
|
|
|
|
n := &network{
|
|
|
|
id: config.ID,
|
|
|
|
driver: d,
|
|
|
|
endpoints: endpointTable{},
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
// add the network
|
|
|
|
d.addNetwork(n)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
|
2019-07-08 13:52:31 -07:00
|
|
|
return foundExisting, nil
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
|
2022-07-01 11:30:46 +02:00
|
|
|
// DeleteNetwork deletes the network for the specified driver type
|
2016-02-16 22:15:18 -05:00
|
|
|
func (d *driver) DeleteNetwork(nid string) error {
|
2016-03-10 13:23:04 -08:00
|
|
|
defer osl.InitOSContext()()
|
2016-02-16 22:15:18 -05:00
|
|
|
n := d.network(nid)
|
2016-03-02 17:13:15 -08: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 17:13:15 -08: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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-13 22:15:20 -04:00
|
|
|
for _, ep := range n.endpoints {
|
|
|
|
if link, err := ns.NlHandle().LinkByName(ep.srcName); err == nil {
|
2018-03-01 01:38:35 +08:00
|
|
|
if err := ns.NlHandle().LinkDel(link); err != nil {
|
|
|
|
logrus.WithError(err).Warnf("Failed to delete interface (%s)'s link on endpoint (%s) delete", ep.srcName, ep.id)
|
|
|
|
}
|
2016-10-13 22:15:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.storeDelete(ep); err != nil {
|
2018-07-05 16:33:01 -04:00
|
|
|
logrus.Warnf("Failed to remove ipvlan endpoint %.7s from store: %v", ep.id, err)
|
2016-10-13 22:15:20 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-16 22:15:18 -05:00
|
|
|
// delete the *network
|
|
|
|
d.deleteNetwork(nid)
|
|
|
|
// delete the network record from persistent cache
|
2016-03-02 17:13:15 -08: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
|
|
|
}
|
|
|
|
|
2022-07-01 11:30:46 +02:00
|
|
|
// parseNetworkOptions parses docker network options
|
2016-02-16 22:15:18 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2019-07-15 10:15:53 +02:00
|
|
|
if val, ok := option[netlabel.Internal]; ok {
|
|
|
|
if internal, ok := val.(bool); ok && internal {
|
|
|
|
config.Internal = true
|
|
|
|
}
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
2022-07-01 10:56:21 +02:00
|
|
|
|
|
|
|
// verify the ipvlan mode from -o ipvlan_mode option
|
|
|
|
switch config.IpvlanMode {
|
|
|
|
case "":
|
|
|
|
// default to ipvlan L2 mode if -o ipvlan_mode is empty
|
|
|
|
config.IpvlanMode = modeL2
|
|
|
|
case modeL2, modeL3, modeL3S:
|
|
|
|
// valid option
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("requested ipvlan mode '%s' is not valid, 'l2' mode is the ipvlan driver default", config.IpvlanMode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify the ipvlan flag from -o ipvlan_flag option
|
|
|
|
switch config.IpvlanFlag {
|
|
|
|
case "":
|
|
|
|
// default to bridge if -o ipvlan_flag is empty
|
|
|
|
config.IpvlanFlag = flagBridge
|
|
|
|
case flagBridge, flagPrivate, flagVepa:
|
|
|
|
// valid option
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("requested ipvlan flag '%s' is not valid, 'bridge' is the ipvlan driver default", config.IpvlanFlag)
|
|
|
|
}
|
|
|
|
|
|
|
|
// loopback is not a valid parent link
|
|
|
|
if config.Parent == "lo" {
|
|
|
|
return nil, fmt.Errorf("loopback interface is not a valid ipvlan parent link")
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:37:30 +02:00
|
|
|
config.ID = id
|
2016-02-16 22:15:18 -05:00
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseNetworkGenericOptions parse generic driver docker network options
|
|
|
|
func parseNetworkGenericOptions(data interface{}) (*configuration, error) {
|
|
|
|
switch opt := data.(type) {
|
|
|
|
case *configuration:
|
2022-07-01 11:18:14 +02:00
|
|
|
return opt, nil
|
2016-02-16 22:15:18 -05:00
|
|
|
case map[string]string:
|
2022-07-01 11:17:10 +02:00
|
|
|
return newConfigFromLabels(opt), nil
|
2016-02-16 22:15:18 -05:00
|
|
|
case options.Generic:
|
2022-07-01 11:18:14 +02:00
|
|
|
var config *configuration
|
|
|
|
opaqueConfig, err := options.GenerateFromModel(opt, config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
2022-07-01 11:18:14 +02:00
|
|
|
return opaqueConfig.(*configuration), nil
|
2016-02-16 22:15:18 -05:00
|
|
|
default:
|
2022-07-01 11:18:14 +02:00
|
|
|
return nil, types.BadRequestErrorf("unrecognized network configuration format: %v", opt)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-01 11:17:10 +02:00
|
|
|
// newConfigFromLabels creates a new configuration from the given labels.
|
|
|
|
func newConfigFromLabels(labels map[string]string) *configuration {
|
|
|
|
config := &configuration{}
|
2016-02-16 22:15:18 -05:00
|
|
|
for label, value := range labels {
|
|
|
|
switch label {
|
2016-03-02 17:13:15 -08: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
|
2021-06-19 21:11:14 +08:00
|
|
|
case driverFlagOpt:
|
|
|
|
// parse driver option '-o ipvlan_flag'
|
|
|
|
config.IpvlanFlag = value
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 11:17:10 +02:00
|
|
|
|
|
|
|
return config
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// processIPAM parses v4 and v6 IP information and binds it to the network configuration
|
2022-07-01 10:30:57 +02:00
|
|
|
func (config *configuration) processIPAM(ipamV4Data, ipamV6Data []driverapi.IPAMData) {
|
2022-07-01 10:32:39 +02:00
|
|
|
for _, ipd := range ipamV4Data {
|
|
|
|
config.Ipv4Subnets = append(config.Ipv4Subnets, &ipSubnet{
|
|
|
|
SubnetIP: ipd.Pool.String(),
|
|
|
|
GwIP: ipd.Gateway.String(),
|
|
|
|
})
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
2022-07-01 10:32:39 +02:00
|
|
|
for _, ipd := range ipamV6Data {
|
|
|
|
config.Ipv6Subnets = append(config.Ipv6Subnets, &ipSubnet{
|
|
|
|
SubnetIP: ipd.Pool.String(),
|
|
|
|
GwIP: ipd.Gateway.String(),
|
|
|
|
})
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
}
|