github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/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 aks 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  	// Type returns the the type of this driver, the network type this driver manages
    76  	Type() string
    77  }
    78  
    79  // NetworkInfo provides a go interface for drivers to provide network
    80  // specific information to libnetwork.
    81  type NetworkInfo interface {
    82  	// TableEventRegister registers driver interest in a given
    83  	// table name.
    84  	TableEventRegister(tableName string) error
    85  }
    86  
    87  // InterfaceInfo provides a go interface for drivers to retrive
    88  // network information to interface resources.
    89  type InterfaceInfo interface {
    90  	// SetMacAddress allows the driver to set the mac address to the endpoint interface
    91  	// during the call to CreateEndpoint, if the mac address is not already set.
    92  	SetMacAddress(mac net.HardwareAddr) error
    93  
    94  	// SetIPAddress allows the driver to set the ip address to the endpoint interface
    95  	// during the call to CreateEndpoint, if the address is not already set.
    96  	// The API is to be used to assign both the IPv4 and IPv6 address types.
    97  	SetIPAddress(ip *net.IPNet) error
    98  
    99  	// MacAddress returns the MAC address.
   100  	MacAddress() net.HardwareAddr
   101  
   102  	// Address returns the IPv4 address.
   103  	Address() *net.IPNet
   104  
   105  	// AddressIPv6 returns the IPv6 address.
   106  	AddressIPv6() *net.IPNet
   107  }
   108  
   109  // InterfaceNameInfo provides a go interface for the drivers to assign names
   110  // to interfaces.
   111  type InterfaceNameInfo interface {
   112  	// SetNames method assigns the srcName and dstPrefix for the interface.
   113  	SetNames(srcName, dstPrefix string) error
   114  }
   115  
   116  // JoinInfo represents a set of resources that the driver has the ability to provide during
   117  // join time.
   118  type JoinInfo interface {
   119  	// InterfaceName returns an InterfaceNameInfo go interface to facilitate
   120  	// setting the names for the interface.
   121  	InterfaceName() InterfaceNameInfo
   122  
   123  	// SetGateway sets the default IPv4 gateway when a container joins the endpoint.
   124  	SetGateway(net.IP) error
   125  
   126  	// SetGatewayIPv6 sets the default IPv6 gateway when a container joins the endpoint.
   127  	SetGatewayIPv6(net.IP) error
   128  
   129  	// AddStaticRoute adds a route to the sandbox.
   130  	// It may be used in addition to or instead of a default gateway (as above).
   131  	AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP) error
   132  
   133  	// DisableGatewayService tells libnetwork not to provide Default GW for the container
   134  	DisableGatewayService()
   135  
   136  	// AddTableEntry adds a table entry to the gossip layer
   137  	// passing the table name, key and an opaque value.
   138  	AddTableEntry(tableName string, key string, value []byte) error
   139  }
   140  
   141  // DriverCallback provides a Callback interface for Drivers into LibNetwork
   142  type DriverCallback interface {
   143  	// GetPluginGetter returns the pluginv2 getter.
   144  	GetPluginGetter() plugingetter.PluginGetter
   145  	// RegisterDriver provides a way for Remote drivers to dynamically register new NetworkType and associate with a driver instance
   146  	RegisterDriver(name string, driver Driver, capability Capability) error
   147  }
   148  
   149  // Capability represents the high level capabilities of the drivers which libnetwork can make use of
   150  type Capability struct {
   151  	DataScope string
   152  }
   153  
   154  // IPAMData represents the per-network ip related
   155  // operational information libnetwork will send
   156  // to the network driver during CreateNetwork()
   157  type IPAMData struct {
   158  	AddressSpace string
   159  	Pool         *net.IPNet
   160  	Gateway      *net.IPNet
   161  	AuxAddresses map[string]*net.IPNet
   162  }
   163  
   164  // EventType defines a type for the CRUD event
   165  type EventType uint8
   166  
   167  const (
   168  	// Create event is generated when a table entry is created,
   169  	Create EventType = 1 + iota
   170  	// Update event is generated when a table entry is updated.
   171  	Update
   172  	// Delete event is generated when a table entry is deleted.
   173  	Delete
   174  )