github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/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/MerlinKodo/gvisor/pkg/abi/linux"
    22  	"github.com/MerlinKodo/gvisor/pkg/tcpip"
    23  	"github.com/MerlinKodo/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 restarts the network stack after restore.
    88  	Resume()
    89  
    90  	// Destroy the network stack.
    91  	Destroy()
    92  
    93  	// RegisteredEndpoints returns all endpoints which are currently registered.
    94  	RegisteredEndpoints() []stack.TransportEndpoint
    95  
    96  	// CleanupEndpoints returns endpoints currently in the cleanup state.
    97  	CleanupEndpoints() []stack.TransportEndpoint
    98  
    99  	// RestoreCleanupEndpoints adds endpoints to cleanup tracking. This is useful
   100  	// for restoring a stack after a save.
   101  	RestoreCleanupEndpoints([]stack.TransportEndpoint)
   102  
   103  	// SetForwarding enables or disables packet forwarding between NICs.
   104  	SetForwarding(protocol tcpip.NetworkProtocolNumber, enable bool) error
   105  
   106  	// PortRange returns the UDP and TCP inclusive range of ephemeral ports
   107  	// used in both IPv4 and IPv6.
   108  	PortRange() (uint16, uint16)
   109  
   110  	// SetPortRange sets the UDP and TCP IPv4 and IPv6 ephemeral port range
   111  	// (inclusive).
   112  	SetPortRange(start uint16, end uint16) error
   113  
   114  	// GROTimeout returns the GRO timeout.
   115  	GROTimeout(NICID int32) (time.Duration, error)
   116  
   117  	// GROTimeout sets the GRO timeout.
   118  	SetGROTimeout(NICID int32, timeout time.Duration) error
   119  }
   120  
   121  // Interface contains information about a network interface.
   122  type Interface struct {
   123  	// DeviceType is the device type, a Linux ARPHRD_* constant.
   124  	DeviceType uint16
   125  
   126  	// Flags is the device flags; see netdevice(7), under "Ioctls",
   127  	// "SIOCGIFFLAGS, SIOCSIFFLAGS".
   128  	Flags uint32
   129  
   130  	// Name is the device name.
   131  	Name string
   132  
   133  	// Addr is the hardware device address.
   134  	Addr []byte
   135  
   136  	// MTU is the maximum transmission unit.
   137  	MTU uint32
   138  
   139  	// Features are the device features queried from the host at
   140  	// stack creation time. These are immutable after startup.
   141  	Features []linux.EthtoolGetFeaturesBlock
   142  }
   143  
   144  // InterfaceAddr contains information about a network interface address.
   145  type InterfaceAddr struct {
   146  	// Family is the address family, a Linux AF_* constant.
   147  	Family uint8
   148  
   149  	// PrefixLen is the address prefix length.
   150  	PrefixLen uint8
   151  
   152  	// Flags is the address flags.
   153  	Flags uint8
   154  
   155  	// Addr is the actual address.
   156  	Addr []byte
   157  }
   158  
   159  // TCPBufferSize contains settings controlling TCP buffer sizing.
   160  //
   161  // +stateify savable
   162  type TCPBufferSize struct {
   163  	// Min is the minimum size.
   164  	Min int
   165  
   166  	// Default is the default size.
   167  	Default int
   168  
   169  	// Max is the maximum size.
   170  	Max int
   171  }
   172  
   173  // StatDev describes one line of /proc/net/dev, i.e., stats for one network
   174  // interface.
   175  type StatDev [16]uint64
   176  
   177  // Route contains information about a network route.
   178  type Route struct {
   179  	// Family is the address family, a Linux AF_* constant.
   180  	Family uint8
   181  
   182  	// DstLen is the length of the destination address.
   183  	DstLen uint8
   184  
   185  	// SrcLen is the length of the source address.
   186  	SrcLen uint8
   187  
   188  	// TOS is the Type of Service filter.
   189  	TOS uint8
   190  
   191  	// Table is the routing table ID.
   192  	Table uint8
   193  
   194  	// Protocol is the route origin, a Linux RTPROT_* constant.
   195  	Protocol uint8
   196  
   197  	// Scope is the distance to destination, a Linux RT_SCOPE_* constant.
   198  	Scope uint8
   199  
   200  	// Type is the route origin, a Linux RTN_* constant.
   201  	Type uint8
   202  
   203  	// Flags are route flags. See rtnetlink(7) under "rtm_flags".
   204  	Flags uint32
   205  
   206  	// DstAddr is the route destination address (RTA_DST).
   207  	DstAddr []byte
   208  
   209  	// SrcAddr is the route source address (RTA_SRC).
   210  	SrcAddr []byte
   211  
   212  	// OutputInterface is the output interface index (RTA_OIF).
   213  	OutputInterface int32
   214  
   215  	// GatewayAddr is the route gateway address (RTA_GATEWAY).
   216  	GatewayAddr []byte
   217  }
   218  
   219  // Below SNMP metrics are from Linux/usr/include/linux/snmp.h.
   220  
   221  // StatSNMPIP describes Ip line of /proc/net/snmp.
   222  type StatSNMPIP [19]uint64
   223  
   224  // StatSNMPICMP describes Icmp line of /proc/net/snmp.
   225  type StatSNMPICMP [27]uint64
   226  
   227  // StatSNMPICMPMSG describes IcmpMsg line of /proc/net/snmp.
   228  type StatSNMPICMPMSG [512]uint64
   229  
   230  // StatSNMPTCP describes Tcp line of /proc/net/snmp.
   231  type StatSNMPTCP [15]uint64
   232  
   233  // StatSNMPUDP describes Udp line of /proc/net/snmp.
   234  type StatSNMPUDP [8]uint64
   235  
   236  // StatSNMPUDPLite describes UdpLite line of /proc/net/snmp.
   237  type StatSNMPUDPLite [8]uint64
   238  
   239  // TCPLossRecovery indicates TCP loss detection and recovery methods to use.
   240  type TCPLossRecovery int32
   241  
   242  // Loss recovery constants from include/net/tcp.h which are used to set
   243  // /proc/sys/net/ipv4/tcp_recovery.
   244  const (
   245  	TCP_RACK_LOSS_DETECTION TCPLossRecovery = 1 << iota
   246  	TCP_RACK_STATIC_REO_WND
   247  	TCP_RACK_NO_DUPTHRESH
   248  )