driverapi.go 8.4 KB

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