-
Notifications
You must be signed in to change notification settings - Fork 506
/
device.go
213 lines (188 loc) · 5.24 KB
/
device.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
package vif
import (
"context"
"io"
"net"
"sync"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"gvisor.dev/gvisor/pkg/bufferv2"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/header"
"gvisor.dev/gvisor/pkg/tcpip/link/channel"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"github.com/datawire/dlib/dlog"
"github.com/telepresenceio/telepresence/v2/pkg/routing"
"github.com/telepresenceio/telepresence/v2/pkg/tracing"
"github.com/telepresenceio/telepresence/v2/pkg/vif/buffer"
)
type device struct {
*channel.Endpoint
ctx context.Context
wg sync.WaitGroup
dev *nativeDevice
table routing.Table
}
type Device interface {
stack.LinkEndpoint
io.Closer
Index() int32
Name() string
AddSubnet(context.Context, *net.IPNet) error
RemoveSubnet(context.Context, *net.IPNet) error
SetDNS(context.Context, string, net.IP, []string) (err error)
}
const defaultDevMtu = 1500
// Queue length for outbound packet, arriving at fd side for read. Overflow
// causes packet drops. gVisor implementation-specific.
const defaultDevOutQueueLen = 1024
var _ Device = (*device)(nil)
// OpenTun creates a new TUN device and ensures that it is up and running.
func OpenTun(ctx context.Context, routingTable routing.Table) (Device, error) {
dev, err := openTun(ctx)
if err != nil {
return nil, err
}
return &device{
Endpoint: channel.New(defaultDevOutQueueLen, defaultDevMtu, ""),
ctx: ctx,
dev: dev,
table: routingTable,
}, nil
}
func (d *device) Attach(dp stack.NetworkDispatcher) {
go func() {
d.Endpoint.Attach(dp)
if dp == nil {
// Stack is closing
return
}
dlog.Info(d.ctx, "Starting Endpoint")
ctx, cancel := context.WithCancel(d.ctx)
d.wg.Add(2)
go d.tunToDispatch(cancel)
d.dispatchToTun(ctx)
}()
}
func (d *device) subnetToRoute(subnet *net.IPNet) (*routing.Route, error) {
gw := make(net.IP, len(subnet.IP))
copy(gw, subnet.IP)
gw[len(gw)-1] += 1
iface, err := net.InterfaceByName(d.Name())
if err != nil {
return nil, err
}
return &routing.Route{
LocalIP: subnet.IP,
RoutedNet: subnet,
Interface: iface,
Gateway: gw,
}, nil
}
// AddSubnet adds a subnet to this TUN device and creates a route for that subnet which
// is associated with the device (removing the device will automatically remove the route).
func (d *device) AddSubnet(ctx context.Context, subnet *net.IPNet) (err error) {
ctx, span := otel.GetTracerProvider().Tracer("").Start(ctx, "AddSubnet", trace.WithAttributes(attribute.Stringer("tel2.subnet", subnet)))
defer tracing.EndAndRecord(span, err)
if err := d.dev.addSubnet(ctx, subnet); err != nil {
return err
}
route, err := d.subnetToRoute(subnet)
if err != nil {
return err
}
return d.table.Add(ctx, route)
}
func (d *device) Close() error {
return d.dev.Close()
}
// Index returns the index of this device.
func (d *device) Index() int32 {
return d.dev.index()
}
// Name returns the name of this device, e.g. "tun0".
func (d *device) Name() string {
return d.dev.name
}
// SetDNS sets the DNS configuration for the device on the windows platform.
func (d *device) SetDNS(ctx context.Context, clusterDomain string, server net.IP, domains []string) (err error) {
return d.dev.setDNS(ctx, clusterDomain, server, domains)
}
func (d *device) SetMTU(mtu int) error {
return d.dev.setMTU(mtu)
}
// RemoveSubnet removes a subnet from this TUN device and also removes the route for that subnet which
// is associated with the device.
func (d *device) RemoveSubnet(ctx context.Context, subnet *net.IPNet) (err error) {
route, err := d.subnetToRoute(subnet)
if err != nil {
return err
}
if err := d.table.Remove(ctx, route); err != nil {
return err
}
// Staticcheck screams if this is ctx, span := because it thinks the context argument is being overwritten before being used.
sCtx, span := otel.GetTracerProvider().Tracer("").Start(ctx, "RemoveSubnet", trace.WithAttributes(attribute.Stringer("tel2.subnet", subnet)))
defer tracing.EndAndRecord(span, err)
return d.dev.removeSubnet(sCtx, subnet)
}
func (d *device) Wait() {
d.wg.Wait()
dlog.Info(d.ctx, "Endpoint done")
}
func (d *device) tunToDispatch(cancel context.CancelFunc) {
defer func() {
cancel()
d.wg.Done()
}()
buf := buffer.NewData(0x10000)
data := buf.Buf()
for ok := true; ok; {
n, err := d.dev.readPacket(buf)
if err != nil {
ok = d.IsAttached()
if ok && d.ctx.Err() == nil {
dlog.Errorf(d.ctx, "read packet error: %v", err)
}
return
}
if n == 0 {
continue
}
var ipv tcpip.NetworkProtocolNumber
switch header.IPVersion(data) {
case header.IPv4Version:
ipv = header.IPv4ProtocolNumber
case header.IPv6Version:
ipv = header.IPv6ProtocolNumber
default:
continue
}
pb := stack.NewPacketBuffer(stack.PacketBufferOptions{
Payload: bufferv2.MakeWithData(data[:n]),
})
d.InjectInbound(ipv, pb)
pb.DecRef()
}
}
func (d *device) dispatchToTun(ctx context.Context) {
defer d.wg.Done()
buf := buffer.NewData(0x10000)
for {
pb := d.ReadContext(ctx)
if pb.IsNil() {
break
}
buf.Resize(pb.Size())
b := buf.Buf()
for _, s := range pb.AsSlices() {
copy(b, s)
b = b[len(s):]
}
pb.DecRef()
if _, err := d.dev.writePacket(buf, 0); err != nil {
dlog.Errorf(ctx, "WritePacket failed: %v", err)
}
}
}