macvlan_endpoint.go 2.0 KB

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