-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
229 lines (203 loc) · 5.42 KB
/
main.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"net"
"os"
"runtime"
"strconv"
"unsafe"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
"golang.org/x/sys/unix"
)
// parts copied from Tailscale/Wireguard
const (
// TODO: upstream to x/sys/unix
socketOptionLevelUDP = 17
socketOptionUDPSegment = 103
socketOptionUDPGRO = 104
)
func supportsUDPOffload(conn *net.UDPConn) (txOffload, rxOffload bool) {
rc, err := conn.SyscallConn()
if err != nil {
return
}
err = rc.Control(func(fd uintptr) {
size, errSyscall := unix.GetsockoptInt(int(fd), unix.IPPROTO_UDP, socketOptionUDPSegment)
fmt.Println("gso size = ", size)
if errSyscall != nil {
return
}
txOffload = true
// not sure that one is correct
opt, errSyscall := unix.GetsockoptInt(int(fd), unix.IPPROTO_UDP, socketOptionUDPGRO)
if errSyscall != nil {
return
}
rxOffload = opt == 1
})
if err != nil {
return false, false
}
return txOffload, rxOffload
}
func listenNet(network string, port int) (*net.UDPConn, int, error) {
lc := &net.ListenConfig{}
conn, err := lc.ListenPacket(context.Background(), network, ":"+strconv.Itoa(port))
if err != nil {
return nil, 0, err
}
// Retrieve port.
laddr := conn.LocalAddr()
uaddr, err := net.ResolveUDPAddr(
laddr.Network(),
laddr.String(),
)
if err != nil {
return nil, 0, err
}
return conn.(*net.UDPConn), uaddr.Port, nil
}
const (
sizeOfGSOData = 2
)
func setGSOSize(control *[]byte, gsoSize uint16) {
existingLen := len(*control)
avail := cap(*control) - existingLen
space := unix.CmsgSpace(sizeOfGSOData)
if avail < space {
return
}
*control = (*control)[:cap(*control)]
gsoControl := (*control)[existingLen:]
hdr := (*unix.Cmsghdr)(unsafe.Pointer(&(gsoControl)[0]))
hdr.Level = socketOptionLevelUDP
hdr.Type = socketOptionUDPSegment
hdr.SetLen(unix.CmsgLen(sizeOfGSOData))
copy((gsoControl)[unix.SizeofCmsghdr:], unsafe.Slice((*byte)(unsafe.Pointer(&gsoSize)), sizeOfGSOData))
*control = (*control)[:existingLen+space]
}
var optV4remote = flag.String("r4", "127.0.0.1:0", "remote v4 addr")
var optV6remote = flag.String("r6", "["+net.IPv6loopback.String()+"]:0", "remote v6 addr")
func parseRemote(remote string) (net.IP, int) {
host, port, err := net.SplitHostPort(remote)
if err != nil {
log.Fatalf("bad remote %v", remote)
}
p, err := strconv.Atoi(port)
if err != nil {
log.Fatalf("bad remote port: %v", remote)
}
return net.ParseIP(host), p
}
func main() {
if runtime.GOOS != "linux" {
log.Fatal("only for Linux!")
}
flag.Parse()
remote4, port4 := parseRemote(*optV4remote)
fmt.Printf("remote v4 = %s:%d\n", remote4, port4)
remote6, port6 := parseRemote(*optV6remote)
fmt.Printf("remote v6 = %s:%d\n", remote6, port6)
v4conn, localport4, err := listenNet("udp4", 0)
if err != nil {
log.Fatal(err)
}
if port4 == 0 {
port4 = localport4
}
gsoV4, _ := supportsUDPOffload(v4conn)
v6conn, localport6, err := listenNet("udp6", 0)
if err != nil {
log.Fatal(err)
}
if port6 == 0 {
port6 = localport6
}
gsoV6, _ := supportsUDPOffload(v6conn)
fmt.Println("Kernel detection:")
fmt.Printf(" IPv4 GSO: %t\n", gsoV4)
fmt.Printf(" IPv6 GSO: %t\n", gsoV6)
size := 4000
if gsoV4 {
doTests("IPv4", size, v4conn, remote4, port4, ipv4.NewPacketConn(v4conn))
}
if gsoV6 {
doTests("IPv6", size, v6conn, remote6, port6, ipv6.NewPacketConn(v6conn))
}
}
func doTests(version string, size int, conn *net.UDPConn, addr net.IP, port int, br batchWriter) {
fmt.Println(version, "with GSO:")
fmt.Printf(" Write:")
testWrite(size, conn, addr, port, true, nil)
fmt.Printf(" Write with batch:")
testWrite(size, conn, addr, port, true, br)
fmt.Println(version, "without GSO:")
fmt.Printf(" Write:")
testWrite(size, conn, addr, port, false, nil)
fmt.Printf(" Write with batch:")
testWrite(size, conn, addr, port, false, br)
}
func errShouldDisableUDPGSO(err error) bool {
var serr *os.SyscallError
if errors.As(err, &serr) {
// EIO is returned by udp_send_skb() if the device driver does not have
// tx checksumming enabled, which is a hard requirement of UDP_SEGMENT.
// See:
// https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/man7/udp.7?id=806eabd74910447f21005160e90957bde4db0183#n228
// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/udp.c?h=v6.2&id=c9c3395d5e3dcc6daee66c6908354d47bf98cb0c#n942
return serr.Err == unix.EIO
}
return false
}
type batchWriter interface {
WriteBatch([]ipv6.Message, int) (int, error)
}
func testWrite(size int, conn *net.UDPConn, addr net.IP, port int, gso bool, br batchWriter) {
buff := make([]byte, size)
buffs := make([][]byte, 1)
buffs[0] = buff
// remote = self
remote := &net.UDPAddr{
IP: addr,
Port: port,
}
oob := make([]byte, 0, unix.CmsgSpace(sizeOfGSOData))
if gso {
setGSOSize(&oob, 1200) // should be maxmtu for remote ?
}
msg := []ipv6.Message{{
Buffers: buffs,
OOB: oob,
Addr: remote,
}, {
Buffers: buffs,
OOB: oob,
Addr: remote,
}}
var n, noob int
var err error
if br != nil {
nn, err2 := br.WriteBatch(msg, 0)
if nn != len(msg) {
fmt.Println("unexpected number of msg sent")
}
noob = msg[0].NN
n = msg[0].N
err = err2
} else {
n, noob, err = conn.WriteMsgUDP(buff, oob, remote)
}
if gso && err != nil && errShouldDisableUDPGSO(err) {
fmt.Println(" gso issue detected")
}
errmsg := "nil"
if err != nil {
errmsg = err.Error()
}
fmt.Printf(" n,nb,err = %d,%d,%s\n", n, noob, errmsg)
}