github.com/jfrazelle/docker@v1.1.2-0.20210712172922-bf78e25fe508/libnetwork/drivers/macvlan/macvlan_endpoint.go (about)

     1  // +build linux
     2  
     3  package macvlan
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"github.com/docker/docker/libnetwork/driverapi"
     9  	"github.com/docker/docker/libnetwork/netlabel"
    10  	"github.com/docker/docker/libnetwork/netutils"
    11  	"github.com/docker/docker/libnetwork/ns"
    12  	"github.com/docker/docker/libnetwork/osl"
    13  	"github.com/docker/docker/libnetwork/types"
    14  	"github.com/sirupsen/logrus"
    15  )
    16  
    17  // CreateEndpoint assigns the mac, ip and endpoint id for the new container
    18  func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
    19  	epOptions map[string]interface{}) error {
    20  	defer osl.InitOSContext()()
    21  
    22  	if err := validateID(nid, eid); err != nil {
    23  		return err
    24  	}
    25  	n, err := d.getNetwork(nid)
    26  	if err != nil {
    27  		return fmt.Errorf("network id %q not found", nid)
    28  	}
    29  	ep := &endpoint{
    30  		id:     eid,
    31  		nid:    nid,
    32  		addr:   ifInfo.Address(),
    33  		addrv6: ifInfo.AddressIPv6(),
    34  		mac:    ifInfo.MacAddress(),
    35  	}
    36  	if ep.addr == nil {
    37  		return fmt.Errorf("create endpoint was not passed an IP address")
    38  	}
    39  	if ep.mac == nil {
    40  		ep.mac = netutils.GenerateMACFromIP(ep.addr.IP)
    41  		if err := ifInfo.SetMacAddress(ep.mac); err != nil {
    42  			return err
    43  		}
    44  	}
    45  	// disallow portmapping -p
    46  	if opt, ok := epOptions[netlabel.PortMap]; ok {
    47  		if _, ok := opt.([]types.PortBinding); ok {
    48  			if len(opt.([]types.PortBinding)) > 0 {
    49  				logrus.Warnf("%s driver does not support port mappings", macvlanType)
    50  			}
    51  		}
    52  	}
    53  	// disallow port exposure --expose
    54  	if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
    55  		if _, ok := opt.([]types.TransportPort); ok {
    56  			if len(opt.([]types.TransportPort)) > 0 {
    57  				logrus.Warnf("%s driver does not support port exposures", macvlanType)
    58  			}
    59  		}
    60  	}
    61  
    62  	if err := d.storeUpdate(ep); err != nil {
    63  		return fmt.Errorf("failed to save macvlan endpoint %.7s to store: %v", ep.id, err)
    64  	}
    65  
    66  	n.addEndpoint(ep)
    67  
    68  	return nil
    69  }
    70  
    71  // DeleteEndpoint removes the endpoint and associated netlink interface
    72  func (d *driver) DeleteEndpoint(nid, eid string) error {
    73  	defer osl.InitOSContext()()
    74  	if err := validateID(nid, eid); err != nil {
    75  		return err
    76  	}
    77  	n := d.network(nid)
    78  	if n == nil {
    79  		return fmt.Errorf("network id %q not found", nid)
    80  	}
    81  	ep := n.endpoint(eid)
    82  	if ep == nil {
    83  		return fmt.Errorf("endpoint id %q not found", eid)
    84  	}
    85  	if link, err := ns.NlHandle().LinkByName(ep.srcName); err == nil {
    86  		if err := ns.NlHandle().LinkDel(link); err != nil {
    87  			logrus.WithError(err).Warnf("Failed to delete interface (%s)'s link on endpoint (%s) delete", ep.srcName, ep.id)
    88  		}
    89  	}
    90  
    91  	if err := d.storeDelete(ep); err != nil {
    92  		logrus.Warnf("Failed to remove macvlan endpoint %.7s from store: %v", ep.id, err)
    93  	}
    94  
    95  	n.deleteEndpoint(ep.id)
    96  
    97  	return nil
    98  }