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