lpm_trie.c 20 KB

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