Thread (8 messages) 8 messages, 2 authors, 2021-10-02

Re: [PATCH bpf-next 2/2] selftests/bpf: Test BPF map creation using BTF-defined key/value

From: Andrii Nakryiko <hidden>
Date: 2021-09-09 04:29:55

On Sun, Sep 5, 2021 at 3:09 AM Hengqi Chen [off-list ref] wrote:
quoted hunk ↗ jump to hunk
Test BPF map creation using BTF-defined key/value. The test defines
some specialized maps by specifying BTF types for key/value and
checks those maps are correctly initialized and loaded.

Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
---
 .../selftests/bpf/prog_tests/map_create.c     |  87 ++++++++++++++
 .../selftests/bpf/progs/test_map_create.c     | 110 ++++++++++++++++++
 2 files changed, 197 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/map_create.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_map_create.c
diff --git a/tools/testing/selftests/bpf/prog_tests/map_create.c b/tools/testing/selftests/bpf/prog_tests/map_create.c
new file mode 100644
index 000000000000..6ca32d0dffd2
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/map_create.c
@@ -0,0 +1,87 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2021 Hengqi Chen */
+
+#include <test_progs.h>
+#include "test_map_create.skel.h"
+
+void test_map_create(void)
+{
+       struct test_map_create *skel;
+       int err, fd;
+
+       skel = test_map_create__open();
+       if (!ASSERT_OK_PTR(skel, "test_map_create__open failed"))
+               return;
+
+       err = test_map_create__load(skel);
If load() succeeds, all the maps will definitely be created, so all
the below tests are meaningless.

I think it's better to just change all the existing map definitions
used throughout selftests to use key/value types, instead of
key_size/value_size. That will automatically test this feature without
adding an extra test. Unfortunately to really test that the logic is
working, we'd need to check that libbpf doesn't emit the warning about
retrying map creation w/o BTF, but I think one-time manual check
(please use ./test_progs -v to see libbpf warnings during tests)
should be sufficient for this.
+       if (!ASSERT_OK(err, "test_map_create__load failed"))
+               goto cleanup;
+
+       fd = bpf_map__fd(skel->maps.map1);
+       if (!ASSERT_GT(fd, 0, "bpf_map__fd failed"))
+               goto cleanup;
+       close(fd);
+
+       fd = bpf_map__fd(skel->maps.map2);
+       if (!ASSERT_GT(fd, 0, "bpf_map__fd failed"))
+               goto cleanup;
+       close(fd);
[...]
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help