forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: add test for bpf_for_each_map_elem() with different maps
A test is added for bpf_for_each_map_elem() with either an arraymap or a hashmap. $ tools/testing/selftests/bpf/test_progs -t for_each torvalds#93/1 for_each/hash_map:OK torvalds#93/2 for_each/array_map:OK torvalds#93/3 for_each/write_map_key:OK torvalds#93/4 for_each/multi_maps:OK torvalds#93 for_each:OK Summary: 1/4 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Philo Lu <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
- Loading branch information
Philo Lu
authored and
Alexei Starovoitov
committed
Apr 5, 2024
1 parent
9d482da
commit fecb159
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
#include "vmlinux.h" | ||
#include <bpf/bpf_helpers.h> | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_ARRAY); | ||
__uint(max_entries, 3); | ||
__type(key, __u32); | ||
__type(value, __u64); | ||
} arraymap SEC(".maps"); | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_HASH); | ||
__uint(max_entries, 5); | ||
__type(key, __u32); | ||
__type(value, __u64); | ||
} hashmap SEC(".maps"); | ||
|
||
struct callback_ctx { | ||
int output; | ||
}; | ||
|
||
u32 data_output = 0; | ||
int use_array = 0; | ||
|
||
static __u64 | ||
check_map_elem(struct bpf_map *map, __u32 *key, __u64 *val, | ||
struct callback_ctx *data) | ||
{ | ||
data->output += *val; | ||
return 0; | ||
} | ||
|
||
SEC("tc") | ||
int test_pkt_access(struct __sk_buff *skb) | ||
{ | ||
struct callback_ctx data; | ||
|
||
data.output = 0; | ||
if (use_array) | ||
bpf_for_each_map_elem(&arraymap, check_map_elem, &data, 0); | ||
else | ||
bpf_for_each_map_elem(&hashmap, check_map_elem, &data, 0); | ||
data_output = data.output; | ||
|
||
return 0; | ||
} |