From: Hao Luo <haoluo@google.com>
To: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>
Cc: Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
Yonghong Song <yhs@fb.com>, KP Singh <kpsingh@kernel.org>,
Shakeel Butt <shakeelb@google.com>,
Joe Burton <jevburton.kernel@gmail.com>,
Tejun Heo <tj@kernel.org>,
joshdon@google.com, sdf@google.com, bpf@vger.kernel.org,
linux-kernel@vger.kernel.org, Hao Luo <haoluo@google.com>
Subject: [PATCH bpf-next v1 3/9] selftests/bpf: tests mkdir, rmdir, unlink and pin in syscall
Date: Fri, 25 Feb 2022 15:43:33 -0800 [thread overview]
Message-ID: <20220225234339.2386398-4-haoluo@google.com> (raw)
In-Reply-To: <20220225234339.2386398-1-haoluo@google.com>
Add a subtest in syscall to test bpf_mkdir(), bpf_rmdir(),
bpf_unlink() and object pinning in syscall prog.
Signed-off-by: Hao Luo <haoluo@google.com>
---
.../selftests/bpf/prog_tests/syscall.c | 67 +++++++++++++++++-
.../testing/selftests/bpf/progs/syscall_fs.c | 69 +++++++++++++++++++
2 files changed, 135 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/bpf/progs/syscall_fs.c
diff --git a/tools/testing/selftests/bpf/prog_tests/syscall.c b/tools/testing/selftests/bpf/prog_tests/syscall.c
index f4d40001155a..782b5fe73096 100644
--- a/tools/testing/selftests/bpf/prog_tests/syscall.c
+++ b/tools/testing/selftests/bpf/prog_tests/syscall.c
@@ -1,7 +1,9 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Facebook */
+#include <sys/stat.h>
#include <test_progs.h>
#include "syscall.skel.h"
+#include "syscall_fs.skel.h"
struct args {
__u64 log_buf;
@@ -12,7 +14,7 @@ struct args {
int btf_fd;
};
-void test_syscall(void)
+static void test_syscall_basic(void)
{
static char verifier_log[8192];
struct args ctx = {
@@ -53,3 +55,66 @@ void test_syscall(void)
if (ctx.btf_fd > 0)
close(ctx.btf_fd);
}
+
+static void test_syscall_fs(void)
+{
+ char tmpl[] = "/sys/fs/bpf/syscall_XXXXXX";
+ struct stat statbuf = {};
+ static char verifier_log[8192];
+ struct args ctx = {
+ .log_buf = (uintptr_t) verifier_log,
+ .log_size = sizeof(verifier_log),
+ .prog_fd = 0,
+ };
+ LIBBPF_OPTS(bpf_test_run_opts, tattr,
+ .ctx_in = &ctx,
+ .ctx_size_in = sizeof(ctx),
+ );
+ struct syscall_fs *skel = NULL;
+ int err, mkdir_fd, rmdir_fd;
+ char *root, *dir, *path;
+
+ /* prepares test directories */
+ system("mount -t bpf bpffs /sys/fs/bpf");
+ root = mkdtemp(tmpl);
+ chmod(root, 0755);
+
+ /* loads prog */
+ skel = syscall_fs__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "skel_load"))
+ goto cleanup;
+
+ dir = skel->bss->dirname;
+ snprintf(dir, sizeof(skel->bss->dirname), "%s/test", root);
+ path = skel->bss->pathname;
+ snprintf(path, sizeof(skel->bss->pathname), "%s/prog", dir);
+
+ /* tests mkdir */
+ mkdir_fd = bpf_program__fd(skel->progs.mkdir_prog);
+ err = bpf_prog_test_run_opts(mkdir_fd, &tattr);
+ ASSERT_EQ(err, 0, "mkdir_err");
+ ASSERT_EQ(tattr.retval, 0, "mkdir_retval");
+ ASSERT_OK(stat(dir, &statbuf), "mkdir_success");
+ ASSERT_OK(stat(path, &statbuf), "pin_success");
+
+ /* tests rmdir */
+ rmdir_fd = bpf_program__fd(skel->progs.rmdir_prog);
+ err = bpf_prog_test_run_opts(rmdir_fd, &tattr);
+ ASSERT_EQ(err, 0, "rmdir_err");
+ ASSERT_EQ(tattr.retval, 0, "rmdir_retval");
+ ASSERT_ERR(stat(path, &statbuf), "unlink_success");
+ ASSERT_ERR(stat(dir, &statbuf), "rmdir_success");
+
+cleanup:
+ syscall_fs__destroy(skel);
+ if (ctx.prog_fd > 0)
+ close(ctx.prog_fd);
+ rmdir(root);
+}
+
+void test_syscall(void) {
+ if (test__start_subtest("basic"))
+ test_syscall_basic();
+ if (test__start_subtest("filesystem"))
+ test_syscall_fs();
+}
diff --git a/tools/testing/selftests/bpf/progs/syscall_fs.c b/tools/testing/selftests/bpf/progs/syscall_fs.c
new file mode 100644
index 000000000000..9418d1364c09
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/syscall_fs.c
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2022 Google */
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <../../../tools/include/linux/filter.h>
+
+char _license[] SEC("license") = "GPL";
+
+struct args {
+ __u64 log_buf;
+ __u32 log_size;
+ int max_entries;
+ int map_fd;
+ int prog_fd;
+ int btf_fd;
+};
+
+char dirname[64];
+char pathname[64];
+
+SEC("syscall")
+int mkdir_prog(struct args *ctx)
+{
+ static char license[] = "GPL";
+ static struct bpf_insn insns[] = {
+ BPF_MOV64_IMM(BPF_REG_0, 0),
+ BPF_EXIT_INSN(),
+ };
+ static union bpf_attr load_attr = {
+ .prog_type = BPF_PROG_TYPE_XDP,
+ .insn_cnt = sizeof(insns) / sizeof(insns[0]),
+ };
+ static union bpf_attr pin_attr = {
+ .file_flags = 0,
+ };
+ int ret;
+
+ ret = bpf_mkdir(dirname, sizeof(dirname), 0644);
+ if (ret)
+ return ret;
+
+ load_attr.license = (long) license;
+ load_attr.insns = (long) insns;
+ load_attr.log_buf = ctx->log_buf;
+ load_attr.log_size = ctx->log_size;
+ load_attr.log_level = 1;
+ ret = bpf_sys_bpf(BPF_PROG_LOAD, &load_attr, sizeof(load_attr));
+ if (ret < 0)
+ return ret;
+ else if (ret == 0)
+ return -1;
+ ctx->prog_fd = ret;
+
+ pin_attr.pathname = (__u64)pathname;
+ pin_attr.bpf_fd = ret;
+ return bpf_sys_bpf(BPF_OBJ_PIN, &pin_attr, sizeof(pin_attr));
+}
+
+SEC("syscall")
+int rmdir_prog(struct args *ctx)
+{
+ int ret;
+
+ ret = bpf_unlink(pathname, sizeof(pathname));
+ if (ret)
+ return ret;
+
+ return bpf_rmdir(dirname, sizeof(dirname));
+}
--
2.35.1.574.g5d30c73bfb-goog
next prev parent reply other threads:[~2022-02-25 23:44 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-25 23:43 [PATCH bpf-next v1 0/9] Extend cgroup interface with bpf Hao Luo
2022-02-25 23:43 ` [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall Hao Luo
2022-02-27 5:18 ` Kumar Kartikeya Dwivedi
2022-02-28 22:10 ` Hao Luo
2022-03-02 19:34 ` Alexei Starovoitov
2022-03-03 18:50 ` Hao Luo
2022-03-04 18:37 ` Hao Luo
2022-03-05 23:47 ` Alexei Starovoitov
2022-03-08 21:08 ` Hao Luo
2022-03-02 20:55 ` Yonghong Song
2022-03-03 18:56 ` Hao Luo
2022-03-03 19:13 ` Yonghong Song
2022-03-03 19:15 ` Hao Luo
2022-03-12 3:46 ` Al Viro
2022-03-14 17:07 ` Hao Luo
2022-03-14 23:10 ` Al Viro
2022-03-15 17:27 ` Hao Luo
2022-03-15 18:59 ` Alexei Starovoitov
2022-03-15 19:03 ` Alexei Starovoitov
2022-03-15 19:00 ` Al Viro
2022-03-15 19:47 ` Hao Luo
2022-02-25 23:43 ` [PATCH bpf-next v1 2/9] bpf: Add BPF_OBJ_PIN and BPF_OBJ_GET in the bpf_sys_bpf helper Hao Luo
2022-02-25 23:43 ` Hao Luo [this message]
2022-02-25 23:43 ` [PATCH bpf-next v1 4/9] bpf: Introduce sleepable tracepoints Hao Luo
2022-03-02 19:41 ` Alexei Starovoitov
2022-03-03 19:37 ` Hao Luo
2022-03-03 19:59 ` Alexei Starovoitov
2022-03-02 21:23 ` Yonghong Song
2022-03-02 21:30 ` Alexei Starovoitov
2022-03-03 1:08 ` Yonghong Song
2022-03-03 2:29 ` Alexei Starovoitov
2022-03-03 19:43 ` Hao Luo
2022-03-03 20:02 ` Alexei Starovoitov
2022-03-03 20:04 ` Alexei Starovoitov
2022-03-03 22:06 ` Hao Luo
2022-02-25 23:43 ` [PATCH bpf-next v1 5/9] cgroup: Sleepable cgroup tracepoints Hao Luo
2022-02-25 23:43 ` [PATCH bpf-next v1 6/9] libbpf: Add sleepable tp_btf Hao Luo
2022-02-25 23:43 ` [PATCH bpf-next v1 7/9] bpf: Lift permission check in __sys_bpf when called from kernel Hao Luo
2022-03-02 20:01 ` Alexei Starovoitov
2022-03-03 19:14 ` Hao Luo
2022-02-25 23:43 ` [PATCH bpf-next v1 8/9] bpf: Introduce cgroup iter Hao Luo
2022-02-26 2:32 ` kernel test robot
2022-02-26 2:32 ` kernel test robot
2022-02-26 2:53 ` kernel test robot
2022-03-02 21:59 ` Yonghong Song
2022-03-03 20:02 ` Hao Luo
2022-03-02 22:45 ` Kumar Kartikeya Dwivedi
2022-03-03 2:03 ` Yonghong Song
2022-03-03 3:03 ` Kumar Kartikeya Dwivedi
2022-03-03 4:00 ` Alexei Starovoitov
2022-03-03 7:33 ` Yonghong Song
2022-03-03 8:13 ` Kumar Kartikeya Dwivedi
2022-03-03 21:52 ` Hao Luo
2022-02-25 23:43 ` [PATCH bpf-next v1 9/9] selftests/bpf: Tests using sleepable tracepoints to monitor cgroup events Hao Luo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220225234339.2386398-4-haoluo@google.com \
--to=haoluo@google.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=jevburton.kernel@gmail.com \
--cc=joshdon@google.com \
--cc=kafai@fb.com \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=sdf@google.com \
--cc=shakeelb@google.com \
--cc=songliubraving@fb.com \
--cc=tj@kernel.org \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).