macvlan_endpoint.go 2.4 KB

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