diff mbox series

[RFC,bpf-next,2/2] selftests/bpf: Add bpf link iter test

Message ID 20220422182254.13693-3-9erthalion6@gmail.com (mailing list archive)
State RFC
Delegated to: BPF
Headers show
Series bpf: bpf link iterator | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 7 maintainers not CCed: shuah@kernel.org netdev@vger.kernel.org kafai@fb.com linux-kselftest@vger.kernel.org kennyyu@fb.com john.fastabend@gmail.com kpsingh@kernel.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-1 fail Logs for Kernel LATEST on ubuntu-latest + selftests
bpf/vmtest-bpf-next-VM_Test-2 fail Logs for Kernel LATEST on z15 + selftests

Commit Message

Dmitry Dolgov April 22, 2022, 6:22 p.m. UTC
Add a simple test for bpf link iterator

Signed-off-by: Dmitrii Dolgov <9erthalion6@gmail.com>
---
 .../selftests/bpf/prog_tests/bpf_iter.c        | 15 +++++++++++++++
 .../selftests/bpf/progs/bpf_iter_bpf_link.c    | 18 ++++++++++++++++++
 2 files changed, 33 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c

Comments

Andrii Nakryiko April 27, 2022, 7:03 p.m. UTC | #1
On Fri, Apr 22, 2022 at 11:23 AM Dmitrii Dolgov <9erthalion6@gmail.com> wrote:
>
> Add a simple test for bpf link iterator
>
> Signed-off-by: Dmitrii Dolgov <9erthalion6@gmail.com>
> ---
>  .../selftests/bpf/prog_tests/bpf_iter.c        | 15 +++++++++++++++
>  .../selftests/bpf/progs/bpf_iter_bpf_link.c    | 18 ++++++++++++++++++
>  2 files changed, 33 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
> index 2c403ddc8076..e14a7a6d925c 100644
> --- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
> +++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
> @@ -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;
>
> @@ -1172,6 +1173,20 @@ 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 (CHECK(skel, "bpf_iter_bpf_link__open_and_load",
> +                 "skeleton open_and_load unexpected success\n"))
> +               return;
> +

please don't use CHECK() for new tests, you need ASSERT_OK_PTR() here

> +       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];
> 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
> index 000000000000..a5041fa1cda9
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c
> @@ -0,0 +1,18 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2020 Facebook */
> +#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)

see progs/bpf_iter.h, let's add bpf_iter__bpf_link there as well

> +{
> +       struct seq_file *seq = ctx->meta->seq;
> +       struct bpf_link *link = ctx->link;
> +       int link_id;
> +
> +       link_id = link->id;
> +       bpf_seq_write(seq, &link_id, sizeof(link_id));
> +       return 0;
> +}
> --
> 2.32.0
>
Yonghong Song April 28, 2022, 4:42 p.m. UTC | #2
On 4/22/22 11:22 AM, Dmitrii Dolgov wrote:
> Add a simple test for bpf link iterator
> 
> Signed-off-by: Dmitrii Dolgov <9erthalion6@gmail.com>
> ---
>   .../selftests/bpf/prog_tests/bpf_iter.c        | 15 +++++++++++++++
>   .../selftests/bpf/progs/bpf_iter_bpf_link.c    | 18 ++++++++++++++++++
>   2 files changed, 33 insertions(+)
>   create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
> index 2c403ddc8076..e14a7a6d925c 100644
> --- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
> +++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
> @@ -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;
>   
> @@ -1172,6 +1173,20 @@ static void test_buf_neg_offset(void)
>   		bpf_iter_test_kern6__destroy(skel);
>   }
>   
> +static void test_link_iter(void)

This function is used. Please add a proper subtest for this
in function test_bpf_iter().

> +{
> +	struct bpf_iter_bpf_link *skel;
> +
> +	skel = bpf_iter_bpf_link__open_and_load();
> +	if (CHECK(skel, "bpf_iter_bpf_link__open_and_load",
> +		  "skeleton open_and_load unexpected success\n"))
> +		return;

This is not correct. You should have CHECK(!skel, ...) to return
only if skel is NULL. The error message "skeleton open_and_load 
unexpected success\n" is not correct either. Probably a copy-paste
error.

Also, since you are working on this file, probably convert all
CHECK's in this file to ASSERT_*() macros as patch #2. Then
this patch itself can be patch #3 using ASSERT_*() as well.

> +
> +	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];
> 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
> index 000000000000..a5041fa1cda9
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c
> @@ -0,0 +1,18 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2020 Facebook */

copyright issue.

> +#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)

Please put bpf_iter__bpf_link definition in bpf_iter.h so
the test can work with an old version of vmlinux.h.

> +{
> +	struct seq_file *seq = ctx->meta->seq;
> +	struct bpf_link *link = ctx->link;
> +	int link_id;

The 'link' pointer could be NULL as in previous patch
we have:

+	.ctx_arg_info		= {
+		{ offsetof(struct bpf_iter__bpf_link, link),
+		  PTR_TO_BTF_ID_OR_NULL },
+	},

So you need to add a check below.

	if (!link)
		return 0;

> +
> +	link_id = link->id;
> +	bpf_seq_write(seq, &link_id, sizeof(link_id));
> +	return 0;
> +}
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
index 2c403ddc8076..e14a7a6d925c 100644
--- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
@@ -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;
 
@@ -1172,6 +1173,20 @@  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 (CHECK(skel, "bpf_iter_bpf_link__open_and_load",
+		  "skeleton open_and_load unexpected success\n"))
+		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];
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
index 000000000000..a5041fa1cda9
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c
@@ -0,0 +1,18 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
+#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;
+
+	link_id = link->id;
+	bpf_seq_write(seq, &link_id, sizeof(link_id));
+	return 0;
+}