macvlan_endpoint.go 2.6 KB

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