[RFC bpf-next 3/3] selftests/bpf: add test for module helper

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is a simple test for a module hepler that accepts
2 pointers to integer, prints them (using printk which isn't
directly accessible to eBPF applications) and returns their sum.
The test has been adapted from test_ksyms_module.

Signed-off-by: Usama Arif <usama.arif@xxxxxxxxxxxxx>
---
 tools/testing/selftests/bpf/Makefile          |  3 +-
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   | 21 +++++++
 .../selftests/bpf/prog_tests/helper_module.c  | 59 +++++++++++++++++++
 .../selftests/bpf/progs/test_helper_module.c  | 18 ++++++
 4 files changed, 100 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/helper_module.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_helper_module.c

diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 42ffc24e9e71..34df13cdfb05 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -332,7 +332,8 @@ LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \
 	test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c \
 	map_ptr_kern.c core_kern.c
 # Generate both light skeleton and libbpf skeleton for these
-LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test_subprog.c
+LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test_subprog.c \
+	test_helper_module.c
 SKEL_BLACKLIST += $$(LSKELS)
 
 test_static_linked.skel.h-deps := test_static_linked1.o test_static_linked2.o
diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
index bdbacf5adcd2..38d344e2d12d 100644
--- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2020 Facebook */
+#include <linux/bpf.h>
 #include <linux/btf.h>
 #include <linux/btf_ids.h>
 #include <linux/error-injection.h>
@@ -120,11 +121,29 @@ static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = {
 
 extern int bpf_fentry_test1(int a);
 
+int bpf_helper_print_add(int *input1, int *input2)
+{
+	printk(KERN_INFO "input numbers for module helper %d %d\n", *input1, *input2);
+	return *input1 + *input2;
+}
+
+struct bpf_func_proto bpf_helper_print_add_proto = {
+	.gpl_only       = false,
+	.ret_type       = RET_INTEGER,
+	.arg1_type      = ARG_PTR_TO_INT,
+	.arg2_type      = ARG_PTR_TO_INT,
+};
+
+DEFINE_MOD_HELPER(mod_helper, THIS_MODULE, bpf_helper_print_add, bpf_helper_print_add_proto);
+
 static int bpf_testmod_init(void)
 {
 	int ret;
 
 	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_testmod_kfunc_set);
+	if (ret < 0)
+		return ret;
+	ret = register_mod_helper(&mod_helper);
 	if (ret < 0)
 		return ret;
 	if (bpf_fentry_test1(0) < 0)
@@ -134,6 +153,8 @@ static int bpf_testmod_init(void)
 
 static void bpf_testmod_exit(void)
 {
+	unregister_mod_helper(&mod_helper);
+
 	return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
 }
 
diff --git a/tools/testing/selftests/bpf/prog_tests/helper_module.c b/tools/testing/selftests/bpf/prog_tests/helper_module.c
new file mode 100644
index 000000000000..d8e8600ab3be
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/helper_module.c
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include <network_helpers.h>
+#include "test_helper_module.lskel.h"
+#include "test_helper_module.skel.h"
+
+void test_helper_module_lskel(void)
+{
+	struct test_helper_module_lskel *skel;
+	int retval;
+	int err;
+
+	if (!env.has_testmod) {
+		test__skip();
+		return;
+	}
+
+	skel = test_helper_module_lskel__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "test_helper_module_lskel__open_and_load"))
+		return;
+	err = bpf_prog_test_run(skel->progs.load.prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
+				NULL, NULL, (__u32 *)&retval, NULL);
+	if (!ASSERT_OK(err, "bpf_prog_test_run"))
+		goto cleanup;
+	ASSERT_EQ(retval, 7, "retval");
+cleanup:
+	test_helper_module_lskel__destroy(skel);
+}
+
+void test_helper_module_libbpf(void)
+{
+	struct test_helper_module *skel;
+	int retval, err;
+
+	if (!env.has_testmod) {
+		test__skip();
+		return;
+	}
+
+	skel = test_helper_module__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "test_helper_module__open"))
+		return;
+	err = bpf_prog_test_run(bpf_program__fd(skel->progs.load), 1, &pkt_v4,
+				sizeof(pkt_v4), NULL, NULL, (__u32 *)&retval, NULL);
+	if (!ASSERT_OK(err, "bpf_prog_test_run"))
+		goto cleanup;
+	ASSERT_EQ(retval, 7, "retval");
+cleanup:
+	test_helper_module__destroy(skel);
+}
+
+void test_helper_module(void)
+{
+	if (test__start_subtest("lskel"))
+		test_helper_module_lskel();
+	if (test__start_subtest("libbpf"))
+		test_helper_module_libbpf();
+}
diff --git a/tools/testing/selftests/bpf/progs/test_helper_module.c b/tools/testing/selftests/bpf/progs/test_helper_module.c
new file mode 100644
index 000000000000..66dadd317498
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_helper_module.c
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+extern int bpf_helper_print_add(int *a, int *b) __ksym;
+
+SEC("tc")
+int load(struct __sk_buff *skb)
+{
+	int a, b;
+
+	a = 3;
+	b = 4;
+	return bpf_helper_print_add(&a, &b);
+}
+
+char LICENSE[] SEC("license") = "GPL";
-- 
2.25.1




[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux