lpm_trie.c 22 KB

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