2 * Longest prefix match list implementation
4 * Copyright (c) 2016,2017 Daniel Mack
5 * Copyright (c) 2016 David Herrmann
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.
12 #include <linux/bpf.h>
13 #include <linux/btf.h>
14 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/vmalloc.h>
19 #include <uapi/linux/btf.h>
21 /* Intermediate node */
22 #define LPM_TREE_NODE_FLAG_IM BIT(0)
26 struct lpm_trie_node {
28 struct lpm_trie_node __rcu *child[2];
36 struct lpm_trie_node __rcu *root;
43 /* This trie implements a longest prefix match algorithm that can be used to
44 * match IP addresses to a stored set of ranges.
46 * Data stored in @data of struct bpf_lpm_key and struct lpm_trie_node is
47 * interpreted as big endian, so data[0] stores the most significant byte.
49 * Match ranges are internally stored in instances of struct lpm_trie_node
50 * which each contain their prefix length as well as two pointers that may
51 * lead to more nodes containing more specific matches. Each node also stores
52 * a value that is defined by and returned to userspace via the update_elem
53 * and lookup functions.
55 * For instance, let's start with a trie that was created with a prefix length
56 * of 32, so it can be used for IPv4 addresses, and one single element that
57 * matches 192.168.0.0/16. The data array would hence contain
58 * [0xc0, 0xa8, 0x00, 0x00] in big-endian notation. This documentation will
59 * stick to IP-address notation for readability though.
61 * As the trie is empty initially, the new node (1) will be places as root
62 * node, denoted as (R) in the example below. As there are no other node, both
63 * child pointers are %NULL.
72 * Next, let's add a new node (2) matching 192.168.0.0/24. As there is already
73 * a node with the same data and a smaller prefix (ie, a less specific one),
74 * node (2) will become a child of (1). In child index depends on the next bit
75 * that is outside of what (1) matches, and that bit is 0, so (2) will be
92 * The child[1] slot of (1) could be filled with another node which has bit #17
93 * (the next bit after the ones that (1) matches on) set to 1. For instance,
103 * +----------------+ +------------------+
105 * | 192.168.0.0/24 | | 192.168.128.0/24 |
106 * | value: 2 | | value: 3 |
107 * | [0] [1] | | [0] [1] |
108 * +----------------+ +------------------+
110 * Let's add another node (4) to the game for 192.168.1.0/24. In order to place
111 * it, node (1) is looked at first, and because (4) of the semantics laid out
112 * above (bit #17 is 0), it would normally be attached to (1) as child[0].
113 * However, that slot is already allocated, so a new node is needed in between.
114 * That node does not have a value attached to it and it will never be
115 * returned to users as result of a lookup. It is only there to differentiate
116 * the traversal further. It will get a prefix as wide as necessary to
117 * distinguish its two children:
126 * +----------------+ +------------------+
127 * | (4) (I) | | (3) |
128 * | 192.168.0.0/23 | | 192.168.128.0/24 |
129 * | value: --- | | value: 3 |
130 * | [0] [1] | | [0] [1] |
131 * +----------------+ +------------------+
133 * +----------------+ +----------------+
135 * | 192.168.0.0/24 | | 192.168.1.0/24 |
136 * | value: 2 | | value: 5 |
137 * | [0] [1] | | [0] [1] |
138 * +----------------+ +----------------+
140 * 192.168.1.1/32 would be a child of (5) etc.
142 * An intermediate node will be turned into a 'real' node on demand. In the
143 * example above, (4) would be re-used if 192.168.0.0/23 is added to the trie.
145 * A fully populated trie would have a height of 32 nodes, as the trie was
146 * created with a prefix length of 32.
148 * The lookup starts at the root node. If the current node matches and if there
149 * is a child that can be used to become more specific, the trie is traversed
150 * downwards. The last node in the traversal that is a non-intermediate one is
154 static inline int extract_bit(const u8 *data, size_t index)
156 return !!(data[index / 8] & (1 << (7 - (index % 8))));
160 * longest_prefix_match() - determine the longest prefix
161 * @trie: The trie to get internal sizes from
162 * @node: The node to operate on
163 * @key: The key to compare to @node
165 * Determine the longest prefix of @node that matches the bits in @key.
167 static size_t longest_prefix_match(const struct lpm_trie *trie,
168 const struct lpm_trie_node *node,
169 const struct bpf_lpm_trie_key *key)
171 u32 limit = min(node->prefixlen, key->prefixlen);
172 u32 prefixlen = 0, i = 0;
174 BUILD_BUG_ON(offsetof(struct lpm_trie_node, data) % sizeof(u32));
175 BUILD_BUG_ON(offsetof(struct bpf_lpm_trie_key, data) % sizeof(u32));
177 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && defined(CONFIG_64BIT)
179 /* data_size >= 16 has very small probability.
180 * We do not use a loop for optimal code generation.
182 if (trie->data_size >= 8) {
183 u64 diff = be64_to_cpu(*(__be64 *)node->data ^
184 *(__be64 *)key->data);
186 prefixlen = 64 - fls64(diff);
187 if (prefixlen >= limit)
195 while (trie->data_size >= i + 4) {
196 u32 diff = be32_to_cpu(*(__be32 *)&node->data[i] ^
197 *(__be32 *)&key->data[i]);
199 prefixlen += 32 - fls(diff);
200 if (prefixlen >= limit)
207 if (trie->data_size >= i + 2) {
208 u16 diff = be16_to_cpu(*(__be16 *)&node->data[i] ^
209 *(__be16 *)&key->data[i]);
211 prefixlen += 16 - fls(diff);
212 if (prefixlen >= limit)
219 if (trie->data_size >= i + 1) {
220 prefixlen += 8 - fls(node->data[i] ^ key->data[i]);
222 if (prefixlen >= limit)
229 /* Called from syscall or from eBPF program */
230 static void *trie_lookup_elem(struct bpf_map *map, void *_key)
232 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
233 struct lpm_trie_node *node, *found = NULL;
234 struct bpf_lpm_trie_key *key = _key;
236 /* Start walking the trie from the root node ... */
238 for (node = rcu_dereference(trie->root); node;) {
239 unsigned int next_bit;
242 /* Determine the longest prefix of @node that matches @key.
243 * If it's the maximum possible prefix for this trie, we have
244 * an exact match and can return it directly.
246 matchlen = longest_prefix_match(trie, node, key);
247 if (matchlen == trie->max_prefixlen) {
252 /* If the number of bits that match is smaller than the prefix
253 * length of @node, bail out and return the node we have seen
254 * last in the traversal (ie, the parent).
256 if (matchlen < node->prefixlen)
259 /* Consider this node as return candidate unless it is an
260 * artificially added intermediate one.
262 if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
265 /* If the node match is fully satisfied, let's see if we can
266 * become more specific. Determine the next bit in the key and
269 next_bit = extract_bit(key->data, node->prefixlen);
270 node = rcu_dereference(node->child[next_bit]);
276 return found->data + trie->data_size;
279 static struct lpm_trie_node *lpm_trie_node_alloc(const struct lpm_trie *trie,
282 struct lpm_trie_node *node;
283 size_t size = sizeof(struct lpm_trie_node) + trie->data_size;
286 size += trie->map.value_size;
288 node = kmalloc_node(size, GFP_ATOMIC | __GFP_NOWARN,
289 trie->map.numa_node);
296 memcpy(node->data + trie->data_size, value,
297 trie->map.value_size);
302 /* Called from syscall or from eBPF program */
303 static int trie_update_elem(struct bpf_map *map,
304 void *_key, void *value, u64 flags)
306 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
307 struct lpm_trie_node *node, *im_node = NULL, *new_node = NULL;
308 struct lpm_trie_node __rcu **slot;
309 struct bpf_lpm_trie_key *key = _key;
310 unsigned long irq_flags;
311 unsigned int next_bit;
315 if (unlikely(flags > BPF_EXIST))
318 if (key->prefixlen > trie->max_prefixlen)
321 raw_spin_lock_irqsave(&trie->lock, irq_flags);
323 /* Allocate and fill a new node */
325 if (trie->n_entries == trie->map.max_entries) {
330 new_node = lpm_trie_node_alloc(trie, value);
338 new_node->prefixlen = key->prefixlen;
339 RCU_INIT_POINTER(new_node->child[0], NULL);
340 RCU_INIT_POINTER(new_node->child[1], NULL);
341 memcpy(new_node->data, key->data, trie->data_size);
343 /* Now find a slot to attach the new node. To do that, walk the tree
344 * from the root and match as many bits as possible for each node until
345 * we either find an empty slot or a slot that needs to be replaced by
346 * an intermediate node.
350 while ((node = rcu_dereference_protected(*slot,
351 lockdep_is_held(&trie->lock)))) {
352 matchlen = longest_prefix_match(trie, node, key);
354 if (node->prefixlen != matchlen ||
355 node->prefixlen == key->prefixlen ||
356 node->prefixlen == trie->max_prefixlen)
359 next_bit = extract_bit(key->data, node->prefixlen);
360 slot = &node->child[next_bit];
363 /* If the slot is empty (a free child pointer or an empty root),
364 * simply assign the @new_node to that slot and be done.
367 rcu_assign_pointer(*slot, new_node);
371 /* If the slot we picked already exists, replace it with @new_node
372 * which already has the correct data array set.
374 if (node->prefixlen == matchlen) {
375 new_node->child[0] = node->child[0];
376 new_node->child[1] = node->child[1];
378 if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
381 rcu_assign_pointer(*slot, new_node);
382 kfree_rcu(node, rcu);
387 /* If the new node matches the prefix completely, it must be inserted
388 * as an ancestor. Simply insert it between @node and *@slot.
390 if (matchlen == key->prefixlen) {
391 next_bit = extract_bit(node->data, matchlen);
392 rcu_assign_pointer(new_node->child[next_bit], node);
393 rcu_assign_pointer(*slot, new_node);
397 im_node = lpm_trie_node_alloc(trie, NULL);
403 im_node->prefixlen = matchlen;
404 im_node->flags |= LPM_TREE_NODE_FLAG_IM;
405 memcpy(im_node->data, node->data, trie->data_size);
407 /* Now determine which child to install in which slot */
408 if (extract_bit(key->data, matchlen)) {
409 rcu_assign_pointer(im_node->child[0], node);
410 rcu_assign_pointer(im_node->child[1], new_node);
412 rcu_assign_pointer(im_node->child[0], new_node);
413 rcu_assign_pointer(im_node->child[1], node);
416 /* Finally, assign the intermediate node to the determined spot */
417 rcu_assign_pointer(*slot, im_node);
428 raw_spin_unlock_irqrestore(&trie->lock, irq_flags);
433 /* Called from syscall or from eBPF program */
434 static int trie_delete_elem(struct bpf_map *map, void *_key)
436 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
437 struct bpf_lpm_trie_key *key = _key;
438 struct lpm_trie_node __rcu **trim, **trim2;
439 struct lpm_trie_node *node, *parent;
440 unsigned long irq_flags;
441 unsigned int next_bit;
445 if (key->prefixlen > trie->max_prefixlen)
448 raw_spin_lock_irqsave(&trie->lock, irq_flags);
450 /* Walk the tree looking for an exact key/length match and keeping
451 * track of the path we traverse. We will need to know the node
452 * we wish to delete, and the slot that points to the node we want
453 * to delete. We may also need to know the nodes parent and the
454 * slot that contains it.
459 while ((node = rcu_dereference_protected(
460 *trim, lockdep_is_held(&trie->lock)))) {
461 matchlen = longest_prefix_match(trie, node, key);
463 if (node->prefixlen != matchlen ||
464 node->prefixlen == key->prefixlen)
469 next_bit = extract_bit(key->data, node->prefixlen);
470 trim = &node->child[next_bit];
473 if (!node || node->prefixlen != key->prefixlen ||
474 (node->flags & LPM_TREE_NODE_FLAG_IM)) {
481 /* If the node we are removing has two children, simply mark it
482 * as intermediate and we are done.
484 if (rcu_access_pointer(node->child[0]) &&
485 rcu_access_pointer(node->child[1])) {
486 node->flags |= LPM_TREE_NODE_FLAG_IM;
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.
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]))
501 *trim2, rcu_access_pointer(parent->child[1]));
504 *trim2, rcu_access_pointer(parent->child[0]));
505 kfree_rcu(parent, rcu);
506 kfree_rcu(node, rcu);
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.
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]));
519 RCU_INIT_POINTER(*trim, NULL);
520 kfree_rcu(node, rcu);
523 raw_spin_unlock_irqrestore(&trie->lock, irq_flags);
528 #define LPM_DATA_SIZE_MAX 256
529 #define LPM_DATA_SIZE_MIN 1
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
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)
539 #define LPM_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_NUMA_NODE | \
540 BPF_F_RDONLY | BPF_F_WRONLY)
542 static struct bpf_map *trie_alloc(union bpf_attr *attr)
544 struct lpm_trie *trie;
545 u64 cost = sizeof(*trie), cost_per_node;
548 if (!capable(CAP_SYS_ADMIN))
549 return ERR_PTR(-EPERM);
551 /* check sanity of attributes */
552 if (attr->max_entries == 0 ||
553 !(attr->map_flags & BPF_F_NO_PREALLOC) ||
554 attr->map_flags & ~LPM_CREATE_FLAG_MASK ||
555 attr->key_size < LPM_KEY_SIZE_MIN ||
556 attr->key_size > LPM_KEY_SIZE_MAX ||
557 attr->value_size < LPM_VAL_SIZE_MIN ||
558 attr->value_size > LPM_VAL_SIZE_MAX)
559 return ERR_PTR(-EINVAL);
561 trie = kzalloc(sizeof(*trie), GFP_USER | __GFP_NOWARN);
563 return ERR_PTR(-ENOMEM);
565 /* copy mandatory map attributes */
566 bpf_map_init_from_attr(&trie->map, attr);
567 trie->data_size = attr->key_size -
568 offsetof(struct bpf_lpm_trie_key, data);
569 trie->max_prefixlen = trie->data_size * 8;
571 cost_per_node = sizeof(struct lpm_trie_node) +
572 attr->value_size + trie->data_size;
573 cost += (u64) attr->max_entries * cost_per_node;
574 if (cost >= U32_MAX - PAGE_SIZE) {
579 trie->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
581 ret = bpf_map_precharge_memlock(trie->map.pages);
585 raw_spin_lock_init(&trie->lock);
593 static void trie_free(struct bpf_map *map)
595 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
596 struct lpm_trie_node __rcu **slot;
597 struct lpm_trie_node *node;
599 /* Wait for outstanding programs to complete
600 * update/lookup/delete/get_next_key and free the trie.
604 /* Always start at the root and walk down to a node that has no
605 * children. Then free that node, nullify its reference in the parent
613 node = rcu_dereference_protected(*slot, 1);
617 if (rcu_access_pointer(node->child[0])) {
618 slot = &node->child[0];
622 if (rcu_access_pointer(node->child[1])) {
623 slot = &node->child[1];
628 RCU_INIT_POINTER(*slot, NULL);
637 static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key)
639 struct lpm_trie_node *node, *next_node = NULL, *parent, *search_root;
640 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
641 struct bpf_lpm_trie_key *key = _key, *next_key = _next_key;
642 struct lpm_trie_node **node_stack = NULL;
643 int err = 0, stack_ptr = -1;
644 unsigned int next_bit;
647 /* The get_next_key follows postorder. For the 4 node example in
648 * the top of this file, the trie_get_next_key() returns the following
655 * The idea is to return more specific keys before less specific ones.
659 search_root = rcu_dereference(trie->root);
663 /* For invalid key, find the leftmost node in the trie */
664 if (!key || key->prefixlen > trie->max_prefixlen)
667 node_stack = kmalloc_array(trie->max_prefixlen,
668 sizeof(struct lpm_trie_node *),
669 GFP_ATOMIC | __GFP_NOWARN);
673 /* Try to find the exact node for the given key */
674 for (node = search_root; node;) {
675 node_stack[++stack_ptr] = node;
676 matchlen = longest_prefix_match(trie, node, key);
677 if (node->prefixlen != matchlen ||
678 node->prefixlen == key->prefixlen)
681 next_bit = extract_bit(key->data, node->prefixlen);
682 node = rcu_dereference(node->child[next_bit]);
684 if (!node || node->prefixlen != key->prefixlen ||
685 (node->flags & LPM_TREE_NODE_FLAG_IM))
688 /* The node with the exactly-matching key has been found,
689 * find the first node in postorder after the matched node.
691 node = node_stack[stack_ptr];
692 while (stack_ptr > 0) {
693 parent = node_stack[stack_ptr - 1];
694 if (rcu_dereference(parent->child[0]) == node) {
695 search_root = rcu_dereference(parent->child[1]);
699 if (!(parent->flags & LPM_TREE_NODE_FLAG_IM)) {
708 /* did not find anything */
713 /* Find the leftmost non-intermediate node, all intermediate nodes
714 * have exact two children, so this function will never return NULL.
716 for (node = search_root; node;) {
717 if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
719 node = rcu_dereference(node->child[0]);
722 next_key->prefixlen = next_node->prefixlen;
723 memcpy((void *)next_key + offsetof(struct bpf_lpm_trie_key, data),
724 next_node->data, trie->data_size);
730 static int trie_check_btf(const struct bpf_map *map,
731 const struct btf_type *key_type,
732 const struct btf_type *value_type)
734 /* Keys must have struct bpf_lpm_trie_key embedded. */
735 return BTF_INFO_KIND(key_type->info) != BTF_KIND_STRUCT ?
739 const struct bpf_map_ops trie_map_ops = {
740 .map_alloc = trie_alloc,
741 .map_free = trie_free,
742 .map_get_next_key = trie_get_next_key,
743 .map_lookup_elem = trie_lookup_elem,
744 .map_update_elem = trie_update_elem,
745 .map_delete_elem = trie_delete_elem,
746 .map_check_btf = trie_check_btf,