xref: /openbmc/linux/tools/testing/selftests/bpf/test_lpm_map.c (revision 1760371b277718062211fc7eb6f3042c5051c1a5)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
24d3381f5SDavid Herrmann /*
34d3381f5SDavid Herrmann  * Randomized tests for eBPF longest-prefix-match maps
44d3381f5SDavid Herrmann  *
54d3381f5SDavid Herrmann  * This program runs randomized tests against the lpm-bpf-map. It implements a
64d3381f5SDavid Herrmann  * "Trivial Longest Prefix Match" (tlpm) based on simple, linear, singly linked
74d3381f5SDavid Herrmann  * lists. The implementation should be pretty straightforward.
84d3381f5SDavid Herrmann  *
94d3381f5SDavid Herrmann  * Based on tlpm, this inserts randomized data into bpf-lpm-maps and verifies
104d3381f5SDavid Herrmann  * the trie-based bpf-map implementation behaves the same way as tlpm.
114d3381f5SDavid Herrmann  */
124d3381f5SDavid Herrmann 
134d3381f5SDavid Herrmann #include <assert.h>
144d3381f5SDavid Herrmann #include <errno.h>
154d3381f5SDavid Herrmann #include <inttypes.h>
164d3381f5SDavid Herrmann #include <linux/bpf.h>
17af32efeeSYonghong Song #include <pthread.h>
184d3381f5SDavid Herrmann #include <stdio.h>
194d3381f5SDavid Herrmann #include <stdlib.h>
204d3381f5SDavid Herrmann #include <string.h>
214d3381f5SDavid Herrmann #include <time.h>
224d3381f5SDavid Herrmann #include <unistd.h>
234d3381f5SDavid Herrmann #include <arpa/inet.h>
244d3381f5SDavid Herrmann #include <sys/time.h>
254d3381f5SDavid Herrmann 
2610ecc728SMickaël Salaün #include <bpf/bpf.h>
27fe8d662aSDaniel Borkmann 
284d3381f5SDavid Herrmann #include "bpf_util.h"
294d3381f5SDavid Herrmann 
304d3381f5SDavid Herrmann struct tlpm_node {
314d3381f5SDavid Herrmann 	struct tlpm_node *next;
324d3381f5SDavid Herrmann 	size_t n_bits;
334d3381f5SDavid Herrmann 	uint8_t key[];
344d3381f5SDavid Herrmann };
354d3381f5SDavid Herrmann 
36bae30468SCraig Gallek static struct tlpm_node *tlpm_match(struct tlpm_node *list,
37bae30468SCraig Gallek 				    const uint8_t *key,
38bae30468SCraig Gallek 				    size_t n_bits);
39bae30468SCraig Gallek 
tlpm_add(struct tlpm_node * list,const uint8_t * key,size_t n_bits)404d3381f5SDavid Herrmann static struct tlpm_node *tlpm_add(struct tlpm_node *list,
414d3381f5SDavid Herrmann 				  const uint8_t *key,
424d3381f5SDavid Herrmann 				  size_t n_bits)
434d3381f5SDavid Herrmann {
444d3381f5SDavid Herrmann 	struct tlpm_node *node;
454d3381f5SDavid Herrmann 	size_t n;
464d3381f5SDavid Herrmann 
47bae30468SCraig Gallek 	n = (n_bits + 7) / 8;
48bae30468SCraig Gallek 
49bae30468SCraig Gallek 	/* 'overwrite' an equivalent entry if one already exists */
50bae30468SCraig Gallek 	node = tlpm_match(list, key, n_bits);
51bae30468SCraig Gallek 	if (node && node->n_bits == n_bits) {
52bae30468SCraig Gallek 		memcpy(node->key, key, n);
53bae30468SCraig Gallek 		return list;
54bae30468SCraig Gallek 	}
55bae30468SCraig Gallek 
564d3381f5SDavid Herrmann 	/* add new entry with @key/@n_bits to @list and return new head */
574d3381f5SDavid Herrmann 
584d3381f5SDavid Herrmann 	node = malloc(sizeof(*node) + n);
594d3381f5SDavid Herrmann 	assert(node);
604d3381f5SDavid Herrmann 
614d3381f5SDavid Herrmann 	node->next = list;
624d3381f5SDavid Herrmann 	node->n_bits = n_bits;
634d3381f5SDavid Herrmann 	memcpy(node->key, key, n);
644d3381f5SDavid Herrmann 
654d3381f5SDavid Herrmann 	return node;
664d3381f5SDavid Herrmann }
674d3381f5SDavid Herrmann 
tlpm_clear(struct tlpm_node * list)684d3381f5SDavid Herrmann static void tlpm_clear(struct tlpm_node *list)
694d3381f5SDavid Herrmann {
704d3381f5SDavid Herrmann 	struct tlpm_node *node;
714d3381f5SDavid Herrmann 
724d3381f5SDavid Herrmann 	/* free all entries in @list */
734d3381f5SDavid Herrmann 
744d3381f5SDavid Herrmann 	while ((node = list)) {
754d3381f5SDavid Herrmann 		list = list->next;
764d3381f5SDavid Herrmann 		free(node);
774d3381f5SDavid Herrmann 	}
784d3381f5SDavid Herrmann }
794d3381f5SDavid Herrmann 
tlpm_match(struct tlpm_node * list,const uint8_t * key,size_t n_bits)804d3381f5SDavid Herrmann static struct tlpm_node *tlpm_match(struct tlpm_node *list,
814d3381f5SDavid Herrmann 				    const uint8_t *key,
824d3381f5SDavid Herrmann 				    size_t n_bits)
834d3381f5SDavid Herrmann {
844d3381f5SDavid Herrmann 	struct tlpm_node *best = NULL;
854d3381f5SDavid Herrmann 	size_t i;
864d3381f5SDavid Herrmann 
874d3381f5SDavid Herrmann 	/* Perform longest prefix-match on @key/@n_bits. That is, iterate all
884d3381f5SDavid Herrmann 	 * entries and match each prefix against @key. Remember the "best"
894d3381f5SDavid Herrmann 	 * entry we find (i.e., the longest prefix that matches) and return it
904d3381f5SDavid Herrmann 	 * to the caller when done.
914d3381f5SDavid Herrmann 	 */
924d3381f5SDavid Herrmann 
934d3381f5SDavid Herrmann 	for ( ; list; list = list->next) {
944d3381f5SDavid Herrmann 		for (i = 0; i < n_bits && i < list->n_bits; ++i) {
954d3381f5SDavid Herrmann 			if ((key[i / 8] & (1 << (7 - i % 8))) !=
964d3381f5SDavid Herrmann 			    (list->key[i / 8] & (1 << (7 - i % 8))))
974d3381f5SDavid Herrmann 				break;
984d3381f5SDavid Herrmann 		}
994d3381f5SDavid Herrmann 
1004d3381f5SDavid Herrmann 		if (i >= list->n_bits) {
1014d3381f5SDavid Herrmann 			if (!best || i > best->n_bits)
1024d3381f5SDavid Herrmann 				best = list;
1034d3381f5SDavid Herrmann 		}
1044d3381f5SDavid Herrmann 	}
1054d3381f5SDavid Herrmann 
1064d3381f5SDavid Herrmann 	return best;
1074d3381f5SDavid Herrmann }
1084d3381f5SDavid Herrmann 
tlpm_delete(struct tlpm_node * list,const uint8_t * key,size_t n_bits)109e8d17499SCraig Gallek static struct tlpm_node *tlpm_delete(struct tlpm_node *list,
110e8d17499SCraig Gallek 				     const uint8_t *key,
111e8d17499SCraig Gallek 				     size_t n_bits)
112e8d17499SCraig Gallek {
113e8d17499SCraig Gallek 	struct tlpm_node *best = tlpm_match(list, key, n_bits);
114e8d17499SCraig Gallek 	struct tlpm_node *node;
115e8d17499SCraig Gallek 
116e8d17499SCraig Gallek 	if (!best || best->n_bits != n_bits)
117e8d17499SCraig Gallek 		return list;
118e8d17499SCraig Gallek 
119e8d17499SCraig Gallek 	if (best == list) {
120e8d17499SCraig Gallek 		node = best->next;
121e8d17499SCraig Gallek 		free(best);
122e8d17499SCraig Gallek 		return node;
123e8d17499SCraig Gallek 	}
124e8d17499SCraig Gallek 
125e8d17499SCraig Gallek 	for (node = list; node; node = node->next) {
126e8d17499SCraig Gallek 		if (node->next == best) {
127e8d17499SCraig Gallek 			node->next = best->next;
128e8d17499SCraig Gallek 			free(best);
129e8d17499SCraig Gallek 			return list;
130e8d17499SCraig Gallek 		}
131e8d17499SCraig Gallek 	}
132e8d17499SCraig Gallek 	/* should never get here */
133e8d17499SCraig Gallek 	assert(0);
134e8d17499SCraig Gallek 	return list;
135e8d17499SCraig Gallek }
136e8d17499SCraig Gallek 
test_lpm_basic(void)1374d3381f5SDavid Herrmann static void test_lpm_basic(void)
1384d3381f5SDavid Herrmann {
1394d3381f5SDavid Herrmann 	struct tlpm_node *list = NULL, *t1, *t2;
1404d3381f5SDavid Herrmann 
1414d3381f5SDavid Herrmann 	/* very basic, static tests to verify tlpm works as expected */
1424d3381f5SDavid Herrmann 
1434d3381f5SDavid Herrmann 	assert(!tlpm_match(list, (uint8_t[]){ 0xff }, 8));
1444d3381f5SDavid Herrmann 
1454d3381f5SDavid Herrmann 	t1 = list = tlpm_add(list, (uint8_t[]){ 0xff }, 8);
1464d3381f5SDavid Herrmann 	assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff }, 8));
1474d3381f5SDavid Herrmann 	assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff, 0xff }, 16));
1484d3381f5SDavid Herrmann 	assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff, 0x00 }, 16));
1494d3381f5SDavid Herrmann 	assert(!tlpm_match(list, (uint8_t[]){ 0x7f }, 8));
1504d3381f5SDavid Herrmann 	assert(!tlpm_match(list, (uint8_t[]){ 0xfe }, 8));
1514d3381f5SDavid Herrmann 	assert(!tlpm_match(list, (uint8_t[]){ 0xff }, 7));
1524d3381f5SDavid Herrmann 
1534d3381f5SDavid Herrmann 	t2 = list = tlpm_add(list, (uint8_t[]){ 0xff, 0xff }, 16);
1544d3381f5SDavid Herrmann 	assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff }, 8));
1554d3381f5SDavid Herrmann 	assert(t2 == tlpm_match(list, (uint8_t[]){ 0xff, 0xff }, 16));
1564d3381f5SDavid Herrmann 	assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff, 0xff }, 15));
1574d3381f5SDavid Herrmann 	assert(!tlpm_match(list, (uint8_t[]){ 0x7f, 0xff }, 16));
1584d3381f5SDavid Herrmann 
159e8d17499SCraig Gallek 	list = tlpm_delete(list, (uint8_t[]){ 0xff, 0xff }, 16);
160e8d17499SCraig Gallek 	assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff }, 8));
161e8d17499SCraig Gallek 	assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff, 0xff }, 16));
162e8d17499SCraig Gallek 
163e8d17499SCraig Gallek 	list = tlpm_delete(list, (uint8_t[]){ 0xff }, 8);
164e8d17499SCraig Gallek 	assert(!tlpm_match(list, (uint8_t[]){ 0xff }, 8));
165e8d17499SCraig Gallek 
1664d3381f5SDavid Herrmann 	tlpm_clear(list);
1674d3381f5SDavid Herrmann }
1684d3381f5SDavid Herrmann 
test_lpm_order(void)1694d3381f5SDavid Herrmann static void test_lpm_order(void)
1704d3381f5SDavid Herrmann {
1714d3381f5SDavid Herrmann 	struct tlpm_node *t1, *t2, *l1 = NULL, *l2 = NULL;
1724d3381f5SDavid Herrmann 	size_t i, j;
1734d3381f5SDavid Herrmann 
1744d3381f5SDavid Herrmann 	/* Verify the tlpm implementation works correctly regardless of the
1754d3381f5SDavid Herrmann 	 * order of entries. Insert a random set of entries into @l1, and copy
1764d3381f5SDavid Herrmann 	 * the same data in reverse order into @l2. Then verify a lookup of
1774d3381f5SDavid Herrmann 	 * random keys will yield the same result in both sets.
1784d3381f5SDavid Herrmann 	 */
1794d3381f5SDavid Herrmann 
1804d3381f5SDavid Herrmann 	for (i = 0; i < (1 << 12); ++i)
1814d3381f5SDavid Herrmann 		l1 = tlpm_add(l1, (uint8_t[]){
1824d3381f5SDavid Herrmann 					rand() % 0xff,
1834d3381f5SDavid Herrmann 					rand() % 0xff,
1844d3381f5SDavid Herrmann 				}, rand() % 16 + 1);
1854d3381f5SDavid Herrmann 
1864d3381f5SDavid Herrmann 	for (t1 = l1; t1; t1 = t1->next)
1874d3381f5SDavid Herrmann 		l2 = tlpm_add(l2, t1->key, t1->n_bits);
1884d3381f5SDavid Herrmann 
1894d3381f5SDavid Herrmann 	for (i = 0; i < (1 << 8); ++i) {
1904d3381f5SDavid Herrmann 		uint8_t key[] = { rand() % 0xff, rand() % 0xff };
1914d3381f5SDavid Herrmann 
1924d3381f5SDavid Herrmann 		t1 = tlpm_match(l1, key, 16);
1934d3381f5SDavid Herrmann 		t2 = tlpm_match(l2, key, 16);
1944d3381f5SDavid Herrmann 
1954d3381f5SDavid Herrmann 		assert(!t1 == !t2);
1964d3381f5SDavid Herrmann 		if (t1) {
1974d3381f5SDavid Herrmann 			assert(t1->n_bits == t2->n_bits);
1984d3381f5SDavid Herrmann 			for (j = 0; j < t1->n_bits; ++j)
1994d3381f5SDavid Herrmann 				assert((t1->key[j / 8] & (1 << (7 - j % 8))) ==
2004d3381f5SDavid Herrmann 				       (t2->key[j / 8] & (1 << (7 - j % 8))));
2014d3381f5SDavid Herrmann 		}
2024d3381f5SDavid Herrmann 	}
2034d3381f5SDavid Herrmann 
2044d3381f5SDavid Herrmann 	tlpm_clear(l1);
2054d3381f5SDavid Herrmann 	tlpm_clear(l2);
2064d3381f5SDavid Herrmann }
2074d3381f5SDavid Herrmann 
test_lpm_map(int keysize)2084d3381f5SDavid Herrmann static void test_lpm_map(int keysize)
2094d3381f5SDavid Herrmann {
2102fe256a4SAndrii Nakryiko 	LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
211ccaff3d5SYonghong Song 	volatile size_t n_matches, n_matches_after_delete;
212ccaff3d5SYonghong Song 	size_t i, j, n_nodes, n_lookups;
2134d3381f5SDavid Herrmann 	struct tlpm_node *t, *list = NULL;
214*ef33f029SKees Cook 	struct bpf_lpm_trie_key_u8 *key;
2154d3381f5SDavid Herrmann 	uint8_t *data, *value;
2164d3381f5SDavid Herrmann 	int r, map;
2174d3381f5SDavid Herrmann 
2184d3381f5SDavid Herrmann 	/* Compare behavior of tlpm vs. bpf-lpm. Create a randomized set of
2194d3381f5SDavid Herrmann 	 * prefixes and insert it into both tlpm and bpf-lpm. Then run some
2204d3381f5SDavid Herrmann 	 * randomized lookups and verify both maps return the same result.
2214d3381f5SDavid Herrmann 	 */
2224d3381f5SDavid Herrmann 
2234d3381f5SDavid Herrmann 	n_matches = 0;
224e8d17499SCraig Gallek 	n_matches_after_delete = 0;
2254d3381f5SDavid Herrmann 	n_nodes = 1 << 8;
2264d3381f5SDavid Herrmann 	n_lookups = 1 << 16;
2274d3381f5SDavid Herrmann 
2284d3381f5SDavid Herrmann 	data = alloca(keysize);
2294d3381f5SDavid Herrmann 	memset(data, 0, keysize);
2304d3381f5SDavid Herrmann 
2314d3381f5SDavid Herrmann 	value = alloca(keysize + 1);
2324d3381f5SDavid Herrmann 	memset(value, 0, keysize + 1);
2334d3381f5SDavid Herrmann 
2344d3381f5SDavid Herrmann 	key = alloca(sizeof(*key) + keysize);
2354d3381f5SDavid Herrmann 	memset(key, 0, sizeof(*key) + keysize);
2364d3381f5SDavid Herrmann 
2372fe256a4SAndrii Nakryiko 	map = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL,
2384d3381f5SDavid Herrmann 			     sizeof(*key) + keysize,
2394d3381f5SDavid Herrmann 			     keysize + 1,
2404d3381f5SDavid Herrmann 			     4096,
2412fe256a4SAndrii Nakryiko 			     &opts);
2424d3381f5SDavid Herrmann 	assert(map >= 0);
2434d3381f5SDavid Herrmann 
2444d3381f5SDavid Herrmann 	for (i = 0; i < n_nodes; ++i) {
2454d3381f5SDavid Herrmann 		for (j = 0; j < keysize; ++j)
2464d3381f5SDavid Herrmann 			value[j] = rand() & 0xff;
2474d3381f5SDavid Herrmann 		value[keysize] = rand() % (8 * keysize + 1);
2484d3381f5SDavid Herrmann 
2494d3381f5SDavid Herrmann 		list = tlpm_add(list, value, value[keysize]);
2504d3381f5SDavid Herrmann 
2514d3381f5SDavid Herrmann 		key->prefixlen = value[keysize];
2524d3381f5SDavid Herrmann 		memcpy(key->data, value, keysize);
25310ecc728SMickaël Salaün 		r = bpf_map_update_elem(map, key, value, 0);
2544d3381f5SDavid Herrmann 		assert(!r);
2554d3381f5SDavid Herrmann 	}
2564d3381f5SDavid Herrmann 
2574d3381f5SDavid Herrmann 	for (i = 0; i < n_lookups; ++i) {
2584d3381f5SDavid Herrmann 		for (j = 0; j < keysize; ++j)
2594d3381f5SDavid Herrmann 			data[j] = rand() & 0xff;
2604d3381f5SDavid Herrmann 
2614d3381f5SDavid Herrmann 		t = tlpm_match(list, data, 8 * keysize);
2624d3381f5SDavid Herrmann 
2634d3381f5SDavid Herrmann 		key->prefixlen = 8 * keysize;
2644d3381f5SDavid Herrmann 		memcpy(key->data, data, keysize);
265e5ff7c40SMickaël Salaün 		r = bpf_map_lookup_elem(map, key, value);
2664d3381f5SDavid Herrmann 		assert(!r || errno == ENOENT);
2674d3381f5SDavid Herrmann 		assert(!t == !!r);
2684d3381f5SDavid Herrmann 
2694d3381f5SDavid Herrmann 		if (t) {
2704d3381f5SDavid Herrmann 			++n_matches;
2714d3381f5SDavid Herrmann 			assert(t->n_bits == value[keysize]);
2724d3381f5SDavid Herrmann 			for (j = 0; j < t->n_bits; ++j)
2734d3381f5SDavid Herrmann 				assert((t->key[j / 8] & (1 << (7 - j % 8))) ==
2744d3381f5SDavid Herrmann 				       (value[j / 8] & (1 << (7 - j % 8))));
2754d3381f5SDavid Herrmann 		}
2764d3381f5SDavid Herrmann 	}
2774d3381f5SDavid Herrmann 
278e8d17499SCraig Gallek 	/* Remove the first half of the elements in the tlpm and the
279e8d17499SCraig Gallek 	 * corresponding nodes from the bpf-lpm.  Then run the same
280e8d17499SCraig Gallek 	 * large number of random lookups in both and make sure they match.
281e8d17499SCraig Gallek 	 * Note: we need to count the number of nodes actually inserted
282e8d17499SCraig Gallek 	 * since there may have been duplicates.
283e8d17499SCraig Gallek 	 */
284e8d17499SCraig Gallek 	for (i = 0, t = list; t; i++, t = t->next)
285e8d17499SCraig Gallek 		;
286e8d17499SCraig Gallek 	for (j = 0; j < i / 2; ++j) {
287e8d17499SCraig Gallek 		key->prefixlen = list->n_bits;
288e8d17499SCraig Gallek 		memcpy(key->data, list->key, keysize);
289e8d17499SCraig Gallek 		r = bpf_map_delete_elem(map, key);
290e8d17499SCraig Gallek 		assert(!r);
291e8d17499SCraig Gallek 		list = tlpm_delete(list, list->key, list->n_bits);
292e8d17499SCraig Gallek 		assert(list);
293e8d17499SCraig Gallek 	}
294e8d17499SCraig Gallek 	for (i = 0; i < n_lookups; ++i) {
295e8d17499SCraig Gallek 		for (j = 0; j < keysize; ++j)
296e8d17499SCraig Gallek 			data[j] = rand() & 0xff;
297e8d17499SCraig Gallek 
298e8d17499SCraig Gallek 		t = tlpm_match(list, data, 8 * keysize);
299e8d17499SCraig Gallek 
300e8d17499SCraig Gallek 		key->prefixlen = 8 * keysize;
301e8d17499SCraig Gallek 		memcpy(key->data, data, keysize);
302e8d17499SCraig Gallek 		r = bpf_map_lookup_elem(map, key, value);
303e8d17499SCraig Gallek 		assert(!r || errno == ENOENT);
304e8d17499SCraig Gallek 		assert(!t == !!r);
305e8d17499SCraig Gallek 
306e8d17499SCraig Gallek 		if (t) {
307e8d17499SCraig Gallek 			++n_matches_after_delete;
308e8d17499SCraig Gallek 			assert(t->n_bits == value[keysize]);
309e8d17499SCraig Gallek 			for (j = 0; j < t->n_bits; ++j)
310e8d17499SCraig Gallek 				assert((t->key[j / 8] & (1 << (7 - j % 8))) ==
311e8d17499SCraig Gallek 				       (value[j / 8] & (1 << (7 - j % 8))));
312e8d17499SCraig Gallek 		}
313e8d17499SCraig Gallek 	}
314e8d17499SCraig Gallek 
3154d3381f5SDavid Herrmann 	close(map);
3164d3381f5SDavid Herrmann 	tlpm_clear(list);
3174d3381f5SDavid Herrmann 
3184d3381f5SDavid Herrmann 	/* With 255 random nodes in the map, we are pretty likely to match
3194d3381f5SDavid Herrmann 	 * something on every lookup. For statistics, use this:
3204d3381f5SDavid Herrmann 	 *
3214d3381f5SDavid Herrmann 	 *     printf("          nodes: %zu\n"
3224d3381f5SDavid Herrmann 	 *            "        lookups: %zu\n"
323e8d17499SCraig Gallek 	 *            "        matches: %zu\n"
324e8d17499SCraig Gallek 	 *            "matches(delete): %zu\n",
325e8d17499SCraig Gallek 	 *            n_nodes, n_lookups, n_matches, n_matches_after_delete);
3264d3381f5SDavid Herrmann 	 */
3274d3381f5SDavid Herrmann }
3284d3381f5SDavid Herrmann 
3294d3381f5SDavid Herrmann /* Test the implementation with some 'real world' examples */
3304d3381f5SDavid Herrmann 
test_lpm_ipaddr(void)3314d3381f5SDavid Herrmann static void test_lpm_ipaddr(void)
3324d3381f5SDavid Herrmann {
3332fe256a4SAndrii Nakryiko 	LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
334*ef33f029SKees Cook 	struct bpf_lpm_trie_key_u8 *key_ipv4;
335*ef33f029SKees Cook 	struct bpf_lpm_trie_key_u8 *key_ipv6;
3364d3381f5SDavid Herrmann 	size_t key_size_ipv4;
3374d3381f5SDavid Herrmann 	size_t key_size_ipv6;
3384d3381f5SDavid Herrmann 	int map_fd_ipv4;
3394d3381f5SDavid Herrmann 	int map_fd_ipv6;
3404d3381f5SDavid Herrmann 	__u64 value;
3414d3381f5SDavid Herrmann 
3424d3381f5SDavid Herrmann 	key_size_ipv4 = sizeof(*key_ipv4) + sizeof(__u32);
3434d3381f5SDavid Herrmann 	key_size_ipv6 = sizeof(*key_ipv6) + sizeof(__u32) * 4;
3444d3381f5SDavid Herrmann 	key_ipv4 = alloca(key_size_ipv4);
3454d3381f5SDavid Herrmann 	key_ipv6 = alloca(key_size_ipv6);
3464d3381f5SDavid Herrmann 
3472fe256a4SAndrii Nakryiko 	map_fd_ipv4 = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL,
3484d3381f5SDavid Herrmann 				     key_size_ipv4, sizeof(value),
3492fe256a4SAndrii Nakryiko 				     100, &opts);
3504d3381f5SDavid Herrmann 	assert(map_fd_ipv4 >= 0);
3514d3381f5SDavid Herrmann 
3522fe256a4SAndrii Nakryiko 	map_fd_ipv6 = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL,
3534d3381f5SDavid Herrmann 				     key_size_ipv6, sizeof(value),
3542fe256a4SAndrii Nakryiko 				     100, &opts);
3554d3381f5SDavid Herrmann 	assert(map_fd_ipv6 >= 0);
3564d3381f5SDavid Herrmann 
3574d3381f5SDavid Herrmann 	/* Fill data some IPv4 and IPv6 address ranges */
3584d3381f5SDavid Herrmann 	value = 1;
3594d3381f5SDavid Herrmann 	key_ipv4->prefixlen = 16;
3604d3381f5SDavid Herrmann 	inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
36110ecc728SMickaël Salaün 	assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
3624d3381f5SDavid Herrmann 
3634d3381f5SDavid Herrmann 	value = 2;
3644d3381f5SDavid Herrmann 	key_ipv4->prefixlen = 24;
3654d3381f5SDavid Herrmann 	inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
36610ecc728SMickaël Salaün 	assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
3674d3381f5SDavid Herrmann 
3684d3381f5SDavid Herrmann 	value = 3;
3694d3381f5SDavid Herrmann 	key_ipv4->prefixlen = 24;
3704d3381f5SDavid Herrmann 	inet_pton(AF_INET, "192.168.128.0", key_ipv4->data);
37110ecc728SMickaël Salaün 	assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
3724d3381f5SDavid Herrmann 
3734d3381f5SDavid Herrmann 	value = 5;
3744d3381f5SDavid Herrmann 	key_ipv4->prefixlen = 24;
3754d3381f5SDavid Herrmann 	inet_pton(AF_INET, "192.168.1.0", key_ipv4->data);
37610ecc728SMickaël Salaün 	assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
3774d3381f5SDavid Herrmann 
3784d3381f5SDavid Herrmann 	value = 4;
3794d3381f5SDavid Herrmann 	key_ipv4->prefixlen = 23;
3804d3381f5SDavid Herrmann 	inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
38110ecc728SMickaël Salaün 	assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
3824d3381f5SDavid Herrmann 
3834d3381f5SDavid Herrmann 	value = 0xdeadbeef;
3844d3381f5SDavid Herrmann 	key_ipv6->prefixlen = 64;
3854d3381f5SDavid Herrmann 	inet_pton(AF_INET6, "2a00:1450:4001:814::200e", key_ipv6->data);
38610ecc728SMickaël Salaün 	assert(bpf_map_update_elem(map_fd_ipv6, key_ipv6, &value, 0) == 0);
3874d3381f5SDavid Herrmann 
3884d3381f5SDavid Herrmann 	/* Set tprefixlen to maximum for lookups */
3894d3381f5SDavid Herrmann 	key_ipv4->prefixlen = 32;
3904d3381f5SDavid Herrmann 	key_ipv6->prefixlen = 128;
3914d3381f5SDavid Herrmann 
3924d3381f5SDavid Herrmann 	/* Test some lookups that should come back with a value */
3934d3381f5SDavid Herrmann 	inet_pton(AF_INET, "192.168.128.23", key_ipv4->data);
394e5ff7c40SMickaël Salaün 	assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == 0);
3954d3381f5SDavid Herrmann 	assert(value == 3);
3964d3381f5SDavid Herrmann 
3974d3381f5SDavid Herrmann 	inet_pton(AF_INET, "192.168.0.1", key_ipv4->data);
398e5ff7c40SMickaël Salaün 	assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == 0);
3994d3381f5SDavid Herrmann 	assert(value == 2);
4004d3381f5SDavid Herrmann 
4014d3381f5SDavid Herrmann 	inet_pton(AF_INET6, "2a00:1450:4001:814::", key_ipv6->data);
402e5ff7c40SMickaël Salaün 	assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == 0);
4034d3381f5SDavid Herrmann 	assert(value == 0xdeadbeef);
4044d3381f5SDavid Herrmann 
4054d3381f5SDavid Herrmann 	inet_pton(AF_INET6, "2a00:1450:4001:814::1", key_ipv6->data);
406e5ff7c40SMickaël Salaün 	assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == 0);
4074d3381f5SDavid Herrmann 	assert(value == 0xdeadbeef);
4084d3381f5SDavid Herrmann 
4094d3381f5SDavid Herrmann 	/* Test some lookups that should not match any entry */
4104d3381f5SDavid Herrmann 	inet_pton(AF_INET, "10.0.0.1", key_ipv4->data);
411c14766a8SArtem Savkov 	assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == -ENOENT);
4124d3381f5SDavid Herrmann 
4134d3381f5SDavid Herrmann 	inet_pton(AF_INET, "11.11.11.11", key_ipv4->data);
414c14766a8SArtem Savkov 	assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == -ENOENT);
4154d3381f5SDavid Herrmann 
4164d3381f5SDavid Herrmann 	inet_pton(AF_INET6, "2a00:ffff::", key_ipv6->data);
417c14766a8SArtem Savkov 	assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == -ENOENT);
4184d3381f5SDavid Herrmann 
4194d3381f5SDavid Herrmann 	close(map_fd_ipv4);
4204d3381f5SDavid Herrmann 	close(map_fd_ipv6);
4214d3381f5SDavid Herrmann }
4224d3381f5SDavid Herrmann 
test_lpm_delete(void)423e8d17499SCraig Gallek static void test_lpm_delete(void)
424e8d17499SCraig Gallek {
4252fe256a4SAndrii Nakryiko 	LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
426*ef33f029SKees Cook 	struct bpf_lpm_trie_key_u8 *key;
427e8d17499SCraig Gallek 	size_t key_size;
428e8d17499SCraig Gallek 	int map_fd;
429e8d17499SCraig Gallek 	__u64 value;
430e8d17499SCraig Gallek 
431e8d17499SCraig Gallek 	key_size = sizeof(*key) + sizeof(__u32);
432e8d17499SCraig Gallek 	key = alloca(key_size);
433e8d17499SCraig Gallek 
4342fe256a4SAndrii Nakryiko 	map_fd = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL,
435e8d17499SCraig Gallek 				key_size, sizeof(value),
4362fe256a4SAndrii Nakryiko 				100, &opts);
437e8d17499SCraig Gallek 	assert(map_fd >= 0);
438e8d17499SCraig Gallek 
439e8d17499SCraig Gallek 	/* Add nodes:
440e8d17499SCraig Gallek 	 * 192.168.0.0/16   (1)
441e8d17499SCraig Gallek 	 * 192.168.0.0/24   (2)
442e8d17499SCraig Gallek 	 * 192.168.128.0/24 (3)
443e8d17499SCraig Gallek 	 * 192.168.1.0/24   (4)
444e8d17499SCraig Gallek 	 *
445e8d17499SCraig Gallek 	 *         (1)
446e8d17499SCraig Gallek 	 *        /   \
447e8d17499SCraig Gallek          *     (IM)    (3)
448e8d17499SCraig Gallek 	 *    /   \
449e8d17499SCraig Gallek          *   (2)  (4)
450e8d17499SCraig Gallek 	 */
451e8d17499SCraig Gallek 	value = 1;
452e8d17499SCraig Gallek 	key->prefixlen = 16;
453e8d17499SCraig Gallek 	inet_pton(AF_INET, "192.168.0.0", key->data);
454e8d17499SCraig Gallek 	assert(bpf_map_update_elem(map_fd, key, &value, 0) == 0);
455e8d17499SCraig Gallek 
456e8d17499SCraig Gallek 	value = 2;
457e8d17499SCraig Gallek 	key->prefixlen = 24;
458e8d17499SCraig Gallek 	inet_pton(AF_INET, "192.168.0.0", key->data);
459e8d17499SCraig Gallek 	assert(bpf_map_update_elem(map_fd, key, &value, 0) == 0);
460e8d17499SCraig Gallek 
461e8d17499SCraig Gallek 	value = 3;
462e8d17499SCraig Gallek 	key->prefixlen = 24;
463e8d17499SCraig Gallek 	inet_pton(AF_INET, "192.168.128.0", key->data);
464e8d17499SCraig Gallek 	assert(bpf_map_update_elem(map_fd, key, &value, 0) == 0);
465e8d17499SCraig Gallek 
466e8d17499SCraig Gallek 	value = 4;
467e8d17499SCraig Gallek 	key->prefixlen = 24;
468e8d17499SCraig Gallek 	inet_pton(AF_INET, "192.168.1.0", key->data);
469e8d17499SCraig Gallek 	assert(bpf_map_update_elem(map_fd, key, &value, 0) == 0);
470e8d17499SCraig Gallek 
471e8d17499SCraig Gallek 	/* remove non-existent node */
472e8d17499SCraig Gallek 	key->prefixlen = 32;
473e8d17499SCraig Gallek 	inet_pton(AF_INET, "10.0.0.1", key->data);
474c14766a8SArtem Savkov 	assert(bpf_map_lookup_elem(map_fd, key, &value) == -ENOENT);
475e8d17499SCraig Gallek 
4767c0cdf0bSAlban Crequy 	key->prefixlen = 30; // unused prefix so far
4777c0cdf0bSAlban Crequy 	inet_pton(AF_INET, "192.255.0.0", key->data);
478c14766a8SArtem Savkov 	assert(bpf_map_delete_elem(map_fd, key) == -ENOENT);
4797c0cdf0bSAlban Crequy 
4807c0cdf0bSAlban Crequy 	key->prefixlen = 16; // same prefix as the root node
4817c0cdf0bSAlban Crequy 	inet_pton(AF_INET, "192.255.0.0", key->data);
482c14766a8SArtem Savkov 	assert(bpf_map_delete_elem(map_fd, key) == -ENOENT);
4837c0cdf0bSAlban Crequy 
484e8d17499SCraig Gallek 	/* assert initial lookup */
485e8d17499SCraig Gallek 	key->prefixlen = 32;
486e8d17499SCraig Gallek 	inet_pton(AF_INET, "192.168.0.1", key->data);
487e8d17499SCraig Gallek 	assert(bpf_map_lookup_elem(map_fd, key, &value) == 0);
488e8d17499SCraig Gallek 	assert(value == 2);
489e8d17499SCraig Gallek 
490e8d17499SCraig Gallek 	/* remove leaf node */
491e8d17499SCraig Gallek 	key->prefixlen = 24;
492e8d17499SCraig Gallek 	inet_pton(AF_INET, "192.168.0.0", key->data);
493e8d17499SCraig Gallek 	assert(bpf_map_delete_elem(map_fd, key) == 0);
494e8d17499SCraig Gallek 
495e8d17499SCraig Gallek 	key->prefixlen = 32;
496e8d17499SCraig Gallek 	inet_pton(AF_INET, "192.168.0.1", key->data);
497e8d17499SCraig Gallek 	assert(bpf_map_lookup_elem(map_fd, key, &value) == 0);
498e8d17499SCraig Gallek 	assert(value == 1);
499e8d17499SCraig Gallek 
500e8d17499SCraig Gallek 	/* remove leaf (and intermediary) node */
501e8d17499SCraig Gallek 	key->prefixlen = 24;
502e8d17499SCraig Gallek 	inet_pton(AF_INET, "192.168.1.0", key->data);
503e8d17499SCraig Gallek 	assert(bpf_map_delete_elem(map_fd, key) == 0);
504e8d17499SCraig Gallek 
505e8d17499SCraig Gallek 	key->prefixlen = 32;
506e8d17499SCraig Gallek 	inet_pton(AF_INET, "192.168.1.1", key->data);
507e8d17499SCraig Gallek 	assert(bpf_map_lookup_elem(map_fd, key, &value) == 0);
508e8d17499SCraig Gallek 	assert(value == 1);
509e8d17499SCraig Gallek 
510e8d17499SCraig Gallek 	/* remove root node */
511e8d17499SCraig Gallek 	key->prefixlen = 16;
512e8d17499SCraig Gallek 	inet_pton(AF_INET, "192.168.0.0", key->data);
513e8d17499SCraig Gallek 	assert(bpf_map_delete_elem(map_fd, key) == 0);
514e8d17499SCraig Gallek 
515e8d17499SCraig Gallek 	key->prefixlen = 32;
516e8d17499SCraig Gallek 	inet_pton(AF_INET, "192.168.128.1", key->data);
517e8d17499SCraig Gallek 	assert(bpf_map_lookup_elem(map_fd, key, &value) == 0);
518e8d17499SCraig Gallek 	assert(value == 3);
519e8d17499SCraig Gallek 
520e8d17499SCraig Gallek 	/* remove last node */
521e8d17499SCraig Gallek 	key->prefixlen = 24;
522e8d17499SCraig Gallek 	inet_pton(AF_INET, "192.168.128.0", key->data);
523e8d17499SCraig Gallek 	assert(bpf_map_delete_elem(map_fd, key) == 0);
524e8d17499SCraig Gallek 
525e8d17499SCraig Gallek 	key->prefixlen = 32;
526e8d17499SCraig Gallek 	inet_pton(AF_INET, "192.168.128.1", key->data);
527c14766a8SArtem Savkov 	assert(bpf_map_lookup_elem(map_fd, key, &value) == -ENOENT);
528e8d17499SCraig Gallek 
529e8d17499SCraig Gallek 	close(map_fd);
530e8d17499SCraig Gallek }
531e8d17499SCraig Gallek 
test_lpm_get_next_key(void)5328c417dc1SYonghong Song static void test_lpm_get_next_key(void)
5338c417dc1SYonghong Song {
5342fe256a4SAndrii Nakryiko 	LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
535*ef33f029SKees Cook 	struct bpf_lpm_trie_key_u8 *key_p, *next_key_p;
5368c417dc1SYonghong Song 	size_t key_size;
5378c417dc1SYonghong Song 	__u32 value = 0;
5388c417dc1SYonghong Song 	int map_fd;
5398c417dc1SYonghong Song 
5408c417dc1SYonghong Song 	key_size = sizeof(*key_p) + sizeof(__u32);
5418c417dc1SYonghong Song 	key_p = alloca(key_size);
5428c417dc1SYonghong Song 	next_key_p = alloca(key_size);
5438c417dc1SYonghong Song 
5442fe256a4SAndrii Nakryiko 	map_fd = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL, key_size, sizeof(value), 100, &opts);
5458c417dc1SYonghong Song 	assert(map_fd >= 0);
5468c417dc1SYonghong Song 
5478c417dc1SYonghong Song 	/* empty tree. get_next_key should return ENOENT */
548c14766a8SArtem Savkov 	assert(bpf_map_get_next_key(map_fd, NULL, key_p) == -ENOENT);
5498c417dc1SYonghong Song 
5508c417dc1SYonghong Song 	/* get and verify the first key, get the second one should fail. */
5518c417dc1SYonghong Song 	key_p->prefixlen = 16;
5528c417dc1SYonghong Song 	inet_pton(AF_INET, "192.168.0.0", key_p->data);
5538c417dc1SYonghong Song 	assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
5548c417dc1SYonghong Song 
5558c417dc1SYonghong Song 	memset(key_p, 0, key_size);
5568c417dc1SYonghong Song 	assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
5578c417dc1SYonghong Song 	assert(key_p->prefixlen == 16 && key_p->data[0] == 192 &&
5588c417dc1SYonghong Song 	       key_p->data[1] == 168);
5598c417dc1SYonghong Song 
560c14766a8SArtem Savkov 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -ENOENT);
5618c417dc1SYonghong Song 
5628c417dc1SYonghong Song 	/* no exact matching key should get the first one in post order. */
5638c417dc1SYonghong Song 	key_p->prefixlen = 8;
5648c417dc1SYonghong Song 	assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
5658c417dc1SYonghong Song 	assert(key_p->prefixlen == 16 && key_p->data[0] == 192 &&
5668c417dc1SYonghong Song 	       key_p->data[1] == 168);
5678c417dc1SYonghong Song 
5688c417dc1SYonghong Song 	/* add one more element (total two) */
5698c417dc1SYonghong Song 	key_p->prefixlen = 24;
570da2577fdSJonathan Lemon 	inet_pton(AF_INET, "192.168.128.0", key_p->data);
5718c417dc1SYonghong Song 	assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
5728c417dc1SYonghong Song 
5738c417dc1SYonghong Song 	memset(key_p, 0, key_size);
5748c417dc1SYonghong Song 	assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
5758c417dc1SYonghong Song 	assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
576da2577fdSJonathan Lemon 	       key_p->data[1] == 168 && key_p->data[2] == 128);
5778c417dc1SYonghong Song 
5788c417dc1SYonghong Song 	memset(next_key_p, 0, key_size);
5798c417dc1SYonghong Song 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
5808c417dc1SYonghong Song 	assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
5818c417dc1SYonghong Song 	       next_key_p->data[1] == 168);
5828c417dc1SYonghong Song 
5838c417dc1SYonghong Song 	memcpy(key_p, next_key_p, key_size);
584c14766a8SArtem Savkov 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -ENOENT);
5858c417dc1SYonghong Song 
5868c417dc1SYonghong Song 	/* Add one more element (total three) */
5878c417dc1SYonghong Song 	key_p->prefixlen = 24;
588da2577fdSJonathan Lemon 	inet_pton(AF_INET, "192.168.0.0", key_p->data);
5898c417dc1SYonghong Song 	assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
5908c417dc1SYonghong Song 
5918c417dc1SYonghong Song 	memset(key_p, 0, key_size);
5928c417dc1SYonghong Song 	assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
5938c417dc1SYonghong Song 	assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
5948c417dc1SYonghong Song 	       key_p->data[1] == 168 && key_p->data[2] == 0);
5958c417dc1SYonghong Song 
5968c417dc1SYonghong Song 	memset(next_key_p, 0, key_size);
5978c417dc1SYonghong Song 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
5988c417dc1SYonghong Song 	assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
5998c417dc1SYonghong Song 	       next_key_p->data[1] == 168 && next_key_p->data[2] == 128);
6008c417dc1SYonghong Song 
6018c417dc1SYonghong Song 	memcpy(key_p, next_key_p, key_size);
6028c417dc1SYonghong Song 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
6038c417dc1SYonghong Song 	assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
6048c417dc1SYonghong Song 	       next_key_p->data[1] == 168);
6058c417dc1SYonghong Song 
6068c417dc1SYonghong Song 	memcpy(key_p, next_key_p, key_size);
607c14766a8SArtem Savkov 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -ENOENT);
6088c417dc1SYonghong Song 
6098c417dc1SYonghong Song 	/* Add one more element (total four) */
6108c417dc1SYonghong Song 	key_p->prefixlen = 24;
6118c417dc1SYonghong Song 	inet_pton(AF_INET, "192.168.1.0", key_p->data);
6128c417dc1SYonghong Song 	assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
6138c417dc1SYonghong Song 
6148c417dc1SYonghong Song 	memset(key_p, 0, key_size);
6158c417dc1SYonghong Song 	assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
6168c417dc1SYonghong Song 	assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
6178c417dc1SYonghong Song 	       key_p->data[1] == 168 && key_p->data[2] == 0);
6188c417dc1SYonghong Song 
6198c417dc1SYonghong Song 	memset(next_key_p, 0, key_size);
6208c417dc1SYonghong Song 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
6218c417dc1SYonghong Song 	assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
6228c417dc1SYonghong Song 	       next_key_p->data[1] == 168 && next_key_p->data[2] == 1);
6238c417dc1SYonghong Song 
6248c417dc1SYonghong Song 	memcpy(key_p, next_key_p, key_size);
6258c417dc1SYonghong Song 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
6268c417dc1SYonghong Song 	assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
6278c417dc1SYonghong Song 	       next_key_p->data[1] == 168 && next_key_p->data[2] == 128);
6288c417dc1SYonghong Song 
6298c417dc1SYonghong Song 	memcpy(key_p, next_key_p, key_size);
6308c417dc1SYonghong Song 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
6318c417dc1SYonghong Song 	assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
6328c417dc1SYonghong Song 	       next_key_p->data[1] == 168);
6338c417dc1SYonghong Song 
6348c417dc1SYonghong Song 	memcpy(key_p, next_key_p, key_size);
635c14766a8SArtem Savkov 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -ENOENT);
6368c417dc1SYonghong Song 
637da2577fdSJonathan Lemon 	/* Add one more element (total five) */
638da2577fdSJonathan Lemon 	key_p->prefixlen = 28;
639da2577fdSJonathan Lemon 	inet_pton(AF_INET, "192.168.1.128", key_p->data);
640da2577fdSJonathan Lemon 	assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
641da2577fdSJonathan Lemon 
642da2577fdSJonathan Lemon 	memset(key_p, 0, key_size);
643da2577fdSJonathan Lemon 	assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
644da2577fdSJonathan Lemon 	assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
645da2577fdSJonathan Lemon 	       key_p->data[1] == 168 && key_p->data[2] == 0);
646da2577fdSJonathan Lemon 
647da2577fdSJonathan Lemon 	memset(next_key_p, 0, key_size);
648da2577fdSJonathan Lemon 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
649da2577fdSJonathan Lemon 	assert(next_key_p->prefixlen == 28 && next_key_p->data[0] == 192 &&
650da2577fdSJonathan Lemon 	       next_key_p->data[1] == 168 && next_key_p->data[2] == 1 &&
651da2577fdSJonathan Lemon 	       next_key_p->data[3] == 128);
652da2577fdSJonathan Lemon 
653da2577fdSJonathan Lemon 	memcpy(key_p, next_key_p, key_size);
654da2577fdSJonathan Lemon 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
655da2577fdSJonathan Lemon 	assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
656da2577fdSJonathan Lemon 	       next_key_p->data[1] == 168 && next_key_p->data[2] == 1);
657da2577fdSJonathan Lemon 
658da2577fdSJonathan Lemon 	memcpy(key_p, next_key_p, key_size);
659da2577fdSJonathan Lemon 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
660da2577fdSJonathan Lemon 	assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
661da2577fdSJonathan Lemon 	       next_key_p->data[1] == 168 && next_key_p->data[2] == 128);
662da2577fdSJonathan Lemon 
663da2577fdSJonathan Lemon 	memcpy(key_p, next_key_p, key_size);
664da2577fdSJonathan Lemon 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
665da2577fdSJonathan Lemon 	assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
666da2577fdSJonathan Lemon 	       next_key_p->data[1] == 168);
667da2577fdSJonathan Lemon 
668da2577fdSJonathan Lemon 	memcpy(key_p, next_key_p, key_size);
669c14766a8SArtem Savkov 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -ENOENT);
670da2577fdSJonathan Lemon 
6718c417dc1SYonghong Song 	/* no exact matching key should return the first one in post order */
6728c417dc1SYonghong Song 	key_p->prefixlen = 22;
6738c417dc1SYonghong Song 	inet_pton(AF_INET, "192.168.1.0", key_p->data);
6748c417dc1SYonghong Song 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
6758c417dc1SYonghong Song 	assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
6768c417dc1SYonghong Song 	       next_key_p->data[1] == 168 && next_key_p->data[2] == 0);
6778c417dc1SYonghong Song 
6788c417dc1SYonghong Song 	close(map_fd);
6798c417dc1SYonghong Song }
6808c417dc1SYonghong Song 
681af32efeeSYonghong Song #define MAX_TEST_KEYS	4
682af32efeeSYonghong Song struct lpm_mt_test_info {
683af32efeeSYonghong Song 	int cmd; /* 0: update, 1: delete, 2: lookup, 3: get_next_key */
684af32efeeSYonghong Song 	int iter;
685af32efeeSYonghong Song 	int map_fd;
686af32efeeSYonghong Song 	struct {
687af32efeeSYonghong Song 		__u32 prefixlen;
688af32efeeSYonghong Song 		__u32 data;
689af32efeeSYonghong Song 	} key[MAX_TEST_KEYS];
690af32efeeSYonghong Song };
691af32efeeSYonghong Song 
lpm_test_command(void * arg)692af32efeeSYonghong Song static void *lpm_test_command(void *arg)
693af32efeeSYonghong Song {
694af32efeeSYonghong Song 	int i, j, ret, iter, key_size;
695af32efeeSYonghong Song 	struct lpm_mt_test_info *info = arg;
696*ef33f029SKees Cook 	struct bpf_lpm_trie_key_u8 *key_p;
697af32efeeSYonghong Song 
698*ef33f029SKees Cook 	key_size = sizeof(*key_p) + sizeof(__u32);
699af32efeeSYonghong Song 	key_p = alloca(key_size);
700af32efeeSYonghong Song 	for (iter = 0; iter < info->iter; iter++)
701af32efeeSYonghong Song 		for (i = 0; i < MAX_TEST_KEYS; i++) {
702af32efeeSYonghong Song 			/* first half of iterations in forward order,
703af32efeeSYonghong Song 			 * and second half in backward order.
704af32efeeSYonghong Song 			 */
705af32efeeSYonghong Song 			j = (iter < (info->iter / 2)) ? i : MAX_TEST_KEYS - i - 1;
706af32efeeSYonghong Song 			key_p->prefixlen = info->key[j].prefixlen;
707af32efeeSYonghong Song 			memcpy(key_p->data, &info->key[j].data, sizeof(__u32));
708af32efeeSYonghong Song 			if (info->cmd == 0) {
709af32efeeSYonghong Song 				__u32 value = j;
710af32efeeSYonghong Song 				/* update must succeed */
711af32efeeSYonghong Song 				assert(bpf_map_update_elem(info->map_fd, key_p, &value, 0) == 0);
712af32efeeSYonghong Song 			} else if (info->cmd == 1) {
713af32efeeSYonghong Song 				ret = bpf_map_delete_elem(info->map_fd, key_p);
714af32efeeSYonghong Song 				assert(ret == 0 || errno == ENOENT);
715af32efeeSYonghong Song 			} else if (info->cmd == 2) {
716af32efeeSYonghong Song 				__u32 value;
717af32efeeSYonghong Song 				ret = bpf_map_lookup_elem(info->map_fd, key_p, &value);
718af32efeeSYonghong Song 				assert(ret == 0 || errno == ENOENT);
719af32efeeSYonghong Song 			} else {
720*ef33f029SKees Cook 				struct bpf_lpm_trie_key_u8 *next_key_p = alloca(key_size);
721af32efeeSYonghong Song 				ret = bpf_map_get_next_key(info->map_fd, key_p, next_key_p);
722af32efeeSYonghong Song 				assert(ret == 0 || errno == ENOENT || errno == ENOMEM);
723af32efeeSYonghong Song 			}
724af32efeeSYonghong Song 		}
725af32efeeSYonghong Song 
726af32efeeSYonghong Song 	// Pass successful exit info back to the main thread
727af32efeeSYonghong Song 	pthread_exit((void *)info);
728af32efeeSYonghong Song }
729af32efeeSYonghong Song 
setup_lpm_mt_test_info(struct lpm_mt_test_info * info,int map_fd)730af32efeeSYonghong Song static void setup_lpm_mt_test_info(struct lpm_mt_test_info *info, int map_fd)
731af32efeeSYonghong Song {
732af32efeeSYonghong Song 	info->iter = 2000;
733af32efeeSYonghong Song 	info->map_fd = map_fd;
734af32efeeSYonghong Song 	info->key[0].prefixlen = 16;
735af32efeeSYonghong Song 	inet_pton(AF_INET, "192.168.0.0", &info->key[0].data);
736af32efeeSYonghong Song 	info->key[1].prefixlen = 24;
737af32efeeSYonghong Song 	inet_pton(AF_INET, "192.168.0.0", &info->key[1].data);
738af32efeeSYonghong Song 	info->key[2].prefixlen = 24;
739af32efeeSYonghong Song 	inet_pton(AF_INET, "192.168.128.0", &info->key[2].data);
740af32efeeSYonghong Song 	info->key[3].prefixlen = 24;
741af32efeeSYonghong Song 	inet_pton(AF_INET, "192.168.1.0", &info->key[3].data);
742af32efeeSYonghong Song }
743af32efeeSYonghong Song 
test_lpm_multi_thread(void)744af32efeeSYonghong Song static void test_lpm_multi_thread(void)
745af32efeeSYonghong Song {
7462fe256a4SAndrii Nakryiko 	LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
747af32efeeSYonghong Song 	struct lpm_mt_test_info info[4];
748af32efeeSYonghong Song 	size_t key_size, value_size;
749af32efeeSYonghong Song 	pthread_t thread_id[4];
750af32efeeSYonghong Song 	int i, map_fd;
751af32efeeSYonghong Song 	void *ret;
752af32efeeSYonghong Song 
753af32efeeSYonghong Song 	/* create a trie */
754af32efeeSYonghong Song 	value_size = sizeof(__u32);
755*ef33f029SKees Cook 	key_size = sizeof(struct bpf_lpm_trie_key_hdr) + value_size;
7562fe256a4SAndrii Nakryiko 	map_fd = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, NULL, key_size, value_size, 100, &opts);
757af32efeeSYonghong Song 
758af32efeeSYonghong Song 	/* create 4 threads to test update, delete, lookup and get_next_key */
759af32efeeSYonghong Song 	setup_lpm_mt_test_info(&info[0], map_fd);
760af32efeeSYonghong Song 	for (i = 0; i < 4; i++) {
761af32efeeSYonghong Song 		if (i != 0)
762af32efeeSYonghong Song 			memcpy(&info[i], &info[0], sizeof(info[i]));
763af32efeeSYonghong Song 		info[i].cmd = i;
764af32efeeSYonghong Song 		assert(pthread_create(&thread_id[i], NULL, &lpm_test_command, &info[i]) == 0);
765af32efeeSYonghong Song 	}
766af32efeeSYonghong Song 
767af32efeeSYonghong Song 	for (i = 0; i < 4; i++)
768af32efeeSYonghong Song 		assert(pthread_join(thread_id[i], &ret) == 0 && ret == (void *)&info[i]);
769af32efeeSYonghong Song 
770af32efeeSYonghong Song 	close(map_fd);
771af32efeeSYonghong Song }
772af32efeeSYonghong Song 
main(void)7734d3381f5SDavid Herrmann int main(void)
7744d3381f5SDavid Herrmann {
775fe8d662aSDaniel Borkmann 	int i;
7764d3381f5SDavid Herrmann 
7774d3381f5SDavid Herrmann 	/* we want predictable, pseudo random tests */
7784d3381f5SDavid Herrmann 	srand(0xf00ba1);
7794d3381f5SDavid Herrmann 
780b858ba8cSYafang Shao 	/* Use libbpf 1.0 API mode */
781b858ba8cSYafang Shao 	libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
782b858ba8cSYafang Shao 
7834d3381f5SDavid Herrmann 	test_lpm_basic();
7844d3381f5SDavid Herrmann 	test_lpm_order();
7854d3381f5SDavid Herrmann 
7864d3381f5SDavid Herrmann 	/* Test with 8, 16, 24, 32, ... 128 bit prefix length */
7874d3381f5SDavid Herrmann 	for (i = 1; i <= 16; ++i)
7884d3381f5SDavid Herrmann 		test_lpm_map(i);
7894d3381f5SDavid Herrmann 
7904d3381f5SDavid Herrmann 	test_lpm_ipaddr();
791e8d17499SCraig Gallek 	test_lpm_delete();
7928c417dc1SYonghong Song 	test_lpm_get_next_key();
793af32efeeSYonghong Song 	test_lpm_multi_thread();
794af32efeeSYonghong Song 
7954d3381f5SDavid Herrmann 	printf("test_lpm: OK\n");
7964d3381f5SDavid Herrmann 	return 0;
7974d3381f5SDavid Herrmann }
798