From: Lorenzo Fontana <fontanalorenz@gmail.com>
To: bpf@vger.kernel.org
Cc: ast@kernel.org, daniel@iogearbox.net
Subject: [PATCH bpf-next 1/2] tools/lib/bpf: bpf_program__insns allow to retrieve insns in libbpf
Date: Tue, 13 Jul 2021 20:33:04 +0200 [thread overview]
Message-ID: <aa97c776-9a82-9acc-fb13-dd082fdcaa61@gmail.com> (raw)
This allows consumers of libbpf to iterate trough the insns
of a program without loading it first directly after the ELF parsing.
Being able to do that is useful to create tooling that can show
the structure of a BPF program using libbpf without having to
parse the ELF separately.
Usage:
struct bpf_insn *insn;
insn = bpf_program__insns(prog);
Signed-off-by: Lorenzo Fontana <fontanalorenz@gmail.com>
---
tools/lib/bpf/libbpf.c | 5 +++++
tools/lib/bpf/libbpf.h | 1 +
2 files changed, 6 insertions(+)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 1e04ce724240..67d51531f6b6 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -8866,6 +8866,11 @@ void *bpf_program__priv(const struct bpf_program *prog)
return prog ? prog->priv : libbpf_err_ptr(-EINVAL);
}
+struct bpf_insn *bpf_program__insns(const struct bpf_program *prog)
+{
+ return prog ? prog->insns : libbpf_err_ptr(-EINVAL);
+}
+
void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
{
prog->prog_ifindex = ifindex;
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 6e61342ba56c..e4a1c98ae6d9 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -195,6 +195,7 @@ typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
bpf_program_clear_priv_t clear_priv);
+LIBBPF_API struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
__u32 ifindex);
--
2.32.0
next reply other threads:[~2021-07-13 18:33 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-07-13 18:33 Lorenzo Fontana [this message]
2021-07-13 18:35 ` [PATCH bpf-next 2/2] tools/bpf/bpftool: xlated dump from ELF file directly Lorenzo Fontana
2021-07-14 0:00 ` Lorenzo Fontana
2021-07-15 5:48 ` John Fastabend
2021-07-15 8:10 ` liwei (GF)
2021-07-15 9:55 ` Quentin Monnet
2021-07-14 4:19 ` [PATCH bpf-next 1/2] tools/lib/bpf: bpf_program__insns allow to retrieve insns in libbpf kernel test robot
2021-07-15 5:48 ` John Fastabend
2021-07-15 10:12 ` Quentin Monnet
2021-07-15 21:40 ` Andrii Nakryiko
2021-07-16 1:51 ` Alexei Starovoitov
2021-07-19 16:42 ` Lorenzo Fontana
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=aa97c776-9a82-9acc-fb13-dd082fdcaa61@gmail.com \
--to=fontanalorenz@gmail.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
/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).