-
Notifications
You must be signed in to change notification settings - Fork 4
/
sharedmem_server.go
368 lines (318 loc) · 10 KB
/
sharedmem_server.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
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
// Copyright 2021 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build linux
// +build linux
package sharedmem
import (
"github.com/nicocha30/gvisor-ligolo/pkg/atomicbitops"
"github.com/nicocha30/gvisor-ligolo/pkg/buffer"
"github.com/nicocha30/gvisor-ligolo/pkg/sync"
"github.com/nicocha30/gvisor-ligolo/pkg/tcpip"
"github.com/nicocha30/gvisor-ligolo/pkg/tcpip/header"
"github.com/nicocha30/gvisor-ligolo/pkg/tcpip/link/rawfile"
"github.com/nicocha30/gvisor-ligolo/pkg/tcpip/stack"
)
type serverEndpoint struct {
// mtu (maximum transmission unit) is the maximum size of a packet.
// mtu is immutable.
mtu uint32
// bufferSize is the size of each individual buffer.
// bufferSize is immutable.
bufferSize uint32
// addr is the local address of this endpoint.
// addr is immutable
addr tcpip.LinkAddress
// rx is the receive queue.
rx serverRx
// stopRequested determines whether the worker goroutines should stop.
stopRequested atomicbitops.Uint32
// Wait group used to indicate that all workers have stopped.
completed sync.WaitGroup
// peerFD is an fd to the peer that can be used to detect when the peer is
// gone.
// peerFD is immutable.
peerFD int
// caps holds the endpoint capabilities.
caps stack.LinkEndpointCapabilities
// hdrSize is the size of the link layer header if any.
// hdrSize is immutable.
hdrSize uint32
// virtioNetHeaderRequired if true indicates that a virtio header is expected
// in all inbound/outbound packets.
virtioNetHeaderRequired bool
// onClosed is a function to be called when the FD's peer (if any) closes its
// end of the communication pipe.
onClosed func(tcpip.Error)
// mu protects the following fields.
mu sync.Mutex
// tx is the transmit queue.
// +checklocks:mu
tx serverTx
// workerStarted specifies whether the worker goroutine was started.
// +checklocks:mu
workerStarted bool
}
// NewServerEndpoint creates a new shared-memory-based endpoint. Buffers will be
// broken up into buffers of "bufferSize" bytes.
func NewServerEndpoint(opts Options) (stack.LinkEndpoint, error) {
e := &serverEndpoint{
mtu: opts.MTU,
bufferSize: opts.BufferSize,
addr: opts.LinkAddress,
peerFD: opts.PeerFD,
onClosed: opts.OnClosed,
}
if err := e.tx.init(&opts.RX); err != nil {
return nil, err
}
if err := e.rx.init(&opts.TX); err != nil {
e.tx.cleanup()
return nil, err
}
e.caps = stack.LinkEndpointCapabilities(0)
if opts.RXChecksumOffload {
e.caps |= stack.CapabilityRXChecksumOffload
}
if opts.TXChecksumOffload {
e.caps |= stack.CapabilityTXChecksumOffload
}
if opts.LinkAddress != "" {
e.hdrSize = header.EthernetMinimumSize
e.caps |= stack.CapabilityResolutionRequired
}
return e, nil
}
// Close frees all resources associated with the endpoint.
func (e *serverEndpoint) Close() {
// Tell dispatch goroutine to stop, then write to the eventfd so that it wakes
// up in case it's sleeping.
e.stopRequested.Store(1)
e.rx.eventFD.Notify()
// Cleanup the queues inline if the worker hasn't started yet; we also know it
// won't start from now on because stopRequested is set to 1.
e.mu.Lock()
defer e.mu.Unlock()
workerPresent := e.workerStarted
if !workerPresent {
e.tx.cleanup()
e.rx.cleanup()
}
}
// Wait implements stack.LinkEndpoint.Wait. It waits until all workers have
// stopped after a Close() call.
func (e *serverEndpoint) Wait() {
e.completed.Wait()
}
// Attach implements stack.LinkEndpoint.Attach. It launches the goroutine that
// reads packets from the rx queue.
func (e *serverEndpoint) Attach(dispatcher stack.NetworkDispatcher) {
e.mu.Lock()
if !e.workerStarted && e.stopRequested.Load() == 0 {
e.workerStarted = true
e.completed.Add(1)
if e.peerFD >= 0 {
e.completed.Add(1)
// Spin up a goroutine to monitor for peer shutdown.
go func() {
b := make([]byte, 1)
// When sharedmem endpoint is in use the peerFD is never used for any
// data transfer and this Read should only return if the peer is
// shutting down.
_, err := rawfile.BlockingRead(e.peerFD, b)
if e.onClosed != nil {
e.onClosed(err)
}
e.completed.Done()
}()
}
// Link endpoints are not savable. When transportation endpoints are saved,
// they stop sending outgoing packets and all incoming packets are rejected.
go e.dispatchLoop(dispatcher) // S/R-SAFE: see above.
}
e.mu.Unlock()
}
// IsAttached implements stack.LinkEndpoint.IsAttached.
func (e *serverEndpoint) IsAttached() bool {
e.mu.Lock()
defer e.mu.Unlock()
return e.workerStarted
}
// MTU implements stack.LinkEndpoint.MTU. It returns the value initialized
// during construction.
func (e *serverEndpoint) MTU() uint32 {
return e.mtu
}
// Capabilities implements stack.LinkEndpoint.Capabilities.
func (e *serverEndpoint) Capabilities() stack.LinkEndpointCapabilities {
return e.caps
}
// MaxHeaderLength implements stack.LinkEndpoint.MaxHeaderLength. It returns the
// ethernet frame header size.
func (e *serverEndpoint) MaxHeaderLength() uint16 {
return uint16(e.hdrSize)
}
// LinkAddress implements stack.LinkEndpoint.LinkAddress. It returns the local
// link address.
func (e *serverEndpoint) LinkAddress() tcpip.LinkAddress {
return e.addr
}
// AddHeader implements stack.LinkEndpoint.AddHeader.
func (e *serverEndpoint) AddHeader(pkt stack.PacketBufferPtr) {
// Add ethernet header if needed.
if len(e.addr) == 0 {
return
}
eth := header.Ethernet(pkt.LinkHeader().Push(header.EthernetMinimumSize))
eth.Encode(&header.EthernetFields{
SrcAddr: pkt.EgressRoute.LocalLinkAddress,
DstAddr: pkt.EgressRoute.RemoteLinkAddress,
Type: pkt.NetworkProtocolNumber,
})
}
func (e *serverEndpoint) parseHeader(pkt stack.PacketBufferPtr) bool {
_, ok := pkt.LinkHeader().Consume(header.EthernetMinimumSize)
return ok
}
// ParseHeader implements stack.LinkEndpoint.ParseHeader.
func (e *serverEndpoint) ParseHeader(pkt stack.PacketBufferPtr) bool {
// Add ethernet header if needed.
if len(e.addr) == 0 {
return true
}
return e.parseHeader(pkt)
}
func (e *serverEndpoint) AddVirtioNetHeader(pkt stack.PacketBufferPtr) {
virtio := header.VirtioNetHeader(pkt.VirtioNetHeader().Push(header.VirtioNetHeaderSize))
virtio.Encode(&header.VirtioNetHeaderFields{})
}
// +checklocks:e.mu
func (e *serverEndpoint) writePacketLocked(r stack.RouteInfo, protocol tcpip.NetworkProtocolNumber, pkt stack.PacketBufferPtr) tcpip.Error {
if e.virtioNetHeaderRequired {
e.AddVirtioNetHeader(pkt)
}
ok := e.tx.transmit(pkt)
if !ok {
return &tcpip.ErrWouldBlock{}
}
return nil
}
// WritePacket writes outbound packets to the file descriptor. If it is not
// currently writable, the packet is dropped.
// WritePacket implements stack.LinkEndpoint.WritePacket.
func (e *serverEndpoint) WritePacket(_ stack.RouteInfo, _ tcpip.NetworkProtocolNumber, pkt stack.PacketBufferPtr) tcpip.Error {
// Transmit the packet.
e.mu.Lock()
defer e.mu.Unlock()
if err := e.writePacketLocked(pkt.EgressRoute, pkt.NetworkProtocolNumber, pkt); err != nil {
return err
}
e.tx.notify()
return nil
}
// WritePackets implements stack.LinkEndpoint.WritePackets.
func (e *serverEndpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
n := 0
var err tcpip.Error
e.mu.Lock()
defer e.mu.Unlock()
for _, pkt := range pkts.AsSlice() {
if err = e.writePacketLocked(pkt.EgressRoute, pkt.NetworkProtocolNumber, pkt); err != nil {
break
}
n++
}
// WritePackets never returns an error if it successfully transmitted at least
// one packet.
if err != nil && n == 0 {
return 0, err
}
e.tx.notify()
return n, nil
}
// dispatchLoop reads packets from the rx queue in a loop and dispatches them
// to the network stack.
func (e *serverEndpoint) dispatchLoop(d stack.NetworkDispatcher) {
for e.stopRequested.Load() == 0 {
b := e.rx.receive()
if b == nil {
e.rx.EnableNotification()
// Now pull again to make sure we didn't receive any packets
// while notifications were not enabled.
for {
b = e.rx.receive()
if b != nil {
// Disable notifications as we only need to be notified when we are going
// to block on eventFD. This should prevent the peer from needlessly
// writing to eventFD when this end is already awake and processing
// packets.
e.rx.DisableNotification()
break
}
e.rx.waitForPackets()
}
}
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
Payload: buffer.MakeWithView(b),
})
if e.virtioNetHeaderRequired {
_, ok := pkt.VirtioNetHeader().Consume(header.VirtioNetHeaderSize)
if !ok {
pkt.DecRef()
continue
}
}
var proto tcpip.NetworkProtocolNumber
if len(e.addr) != 0 {
if !e.parseHeader(pkt) {
pkt.DecRef()
continue
}
proto = header.Ethernet(pkt.LinkHeader().Slice()).Type()
} else {
// We don't get any indication of what the packet is, so try to guess
// if it's an IPv4 or IPv6 packet.
// IP version information is at the first octet, so pulling up 1 byte.
h, ok := pkt.Data().PullUp(1)
if !ok {
pkt.DecRef()
continue
}
switch header.IPVersion(h) {
case header.IPv4Version:
proto = header.IPv4ProtocolNumber
case header.IPv6Version:
proto = header.IPv6ProtocolNumber
default:
pkt.DecRef()
continue
}
}
// Send packet up the stack.
d.DeliverNetworkPacket(proto, pkt)
pkt.DecRef()
}
e.mu.Lock()
defer e.mu.Unlock()
// Clean state.
e.tx.cleanup()
e.rx.cleanup()
e.completed.Done()
}
// ARPHardwareType implements stack.LinkEndpoint.ARPHardwareType
func (e *serverEndpoint) ARPHardwareType() header.ARPHardwareType {
if e.hdrSize > 0 {
return header.ARPHardwareEther
}
return header.ARPHardwareNone
}