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