All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next v2] selftests/bpf: Test btf__load_vmlinux_btf/btf__load_module_btf APIs
@ 2021-08-02 16:05 Hengqi Chen
  2021-08-02 21:17 ` Andrii Nakryiko
  0 siblings, 1 reply; 2+ messages in thread
From: Hengqi Chen @ 2021-08-02 16:05 UTC (permalink / raw)
  To: bpf; +Cc: ast, daniel, andrii, yhs, john.fastabend, hengqi.chen

Add test for btf__load_vmlinux_btf/btf__load_module_btf APIs. It first
checks that if btrfs module BTF exists, if yes, load module BTF and
check symbol existence.

Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
---
 .../selftests/bpf/prog_tests/btf_module.c     | 32 +++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_module.c

diff --git a/tools/testing/selftests/bpf/prog_tests/btf_module.c b/tools/testing/selftests/bpf/prog_tests/btf_module.c
new file mode 100644
index 000000000000..9314a46e001c
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/btf_module.c
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Hengqi Chen */
+
+#include <test_progs.h>
+#include <bpf/btf.h>
+
+static const char *module_path = "/sys/kernel/btf/btrfs";
+static const char *module_name = "btrfs";
+
+void test_btf_module()
+{
+	struct btf *vmlinux_btf, *module_btf;
+	__s32 type_id;
+
+	if (access(module_path, F_OK))
+		return;
+
+	vmlinux_btf = btf__load_vmlinux_btf();
+	if (!ASSERT_OK_PTR(vmlinux_btf, "could not load vmlinux BTF"))
+		return;
+
+	module_btf = btf__load_module_btf(module_name, vmlinux_btf);
+	if (!ASSERT_OK_PTR(module_btf, "could not load module BTF"))
+		goto cleanup;
+
+	type_id = btf__find_by_name(module_btf, "btrfs_file_open");
+	ASSERT_GT(type_id, 0, "func btrfs_file_open not found");
+
+cleanup:
+	btf__free(module_btf);
+	btf__free(vmlinux_btf);
+}
--
2.25.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH bpf-next v2] selftests/bpf: Test btf__load_vmlinux_btf/btf__load_module_btf APIs
  2021-08-02 16:05 [PATCH bpf-next v2] selftests/bpf: Test btf__load_vmlinux_btf/btf__load_module_btf APIs Hengqi Chen
@ 2021-08-02 21:17 ` Andrii Nakryiko
  0 siblings, 0 replies; 2+ messages in thread
From: Andrii Nakryiko @ 2021-08-02 21:17 UTC (permalink / raw)
  To: Hengqi Chen
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Yonghong Song, john fastabend

On Mon, Aug 2, 2021 at 9:06 AM Hengqi Chen <hengqi.chen@gmail.com> wrote:
>
> Add test for btf__load_vmlinux_btf/btf__load_module_btf APIs. It first
> checks that if btrfs module BTF exists, if yes, load module BTF and
> check symbol existence.
>
> Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
> ---
>  .../selftests/bpf/prog_tests/btf_module.c     | 32 +++++++++++++++++++
>  1 file changed, 32 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_module.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/btf_module.c b/tools/testing/selftests/bpf/prog_tests/btf_module.c
> new file mode 100644
> index 000000000000..9314a46e001c
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/btf_module.c
> @@ -0,0 +1,32 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2021 Hengqi Chen */
> +
> +#include <test_progs.h>
> +#include <bpf/btf.h>
> +
> +static const char *module_path = "/sys/kernel/btf/btrfs";
> +static const char *module_name = "btrfs";
> +
> +void test_btf_module()
> +{
> +       struct btf *vmlinux_btf, *module_btf;
> +       __s32 type_id;
> +
> +       if (access(module_path, F_OK))
> +               return;

we shouldn't use btrfs module, as it might not be mounted in our CI
environment. Good news is that selftests uses its own custom module to
do various module-related logic. It's already preloaded for all tests,
so you can just assume its existence (unless pre-loading fails, then
error out). See how "bpf_testmod" is used in selftests.

> +
> +       vmlinux_btf = btf__load_vmlinux_btf();
> +       if (!ASSERT_OK_PTR(vmlinux_btf, "could not load vmlinux BTF"))
> +               return;
> +
> +       module_btf = btf__load_module_btf(module_name, vmlinux_btf);
> +       if (!ASSERT_OK_PTR(module_btf, "could not load module BTF"))
> +               goto cleanup;
> +
> +       type_id = btf__find_by_name(module_btf, "btrfs_file_open");
> +       ASSERT_GT(type_id, 0, "func btrfs_file_open not found");
> +
> +cleanup:
> +       btf__free(module_btf);
> +       btf__free(vmlinux_btf);
> +}
> --
> 2.25.1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-02 21:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-02 16:05 [PATCH bpf-next v2] selftests/bpf: Test btf__load_vmlinux_btf/btf__load_module_btf APIs Hengqi Chen
2021-08-02 21:17 ` Andrii Nakryiko

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.