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