blob: 09334f13a8a0cb16c647ef4080ef93939936d00d [file] [log] [blame]
Daniel Mackb95a5c42017-01-21 17:26:11 +01001/*
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>
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020013#include <linux/btf.h>
Daniel Mackb95a5c42017-01-21 17:26:11 +010014#include <linux/err.h>
15#include <linux/slab.h>
16#include <linux/spinlock.h>
17#include <linux/vmalloc.h>
18#include <net/ipv6.h>
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020019#include <uapi/linux/btf.h>
Daniel Mackb95a5c42017-01-21 17:26:11 +010020
21/* Intermediate node */
22#define LPM_TREE_NODE_FLAG_IM BIT(0)
23
24struct lpm_trie_node;
25
26struct lpm_trie_node {
27 struct rcu_head rcu;
28 struct lpm_trie_node __rcu *child[2];
29 u32 prefixlen;
30 u32 flags;
31 u8 data[0];
32};
33
34struct lpm_trie {
35 struct bpf_map map;
36 struct lpm_trie_node __rcu *root;
37 size_t n_entries;
38 size_t max_prefixlen;
39 size_t data_size;
40 raw_spinlock_t lock;
41};
42
43/* This trie implements a longest prefix match algorithm that can be used to
44 * match IP addresses to a stored set of ranges.
45 *
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.
48 *
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.
54 *
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.
60 *
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.
64 *
65 * +----------------+
66 * | (1) (R) |
67 * | 192.168.0.0/16 |
68 * | value: 1 |
69 * | [0] [1] |
70 * +----------------+
71 *
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
76 * child[0] of (1):
77 *
78 * +----------------+
79 * | (1) (R) |
80 * | 192.168.0.0/16 |
81 * | value: 1 |
82 * | [0] [1] |
83 * +----------------+
84 * |
85 * +----------------+
86 * | (2) |
87 * | 192.168.0.0/24 |
88 * | value: 2 |
89 * | [0] [1] |
90 * +----------------+
91 *
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,
94 * 192.168.128.0/24:
95 *
96 * +----------------+
97 * | (1) (R) |
98 * | 192.168.0.0/16 |
99 * | value: 1 |
100 * | [0] [1] |
101 * +----------------+
102 * | |
103 * +----------------+ +------------------+
104 * | (2) | | (3) |
105 * | 192.168.0.0/24 | | 192.168.128.0/24 |
106 * | value: 2 | | value: 3 |
107 * | [0] [1] | | [0] [1] |
108 * +----------------+ +------------------+
109 *
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:
118 *
119 * +----------------+
120 * | (1) (R) |
121 * | 192.168.0.0/16 |
122 * | value: 1 |
123 * | [0] [1] |
124 * +----------------+
125 * | |
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 * +----------------+ +------------------+
132 * | |
133 * +----------------+ +----------------+
134 * | (2) | | (5) |
135 * | 192.168.0.0/24 | | 192.168.1.0/24 |
136 * | value: 2 | | value: 5 |
137 * | [0] [1] | | [0] [1] |
138 * +----------------+ +----------------+
139 *
140 * 192.168.1.1/32 would be a child of (5) etc.
141 *
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.
144 *
145 * A fully populated trie would have a height of 32 nodes, as the trie was
146 * created with a prefix length of 32.
147 *
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
151 * returned.
152 */
153
154static inline int extract_bit(const u8 *data, size_t index)
155{
156 return !!(data[index / 8] & (1 << (7 - (index % 8))));
157}
158
159/**
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
164 *
165 * Determine the longest prefix of @node that matches the bits in @key.
166 */
167static 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)
170{
Eric Dumazet8d758392018-11-21 21:39:52 -0800171 u32 limit = min(node->prefixlen, key->prefixlen);
172 u32 prefixlen = 0, i = 0;
Daniel Mackb95a5c42017-01-21 17:26:11 +0100173
Eric Dumazet8d758392018-11-21 21:39:52 -0800174 BUILD_BUG_ON(offsetof(struct lpm_trie_node, data) % sizeof(u32));
175 BUILD_BUG_ON(offsetof(struct bpf_lpm_trie_key, data) % sizeof(u32));
Daniel Mackb95a5c42017-01-21 17:26:11 +0100176
Eric Dumazet8d758392018-11-21 21:39:52 -0800177#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && defined(CONFIG_64BIT)
Daniel Mackb95a5c42017-01-21 17:26:11 +0100178
Eric Dumazet8d758392018-11-21 21:39:52 -0800179 /* data_size >= 16 has very small probability.
180 * We do not use a loop for optimal code generation.
181 */
182 if (trie->data_size >= 8) {
183 u64 diff = be64_to_cpu(*(__be64 *)node->data ^
184 *(__be64 *)key->data);
Daniel Mackb95a5c42017-01-21 17:26:11 +0100185
Eric Dumazet8d758392018-11-21 21:39:52 -0800186 prefixlen = 64 - fls64(diff);
187 if (prefixlen >= limit)
188 return limit;
189 if (diff)
190 return prefixlen;
191 i = 8;
192 }
193#endif
194
195 while (trie->data_size >= i + 4) {
196 u32 diff = be32_to_cpu(*(__be32 *)&node->data[i] ^
197 *(__be32 *)&key->data[i]);
198
199 prefixlen += 32 - fls(diff);
200 if (prefixlen >= limit)
201 return limit;
202 if (diff)
203 return prefixlen;
204 i += 4;
205 }
206
207 if (trie->data_size >= i + 2) {
208 u16 diff = be16_to_cpu(*(__be16 *)&node->data[i] ^
209 *(__be16 *)&key->data[i]);
210
211 prefixlen += 16 - fls(diff);
212 if (prefixlen >= limit)
213 return limit;
214 if (diff)
215 return prefixlen;
216 i += 2;
217 }
218
219 if (trie->data_size >= i + 1) {
220 prefixlen += 8 - fls(node->data[i] ^ key->data[i]);
221
222 if (prefixlen >= limit)
223 return limit;
Daniel Mackb95a5c42017-01-21 17:26:11 +0100224 }
225
226 return prefixlen;
227}
228
229/* Called from syscall or from eBPF program */
230static void *trie_lookup_elem(struct bpf_map *map, void *_key)
231{
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;
235
236 /* Start walking the trie from the root node ... */
237
238 for (node = rcu_dereference(trie->root); node;) {
239 unsigned int next_bit;
240 size_t matchlen;
241
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.
245 */
246 matchlen = longest_prefix_match(trie, node, key);
247 if (matchlen == trie->max_prefixlen) {
248 found = node;
249 break;
250 }
251
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).
255 */
256 if (matchlen < node->prefixlen)
257 break;
258
259 /* Consider this node as return candidate unless it is an
260 * artificially added intermediate one.
261 */
262 if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
263 found = node;
264
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
267 * traverse down.
268 */
269 next_bit = extract_bit(key->data, node->prefixlen);
270 node = rcu_dereference(node->child[next_bit]);
271 }
272
273 if (!found)
274 return NULL;
275
276 return found->data + trie->data_size;
277}
278
279static struct lpm_trie_node *lpm_trie_node_alloc(const struct lpm_trie *trie,
280 const void *value)
281{
282 struct lpm_trie_node *node;
283 size_t size = sizeof(struct lpm_trie_node) + trie->data_size;
284
285 if (value)
286 size += trie->map.value_size;
287
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700288 node = kmalloc_node(size, GFP_ATOMIC | __GFP_NOWARN,
289 trie->map.numa_node);
Daniel Mackb95a5c42017-01-21 17:26:11 +0100290 if (!node)
291 return NULL;
292
293 node->flags = 0;
294
295 if (value)
296 memcpy(node->data + trie->data_size, value,
297 trie->map.value_size);
298
299 return node;
300}
301
302/* Called from syscall or from eBPF program */
303static int trie_update_elem(struct bpf_map *map,
304 void *_key, void *value, u64 flags)
305{
306 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
Daniel Borkmannd1401992017-01-24 01:26:46 +0100307 struct lpm_trie_node *node, *im_node = NULL, *new_node = NULL;
Daniel Mackb95a5c42017-01-21 17:26:11 +0100308 struct lpm_trie_node __rcu **slot;
309 struct bpf_lpm_trie_key *key = _key;
310 unsigned long irq_flags;
311 unsigned int next_bit;
312 size_t matchlen = 0;
313 int ret = 0;
314
315 if (unlikely(flags > BPF_EXIST))
316 return -EINVAL;
317
318 if (key->prefixlen > trie->max_prefixlen)
319 return -EINVAL;
320
321 raw_spin_lock_irqsave(&trie->lock, irq_flags);
322
323 /* Allocate and fill a new node */
324
325 if (trie->n_entries == trie->map.max_entries) {
326 ret = -ENOSPC;
327 goto out;
328 }
329
330 new_node = lpm_trie_node_alloc(trie, value);
331 if (!new_node) {
332 ret = -ENOMEM;
333 goto out;
334 }
335
336 trie->n_entries++;
337
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);
342
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.
347 */
348 slot = &trie->root;
349
350 while ((node = rcu_dereference_protected(*slot,
351 lockdep_is_held(&trie->lock)))) {
352 matchlen = longest_prefix_match(trie, node, key);
353
354 if (node->prefixlen != matchlen ||
355 node->prefixlen == key->prefixlen ||
356 node->prefixlen == trie->max_prefixlen)
357 break;
358
359 next_bit = extract_bit(key->data, node->prefixlen);
360 slot = &node->child[next_bit];
361 }
362
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.
365 */
366 if (!node) {
367 rcu_assign_pointer(*slot, new_node);
368 goto out;
369 }
370
371 /* If the slot we picked already exists, replace it with @new_node
372 * which already has the correct data array set.
373 */
374 if (node->prefixlen == matchlen) {
375 new_node->child[0] = node->child[0];
376 new_node->child[1] = node->child[1];
377
378 if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
379 trie->n_entries--;
380
381 rcu_assign_pointer(*slot, new_node);
382 kfree_rcu(node, rcu);
383
384 goto out;
385 }
386
387 /* If the new node matches the prefix completely, it must be inserted
388 * as an ancestor. Simply insert it between @node and *@slot.
389 */
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);
394 goto out;
395 }
396
397 im_node = lpm_trie_node_alloc(trie, NULL);
398 if (!im_node) {
399 ret = -ENOMEM;
400 goto out;
401 }
402
403 im_node->prefixlen = matchlen;
404 im_node->flags |= LPM_TREE_NODE_FLAG_IM;
405 memcpy(im_node->data, node->data, trie->data_size);
406
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);
411 } else {
412 rcu_assign_pointer(im_node->child[0], new_node);
413 rcu_assign_pointer(im_node->child[1], node);
414 }
415
416 /* Finally, assign the intermediate node to the determined spot */
417 rcu_assign_pointer(*slot, im_node);
418
419out:
420 if (ret) {
421 if (new_node)
422 trie->n_entries--;
423
424 kfree(new_node);
425 kfree(im_node);
426 }
427
428 raw_spin_unlock_irqrestore(&trie->lock, irq_flags);
429
430 return ret;
431}
432
Craig Galleke454cf52017-09-18 15:30:55 -0400433/* Called from syscall or from eBPF program */
434static int trie_delete_elem(struct bpf_map *map, void *_key)
Daniel Mackb95a5c42017-01-21 17:26:11 +0100435{
Craig Galleke454cf52017-09-18 15:30:55 -0400436 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
437 struct bpf_lpm_trie_key *key = _key;
Craig Gallekb5d73882017-09-21 18:43:29 -0400438 struct lpm_trie_node __rcu **trim, **trim2;
439 struct lpm_trie_node *node, *parent;
Craig Galleke454cf52017-09-18 15:30:55 -0400440 unsigned long irq_flags;
441 unsigned int next_bit;
442 size_t matchlen = 0;
443 int ret = 0;
444
445 if (key->prefixlen > trie->max_prefixlen)
446 return -EINVAL;
447
448 raw_spin_lock_irqsave(&trie->lock, irq_flags);
449
450 /* Walk the tree looking for an exact key/length match and keeping
Craig Gallekb5d73882017-09-21 18:43:29 -0400451 * 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.
Craig Galleke454cf52017-09-18 15:30:55 -0400455 */
456 trim = &trie->root;
Craig Gallekb5d73882017-09-21 18:43:29 -0400457 trim2 = trim;
458 parent = NULL;
459 while ((node = rcu_dereference_protected(
460 *trim, lockdep_is_held(&trie->lock)))) {
Craig Galleke454cf52017-09-18 15:30:55 -0400461 matchlen = longest_prefix_match(trie, node, key);
462
463 if (node->prefixlen != matchlen ||
464 node->prefixlen == key->prefixlen)
465 break;
466
Craig Gallekb5d73882017-09-21 18:43:29 -0400467 parent = node;
468 trim2 = trim;
Craig Galleke454cf52017-09-18 15:30:55 -0400469 next_bit = extract_bit(key->data, node->prefixlen);
Craig Gallekb5d73882017-09-21 18:43:29 -0400470 trim = &node->child[next_bit];
Craig Galleke454cf52017-09-18 15:30:55 -0400471 }
472
473 if (!node || node->prefixlen != key->prefixlen ||
Alban Crequy7c0cdf02019-02-22 14:19:08 +0100474 node->prefixlen != matchlen ||
Craig Galleke454cf52017-09-18 15:30:55 -0400475 (node->flags & LPM_TREE_NODE_FLAG_IM)) {
476 ret = -ENOENT;
477 goto out;
478 }
479
480 trie->n_entries--;
481
Craig Gallekb5d73882017-09-21 18:43:29 -0400482 /* If the node we are removing has two children, simply mark it
Craig Galleke454cf52017-09-18 15:30:55 -0400483 * as intermediate and we are done.
484 */
Craig Gallekb5d73882017-09-21 18:43:29 -0400485 if (rcu_access_pointer(node->child[0]) &&
Craig Galleke454cf52017-09-18 15:30:55 -0400486 rcu_access_pointer(node->child[1])) {
487 node->flags |= LPM_TREE_NODE_FLAG_IM;
488 goto out;
489 }
490
Craig Gallekb5d73882017-09-21 18:43:29 -0400491 /* If the parent of the node we are about to delete is an intermediate
492 * node, and the deleted node doesn't have any children, we can delete
493 * the intermediate parent as well and promote its other child
494 * up the tree. Doing this maintains the invariant that all
495 * intermediate nodes have exactly 2 children and that there are no
496 * unnecessary intermediate nodes in the tree.
Craig Galleke454cf52017-09-18 15:30:55 -0400497 */
Craig Gallekb5d73882017-09-21 18:43:29 -0400498 if (parent && (parent->flags & LPM_TREE_NODE_FLAG_IM) &&
499 !node->child[0] && !node->child[1]) {
500 if (node == rcu_access_pointer(parent->child[0]))
501 rcu_assign_pointer(
502 *trim2, rcu_access_pointer(parent->child[1]));
503 else
504 rcu_assign_pointer(
505 *trim2, rcu_access_pointer(parent->child[0]));
506 kfree_rcu(parent, rcu);
Craig Galleke454cf52017-09-18 15:30:55 -0400507 kfree_rcu(node, rcu);
Craig Gallekb5d73882017-09-21 18:43:29 -0400508 goto out;
Craig Galleke454cf52017-09-18 15:30:55 -0400509 }
510
Craig Gallekb5d73882017-09-21 18:43:29 -0400511 /* The node we are removing has either zero or one child. If there
512 * is a child, move it into the removed node's slot then delete
513 * the node. Otherwise just clear the slot and delete the node.
514 */
515 if (node->child[0])
516 rcu_assign_pointer(*trim, rcu_access_pointer(node->child[0]));
517 else if (node->child[1])
518 rcu_assign_pointer(*trim, rcu_access_pointer(node->child[1]));
519 else
520 RCU_INIT_POINTER(*trim, NULL);
521 kfree_rcu(node, rcu);
522
Craig Galleke454cf52017-09-18 15:30:55 -0400523out:
524 raw_spin_unlock_irqrestore(&trie->lock, irq_flags);
525
526 return ret;
Daniel Mackb95a5c42017-01-21 17:26:11 +0100527}
528
Daniel Borkmannc502faf2017-02-08 01:19:43 +0100529#define LPM_DATA_SIZE_MAX 256
530#define LPM_DATA_SIZE_MIN 1
531
532#define LPM_VAL_SIZE_MAX (KMALLOC_MAX_SIZE - LPM_DATA_SIZE_MAX - \
533 sizeof(struct lpm_trie_node))
534#define LPM_VAL_SIZE_MIN 1
535
536#define LPM_KEY_SIZE(X) (sizeof(struct bpf_lpm_trie_key) + (X))
537#define LPM_KEY_SIZE_MAX LPM_KEY_SIZE(LPM_DATA_SIZE_MAX)
538#define LPM_KEY_SIZE_MIN LPM_KEY_SIZE(LPM_DATA_SIZE_MIN)
539
Chenbo Feng6e71b042017-10-18 13:00:22 -0700540#define LPM_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_NUMA_NODE | \
Daniel Borkmann591fe982019-04-09 23:20:05 +0200541 BPF_F_ACCESS_MASK)
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700542
Daniel Mackb95a5c42017-01-21 17:26:11 +0100543static struct bpf_map *trie_alloc(union bpf_attr *attr)
544{
Daniel Mackb95a5c42017-01-21 17:26:11 +0100545 struct lpm_trie *trie;
Daniel Borkmannc502faf2017-02-08 01:19:43 +0100546 u64 cost = sizeof(*trie), cost_per_node;
Daniel Mackb95a5c42017-01-21 17:26:11 +0100547 int ret;
548
549 if (!capable(CAP_SYS_ADMIN))
550 return ERR_PTR(-EPERM);
551
552 /* check sanity of attributes */
553 if (attr->max_entries == 0 ||
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700554 !(attr->map_flags & BPF_F_NO_PREALLOC) ||
555 attr->map_flags & ~LPM_CREATE_FLAG_MASK ||
Daniel Borkmann591fe982019-04-09 23:20:05 +0200556 !bpf_map_flags_access_ok(attr->map_flags) ||
Daniel Borkmannc502faf2017-02-08 01:19:43 +0100557 attr->key_size < LPM_KEY_SIZE_MIN ||
558 attr->key_size > LPM_KEY_SIZE_MAX ||
559 attr->value_size < LPM_VAL_SIZE_MIN ||
560 attr->value_size > LPM_VAL_SIZE_MAX)
Daniel Mackb95a5c42017-01-21 17:26:11 +0100561 return ERR_PTR(-EINVAL);
562
563 trie = kzalloc(sizeof(*trie), GFP_USER | __GFP_NOWARN);
564 if (!trie)
565 return ERR_PTR(-ENOMEM);
566
567 /* copy mandatory map attributes */
Jakub Kicinskibd475642018-01-11 20:29:06 -0800568 bpf_map_init_from_attr(&trie->map, attr);
Daniel Mackb95a5c42017-01-21 17:26:11 +0100569 trie->data_size = attr->key_size -
570 offsetof(struct bpf_lpm_trie_key, data);
571 trie->max_prefixlen = trie->data_size * 8;
572
573 cost_per_node = sizeof(struct lpm_trie_node) +
574 attr->value_size + trie->data_size;
Daniel Borkmannc502faf2017-02-08 01:19:43 +0100575 cost += (u64) attr->max_entries * cost_per_node;
Daniel Borkmannc502faf2017-02-08 01:19:43 +0100576
Roman Gushchinc85d6912019-05-29 18:03:59 -0700577 ret = bpf_map_charge_init(&trie->map.memory, cost);
Daniel Borkmannc502faf2017-02-08 01:19:43 +0100578 if (ret)
579 goto out_err;
Daniel Mackb95a5c42017-01-21 17:26:11 +0100580
581 raw_spin_lock_init(&trie->lock);
582
583 return &trie->map;
Daniel Borkmannc502faf2017-02-08 01:19:43 +0100584out_err:
585 kfree(trie);
586 return ERR_PTR(ret);
Daniel Mackb95a5c42017-01-21 17:26:11 +0100587}
588
589static void trie_free(struct bpf_map *map)
590{
591 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
592 struct lpm_trie_node __rcu **slot;
593 struct lpm_trie_node *node;
594
Yonghong Song9a3efb62018-02-13 19:00:21 -0800595 /* Wait for outstanding programs to complete
596 * update/lookup/delete/get_next_key and free the trie.
597 */
598 synchronize_rcu();
Daniel Mackb95a5c42017-01-21 17:26:11 +0100599
600 /* Always start at the root and walk down to a node that has no
601 * children. Then free that node, nullify its reference in the parent
602 * and start over.
603 */
604
605 for (;;) {
606 slot = &trie->root;
607
608 for (;;) {
Yonghong Song6c5f6102018-02-22 10:10:35 -0800609 node = rcu_dereference_protected(*slot, 1);
Daniel Mackb95a5c42017-01-21 17:26:11 +0100610 if (!node)
Yonghong Song9a3efb62018-02-13 19:00:21 -0800611 goto out;
Daniel Mackb95a5c42017-01-21 17:26:11 +0100612
613 if (rcu_access_pointer(node->child[0])) {
614 slot = &node->child[0];
615 continue;
616 }
617
618 if (rcu_access_pointer(node->child[1])) {
619 slot = &node->child[1];
620 continue;
621 }
622
623 kfree(node);
624 RCU_INIT_POINTER(*slot, NULL);
625 break;
626 }
627 }
628
Yonghong Song9a3efb62018-02-13 19:00:21 -0800629out:
630 kfree(trie);
Daniel Mackb95a5c42017-01-21 17:26:11 +0100631}
632
Yonghong Songb471f2f2018-01-18 15:08:50 -0800633static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key)
Alexei Starovoitovf38837b2017-03-05 09:41:08 -0800634{
Yonghong Song6dd1ec62018-01-26 15:06:07 -0800635 struct lpm_trie_node *node, *next_node = NULL, *parent, *search_root;
Yonghong Songb471f2f2018-01-18 15:08:50 -0800636 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
637 struct bpf_lpm_trie_key *key = _key, *next_key = _next_key;
Yonghong Songb471f2f2018-01-18 15:08:50 -0800638 struct lpm_trie_node **node_stack = NULL;
Yonghong Songb471f2f2018-01-18 15:08:50 -0800639 int err = 0, stack_ptr = -1;
640 unsigned int next_bit;
641 size_t matchlen;
642
643 /* The get_next_key follows postorder. For the 4 node example in
644 * the top of this file, the trie_get_next_key() returns the following
645 * one after another:
646 * 192.168.0.0/24
647 * 192.168.1.0/24
648 * 192.168.128.0/24
649 * 192.168.0.0/16
650 *
651 * The idea is to return more specific keys before less specific ones.
652 */
653
654 /* Empty trie */
Yonghong Song6dd1ec62018-01-26 15:06:07 -0800655 search_root = rcu_dereference(trie->root);
656 if (!search_root)
Yonghong Songb471f2f2018-01-18 15:08:50 -0800657 return -ENOENT;
658
659 /* For invalid key, find the leftmost node in the trie */
Yonghong Song6dd1ec62018-01-26 15:06:07 -0800660 if (!key || key->prefixlen > trie->max_prefixlen)
Yonghong Songb471f2f2018-01-18 15:08:50 -0800661 goto find_leftmost;
Yonghong Songb471f2f2018-01-18 15:08:50 -0800662
Kees Cook6da2ec52018-06-12 13:55:00 -0700663 node_stack = kmalloc_array(trie->max_prefixlen,
664 sizeof(struct lpm_trie_node *),
665 GFP_ATOMIC | __GFP_NOWARN);
Yonghong Songb471f2f2018-01-18 15:08:50 -0800666 if (!node_stack)
667 return -ENOMEM;
668
669 /* Try to find the exact node for the given key */
Yonghong Song6dd1ec62018-01-26 15:06:07 -0800670 for (node = search_root; node;) {
Yonghong Songb471f2f2018-01-18 15:08:50 -0800671 node_stack[++stack_ptr] = node;
672 matchlen = longest_prefix_match(trie, node, key);
673 if (node->prefixlen != matchlen ||
674 node->prefixlen == key->prefixlen)
675 break;
676
677 next_bit = extract_bit(key->data, node->prefixlen);
678 node = rcu_dereference(node->child[next_bit]);
679 }
680 if (!node || node->prefixlen != key->prefixlen ||
Yonghong Song6dd1ec62018-01-26 15:06:07 -0800681 (node->flags & LPM_TREE_NODE_FLAG_IM))
Yonghong Songb471f2f2018-01-18 15:08:50 -0800682 goto find_leftmost;
Yonghong Songb471f2f2018-01-18 15:08:50 -0800683
684 /* The node with the exactly-matching key has been found,
685 * find the first node in postorder after the matched node.
686 */
687 node = node_stack[stack_ptr];
688 while (stack_ptr > 0) {
689 parent = node_stack[stack_ptr - 1];
Yonghong Song6dd1ec62018-01-26 15:06:07 -0800690 if (rcu_dereference(parent->child[0]) == node) {
691 search_root = rcu_dereference(parent->child[1]);
692 if (search_root)
693 goto find_leftmost;
Yonghong Songb471f2f2018-01-18 15:08:50 -0800694 }
695 if (!(parent->flags & LPM_TREE_NODE_FLAG_IM)) {
696 next_node = parent;
697 goto do_copy;
698 }
699
700 node = parent;
701 stack_ptr--;
702 }
703
704 /* did not find anything */
705 err = -ENOENT;
706 goto free_stack;
707
708find_leftmost:
709 /* Find the leftmost non-intermediate node, all intermediate nodes
710 * have exact two children, so this function will never return NULL.
711 */
Yonghong Song6dd1ec62018-01-26 15:06:07 -0800712 for (node = search_root; node;) {
Yonghong Songb471f2f2018-01-18 15:08:50 -0800713 if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
714 next_node = node;
715 node = rcu_dereference(node->child[0]);
716 }
717do_copy:
718 next_key->prefixlen = next_node->prefixlen;
719 memcpy((void *)next_key + offsetof(struct bpf_lpm_trie_key, data),
720 next_node->data, trie->data_size);
721free_stack:
722 kfree(node_stack);
723 return err;
Alexei Starovoitovf38837b2017-03-05 09:41:08 -0800724}
725
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200726static int trie_check_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800727 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200728 const struct btf_type *key_type,
729 const struct btf_type *value_type)
730{
731 /* Keys must have struct bpf_lpm_trie_key embedded. */
732 return BTF_INFO_KIND(key_type->info) != BTF_KIND_STRUCT ?
733 -EINVAL : 0;
734}
735
Johannes Berg40077e02017-04-11 15:34:58 +0200736const struct bpf_map_ops trie_map_ops = {
Daniel Mackb95a5c42017-01-21 17:26:11 +0100737 .map_alloc = trie_alloc,
738 .map_free = trie_free,
Alexei Starovoitovf38837b2017-03-05 09:41:08 -0800739 .map_get_next_key = trie_get_next_key,
Daniel Mackb95a5c42017-01-21 17:26:11 +0100740 .map_lookup_elem = trie_lookup_elem,
741 .map_update_elem = trie_update_elem,
742 .map_delete_elem = trie_delete_elem,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200743 .map_check_btf = trie_check_btf,
Daniel Mackb95a5c42017-01-21 17:26:11 +0100744};