b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 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 | |
| 9 | #include <linux/bpf.h> |
| 10 | #include <linux/btf.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/spinlock.h> |
| 14 | #include <linux/vmalloc.h> |
| 15 | #include <net/ipv6.h> |
| 16 | #include <uapi/linux/btf.h> |
| 17 | |
| 18 | /* Intermediate node */ |
| 19 | #define LPM_TREE_NODE_FLAG_IM BIT(0) |
| 20 | |
| 21 | struct lpm_trie_node; |
| 22 | |
| 23 | struct lpm_trie_node { |
| 24 | struct rcu_head rcu; |
| 25 | struct lpm_trie_node __rcu *child[2]; |
| 26 | u32 prefixlen; |
| 27 | u32 flags; |
| 28 | u8 data[0]; |
| 29 | }; |
| 30 | |
| 31 | struct lpm_trie { |
| 32 | struct bpf_map map; |
| 33 | struct lpm_trie_node __rcu *root; |
| 34 | size_t n_entries; |
| 35 | size_t max_prefixlen; |
| 36 | size_t data_size; |
| 37 | raw_spinlock_t lock; |
| 38 | }; |
| 39 | |
| 40 | /* This trie implements a longest prefix match algorithm that can be used to |
| 41 | * match IP addresses to a stored set of ranges. |
| 42 | * |
| 43 | * Data stored in @data of struct bpf_lpm_key and struct lpm_trie_node is |
| 44 | * interpreted as big endian, so data[0] stores the most significant byte. |
| 45 | * |
| 46 | * Match ranges are internally stored in instances of struct lpm_trie_node |
| 47 | * which each contain their prefix length as well as two pointers that may |
| 48 | * lead to more nodes containing more specific matches. Each node also stores |
| 49 | * a value that is defined by and returned to userspace via the update_elem |
| 50 | * and lookup functions. |
| 51 | * |
| 52 | * For instance, let's start with a trie that was created with a prefix length |
| 53 | * of 32, so it can be used for IPv4 addresses, and one single element that |
| 54 | * matches 192.168.0.0/16. The data array would hence contain |
| 55 | * [0xc0, 0xa8, 0x00, 0x00] in big-endian notation. This documentation will |
| 56 | * stick to IP-address notation for readability though. |
| 57 | * |
| 58 | * As the trie is empty initially, the new node (1) will be places as root |
| 59 | * node, denoted as (R) in the example below. As there are no other node, both |
| 60 | * child pointers are %NULL. |
| 61 | * |
| 62 | * +----------------+ |
| 63 | * | (1) (R) | |
| 64 | * | 192.168.0.0/16 | |
| 65 | * | value: 1 | |
| 66 | * | [0] [1] | |
| 67 | * +----------------+ |
| 68 | * |
| 69 | * Next, let's add a new node (2) matching 192.168.0.0/24. As there is already |
| 70 | * a node with the same data and a smaller prefix (ie, a less specific one), |
| 71 | * node (2) will become a child of (1). In child index depends on the next bit |
| 72 | * that is outside of what (1) matches, and that bit is 0, so (2) will be |
| 73 | * child[0] of (1): |
| 74 | * |
| 75 | * +----------------+ |
| 76 | * | (1) (R) | |
| 77 | * | 192.168.0.0/16 | |
| 78 | * | value: 1 | |
| 79 | * | [0] [1] | |
| 80 | * +----------------+ |
| 81 | * | |
| 82 | * +----------------+ |
| 83 | * | (2) | |
| 84 | * | 192.168.0.0/24 | |
| 85 | * | value: 2 | |
| 86 | * | [0] [1] | |
| 87 | * +----------------+ |
| 88 | * |
| 89 | * The child[1] slot of (1) could be filled with another node which has bit #17 |
| 90 | * (the next bit after the ones that (1) matches on) set to 1. For instance, |
| 91 | * 192.168.128.0/24: |
| 92 | * |
| 93 | * +----------------+ |
| 94 | * | (1) (R) | |
| 95 | * | 192.168.0.0/16 | |
| 96 | * | value: 1 | |
| 97 | * | [0] [1] | |
| 98 | * +----------------+ |
| 99 | * | | |
| 100 | * +----------------+ +------------------+ |
| 101 | * | (2) | | (3) | |
| 102 | * | 192.168.0.0/24 | | 192.168.128.0/24 | |
| 103 | * | value: 2 | | value: 3 | |
| 104 | * | [0] [1] | | [0] [1] | |
| 105 | * +----------------+ +------------------+ |
| 106 | * |
| 107 | * Let's add another node (4) to the game for 192.168.1.0/24. In order to place |
| 108 | * it, node (1) is looked at first, and because (4) of the semantics laid out |
| 109 | * above (bit #17 is 0), it would normally be attached to (1) as child[0]. |
| 110 | * However, that slot is already allocated, so a new node is needed in between. |
| 111 | * That node does not have a value attached to it and it will never be |
| 112 | * returned to users as result of a lookup. It is only there to differentiate |
| 113 | * the traversal further. It will get a prefix as wide as necessary to |
| 114 | * distinguish its two children: |
| 115 | * |
| 116 | * +----------------+ |
| 117 | * | (1) (R) | |
| 118 | * | 192.168.0.0/16 | |
| 119 | * | value: 1 | |
| 120 | * | [0] [1] | |
| 121 | * +----------------+ |
| 122 | * | | |
| 123 | * +----------------+ +------------------+ |
| 124 | * | (4) (I) | | (3) | |
| 125 | * | 192.168.0.0/23 | | 192.168.128.0/24 | |
| 126 | * | value: --- | | value: 3 | |
| 127 | * | [0] [1] | | [0] [1] | |
| 128 | * +----------------+ +------------------+ |
| 129 | * | | |
| 130 | * +----------------+ +----------------+ |
| 131 | * | (2) | | (5) | |
| 132 | * | 192.168.0.0/24 | | 192.168.1.0/24 | |
| 133 | * | value: 2 | | value: 5 | |
| 134 | * | [0] [1] | | [0] [1] | |
| 135 | * +----------------+ +----------------+ |
| 136 | * |
| 137 | * 192.168.1.1/32 would be a child of (5) etc. |
| 138 | * |
| 139 | * An intermediate node will be turned into a 'real' node on demand. In the |
| 140 | * example above, (4) would be re-used if 192.168.0.0/23 is added to the trie. |
| 141 | * |
| 142 | * A fully populated trie would have a height of 32 nodes, as the trie was |
| 143 | * created with a prefix length of 32. |
| 144 | * |
| 145 | * The lookup starts at the root node. If the current node matches and if there |
| 146 | * is a child that can be used to become more specific, the trie is traversed |
| 147 | * downwards. The last node in the traversal that is a non-intermediate one is |
| 148 | * returned. |
| 149 | */ |
| 150 | |
| 151 | static inline int extract_bit(const u8 *data, size_t index) |
| 152 | { |
| 153 | return !!(data[index / 8] & (1 << (7 - (index % 8)))); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * longest_prefix_match() - determine the longest prefix |
| 158 | * @trie: The trie to get internal sizes from |
| 159 | * @node: The node to operate on |
| 160 | * @key: The key to compare to @node |
| 161 | * |
| 162 | * Determine the longest prefix of @node that matches the bits in @key. |
| 163 | */ |
| 164 | static size_t longest_prefix_match(const struct lpm_trie *trie, |
| 165 | const struct lpm_trie_node *node, |
| 166 | const struct bpf_lpm_trie_key *key) |
| 167 | { |
| 168 | u32 limit = min(node->prefixlen, key->prefixlen); |
| 169 | u32 prefixlen = 0, i = 0; |
| 170 | |
| 171 | BUILD_BUG_ON(offsetof(struct lpm_trie_node, data) % sizeof(u32)); |
| 172 | BUILD_BUG_ON(offsetof(struct bpf_lpm_trie_key, data) % sizeof(u32)); |
| 173 | |
| 174 | #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && defined(CONFIG_64BIT) |
| 175 | |
| 176 | /* data_size >= 16 has very small probability. |
| 177 | * We do not use a loop for optimal code generation. |
| 178 | */ |
| 179 | if (trie->data_size >= 8) { |
| 180 | u64 diff = be64_to_cpu(*(__be64 *)node->data ^ |
| 181 | *(__be64 *)key->data); |
| 182 | |
| 183 | prefixlen = 64 - fls64(diff); |
| 184 | if (prefixlen >= limit) |
| 185 | return limit; |
| 186 | if (diff) |
| 187 | return prefixlen; |
| 188 | i = 8; |
| 189 | } |
| 190 | #endif |
| 191 | |
| 192 | while (trie->data_size >= i + 4) { |
| 193 | u32 diff = be32_to_cpu(*(__be32 *)&node->data[i] ^ |
| 194 | *(__be32 *)&key->data[i]); |
| 195 | |
| 196 | prefixlen += 32 - fls(diff); |
| 197 | if (prefixlen >= limit) |
| 198 | return limit; |
| 199 | if (diff) |
| 200 | return prefixlen; |
| 201 | i += 4; |
| 202 | } |
| 203 | |
| 204 | if (trie->data_size >= i + 2) { |
| 205 | u16 diff = be16_to_cpu(*(__be16 *)&node->data[i] ^ |
| 206 | *(__be16 *)&key->data[i]); |
| 207 | |
| 208 | prefixlen += 16 - fls(diff); |
| 209 | if (prefixlen >= limit) |
| 210 | return limit; |
| 211 | if (diff) |
| 212 | return prefixlen; |
| 213 | i += 2; |
| 214 | } |
| 215 | |
| 216 | if (trie->data_size >= i + 1) { |
| 217 | prefixlen += 8 - fls(node->data[i] ^ key->data[i]); |
| 218 | |
| 219 | if (prefixlen >= limit) |
| 220 | return limit; |
| 221 | } |
| 222 | |
| 223 | return prefixlen; |
| 224 | } |
| 225 | |
| 226 | /* Called from syscall or from eBPF program */ |
| 227 | static void *trie_lookup_elem(struct bpf_map *map, void *_key) |
| 228 | { |
| 229 | struct lpm_trie *trie = container_of(map, struct lpm_trie, map); |
| 230 | struct lpm_trie_node *node, *found = NULL; |
| 231 | struct bpf_lpm_trie_key *key = _key; |
| 232 | |
| 233 | if (key->prefixlen > trie->max_prefixlen) |
| 234 | return NULL; |
| 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 | |
| 279 | static 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 | |
| 288 | node = kmalloc_node(size, GFP_ATOMIC | __GFP_NOWARN, |
| 289 | trie->map.numa_node); |
| 290 | 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 */ |
| 303 | static 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); |
| 307 | struct lpm_trie_node *node, *im_node = NULL, *new_node = NULL; |
| 308 | struct lpm_trie_node __rcu **slot; |
| 309 | struct bpf_lpm_trie_key *key = _key; |
| 310 | unsigned long irq_flags; |
| 311 | unsigned int next_bit; |
| 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 | if (flags == BPF_EXIST) { |
| 368 | ret = -ENOENT; |
| 369 | goto out; |
| 370 | } |
| 371 | rcu_assign_pointer(*slot, new_node); |
| 372 | goto out; |
| 373 | } |
| 374 | |
| 375 | /* If the slot we picked already exists, replace it with @new_node |
| 376 | * which already has the correct data array set. |
| 377 | */ |
| 378 | if (node->prefixlen == matchlen) { |
| 379 | if (!(node->flags & LPM_TREE_NODE_FLAG_IM)) { |
| 380 | if (flags == BPF_NOEXIST) { |
| 381 | ret = -EEXIST; |
| 382 | goto out; |
| 383 | } |
| 384 | trie->n_entries--; |
| 385 | } else if (flags == BPF_EXIST) { |
| 386 | ret = -ENOENT; |
| 387 | goto out; |
| 388 | } |
| 389 | |
| 390 | new_node->child[0] = node->child[0]; |
| 391 | new_node->child[1] = node->child[1]; |
| 392 | |
| 393 | rcu_assign_pointer(*slot, new_node); |
| 394 | kfree_rcu(node, rcu); |
| 395 | |
| 396 | goto out; |
| 397 | } |
| 398 | |
| 399 | if (flags == BPF_EXIST) { |
| 400 | ret = -ENOENT; |
| 401 | goto out; |
| 402 | } |
| 403 | |
| 404 | /* If the new node matches the prefix completely, it must be inserted |
| 405 | * as an ancestor. Simply insert it between @node and *@slot. |
| 406 | */ |
| 407 | if (matchlen == key->prefixlen) { |
| 408 | next_bit = extract_bit(node->data, matchlen); |
| 409 | rcu_assign_pointer(new_node->child[next_bit], node); |
| 410 | rcu_assign_pointer(*slot, new_node); |
| 411 | goto out; |
| 412 | } |
| 413 | |
| 414 | im_node = lpm_trie_node_alloc(trie, NULL); |
| 415 | if (!im_node) { |
| 416 | ret = -ENOMEM; |
| 417 | goto out; |
| 418 | } |
| 419 | |
| 420 | im_node->prefixlen = matchlen; |
| 421 | im_node->flags |= LPM_TREE_NODE_FLAG_IM; |
| 422 | memcpy(im_node->data, node->data, trie->data_size); |
| 423 | |
| 424 | /* Now determine which child to install in which slot */ |
| 425 | if (extract_bit(key->data, matchlen)) { |
| 426 | rcu_assign_pointer(im_node->child[0], node); |
| 427 | rcu_assign_pointer(im_node->child[1], new_node); |
| 428 | } else { |
| 429 | rcu_assign_pointer(im_node->child[0], new_node); |
| 430 | rcu_assign_pointer(im_node->child[1], node); |
| 431 | } |
| 432 | |
| 433 | /* Finally, assign the intermediate node to the determined spot */ |
| 434 | rcu_assign_pointer(*slot, im_node); |
| 435 | |
| 436 | out: |
| 437 | if (ret) { |
| 438 | if (new_node) |
| 439 | trie->n_entries--; |
| 440 | |
| 441 | kfree(new_node); |
| 442 | kfree(im_node); |
| 443 | } |
| 444 | |
| 445 | raw_spin_unlock_irqrestore(&trie->lock, irq_flags); |
| 446 | |
| 447 | return ret; |
| 448 | } |
| 449 | |
| 450 | /* Called from syscall or from eBPF program */ |
| 451 | static int trie_delete_elem(struct bpf_map *map, void *_key) |
| 452 | { |
| 453 | struct lpm_trie *trie = container_of(map, struct lpm_trie, map); |
| 454 | struct bpf_lpm_trie_key *key = _key; |
| 455 | struct lpm_trie_node __rcu **trim, **trim2; |
| 456 | struct lpm_trie_node *node, *parent; |
| 457 | unsigned long irq_flags; |
| 458 | unsigned int next_bit; |
| 459 | size_t matchlen = 0; |
| 460 | int ret = 0; |
| 461 | |
| 462 | if (key->prefixlen > trie->max_prefixlen) |
| 463 | return -EINVAL; |
| 464 | |
| 465 | raw_spin_lock_irqsave(&trie->lock, irq_flags); |
| 466 | |
| 467 | /* Walk the tree looking for an exact key/length match and keeping |
| 468 | * track of the path we traverse. We will need to know the node |
| 469 | * we wish to delete, and the slot that points to the node we want |
| 470 | * to delete. We may also need to know the nodes parent and the |
| 471 | * slot that contains it. |
| 472 | */ |
| 473 | trim = &trie->root; |
| 474 | trim2 = trim; |
| 475 | parent = NULL; |
| 476 | while ((node = rcu_dereference_protected( |
| 477 | *trim, lockdep_is_held(&trie->lock)))) { |
| 478 | matchlen = longest_prefix_match(trie, node, key); |
| 479 | |
| 480 | if (node->prefixlen != matchlen || |
| 481 | node->prefixlen == key->prefixlen) |
| 482 | break; |
| 483 | |
| 484 | parent = node; |
| 485 | trim2 = trim; |
| 486 | next_bit = extract_bit(key->data, node->prefixlen); |
| 487 | trim = &node->child[next_bit]; |
| 488 | } |
| 489 | |
| 490 | if (!node || node->prefixlen != key->prefixlen || |
| 491 | node->prefixlen != matchlen || |
| 492 | (node->flags & LPM_TREE_NODE_FLAG_IM)) { |
| 493 | ret = -ENOENT; |
| 494 | goto out; |
| 495 | } |
| 496 | |
| 497 | trie->n_entries--; |
| 498 | |
| 499 | /* If the node we are removing has two children, simply mark it |
| 500 | * as intermediate and we are done. |
| 501 | */ |
| 502 | if (rcu_access_pointer(node->child[0]) && |
| 503 | rcu_access_pointer(node->child[1])) { |
| 504 | node->flags |= LPM_TREE_NODE_FLAG_IM; |
| 505 | goto out; |
| 506 | } |
| 507 | |
| 508 | /* If the parent of the node we are about to delete is an intermediate |
| 509 | * node, and the deleted node doesn't have any children, we can delete |
| 510 | * the intermediate parent as well and promote its other child |
| 511 | * up the tree. Doing this maintains the invariant that all |
| 512 | * intermediate nodes have exactly 2 children and that there are no |
| 513 | * unnecessary intermediate nodes in the tree. |
| 514 | */ |
| 515 | if (parent && (parent->flags & LPM_TREE_NODE_FLAG_IM) && |
| 516 | !node->child[0] && !node->child[1]) { |
| 517 | if (node == rcu_access_pointer(parent->child[0])) |
| 518 | rcu_assign_pointer( |
| 519 | *trim2, rcu_access_pointer(parent->child[1])); |
| 520 | else |
| 521 | rcu_assign_pointer( |
| 522 | *trim2, rcu_access_pointer(parent->child[0])); |
| 523 | kfree_rcu(parent, rcu); |
| 524 | kfree_rcu(node, rcu); |
| 525 | goto out; |
| 526 | } |
| 527 | |
| 528 | /* The node we are removing has either zero or one child. If there |
| 529 | * is a child, move it into the removed node's slot then delete |
| 530 | * the node. Otherwise just clear the slot and delete the node. |
| 531 | */ |
| 532 | if (node->child[0]) |
| 533 | rcu_assign_pointer(*trim, rcu_access_pointer(node->child[0])); |
| 534 | else if (node->child[1]) |
| 535 | rcu_assign_pointer(*trim, rcu_access_pointer(node->child[1])); |
| 536 | else |
| 537 | RCU_INIT_POINTER(*trim, NULL); |
| 538 | kfree_rcu(node, rcu); |
| 539 | |
| 540 | out: |
| 541 | raw_spin_unlock_irqrestore(&trie->lock, irq_flags); |
| 542 | |
| 543 | return ret; |
| 544 | } |
| 545 | |
| 546 | #define LPM_DATA_SIZE_MAX 256 |
| 547 | #define LPM_DATA_SIZE_MIN 1 |
| 548 | |
| 549 | #define LPM_VAL_SIZE_MAX (KMALLOC_MAX_SIZE - LPM_DATA_SIZE_MAX - \ |
| 550 | sizeof(struct lpm_trie_node)) |
| 551 | #define LPM_VAL_SIZE_MIN 1 |
| 552 | |
| 553 | #define LPM_KEY_SIZE(X) (sizeof(struct bpf_lpm_trie_key) + (X)) |
| 554 | #define LPM_KEY_SIZE_MAX LPM_KEY_SIZE(LPM_DATA_SIZE_MAX) |
| 555 | #define LPM_KEY_SIZE_MIN LPM_KEY_SIZE(LPM_DATA_SIZE_MIN) |
| 556 | |
| 557 | #define LPM_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_NUMA_NODE | \ |
| 558 | BPF_F_ACCESS_MASK) |
| 559 | |
| 560 | static struct bpf_map *trie_alloc(union bpf_attr *attr) |
| 561 | { |
| 562 | struct lpm_trie *trie; |
| 563 | u64 cost = sizeof(*trie), cost_per_node; |
| 564 | int ret; |
| 565 | |
| 566 | if (!capable(CAP_SYS_ADMIN)) |
| 567 | return ERR_PTR(-EPERM); |
| 568 | |
| 569 | /* check sanity of attributes */ |
| 570 | if (attr->max_entries == 0 || |
| 571 | !(attr->map_flags & BPF_F_NO_PREALLOC) || |
| 572 | attr->map_flags & ~LPM_CREATE_FLAG_MASK || |
| 573 | !bpf_map_flags_access_ok(attr->map_flags) || |
| 574 | attr->key_size < LPM_KEY_SIZE_MIN || |
| 575 | attr->key_size > LPM_KEY_SIZE_MAX || |
| 576 | attr->value_size < LPM_VAL_SIZE_MIN || |
| 577 | attr->value_size > LPM_VAL_SIZE_MAX) |
| 578 | return ERR_PTR(-EINVAL); |
| 579 | |
| 580 | trie = kzalloc(sizeof(*trie), GFP_USER | __GFP_NOWARN); |
| 581 | if (!trie) |
| 582 | return ERR_PTR(-ENOMEM); |
| 583 | |
| 584 | /* copy mandatory map attributes */ |
| 585 | bpf_map_init_from_attr(&trie->map, attr); |
| 586 | trie->data_size = attr->key_size - |
| 587 | offsetof(struct bpf_lpm_trie_key, data); |
| 588 | trie->max_prefixlen = trie->data_size * 8; |
| 589 | |
| 590 | cost_per_node = sizeof(struct lpm_trie_node) + |
| 591 | attr->value_size + trie->data_size; |
| 592 | cost += (u64) attr->max_entries * cost_per_node; |
| 593 | |
| 594 | ret = bpf_map_charge_init(&trie->map.memory, cost); |
| 595 | if (ret) |
| 596 | goto out_err; |
| 597 | |
| 598 | raw_spin_lock_init(&trie->lock); |
| 599 | |
| 600 | return &trie->map; |
| 601 | out_err: |
| 602 | kfree(trie); |
| 603 | return ERR_PTR(ret); |
| 604 | } |
| 605 | |
| 606 | static void trie_free(struct bpf_map *map) |
| 607 | { |
| 608 | struct lpm_trie *trie = container_of(map, struct lpm_trie, map); |
| 609 | struct lpm_trie_node __rcu **slot; |
| 610 | struct lpm_trie_node *node; |
| 611 | |
| 612 | /* Wait for outstanding programs to complete |
| 613 | * update/lookup/delete/get_next_key and free the trie. |
| 614 | */ |
| 615 | synchronize_rcu(); |
| 616 | |
| 617 | /* Always start at the root and walk down to a node that has no |
| 618 | * children. Then free that node, nullify its reference in the parent |
| 619 | * and start over. |
| 620 | */ |
| 621 | |
| 622 | for (;;) { |
| 623 | slot = &trie->root; |
| 624 | |
| 625 | for (;;) { |
| 626 | node = rcu_dereference_protected(*slot, 1); |
| 627 | if (!node) |
| 628 | goto out; |
| 629 | |
| 630 | if (rcu_access_pointer(node->child[0])) { |
| 631 | slot = &node->child[0]; |
| 632 | continue; |
| 633 | } |
| 634 | |
| 635 | if (rcu_access_pointer(node->child[1])) { |
| 636 | slot = &node->child[1]; |
| 637 | continue; |
| 638 | } |
| 639 | |
| 640 | kfree(node); |
| 641 | RCU_INIT_POINTER(*slot, NULL); |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | out: |
| 647 | kfree(trie); |
| 648 | } |
| 649 | |
| 650 | static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key) |
| 651 | { |
| 652 | struct lpm_trie_node *node, *next_node = NULL, *parent, *search_root; |
| 653 | struct lpm_trie *trie = container_of(map, struct lpm_trie, map); |
| 654 | struct bpf_lpm_trie_key *key = _key, *next_key = _next_key; |
| 655 | struct lpm_trie_node **node_stack = NULL; |
| 656 | int err = 0, stack_ptr = -1; |
| 657 | unsigned int next_bit; |
| 658 | size_t matchlen = 0; |
| 659 | |
| 660 | /* The get_next_key follows postorder. For the 4 node example in |
| 661 | * the top of this file, the trie_get_next_key() returns the following |
| 662 | * one after another: |
| 663 | * 192.168.0.0/24 |
| 664 | * 192.168.1.0/24 |
| 665 | * 192.168.128.0/24 |
| 666 | * 192.168.0.0/16 |
| 667 | * |
| 668 | * The idea is to return more specific keys before less specific ones. |
| 669 | */ |
| 670 | |
| 671 | /* Empty trie */ |
| 672 | search_root = rcu_dereference(trie->root); |
| 673 | if (!search_root) |
| 674 | return -ENOENT; |
| 675 | |
| 676 | /* For invalid key, find the leftmost node in the trie */ |
| 677 | if (!key || key->prefixlen > trie->max_prefixlen) |
| 678 | goto find_leftmost; |
| 679 | |
| 680 | node_stack = kmalloc_array(trie->max_prefixlen + 1, |
| 681 | sizeof(struct lpm_trie_node *), |
| 682 | GFP_ATOMIC | __GFP_NOWARN); |
| 683 | if (!node_stack) |
| 684 | return -ENOMEM; |
| 685 | |
| 686 | /* Try to find the exact node for the given key */ |
| 687 | for (node = search_root; node;) { |
| 688 | node_stack[++stack_ptr] = node; |
| 689 | matchlen = longest_prefix_match(trie, node, key); |
| 690 | if (node->prefixlen != matchlen || |
| 691 | node->prefixlen == key->prefixlen) |
| 692 | break; |
| 693 | |
| 694 | next_bit = extract_bit(key->data, node->prefixlen); |
| 695 | node = rcu_dereference(node->child[next_bit]); |
| 696 | } |
| 697 | if (!node || node->prefixlen != matchlen || |
| 698 | (node->flags & LPM_TREE_NODE_FLAG_IM)) |
| 699 | goto find_leftmost; |
| 700 | |
| 701 | /* The node with the exactly-matching key has been found, |
| 702 | * find the first node in postorder after the matched node. |
| 703 | */ |
| 704 | node = node_stack[stack_ptr]; |
| 705 | while (stack_ptr > 0) { |
| 706 | parent = node_stack[stack_ptr - 1]; |
| 707 | if (rcu_dereference(parent->child[0]) == node) { |
| 708 | search_root = rcu_dereference(parent->child[1]); |
| 709 | if (search_root) |
| 710 | goto find_leftmost; |
| 711 | } |
| 712 | if (!(parent->flags & LPM_TREE_NODE_FLAG_IM)) { |
| 713 | next_node = parent; |
| 714 | goto do_copy; |
| 715 | } |
| 716 | |
| 717 | node = parent; |
| 718 | stack_ptr--; |
| 719 | } |
| 720 | |
| 721 | /* did not find anything */ |
| 722 | err = -ENOENT; |
| 723 | goto free_stack; |
| 724 | |
| 725 | find_leftmost: |
| 726 | /* Find the leftmost non-intermediate node, all intermediate nodes |
| 727 | * have exact two children, so this function will never return NULL. |
| 728 | */ |
| 729 | for (node = search_root; node;) { |
| 730 | if (node->flags & LPM_TREE_NODE_FLAG_IM) { |
| 731 | node = rcu_dereference(node->child[0]); |
| 732 | } else { |
| 733 | next_node = node; |
| 734 | node = rcu_dereference(node->child[0]); |
| 735 | if (!node) |
| 736 | node = rcu_dereference(next_node->child[1]); |
| 737 | } |
| 738 | } |
| 739 | do_copy: |
| 740 | next_key->prefixlen = next_node->prefixlen; |
| 741 | memcpy((void *)next_key + offsetof(struct bpf_lpm_trie_key, data), |
| 742 | next_node->data, trie->data_size); |
| 743 | free_stack: |
| 744 | kfree(node_stack); |
| 745 | return err; |
| 746 | } |
| 747 | |
| 748 | static int trie_check_btf(const struct bpf_map *map, |
| 749 | const struct btf *btf, |
| 750 | const struct btf_type *key_type, |
| 751 | const struct btf_type *value_type) |
| 752 | { |
| 753 | /* Keys must have struct bpf_lpm_trie_key embedded. */ |
| 754 | return BTF_INFO_KIND(key_type->info) != BTF_KIND_STRUCT ? |
| 755 | -EINVAL : 0; |
| 756 | } |
| 757 | |
| 758 | const struct bpf_map_ops trie_map_ops = { |
| 759 | .map_alloc = trie_alloc, |
| 760 | .map_free = trie_free, |
| 761 | .map_get_next_key = trie_get_next_key, |
| 762 | .map_lookup_elem = trie_lookup_elem, |
| 763 | .map_update_elem = trie_update_elem, |
| 764 | .map_delete_elem = trie_delete_elem, |
| 765 | .map_check_btf = trie_check_btf, |
| 766 | }; |