diff mbox series

[bpf-next,23/29] selftests/bpf: Add bpf_arg/bpf_ret_value test

Message ID 20211118112455.475349-24-jolsa@kernel.org (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series bpf: Add batch support for attaching trampolines | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next fail VM_Test
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count fail Series longer than 15 patches (and no cover letter)
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 3 maintainers not CCed: shuah@kernel.org kpsingh@kernel.org linux-kselftest@vger.kernel.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning CHECK: No space is necessary after a cast WARNING: From:/Signed-off-by: email address mismatch: 'From: Jiri Olsa <jolsa@redhat.com>' != 'Signed-off-by: Jiri Olsa <jolsa@kernel.org>' WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Jiri Olsa Nov. 18, 2021, 11:24 a.m. UTC
Adding tests for bpf_arg/bpf_ret_value helpers on
both fentry and fexit programs.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 .../selftests/bpf/prog_tests/args_test.c      | 34 +++++++++++++++++++
 tools/testing/selftests/bpf/progs/args_test.c | 30 ++++++++++++++++
 2 files changed, 64 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/args_test.c
 create mode 100644 tools/testing/selftests/bpf/progs/args_test.c
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/args_test.c b/tools/testing/selftests/bpf/prog_tests/args_test.c
new file mode 100644
index 000000000000..1938f2616ee9
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/args_test.c
@@ -0,0 +1,34 @@ 
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include "args_test.skel.h"
+
+void test_args_test(void)
+{
+	struct args_test *skel = NULL;
+	__u32 duration = 0, retval;
+	int err, prog_fd;
+
+	skel = args_test__open();
+	if (!ASSERT_OK_PTR(skel, "args_test__open"))
+		return;
+
+	err = args_test__load(skel);
+	if (!ASSERT_OK(err, "args_test__load"))
+		goto cleanup;
+
+	err = args_test__attach(skel);
+	if (!ASSERT_OK(err, "args_test__attach"))
+		goto cleanup;
+
+	prog_fd = bpf_program__fd(skel->progs.test1);
+	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
+				NULL, NULL, &retval, &duration);
+	ASSERT_OK(err, "test_run");
+	ASSERT_EQ(retval, 0, "test_run");
+
+	ASSERT_EQ(skel->bss->test1_result, 1, "test1_result");
+	ASSERT_EQ(skel->bss->test2_result, 1, "test2_result");
+
+cleanup:
+	args_test__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/args_test.c b/tools/testing/selftests/bpf/progs/args_test.c
new file mode 100644
index 000000000000..7fc8e9fb41bd
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/args_test.c
@@ -0,0 +1,30 @@ 
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+__u64 test1_result = 0;
+SEC("fentry/bpf_fentry_test1")
+int BPF_PROG(test1)
+{
+	__u64 a = bpf_arg(ctx, 0);
+	__u64 x = bpf_arg(ctx, 1);
+
+	test1_result = (int) a == 1 && x == 0;
+	return 0;
+}
+
+__u64 test2_result = 0;
+SEC("fexit/bpf_fentry_test2")
+int BPF_PROG(test2)
+{
+	__u64 ret = bpf_ret_value(ctx);
+	__u64 a = bpf_arg(ctx, 0);
+	__u64 b = bpf_arg(ctx, 1);
+	__u64 x = bpf_arg(ctx, 2);
+
+	test2_result = (int) a == 2 && b == 3 && ret == 5 && x == 0;
+	return 0;
+}