1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Randomized tests for eBPF longest-prefix-match maps
4  *
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.
8  *
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.
11  */
12 
13 #include <assert.h>
14 #include <errno.h>
15 #include <inttypes.h>
16 #include <linux/bpf.h>
17 #include <pthread.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <time.h>
22 #include <unistd.h>
23 #include <arpa/inet.h>
24 #include <sys/time.h>
25 
26 #include <bpf/bpf.h>
27 
28 #include "bpf_util.h"
29 #include "bpf_rlimit.h"
30 
31 struct tlpm_node {
32 	struct tlpm_node *next;
33 	size_t n_bits;
34 	uint8_t key[];
35 };
36 
37 static struct tlpm_node *tlpm_match(struct tlpm_node *list,
38 				    const uint8_t *key,
39 				    size_t n_bits);
40 
41 static struct tlpm_node *tlpm_add(struct tlpm_node *list,
42 				  const uint8_t *key,
43 				  size_t n_bits)
44 {
45 	struct tlpm_node *node;
46 	size_t n;
47 
48 	n = (n_bits + 7) / 8;
49 
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);
54 		return list;
55 	}
56 
57 	/* add new entry with @key/@n_bits to @list and return new head */
58 
59 	node = malloc(sizeof(*node) + n);
60 	assert(node);
61 
62 	node->next = list;
63 	node->n_bits = n_bits;
64 	memcpy(node->key, key, n);
65 
66 	return node;
67 }
68 
69 static void tlpm_clear(struct tlpm_node *list)
70 {
71 	struct tlpm_node *node;
72 
73 	/* free all entries in @list */
74 
75 	while ((node = list)) {
76 		list = list->next;
77 		free(node);
78 	}
79 }
80 
81 static struct tlpm_node *tlpm_match(struct tlpm_node *list,
82 				    const uint8_t *key,
83 				    size_t n_bits)
84 {
85 	struct tlpm_node *best = NULL;
86 	size_t i;
87 
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.
92 	 */
93 
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))))
98 				break;
99 		}
100 
101 		if (i >= list->n_bits) {
102 			if (!best || i > best->n_bits)
103 				best = list;
104 		}
105 	}
106 
107 	return best;
108 }
109 
110 static struct tlpm_node *tlpm_delete(struct tlpm_node *list,
111 				     const uint8_t *key,
112 				     size_t n_bits)
113 {
114 	struct tlpm_node *best = tlpm_match(list, key, n_bits);
115 	struct tlpm_node *node;
116 
117 	if (!best || best->n_bits != n_bits)
118 		return list;
119 
120 	if (best == list) {
121 		node = best->next;
122 		free(best);
123 		return node;
124 	}
125 
126 	for (node = list; node; node = node->next) {
127 		if (node->next == best) {
128 			node->next = best->next;
129 			free(best);
130 			return list;
131 		}
132 	}
133 	/* should never get here */
134 	assert(0);
135 	return list;
136 }
137 
138 static void test_lpm_basic(void)
139 {
140 	struct tlpm_node *list = NULL, *t1, *t2;
141 
142 	/* very basic, static tests to verify tlpm works as expected */
143 
144 	assert(!tlpm_match(list, (uint8_t[]){ 0xff }, 8));
145 
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));
153 
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));
159 
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));
163 
164 	list = tlpm_delete(list, (uint8_t[]){ 0xff }, 8);
165 	assert(!tlpm_match(list, (uint8_t[]){ 0xff }, 8));
166 
167 	tlpm_clear(list);
168 }
169 
170 static void test_lpm_order(void)
171 {
172 	struct tlpm_node *t1, *t2, *l1 = NULL, *l2 = NULL;
173 	size_t i, j;
174 
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.
179 	 */
180 
181 	for (i = 0; i < (1 << 12); ++i)
182 		l1 = tlpm_add(l1, (uint8_t[]){
183 					rand() % 0xff,
184 					rand() % 0xff,
185 				}, rand() % 16 + 1);
186 
187 	for (t1 = l1; t1; t1 = t1->next)
188 		l2 = tlpm_add(l2, t1->key, t1->n_bits);
189 
190 	for (i = 0; i < (1 << 8); ++i) {
191 		uint8_t key[] = { rand() % 0xff, rand() % 0xff };
192 
193 		t1 = tlpm_match(l1, key, 16);
194 		t2 = tlpm_match(l2, key, 16);
195 
196 		assert(!t1 == !t2);
197 		if (t1) {
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))));
202 		}
203 	}
204 
205 	tlpm_clear(l1);
206 	tlpm_clear(l2);
207 }
208 
209 static void test_lpm_map(int keysize)
210 {
211 	LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
212 	size_t i, j, n_matches, n_matches_after_delete, n_nodes, n_lookups;
213 	struct tlpm_node *t, *list = NULL;
214 	struct bpf_lpm_trie_key *key;
215 	uint8_t *data, *value;
216 	int r, map;
217 
218 	/* Compare behavior of tlpm vs. bpf-lpm. Create a randomized set of
219 	 * prefixes and insert it into both tlpm and bpf-lpm. Then run some
220 	 * randomized lookups and verify both maps return the same result.
221 	 */
222 
223 	n_matches = 0;
224 	n_matches_after_delete = 0;
225 	n_nodes = 1 << 8;
226 	n_lookups = 1 << 16;
227 
228 	data = alloca(keysize);
229 	memset(data, 0, keysize);
230 
231 	value = alloca(keysize + 1);
232 	memset(value, 0, keysize + 1);
233 
234 	key = alloca(sizeof(*key) + keysize);
235 	memset(key, 0, sizeof(*key) + keysize);
236 
237 	map = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL,
238 			     sizeof(*key) + keysize,
239 			     keysize + 1,
240 			     4096,
241 			     &opts);
242 	assert(map >= 0);
243 
244 	for (i = 0; i < n_nodes; ++i) {
245 		for (j = 0; j < keysize; ++j)
246 			value[j] = rand() & 0xff;
247 		value[keysize] = rand() % (8 * keysize + 1);
248 
249 		list = tlpm_add(list, value, value[keysize]);
250 
251 		key->prefixlen = value[keysize];
252 		memcpy(key->data, value, keysize);
253 		r = bpf_map_update_elem(map, key, value, 0);
254 		assert(!r);
255 	}
256 
257 	for (i = 0; i < n_lookups; ++i) {
258 		for (j = 0; j < keysize; ++j)
259 			data[j] = rand() & 0xff;
260 
261 		t = tlpm_match(list, data, 8 * keysize);
262 
263 		key->prefixlen = 8 * keysize;
264 		memcpy(key->data, data, keysize);
265 		r = bpf_map_lookup_elem(map, key, value);
266 		assert(!r || errno == ENOENT);
267 		assert(!t == !!r);
268 
269 		if (t) {
270 			++n_matches;
271 			assert(t->n_bits == value[keysize]);
272 			for (j = 0; j < t->n_bits; ++j)
273 				assert((t->key[j / 8] & (1 << (7 - j % 8))) ==
274 				       (value[j / 8] & (1 << (7 - j % 8))));
275 		}
276 	}
277 
278 	/* Remove the first half of the elements in the tlpm and the
279 	 * corresponding nodes from the bpf-lpm.  Then run the same
280 	 * large number of random lookups in both and make sure they match.
281 	 * Note: we need to count the number of nodes actually inserted
282 	 * since there may have been duplicates.
283 	 */
284 	for (i = 0, t = list; t; i++, t = t->next)
285 		;
286 	for (j = 0; j < i / 2; ++j) {
287 		key->prefixlen = list->n_bits;
288 		memcpy(key->data, list->key, keysize);
289 		r = bpf_map_delete_elem(map, key);
290 		assert(!r);
291 		list = tlpm_delete(list, list->key, list->n_bits);
292 		assert(list);
293 	}
294 	for (i = 0; i < n_lookups; ++i) {
295 		for (j = 0; j < keysize; ++j)
296 			data[j] = rand() & 0xff;
297 
298 		t = tlpm_match(list, data, 8 * keysize);
299 
300 		key->prefixlen = 8 * keysize;
301 		memcpy(key->data, data, keysize);
302 		r = bpf_map_lookup_elem(map, key, value);
303 		assert(!r || errno == ENOENT);
304 		assert(!t == !!r);
305 
306 		if (t) {
307 			++n_matches_after_delete;
308 			assert(t->n_bits == value[keysize]);
309 			for (j = 0; j < t->n_bits; ++j)
310 				assert((t->key[j / 8] & (1 << (7 - j % 8))) ==
311 				       (value[j / 8] & (1 << (7 - j % 8))));
312 		}
313 	}
314 
315 	close(map);
316 	tlpm_clear(list);
317 
318 	/* With 255 random nodes in the map, we are pretty likely to match
319 	 * something on every lookup. For statistics, use this:
320 	 *
321 	 *     printf("          nodes: %zu\n"
322 	 *            "        lookups: %zu\n"
323 	 *            "        matches: %zu\n"
324 	 *            "matches(delete): %zu\n",
325 	 *            n_nodes, n_lookups, n_matches, n_matches_after_delete);
326 	 */
327 }
328 
329 /* Test the implementation with some 'real world' examples */
330 
331 static void test_lpm_ipaddr(void)
332 {
333 	LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
334 	struct bpf_lpm_trie_key *key_ipv4;
335 	struct bpf_lpm_trie_key *key_ipv6;
336 	size_t key_size_ipv4;
337 	size_t key_size_ipv6;
338 	int map_fd_ipv4;
339 	int map_fd_ipv6;
340 	__u64 value;
341 
342 	key_size_ipv4 = sizeof(*key_ipv4) + sizeof(__u32);
343 	key_size_ipv6 = sizeof(*key_ipv6) + sizeof(__u32) * 4;
344 	key_ipv4 = alloca(key_size_ipv4);
345 	key_ipv6 = alloca(key_size_ipv6);
346 
347 	map_fd_ipv4 = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL,
348 				     key_size_ipv4, sizeof(value),
349 				     100, &opts);
350 	assert(map_fd_ipv4 >= 0);
351 
352 	map_fd_ipv6 = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL,
353 				     key_size_ipv6, sizeof(value),
354 				     100, &opts);
355 	assert(map_fd_ipv6 >= 0);
356 
357 	/* Fill data some IPv4 and IPv6 address ranges */
358 	value = 1;
359 	key_ipv4->prefixlen = 16;
360 	inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
361 	assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
362 
363 	value = 2;
364 	key_ipv4->prefixlen = 24;
365 	inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
366 	assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
367 
368 	value = 3;
369 	key_ipv4->prefixlen = 24;
370 	inet_pton(AF_INET, "192.168.128.0", key_ipv4->data);
371 	assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
372 
373 	value = 5;
374 	key_ipv4->prefixlen = 24;
375 	inet_pton(AF_INET, "192.168.1.0", key_ipv4->data);
376 	assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
377 
378 	value = 4;
379 	key_ipv4->prefixlen = 23;
380 	inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
381 	assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
382 
383 	value = 0xdeadbeef;
384 	key_ipv6->prefixlen = 64;
385 	inet_pton(AF_INET6, "2a00:1450:4001:814::200e", key_ipv6->data);
386 	assert(bpf_map_update_elem(map_fd_ipv6, key_ipv6, &value, 0) == 0);
387 
388 	/* Set tprefixlen to maximum for lookups */
389 	key_ipv4->prefixlen = 32;
390 	key_ipv6->prefixlen = 128;
391 
392 	/* Test some lookups that should come back with a value */
393 	inet_pton(AF_INET, "192.168.128.23", key_ipv4->data);
394 	assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == 0);
395 	assert(value == 3);
396 
397 	inet_pton(AF_INET, "192.168.0.1", key_ipv4->data);
398 	assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == 0);
399 	assert(value == 2);
400 
401 	inet_pton(AF_INET6, "2a00:1450:4001:814::", key_ipv6->data);
402 	assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == 0);
403 	assert(value == 0xdeadbeef);
404 
405 	inet_pton(AF_INET6, "2a00:1450:4001:814::1", key_ipv6->data);
406 	assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == 0);
407 	assert(value == 0xdeadbeef);
408 
409 	/* Test some lookups that should not match any entry */
410 	inet_pton(AF_INET, "10.0.0.1", key_ipv4->data);
411 	assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == -1 &&
412 	       errno == ENOENT);
413 
414 	inet_pton(AF_INET, "11.11.11.11", key_ipv4->data);
415 	assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == -1 &&
416 	       errno == ENOENT);
417 
418 	inet_pton(AF_INET6, "2a00:ffff::", key_ipv6->data);
419 	assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == -1 &&
420 	       errno == ENOENT);
421 
422 	close(map_fd_ipv4);
423 	close(map_fd_ipv6);
424 }
425 
426 static void test_lpm_delete(void)
427 {
428 	LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
429 	struct bpf_lpm_trie_key *key;
430 	size_t key_size;
431 	int map_fd;
432 	__u64 value;
433 
434 	key_size = sizeof(*key) + sizeof(__u32);
435 	key = alloca(key_size);
436 
437 	map_fd = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL,
438 				key_size, sizeof(value),
439 				100, &opts);
440 	assert(map_fd >= 0);
441 
442 	/* Add nodes:
443 	 * 192.168.0.0/16   (1)
444 	 * 192.168.0.0/24   (2)
445 	 * 192.168.128.0/24 (3)
446 	 * 192.168.1.0/24   (4)
447 	 *
448 	 *         (1)
449 	 *        /   \
450          *     (IM)    (3)
451 	 *    /   \
452          *   (2)  (4)
453 	 */
454 	value = 1;
455 	key->prefixlen = 16;
456 	inet_pton(AF_INET, "192.168.0.0", key->data);
457 	assert(bpf_map_update_elem(map_fd, key, &value, 0) == 0);
458 
459 	value = 2;
460 	key->prefixlen = 24;
461 	inet_pton(AF_INET, "192.168.0.0", key->data);
462 	assert(bpf_map_update_elem(map_fd, key, &value, 0) == 0);
463 
464 	value = 3;
465 	key->prefixlen = 24;
466 	inet_pton(AF_INET, "192.168.128.0", key->data);
467 	assert(bpf_map_update_elem(map_fd, key, &value, 0) == 0);
468 
469 	value = 4;
470 	key->prefixlen = 24;
471 	inet_pton(AF_INET, "192.168.1.0", key->data);
472 	assert(bpf_map_update_elem(map_fd, key, &value, 0) == 0);
473 
474 	/* remove non-existent node */
475 	key->prefixlen = 32;
476 	inet_pton(AF_INET, "10.0.0.1", key->data);
477 	assert(bpf_map_lookup_elem(map_fd, key, &value) == -1 &&
478 		errno == ENOENT);
479 
480 	key->prefixlen = 30; // unused prefix so far
481 	inet_pton(AF_INET, "192.255.0.0", key->data);
482 	assert(bpf_map_delete_elem(map_fd, key) == -1 &&
483 		errno == ENOENT);
484 
485 	key->prefixlen = 16; // same prefix as the root node
486 	inet_pton(AF_INET, "192.255.0.0", key->data);
487 	assert(bpf_map_delete_elem(map_fd, key) == -1 &&
488 		errno == ENOENT);
489 
490 	/* assert initial lookup */
491 	key->prefixlen = 32;
492 	inet_pton(AF_INET, "192.168.0.1", key->data);
493 	assert(bpf_map_lookup_elem(map_fd, key, &value) == 0);
494 	assert(value == 2);
495 
496 	/* remove leaf node */
497 	key->prefixlen = 24;
498 	inet_pton(AF_INET, "192.168.0.0", key->data);
499 	assert(bpf_map_delete_elem(map_fd, key) == 0);
500 
501 	key->prefixlen = 32;
502 	inet_pton(AF_INET, "192.168.0.1", key->data);
503 	assert(bpf_map_lookup_elem(map_fd, key, &value) == 0);
504 	assert(value == 1);
505 
506 	/* remove leaf (and intermediary) node */
507 	key->prefixlen = 24;
508 	inet_pton(AF_INET, "192.168.1.0", key->data);
509 	assert(bpf_map_delete_elem(map_fd, key) == 0);
510 
511 	key->prefixlen = 32;
512 	inet_pton(AF_INET, "192.168.1.1", key->data);
513 	assert(bpf_map_lookup_elem(map_fd, key, &value) == 0);
514 	assert(value == 1);
515 
516 	/* remove root node */
517 	key->prefixlen = 16;
518 	inet_pton(AF_INET, "192.168.0.0", key->data);
519 	assert(bpf_map_delete_elem(map_fd, key) == 0);
520 
521 	key->prefixlen = 32;
522 	inet_pton(AF_INET, "192.168.128.1", key->data);
523 	assert(bpf_map_lookup_elem(map_fd, key, &value) == 0);
524 	assert(value == 3);
525 
526 	/* remove last node */
527 	key->prefixlen = 24;
528 	inet_pton(AF_INET, "192.168.128.0", key->data);
529 	assert(bpf_map_delete_elem(map_fd, key) == 0);
530 
531 	key->prefixlen = 32;
532 	inet_pton(AF_INET, "192.168.128.1", key->data);
533 	assert(bpf_map_lookup_elem(map_fd, key, &value) == -1 &&
534 		errno == ENOENT);
535 
536 	close(map_fd);
537 }
538 
539 static void test_lpm_get_next_key(void)
540 {
541 	LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
542 	struct bpf_lpm_trie_key *key_p, *next_key_p;
543 	size_t key_size;
544 	__u32 value = 0;
545 	int map_fd;
546 
547 	key_size = sizeof(*key_p) + sizeof(__u32);
548 	key_p = alloca(key_size);
549 	next_key_p = alloca(key_size);
550 
551 	map_fd = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL, key_size, sizeof(value), 100, &opts);
552 	assert(map_fd >= 0);
553 
554 	/* empty tree. get_next_key should return ENOENT */
555 	assert(bpf_map_get_next_key(map_fd, NULL, key_p) == -1 &&
556 	       errno == ENOENT);
557 
558 	/* get and verify the first key, get the second one should fail. */
559 	key_p->prefixlen = 16;
560 	inet_pton(AF_INET, "192.168.0.0", key_p->data);
561 	assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
562 
563 	memset(key_p, 0, key_size);
564 	assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
565 	assert(key_p->prefixlen == 16 && key_p->data[0] == 192 &&
566 	       key_p->data[1] == 168);
567 
568 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
569 	       errno == ENOENT);
570 
571 	/* no exact matching key should get the first one in post order. */
572 	key_p->prefixlen = 8;
573 	assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
574 	assert(key_p->prefixlen == 16 && key_p->data[0] == 192 &&
575 	       key_p->data[1] == 168);
576 
577 	/* add one more element (total two) */
578 	key_p->prefixlen = 24;
579 	inet_pton(AF_INET, "192.168.128.0", key_p->data);
580 	assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
581 
582 	memset(key_p, 0, key_size);
583 	assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
584 	assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
585 	       key_p->data[1] == 168 && key_p->data[2] == 128);
586 
587 	memset(next_key_p, 0, key_size);
588 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
589 	assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
590 	       next_key_p->data[1] == 168);
591 
592 	memcpy(key_p, next_key_p, key_size);
593 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
594 	       errno == ENOENT);
595 
596 	/* Add one more element (total three) */
597 	key_p->prefixlen = 24;
598 	inet_pton(AF_INET, "192.168.0.0", key_p->data);
599 	assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
600 
601 	memset(key_p, 0, key_size);
602 	assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
603 	assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
604 	       key_p->data[1] == 168 && key_p->data[2] == 0);
605 
606 	memset(next_key_p, 0, key_size);
607 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
608 	assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
609 	       next_key_p->data[1] == 168 && next_key_p->data[2] == 128);
610 
611 	memcpy(key_p, next_key_p, key_size);
612 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
613 	assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
614 	       next_key_p->data[1] == 168);
615 
616 	memcpy(key_p, next_key_p, key_size);
617 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
618 	       errno == ENOENT);
619 
620 	/* Add one more element (total four) */
621 	key_p->prefixlen = 24;
622 	inet_pton(AF_INET, "192.168.1.0", key_p->data);
623 	assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
624 
625 	memset(key_p, 0, key_size);
626 	assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
627 	assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
628 	       key_p->data[1] == 168 && key_p->data[2] == 0);
629 
630 	memset(next_key_p, 0, key_size);
631 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
632 	assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
633 	       next_key_p->data[1] == 168 && next_key_p->data[2] == 1);
634 
635 	memcpy(key_p, next_key_p, key_size);
636 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
637 	assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
638 	       next_key_p->data[1] == 168 && next_key_p->data[2] == 128);
639 
640 	memcpy(key_p, next_key_p, key_size);
641 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
642 	assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
643 	       next_key_p->data[1] == 168);
644 
645 	memcpy(key_p, next_key_p, key_size);
646 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
647 	       errno == ENOENT);
648 
649 	/* Add one more element (total five) */
650 	key_p->prefixlen = 28;
651 	inet_pton(AF_INET, "192.168.1.128", key_p->data);
652 	assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
653 
654 	memset(key_p, 0, key_size);
655 	assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
656 	assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
657 	       key_p->data[1] == 168 && key_p->data[2] == 0);
658 
659 	memset(next_key_p, 0, key_size);
660 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
661 	assert(next_key_p->prefixlen == 28 && next_key_p->data[0] == 192 &&
662 	       next_key_p->data[1] == 168 && next_key_p->data[2] == 1 &&
663 	       next_key_p->data[3] == 128);
664 
665 	memcpy(key_p, next_key_p, key_size);
666 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
667 	assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
668 	       next_key_p->data[1] == 168 && next_key_p->data[2] == 1);
669 
670 	memcpy(key_p, next_key_p, key_size);
671 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
672 	assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
673 	       next_key_p->data[1] == 168 && next_key_p->data[2] == 128);
674 
675 	memcpy(key_p, next_key_p, key_size);
676 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
677 	assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
678 	       next_key_p->data[1] == 168);
679 
680 	memcpy(key_p, next_key_p, key_size);
681 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
682 	       errno == ENOENT);
683 
684 	/* no exact matching key should return the first one in post order */
685 	key_p->prefixlen = 22;
686 	inet_pton(AF_INET, "192.168.1.0", key_p->data);
687 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
688 	assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
689 	       next_key_p->data[1] == 168 && next_key_p->data[2] == 0);
690 
691 	close(map_fd);
692 }
693 
694 #define MAX_TEST_KEYS	4
695 struct lpm_mt_test_info {
696 	int cmd; /* 0: update, 1: delete, 2: lookup, 3: get_next_key */
697 	int iter;
698 	int map_fd;
699 	struct {
700 		__u32 prefixlen;
701 		__u32 data;
702 	} key[MAX_TEST_KEYS];
703 };
704 
705 static void *lpm_test_command(void *arg)
706 {
707 	int i, j, ret, iter, key_size;
708 	struct lpm_mt_test_info *info = arg;
709 	struct bpf_lpm_trie_key *key_p;
710 
711 	key_size = sizeof(struct bpf_lpm_trie_key) + sizeof(__u32);
712 	key_p = alloca(key_size);
713 	for (iter = 0; iter < info->iter; iter++)
714 		for (i = 0; i < MAX_TEST_KEYS; i++) {
715 			/* first half of iterations in forward order,
716 			 * and second half in backward order.
717 			 */
718 			j = (iter < (info->iter / 2)) ? i : MAX_TEST_KEYS - i - 1;
719 			key_p->prefixlen = info->key[j].prefixlen;
720 			memcpy(key_p->data, &info->key[j].data, sizeof(__u32));
721 			if (info->cmd == 0) {
722 				__u32 value = j;
723 				/* update must succeed */
724 				assert(bpf_map_update_elem(info->map_fd, key_p, &value, 0) == 0);
725 			} else if (info->cmd == 1) {
726 				ret = bpf_map_delete_elem(info->map_fd, key_p);
727 				assert(ret == 0 || errno == ENOENT);
728 			} else if (info->cmd == 2) {
729 				__u32 value;
730 				ret = bpf_map_lookup_elem(info->map_fd, key_p, &value);
731 				assert(ret == 0 || errno == ENOENT);
732 			} else {
733 				struct bpf_lpm_trie_key *next_key_p = alloca(key_size);
734 				ret = bpf_map_get_next_key(info->map_fd, key_p, next_key_p);
735 				assert(ret == 0 || errno == ENOENT || errno == ENOMEM);
736 			}
737 		}
738 
739 	// Pass successful exit info back to the main thread
740 	pthread_exit((void *)info);
741 }
742 
743 static void setup_lpm_mt_test_info(struct lpm_mt_test_info *info, int map_fd)
744 {
745 	info->iter = 2000;
746 	info->map_fd = map_fd;
747 	info->key[0].prefixlen = 16;
748 	inet_pton(AF_INET, "192.168.0.0", &info->key[0].data);
749 	info->key[1].prefixlen = 24;
750 	inet_pton(AF_INET, "192.168.0.0", &info->key[1].data);
751 	info->key[2].prefixlen = 24;
752 	inet_pton(AF_INET, "192.168.128.0", &info->key[2].data);
753 	info->key[3].prefixlen = 24;
754 	inet_pton(AF_INET, "192.168.1.0", &info->key[3].data);
755 }
756 
757 static void test_lpm_multi_thread(void)
758 {
759 	LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
760 	struct lpm_mt_test_info info[4];
761 	size_t key_size, value_size;
762 	pthread_t thread_id[4];
763 	int i, map_fd;
764 	void *ret;
765 
766 	/* create a trie */
767 	value_size = sizeof(__u32);
768 	key_size = sizeof(struct bpf_lpm_trie_key) + value_size;
769 	map_fd = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL, key_size, value_size, 100, &opts);
770 
771 	/* create 4 threads to test update, delete, lookup and get_next_key */
772 	setup_lpm_mt_test_info(&info[0], map_fd);
773 	for (i = 0; i < 4; i++) {
774 		if (i != 0)
775 			memcpy(&info[i], &info[0], sizeof(info[i]));
776 		info[i].cmd = i;
777 		assert(pthread_create(&thread_id[i], NULL, &lpm_test_command, &info[i]) == 0);
778 	}
779 
780 	for (i = 0; i < 4; i++)
781 		assert(pthread_join(thread_id[i], &ret) == 0 && ret == (void *)&info[i]);
782 
783 	close(map_fd);
784 }
785 
786 int main(void)
787 {
788 	int i;
789 
790 	/* we want predictable, pseudo random tests */
791 	srand(0xf00ba1);
792 
793 	test_lpm_basic();
794 	test_lpm_order();
795 
796 	/* Test with 8, 16, 24, 32, ... 128 bit prefix length */
797 	for (i = 1; i <= 16; ++i)
798 		test_lpm_map(i);
799 
800 	test_lpm_ipaddr();
801 	test_lpm_delete();
802 	test_lpm_get_next_key();
803 	test_lpm_multi_thread();
804 
805 	printf("test_lpm: OK\n");
806 	return 0;
807 }
808