github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/libnetwork/drivers/ipvlan/ipvlan_endpoint.go (about) 1 //go:build linux 2 // +build linux 3 4 package ipvlan 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/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 if ifInfo.MacAddress() != nil { 30 return fmt.Errorf("%s interfaces do not support custom mac address assignment", ipvlanType) 31 } 32 ep := &endpoint{ 33 id: eid, 34 nid: nid, 35 addr: ifInfo.Address(), 36 addrv6: ifInfo.AddressIPv6(), 37 } 38 if ep.addr == nil { 39 return fmt.Errorf("create endpoint was not passed an IP address") 40 } 41 // disallow port mapping -p 42 if opt, ok := epOptions[netlabel.PortMap]; ok { 43 if _, ok := opt.([]types.PortBinding); ok { 44 if len(opt.([]types.PortBinding)) > 0 { 45 logrus.Warnf("%s driver does not support port mappings", ipvlanType) 46 } 47 } 48 } 49 // disallow port exposure --expose 50 if opt, ok := epOptions[netlabel.ExposedPorts]; ok { 51 if _, ok := opt.([]types.TransportPort); ok { 52 if len(opt.([]types.TransportPort)) > 0 { 53 logrus.Warnf("%s driver does not support port exposures", ipvlanType) 54 } 55 } 56 } 57 58 if err := d.storeUpdate(ep); err != nil { 59 return fmt.Errorf("failed to save ipvlan endpoint %.7s to store: %v", ep.id, err) 60 } 61 62 n.addEndpoint(ep) 63 64 return nil 65 } 66 67 // DeleteEndpoint remove the endpoint and associated netlink interface 68 func (d *driver) DeleteEndpoint(nid, eid string) error { 69 defer osl.InitOSContext()() 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 logrus.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 logrus.Warnf("Failed to remove ipvlan endpoint %.7s from store: %v", ep.id, err) 89 } 90 n.deleteEndpoint(ep.id) 91 return nil 92 }