[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20200415192800.4084266-1-yhs@fb.com>
Date: Wed, 15 Apr 2020 12:28:00 -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 v2 17/17] 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 | 42 +++++++++++++++++++
.../selftests/bpf/progs/bpfdump_test_kern.c | 31 ++++++++++++++
2 files changed, 73 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..8978e04c3ca9
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/bpfdump_test.c
@@ -0,0 +1,42 @@
+// 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_raw_tracepoint_open(NULL, prog_fd);
+ if (CHECK(dumper_fd < 0, "bpf_raw_tracepoint_open",
+ "anonymous dumper creation 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..f6bd61a75a22
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpfdump_test_kern.c
@@ -0,0 +1,31 @@
+// 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 dump_tasks(struct bpfdump__task *ctx)
+{
+ struct seq_file *seq = ctx->meta->seq;
+ struct task_struct *task = ctx->task;
+ static char fmt[] = "%d";
+ char c;
+
+ if (task == (void *)0)
+ return 0;
+
+ 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