1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
3 #include <test_progs.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include "rhash.skel.h"
7 #include "bpf_iter_bpf_rhash_map.skel.h"
8 #include <linux/bpf.h>
9 #include <linux/perf_event.h>
10 #include <sys/syscall.h>
11
rhash_run(const char * prog_name)12 static void rhash_run(const char *prog_name)
13 {
14 struct rhash *skel;
15 struct bpf_program *prog;
16 LIBBPF_OPTS(bpf_test_run_opts, opts);
17 int err;
18
19 skel = rhash__open();
20 if (!ASSERT_OK_PTR(skel, "rhash__open"))
21 return;
22
23 prog = bpf_object__find_program_by_name(skel->obj, prog_name);
24 if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
25 goto cleanup;
26 bpf_program__set_autoload(prog, true);
27
28 err = rhash__load(skel);
29 if (!ASSERT_OK(err, "skel_load"))
30 goto cleanup;
31
32 err = bpf_prog_test_run_opts(bpf_program__fd(prog), &opts);
33 if (!ASSERT_OK(err, "prog run"))
34 goto cleanup;
35
36 if (!ASSERT_OK(opts.retval, "prog retval"))
37 goto cleanup;
38
39 if (!ASSERT_OK(skel->bss->err, "bss->err"))
40 goto cleanup;
41
42 cleanup:
43 rhash__destroy(skel);
44 }
45
rhash_map_create(__u32 max_entries,__u64 map_extra)46 static int rhash_map_create(__u32 max_entries, __u64 map_extra)
47 {
48 LIBBPF_OPTS(bpf_map_create_opts, opts,
49 .map_flags = BPF_F_NO_PREALLOC,
50 .map_extra = map_extra);
51
52 return bpf_map_create(BPF_MAP_TYPE_RHASH, "rhash_extra",
53 sizeof(__u32), sizeof(__u64), max_entries, &opts);
54 }
55
rhash_map_extra_presize(void)56 static void rhash_map_extra_presize(void)
57 {
58 const __u32 max_entries = 1024;
59 const __u32 nelem_hint = 256;
60 struct bpf_map_info info = {};
61 __u32 info_len = sizeof(info);
62 __u64 val = 0;
63 __u32 key;
64 int fd, i;
65
66 fd = rhash_map_create(max_entries, nelem_hint);
67 if (!ASSERT_GE(fd, 0, "rhash_map_create presize"))
68 return;
69
70 if (!ASSERT_OK(bpf_map_get_info_by_fd(fd, &info, &info_len), "info"))
71 goto close;
72 ASSERT_EQ(info.map_extra, nelem_hint, "info.map_extra");
73
74 for (i = 0; i < (int)nelem_hint; i++) {
75 key = i;
76 if (!ASSERT_OK(bpf_map_update_elem(fd, &key, &val, BPF_NOEXIST),
77 "update"))
78 goto close;
79 }
80 close:
81 close(fd);
82 }
83
rhash_map_extra_too_big(void)84 static void rhash_map_extra_too_big(void)
85 {
86 int fd;
87
88 fd = rhash_map_create(1U << 20, 0x10000);
89 if (!ASSERT_LT(fd, 0, "rhash_map_create hint > U16_MAX"))
90 close(fd);
91 }
92
rhash_iter_test(void)93 static void rhash_iter_test(void)
94 {
95 DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
96 struct bpf_iter_bpf_rhash_map *skel;
97 int err, i, len, map_fd, iter_fd;
98 union bpf_iter_link_info linfo;
99 u32 expected_key_sum = 0, key;
100 struct bpf_link *link;
101 u64 val = 0;
102 char buf[64];
103
104 skel = bpf_iter_bpf_rhash_map__open();
105 if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_rhash_map__open"))
106 return;
107
108 err = bpf_iter_bpf_rhash_map__load(skel);
109 if (!ASSERT_OK(err, "bpf_iter_bpf_rhash_map__load"))
110 goto out;
111
112 map_fd = bpf_map__fd(skel->maps.rhashmap);
113
114 /* Populate map with test data */
115 for (i = 0; i < 64; i++) {
116 key = i + 1;
117 expected_key_sum += key;
118
119 err = bpf_map_update_elem(map_fd, &key, &val, BPF_NOEXIST);
120 if (!ASSERT_OK(err, "map_update"))
121 goto out;
122 }
123
124 memset(&linfo, 0, sizeof(linfo));
125 linfo.map.map_fd = map_fd;
126 opts.link_info = &linfo;
127 opts.link_info_len = sizeof(linfo);
128
129 link = bpf_program__attach_iter(skel->progs.dump_bpf_rhash_map, &opts);
130 if (!ASSERT_OK_PTR(link, "attach_iter"))
131 goto out;
132
133 iter_fd = bpf_iter_create(bpf_link__fd(link));
134 if (!ASSERT_GE(iter_fd, 0, "create_iter"))
135 goto free_link;
136
137 do {
138 len = read(iter_fd, buf, sizeof(buf));
139 } while (len > 0);
140
141 ASSERT_EQ(skel->bss->key_sum, expected_key_sum, "key_sum");
142 ASSERT_EQ(skel->bss->elem_count, 64, "elem_count");
143
144 close(iter_fd);
145
146 free_link:
147 bpf_link__destroy(link);
148 out:
149 bpf_iter_bpf_rhash_map__destroy(skel);
150 }
151
test_rhash(void)152 void test_rhash(void)
153 {
154 if (test__start_subtest("test_rhash_lookup_update"))
155 rhash_run("test_rhash_lookup_update");
156
157 if (test__start_subtest("test_rhash_update_delete"))
158 rhash_run("test_rhash_update_delete");
159
160 if (test__start_subtest("test_rhash_update_elements"))
161 rhash_run("test_rhash_update_elements");
162
163 if (test__start_subtest("test_rhash_update_exist"))
164 rhash_run("test_rhash_update_exist");
165
166 if (test__start_subtest("test_rhash_update_any"))
167 rhash_run("test_rhash_update_any");
168
169 if (test__start_subtest("test_rhash_noexist_duplicate"))
170 rhash_run("test_rhash_noexist_duplicate");
171
172 if (test__start_subtest("test_rhash_delete_nonexistent"))
173 rhash_run("test_rhash_delete_nonexistent");
174
175 if (test__start_subtest("test_rhash_kptr_update"))
176 rhash_run("test_rhash_kptr_update");
177
178 if (test__start_subtest("test_rhash_kptr_delete"))
179 rhash_run("test_rhash_kptr_delete");
180
181 if (test__start_subtest("test_rhash_map_extra_presize"))
182 rhash_map_extra_presize();
183
184 if (test__start_subtest("test_rhash_map_extra_too_big"))
185 rhash_map_extra_too_big();
186
187 if (test__start_subtest("test_rhash_iter"))
188 rhash_iter_test();
189 }
190