libbpf: Add ability to fetch bpf_program's underlying instructions
Add APIs providing read-only access to bpf_program BPF instructions ([0]). This is useful for diagnostics purposes, but it also allows a cleaner support for cloning BPF programs after libbpf did all the FD resolution and CO-RE relocations, subprog instructions appending, etc. Currently, cloning BPF program is possible only through hijacking a half-broken bpf_program__set_prep() API, which doesn't really work well for anything but most primitive programs. For instance, set_prep() API doesn't allow adjusting BPF program load parameters which are necessary for loading fentry/fexit BPF programs (the case where BPF program cloning is a necessity if doing some sort of mass-attachment functionality). Given bpf_program__set_prep() API is set to be deprecated, having a cleaner alternative is a must. libbpf internally already keeps track of linear array of struct bpf_insn, so it's not hard to expose it. The only gotcha is that libbpf previously freed instructions array during bpf_object load time, which would make this API much less useful overall, because in between bpf_object__open() and bpf_object__load() a lot of changes to instructions are done by libbpf. So this patch makes libbpf hold onto prog->insns array even after BPF program loading. I think this is a small price for added functionality and improved introspection of BPF program code. See retsnoop PR ([1]) for how it can be used in practice and code savings compared to relying on bpf_program__set_prep(). [0] Closes: https://github.com/libbpf/libbpf/issues/298 [1] https://github.com/anakryiko/retsnoop/pull/1 Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20211025224531.1088894-3-andrii@kernel.org
This commit is contained in:
committed by
Alexei Starovoitov
parent
de5d0dcef6
commit
65a7fa2e4e
@@ -6653,8 +6653,6 @@ int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
|
||||
out:
|
||||
if (err)
|
||||
pr_warn("failed to load program '%s'\n", prog->name);
|
||||
zfree(&prog->insns);
|
||||
prog->insns_cnt = 0;
|
||||
return libbpf_err(err);
|
||||
}
|
||||
|
||||
@@ -8143,6 +8141,16 @@ size_t bpf_program__size(const struct bpf_program *prog)
|
||||
return prog->insns_cnt * BPF_INSN_SZ;
|
||||
}
|
||||
|
||||
const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog)
|
||||
{
|
||||
return prog->insns;
|
||||
}
|
||||
|
||||
size_t bpf_program__insn_cnt(const struct bpf_program *prog)
|
||||
{
|
||||
return prog->insns_cnt;
|
||||
}
|
||||
|
||||
int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
|
||||
bpf_program_prep_t prep)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user