Skip to content

Commit

Permalink
selftests/bpf: Add mprog tests for BPF tc opts and links
Browse files Browse the repository at this point in the history
  ./test_progs -t tc_link
  [    1.409177] tsc: Refined TSC clocksource calibration: 3407.991 MHz
  [    1.411604] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x311fcb8dbdf, max_idle_ns: 440795301826 ns
  [    1.415577] clocksource: Switched to clocksource tsc
  [    1.430401] bpf_testmod: loading out-of-tree module taints kernel.
  [    1.432324] bpf_testmod: module verification failed: signature and/or required key missing - tainting kernel
  torvalds#220     tc_link_opts_after:OK
  torvalds#221     tc_link_opts_basic:OK
  torvalds#222     tc_link_opts_before:OK
  torvalds#223     tc_link_opts_both:OK
  torvalds#224     tc_link_opts_chain_classic:OK
  torvalds#225     tc_link_opts_first:OK
  torvalds#226     tc_link_opts_invalid:OK
  torvalds#227     tc_link_opts_last:OK
  torvalds#228     tc_link_opts_replace:OK
  torvalds#229     tc_link_opts_revision:OK
  Summary: 10/0 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Daniel Borkmann <[email protected]>
  • Loading branch information
borkmann committed Jun 2, 2023
1 parent 13ac788 commit 6eec20f
Show file tree
Hide file tree
Showing 4 changed files with 4,459 additions and 0 deletions.
72 changes: 72 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/tc_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Isovalent */
#ifndef TC_HELPERS
#define TC_HELPERS
#include <test_progs.h>

static inline __u32 id_from_prog_fd(int fd)
{
struct bpf_prog_info prog_info = {};
__u32 prog_info_len = sizeof(prog_info);
int err;

err = bpf_obj_get_info_by_fd(fd, &prog_info, &prog_info_len);
if (!ASSERT_OK(err, "id_from_prog_fd"))
return 0;

ASSERT_NEQ(prog_info.id, 0, "prog_info.id");
return prog_info.id;
}

static inline __u32 id_from_link_fd(int fd)
{
struct bpf_link_info link_info = {};
__u32 link_info_len = sizeof(link_info);
int err;

err = bpf_link_get_info_by_fd(fd, &link_info, &link_info_len);
if (!ASSERT_OK(err, "id_from_link_fd"))
return 0;

ASSERT_NEQ(link_info.id, 0, "link_info.id");
return link_info.id;
}

static inline __u32 ifindex_from_link_fd(int fd)
{
struct bpf_link_info link_info = {};
__u32 link_info_len = sizeof(link_info);
int err;

err = bpf_link_get_info_by_fd(fd, &link_info, &link_info_len);
if (!ASSERT_OK(err, "id_from_link_fd"))
return 0;

return link_info.tcx.ifindex;
}

static inline void __assert_mprog_count(int target, int expected, bool miniq, int ifindex)
{
__u32 count = 0, attach_flags = 0;
int err;

err = bpf_prog_query(ifindex, target, 0, &attach_flags,
NULL, &count);
ASSERT_EQ(count, expected, "count");
if (!expected && !miniq)
ASSERT_EQ(err, -ENOENT, "prog_query");
else
ASSERT_EQ(err, 0, "prog_query");
}

static inline void assert_mprog_count(int target, int expected)
{
__assert_mprog_count(target, expected, false, loopback);
}

static inline void assert_mprog_count_ifindex(int ifindex, int target, int expected)
{
__assert_mprog_count(target, expected, false, ifindex);
}

#endif /* TC_HELPERS */
Loading

0 comments on commit 6eec20f

Please sign in to comment.