Skip to content

Commit

Permalink
selftests/bpf: Add
Browse files Browse the repository at this point in the history
  [...]
  torvalds#234     tc_links_after:OK
  torvalds#235     tc_links_append:OK
  torvalds#236     tc_links_basic:OK
  torvalds#237     tc_links_before:OK
  torvalds#238     tc_links_chain_classic:OK
  torvalds#239     tc_links_chain_mixed:OK
  torvalds#240     tc_links_dev_cleanup:OK
  torvalds#241     tc_links_ingress:OK
  torvalds#242     tc_links_invalid:OK
  torvalds#243     tc_links_prepend:OK
  torvalds#244     tc_links_replace:OK
  torvalds#245     tc_links_revision:OK
  torvalds#246     tc_opts_after:OK
  torvalds#247     tc_opts_append:OK
  torvalds#248     tc_opts_basic:OK
  torvalds#249     tc_opts_before:OK
  torvalds#250     tc_opts_chain_classic:OK
  torvalds#251     tc_opts_chain_mixed:OK
  torvalds#252     tc_opts_delete_empty:OK
  torvalds#253     tc_opts_demixed:OK
  torvalds#254     tc_opts_detach:OK
  torvalds#255     tc_opts_detach_after:OK
  torvalds#256     tc_opts_detach_before:OK
  torvalds#257     tc_opts_dev_cleanup:OK
  torvalds#258     tc_opts_invalid:OK
  torvalds#259     tc_opts_mixed:OK
  torvalds#260     tc_opts_prepend:OK
  torvalds#261     tc_opts_replace:OK
  torvalds#262     tc_opts_revision:OK
  [...]

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
  • Loading branch information
borkmann committed Aug 14, 2023
1 parent 05a3195 commit 05fe108
Show file tree
Hide file tree
Showing 3 changed files with 349 additions and 0 deletions.
223 changes: 223 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/tc_links.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Isovalent */
#include <uapi/linux/if_link.h>
#include <uapi/linux/pkt_sched.h>
#include <net/if.h>
#include <test_progs.h>

Expand Down Expand Up @@ -1581,3 +1582,225 @@ void serial_test_tc_links_dev_cleanup(void)
test_tc_links_dev_cleanup_target(BPF_TCX_INGRESS);
test_tc_links_dev_cleanup_target(BPF_TCX_EGRESS);
}

