1 // SPDX-License-Identifier: GPL-2.0
3 * Randomized tests for eBPF longest-prefix-match maps
5 * This program runs randomized tests against the lpm-bpf-map. It implements a
6 * "Trivial Longest Prefix Match" (tlpm) based on simple, linear, singly linked
7 * lists. The implementation should be pretty straightforward.
9 * Based on tlpm, this inserts randomized data into bpf-lpm-maps and verifies
10 * the trie-based bpf-map implementation behaves the same way as tlpm.
16 #include <linux/bpf.h>
23 #include <arpa/inet.h>
29 #include "bpf_rlimit.h"
32 struct tlpm_node *next;
37 static struct tlpm_node *tlpm_match(struct tlpm_node *list,
41 static struct tlpm_node *tlpm_add(struct tlpm_node *list,
45 struct tlpm_node *node;
50 /* 'overwrite' an equivalent entry if one already exists */
51 node = tlpm_match(list, key, n_bits);
52 if (node && node->n_bits == n_bits) {
53 memcpy(node->key, key, n);
57 /* add new entry with @key/@n_bits to @list and return new head */
59 node = malloc(sizeof(*node) + n);
63 node->n_bits = n_bits;
64 memcpy(node->key, key, n);
69 static void tlpm_clear(struct tlpm_node *list)
71 struct tlpm_node *node;
73 /* free all entries in @list */
75 while ((node = list)) {
81 static struct tlpm_node *tlpm_match(struct tlpm_node *list,
85 struct tlpm_node *best = NULL;
88 /* Perform longest prefix-match on @key/@n_bits. That is, iterate all
89 * entries and match each prefix against @key. Remember the "best"
90 * entry we find (i.e., the longest prefix that matches) and return it
91 * to the caller when done.
94 for ( ; list; list = list->next) {
95 for (i = 0; i < n_bits && i < list->n_bits; ++i) {
96 if ((key[i / 8] & (1 << (7 - i % 8))) !=
97 (list->key[i / 8] & (1 << (7 - i % 8))))
101 if (i >= list->n_bits) {
102 if (!best || i > best->n_bits)
110 static struct tlpm_node *tlpm_delete(struct tlpm_node *list,
114 struct tlpm_node *best = tlpm_match(list, key, n_bits);
115 struct tlpm_node *node;
117 if (!best || best->n_bits != n_bits)
126 for (node = list; node; node = node->next) {
127 if (node->next == best) {
128 node->next = best->next;
133 /* should never get here */
138 static void test_lpm_basic(void)
140 struct tlpm_node *list = NULL, *t1, *t2;
142 /* very basic, static tests to verify tlpm works as expected */
144 assert(!tlpm_match(list, (uint8_t[]){ 0xff }, 8));
146 t1 = list = tlpm_add(list, (uint8_t[]){ 0xff }, 8);
147 assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff }, 8));
148 assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff, 0xff }, 16));
149 assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff, 0x00 }, 16));
150 assert(!tlpm_match(list, (uint8_t[]){ 0x7f }, 8));
151 assert(!tlpm_match(list, (uint8_t[]){ 0xfe }, 8));
152 assert(!tlpm_match(list, (uint8_t[]){ 0xff }, 7));
154 t2 = list = tlpm_add(list, (uint8_t[]){ 0xff, 0xff }, 16);
155 assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff }, 8));
156 assert(t2 == tlpm_match(list, (uint8_t[]){ 0xff, 0xff }, 16));
157 assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff, 0xff }, 15));
158 assert(!tlpm_match(list, (uint8_t[]){ 0x7f, 0xff }, 16));
160 list = tlpm_delete(list, (uint8_t[]){ 0xff, 0xff }, 16);
161 assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff }, 8));
162 assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff, 0xff }, 16));
164 list = tlpm_delete(list, (uint8_t[]){ 0xff }, 8);
165 assert(!tlpm_match(list, (uint8_t[]){ 0xff }, 8));
170 static void test_lpm_order(void)
172 struct tlpm_node *t1, *t2, *l1 = NULL, *l2 = NULL;
175 /* Verify the tlpm implementation works correctly regardless of the
176 * order of entries. Insert a random set of entries into @l1, and copy
177 * the same data in reverse order into @l2. Then verify a lookup of
178 * random keys will yield the same result in both sets.
181 for (i = 0; i < (1 << 12); ++i)
182 l1 = tlpm_add(l1, (uint8_t[]){
187 for (t1 = l1; t1; t1 = t1->next)
188 l2 = tlpm_add(l2, t1->key, t1->n_bits);
190 for (i = 0; i < (1 << 8); ++i) {
191 uint8_t key[] = { rand() % 0xff, rand() % 0xff };
193 t1 = tlpm_match(l1, key, 16);
194 t2 = tlpm_match(l2, key, 16);
198 assert(t1->n_bits == t2->n_bits);
199 for (j = 0; j < t1->n_bits; ++j)
200 assert((t1->key[j / 8] & (1 << (7 - j % 8))) ==
201 (t2->key[j / 8] & (1 << (7 - j % 8))));
209 static void test_lpm_map(int keysize)
211 LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
212 volatile size_t n_matches, n_matches_after_delete;
213 size_t i, j, n_nodes, n_lookups;
214 struct tlpm_node *t, *list = NULL;
215 struct bpf_lpm_trie_key *key;
216 uint8_t *data, *value;
219 /* Compare behavior of tlpm vs. bpf-lpm. Create a randomized set of
220 * prefixes and insert it into both tlpm and bpf-lpm. Then run some
221 * randomized lookups and verify both maps return the same result.
225 n_matches_after_delete = 0;
229 data = alloca(keysize);
230 memset(data, 0, keysize);
232 value = alloca(keysize + 1);
233 memset(value, 0, keysize + 1);
235 key = alloca(sizeof(*key) + keysize);
236 memset(key, 0, sizeof(*key) + keysize);
238 map = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL,
239 sizeof(*key) + keysize,
245 for (i = 0; i < n_nodes; ++i) {
246 for (j = 0; j < keysize; ++j)
247 value[j] = rand() & 0xff;
248 value[keysize] = rand() % (8 * keysize + 1);
250 list = tlpm_add(list, value, value[keysize]);
252 key->prefixlen = value[keysize];
253 memcpy(key->data, value, keysize);
254 r = bpf_map_update_elem(map, key, value, 0);
258 for (i = 0; i < n_lookups; ++i) {
259 for (j = 0; j < keysize; ++j)
260 data[j] = rand() & 0xff;
262 t = tlpm_match(list, data, 8 * keysize);
264 key->prefixlen = 8 * keysize;
265 memcpy(key->data, data, keysize);
266 r = bpf_map_lookup_elem(map, key, value);
267 assert(!r || errno == ENOENT);
272 assert(t->n_bits == value[keysize]);
273 for (j = 0; j < t->n_bits; ++j)
274 assert((t->key[j / 8] & (1 << (7 - j % 8))) ==
275 (value[j / 8] & (1 << (7 - j % 8))));
279 /* Remove the first half of the elements in the tlpm and the
280 * corresponding nodes from the bpf-lpm. Then run the same
281 * large number of random lookups in both and make sure they match.
282 * Note: we need to count the number of nodes actually inserted
283 * since there may have been duplicates.
285 for (i = 0, t = list; t; i++, t = t->next)
287 for (j = 0; j < i / 2; ++j) {
288 key->prefixlen = list->n_bits;
289 memcpy(key->data, list->key, keysize);
290 r = bpf_map_delete_elem(map, key);
292 list = tlpm_delete(list, list->key, list->n_bits);
295 for (i = 0; i < n_lookups; ++i) {
296 for (j = 0; j < keysize; ++j)
297 data[j] = rand() & 0xff;
299 t = tlpm_match(list, data, 8 * keysize);
301 key->prefixlen = 8 * keysize;
302 memcpy(key->data, data, keysize);
303 r = bpf_map_lookup_elem(map, key, value);
304 assert(!r || errno == ENOENT);
308 ++n_matches_after_delete;
309 assert(t->n_bits == value[keysize]);
310 for (j = 0; j < t->n_bits; ++j)
311 assert((t->key[j / 8] & (1 << (7 - j % 8))) ==
312 (value[j / 8] & (1 << (7 - j % 8))));
319 /* With 255 random nodes in the map, we are pretty likely to match
320 * something on every lookup. For statistics, use this:
322 * printf(" nodes: %zu\n"
325 * "matches(delete): %zu\n",
326 * n_nodes, n_lookups, n_matches, n_matches_after_delete);
330 /* Test the implementation with some 'real world' examples */
332 static void test_lpm_ipaddr(void)
334 LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
335 struct bpf_lpm_trie_key *key_ipv4;
336 struct bpf_lpm_trie_key *key_ipv6;
337 size_t key_size_ipv4;
338 size_t key_size_ipv6;
343 key_size_ipv4 = sizeof(*key_ipv4) + sizeof(__u32);
344 key_size_ipv6 = sizeof(*key_ipv6) + sizeof(__u32) * 4;
345 key_ipv4 = alloca(key_size_ipv4);
346 key_ipv6 = alloca(key_size_ipv6);
348 map_fd_ipv4 = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL,
349 key_size_ipv4, sizeof(value),
351 assert(map_fd_ipv4 >= 0);
353 map_fd_ipv6 = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL,
354 key_size_ipv6, sizeof(value),
356 assert(map_fd_ipv6 >= 0);
358 /* Fill data some IPv4 and IPv6 address ranges */
360 key_ipv4->prefixlen = 16;
361 inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
362 assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
365 key_ipv4->prefixlen = 24;
366 inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
367 assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
370 key_ipv4->prefixlen = 24;
371 inet_pton(AF_INET, "192.168.128.0", key_ipv4->data);
372 assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
375 key_ipv4->prefixlen = 24;
376 inet_pton(AF_INET, "192.168.1.0", key_ipv4->data);
377 assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
380 key_ipv4->prefixlen = 23;
381 inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
382 assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
385 key_ipv6->prefixlen = 64;
386 inet_pton(AF_INET6, "2a00:1450:4001:814::200e", key_ipv6->data);
387 assert(bpf_map_update_elem(map_fd_ipv6, key_ipv6, &value, 0) == 0);
389 /* Set tprefixlen to maximum for lookups */
390 key_ipv4->prefixlen = 32;
391 key_ipv6->prefixlen = 128;
393 /* Test some lookups that should come back with a value */
394 inet_pton(AF_INET, "192.168.128.23", key_ipv4->data);
395 assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == 0);
398 inet_pton(AF_INET, "192.168.0.1", key_ipv4->data);
399 assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == 0);
402 inet_pton(AF_INET6, "2a00:1450:4001:814::", key_ipv6->data);
403 assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == 0);
404 assert(value == 0xdeadbeef);
406 inet_pton(AF_INET6, "2a00:1450:4001:814::1", key_ipv6->data);
407 assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == 0);
408 assert(value == 0xdeadbeef);
410 /* Test some lookups that should not match any entry */
411 inet_pton(AF_INET, "10.0.0.1", key_ipv4->data);
412 assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == -1 &&
415 inet_pton(AF_INET, "11.11.11.11", key_ipv4->data);
416 assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == -1 &&
419 inet_pton(AF_INET6, "2a00:ffff::", key_ipv6->data);
420 assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == -1 &&
427 static void test_lpm_delete(void)
429 LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
430 struct bpf_lpm_trie_key *key;
435 key_size = sizeof(*key) + sizeof(__u32);
436 key = alloca(key_size);
438 map_fd = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL,
439 key_size, sizeof(value),
446 * 192.168.128.0/24 (3)
457 inet_pton(AF_INET, "192.168.0.0", key->data);
458 assert(bpf_map_update_elem(map_fd, key, &value, 0) == 0);
462 inet_pton(AF_INET, "192.168.0.0", key->data);
463 assert(bpf_map_update_elem(map_fd, key, &value, 0) == 0);
467 inet_pton(AF_INET, "192.168.128.0", key->data);
468 assert(bpf_map_update_elem(map_fd, key, &value, 0) == 0);
472 inet_pton(AF_INET, "192.168.1.0", key->data);
473 assert(bpf_map_update_elem(map_fd, key, &value, 0) == 0);
475 /* remove non-existent node */
477 inet_pton(AF_INET, "10.0.0.1", key->data);
478 assert(bpf_map_lookup_elem(map_fd, key, &value) == -1 &&
481 key->prefixlen = 30; // unused prefix so far
482 inet_pton(AF_INET, "192.255.0.0", key->data);
483 assert(bpf_map_delete_elem(map_fd, key) == -1 &&
486 key->prefixlen = 16; // same prefix as the root node
487 inet_pton(AF_INET, "192.255.0.0", key->data);
488 assert(bpf_map_delete_elem(map_fd, key) == -1 &&
491 /* assert initial lookup */
493 inet_pton(AF_INET, "192.168.0.1", key->data);
494 assert(bpf_map_lookup_elem(map_fd, key, &value) == 0);
497 /* remove leaf node */
499 inet_pton(AF_INET, "192.168.0.0", key->data);
500 assert(bpf_map_delete_elem(map_fd, key) == 0);
503 inet_pton(AF_INET, "192.168.0.1", key->data);
504 assert(bpf_map_lookup_elem(map_fd, key, &value) == 0);
507 /* remove leaf (and intermediary) node */
509 inet_pton(AF_INET, "192.168.1.0", key->data);
510 assert(bpf_map_delete_elem(map_fd, key) == 0);
513 inet_pton(AF_INET, "192.168.1.1", key->data);
514 assert(bpf_map_lookup_elem(map_fd, key, &value) == 0);
517 /* remove root node */
519 inet_pton(AF_INET, "192.168.0.0", key->data);
520 assert(bpf_map_delete_elem(map_fd, key) == 0);
523 inet_pton(AF_INET, "192.168.128.1", key->data);
524 assert(bpf_map_lookup_elem(map_fd, key, &value) == 0);
527 /* remove last node */
529 inet_pton(AF_INET, "192.168.128.0", key->data);
530 assert(bpf_map_delete_elem(map_fd, key) == 0);
533 inet_pton(AF_INET, "192.168.128.1", key->data);
534 assert(bpf_map_lookup_elem(map_fd, key, &value) == -1 &&
540 static void test_lpm_get_next_key(void)
542 LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
543 struct bpf_lpm_trie_key *key_p, *next_key_p;
548 key_size = sizeof(*key_p) + sizeof(__u32);
549 key_p = alloca(key_size);
550 next_key_p = alloca(key_size);
552 map_fd = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL, key_size, sizeof(value), 100, &opts);
555 /* empty tree. get_next_key should return ENOENT */
556 assert(bpf_map_get_next_key(map_fd, NULL, key_p) == -1 &&
559 /* get and verify the first key, get the second one should fail. */
560 key_p->prefixlen = 16;
561 inet_pton(AF_INET, "192.168.0.0", key_p->data);
562 assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
564 memset(key_p, 0, key_size);
565 assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
566 assert(key_p->prefixlen == 16 && key_p->data[0] == 192 &&
567 key_p->data[1] == 168);
569 assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
572 /* no exact matching key should get the first one in post order. */
573 key_p->prefixlen = 8;
574 assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
575 assert(key_p->prefixlen == 16 && key_p->data[0] == 192 &&
576 key_p->data[1] == 168);
578 /* add one more element (total two) */
579 key_p->prefixlen = 24;
580 inet_pton(AF_INET, "192.168.128.0", key_p->data);
581 assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
583 memset(key_p, 0, key_size);
584 assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
585 assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
586 key_p->data[1] == 168 && key_p->data[2] == 128);
588 memset(next_key_p, 0, key_size);
589 assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
590 assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
591 next_key_p->data[1] == 168);
593 memcpy(key_p, next_key_p, key_size);
594 assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
597 /* Add one more element (total three) */
598 key_p->prefixlen = 24;
599 inet_pton(AF_INET, "192.168.0.0", key_p->data);
600 assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
602 memset(key_p, 0, key_size);
603 assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
604 assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
605 key_p->data[1] == 168 && key_p->data[2] == 0);
607 memset(next_key_p, 0, key_size);
608 assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
609 assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
610 next_key_p->data[1] == 168 && next_key_p->data[2] == 128);
612 memcpy(key_p, next_key_p, key_size);
613 assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
614 assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
615 next_key_p->data[1] == 168);
617 memcpy(key_p, next_key_p, key_size);
618 assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
621 /* Add one more element (total four) */
622 key_p->prefixlen = 24;
623 inet_pton(AF_INET, "192.168.1.0", key_p->data);
624 assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
626 memset(key_p, 0, key_size);
627 assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
628 assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
629 key_p->data[1] == 168 && key_p->data[2] == 0);
631 memset(next_key_p, 0, key_size);
632 assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
633 assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
634 next_key_p->data[1] == 168 && next_key_p->data[2] == 1);
636 memcpy(key_p, next_key_p, key_size);
637 assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
638 assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
639 next_key_p->data[1] == 168 && next_key_p->data[2] == 128);
641 memcpy(key_p, next_key_p, key_size);
642 assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
643 assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
644 next_key_p->data[1] == 168);
646 memcpy(key_p, next_key_p, key_size);
647 assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
650 /* Add one more element (total five) */
651 key_p->prefixlen = 28;
652 inet_pton(AF_INET, "192.168.1.128", key_p->data);
653 assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
655 memset(key_p, 0, key_size);
656 assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
657 assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
658 key_p->data[1] == 168 && key_p->data[2] == 0);
660 memset(next_key_p, 0, key_size);
661 assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
662 assert(next_key_p->prefixlen == 28 && next_key_p->data[0] == 192 &&
663 next_key_p->data[1] == 168 && next_key_p->data[2] == 1 &&
664 next_key_p->data[3] == 128);
666 memcpy(key_p, next_key_p, key_size);
667 assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
668 assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
669 next_key_p->data[1] == 168 && next_key_p->data[2] == 1);
671 memcpy(key_p, next_key_p, key_size);
672 assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
673 assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
674 next_key_p->data[1] == 168 && next_key_p->data[2] == 128);
676 memcpy(key_p, next_key_p, key_size);
677 assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
678 assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
679 next_key_p->data[1] == 168);
681 memcpy(key_p, next_key_p, key_size);
682 assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
685 /* no exact matching key should return the first one in post order */
686 key_p->prefixlen = 22;
687 inet_pton(AF_INET, "192.168.1.0", key_p->data);
688 assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
689 assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
690 next_key_p->data[1] == 168 && next_key_p->data[2] == 0);
695 #define MAX_TEST_KEYS 4
696 struct lpm_mt_test_info {
697 int cmd; /* 0: update, 1: delete, 2: lookup, 3: get_next_key */
703 } key[MAX_TEST_KEYS];
706 static void *lpm_test_command(void *arg)
708 int i, j, ret, iter, key_size;
709 struct lpm_mt_test_info *info = arg;
710 struct bpf_lpm_trie_key *key_p;
712 key_size = sizeof(struct bpf_lpm_trie_key) + sizeof(__u32);
713 key_p = alloca(key_size);
714 for (iter = 0; iter < info->iter; iter++)
715 for (i = 0; i < MAX_TEST_KEYS; i++) {
716 /* first half of iterations in forward order,
717 * and second half in backward order.
719 j = (iter < (info->iter / 2)) ? i : MAX_TEST_KEYS - i - 1;
720 key_p->prefixlen = info->key[j].prefixlen;
721 memcpy(key_p->data, &info->key[j].data, sizeof(__u32));
722 if (info->cmd == 0) {
724 /* update must succeed */
725 assert(bpf_map_update_elem(info->map_fd, key_p, &value, 0) == 0);
726 } else if (info->cmd == 1) {
727 ret = bpf_map_delete_elem(info->map_fd, key_p);
728 assert(ret == 0 || errno == ENOENT);
729 } else if (info->cmd == 2) {
731 ret = bpf_map_lookup_elem(info->map_fd, key_p, &value);
732 assert(ret == 0 || errno == ENOENT);
734 struct bpf_lpm_trie_key *next_key_p = alloca(key_size);
735 ret = bpf_map_get_next_key(info->map_fd, key_p, next_key_p);
736 assert(ret == 0 || errno == ENOENT || errno == ENOMEM);
740 // Pass successful exit info back to the main thread
741 pthread_exit((void *)info);
744 static void setup_lpm_mt_test_info(struct lpm_mt_test_info *info, int map_fd)
747 info->map_fd = map_fd;
748 info->key[0].prefixlen = 16;
749 inet_pton(AF_INET, "192.168.0.0", &info->key[0].data);
750 info->key[1].prefixlen = 24;
751 inet_pton(AF_INET, "192.168.0.0", &info->key[1].data);
752 info->key[2].prefixlen = 24;
753 inet_pton(AF_INET, "192.168.128.0", &info->key[2].data);
754 info->key[3].prefixlen = 24;
755 inet_pton(AF_INET, "192.168.1.0", &info->key[3].data);
758 static void test_lpm_multi_thread(void)
760 LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
761 struct lpm_mt_test_info info[4];
762 size_t key_size, value_size;
763 pthread_t thread_id[4];
768 value_size = sizeof(__u32);
769 key_size = sizeof(struct bpf_lpm_trie_key) + value_size;
770 map_fd = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL, key_size, value_size, 100, &opts);
772 /* create 4 threads to test update, delete, lookup and get_next_key */
773 setup_lpm_mt_test_info(&info[0], map_fd);
774 for (i = 0; i < 4; i++) {
776 memcpy(&info[i], &info[0], sizeof(info[i]));
778 assert(pthread_create(&thread_id[i], NULL, &lpm_test_command, &info[i]) == 0);
781 for (i = 0; i < 4; i++)
782 assert(pthread_join(thread_id[i], &ret) == 0 && ret == (void *)&info[i]);
791 /* we want predictable, pseudo random tests */
797 /* Test with 8, 16, 24, 32, ... 128 bit prefix length */
798 for (i = 1; i <= 16; ++i)
803 test_lpm_get_next_key();
804 test_lpm_multi_thread();
806 printf("test_lpm: OK\n");