macvlan_endpoint.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/osl"
  9. "github.com/docker/libnetwork/types"
  10. "github.com/vishvananda/netlink"
  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. addr: ifInfo.Address(),
  26. addrv6: ifInfo.AddressIPv6(),
  27. mac: ifInfo.MacAddress(),
  28. }
  29. if ep.addr == nil {
  30. return fmt.Errorf("create endpoint was not passed an IP address")
  31. }
  32. if ep.mac == nil {
  33. ep.mac = netutils.GenerateMACFromIP(ep.addr.IP)
  34. if err := ifInfo.SetMacAddress(ep.mac); err != nil {
  35. return err
  36. }
  37. }
  38. // disallow portmapping -p
  39. if opt, ok := epOptions[netlabel.PortMap]; ok {
  40. if _, ok := opt.([]types.PortBinding); ok {
  41. if len(opt.([]types.PortBinding)) > 0 {
  42. logrus.Warnf("%s driver does not support port mappings", macvlanType)
  43. }
  44. }
  45. }
  46. // disallow port exposure --expose
  47. if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
  48. if _, ok := opt.([]types.TransportPort); ok {
  49. if len(opt.([]types.TransportPort)) > 0 {
  50. logrus.Warnf("%s driver does not support port exposures", macvlanType)
  51. }
  52. }
  53. }
  54. n.addEndpoint(ep)
  55. return nil
  56. }
  57. // DeleteEndpoint remove the endpoint and associated netlink interface
  58. func (d *driver) DeleteEndpoint(nid, eid string) error {
  59. defer osl.InitOSContext()()
  60. if err := validateID(nid, eid); err != nil {
  61. return err
  62. }
  63. n := d.network(nid)
  64. if n == nil {
  65. return fmt.Errorf("network id %q not found", nid)
  66. }
  67. ep := n.endpoint(eid)
  68. if ep == nil {
  69. return fmt.Errorf("endpoint id %q not found", eid)
  70. }
  71. if link, err := netlink.LinkByName(ep.srcName); err == nil {
  72. netlink.LinkDel(link)
  73. }
  74. return nil
  75. }