[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20200408232539.2676695-1-yhs@fb.com>
Date: Wed, 8 Apr 2020 16:25:39 -0700
From: Yonghong Song <yhs@...com>
To: Andrii Nakryiko <andriin@...com>, <bpf@...r.kernel.org>,
Martin KaFai Lau <kafai@...com>, <netdev@...r.kernel.org>
CC: Alexei Starovoitov <ast@...com>,
Daniel Borkmann <daniel@...earbox.net>, <kernel-team@...com>
Subject: [RFC PATCH bpf-next 16/16] tools/bpf: selftests: add a selftest for anonymous dumper
The selftest creates a anonymous dumper for the
/sys/kernel/bpfdump/task/ target and ensure the
user space got the expected contents. Both
bpf_seq_printf() and bpf_seq_write() helpers
are tested in this selftest.
$ test_progs -n 2
#2 bpfdump_test:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
Signed-off-by: Yonghong Song <yhs@...com>
---
.../selftests/bpf/prog_tests/bpfdump_test.c | 41 +++++++++++++++++++
.../selftests/bpf/progs/bpfdump_test_kern.c | 26 ++++++++++++
2 files changed, 67 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/bpfdump_test.c
create mode 100644 tools/testing/selftests/bpf/progs/bpfdump_test_kern.c
diff --git a/tools/testing/selftests/bpf/prog_tests/bpfdump_test.c b/tools/testing/selftests/bpf/prog_tests/bpfdump_test.c
new file mode 100644
index 000000000000..a04fae7f1e3d
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/bpfdump_test.c
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include "bpfdump_test_kern.skel.h"
+
+void test_bpfdump_test(void)
+{
+ int err, prog_fd, dumper_fd, duration = 0;
+ struct bpfdump_test_kern *skel;
+ char buf[16] = {};
+ const char *expected = "0A1B2C3D";
+
+ skel = bpfdump_test_kern__open_and_load();
+ if (CHECK(!skel, "skel_open_and_load",
+ "skeleton open_and_load failed\n"))
+ return;
+
+ prog_fd = bpf_program__fd(skel->progs.dump_tasks);
+ dumper_fd = bpf_prog_attach(prog_fd, 0, BPF_TRACE_DUMP, 0);
+ if (CHECK(dumper_fd < 0, "bpf_prog_attach", "attach dumper failed\n"))
+ goto destroy_skel;
+
+ err = -EINVAL;
+ while (read(dumper_fd, buf, sizeof(buf)) > 0) {
+ if (CHECK(!err, "read", "unexpected extra read\n"))
+ goto close_fd;
+
+ err = strcmp(buf, expected) != 0;
+ if (CHECK(err, "read",
+ "read failed: buf %s, expected %s\n", buf,
+ expected))
+ goto close_fd;
+ }
+
+ CHECK(err, "read", "real failed: no read, expected %s\n",
+ expected);
+
+close_fd:
+ close(dumper_fd);
+destroy_skel:
+ bpfdump_test_kern__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/bpfdump_test_kern.c b/tools/testing/selftests/bpf/progs/bpfdump_test_kern.c
new file mode 100644
index 000000000000..4758f5d11d9c
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpfdump_test_kern.c
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_endian.h>
+
+char _license[] SEC("license") = "GPL";
+
+int count = 0;
+
+SEC("dump//sys/kernel/bpfdump/task")
+int BPF_PROG(dump_tasks, struct task_struct *task, struct seq_file *seq, u64 seq_num)
+{
+ static char fmt[] = "%d";
+ char c;
+
+ if (count < 4) {
+ bpf_seq_printf(seq, fmt, sizeof(fmt), count);
+ c = 'A' + count;
+ bpf_seq_write(seq, &c, sizeof(c));
+ count++;
+ }
+
+ return 0;
+}
--
2.24.1
Powered by blists - more mailing lists