-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathstack_go_io_darwin.go
More file actions
837 lines (791 loc) · 25.3 KB
/
Copy pathstack_go_io_darwin.go
File metadata and controls
837 lines (791 loc) · 25.3 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
package tun
import (
"net"
"os"
"sync"
"sync/atomic"
"time"
"unsafe"
"github.com/sagernet/sing-tun/gtcpip/header"
rawfile "github.com/sagernet/sing-tun/internal/rawfile_darwin"
"github.com/sagernet/sing/common/buf"
E "github.com/sagernet/sing/common/exceptions"
N "github.com/sagernet/sing/common/network"
"golang.org/x/sys/unix"
)
const (
goWakeIdent = 0
goEngineInlineTransmit = true
goDarwinTransmitBatch = 64
goDarwinBatchHeader = 128
goDarwinBatchIovecs = 8
// sendit_x builds every packet of the batch before sending any of it, and mbuf_allocpacket
// takes a 16 KB cluster for a packet above MBIGCLBYTES (4096 bytes, prefix included) from a
// pool of a few hundred; a batch of large frames exhausts it and fails with ENOBUFS, so only
// frames that fit a 4 KB cluster are batched and larger ones keep the per-frame writev.
goDarwinBatchFrameLimit = 4096 - PacketOffset
// utun_pkt_input in netif mode returns ENOSPC once utun_input_chain_count exceeds
// net.utun.max_pending_input (512) and does not free the mbuf, and neither does ctl_send:
// every ENOSPC leaks one mbuf cluster until reboot. The chain only drains when the host side
// syncs the rx ring, which kern_channel_notify triggers per successful input, so the gate keeps
// the estimated chain depth well under the limit and, when nothing moves, submits a small probe
// to trigger a sync rather than wait forever.
goDarwinGateLimit = 512
goDarwinGateHighWater = 384
goDarwinGateProbe = 8
goDarwinGatePoll = 100 * time.Microsecond
goDarwinGateSample = time.Millisecond
goDarwinGateProbeAfter = 16
goDarwinGateRIBSize = 4096
goDarwinGateRIBSizeMax = 1 << 20
sysprotoControl = 2
utunOptionEnableNetif = 20
)
type goDarwinIO struct {
stack *Go
tunFd int
kqueueFd int
transmitAccess *sync.Mutex
receiveBuffers []*buf.Buffer
readWaitOptions N.ReadWaitOptions
receiveIovecs []unix.Iovec
messageHeaders []rawfile.MsgHdrX
events [goSocketEventBatch + 3]unix.Kevent_t
changes [2]unix.Kevent_t
wakeEvent [1]unix.Kevent_t
transmitWritableEvent [1]unix.Kevent_t
transmitWritable atomic.Bool
receiveBatch int
transmitIovecs []unix.Iovec
socketTokens []uint32
socketInterests []uint8
droppedEngineFrames goDropCounter
droppedBatchFrames goDropCounter
leakedClusters goDropCounter
closing atomic.Bool
batchHeaders []byte
batchIovecs []unix.Iovec
batchMessages []rawfile.MsgHdrX
batchSpill []byte
batchSpilled []bool
batchStart int
batchCount int
packetIovecs [goPacketBatchSize * 3]unix.Iovec
packetMessages [goPacketBatchSize]rawfile.MsgHdrX
netif bool
interfaceIndex int
epoch time.Time
gateRIB []byte
gateSubmitted int64
gateSampledAt int64
gateSampledTime atomic.Int64
gateInflightSample int64
gateConsumedBase int64
gateBlocked atomic.Bool
gateIdlePolls int
gateLastConsumed int64
gateProbeEnd int64
}
func newGoPlatformQueues(stack *Go) ([]goPlatformIO, error) {
return []goPlatformIO{&goDarwinIO{stack: stack, epoch: time.Now()}}, nil
}
func (o *goDarwinIO) start() error {
nativeTun, isNative := o.stack.tun.(*NativeTun)
if !isNative {
return E.New("go: unsupported TUN implementation")
}
err := nativeTun.detachRuntimePoller()
if err != nil {
return E.Cause(err, "go: detach tun from runtime poller")
}
o.tunFd = nativeTun.rawFileDescriptor()
o.transmitAccess = nativeTun.transmitAccess()
err = nativeTun.enableMaxPendingPackets()
if err != nil {
return E.Cause(err, "go: set UTUN_OPT_MAX_PENDING_PACKETS")
}
kqueueFd, err := unix.Kqueue()
if err != nil {
return E.Cause(err, "go: create kqueue")
}
registrations := []unix.Kevent_t{
{Ident: uint64(o.tunFd), Filter: unix.EVFILT_READ, Flags: unix.EV_ADD | unix.EV_ENABLE},
{Ident: goWakeIdent, Filter: unix.EVFILT_USER, Flags: unix.EV_ADD | unix.EV_ENABLE | unix.EV_CLEAR},
}
_, err = unix.Kevent(kqueueFd, registrations, nil, nil)
if err != nil {
unix.Close(kqueueFd)
return E.Cause(err, "go: register kqueue events")
}
o.kqueueFd = kqueueFd
o.wakeEvent[0] = unix.Kevent_t{Ident: goWakeIdent, Filter: unix.EVFILT_USER, Fflags: unix.NOTE_TRIGGER}
o.transmitWritableEvent[0] = unix.Kevent_t{Ident: uint64(o.tunFd), Filter: unix.EVFILT_WRITE, Flags: unix.EV_ADD | unix.EV_ENABLE | unix.EV_ONESHOT}
o.receiveIovecs = make([]unix.Iovec, goReadBatch)
o.messageHeaders = make([]rawfile.MsgHdrX, goReadBatch)
o.transmitIovecs = make([]unix.Iovec, 0, 8)
o.receiveBatch = goReceiveBatchMin
o.batchHeaders = make([]byte, goDarwinTransmitBatch*goDarwinBatchHeader)
o.batchIovecs = make([]unix.Iovec, goDarwinTransmitBatch*goDarwinBatchIovecs)
o.batchMessages = make([]rawfile.MsgHdrX, goDarwinTransmitBatch)
o.batchSpilled = make([]bool, goDarwinTransmitBatch)
netif, err := unix.GetsockoptInt(o.tunFd, sysprotoControl, utunOptionEnableNetif)
if err == nil && netif != 0 {
name, nameErr := nativeTun.Name()
if nameErr != nil {
unix.Close(kqueueFd)
return E.Cause(nameErr, "go: utun name")
}
netInterface, lookupErr := net.InterfaceByName(name)
if lookupErr != nil {
unix.Close(kqueueFd)
return E.Cause(lookupErr, "go: utun interface")
}
o.netif = true
o.interfaceIndex = netInterface.Index
consumed, sampleErr := o.readInterfacePackets()
if sampleErr != nil {
unix.Close(kqueueFd)
return E.Cause(sampleErr, "go: utun statistics")
}
o.gateConsumedBase = consumed
}
return nil
}
// The NET_RT_IFLIST2 route sysctl takes the interface index as its last name component and
// sysctl_iflist2 skips every other interface, so one interface costs a single small transfer.
func (o *goDarwinIO) readInterfacePackets() (int64, error) {
mib := [6]int32{unix.CTL_NET, unix.AF_ROUTE, 0, unix.AF_UNSPEC, unix.NET_RT_IFLIST2, int32(o.interfaceIndex)}
if o.gateRIB == nil {
o.gateRIB = make([]byte, goDarwinGateRIBSize)
}
var rib []byte
for {
length := uintptr(len(o.gateRIB))
_, _, errno := unix.Syscall6(unix.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), uintptr(len(mib)), uintptr(unsafe.Pointer(&o.gateRIB[0])), uintptr(unsafe.Pointer(&length)), 0, 0)
if errno == unix.ENOMEM && len(o.gateRIB) < goDarwinGateRIBSizeMax {
o.gateRIB = make([]byte, 2*len(o.gateRIB))
continue
}
if errno != 0 {
return 0, errno
}
rib = o.gateRIB[:length]
break
}
for len(rib) >= int(unsafe.Sizeof(unix.IfMsghdr2{})) {
message := (*unix.IfMsghdr2)(unsafe.Pointer(&rib[0]))
if message.Type == unix.RTM_IFINFO2 && int(message.Index) == o.interfaceIndex {
return int64(message.Data.Ipackets), nil
}
if message.Msglen == 0 {
break
}
rib = rib[message.Msglen:]
}
return 0, unix.ENOENT
}
func (o *goDarwinIO) wait(timeout time.Duration, events []goSocketEvent) (bool, int, error) {
if o.gateBlocked.Load() {
poll := goDarwinGatePoll
if o.netif {
poll = max(goDarwinGateSample-(time.Since(o.epoch)-time.Duration(o.gateSampledTime.Load())), 0)
}
if timeout < 0 || timeout > poll {
timeout = poll
}
}
var timeoutSpec *unix.Timespec
if timeout >= 0 {
spec := unix.NsecToTimespec(timeout.Nanoseconds())
timeoutSpec = &spec
}
var eventCount int
var err error
if timeout == 0 {
//nolint:staticcheck
count, _, errno := unix.RawSyscall6(unix.SYS_KEVENT, uintptr(o.kqueueFd), 0, 0, uintptr(unsafe.Pointer(&o.events[0])), uintptr(len(o.events)), uintptr(unsafe.Pointer(timeoutSpec)))
eventCount = int(count)
if errno != 0 {
err = errno
}
} else {
eventCount, err = unix.Kevent(o.kqueueFd, nil, o.events[:], timeoutSpec)
}
if o.gateBlocked.Load() {
o.gatePoll()
}
if err != nil {
if err == unix.EINTR {
return false, 0, nil
}
return false, 0, E.Cause(err, "go: kevent wait")
}
tunReadable := false
socketCount := 0
for index := range eventCount {
event := &o.events[index]
if event.Filter == unix.EVFILT_USER {
continue
}
if int(event.Ident) == o.tunFd {
if event.Filter == unix.EVFILT_WRITE {
o.transmitWritable.Store(true)
continue
}
if event.Flags&unix.EV_ERROR != 0 && event.Data != 0 {
return false, 0, unix.Errno(event.Data)
}
if event.Flags&unix.EV_EOF != 0 {
return false, 0, unix.ECONNRESET
}
tunReadable = true
continue
}
if int(event.Ident) >= len(o.socketTokens) || socketCount == len(events) {
continue
}
events[socketCount] = goSocketEvent{
token: o.socketTokens[event.Ident],
readable: event.Filter == unix.EVFILT_READ,
writable: event.Filter == unix.EVFILT_WRITE,
}
socketCount++
}
return tunReadable, socketCount, nil
}
func (o *goDarwinIO) registerSocket(socket *goSocket, token uint32, interest uint8) error {
if socket.fd >= len(o.socketTokens) {
grown := max(socket.fd+1, 2*len(o.socketTokens), 64)
o.socketTokens = append(o.socketTokens, make([]uint32, grown-len(o.socketTokens))...)
o.socketInterests = append(o.socketInterests, make([]uint8, grown-len(o.socketInterests))...)
}
o.socketTokens[socket.fd] = token
o.socketInterests[socket.fd] = 0
return o.updateSocket(socket, interest)
}
func (o *goDarwinIO) updateSocket(socket *goSocket, interest uint8) error {
current := o.socketInterests[socket.fd]
if current == interest {
return nil
}
changes := o.changes[:0]
if current&goInterestRead != interest&goInterestRead {
changes = append(changes, unix.Kevent_t{Ident: uint64(socket.fd), Filter: unix.EVFILT_READ, Flags: goKqueueFlags(interest&goInterestRead != 0)})
}
if current&goInterestWrite != interest&goInterestWrite {
changes = append(changes, unix.Kevent_t{Ident: uint64(socket.fd), Filter: unix.EVFILT_WRITE, Flags: goKqueueFlags(interest&goInterestWrite != 0)})
}
o.socketInterests[socket.fd] = interest
_, err := unix.Kevent(o.kqueueFd, changes, nil, nil)
if err != nil {
return E.Cause(err, "go: update socket interest")
}
return nil
}
func goKqueueFlags(enable bool) uint16 {
if enable {
return unix.EV_ADD | unix.EV_ENABLE
}
return unix.EV_DELETE
}
func (o *goDarwinIO) unregisterSocket(socket *goSocket) {
o.updateSocket(socket, 0)
o.socketTokens[socket.fd] = 0
}
func (o *goDarwinIO) readBurst(frames []goFrame, options N.ReadWaitOptions) (int, bool, error) {
if o.receiveBuffers == nil || o.readWaitOptions != options {
o.releaseReadBuffers()
if o.receiveBuffers == nil {
o.receiveBuffers = make([]*buf.Buffer, goReadBatch)
}
o.readWaitOptions = options
}
// recvmsg_x walks every submitted msghdr slot, whether or not a packet is pending for it.
count := min(o.receiveBatch, len(frames))
for index := range count {
buffer := o.receiveBuffers[index]
if buffer == nil {
buffer = options.NewBufferSize(o.stack.mtu + PacketOffset)
o.receiveBuffers[index] = buffer
}
buffer.Reset()
buffer.Resize(options.FrontHeadroom, 0)
buffer.Reserve(options.RearHeadroom)
o.receiveIovecs[index] = rawfile.IovecFromBytes(buffer.FreeBytes())
// Cannot clear only the length field. Older versions of the darwin kernel will check whether other data is empty.
// https://github.com/Darm64/XNU/blob/xnu-2782.40.9/bsd/kern/uipc_syscalls.c#L2026-L2048
o.messageHeaders[index] = rawfile.MsgHdrX{}
o.messageHeaders[index].Msg.Iov = &o.receiveIovecs[index]
o.messageHeaders[index].Msg.Iovlen = 1
}
received, errno := receiveMessageBatch(o.tunFd, o.messageHeaders[:count])
if errno != 0 {
if errno == unix.EWOULDBLOCK || errno == unix.EINTR {
return 0, true, nil
}
return 0, false, E.Cause(errno, "go: recvmsg_x")
}
if received >= count {
o.receiveBatch = min(count*2, len(o.messageHeaders))
} else if received*2 < count {
o.receiveBatch = max(count/2, goReceiveBatchMin)
}
frameCount := 0
for index := range received {
dataLen := int(o.messageHeaders[index].DataLen)
if dataLen <= PacketOffset || dataLen > o.receiveBuffers[index].FreeLen() || o.messageHeaders[index].Msg.Flags&unix.MSG_TRUNC != 0 {
continue
}
buffer := o.receiveBuffers[index]
buffer.Reset()
buffer.Resize(options.FrontHeadroom+PacketOffset, dataLen-PacketOffset)
frames[frameCount] = goFrame{buffer: buffer}
frameCount++
}
return frameCount, received == 0, nil
}
func (o *goDarwinIO) releaseReadBuffers() {
clear(o.receiveIovecs)
clear(o.messageHeaders)
buf.ReleaseMulti(o.receiveBuffers)
clear(o.receiveBuffers)
o.transmitAccess.Lock()
clear(o.transmitIovecs)
if o.batchCount == 0 {
clear(o.batchIovecs)
clear(o.batchMessages)
o.batchSpill = nil
}
o.transmitAccess.Unlock()
}
func receiveMessageBatch(fd int, headers []rawfile.MsgHdrX) (int, unix.Errno) {
//nolint:staticcheck
n, _, errno := unix.RawSyscall6(unix.SYS_RECVMSG_X, uintptr(fd), uintptr(unsafe.Pointer(&headers[0])), uintptr(len(headers)), unix.MSG_DONTWAIT, 0, 0)
return int(n), errno
}
func (o *goDarwinIO) writeFrame(frame [][]byte, meta ForwardFrameMeta) error {
o.transmitAccess.Lock()
err := o.writePacketLocked(frame)
o.transmitAccess.Unlock()
if err == errGoTransmitBlocked {
o.droppedEngineFrames.record(o.stack.logger, "engine frames")
return errGoFrameDropped
}
return err
}
func (o *goDarwinIO) writePacket(packet []byte, meta ForwardFrameMeta) error {
frame := [1][]byte{packet}
return o.writeFrame(frame[:], meta)
}
func (o *goDarwinIO) writeData(frame [][]byte, meta ForwardFrameMeta) error {
o.transmitAccess.Lock()
defer o.transmitAccess.Unlock()
frameLength := 0
for _, segment := range frame {
frameLength += len(segment)
}
if frameLength > goDarwinBatchFrameLimit {
return o.writeUnbatchedLocked(frame)
}
if o.batchCount == goDarwinTransmitBatch {
o.flushLocked()
if o.batchCount == goDarwinTransmitBatch {
if o.batchStart == 0 {
return errGoTransmitBlocked
}
o.compactBatch()
}
}
slot := o.batchCount
iovecs := o.batchIovecs[slot*goDarwinBatchIovecs : slot*goDarwinBatchIovecs : (slot+1)*goDarwinBatchIovecs]
if header.IPVersion(frame[0]) == header.IPv4Version {
iovecs = append(iovecs, packetHeaderVec4)
} else {
iovecs = append(iovecs, packetHeaderVec6)
}
for index, segment := range frame {
if len(segment) == 0 {
continue
}
if index == 0 && len(frame) > 1 {
if len(segment) > goDarwinBatchHeader {
return o.writeUnbatchedLocked(frame)
}
headerCopy := o.batchHeaders[slot*goDarwinBatchHeader : slot*goDarwinBatchHeader+len(segment)]
copy(headerCopy, segment)
segment = headerCopy
}
if len(iovecs) == cap(iovecs) {
return o.writeUnbatchedLocked(frame)
}
iovecs = append(iovecs, rawfile.IovecFromBytes(segment))
}
o.batchMessages[slot] = rawfile.MsgHdrX{}
o.batchMessages[slot].Msg.Iov = &iovecs[0]
o.batchMessages[slot].Msg.Iovlen = int32(len(iovecs))
o.batchSpilled[slot] = false
o.batchCount++
return nil
}
func (o *goDarwinIO) writeUnbatchedLocked(frame [][]byte) error {
o.flushLocked()
if o.batchCount > o.batchStart {
return errGoTransmitBlocked
}
return o.writePacketLocked(frame)
}
func (o *goDarwinIO) flush() {
o.transmitAccess.Lock()
o.flushLocked()
o.transmitAccess.Unlock()
}
func (o *goDarwinIO) gateEstimate() int64 {
return o.gateInflightSample + (o.gateSubmitted - o.gateSampledAt)
}
func (o *goDarwinIO) gateSample() (int64, bool, bool) {
if !o.netif {
return o.gateEstimate(), false, false
}
now := int64(time.Since(o.epoch))
if now-o.gateSampledTime.Load() < int64(goDarwinGateSample) {
return o.gateEstimate(), false, false
}
o.gateSampledTime.Store(now)
consumed, err := o.readInterfacePackets()
if err != nil {
return o.gateEstimate(), false, true
}
progressed := consumed != o.gateLastConsumed
if progressed {
o.gateProbeEnd = 0
}
o.gateLastConsumed = consumed
o.gateInflightSample = max(o.gateSubmitted-(consumed-o.gateConsumedBase), 0)
o.gateSampledAt = o.gateSubmitted
return o.gateInflightSample, progressed, true
}
func (o *goDarwinIO) gateRoom(pending int) int {
if !o.netif {
return pending
}
estimate := o.gateEstimate()
if estimate+int64(pending) > goDarwinGateHighWater {
estimate, _, _ = o.gateSample()
}
room := goDarwinGateHighWater - estimate
room = max(room, min(o.gateProbeEnd-o.gateSubmitted, goDarwinGateLimit-1-estimate))
return int(max(min(room, int64(pending)), 0))
}
// flushLocked submits the queued frames. sendmsg_x reports no count on failure. ENOBUFS comes from
// sendit_x failing to build the packets before anything is sent, so the batch is kept and retried.
// For the other errors ctl_send_list frees whatever follows the failing packet, so the call
// consumed every frame handed to it: the ones before the failure were delivered, the failing one
// is charged to the kernel (an ENOSPC leaks it), the rest are dropped and left to retransmission.
func (o *goDarwinIO) flushLocked() {
pending := o.batchCount - o.batchStart
if pending == 0 {
o.gateReleaseLocked()
return
}
room := o.gateRoom(pending)
sentFrom := o.batchStart
for room > 0 {
messages := o.batchMessages[o.batchStart : o.batchStart+room]
n, errno := rawfile.NonBlockingSendMMsg(o.tunFd, messages)
if errno == unix.EINTR {
continue
}
if errno == unix.ENOBUFS || errno == unix.EAGAIN {
break
}
if errno != 0 {
o.batchStart += room
o.gateSubmitted += int64(room)
o.recordWriteError(errno)
break
}
o.batchStart += n
o.gateSubmitted += int64(n)
room -= n
}
clear(o.batchIovecs[sentFrom*goDarwinBatchIovecs : o.batchStart*goDarwinBatchIovecs])
if o.batchStart == o.batchCount {
o.batchStart = 0
o.batchCount = 0
o.gateReleaseLocked()
return
}
o.gateBlockLocked()
o.spillHeld()
}
func (o *goDarwinIO) gateBlockLocked() {
if !o.gateBlocked.Swap(true) {
o.wake()
}
}
func (o *goDarwinIO) gateReleaseLocked() {
if !o.gateBlocked.Load() {
return
}
if o.gateRoom(1) == 0 {
return
}
o.gateBlocked.Store(false)
o.gateIdlePolls = 0
o.transmitWritable.Store(true)
o.wake()
}
// recordWriteError realigns the estimate after ENOSPC: the chain held more than the limit when
// the kernel refused, and the frames the kernel freed behind the refused one never reach the
// interface counter, so the counter baseline is moved to make the estimate read the limit now.
func (o *goDarwinIO) recordWriteError(errno unix.Errno) {
if errno == unix.ENOSPC {
o.leakedClusters.record(o.stack.logger, "frames into a full utun input queue, each leaking one kernel mbuf")
if !o.netif {
return
}
consumed, err := o.readInterfacePackets()
if err == nil {
o.gateLastConsumed = consumed
o.gateConsumedBase = consumed - (o.gateSubmitted - (goDarwinGateLimit + 1))
}
o.gateSampledTime.Store(int64(time.Since(o.epoch)))
o.gateInflightSample = goDarwinGateLimit + 1
o.gateSampledAt = o.gateSubmitted
o.gateProbeEnd = 0
return
}
o.droppedBatchFrames.record(o.stack.logger, "batched frames")
}
func (o *goDarwinIO) gatePoll() {
o.transmitAccess.Lock()
defer o.transmitAccess.Unlock()
if !o.gateBlocked.Load() {
return
}
_, progressed, sampled := o.gateSample()
if !sampled {
o.flushLocked()
return
}
if progressed {
o.gateIdlePolls = 0
} else {
o.gateIdlePolls++
}
if o.gateIdlePolls >= goDarwinGateProbeAfter {
o.gateProbeEnd = o.gateSubmitted + goDarwinGateProbe
o.gateIdlePolls = 0
}
o.flushLocked()
}
func (o *goDarwinIO) spillHeld() {
slotSize := o.stack.mtu + PacketOffset
if o.batchSpill == nil {
o.batchSpill = make([]byte, goDarwinTransmitBatch*slotSize)
}
for slot := o.batchStart; slot < o.batchCount; slot++ {
if o.batchSpilled[slot] {
continue
}
message := &o.batchMessages[slot]
iovecs := unsafe.Slice(message.Msg.Iov, int(message.Msg.Iovlen))
spill := o.batchSpill[slot*slotSize : (slot+1)*slotSize]
length := 0
for _, iovec := range iovecs[1:] {
length += copy(spill[length:], bytesFromIovec(iovec))
}
iovecs[1] = rawfile.IovecFromBytes(spill[:length])
clear(iovecs[2:])
message.Msg.Iovlen = 2
o.batchSpilled[slot] = true
}
}
func bytesFromIovec(iovec unix.Iovec) []byte {
return unsafe.Slice(iovec.Base, int(iovec.Len))
}
func (o *goDarwinIO) compactBatch() {
slotSize := o.stack.mtu + PacketOffset
for slot := o.batchStart; slot < o.batchCount; slot++ {
target := slot - o.batchStart
source := &o.batchMessages[slot]
sourceIovecs := unsafe.Slice(source.Msg.Iov, int(source.Msg.Iovlen))
targetIovecs := o.batchIovecs[target*goDarwinBatchIovecs : target*goDarwinBatchIovecs+len(sourceIovecs)]
copy(targetIovecs, sourceIovecs)
for index := range targetIovecs {
base := uintptr(unsafe.Pointer(targetIovecs[index].Base))
headerStart := uintptr(unsafe.Pointer(&o.batchHeaders[slot*goDarwinBatchHeader]))
if base >= headerStart && base < headerStart+goDarwinBatchHeader {
copy(o.batchHeaders[target*goDarwinBatchHeader:(target+1)*goDarwinBatchHeader], o.batchHeaders[slot*goDarwinBatchHeader:(slot+1)*goDarwinBatchHeader])
targetIovecs[index].Base = &o.batchHeaders[target*goDarwinBatchHeader+int(base-headerStart)]
continue
}
if o.batchSpilled[slot] {
spillStart := uintptr(unsafe.Pointer(&o.batchSpill[slot*slotSize]))
if base >= spillStart && base < spillStart+uintptr(slotSize) {
copy(o.batchSpill[target*slotSize:(target+1)*slotSize], o.batchSpill[slot*slotSize:(slot+1)*slotSize])
targetIovecs[index].Base = &o.batchSpill[target*slotSize+int(base-spillStart)]
}
}
}
o.batchMessages[target] = rawfile.MsgHdrX{}
o.batchMessages[target].Msg.Iov = &targetIovecs[0]
o.batchMessages[target].Msg.Iovlen = int32(len(targetIovecs))
o.batchSpilled[target] = o.batchSpilled[slot]
}
clear(o.batchIovecs[(o.batchCount-o.batchStart)*goDarwinBatchIovecs : o.batchCount*goDarwinBatchIovecs])
o.batchCount -= o.batchStart
o.batchStart = 0
}
func (o *goDarwinIO) writePacketLocked(frame [][]byte) error {
defer func() { clear(o.transmitIovecs) }()
if o.gateRoom(1) == 0 {
o.gateBlockLocked()
return errGoTransmitBlocked
}
headerVec := packetHeaderVec6
if header.IPVersion(frame[0]) == header.IPv4Version {
headerVec = packetHeaderVec4
}
iovecs := append(o.transmitIovecs[:0], headerVec)
for _, segment := range frame {
if len(segment) == 0 {
continue
}
iovecs = append(iovecs, rawfile.IovecFromBytes(segment))
}
o.transmitIovecs = iovecs
for {
errno := rawfile.NonBlockingWriteIovec(o.tunFd, iovecs)
if errno == 0 {
o.gateSubmitted++
return nil
}
if errno == unix.EINTR {
if o.closing.Load() {
return os.ErrClosed
}
continue
}
if errno == unix.ENOSPC {
o.gateSubmitted++
o.recordWriteError(errno)
o.gateBlockLocked()
return errGoTransmitBlocked
}
if errno == unix.ENOBUFS || errno == unix.EAGAIN {
if o.closing.Load() {
return os.ErrClosed
}
o.gateBlockLocked()
return errGoTransmitBlocked
}
return errno
}
}
func (o *goDarwinIO) transmitPrefix() int {
return PacketOffset
}
func (o *goDarwinIO) transmitChecksumOffload() bool {
return false
}
func (o *goDarwinIO) transmitSegmentOffload() bool {
return false
}
func (o *goDarwinIO) armTransmitWritable() (bool, error) {
if o.gateBlocked.Load() {
return true, nil
}
_, err := unix.Kevent(o.kqueueFd, o.transmitWritableEvent[:], nil, nil)
if err != nil {
return false, E.Cause(err, "go: arm EVFILT_WRITE")
}
return true, nil
}
func (o *goDarwinIO) takeTransmitWritable() bool {
return o.transmitWritable.Swap(false)
}
func (o *goDarwinIO) wake() {
_, _ = unix.Kevent(o.kqueueFd, o.wakeEvent[:], nil, nil)
}
func (o *goDarwinIO) close() error {
o.closing.Store(true)
return unix.Close(o.kqueueFd)
}
func (o *goDarwinIO) writePacketBatch(frames []goUDPFrame) error {
o.transmitAccess.Lock()
defer o.transmitAccess.Unlock()
defer func() {
clear(o.packetMessages[:])
clear(o.packetIovecs[:])
}()
o.flushLocked()
limit := len(frames)
for index := 0; index < len(frames); {
room := o.gateRoom(min(len(frames)-index, limit))
if room == 0 || o.batchCount > o.batchStart {
o.gateBlockLocked()
o.droppedEngineFrames.record(o.stack.logger, "UDP frames")
return errGoFrameDropped
}
frame := &frames[index]
if frame.length+len(frame.payload) > goDarwinBatchFrameLimit {
segments := [2][]byte{frame.header[:frame.length], frame.payload}
err := o.writePacketLocked(segments[:])
if err == errGoTransmitBlocked {
o.droppedEngineFrames.record(o.stack.logger, "UDP frames")
return errGoFrameDropped
} else if err != nil {
return err
}
index++
continue
}
count := 0
for count < room {
next := &frames[index+count]
if next.length+len(next.payload) > goDarwinBatchFrameLimit {
break
}
iovecs := o.packetIovecs[count*3 : (count+1)*3]
iovecs[0] = packetHeaderVec6
if next.length == header.IPv4MinimumSize+header.UDPMinimumSize {
iovecs[0] = packetHeaderVec4
}
iovecs[1] = rawfile.IovecFromBytes(next.header[:next.length])
iovecCount := 2
if len(next.payload) > 0 {
iovecs[2] = rawfile.IovecFromBytes(next.payload)
iovecCount++
}
o.packetMessages[count] = rawfile.MsgHdrX{}
o.packetMessages[count].Msg.Iov = &iovecs[0]
o.packetMessages[count].Msg.Iovlen = int32(iovecCount)
count++
}
written, errno := rawfile.NonBlockingSendMMsg(o.tunFd, o.packetMessages[:count])
if errno == unix.EINTR {
continue
}
if (errno == unix.EMSGSIZE || errno == unix.ENOBUFS) && count > 1 {
limit = (count + 1) / 2
continue
}
if errno == unix.ENOBUFS || errno == unix.EAGAIN {
o.gateBlockLocked()
o.droppedEngineFrames.record(o.stack.logger, "UDP frames")
return errGoFrameDropped
}
if errno != 0 {
o.gateSubmitted += int64(count)
o.recordWriteError(errno)
return errGoFrameDropped
}
if written == 0 {
return unix.EIO
}
o.gateSubmitted += int64(written)
index += written
}
return nil
}