libbpf: Allow skipping attach_func_name in bpf_program__set_attach_target()
authorAndrii Nakryiko <andrii@kernel.org>
Thu, 16 Sep 2021 01:58:33 +0000 (18:58 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 17 Sep 2021 16:05:00 +0000 (09:05 -0700)
commit2d5ec1c66e25f0b4dd895a211e651a12dec2ef4f
tree2e0b4d06450323d5c9cd64e2a9afb6e189009747
parent277641859e833549722eb82c97cbc2d55421df7c
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: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210916015836.1248906-5-andrii@kernel.org
tools/lib/bpf/libbpf.c