1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <test_progs.h>
4 
5 #include "atomics.lskel.h"
6 
7 static void test_add(struct atomics_lskel *skel)
8 {
9 	int err, prog_fd;
10 	int link_fd;
11 	LIBBPF_OPTS(bpf_test_run_opts, topts);
12 
13 	link_fd = atomics_lskel__add__attach(skel);
14 	if (!ASSERT_GT(link_fd, 0, "attach(add)"))
15 		return;
16 
17 	prog_fd = skel->progs.add.prog_fd;
18 	err = bpf_prog_test_run_opts(prog_fd, &topts);
19 	if (!ASSERT_OK(err, "test_run_opts err"))
20 		goto cleanup;
21 	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
22 		goto cleanup;
23 
24 	ASSERT_EQ(skel->data->add64_value, 3, "add64_value");
25 	ASSERT_EQ(skel->bss->add64_result, 1, "add64_result");
26 
27 	ASSERT_EQ(skel->data->add32_value, 3, "add32_value");
28 	ASSERT_EQ(skel->bss->add32_result, 1, "add32_result");
29 
30 	ASSERT_EQ(skel->bss->add_stack_value_copy, 3, "add_stack_value");
31 	ASSERT_EQ(skel->bss->add_stack_result, 1, "add_stack_result");
32 
33 	ASSERT_EQ(skel->data->add_noreturn_value, 3, "add_noreturn_value");
34 
35 cleanup:
36 	close(link_fd);
37 }
38 
39 static void test_sub(struct atomics_lskel *skel)
40 {
41 	int err, prog_fd;
42 	int link_fd;
43 	LIBBPF_OPTS(bpf_test_run_opts, topts);
44 
45 	link_fd = atomics_lskel__sub__attach(skel);
46 	if (!ASSERT_GT(link_fd, 0, "attach(sub)"))
47 		return;
48 
49 	prog_fd = skel->progs.sub.prog_fd;
50 	err = bpf_prog_test_run_opts(prog_fd, &topts);
51 	if (!ASSERT_OK(err, "test_run_opts err"))
52 		goto cleanup;
53 	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
54 		goto cleanup;
55 
56 	ASSERT_EQ(skel->data->sub64_value, -1, "sub64_value");
57 	ASSERT_EQ(skel->bss->sub64_result, 1, "sub64_result");
58 
59 	ASSERT_EQ(skel->data->sub32_value, -1, "sub32_value");
60 	ASSERT_EQ(skel->bss->sub32_result, 1, "sub32_result");
61 
62 	ASSERT_EQ(skel->bss->sub_stack_value_copy, -1, "sub_stack_value");
63 	ASSERT_EQ(skel->bss->sub_stack_result, 1, "sub_stack_result");
64 
65 	ASSERT_EQ(skel->data->sub_noreturn_value, -1, "sub_noreturn_value");
66 
67 cleanup:
68 	close(link_fd);
69 }
70 
71 static void test_and(struct atomics_lskel *skel)
72 {
73 	int err, prog_fd;
74 	int link_fd;
75 	LIBBPF_OPTS(bpf_test_run_opts, topts);
76 
77 	link_fd = atomics_lskel__and__attach(skel);
78 	if (!ASSERT_GT(link_fd, 0, "attach(and)"))
79 		return;
80 
81 	prog_fd = skel->progs.and.prog_fd;
82 	err = bpf_prog_test_run_opts(prog_fd, &topts);
83 	if (!ASSERT_OK(err, "test_run_opts err"))
84 		goto cleanup;
85 	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
86 		goto cleanup;
87 
88 	ASSERT_EQ(skel->data->and64_value, 0x010ull << 32, "and64_value");
89 	ASSERT_EQ(skel->bss->and64_result, 0x110ull << 32, "and64_result");
90 
91 	ASSERT_EQ(skel->data->and32_value, 0x010, "and32_value");
92 	ASSERT_EQ(skel->bss->and32_result, 0x110, "and32_result");
93 
94 	ASSERT_EQ(skel->data->and_noreturn_value, 0x010ull << 32, "and_noreturn_value");
95 cleanup:
96 	close(link_fd);
97 }
98 
99 static void test_or(struct atomics_lskel *skel)
100 {
101 	int err, prog_fd;
102 	int link_fd;
103 	LIBBPF_OPTS(bpf_test_run_opts, topts);
104 
105 	link_fd = atomics_lskel__or__attach(skel);
106 	if (!ASSERT_GT(link_fd, 0, "attach(or)"))
107 		return;
108 
109 	prog_fd = skel->progs.or.prog_fd;
110 	err = bpf_prog_test_run_opts(prog_fd, &topts);
111 	if (!ASSERT_OK(err, "test_run_opts err"))
112 		goto cleanup;
113 	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
114 		goto cleanup;
115 
116 	ASSERT_EQ(skel->data->or64_value, 0x111ull << 32, "or64_value");
117 	ASSERT_EQ(skel->bss->or64_result, 0x110ull << 32, "or64_result");
118 
119 	ASSERT_EQ(skel->data->or32_value, 0x111, "or32_value");
120 	ASSERT_EQ(skel->bss->or32_result, 0x110, "or32_result");
121 
122 	ASSERT_EQ(skel->data->or_noreturn_value, 0x111ull << 32, "or_noreturn_value");
123 cleanup:
124 	close(link_fd);
125 }
126 
127 static void test_xor(struct atomics_lskel *skel)
128 {
129 	int err, prog_fd;
130 	int link_fd;
131 	LIBBPF_OPTS(bpf_test_run_opts, topts);
132 
133 	link_fd = atomics_lskel__xor__attach(skel);
134 	if (!ASSERT_GT(link_fd, 0, "attach(xor)"))
135 		return;
136 
137 	prog_fd = skel->progs.xor.prog_fd;
138 	err = bpf_prog_test_run_opts(prog_fd, &topts);
139 	if (!ASSERT_OK(err, "test_run_opts err"))
140 		goto cleanup;
141 	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
142 		goto cleanup;
143 
144 	ASSERT_EQ(skel->data->xor64_value, 0x101ull << 32, "xor64_value");
145 	ASSERT_EQ(skel->bss->xor64_result, 0x110ull << 32, "xor64_result");
146 
147 	ASSERT_EQ(skel->data->xor32_value, 0x101, "xor32_value");
148 	ASSERT_EQ(skel->bss->xor32_result, 0x110, "xor32_result");
149 
150 	ASSERT_EQ(skel->data->xor_noreturn_value, 0x101ull << 32, "xor_nxoreturn_value");
151 cleanup:
152 	close(link_fd);
153 }
154 
155 static void test_cmpxchg(struct atomics_lskel *skel)
156 {
157 	int err, prog_fd;
158 	int link_fd;
159 	LIBBPF_OPTS(bpf_test_run_opts, topts);
160 
161 	link_fd = atomics_lskel__cmpxchg__attach(skel);
162 	if (!ASSERT_GT(link_fd, 0, "attach(cmpxchg)"))
163 		return;
164 
165 	prog_fd = skel->progs.cmpxchg.prog_fd;
166 	err = bpf_prog_test_run_opts(prog_fd, &topts);
167 	if (!ASSERT_OK(err, "test_run_opts err"))
168 		goto cleanup;
169 	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
170 		goto cleanup;
171 
172 	ASSERT_EQ(skel->data->cmpxchg64_value, 2, "cmpxchg64_value");
173 	ASSERT_EQ(skel->bss->cmpxchg64_result_fail, 1, "cmpxchg_result_fail");
174 	ASSERT_EQ(skel->bss->cmpxchg64_result_succeed, 1, "cmpxchg_result_succeed");
175 
176 	ASSERT_EQ(skel->data->cmpxchg32_value, 2, "lcmpxchg32_value");
177 	ASSERT_EQ(skel->bss->cmpxchg32_result_fail, 1, "cmpxchg_result_fail");
178 	ASSERT_EQ(skel->bss->cmpxchg32_result_succeed, 1, "cmpxchg_result_succeed");
179 
180 cleanup:
181 	close(link_fd);
182 }
183 
184 static void test_xchg(struct atomics_lskel *skel)
185 {
186 	int err, prog_fd;
187 	int link_fd;
188 	LIBBPF_OPTS(bpf_test_run_opts, topts);
189 
190 	link_fd = atomics_lskel__xchg__attach(skel);
191 	if (!ASSERT_GT(link_fd, 0, "attach(xchg)"))
192 		return;
193 
194 	prog_fd = skel->progs.xchg.prog_fd;
195 	err = bpf_prog_test_run_opts(prog_fd, &topts);
196 	if (!ASSERT_OK(err, "test_run_opts err"))
197 		goto cleanup;
198 	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
199 		goto cleanup;
200 
201 	ASSERT_EQ(skel->data->xchg64_value, 2, "xchg64_value");
202 	ASSERT_EQ(skel->bss->xchg64_result, 1, "xchg64_result");
203 
204 	ASSERT_EQ(skel->data->xchg32_value, 2, "xchg32_value");
205 	ASSERT_EQ(skel->bss->xchg32_result, 1, "xchg32_result");
206 
207 cleanup:
208 	close(link_fd);
209 }
210 
211 void test_atomics(void)
212 {
213 	struct atomics_lskel *skel;
214 	__u32 duration = 0;
215 
216 	skel = atomics_lskel__open_and_load();
217 	if (CHECK(!skel, "skel_load", "atomics skeleton failed\n"))
218 		return;
219 
220 	if (skel->data->skip_tests) {
221 		printf("%s:SKIP:no ENABLE_ATOMICS_TESTS (missing Clang BPF atomics support)",
222 		       __func__);
223 		test__skip();
224 		goto cleanup;
225 	}
226 	skel->bss->pid = getpid();
227 
228 	if (test__start_subtest("add"))
229 		test_add(skel);
230 	if (test__start_subtest("sub"))
231 		test_sub(skel);
232 	if (test__start_subtest("and"))
233 		test_and(skel);
234 	if (test__start_subtest("or"))
235 		test_or(skel);
236 	if (test__start_subtest("xor"))
237 		test_xor(skel);
238 	if (test__start_subtest("cmpxchg"))
239 		test_cmpxchg(skel);
240 	if (test__start_subtest("xchg"))
241 		test_xchg(skel);
242 
243 cleanup:
244 	atomics_lskel__destroy(skel);
245 }
246