static void test_tc_chain_mixed(int target)
{
LIBBPF_OPTS(bpf_tc_opts, tc_opts, .handle = 1, .priority = 1);
LIBBPF_OPTS(bpf_tc_hook, tc_hook, .ifindex = loopback);
LIBBPF_OPTS(bpf_tcx_opts, optl);
struct test_tc_link *skel;
struct bpf_link *link;
__u32 pid1, pid2, pid3;
int err;

skel = test_tc_link__open();
if (!ASSERT_OK_PTR(skel, "skel_open"))
goto cleanup;

ASSERT_EQ(bpf_program__set_expected_attach_type(skel->progs.tc4, target),
0, "tc4_attach_type");
ASSERT_EQ(bpf_program__set_expected_attach_type(skel->progs.tc5, target),
0, "tc5_attach_type");
ASSERT_EQ(bpf_program__set_expected_attach_type(skel->progs.tc6, target),
0, "tc6_attach_type");

err = test_tc_link__load(skel);
if (!ASSERT_OK(err, "skel_load"))
goto cleanup;

pid1 = id_from_prog_fd(bpf_program__fd(skel->progs.tc4));
pid2 = id_from_prog_fd(bpf_program__fd(skel->progs.tc5));
pid3 = id_from_prog_fd(bpf_program__fd(skel->progs.tc6));

ASSERT_NEQ(pid1, pid2, "prog_ids_1_2");
ASSERT_NEQ(pid2, pid3, "prog_ids_2_3");

assert_mprog_count(target, 0);

tc_hook.attach_point = target == BPF_TCX_INGRESS ?
BPF_TC_INGRESS : BPF_TC_EGRESS;
err = bpf_tc_hook_create(&tc_hook);
err = err == -EEXIST ? 0 : err;
if (!ASSERT_OK(err, "bpf_tc_hook_create"))
goto cleanup;

tc_opts.prog_fd = bpf_program__fd(skel->progs.tc5);
err = bpf_tc_attach(&tc_hook, &tc_opts);
if (!ASSERT_OK(err, "bpf_tc_attach"))
goto cleanup;

link = bpf_program__attach_tcx(skel->progs.tc6, loopback, &optl);
if (!ASSERT_OK_PTR(link, "link_attach"))
goto cleanup;

skel->links.tc6 = link;

assert_mprog_count(target, 1);

ASSERT_OK(system(ping_cmd), ping_cmd);

ASSERT_EQ(skel->bss->seen_tc4, false, "seen_tc4");
ASSERT_EQ(skel->bss->seen_tc5, false, "seen_tc5");
ASSERT_EQ(skel->bss->seen_tc6, true, "seen_tc6");

skel->bss->seen_tc4 = false;
skel->bss->seen_tc5 = false;
skel->bss->seen_tc6 = false;

err = bpf_link__update_program(skel->links.tc6, skel->progs.tc4);
if (!ASSERT_OK(err, "link_update"))
goto cleanup;

assert_mprog_count(target, 1);

ASSERT_OK(system(ping_cmd), ping_cmd);

ASSERT_EQ(skel->bss->seen_tc4, true, "seen_tc4");
ASSERT_EQ(skel->bss->seen_tc5, true, "seen_tc5");
ASSERT_EQ(skel->bss->seen_tc6, false, "seen_tc6");

skel->bss->seen_tc4 = false;
skel->bss->seen_tc5 = false;
skel->bss->seen_tc6 = false;

err = bpf_link__detach(skel->links.tc6);
if (!ASSERT_OK(err, "prog_detach"))
goto cleanup;

__assert_mprog_count(target, 0, true, loopback);

ASSERT_OK(system(ping_cmd), ping_cmd);

ASSERT_EQ(skel->bss->seen_tc4, false, "seen_tc4");
ASSERT_EQ(skel->bss->seen_tc5, true, "seen_tc5");
ASSERT_EQ(skel->bss->seen_tc6, false, "seen_tc6");

cleanup:
tc_opts.flags = tc_opts.prog_fd = tc_opts.prog_id = 0;
err = bpf_tc_detach(&tc_hook, &tc_opts);
ASSERT_OK(err, "bpf_tc_detach");

tc_hook.attach_point = BPF_TC_INGRESS | BPF_TC_EGRESS;
bpf_tc_hook_destroy(&tc_hook);

test_tc_link__destroy(skel);
}

void serial_test_tc_links_chain_mixed(void)
{
test_tc_chain_mixed(BPF_TCX_INGRESS);
test_tc_chain_mixed(BPF_TCX_EGRESS);
}

