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 11 static inline __u32 id_from_prog_fd(int fd) 12 { 13 struct bpf_prog_info prog_info = {}; 14 __u32 prog_info_len = sizeof(prog_info); 15 int err; 16 17 err = bpf_obj_get_info_by_fd(fd, &prog_info, &prog_info_len); 18 if (!ASSERT_OK(err, "id_from_prog_fd")) 19 return 0; 20 21 ASSERT_NEQ(prog_info.id, 0, "prog_info.id"); 22 return prog_info.id; 23 } 24 25 static inline __u32 id_from_link_fd(int fd) 26 { 27 struct bpf_link_info link_info = {}; 28 __u32 link_info_len = sizeof(link_info); 29 int err; 30 31 err = bpf_link_get_info_by_fd(fd, &link_info, &link_info_len); 32 if (!ASSERT_OK(err, "id_from_link_fd")) 33 return 0; 34 35 ASSERT_NEQ(link_info.id, 0, "link_info.id"); 36 return link_info.id; 37 } 38 39 static inline __u32 ifindex_from_link_fd(int fd) 40 { 41 struct bpf_link_info link_info = {}; 42 __u32 link_info_len = sizeof(link_info); 43 int err; 44 45 err = bpf_link_get_info_by_fd(fd, &link_info, &link_info_len); 46 if (!ASSERT_OK(err, "id_from_link_fd")) 47 return 0; 48 49 return link_info.tcx.ifindex; 50 } 51 52 static inline void __assert_mprog_count(int target, int expected, int ifindex) 53 { 54 __u32 count = 0, attach_flags = 0; 55 int err; 56 57 err = bpf_prog_query(ifindex, target, 0, &attach_flags, 58 NULL, &count); 59 ASSERT_EQ(count, expected, "count"); 60 ASSERT_EQ(err, 0, "prog_query"); 61 } 62 63 static inline void assert_mprog_count(int target, int expected) 64 { 65 __assert_mprog_count(target, expected, loopback); 66 } 67 68 static inline void assert_mprog_count_ifindex(int ifindex, int target, int expected) 69 { 70 __assert_mprog_count(target, expected, ifindex); 71 } 72 73 static inline void tc_skel_reset_all_seen(struct test_tc_link *skel) 74 { 75 memset(skel->bss, 0, sizeof(*skel->bss)); 76 } 77 78 #endif /* TC_HELPERS */ 79