github.com/rawahars/moby@v24.0.4+incompatible/libnetwork/drivers/overlay/ov_endpoint.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package overlay
     5  
     6  import (
     7  	"fmt"
     8  	"net"
     9  
    10  	"github.com/docker/docker/libnetwork/driverapi"
    11  	"github.com/docker/docker/libnetwork/netutils"
    12  	"github.com/docker/docker/libnetwork/ns"
    13  	"github.com/sirupsen/logrus"
    14  )
    15  
    16  type endpointTable map[string]*endpoint
    17  
    18  type endpoint struct {
    19  	id     string
    20  	nid    string
    21  	ifName string
    22  	mac    net.HardwareAddr
    23  	addr   *net.IPNet
    24  }
    25  
    26  func (n *network) endpoint(eid string) *endpoint {
    27  	n.Lock()
    28  	defer n.Unlock()
    29  
    30  	return n.endpoints[eid]
    31  }
    32  
    33  func (n *network) addEndpoint(ep *endpoint) {
    34  	n.Lock()
    35  	n.endpoints[ep.id] = ep
    36  	n.Unlock()
    37  }
    38  
    39  func (n *network) deleteEndpoint(eid string) {
    40  	n.Lock()
    41  	delete(n.endpoints, eid)
    42  	n.Unlock()
    43  }
    44  
    45  func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
    46  	epOptions map[string]interface{}) error {
    47  	var err error
    48  
    49  	if err = validateID(nid, eid); err != nil {
    50  		return err
    51  	}
    52  
    53  	// Since we perform lazy configuration make sure we try
    54  	// configuring the driver when we enter CreateEndpoint since
    55  	// CreateNetwork may not be called in every node.
    56  	if err := d.configure(); err != nil {
    57  		return err
    58  	}
    59  
    60  	n := d.network(nid)
    61  	if n == nil {
    62  		return fmt.Errorf("network id %q not found", nid)
    63  	}
    64  
    65  	ep := &endpoint{
    66  		id:   eid,
    67  		nid:  n.id,
    68  		addr: ifInfo.Address(),
    69  		mac:  ifInfo.MacAddress(),
    70  	}
    71  	if ep.addr == nil {
    72  		return fmt.Errorf("create endpoint was not passed interface IP address")
    73  	}
    74  
    75  	if s := n.getSubnetforIP(ep.addr); s == nil {
    76  		return fmt.Errorf("no matching subnet for IP %q in network %q", ep.addr, nid)
    77  	}
    78  
    79  	if ep.mac == nil {
    80  		ep.mac = netutils.GenerateMACFromIP(ep.addr.IP)
    81  		if err := ifInfo.SetMacAddress(ep.mac); err != nil {
    82  			return err
    83  		}
    84  	}
    85  
    86  	n.addEndpoint(ep)
    87  
    88  	return nil
    89  }
    90  
    91  func (d *driver) DeleteEndpoint(nid, eid string) error {
    92  	nlh := ns.NlHandle()
    93  
    94  	if err := validateID(nid, eid); err != nil {
    95  		return err
    96  	}
    97  
    98  	n := d.network(nid)
    99  	if n == nil {
   100  		return fmt.Errorf("network id %q not found", nid)
   101  	}
   102  
   103  	ep := n.endpoint(eid)
   104  	if ep == nil {
   105  		return fmt.Errorf("endpoint id %q not found", eid)
   106  	}
   107  
   108  	n.deleteEndpoint(eid)
   109  
   110  	if ep.ifName == "" {
   111  		return nil
   112  	}
   113  
   114  	link, err := nlh.LinkByName(ep.ifName)
   115  	if err != nil {
   116  		logrus.Debugf("Failed to retrieve interface (%s)'s link on endpoint (%s) delete: %v", ep.ifName, ep.id, err)
   117  		return nil
   118  	}
   119  	if err := nlh.LinkDel(link); err != nil {
   120  		logrus.Debugf("Failed to delete interface (%s)'s link on endpoint (%s) delete: %v", ep.ifName, ep.id, err)
   121  	}
   122  
   123  	return nil
   124  }
   125  
   126  func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) {
   127  	return make(map[string]interface{}), nil
   128  }