static void test_tc_links_ingress(int target, bool chain_tc_old,
bool tcx_teardown_first)
{
LIBBPF_OPTS(bpf_tc_opts, tc_opts,
.handle = 1,
.priority = 1,
);
LIBBPF_OPTS(bpf_tc_hook, tc_hook,
.ifindex = loopback,
.attach_point = BPF_TC_CUSTOM,
.parent = TC_H_INGRESS,
);
bool hook_created = false, tc_attached = false;
LIBBPF_OPTS(bpf_tcx_opts, optl);
__u32 pid1, pid2, pid3;
struct test_tc_link *skel;
struct bpf_link *link;
int err;

skel = test_tc_link__open();
if (!ASSERT_OK_PTR(skel, "skel_open"))
goto cleanup;

ASSERT_EQ(bpf_program__set_expected_attach_type(skel->progs.tc1, target),
0, "tc1_attach_type");
ASSERT_EQ(bpf_program__set_expected_attach_type(skel->progs.tc2, target),
0, "tc2_attach_type");

err = test_tc_link__load(skel);
if (!ASSERT_OK(err, "skel_load"))
goto cleanup;

pid1 = id_from_prog_fd(bpf_program__fd(skel->progs.tc1));
pid2 = id_from_prog_fd(bpf_program__fd(skel->progs.tc2));
pid3 = id_from_prog_fd(bpf_program__fd(skel->progs.tc3));

ASSERT_NEQ(pid1, pid2, "prog_ids_1_2");
ASSERT_NEQ(pid2, pid3, "prog_ids_2_3");

assert_mprog_count(target, 0);

if (chain_tc_old) {
ASSERT_OK(system("tc qdisc add dev lo ingress"), "add_ingress");
hook_created = true;

tc_opts.prog_fd = bpf_program__fd(skel->progs.tc3);
err = bpf_tc_attach(&tc_hook, &tc_opts);
if (!ASSERT_OK(err, "bpf_tc_attach"))
goto cleanup;
tc_attached = true;
}

link = bpf_program__attach_tcx(skel->progs.tc1, loopback, &optl);
if (!ASSERT_OK_PTR(link, "link_attach"))
goto cleanup;

skel->links.tc1 = link;

link = bpf_program__attach_tcx(skel->progs.tc2, loopback, &optl);
if (!ASSERT_OK_PTR(link, "link_attach"))
goto cleanup;

skel->links.tc2 = link;

assert_mprog_count(target, 2);

ASSERT_OK(system(ping_cmd), ping_cmd);

ASSERT_EQ(skel->bss->seen_tc1, true, "seen_tc1");
ASSERT_EQ(skel->bss->seen_tc2, true, "seen_tc2");
ASSERT_EQ(skel->bss->seen_tc3, chain_tc_old, "seen_tc3");

skel->bss->seen_tc1 = false;
skel->bss->seen_tc2 = false;
skel->bss->seen_tc3 = false;

err = bpf_link__detach(skel->links.tc2);
if (!ASSERT_OK(err, "prog_detach"))
goto cleanup;

assert_mprog_count(target, 1);

ASSERT_OK(system(ping_cmd), ping_cmd);

ASSERT_EQ(skel->bss->seen_tc1, true, "seen_tc1");
ASSERT_EQ(skel->bss->seen_tc2, false, "seen_tc2");
ASSERT_EQ(skel->bss->seen_tc3, chain_tc_old, "seen_tc3");
cleanup:
if (tc_attached) {
tc_opts.flags = tc_opts.prog_fd = tc_opts.prog_id = 0;
err = bpf_tc_detach(&tc_hook, &tc_opts);
ASSERT_OK(err, "bpf_tc_detach");
}
ASSERT_OK(system(ping_cmd), ping_cmd);
assert_mprog_count(target, 1);
if (hook_created && tcx_teardown_first)
ASSERT_OK(system("tc qdisc del dev lo ingress"), "del_ingress");
ASSERT_OK(system(ping_cmd), ping_cmd);
test_tc_link__destroy(skel);
ASSERT_OK(system(ping_cmd), ping_cmd);
if (hook_created && !tcx_teardown_first)
ASSERT_OK(system("tc qdisc del dev lo ingress"), "del_ingress");
ASSERT_OK(system(ping_cmd), ping_cmd);
assert_mprog_count(target, 0);
}

void serial_test_tc_links_ingress(void)
{
test_tc_links_ingress(BPF_TCX_INGRESS, true, true);
test_tc_links_ingress(BPF_TCX_INGRESS, true, false);
test_tc_links_ingress(BPF_TCX_INGRESS, false, false);
}
110 changes: 110 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/tc_opts.c
Original file line number Diff line number Diff line change
Expand Up @@ -2268,3 +2268,113 @@ void serial_test_tc_opts_delete_empty(void)
test_tc_opts_delete_empty(BPF_TCX_INGRESS, true);
test_tc_opts_delete_empty(BPF_TCX_EGRESS, true);
}

