]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: Test attaching a bpf qdisc with incomplete operators
authorAmery Hung <ameryhung@gmail.com>
Fri, 2 May 2025 20:16:23 +0000 (13:16 -0700)
committerMartin KaFai Lau <martin.lau@kernel.org>
Fri, 2 May 2025 22:42:48 +0000 (15:42 -0700)
Implement .destroy in bpf_fq and bpf_fifo as it is now mandatory.

Test attaching a bpf qdisc with a missing operator .init. This is not
allowed as bpf qdisc qdisc_watchdog_cancel() could have been called with
an uninitialized timer.

Signed-off-by: Amery Hung <ameryhung@gmail.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c
tools/testing/selftests/bpf/progs/bpf_qdisc_fail__incompl_ops.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/bpf_qdisc_fifo.c
tools/testing/selftests/bpf/progs/bpf_qdisc_fq.c

index 8952ddd2d5fcc567f42657301ba546f0f1f27702..4b7aadb8ffe63e0de842de24bdef7c4e14d00c67 100644 (file)
@@ -7,6 +7,7 @@
 #include "network_helpers.h"
 #include "bpf_qdisc_fifo.skel.h"
 #include "bpf_qdisc_fq.skel.h"
+#include "bpf_qdisc_fail__incompl_ops.skel.h"
 
 #define LO_IFINDEX 1
 
@@ -159,6 +160,22 @@ out:
        bpf_qdisc_fifo__destroy(fifo_skel);
 }
 
+static void test_incompl_ops(void)
+{
+       struct bpf_qdisc_fail__incompl_ops *skel;
+       struct bpf_link *link;
+
+       skel = bpf_qdisc_fail__incompl_ops__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "bpf_qdisc_fifo__open_and_load"))
+               return;
+
+       link = bpf_map__attach_struct_ops(skel->maps.test);
+       if (!ASSERT_ERR_PTR(link, "bpf_map__attach_struct_ops"))
+               bpf_link__destroy(link);
+
+       bpf_qdisc_fail__incompl_ops__destroy(skel);
+}
+
 static int get_default_qdisc(char *qdisc_name)
 {
        FILE *f;
@@ -230,6 +247,8 @@ void test_bpf_qdisc(void)
                test_qdisc_attach_to_mq();
        if (test__start_subtest("attach to non root"))
                test_qdisc_attach_to_non_root();
+       if (test__start_subtest("incompl_ops"))
+               test_incompl_ops();
 
        netns_free(netns);
 }
diff --git a/tools/testing/selftests/bpf/progs/bpf_qdisc_fail__incompl_ops.c b/tools/testing/selftests/bpf/progs/bpf_qdisc_fail__incompl_ops.c
new file mode 100644 (file)
index 0000000..f188062
--- /dev/null
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <vmlinux.h>
+#include "bpf_experimental.h"
+#include "bpf_qdisc_common.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("struct_ops")
+int BPF_PROG(bpf_qdisc_test_enqueue, struct sk_buff *skb, struct Qdisc *sch,
+            struct bpf_sk_buff_ptr *to_free)
+{
+       bpf_qdisc_skb_drop(skb, to_free);
+       return NET_XMIT_DROP;
+}
+
+SEC("struct_ops")
+struct sk_buff *BPF_PROG(bpf_qdisc_test_dequeue, struct Qdisc *sch)
+{
+       return NULL;
+}
+
+SEC("struct_ops")
+void BPF_PROG(bpf_qdisc_test_reset, struct Qdisc *sch)
+{
+}
+
+SEC("struct_ops")
+void BPF_PROG(bpf_qdisc_test_destroy, struct Qdisc *sch)
+{
+}
+
+SEC(".struct_ops")
+struct Qdisc_ops test = {
+       .enqueue   = (void *)bpf_qdisc_test_enqueue,
+       .dequeue   = (void *)bpf_qdisc_test_dequeue,
+       .reset     = (void *)bpf_qdisc_test_reset,
+       .destroy   = (void *)bpf_qdisc_test_destroy,
+       .id        = "bpf_qdisc_test",
+};
+
index 571fa7233ec093e0c34d35e6b56dedfd2dc2b133..1de2be3e370b76a0fcf7a0830b8b4382a8d7f4da 100644 (file)
@@ -109,12 +109,18 @@ void BPF_PROG(bpf_fifo_reset, struct Qdisc *sch)
        sch->q.qlen = 0;
 }
 
+SEC("struct_ops")
+void BPF_PROG(bpf_fifo_destroy, struct Qdisc *sch)
+{
+}
+
 SEC(".struct_ops")
 struct Qdisc_ops fifo = {
        .enqueue   = (void *)bpf_fifo_enqueue,
        .dequeue   = (void *)bpf_fifo_dequeue,
        .init      = (void *)bpf_fifo_init,
        .reset     = (void *)bpf_fifo_reset,
+       .destroy   = (void *)bpf_fifo_destroy,
        .id        = "bpf_fifo",
 };
 
index 7c110a1562247829cd612fed322c3fc3a510e9a9..1a3233a275c7ea029ee0c8dcd2550819f640bcfc 100644 (file)
@@ -740,11 +740,17 @@ int BPF_PROG(bpf_fq_init, struct Qdisc *sch, struct nlattr *opt,
        return 0;
 }
 
+SEC("struct_ops")
+void BPF_PROG(bpf_fq_destroy, struct Qdisc *sch)
+{
+}
+
 SEC(".struct_ops")
 struct Qdisc_ops fq = {
        .enqueue   = (void *)bpf_fq_enqueue,
        .dequeue   = (void *)bpf_fq_dequeue,
        .reset     = (void *)bpf_fq_reset,
        .init      = (void *)bpf_fq_init,
+       .destroy   = (void *)bpf_fq_destroy,
        .id        = "bpf_fq",
 };