-
Notifications
You must be signed in to change notification settings - Fork 20
/
open_linux.go
108 lines (87 loc) · 2.26 KB
/
open_linux.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
package tun
import (
"fmt"
"sync"
"github.com/Asutorufa/yuhaiin/pkg/net/nat"
"github.com/Asutorufa/yuhaiin/pkg/net/netlink"
"github.com/tailscale/wireguard-go/conn"
wun "github.com/tailscale/wireguard-go/tun"
)
const (
offset = 0
)
func OpenWriter(sc netlink.TunScheme, mtu int) (netlink.Tun, error) {
var err error
var device wun.Device
switch sc.Scheme {
case "tun":
wd, err := wun.CreateTUN(sc.Name, mtu)
if err != nil {
return nil, fmt.Errorf("create tun failed: %w", err)
}
if wd.BatchSize() == conn.IdealBatchSize {
wd = newWrapGsoDevice(wd)
// gso enabled
}
device = wd
case "fd":
device, _, err = wun.CreateUnmonitoredTUNFromFD(sc.Fd)
default:
return nil, fmt.Errorf("invalid tun: %v", sc)
}
if err != nil {
return nil, fmt.Errorf("create tun failed: %w", err)
}
return NewDevice(device, offset), nil
}
type wrapGsoDevice struct {
wun.Device
mtu int
w sync.Mutex
wbuffers [][]byte
}
func newWrapGsoDevice(device wun.Device) *wrapGsoDevice {
mtu, _ := device.MTU()
if mtu <= 0 {
mtu = nat.MaxSegmentSize
}
w := &wrapGsoDevice{
Device: device,
mtu: mtu,
wbuffers: getBuffer(device.BatchSize(), mtu+offset+10),
}
return w
}
func (w *wrapGsoDevice) Write(bufs [][]byte, offset int) (int, error) {
// https://github.com/WireGuard/wireguard-go/blob/12269c2761734b15625017d8565745096325392f/tun/offload_linux.go#L867
//
// virtioNetHdrLen = 10
if len(bufs) > len(w.wbuffers) {
return 0, fmt.Errorf("buffer %d is larger than recevied: %d", len(w.wbuffers), len(bufs))
}
w.w.Lock()
defer w.w.Unlock()
buffs := buffPool(len(bufs), false).Get().([][]byte)
defer buffPool(len(bufs), false).Put(buffs)
for i := range bufs {
n := copy(w.wbuffers[i][10:], bufs[i])
buffs[i] = w.wbuffers[i][:n+10]
}
return w.Device.Write(buffs, 10)
}
func (w *wrapGsoDevice) Read(bufs [][]byte, sizes []int, offset int) (n int, err error) {
// https://github.com/WireGuard/wireguard-go/blob/12269c2761734b15625017d8565745096325392f/tun/offload_linux.go#L867
//
// virtioNetHdrLen = 10
n, err = w.Device.Read(bufs, sizes, 10)
if err != nil {
return
}
for x := range n {
if sizes[x] < 10 {
return n, fmt.Errorf("invalid packet size small than virtioHdr 10: %d", sizes[x])
}
copy(bufs[x], bufs[x][10:])
}
return
}