github.com/rawahars/moby@v24.0.4+incompatible/libnetwork/osl/sandbox.go (about)

     1  // Package osl describes structures and interfaces which abstract os entities
     2  package osl
     3  
     4  import (
     5  	"net"
     6  
     7  	"github.com/docker/docker/libnetwork/types"
     8  )
     9  
    10  // SandboxType specify the time of the sandbox, this can be used to apply special configs
    11  type SandboxType int
    12  
    13  const (
    14  	// SandboxTypeIngress indicates that the sandbox is for the ingress
    15  	SandboxTypeIngress = iota
    16  	// SandboxTypeLoadBalancer indicates that the sandbox is a load balancer
    17  	SandboxTypeLoadBalancer = iota
    18  )
    19  
    20  type Iface struct {
    21  	SrcName, DstPrefix string
    22  }
    23  
    24  // IfaceOption is a function option type to set interface options.
    25  type IfaceOption func(i *nwIface)
    26  
    27  // NeighOption is a function option type to set neighbor options.
    28  type NeighOption func(nh *neigh)
    29  
    30  // Sandbox represents a network sandbox, identified by a specific key.  It
    31  // holds a list of Interfaces, routes etc, and more can be added dynamically.
    32  type Sandbox interface {
    33  	// Key returns the path where the network namespace is mounted.
    34  	Key() string
    35  
    36  	// AddInterface adds an existing Interface to this sandbox. The operation will rename
    37  	// from the Interface SrcName to DstName as it moves, and reconfigure the
    38  	// interface according to the specified settings. The caller is expected
    39  	// to only provide a prefix for DstName. The AddInterface api will auto-generate
    40  	// an appropriate suffix for the DstName to disambiguate.
    41  	AddInterface(SrcName string, DstPrefix string, options ...IfaceOption) error
    42  
    43  	// SetGateway sets the default IPv4 gateway for the sandbox.
    44  	SetGateway(gw net.IP) error
    45  
    46  	// SetGatewayIPv6 sets the default IPv6 gateway for the sandbox.
    47  	SetGatewayIPv6(gw net.IP) error
    48  
    49  	// UnsetGateway the previously set default IPv4 gateway in the sandbox.
    50  	UnsetGateway() error
    51  
    52  	// UnsetGatewayIPv6 unsets the previously set default IPv6 gateway in the sandbox.
    53  	UnsetGatewayIPv6() error
    54  
    55  	// GetLoopbackIfaceName returns the name of the loopback interface
    56  	GetLoopbackIfaceName() string
    57  
    58  	// AddAliasIP adds the passed IP address to the named interface
    59  	AddAliasIP(ifName string, ip *net.IPNet) error
    60  
    61  	// RemoveAliasIP removes the passed IP address from the named interface
    62  	RemoveAliasIP(ifName string, ip *net.IPNet) error
    63  
    64  	// DisableARPForVIP disables ARP replies and requests for VIP addresses
    65  	// on a particular interface.
    66  	DisableARPForVIP(ifName string) error
    67  
    68  	// AddStaticRoute adds a static route to the sandbox.
    69  	AddStaticRoute(*types.StaticRoute) error
    70  
    71  	// RemoveStaticRoute removes a static route from the sandbox.
    72  	RemoveStaticRoute(*types.StaticRoute) error
    73  
    74  	// AddNeighbor adds a neighbor entry into the sandbox.
    75  	AddNeighbor(dstIP net.IP, dstMac net.HardwareAddr, force bool, option ...NeighOption) error
    76  
    77  	// DeleteNeighbor deletes neighbor entry from the sandbox.
    78  	DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr, osDelete bool) error
    79  
    80  	// NeighborOptions returns an interface with methods to set neighbor options.
    81  	NeighborOptions() NeighborOptionSetter
    82  
    83  	// InterfaceOptions an interface with methods to set interface options.
    84  	InterfaceOptions() IfaceOptionSetter
    85  
    86  	// InvokeFunc invoke a function in the network namespace.
    87  	InvokeFunc(func()) error
    88  
    89  	// Info returns an interface with methods to get sandbox state.
    90  	Info() Info
    91  
    92  	// Destroy destroys the sandbox.
    93  	Destroy() error
    94  
    95  	// Restore restores the sandbox.
    96  	Restore(ifsopt map[Iface][]IfaceOption, routes []*types.StaticRoute, gw net.IP, gw6 net.IP) error
    97  
    98  	// ApplyOSTweaks applies operating system specific knobs on the sandbox.
    99  	ApplyOSTweaks([]SandboxType)
   100  }
   101  
   102  // NeighborOptionSetter interface defines the option setter methods for interface options
   103  type NeighborOptionSetter interface {
   104  	// LinkName returns an option setter to set the srcName of the link that should
   105  	// be used in the neighbor entry
   106  	LinkName(string) NeighOption
   107  
   108  	// Family returns an option setter to set the address family for the neighbor
   109  	// entry. eg. AF_BRIDGE
   110  	Family(int) NeighOption
   111  }
   112  
   113  // IfaceOptionSetter interface defines the option setter methods for interface options.
   114  type IfaceOptionSetter interface {
   115  	// Bridge returns an option setter to set if the interface is a bridge.
   116  	Bridge(bool) IfaceOption
   117  
   118  	// MacAddress returns an option setter to set the MAC address.
   119  	MacAddress(net.HardwareAddr) IfaceOption
   120  
   121  	// Address returns an option setter to set IPv4 address.
   122  	Address(*net.IPNet) IfaceOption
   123  
   124  	// AddressIPv6 returns an option setter to set IPv6 address.
   125  	AddressIPv6(*net.IPNet) IfaceOption
   126  
   127  	// LinkLocalAddresses returns an option setter to set the link-local IP addresses.
   128  	LinkLocalAddresses([]*net.IPNet) IfaceOption
   129  
   130  	// Master returns an option setter to set the master interface if any for this
   131  	// interface. The master interface name should refer to the srcname of a
   132  	// previously added interface of type bridge.
   133  	Master(string) IfaceOption
   134  
   135  	// Routes returns an option setter to set interface routes.
   136  	Routes([]*net.IPNet) IfaceOption
   137  }
   138  
   139  // Info represents all possible information that
   140  // the driver wants to place in the sandbox which includes
   141  // interfaces, routes and gateway
   142  type Info interface {
   143  	// Interfaces returns the collection of Interface previously added with the AddInterface
   144  	// method. Note that this doesn't include network interfaces added in any
   145  	// other way (such as the default loopback interface which is automatically
   146  	// created on creation of a sandbox).
   147  	Interfaces() []Interface
   148  
   149  	// Gateway returns the IPv4 gateway for the sandbox.
   150  	Gateway() net.IP
   151  
   152  	// GatewayIPv6 returns the IPv6 gateway for the sandbox.
   153  	GatewayIPv6() net.IP
   154  
   155  	// StaticRoutes returns additional static routes for the sandbox. Note that
   156  	// directly connected routes are stored on the particular interface they
   157  	// refer to.
   158  	StaticRoutes() []*types.StaticRoute
   159  
   160  	// TODO: Add ip tables etc.
   161  }
   162  
   163  // Interface represents the settings and identity of a network device. It is
   164  // used as a return type for Network.Link, and it is common practice for the
   165  // caller to use this information when moving interface SrcName from host
   166  // namespace to DstName in a different net namespace with the appropriate
   167  // network settings.
   168  type Interface interface {
   169  	// SrcName returns the name of the interface in the origin network namespace.
   170  	SrcName() string
   171  
   172  	// DstName returns the name that will be assigned to the interface once
   173  	// moved inside a network namespace. When the caller passes in a DstName,
   174  	// it is only expected to pass a prefix. The name will be modified with an
   175  	// auto-generated suffix.
   176  	DstName() string
   177  
   178  	// Address returns the IPv4 address for the interface.
   179  	Address() *net.IPNet
   180  
   181  	// AddressIPv6 returns the IPv6 address for the interface.
   182  	AddressIPv6() *net.IPNet
   183  
   184  	// LinkLocalAddresses returns the link-local IP addresses assigned to the
   185  	// interface.
   186  	LinkLocalAddresses() []*net.IPNet
   187  
   188  	// Routes returns IP routes for the interface.
   189  	Routes() []*net.IPNet
   190  
   191  	// Bridge returns true if the interface is a bridge.
   192  	Bridge() bool
   193  
   194  	// Master returns the srcname of the master interface for this interface.
   195  	Master() string
   196  
   197  	// Remove an interface from the sandbox by renaming to original name
   198  	// and moving it out of the sandbox.
   199  	Remove() error
   200  
   201  	// Statistics returns the statistics for this interface
   202  	Statistics() (*types.InterfaceStatistics, error)
   203  }