All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Toke Høiland-Jørgensen" <toke@redhat.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>,
	Alexei Starovoitov <ast@kernel.org>,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Yonghong Song <yhs@fb.com>,
	Jesper Dangaard Brouer <brouer@redhat.com>,
	David Miller <davem@davemloft.net>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>
Subject: Re: [PATCH bpf-next v4 5/5] selftests: Add tests for automatic map pinning
Date: Thu, 31 Oct 2019 19:18:10 +0100	[thread overview]
Message-ID: <87wockn5i5.fsf@toke.dk> (raw)
In-Reply-To: <CAEf4BzZ6OYFYZNfQ4n7gPjyg0tWtsAaNWzZie3L23TE9KNtOoA@mail.gmail.com>

Andrii Nakryiko <andrii.nakryiko@gmail.com> writes:

> On Tue, Oct 29, 2019 at 12:39 PM Toke Høiland-Jørgensen <toke@redhat.com> wrote:
>>
>> From: Toke Høiland-Jørgensen <toke@redhat.com>
>>
>> This adds a new BPF selftest to exercise the new automatic map pinning
>> code.
>>
>> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
>> ---
>>  tools/testing/selftests/bpf/prog_tests/pinning.c |  157 ++++++++++++++++++++++
>>  tools/testing/selftests/bpf/progs/test_pinning.c |   29 ++++
>>  2 files changed, 186 insertions(+)
>>  create mode 100644 tools/testing/selftests/bpf/prog_tests/pinning.c
>>  create mode 100644 tools/testing/selftests/bpf/progs/test_pinning.c
>>
>> diff --git a/tools/testing/selftests/bpf/prog_tests/pinning.c b/tools/testing/selftests/bpf/prog_tests/pinning.c
>> new file mode 100644
>> index 000000000000..71f7dc51edc7
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/prog_tests/pinning.c
>> @@ -0,0 +1,157 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +
>> +#include <sys/types.h>
>> +#include <sys/stat.h>
>> +#include <unistd.h>
>> +#include <test_progs.h>
>> +
>> +__u32 get_map_id(struct bpf_object *obj, const char *name)
>> +{
>> +       __u32 map_info_len, duration, retval;
>> +       struct bpf_map_info map_info = {};
>> +       struct bpf_map *map;
>> +       int err;
>> +
>> +       map_info_len = sizeof(map_info);
>> +
>> +       map = bpf_object__find_map_by_name(obj, name);
>> +       if (CHECK(!map, "find map", "NULL map"))
>> +               return 0;
>> +
>> +       err = bpf_obj_get_info_by_fd(bpf_map__fd(map),
>> +                                    &map_info, &map_info_len);
>> +       CHECK(err, "get map info", "err %d errno %d", err, errno);
>> +       return map_info.id;
>> +}
>> +
>> +void test_pinning(void)
>> +{
>> +       __u32 duration, retval, size, map_id, map_id2;
>> +       const char *custpinpath = "/sys/fs/bpf/custom/pinmap";
>> +       const char *nopinpath = "/sys/fs/bpf/nopinmap";
>> +       const char *custpath = "/sys/fs/bpf/custom";
>
> Should this test mount/unmount (if necessary) /sys/fs/bpf? They will
> all fail if BPF FS is not mounted, right?

Yeah; I was kinda expecting that the test harness takes care of this. Is
it really a good idea for a selftest to mess with mount()?

>> +       const char *pinpath = "/sys/fs/bpf/pinmap";
>> +       const char *file = "./test_pinning.o";
>> +       struct stat statbuf = {};
>> +       struct bpf_object *obj;
>> +       struct bpf_map *map;
>> +       DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
>> +               .pin_root_path = custpath,
>> +       );
>> +
>> +       int err;
>> +       obj = bpf_object__open_file(file, NULL);
>> +       if (CHECK_FAIL(libbpf_get_error(obj)))
>> +               return;
>> +
>> +       err = bpf_object__load(obj);
>> +       if (CHECK(err, "default load", "err %d errno %d\n", err, errno))
>> +               goto out;
>> +
>> +       /* check that pinmap was pinned */
>> +       err = stat(pinpath, &statbuf);
>> +       if (CHECK(err, "stat pinpath", "err %d errno %d\n", err, errno))
>> +               goto out;
>> +
>> +        /* check that nopinmap was *not* pinned */
>> +       err = stat(nopinpath, &statbuf);
>> +       if (CHECK(!err || errno != ENOENT, "stat nopinpath",
>> +                 "err %d errno %d\n", err, errno))
>> +               goto out;
>> +
>> +        map_id = get_map_id(obj, "pinmap");
>
> something wrong with whitespaces here? can you please run
> scripts/checkpatch.pl to double-check?

Yup, some space got in where a tab should be

>> +       if (!map_id)
>> +               goto out;
>> +
>> +       bpf_object__close(obj);
>> +
>> +       obj = bpf_object__open_file(file, NULL);
>> +       if (CHECK_FAIL(libbpf_get_error(obj)))
>
> obj = NULL here before you go to out

