12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151 |
- package bridge
- import (
- "errors"
- "net"
- "strconv"
- "strings"
- "sync"
- "github.com/docker/libnetwork/driverapi"
- "github.com/docker/libnetwork/ipallocator"
- "github.com/docker/libnetwork/netlabel"
- "github.com/docker/libnetwork/netutils"
- "github.com/docker/libnetwork/options"
- "github.com/docker/libnetwork/portmapper"
- "github.com/docker/libnetwork/sandbox"
- "github.com/docker/libnetwork/types"
- "github.com/vishvananda/netlink"
- )
- const (
- networkType = "bridge"
- vethPrefix = "veth"
- vethLen = 7
- containerVethPrefix = "eth"
- maxAllocatePortAttempts = 10
- ifaceID = 1
- )
- var (
- ipAllocator *ipallocator.IPAllocator
- portMapper *portmapper.PortMapper
- )
- // configuration info for the "bridge" driver.
- type configuration struct {
- EnableIPForwarding bool
- }
- // networkConfiguration for network specific configuration
- type networkConfiguration struct {
- BridgeName string
- AddressIPv4 *net.IPNet
- FixedCIDR *net.IPNet
- FixedCIDRv6 *net.IPNet
- EnableIPv6 bool
- EnableIPTables bool
- EnableIPMasquerade bool
- EnableICC bool
- Mtu int
- DefaultGatewayIPv4 net.IP
- DefaultGatewayIPv6 net.IP
- DefaultBindingIP net.IP
- AllowNonDefaultBridge bool
- EnableUserlandProxy bool
- }
- // endpointConfiguration represents the user specified configuration for the sandbox endpoint
- type endpointConfiguration struct {
- MacAddress net.HardwareAddr
- PortBindings []types.PortBinding
- ExposedPorts []types.TransportPort
- }
- // containerConfiguration represents the user specified configuration for a container
- type containerConfiguration struct {
- ParentEndpoints []string
- ChildEndpoints []string
- }
- type bridgeEndpoint struct {
- id types.UUID
- intf *sandbox.Interface
- macAddress net.HardwareAddr
- config *endpointConfiguration // User specified parameters
- containerConfig *containerConfiguration
- portMapping []types.PortBinding // Operation port bindings
- }
- type bridgeNetwork struct {
- id types.UUID
- bridge *bridgeInterface // The bridge's L3 interface
- config *networkConfiguration
- endpoints map[types.UUID]*bridgeEndpoint // key: endpoint id
- sync.Mutex
- }
- type driver struct {
- config *configuration
- network *bridgeNetwork
- networks map[types.UUID]*bridgeNetwork
- sync.Mutex
- }
- func init() {
- ipAllocator = ipallocator.New()
- portMapper = portmapper.New()
- }
- // New constructs a new bridge driver
- func newDriver() driverapi.Driver {
- return &driver{networks: map[types.UUID]*bridgeNetwork{}}
- }
- // Init registers a new instance of bridge driver
- func Init(dc driverapi.DriverCallback) error {
- return dc.RegisterDriver(networkType, newDriver())
- }
- // Validate performs a static validation on the network configuration parameters.
- // Whatever can be assessed a priori before attempting any programming.
- func (c *networkConfiguration) Validate() error {
- if c.Mtu < 0 {
- return ErrInvalidMtu(c.Mtu)
- }
- // 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) {
- return &ErrInvalidContainerSubnet{}
- }
- // Check it is effectively a subset
- brNetLen, _ := c.AddressIPv4.Mask.Size()
- cnNetLen, _ := c.FixedCIDR.Mask.Size()
- if brNetLen > cnNetLen {
- return &ErrInvalidContainerSubnet{}
- }
- }
- // If default gw is specified, it must be part of bridge subnet
- if c.DefaultGatewayIPv4 != nil {
- if !c.AddressIPv4.Contains(c.DefaultGatewayIPv4) {
- return &ErrInvalidGateway{}
- }
- }
- }
- // 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) {
- return &ErrInvalidGateway{}
- }
- }
- return nil
- }
- // 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
- }
- // FromMap retrieve the configuration data from the map form.
- func (c *networkConfiguration) FromMap(data map[string]interface{}) error {
- if i, ok := data["BridgeName"]; ok && i != nil {
- if c.BridgeName, ok = i.(string); !ok {
- return types.BadRequestErrorf("invalid type for BridgeName value")
- }
- }
- var err error
- 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
- }
- func (n *bridgeNetwork) getEndpoint(eid types.UUID) (*bridgeEndpoint, error) {
- n.Lock()
- defer n.Unlock()
- if eid == "" {
- return nil, InvalidEndpointIDError(eid)
- }
- if ep, ok := n.endpoints[eid]; ok {
- return ep, nil
- }
- return nil, nil
- }
- func (d *driver) Config(option map[string]interface{}) error {
- var config *configuration
- d.Lock()
- defer d.Unlock()
- if d.config != nil {
- return &ErrConfigExists{}
- }
- genericData, ok := option[netlabel.GenericData]
- if ok && genericData != nil {
- switch opt := genericData.(type) {
- case options.Generic:
- opaqueConfig, err := options.GenerateFromModel(opt, &configuration{})
- if err != nil {
- return err
- }
- config = opaqueConfig.(*configuration)
- case *configuration:
- config = opt
- default:
- return &ErrInvalidDriverConfig{}
- }
- d.config = config
- } else {
- config = &configuration{}
- }
- if config.EnableIPForwarding {
- return setupIPForwarding(config)
- }
- return nil
- }
- func (d *driver) getNetwork(id types.UUID) (*bridgeNetwork, error) {
- d.Lock()
- defer d.Unlock()
- if id == "" {
- return nil, types.BadRequestErrorf("invalid network id: %s", id)
- }
- if nw, ok := d.networks[id]; ok {
- return nw, nil
- }
- return nil, nil
- }
- func parseNetworkOptions(option options.Generic) (*networkConfiguration, error) {
- config := &networkConfiguration{}
- if genData, ok := option[netlabel.GenericData]; ok && genData != nil {
- switch opt := genData.(type) {
- case map[string]interface{}:
- if err := config.FromMap(opt); err != nil {
- return nil, err
- }
- case options.Generic:
- opaqueConfig, err := options.GenerateFromModel(opt, &networkConfiguration{})
- if err != nil {
- return nil, err
- }
- config = opaqueConfig.(*networkConfiguration)
- case *networkConfiguration:
- config = opt
- default:
- return nil, types.BadRequestErrorf("do not recognize network configuration format: %T", opt)
- }
- }
- if err := config.Validate(); err != nil {
- return nil, err
- }
- if _, ok := option[netlabel.EnableIPv6]; ok {
- config.EnableIPv6 = option[netlabel.EnableIPv6].(bool)
- }
- return config, nil
- }
- // Create a new network using bridge plugin
- func (d *driver) CreateNetwork(id types.UUID, option map[string]interface{}) error {
- var err error
- // Driver must be configured
- d.Lock()
- // Sanity checks
- if _, ok := d.networks[id]; ok {
- d.Unlock()
- 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)
- }
- }
- // Create and set network handler in driver
- network := &bridgeNetwork{id: id, endpoints: make(map[types.UUID]*bridgeEndpoint), config: config}
- d.networks[id] = network
- d.Unlock()
- // On failure make sure to reset driver network handler to nil
- defer func() {
- if err != nil {
- d.Lock()
- delete(d.networks, id)
- d.Unlock()
- }
- }()
- // Create or retrieve the bridge L3 interface
- bridgeIface := newInterface(config)
- network.bridge = bridgeIface
- // 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()
- // Prepare the bridge setup configuration
- bridgeSetup := newBridgeSetup(config, bridgeIface)
- // 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.
- bridgeAlreadyExists := bridgeIface.exists()
- if !bridgeAlreadyExists {
- bridgeSetup.queueStep(setupDevice)
- }
- // Even if a bridge exists try to setup IPv4.
- bridgeSetup.queueStep(setupBridgeIPv4)
- // Conditionally queue setup steps depending on configuration values.
- for _, step := range []struct {
- Condition bool
- Fn setupStep
- }{
- // 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.
- {config.EnableIPv6, setupBridgeIPv6},
- // We ensure that the bridge has the expectedIPv4 and IPv6 addresses in
- // the case of a previously existing device.
- {bridgeAlreadyExists, setupVerifyAndReconcile},
- // Setup the bridge to allocate containers IPv4 addresses in the
- // specified subnet.
- {config.FixedCIDR != nil, setupFixedCIDRv4},
- // Setup the bridge to allocate containers global IPv6 addresses in the
- // specified subnet.
- {config.FixedCIDRv6 != nil, setupFixedCIDRv6},
- // Setup Loopback Adresses Routing
- {!config.EnableUserlandProxy, setupLoopbackAdressesRouting},
- // Setup IPTables.
- {config.EnableIPTables, setupIPTables},
- // Setup DefaultGatewayIPv4
- {config.DefaultGatewayIPv4 != nil, setupGatewayIPv4},
- // Setup DefaultGatewayIPv6
- {config.DefaultGatewayIPv6 != nil, setupGatewayIPv6},
- } {
- if step.Condition {
- bridgeSetup.queueStep(step.Fn)
- }
- }
- // Block bridge IP from being allocated.
- bridgeSetup.queueStep(allocateBridgeIP)
- // Apply the prepared list of steps, and abort at the first error.
- bridgeSetup.queueStep(setupDeviceUp)
- if err = bridgeSetup.apply(); err != nil {
- return err
- }
- return nil
- }
- func (d *driver) DeleteNetwork(nid types.UUID) error {
- var err error
- // Get network handler and remove it from driver
- d.Lock()
- n, ok := d.networks[nid]
- if !ok {
- d.Unlock()
- return types.InternalMaskableErrorf("network %s does not exist", nid)
- }
- delete(d.networks, nid)
- d.Unlock()
- // On failure set network handler back in driver, but
- // only if is not already taken over by some other thread
- defer func() {
- if err != nil {
- d.Lock()
- if _, ok := d.networks[nid]; !ok {
- d.networks[nid] = n
- }
- d.Unlock()
- }
- }()
- // Sanity check
- if n == nil {
- err = driverapi.ErrNoNetwork(nid)
- return err
- }
- // Cannot remove network if endpoints are still present
- if len(n.endpoints) != 0 {
- err = ActiveEndpointsError(n.id)
- return err
- }
- // Programming
- err = netlink.LinkDel(n.bridge.Link)
- return err
- }
- func (d *driver) CreateEndpoint(nid, eid types.UUID, epInfo driverapi.EndpointInfo, epOptions map[string]interface{}) error {
- var (
- ipv6Addr *net.IPNet
- err error
- )
- 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")
- }
- // Get the network handler and make sure it exists
- d.Lock()
- n, ok := d.networks[nid]
- if !ok {
- d.Unlock()
- return types.NotFoundErrorf("network %s does not exist", nid)
- }
- config := n.config
- d.Unlock()
- if n == nil {
- return driverapi.ErrNoNetwork(nid)
- }
- // Sanity check
- n.Lock()
- if n.id != nid {
- n.Unlock()
- return InvalidNetworkIDError(nid)
- }
- n.Unlock()
- // Check if endpoint id is good and retrieve correspondent endpoint
- ep, err := n.getEndpoint(eid)
- if err != nil {
- return err
- }
- // Endpoint with that id exists either on desired or other sandbox
- if ep != nil {
- return driverapi.ErrEndpointExists(eid)
- }
- // Try to convert the options to endpoint configuration
- epConfig, err := parseEndpointOptions(epOptions)
- if err != nil {
- return err
- }
- // Create and add the endpoint
- n.Lock()
- endpoint := &bridgeEndpoint{id: eid, config: epConfig}
- n.endpoints[eid] = endpoint
- n.Unlock()
- // On failure make sure to remove the endpoint
- defer func() {
- if err != nil {
- n.Lock()
- delete(n.endpoints, eid)
- n.Unlock()
- }
- }()
- // Generate a name for what will be the host side pipe interface
- name1, err := generateIfaceName()
- if err != nil {
- return err
- }
- // Generate a name for what will be the sandbox side pipe interface
- name2, err := generateIfaceName()
- if err != nil {
- return err
- }
- // Generate and add the interface pipe host <-> sandbox
- veth := &netlink.Veth{
- LinkAttrs: netlink.LinkAttrs{Name: name1, TxQLen: 0},
- PeerName: name2}
- if err = netlink.LinkAdd(veth); err != nil {
- return err
- }
- // Get the host side pipe interface handler
- host, err := netlink.LinkByName(name1)
- if err != nil {
- return err
- }
- defer func() {
- if err != nil {
- netlink.LinkDel(host)
- }
- }()
- // Get the sandbox side pipe interface handler
- sbox, err := netlink.LinkByName(name2)
- if err != nil {
- return err
- }
- defer func() {
- if err != nil {
- netlink.LinkDel(sbox)
- }
- }()
- // Set the sbox's MAC. If specified, use the one configured by user, otherwise use a random one
- mac := electMacAddress(epConfig)
- err = netlink.LinkSetHardwareAddr(sbox, mac)
- if err != nil {
- return err
- }
- endpoint.macAddress = mac
- // Add bridge inherited attributes to pipe interfaces
- if config.Mtu != 0 {
- err = netlink.LinkSetMTU(host, config.Mtu)
- if err != nil {
- return err
- }
- err = netlink.LinkSetMTU(sbox, config.Mtu)
- if err != nil {
- return err
- }
- }
- // Attach host side pipe interface into the bridge
- if err = netlink.LinkSetMaster(host,
- &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: config.BridgeName}}); err != nil {
- return err
- }
- // v4 address for the sandbox side pipe interface
- ip4, err := ipAllocator.RequestIP(n.bridge.bridgeIPv4, nil)
- if err != nil {
- return err
- }
- ipv4Addr := &net.IPNet{IP: ip4, Mask: n.bridge.bridgeIPv4.Mask}
- // v6 address for the sandbox side pipe interface
- ipv6Addr = &net.IPNet{}
- if config.EnableIPv6 {
- 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)
- if err != nil {
- return err
- }
- ipv6Addr = &net.IPNet{IP: ip6, Mask: network.Mask}
- }
- // Create the sandbox side pipe interface
- intf := &sandbox.Interface{}
- intf.SrcName = name2
- intf.DstName = containerVethPrefix
- intf.Address = ipv4Addr
- if config.EnableIPv6 {
- intf.AddressIPv6 = ipv6Addr
- }
- // Store the interface in endpoint, this is needed for cleanup on DeleteEndpoint()
- endpoint.intf = intf
- err = epInfo.AddInterface(ifaceID, endpoint.macAddress, *ipv4Addr, *ipv6Addr)
- if err != nil {
- return err
- }
- // Program any required port mapping and store them in the endpoint
- endpoint.portMapping, err = allocatePorts(epConfig, intf, config.DefaultBindingIP, config.EnableUserlandProxy)
- if err != nil {
- return err
- }
- return nil
- }
- func (d *driver) DeleteEndpoint(nid, eid types.UUID) error {
- var err error
- // Get the network handler and make sure it exists
- d.Lock()
- n, ok := d.networks[nid]
- if !ok {
- d.Unlock()
- return types.NotFoundErrorf("network %s does not exist", nid)
- }
- config := n.config
- d.Unlock()
- if n == nil {
- return driverapi.ErrNoNetwork(nid)
- }
- // Sanity Check
- n.Lock()
- if n.id != nid {
- n.Unlock()
- return InvalidNetworkIDError(nid)
- }
- n.Unlock()
- // Check endpoint id and if an endpoint is actually there
- ep, err := n.getEndpoint(eid)
- if err != nil {
- return err
- }
- if ep == nil {
- return EndpointNotFoundError(eid)
- }
- // Remove it
- n.Lock()
- delete(n.endpoints, eid)
- n.Unlock()
- // 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.
- defer func() {
- if err != nil {
- n.Lock()
- if _, ok := n.endpoints[eid]; !ok {
- n.endpoints[eid] = ep
- }
- n.Unlock()
- }
- }()
- // Remove port mappings. Do not stop endpoint delete on unmap failure
- releasePorts(ep)
- // Release the v4 address allocated to this endpoint's sandbox interface
- err = ipAllocator.ReleaseIP(n.bridge.bridgeIPv4, ep.intf.Address.IP)
- if err != nil {
- return err
- }
- // Release the v6 address allocated to this endpoint's sandbox interface
- if config.EnableIPv6 {
- err := ipAllocator.ReleaseIP(n.bridge.bridgeIPv6, ep.intf.AddressIPv6.IP)
- if err != nil {
- return err
- }
- }
- // Try removal of link. Discard error: link pair might have
- // already been deleted by sandbox delete.
- link, err := netlink.LinkByName(ep.intf.SrcName)
- if err == nil {
- netlink.LinkDel(link)
- }
- return nil
- }
- func (d *driver) EndpointOperInfo(nid, eid types.UUID) (map[string]interface{}, error) {
- // Get the network handler and make sure it exists
- d.Lock()
- n, ok := d.networks[nid]
- if !ok {
- d.Unlock()
- return nil, types.NotFoundErrorf("network %s does not exist", nid)
- }
- d.Unlock()
- if n == nil {
- return nil, driverapi.ErrNoNetwork(nid)
- }
- // 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 {
- return nil, driverapi.ErrNoEndpoint(eid)
- }
- m := make(map[string]interface{})
- if ep.portMapping != nil {
- // Return a copy of the operational data
- pmc := make([]types.PortBinding, 0, len(ep.portMapping))
- for _, pm := range ep.portMapping {
- pmc = append(pmc, pm.GetCopy())
- }
- m[netlabel.PortMap] = pmc
- }
- if len(ep.macAddress) != 0 {
- m[netlabel.MacAddress] = ep.macAddress
- }
- return m, nil
- }
- // Join method is invoked when a Sandbox is attached to an endpoint.
- func (d *driver) Join(nid, eid types.UUID, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
- network, err := d.getNetwork(nid)
- if err != nil {
- return err
- }
- endpoint, err := network.getEndpoint(eid)
- if err != nil {
- return err
- }
- if endpoint == nil {
- return EndpointNotFoundError(eid)
- }
- 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)
- if err != nil {
- return err
- }
- if !network.config.EnableICC {
- return d.link(network, endpoint, options, true)
- }
- return nil
- }
- // Leave method is invoked when a Sandbox detaches from an endpoint.
- func (d *driver) Leave(nid, eid types.UUID) error {
- network, err := d.getNetwork(nid)
- if err != nil {
- return err
- }
- endpoint, err := network.getEndpoint(eid)
- if err != nil {
- return err
- }
- if endpoint == nil {
- return EndpointNotFoundError(eid)
- }
- 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 (
- cc *containerConfiguration
- err error
- )
- if enable {
- cc, err = parseContainerOptions(options)
- if err != nil {
- return err
- }
- } else {
- cc = endpoint.containerConfig
- }
- if cc == nil {
- return nil
- }
- if endpoint.config != nil && endpoint.config.ExposedPorts != nil {
- for _, p := range cc.ParentEndpoints {
- var parentEndpoint *bridgeEndpoint
- parentEndpoint, err = network.getEndpoint(types.UUID(p))
- if err != nil {
- return err
- }
- if parentEndpoint == nil {
- err = InvalidEndpointIDError(p)
- return err
- }
- l := newLink(parentEndpoint.intf.Address.IP.String(),
- endpoint.intf.Address.IP.String(),
- endpoint.config.ExposedPorts, network.config.BridgeName)
- if enable {
- err = l.Enable()
- if err != nil {
- return err
- }
- defer func() {
- if err != nil {
- l.Disable()
- }
- }()
- } else {
- l.Disable()
- }
- }
- }
- for _, c := range cc.ChildEndpoints {
- var childEndpoint *bridgeEndpoint
- childEndpoint, err = network.getEndpoint(types.UUID(c))
- if err != nil {
- return err
- }
- if childEndpoint == nil {
- err = InvalidEndpointIDError(c)
- return err
- }
- if childEndpoint.config == nil || childEndpoint.config.ExposedPorts == nil {
- continue
- }
- l := newLink(endpoint.intf.Address.IP.String(),
- childEndpoint.intf.Address.IP.String(),
- childEndpoint.config.ExposedPorts, network.config.BridgeName)
- if enable {
- err = l.Enable()
- if err != nil {
- return err
- }
- defer func() {
- if err != nil {
- l.Disable()
- }
- }()
- } else {
- l.Disable()
- }
- }
- if enable {
- endpoint.containerConfig = cc
- }
- return nil
- }
- func (d *driver) Type() string {
- return networkType
- }
- func parseEndpointOptions(epOptions map[string]interface{}) (*endpointConfiguration, error) {
- if epOptions == nil {
- return nil, nil
- }
- ec := &endpointConfiguration{}
- if opt, ok := epOptions[netlabel.MacAddress]; ok {
- if mac, ok := opt.(net.HardwareAddr); ok {
- ec.MacAddress = mac
- } else {
- return nil, &ErrInvalidEndpointConfig{}
- }
- }
- if opt, ok := epOptions[netlabel.PortMap]; ok {
- if bs, ok := opt.([]types.PortBinding); ok {
- ec.PortBindings = bs
- } else {
- return nil, &ErrInvalidEndpointConfig{}
- }
- }
- if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
- if ports, ok := opt.([]types.TransportPort); ok {
- ec.ExposedPorts = ports
- } else {
- return nil, &ErrInvalidEndpointConfig{}
- }
- }
- return ec, nil
- }
- func parseContainerOptions(cOptions map[string]interface{}) (*containerConfiguration, error) {
- if cOptions == nil {
- return nil, nil
- }
- genericData := cOptions[netlabel.GenericData]
- if genericData == nil {
- return nil, nil
- }
- switch opt := genericData.(type) {
- case options.Generic:
- opaqueConfig, err := options.GenerateFromModel(opt, &containerConfiguration{})
- if err != nil {
- return nil, err
- }
- return opaqueConfig.(*containerConfiguration), nil
- case *containerConfiguration:
- return opt, nil
- default:
- return nil, nil
- }
- }
- func electMacAddress(epConfig *endpointConfiguration) net.HardwareAddr {
- if epConfig != nil && epConfig.MacAddress != nil {
- return epConfig.MacAddress
- }
- return netutils.GenerateRandomMAC()
- }
- // 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)
- func generateIfaceName() (string, error) {
- for i := 0; i < 3; i++ {
- name, err := netutils.GenerateRandomName(vethPrefix, vethLen)
- if err != nil {
- continue
- }
- if _, err := net.InterfaceByName(name); err != nil {
- if strings.Contains(err.Error(), "no such") {
- return name, nil
- }
- return "", err
- }
- }
- return "", &ErrIfaceName{}
- }
|