Skip to content

Commit eae6a07

Browse files
Hou TaoAlexei Starovoitov
authored andcommitted
bpf: Handle BPF_EXIST and BPF_NOEXIST for LPM trie
Add the currently missing handling for the BPF_EXIST and BPF_NOEXIST flags. These flags can be specified by users and are relevant since LPM trie supports exact matches during update. Fixes: b95a5c4 ("bpf: add a longest prefix match trie map implementation") Reviewed-by: Toke Høiland-Jørgensen <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Signed-off-by: Hou Tao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 3d5611b commit eae6a07

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

kernel/bpf/lpm_trie.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ static long trie_update_elem(struct bpf_map *map,
375375
* simply assign the @new_node to that slot and be done.
376376
*/
377377
if (!node) {
378+
if (flags == BPF_EXIST) {
379+
ret = -ENOENT;
380+
goto out;
381+
}
378382
rcu_assign_pointer(*slot, new_node);
379383
goto out;
380384
}
@@ -383,18 +387,31 @@ static long trie_update_elem(struct bpf_map *map,
383387
* which already has the correct data array set.
384388
*/
385389
if (node->prefixlen == matchlen) {
390+
if (!(node->flags & LPM_TREE_NODE_FLAG_IM)) {
391+
if (flags == BPF_NOEXIST) {
392+
ret = -EEXIST;
393+
goto out;
394+
}
395+
trie->n_entries--;
396+
} else if (flags == BPF_EXIST) {
397+
ret = -ENOENT;
398+
goto out;
399+
}
400+
386401
new_node->child[0] = node->child[0];
387402
new_node->child[1] = node->child[1];
388403

389-
if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
390-
trie->n_entries--;
391-
392404
rcu_assign_pointer(*slot, new_node);
393405
free_node = node;
394406

395407
goto out;
396408
}
397409

410+
if (flags == BPF_EXIST) {
411+
ret = -ENOENT;
412+
goto out;
413+
}
414+
398415
/* If the new node matches the prefix completely, it must be inserted
399416
* as an ancestor. Simply insert it between @node and *@slot.
400417
*/

0 commit comments

Comments
 (0)