2019-04-29 14:25:05 -07:00
|
|
|
// Copyright 2018 The gVisor Authors.
|
2018-04-27 10:37:02 -07:00
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
package inet
|
|
|
|
|
|
2019-10-27 15:14:35 +03:00
|
|
|
import (
|
2020-10-27 00:16:14 -07:00
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
2022-11-08 16:31:37 -08:00
|
|
|
"time"
|
2020-10-27 00:16:14 -07:00
|
|
|
|
2024-04-24 18:36:27 -07:00
|
|
|
"gvisor.dev/gvisor/pkg/context"
|
|
|
|
|
"gvisor.dev/gvisor/pkg/sentry/socket/netlink/nlmsg"
|
|
|
|
|
"gvisor.dev/gvisor/pkg/syserr"
|
2019-10-27 15:14:35 +03:00
|
|
|
"gvisor.dev/gvisor/pkg/tcpip"
|
2020-08-17 21:44:31 -04:00
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
2019-10-27 15:14:35 +03:00
|
|
|
)
|
|
|
|
|
|
2024-07-09 18:01:07 -07:00
|
|
|
var _ Stack = (*TestStack)(nil)
|
|
|
|
|
|
2018-04-27 10:37:02 -07:00
|
|
|
// TestStack is a dummy implementation of Stack for tests.
|
|
|
|
|
type TestStack struct {
|
|
|
|
|
InterfacesMap map[int32]Interface
|
|
|
|
|
InterfaceAddrsMap map[int32][]InterfaceAddr
|
2019-07-31 20:29:07 -07:00
|
|
|
RouteList []Route
|
2018-04-27 10:37:02 -07:00
|
|
|
SupportsIPv6Flag bool
|
|
|
|
|
TCPRecvBufSize TCPBufferSize
|
|
|
|
|
TCPSendBufSize TCPBufferSize
|
|
|
|
|
TCPSACKFlag bool
|
2020-08-05 20:45:02 -07:00
|
|
|
Recovery TCPLossRecovery
|
2019-10-27 15:14:35 +03:00
|
|
|
IPForwarding bool
|
2018-04-27 10:37:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewTestStack returns a TestStack with no network interfaces. The value of
|
|
|
|
|
// all other options is unspecified; tests that rely on specific values must
|
|
|
|
|
// set them explicitly.
|
|
|
|
|
func NewTestStack() *TestStack {
|
|
|
|
|
return &TestStack{
|
|
|
|
|
InterfacesMap: make(map[int32]Interface),
|
|
|
|
|
InterfaceAddrsMap: make(map[int32][]InterfaceAddr),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// Interfaces implements Stack.
|
2018-04-27 10:37:02 -07:00
|
|
|
func (s *TestStack) Interfaces() map[int32]Interface {
|
|
|
|
|
return s.InterfacesMap
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-13 15:02:23 -07:00
|
|
|
// Destroy implements Stack.
|
|
|
|
|
func (s *TestStack) Destroy() {
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// RemoveInterface implements Stack.
|
|
|
|
|
func (s *TestStack) RemoveInterface(idx int32) error {
|
|
|
|
|
delete(s.InterfacesMap, idx)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-24 18:36:27 -07:00
|
|
|
// SetInterface implements Stack.
|
|
|
|
|
func (s *TestStack) SetInterface(ctx context.Context, msg *nlmsg.Message) *syserr.Error {
|
|
|
|
|
panic("unimplemented")
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// InterfaceAddrs implements Stack.
|
2018-04-27 10:37:02 -07:00
|
|
|
func (s *TestStack) InterfaceAddrs() map[int32][]InterfaceAddr {
|
|
|
|
|
return s.InterfaceAddrsMap
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// AddInterfaceAddr implements Stack.
|
2020-02-04 18:04:26 -08:00
|
|
|
func (s *TestStack) AddInterfaceAddr(idx int32, addr InterfaceAddr) error {
|
|
|
|
|
s.InterfaceAddrsMap[idx] = append(s.InterfaceAddrsMap[idx], addr)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// RemoveInterfaceAddr implements Stack.
|
2020-10-27 00:16:14 -07:00
|
|
|
func (s *TestStack) RemoveInterfaceAddr(idx int32, addr InterfaceAddr) error {
|
|
|
|
|
interfaceAddrs, ok := s.InterfaceAddrsMap[idx]
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("unknown idx: %d", idx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var filteredAddrs []InterfaceAddr
|
|
|
|
|
for _, interfaceAddr := range interfaceAddrs {
|
|
|
|
|
if !bytes.Equal(interfaceAddr.Addr, addr.Addr) {
|
|
|
|
|
filteredAddrs = append(filteredAddrs, addr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
s.InterfaceAddrsMap[idx] = filteredAddrs
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// SupportsIPv6 implements Stack.
|
2018-04-27 10:37:02 -07:00
|
|
|
func (s *TestStack) SupportsIPv6() bool {
|
|
|
|
|
return s.SupportsIPv6Flag
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// TCPReceiveBufferSize implements Stack.
|
2018-04-27 10:37:02 -07:00
|
|
|
func (s *TestStack) TCPReceiveBufferSize() (TCPBufferSize, error) {
|
|
|
|
|
return s.TCPRecvBufSize, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// SetTCPReceiveBufferSize implements Stack.
|
2018-04-27 10:37:02 -07:00
|
|
|
func (s *TestStack) SetTCPReceiveBufferSize(size TCPBufferSize) error {
|
|
|
|
|
s.TCPRecvBufSize = size
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// TCPSendBufferSize implements Stack.
|
2018-04-27 10:37:02 -07:00
|
|
|
func (s *TestStack) TCPSendBufferSize() (TCPBufferSize, error) {
|
|
|
|
|
return s.TCPSendBufSize, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// SetTCPSendBufferSize implements Stack.
|
2018-04-27 10:37:02 -07:00
|
|
|
func (s *TestStack) SetTCPSendBufferSize(size TCPBufferSize) error {
|
|
|
|
|
s.TCPSendBufSize = size
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// TCPSACKEnabled implements Stack.
|
2018-04-27 10:37:02 -07:00
|
|
|
func (s *TestStack) TCPSACKEnabled() (bool, error) {
|
|
|
|
|
return s.TCPSACKFlag, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// SetTCPSACKEnabled implements Stack.
|
2018-04-27 10:37:02 -07:00
|
|
|
func (s *TestStack) SetTCPSACKEnabled(enabled bool) error {
|
|
|
|
|
s.TCPSACKFlag = enabled
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-07-15 22:49:58 -07:00
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// TCPRecovery implements Stack.
|
2020-08-05 20:45:02 -07:00
|
|
|
func (s *TestStack) TCPRecovery() (TCPLossRecovery, error) {
|
|
|
|
|
return s.Recovery, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// SetTCPRecovery implements Stack.
|
2020-08-05 20:45:02 -07:00
|
|
|
func (s *TestStack) SetTCPRecovery(recovery TCPLossRecovery) error {
|
|
|
|
|
s.Recovery = recovery
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// Statistics implements Stack.
|
2022-11-08 13:12:12 -08:00
|
|
|
func (s *TestStack) Statistics(stat any, arg string) error {
|
2019-07-15 22:49:58 -07:00
|
|
|
return nil
|
|
|
|
|
}
|
2019-07-31 20:29:07 -07:00
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// RouteTable implements Stack.
|
2019-07-31 20:29:07 -07:00
|
|
|
func (s *TestStack) RouteTable() []Route {
|
|
|
|
|
return s.RouteList
|
|
|
|
|
}
|
2019-08-08 12:32:00 -07:00
|
|
|
|
2024-09-17 14:30:56 -07:00
|
|
|
// RemoveRoute implements Stack.
|
|
|
|
|
func (s *TestStack) RemoveRoute(ctx context.Context, msg *nlmsg.Message) *syserr.Error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-09 18:01:07 -07:00
|
|
|
// NewRoute implements Stack.
|
|
|
|
|
func (s *TestStack) NewRoute(ctx context.Context, msg *nlmsg.Message) *syserr.Error {
|
|
|
|
|
return syserr.ErrNotPermitted
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-18 17:26:26 -07:00
|
|
|
// Pause implements Stack.
|
|
|
|
|
func (s *TestStack) Pause() {}
|
|
|
|
|
|
2024-03-06 13:22:36 -08:00
|
|
|
// Restore implements Stack.
|
|
|
|
|
func (s *TestStack) Restore() {}
|
2019-10-29 16:13:43 -07:00
|
|
|
|
2024-11-20 11:08:45 -08:00
|
|
|
// ReplaceConfig implements Stack.
|
|
|
|
|
func (s *TestStack) ReplaceConfig(_ Stack) {}
|
|
|
|
|
|
2024-03-13 10:33:23 -07:00
|
|
|
// Resume implements Stack.
|
|
|
|
|
func (s *TestStack) Resume() {}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// RegisteredEndpoints implements Stack.
|
2019-10-29 16:13:43 -07:00
|
|
|
func (s *TestStack) RegisteredEndpoints() []stack.TransportEndpoint {
|
|
|
|
|
return nil
|
2019-08-08 12:32:00 -07:00
|
|
|
}
|
2019-10-27 15:14:35 +03:00
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// CleanupEndpoints implements Stack.
|
2019-10-29 16:13:43 -07:00
|
|
|
func (s *TestStack) CleanupEndpoints() []stack.TransportEndpoint {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// RestoreCleanupEndpoints implements Stack.
|
2019-10-29 16:13:43 -07:00
|
|
|
func (s *TestStack) RestoreCleanupEndpoints([]stack.TransportEndpoint) {}
|
2020-08-17 21:44:31 -04:00
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// SetForwarding implements Stack.
|
2019-10-27 15:14:35 +03:00
|
|
|
func (s *TestStack) SetForwarding(protocol tcpip.NetworkProtocolNumber, enable bool) error {
|
|
|
|
|
s.IPForwarding = enable
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-03-08 20:37:14 -08:00
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// PortRange implements Stack.
|
2021-03-08 20:37:14 -08:00
|
|
|
func (*TestStack) PortRange() (uint16, uint16) {
|
|
|
|
|
// Use the default Linux values per net/ipv4/af_inet.c:inet_init_net().
|
2023-04-21 14:29:08 -07:00
|
|
|
return 32768, 60999
|
2021-03-08 20:37:14 -08:00
|
|
|
}
|
|
|
|
|
|
2021-07-30 15:38:51 -07:00
|
|
|
// SetPortRange implements Stack.
|
2021-03-08 20:37:14 -08:00
|
|
|
func (*TestStack) SetPortRange(start uint16, end uint16) error {
|
|
|
|
|
// No-op.
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-11-08 16:31:37 -08:00
|
|
|
|
|
|
|
|
// GROTimeout implements Stack.
|
|
|
|
|
func (*TestStack) GROTimeout(NICID int32) (time.Duration, error) {
|
|
|
|
|
// No-op.
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetGROTimeout implements Stack.
|
|
|
|
|
func (*TestStack) SetGROTimeout(NICID int32, timeout time.Duration) error {
|
|
|
|
|
// No-op.
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2024-08-28 12:45:30 -07:00
|
|
|
|
|
|
|
|
// EnableSaveRestore implements Stack.
|
|
|
|
|
func (*TestStack) EnableSaveRestore() error {
|
|
|
|
|
// No-op.
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2024-11-20 11:08:45 -08:00
|
|
|
|
|
|
|
|
// IsSaveRestoreEnabled implements Stack.
|
|
|
|
|
func (*TestStack) IsSaveRestoreEnabled() bool {
|
|
|
|
|
// No-op.
|
|
|
|
|
return false
|
|
|
|
|
}
|
2024-12-18 12:48:08 -08:00
|
|
|
|
|
|
|
|
// Stats implements Stack.
|
|
|
|
|
func (*TestStack) Stats() tcpip.Stats {
|
|
|
|
|
// No-op.
|
|
|
|
|
return tcpip.Stats{}
|
|
|
|
|
}
|