Subject: Re: [PATCH bpf-next v4 5/5] selftests: Add tests for automatic map pinning

From: Toke Høiland-Jørgensen <[email-masked]>
Date: Thu, 31 Oct 2019 19:18:10 +0100

Andrii Nakryiko <[email-masked]> writes:

> On Tue, Oct 29, 2019 at 12:39 PM Toke Høiland-Jørgensen <[email-masked]> wrote:
>>
>> From: Toke Høiland-Jørgensen <[email-masked]>
>>
>> This adds a new BPF selftest to exercise the new automatic map pinning
>> code.
>>
>> Signed-off-by: Toke Høiland-Jørgensen <[email-masked]>
>> ---
>>  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";
>>