github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/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/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. 12 type 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 // Add a static route to the sandbox. 36 AddStaticRoute(*types.StaticRoute) error 37 38 // Remove a static route from the sandbox. 39 RemoveStaticRoute(*types.StaticRoute) error 40 41 // AddNeighbor adds a neighbor entry into the sandbox. 42 AddNeighbor(dstIP net.IP, dstMac net.HardwareAddr, option ...NeighOption) error 43 44 // DeleteNeighbor deletes neighbor entry from the sandbox. 45 DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr, osDelete bool) error 46 47 // Returns an interface with methods to set neighbor options. 48 NeighborOptions() NeighborOptionSetter 49 50 // Returns an interface with methods to set interface options. 51 InterfaceOptions() IfaceOptionSetter 52 53 //Invoke 54 InvokeFunc(func()) error 55 56 // Returns an interface with methods to get sandbox state. 57 Info() Info 58 59 // Destroy the sandbox 60 Destroy() error 61 62 // restore sandbox 63 Restore(ifsopt map[string][]IfaceOption, routes []*types.StaticRoute, gw net.IP, gw6 net.IP) error 64 } 65 66 // NeighborOptionSetter interface defines the option setter methods for interface options 67 type NeighborOptionSetter interface { 68 // LinkName returns an option setter to set the srcName of the link that should 69 // be used in the neighbor entry 70 LinkName(string) NeighOption 71 72 // Family returns an option setter to set the address family for the neighbor 73 // entry. eg. AF_BRIDGE 74 Family(int) NeighOption 75 } 76 77 // IfaceOptionSetter interface defines the option setter methods for interface options. 78 type IfaceOptionSetter interface { 79 // Bridge returns an option setter to set if the interface is a bridge. 80 Bridge(bool) IfaceOption 81 82 // MacAddress returns an option setter to set the MAC address. 83 MacAddress(net.HardwareAddr) IfaceOption 84 85 // Address returns an option setter to set IPv4 address. 86 Address(*net.IPNet) IfaceOption 87 88 // Address returns an option setter to set IPv6 address. 89 AddressIPv6(*net.IPNet) IfaceOption 90 91 // LinkLocalAddresses returns an option setter to set the link-local IP addresses. 92 LinkLocalAddresses([]*net.IPNet) IfaceOption 93 94 // IPAliases returns an option setter to set IP address Aliases 95 IPAliases([]*net.IPNet) IfaceOption 96 97 // Master returns an option setter to set the master interface if any for this 98 // interface. The master interface name should refer to the srcname of a 99 // previously added interface of type bridge. 100 Master(string) IfaceOption 101 102 // Address returns an option setter to set interface routes. 103 Routes([]*net.IPNet) IfaceOption 104 } 105 106 // Info represents all possible information that 107 // the driver wants to place in the sandbox which includes 108 // interfaces, routes and gateway 109 type Info interface { 110 // The collection of Interface previously added with the AddInterface 111 // method. Note that this doesn't include network interfaces added in any 112 // other way (such as the default loopback interface which is automatically 113 // created on creation of a sandbox). 114 Interfaces() []Interface 115 116 // IPv4 gateway for the sandbox. 117 Gateway() net.IP 118 119 // IPv6 gateway for the sandbox. 120 GatewayIPv6() net.IP 121 122 // Additional static routes for the sandbox. (Note that directly 123 // connected routes are stored on the particular interface they refer to.) 124 StaticRoutes() []*types.StaticRoute 125 126 // TODO: Add ip tables etc. 127 } 128 129 // Interface represents the settings and identity of a network device. It is 130 // used as a return type for Network.Link, and it is common practice for the 131 // caller to use this information when moving interface SrcName from host 132 // namespace to DstName in a different net namespace with the appropriate 133 // network settings. 134 type Interface interface { 135 // The name of the interface in the origin network namespace. 136 SrcName() string 137 138 // The name that will be assigned to the interface once moves inside a 139 // network namespace. When the caller passes in a DstName, it is only 140 // expected to pass a prefix. The name will modified with an appropriately 141 // auto-generated suffix. 142 DstName() string 143 144 // IPv4 address for the interface. 145 Address() *net.IPNet 146 147 // IPv6 address for the interface. 148 AddressIPv6() *net.IPNet 149 150 // LinkLocalAddresses returns the link-local IP addresses assigned to the interface. 151 LinkLocalAddresses() []*net.IPNet 152 153 // IPAliases returns the IP address aliases assigned to the interface. 154 IPAliases() []*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 }