macvlan_endpoint.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //go:build linux
  2. // +build linux
  3. package macvlan
  4. import (
  5. "fmt"
  6. "github.com/docker/docker/libnetwork/driverapi"
  7. "github.com/docker/docker/libnetwork/netlabel"
  8. "github.com/docker/docker/libnetwork/netutils"
  9. "github.com/docker/docker/libnetwork/ns"
  10. "github.com/docker/docker/libnetwork/osl"
  11. "github.com/docker/docker/libnetwork/types"
  12. "github.com/sirupsen/logrus"
  13. )
  14. // CreateEndpoint assigns the mac, ip and endpoint id for the new container
  15. func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
  16. epOptions map[string]interface{}) error {
  17. defer osl.InitOSContext()()
  18. if err := validateID(nid, eid); err != nil {
  19. return err
  20. }
  21. n, err := d.getNetwork(nid)
  22. if err != nil {
  23. return fmt.Errorf("network id %q not found", nid)
  24. }
  25. ep := &endpoint{
  26. id: eid,
  27. nid: nid,
  28. addr: ifInfo.Address(),
  29. addrv6: ifInfo.AddressIPv6(),
  30. mac: ifInfo.MacAddress(),
  31. }
  32. if ep.addr == nil {
  33. return fmt.Errorf("create endpoint was not passed an IP address")
  34. }
  35. if ep.mac == nil {
  36. ep.mac = netutils.GenerateMACFromIP(ep.addr.IP)
  37. if err := ifInfo.SetMacAddress(ep.mac); err != nil {
  38. return err
  39. }
  40. }
  41. // disallow portmapping -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("macvlan driver does not support port mappings")
  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("macvlan driver does not support port exposures")
  54. }
  55. }
  56. }
  57. if err := d.storeUpdate(ep); err != nil {
  58. return fmt.Errorf("failed to save macvlan endpoint %.7s to store: %v", ep.id, err)
  59. }
  60. n.addEndpoint(ep)
  61. return nil
  62. }
  63. // DeleteEndpoint removes the endpoint and associated netlink interface
  64. func (d *driver) DeleteEndpoint(nid, eid string) error {
  65. defer osl.InitOSContext()()
  66. if err := validateID(nid, eid); err != nil {
  67. return err
  68. }
  69. n := d.network(nid)
  70. if n == nil {
  71. return fmt.Errorf("network id %q not found", nid)
  72. }
  73. ep := n.endpoint(eid)
  74. if ep == nil {
  75. return fmt.Errorf("endpoint id %q not found", eid)
  76. }
  77. if link, err := ns.NlHandle().LinkByName(ep.srcName); err == nil {
  78. if err := ns.NlHandle().LinkDel(link); err != nil {
  79. logrus.WithError(err).Warnf("Failed to delete interface (%s)'s link on endpoint (%s) delete", ep.srcName, ep.id)
  80. }
  81. }
  82. if err := d.storeDelete(ep); err != nil {
  83. logrus.Warnf("Failed to remove macvlan endpoint %.7s from store: %v", ep.id, err)
  84. }
  85. n.deleteEndpoint(ep.id)
  86. return nil
  87. }