-
-
Notifications
You must be signed in to change notification settings - Fork 43
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 arraymap test for bpf_for_each_map_elem() helper
A test is added for arraymap and percpu arraymap. The test also exercises the early return for the helper which does not traverse all elements. $ ./test_progs -n 45 #45/1 hash_map:OK #45/2 array_map:OK #45 for_each:OK Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Yonghong Song <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
- Loading branch information
1 parent
9de7f0f
commit 6b9e333
Showing
2 changed files
with
118 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
61 changes: 61 additions & 0 deletions
61
tools/testing/selftests/bpf/progs/for_each_array_map_elem.c
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,61 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2021 Facebook */ | ||
#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_PERCPU_ARRAY); | ||
__uint(max_entries, 1); | ||
__type(key, __u32); | ||
__type(value, __u64); | ||
} percpu_map SEC(".maps"); | ||
|
||
struct callback_ctx { | ||
int output; | ||
}; | ||
|
||
static __u64 | ||
check_array_elem(struct bpf_map *map, __u32 *key, __u64 *val, | ||
struct callback_ctx *data) | ||
{ | ||
data->output += *val; | ||
if (*key == 1) | ||
return 1; /* stop the iteration */ | ||
return 0; | ||
} | ||
|
||
__u32 cpu = 0; | ||
__u64 percpu_val = 0; | ||
|
||
static __u64 | ||
check_percpu_elem(struct bpf_map *map, __u32 *key, __u64 *val, | ||
struct callback_ctx *data) | ||
{ | ||
cpu = bpf_get_smp_processor_id(); | ||
percpu_val = *val; | ||
return 0; | ||
} | ||
|
||
u32 arraymap_output = 0; | ||
|
||
SEC("classifier") | ||
int test_pkt_access(struct __sk_buff *skb) | ||
{ | ||
struct callback_ctx data; | ||
|
||
data.output = 0; | ||
bpf_for_each_map_elem(&arraymap, check_array_elem, &data, 0); | ||
arraymap_output = data.output; | ||
|
||
bpf_for_each_map_elem(&percpu_map, check_percpu_elem, (void *)0, 0); | ||
return 0; | ||
} |