This repository has been archived by the owner on Mar 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
map_test.go
744 lines (619 loc) · 13.9 KB
/
map_test.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
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
package ebpf
import (
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
"sort"
"testing"
"unsafe"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)
func TestMain(m *testing.M) {
err := unix.Setrlimit(8, &unix.Rlimit{
Cur: math.MaxUint64,
Max: math.MaxUint64,
})
if err != nil {
fmt.Println("WARNING: Failed to adjust rlimit, tests may fail")
}
os.Exit(m.Run())
}
func TestMap(t *testing.T) {
m := createArray(t)
defer m.Close()
t.Log(m)
if err := m.Put(uint32(0), uint32(42)); err != nil {
t.Fatal("Can't put:", err)
}
if err := m.Put(uint32(1), uint32(4242)); err != nil {
t.Fatal("Can't put:", err)
}
m2, err := m.Clone()
if err != nil {
t.Fatal("Can't clone map:", err)
}
defer m2.Close()
m.Close()
m = m2
var v uint32
if ok, err := m.Get(uint32(0), &v); err != nil {
t.Fatal("Can't get:", err)
} else if !ok {
t.Fatal("Key doesn't exist")
}
if v != 42 {
t.Error("Want value 42, got", v)
}
var k uint32
if ok, err := m.NextKey(uint32(0), &k); err != nil {
t.Fatal("Can't get:", err)
} else if !ok {
t.Fatal("Key doesn't exist")
}
if k != 1 {
t.Error("Want key 1, got", k)
}
}
func TestMapClose(t *testing.T) {
m := createArray(t)
if err := m.Close(); err != nil {
t.Fatal("Can't close map:", err)
}
if err := m.Put(uint32(0), uint32(42)); errors.Cause(err) != errClosedFd {
t.Fatal("Put doesn't check for closed fd", err)
}
if _, err := m.GetBytes(uint32(0)); errors.Cause(err) != errClosedFd {
t.Fatal("Get doesn't check for closed fd", err)
}
}
func TestMapCloneNil(t *testing.T) {
m, err := (*Map)(nil).Clone()
if err != nil {
t.Fatal(err)
}
if m != nil {
t.Fatal("Cloning a nil map doesn't return nil")
}
}
func TestMapPin(t *testing.T) {
m := createArray(t)
defer m.Close()
if err := m.Put(uint32(0), uint32(42)); err != nil {
t.Fatal("Can't put:", err)
}
tmp, err := ioutil.TempDir("/sys/fs/bpf", "ebpf-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)
path := filepath.Join(tmp, "map")
if err := m.Pin(path); err != nil {
t.Fatal(err)
}
m.Close()
m, err = LoadPinnedMap(path)
if err != nil {
t.Fatal(err)
}
defer m.Close()
var v uint32
if ok, err := m.Get(uint32(0), &v); err != nil {
t.Fatal("Can't get:", err)
} else if !ok {
t.Fatal("Key doesn't exist")
}
if v != 42 {
t.Error("Want value 42, got", v)
}
}
func createArray(t *testing.T) *Map {
t.Helper()
m, err := NewMap(&MapSpec{
Type: Array,
KeySize: 4,
ValueSize: 4,
MaxEntries: 2,
})
if err != nil {
t.Fatal(err)
}
return m
}
func TestMapInMap(t *testing.T) {
for _, typ := range []MapType{ArrayOfMaps, HashOfMaps} {
t.Run(typ.String(), func(t *testing.T) {
spec := &MapSpec{
Type: typ,
KeySize: 4,
MaxEntries: 2,
InnerMap: &MapSpec{
Type: Array,
KeySize: 4,
ValueSize: 4,
MaxEntries: 2,
},
}
inner, err := NewMap(spec.InnerMap)
if err != nil {
t.Fatal(err)
}
if err := inner.Put(uint32(1), uint32(4242)); err != nil {
t.Fatal(err)
}
defer inner.Close()
outer, err := NewMap(spec)
if err != nil {
t.Fatal(err)
}
defer outer.Close()
if err := outer.Put(uint32(0), inner); err != nil {
t.Fatal("Can't put inner map:", err)
}
var inner2 *Map
if ok, err := outer.Get(uint32(0), &inner2); err != nil {
t.Fatal(err)
} else if !ok {
t.Fatal("Missing key 0")
}
defer inner2.Close()
var v uint32
if ok, err := inner2.Get(uint32(1), &v); err != nil {
t.Fatal(err, inner)
} else if !ok {
t.Fatal("Missing key 0")
}
if v != 4242 {
t.Error("Expected value 4242, got", v)
}
inner2.Close()
// Make sure we can still access the original map
if ok, err := inner.Get(uint32(1), &v); err != nil {
t.Fatal(err, inner)
} else if !ok {
t.Fatal("Missing key 0 from inner")
}
if v != 4242 {
t.Error("Expected value 4242, got", v)
}
})
}
}
func TestMapInMapABI(t *testing.T) {
m := createMapInMap(t, ArrayOfMaps)
defer m.Close()
if m.abi.InnerMap == nil {
t.Error("ABI is missing InnerMap")
}
}
func createMapInMap(t *testing.T, typ MapType) *Map {
t.Helper()
spec := &MapSpec{
Type: typ,
KeySize: 4,
MaxEntries: 2,
InnerMap: &MapSpec{
Type: Array,
KeySize: 4,
ValueSize: 4,
MaxEntries: 2,
},
}
m, err := NewMap(spec)
if err != nil {
t.Fatal(err)
}
return m
}
func TestMapInMapValueSize(t *testing.T) {
spec := &MapSpec{
Type: ArrayOfMaps,
KeySize: 4,
ValueSize: 0,
MaxEntries: 2,
InnerMap: &MapSpec{
Type: Array,
KeySize: 4,
ValueSize: 4,
MaxEntries: 2,
},
}
if _, err := NewMap(spec); err != nil {
t.Fatal(err)
}
spec.ValueSize = 4
if _, err := NewMap(spec); err != nil {
t.Fatal(err)
}
spec.ValueSize = 1
if _, err := NewMap(spec); err == nil {
t.Fatal("Expected an error")
}
}
func TestIterateEmptyMap(t *testing.T) {
hash := createHash()
defer hash.Close()
entries := hash.Iterate()
var key string
var value uint32
if entries.Next(&key, &value) != false {
t.Error("Empty map should not be iterable")
}
}
func TestMapIterate(t *testing.T) {
hash := createHash()
defer hash.Close()
if err := hash.Put("hello", uint32(21)); err != nil {
t.Fatal(err)
}
if err := hash.Put("world", uint32(42)); err != nil {
t.Fatal(err)
}
var key string
var value uint32
var keys []string
entries := hash.Iterate()
for entries.Next(&key, &value) {
keys = append(keys, key)
}
if err := entries.Err(); err != nil {
t.Fatal(err)
}
sort.Strings(keys)
if keys[0] != "hello" {
t.Error("Expected index 0 to be hello, got", keys[0])
}
if keys[1] != "world" {
t.Error("Expected index 1 to be hello, got", keys[1])
}
}
func TestIterateMapInMap(t *testing.T) {
const idx = uint32(1)
parent := createMapInMap(t, ArrayOfMaps)
defer parent.Close()
a := createArray(t)
defer a.Close()
if err := parent.Put(idx, a); err != nil {
t.Fatal(err)
}
var (
key uint32
m *Map
entries = parent.Iterate()
)
defer m.Close()
if !entries.Next(&key, &m) {
t.Fatal("Iterator encountered error:", entries.Err())
}
if key != 1 {
t.Error("Iterator didn't skip first entry")
}
if m == nil {
t.Fatal("Map is nil")
}
}
func TestPerCPUMarshaling(t *testing.T) {
numCPU, err := possibleCPUs()
if err != nil {
t.Fatal(err)
}
if numCPU < 2 {
t.Skip("Test requires at least two CPUs")
}
arr, err := NewMap(&MapSpec{
Type: PerCPUArray,
KeySize: 4,
ValueSize: 5,
MaxEntries: 1,
})
if err != nil {
t.Fatal(err)
}
defer arr.Close()
values := []*customEncoding{
&customEncoding{"hello"},
&customEncoding{"world"},
}
if err := arr.Put(uint32(0), values); err != nil {
t.Fatal(err)
}
// Make sure unmarshaling works on slices containing pointers
var retrieved []*customEncoding
if ok, err := arr.Get(uint32(0), &retrieved); err != nil {
t.Fatal(err)
} else if !ok {
t.Fatal("Can't retrieve key 0")
}
for i, want := range []string{"HELLO", "WORLD"} {
if retrieved[i] == nil {
t.Error("First item is nil")
} else if have := retrieved[i].data; have != want {
t.Errorf("Put doesn't use BinaryMarshaler, expected %s but got %s", want, have)
}
}
}
func TestMapMarshalUnsafe(t *testing.T) {
m, err := NewMap(&MapSpec{
Type: Hash,
KeySize: 4,
ValueSize: 4,
MaxEntries: 1,
})
if err != nil {
t.Fatal(err)
}
defer m.Close()
key := uint32(1)
value := uint32(42)
if err := m.Put(unsafe.Pointer(&key), unsafe.Pointer(&value)); err != nil {
t.Fatal(err)
}
var res uint32
if ok, err := m.Get(unsafe.Pointer(&key), unsafe.Pointer(&res)); !ok || err != nil {
t.Fatal("Can't get item:", ok, err)
}
var sum uint32
iter := m.Iterate()
for iter.Next(&key, unsafe.Pointer(&res)) {
sum += res
}
if err := iter.Err(); err != nil {
t.Fatal(err)
}
if res != 42 {
t.Fatalf("Expected 42, got %d", res)
}
iter = m.Iterate()
iter.Next(unsafe.Pointer(&key), &res)
if err := iter.Err(); err != nil {
t.Error(err)
}
if key != 1 {
t.Errorf("Expected key 1, got %d", key)
}
if err := m.DeleteStrict(unsafe.Pointer(&key)); err != nil {
t.Fatal("Can't delete:", err)
}
}
func TestMapName(t *testing.T) {
m, err := NewMap(&MapSpec{
Name: "test",
Type: Array,
KeySize: 4,
ValueSize: 4,
MaxEntries: 1,
})
if err != nil {
t.Fatal(err)
}
defer m.Close()
info, err := bpfGetMapInfoByFD(m.fd)
if err != nil {
t.Fatal(err)
}
if name := convertCString(info.mapName[:]); name != "test" {
t.Error("Expected name to be test, got", name)
}
}
type benchValue struct {
ID uint32
Val16 uint16
Val16_2 uint16
Name [8]byte
LID uint64
}
type customBenchValue benchValue
func (cbv *customBenchValue) UnmarshalBinary(buf []byte) error {
cbv.ID = nativeEndian.Uint32(buf)
cbv.Val16 = nativeEndian.Uint16(buf[4:])
cbv.Val16_2 = nativeEndian.Uint16(buf[6:])
copy(cbv.Name[:], buf[8:])
cbv.LID = nativeEndian.Uint64(buf[16:])
return nil
}
func (cbv *customBenchValue) MarshalBinary() ([]byte, error) {
buf := make([]byte, 24)
nativeEndian.PutUint32(buf, cbv.ID)
nativeEndian.PutUint16(buf[4:], cbv.Val16)
nativeEndian.PutUint16(buf[6:], cbv.Val16_2)
copy(buf[8:], cbv.Name[:])
nativeEndian.PutUint64(buf[16:], cbv.LID)
return buf, nil
}
func BenchmarkMarshalling(b *testing.B) {
newMap := func(valueSize uint32) *Map {
m, err := NewMap(&MapSpec{
Type: Hash,
KeySize: 8,
ValueSize: valueSize,
MaxEntries: 1,
})
if err != nil {
b.Fatal(err)
}
return m
}
key := uint64(0)
m := newMap(24)
if err := m.Put(key, benchValue{}); err != nil {
b.Fatal(err)
}
b.Run("reflection", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var value benchValue
for i := 0; i < b.N; i++ {
ok, err := m.Get(unsafe.Pointer(&key), &value)
if !ok || err != nil {
b.Fatal("Can't get key:", ok, err)
}
}
})
b.Run("custom", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var value customBenchValue
for i := 0; i < b.N; i++ {
ok, err := m.Get(unsafe.Pointer(&key), &value)
if !ok || err != nil {
b.Fatal("Can't get key:", ok, err)
}
}
})
b.Run("unsafe", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var value benchValue
for i := 0; i < b.N; i++ {
ok, err := m.Get(unsafe.Pointer(&key), unsafe.Pointer(&value))
if !ok || err != nil {
b.Fatal("Can't get key:", ok, err)
}
}
})
}
// Per CPU maps store a distinct value for each CPU. They are useful
// to collect metrics.
func ExampleMap_perCPU() {
arr, err := NewMap(&MapSpec{
Type: PerCPUArray,
KeySize: 4,
ValueSize: 4,
MaxEntries: 2,
})
if err != nil {
panic(err)
}
first := []uint32{4, 5}
if err := arr.Put(uint32(0), first); err != nil {
panic(err)
}
second := []uint32{2, 8}
if err := arr.Put(uint32(1), second); err != nil {
panic(err)
}
var values []uint32
if ok, err := arr.Get(uint32(0), &values); err != nil {
panic(err)
} else if !ok {
panic("item 0 not found")
}
fmt.Println("First two values:", values[:2])
var (
key uint32
entries = arr.Iterate()
)
for entries.Next(&key, &values) {
// NB: sum can overflow, real code should check for this
var sum uint32
for _, n := range values {
sum += n
}
fmt.Printf("Sum of %d: %d\n", key, sum)
}
if err := entries.Err(); err != nil {
panic(err)
}
}
// It is possible to use unsafe.Pointer to avoid marshalling
// and copy overhead. It is the resposibility of the caller to ensure
// the correct size of unsafe.Pointers.
//
// Note that using unsafe.Pointer is only marginally faster than
// implementing Marshaler on the type.
func ExampleMap_zeroCopy() {
hash := createHash()
defer hash.Close()
key := [5]byte{'h', 'e', 'l', 'l', 'o'}
value := uint32(23)
if err := hash.Put(unsafe.Pointer(&key), unsafe.Pointer(&value)); err != nil {
panic(err)
}
value = 0
if ok, err := hash.Get(unsafe.Pointer(&key), unsafe.Pointer(&value)); !ok || err != nil {
panic("can't get value:" + err.Error())
}
fmt.Printf("The value is: %d\n", value)
// Output: The value is: 23
}
func createHash() *Map {
hash, err := NewMap(&MapSpec{
Type: Hash,
KeySize: 5,
ValueSize: 4,
MaxEntries: 10,
})
if err != nil {
panic(err)
}
return hash
}
func ExampleMap_NextKey() {
hash := createHash()
defer hash.Close()
if err := hash.Put("hello", uint32(21)); err != nil {
panic(err)
}
if err := hash.Put("world", uint32(42)); err != nil {
panic(err)
}
var firstKey string
if ok, err := hash.NextKey(nil, &firstKey); err != nil {
panic(err)
} else if !ok {
panic("map is empty")
}
var nextKey string
if ok, err := hash.NextKey(firstKey, &nextKey); err != nil {
panic(err)
} else if !ok {
panic("no keys after " + firstKey)
}
// Order of keys is non-deterministic due to randomized map seed
}
// ExampleMap_Iterate demonstrates how to iterate over all entries
// in a map.
func ExampleMap_Iterate() {
hash := createHash()
defer hash.Close()
if err := hash.Put("hello", uint32(21)); err != nil {
panic(err)
}
if err := hash.Put("world", uint32(42)); err != nil {
panic(err)
}
var (
key string
value uint32
entries = hash.Iterate()
)
for entries.Next(&key, &value) {
// Order of keys is non-deterministic due to randomized map seed
fmt.Printf("key: %s, value: %d\n", key, value)
}
if err := entries.Err(); err != nil {
panic(fmt.Sprint("Iterator encountered an error:", err))
}
}
// It is possible to iterate nested maps and program arrays by
// unmarshaling into a *Map or *Program.
func ExampleMap_Iterate_nestedMapsAndProgramArrays() {
var arrayOfMaps *Map // Set this up somehow
var (
key uint32
m *Map
entries = arrayOfMaps.Iterate()
)
// Make sure that the iterated map is closed after
// we are done.
defer m.Close()
for entries.Next(&key, &m) {
// Order of keys is non-deterministic due to randomized map seed
fmt.Printf("key: %v, map: %v\n", key, m)
}
if err := entries.Err(); err != nil {
panic(fmt.Sprint("Iterator encountered an error:", err))
}
}