bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: "Mauricio Vásquez" <mauricio@kinvolk.io>
Cc: Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Quentin Monnet <quentin@isovalent.com>,
	Rafael David Tinoco <rafaeldtinoco@gmail.com>,
	Lorenzo Fontana <lorenzo.fontana@elastic.co>,
	Leonardo Di Donato <leonardo.didonato@elastic.co>
Subject: Re: [PATCH bpf-next v7 7/7] selftests/bpf: Test "bpftool gen min_core_btf"
Date: Wed, 16 Feb 2022 10:20:58 -0800	[thread overview]
Message-ID: <CAEf4BzYuXTYRP+3Mkk9a1rofdNJOWG52HDMjRsyYosyfs4sQhQ@mail.gmail.com> (raw)
In-Reply-To: <20220215225856.671072-8-mauricio@kinvolk.io>

On Tue, Feb 15, 2022 at 2:59 PM Mauricio Vásquez <mauricio@kinvolk.io> wrote:
>
> This commit reuses the core_reloc test to check if the BTF files
> generated with "bpftool gen min_core_btf" are correct. This introduces
> test_core_btfgen() that runs all the core_reloc tests, but this time
> the source BTF files are generated by using "bpftool gen min_core_btf".
>
> The goal of this test is to check that the generated files are usable,
> and not to check if the algorithm is creating an optimized BTF file.
>
> Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
> Signed-off-by: Rafael David Tinoco <rafael.tinoco@aquasec.com>
> Signed-off-by: Lorenzo Fontana <lorenzo.fontana@elastic.co>
> Signed-off-by: Leonardo Di Donato <leonardo.didonato@elastic.co>
> ---
>  .../selftests/bpf/prog_tests/core_reloc.c     | 50 ++++++++++++++++++-
>  1 file changed, 48 insertions(+), 2 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/core_reloc.c b/tools/testing/selftests/bpf/prog_tests/core_reloc.c
> index 68e4c8dafa00..fa2908879c77 100644
> --- a/tools/testing/selftests/bpf/prog_tests/core_reloc.c
> +++ b/tools/testing/selftests/bpf/prog_tests/core_reloc.c
> @@ -2,6 +2,7 @@
>  #include <test_progs.h>
>  #include "progs/core_reloc_types.h"
>  #include "bpf_testmod/bpf_testmod.h"
> +#include <linux/limits.h>
>  #include <sys/mman.h>
>  #include <sys/syscall.h>
>  #include <bpf/btf.h>
> @@ -836,13 +837,27 @@ static size_t roundup_page(size_t sz)
>         return (sz + page_size - 1) / page_size * page_size;
>  }
>
> -void test_core_reloc(void)
> +static int run_btfgen(const char *src_btf, const char *dst_btf, const char *objpath)
> +{
> +       char command[4096];
> +       int n;
> +
> +       n = snprintf(command, sizeof(command),
> +                    "./tools/build/bpftool/bpftool gen min_core_btf %s %s %s",
> +                    src_btf, dst_btf, objpath);
> +       if (n < 0 || n >= sizeof(command))
> +               return -1;
> +
> +       return system(command);
> +}
> +
> +static void run_core_reloc_tests(bool use_btfgen)
>  {
>         const size_t mmap_sz = roundup_page(sizeof(struct data));
>         DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts);
>         struct core_reloc_test_case *test_case;
>         const char *tp_name, *probe_name;
> -       int err, i, equal;
> +       int err, i, equal, fd;
>         struct bpf_link *link = NULL;
>         struct bpf_map *data_map;
>         struct bpf_program *prog;
> @@ -854,6 +869,7 @@ void test_core_reloc(void)
>         my_pid_tgid = getpid() | ((uint64_t)syscall(SYS_gettid) << 32);
>
>         for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
> +               char btf_file[] = "/tmp/core_reloc.btf.XXXXXX";
>                 test_case = &test_cases[i];
>                 if (!test__start_subtest(test_case->case_name))
>                         continue;
> @@ -863,6 +879,25 @@ void test_core_reloc(void)
>                         continue;
>                 }
>
> +               /* generate a "minimal" BTF file and use it as source */
> +               if (use_btfgen) {
> +                       if (!test_case->btf_src_file || test_case->fails) {
> +                               test__skip();
> +                               continue;
> +                       }
> +
> +                       fd = mkstemp(btf_file);
> +                       if (CHECK(fd < 0, "btf_tmp", "failed to create file: %d\n", fd))
> +                               goto cleanup;

no new CHECK()s, please

> +                       close(fd); /* we only need the path */
> +                       err = run_btfgen(test_case->btf_src_file, btf_file,
> +                                        test_case->bpf_obj_file);
> +                       if (!ASSERT_OK(err, "run_btfgen"))
> +                               goto cleanup;
> +
> +                       test_case->btf_src_file = btf_file;
> +               }
> +
>                 if (test_case->setup) {
>                         err = test_case->setup(test_case);
>                         if (CHECK(err, "test_setup", "test #%d setup failed: %d\n", i, err))
> @@ -954,8 +989,19 @@ void test_core_reloc(void)
>                         CHECK_FAIL(munmap(mmap_data, mmap_sz));
>                         mmap_data = NULL;
>                 }
> +               remove(btf_file);

