Skip to content

Commit 9de7f0f

Browse files
yonghong-songAlexei Starovoitov
authored and
Alexei Starovoitov
committed
selftests/bpf: Add hashmap test for bpf_for_each_map_elem() helper
A test case is added for hashmap and percpu hashmap. The test also exercises nested bpf_for_each_map_elem() calls like bpf_prog: bpf_for_each_map_elem(func1) func1: bpf_for_each_map_elem(func2) func2: $ ./test_progs -n 45 #45/1 hash_map:OK #45 for_each:OK Summary: 1/1 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]
1 parent f1f9f0d commit 9de7f0f

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2021 Facebook */
3+
#include <test_progs.h>
4+
#include <network_helpers.h>
5+
#include "for_each_hash_map_elem.skel.h"
6+
7+
static unsigned int duration;
8+
9+
static void test_hash_map(void)
10+
{
11+
int i, err, hashmap_fd, max_entries, percpu_map_fd;
12+
struct for_each_hash_map_elem *skel;
13+
__u64 *percpu_valbuf = NULL;
14+
__u32 key, num_cpus, retval;
15+
__u64 val;
16+
17+
skel = for_each_hash_map_elem__open_and_load();
18+
if (!ASSERT_OK_PTR(skel, "for_each_hash_map_elem__open_and_load"))
19+
return;
20+
21+
hashmap_fd = bpf_map__fd(skel->maps.hashmap);
22+
max_entries = bpf_map__max_entries(skel->maps.hashmap);
23+
for (i = 0; i < max_entries; i++) {
24+
key = i;
25+
val = i + 1;
26+
err = bpf_map_update_elem(hashmap_fd, &key, &val, BPF_ANY);
27+
if (!ASSERT_OK(err, "map_update"))
28+
goto out;
29+
}
30+
31+
num_cpus = bpf_num_possible_cpus();
32+
percpu_map_fd = bpf_map__fd(skel->maps.percpu_map);
33+
percpu_valbuf = malloc(sizeof(__u64) * num_cpus);
34+
if (!ASSERT_OK_PTR(percpu_valbuf, "percpu_valbuf"))
35+
goto out;
36+
37+
key = 1;
38+
for (i = 0; i < num_cpus; i++)
39+
percpu_valbuf[i] = i + 1;
40+
err = bpf_map_update_elem(percpu_map_fd, &key, percpu_valbuf, BPF_ANY);
41+
if (!ASSERT_OK(err, "percpu_map_update"))
42+
goto out;
43+
44+
err = bpf_prog_test_run(bpf_program__fd(skel->progs.test_pkt_access),
45+
1, &pkt_v4, sizeof(pkt_v4), NULL, NULL,
46+
&retval, &duration);
47+
if (CHECK(err || retval, "ipv4", "err %d errno %d retval %d\n",
48+
err, errno, retval))
49+
goto out;
50+
51+
ASSERT_EQ(skel->bss->hashmap_output, 4, "hashmap_output");
52+
ASSERT_EQ(skel->bss->hashmap_elems, max_entries, "hashmap_elems");
53+
54+
key = 1;
55+
err = bpf_map_lookup_elem(hashmap_fd, &key, &val);
56+
ASSERT_ERR(err, "hashmap_lookup");
57+
58+
ASSERT_EQ(skel->bss->percpu_called, 1, "percpu_called");
59+
ASSERT_LT(skel->bss->cpu, num_cpus, "num_cpus");
60+
ASSERT_EQ(skel->bss->percpu_map_elems, 1, "percpu_map_elems");
61+
ASSERT_EQ(skel->bss->percpu_key, 1, "percpu_key");
62+
ASSERT_EQ(skel->bss->percpu_val, skel->bss->cpu + 1, "percpu_val");
63+
ASSERT_EQ(skel->bss->percpu_output, 100, "percpu_output");
64+
out:
65+
free(percpu_valbuf);
66+
for_each_hash_map_elem__destroy(skel);
67+
}
68+
69+
void test_for_each(void)
70+
{
71+
if (test__start_subtest("hash_map"))
72+
test_hash_map();
73+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2021 Facebook */
3+
#include "vmlinux.h"
4+
#include <bpf/bpf_helpers.h>
5+
6+
char _license[] SEC("license") = "GPL";
7+
8+
struct {
9+
__uint(type, BPF_MAP_TYPE_HASH);
10+
__uint(max_entries, 3);
11+
__type(key, __u32);
12+
__type(value, __u64);
13+
} hashmap SEC(".maps");
14+
15+
struct {
16+
__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
17+
__uint(max_entries, 1);
18+
__type(key, __u32);
19+
__type(value, __u64);
20+
} percpu_map SEC(".maps");
21+
22+
struct callback_ctx {
23+
struct __sk_buff *ctx;
24+
int input;
25+
int output;
26+
};
27+
28+
static __u64
29+
check_hash_elem(struct bpf_map *map, __u32 *key, __u64 *val,
30+
struct callback_ctx *data)
31+
{
32+
struct __sk_buff *skb = data->ctx;
33+
__u32 k;
34+
__u64 v;
35+
36+
if (skb) {
37+
k = *key;
38+
v = *val;
39+
if (skb->len == 10000 && k == 10 && v == 10)
40+
data->output = 3; /* impossible path */
41+
else
42+
data->output = 4;
43+
} else {
44+
data->output = data->input;
45+
bpf_map_delete_elem(map, key);
46+
}
47+
48+
return 0;
49+
}
50+
51+
__u32 cpu = 0;
52+
__u32 percpu_called = 0;
53+
__u32 percpu_key = 0;
54+
__u64 percpu_val = 0;
55+
int percpu_output = 0;
56+
57+
static __u64
58+
check_percpu_elem(struct bpf_map *map, __u32 *key, __u64 *val,
59+
struct callback_ctx *unused)
60+
{
61+
struct callback_ctx data;
62+
63+
percpu_called++;
64+
cpu = bpf_get_smp_processor_id();
65+
percpu_key = *key;
66+
percpu_val = *val;
67+
68+
data.ctx = 0;
69+
data.input = 100;
70+
data.output = 0;
71+
bpf_for_each_map_elem(&hashmap, check_hash_elem, &data, 0);
72+
percpu_output = data.output;
73+
74+
return 0;
75+
}
76+
77+
int hashmap_output = 0;
78+
int hashmap_elems = 0;
79+
int percpu_map_elems = 0;
80+
81+
SEC("classifier")
82+
int test_pkt_access(struct __sk_buff *skb)
83+
{
84+
struct callback_ctx data;
85+
86+
data.ctx = skb;
87+
data.input = 10;
88+
data.output = 0;
89+
hashmap_elems = bpf_for_each_map_elem(&hashmap, check_hash_elem, &data, 0);
90+
hashmap_output = data.output;
91+
92+
percpu_map_elems = bpf_for_each_map_elem(&percpu_map, check_percpu_elem,
93+
(void *)0, 0);
94+
return 0;
95+
}

tools/testing/selftests/bpf/test_progs.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ extern int test__join_cgroup(const char *path);
152152
___ok; \
153153
})
154154

155+
#define ASSERT_LT(actual, expected, name) ({ \
156+
static int duration = 0; \
157+
typeof(actual) ___act = (actual); \
158+
typeof(expected) ___exp = (expected); \
159+
bool ___ok = ___act < ___exp; \
160+
CHECK(!___ok, (name), \
161+
"unexpected %s: actual %lld >= expected %lld\n", \
162+
(name), (long long)(___act), (long long)(___exp)); \
163+
___ok; \
164+
})
165+
155166
#define ASSERT_STREQ(actual, expected, name) ({ \
156167
static int duration = 0; \
157168
const char *___act = actual; \

0 commit comments

Comments
 (0)