forked from MetaCubeX/sing-tun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtun_darwin_gvisor.go
More file actions
66 lines (58 loc) · 1.71 KB
/
Copy pathtun_darwin_gvisor.go
File metadata and controls
66 lines (58 loc) · 1.71 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
//go:build with_gvisor && darwin
package tun
import (
"github.com/metacubex/gvisor/pkg/tcpip/header"
"github.com/metacubex/gvisor/pkg/tcpip/link/qdisc/fifo"
"github.com/metacubex/gvisor/pkg/tcpip/stack"
"github.com/metacubex/sing-tun/internal/fdbased_darwin"
"github.com/metacubex/sing-tun/internal/rawfile_darwin"
"golang.org/x/sys/unix"
)
var _ GVisorTun = (*NativeTun)(nil)
func (t *NativeTun) WritePacket(pkt *stack.PacketBuffer) (int, error) {
views := pkt.AsSlices()
numIovecs := len(views)
numIovecs++ // for packetHeaderVec4/6
// Allocate small iovec arrays on the stack.
var iovecsArr [8]unix.Iovec
iovecs := iovecsArr[:0]
if numIovecs > len(iovecsArr) {
iovecs = make([]unix.Iovec, 0, numIovecs)
}
if pkt.NetworkProtocolNumber == header.IPv4ProtocolNumber {
iovecs = append(iovecs, packetHeaderVec4)
} else {
iovecs = append(iovecs, packetHeaderVec6)
}
var dataLen int
for _, packetSlice := range views {
dataLen += len(packetSlice)
iovec := unix.Iovec{
Base: &packetSlice[0],
}
iovec.SetLen(len(packetSlice))
iovecs = append(iovecs, iovec)
}
errno := rawfile.NonBlockingWriteIovec(t.tunFd, iovecs)
if errno == 0 {
return dataLen, nil
} else {
return 0, errno
}
}
func (t *NativeTun) NewEndpoint() (stack.LinkEndpoint, stack.NICOptions, error) {
ep, err := fdbased.New(&fdbased.Options{
ProcessorsPerChannel: t.options.EXP_ProcessorsPerChannel,
FDs: []int{t.tunFd},
MTU: t.options.MTU,
RXChecksumOffload: true,
RecvMsgX: t.options.EXP_RecvMsgX,
SendMsgX: t.options.EXP_SendMsgX,
})
if err != nil {
return nil, stack.NICOptions{}, err
}
return ep, stack.NICOptions{
QDisc: fifo.New(ep, 1, 1000),
}, nil
}