-
Notifications
You must be signed in to change notification settings - Fork 0
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 array map iterators
Two subtests are added. $ ./test_progs -n 4 ... #4/20 bpf_array_map:OK #4/21 bpf_percpu_array_map:OK ... The bpf_array_map subtest also tested bpf program changing array element values and send key/value to user space through bpf_seq_write() interface. Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200723184121.591367-1-yhs@fb.com
- Loading branch information
Yonghong Song
authored and
Alexei Starovoitov
committed
Jul 26, 2020
1 parent
2a7c2ff
commit 60dd49e
Showing
3 changed files
with
247 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
40 changes: 40 additions & 0 deletions
40
tools/testing/selftests/bpf/progs/bpf_iter_bpf_array_map.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,40 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2020 Facebook */ | ||
#include "bpf_iter.h" | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
|
||
struct key_t { | ||
int a; | ||
int b; | ||
int c; | ||
}; | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_ARRAY); | ||
__uint(max_entries, 3); | ||
__type(key, __u32); | ||
__type(value, __u64); | ||
} arraymap1 SEC(".maps"); | ||
|
||
__u32 key_sum = 0; | ||
__u64 val_sum = 0; | ||
|
||
SEC("iter/bpf_map_elem") | ||
int dump_bpf_array_map(struct bpf_iter__bpf_map_elem *ctx) | ||
{ | ||
__u32 *key = ctx->key; | ||
__u64 *val = ctx->value; | ||
|
||
if (key == (void *)0 || val == (void *)0) | ||
return 0; | ||
|
||
bpf_seq_write(ctx->meta->seq, key, sizeof(__u32)); | ||
bpf_seq_write(ctx->meta->seq, val, sizeof(__u64)); | ||
key_sum += *key; | ||
val_sum += *val; | ||
*val = *key; | ||
return 0; | ||
} |
46 changes: 46 additions & 0 deletions
46
tools/testing/selftests/bpf/progs/bpf_iter_bpf_percpu_array_map.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,46 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2020 Facebook */ | ||
#include "bpf_iter.h" | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
|
||
struct key_t { | ||
int a; | ||
int b; | ||
int c; | ||
}; | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); | ||
__uint(max_entries, 3); | ||
__type(key, __u32); | ||
__type(value, __u32); | ||
} arraymap1 SEC(".maps"); | ||
|
||
/* will set before prog run */ | ||
volatile const __u32 num_cpus = 0; | ||
|
||
__u32 key_sum = 0, val_sum = 0; | ||
|
||
SEC("iter/bpf_map_elem") | ||
int dump_bpf_percpu_array_map(struct bpf_iter__bpf_map_elem *ctx) | ||
{ | ||
__u32 *key = ctx->key; | ||
void *pptr = ctx->value; | ||
__u32 step; | ||
int i; | ||
|
||
if (key == (void *)0 || pptr == (void *)0) | ||
return 0; | ||
|
||
key_sum += *key; | ||
|
||
step = 8; | ||
for (i = 0; i < num_cpus; i++) { | ||
val_sum += *(__u32 *)pptr; | ||
pptr += step; | ||
} | ||
return 0; | ||
} |