Skip to content

Commit ea3f66d

Browse files
jlemonkondors1995
authored andcommitted
bpf: lpm_trie: check left child of last leftmost node for NULL
If the leftmost parent node of the tree has does not have a child on the left side, then trie_get_next_key (and bpftool map dump) will not look at the child on the right. This leads to the traversal missing elements. Lookup is not affected. Update selftest to handle this case. Reproducer: bpftool map create /sys/fs/bpf/lpm type lpm_trie key 6 \ value 1 entries 256 name test_lpm flags 1 bpftool map update pinned /sys/fs/bpf/lpm key 8 0 0 0 0 0 value 1 bpftool map update pinned /sys/fs/bpf/lpm key 16 0 0 0 0 128 value 2 bpftool map dump pinned /sys/fs/bpf/lpm Returns only 1 element. (2 expected) Fixes: b471f2f1de8b ("bpf: implement MAP_GET_NEXT_KEY command for LPM_TRIE") Change-Id: I942431b7feaa82aab38d4c37b3b5920ae70d8e24 Signed-off-by: Jonathan Lemon <jonathan.lemon@gmail.com> Acked-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent b539d1c commit ea3f66d

File tree

2 files changed

+7
-361
lines changed

2 files changed

+7
-361
lines changed

kernel/bpf/lpm_trie.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,14 @@ static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key)
592592
* have exact two children, so this function will never return NULL.
593593
*/
594594
for (node = search_root; node;) {
595-
if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
595+
if (node->flags & LPM_TREE_NODE_FLAG_IM) {
596+
node = rcu_dereference(node->child[0]);
597+
} else {
596598
next_node = node;
597-
node = rcu_dereference(node->child[0]);
599+
node = rcu_dereference(node->child[0]);
600+
if (!node)
601+
node = rcu_dereference(next_node->child[1]);
602+
}
598603
}
599604
do_copy:
600605
next_key->prefixlen = next_node->prefixlen;

tools/testing/selftests/bpf/test_lpm_map.c

Lines changed: 0 additions & 359 deletions
This file was deleted.

0 commit comments

Comments
 (0)