Repository navigation
Expand file tree
/
Copy pathstack_go_io_linux.go
More file actions
432 lines (403 loc) · 12 KB
/
Copy pathstack_go_io_linux.go
File metadata and controls
432 lines (403 loc) · 12 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package tun
import (
"net/netip"
"os"
"runtime"
"sync"
"sync/atomic"
"time"
"unsafe"
"github.com/sagernet/sing-tun/gtcpip/header"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
N "github.com/sagernet/sing/common/network"
"golang.org/x/sys/unix"
)
const goEngineTransmits = false
var goEmptyVirtioHeader [virtioNetHdrLen]byte
type goLinuxDevice struct {
stack *Go
vnetHeader bool
offloadProbePending atomic.Bool
transmitOffload atomic.Bool
udpTransmitDisabled atomic.Bool
droppedEngineFrames goDropCounter
droppedDataFrames goDropCounter
closing atomic.Bool
}
type goLinuxIO struct {
device *goLinuxDevice
tun *NativeTun
tunFd int
ownsFd bool
goPoller
fdAccess sync.RWMutex
receiveSlots goReadSlots
}
func newGoPlatformQueues(stack *Go) ([]goPlatformIO, error) {
nativeTun, isNative := stack.tun.(*NativeTun)
if !isNative {
return nil, E.New("go: unsupported TUN implementation")
}
err := nativeTun.detachRuntimePoller()
if err != nil {
return nil, E.Cause(err, "go: detach tun from runtime poller")
}
device := &goLinuxDevice{stack: stack}
vnetHeader, err := nativeTun.vnetHeaderEnabled()
if err != nil {
stack.logger.Warn(E.Cause(err, "go: check IFF_VNET_HDR"))
vnetHeader = false
}
device.vnetHeader = vnetHeader
if vnetHeader {
offloadErr := setUDPOffload(nativeTun.rawFileDescriptor())
if offloadErr != nil {
offloadErr = setTCPOffload(nativeTun.rawFileDescriptor())
}
if offloadErr != nil {
stack.logger.Warn(E.Cause(offloadErr, "go: set TSO offload"))
} else {
device.offloadProbePending.Store(true)
}
}
queueCount := 1
if nativeTun.multiQueue {
queueCount = runtime.GOMAXPROCS(0)
} else if nativeTun.options.MultiQueue {
stack.logger.Warn("go: multi-queue requested but the tun device is single-queue")
}
queues := make([]goPlatformIO, queueCount)
queues[0] = &goLinuxIO{device: device, tun: nativeTun, tunFd: nativeTun.rawFileDescriptor()}
for index := 1; index < len(queues); index++ {
queues[index] = &goLinuxIO{device: device, tun: nativeTun, tunFd: -1}
}
return queues, nil
}
func (o *goLinuxIO) start() error {
if o.tunFd < 0 {
queueFd, err := o.tun.openQueue()
if err != nil {
o.device.stack.logger.Warn(E.Cause(err, "go: attach tun queue"))
return errGoQueueUnavailable
}
o.tunFd = queueFd
o.ownsFd = true
}
err := o.goPoller.open(o.tunFd)
if err != nil && o.ownsFd {
err = E.Errors(err, unix.Close(o.tunFd))
}
return err
}
func (o *goLinuxIO) probeTransmitOffload() error {
probeAddr := netip.AddrFrom4([4]byte{127, 0, 0, 1})
fingerprint := []byte("sing-tun-probe-tun-gro")
segmentSize := len(fingerprint)
const ipHeaderLength = 20
const tcpHeaderLength = 20
totalLength := ipHeaderLength + tcpHeaderLength + 2*segmentSize
packet := make([]byte, totalLength)
probeIPv4 := header.IPv4(packet)
probeIPv4.Encode(&header.IPv4Fields{
SrcAddr: probeAddr,
DstAddr: probeAddr,
Protocol: unix.IPPROTO_TCP,
TTL: 0,
TotalLength: uint16(totalLength),
})
probeTCP := header.TCP(packet[ipHeaderLength:])
probeTCP.Encode(&header.TCPFields{
SrcPort: 0,
DstPort: 0,
SeqNum: 1,
AckNum: 1,
DataOffset: tcpHeaderLength,
Flags: header.TCPFlagAck,
WindowSize: 3000,
})
copy(packet[ipHeaderLength+tcpHeaderLength:], fingerprint)
copy(packet[ipHeaderLength+tcpHeaderLength+segmentSize:], fingerprint)
probeIPv4.SetChecksum(^probeIPv4.CalculateChecksum())
pseudoSum := header.PseudoHeaderChecksum(unix.IPPROTO_TCP, probeIPv4.SourceAddressSlice(), probeIPv4.DestinationAddressSlice(), uint16(tcpHeaderLength+2*segmentSize))
probeTCP.SetChecksum(pseudoSum)
errno := o.transmitFrame([][]byte{packet}, ForwardFrameMeta{
needsChecksum: true,
checksumStart: ipHeaderLength,
checksumOffset: header.TCPChecksumOffset,
gsoType: unix.VIRTIO_NET_HDR_GSO_TCPV4,
gsoSize: uint16(segmentSize),
})
if errno != 0 {
return E.Cause(errno, "go: tso probe write")
}
return nil
}
func (o *goLinuxIO) wait(timeout time.Duration, events []goSocketEvent) (bool, int, error) {
deviceReady, socketCount, err := o.goPoller.wait(timeout, events)
return deviceReady&goInterestRead != 0, socketCount, err
}
func (o *goLinuxIO) readBurst(frames []goFrame, options N.ReadWaitOptions) (int, bool, error) {
// Linux hands pre-segmentation TSO aggregates to the TUN fd even with IFF_VNET_HDR off
// (observed on 6.x kernels).
o.receiveSlots.configure(goPacketBatchSize, virtioNetHdrLen+gsoMaxSize+options.FrontHeadroom+options.RearHeadroom)
limit := min(len(frames), goPacketBatchSize)
o.receiveSlots.wake(limit)
count := 0
for count < limit {
buffer := o.receiveSlots.slot(count)
buffer.Reset()
buffer.Resize(options.FrontHeadroom, 0)
buffer.Reserve(options.RearHeadroom)
scratch := buffer.FreeBytes()
//nolint:staticcheck
n, _, errno := unix.RawSyscall(unix.SYS_READ, uintptr(o.tunFd), uintptr(unsafe.Pointer(&scratch[0])), uintptr(len(scratch)))
if errno != 0 {
if errno == unix.EAGAIN {
return count, true, nil
}
if errno == unix.EINTR {
continue
}
return 0, false, E.Cause(errno, "go: read tun")
}
if n == 0 {
return 0, false, os.ErrClosed
}
// The kernel rejects writes with EIO while the device is not up (tun_get_user).
if o.device.offloadProbePending.CompareAndSwap(true, false) {
probeErr := o.probeTransmitOffload()
if probeErr != nil {
o.device.stack.logger.Warn(E.Cause(probeErr, "go: TSO write probe"))
} else {
o.device.transmitOffload.Store(true)
}
}
packet := scratch[:n]
if !o.device.vnetHeader {
buffer.Truncate(int(n))
options.PostReturn(buffer)
frames[count] = goFrame{buffer: buffer}
count++
continue
}
payload, virtioOptions, parseErr := parseVirtioRead(packet)
if parseErr != nil {
continue
}
meta := goFrameMeta(virtioOptions)
buffer.Resize(options.FrontHeadroom+len(packet)-len(payload), len(payload))
options.PostReturn(buffer)
frames[count] = goFrame{buffer: buffer, meta: meta}
count++
}
return count, false, nil
}
func (o *goLinuxIO) releaseReadBuffers() {
o.receiveSlots.sleep()
}
func (o *goLinuxIO) writeFrame(frame [][]byte, meta ForwardFrameMeta) error {
for {
errno := o.transmitFrame(frame, meta)
switch errno {
case 0:
return nil
case unix.EINTR:
if o.device.closing.Load() {
return os.ErrClosed
}
case unix.EAGAIN, unix.ENOBUFS, unix.ENOMEM:
o.device.droppedEngineFrames.record(o.device.stack.logger, "engine frames")
return errGoFrameDropped
default:
if errno == unix.EBADF && o.device.closing.Load() {
return os.ErrClosed
}
return E.Cause(errno, "go: write tun")
}
}
}
func (o *goLinuxIO) writePacket(packet []byte, meta ForwardFrameMeta) error {
frame := [1][]byte{packet}
return o.writeFrame(frame[:], meta)
}
func (o *goLinuxIO) writeData(frame [][]byte, meta ForwardFrameMeta, owner *GoConn, segmentEnd uint64) error {
delay := goTransmitBackoffMin
waited := time.Duration(0)
for {
errno := o.transmitFrame(frame, meta)
switch errno {
case 0:
return nil
case unix.EINTR:
if o.device.closing.Load() {
return os.ErrClosed
}
case unix.ENOBUFS, unix.ENOMEM:
// Before Linux 6.6, tun_alloc_skb places the whole frame in the skb head unless the
// virtio header carries a header length, so a large frame needs a high-order
// allocation, which fails with ENOBUFS under memory fragmentation.
o.device.droppedDataFrames.record(o.device.stack.logger, "data frames")
return errGoFrameDropped
case unix.EAGAIN:
// tun chardev writes complete into netif_rx, which drops on backlog overflow
// instead of reporting EAGAIN, and tun_chr_poll reports the fd always writable.
if o.device.closing.Load() {
return os.ErrClosed
}
if waited >= goTransmitBackoffBudget {
o.device.droppedDataFrames.record(o.device.stack.logger, "data frames")
return errGoFrameDropped
}
time.Sleep(delay)
if o.device.closing.Load() {
return os.ErrClosed
}
waited += delay
delay = min(2*delay, goTransmitBackoffMax)
default:
if errno == unix.EBADF && o.device.closing.Load() {
return os.ErrClosed
}
return errno
}
}
}
func (o *goLinuxIO) transmitFrame(frame [][]byte, meta ForwardFrameMeta) unix.Errno {
o.fdAccess.RLock()
defer o.fdAccess.RUnlock()
if o.device.closing.Load() {
return unix.EBADF
}
var headerStorage [virtioNetHdrLen]byte
var iovecStorage [goPacketBatchSize + 2]unix.Iovec
iovecs := iovecStorage[:]
iovecCapacity := len(frame)
if o.device.vnetHeader {
iovecCapacity++
}
if iovecCapacity > len(iovecs) {
iovecs = make([]unix.Iovec, iovecCapacity)
}
iovecCount := 0
if o.device.vnetHeader {
prefix := goEmptyVirtioHeader[:]
if meta != (ForwardFrameMeta{}) {
virtioHeader := virtioNetHdr{gsoType: meta.gsoType, gsoSize: meta.gsoSize, hdrLen: meta.segmentHeaderLength(frame[0])}
if meta.needsChecksum {
virtioHeader.flags = unix.VIRTIO_NET_HDR_F_NEEDS_CSUM
virtioHeader.csumStart = meta.checksumStart
virtioHeader.csumOffset = meta.checksumOffset
}
common.Must(virtioHeader.encode(headerStorage[:]))
prefix = headerStorage[:]
}
vector := unix.Iovec{Base: &prefix[0]}
vector.SetLen(len(prefix))
iovecs[iovecCount] = vector
iovecCount++
}
for _, segment := range frame {
if len(segment) == 0 {
continue
}
vector := unix.Iovec{Base: &segment[0]}
vector.SetLen(len(segment))
iovecs[iovecCount] = vector
iovecCount++
}
//nolint:staticcheck
_, _, errno := unix.RawSyscall(unix.SYS_WRITEV, uintptr(o.tunFd), uintptr(unsafe.Pointer(&iovecs[0])), uintptr(iovecCount))
return errno
}
func (o *goLinuxIO) mtu() int {
return o.device.stack.mtu
}
func (o *goLinuxIO) supportsSockets() bool {
return true
}
func (o *goLinuxIO) transmitPrefix() int {
if o.device.vnetHeader {
return virtioNetHdrLen
}
return 0
}
func (o *goLinuxIO) transmitChecksumOffload() bool {
return o.device.vnetHeader
}
func (o *goLinuxIO) transmitSegmentOffload() bool {
return o.device.transmitOffload.Load()
}
func (o *goLinuxIO) armTransmitWritable(conn *GoConn) (*goBlockedWriters, error) {
return nil, nil
}
func (o *goLinuxIO) takeTransmitWritable(writable []*goBlockedWriters) []*goBlockedWriters {
return writable
}
func (o *goLinuxIO) close() error {
o.device.closing.Store(true)
o.fdAccess.Lock()
defer o.fdAccess.Unlock()
o.receiveSlots.release()
err := o.goPoller.close()
if o.ownsFd {
err = E.Errors(err, unix.Close(o.tunFd))
}
return err
}
func (o *goLinuxIO) writeDatagram(packet []byte, meta ForwardFrameMeta) error {
return o.writePacket(packet, meta)
}
func (o *goLinuxIO) transmitBacklogBelowBatch(conn *GoConn) bool {
return true
}
func (o *goLinuxIO) flush() {
}
func (o *goLinuxIO) writePacketBatch(frames []goUDPFrame) error {
if o.device.closing.Load() {
return os.ErrClosed
}
var writeError error
var segments [goPacketBatchSize + 1][]byte
for index := 0; index < len(frames); {
frame := &frames[index]
end := index + 1
payloadLength := len(frame.payload)
if o.device.vnetHeader && !o.device.udpTransmitDisabled.Load() {
end, payloadLength = goUDPSegmentRun(frames, index)
}
packetHeader := frame.header
meta := frame.meta
if end-index > 1 {
meta = frame.coalesce(payloadLength)
}
segments[0] = frame.header[:frame.length]
for packetIndex := index; packetIndex < end; packetIndex++ {
segments[packetIndex-index+1] = frames[packetIndex].payload
}
errno := o.transmitFrame(segments[:end-index+1], meta)
if errno == unix.EINTR {
frame.header = packetHeader
continue
}
if errno == unix.EBADF && o.device.closing.Load() {
return os.ErrClosed
}
if errno != 0 && end-index > 1 {
switch errno {
case unix.EINVAL, unix.EIO, unix.EOPNOTSUPP:
o.device.udpTransmitDisabled.Store(true)
frame.header = packetHeader
continue
}
}
if errno == unix.EAGAIN || errno == unix.ENOBUFS || errno == unix.ENOMEM {
o.device.droppedEngineFrames.record(o.device.stack.logger, "UDP frames")
writeError = E.Errors(writeError, errGoFrameDropped)
} else if errno != 0 {
writeError = E.Errors(writeError, E.Cause(errno, "go: write UDP batch"))
}
index = end
}
return writeError
}