ipvlan_endpoint.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package ipvlan
  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/ns"
  8. "github.com/docker/libnetwork/osl"
  9. "github.com/docker/libnetwork/types"
  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. defer osl.InitOSContext()()
  15. if err := validateID(nid, eid); err != nil {
  16. return err
  17. }
  18. n, err := d.getNetwork(nid)
  19. if err != nil {
  20. return fmt.Errorf("network id %q not found", nid)
  21. }
  22. if ifInfo.MacAddress() != nil {
  23. return fmt.Errorf("%s interfaces do not support custom mac address assigment", ipvlanType)
  24. }
  25. ep := &endpoint{
  26. id: eid,
  27. nid: nid,
  28. addr: ifInfo.Address(),
  29. addrv6: ifInfo.AddressIPv6(),
  30. }
  31. if ep.addr == nil {
  32. return fmt.Errorf("create endpoint was not passed an IP address")
  33. }
  34. // disallow port mapping -p
  35. if opt, ok := epOptions[netlabel.PortMap]; ok {
  36. if _, ok := opt.([]types.PortBinding); ok {
  37. if len(opt.([]types.PortBinding)) > 0 {
  38. logrus.Warnf("%s driver does not support port mappings", ipvlanType)
  39. }
  40. }
  41. }
  42. // disallow port exposure --expose
  43. if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
  44. if _, ok := opt.([]types.TransportPort); ok {
  45. if len(opt.([]types.TransportPort)) > 0 {
  46. logrus.Warnf("%s driver does not support port exposures", ipvlanType)
  47. }
  48. }
  49. }
  50. if err := d.storeUpdate(ep); err != nil {
  51. return fmt.Errorf("failed to save ipvlan endpoint %s to store: %v", ep.id[0:7], err)
  52. }
  53. n.addEndpoint(ep)
  54. return nil
  55. }
  56. // DeleteEndpoint remove the endpoint and associated netlink interface
  57. func (d *driver) DeleteEndpoint(nid, eid string) error {
  58. defer osl.InitOSContext()()
  59. if err := validateID(nid, eid); err != nil {
  60. return err
  61. }
  62. n := d.network(nid)
  63. if n == nil {
  64. return fmt.Errorf("network id %q not found", nid)
  65. }
  66. ep := n.endpoint(eid)
  67. if ep == nil {
  68. return fmt.Errorf("endpoint id %q not found", eid)
  69. }
  70. if link, err := ns.NlHandle().LinkByName(ep.srcName); err == nil {
  71. ns.NlHandle().LinkDel(link)
  72. }
  73. if err := d.storeDelete(ep); err != nil {
  74. logrus.Warnf("Failed to remove ipvlan endpoint %s from store: %v", ep.id[0:7], err)
  75. }
  76. n.deleteEndpoint(ep.id)
  77. return nil
  78. }