Skip to content

Commit 197b593

Browse files
borkmannKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
selftests/bpf: Add mprog API tests for BPF tcx opts
Add a big batch of test coverage to assert all aspects of the tcx opts attach, detach and query API: # ./vmtest.sh -- ./test_progs -t tc_opts [...] #237 tc_opts_after:OK #238 tc_opts_append:OK #239 tc_opts_basic:OK #240 tc_opts_before:OK #241 tc_opts_both:OK #242 tc_opts_chain_classic:OK #243 tc_opts_demixed:OK #244 tc_opts_detach:OK #245 tc_opts_detach_after:OK #246 tc_opts_detach_before:OK #247 tc_opts_dev_cleanup:OK #248 tc_opts_first:OK #249 tc_opts_invalid:OK #250 tc_opts_last:OK #251 tc_opts_mixed:OK #252 tc_opts_prepend:OK #253 tc_opts_replace:OK #254 tc_opts_revision:OK Summary: 18/0 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 8812be6 commit 197b593

File tree

3 files changed

+2810
-0
lines changed

3 files changed

+2810
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/* Copyright (c) 2023 Isovalent */
3+
#ifndef TC_HELPERS
4+
#define TC_HELPERS
5+
#include <test_progs.h>
6+
7+
static inline __u32 id_from_prog_fd(int fd)
8+
{
9+
struct bpf_prog_info prog_info = {};
10+
__u32 prog_info_len = sizeof(prog_info);
11+
int err;
12+
13+
err = bpf_obj_get_info_by_fd(fd, &prog_info, &prog_info_len);
14+
if (!ASSERT_OK(err, "id_from_prog_fd"))
15+
return 0;
16+
17+
ASSERT_NEQ(prog_info.id, 0, "prog_info.id");
18+
return prog_info.id;
19+
}
20+
21+
static inline __u32 id_from_link_fd(int fd)
22+
{
23+
struct bpf_link_info link_info = {};
24+
__u32 link_info_len = sizeof(link_info);
25+
int err;
26+
27+
err = bpf_link_get_info_by_fd(fd, &link_info, &link_info_len);
28+
if (!ASSERT_OK(err, "id_from_link_fd"))
29+
return 0;
30+
31+
ASSERT_NEQ(link_info.id, 0, "link_info.id");
32+
return link_info.id;
33+
}
34+
35+
static inline __u32 ifindex_from_link_fd(int fd)
36+
{
37+
struct bpf_link_info link_info = {};
38+
__u32 link_info_len = sizeof(link_info);
39+
int err;
40+
41+
err = bpf_link_get_info_by_fd(fd, &link_info, &link_info_len);
42+
if (!ASSERT_OK(err, "id_from_link_fd"))
43+
return 0;
44+
45+
return link_info.tcx.ifindex;
46+
}
47+
48+
static inline void __assert_mprog_count(int target, int expected, bool miniq, int ifindex)
49+
{
50+
__u32 count = 0, attach_flags = 0;
51+
int err;
52+
53+
err = bpf_prog_query(ifindex, target, 0, &attach_flags,
54+
NULL, &count);
55+
ASSERT_EQ(count, expected, "count");
56+
if (!expected && !miniq)
57+
ASSERT_EQ(err, -ENOENT, "prog_query");
58+
else
59+
ASSERT_EQ(err, 0, "prog_query");
60+
}
61+
62+
static inline void assert_mprog_count(int target, int expected)
63+
{
64+
__assert_mprog_count(target, expected, false, loopback);
65+
}
66+
67+
static inline void assert_mprog_count_ifindex(int ifindex, int target, int expected)
68+
{
69+
__assert_mprog_count(target, expected, false, ifindex);
70+
}
71+
72+
#endif /* TC_HELPERS */

0 commit comments

Comments
 (0)