static void test_tc_chain_mixed(int target)
{
LIBBPF_OPTS(bpf_tc_opts, tc_opts, .handle = 1, .priority = 1);
LIBBPF_OPTS(bpf_tc_hook, tc_hook, .ifindex = loopback);
LIBBPF_OPTS(bpf_prog_attach_opts, opta);
LIBBPF_OPTS(bpf_prog_detach_opts, optd);
__u32 fd1, fd2, fd3, id1, id2, id3;
struct test_tc_link *skel;
int err, detach_fd;

skel = test_tc_link__open_and_load();
if (!ASSERT_OK_PTR(skel, "skel_load"))
goto cleanup;

fd1 = bpf_program__fd(skel->progs.tc4);
fd2 = bpf_program__fd(skel->progs.tc5);
fd3 = bpf_program__fd(skel->progs.tc6);

id1 = id_from_prog_fd(fd1);
id2 = id_from_prog_fd(fd2);
id3 = id_from_prog_fd(fd3);

ASSERT_NEQ(id1, id2, "prog_ids_1_2");
ASSERT_NEQ(id2, id3, "prog_ids_2_3");

assert_mprog_count(target, 0);

tc_hook.attach_point = target == BPF_TCX_INGRESS ?
BPF_TC_INGRESS : BPF_TC_EGRESS;
err = bpf_tc_hook_create(&tc_hook);
err = err == -EEXIST ? 0 : err;
if (!ASSERT_OK(err, "bpf_tc_hook_create"))
goto cleanup;

tc_opts.prog_fd = fd2;
err = bpf_tc_attach(&tc_hook, &tc_opts);
if (!ASSERT_OK(err, "bpf_tc_attach"))
goto cleanup_hook;

err = bpf_prog_attach_opts(fd3, loopback, target, &opta);
if (!ASSERT_EQ(err, 0, "prog_attach"))
goto cleanup_filter;

detach_fd = fd3;

assert_mprog_count(target, 1);

ASSERT_OK(system(ping_cmd), ping_cmd);

ASSERT_EQ(skel->bss->seen_tc4, false, "seen_tc4");
ASSERT_EQ(skel->bss->seen_tc5, false, "seen_tc5");
ASSERT_EQ(skel->bss->seen_tc6, true, "seen_tc6");

skel->bss->seen_tc4 = false;
skel->bss->seen_tc5 = false;
skel->bss->seen_tc6 = false;

LIBBPF_OPTS_RESET(opta,
.flags = BPF_F_REPLACE,
.replace_prog_fd = fd3,
);

err = bpf_prog_attach_opts(fd1, loopback, target, &opta);
if (!ASSERT_EQ(err, 0, "prog_attach"))
goto cleanup_opts;

detach_fd = fd1;

assert_mprog_count(target, 1);

ASSERT_OK(system(ping_cmd), ping_cmd);

ASSERT_EQ(skel->bss->seen_tc4, true, "seen_tc4");
ASSERT_EQ(skel->bss->seen_tc5, true, "seen_tc5");
ASSERT_EQ(skel->bss->seen_tc6, false, "seen_tc6");

skel->bss->seen_tc4 = false;
skel->bss->seen_tc5 = false;
skel->bss->seen_tc6 = false;

cleanup_opts:
err = bpf_prog_detach_opts(detach_fd, loopback, target, &optd);
ASSERT_OK(err, "prog_detach");
__assert_mprog_count(target, 0, true, loopback);

ASSERT_OK(system(ping_cmd), ping_cmd);

ASSERT_EQ(skel->bss->seen_tc4, false, "seen_tc4");
ASSERT_EQ(skel->bss->seen_tc5, true, "seen_tc5");
ASSERT_EQ(skel->bss->seen_tc6, false, "seen_tc6");

cleanup_filter:
tc_opts.flags = tc_opts.prog_fd = tc_opts.prog_id = 0;
err = bpf_tc_detach(&tc_hook, &tc_opts);
ASSERT_OK(err, "bpf_tc_detach");

cleanup_hook:
tc_hook.attach_point = BPF_TC_INGRESS | BPF_TC_EGRESS;
bpf_tc_hook_destroy(&tc_hook);

cleanup:
test_tc_link__destroy(skel);
}

void serial_test_tc_opts_chain_mixed(void)
{
test_tc_chain_mixed(BPF_TCX_INGRESS);
test_tc_chain_mixed(BPF_TCX_EGRESS);
}
16 changes: 16 additions & 0 deletions tools/testing/selftests/bpf/progs/test_tc_link.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ bool seen_tc1;
bool seen_tc2;
bool seen_tc3;
bool seen_tc4;
bool seen_tc5;
bool seen_tc6;

SEC("tc/ingress")
int tc1(struct __sk_buff *skb)
Expand Down Expand Up @@ -38,3 +40,17 @@ int tc4(struct __sk_buff *skb)
seen_tc4 = true;
return TCX_NEXT;
}

SEC("tc/egress")
int tc5(struct __sk_buff *skb)
{
seen_tc5 = true;
return TCX_PASS;
}

SEC("tc/egress")
int tc6(struct __sk_buff *skb)
{
seen_tc6 = true;
return TCX_PASS;
}

0 comments on commit 05fe108

Please sign in to comment.