selftests/bpf: Add 6-argument syscall tracing test
authorAndrii Nakryiko <andrii@kernel.org>
Fri, 20 Jan 2023 20:09:13 +0000 (12:09 -0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Mon, 23 Jan 2023 19:53:01 +0000 (20:53 +0100)
Turns out splice() is one of the syscalls that's using current maximum
number of arguments (six). This is perfect for testing, so extend
bpf_syscall_macro selftest to also trace splice() syscall, using
BPF_KSYSCALL() macro. This makes sure all the syscall argument register
definitions are correct.

Suggested-by: Ilya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Tested-by: Alan Maguire <alan.maguire@oracle.com> # arm64
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com> # s390x
Link: https://lore.kernel.org/bpf/20230120200914.3008030-25-andrii@kernel.org
tools/testing/selftests/bpf/prog_tests/test_bpf_syscall_macro.c
tools/testing/selftests/bpf/progs/bpf_syscall_macro.c

index c381faa..2900c5e 100644 (file)
@@ -1,5 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright 2022 Sony Group Corporation */
+#define _GNU_SOURCE
+#include <fcntl.h>
 #include <sys/prctl.h>
 #include <test_progs.h>
 #include "bpf_syscall_macro.skel.h"
@@ -13,6 +15,8 @@ void test_bpf_syscall_macro(void)
        unsigned long exp_arg3 = 13;
        unsigned long exp_arg4 = 14;
        unsigned long exp_arg5 = 15;
+       loff_t off_in, off_out;
+       ssize_t r;
 
        /* check whether it can open program */
        skel = bpf_syscall_macro__open();
@@ -33,6 +37,7 @@ void test_bpf_syscall_macro(void)
 
        /* check whether args of syscall are copied correctly */
        prctl(exp_arg1, exp_arg2, exp_arg3, exp_arg4, exp_arg5);
+
 #if defined(__aarch64__) || defined(__s390__)
        ASSERT_NEQ(skel->bss->arg1, exp_arg1, "syscall_arg1");
 #else
@@ -68,6 +73,18 @@ void test_bpf_syscall_macro(void)
        ASSERT_EQ(skel->bss->arg4_syscall, exp_arg4, "BPF_KPROBE_SYSCALL_arg4");
        ASSERT_EQ(skel->bss->arg5_syscall, exp_arg5, "BPF_KPROBE_SYSCALL_arg5");
 
+       r = splice(-42, &off_in, 42, &off_out, 0x12340000, SPLICE_F_NONBLOCK);
+       err = -errno;
+       ASSERT_EQ(r, -1, "splice_res");
+       ASSERT_EQ(err, -EBADF, "splice_err");
+
+       ASSERT_EQ(skel->bss->splice_fd_in, -42, "splice_arg1");
+       ASSERT_EQ(skel->bss->splice_off_in, (__u64)&off_in, "splice_arg2");
+       ASSERT_EQ(skel->bss->splice_fd_out, 42, "splice_arg3");
+       ASSERT_EQ(skel->bss->splice_off_out, (__u64)&off_out, "splice_arg4");
+       ASSERT_EQ(skel->bss->splice_len, 0x12340000, "splice_arg5");
+       ASSERT_EQ(skel->bss->splice_flags, SPLICE_F_NONBLOCK, "splice_arg6");
+
 cleanup:
        bpf_syscall_macro__destroy(skel);
 }
index e1e1189..1a476d8 100644 (file)
@@ -81,4 +81,30 @@ int BPF_KSYSCALL(prctl_enter, int option, unsigned long arg2,
        return 0;
 }
 
+__u64 splice_fd_in;
+__u64 splice_off_in;
+__u64 splice_fd_out;
+__u64 splice_off_out;
+__u64 splice_len;
+__u64 splice_flags;
+
+SEC("ksyscall/splice")
+int BPF_KSYSCALL(splice_enter, int fd_in, loff_t *off_in, int fd_out,
+                loff_t *off_out, size_t len, unsigned int flags)
+{
+       pid_t pid = bpf_get_current_pid_tgid() >> 32;
+
+       if (pid != filter_pid)
+               return 0;
+
+       splice_fd_in = fd_in;
+       splice_off_in = (__u64)off_in;
+       splice_fd_out = fd_out;
+       splice_off_out = (__u64)off_out;
+       splice_len = len;
+       splice_flags = flags;
+
+       return 0;
+}
+
 char _license[] SEC("license") = "GPL";