gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/inet/test_stack.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
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"time"
    21  
    22  	"gvisor.dev/gvisor/pkg/context"
    23  	"gvisor.dev/gvisor/pkg/sentry/socket/netlink/nlmsg"
    24  	"gvisor.dev/gvisor/pkg/syserr"
    25  	"gvisor.dev/gvisor/pkg/tcpip"
    26  	"gvisor.dev/gvisor/pkg/tcpip/stack"
    27  )
    28  
    29  // TestStack is a dummy implementation of Stack for tests.
    30  type TestStack struct {
    31  	InterfacesMap     map[int32]Interface
    32  	InterfaceAddrsMap map[int32][]InterfaceAddr
    33  	RouteList         []Route
    34  	SupportsIPv6Flag  bool
    35  	TCPRecvBufSize    TCPBufferSize
    36  	TCPSendBufSize    TCPBufferSize
    37  	TCPSACKFlag       bool
    38  	Recovery          TCPLossRecovery
    39  	IPForwarding      bool
    40  }
    41  
    42  // NewTestStack returns a TestStack with no network interfaces. The value of
    43  // all other options is unspecified; tests that rely on specific values must
    44  // set them explicitly.
    45  func NewTestStack() *TestStack {
    46  	return &TestStack{
    47  		InterfacesMap:     make(map[int32]Interface),
    48  		InterfaceAddrsMap: make(map[int32][]InterfaceAddr),
    49  	}
    50  }
    51  
    52  // Interfaces implements Stack.
    53  func (s *TestStack) Interfaces() map[int32]Interface {
    54  	return s.InterfacesMap
    55  }
    56  
    57  // Destroy implements Stack.
    58  func (s *TestStack) Destroy() {
    59  }
    60  
    61  // RemoveInterface implements Stack.
    62  func (s *TestStack) RemoveInterface(idx int32) error {
    63  	delete(s.InterfacesMap, idx)
    64  	return nil
    65  }
    66  
    67  // SetInterface implements Stack.
    68  func (s *TestStack) SetInterface(ctx context.Context, msg *nlmsg.Message) *syserr.Error {
    69  	panic("unimplemented")
    70  }
    71  
    72  // InterfaceAddrs implements Stack.
    73  func (s *TestStack) InterfaceAddrs() map[int32][]InterfaceAddr {
    74  	return s.InterfaceAddrsMap
    75  }
    76  
    77  // AddInterfaceAddr implements Stack.
    78  func (s *TestStack) AddInterfaceAddr(idx int32, addr InterfaceAddr) error {
    79  	s.InterfaceAddrsMap[idx] = append(s.InterfaceAddrsMap[idx], addr)
    80  	return nil
    81  }
    82  
    83  // RemoveInterfaceAddr implements Stack.
    84  func (s *TestStack) RemoveInterfaceAddr(idx int32, addr InterfaceAddr) error {
    85  	interfaceAddrs, ok := s.InterfaceAddrsMap[idx]
    86  	if !ok {
    87  		return fmt.Errorf("unknown idx: %d", idx)
    88  	}
    89  
    90  	var filteredAddrs []InterfaceAddr
    91  	for _, interfaceAddr := range interfaceAddrs {
    92  		if !bytes.Equal(interfaceAddr.Addr, addr.Addr) {
    93  			filteredAddrs = append(filteredAddrs, addr)
    94  		}
    95  	}
    96  	s.InterfaceAddrsMap[idx] = filteredAddrs
    97  
    98  	return nil
    99  }
   100  
   101  // SupportsIPv6 implements Stack.
   102  func (s *TestStack) SupportsIPv6() bool {
   103  	return s.SupportsIPv6Flag
   104  }
   105  
   106  // TCPReceiveBufferSize implements Stack.
   107  func (s *TestStack) TCPReceiveBufferSize() (TCPBufferSize, error) {
   108  	return s.TCPRecvBufSize, nil
   109  }
   110  
   111  // SetTCPReceiveBufferSize implements Stack.
   112  func (s *TestStack) SetTCPReceiveBufferSize(size TCPBufferSize) error {
   113  	s.TCPRecvBufSize = size
   114  	return nil
   115  }
   116  
   117  // TCPSendBufferSize implements Stack.
   118  func (s *TestStack) TCPSendBufferSize() (TCPBufferSize, error) {
   119  	return s.TCPSendBufSize, nil
   120  }
   121  
   122  // SetTCPSendBufferSize implements Stack.
   123  func (s *TestStack) SetTCPSendBufferSize(size TCPBufferSize) error {
   124  	s.TCPSendBufSize = size
   125  	return nil
   126  }
   127  
   128  // TCPSACKEnabled implements Stack.
   129  func (s *TestStack) TCPSACKEnabled() (bool, error) {
   130  	return s.TCPSACKFlag, nil
   131  }
   132  
   133  // SetTCPSACKEnabled implements Stack.
   134  func (s *TestStack) SetTCPSACKEnabled(enabled bool) error {
   135  	s.TCPSACKFlag = enabled
   136  	return nil
   137  }
   138  
   139  // TCPRecovery implements Stack.
   140  func (s *TestStack) TCPRecovery() (TCPLossRecovery, error) {
   141  	return s.Recovery, nil
   142  }
   143  
   144  // SetTCPRecovery implements Stack.
   145  func (s *TestStack) SetTCPRecovery(recovery TCPLossRecovery) error {
   146  	s.Recovery = recovery
   147  	return nil
   148  }
   149  
   150  // Statistics implements Stack.
   151  func (s *TestStack) Statistics(stat any, arg string) error {
   152  	return nil
   153  }
   154  
   155  // RouteTable implements Stack.
   156  func (s *TestStack) RouteTable() []Route {
   157  	return s.RouteList
   158  }
   159  
   160  // Pause implements Stack.
   161  func (s *TestStack) Pause() {}
   162  
   163  // Restore implements Stack.
   164  func (s *TestStack) Restore() {}
   165  
   166  // Resume implements Stack.
   167  func (s *TestStack) Resume() {}
   168  
   169  // RegisteredEndpoints implements Stack.
   170  func (s *TestStack) RegisteredEndpoints() []stack.TransportEndpoint {
   171  	return nil
   172  }
   173  
   174  // CleanupEndpoints implements Stack.
   175  func (s *TestStack) CleanupEndpoints() []stack.TransportEndpoint {
   176  	return nil
   177  }
   178  
   179  // RestoreCleanupEndpoints implements Stack.
   180  func (s *TestStack) RestoreCleanupEndpoints([]stack.TransportEndpoint) {}
   181  
   182  // SetForwarding implements Stack.
   183  func (s *TestStack) SetForwarding(protocol tcpip.NetworkProtocolNumber, enable bool) error {
   184  	s.IPForwarding = enable
   185  	return nil
   186  }
   187  
   188  // PortRange implements Stack.
   189  func (*TestStack) PortRange() (uint16, uint16) {
   190  	// Use the default Linux values per net/ipv4/af_inet.c:inet_init_net().
   191  	return 32768, 60999
   192  }
   193  
   194  // SetPortRange implements Stack.
   195  func (*TestStack) SetPortRange(start uint16, end uint16) error {
   196  	// No-op.
   197  	return nil
   198  }
   199  
   200  // GROTimeout implements Stack.
   201  func (*TestStack) GROTimeout(NICID int32) (time.Duration, error) {
   202  	// No-op.
   203  	return 0, nil
   204  }
   205  
   206  // SetGROTimeout implements Stack.
   207  func (*TestStack) SetGROTimeout(NICID int32, timeout time.Duration) error {
   208  	// No-op.
   209  	return nil
   210  }