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