github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/sentry/inet/inet.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package inet defines semantics for IP stacks. 16 package inet 17 18 import ( 19 "time" 20 21 "github.com/metacubex/gvisor/pkg/abi/linux" 22 "github.com/metacubex/gvisor/pkg/tcpip" 23 "github.com/metacubex/gvisor/pkg/tcpip/stack" 24 ) 25 26 // Stack represents a TCP/IP stack. 27 type Stack interface { 28 // Interfaces returns all network interfaces as a mapping from interface 29 // indexes to interface properties. Interface indices are strictly positive 30 // integers. 31 Interfaces() map[int32]Interface 32 33 // RemoveInterface removes the specified network interface. 34 RemoveInterface(idx int32) error 35 36 // InterfaceAddrs returns all network interface addresses as a mapping from 37 // interface indexes to a slice of associated interface address properties. 38 InterfaceAddrs() map[int32][]InterfaceAddr 39 40 // AddInterfaceAddr adds an address to the network interface identified by 41 // idx. 42 AddInterfaceAddr(idx int32, addr InterfaceAddr) error 43 44 // RemoveInterfaceAddr removes an address from the network interface 45 // identified by idx. 46 RemoveInterfaceAddr(idx int32, addr InterfaceAddr) error 47 48 // SupportsIPv6 returns true if the stack supports IPv6 connectivity. 49 SupportsIPv6() bool 50 51 // TCPReceiveBufferSize returns TCP receive buffer size settings. 52 TCPReceiveBufferSize() (TCPBufferSize, error) 53 54 // SetTCPReceiveBufferSize attempts to change TCP receive buffer size 55 // settings. 56 SetTCPReceiveBufferSize(size TCPBufferSize) error 57 58 // TCPSendBufferSize returns TCP send buffer size settings. 59 TCPSendBufferSize() (TCPBufferSize, error) 60 61 // SetTCPSendBufferSize attempts to change TCP send buffer size settings. 62 SetTCPSendBufferSize(size TCPBufferSize) error 63 64 // TCPSACKEnabled returns true if RFC 2018 TCP Selective Acknowledgements 65 // are enabled. 66 TCPSACKEnabled() (bool, error) 67 68 // SetTCPSACKEnabled attempts to change TCP selective acknowledgement 69 // settings. 70 SetTCPSACKEnabled(enabled bool) error 71 72 // TCPRecovery returns the TCP loss detection algorithm. 73 TCPRecovery() (TCPLossRecovery, error) 74 75 // SetTCPRecovery attempts to change TCP loss detection algorithm. 76 SetTCPRecovery(recovery TCPLossRecovery) error 77 78 // Statistics reports stack statistics. 79 Statistics(stat any, arg string) error 80 81 // RouteTable returns the network stack's route table. 82 RouteTable() []Route 83 84 // Pause pauses the network stack before save. 85 Pause() 86 87 // Resume resumes the network stack after save. 88 Resume() 89 90 // Restore restarts the network stack after restore. 91 Restore() 92 93 // Destroy the network stack. 94 Destroy() 95 96 // RegisteredEndpoints returns all endpoints which are currently registered. 97 RegisteredEndpoints() []stack.TransportEndpoint 98 99 // CleanupEndpoints returns endpoints currently in the cleanup state. 100 CleanupEndpoints() []stack.TransportEndpoint 101 102 // RestoreCleanupEndpoints adds endpoints to cleanup tracking. This is useful 103 // for restoring a stack after a save. 104 RestoreCleanupEndpoints([]stack.TransportEndpoint) 105 106 // SetForwarding enables or disables packet forwarding between NICs. 107 SetForwarding(protocol tcpip.NetworkProtocolNumber, enable bool) error 108 109 // PortRange returns the UDP and TCP inclusive range of ephemeral ports 110 // used in both IPv4 and IPv6. 111 PortRange() (uint16, uint16) 112 113 // SetPortRange sets the UDP and TCP IPv4 and IPv6 ephemeral port range 114 // (inclusive). 115 SetPortRange(start uint16, end uint16) error 116 117 // GROTimeout returns the GRO timeout. 118 GROTimeout(NICID int32) (time.Duration, error) 119 120 // GROTimeout sets the GRO timeout. 121 SetGROTimeout(NICID int32, timeout time.Duration) error 122 } 123 124 // Interface contains information about a network interface. 125 type Interface struct { 126 // DeviceType is the device type, a Linux ARPHRD_* constant. 127 DeviceType uint16 128 129 // Flags is the device flags; see netdevice(7), under "Ioctls", 130 // "SIOCGIFFLAGS, SIOCSIFFLAGS". 131 Flags uint32 132 133 // Name is the device name. 134 Name string 135 136 // Addr is the hardware device address. 137 Addr []byte 138 139 // MTU is the maximum transmission unit. 140 MTU uint32 141 142 // Features are the device features queried from the host at 143 // stack creation time. These are immutable after startup. 144 Features []linux.EthtoolGetFeaturesBlock 145 } 146 147 // InterfaceAddr contains information about a network interface address. 148 type InterfaceAddr struct { 149 // Family is the address family, a Linux AF_* constant. 150 Family uint8 151 152 // PrefixLen is the address prefix length. 153 PrefixLen uint8 154 155 // Flags is the address flags. 156 Flags uint8 157 158 // Addr is the actual address. 159 Addr []byte 160 } 161 162 // TCPBufferSize contains settings controlling TCP buffer sizing. 163 // 164 // +stateify savable 165 type TCPBufferSize struct { 166 // Min is the minimum size. 167 Min int 168 169 // Default is the default size. 170 Default int 171 172 // Max is the maximum size. 173 Max int 174 } 175 176 // StatDev describes one line of /proc/net/dev, i.e., stats for one network 177 // interface. 178 type StatDev [16]uint64 179 180 // Route contains information about a network route. 181 type Route struct { 182 // Family is the address family, a Linux AF_* constant. 183 Family uint8 184 185 // DstLen is the length of the destination address. 186 DstLen uint8 187 188 // SrcLen is the length of the source address. 189 SrcLen uint8 190 191 // TOS is the Type of Service filter. 192 TOS uint8 193 194 // Table is the routing table ID. 195 Table uint8 196 197 // Protocol is the route origin, a Linux RTPROT_* constant. 198 Protocol uint8 199 200 // Scope is the distance to destination, a Linux RT_SCOPE_* constant. 201 Scope uint8 202 203 // Type is the route origin, a Linux RTN_* constant. 204 Type uint8 205 206 // Flags are route flags. See rtnetlink(7) under "rtm_flags". 207 Flags uint32 208 209 // DstAddr is the route destination address (RTA_DST). 210 DstAddr []byte 211 212 // SrcAddr is the route source address (RTA_SRC). 213 SrcAddr []byte 214 215 // OutputInterface is the output interface index (RTA_OIF). 216 OutputInterface int32 217 218 // GatewayAddr is the route gateway address (RTA_GATEWAY). 219 GatewayAddr []byte 220 } 221 222 // Below SNMP metrics are from Linux/usr/include/linux/snmp.h. 223 224 // StatSNMPIP describes Ip line of /proc/net/snmp. 225 type StatSNMPIP [19]uint64 226 227 // StatSNMPICMP describes Icmp line of /proc/net/snmp. 228 type StatSNMPICMP [27]uint64 229 230 // StatSNMPICMPMSG describes IcmpMsg line of /proc/net/snmp. 231 type StatSNMPICMPMSG [512]uint64 232 233 // StatSNMPTCP describes Tcp line of /proc/net/snmp. 234 type StatSNMPTCP [15]uint64 235 236 // StatSNMPUDP describes Udp line of /proc/net/snmp. 237 type StatSNMPUDP [8]uint64 238 239 // StatSNMPUDPLite describes UdpLite line of /proc/net/snmp. 240 type StatSNMPUDPLite [8]uint64 241 242 // TCPLossRecovery indicates TCP loss detection and recovery methods to use. 243 type TCPLossRecovery int32 244 245 // Loss recovery constants from include/net/tcp.h which are used to set 246 // /proc/sys/net/ipv4/tcp_recovery. 247 const ( 248 TCP_RACK_LOSS_DETECTION TCPLossRecovery = 1 << iota 249 TCP_RACK_STATIC_REO_WND 250 TCP_RACK_NO_DUPTHRESH 251 )