brintos

brintos / linux-shallow public Read only

0
0
Text · 1.8 KiB · 924d0e2 Raw
79 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2/* Copyright (c) 2023 Isovalent */3#ifndef TC_HELPERS4#define TC_HELPERS5#include <test_progs.h>6 7#ifndef loopback8# define loopback 19#endif10 11static 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 25static 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 39static 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 52static 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 63static inline void assert_mprog_count(int target, int expected)64{65	__assert_mprog_count(target, expected, loopback);66}67 68static inline void assert_mprog_count_ifindex(int ifindex, int target, int expected)69{70	__assert_mprog_count(target, expected, ifindex);71}72 73static 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