forked from Code-Hex/gvisor-vmnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.go
125 lines (117 loc) · 3.41 KB
/
stack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package vmnet
import (
"fmt"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/network/arp"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/pkg/tcpip/transport/icmp"
"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
"gvisor.dev/gvisor/pkg/tcpip/transport/udp"
)
const nicID = 1
func createBaseNetStack() (*stack.Stack, error) {
netProtos := []stack.NetworkProtocolFactory{
ipv4.NewProtocol,
// ipv6.NewProtocol,
arp.NewProtocol,
}
transProtos := []stack.TransportProtocolFactory{
tcp.NewProtocol,
udp.NewProtocol,
icmp.NewProtocol4,
// icmp.NewProtocol6,
}
s := stack.New(stack.Options{
NetworkProtocols: netProtos,
TransportProtocols: transProtos,
})
transOpts := []struct {
name string
f func(*stack.Stack) tcpip.Error
}{
{
name: "Enable SACK",
f: func(s *stack.Stack) tcpip.Error {
opt := tcpip.TCPSACKEnabled(true)
return s.SetTransportProtocolOption(tcp.ProtocolNumber, &opt)
},
},
// {
// name: "Set reno congestion control",
// f: func(s *stack.Stack) tcpip.Error {
// opt := tcpip.CongestionControlOption("cubic") // "reno" or "cubic"
// return s.SetTransportProtocolOption(tcp.ProtocolNumber, &opt)
// },
// },
// {
// // https://gvisor.dev/blog/2021/08/31/gvisor-rack/
// name: "Enable RACK Recovery",
// f: func(s *stack.Stack) tcpip.Error {
// opt := tcpip.TCPRACKLossDetection
// return s.SetTransportProtocolOption(tcp.ProtocolNumber, &opt)
// },
// },
// {
// name: "Disable TCP Delay",
// f: func(s *stack.Stack) tcpip.Error {
// opt := tcpip.TCPDelayEnabled(false)
// return s.SetTransportProtocolOption(tcp.ProtocolNumber, &opt)
// },
// },
{
name: "Enable Receive Buffer Auto-Tuning",
f: func(s *stack.Stack) tcpip.Error {
opt := tcpip.TCPModerateReceiveBufferOption(true)
return s.SetTransportProtocolOption(tcp.ProtocolNumber, &opt)
},
},
{
name: "Set TCP Send Buffer Size Range",
f: func(s *stack.Stack) tcpip.Error {
return s.SetTransportProtocolOption(tcp.ProtocolNumber,
&tcpip.TCPSendBufferSizeRangeOption{
Min: tcp.MinBufferSize,
Default: tcp.DefaultSendBufferSize,
Max: tcp.MaxBufferSize,
})
},
},
{
name: "Set TCP Receive Buffer Size Range",
f: func(s *stack.Stack) tcpip.Error {
return s.SetTransportProtocolOption(tcp.ProtocolNumber,
&tcpip.TCPReceiveBufferSizeRangeOption{
Min: tcp.MinBufferSize,
Default: tcp.DefaultReceiveBufferSize,
Max: tcp.MaxBufferSize,
})
},
},
}
for _, transOpt := range transOpts {
if err := transOpt.f(s); err != nil {
return nil, fmt.Errorf("%s: %v", transOpt.name, err)
}
}
opt := tcpip.DefaultTTLOption(65)
if err := s.SetNetworkProtocolOption(ipv4.ProtocolNumber, &opt); err != nil {
return nil, fmt.Errorf("SetNetworkProtocolOption(%d, &%T(%d)): %s", ipv4.ProtocolNumber, opt, opt, err)
}
return s, nil
}
// used in TCP, UDP relay
func addAddress(s *stack.Stack, ip tcpip.Address) error {
protoAddr := tcpip.ProtocolAddress{
Protocol: ipv4.ProtocolNumber,
AddressWithPrefix: ip.WithPrefix(),
}
tcpipErr := s.AddProtocolAddress(nicID, protoAddr, stack.AddressProperties{
PEB: stack.CanBePrimaryEndpoint,
ConfigType: stack.AddressConfigStatic,
})
if tcpipErr != nil {
return fmt.Errorf("failed to add protocol address (%v): %v", ip, tcpipErr)
}
return nil
}