Skip to content

Commit 7a67087

Browse files
jkoong-fbAlexei Starovoitov
authored andcommitted
selftests/bpf: Add bloom map success test for userspace calls
This patch has two changes: 1) Adds a new function "test_success_cases" to test successfully creating + adding + looking up a value in a bloom filter map from the userspace side. 2) Use bpf_create_map instead of bpf_create_map_xattr in the "test_fail_cases" and test_inner_map to make the code look cleaner. Signed-off-by: Joanne Koong <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 8845b46 commit 7a67087

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

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

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,31 @@
77

88
static void test_fail_cases(void)
99
{
10-
struct bpf_create_map_attr xattr = {
11-
.name = "bloom_filter_map",
12-
.map_type = BPF_MAP_TYPE_BLOOM_FILTER,
13-
.max_entries = 100,
14-
.value_size = 11,
15-
};
1610
__u32 value;
1711
int fd, err;
1812

1913
/* Invalid key size */
20-
xattr.key_size = 4;
21-
fd = bpf_create_map_xattr(&xattr);
14+
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 4, sizeof(value), 100, 0);
2215
if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid key size"))
2316
close(fd);
24-
xattr.key_size = 0;
2517

2618
/* Invalid value size */
27-
xattr.value_size = 0;
28-
fd = bpf_create_map_xattr(&xattr);
19+
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, 0, 100, 0);
2920
if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid value size 0"))
3021
close(fd);
31-
xattr.value_size = 11;
3222

3323
/* Invalid max entries size */
34-
xattr.max_entries = 0;
35-
fd = bpf_create_map_xattr(&xattr);
24+
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 0, 0);
3625
if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid max entries size"))
3726
close(fd);
38-
xattr.max_entries = 100;
3927

4028
/* Bloom filter maps do not support BPF_F_NO_PREALLOC */
41-
xattr.map_flags = BPF_F_NO_PREALLOC;
42-
fd = bpf_create_map_xattr(&xattr);
29+
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 100,
30+
BPF_F_NO_PREALLOC);
4331
if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid flags"))
4432
close(fd);
45-
xattr.map_flags = 0;
4633

47-
fd = bpf_create_map_xattr(&xattr);
34+
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 100, 0);
4835
if (!ASSERT_GE(fd, 0, "bpf_create_map bloom filter"))
4936
return;
5037

@@ -67,6 +54,30 @@ static void test_fail_cases(void)
6754
close(fd);
6855
}
6956

57+
static void test_success_cases(void)
58+
{
59+
char value[11];
60+
int fd, err;
61+
62+
/* Create a map */
63+
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 100,
64+
BPF_F_ZERO_SEED | BPF_F_NUMA_NODE);
65+
if (!ASSERT_GE(fd, 0, "bpf_create_map bloom filter success case"))
66+
return;
67+
68+
/* Add a value to the bloom filter */
69+
err = bpf_map_update_elem(fd, NULL, &value, 0);
70+
if (!ASSERT_OK(err, "bpf_map_update_elem bloom filter success case"))
71+
goto done;
72+
73+
/* Lookup a value in the bloom filter */
74+
err = bpf_map_lookup_elem(fd, NULL, &value);
75+
ASSERT_OK(err, "bpf_map_update_elem bloom filter success case");
76+
77+
done:
78+
close(fd);
79+
}
80+
7081
static void check_bloom(struct bloom_filter_map *skel)
7182
{
7283
struct bpf_link *link;
@@ -86,16 +97,11 @@ static void test_inner_map(struct bloom_filter_map *skel, const __u32 *rand_vals
8697
__u32 nr_rand_vals)
8798
{
8899
int outer_map_fd, inner_map_fd, err, i, key = 0;
89-
struct bpf_create_map_attr xattr = {
90-
.name = "bloom_filter_inner_map",
91-
.map_type = BPF_MAP_TYPE_BLOOM_FILTER,
92-
.value_size = sizeof(__u32),
93-
.max_entries = nr_rand_vals,
94-
};
95100
struct bpf_link *link;
96101

97102
/* Create a bloom filter map that will be used as the inner map */
98-
inner_map_fd = bpf_create_map_xattr(&xattr);
103+
inner_map_fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(*rand_vals),
104+
nr_rand_vals, 0);
99105
if (!ASSERT_GE(inner_map_fd, 0, "bpf_create_map bloom filter inner map"))
100106
return;
101107

@@ -190,6 +196,7 @@ void test_bloom_filter_map(void)
190196
int err;
191197

192198
test_fail_cases();
199+
test_success_cases();
193200

194201
err = setup_progs(&skel, &rand_vals, &nr_rand_vals);
195202
if (err)

0 commit comments

Comments
 (0)