Loading...
1/*
2 * Longest prefix match list implementation
3 *
4 * Copyright (c) 2016,2017 Daniel Mack
5 * Copyright (c) 2016 David Herrmann
6 *
7 * This file is subject to the terms and conditions of version 2 of the GNU
8 * General Public License. See the file COPYING in the main directory of the
9 * Linux distribution for more details.
10 */
11
12#include <linux/bpf.h>
13#include <linux/err.h>
14#include <linux/slab.h>
15#include <linux/spinlock.h>
16#include <linux/vmalloc.h>
17#include <net/ipv6.h>
18
19/* Intermediate node */
20#define LPM_TREE_NODE_FLAG_IM BIT(0)
21
22struct lpm_trie_node;
23
24struct lpm_trie_node {
25 struct rcu_head rcu;
26 struct lpm_trie_node __rcu *child[2];
27 u32 prefixlen;
28 u32 flags;
29 u8 data[0];
30};
31
32struct lpm_trie {
33 struct bpf_map map;
34 struct lpm_trie_node __rcu *root;
35 size_t n_entries;
36 size_t max_prefixlen;
37 size_t data_size;
38 raw_spinlock_t lock;
39};
40
41/* This trie implements a longest prefix match algorithm that can be used to
42 * match IP addresses to a stored set of ranges.
43 *
44 * Data stored in @data of struct bpf_lpm_key and struct lpm_trie_node is
45 * interpreted as big endian, so data[0] stores the most significant byte.
46 *
47 * Match ranges are internally stored in instances of struct lpm_trie_node
48 * which each contain their prefix length as well as two pointers that may
49 * lead to more nodes containing more specific matches. Each node also stores
50 * a value that is defined by and returned to userspace via the update_elem
51 * and lookup functions.
52 *
53 * For instance, let's start with a trie that was created with a prefix length
54 * of 32, so it can be used for IPv4 addresses, and one single element that
55 * matches 192.168.0.0/16. The data array would hence contain
56 * [0xc0, 0xa8, 0x00, 0x00] in big-endian notation. This documentation will
57 * stick to IP-address notation for readability though.
58 *
59 * As the trie is empty initially, the new node (1) will be places as root
60 * node, denoted as (R) in the example below. As there are no other node, both
61 * child pointers are %NULL.
62 *
63 * +----------------+
64 * | (1) (R) |
65 * | 192.168.0.0/16 |
66 * | value: 1 |
67 * | [0] [1] |
68 * +----------------+
69 *
70 * Next, let's add a new node (2) matching 192.168.0.0/24. As there is already
71 * a node with the same data and a smaller prefix (ie, a less specific one),
72 * node (2) will become a child of (1). In child index depends on the next bit
73 * that is outside of what (1) matches, and that bit is 0, so (2) will be
74 * child[0] of (1):
75 *
76 * +----------------+
77 * | (1) (R) |
78 * | 192.168.0.0/16 |
79 * | value: 1 |
80 * | [0] [1] |
81 * +----------------+
82 * |
83 * +----------------+
84 * | (2) |
85 * | 192.168.0.0/24 |
86 * | value: 2 |
87 * | [0] [1] |
88 * +----------------+
89 *
90 * The child[1] slot of (1) could be filled with another node which has bit #17
91 * (the next bit after the ones that (1) matches on) set to 1. For instance,
92 * 192.168.128.0/24:
93 *
94 * +----------------+
95 * | (1) (R) |
96 * | 192.168.0.0/16 |
97 * | value: 1 |
98 * | [0] [1] |
99 * +----------------+
100 * | |
101 * +----------------+ +------------------+
102 * | (2) | | (3) |
103 * | 192.168.0.0/24 | | 192.168.128.0/24 |
104 * | value: 2 | | value: 3 |
105 * | [0] [1] | | [0] [1] |
106 * +----------------+ +------------------+
107 *
108 * Let's add another node (4) to the game for 192.168.1.0/24. In order to place
109 * it, node (1) is looked at first, and because (4) of the semantics laid out
110 * above (bit #17 is 0), it would normally be attached to (1) as child[0].
111 * However, that slot is already allocated, so a new node is needed in between.
112 * That node does not have a value attached to it and it will never be
113 * returned to users as result of a lookup. It is only there to differentiate
114 * the traversal further. It will get a prefix as wide as necessary to
115 * distinguish its two children:
116 *
117 * +----------------+
118 * | (1) (R) |
119 * | 192.168.0.0/16 |
120 * | value: 1 |
121 * | [0] [1] |
122 * +----------------+
123 * | |
124 * +----------------+ +------------------+
125 * | (4) (I) | | (3) |
126 * | 192.168.0.0/23 | | 192.168.128.0/24 |
127 * | value: --- | | value: 3 |
128 * | [0] [1] | | [0] [1] |
129 * +----------------+ +------------------+
130 * | |
131 * +----------------+ +----------------+
132 * | (2) | | (5) |
133 * | 192.168.0.0/24 | | 192.168.1.0/24 |
134 * | value: 2 | | value: 5 |
135 * | [0] [1] | | [0] [1] |
136 * +----------------+ +----------------+
137 *
138 * 192.168.1.1/32 would be a child of (5) etc.
139 *
140 * An intermediate node will be turned into a 'real' node on demand. In the
141 * example above, (4) would be re-used if 192.168.0.0/23 is added to the trie.
142 *
143 * A fully populated trie would have a height of 32 nodes, as the trie was
144 * created with a prefix length of 32.
145 *
146 * The lookup starts at the root node. If the current node matches and if there
147 * is a child that can be used to become more specific, the trie is traversed
148 * downwards. The last node in the traversal that is a non-intermediate one is
149 * returned.
150 */
151
152static inline int extract_bit(const u8 *data, size_t index)
153{
154 return !!(data[index / 8] & (1 << (7 - (index % 8))));
155}
156
157/**
158 * longest_prefix_match() - determine the longest prefix
159 * @trie: The trie to get internal sizes from
160 * @node: The node to operate on
161 * @key: The key to compare to @node
162 *
163 * Determine the longest prefix of @node that matches the bits in @key.
164 */
165static size_t longest_prefix_match(const struct lpm_trie *trie,
166 const struct lpm_trie_node *node,
167 const struct bpf_lpm_trie_key *key)
168{
169 size_t prefixlen = 0;
170 size_t i;
171
172 for (i = 0; i < trie->data_size; i++) {
173 size_t b;
174
175 b = 8 - fls(node->data[i] ^ key->data[i]);
176 prefixlen += b;
177
178 if (prefixlen >= node->prefixlen || prefixlen >= key->prefixlen)
179 return min(node->prefixlen, key->prefixlen);
180
181 if (b < 8)
182 break;
183 }
184
185 return prefixlen;
186}
187
188/* Called from syscall or from eBPF program */
189static void *trie_lookup_elem(struct bpf_map *map, void *_key)
190{
191 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
192 struct lpm_trie_node *node, *found = NULL;
193 struct bpf_lpm_trie_key *key = _key;
194
195 /* Start walking the trie from the root node ... */
196
197 for (node = rcu_dereference(trie->root); node;) {
198 unsigned int next_bit;
199 size_t matchlen;
200
201 /* Determine the longest prefix of @node that matches @key.
202 * If it's the maximum possible prefix for this trie, we have
203 * an exact match and can return it directly.
204 */
205 matchlen = longest_prefix_match(trie, node, key);
206 if (matchlen == trie->max_prefixlen) {
207 found = node;
208 break;
209 }
210
211 /* If the number of bits that match is smaller than the prefix
212 * length of @node, bail out and return the node we have seen
213 * last in the traversal (ie, the parent).
214 */
215 if (matchlen < node->prefixlen)
216 break;
217
218 /* Consider this node as return candidate unless it is an
219 * artificially added intermediate one.
220 */
221 if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
222 found = node;
223
224 /* If the node match is fully satisfied, let's see if we can
225 * become more specific. Determine the next bit in the key and
226 * traverse down.
227 */
228 next_bit = extract_bit(key->data, node->prefixlen);
229 node = rcu_dereference(node->child[next_bit]);
230 }
231
232 if (!found)
233 return NULL;
234
235 return found->data + trie->data_size;
236}
237
238static struct lpm_trie_node *lpm_trie_node_alloc(const struct lpm_trie *trie,
239 const void *value)
240{
241 struct lpm_trie_node *node;
242 size_t size = sizeof(struct lpm_trie_node) + trie->data_size;
243
244 if (value)
245 size += trie->map.value_size;
246
247 node = kmalloc_node(size, GFP_ATOMIC | __GFP_NOWARN,
248 trie->map.numa_node);
249 if (!node)
250 return NULL;
251
252 node->flags = 0;
253
254 if (value)
255 memcpy(node->data + trie->data_size, value,
256 trie->map.value_size);
257
258 return node;
259}
260
261/* Called from syscall or from eBPF program */
262static int trie_update_elem(struct bpf_map *map,
263 void *_key, void *value, u64 flags)
264{
265 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
266 struct lpm_trie_node *node, *im_node = NULL, *new_node = NULL;
267 struct lpm_trie_node __rcu **slot;
268 struct bpf_lpm_trie_key *key = _key;
269 unsigned long irq_flags;
270 unsigned int next_bit;
271 size_t matchlen = 0;
272 int ret = 0;
273
274 if (unlikely(flags > BPF_EXIST))
275 return -EINVAL;
276
277 if (key->prefixlen > trie->max_prefixlen)
278 return -EINVAL;
279
280 raw_spin_lock_irqsave(&trie->lock, irq_flags);
281
282 /* Allocate and fill a new node */
283
284 if (trie->n_entries == trie->map.max_entries) {
285 ret = -ENOSPC;
286 goto out;
287 }
288
289 new_node = lpm_trie_node_alloc(trie, value);
290 if (!new_node) {
291 ret = -ENOMEM;
292 goto out;
293 }
294
295 trie->n_entries++;
296
297 new_node->prefixlen = key->prefixlen;
298 RCU_INIT_POINTER(new_node->child[0], NULL);
299 RCU_INIT_POINTER(new_node->child[1], NULL);
300 memcpy(new_node->data, key->data, trie->data_size);
301
302 /* Now find a slot to attach the new node. To do that, walk the tree
303 * from the root and match as many bits as possible for each node until
304 * we either find an empty slot or a slot that needs to be replaced by
305 * an intermediate node.
306 */
307 slot = &trie->root;
308
309 while ((node = rcu_dereference_protected(*slot,
310 lockdep_is_held(&trie->lock)))) {
311 matchlen = longest_prefix_match(trie, node, key);
312
313 if (node->prefixlen != matchlen ||
314 node->prefixlen == key->prefixlen ||
315 node->prefixlen == trie->max_prefixlen)
316 break;
317
318 next_bit = extract_bit(key->data, node->prefixlen);
319 slot = &node->child[next_bit];
320 }
321
322 /* If the slot is empty (a free child pointer or an empty root),
323 * simply assign the @new_node to that slot and be done.
324 */
325 if (!node) {
326 rcu_assign_pointer(*slot, new_node);
327 goto out;
328 }
329
330 /* If the slot we picked already exists, replace it with @new_node
331 * which already has the correct data array set.
332 */
333 if (node->prefixlen == matchlen) {
334 new_node->child[0] = node->child[0];
335 new_node->child[1] = node->child[1];
336
337 if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
338 trie->n_entries--;
339
340 rcu_assign_pointer(*slot, new_node);
341 kfree_rcu(node, rcu);
342
343 goto out;
344 }
345
346 /* If the new node matches the prefix completely, it must be inserted
347 * as an ancestor. Simply insert it between @node and *@slot.
348 */
349 if (matchlen == key->prefixlen) {
350 next_bit = extract_bit(node->data, matchlen);
351 rcu_assign_pointer(new_node->child[next_bit], node);
352 rcu_assign_pointer(*slot, new_node);
353 goto out;
354 }
355
356 im_node = lpm_trie_node_alloc(trie, NULL);
357 if (!im_node) {
358 ret = -ENOMEM;
359 goto out;
360 }
361
362 im_node->prefixlen = matchlen;
363 im_node->flags |= LPM_TREE_NODE_FLAG_IM;
364 memcpy(im_node->data, node->data, trie->data_size);
365
366 /* Now determine which child to install in which slot */
367 if (extract_bit(key->data, matchlen)) {
368 rcu_assign_pointer(im_node->child[0], node);
369 rcu_assign_pointer(im_node->child[1], new_node);
370 } else {
371 rcu_assign_pointer(im_node->child[0], new_node);
372 rcu_assign_pointer(im_node->child[1], node);
373 }
374
375 /* Finally, assign the intermediate node to the determined spot */
376 rcu_assign_pointer(*slot, im_node);
377
378out:
379 if (ret) {
380 if (new_node)
381 trie->n_entries--;
382
383 kfree(new_node);
384 kfree(im_node);
385 }
386
387 raw_spin_unlock_irqrestore(&trie->lock, irq_flags);
388
389 return ret;
390}
391
392/* Called from syscall or from eBPF program */
393static int trie_delete_elem(struct bpf_map *map, void *_key)
394{
395 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
396 struct bpf_lpm_trie_key *key = _key;
397 struct lpm_trie_node __rcu **trim, **trim2;
398 struct lpm_trie_node *node, *parent;
399 unsigned long irq_flags;
400 unsigned int next_bit;
401 size_t matchlen = 0;
402 int ret = 0;
403
404 if (key->prefixlen > trie->max_prefixlen)
405 return -EINVAL;
406
407 raw_spin_lock_irqsave(&trie->lock, irq_flags);
408
409 /* Walk the tree looking for an exact key/length match and keeping
410 * track of the path we traverse. We will need to know the node
411 * we wish to delete, and the slot that points to the node we want
412 * to delete. We may also need to know the nodes parent and the
413 * slot that contains it.
414 */
415 trim = &trie->root;
416 trim2 = trim;
417 parent = NULL;
418 while ((node = rcu_dereference_protected(
419 *trim, lockdep_is_held(&trie->lock)))) {
420 matchlen = longest_prefix_match(trie, node, key);
421
422 if (node->prefixlen != matchlen ||
423 node->prefixlen == key->prefixlen)
424 break;
425
426 parent = node;
427 trim2 = trim;
428 next_bit = extract_bit(key->data, node->prefixlen);
429 trim = &node->child[next_bit];
430 }
431
432 if (!node || node->prefixlen != key->prefixlen ||
433 (node->flags & LPM_TREE_NODE_FLAG_IM)) {
434 ret = -ENOENT;
435 goto out;
436 }
437
438 trie->n_entries--;
439
440 /* If the node we are removing has two children, simply mark it
441 * as intermediate and we are done.
442 */
443 if (rcu_access_pointer(node->child[0]) &&
444 rcu_access_pointer(node->child[1])) {
445 node->flags |= LPM_TREE_NODE_FLAG_IM;
446 goto out;
447 }
448
449 /* If the parent of the node we are about to delete is an intermediate
450 * node, and the deleted node doesn't have any children, we can delete
451 * the intermediate parent as well and promote its other child
452 * up the tree. Doing this maintains the invariant that all
453 * intermediate nodes have exactly 2 children and that there are no
454 * unnecessary intermediate nodes in the tree.
455 */
456 if (parent && (parent->flags & LPM_TREE_NODE_FLAG_IM) &&
457 !node->child[0] && !node->child[1]) {
458 if (node == rcu_access_pointer(parent->child[0]))
459 rcu_assign_pointer(
460 *trim2, rcu_access_pointer(parent->child[1]));
461 else
462 rcu_assign_pointer(
463 *trim2, rcu_access_pointer(parent->child[0]));
464 kfree_rcu(parent, rcu);
465 kfree_rcu(node, rcu);
466 goto out;
467 }
468
469 /* The node we are removing has either zero or one child. If there
470 * is a child, move it into the removed node's slot then delete
471 * the node. Otherwise just clear the slot and delete the node.
472 */
473 if (node->child[0])
474 rcu_assign_pointer(*trim, rcu_access_pointer(node->child[0]));
475 else if (node->child[1])
476 rcu_assign_pointer(*trim, rcu_access_pointer(node->child[1]));
477 else
478 RCU_INIT_POINTER(*trim, NULL);
479 kfree_rcu(node, rcu);
480
481out:
482 raw_spin_unlock_irqrestore(&trie->lock, irq_flags);
483
484 return ret;
485}
486
487#define LPM_DATA_SIZE_MAX 256
488#define LPM_DATA_SIZE_MIN 1
489
490#define LPM_VAL_SIZE_MAX (KMALLOC_MAX_SIZE - LPM_DATA_SIZE_MAX - \
491 sizeof(struct lpm_trie_node))
492#define LPM_VAL_SIZE_MIN 1
493
494#define LPM_KEY_SIZE(X) (sizeof(struct bpf_lpm_trie_key) + (X))
495#define LPM_KEY_SIZE_MAX LPM_KEY_SIZE(LPM_DATA_SIZE_MAX)
496#define LPM_KEY_SIZE_MIN LPM_KEY_SIZE(LPM_DATA_SIZE_MIN)
497
498#define LPM_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_NUMA_NODE | \
499 BPF_F_RDONLY | BPF_F_WRONLY)
500
501static struct bpf_map *trie_alloc(union bpf_attr *attr)
502{
503 struct lpm_trie *trie;
504 u64 cost = sizeof(*trie), cost_per_node;
505 int ret;
506
507 if (!capable(CAP_SYS_ADMIN))
508 return ERR_PTR(-EPERM);
509
510 /* check sanity of attributes */
511 if (attr->max_entries == 0 ||
512 !(attr->map_flags & BPF_F_NO_PREALLOC) ||
513 attr->map_flags & ~LPM_CREATE_FLAG_MASK ||
514 attr->key_size < LPM_KEY_SIZE_MIN ||
515 attr->key_size > LPM_KEY_SIZE_MAX ||
516 attr->value_size < LPM_VAL_SIZE_MIN ||
517 attr->value_size > LPM_VAL_SIZE_MAX)
518 return ERR_PTR(-EINVAL);
519
520 trie = kzalloc(sizeof(*trie), GFP_USER | __GFP_NOWARN);
521 if (!trie)
522 return ERR_PTR(-ENOMEM);
523
524 /* copy mandatory map attributes */
525 bpf_map_init_from_attr(&trie->map, attr);
526 trie->data_size = attr->key_size -
527 offsetof(struct bpf_lpm_trie_key, data);
528 trie->max_prefixlen = trie->data_size * 8;
529
530 cost_per_node = sizeof(struct lpm_trie_node) +
531 attr->value_size + trie->data_size;
532 cost += (u64) attr->max_entries * cost_per_node;
533 if (cost >= U32_MAX - PAGE_SIZE) {
534 ret = -E2BIG;
535 goto out_err;
536 }
537
538 trie->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
539
540 ret = bpf_map_precharge_memlock(trie->map.pages);
541 if (ret)
542 goto out_err;
543
544 raw_spin_lock_init(&trie->lock);
545
546 return &trie->map;
547out_err:
548 kfree(trie);
549 return ERR_PTR(ret);
550}
551
552static void trie_free(struct bpf_map *map)
553{
554 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
555 struct lpm_trie_node __rcu **slot;
556 struct lpm_trie_node *node;
557
558 /* Wait for outstanding programs to complete
559 * update/lookup/delete/get_next_key and free the trie.
560 */
561 synchronize_rcu();
562
563 /* Always start at the root and walk down to a node that has no
564 * children. Then free that node, nullify its reference in the parent
565 * and start over.
566 */
567
568 for (;;) {
569 slot = &trie->root;
570
571 for (;;) {
572 node = rcu_dereference_protected(*slot, 1);
573 if (!node)
574 goto out;
575
576 if (rcu_access_pointer(node->child[0])) {
577 slot = &node->child[0];
578 continue;
579 }
580
581 if (rcu_access_pointer(node->child[1])) {
582 slot = &node->child[1];
583 continue;
584 }
585
586 kfree(node);
587 RCU_INIT_POINTER(*slot, NULL);
588 break;
589 }
590 }
591
592out:
593 kfree(trie);
594}
595
596static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key)
597{
598 struct lpm_trie_node *node, *next_node = NULL, *parent, *search_root;
599 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
600 struct bpf_lpm_trie_key *key = _key, *next_key = _next_key;
601 struct lpm_trie_node **node_stack = NULL;
602 int err = 0, stack_ptr = -1;
603 unsigned int next_bit;
604 size_t matchlen;
605
606 /* The get_next_key follows postorder. For the 4 node example in
607 * the top of this file, the trie_get_next_key() returns the following
608 * one after another:
609 * 192.168.0.0/24
610 * 192.168.1.0/24
611 * 192.168.128.0/24
612 * 192.168.0.0/16
613 *
614 * The idea is to return more specific keys before less specific ones.
615 */
616
617 /* Empty trie */
618 search_root = rcu_dereference(trie->root);
619 if (!search_root)
620 return -ENOENT;
621
622 /* For invalid key, find the leftmost node in the trie */
623 if (!key || key->prefixlen > trie->max_prefixlen)
624 goto find_leftmost;
625
626 node_stack = kmalloc(trie->max_prefixlen * sizeof(struct lpm_trie_node *),
627 GFP_ATOMIC | __GFP_NOWARN);
628 if (!node_stack)
629 return -ENOMEM;
630
631 /* Try to find the exact node for the given key */
632 for (node = search_root; node;) {
633 node_stack[++stack_ptr] = node;
634 matchlen = longest_prefix_match(trie, node, key);
635 if (node->prefixlen != matchlen ||
636 node->prefixlen == key->prefixlen)
637 break;
638
639 next_bit = extract_bit(key->data, node->prefixlen);
640 node = rcu_dereference(node->child[next_bit]);
641 }
642 if (!node || node->prefixlen != key->prefixlen ||
643 (node->flags & LPM_TREE_NODE_FLAG_IM))
644 goto find_leftmost;
645
646 /* The node with the exactly-matching key has been found,
647 * find the first node in postorder after the matched node.
648 */
649 node = node_stack[stack_ptr];
650 while (stack_ptr > 0) {
651 parent = node_stack[stack_ptr - 1];
652 if (rcu_dereference(parent->child[0]) == node) {
653 search_root = rcu_dereference(parent->child[1]);
654 if (search_root)
655 goto find_leftmost;
656 }
657 if (!(parent->flags & LPM_TREE_NODE_FLAG_IM)) {
658 next_node = parent;
659 goto do_copy;
660 }
661
662 node = parent;
663 stack_ptr--;
664 }
665
666 /* did not find anything */
667 err = -ENOENT;
668 goto free_stack;
669
670find_leftmost:
671 /* Find the leftmost non-intermediate node, all intermediate nodes
672 * have exact two children, so this function will never return NULL.
673 */
674 for (node = search_root; node;) {
675 if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
676 next_node = node;
677 node = rcu_dereference(node->child[0]);
678 }
679do_copy:
680 next_key->prefixlen = next_node->prefixlen;
681 memcpy((void *)next_key + offsetof(struct bpf_lpm_trie_key, data),
682 next_node->data, trie->data_size);
683free_stack:
684 kfree(node_stack);
685 return err;
686}
687
688const struct bpf_map_ops trie_map_ops = {
689 .map_alloc = trie_alloc,
690 .map_free = trie_free,
691 .map_get_next_key = trie_get_next_key,
692 .map_lookup_elem = trie_lookup_elem,
693 .map_update_elem = trie_update_elem,
694 .map_delete_elem = trie_delete_elem,
695};
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Longest prefix match list implementation
4 *
5 * Copyright (c) 2016,2017 Daniel Mack
6 * Copyright (c) 2016 David Herrmann
7 */
8
9#include <linux/bpf.h>
10#include <linux/btf.h>
11#include <linux/err.h>
12#include <linux/slab.h>
13#include <linux/spinlock.h>
14#include <linux/vmalloc.h>
15#include <net/ipv6.h>
16#include <uapi/linux/btf.h>
17
18/* Intermediate node */
19#define LPM_TREE_NODE_FLAG_IM BIT(0)
20
21struct lpm_trie_node;
22
23struct lpm_trie_node {
24 struct rcu_head rcu;
25 struct lpm_trie_node __rcu *child[2];
26 u32 prefixlen;
27 u32 flags;
28 u8 data[];
29};
30
31struct lpm_trie {
32 struct bpf_map map;
33 struct lpm_trie_node __rcu *root;
34 size_t n_entries;
35 size_t max_prefixlen;
36 size_t data_size;
37 spinlock_t lock;
38};
39
40/* This trie implements a longest prefix match algorithm that can be used to
41 * match IP addresses to a stored set of ranges.
42 *
43 * Data stored in @data of struct bpf_lpm_key and struct lpm_trie_node is
44 * interpreted as big endian, so data[0] stores the most significant byte.
45 *
46 * Match ranges are internally stored in instances of struct lpm_trie_node
47 * which each contain their prefix length as well as two pointers that may
48 * lead to more nodes containing more specific matches. Each node also stores
49 * a value that is defined by and returned to userspace via the update_elem
50 * and lookup functions.
51 *
52 * For instance, let's start with a trie that was created with a prefix length
53 * of 32, so it can be used for IPv4 addresses, and one single element that
54 * matches 192.168.0.0/16. The data array would hence contain
55 * [0xc0, 0xa8, 0x00, 0x00] in big-endian notation. This documentation will
56 * stick to IP-address notation for readability though.
57 *
58 * As the trie is empty initially, the new node (1) will be places as root
59 * node, denoted as (R) in the example below. As there are no other node, both
60 * child pointers are %NULL.
61 *
62 * +----------------+
63 * | (1) (R) |
64 * | 192.168.0.0/16 |
65 * | value: 1 |
66 * | [0] [1] |
67 * +----------------+
68 *
69 * Next, let's add a new node (2) matching 192.168.0.0/24. As there is already
70 * a node with the same data and a smaller prefix (ie, a less specific one),
71 * node (2) will become a child of (1). In child index depends on the next bit
72 * that is outside of what (1) matches, and that bit is 0, so (2) will be
73 * child[0] of (1):
74 *
75 * +----------------+
76 * | (1) (R) |
77 * | 192.168.0.0/16 |
78 * | value: 1 |
79 * | [0] [1] |
80 * +----------------+
81 * |
82 * +----------------+
83 * | (2) |
84 * | 192.168.0.0/24 |
85 * | value: 2 |
86 * | [0] [1] |
87 * +----------------+
88 *
89 * The child[1] slot of (1) could be filled with another node which has bit #17
90 * (the next bit after the ones that (1) matches on) set to 1. For instance,
91 * 192.168.128.0/24:
92 *
93 * +----------------+
94 * | (1) (R) |
95 * | 192.168.0.0/16 |
96 * | value: 1 |
97 * | [0] [1] |
98 * +----------------+
99 * | |
100 * +----------------+ +------------------+
101 * | (2) | | (3) |
102 * | 192.168.0.0/24 | | 192.168.128.0/24 |
103 * | value: 2 | | value: 3 |
104 * | [0] [1] | | [0] [1] |
105 * +----------------+ +------------------+
106 *
107 * Let's add another node (4) to the game for 192.168.1.0/24. In order to place
108 * it, node (1) is looked at first, and because (4) of the semantics laid out
109 * above (bit #17 is 0), it would normally be attached to (1) as child[0].
110 * However, that slot is already allocated, so a new node is needed in between.
111 * That node does not have a value attached to it and it will never be
112 * returned to users as result of a lookup. It is only there to differentiate
113 * the traversal further. It will get a prefix as wide as necessary to
114 * distinguish its two children:
115 *
116 * +----------------+
117 * | (1) (R) |
118 * | 192.168.0.0/16 |
119 * | value: 1 |
120 * | [0] [1] |
121 * +----------------+
122 * | |
123 * +----------------+ +------------------+
124 * | (4) (I) | | (3) |
125 * | 192.168.0.0/23 | | 192.168.128.0/24 |
126 * | value: --- | | value: 3 |
127 * | [0] [1] | | [0] [1] |
128 * +----------------+ +------------------+
129 * | |
130 * +----------------+ +----------------+
131 * | (2) | | (5) |
132 * | 192.168.0.0/24 | | 192.168.1.0/24 |
133 * | value: 2 | | value: 5 |
134 * | [0] [1] | | [0] [1] |
135 * +----------------+ +----------------+
136 *
137 * 192.168.1.1/32 would be a child of (5) etc.
138 *
139 * An intermediate node will be turned into a 'real' node on demand. In the
140 * example above, (4) would be re-used if 192.168.0.0/23 is added to the trie.
141 *
142 * A fully populated trie would have a height of 32 nodes, as the trie was
143 * created with a prefix length of 32.
144 *
145 * The lookup starts at the root node. If the current node matches and if there
146 * is a child that can be used to become more specific, the trie is traversed
147 * downwards. The last node in the traversal that is a non-intermediate one is
148 * returned.
149 */
150
151static inline int extract_bit(const u8 *data, size_t index)
152{
153 return !!(data[index / 8] & (1 << (7 - (index % 8))));
154}
155
156/**
157 * longest_prefix_match() - determine the longest prefix
158 * @trie: The trie to get internal sizes from
159 * @node: The node to operate on
160 * @key: The key to compare to @node
161 *
162 * Determine the longest prefix of @node that matches the bits in @key.
163 */
164static size_t longest_prefix_match(const struct lpm_trie *trie,
165 const struct lpm_trie_node *node,
166 const struct bpf_lpm_trie_key *key)
167{
168 u32 limit = min(node->prefixlen, key->prefixlen);
169 u32 prefixlen = 0, i = 0;
170
171 BUILD_BUG_ON(offsetof(struct lpm_trie_node, data) % sizeof(u32));
172 BUILD_BUG_ON(offsetof(struct bpf_lpm_trie_key, data) % sizeof(u32));
173
174#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && defined(CONFIG_64BIT)
175
176 /* data_size >= 16 has very small probability.
177 * We do not use a loop for optimal code generation.
178 */
179 if (trie->data_size >= 8) {
180 u64 diff = be64_to_cpu(*(__be64 *)node->data ^
181 *(__be64 *)key->data);
182
183 prefixlen = 64 - fls64(diff);
184 if (prefixlen >= limit)
185 return limit;
186 if (diff)
187 return prefixlen;
188 i = 8;
189 }
190#endif
191
192 while (trie->data_size >= i + 4) {
193 u32 diff = be32_to_cpu(*(__be32 *)&node->data[i] ^
194 *(__be32 *)&key->data[i]);
195
196 prefixlen += 32 - fls(diff);
197 if (prefixlen >= limit)
198 return limit;
199 if (diff)
200 return prefixlen;
201 i += 4;
202 }
203
204 if (trie->data_size >= i + 2) {
205 u16 diff = be16_to_cpu(*(__be16 *)&node->data[i] ^
206 *(__be16 *)&key->data[i]);
207
208 prefixlen += 16 - fls(diff);
209 if (prefixlen >= limit)
210 return limit;
211 if (diff)
212 return prefixlen;
213 i += 2;
214 }
215
216 if (trie->data_size >= i + 1) {
217 prefixlen += 8 - fls(node->data[i] ^ key->data[i]);
218
219 if (prefixlen >= limit)
220 return limit;
221 }
222
223 return prefixlen;
224}
225
226/* Called from syscall or from eBPF program */
227static void *trie_lookup_elem(struct bpf_map *map, void *_key)
228{
229 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
230 struct lpm_trie_node *node, *found = NULL;
231 struct bpf_lpm_trie_key *key = _key;
232
233 /* Start walking the trie from the root node ... */
234
235 for (node = rcu_dereference_check(trie->root, rcu_read_lock_bh_held());
236 node;) {
237 unsigned int next_bit;
238 size_t matchlen;
239
240 /* Determine the longest prefix of @node that matches @key.
241 * If it's the maximum possible prefix for this trie, we have
242 * an exact match and can return it directly.
243 */
244 matchlen = longest_prefix_match(trie, node, key);
245 if (matchlen == trie->max_prefixlen) {
246 found = node;
247 break;
248 }
249
250 /* If the number of bits that match is smaller than the prefix
251 * length of @node, bail out and return the node we have seen
252 * last in the traversal (ie, the parent).
253 */
254 if (matchlen < node->prefixlen)
255 break;
256
257 /* Consider this node as return candidate unless it is an
258 * artificially added intermediate one.
259 */
260 if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
261 found = node;
262
263 /* If the node match is fully satisfied, let's see if we can
264 * become more specific. Determine the next bit in the key and
265 * traverse down.
266 */
267 next_bit = extract_bit(key->data, node->prefixlen);
268 node = rcu_dereference_check(node->child[next_bit],
269 rcu_read_lock_bh_held());
270 }
271
272 if (!found)
273 return NULL;
274
275 return found->data + trie->data_size;
276}
277
278static struct lpm_trie_node *lpm_trie_node_alloc(const struct lpm_trie *trie,
279 const void *value)
280{
281 struct lpm_trie_node *node;
282 size_t size = sizeof(struct lpm_trie_node) + trie->data_size;
283
284 if (value)
285 size += trie->map.value_size;
286
287 node = bpf_map_kmalloc_node(&trie->map, size, GFP_ATOMIC | __GFP_NOWARN,
288 trie->map.numa_node);
289 if (!node)
290 return NULL;
291
292 node->flags = 0;
293
294 if (value)
295 memcpy(node->data + trie->data_size, value,
296 trie->map.value_size);
297
298 return node;
299}
300
301/* Called from syscall or from eBPF program */
302static int trie_update_elem(struct bpf_map *map,
303 void *_key, void *value, u64 flags)
304{
305 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
306 struct lpm_trie_node *node, *im_node = NULL, *new_node = NULL;
307 struct lpm_trie_node __rcu **slot;
308 struct bpf_lpm_trie_key *key = _key;
309 unsigned long irq_flags;
310 unsigned int next_bit;
311 size_t matchlen = 0;
312 int ret = 0;
313
314 if (unlikely(flags > BPF_EXIST))
315 return -EINVAL;
316
317 if (key->prefixlen > trie->max_prefixlen)
318 return -EINVAL;
319
320 spin_lock_irqsave(&trie->lock, irq_flags);
321
322 /* Allocate and fill a new node */
323
324 if (trie->n_entries == trie->map.max_entries) {
325 ret = -ENOSPC;
326 goto out;
327 }
328
329 new_node = lpm_trie_node_alloc(trie, value);
330 if (!new_node) {
331 ret = -ENOMEM;
332 goto out;
333 }
334
335 trie->n_entries++;
336
337 new_node->prefixlen = key->prefixlen;
338 RCU_INIT_POINTER(new_node->child[0], NULL);
339 RCU_INIT_POINTER(new_node->child[1], NULL);
340 memcpy(new_node->data, key->data, trie->data_size);
341
342 /* Now find a slot to attach the new node. To do that, walk the tree
343 * from the root and match as many bits as possible for each node until
344 * we either find an empty slot or a slot that needs to be replaced by
345 * an intermediate node.
346 */
347 slot = &trie->root;
348
349 while ((node = rcu_dereference_protected(*slot,
350 lockdep_is_held(&trie->lock)))) {
351 matchlen = longest_prefix_match(trie, node, key);
352
353 if (node->prefixlen != matchlen ||
354 node->prefixlen == key->prefixlen ||
355 node->prefixlen == trie->max_prefixlen)
356 break;
357
358 next_bit = extract_bit(key->data, node->prefixlen);
359 slot = &node->child[next_bit];
360 }
361
362 /* If the slot is empty (a free child pointer or an empty root),
363 * simply assign the @new_node to that slot and be done.
364 */
365 if (!node) {
366 rcu_assign_pointer(*slot, new_node);
367 goto out;
368 }
369
370 /* If the slot we picked already exists, replace it with @new_node
371 * which already has the correct data array set.
372 */
373 if (node->prefixlen == matchlen) {
374 new_node->child[0] = node->child[0];
375 new_node->child[1] = node->child[1];
376
377 if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
378 trie->n_entries--;
379
380 rcu_assign_pointer(*slot, new_node);
381 kfree_rcu(node, rcu);
382
383 goto out;
384 }
385
386 /* If the new node matches the prefix completely, it must be inserted
387 * as an ancestor. Simply insert it between @node and *@slot.
388 */
389 if (matchlen == key->prefixlen) {
390 next_bit = extract_bit(node->data, matchlen);
391 rcu_assign_pointer(new_node->child[next_bit], node);
392 rcu_assign_pointer(*slot, new_node);
393 goto out;
394 }
395
396 im_node = lpm_trie_node_alloc(trie, NULL);
397 if (!im_node) {
398 ret = -ENOMEM;
399 goto out;
400 }
401
402 im_node->prefixlen = matchlen;
403 im_node->flags |= LPM_TREE_NODE_FLAG_IM;
404 memcpy(im_node->data, node->data, trie->data_size);
405
406 /* Now determine which child to install in which slot */
407 if (extract_bit(key->data, matchlen)) {
408 rcu_assign_pointer(im_node->child[0], node);
409 rcu_assign_pointer(im_node->child[1], new_node);
410 } else {
411 rcu_assign_pointer(im_node->child[0], new_node);
412 rcu_assign_pointer(im_node->child[1], node);
413 }
414
415 /* Finally, assign the intermediate node to the determined spot */
416 rcu_assign_pointer(*slot, im_node);
417
418out:
419 if (ret) {
420 if (new_node)
421 trie->n_entries--;
422
423 kfree(new_node);
424 kfree(im_node);
425 }
426
427 spin_unlock_irqrestore(&trie->lock, irq_flags);
428
429 return ret;
430}
431
432/* Called from syscall or from eBPF program */
433static int trie_delete_elem(struct bpf_map *map, void *_key)
434{
435 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
436 struct bpf_lpm_trie_key *key = _key;
437 struct lpm_trie_node __rcu **trim, **trim2;
438 struct lpm_trie_node *node, *parent;
439 unsigned long irq_flags;
440 unsigned int next_bit;
441 size_t matchlen = 0;
442 int ret = 0;
443
444 if (key->prefixlen > trie->max_prefixlen)
445 return -EINVAL;
446
447 spin_lock_irqsave(&trie->lock, irq_flags);
448
449 /* Walk the tree looking for an exact key/length match and keeping
450 * track of the path we traverse. We will need to know the node
451 * we wish to delete, and the slot that points to the node we want
452 * to delete. We may also need to know the nodes parent and the
453 * slot that contains it.
454 */
455 trim = &trie->root;
456 trim2 = trim;
457 parent = NULL;
458 while ((node = rcu_dereference_protected(
459 *trim, lockdep_is_held(&trie->lock)))) {
460 matchlen = longest_prefix_match(trie, node, key);
461
462 if (node->prefixlen != matchlen ||
463 node->prefixlen == key->prefixlen)
464 break;
465
466 parent = node;
467 trim2 = trim;
468 next_bit = extract_bit(key->data, node->prefixlen);
469 trim = &node->child[next_bit];
470 }
471
472 if (!node || node->prefixlen != key->prefixlen ||
473 node->prefixlen != matchlen ||
474 (node->flags & LPM_TREE_NODE_FLAG_IM)) {
475 ret = -ENOENT;
476 goto out;
477 }
478
479 trie->n_entries--;
480
481 /* If the node we are removing has two children, simply mark it
482 * as intermediate and we are done.
483 */
484 if (rcu_access_pointer(node->child[0]) &&
485 rcu_access_pointer(node->child[1])) {
486 node->flags |= LPM_TREE_NODE_FLAG_IM;
487 goto out;
488 }
489
490 /* If the parent of the node we are about to delete is an intermediate
491 * node, and the deleted node doesn't have any children, we can delete
492 * the intermediate parent as well and promote its other child
493 * up the tree. Doing this maintains the invariant that all
494 * intermediate nodes have exactly 2 children and that there are no
495 * unnecessary intermediate nodes in the tree.
496 */
497 if (parent && (parent->flags & LPM_TREE_NODE_FLAG_IM) &&
498 !node->child[0] && !node->child[1]) {
499 if (node == rcu_access_pointer(parent->child[0]))
500 rcu_assign_pointer(
501 *trim2, rcu_access_pointer(parent->child[1]));
502 else
503 rcu_assign_pointer(
504 *trim2, rcu_access_pointer(parent->child[0]));
505 kfree_rcu(parent, rcu);
506 kfree_rcu(node, rcu);
507 goto out;
508 }
509
510 /* The node we are removing has either zero or one child. If there
511 * is a child, move it into the removed node's slot then delete
512 * the node. Otherwise just clear the slot and delete the node.
513 */
514 if (node->child[0])
515 rcu_assign_pointer(*trim, rcu_access_pointer(node->child[0]));
516 else if (node->child[1])
517 rcu_assign_pointer(*trim, rcu_access_pointer(node->child[1]));
518 else
519 RCU_INIT_POINTER(*trim, NULL);
520 kfree_rcu(node, rcu);
521
522out:
523 spin_unlock_irqrestore(&trie->lock, irq_flags);
524
525 return ret;
526}
527
528#define LPM_DATA_SIZE_MAX 256
529#define LPM_DATA_SIZE_MIN 1
530
531#define LPM_VAL_SIZE_MAX (KMALLOC_MAX_SIZE - LPM_DATA_SIZE_MAX - \
532 sizeof(struct lpm_trie_node))
533#define LPM_VAL_SIZE_MIN 1
534
535#define LPM_KEY_SIZE(X) (sizeof(struct bpf_lpm_trie_key) + (X))
536#define LPM_KEY_SIZE_MAX LPM_KEY_SIZE(LPM_DATA_SIZE_MAX)
537#define LPM_KEY_SIZE_MIN LPM_KEY_SIZE(LPM_DATA_SIZE_MIN)
538
539#define LPM_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_NUMA_NODE | \
540 BPF_F_ACCESS_MASK)
541
542static struct bpf_map *trie_alloc(union bpf_attr *attr)
543{
544 struct lpm_trie *trie;
545
546 if (!bpf_capable())
547 return ERR_PTR(-EPERM);
548
549 /* check sanity of attributes */
550 if (attr->max_entries == 0 ||
551 !(attr->map_flags & BPF_F_NO_PREALLOC) ||
552 attr->map_flags & ~LPM_CREATE_FLAG_MASK ||
553 !bpf_map_flags_access_ok(attr->map_flags) ||
554 attr->key_size < LPM_KEY_SIZE_MIN ||
555 attr->key_size > LPM_KEY_SIZE_MAX ||
556 attr->value_size < LPM_VAL_SIZE_MIN ||
557 attr->value_size > LPM_VAL_SIZE_MAX)
558 return ERR_PTR(-EINVAL);
559
560 trie = kzalloc(sizeof(*trie), GFP_USER | __GFP_NOWARN | __GFP_ACCOUNT);
561 if (!trie)
562 return ERR_PTR(-ENOMEM);
563
564 /* copy mandatory map attributes */
565 bpf_map_init_from_attr(&trie->map, attr);
566 trie->data_size = attr->key_size -
567 offsetof(struct bpf_lpm_trie_key, data);
568 trie->max_prefixlen = trie->data_size * 8;
569
570 spin_lock_init(&trie->lock);
571
572 return &trie->map;
573}
574
575static void trie_free(struct bpf_map *map)
576{
577 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
578 struct lpm_trie_node __rcu **slot;
579 struct lpm_trie_node *node;
580
581 /* Always start at the root and walk down to a node that has no
582 * children. Then free that node, nullify its reference in the parent
583 * and start over.
584 */
585
586 for (;;) {
587 slot = &trie->root;
588
589 for (;;) {
590 node = rcu_dereference_protected(*slot, 1);
591 if (!node)
592 goto out;
593
594 if (rcu_access_pointer(node->child[0])) {
595 slot = &node->child[0];
596 continue;
597 }
598
599 if (rcu_access_pointer(node->child[1])) {
600 slot = &node->child[1];
601 continue;
602 }
603
604 kfree(node);
605 RCU_INIT_POINTER(*slot, NULL);
606 break;
607 }
608 }
609
610out:
611 kfree(trie);
612}
613
614static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key)
615{
616 struct lpm_trie_node *node, *next_node = NULL, *parent, *search_root;
617 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
618 struct bpf_lpm_trie_key *key = _key, *next_key = _next_key;
619 struct lpm_trie_node **node_stack = NULL;
620 int err = 0, stack_ptr = -1;
621 unsigned int next_bit;
622 size_t matchlen;
623
624 /* The get_next_key follows postorder. For the 4 node example in
625 * the top of this file, the trie_get_next_key() returns the following
626 * one after another:
627 * 192.168.0.0/24
628 * 192.168.1.0/24
629 * 192.168.128.0/24
630 * 192.168.0.0/16
631 *
632 * The idea is to return more specific keys before less specific ones.
633 */
634
635 /* Empty trie */
636 search_root = rcu_dereference(trie->root);
637 if (!search_root)
638 return -ENOENT;
639
640 /* For invalid key, find the leftmost node in the trie */
641 if (!key || key->prefixlen > trie->max_prefixlen)
642 goto find_leftmost;
643
644 node_stack = kmalloc_array(trie->max_prefixlen,
645 sizeof(struct lpm_trie_node *),
646 GFP_ATOMIC | __GFP_NOWARN);
647 if (!node_stack)
648 return -ENOMEM;
649
650 /* Try to find the exact node for the given key */
651 for (node = search_root; node;) {
652 node_stack[++stack_ptr] = node;
653 matchlen = longest_prefix_match(trie, node, key);
654 if (node->prefixlen != matchlen ||
655 node->prefixlen == key->prefixlen)
656 break;
657
658 next_bit = extract_bit(key->data, node->prefixlen);
659 node = rcu_dereference(node->child[next_bit]);
660 }
661 if (!node || node->prefixlen != key->prefixlen ||
662 (node->flags & LPM_TREE_NODE_FLAG_IM))
663 goto find_leftmost;
664
665 /* The node with the exactly-matching key has been found,
666 * find the first node in postorder after the matched node.
667 */
668 node = node_stack[stack_ptr];
669 while (stack_ptr > 0) {
670 parent = node_stack[stack_ptr - 1];
671 if (rcu_dereference(parent->child[0]) == node) {
672 search_root = rcu_dereference(parent->child[1]);
673 if (search_root)
674 goto find_leftmost;
675 }
676 if (!(parent->flags & LPM_TREE_NODE_FLAG_IM)) {
677 next_node = parent;
678 goto do_copy;
679 }
680
681 node = parent;
682 stack_ptr--;
683 }
684
685 /* did not find anything */
686 err = -ENOENT;
687 goto free_stack;
688
689find_leftmost:
690 /* Find the leftmost non-intermediate node, all intermediate nodes
691 * have exact two children, so this function will never return NULL.
692 */
693 for (node = search_root; node;) {
694 if (node->flags & LPM_TREE_NODE_FLAG_IM) {
695 node = rcu_dereference(node->child[0]);
696 } else {
697 next_node = node;
698 node = rcu_dereference(node->child[0]);
699 if (!node)
700 node = rcu_dereference(next_node->child[1]);
701 }
702 }
703do_copy:
704 next_key->prefixlen = next_node->prefixlen;
705 memcpy((void *)next_key + offsetof(struct bpf_lpm_trie_key, data),
706 next_node->data, trie->data_size);
707free_stack:
708 kfree(node_stack);
709 return err;
710}
711
712static int trie_check_btf(const struct bpf_map *map,
713 const struct btf *btf,
714 const struct btf_type *key_type,
715 const struct btf_type *value_type)
716{
717 /* Keys must have struct bpf_lpm_trie_key embedded. */
718 return BTF_INFO_KIND(key_type->info) != BTF_KIND_STRUCT ?
719 -EINVAL : 0;
720}
721
722static int trie_map_btf_id;
723const struct bpf_map_ops trie_map_ops = {
724 .map_meta_equal = bpf_map_meta_equal,
725 .map_alloc = trie_alloc,
726 .map_free = trie_free,
727 .map_get_next_key = trie_get_next_key,
728 .map_lookup_elem = trie_lookup_elem,
729 .map_update_elem = trie_update_elem,
730 .map_delete_elem = trie_delete_elem,
731 .map_lookup_batch = generic_map_lookup_batch,
732 .map_update_batch = generic_map_update_batch,
733 .map_delete_batch = generic_map_delete_batch,
734 .map_check_btf = trie_check_btf,
735 .map_btf_name = "lpm_trie",
736 .map_btf_id = &trie_map_btf_id,
737};