driverapi.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package driverapi
  2. import (
  3. "net"
  4. "github.com/docker/docker/libnetwork/discoverapi"
  5. )
  6. // NetworkPluginEndpointType represents the Endpoint Type used by Plugin system
  7. const NetworkPluginEndpointType = "NetworkDriver"
  8. // Driver is an interface that every plugin driver needs to implement.
  9. type Driver interface {
  10. discoverapi.Discover
  11. // NetworkAllocate invokes the driver method to allocate network
  12. // specific resources passing network id and network specific config.
  13. // It returns a key,value pair of network specific driver allocations
  14. // to the caller.
  15. NetworkAllocate(nid string, options map[string]string, ipV4Data, ipV6Data []IPAMData) (map[string]string, error)
  16. // NetworkFree invokes the driver method to free network specific resources
  17. // associated with a given network id.
  18. NetworkFree(nid string) error
  19. // CreateNetwork invokes the driver method to create a network
  20. // passing the network id and network specific config. The
  21. // config mechanism will eventually be replaced with labels
  22. // which are yet to be introduced. The driver can return a
  23. // list of table names for which it is interested in receiving
  24. // notification when a CRUD operation is performed on any
  25. // entry in that table. This will be ignored for local scope
  26. // drivers.
  27. CreateNetwork(nid string, options map[string]interface{}, nInfo NetworkInfo, ipV4Data, ipV6Data []IPAMData) error
  28. // DeleteNetwork invokes the driver method to delete network passing
  29. // the network id.
  30. DeleteNetwork(nid string) error
  31. // CreateEndpoint invokes the driver method to create an endpoint
  32. // passing the network id, endpoint id endpoint information and driver
  33. // specific config. The endpoint information can be either consumed by
  34. // the driver or populated by the driver. The config mechanism will
  35. // eventually be replaced with labels which are yet to be introduced.
  36. CreateEndpoint(nid, eid string, ifInfo InterfaceInfo, options map[string]interface{}) error
  37. // DeleteEndpoint invokes the driver method to delete an endpoint
  38. // passing the network id and endpoint id.
  39. DeleteEndpoint(nid, eid string) error
  40. // EndpointOperInfo retrieves from the driver the operational data related to the specified endpoint
  41. EndpointOperInfo(nid, eid string) (map[string]interface{}, error)
  42. // Join method is invoked when a Sandbox is attached to an endpoint.
  43. Join(nid, eid string, sboxKey string, jinfo JoinInfo, options map[string]interface{}) error
  44. // Leave method is invoked when a Sandbox detaches from an endpoint.
  45. Leave(nid, eid string) error
  46. // ProgramExternalConnectivity invokes the driver method which does the necessary
  47. // programming to allow the external connectivity dictated by the passed options
  48. ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error
  49. // RevokeExternalConnectivity asks the driver to remove any external connectivity
  50. // programming that was done so far
  51. RevokeExternalConnectivity(nid, eid string) error
  52. // EventNotify notifies the driver when a CRUD operation has
  53. // happened on a table of its interest as soon as this node
  54. // receives such an event in the gossip layer. This method is
  55. // only invoked for the global scope driver.
  56. EventNotify(event EventType, nid string, tableName string, key string, value []byte)
  57. // DecodeTableEntry passes the driver a key, value pair from table it registered
  58. // with libnetwork. Driver should return {object ID, map[string]string} tuple.
  59. // If DecodeTableEntry is called for a table associated with NetworkObject or
  60. // EndpointObject the return object ID should be the network id or endpoint id
  61. // associated with that entry. map should have information about the object that
  62. // can be presented to the user.
  63. // For example: overlay driver returns the VTEP IP of the host that has the endpoint
  64. // which is shown in 'network inspect --verbose'
  65. DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string)
  66. // Type returns the type of this driver, the network type this driver manages
  67. Type() string
  68. // IsBuiltIn returns true if it is a built-in driver
  69. IsBuiltIn() bool
  70. }
  71. // NetworkInfo provides a go interface for drivers to provide network
  72. // specific information to libnetwork.
  73. type NetworkInfo interface {
  74. // TableEventRegister registers driver interest in a given
  75. // table name.
  76. TableEventRegister(tableName string, objType ObjectType) error
  77. // UpdateIPamConfig updates the networks IPAM configuration
  78. // based on information from the driver. In windows, the OS (HNS) chooses
  79. // the IP address space if the user does not specify an address space.
  80. UpdateIpamConfig(ipV4Data []IPAMData)
  81. }
  82. // InterfaceInfo provides a go interface for drivers to retrieve
  83. // network information to interface resources.
  84. type InterfaceInfo interface {
  85. // SetMacAddress allows the driver to set the mac address to the endpoint interface
  86. // during the call to CreateEndpoint, if the mac address is not already set.
  87. SetMacAddress(mac net.HardwareAddr) error
  88. // SetIPAddress allows the driver to set the ip address to the endpoint interface
  89. // during the call to CreateEndpoint, if the address is not already set.
  90. // The API is to be used to assign both the IPv4 and IPv6 address types.
  91. SetIPAddress(ip *net.IPNet) error
  92. // MacAddress returns the MAC address.
  93. MacAddress() net.HardwareAddr
  94. // Address returns the IPv4 address.
  95. Address() *net.IPNet
  96. // AddressIPv6 returns the IPv6 address.
  97. AddressIPv6() *net.IPNet
  98. }
  99. // InterfaceNameInfo provides a go interface for the drivers to assign names
  100. // to interfaces.
  101. type InterfaceNameInfo interface {
  102. // SetNames method assigns the srcName and dstPrefix for the interface.
  103. SetNames(srcName, dstPrefix string) error
  104. }
  105. // JoinInfo represents a set of resources that the driver has the ability to provide during
  106. // join time.
  107. type JoinInfo interface {
  108. // InterfaceName returns an InterfaceNameInfo go interface to facilitate
  109. // setting the names for the interface.
  110. InterfaceName() InterfaceNameInfo
  111. // SetGateway sets the default IPv4 gateway when a container joins the endpoint.
  112. SetGateway(net.IP) error
  113. // SetGatewayIPv6 sets the default IPv6 gateway when a container joins the endpoint.
  114. SetGatewayIPv6(net.IP) error
  115. // AddStaticRoute adds a route to the sandbox.
  116. // It may be used in addition to or instead of a default gateway (as above).
  117. AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP) error
  118. // DisableGatewayService tells libnetwork not to provide Default GW for the container
  119. DisableGatewayService()
  120. // AddTableEntry adds a table entry to the gossip layer
  121. // passing the table name, key and an opaque value.
  122. AddTableEntry(tableName string, key string, value []byte) error
  123. }
  124. // Registerer provides a way for network drivers to be dynamically registered.
  125. type Registerer interface {
  126. RegisterDriver(name string, driver Driver, capability Capability) error
  127. }
  128. // Capability represents the high level capabilities of the drivers which libnetwork can make use of
  129. type Capability struct {
  130. DataScope string
  131. ConnectivityScope string
  132. }
  133. // IPAMData represents the per-network ip related
  134. // operational information libnetwork will send
  135. // to the network driver during CreateNetwork()
  136. type IPAMData struct {
  137. AddressSpace string
  138. Pool *net.IPNet
  139. Gateway *net.IPNet
  140. AuxAddresses map[string]*net.IPNet
  141. }
  142. // EventType defines a type for the CRUD event
  143. type EventType uint8
  144. const (
  145. // Create event is generated when a table entry is created,
  146. Create EventType = 1 + iota
  147. // Update event is generated when a table entry is updated.
  148. Update
  149. // Delete event is generated when a table entry is deleted.
  150. Delete
  151. )
  152. // ObjectType represents the type of object driver wants to store in libnetwork's networkDB
  153. type ObjectType int
  154. const (
  155. // EndpointObject should be set for libnetwork endpoint object related data
  156. EndpointObject ObjectType = 1 + iota
  157. // NetworkObject should be set for libnetwork network object related data
  158. NetworkObject
  159. // OpaqueObject is for driver specific data with no corresponding libnetwork object
  160. OpaqueObject
  161. )
  162. // IsValidType validates the passed in type against the valid object types
  163. func IsValidType(objType ObjectType) bool {
  164. switch objType {
  165. case EndpointObject:
  166. fallthrough
  167. case NetworkObject:
  168. fallthrough
  169. case OpaqueObject:
  170. return true
  171. }
  172. return false
  173. }