-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmap_test.go
609 lines (527 loc) · 13.1 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
// Copyright (c) 2019 Dropbox, Inc.
// Full license can be found in the LICENSE file.
package itest
import (
"encoding/binary"
"os"
"testing"
"github.com/dropbox/goebpf"
"github.com/stretchr/testify/suite"
)
type mapTestSuite struct {
suite.Suite
}
// CRUD for BPF hash maps
func (ts *mapTestSuite) TestMapHash() {
// Create map
m := &goebpf.EbpfMap{
Type: goebpf.MapTypeHash,
KeySize: 8,
ValueSize: 8,
MaxEntries: 10,
}
err := m.Create()
ts.NoError(err)
// Insert few items into hash map
err = m.Insert("str1", "value1")
ts.NoError(err)
err = m.Insert("str123", "value2")
ts.NoError(err)
err = m.Insert("empty", "")
ts.NoError(err)
//use upsert to insert
err = m.Upsert("upsert1", "upvalue1")
ts.NoError(err)
err = m.Upsert("upsert2", "upvalue2")
ts.NoError(err)
// Lookup(generic) previously inserted item
bval, err := m.Lookup("str123")
ts.NoError(err)
ts.Equal([]byte("value2\x00\x00"), bval)
// Lookup(string)
sval, err := m.LookupString("str1")
ts.NoError(err)
ts.Equal("value1", sval)
sval, err = m.LookupString("empty")
ts.NoError(err)
ts.Equal("", sval)
// Lookup upserted items(string)
sval, err = m.LookupString("upsert1")
ts.NoError(err)
ts.Equal("upvalue1", sval)
sval, err = m.LookupString("upsert2")
ts.NoError(err)
ts.Equal("upvalue2", sval)
// Update item
err = m.Update("str123", "newval")
ts.NoError(err)
// Lookup again - to verify that value got updated
sval, err = m.LookupString("str123")
ts.NoError(err)
ts.Equal("newval", sval)
// update item using upsert
err = m.Upsert("upsert1", "newupval")
ts.NoError(err)
// Lookup again - to verify that value got updated
sval, err = m.LookupString("upsert1")
ts.NoError(err)
ts.Equal("newupval", sval)
// Delete item
err = m.Delete("str1")
ts.NoError(err)
// Lookup again - to be sure that element has been deleted
_, err = m.Lookup("str1")
ts.Error(err)
// Negative tests
// Insert already existing item
err = m.Insert("str123", "value2")
ts.Error(err)
// Update non-existing item
err = m.Update("dummy", "1")
ts.Error(err)
}
func (ts *mapTestSuite) TestMapArrayInt16() {
// Create map
m := &goebpf.EbpfMap{
Type: goebpf.MapTypeArray,
ValueSize: 2, // uint16
MaxEntries: 5,
}
err := m.Create()
ts.NoError(err)
// Array type doesn't support add/delete items, so
// update all items (by default all items are initialized to zero)
for i := 0; i < m.MaxEntries; i++ {
err := m.Update(i, i+1)
ts.NoError(err)
}
// Lookup all items - ensure that it works great :)
for i := 0; i < m.MaxEntries; i++ {
res, err := m.LookupInt(i)
ts.NoError(err)
ts.Equal(i+1, res)
}
// Some corner values
runs := []int{
0,
0xff,
256,
4095,
0xfff,
0xffff,
}
for _, r := range runs {
err := m.Update(0, r)
ts.NoError(err)
val, err := m.LookupInt(0)
ts.NoError(err)
ts.Equal(r, val)
}
// Negative: int too large
err = m.Update(0, 0xfffff)
ts.Error(err)
// Non existing item
_, err = m.LookupInt(100)
ts.Error(err)
}
func (ts *mapTestSuite) TestMapArrayInt() {
m := &goebpf.EbpfMap{
Type: goebpf.MapTypeArray,
ValueSize: 8,
MaxEntries: 5,
}
err := m.Create()
ts.NoError(err)
// Test some int values
runs := []int{
0,
-1,
-10000000000,
0x7fffffffffffffff,
0xff,
256,
4095,
0xfff,
0xffff,
}
for _, r := range runs {
err := m.Update(0, r)
ts.NoError(err)
val, err := m.LookupInt(0)
ts.NoError(err)
ts.Equal(r, val)
}
}
// Array type doesn't support add/delete items, only update
func (ts *mapTestSuite) TestMapArrayUInt64() {
m := &goebpf.EbpfMap{
Type: goebpf.MapTypeArray,
KeySize: 4,
ValueSize: 8,
MaxEntries: 5,
}
err := m.Create()
ts.NoError(err)
// Update all items (By default all items are initialized to zero)
start := uint64(1 << 61) // 0xFFFFFFFFFFFFFFF8 (3 bits remaining)
for i := 0; i < m.MaxEntries; i++ {
err := m.Update(i, start+uint64(i))
ts.NoError(err)
}
// Lookup all items - ensure that it works great :)
for i := 0; i < m.MaxEntries; i++ {
res, err := m.LookupUint64(i)
ts.NoError(err)
ts.Equal(start+uint64(i), res)
}
}
// Long Prefix Match Trie test
func (ts *mapTestSuite) TestMapLPMTrieIPv4() {
// Create map
m := &goebpf.EbpfMap{
Type: goebpf.MapTypeLPMTrie,
KeySize: 8, // prefix size + ipv4
ValueSize: 8,
MaxEntries: 10,
}
err := m.Create()
ts.NoError(err)
// Insert few subnets
err = m.Insert(goebpf.CreateLPMtrieKey("192.168.0.0/16"), "value16")
ts.NoError(err)
err = m.Insert(goebpf.CreateLPMtrieKey("192.168.0.0/24"), "value24")
ts.NoError(err)
// Perform lookup (it is usually done from XDP program - here is only for integration tests)
val1, err := m.LookupString(goebpf.CreateLPMtrieKey("192.168.0.10"))
ts.NoError(err)
ts.Equal("value24", val1)
// Negative lookup: IP doesn't belong to any range
_, err = m.LookupString(goebpf.CreateLPMtrieKey("10.10.10.10"))
ts.Error(err)
// "Default route" :)
err = m.Insert(goebpf.CreateLPMtrieKey("0.0.0.0/0"), "value0")
ts.NoError(err)
// Lookup any IP
val2, err := m.LookupString(goebpf.CreateLPMtrieKey("222.222.222.222"))
ts.NoError(err)
ts.Equal("value0", val2)
}
func (ts *mapTestSuite) TestMapLPMTrieIPv6() {
// Create map
m := &goebpf.EbpfMap{
Type: goebpf.MapTypeLPMTrie,
KeySize: 20, // prefix len + ipv6
ValueSize: 8,
MaxEntries: 10,
}
err := m.Create()
ts.NoError(err)
// Insert few subnets
err = m.Insert(goebpf.CreateLPMtrieKey("fafa::fbf8/120"), "value120")
ts.NoError(err)
err = m.Insert(goebpf.CreateLPMtrieKey("fafa::fbf8/125"), "val125")
ts.NoError(err)
// Perform lookup (it is usually done from XDP program - here is only for integration tests)
val1, err := m.LookupString(goebpf.CreateLPMtrieKey("fafa::fbfa"))
ts.NoError(err)
ts.Equal("val125", val1)
// Negative lookup: IP doesn't belong to any range
_, err = m.LookupString(goebpf.CreateLPMtrieKey("1afa::fbfa"))
ts.Error(err)
// "Default route" :)
err = m.Insert(goebpf.CreateLPMtrieKey("::/0"), "value0")
ts.NoError(err)
// Lookup any IP
val2, err := m.LookupString(goebpf.CreateLPMtrieKey("1111::1111"))
ts.NoError(err)
ts.Equal("value0", val2)
}
func (ts *mapTestSuite) TestArrayOfMaps() {
// Inner map template
templ := goebpf.EbpfMap{
Type: goebpf.MapTypeArray,
KeySize: 4,
ValueSize: 4,
MaxEntries: 10,
}
m := templ
err := m.Create()
ts.NoError(err)
// Create special map which contains other maps as value
mm := &goebpf.EbpfMap{
Type: goebpf.MapTypeArrayOfMaps,
MaxEntries: 10,
InnerMapFd: m.GetFd(),
}
err = mm.Create()
ts.NoError(err)
// Create few inner maps - and insert them into array of maps (m)
for i := 0; i < 5; i++ {
m1 := templ
err = m1.Create()
ts.NoError(err)
// Insert it into outer map (main map)
err = mm.Update(i, m1.GetFd())
ts.NoError(err)
}
}
func (ts *mapTestSuite) TestHashOfMaps() {
// Inner map template
templ := &goebpf.EbpfMap{
Type: goebpf.MapTypeArray,
KeySize: 4,
ValueSize: 4,
MaxEntries: 10,
}
m := templ
err := m.Create()
ts.NoError(err)
// Create special map which contains other maps as value
mm := &goebpf.EbpfMap{
Type: goebpf.MapTypeHashOfMaps,
KeySize: 4,
MaxEntries: 10,
InnerMapFd: m.GetFd(),
}
err = mm.Create()
ts.NoError(err)
// Create few inner maps - and insert them into hash of maps (m)
for i := 0; i < 5; i++ {
m1 := templ
err = m.Create()
ts.NoError(err)
// Insert it into outer hash map (main map)
err = mm.Insert(i, m1.GetFd())
ts.NoError(err)
}
}
// Array of bpf programs.
// Since we don't have BPF programs loaded - we'll simply try to create this map.
// Additional tests are in xdp_test.go
func (ts *mapTestSuite) TestMapProgArray() {
// Create map
m := &goebpf.EbpfMap{
Type: goebpf.MapTypeProgArray,
MaxEntries: 5,
}
err := m.Create()
ts.NoError(err)
}
// Persistent map test
func (ts *mapTestSuite) TestMapPersistent() {
// Create system wide map (i.e. regular create with object pinning to given path)
cnt := 5
path := bpfPath + "/test"
m1 := &goebpf.EbpfMap{
Type: goebpf.MapTypeArray,
ValueSize: 4,
MaxEntries: cnt,
PersistentPath: path,
}
err := m1.Create()
ts.NoError(err)
// Ensure that special file in BPF fs present
ts.FileExists(path)
// Fill map with some values
for i := 0; i < cnt; i++ {
m1.Update(i, i)
}
// Since last map is declared as system wide and present at given location
// it is possible to "create" one more the same map points to the same location.
// Map.Create() will not create new map, it will call bpf_obj_get() instead.
m2 := m1.CloneTemplate()
m2.Create()
// Lookup items (they've been inserted before in m1)
for i := 0; i < cnt; i++ {
res, err := m2.LookupInt(i)
ts.NoError(err)
ts.Equal(i, res)
}
// Unlink file
err = os.Remove(path)
ts.NoError(err)
// Close all maps
err = m1.Close()
ts.NoError(err)
err = m2.Close()
ts.NoError(err)
}
// Double close negative test
func (ts *mapTestSuite) TestMapDoubleClose() {
m := &goebpf.EbpfMap{
Type: goebpf.MapTypeArray,
ValueSize: 4,
MaxEntries: 4,
}
// Try to close non-created map
err := m.Close()
ts.Error(err)
err = m.Create()
ts.NoError(err)
// Finally close it twice
err = m.Close()
ts.NoError(err)
err = m.Close()
ts.Error(err)
}
func (ts *mapTestSuite) TestMapFromExistingByFd() {
// Create map which will be used to create new from
m1 := &goebpf.EbpfMap{
Name: "test",
Type: goebpf.MapTypeArray,
ValueSize: 4,
MaxEntries: 4,
}
err := m1.Create()
ts.NoError(err)
// Create map structure from already existing map in kernel
// by its FD (not ID! since this is the same process)
m2, err := goebpf.NewMapFromExistingMapByFd(m1.GetFd())
ts.NoError(err)
ts.Equal(m1.GetFd(), m2.GetFd())
// Create() should not re-create map, just re-use existing fd
m2.Create()
ts.Equal(m1, m2)
}
func (ts *mapTestSuite) TestGetNextKeyString() {
// Create map
m := &goebpf.EbpfMap{
Type: goebpf.MapTypeHash,
KeySize: 8,
ValueSize: 8,
MaxEntries: 10,
}
err := m.Create()
ts.NoError(err)
mapData := map[string]string{
"key1": "val1",
"key2": "val2",
"key3": "val3",
"": "val4",
}
result := map[string]string{}
// Insert items into hash map
for key, value := range mapData {
err = m.Insert(key, value)
ts.NoError(err)
}
currentKey, err := m.GetNextKeyString(nil)
ts.NoError(err)
for {
val, err := m.LookupString(currentKey)
ts.NoError(err)
ts.Equal(mapData[currentKey], string(val))
result[currentKey] = val
nextKey, err := m.GetNextKeyString(currentKey)
if err != nil {
break
}
currentKey = nextKey
}
ts.Equal(mapData, result)
}
func (ts *mapTestSuite) TestGetNextKeyInt() {
// Create map
m := &goebpf.EbpfMap{
Type: goebpf.MapTypeHash,
KeySize: 8,
ValueSize: 8,
MaxEntries: 10,
}
err := m.Create()
ts.NoError(err)
mapData := map[int]int{
1234: 4321,
5678: 8765,
9012: 2109,
0: 1234,
}
result := map[int]int{}
// Insert items into hash map
for key, value := range mapData {
err = m.Insert(key, value)
ts.NoError(err)
}
currentKey, err := m.GetNextKeyInt(nil)
ts.NoError(err)
for {
val, err := m.LookupInt(currentKey)
ts.NoError(err)
ts.Equal(mapData[currentKey], int(val))
result[currentKey] = val
nextKey, err := m.GetNextKeyInt(currentKey)
if err != nil {
break
}
currentKey = nextKey
}
ts.Equal(mapData, result)
}
func (ts *mapTestSuite) TestMapFromExistingByPath() {
path := bpfPath + "/test"
// Make sure we're not loading an old map
os.Remove(path)
ts.NoFileExists(path)
// Create map which will be used to create new from
m1 := &goebpf.EbpfMap{
Name: "test",
Type: goebpf.MapTypeArray,
ValueSize: 4,
MaxEntries: 4,
PersistentPath: path,
}
err := m1.Create()
ts.NoError(err)
// Create map structure from already existing map in kernel by its path
m2, err := goebpf.NewMapFromExistingMapByPath(m1.PersistentPath)
ts.NoError(err)
// Insert to the original map object
err = m1.Update(2, 1337)
ts.NoError(err)
// Lookup from the newly opened map object
res, err := m2.LookupInt(2)
ts.NoError(err)
ts.Equal(1337, res)
// The map should be the same except for the fd
ts.Equal(m1.Name, m2.Name)
ts.Equal(m1.Type, m2.Type)
ts.Equal(m1.ValueSize, m2.ValueSize)
ts.Equal(m1.MaxEntries, m2.MaxEntries)
ts.Equal(m1.MaxEntries, m2.MaxEntries)
ts.Equal(m1.PersistentPath, m2.PersistentPath)
os.Remove(path)
}
func (ts *mapTestSuite) TestMapPerCpuValues() {
// Create a map with percpu values
m := &goebpf.EbpfMap{
Name: "test",
Type: goebpf.MapTypePerCPUHash,
KeySize: 4,
ValueSize: 4,
MaxEntries: 16,
}
err := m.Create()
ts.NoError(err)
// Prepare values for insert
numCpus, err := goebpf.GetNumOfPossibleCpus()
ts.NoError(err)
// The values need to be padded to 8 bytes
val := make([]byte, 8*numCpus)
for i := 0; i < numCpus; i++ {
binary.LittleEndian.PutUint32(val[i*8:i*8+4], uint32(i))
}
// Insert the values
err = m.Insert("str1", val)
ts.NoError(err)
// Lookup the inserted values
bval, err := m.Lookup("str1")
ts.NoError(err)
for i := 0; i < numCpus; i++ {
v := binary.LittleEndian.Uint32(bval[i*8 : i*8+4])
ts.EqualValues(i, v)
}
}
// Run suite
func TestMapSuite(t *testing.T) {
suite.Run(t, new(mapTestSuite))
}