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
|
// Copyright 2018 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.
package queue
import (
"encoding/binary"
"reflect"
"testing"
"gvisor.dev/gvisor/pkg/tcpip/link/sharedmem/pipe"
)
func TestBasicTxQueue(t *testing.T) {
// Tests that a basic transmit on a queue works, and that completion
// gets properly reported as well.
pb1 := make([]byte, 100)
pb2 := make([]byte, 100)
var rxp pipe.Rx
rxp.Init(pb1)
var txp pipe.Tx
txp.Init(pb2)
var q Tx
q.Init(pb1, pb2)
// Enqueue two buffers.
b := []TxBuffer{
{nil, 100, 60},
{nil, 200, 40},
}
b[0].Next = &b[1]
const usedID = 1002
const usedTotalSize = 100
if !q.Enqueue(usedID, usedTotalSize, 2, &b[0]) {
t.Fatalf("Enqueue failed on empty queue")
}
// Check the contents of the pipe.
d := rxp.Pull()
if d == nil {
t.Fatalf("Tx pipe is empty after Enqueue")
}
want := []byte{
234, 3, 0, 0, 0, 0, 0, 0, // id
100, 0, 0, 0, // total size
0, 0, 0, 0, // reserved
100, 0, 0, 0, 0, 0, 0, 0, // offset 1
60, 0, 0, 0, // size 1
200, 0, 0, 0, 0, 0, 0, 0, // offset 2
40, 0, 0, 0, // size 2
}
if !reflect.DeepEqual(want, d) {
t.Fatalf("Bad posted packet: got %v, want %v", d, want)
}
rxp.Flush()
// Check that there are no completions yet.
if _, ok := q.CompletedPacket(); ok {
t.Fatalf("Packet reported as completed too soon")
}
// Post a completion.
d = txp.Push(8)
if d == nil {
t.Fatalf("Unable to push to rx pipe")
}
binary.LittleEndian.PutUint64(d, usedID)
txp.Flush()
// Check that completion is properly reported.
id, ok := q.CompletedPacket()
if !ok {
t.Fatalf("Completion not reported")
}
if id != usedID {
t.Fatalf("Bad completion id: got %v, want %v", id, usedID)
}
}
func TestBasicRxQueue(t *testing.T) {
// Tests that a basic receive on a queue works.
pb1 := make([]byte, 100)
pb2 := make([]byte, 100)
var rxp pipe.Rx
rxp.Init(pb1)
var txp pipe.Tx
txp.Init(pb2)
var q Rx
q.Init(pb1, pb2, nil)
// Post two buffers.
b := []RxBuffer{
{100, 60, 1077, 0},
{200, 40, 2123, 0},
}
if !q.PostBuffers(b) {
t.Fatalf("PostBuffers failed on empty queue")
}
// Check the contents of the pipe.
want := [][]byte{
{
100, 0, 0, 0, 0, 0, 0, 0, // Offset1
60, 0, 0, 0, // Size1
0, 0, 0, 0, // Remaining in group 1
0, 0, 0, 0, 0, 0, 0, 0, // User data 1
53, 4, 0, 0, 0, 0, 0, 0, // ID 1
},
{
200, 0, 0, 0, 0, 0, 0, 0, // Offset2
40, 0, 0, 0, // Size2
0, 0, 0, 0, // Remaining in group 2
0, 0, 0, 0, 0, 0, 0, 0, // User data 2
75, 8, 0, 0, 0, 0, 0, 0, // ID 2
},
}
for i := range b {
d := rxp.Pull()
if d == nil {
t.Fatalf("Tx pipe is empty after PostBuffers")
}
if !reflect.DeepEqual(want[i], d) {
t.Fatalf("Bad posted packet: got %v, want %v", d, want[i])
}
rxp.Flush()
}
// Check that there are no completions.
if _, n := q.Dequeue(nil); n != 0 {
t.Fatalf("Packet reported as received too soon")
}
// Post a completion.
d := txp.Push(sizeOfConsumedPacketHeader + 2*sizeOfConsumedBuffer)
if d == nil {
t.Fatalf("Unable to push to rx pipe")
}
copy(d, []byte{
100, 0, 0, 0, // packet size
0, 0, 0, 0, // reserved
100, 0, 0, 0, 0, 0, 0, 0, // offset 1
60, 0, 0, 0, // size 1
0, 0, 0, 0, 0, 0, 0, 0, // user data 1
53, 4, 0, 0, 0, 0, 0, 0, // ID 1
200, 0, 0, 0, 0, 0, 0, 0, // offset 2
40, 0, 0, 0, // size 2
0, 0, 0, 0, 0, 0, 0, 0, // user data 2
75, 8, 0, 0, 0, 0, 0, 0, // ID 2
})
txp.Flush()
// Check that completion is properly reported.
bufs, n := q.Dequeue(nil)
if n != 100 {
t.Fatalf("Bad packet size: got %v, want %v", n, 100)
}
if !reflect.DeepEqual(bufs, b) {
t.Fatalf("Bad returned buffers: got %v, want %v", bufs, b)
}
}
func TestBadTxCompletion(t *testing.T) {
// Check that tx completions with bad sizes are properly ignored.
pb1 := make([]byte, 100)
pb2 := make([]byte, 100)
var rxp pipe.Rx
rxp.Init(pb1)
var txp pipe.Tx
txp.Init(pb2)
var q Tx
q.Init(pb1, pb2)
// Post a completion that is too short, and check that it is ignored.
if d := txp.Push(7); d == nil {
t.Fatalf("Unable to push to rx pipe")
}
txp.Flush()
if _, ok := q.CompletedPacket(); ok {
t.Fatalf("Bad completion not ignored")
}
// Post a completion that is too long, and check that it is ignored.
if d := txp.Push(10); d == nil {
t.Fatalf("Unable to push to rx pipe")
}
txp.Flush()
if _, ok := q.CompletedPacket(); ok {
t.Fatalf("Bad completion not ignored")
}
}
func TestBadRxCompletion(t *testing.T) {
// Check that bad rx completions are properly ignored.
pb1 := make([]byte, 100)
pb2 := make([]byte, 100)
var rxp pipe.Rx
rxp.Init(pb1)
var txp pipe.Tx
txp.Init(pb2)
var q Rx
q.Init(pb1, pb2, nil)
// Post a completion that is too short, and check that it is ignored.
if d := txp.Push(7); d == nil {
t.Fatalf("Unable to push to rx pipe")
}
txp.Flush()
if b, _ := q.Dequeue(nil); b != nil {
t.Fatalf("Bad completion not ignored")
}
// Post a completion whose buffer sizes add up to less than the total
// size.
d := txp.Push(sizeOfConsumedPacketHeader + 2*sizeOfConsumedBuffer)
if d == nil {
t.Fatalf("Unable to push to rx pipe")
}
copy(d, []byte{
100, 0, 0, 0, // packet size
0, 0, 0, 0, // reserved
100, 0, 0, 0, 0, 0, 0, 0, // offset 1
10, 0, 0, 0, // size 1
0, 0, 0, 0, 0, 0, 0, 0, // user data 1
53, 4, 0, 0, 0, 0, 0, 0, // ID 1
200, 0, 0, 0, 0, 0, 0, 0, // offset 2
10, 0, 0, 0, // size 2
0, 0, 0, 0, 0, 0, 0, 0, // user data 2
75, 8, 0, 0, 0, 0, 0, 0, // ID 2
})
txp.Flush()
if b, _ := q.Dequeue(nil); b != nil {
t.Fatalf("Bad completion not ignored")
}
// Post a completion whose buffer sizes will cause a 32-bit overflow,
// but adds up to the right number.
d = txp.Push(sizeOfConsumedPacketHeader + 2*sizeOfConsumedBuffer)
if d == nil {
t.Fatalf("Unable to push to rx pipe")
}
copy(d, []byte{
100, 0, 0, 0, // packet size
0, 0, 0, 0, // reserved
100, 0, 0, 0, 0, 0, 0, 0, // offset 1
255, 255, 255, 255, // size 1
0, 0, 0, 0, 0, 0, 0, 0, // user data 1
53, 4, 0, 0, 0, 0, 0, 0, // ID 1
200, 0, 0, 0, 0, 0, 0, 0, // offset 2
101, 0, 0, 0, // size 2
0, 0, 0, 0, 0, 0, 0, 0, // user data 2
75, 8, 0, 0, 0, 0, 0, 0, // ID 2
})
txp.Flush()
if b, _ := q.Dequeue(nil); b != nil {
t.Fatalf("Bad completion not ignored")
}
}
func TestFillTxPipe(t *testing.T) {
// Check that transmitting a new buffer when the buffer pipe is full
// fails gracefully.
pb1 := make([]byte, 104)
pb2 := make([]byte, 104)
var rxp pipe.Rx
rxp.Init(pb1)
var txp pipe.Tx
txp.Init(pb2)
var q Tx
q.Init(pb1, pb2)
// Transmit twice, which should fill the tx pipe.
b := []TxBuffer{
{nil, 100, 60},
{nil, 200, 40},
}
b[0].Next = &b[1]
const usedID = 1002
const usedTotalSize = 100
for i := uint64(0); i < 2; i++ {
if !q.Enqueue(usedID+i, usedTotalSize, 2, &b[0]) {
t.Fatalf("Failed to transmit buffer")
}
}
// Transmit another packet now that the tx pipe is full.
if q.Enqueue(usedID+2, usedTotalSize, 2, &b[0]) {
t.Fatalf("Enqueue succeeded when tx pipe is full")
}
}
func TestFillRxPipe(t *testing.T) {
// Check that posting a new buffer when the buffer pipe is full fails
// gracefully.
pb1 := make([]byte, 100)
pb2 := make([]byte, 100)
var rxp pipe.Rx
rxp.Init(pb1)
var txp pipe.Tx
txp.Init(pb2)
var q Rx
q.Init(pb1, pb2, nil)
// Post a buffer twice, it should fill the tx pipe.
b := []RxBuffer{
{100, 60, 1077, 0},
}
for i := 0; i < 2; i++ {
if !q.PostBuffers(b) {
t.Fatalf("PostBuffers failed on non-full queue")
}
}
// Post another buffer now that the tx pipe is full.
if q.PostBuffers(b) {
t.Fatalf("PostBuffers succeeded on full queue")
}
}
func TestLotsOfTransmissions(t *testing.T) {
// Make sure pipes are being properly flushed when transmitting packets.
pb1 := make([]byte, 100)
pb2 := make([]byte, 100)
var rxp pipe.Rx
rxp.Init(pb1)
var txp pipe.Tx
txp.Init(pb2)
var q Tx
q.Init(pb1, pb2)
// Prepare packet with two buffers.
b := []TxBuffer{
{nil, 100, 60},
{nil, 200, 40},
}
b[0].Next = &b[1]
const usedID = 1002
const usedTotalSize = 100
// Post 100000 packets and completions.
for i := 100000; i > 0; i-- {
if !q.Enqueue(usedID, usedTotalSize, 2, &b[0]) {
t.Fatalf("Enqueue failed on non-full queue")
}
if d := rxp.Pull(); d == nil {
t.Fatalf("Tx pipe is empty after Enqueue")
}
rxp.Flush()
d := txp.Push(8)
if d == nil {
t.Fatalf("Unable to write to rx pipe")
}
binary.LittleEndian.PutUint64(d, usedID)
txp.Flush()
if _, ok := q.CompletedPacket(); !ok {
t.Fatalf("Completion not returned")
}
}
}
func TestLotsOfReceptions(t *testing.T) {
// Make sure pipes are being properly flushed when receiving packets.
pb1 := make([]byte, 100)
pb2 := make([]byte, 100)
var rxp pipe.Rx
rxp.Init(pb1)
var txp pipe.Tx
txp.Init(pb2)
var q Rx
q.Init(pb1, pb2, nil)
// Prepare for posting two buffers.
b := []RxBuffer{
{100, 60, 1077, 0},
{200, 40, 2123, 0},
}
// Post 100000 buffers and completions.
for i := 100000; i > 0; i-- {
if !q.PostBuffers(b) {
t.Fatalf("PostBuffers failed on non-full queue")
}
if d := rxp.Pull(); d == nil {
t.Fatalf("Tx pipe is empty after PostBuffers")
}
rxp.Flush()
if d := rxp.Pull(); d == nil {
t.Fatalf("Tx pipe is empty after PostBuffers")
}
rxp.Flush()
d := txp.Push(sizeOfConsumedPacketHeader + 2*sizeOfConsumedBuffer)
if d == nil {
t.Fatalf("Unable to push to rx pipe")
}
copy(d, []byte{
100, 0, 0, 0, // packet size
0, 0, 0, 0, // reserved
100, 0, 0, 0, 0, 0, 0, 0, // offset 1
60, 0, 0, 0, // size 1
0, 0, 0, 0, 0, 0, 0, 0, // user data 1
53, 4, 0, 0, 0, 0, 0, 0, // ID 1
200, 0, 0, 0, 0, 0, 0, 0, // offset 2
40, 0, 0, 0, // size 2
0, 0, 0, 0, 0, 0, 0, 0, // user data 2
75, 8, 0, 0, 0, 0, 0, 0, // ID 2
})
txp.Flush()
if _, n := q.Dequeue(nil); n == 0 {
t.Fatalf("Dequeue failed when there is a completion")
}
}
}
func TestRxEnableNotification(t *testing.T) {
// Check that enabling nofifications results in properly updated state.
pb1 := make([]byte, 100)
pb2 := make([]byte, 100)
var state uint32
var q Rx
q.Init(pb1, pb2, &state)
q.EnableNotification()
if state != eventFDEnabled {
t.Fatalf("Bad value in shared state: got %v, want %v", state, eventFDEnabled)
}
}
func TestRxDisableNotification(t *testing.T) {
// Check that disabling nofifications results in properly updated state.
pb1 := make([]byte, 100)
pb2 := make([]byte, 100)
var state uint32
var q Rx
q.Init(pb1, pb2, &state)
q.DisableNotification()
if state != eventFDDisabled {
t.Fatalf("Bad value in shared state: got %v, want %v", state, eventFDDisabled)
}
}
|