ov_endpoint.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package overlay
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "github.com/Sirupsen/logrus"
  7. "github.com/docker/libnetwork/datastore"
  8. "github.com/docker/libnetwork/driverapi"
  9. "github.com/docker/libnetwork/netutils"
  10. "github.com/docker/libnetwork/types"
  11. )
  12. type endpointTable map[string]*endpoint
  13. const overlayEndpointPrefix = "overlay/endpoint"
  14. type endpoint struct {
  15. id string
  16. nid string
  17. ifName string
  18. mac net.HardwareAddr
  19. addr *net.IPNet
  20. dbExists bool
  21. dbIndex uint64
  22. }
  23. func (n *network) endpoint(eid string) *endpoint {
  24. n.Lock()
  25. defer n.Unlock()
  26. return n.endpoints[eid]
  27. }
  28. func (n *network) addEndpoint(ep *endpoint) {
  29. n.Lock()
  30. n.endpoints[ep.id] = ep
  31. n.Unlock()
  32. }
  33. func (n *network) deleteEndpoint(eid string) {
  34. n.Lock()
  35. delete(n.endpoints, eid)
  36. n.Unlock()
  37. }
  38. func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
  39. epOptions map[string]interface{}) error {
  40. var err error
  41. if err = validateID(nid, eid); err != nil {
  42. return err
  43. }
  44. // Since we perform lazy configuration make sure we try
  45. // configuring the driver when we enter CreateEndpoint since
  46. // CreateNetwork may not be called in every node.
  47. if err := d.configure(); err != nil {
  48. return err
  49. }
  50. n := d.network(nid)
  51. if n == nil {
  52. return fmt.Errorf("network id %q not found", nid)
  53. }
  54. ep := &endpoint{
  55. id: eid,
  56. nid: n.id,
  57. addr: ifInfo.Address(),
  58. mac: ifInfo.MacAddress(),
  59. }
  60. if ep.addr == nil {
  61. return fmt.Errorf("create endpoint was not passed interface IP address")
  62. }
  63. if s := n.getSubnetforIP(ep.addr); s == nil {
  64. return fmt.Errorf("no matching subnet for IP %q in network %q\n", ep.addr, nid)
  65. }
  66. if ep.mac == nil {
  67. ep.mac = netutils.GenerateMACFromIP(ep.addr.IP)
  68. if err := ifInfo.SetMacAddress(ep.mac); err != nil {
  69. return err
  70. }
  71. }
  72. n.addEndpoint(ep)
  73. if err := d.writeEndpointToStore(ep); err != nil {
  74. return fmt.Errorf("failed to update overlay endpoint %s to local store: %v", ep.id[0:7], err)
  75. }
  76. return nil
  77. }
  78. func (d *driver) DeleteEndpoint(nid, eid string) error {
  79. if err := validateID(nid, eid); err != nil {
  80. return err
  81. }
  82. n := d.network(nid)
  83. if n == nil {
  84. return fmt.Errorf("network id %q not found", nid)
  85. }
  86. ep := n.endpoint(eid)
  87. if ep == nil {
  88. return fmt.Errorf("endpoint id %q not found", eid)
  89. }
  90. n.deleteEndpoint(eid)
  91. if err := d.deleteEndpointFromStore(ep); err != nil {
  92. logrus.Warnf("Failed to delete overlay endpoint %s from local store: %v", ep.id[0:7], err)
  93. }
  94. if ep.ifName == "" {
  95. return nil
  96. }
  97. // OVERLAY_SOLARIS: Add Solaris unplumbing for removing the interface endpoint
  98. return nil
  99. }
  100. func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) {
  101. return make(map[string]interface{}, 0), nil
  102. }
  103. func (d *driver) deleteEndpointFromStore(e *endpoint) error {
  104. if d.localStore == nil {
  105. return fmt.Errorf("overlay local store not initialized, ep not deleted")
  106. }
  107. if err := d.localStore.DeleteObjectAtomic(e); err != nil {
  108. return err
  109. }
  110. return nil
  111. }
  112. func (d *driver) writeEndpointToStore(e *endpoint) error {
  113. if d.localStore == nil {
  114. return fmt.Errorf("overlay local store not initialized, ep not added")
  115. }
  116. if err := d.localStore.PutObjectAtomic(e); err != nil {
  117. return err
  118. }
  119. return nil
  120. }
  121. func (ep *endpoint) DataScope() string {
  122. return datastore.LocalScope
  123. }
  124. func (ep *endpoint) New() datastore.KVObject {
  125. return &endpoint{}
  126. }
  127. func (ep *endpoint) CopyTo(o datastore.KVObject) error {
  128. dstep := o.(*endpoint)
  129. *dstep = *ep
  130. return nil
  131. }
  132. func (ep *endpoint) Key() []string {
  133. return []string{overlayEndpointPrefix, ep.id}
  134. }
  135. func (ep *endpoint) KeyPrefix() []string {
  136. return []string{overlayEndpointPrefix}
  137. }
  138. func (ep *endpoint) Index() uint64 {
  139. return ep.dbIndex
  140. }
  141. func (ep *endpoint) SetIndex(index uint64) {
  142. ep.dbIndex = index
  143. ep.dbExists = true
  144. }
  145. func (ep *endpoint) Exists() bool {
  146. return ep.dbExists
  147. }
  148. func (ep *endpoint) Skip() bool {
  149. return false
  150. }
  151. func (ep *endpoint) Value() []byte {
  152. b, err := json.Marshal(ep)
  153. if err != nil {
  154. return nil
  155. }
  156. return b
  157. }
  158. func (ep *endpoint) SetValue(value []byte) error {
  159. return json.Unmarshal(value, ep)
  160. }
  161. func (ep *endpoint) MarshalJSON() ([]byte, error) {
  162. epMap := make(map[string]interface{})
  163. epMap["id"] = ep.id
  164. epMap["nid"] = ep.nid
  165. if ep.ifName != "" {
  166. epMap["ifName"] = ep.ifName
  167. }
  168. if ep.addr != nil {
  169. epMap["addr"] = ep.addr.String()
  170. }
  171. if len(ep.mac) != 0 {
  172. epMap["mac"] = ep.mac.String()
  173. }
  174. return json.Marshal(epMap)
  175. }
  176. func (ep *endpoint) UnmarshalJSON(value []byte) error {
  177. var (
  178. err error
  179. epMap map[string]interface{}
  180. )
  181. json.Unmarshal(value, &epMap)
  182. ep.id = epMap["id"].(string)
  183. ep.nid = epMap["nid"].(string)
  184. if v, ok := epMap["mac"]; ok {
  185. if ep.mac, err = net.ParseMAC(v.(string)); err != nil {
  186. return types.InternalErrorf("failed to decode endpoint interface mac address after json unmarshal: %s", v.(string))
  187. }
  188. }
  189. if v, ok := epMap["addr"]; ok {
  190. if ep.addr, err = types.ParseCIDR(v.(string)); err != nil {
  191. return types.InternalErrorf("failed to decode endpoint interface ipv4 address after json unmarshal: %v", err)
  192. }
  193. }
  194. if v, ok := epMap["ifName"]; ok {
  195. ep.ifName = v.(string)
  196. }
  197. return nil
  198. }