Skip to content

Commit 85f7df4

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

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 (CHECK(!skel, "for_each_hash_map_elem__open_and_load",
19+
"skeleton open_and_load failed\n"))
20+
return;
21+
22+
hashmap_fd = bpf_map__fd(skel->maps.hashmap);
23+
max_entries = bpf_map__max_entries(skel->maps.hashmap);
24+
for (i = 0; i < max_entries; i++) {
25+
key = i;
26+
val = i + 1;
27+
err = bpf_map_update_elem(hashmap_fd, &key, &val, BPF_ANY);
28+
if (CHECK(err, "map_update", "map_update failed\n"))
29+
goto out;
30+
}
31+
32+
num_cpus = bpf_num_possible_cpus();
33+
percpu_map_fd = bpf_map__fd(skel->maps.percpu_map);
34+
percpu_valbuf = malloc(sizeof(__u64) * num_cpus);
35+
if (!ASSERT_OK_PTR(percpu_valbuf, "percpu_valbuf"))
36+
goto out;
37+
38+
key = 1;
39+
for (i = 0; i < num_cpus; i++)
40+
percpu_valbuf[i] = i + 1;
41+
err = bpf_map_update_elem(percpu_map_fd, &key, percpu_valbuf, BPF_ANY);
42+
if (CHECK(err, "percpu_map_update", "map_update failed\n"))
43+
goto out;
44+
45+
err = bpf_prog_test_run(bpf_program__fd(skel->progs.test_pkt_access),
46+
1, &pkt_v4, sizeof(pkt_v4), NULL, NULL,
47+
&retval, &duration);
48+
if (CHECK(err || retval, "ipv4", "err %d errno %d retval %d\n",
49+
err, errno, retval))
50+
goto out;
51+
52+
ASSERT_EQ(skel->bss->hashmap_output, 4, "hashmap_output");
53+
ASSERT_EQ(skel->bss->hashmap_elems, max_entries, "hashmap_elems");
54+
55+
key = 1;
56+
err = bpf_map_lookup_elem(hashmap_fd, &key, &val);
57+
ASSERT_ERR(err, "hashmap_lookup");
58+
59+
ASSERT_EQ(skel->bss->percpu_called, 1, "percpu_called");
60+
ASSERT_EQ(skel->bss->cpu < num_cpus, 1, "num_cpus");
61+
ASSERT_EQ(skel->bss->percpu_map_elems, 1, "percpu_map_elems");
62+
ASSERT_EQ(skel->bss->percpu_key, 1, "percpu_key");
63+
ASSERT_EQ(skel->bss->percpu_val, skel->bss->cpu + 1, "percpu_val");
64+
ASSERT_EQ(skel->bss->percpu_output, 100, "percpu_output");
65+
out:
66+
free(percpu_valbuf);
67+
for_each_hash_map_elem__destroy(skel);
68+
}
69+
70+
void test_for_each(void)
71+
{
72+
if (test__start_subtest("hash_map"))
73+
test_hash_map();
74+
}
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+
}

0 commit comments

Comments
 (0)