Skip to content

Commit 8cad50b

Browse files
Hou Taomehmetb0
authored andcommitted
bpf: Handle BPF_EXIST and BPF_NOEXIST for LPM trie
BugLink: https://bugs.launchpad.net/bugs/2095283 [ Upstream commit eae6a075e9537dd69891cf77ca5a88fa8a28b4a1 ] 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 <toke@redhat.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Hou Tao <houtao1@huawei.com> Link: https://lore.kernel.org/r/20241206110622.1161752-4-houtao@huaweicloud.com Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Koichiro Den <koichiro.den@canonical.com>
1 parent bf8fb19 commit 8cad50b

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
@@ -366,6 +366,10 @@ static int trie_update_elem(struct bpf_map *map,
366366
* simply assign the @new_node to that slot and be done.
367367
*/
368368
if (!node) {
369+
if (flags == BPF_EXIST) {
370+
ret = -ENOENT;
371+
goto out;
372+
}
369373
rcu_assign_pointer(*slot, new_node);
370374
goto out;
371375
}
@@ -374,18 +378,31 @@ static int trie_update_elem(struct bpf_map *map,
374378
* which already has the correct data array set.
375379
*/
376380
if (node->prefixlen == matchlen) {
381+
if (!(node->flags & LPM_TREE_NODE_FLAG_IM)) {
382+
if (flags == BPF_NOEXIST) {
383+
ret = -EEXIST;
384+
goto out;
385+
}
386+
trie->n_entries--;
387+
} else if (flags == BPF_EXIST) {
388+
ret = -ENOENT;
389+
goto out;
390+
}
391+
377392
new_node->child[0] = node->child[0];
378393
new_node->child[1] = node->child[1];
379394

380-
if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
381-
trie->n_entries--;
382-
383395
rcu_assign_pointer(*slot, new_node);
384396
kfree_rcu(node, rcu);
385397

386398
goto out;
387399
}
388400

401+
if (flags == BPF_EXIST) {
402+
ret = -ENOENT;
403+
goto out;
404+
}
405+
389406
/* If the new node matches the prefix completely, it must be inserted
390407
* as an ancestor. Simply insert it between @node and *@slot.
391408
*/

0 commit comments

Comments
 (0)