2015-02-19 17:21:42 -08:00
|
|
|
package bridge
|
|
|
|
|
2015-02-19 22:21:05 -08:00
|
|
|
import (
|
2015-05-14 06:23:45 +00:00
|
|
|
"errors"
|
2015-02-19 22:21:05 -08:00
|
|
|
"net"
|
2015-05-22 10:56:36 -07:00
|
|
|
"strconv"
|
2015-04-13 18:40:42 +00:00
|
|
|
"strings"
|
2015-04-10 11:59:05 -07:00
|
|
|
"sync"
|
2015-02-19 22:21:05 -08:00
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
"github.com/docker/libnetwork/driverapi"
|
2015-04-10 11:59:05 -07:00
|
|
|
"github.com/docker/libnetwork/ipallocator"
|
2015-05-16 16:02:51 -07:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
2015-04-14 00:12:40 -04:00
|
|
|
"github.com/docker/libnetwork/netutils"
|
2015-05-16 16:02:51 -07:00
|
|
|
"github.com/docker/libnetwork/options"
|
2015-04-10 11:59:05 -07:00
|
|
|
"github.com/docker/libnetwork/portmapper"
|
2015-04-20 08:44:06 -07:00
|
|
|
"github.com/docker/libnetwork/sandbox"
|
|
|
|
"github.com/docker/libnetwork/types"
|
2015-04-13 18:40:42 +00:00
|
|
|
"github.com/vishvananda/netlink"
|
2015-02-19 22:21:05 -08:00
|
|
|
)
|
2015-02-19 17:21:42 -08:00
|
|
|
|
2015-02-22 17:24:22 -08:00
|
|
|
const (
|
2015-05-01 17:01:21 -07:00
|
|
|
networkType = "bridge"
|
|
|
|
vethPrefix = "veth"
|
|
|
|
vethLen = 7
|
2015-05-21 18:04:49 +00:00
|
|
|
containerVethPrefix = "eth"
|
2015-05-01 17:01:21 -07:00
|
|
|
maxAllocatePortAttempts = 10
|
2015-05-14 06:23:45 +00:00
|
|
|
ifaceID = 1
|
2015-02-22 17:24:22 -08:00
|
|
|
)
|
2015-02-19 17:21:42 -08:00
|
|
|
|
2015-04-10 11:59:05 -07:00
|
|
|
var (
|
|
|
|
ipAllocator *ipallocator.IPAllocator
|
|
|
|
portMapper *portmapper.PortMapper
|
|
|
|
)
|
|
|
|
|
2015-05-22 10:56:36 -07:00
|
|
|
// configuration info for the "bridge" driver.
|
|
|
|
type configuration struct {
|
2015-05-06 05:51:26 +00:00
|
|
|
EnableIPForwarding bool
|
|
|
|
}
|
|
|
|
|
2015-05-22 10:56:36 -07:00
|
|
|
// networkConfiguration for network specific configuration
|
|
|
|
type networkConfiguration struct {
|
2015-04-15 05:25:42 +00:00
|
|
|
BridgeName string
|
|
|
|
AddressIPv4 *net.IPNet
|
|
|
|
FixedCIDR *net.IPNet
|
|
|
|
FixedCIDRv6 *net.IPNet
|
|
|
|
EnableIPv6 bool
|
|
|
|
EnableIPTables bool
|
|
|
|
EnableIPMasquerade bool
|
|
|
|
EnableICC bool
|
2015-04-23 13:39:41 -07:00
|
|
|
Mtu int
|
2015-04-24 15:13:44 -07:00
|
|
|
DefaultGatewayIPv4 net.IP
|
|
|
|
DefaultGatewayIPv6 net.IP
|
2015-05-01 17:01:21 -07:00
|
|
|
DefaultBindingIP net.IP
|
2015-05-06 05:51:26 +00:00
|
|
|
AllowNonDefaultBridge bool
|
2015-05-18 16:49:12 -07:00
|
|
|
EnableUserlandProxy bool
|
2015-02-19 17:21:42 -08:00
|
|
|
}
|
|
|
|
|
2015-05-22 10:56:36 -07:00
|
|
|
// endpointConfiguration represents the user specified configuration for the sandbox endpoint
|
|
|
|
type endpointConfiguration struct {
|
2015-05-01 17:01:21 -07:00
|
|
|
MacAddress net.HardwareAddr
|
2015-05-20 20:28:46 +00:00
|
|
|
PortBindings []types.PortBinding
|
|
|
|
ExposedPorts []types.TransportPort
|
2015-04-23 11:15:15 -07:00
|
|
|
}
|
|
|
|
|
2015-05-22 10:56:36 -07:00
|
|
|
// containerConfiguration represents the user specified configuration for a container
|
|
|
|
type containerConfiguration struct {
|
2015-05-05 20:46:11 +00:00
|
|
|
ParentEndpoints []string
|
|
|
|
ChildEndpoints []string
|
2015-04-30 14:52:46 -07:00
|
|
|
}
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
type bridgeEndpoint struct {
|
2015-05-06 00:33:08 +00:00
|
|
|
id types.UUID
|
|
|
|
intf *sandbox.Interface
|
|
|
|
macAddress net.HardwareAddr
|
2015-05-22 10:56:36 -07:00
|
|
|
config *endpointConfiguration // User specified parameters
|
|
|
|
containerConfig *containerConfiguration
|
2015-05-20 20:28:46 +00:00
|
|
|
portMapping []types.PortBinding // Operation port bindings
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type bridgeNetwork struct {
|
2015-04-20 04:23:04 -07:00
|
|
|
id types.UUID
|
2015-05-06 05:51:26 +00:00
|
|
|
bridge *bridgeInterface // The bridge's L3 interface
|
2015-05-22 10:56:36 -07:00
|
|
|
config *networkConfiguration
|
2015-04-28 05:57:36 +00:00
|
|
|
endpoints map[types.UUID]*bridgeEndpoint // key: endpoint id
|
2015-04-13 18:40:42 +00:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type driver struct {
|
2015-05-21 13:13:19 -07:00
|
|
|
config *configuration
|
|
|
|
network *bridgeNetwork
|
|
|
|
networks map[types.UUID]*bridgeNetwork
|
2015-04-13 18:40:42 +00:00
|
|
|
sync.Mutex
|
|
|
|
}
|
2015-03-05 19:04:07 +00:00
|
|
|
|
2015-02-19 22:21:05 -08:00
|
|
|
func init() {
|
2015-04-10 11:59:05 -07:00
|
|
|
ipAllocator = ipallocator.New()
|
2015-04-16 15:16:11 -07:00
|
|
|
portMapper = portmapper.New()
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
Make driver packages register themselves via DriverCallback
In the present code, each driver package provides a `New()` method
which constructs a driver of its type, which is then registered with
the controller.
However, this is not suitable for the `drivers/remote` package, since
it does not provide a (singleton) driver, but a mechanism for drivers
to be added dynamically. As a result, the implementation is oddly
dual-purpose, and a spurious `"remote"` driver is added to the
controller's list of available drivers.
Instead, it is better to provide the registration callback to each
package and let it register its own driver or drivers. That way, the
singleton driver packages can construct one and register it, and the
remote package can hook the callback up with whatever the dynamic
driver mechanism turns out to be.
NB there are some method signature changes; in particular to
controller.New, which can return an error if the built-in driver
packages fail to initialise.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-11 13:46:29 +01:00
|
|
|
// New constructs a new bridge driver
|
|
|
|
func newDriver() driverapi.Driver {
|
2015-05-21 13:13:19 -07:00
|
|
|
return &driver{networks: map[types.UUID]*bridgeNetwork{}}
|
Make driver packages register themselves via DriverCallback
In the present code, each driver package provides a `New()` method
which constructs a driver of its type, which is then registered with
the controller.
However, this is not suitable for the `drivers/remote` package, since
it does not provide a (singleton) driver, but a mechanism for drivers
to be added dynamically. As a result, the implementation is oddly
dual-purpose, and a spurious `"remote"` driver is added to the
controller's list of available drivers.
Instead, it is better to provide the registration callback to each
package and let it register its own driver or drivers. That way, the
singleton driver packages can construct one and register it, and the
remote package can hook the callback up with whatever the dynamic
driver mechanism turns out to be.
NB there are some method signature changes; in particular to
controller.New, which can return an error if the built-in driver
packages fail to initialise.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-11 13:46:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init registers a new instance of bridge driver
|
|
|
|
func Init(dc driverapi.DriverCallback) error {
|
|
|
|
return dc.RegisterDriver(networkType, newDriver())
|
2015-02-22 17:24:22 -08:00
|
|
|
}
|
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
// Validate performs a static validation on the network configuration parameters.
|
2015-04-24 15:13:44 -07:00
|
|
|
// Whatever can be assessed a priori before attempting any programming.
|
2015-05-22 10:56:36 -07:00
|
|
|
func (c *networkConfiguration) Validate() error {
|
2015-04-24 15:13:44 -07:00
|
|
|
if c.Mtu < 0 {
|
2015-05-14 14:56:15 -07:00
|
|
|
return ErrInvalidMtu(c.Mtu)
|
2015-04-24 15:13:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If bridge v4 subnet is specified
|
|
|
|
if c.AddressIPv4 != nil {
|
|
|
|
// If Container restricted subnet is specified, it must be a subset of bridge subnet
|
|
|
|
if c.FixedCIDR != nil {
|
|
|
|
// Check Network address
|
|
|
|
if !c.AddressIPv4.Contains(c.FixedCIDR.IP) {
|
2015-05-14 14:56:15 -07:00
|
|
|
return &ErrInvalidContainerSubnet{}
|
2015-04-24 15:13:44 -07:00
|
|
|
}
|
|
|
|
// Check it is effectively a subset
|
|
|
|
brNetLen, _ := c.AddressIPv4.Mask.Size()
|
|
|
|
cnNetLen, _ := c.FixedCIDR.Mask.Size()
|
|
|
|
if brNetLen > cnNetLen {
|
2015-05-14 14:56:15 -07:00
|
|
|
return &ErrInvalidContainerSubnet{}
|
2015-04-24 15:13:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If default gw is specified, it must be part of bridge subnet
|
|
|
|
if c.DefaultGatewayIPv4 != nil {
|
|
|
|
if !c.AddressIPv4.Contains(c.DefaultGatewayIPv4) {
|
2015-05-14 14:56:15 -07:00
|
|
|
return &ErrInvalidGateway{}
|
2015-04-24 15:13:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If default v6 gw is specified, FixedCIDRv6 must be specified and gw must belong to FixedCIDRv6 subnet
|
|
|
|
if c.EnableIPv6 && c.DefaultGatewayIPv6 != nil {
|
|
|
|
if c.FixedCIDRv6 == nil || !c.FixedCIDRv6.Contains(c.DefaultGatewayIPv6) {
|
2015-05-14 14:56:15 -07:00
|
|
|
return &ErrInvalidGateway{}
|
2015-04-24 15:13:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-21 13:13:19 -07:00
|
|
|
// Conflict check if two NetworkConfiguration objects overlap in the multinetwork
|
|
|
|
func (c *networkConfiguration) Conflict(o *networkConfiguration) bool {
|
|
|
|
if o == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also empty, becasue only one network with empty name is allowed
|
|
|
|
if c.BridgeName == o.BridgeName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// They must be in different subnets
|
|
|
|
|
|
|
|
if (c.AddressIPv4 != nil && o.AddressIPv4.IP != nil) &&
|
|
|
|
(c.AddressIPv4.Contains(o.AddressIPv4.IP) || o.AddressIPv4.Contains(c.AddressIPv4.IP)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-22 10:56:36 -07:00
|
|
|
// FromMap retrieve the configuration data from the map form.
|
|
|
|
func (c *networkConfiguration) FromMap(data map[string]interface{}) error {
|
2015-05-24 11:45:07 -07:00
|
|
|
var err error
|
|
|
|
|
2015-05-22 10:56:36 -07:00
|
|
|
if i, ok := data["BridgeName"]; ok && i != nil {
|
|
|
|
if c.BridgeName, ok = i.(string); !ok {
|
|
|
|
return types.BadRequestErrorf("invalid type for BridgeName value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := data["Mtu"]; ok && i != nil {
|
|
|
|
if s, ok := i.(string); ok {
|
|
|
|
if c.Mtu, err = strconv.Atoi(s); err != nil {
|
|
|
|
return types.BadRequestErrorf("failed to parse Mtu value: %s", err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return types.BadRequestErrorf("invalid type for Mtu value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := data["EnableIPv6"]; ok && i != nil {
|
|
|
|
if s, ok := i.(string); ok {
|
|
|
|
if c.EnableIPv6, err = strconv.ParseBool(s); err != nil {
|
|
|
|
return types.BadRequestErrorf("failed to parse EnableIPv6 value: %s", err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return types.BadRequestErrorf("invalid type for EnableIPv6 value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := data["EnableIPTables"]; ok && i != nil {
|
|
|
|
if s, ok := i.(string); ok {
|
|
|
|
if c.EnableIPTables, err = strconv.ParseBool(s); err != nil {
|
|
|
|
return types.BadRequestErrorf("failed to parse EnableIPTables value: %s", err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return types.BadRequestErrorf("invalid type for EnableIPTables value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := data["EnableIPMasquerade"]; ok && i != nil {
|
|
|
|
if s, ok := i.(string); ok {
|
|
|
|
if c.EnableIPMasquerade, err = strconv.ParseBool(s); err != nil {
|
|
|
|
return types.BadRequestErrorf("failed to parse EnableIPMasquerade value: %s", err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return types.BadRequestErrorf("invalid type for EnableIPMasquerade value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := data["EnableICC"]; ok && i != nil {
|
|
|
|
if s, ok := i.(string); ok {
|
|
|
|
if c.EnableICC, err = strconv.ParseBool(s); err != nil {
|
|
|
|
return types.BadRequestErrorf("failed to parse EnableICC value: %s", err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return types.BadRequestErrorf("invalid type for EnableICC value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := data["AllowNonDefaultBridge"]; ok && i != nil {
|
|
|
|
if s, ok := i.(string); ok {
|
|
|
|
if c.AllowNonDefaultBridge, err = strconv.ParseBool(s); err != nil {
|
|
|
|
return types.BadRequestErrorf("failed to parse AllowNonDefaultBridge value: %s", err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return types.BadRequestErrorf("invalid type for AllowNonDefaultBridge value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := data["AddressIPv4"]; ok && i != nil {
|
|
|
|
if s, ok := i.(string); ok {
|
|
|
|
if ip, nw, e := net.ParseCIDR(s); e == nil {
|
|
|
|
nw.IP = ip
|
|
|
|
c.AddressIPv4 = nw
|
|
|
|
} else {
|
|
|
|
return types.BadRequestErrorf("failed to parse AddressIPv4 value")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return types.BadRequestErrorf("invalid type for AddressIPv4 value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := data["FixedCIDR"]; ok && i != nil {
|
|
|
|
if s, ok := i.(string); ok {
|
|
|
|
if ip, nw, e := net.ParseCIDR(s); e == nil {
|
|
|
|
nw.IP = ip
|
|
|
|
c.FixedCIDR = nw
|
|
|
|
} else {
|
|
|
|
return types.BadRequestErrorf("failed to parse FixedCIDR value")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return types.BadRequestErrorf("invalid type for FixedCIDR value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := data["FixedCIDRv6"]; ok && i != nil {
|
|
|
|
if s, ok := i.(string); ok {
|
|
|
|
if ip, nw, e := net.ParseCIDR(s); e == nil {
|
|
|
|
nw.IP = ip
|
|
|
|
c.FixedCIDRv6 = nw
|
|
|
|
} else {
|
|
|
|
return types.BadRequestErrorf("failed to parse FixedCIDRv6 value")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return types.BadRequestErrorf("invalid type for FixedCIDRv6 value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := data["DefaultGatewayIPv4"]; ok && i != nil {
|
|
|
|
if s, ok := i.(string); ok {
|
|
|
|
if c.DefaultGatewayIPv4 = net.ParseIP(s); c.DefaultGatewayIPv4 == nil {
|
|
|
|
return types.BadRequestErrorf("failed to parse DefaultGatewayIPv4 value")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return types.BadRequestErrorf("invalid type for DefaultGatewayIPv4 value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := data["DefaultGatewayIPv6"]; ok && i != nil {
|
|
|
|
if s, ok := i.(string); ok {
|
|
|
|
if c.DefaultGatewayIPv6 = net.ParseIP(s); c.DefaultGatewayIPv6 == nil {
|
|
|
|
return types.BadRequestErrorf("failed to parse DefaultGatewayIPv6 value")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return types.BadRequestErrorf("invalid type for DefaultGatewayIPv6 value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := data["DefaultBindingIP"]; ok && i != nil {
|
|
|
|
if s, ok := i.(string); ok {
|
|
|
|
if c.DefaultBindingIP = net.ParseIP(s); c.DefaultBindingIP == nil {
|
|
|
|
return types.BadRequestErrorf("failed to parse DefaultBindingIP value")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return types.BadRequestErrorf("invalid type for DefaultBindingIP value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-28 05:57:36 +00:00
|
|
|
func (n *bridgeNetwork) getEndpoint(eid types.UUID) (*bridgeEndpoint, error) {
|
2015-04-20 04:23:04 -07:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
if eid == "" {
|
2015-04-28 05:57:36 +00:00
|
|
|
return nil, InvalidEndpointIDError(eid)
|
2015-04-20 04:23:04 -07:00
|
|
|
}
|
|
|
|
|
2015-04-28 05:57:36 +00:00
|
|
|
if ep, ok := n.endpoints[eid]; ok {
|
|
|
|
return ep, nil
|
2015-04-20 04:23:04 -07:00
|
|
|
}
|
|
|
|
|
2015-04-28 05:57:36 +00:00
|
|
|
return nil, nil
|
2015-04-20 04:23:04 -07:00
|
|
|
}
|
|
|
|
|
2015-04-30 17:57:06 -07:00
|
|
|
func (d *driver) Config(option map[string]interface{}) error {
|
2015-05-22 10:56:36 -07:00
|
|
|
var config *configuration
|
2015-04-15 05:25:42 +00:00
|
|
|
|
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
|
|
|
|
if d.config != nil {
|
2015-05-14 14:56:15 -07:00
|
|
|
return &ErrConfigExists{}
|
2015-04-15 05:25:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 06:41:20 +00:00
|
|
|
genericData, ok := option[netlabel.GenericData]
|
|
|
|
if ok && genericData != nil {
|
2015-04-30 17:57:06 -07:00
|
|
|
switch opt := genericData.(type) {
|
|
|
|
case options.Generic:
|
2015-05-22 10:56:36 -07:00
|
|
|
opaqueConfig, err := options.GenerateFromModel(opt, &configuration{})
|
2015-04-30 17:57:06 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-22 10:56:36 -07:00
|
|
|
config = opaqueConfig.(*configuration)
|
|
|
|
case *configuration:
|
2015-04-30 17:57:06 -07:00
|
|
|
config = opt
|
|
|
|
default:
|
2015-05-14 14:56:15 -07:00
|
|
|
return &ErrInvalidDriverConfig{}
|
2015-04-15 05:25:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 17:57:06 -07:00
|
|
|
d.config = config
|
2015-05-06 06:41:20 +00:00
|
|
|
} else {
|
2015-05-22 10:56:36 -07:00
|
|
|
config = &configuration{}
|
2015-04-24 15:13:44 -07:00
|
|
|
}
|
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
if config.EnableIPForwarding {
|
|
|
|
return setupIPForwarding(config)
|
|
|
|
}
|
|
|
|
|
2015-04-15 05:25:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-01 17:14:04 -07:00
|
|
|
func (d *driver) getNetwork(id types.UUID) (*bridgeNetwork, error) {
|
2015-05-04 13:33:53 -07:00
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
2015-05-21 13:13:19 -07:00
|
|
|
|
|
|
|
if id == "" {
|
|
|
|
return nil, types.BadRequestErrorf("invalid network id: %s", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nw, ok := d.networks[id]; ok {
|
|
|
|
return nw, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
2015-05-01 17:14:04 -07:00
|
|
|
}
|
|
|
|
|
2015-05-24 11:45:07 -07:00
|
|
|
func parseNetworkGenericOptions(data interface{}) (*networkConfiguration, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
config *networkConfiguration
|
|
|
|
)
|
|
|
|
|
|
|
|
switch opt := data.(type) {
|
|
|
|
case *networkConfiguration:
|
|
|
|
config = opt
|
|
|
|
case map[string]interface{}:
|
|
|
|
config = &networkConfiguration{}
|
|
|
|
err = config.FromMap(opt)
|
|
|
|
case options.Generic:
|
|
|
|
var opaqueConfig interface{}
|
|
|
|
if opaqueConfig, err = options.GenerateFromModel(opt, config); err == nil {
|
|
|
|
config = opaqueConfig.(*networkConfiguration)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
err = types.BadRequestErrorf("do not recognize network configuration format: %T", opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
2015-05-22 10:56:36 -07:00
|
|
|
func parseNetworkOptions(option options.Generic) (*networkConfiguration, error) {
|
2015-05-24 11:45:07 -07:00
|
|
|
var err error
|
2015-05-22 10:56:36 -07:00
|
|
|
config := &networkConfiguration{}
|
2015-05-06 05:51:26 +00:00
|
|
|
|
2015-05-24 11:45:07 -07:00
|
|
|
// Parse generic label first, config will be re-assigned
|
|
|
|
if genData, ok := option[netlabel.GenericData]; ok && genData != nil {
|
|
|
|
if config, err = parseNetworkGenericOptions(genData); err != nil {
|
|
|
|
return nil, err
|
2015-05-06 05:51:26 +00:00
|
|
|
}
|
2015-05-22 10:56:36 -07:00
|
|
|
}
|
2015-05-06 05:51:26 +00:00
|
|
|
|
2015-05-24 11:45:07 -07:00
|
|
|
// Process well-known labels next
|
2015-05-06 06:41:20 +00:00
|
|
|
if _, ok := option[netlabel.EnableIPv6]; ok {
|
|
|
|
config.EnableIPv6 = option[netlabel.EnableIPv6].(bool)
|
|
|
|
}
|
|
|
|
|
2015-05-24 11:45:07 -07:00
|
|
|
// Finally validate the configuration
|
|
|
|
if err = config.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2015-04-23 10:49:57 -07:00
|
|
|
// Create a new network using bridge plugin
|
2015-04-30 17:57:06 -07:00
|
|
|
func (d *driver) CreateNetwork(id types.UUID, option map[string]interface{}) error {
|
2015-04-20 04:23:04 -07:00
|
|
|
var err error
|
2015-04-13 18:40:42 +00:00
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Driver must be configured
|
2015-04-13 18:40:42 +00:00
|
|
|
d.Lock()
|
2015-04-15 05:25:42 +00:00
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Sanity checks
|
2015-05-21 13:13:19 -07:00
|
|
|
if _, ok := d.networks[id]; ok {
|
2015-04-13 18:40:42 +00:00
|
|
|
d.Unlock()
|
2015-05-21 13:13:19 -07:00
|
|
|
return types.ForbiddenErrorf("network %s exists", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse and validate the config. It should not conflict with existing networks' config
|
|
|
|
config, err := parseNetworkOptions(option)
|
|
|
|
if err != nil {
|
|
|
|
d.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, nw := range d.networks {
|
|
|
|
if nw.config.Conflict(config) {
|
|
|
|
d.Unlock()
|
|
|
|
return types.ForbiddenErrorf("conflicts with network %s (%s)", nw.id, nw.config.BridgeName)
|
|
|
|
}
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
2015-04-20 04:23:04 -07:00
|
|
|
|
|
|
|
// Create and set network handler in driver
|
2015-05-21 13:13:19 -07:00
|
|
|
network := &bridgeNetwork{id: id, endpoints: make(map[types.UUID]*bridgeEndpoint), config: config}
|
|
|
|
d.networks[id] = network
|
2015-04-13 18:40:42 +00:00
|
|
|
d.Unlock()
|
2015-04-20 04:23:04 -07:00
|
|
|
|
|
|
|
// On failure make sure to reset driver network handler to nil
|
2015-04-13 18:40:42 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
d.Lock()
|
2015-05-21 13:13:19 -07:00
|
|
|
delete(d.networks, id)
|
2015-04-13 18:40:42 +00:00
|
|
|
d.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Create or retrieve the bridge L3 interface
|
2015-04-13 18:40:42 +00:00
|
|
|
bridgeIface := newInterface(config)
|
2015-05-06 05:51:26 +00:00
|
|
|
network.bridge = bridgeIface
|
2015-04-20 04:23:04 -07:00
|
|
|
|
2015-05-21 13:13:19 -07:00
|
|
|
// Verify network does not conflict with previously configured networks
|
|
|
|
// on parameters that were chosen by the driver.
|
|
|
|
d.Lock()
|
|
|
|
for _, nw := range d.networks {
|
|
|
|
if nw.id == id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Verify the name (which may have been set by newInterface()) does not conflict with
|
|
|
|
// existing bridge interfaces. Ironically the system chosen name gets stored in the config...
|
|
|
|
// Basically we are checking if the two original configs were both empty.
|
|
|
|
if nw.config.BridgeName == config.BridgeName {
|
|
|
|
d.Unlock()
|
|
|
|
return types.ForbiddenErrorf("conflicts with network %s (%s)", nw.id, nw.config.BridgeName)
|
|
|
|
}
|
|
|
|
// If this network config specifies the AddressIPv4, we need
|
|
|
|
// to make sure it does not conflict with any previously allocated
|
|
|
|
// bridges. This could not be completely caught by the config conflict
|
|
|
|
// check, because networks which config does not specify the AddressIPv4
|
|
|
|
// get their address and subnet selected by the driver (see electBridgeIPv4())
|
|
|
|
if config.AddressIPv4 != nil {
|
|
|
|
if nw.bridge.bridgeIPv4.Contains(config.AddressIPv4.IP) ||
|
|
|
|
config.AddressIPv4.Contains(nw.bridge.bridgeIPv4.IP) {
|
|
|
|
d.Unlock()
|
|
|
|
return types.ForbiddenErrorf("conflicts with network %s (%s)", nw.id, nw.config.BridgeName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.Unlock()
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Prepare the bridge setup configuration
|
2015-04-15 05:25:42 +00:00
|
|
|
bridgeSetup := newBridgeSetup(config, bridgeIface)
|
2015-02-22 17:24:22 -08:00
|
|
|
|
|
|
|
// If the bridge interface doesn't exist, we need to start the setup steps
|
|
|
|
// by creating a new device and assigning it an IPv4 address.
|
2015-04-13 18:40:42 +00:00
|
|
|
bridgeAlreadyExists := bridgeIface.exists()
|
2015-02-22 17:24:22 -08:00
|
|
|
if !bridgeAlreadyExists {
|
2015-03-04 13:25:43 -08:00
|
|
|
bridgeSetup.queueStep(setupDevice)
|
2015-02-22 17:24:22 -08:00
|
|
|
}
|
|
|
|
|
2015-05-03 20:23:52 +00:00
|
|
|
// Even if a bridge exists try to setup IPv4.
|
|
|
|
bridgeSetup.queueStep(setupBridgeIPv4)
|
|
|
|
|
2015-05-04 09:54:19 +00:00
|
|
|
// Conditionally queue setup steps depending on configuration values.
|
2015-02-22 17:58:52 -08:00
|
|
|
for _, step := range []struct {
|
2015-02-22 17:24:22 -08:00
|
|
|
Condition bool
|
2015-03-04 13:25:43 -08:00
|
|
|
Fn setupStep
|
2015-02-22 17:24:22 -08:00
|
|
|
}{
|
|
|
|
// Enable IPv6 on the bridge if required. We do this even for a
|
|
|
|
// previously existing bridge, as it may be here from a previous
|
|
|
|
// installation where IPv6 wasn't supported yet and needs to be
|
|
|
|
// assigned an IPv6 link-local address.
|
2015-03-04 13:25:43 -08:00
|
|
|
{config.EnableIPv6, setupBridgeIPv6},
|
2015-02-22 17:24:22 -08:00
|
|
|
|
|
|
|
// We ensure that the bridge has the expectedIPv4 and IPv6 addresses in
|
|
|
|
// the case of a previously existing device.
|
2015-04-16 05:02:21 +00:00
|
|
|
{bridgeAlreadyExists, setupVerifyAndReconcile},
|
2015-02-22 17:24:22 -08:00
|
|
|
|
|
|
|
// Setup the bridge to allocate containers IPv4 addresses in the
|
|
|
|
// specified subnet.
|
2015-03-04 13:25:43 -08:00
|
|
|
{config.FixedCIDR != nil, setupFixedCIDRv4},
|
2015-02-22 17:24:22 -08:00
|
|
|
|
|
|
|
// Setup the bridge to allocate containers global IPv6 addresses in the
|
|
|
|
// specified subnet.
|
2015-03-04 13:25:43 -08:00
|
|
|
{config.FixedCIDRv6 != nil, setupFixedCIDRv6},
|
2015-02-22 17:24:22 -08:00
|
|
|
|
2015-05-18 16:49:12 -07:00
|
|
|
// Setup Loopback Adresses Routing
|
|
|
|
{!config.EnableUserlandProxy, setupLoopbackAdressesRouting},
|
|
|
|
|
2015-02-22 17:24:22 -08:00
|
|
|
// Setup IPTables.
|
2015-03-04 13:25:43 -08:00
|
|
|
{config.EnableIPTables, setupIPTables},
|
2015-02-22 17:24:22 -08:00
|
|
|
|
2015-04-24 15:13:44 -07:00
|
|
|
// Setup DefaultGatewayIPv4
|
|
|
|
{config.DefaultGatewayIPv4 != nil, setupGatewayIPv4},
|
|
|
|
|
|
|
|
// Setup DefaultGatewayIPv6
|
|
|
|
{config.DefaultGatewayIPv6 != nil, setupGatewayIPv6},
|
2015-02-22 17:58:52 -08:00
|
|
|
} {
|
2015-02-22 17:24:22 -08:00
|
|
|
if step.Condition {
|
2015-03-04 13:25:43 -08:00
|
|
|
bridgeSetup.queueStep(step.Fn)
|
2015-02-22 17:24:22 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 20:23:52 +00:00
|
|
|
// Block bridge IP from being allocated.
|
|
|
|
bridgeSetup.queueStep(allocateBridgeIP)
|
2015-02-22 17:24:22 -08:00
|
|
|
// Apply the prepared list of steps, and abort at the first error.
|
2015-03-04 13:25:43 -08:00
|
|
|
bridgeSetup.queueStep(setupDeviceUp)
|
2015-04-13 18:40:42 +00:00
|
|
|
if err = bridgeSetup.apply(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-20 08:44:06 -07:00
|
|
|
func (d *driver) DeleteNetwork(nid types.UUID) error {
|
2015-04-13 18:40:42 +00:00
|
|
|
var err error
|
2015-04-20 04:23:04 -07:00
|
|
|
|
|
|
|
// Get network handler and remove it from driver
|
2015-04-13 18:40:42 +00:00
|
|
|
d.Lock()
|
2015-05-21 13:13:19 -07:00
|
|
|
n, ok := d.networks[nid]
|
|
|
|
if !ok {
|
|
|
|
d.Unlock()
|
|
|
|
return types.InternalMaskableErrorf("network %s does not exist", nid)
|
|
|
|
}
|
|
|
|
delete(d.networks, nid)
|
2015-04-13 18:40:42 +00:00
|
|
|
d.Unlock()
|
2015-04-20 04:23:04 -07:00
|
|
|
|
|
|
|
// On failure set network handler back in driver, but
|
|
|
|
// only if is not already taken over by some other thread
|
2015-04-13 18:40:42 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
d.Lock()
|
2015-05-21 13:13:19 -07:00
|
|
|
if _, ok := d.networks[nid]; !ok {
|
|
|
|
d.networks[nid] = n
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
d.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Sanity check
|
2015-04-13 18:40:42 +00:00
|
|
|
if n == nil {
|
2015-05-14 14:56:15 -07:00
|
|
|
err = driverapi.ErrNoNetwork(nid)
|
2015-04-13 18:40:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Cannot remove network if endpoints are still present
|
|
|
|
if len(n.endpoints) != 0 {
|
|
|
|
err = ActiveEndpointsError(n.id)
|
2015-04-13 18:40:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Programming
|
2015-04-13 18:40:42 +00:00
|
|
|
err = netlink.LinkDel(n.bridge.Link)
|
2015-04-20 04:23:04 -07:00
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
func (d *driver) CreateEndpoint(nid, eid types.UUID, epInfo driverapi.EndpointInfo, epOptions map[string]interface{}) error {
|
2015-04-13 18:40:42 +00:00
|
|
|
var (
|
2015-04-17 15:42:23 -07:00
|
|
|
ipv6Addr *net.IPNet
|
2015-04-13 18:40:42 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
if epInfo == nil {
|
|
|
|
return errors.New("invalid endpoint info passed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(epInfo.Interfaces()) != 0 {
|
|
|
|
return errors.New("non empty interface list passed to bridge(local) driver")
|
|
|
|
}
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Get the network handler and make sure it exists
|
2015-04-13 18:40:42 +00:00
|
|
|
d.Lock()
|
2015-05-21 13:13:19 -07:00
|
|
|
n, ok := d.networks[nid]
|
|
|
|
if !ok {
|
|
|
|
d.Unlock()
|
|
|
|
return types.NotFoundErrorf("network %s does not exist", nid)
|
|
|
|
}
|
2015-05-06 05:51:26 +00:00
|
|
|
config := n.config
|
2015-04-13 18:40:42 +00:00
|
|
|
d.Unlock()
|
|
|
|
if n == nil {
|
2015-05-14 14:56:15 -07:00
|
|
|
return driverapi.ErrNoNetwork(nid)
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Sanity check
|
2015-04-13 18:40:42 +00:00
|
|
|
n.Lock()
|
|
|
|
if n.id != nid {
|
|
|
|
n.Unlock()
|
2015-05-14 06:23:45 +00:00
|
|
|
return InvalidNetworkIDError(nid)
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
2015-04-20 04:23:04 -07:00
|
|
|
n.Unlock()
|
2015-04-13 18:40:42 +00:00
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Check if endpoint id is good and retrieve correspondent endpoint
|
2015-04-28 05:57:36 +00:00
|
|
|
ep, err := n.getEndpoint(eid)
|
2015-04-20 04:23:04 -07:00
|
|
|
if err != nil {
|
2015-05-14 06:23:45 +00:00
|
|
|
return err
|
2015-04-20 04:23:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Endpoint with that id exists either on desired or other sandbox
|
|
|
|
if ep != nil {
|
2015-05-14 14:56:15 -07:00
|
|
|
return driverapi.ErrEndpointExists(eid)
|
2015-04-20 04:23:04 -07:00
|
|
|
}
|
|
|
|
|
2015-04-23 11:15:15 -07:00
|
|
|
// Try to convert the options to endpoint configuration
|
|
|
|
epConfig, err := parseEndpointOptions(epOptions)
|
|
|
|
if err != nil {
|
2015-05-14 06:23:45 +00:00
|
|
|
return err
|
2015-04-23 11:15:15 -07:00
|
|
|
}
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Create and add the endpoint
|
2015-04-28 05:57:36 +00:00
|
|
|
n.Lock()
|
2015-04-23 11:15:15 -07:00
|
|
|
endpoint := &bridgeEndpoint{id: eid, config: epConfig}
|
2015-04-28 05:57:36 +00:00
|
|
|
n.endpoints[eid] = endpoint
|
2015-04-13 18:40:42 +00:00
|
|
|
n.Unlock()
|
2015-04-20 04:23:04 -07:00
|
|
|
|
|
|
|
// On failure make sure to remove the endpoint
|
2015-04-13 18:40:42 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
n.Lock()
|
2015-04-28 05:57:36 +00:00
|
|
|
delete(n.endpoints, eid)
|
2015-04-13 18:40:42 +00:00
|
|
|
n.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Generate a name for what will be the host side pipe interface
|
2015-04-13 18:40:42 +00:00
|
|
|
name1, err := generateIfaceName()
|
|
|
|
if err != nil {
|
2015-05-14 06:23:45 +00:00
|
|
|
return err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Generate a name for what will be the sandbox side pipe interface
|
2015-04-13 18:40:42 +00:00
|
|
|
name2, err := generateIfaceName()
|
|
|
|
if err != nil {
|
2015-05-14 06:23:45 +00:00
|
|
|
return err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Generate and add the interface pipe host <-> sandbox
|
2015-04-13 18:40:42 +00:00
|
|
|
veth := &netlink.Veth{
|
|
|
|
LinkAttrs: netlink.LinkAttrs{Name: name1, TxQLen: 0},
|
|
|
|
PeerName: name2}
|
|
|
|
if err = netlink.LinkAdd(veth); err != nil {
|
2015-05-14 06:23:45 +00:00
|
|
|
return err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Get the host side pipe interface handler
|
2015-04-13 18:40:42 +00:00
|
|
|
host, err := netlink.LinkByName(name1)
|
|
|
|
if err != nil {
|
2015-05-14 06:23:45 +00:00
|
|
|
return err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
netlink.LinkDel(host)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Get the sandbox side pipe interface handler
|
|
|
|
sbox, err := netlink.LinkByName(name2)
|
2015-04-13 18:40:42 +00:00
|
|
|
if err != nil {
|
2015-05-14 06:23:45 +00:00
|
|
|
return err
|
2015-02-22 17:24:22 -08:00
|
|
|
}
|
2015-04-23 13:39:41 -07:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
netlink.LinkDel(sbox)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-23 11:15:15 -07:00
|
|
|
|
2015-05-01 13:51:17 -07:00
|
|
|
// Set the sbox's MAC. If specified, use the one configured by user, otherwise use a random one
|
|
|
|
mac := electMacAddress(epConfig)
|
2015-04-28 05:57:36 +00:00
|
|
|
err = netlink.LinkSetHardwareAddr(sbox, mac)
|
|
|
|
if err != nil {
|
2015-05-14 06:23:45 +00:00
|
|
|
return err
|
2015-04-23 11:15:15 -07:00
|
|
|
}
|
2015-05-05 03:27:34 +00:00
|
|
|
endpoint.macAddress = mac
|
2015-04-23 11:15:15 -07:00
|
|
|
|
2015-04-23 13:39:41 -07:00
|
|
|
// Add bridge inherited attributes to pipe interfaces
|
|
|
|
if config.Mtu != 0 {
|
|
|
|
err = netlink.LinkSetMTU(host, config.Mtu)
|
2015-04-13 18:40:42 +00:00
|
|
|
if err != nil {
|
2015-05-14 06:23:45 +00:00
|
|
|
return err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
2015-04-23 13:39:41 -07:00
|
|
|
err = netlink.LinkSetMTU(sbox, config.Mtu)
|
|
|
|
if err != nil {
|
2015-05-14 06:23:45 +00:00
|
|
|
return err
|
2015-04-23 13:39:41 -07:00
|
|
|
}
|
|
|
|
}
|
2015-04-13 18:40:42 +00:00
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Attach host side pipe interface into the bridge
|
2015-04-13 18:40:42 +00:00
|
|
|
if err = netlink.LinkSetMaster(host,
|
2015-04-15 05:25:42 +00:00
|
|
|
&netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: config.BridgeName}}); err != nil {
|
2015-05-14 06:23:45 +00:00
|
|
|
return err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 11:15:15 -07:00
|
|
|
// v4 address for the sandbox side pipe interface
|
2015-04-13 18:40:42 +00:00
|
|
|
ip4, err := ipAllocator.RequestIP(n.bridge.bridgeIPv4, nil)
|
|
|
|
if err != nil {
|
2015-05-14 06:23:45 +00:00
|
|
|
return err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
2015-04-17 15:42:23 -07:00
|
|
|
ipv4Addr := &net.IPNet{IP: ip4, Mask: n.bridge.bridgeIPv4.Mask}
|
2015-04-13 18:40:42 +00:00
|
|
|
|
2015-04-23 11:15:15 -07:00
|
|
|
// v6 address for the sandbox side pipe interface
|
2015-05-14 06:23:45 +00:00
|
|
|
ipv6Addr = &net.IPNet{}
|
2015-04-15 05:25:42 +00:00
|
|
|
if config.EnableIPv6 {
|
2015-04-28 05:57:36 +00:00
|
|
|
var ip6 net.IP
|
|
|
|
|
|
|
|
network := n.bridge.bridgeIPv6
|
|
|
|
if config.FixedCIDRv6 != nil {
|
|
|
|
network = config.FixedCIDRv6
|
|
|
|
}
|
|
|
|
|
|
|
|
ones, _ := network.Mask.Size()
|
|
|
|
if ones <= 80 {
|
|
|
|
ip6 = make(net.IP, len(network.IP))
|
|
|
|
copy(ip6, network.IP)
|
|
|
|
for i, h := range mac {
|
|
|
|
ip6[i+10] = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ip6, err := ipAllocator.RequestIP(network, ip6)
|
2015-04-13 18:40:42 +00:00
|
|
|
if err != nil {
|
2015-05-14 06:23:45 +00:00
|
|
|
return err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
2015-04-28 05:57:36 +00:00
|
|
|
|
|
|
|
ipv6Addr = &net.IPNet{IP: ip6, Mask: network.Mask}
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-01 17:01:21 -07:00
|
|
|
// Create the sandbox side pipe interface
|
2015-04-20 08:44:06 -07:00
|
|
|
intf := &sandbox.Interface{}
|
2015-04-13 18:40:42 +00:00
|
|
|
intf.SrcName = name2
|
2015-05-21 18:04:49 +00:00
|
|
|
intf.DstName = containerVethPrefix
|
2015-04-14 01:36:58 +00:00
|
|
|
intf.Address = ipv4Addr
|
2015-04-20 04:23:04 -07:00
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
if config.EnableIPv6 {
|
|
|
|
intf.AddressIPv6 = ipv6Addr
|
|
|
|
}
|
|
|
|
|
2015-05-01 17:01:21 -07:00
|
|
|
// Store the interface in endpoint, this is needed for cleanup on DeleteEndpoint()
|
2015-05-01 17:14:04 -07:00
|
|
|
endpoint.intf = intf
|
2015-04-20 04:23:04 -07:00
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
err = epInfo.AddInterface(ifaceID, endpoint.macAddress, *ipv4Addr, *ipv6Addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-01 17:01:21 -07:00
|
|
|
// Program any required port mapping and store them in the endpoint
|
2015-05-18 16:49:12 -07:00
|
|
|
endpoint.portMapping, err = allocatePorts(epConfig, intf, config.DefaultBindingIP, config.EnableUserlandProxy)
|
2015-05-01 17:01:21 -07:00
|
|
|
if err != nil {
|
2015-05-14 06:23:45 +00:00
|
|
|
return err
|
2015-05-01 17:01:21 -07:00
|
|
|
}
|
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
return nil
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 08:44:06 -07:00
|
|
|
func (d *driver) DeleteEndpoint(nid, eid types.UUID) error {
|
2015-04-13 18:40:42 +00:00
|
|
|
var err error
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Get the network handler and make sure it exists
|
2015-04-13 18:40:42 +00:00
|
|
|
d.Lock()
|
2015-05-21 13:13:19 -07:00
|
|
|
n, ok := d.networks[nid]
|
|
|
|
if !ok {
|
|
|
|
d.Unlock()
|
|
|
|
return types.NotFoundErrorf("network %s does not exist", nid)
|
|
|
|
}
|
2015-05-06 05:51:26 +00:00
|
|
|
config := n.config
|
2015-04-13 18:40:42 +00:00
|
|
|
d.Unlock()
|
|
|
|
if n == nil {
|
2015-05-14 14:56:15 -07:00
|
|
|
return driverapi.ErrNoNetwork(nid)
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Sanity Check
|
2015-04-13 18:40:42 +00:00
|
|
|
n.Lock()
|
|
|
|
if n.id != nid {
|
|
|
|
n.Unlock()
|
2015-04-17 02:47:12 +00:00
|
|
|
return InvalidNetworkIDError(nid)
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
2015-04-20 04:23:04 -07:00
|
|
|
n.Unlock()
|
2015-04-13 18:40:42 +00:00
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Check endpoint id and if an endpoint is actually there
|
2015-04-28 05:57:36 +00:00
|
|
|
ep, err := n.getEndpoint(eid)
|
2015-04-20 04:23:04 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
2015-04-20 04:23:04 -07:00
|
|
|
if ep == nil {
|
|
|
|
return EndpointNotFoundError(eid)
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Remove it
|
|
|
|
n.Lock()
|
2015-04-28 05:57:36 +00:00
|
|
|
delete(n.endpoints, eid)
|
2015-04-13 18:40:42 +00:00
|
|
|
n.Unlock()
|
2015-04-20 04:23:04 -07:00
|
|
|
|
|
|
|
// On failure make sure to set back ep in n.endpoints, but only
|
|
|
|
// if it hasn't been taken over already by some other thread.
|
2015-04-13 18:40:42 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
n.Lock()
|
2015-04-28 05:57:36 +00:00
|
|
|
if _, ok := n.endpoints[eid]; !ok {
|
|
|
|
n.endpoints[eid] = ep
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
n.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
2015-02-22 17:24:22 -08:00
|
|
|
|
2015-05-01 17:01:21 -07:00
|
|
|
// Remove port mappings. Do not stop endpoint delete on unmap failure
|
|
|
|
releasePorts(ep)
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Release the v4 address allocated to this endpoint's sandbox interface
|
2015-05-01 17:14:04 -07:00
|
|
|
err = ipAllocator.ReleaseIP(n.bridge.bridgeIPv4, ep.intf.Address.IP)
|
2015-04-13 18:40:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Release the v6 address allocated to this endpoint's sandbox interface
|
2015-04-15 05:25:42 +00:00
|
|
|
if config.EnableIPv6 {
|
2015-05-01 17:14:04 -07:00
|
|
|
err := ipAllocator.ReleaseIP(n.bridge.bridgeIPv6, ep.intf.AddressIPv6.IP)
|
2015-04-13 18:40:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 04:23:04 -07:00
|
|
|
// Try removal of link. Discard error: link pair might have
|
|
|
|
// already been deleted by sandbox delete.
|
2015-05-01 17:14:04 -07:00
|
|
|
link, err := netlink.LinkByName(ep.intf.SrcName)
|
2015-04-20 04:23:04 -07:00
|
|
|
if err == nil {
|
|
|
|
netlink.LinkDel(link)
|
|
|
|
}
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
func (d *driver) EndpointOperInfo(nid, eid types.UUID) (map[string]interface{}, error) {
|
2015-05-04 11:49:53 -07:00
|
|
|
// Get the network handler and make sure it exists
|
|
|
|
d.Lock()
|
2015-05-21 13:13:19 -07:00
|
|
|
n, ok := d.networks[nid]
|
|
|
|
if !ok {
|
|
|
|
d.Unlock()
|
|
|
|
return nil, types.NotFoundErrorf("network %s does not exist", nid)
|
|
|
|
}
|
2015-05-04 11:49:53 -07:00
|
|
|
d.Unlock()
|
|
|
|
if n == nil {
|
2015-05-14 14:56:15 -07:00
|
|
|
return nil, driverapi.ErrNoNetwork(nid)
|
2015-05-04 11:49:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check
|
|
|
|
n.Lock()
|
|
|
|
if n.id != nid {
|
|
|
|
n.Unlock()
|
|
|
|
return nil, InvalidNetworkIDError(nid)
|
|
|
|
}
|
|
|
|
n.Unlock()
|
|
|
|
|
|
|
|
// Check if endpoint id is good and retrieve correspondent endpoint
|
|
|
|
ep, err := n.getEndpoint(eid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ep == nil {
|
2015-05-14 14:56:15 -07:00
|
|
|
return nil, driverapi.ErrNoEndpoint(eid)
|
2015-05-04 11:49:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
|
|
|
|
if ep.portMapping != nil {
|
|
|
|
// Return a copy of the operational data
|
2015-05-20 20:28:46 +00:00
|
|
|
pmc := make([]types.PortBinding, 0, len(ep.portMapping))
|
2015-05-04 11:49:53 -07:00
|
|
|
for _, pm := range ep.portMapping {
|
|
|
|
pmc = append(pmc, pm.GetCopy())
|
|
|
|
}
|
2015-05-06 04:19:57 +00:00
|
|
|
m[netlabel.PortMap] = pmc
|
2015-05-04 11:49:53 -07:00
|
|
|
}
|
|
|
|
|
2015-05-05 03:27:34 +00:00
|
|
|
if len(ep.macAddress) != 0 {
|
2015-05-06 04:19:57 +00:00
|
|
|
m[netlabel.MacAddress] = ep.macAddress
|
2015-05-05 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 11:49:53 -07:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2015-04-30 14:52:46 -07:00
|
|
|
// Join method is invoked when a Sandbox is attached to an endpoint.
|
2015-05-14 06:23:45 +00:00
|
|
|
func (d *driver) Join(nid, eid types.UUID, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
|
2015-05-06 05:51:26 +00:00
|
|
|
network, err := d.getNetwork(nid)
|
|
|
|
if err != nil {
|
2015-05-14 06:23:45 +00:00
|
|
|
return err
|
2015-05-06 05:51:26 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
endpoint, err := network.getEndpoint(eid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-05-01 17:14:04 -07:00
|
|
|
}
|
2015-05-06 05:51:26 +00:00
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
if endpoint == nil {
|
|
|
|
return EndpointNotFoundError(eid)
|
|
|
|
}
|
2015-04-30 14:52:46 -07:00
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
for _, iNames := range jinfo.InterfaceNames() {
|
|
|
|
// Make sure to set names on the correct interface ID.
|
|
|
|
if iNames.ID() == ifaceID {
|
|
|
|
err = iNames.SetNames(endpoint.intf.SrcName, endpoint.intf.DstName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = jinfo.SetGateway(network.bridge.gatewayIPv4)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = jinfo.SetGatewayIPv6(network.bridge.gatewayIPv6)
|
2015-05-06 05:51:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !network.config.EnableICC {
|
2015-05-14 06:23:45 +00:00
|
|
|
return d.link(network, endpoint, options, true)
|
2015-05-01 17:14:04 -07:00
|
|
|
}
|
2015-05-06 05:51:26 +00:00
|
|
|
|
2015-05-04 13:33:53 -07:00
|
|
|
return nil
|
2015-05-01 17:14:04 -07:00
|
|
|
}
|
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
// Leave method is invoked when a Sandbox detaches from an endpoint.
|
|
|
|
func (d *driver) Leave(nid, eid types.UUID) error {
|
2015-05-01 17:14:04 -07:00
|
|
|
network, err := d.getNetwork(nid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-14 06:23:45 +00:00
|
|
|
|
2015-05-01 17:14:04 -07:00
|
|
|
endpoint, err := network.getEndpoint(eid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint == nil {
|
|
|
|
return EndpointNotFoundError(eid)
|
|
|
|
}
|
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
if !network.config.EnableICC {
|
|
|
|
return d.link(network, endpoint, nil, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) link(network *bridgeNetwork, endpoint *bridgeEndpoint, options map[string]interface{}, enable bool) error {
|
|
|
|
var (
|
2015-05-22 10:56:36 -07:00
|
|
|
cc *containerConfiguration
|
2015-05-14 06:23:45 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2015-05-06 00:33:08 +00:00
|
|
|
if enable {
|
|
|
|
cc, err = parseContainerOptions(options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cc = endpoint.containerConfig
|
2015-05-01 17:14:04 -07:00
|
|
|
}
|
2015-05-06 00:33:08 +00:00
|
|
|
|
2015-05-01 17:14:04 -07:00
|
|
|
if cc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-05 10:46:18 +08:00
|
|
|
if endpoint.config != nil && endpoint.config.ExposedPorts != nil {
|
2015-05-05 20:46:11 +00:00
|
|
|
for _, p := range cc.ParentEndpoints {
|
2015-05-01 17:14:04 -07:00
|
|
|
var parentEndpoint *bridgeEndpoint
|
2015-05-05 20:46:11 +00:00
|
|
|
parentEndpoint, err = network.getEndpoint(types.UUID(p))
|
2015-05-01 17:14:04 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if parentEndpoint == nil {
|
2015-05-05 20:46:11 +00:00
|
|
|
err = InvalidEndpointIDError(p)
|
2015-05-01 17:14:04 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
l := newLink(parentEndpoint.intf.Address.IP.String(),
|
|
|
|
endpoint.intf.Address.IP.String(),
|
2015-05-06 05:51:26 +00:00
|
|
|
endpoint.config.ExposedPorts, network.config.BridgeName)
|
2015-05-01 17:14:04 -07:00
|
|
|
if enable {
|
|
|
|
err = l.Enable()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
l.Disable()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
l.Disable()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 20:46:11 +00:00
|
|
|
for _, c := range cc.ChildEndpoints {
|
2015-05-01 17:14:04 -07:00
|
|
|
var childEndpoint *bridgeEndpoint
|
2015-05-05 20:46:11 +00:00
|
|
|
childEndpoint, err = network.getEndpoint(types.UUID(c))
|
2015-05-01 17:14:04 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if childEndpoint == nil {
|
2015-05-05 20:46:11 +00:00
|
|
|
err = InvalidEndpointIDError(c)
|
2015-05-01 17:14:04 -07:00
|
|
|
return err
|
|
|
|
}
|
2015-05-05 10:46:18 +08:00
|
|
|
if childEndpoint.config == nil || childEndpoint.config.ExposedPorts == nil {
|
2015-05-01 17:14:04 -07:00
|
|
|
continue
|
|
|
|
}
|
2015-05-06 05:51:26 +00:00
|
|
|
|
2015-05-01 17:14:04 -07:00
|
|
|
l := newLink(endpoint.intf.Address.IP.String(),
|
|
|
|
childEndpoint.intf.Address.IP.String(),
|
2015-05-06 05:51:26 +00:00
|
|
|
childEndpoint.config.ExposedPorts, network.config.BridgeName)
|
2015-05-01 17:14:04 -07:00
|
|
|
if enable {
|
|
|
|
err = l.Enable()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
l.Disable()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
l.Disable()
|
|
|
|
}
|
|
|
|
}
|
2015-05-06 00:33:08 +00:00
|
|
|
|
|
|
|
if enable {
|
|
|
|
endpoint.containerConfig = cc
|
|
|
|
}
|
|
|
|
|
2015-04-30 14:52:46 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-22 16:47:07 -07:00
|
|
|
func (d *driver) Type() string {
|
|
|
|
return networkType
|
|
|
|
}
|
|
|
|
|
2015-05-22 10:56:36 -07:00
|
|
|
func parseEndpointOptions(epOptions map[string]interface{}) (*endpointConfiguration, error) {
|
2015-04-23 11:15:15 -07:00
|
|
|
if epOptions == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2015-05-01 17:01:21 -07:00
|
|
|
|
2015-05-22 10:56:36 -07:00
|
|
|
ec := &endpointConfiguration{}
|
2015-05-01 17:01:21 -07:00
|
|
|
|
2015-05-06 04:19:57 +00:00
|
|
|
if opt, ok := epOptions[netlabel.MacAddress]; ok {
|
2015-05-01 17:01:21 -07:00
|
|
|
if mac, ok := opt.(net.HardwareAddr); ok {
|
|
|
|
ec.MacAddress = mac
|
|
|
|
} else {
|
2015-05-14 14:56:15 -07:00
|
|
|
return nil, &ErrInvalidEndpointConfig{}
|
2015-05-01 17:01:21 -07:00
|
|
|
}
|
2015-04-30 17:57:06 -07:00
|
|
|
}
|
2015-05-01 17:01:21 -07:00
|
|
|
|
2015-05-06 04:19:57 +00:00
|
|
|
if opt, ok := epOptions[netlabel.PortMap]; ok {
|
2015-05-20 20:28:46 +00:00
|
|
|
if bs, ok := opt.([]types.PortBinding); ok {
|
2015-05-01 17:01:21 -07:00
|
|
|
ec.PortBindings = bs
|
|
|
|
} else {
|
2015-05-14 14:56:15 -07:00
|
|
|
return nil, &ErrInvalidEndpointConfig{}
|
2015-04-23 11:15:15 -07:00
|
|
|
}
|
|
|
|
}
|
2015-05-01 17:01:21 -07:00
|
|
|
|
2015-05-06 04:19:57 +00:00
|
|
|
if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
|
2015-05-20 20:28:46 +00:00
|
|
|
if ports, ok := opt.([]types.TransportPort); ok {
|
2015-05-05 10:46:18 +08:00
|
|
|
ec.ExposedPorts = ports
|
|
|
|
} else {
|
2015-05-14 14:56:15 -07:00
|
|
|
return nil, &ErrInvalidEndpointConfig{}
|
2015-05-05 10:46:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 17:01:21 -07:00
|
|
|
return ec, nil
|
2015-04-23 11:15:15 -07:00
|
|
|
}
|
|
|
|
|
2015-05-22 10:56:36 -07:00
|
|
|
func parseContainerOptions(cOptions map[string]interface{}) (*containerConfiguration, error) {
|
2015-04-30 14:52:46 -07:00
|
|
|
if cOptions == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2015-05-06 04:19:57 +00:00
|
|
|
genericData := cOptions[netlabel.GenericData]
|
2015-05-01 17:14:04 -07:00
|
|
|
if genericData == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
switch opt := genericData.(type) {
|
2015-04-30 14:52:46 -07:00
|
|
|
case options.Generic:
|
2015-05-22 10:56:36 -07:00
|
|
|
opaqueConfig, err := options.GenerateFromModel(opt, &containerConfiguration{})
|
2015-04-30 14:52:46 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-22 10:56:36 -07:00
|
|
|
return opaqueConfig.(*containerConfiguration), nil
|
|
|
|
case *containerConfiguration:
|
2015-04-30 14:52:46 -07:00
|
|
|
return opt, nil
|
|
|
|
default:
|
2015-05-01 17:14:04 -07:00
|
|
|
return nil, nil
|
2015-04-30 14:52:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 10:56:36 -07:00
|
|
|
func electMacAddress(epConfig *endpointConfiguration) net.HardwareAddr {
|
2015-05-01 13:51:17 -07:00
|
|
|
if epConfig != nil && epConfig.MacAddress != nil {
|
|
|
|
return epConfig.MacAddress
|
|
|
|
}
|
|
|
|
return netutils.GenerateRandomMAC()
|
|
|
|
}
|
|
|
|
|
2015-04-14 00:12:40 -04:00
|
|
|
// Generates a name to be used for a virtual ethernet
|
|
|
|
// interface. The name is constructed by 'veth' appended
|
|
|
|
// by a randomly generated hex value. (example: veth0f60e2c)
|
2015-04-13 18:40:42 +00:00
|
|
|
func generateIfaceName() (string, error) {
|
2015-04-14 00:12:40 -04:00
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
name, err := netutils.GenerateRandomName(vethPrefix, vethLen)
|
2015-04-13 18:40:42 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, err := net.InterfaceByName(name); err != nil {
|
|
|
|
if strings.Contains(err.Error(), "no such") {
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2015-05-14 14:56:15 -07:00
|
|
|
return "", &ErrIfaceName{}
|
2015-02-22 17:24:22 -08:00
|
|
|
}
|