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 12/16] tools/libbpf: libbpf support for bpfdump
Date: Wed, 8 Apr 2020 16:25:34 -0700 [thread overview]
Message-ID: <20200408232534.2676393-1-yhs@fb.com> (raw)
In-Reply-To: <20200408232520.2675265-1-yhs@fb.com>
Add a few libbpf APIs for bpfdump pin and query.
Also, parse the dump program section name,
retrieve the dump target path and open the path
to get a fd and assignment to prog->attach_prog_fd.
This is not really desirable, and need to think
more how to have equally better user interface
and cope with libbpf well.
Signed-off-by: Yonghong Song <yhs@fb.com>
---
tools/lib/bpf/bpf.c | 33 +++++++++++++++++++++++++++
tools/lib/bpf/bpf.h | 5 +++++
tools/lib/bpf/libbpf.c | 48 ++++++++++++++++++++++++++++++++++++----
tools/lib/bpf/libbpf.h | 1 +
tools/lib/bpf/libbpf.map | 3 +++
5 files changed, 86 insertions(+), 4 deletions(-)
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 5cc1b0785d18..e8d4304fcc98 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -533,6 +533,39 @@ int bpf_obj_get(const char *pathname)
return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
}
+int bpf_obj_pin_dumper(int fd, const char *dname)
+{
+ union bpf_attr attr;
+
+ memset(&attr, 0, sizeof(attr));
+ attr.dumper_name = ptr_to_u64((void *)dname);
+ attr.bpf_fd = fd;
+ attr.file_flags = BPF_F_DUMP;
+
+ return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
+}
+
+int bpf_dump_query(int query_fd, __u32 flags, void *target_proto_buf,
+ __u32 buf_len, __u32 *prog_id)
+{
+ union bpf_attr attr;
+ int ret;
+
+ memset(&attr, 0, sizeof(attr));
+ attr.dump_query.query_fd = query_fd;
+ attr.dump_query.flags = flags;
+ if (target_proto_buf) {
+ attr.dump_query.target_proto = ptr_to_u64((void *)target_proto_buf);
+ attr.dump_query.proto_buf_len = buf_len;
+ }
+
+ ret = sys_bpf(BPF_DUMP_QUERY, &attr, sizeof(attr));
+ if (!ret && prog_id)
+ *prog_id = attr.dump_query.prog_id;
+
+ return ret;
+}
+
int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
unsigned int flags)
{
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 46d47afdd887..2f89f8445962 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -149,8 +149,13 @@ LIBBPF_API int bpf_map_update_batch(int fd, void *keys, void *values,
__u32 *count,
const struct bpf_map_batch_opts *opts);
+LIBBPF_API int bpf_dump_query(int query_fd, __u32 flags,
+ void *target_proto_buf, __u32 buf_len,
+ __u32 *prog_id);
+
LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_pin_dumper(int fd, const char *dname);
struct bpf_prog_attach_opts {
size_t sz; /* size of this struct for forward/backward compatibility */
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index ff9174282a8c..c7a81ede56ce 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -79,6 +79,7 @@ static struct bpf_program *bpf_object__find_prog_by_idx(struct bpf_object *obj,
int idx);
static const struct btf_type *
skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id);
+static int fill_dumper_info(struct bpf_program *prog);
static int __base_pr(enum libbpf_print_level level, const char *format,
va_list args)
@@ -2365,8 +2366,12 @@ static inline bool libbpf_prog_needs_vmlinux_btf(struct bpf_program *prog)
/* BPF_PROG_TYPE_TRACING programs which do not attach to other programs
* also need vmlinux BTF
*/
- if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd)
- return true;
+ if (prog->type == BPF_PROG_TYPE_TRACING) {
+ if (prog->expected_attach_type == BPF_TRACE_DUMP)
+ return false;
+ if (!prog->attach_prog_fd)
+ return true;
+ }
return false;
}
@@ -4958,7 +4963,7 @@ int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
{
int err = 0, fd, i, btf_id;
- if ((prog->type == BPF_PROG_TYPE_TRACING ||
+ if (((prog->type == BPF_PROG_TYPE_TRACING && prog->expected_attach_type != BPF_TRACE_DUMP) ||
prog->type == BPF_PROG_TYPE_LSM ||
prog->type == BPF_PROG_TYPE_EXT) && !prog->attach_btf_id) {
btf_id = libbpf_find_attach_btf_id(prog);
@@ -5319,6 +5324,7 @@ static int bpf_object__resolve_externs(struct bpf_object *obj,
int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
{
+ struct bpf_program *prog;
struct bpf_object *obj;
int err, i;
@@ -5335,7 +5341,17 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
obj->loaded = true;
- err = bpf_object__probe_caps(obj);
+ err = 0;
+ bpf_object__for_each_program(prog, obj) {
+ if (prog->type == BPF_PROG_TYPE_TRACING &&
+ prog->expected_attach_type == BPF_TRACE_DUMP) {
+ err = fill_dumper_info(prog);
+ if (err)
+ break;
+ }
+ }
+
+ err = err ? : bpf_object__probe_caps(obj);
err = err ? : bpf_object__resolve_externs(obj, obj->kconfig);
err = err ? : bpf_object__sanitize_and_load_btf(obj);
err = err ? : bpf_object__sanitize_maps(obj);
@@ -5459,6 +5475,11 @@ int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
return 0;
}
+int bpf_program__pin_dumper(struct bpf_program *prog, const char *dname)
+{
+ return bpf_obj_pin_dumper(bpf_program__fd(prog), dname);
+}
+
int bpf_program__unpin_instance(struct bpf_program *prog, const char *path,
int instance)
{
@@ -6322,6 +6343,8 @@ static const struct bpf_sec_def section_defs[] = {
.is_attach_btf = true,
.expected_attach_type = BPF_LSM_MAC,
.attach_fn = attach_lsm),
+ SEC_DEF("dump/", TRACING,
+ .expected_attach_type = BPF_TRACE_DUMP),
BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP),
BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT),
BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN),
@@ -6401,6 +6424,23 @@ static const struct bpf_sec_def *find_sec_def(const char *sec_name)
return NULL;
}
+static int fill_dumper_info(struct bpf_program *prog)
+{
+ const struct bpf_sec_def *sec;
+ const char *dump_target;
+ int fd;
+
+ sec = find_sec_def(bpf_program__title(prog, false));
+ if (sec) {
+ dump_target = bpf_program__title(prog, false) + sec->len;
+ fd = open(dump_target, O_RDONLY);
+ if (fd < 0)
+ return fd;
+ prog->attach_prog_fd = fd;
+ }
+ return 0;
+}
+
static char *libbpf_get_type_names(bool attach_type)
{
int i, len = ARRAY_SIZE(section_defs) * MAX_TYPE_NAME_SIZE;
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 44df1d3e7287..ccb5d30fff4a 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -216,6 +216,7 @@ LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
+LIBBPF_API int bpf_program__pin_dumper(struct bpf_program *prog, const char *dname);
struct bpf_link;
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index bb8831605b25..ed6234bb199f 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -238,6 +238,7 @@ LIBBPF_0.0.7 {
LIBBPF_0.0.8 {
global:
+ bpf_dump_query;
bpf_link__fd;
bpf_link__open;
bpf_link__pin;
@@ -248,8 +249,10 @@ LIBBPF_0.0.8 {
bpf_link_update;
bpf_map__set_initial_value;
bpf_program__attach_cgroup;
+ bpf_obj_pin_dumper;
bpf_program__attach_lsm;
bpf_program__is_lsm;
+ bpf_program__pin_dumper;
bpf_program__set_attach_target;
bpf_program__set_lsm;
bpf_set_link_xdp_fd_opts;
--
2.24.1
next prev parent reply other threads:[~2020-04-08 23:26 UTC|newest]
Thread overview: 71+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-08 23:25 [RFC PATCH bpf-next 00/16] bpf: implement bpf based dumping of kernel data structures Yonghong Song
2020-04-08 23:25 ` [RFC PATCH bpf-next 01/16] net: refactor net assignment for seq_net_private structure Yonghong Song
2020-04-08 23:25 ` [RFC PATCH bpf-next 02/16] bpf: create /sys/kernel/bpfdump mount file system Yonghong Song
2020-04-08 23:25 ` [RFC PATCH bpf-next 03/16] bpf: provide a way for targets to register themselves Yonghong Song
2020-04-10 22:18 ` Andrii Nakryiko
2020-04-10 23:24 ` Yonghong Song
2020-04-13 19:31 ` Andrii Nakryiko
2020-04-15 22:57 ` Yonghong Song
2020-04-10 22:25 ` Andrii Nakryiko
2020-04-10 23:25 ` Yonghong Song
2020-04-08 23:25 ` [RFC PATCH bpf-next 04/16] bpf: allow loading of a dumper program Yonghong Song
2020-04-10 22:36 ` Andrii Nakryiko
2020-04-10 23:28 ` Yonghong Song
2020-04-13 19:33 ` Andrii Nakryiko
2020-04-08 23:25 ` [RFC PATCH bpf-next 05/16] bpf: create file or anonymous dumpers Yonghong Song
2020-04-10 3:00 ` Alexei Starovoitov
2020-04-10 6:09 ` Yonghong Song
2020-04-10 22:42 ` Yonghong Song
2020-04-10 22:53 ` Andrii Nakryiko
2020-04-10 23:47 ` Yonghong Song
2020-04-11 23:11 ` Alexei Starovoitov
2020-04-12 6:51 ` Yonghong Song
2020-04-13 20:48 ` Andrii Nakryiko
2020-04-10 22:51 ` Andrii Nakryiko
2020-04-10 23:41 ` Yonghong Song
2020-04-13 19:45 ` Andrii Nakryiko
2020-04-10 23:25 ` Andrii Nakryiko
2020-04-11 0:23 ` Yonghong Song
2020-04-11 23:17 ` Alexei Starovoitov
2020-04-13 21:04 ` Andrii Nakryiko
2020-04-13 19:59 ` Andrii Nakryiko
2020-04-14 5:56 ` Andrii Nakryiko
2020-04-14 23:59 ` Yonghong Song
2020-04-15 4:45 ` Andrii Nakryiko
2020-04-15 16:46 ` Alexei Starovoitov
2020-04-16 1:48 ` Andrii Nakryiko
2020-04-16 7:15 ` Yonghong Song
2020-04-16 17:04 ` Alexei Starovoitov
2020-04-16 19:35 ` Andrii Nakryiko
2020-04-16 23:18 ` Alexei Starovoitov
2020-04-17 5:11 ` Andrii Nakryiko
2020-04-19 6:11 ` Yonghong Song
2020-04-08 23:25 ` [RFC PATCH bpf-next 06/16] bpf: add netlink and ipv6_route targets Yonghong Song
2020-04-10 23:13 ` Andrii Nakryiko
2020-04-10 23:52 ` Yonghong Song
2020-04-08 23:25 ` [RFC PATCH bpf-next 07/16] bpf: add bpf_map target Yonghong Song
2020-04-13 22:18 ` Andrii Nakryiko
2020-04-13 22:47 ` Andrii Nakryiko
2020-04-08 23:25 ` [RFC PATCH bpf-next 08/16] bpf: add task and task/file targets Yonghong Song
2020-04-10 3:22 ` Alexei Starovoitov
2020-04-10 6:19 ` Yonghong Song
2020-04-10 21:31 ` Alexei Starovoitov
2020-04-10 21:33 ` Alexei Starovoitov
2020-04-13 23:00 ` Andrii Nakryiko
2020-04-08 23:25 ` [RFC PATCH bpf-next 09/16] bpf: add bpf_seq_printf and bpf_seq_write helpers Yonghong Song
2020-04-10 3:26 ` Alexei Starovoitov
2020-04-10 6:12 ` Yonghong Song
2020-04-14 5:28 ` Andrii Nakryiko
2020-04-08 23:25 ` [RFC PATCH bpf-next 10/16] bpf: support variable length array in tracing programs Yonghong Song
2020-04-14 0:13 ` Andrii Nakryiko
2020-04-08 23:25 ` [RFC PATCH bpf-next 11/16] bpf: implement query for target_proto and file dumper prog_id Yonghong Song
2020-04-10 3:10 ` Alexei Starovoitov
2020-04-10 6:11 ` Yonghong Song
2020-04-08 23:25 ` Yonghong Song [this message]
2020-04-08 23:25 ` [RFC PATCH bpf-next 13/16] tools/bpftool: add bpf dumper support Yonghong Song
2020-04-08 23:25 ` [RFC PATCH bpf-next 14/16] tools/bpf: selftests: add dumper programs for ipv6_route and netlink Yonghong Song
2020-04-14 5:39 ` Andrii Nakryiko
2020-04-08 23:25 ` [RFC PATCH bpf-next 15/16] tools/bpf: selftests: add dumper progs for bpf_map/task/task_file Yonghong Song
2020-04-10 3:33 ` Alexei Starovoitov
2020-04-10 6:41 ` Yonghong Song
2020-04-08 23:25 ` [RFC PATCH bpf-next 16/16] tools/bpf: selftests: add a selftest for anonymous dumper 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=20200408232534.2676393-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