ov_endpoint.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package overlay
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "net"
  6. "github.com/docker/libnetwork/driverapi"
  7. "github.com/docker/libnetwork/netutils"
  8. "github.com/docker/libnetwork/types"
  9. )
  10. type endpointTable map[types.UUID]*endpoint
  11. type endpoint struct {
  12. id types.UUID
  13. mac net.HardwareAddr
  14. addr *net.IPNet
  15. }
  16. func (n *network) endpoint(eid types.UUID) *endpoint {
  17. n.Lock()
  18. defer n.Unlock()
  19. return n.endpoints[eid]
  20. }
  21. func (n *network) addEndpoint(ep *endpoint) {
  22. n.Lock()
  23. n.endpoints[ep.id] = ep
  24. n.Unlock()
  25. }
  26. func (n *network) deleteEndpoint(eid types.UUID) {
  27. n.Lock()
  28. delete(n.endpoints, eid)
  29. n.Unlock()
  30. }
  31. func (d *driver) CreateEndpoint(nid, eid types.UUID, epInfo driverapi.EndpointInfo,
  32. epOptions map[string]interface{}) error {
  33. if err := validateID(nid, eid); err != nil {
  34. return err
  35. }
  36. n := d.network(nid)
  37. if n == nil {
  38. return fmt.Errorf("network id %q not found", nid)
  39. }
  40. ep := &endpoint{
  41. id: eid,
  42. }
  43. if epInfo != nil && (len(epInfo.Interfaces()) > 0) {
  44. addr := epInfo.Interfaces()[0].Address()
  45. ep.addr = &addr
  46. ep.mac = epInfo.Interfaces()[0].MacAddress()
  47. n.addEndpoint(ep)
  48. return nil
  49. }
  50. ipID, err := d.ipAllocator.GetID()
  51. if err != nil {
  52. return fmt.Errorf("could not allocate ip from subnet %s: %v",
  53. bridgeSubnet.String(), err)
  54. }
  55. ep.addr = &net.IPNet{
  56. Mask: bridgeSubnet.Mask,
  57. }
  58. ep.addr.IP = make([]byte, 4)
  59. binary.BigEndian.PutUint32(ep.addr.IP, bridgeSubnetInt+ipID)
  60. ep.mac = netutils.GenerateMACFromIP(ep.addr.IP)
  61. err = epInfo.AddInterface(1, ep.mac, *ep.addr, net.IPNet{})
  62. if err != nil {
  63. return fmt.Errorf("could not add interface to endpoint info: %v", err)
  64. }
  65. n.addEndpoint(ep)
  66. return nil
  67. }
  68. func (d *driver) DeleteEndpoint(nid, eid types.UUID) error {
  69. if err := validateID(nid, eid); err != nil {
  70. return err
  71. }
  72. n := d.network(nid)
  73. if n == nil {
  74. return fmt.Errorf("network id %q not found", nid)
  75. }
  76. ep := n.endpoint(eid)
  77. if ep == nil {
  78. return fmt.Errorf("endpoint id %q not found", eid)
  79. }
  80. d.ipAllocator.Release(binary.BigEndian.Uint32(ep.addr.IP) - bridgeSubnetInt)
  81. n.deleteEndpoint(eid)
  82. return nil
  83. }
  84. func (d *driver) EndpointOperInfo(nid, eid types.UUID) (map[string]interface{}, error) {
  85. return make(map[string]interface{}, 0), nil
  86. }