|
| 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