bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: "Toke Høiland-Jørgensen" <toke@redhat.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>,
	Alexei Starovoitov <ast@fb.com>, bpf <bpf@vger.kernel.org>,
	Networking <netdev@vger.kernel.org>
Subject: Re: [PATCH v3 2/2] selftests: Add test for overriding global data value before load
Date: Sun, 29 Mar 2020 13:08:49 -0700	[thread overview]
Message-ID: <CAEf4BzYxTHoJRFut9NN3gx0QxpUnE7wRK-wyMwAjb4K=2T8-Mw@mail.gmail.com> (raw)
In-Reply-To: <87tv27joh4.fsf@toke.dk>

On Sun, Mar 29, 2020 at 6:10 AM Toke Høiland-Jørgensen <toke@redhat.com> wrote:
>
> Andrii Nakryiko <andrii.nakryiko@gmail.com> writes:
>
> > On Sat, Mar 28, 2020 at 11:29 AM Toke Høiland-Jørgensen <toke@redhat.com> wrote:
> >>
> >> This extends the global_data test to also exercise the new
> >> bpf_map__set_initial_value() function. The test simply overrides the global
> >> data section with all zeroes, and checks that the new value makes it into
> >> the kernel map on load.
> >>
> >> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
> >> ---
> >>  .../selftests/bpf/prog_tests/global_data.c    | 61 +++++++++++++++++++
> >>  1 file changed, 61 insertions(+)
> >>
> >> diff --git a/tools/testing/selftests/bpf/prog_tests/global_data.c b/tools/testing/selftests/bpf/prog_tests/global_data.c
> >> index c680926fce73..f018ce53a8d1 100644
> >> --- a/tools/testing/selftests/bpf/prog_tests/global_data.c
> >> +++ b/tools/testing/selftests/bpf/prog_tests/global_data.c
> >> @@ -121,6 +121,65 @@ static void test_global_data_rdonly(struct bpf_object *obj, __u32 duration)
> >>               "err %d errno %d\n", err, errno);
> >>  }
> >>
> >> +static void test_global_data_set_rdonly(__u32 duration)
> >> +{
> >> +       const char *file = "./test_global_data.o";
> >> +       int err = -ENOMEM, map_fd, zero = 0;
> >> +       __u8 *buff = NULL, *newval = NULL;
> >> +       struct bpf_program *prog;
> >> +       struct bpf_object *obj;
> >> +       struct bpf_map *map;
> >> +       size_t sz;
> >> +
> >> +       obj = bpf_object__open_file(file, NULL);
> >
> > Try using skeleton open and load .o file, it will cut this code almost
> > in half.
>
> Doesn't work, though:

Right, because test_global_data uses BPF-only struct definitions. In
real life those types should be shared in a common header file.

For this feature, I'd suggest to create a separate trivial BPF program
with a simple trivial BPF program and single global variable. There is
nothing you really need from test_global_data setup.

>
> In file included from /home/build/linux/tools/testing/selftests/bpf/prog_tests/global_data_init.c:3:
> /home/build/linux/tools/testing/selftests/bpf/test_global_data.skel.h:31:14: error: field ‘struct1’ has incomplete type
>    31 |   struct foo struct1;
>       |              ^~~~~~~
> /home/build/linux/tools/testing/selftests/bpf/test_global_data.skel.h:37:14: error: field ‘struct3’ has incomplete type
>    37 |   struct foo struct3;
>       |              ^~~~~~~
> /home/build/linux/tools/testing/selftests/bpf/test_global_data.skel.h:45:14: error: field ‘struct0’ has incomplete type
>    45 |   struct foo struct0;
>       |              ^~~~~~~
> /home/build/linux/tools/testing/selftests/bpf/test_global_data.skel.h:46:14: error: field ‘struct2’ has incomplete type
>    46 |   struct foo struct2;
>       |              ^~~~~~~
> make: *** [Makefile:361: /home/build/linux/tools/testing/selftests/bpf/global_data_init.test.o] Error 1
>
> Just fixing the program SEC name as you suggested below already gets rid
> of half the setup code, though, so doesn't really make much difference
> anyway :)
>
> >> +       if (CHECK_FAIL(!obj))
> >> +               return;
> >> +       prog = bpf_program__next(NULL, obj);
> >> +       if (CHECK_FAIL(!prog))
> >> +               goto out;
> >> +       err = bpf_program__set_sched_cls(prog);
> >
> > Please fix SEC() name for that program instead of setting type explicitly.
>
> Yeah, that helps, thanks!
>
> >> +       if (CHECK_FAIL(err))
> >> +               goto out;
> >> +
> >> +       map = bpf_object__find_map_by_name(obj, "test_glo.rodata");
> >> +       if (CHECK_FAIL(!map || !bpf_map__is_internal(map)))
> >> +               goto out;
> >> +
> >> +       sz = bpf_map__def(map)->value_size;
> >> +       newval = malloc(sz);
> >> +       if (CHECK_FAIL(!newval))
> >> +               goto out;
> >> +       memset(newval, 0, sz);
> >> +
> >> +       /* wrong size, should fail */
> >> +       err = bpf_map__set_initial_value(map, newval, sz - 1);
> >> +       if (CHECK(!err, "reject set initial value wrong size", "err %d\n", err))
> >> +               goto out;
> >> +
> >> +       err = bpf_map__set_initial_value(map, newval, sz);
> >> +       if (CHECK_FAIL(err))
> >> +               goto out;
> >> +
> >> +       err = bpf_object__load(obj);
> >> +       if (CHECK_FAIL(err))
> >> +               goto out;
> >> +
> >> +       map_fd = bpf_map__fd(map);
> >> +       if (CHECK_FAIL(map_fd < 0))
> >> +               goto out;
> >> +
> >> +       buff = malloc(sz);
> >> +       if (buff)
> >> +               err = bpf_map_lookup_elem(map_fd, &zero, buff);
> >> +       CHECK(!buff || err || memcmp(buff, newval, sz),
> >> +             "compare .rodata map data override",
> >> +             "err %d errno %d\n", err, errno);
> >> +out:
> >> +       free(buff);
> >> +       free(newval);
> >> +       bpf_object__close(obj);
> >> +}
> >> +
> >>  void test_global_data(void)
> >>  {
> >>         const char *file = "./test_global_data.o";
> >> @@ -144,4 +203,6 @@ void test_global_data(void)
> >>         test_global_data_rdonly(obj, duration);
> >>
> >>         bpf_object__close(obj);
> >> +
> >> +       test_global_data_set_rdonly(duration);
> >
> > This should either be a sub-test or better yet a separate test
> > altogether.
>
> Sure, will move it to its own file.
>
> -Toke
>

      reply	other threads:[~2020-03-29 20:09 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-26 15:17 [PATCH bpf-next] libbpf: Add bpf_object__rodata getter function Toke Høiland-Jørgensen
2020-03-26 19:20 ` Andrii Nakryiko
2020-03-27 12:24   ` Toke Høiland-Jørgensen
2020-03-27 17:49     ` Andrii Nakryiko
2020-03-27 18:39       ` Toke Høiland-Jørgensen
2020-03-27 12:58 ` [PATCH bpf-next v2] libbpf: Add getter for pointer to data area for internal maps Toke Høiland-Jørgensen
2020-03-27 19:17   ` Andrii Nakryiko
2020-03-27 22:26     ` Toke Høiland-Jørgensen
2020-03-27 22:28       ` Alexei Starovoitov
2020-03-28  0:08         ` Toke Høiland-Jørgensen
2020-03-28 18:28   ` [PATCH v3 1/2] libbpf: Add setter for initial value " Toke Høiland-Jørgensen
2020-03-28 20:01     ` Andrii Nakryiko
2020-03-29 12:17       ` Toke Høiland-Jørgensen
2020-03-29 13:22     ` [PATCH v4 " Toke Høiland-Jørgensen
2020-03-29 20:06       ` Andrii Nakryiko
2020-03-29 23:46       ` Daniel Borkmann
2020-03-29 13:22     ` [PATCH v4 2/2] selftests: Add test for overriding global data value before load Toke Høiland-Jørgensen
2020-03-29 20:13       ` Andrii Nakryiko
2020-03-28 18:28   ` [PATCH v3 " Toke Høiland-Jørgensen
2020-03-28 20:06     ` Andrii Nakryiko
2020-03-29 13:10       ` Toke Høiland-Jørgensen
2020-03-29 20:08         ` Andrii Nakryiko [this message]

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='CAEf4BzYxTHoJRFut9NN3gx0QxpUnE7wRK-wyMwAjb4K=2T8-Mw@mail.gmail.com' \
    --to=andrii.nakryiko@gmail.com \
    --cc=ast@fb.com \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=netdev@vger.kernel.org \
    --cc=toke@redhat.com \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).