Thread (22 messages) flat view 22 messages, 5 authors, 2019-10-29

Re: [PATCH bpf-next v3 4/4] selftests: Add tests for automatic map pinning

From: Andrii Nakryiko <hidden>
Date: 2019-10-28 18:43:27
Also in: bpf

On Sun, Oct 27, 2019 at 1:53 PM Toke Høiland-Jørgensen [off-list ref] wrote:
quoted hunk ↗ jump to hunk
From: Toke Høiland-Jørgensen <redacted>

This adds a new BPF selftest to exercise the new automatic map pinning
code.

Signed-off-by: Toke Høiland-Jørgensen <redacted>
---
 tools/testing/selftests/bpf/prog_tests/pinning.c |   91 ++++++++++++++++++++++
 tools/testing/selftests/bpf/progs/test_pinning.c |   29 +++++++
 2 files changed, 120 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..d4a63de72f5a
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/pinning.c
@@ -0,0 +1,91 @@
+// 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")) {
please follow the pattern of "if (CHECK()) { return or goto cleanup
}". There is literally zero cases where we have `if (!CHECK())` in
selftests.
+               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;
+       }
+       return 0;
+}
+
+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";
+       const char *pinpath = "/sys/fs/bpf/pinmap";
+       const char *file = "./test_pinning.o";
+       struct stat statbuf = {};
+       struct bpf_object *obj;
+       DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
+               .auto_pin_path = custpath,
+       );
+
+       int err;
+       obj = bpf_object__open_file(file, NULL);
+       if (CHECK_FAIL(libbpf_get_error(obj)))
+               return;
+
+       err = bpf_object__load(obj);
+       CHECK(err, "default load", "err %d errno %d\n", err, errno);
cleanup and exit, you don't have a valid set up to proceed with
testing. Same for almost every check below.
+
+       /* check that pinmap was pinned */
+       err = stat(pinpath, &statbuf);
+       CHECK(err, "stat pinpath", "err %d errno %d\n", err, errno);
+
+        /* check that nopinmap was *not* pinned */
+       err = stat(nopinpath, &statbuf);
+       CHECK(errno != ENOENT, "stat nopinpath", "err %d errno %d\n", err, errno);
if previous stat succeeded, errno might be from other syscall, you
have to check both
+
+        map_id = get_map_id(obj, "pinmap");
formatting? check that get_map_id succeeded?
quoted hunk ↗ jump to hunk
+       bpf_object__close(obj);
+
+       obj = bpf_object__open_file(file, NULL);
+       if (CHECK_FAIL(libbpf_get_error(obj)))
+               return;
+
+       err = bpf_object__load(obj);
+       CHECK(err, "default load", "err %d errno %d\n", err, errno);
+
+       /* check that same map ID was reused for second load */
+       map_id2 = get_map_id(obj, "pinmap");
+       CHECK(map_id != map_id2, "check reuse",
+             "err %d errno %d id %d id2 %d\n", err, errno, map_id, map_id2);
+       unlink(pinpath);
+       bpf_object__close(obj);
+
+       err = mkdir(custpath, 0700);
+       CHECK(err, "mkdir custpath",  "err %d errno %d\n", err, errno);
+
+       obj = bpf_object__open_file(file, &opts);
+       if (CHECK_FAIL(libbpf_get_error(obj)))
+               return;
+
+       err = bpf_object__load(obj);
+       CHECK(err, "custom load", "err %d errno %d\n", err, errno);
+
+       /* check that pinmap was pinned at the custom path */
+       err = stat(custpinpath, &statbuf);
+       CHECK(err, "stat custpinpath", "err %d errno %d\n", err, errno);
+
+       unlink(custpinpath);
+       rmdir(custpath);
+       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");
+
+SEC("xdp_prog")
+int _xdp_prog(struct xdp_md *xdp)
+{
+       return XDP_PASS;
+}
+
+char _license[] SEC("license") = "GPL";
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help