Yup

>
>> +               goto out;
>> +
>> +       err = bpf_object__load(obj);
>> +       if (CHECK(err, "default load", "err %d errno %d\n", err, errno))
>> +               goto out;
>> +
>
> [...]
>
>> +       err = rmdir(custpath);
>> +       if (CHECK(err, "rmdir custpindir", "err %d errno %d\n", err, errno))
>> +               goto out;
>> +
>> +       bpf_object__close(obj);
>> +
>> +       /* test auto-pinning at custom path with open opt */
>> +       obj = bpf_object__open_file(file, &opts);
>> +       if (CHECK_FAIL(libbpf_get_error(obj)))
>> +               return;
>
> obj = NULL; goto out; to ensure pinpath is unlinked?

Yeah.

>> +
>> +       err = bpf_object__load(obj);
>> +       if (CHECK(err, "custom load", "err %d errno %d\n", err, errno))
>> +               goto out;
>> +
>> +       /* check that pinmap was pinned at the custom path */
>> +       err = stat(custpinpath, &statbuf);
>> +       if (CHECK(err, "stat custpinpath", "err %d errno %d\n", err, errno))
>> +               goto out;
>> +
>> +out:
>> +       unlink(pinpath);
>> +       unlink(nopinpath);
>> +       unlink(custpinpath);
>> +       rmdir(custpath);
>> +       if (obj)
>> +               bpf_object__close(obj);
>> +}
>> diff --git a/tools/testing/selftests/bpf/progs/test_pinning.c b/tools/testing/selftests/bpf/progs/test_pinning.c
>> new file mode 100644
>> index 000000000000..ff2d7447777e
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/progs/test_pinning.c
>> @@ -0,0 +1,29 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +
>> +#include <linux/bpf.h>
>> +#include "bpf_helpers.h"
>> +
>> +int _version SEC("version") = 1;
>> +
>> +struct {
>> +       __uint(type, BPF_MAP_TYPE_ARRAY);
>> +       __uint(max_entries, 1);
>> +       __type(key, __u32);
>> +       __type(value, __u64);
>> +       __uint(pinning, LIBBPF_PIN_BY_NAME);
>> +} pinmap SEC(".maps");
>> +
>> +struct {
>> +       __uint(type, BPF_MAP_TYPE_ARRAY);
>> +       __uint(max_entries, 1);
>> +       __type(key, __u32);
>> +       __type(value, __u64);
>> +} nopinmap SEC(".maps");
>
>
> would be nice to ensure that __uint(pinning, LIBBPF_PIN_NONE) also
> works as expected, do you mind adding one extra map?

Sure, can do...

>> +
>> +SEC("xdp_prog")
>> +int _xdp_prog(struct xdp_md *xdp)
>> +{
>> +       return XDP_PASS;
>> +}
>> +
>> +char _license[] SEC("license") = "GPL";
>>

      reply	other threads:[~2019-10-31 18:18 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-29 19:39 [PATCH bpf-next v4 0/5] libbpf: Support automatic pinning of maps using 'pinning' BTF attribute Toke Høiland-Jørgensen
2019-10-29 19:39 ` [PATCH bpf-next v4 1/5] libbpf: Fix error handling in bpf_map__reuse_fd() Toke Høiland-Jørgensen
2019-10-29 19:39 ` [PATCH bpf-next v4 2/5] libbpf: Store map pin path and status in struct bpf_map Toke Høiland-Jørgensen
2019-10-31 17:22   ` Andrii Nakryiko
2019-10-31 17:26     ` Toke Høiland-Jørgensen
2019-10-31 17:28       ` Andrii Nakryiko
2019-10-31 17:31     ` Toke Høiland-Jørgensen
2019-10-31 17:43       ` Andrii Nakryiko
2019-10-29 19:39 ` [PATCH bpf-next v4 3/5] libbpf: Move directory creation into _pin() functions Toke Høiland-Jørgensen
2019-10-31 17:27   ` Andrii Nakryiko
2019-10-29 19:39 ` [PATCH bpf-next v4 4/5] libbpf: Add auto-pinning of maps when loading BPF objects Toke Høiland-Jørgensen
2019-10-31 17:37   ` Andrii Nakryiko
2019-10-31 17:52     ` Andrii Nakryiko
2019-10-31 18:06       ` Toke Høiland-Jørgensen
2019-10-29 19:39 ` [PATCH bpf-next v4 5/5] selftests: Add tests for automatic map pinning Toke Høiland-Jørgensen
2019-10-31 18:02   ` Andrii Nakryiko
2019-10-31 18:18     ` Toke Høiland-Jørgensen [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=87wockn5i5.fsf@toke.dk \
    --to=toke@redhat.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brouer@redhat.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=kafai@fb.com \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=yhs@fb.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.