-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmap_lpm_trie_test.go
362 lines (313 loc) · 7.19 KB
/
map_lpm_trie_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
//go:build bpftests
// +build bpftests
package gobpfld
import (
"fmt"
"math/rand"
"net"
"reflect"
"testing"
"unsafe"
"github.com/dylandreimerink/gobpfld/bpfsys"
"github.com/dylandreimerink/gobpfld/bpftypes"
"github.com/dylandreimerink/gobpfld/kernelsupport"
)
// TestLPMTrieMapHappyPath executes property based happy path testing, comparing the LPM map to a golang map, assuming the go map is always
// correct.
func TestLPMTrieMapHappyPath(t *testing.T) {
if !kernelsupport.CurrentFeatures.Map.Has(kernelsupport.KFeatMapLPMTrie) {
t.Skip("LPM tree not supported by current kernel version")
}
if !kernelsupport.CurrentFeatures.Map.Has(kernelsupport.KFeatMapLPMTrieNextKey) {
t.Skip("LPM tree iteration not supported by current kernel version")
}
const maxEntries = 128
m := &LPMTrieMap{
AbstractMap: AbstractMap{
Name: MustNewObjName("lpmtest"),
Definition: BPFMapDef{
Type: bpftypes.BPF_MAP_TYPE_LPM_TRIE,
KeySize: uint32(unsafe.Sizeof(LPMTrieIPv4Key{})),
ValueSize: 8,
MaxEntries: maxEntries + 1,
Flags: bpftypes.BPFMapFlagsNoPreAlloc,
},
},
}
err := m.Load()
if err != nil {
t.Fatal(err)
}
defer m.Close()
verificationMap := make(map[LPMTrieIPv4Key]uint64)
randKey := func() *LPMTrieIPv4Key {
_, cidr, err := net.ParseCIDR(fmt.Sprintf(
"%d.%d.%d.%d/%d",
rand.Intn(255),
rand.Intn(255),
rand.Intn(255),
rand.Intn(255),
rand.Intn(32),
))
if err != nil {
t.Fatal(err)
}
// If the key already exists, generate another one
return LPMKeyFromNetwork(*cidr).(*LPMTrieIPv4Key)
}
// Fill the verification map
for i := 0; i < maxEntries; i++ {
key := randKey()
if _, ok := verificationMap[*key]; ok {
i--
continue
}
verificationMap[*key] = rand.Uint64()
}
// Transfer contents to BPF map
for k, v := range verificationMap {
// Copy the key, we can't pass a pointer to a temp variable
kCopy := k
vCopy := v
err = m.Set(&kCopy, &vCopy, bpfsys.BPFMapElemAny)
if err != nil {
t.Fatal(err)
}
}
// Perform less permutations when the -short flag is passed
limit := 1000
if testing.Short() {
limit = 100
}
// Fail if the maps don't match
verify := func() {
read := make(map[LPMTrieIPv4Key]struct{}, len(verificationMap))
for k := range verificationMap {
read[k] = struct{}{}
}
var (
k LPMTrieIPv4Key
v uint64
)
var iter MapIterator = &singleLookupIterator{BPFMap: m}
// If batch ops in LPM maps are supported, use a batch iterator half of the time
if kernelsupport.CurrentFeatures.Map.Has(kernelsupport.KFeatMapLPMTrieBatchOps) && rand.Int()%2 == 0 {
iter = &batchLookupIterator{BPFMap: m}
}
err = iter.Init(&k, &v)
if err != nil {
t.Fatal(err)
}
for {
updated, err := iter.Next()
if err != nil {
t.Fatal(err)
}
if !updated {
break
}
verifyValue, ok := verificationMap[k]
if !ok {
t.Fatal("key in map doesn't exist in verification map")
}
if verifyValue != v {
t.Fatal("value doesn't match")
}
delete(read, k)
}
if err != nil {
t.Fatal(err)
}
if len(read) > 0 {
t.Fatalf("no all keys were read (%d/%d)", len(read), len(verificationMap))
}
}
// Verify initial state
verify()
// Return a random key from the verification map
randValKey := func() LPMTrieIPv4Key {
kIndex := rand.Intn(len(verificationMap) - 1)
ii := 0
for k := range verificationMap {
if ii == kIndex {
return k
}
ii++
}
t.Fatal("should not be possible")
return LPMTrieIPv4Key{}
}
// Update, delete, and add single keys and values
for i := 0; i < limit; i++ {
switch i % 3 {
case 0:
// Update
newVal := rand.Uint64()
k := randValKey()
verificationMap[k] = newVal
err := m.Set(&k, &newVal, bpfsys.BPFMapElemExists)
if err != nil {
t.Fatal(err)
}
case 1:
// Delete
k := randValKey()
delete(verificationMap, k)
err := m.Delete(&k)
if err != nil {
t.Fatal(err)
}
case 2:
// Add
newVal := rand.Uint64()
k := randKey()
verificationMap[*k] = newVal
err := m.Set(k, &newVal, bpfsys.BPFMapElemNoExists)
if err != nil {
t.Fatal(err)
}
}
verify()
}
// Can't execute the rest of the test if the current kernel doesn't support bulk ops
if !kernelsupport.CurrentFeatures.Map.Has(kernelsupport.KFeatMapLPMTrieBatchOps) {
return
}
// Update, delete, and add 10 keys and values at a time
const batchSize = 10
for i := 0; i < limit; i++ {
keys := make([]LPMTrieIPv4Key, batchSize)
values := make([]uint64, batchSize)
switch i % 3 {
case 0:
// Update
// Get random set of existing keys
for j := 0; j < batchSize; j++ {
randKey := randValKey()
exists := false
for _, v := range keys {
if v == randKey {
exists = true
}
}
if exists {
j--
continue
}
keys[j] = randKey
}
for j := 0; j < batchSize; j++ {
// Gen new value
values[j] = rand.Uint64()
// Update value in verification map
verificationMap[keys[j]] = values[j]
}
_, err := m.SetBatch(&keys, &values, bpfsys.BPFMapElemExists, batchSize)
if err != nil {
t.Fatal(err)
}
case 1:
// Delete
// Get random set of existing keys
for j := 0; j < batchSize; j++ {
randKey := randValKey()
exists := false
for _, v := range keys {
if v == randKey {
exists = true
}
}
if exists {
j--
continue
}
keys[j] = randKey
delete(verificationMap, randKey)
}
_, err := m.DeleteBatch(&keys, batchSize)
if err != nil {
t.Fatal(err)
}
case 2:
// Add
// Get random set of existing keys
for j := 0; j < batchSize; j++ {
randKey := *randKey()
exists := false
for _, v := range keys {
if v == randKey {
exists = true
}
}
if exists {
j--
continue
}
keys[j] = randKey
}
for j := 0; j < batchSize; j++ {
// Gen new value
values[j] = rand.Uint64()
// Update value in verification map
verificationMap[keys[j]] = values[j]
}
_, err := m.SetBatch(&keys, &values, bpfsys.BPFMapElemNoExists, batchSize)
if err != nil {
t.Fatal(err)
}
}
verify()
}
}
func TestLPMKeyFromNetwork(t *testing.T) {
cases := []struct {
Name string
CIDR string
Expected LPMTrieKey
}{
{
Name: "IPv4 happy path",
CIDR: "127.0.0.1/32",
Expected: &LPMTrieIPv4Key{
Address: [4]byte{127, 0, 0, 1},
Prefix: 32,
},
},
{
Name: "IPv4 happy path 2",
CIDR: "192.168.12.0/24",
Expected: &LPMTrieIPv4Key{
Address: [4]byte{192, 168, 12, 0},
Prefix: 24,
},
},
{
Name: "IPv6 happy path",
CIDR: "::1/128",
Expected: &LPMTrieIPv6Key{
Address: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
Prefix: 128,
},
},
{
Name: "IPv6 happy path 2",
CIDR: "::192.168.12.0/120",
Expected: &LPMTrieIPv6Key{
Address: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 168, 12, 0},
Prefix: 120,
},
},
}
for _, testCase := range cases {
t.Run(t.Name()+"_"+testCase.Name, func(tt *testing.T) {
_, ipNet, err := net.ParseCIDR(testCase.CIDR)
if err != nil {
tt.Fatal(err)
}
key := LPMKeyFromNetwork(*ipNet)
if !reflect.DeepEqual(key, testCase.Expected) {
tt.Fatal("keys not equal")
}
})
}
}