selftests/bpf: Avoid pinning prog when attaching to tc ingress in btf_skc_cls_ingress
authorMartin KaFai Lau <martin.lau@kernel.org>
Tue, 29 Nov 2022 07:09:00 +0000 (23:09 -0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Wed, 30 Nov 2022 21:47:43 +0000 (22:47 +0100)
This patch removes the need to pin prog when attaching to tc ingress
in the btf_skc_cls_ingress test.  Instead, directly use the
bpf_tc_hook_create() and bpf_tc_attach().  The qdisc clsact
will go away together with the netns, so no need to
bpf_tc_hook_destroy().

Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Stanislav Fomichev <sdf@google.com>
Link: https://lore.kernel.org/bpf/20221129070900.3142427-8-martin.lau@linux.dev
tools/testing/selftests/bpf/prog_tests/btf_skc_cls_ingress.c

index 7a27703..ef4d6a3 100644 (file)
@@ -9,6 +9,7 @@
 #include <string.h>
 #include <errno.h>
 #include <sched.h>
+#include <net/if.h>
 #include <linux/compiler.h>
 #include <bpf/libbpf.h>
 
@@ -20,10 +21,12 @@ static struct test_btf_skc_cls_ingress *skel;
 static struct sockaddr_in6 srv_sa6;
 static __u32 duration;
 
-#define PROG_PIN_FILE "/sys/fs/bpf/btf_skc_cls_ingress"
-
 static int prepare_netns(void)
 {
+       LIBBPF_OPTS(bpf_tc_hook, qdisc_lo, .attach_point = BPF_TC_INGRESS);
+       LIBBPF_OPTS(bpf_tc_opts, tc_attach,
+                   .prog_fd = bpf_program__fd(skel->progs.cls_ingress));
+
        if (CHECK(unshare(CLONE_NEWNET), "create netns",
                  "unshare(CLONE_NEWNET): %s (%d)",
                  strerror(errno), errno))
@@ -33,12 +36,12 @@ static int prepare_netns(void)
                  "ip link set dev lo up", "failed\n"))
                return -1;
 
-       if (CHECK(system("tc qdisc add dev lo clsact"),
-                 "tc qdisc add dev lo clsact", "failed\n"))
+       qdisc_lo.ifindex = if_nametoindex("lo");
+       if (!ASSERT_OK(bpf_tc_hook_create(&qdisc_lo), "qdisc add dev lo clsact"))
                return -1;
 
-       if (CHECK(system("tc filter add dev lo ingress bpf direct-action object-pinned " PROG_PIN_FILE),
-                 "install tc cls-prog at ingress", "failed\n"))
+       if (!ASSERT_OK(bpf_tc_attach(&qdisc_lo, &tc_attach),
+                      "filter add dev lo ingress"))
                return -1;
 
        /* Ensure 20 bytes options (i.e. in total 40 bytes tcp header) for the
@@ -195,19 +198,12 @@ static struct test tests[] = {
 
 void test_btf_skc_cls_ingress(void)
 {
-       int i, err;
+       int i;
 
        skel = test_btf_skc_cls_ingress__open_and_load();
        if (CHECK(!skel, "test_btf_skc_cls_ingress__open_and_load", "failed\n"))
                return;
 
-       err = bpf_program__pin(skel->progs.cls_ingress, PROG_PIN_FILE);
-       if (CHECK(err, "bpf_program__pin",
-                 "cannot pin bpf prog to %s. err:%d\n", PROG_PIN_FILE, err)) {
-               test_btf_skc_cls_ingress__destroy(skel);
-               return;
-       }
-
        for (i = 0; i < ARRAY_SIZE(tests); i++) {
                if (!test__start_subtest(tests[i].desc))
                        continue;
@@ -221,6 +217,5 @@ void test_btf_skc_cls_ingress(void)
                reset_test();
        }
 
-       bpf_program__unpin(skel->progs.cls_ingress, PROG_PIN_FILE);
        test_btf_skc_cls_ingress__destroy(skel);
 }