Skip to content

Commit db58408

Browse files
Hou TaoSasha Levin
authored andcommitted
bpf: Handle BPF_EXIST and BPF_NOEXIST for LPM trie
[ Upstream commit eae6a07 ] 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]> Signed-off-by: Sasha Levin <[email protected]>
1 parent c1eb8e3 commit db58408

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
@@ -376,6 +376,10 @@ static long trie_update_elem(struct bpf_map *map,
376376
* simply assign the @new_node to that slot and be done.
377377
*/
378378
if (!node) {
379+
if (flags == BPF_EXIST) {
380+
ret = -ENOENT;
381+
goto out;
382+
}
379383
rcu_assign_pointer(*slot, new_node);
380384
goto out;
381385
}
@@ -384,18 +388,31 @@ static long trie_update_elem(struct bpf_map *map,
384388
* which already has the correct data array set.
385389
*/
386390
if (node->prefixlen == matchlen) {
391+
if (!(node->flags & LPM_TREE_NODE_FLAG_IM)) {
392+
if (flags == BPF_NOEXIST) {
393+
ret = -EEXIST;
394+
goto out;
395+
}
396+
trie->n_entries--;
397+
} else if (flags == BPF_EXIST) {
398+
ret = -ENOENT;
399+
goto out;
400+
}
401+
387402
new_node->child[0] = node->child[0];
388403
new_node->child[1] = node->child[1];
389404

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

396408
goto out;
397409
}
398410

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

0 commit comments

Comments
 (0)