lpm_trie.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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->prefixlen != matchlen ||
  374. (node->flags & LPM_TREE_NODE_FLAG_IM)) {
  375. ret = -ENOENT;
  376. goto out;
  377. }
  378. trie->n_entries--;
  379. /* If the node we are removing has two children, simply mark it
  380. * as intermediate and we are done.
  381. */
  382. if (rcu_access_pointer(node->child[0]) &&
  383. rcu_access_pointer(node->child[1])) {
  384. node->flags |= LPM_TREE_NODE_FLAG_IM;
  385. goto out;
  386. }
  387. /* If the parent of the node we are about to delete is an intermediate
  388. * node, and the deleted node doesn't have any children, we can delete
  389. * the intermediate parent as well and promote its other child
  390. * up the tree. Doing this maintains the invariant that all
  391. * intermediate nodes have exactly 2 children and that there are no
  392. * unnecessary intermediate nodes in the tree.
  393. */
  394. if (parent && (parent->flags & LPM_TREE_NODE_FLAG_IM) &&
  395. !node->child[0] && !node->child[1]) {
  396. if (node == rcu_access_pointer(parent->child[0]))
  397. rcu_assign_pointer(
  398. *trim2, rcu_access_pointer(parent->child[1]));
  399. else
  400. rcu_assign_pointer(
  401. *trim2, rcu_access_pointer(parent->child[0]));
  402. kfree_rcu(parent, rcu);
  403. kfree_rcu(node, rcu);
  404. goto out;
  405. }
  406. /* The node we are removing has either zero or one child. If there
  407. * is a child, move it into the removed node's slot then delete
  408. * the node. Otherwise just clear the slot and delete the node.
  409. */
  410. if (node->child[0])
  411. rcu_assign_pointer(*trim, rcu_access_pointer(node->child[0]));
  412. else if (node->child[1])
  413. rcu_assign_pointer(*trim, rcu_access_pointer(node->child[1]));
  414. else
  415. RCU_INIT_POINTER(*trim, NULL);
  416. kfree_rcu(node, rcu);
  417. out:
  418. raw_spin_unlock_irqrestore(&trie->lock, irq_flags);
  419. return ret;
  420. }
  421. #define LPM_DATA_SIZE_MAX 256
  422. #define LPM_DATA_SIZE_MIN 1
  423. #define LPM_VAL_SIZE_MAX (KMALLOC_MAX_SIZE - LPM_DATA_SIZE_MAX - \
  424. sizeof(struct lpm_trie_node))
  425. #define LPM_VAL_SIZE_MIN 1
  426. #define LPM_KEY_SIZE(X) (sizeof(struct bpf_lpm_trie_key) + (X))
  427. #define LPM_KEY_SIZE_MAX LPM_KEY_SIZE(LPM_DATA_SIZE_MAX)
  428. #define LPM_KEY_SIZE_MIN LPM_KEY_SIZE(LPM_DATA_SIZE_MIN)
  429. #define LPM_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_NUMA_NODE | \
  430. BPF_F_RDONLY | BPF_F_WRONLY)
  431. static struct bpf_map *trie_alloc(union bpf_attr *attr)
  432. {
  433. struct lpm_trie *trie;
  434. u64 cost = sizeof(*trie), cost_per_node;
  435. int ret;
  436. if (!capable(CAP_SYS_ADMIN))
  437. return ERR_PTR(-EPERM);
  438. /* check sanity of attributes */
  439. if (attr->max_entries == 0 ||
  440. !(attr->map_flags & BPF_F_NO_PREALLOC) ||
  441. attr->map_flags & ~LPM_CREATE_FLAG_MASK ||
  442. attr->key_size < LPM_KEY_SIZE_MIN ||
  443. attr->key_size > LPM_KEY_SIZE_MAX ||
  444. attr->value_size < LPM_VAL_SIZE_MIN ||
  445. attr->value_size > LPM_VAL_SIZE_MAX)
  446. return ERR_PTR(-EINVAL);
  447. trie = kzalloc(sizeof(*trie), GFP_USER | __GFP_NOWARN);
  448. if (!trie)
  449. return ERR_PTR(-ENOMEM);
  450. /* copy mandatory map attributes */
  451. bpf_map_init_from_attr(&trie->map, attr);
  452. trie->data_size = attr->key_size -
  453. offsetof(struct bpf_lpm_trie_key, data);
  454. trie->max_prefixlen = trie->data_size * 8;
  455. cost_per_node = sizeof(struct lpm_trie_node) +
  456. attr->value_size + trie->data_size;
  457. cost += (u64) attr->max_entries * cost_per_node;
  458. if (cost >= U32_MAX - PAGE_SIZE) {
  459. ret = -E2BIG;
  460. goto out_err;
  461. }
  462. trie->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
  463. ret = bpf_map_precharge_memlock(trie->map.pages);
  464. if (ret)
  465. goto out_err;
  466. raw_spin_lock_init(&trie->lock);
  467. return &trie->map;
  468. out_err:
  469. kfree(trie);
  470. return ERR_PTR(ret);
  471. }
  472. static void trie_free(struct bpf_map *map)
  473. {
  474. struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
  475. struct lpm_trie_node __rcu **slot;
  476. struct lpm_trie_node *node;
  477. /* Wait for outstanding programs to complete
  478. * update/lookup/delete/get_next_key and free the trie.
  479. */
  480. synchronize_rcu();
  481. /* Always start at the root and walk down to a node that has no
  482. * children. Then free that node, nullify its reference in the parent
  483. * and start over.
  484. */
  485. for (;;) {
  486. slot = &trie->root;
  487. for (;;) {
  488. node = rcu_dereference_protected(*slot, 1);
  489. if (!node)
  490. goto out;
  491. if (rcu_access_pointer(node->child[0])) {
  492. slot = &node->child[0];
  493. continue;
  494. }
  495. if (rcu_access_pointer(node->child[1])) {
  496. slot = &node->child[1];
  497. continue;
  498. }
  499. kfree(node);
  500. RCU_INIT_POINTER(*slot, NULL);
  501. break;
  502. }
  503. }
  504. out:
  505. kfree(trie);
  506. }
  507. static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key)
  508. {
  509. struct lpm_trie_node *node, *next_node = NULL, *parent, *search_root;
  510. struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
  511. struct bpf_lpm_trie_key *key = _key, *next_key = _next_key;
  512. struct lpm_trie_node **node_stack = NULL;
  513. int err = 0, stack_ptr = -1;
  514. unsigned int next_bit;
  515. size_t matchlen;
  516. /* The get_next_key follows postorder. For the 4 node example in
  517. * the top of this file, the trie_get_next_key() returns the following
  518. * one after another:
  519. * 192.168.0.0/24
  520. * 192.168.1.0/24
  521. * 192.168.128.0/24
  522. * 192.168.0.0/16
  523. *
  524. * The idea is to return more specific keys before less specific ones.
  525. */
  526. /* Empty trie */
  527. search_root = rcu_dereference(trie->root);
  528. if (!search_root)
  529. return -ENOENT;
  530. /* For invalid key, find the leftmost node in the trie */
  531. if (!key || key->prefixlen > trie->max_prefixlen)
  532. goto find_leftmost;
  533. node_stack = kmalloc_array(trie->max_prefixlen,
  534. sizeof(struct lpm_trie_node *),
  535. GFP_ATOMIC | __GFP_NOWARN);
  536. if (!node_stack)
  537. return -ENOMEM;
  538. /* Try to find the exact node for the given key */
  539. for (node = search_root; node;) {
  540. node_stack[++stack_ptr] = node;
  541. matchlen = longest_prefix_match(trie, node, key);
  542. if (node->prefixlen != matchlen ||
  543. node->prefixlen == key->prefixlen)
  544. break;
  545. next_bit = extract_bit(key->data, node->prefixlen);
  546. node = rcu_dereference(node->child[next_bit]);
  547. }
  548. if (!node || node->prefixlen != key->prefixlen ||
  549. (node->flags & LPM_TREE_NODE_FLAG_IM))
  550. goto find_leftmost;
  551. /* The node with the exactly-matching key has been found,
  552. * find the first node in postorder after the matched node.
  553. */
  554. node = node_stack[stack_ptr];
  555. while (stack_ptr > 0) {
  556. parent = node_stack[stack_ptr - 1];
  557. if (rcu_dereference(parent->child[0]) == node) {
  558. search_root = rcu_dereference(parent->child[1]);
  559. if (search_root)
  560. goto find_leftmost;
  561. }
  562. if (!(parent->flags & LPM_TREE_NODE_FLAG_IM)) {
  563. next_node = parent;
  564. goto do_copy;
  565. }
  566. node = parent;
  567. stack_ptr--;
  568. }
  569. /* did not find anything */
  570. err = -ENOENT;
  571. goto free_stack;
  572. find_leftmost:
  573. /* Find the leftmost non-intermediate node, all intermediate nodes
  574. * have exact two children, so this function will never return NULL.
  575. */
  576. for (node = search_root; node;) {
  577. if (node->flags & LPM_TREE_NODE_FLAG_IM) {
  578. node = rcu_dereference(node->child[0]);
  579. } else {
  580. next_node = node;
  581. node = rcu_dereference(node->child[0]);
  582. if (!node)
  583. node = rcu_dereference(next_node->child[1]);
  584. }
  585. }
  586. do_copy:
  587. next_key->prefixlen = next_node->prefixlen;
  588. memcpy((void *)next_key + offsetof(struct bpf_lpm_trie_key, data),
  589. next_node->data, trie->data_size);
  590. free_stack:
  591. kfree(node_stack);
  592. return err;
  593. }
  594. static int trie_check_btf(const struct bpf_map *map,
  595. const struct btf_type *key_type,
  596. const struct btf_type *value_type)
  597. {
  598. /* Keys must have struct bpf_lpm_trie_key embedded. */
  599. return BTF_INFO_KIND(key_type->info) != BTF_KIND_STRUCT ?
  600. -EINVAL : 0;
  601. }
  602. const struct bpf_map_ops trie_map_ops = {
  603. .map_alloc = trie_alloc,
  604. .map_free = trie_free,
  605. .map_get_next_key = trie_get_next_key,
  606. .map_lookup_elem = trie_lookup_elem,
  607. .map_update_elem = trie_update_elem,
  608. .map_delete_elem = trie_delete_elem,
  609. .map_check_btf = trie_check_btf,
  610. };