From: Yonghong Song <yhs@fb.com>
To: Andrii Nakryiko <andriin@fb.com>, <bpf@vger.kernel.org>,
Martin KaFai Lau <kafai@fb.com>, <netdev@vger.kernel.org>
Cc: Alexei Starovoitov <ast@fb.com>,
Daniel Borkmann <daniel@iogearbox.net>, <kernel-team@fb.com>
Subject: [RFC PATCH bpf-next v2 14/17] tools/bpftool: add bpf dumper support
Date: Wed, 15 Apr 2020 12:27:56 -0700 [thread overview]
Message-ID: <20200415192756.4083931-1-yhs@fb.com> (raw)
In-Reply-To: <20200415192740.4082659-1-yhs@fb.com>
Implemented "bpftool dumper" command. Two subcommands now:
bpftool dumper pin <bpf_prog.o> <dumper_name>
bpftool dumper show {target|dumper}
The "pin" subcommand will create a dumper with <dumper_name>
under the dump target (specified in <bpf_prog>.o).
The "show" subcommand will show bpf prog ctx type name , which
will be useful on how to write the bpf program, and
the "dumper" subcommand will further show the corresponding prog_id
for each dumper.
For example, with some of later selftest dumpers are pinned
in the kernel, we can do inspection like below:
$ bpftool dumper show target
target prog_ctx_type
task bpfdump__task
task/file bpfdump__task_file
bpf_map bpfdump__bpf_map
ipv6_route bpfdump__ipv6_route
netlink bpfdump__netlink
$ bpftool dumper show dumper
dumper prog_id prog_ctx_type
task/my1 8 bpfdump__task
task/file/my1 12 bpfdump__task_file
bpf_map/my1 4 bpfdump__bpf_map
ipv6_route/my2 16 bpfdump__ipv6_route
netlink/my2 24 bpfdump__ipv6_route
netlink/my3 20 bpfdump__netlink
Signed-off-by: Yonghong Song <yhs@fb.com>
---
tools/bpf/bpftool/dumper.c | 135 +++++++++++++++++++++++++++++++++++++
tools/bpf/bpftool/main.c | 3 +-
tools/bpf/bpftool/main.h | 1 +
3 files changed, 138 insertions(+), 1 deletion(-)
create mode 100644 tools/bpf/bpftool/dumper.c
diff --git a/tools/bpf/bpftool/dumper.c b/tools/bpf/bpftool/dumper.c
new file mode 100644
index 000000000000..46ca9d1d9a67
--- /dev/null
+++ b/tools/bpf/bpftool/dumper.c
@@ -0,0 +1,135 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+// Copyright (C) 2020 Facebook
+// Author: Yonghong Song <yhs@fb.com>
+
+#define _GNU_SOURCE
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <ftw.h>
+
+#include <linux/err.h>
+#include <bpf/bpf.h>
+#include <bpf/libbpf.h>
+
+#include "main.h"
+
+static int do_pin(int argc, char **argv)
+{
+ struct bpf_program *prog;
+ struct bpf_object *obj;
+ const char *objfile, *dname;
+ int err;
+
+ if (!REQ_ARGS(2)) {
+ usage();
+ return -1;
+ }
+
+ objfile = GET_ARG();
+ dname = GET_ARG();
+
+ obj = bpf_object__open(objfile);
+ if (IS_ERR_OR_NULL(obj))
+ return -1;
+
+ err = bpf_object__load(obj);
+ if (err < 0)
+ return -1;
+
+ prog = bpf_program__next(NULL, obj);
+ return bpf_dump__pin(prog, dname);
+}
+
+static bool for_targets;
+static const char *bpfdump_root = "/sys/kernel/bpfdump";
+
+static int check_file(const char *fpath, const struct stat *sb,
+ int typeflag, struct FTW *ftwbuf)
+{
+ char prog_ctx_cname_buf[64];
+ struct bpf_dump_info info;
+ unsigned info_len;
+ const char *name;
+ int ret, fd;
+
+ if ((for_targets && typeflag == FTW_F) ||
+ (!for_targets && typeflag == FTW_D))
+ return 0;
+
+ if (for_targets && strcmp(fpath, bpfdump_root) == 0)
+ return 0;
+
+ fd = open(fpath, O_RDONLY);
+ if (fd < 0)
+ return fd;
+
+ info_len = sizeof(info);
+ memset(&info, 0, info_len);
+ info.prog_ctx_type_name = ptr_to_u64(prog_ctx_cname_buf);
+ info.type_name_buf_len = sizeof(prog_ctx_cname_buf);
+ ret = bpf_obj_get_info_by_fd(fd, &info, &info_len);
+ if (ret < 0)
+ goto done;
+
+ name = fpath + strlen(bpfdump_root) + 1;
+ if (for_targets)
+ fprintf(stdout, "%-24s%-24s\n", name, prog_ctx_cname_buf);
+ else
+ fprintf(stdout, "%-24s%-10d%-24s\n", name, info.prog_id,
+ prog_ctx_cname_buf);
+
+done:
+ close(fd);
+ return ret;
+}
+
+static int do_show(int argc, char **argv)
+{
+ int flags = FTW_PHYS;
+ int nopenfd = 16;
+ const char *spec;
+
+ if (!REQ_ARGS(1)) {
+ usage();
+ return -1;
+ }
+
+ spec = GET_ARG();
+ if (strcmp(spec, "target") == 0) {
+ for_targets = true;
+ fprintf(stdout, "target prog_ctx_type\n");
+ } else if (strcmp(spec, "dumper") == 0) {
+ fprintf(stdout, "dumper prog_id prog_ctx_type\n");
+ for_targets = false;
+ } else {
+ return -1;
+ }
+
+ if (nftw(bpfdump_root, check_file, nopenfd, flags) == -1)
+ return -1;
+
+ return 0;
+}
+
+static int do_help(int argc, char **argv)
+{
+ return 0;
+}
+
+static const struct cmd cmds[] = {
+ { "help", do_help },
+ { "show", do_show },
+ { "pin", do_pin },
+ { 0 }
+};
+
+int do_dumper(int argc, char **argv)
+{
+ return cmd_select(cmds, argc, argv, do_help);
+}
diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index 466c269eabdd..8489aba6543d 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -58,7 +58,7 @@ static int do_help(int argc, char **argv)
" %s batch file FILE\n"
" %s version\n"
"\n"
- " OBJECT := { prog | map | cgroup | perf | net | feature | btf | gen | struct_ops }\n"
+ " OBJECT := { prog | map | cgroup | perf | net | feature | btf | gen | struct_ops | dumper}\n"
" " HELP_SPEC_OPTIONS "\n"
"",
bin_name, bin_name, bin_name);
@@ -222,6 +222,7 @@ static const struct cmd cmds[] = {
{ "btf", do_btf },
{ "gen", do_gen },
{ "struct_ops", do_struct_ops },
+ { "dumper", do_dumper },
{ "version", do_version },
{ 0 }
};
diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
index 86f14ce26fd7..2c59f319bbe9 100644
--- a/tools/bpf/bpftool/main.h
+++ b/tools/bpf/bpftool/main.h
@@ -162,6 +162,7 @@ int do_feature(int argc, char **argv);
int do_btf(int argc, char **argv);
int do_gen(int argc, char **argv);
int do_struct_ops(int argc, char **argv);
+int do_dumper(int argc, char **arg);
int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what);
int prog_parse_fd(int *argc, char ***argv);
--
2.24.1
next prev parent reply other threads:[~2020-04-15 19:30 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-15 19:27 [RFC PATCH bpf-next v2 00/17] bpf: implement bpf based dumping of kernel data structures Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 01/17] net: refactor net assignment for seq_net_private structure Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 02/17] bpf: create /sys/kernel/bpfdump mount file system Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 03/17] bpf: provide a way for targets to register themselves Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 04/17] bpf: allow loading of a dumper program Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 05/17] bpf: create file or anonymous dumpers Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 06/17] bpf: add PTR_TO_BTF_ID_OR_NULL support Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 07/17] bpf: add netlink and ipv6_route targets Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 08/17] bpf: add bpf_map target Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 09/17] bpf: add task and task/file targets Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 10/17] bpf: add bpf_seq_printf and bpf_seq_write helpers Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 11/17] bpf: support variable length array in tracing programs Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 12/17] bpf: implement query for target_proto and file dumper prog_id Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 13/17] tools/libbpf: libbpf support for bpfdump Yonghong Song
2020-04-15 19:27 ` Yonghong Song [this message]
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 15/17] tools/bpf: selftests: add dumper programs for ipv6_route and netlink Yonghong Song
2020-04-15 19:27 ` [RFC PATCH bpf-next v2 16/17] tools/bpf: selftests: add dumper progs for bpf_map/task/task_file Yonghong Song
2020-04-15 19:28 ` [RFC PATCH bpf-next v2 17/17] tools/bpf: selftests: add a selftest for anonymous dumper Yonghong Song
2020-04-16 2:23 ` [RFC PATCH bpf-next v2 00/17] bpf: implement bpf based dumping of kernel data structures David Ahern
2020-04-16 6:41 ` Yonghong Song
2020-04-17 15:02 ` Alan Maguire
2020-04-19 5:34 ` Yonghong Song
2020-04-17 10:54 ` Alan Maguire
2020-04-19 5:30 ` Yonghong Song
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=20200415192756.4083931-1-yhs@fb.com \
--to=yhs@fb.com \
--cc=andriin@fb.com \
--cc=ast@fb.com \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kafai@fb.com \
--cc=kernel-team@fb.com \
--cc=netdev@vger.kernel.org \
/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).