1 // SPDX-License-Identifier: GPL-2.0
3 #include <test_progs.h>
4 #include <linux/pkt_cls.h>
6 #include "cap_helpers.h"
7 #include "test_tc_bpf.skel.h"
11 #define TEST_DECLARE_OPTS(__fd) \
12 DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts_h, .handle = 1); \
13 DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts_p, .priority = 1); \
14 DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts_f, .prog_fd = __fd); \
15 DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts_hp, .handle = 1, .priority = 1); \
16 DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts_hf, .handle = 1, .prog_fd = __fd); \
17 DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts_pf, .priority = 1, .prog_fd = __fd); \
18 DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts_hpf, .handle = 1, .priority = 1, .prog_fd = __fd); \
19 DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts_hpi, .handle = 1, .priority = 1, .prog_id = 42); \
20 DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts_hpr, .handle = 1, .priority = 1, \
21 .flags = BPF_TC_F_REPLACE); \
22 DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts_hpfi, .handle = 1, .priority = 1, .prog_fd = __fd, \
24 DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts_prio_max, .handle = 1, .priority = UINT16_MAX + 1);
26 static int test_tc_bpf_basic(const struct bpf_tc_hook *hook, int fd)
28 DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 1, .prog_fd = fd);
29 struct bpf_prog_info info = {};
30 __u32 info_len = sizeof(info);
33 ret = bpf_prog_get_info_by_fd(fd, &info, &info_len);
34 if (!ASSERT_OK(ret, "bpf_prog_get_info_by_fd"))
37 ret = bpf_tc_attach(hook, &opts);
38 if (!ASSERT_OK(ret, "bpf_tc_attach"))
41 if (!ASSERT_EQ(opts.handle, 1, "handle set") ||
42 !ASSERT_EQ(opts.priority, 1, "priority set") ||
43 !ASSERT_EQ(opts.prog_id, info.id, "prog_id set"))
47 opts.flags = BPF_TC_F_REPLACE;
48 ret = bpf_tc_attach(hook, &opts);
49 if (!ASSERT_OK(ret, "bpf_tc_attach replace mode"))
52 opts.flags = opts.prog_fd = opts.prog_id = 0;
53 ret = bpf_tc_query(hook, &opts);
54 if (!ASSERT_OK(ret, "bpf_tc_query"))
57 if (!ASSERT_EQ(opts.handle, 1, "handle set") ||
58 !ASSERT_EQ(opts.priority, 1, "priority set") ||
59 !ASSERT_EQ(opts.prog_id, info.id, "prog_id set"))
63 opts.flags = opts.prog_fd = opts.prog_id = 0;
64 ret = bpf_tc_detach(hook, &opts);
65 ASSERT_OK(ret, "bpf_tc_detach");
69 static int test_tc_bpf_api(struct bpf_tc_hook *hook, int fd)
71 DECLARE_LIBBPF_OPTS(bpf_tc_opts, attach_opts, .handle = 1, .priority = 1, .prog_fd = fd);
72 DECLARE_LIBBPF_OPTS(bpf_tc_hook, inv_hook, .attach_point = BPF_TC_INGRESS);
73 DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 1);
76 ret = bpf_tc_hook_create(NULL);
77 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_hook_create invalid hook = NULL"))
80 /* hook ifindex = 0 */
81 ret = bpf_tc_hook_create(&inv_hook);
82 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_hook_create invalid hook ifindex == 0"))
85 ret = bpf_tc_hook_destroy(&inv_hook);
86 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_hook_destroy invalid hook ifindex == 0"))
89 ret = bpf_tc_attach(&inv_hook, &attach_opts);
90 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_attach invalid hook ifindex == 0"))
92 attach_opts.prog_id = 0;
94 ret = bpf_tc_detach(&inv_hook, &opts);
95 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid hook ifindex == 0"))
98 ret = bpf_tc_query(&inv_hook, &opts);
99 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid hook ifindex == 0"))
102 /* hook ifindex < 0 */
103 inv_hook.ifindex = -1;
105 ret = bpf_tc_hook_create(&inv_hook);
106 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_hook_create invalid hook ifindex < 0"))
109 ret = bpf_tc_hook_destroy(&inv_hook);
110 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_hook_destroy invalid hook ifindex < 0"))
113 ret = bpf_tc_attach(&inv_hook, &attach_opts);
114 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_attach invalid hook ifindex < 0"))
116 attach_opts.prog_id = 0;
118 ret = bpf_tc_detach(&inv_hook, &opts);
119 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid hook ifindex < 0"))
122 ret = bpf_tc_query(&inv_hook, &opts);
123 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid hook ifindex < 0"))
126 inv_hook.ifindex = LO_IFINDEX;
128 /* hook.attach_point invalid */
129 inv_hook.attach_point = 0xabcd;
130 ret = bpf_tc_hook_create(&inv_hook);
131 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_hook_create invalid hook.attach_point"))
134 ret = bpf_tc_hook_destroy(&inv_hook);
135 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_hook_destroy invalid hook.attach_point"))
138 ret = bpf_tc_attach(&inv_hook, &attach_opts);
139 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_attach invalid hook.attach_point"))
142 ret = bpf_tc_detach(&inv_hook, &opts);
143 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid hook.attach_point"))
146 ret = bpf_tc_query(&inv_hook, &opts);
147 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid hook.attach_point"))
150 inv_hook.attach_point = BPF_TC_INGRESS;
152 /* hook.attach_point valid, but parent invalid */
153 inv_hook.parent = TC_H_MAKE(1UL << 16, 10);
154 ret = bpf_tc_hook_create(&inv_hook);
155 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_hook_create invalid hook parent"))
158 ret = bpf_tc_hook_destroy(&inv_hook);
159 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_hook_destroy invalid hook parent"))
162 ret = bpf_tc_attach(&inv_hook, &attach_opts);
163 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_attach invalid hook parent"))
166 ret = bpf_tc_detach(&inv_hook, &opts);
167 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid hook parent"))
170 ret = bpf_tc_query(&inv_hook, &opts);
171 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid hook parent"))
174 inv_hook.attach_point = BPF_TC_CUSTOM;
176 /* These return EOPNOTSUPP instead of EINVAL as parent is checked after
177 * attach_point of the hook.
179 ret = bpf_tc_hook_create(&inv_hook);
180 if (!ASSERT_EQ(ret, -EOPNOTSUPP, "bpf_tc_hook_create invalid hook parent"))
183 ret = bpf_tc_hook_destroy(&inv_hook);
184 if (!ASSERT_EQ(ret, -EOPNOTSUPP, "bpf_tc_hook_destroy invalid hook parent"))
187 ret = bpf_tc_attach(&inv_hook, &attach_opts);
188 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_attach invalid hook parent"))
191 ret = bpf_tc_detach(&inv_hook, &opts);
192 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid hook parent"))
195 ret = bpf_tc_query(&inv_hook, &opts);
196 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid hook parent"))
199 inv_hook.attach_point = BPF_TC_INGRESS;
203 TEST_DECLARE_OPTS(fd);
205 ret = bpf_tc_detach(NULL, &opts_hp);
206 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid hook = NULL"))
209 ret = bpf_tc_detach(hook, NULL);
210 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid opts = NULL"))
213 ret = bpf_tc_detach(hook, &opts_hpr);
214 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid flags set"))
217 ret = bpf_tc_detach(hook, &opts_hpf);
218 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid prog_fd set"))
221 ret = bpf_tc_detach(hook, &opts_hpi);
222 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid prog_id set"))
225 ret = bpf_tc_detach(hook, &opts_p);
226 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid handle unset"))
229 ret = bpf_tc_detach(hook, &opts_h);
230 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid priority unset"))
233 ret = bpf_tc_detach(hook, &opts_prio_max);
234 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid priority > UINT16_MAX"))
240 TEST_DECLARE_OPTS(fd);
242 ret = bpf_tc_query(NULL, &opts);
243 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid hook = NULL"))
246 ret = bpf_tc_query(hook, NULL);
247 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid opts = NULL"))
250 ret = bpf_tc_query(hook, &opts_hpr);
251 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid flags set"))
254 ret = bpf_tc_query(hook, &opts_hpf);
255 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid prog_fd set"))
258 ret = bpf_tc_query(hook, &opts_hpi);
259 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid prog_id set"))
262 ret = bpf_tc_query(hook, &opts_p);
263 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid handle unset"))
266 ret = bpf_tc_query(hook, &opts_h);
267 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid priority unset"))
270 ret = bpf_tc_query(hook, &opts_prio_max);
271 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid priority > UINT16_MAX"))
274 /* when chain is not present, kernel returns -EINVAL */
275 ret = bpf_tc_query(hook, &opts_hp);
276 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query valid handle, priority set"))
282 TEST_DECLARE_OPTS(fd);
284 ret = bpf_tc_attach(NULL, &opts_hp);
285 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_attach invalid hook = NULL"))
288 ret = bpf_tc_attach(hook, NULL);
289 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_attach invalid opts = NULL"))
293 ret = bpf_tc_attach(hook, &opts_hp);
294 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_attach invalid flags"))
297 ret = bpf_tc_attach(hook, NULL);
298 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_attach invalid prog_fd unset"))
301 ret = bpf_tc_attach(hook, &opts_hpi);
302 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_attach invalid prog_id set"))
305 ret = bpf_tc_attach(hook, &opts_pf);
306 if (!ASSERT_OK(ret, "bpf_tc_attach valid handle unset"))
308 opts_pf.prog_fd = opts_pf.prog_id = 0;
309 ASSERT_OK(bpf_tc_detach(hook, &opts_pf), "bpf_tc_detach");
311 ret = bpf_tc_attach(hook, &opts_hf);
312 if (!ASSERT_OK(ret, "bpf_tc_attach valid priority unset"))
314 opts_hf.prog_fd = opts_hf.prog_id = 0;
315 ASSERT_OK(bpf_tc_detach(hook, &opts_hf), "bpf_tc_detach");
317 ret = bpf_tc_attach(hook, &opts_prio_max);
318 if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_attach invalid priority > UINT16_MAX"))
321 ret = bpf_tc_attach(hook, &opts_f);
322 if (!ASSERT_OK(ret, "bpf_tc_attach valid both handle and priority unset"))
324 opts_f.prog_fd = opts_f.prog_id = 0;
325 ASSERT_OK(bpf_tc_detach(hook, &opts_f), "bpf_tc_detach");
331 void tc_bpf_root(void)
333 DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = LO_IFINDEX,
334 .attach_point = BPF_TC_INGRESS);
335 struct test_tc_bpf *skel = NULL;
336 bool hook_created = false;
339 skel = test_tc_bpf__open_and_load();
340 if (!ASSERT_OK_PTR(skel, "test_tc_bpf__open_and_load"))
343 cls_fd = bpf_program__fd(skel->progs.cls);
345 ret = bpf_tc_hook_create(&hook);
349 ret = ret == -EEXIST ? 0 : ret;
350 if (!ASSERT_OK(ret, "bpf_tc_hook_create(BPF_TC_INGRESS)"))
353 hook.attach_point = BPF_TC_CUSTOM;
354 hook.parent = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS);
355 ret = bpf_tc_hook_create(&hook);
356 if (!ASSERT_EQ(ret, -EOPNOTSUPP, "bpf_tc_hook_create invalid hook.attach_point"))
359 ret = test_tc_bpf_basic(&hook, cls_fd);
360 if (!ASSERT_OK(ret, "test_tc_internal ingress"))
363 ret = bpf_tc_hook_destroy(&hook);
364 if (!ASSERT_EQ(ret, -EOPNOTSUPP, "bpf_tc_hook_destroy invalid hook.attach_point"))
367 hook.attach_point = BPF_TC_INGRESS;
369 bpf_tc_hook_destroy(&hook);
371 ret = test_tc_bpf_basic(&hook, cls_fd);
372 if (!ASSERT_OK(ret, "test_tc_internal ingress"))
375 bpf_tc_hook_destroy(&hook);
377 hook.attach_point = BPF_TC_EGRESS;
378 ret = test_tc_bpf_basic(&hook, cls_fd);
379 if (!ASSERT_OK(ret, "test_tc_internal egress"))
382 bpf_tc_hook_destroy(&hook);
384 ret = test_tc_bpf_api(&hook, cls_fd);
385 if (!ASSERT_OK(ret, "test_tc_bpf_api"))
388 bpf_tc_hook_destroy(&hook);
392 hook.attach_point = BPF_TC_INGRESS | BPF_TC_EGRESS;
393 bpf_tc_hook_destroy(&hook);
395 test_tc_bpf__destroy(skel);
398 void tc_bpf_non_root(void)
400 struct test_tc_bpf *skel = NULL;
404 /* In case CAP_BPF and CAP_PERFMON is not set */
405 ret = cap_enable_effective(1ULL << CAP_BPF | 1ULL << CAP_NET_ADMIN, &caps);
406 if (!ASSERT_OK(ret, "set_cap_bpf_cap_net_admin"))
408 ret = cap_disable_effective(1ULL << CAP_SYS_ADMIN | 1ULL << CAP_PERFMON, NULL);
409 if (!ASSERT_OK(ret, "disable_cap_sys_admin"))
412 skel = test_tc_bpf__open_and_load();
413 if (!ASSERT_OK_PTR(skel, "test_tc_bpf__open_and_load"))
416 test_tc_bpf__destroy(skel);
420 cap_enable_effective(caps, NULL);
423 void test_tc_bpf(void)
425 if (test__start_subtest("tc_bpf_root"))
427 if (test__start_subtest("tc_bpf_non_root"))