would be a bit safer to do unlink() here instead of remove, but it's
probably fine as is (I didn't touch this part)


>                 bpf_link__destroy(link);
>                 link = NULL;
>                 bpf_object__close(obj);
>         }
>  }
> +
> +void test_core_reloc(void)
> +{
> +       run_core_reloc_tests(false);
> +}
> +
> +void test_core_btfgen(void)
> +{
> +       run_core_reloc_tests(true);
> +}
> --
> 2.25.1
>

  reply	other threads:[~2022-02-16 18:21 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-15 22:58 [PATCH bpf-next v7 0/7] libbpf: Implement BTFGen Mauricio Vásquez
2022-02-15 22:58 ` [PATCH bpf-next v7 1/7] libbpf: split bpf_core_apply_relo() Mauricio Vásquez
2022-02-16  1:52   ` Alexei Starovoitov
2022-02-15 22:58 ` [PATCH bpf-next v7 2/7] libbpf: Expose bpf_core_{add,free}_cands() to bpftool Mauricio Vásquez
2022-02-15 22:58 ` [PATCH bpf-next v7 3/7] bpftool: Add gen min_core_btf command Mauricio Vásquez
2022-02-15 22:58 ` [PATCH bpf-next v7 4/7] bpftool: Implement "gen min_core_btf" logic Mauricio Vásquez
2022-02-18 16:20   ` Quentin Monnet
2022-02-18 19:43     ` Mauricio Vásquez Bernal
2022-02-18 19:48       ` Andrii Nakryiko
2022-02-18 19:52         ` Quentin Monnet
2022-02-15 22:58 ` [PATCH bpf-next v7 5/7] bpftool: Implement btfgen_get_btf() Mauricio Vásquez
2022-02-16 18:20   ` Andrii Nakryiko
2022-02-15 22:58 ` [PATCH bpf-next v7 6/7] bpftool: gen min_core_btf explanation and examples Mauricio Vásquez
2022-02-16 18:20   ` Andrii Nakryiko
2022-02-15 22:58 ` [PATCH bpf-next v7 7/7] selftests/bpf: Test "bpftool gen min_core_btf" Mauricio Vásquez
2022-02-16 18:20   ` Andrii Nakryiko [this message]
2022-02-16 18:20 ` [PATCH bpf-next v7 0/7] libbpf: Implement BTFGen Andrii Nakryiko
2022-02-17 22:07   ` Mauricio Vásquez Bernal
2022-02-17 22:12     ` Andrii Nakryiko
2022-02-16 18:30 ` patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAEf4BzYuXTYRP+3Mkk9a1rofdNJOWG52HDMjRsyYosyfs4sQhQ@mail.gmail.com \
    --to=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=leonardo.didonato@elastic.co \
    --cc=lorenzo.fontana@elastic.co \
    --cc=mauricio@kinvolk.io \
    --cc=netdev@vger.kernel.org \
    --cc=quentin@isovalent.com \
    --cc=rafaeldtinoco@gmail.com \
    --subject='Re: [PATCH bpf-next v7 7/7] selftests/bpf: Test "bpftool gen min_core_btf"' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).