Message ID | 20220422182254.13693-3-9erthalion6@gmail.com (mailing list archive) |
---|---|
State | RFC |
Delegated to: | BPF |
Headers | show |
Series | bpf: bpf link iterator | expand |
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 >
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 --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; +}
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