Skip to content

Commit e0a21f1

Browse files
yonghong-songkernel-patches-bot
authored andcommitted
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 44 #44/1 hash_map:OK #44/2 array_map:OK #44 for_each:OK Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Yonghong Song <[email protected]>
1 parent fa69646 commit e0a21f1

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

tools/testing/selftests/bpf/prog_tests/for_each.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* Copyright (c) 2021 Facebook */
33
#include <test_progs.h>
44
#include "for_each_hash_map_elem.skel.h"
5+
#include "for_each_array_map_elem.skel.h"
56

67
static int duration;
78

@@ -84,8 +85,61 @@ static void test_hash_map(void)
8485
for_each_hash_map_elem__destroy(skel);
8586
}
8687

88+
static void test_array_map(void)
89+
{
90+
int i, arraymap_fd, percpu_map_fd, err;
91+
struct for_each_array_map_elem *skel;
92+
__u32 key, num_cpus, max_entries;
93+
__u64 *percpu_valbuf = NULL;
94+
__u64 val, expected_total;
95+
96+
skel = for_each_array_map_elem__open_and_load();
97+
if (CHECK(!skel, "for_each_array_map_elem__open_and_load",
98+
"skeleton open_and_load failed\n"))
99+
return;
100+
101+
arraymap_fd = bpf_map__fd(skel->maps.arraymap);
102+
expected_total = 0;
103+
max_entries = bpf_map__max_entries(skel->maps.arraymap);
104+
for (i = 0; i < max_entries; i++) {
105+
key = i;
106+
val = i + 1;
107+
/* skip the last iteration for expected total */
108+
if (i != max_entries - 1)
109+
expected_total += val;
110+
err = bpf_map_update_elem(arraymap_fd, &key, &val, BPF_ANY);
111+
if (CHECK(err, "map_update", "map_update failed\n"))
112+
goto out;
113+
}
114+
115+
num_cpus = bpf_num_possible_cpus();
116+
percpu_map_fd = bpf_map__fd(skel->maps.percpu_map);
117+
percpu_valbuf = malloc(sizeof(__u64) * num_cpus);
118+
if (CHECK_FAIL(!percpu_valbuf))
119+
goto out;
120+
121+
key = 0;
122+
for (i = 0; i < num_cpus; i++)
123+
percpu_valbuf[i] = i + 1;
124+
err = bpf_map_update_elem(percpu_map_fd, &key, percpu_valbuf, BPF_ANY);
125+
if (CHECK(err, "percpu_map_update", "map_update failed\n"))
126+
goto out;
127+
128+
do_dummy_read(skel->progs.dump_task);
129+
130+
ASSERT_EQ(skel->bss->called, 1, "called");
131+
ASSERT_EQ(skel->bss->arraymap_output, expected_total, "array_output");
132+
ASSERT_EQ(skel->bss->cpu + 1, skel->bss->percpu_val, "percpu_val");
133+
134+
out:
135+
free(percpu_valbuf);
136+
for_each_array_map_elem__destroy(skel);
137+
}
138+
87139
void test_for_each(void)
88140
{
89141
if (test__start_subtest("hash_map"))
90142
test_hash_map();
143+
if (test__start_subtest("array_map"))
144+
test_array_map();
91145
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2021 Facebook */
3+
#include "bpf_iter.h"
4+
#include <bpf/bpf_helpers.h>
5+
6+
char _license[] SEC("license") = "GPL";
7+
8+
struct {
9+
__uint(type, BPF_MAP_TYPE_ARRAY);
10+
__uint(max_entries, 3);
11+
__type(key, __u32);
12+
__type(value, __u64);
13+
} arraymap SEC(".maps");
14+
15+
struct {
16+
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
17+
__uint(max_entries, 1);
18+
__type(key, __u32);
19+
__type(value, __u64);
20+
} percpu_map SEC(".maps");
21+
22+
struct callback_ctx {
23+
int output;
24+
};
25+
26+
static __u64
27+
check_array_elem(struct bpf_map *map, __u32 *key, __u64 *val,
28+
struct callback_ctx *data)
29+
{
30+
data->output += *val;
31+
if (*key == 1)
32+
return 1; /* stop the iteration */
33+
return 0;
34+
}
35+
36+
__u32 cpu = 0;
37+
__u64 percpu_val = 0;
38+
39+
static __u64
40+
check_percpu_elem(struct bpf_map *map, __u32 *key, __u64 *val,
41+
struct callback_ctx *data)
42+
{
43+
cpu = bpf_get_smp_processor_id();
44+
percpu_val = *val;
45+
return 0;
46+
}
47+
48+
u32 called = 0;
49+
u32 arraymap_output = 0;
50+
51+
SEC("iter/task")
52+
int dump_task(struct bpf_iter__task *ctx)
53+
{
54+
struct seq_file *seq = ctx->meta->seq;
55+
struct task_struct *task = ctx->task;
56+
struct callback_ctx data;
57+
58+
/* only call once */
59+
if (called > 0)
60+
return 0;
61+
62+
called++;
63+
64+
data.output = 0;
65+
bpf_for_each_map_elem(&arraymap, check_array_elem, &data, 0);
66+
arraymap_output = data.output;
67+
68+
bpf_for_each_map_elem(&percpu_map, check_percpu_elem, 0, 0);
69+
70+
return 0;
71+
}

0 commit comments

Comments
 (0)