selftests/bpf: Add bpf link iter test
authorDmitrii Dolgov <9erthalion6@gmail.com>
Tue, 10 May 2022 15:52:33 +0000 (17:52 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 10 May 2022 18:20:45 +0000 (11:20 -0700)
Add a simple test for bpf link iterator

Signed-off-by: Dmitrii Dolgov <9erthalion6@gmail.com>
Link: https://lore.kernel.org/r/20220510155233.9815-5-9erthalion6@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/bpf_iter.c
tools/testing/selftests/bpf/progs/bpf_iter.h
tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c [new file with mode: 0644]

index 48289c8..7ff5fa9 100644 (file)
@@ -26,6 +26,7 @@
 #include "bpf_iter_bpf_sk_storage_map.skel.h"
 #include "bpf_iter_test_kern5.skel.h"
 #include "bpf_iter_test_kern6.skel.h"
+#include "bpf_iter_bpf_link.skel.h"
 
 static int duration;
 
@@ -1106,6 +1107,19 @@ static void test_buf_neg_offset(void)
                bpf_iter_test_kern6__destroy(skel);
 }
 
+static void test_link_iter(void)
+{
+       struct bpf_iter_bpf_link *skel;
+
+       skel = bpf_iter_bpf_link__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_link__open_and_load"))
+               return;
+
+       do_dummy_read(skel->progs.dump_bpf_link);
+
+       bpf_iter_bpf_link__destroy(skel);
+}
+
 #define CMP_BUFFER_SIZE 1024
 static char task_vma_output[CMP_BUFFER_SIZE];
 static char proc_maps_output[CMP_BUFFER_SIZE];
@@ -1251,4 +1265,6 @@ void test_bpf_iter(void)
                test_rdonly_buf_out_of_bound();
        if (test__start_subtest("buf-neg-offset"))
                test_buf_neg_offset();
+       if (test__start_subtest("link-iter"))
+               test_link_iter();
 }
index 8cfaeba..97ec8bc 100644 (file)
@@ -16,6 +16,7 @@
 #define bpf_iter__bpf_map_elem bpf_iter__bpf_map_elem___not_used
 #define bpf_iter__bpf_sk_storage_map bpf_iter__bpf_sk_storage_map___not_used
 #define bpf_iter__sockmap bpf_iter__sockmap___not_used
+#define bpf_iter__bpf_link bpf_iter__bpf_link___not_used
 #define btf_ptr btf_ptr___not_used
 #define BTF_F_COMPACT BTF_F_COMPACT___not_used
 #define BTF_F_NONAME BTF_F_NONAME___not_used
@@ -37,6 +38,7 @@
 #undef bpf_iter__bpf_map_elem
 #undef bpf_iter__bpf_sk_storage_map
 #undef bpf_iter__sockmap
+#undef bpf_iter__bpf_link
 #undef btf_ptr
 #undef BTF_F_COMPACT
 #undef BTF_F_NONAME
@@ -132,6 +134,11 @@ struct bpf_iter__sockmap {
        struct sock *sk;
 };
 
+struct bpf_iter__bpf_link {
+       struct bpf_iter_meta *meta;
+       struct bpf_link *link;
+};
+
 struct btf_ptr {
        void *ptr;
        __u32 type_id;
diff --git a/tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c b/tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c
new file mode 100644 (file)
index 0000000..e1af2f8
--- /dev/null
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2022 Red Hat, Inc. */
+#include "bpf_iter.h"
+#include <bpf/bpf_helpers.h>
+
+char _license[] SEC("license") = "GPL";
+
+SEC("iter/bpf_link")
+int dump_bpf_link(struct bpf_iter__bpf_link *ctx)
+{
+       struct seq_file *seq = ctx->meta->seq;
+       struct bpf_link *link = ctx->link;
+       int link_id;
+
+       if (!link)
+               return 0;
+
+       link_id = link->id;
+       bpf_seq_write(seq, &link_id, sizeof(link_id));
+       return 0;
+}