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