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 #ifndef loopback
8 # define loopback 1
9 #endif
10 
ifindex_from_link_fd(int fd)11 static inline __u32 ifindex_from_link_fd(int fd)
12 {
13 	struct bpf_link_info link_info = {};
14 	__u32 link_info_len = sizeof(link_info);
15 	int err;
16 
17 	err = bpf_link_get_info_by_fd(fd, &link_info, &link_info_len);
18 	if (!ASSERT_OK(err, "id_from_link_fd"))
19 		return 0;
20 
21 	return link_info.tcx.ifindex;
22 }
23 
__assert_mprog_count(int target,int expected,int ifindex)24 static inline void __assert_mprog_count(int target, int expected, int ifindex)
25 {
26 	__u32 count = 0, attach_flags = 0;
27 	int err;
28 
29 	err = bpf_prog_query(ifindex, target, 0, &attach_flags,
30 			     NULL, &count);
31 	ASSERT_EQ(count, expected, "count");
32 	ASSERT_EQ(err, 0, "prog_query");
33 }
34 
assert_mprog_count(int target,int expected)35 static inline void assert_mprog_count(int target, int expected)
36 {
37 	__assert_mprog_count(target, expected, loopback);
38 }
39 
assert_mprog_count_ifindex(int ifindex,int target,int expected)40 static inline void assert_mprog_count_ifindex(int ifindex, int target, int expected)
41 {
42 	__assert_mprog_count(target, expected, ifindex);
43 }
44 
tc_skel_reset_all_seen(struct test_tc_link * skel)45 static inline void tc_skel_reset_all_seen(struct test_tc_link *skel)
46 {
47 	memset(skel->bss, 0, sizeof(*skel->bss));
48 }
49 
50 #endif /* TC_HELPERS */
51