Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: Add mprog API tests for BPF tcx opts
Browse files Browse the repository at this point in the history
Add a big batch of test coverage to assert all aspects of the tcx opts
attach, detach and query API:

  # ./vmtest.sh -- ./test_progs -t tc_opts
  [...]
  torvalds#238     tc_opts_after:OK
  torvalds#239     tc_opts_append:OK
  torvalds#240     tc_opts_basic:OK
  torvalds#241     tc_opts_before:OK
  torvalds#242     tc_opts_chain_classic:OK
  torvalds#243     tc_opts_demixed:OK
  torvalds#244     tc_opts_detach:OK
  torvalds#245     tc_opts_detach_after:OK
  torvalds#246     tc_opts_detach_before:OK
  torvalds#247     tc_opts_dev_cleanup:OK
  torvalds#248     tc_opts_invalid:OK
  torvalds#249     tc_opts_mixed:OK
  torvalds#250     tc_opts_prepend:OK
  torvalds#251     tc_opts_replace:OK
  torvalds#252     tc_opts_revision:OK
  Summary: 15/0 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Daniel Borkmann <[email protected]>
borkmann committed Jul 11, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent ffe58ec commit 540d2fa
Showing 3 changed files with 2,351 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 */
2,239 changes: 2,239 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/tc_opts.c

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions tools/testing/selftests/bpf/progs/test_tc_link.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Isovalent */
#include <stdbool.h>
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

char LICENSE[] SEC("license") = "GPL";

bool seen_tc1;
bool seen_tc2;
bool seen_tc3;
bool seen_tc4;

SEC("tc/ingress")
int tc1(struct __sk_buff *skb)
{
seen_tc1 = true;
return TCX_NEXT;
}

SEC("tc/egress")
int tc2(struct __sk_buff *skb)
{
seen_tc2 = true;
return TCX_NEXT;
}

SEC("tc/egress")
int tc3(struct __sk_buff *skb)
{
seen_tc3 = true;
return TCX_NEXT;
}

SEC("tc/egress")
int tc4(struct __sk_buff *skb)
{
seen_tc4 = true;
return TCX_NEXT;
}

0 comments on commit 540d2fa

Please sign in to comment.