提交 2d5ec1c6 编写于 作者: A Andrii Nakryiko 提交者: Alexei Starovoitov

libbpf: Allow skipping attach_func_name in bpf_program__set_attach_target()

Allow to use bpf_program__set_attach_target to only set target attach
program FD, while letting libbpf to use target attach function name from
SEC() definition. This might be useful for some scenarios where
bpf_object contains multiple related freplace BPF programs intended to
replace different sub-programs in target BPF program. In such case all
programs will have the same attach_prog_fd, but different
attach_func_name. It's convenient to specify such target function names
declaratively in SEC() definitions, but attach_prog_fd is a dynamic
runtime setting.

To simplify such scenario, allow bpf_program__set_attach_target() to
delay BTF ID resolution till the BPF program load time by providing NULL
attach_func_name. In that case the behavior will be similar to using
bpf_object_open_opts.attach_prog_fd (which is marked deprecated since
v0.7), but has the benefit of allowing more control by user in what is
attached to what. Such setup allows having BPF programs attached to
different target attach_prog_fd with target functions still declaratively
recorded in BPF source code in SEC() definitions.

Selftests changes in the next patch should make this more obvious.
Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
Acked-by: NYonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210916015836.1248906-5-andrii@kernel.org
上级 27764185
...@@ -10643,18 +10643,29 @@ int bpf_program__set_attach_target(struct bpf_program *prog, ...@@ -10643,18 +10643,29 @@ int bpf_program__set_attach_target(struct bpf_program *prog,
{ {
int btf_obj_fd = 0, btf_id = 0, err; int btf_obj_fd = 0, btf_id = 0, err;
if (!prog || attach_prog_fd < 0 || !attach_func_name) if (!prog || attach_prog_fd < 0)
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
if (prog->obj->loaded) if (prog->obj->loaded)
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
if (attach_prog_fd && !attach_func_name) {
/* remember attach_prog_fd and let bpf_program__load() find
* BTF ID during the program load
*/
prog->attach_prog_fd = attach_prog_fd;
return 0;
}
if (attach_prog_fd) { if (attach_prog_fd) {
btf_id = libbpf_find_prog_btf_id(attach_func_name, btf_id = libbpf_find_prog_btf_id(attach_func_name,
attach_prog_fd); attach_prog_fd);
if (btf_id < 0) if (btf_id < 0)
return libbpf_err(btf_id); return libbpf_err(btf_id);
} else { } else {
if (!attach_func_name)
return libbpf_err(-EINVAL);
/* load btf_vmlinux, if not yet */ /* load btf_vmlinux, if not yet */
err = bpf_object__load_vmlinux_btf(prog->obj, true); err = bpf_object__load_vmlinux_btf(prog->obj, true);
if (err) if (err)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册