libbpf: Add support for freplace attachment in bpf_link_create
This adds support for supplying a target btf ID for the bpf_link_create() operation, and adds a new bpf_program__attach_freplace() high-level API for attaching freplace functions with a target. Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/160138355387.48470.18026176785351166890.stgit@toke.dk
This commit is contained in:
parent
43bc2874e7
commit
a535909142
|
@ -586,19 +586,31 @@ int bpf_link_create(int prog_fd, int target_fd,
|
|||
enum bpf_attach_type attach_type,
|
||||
const struct bpf_link_create_opts *opts)
|
||||
{
|
||||
__u32 target_btf_id, iter_info_len;
|
||||
union bpf_attr attr;
|
||||
|
||||
if (!OPTS_VALID(opts, bpf_link_create_opts))
|
||||
return -EINVAL;
|
||||
|
||||
iter_info_len = OPTS_GET(opts, iter_info_len, 0);
|
||||
target_btf_id = OPTS_GET(opts, target_btf_id, 0);
|
||||
|
||||
if (iter_info_len && target_btf_id)
|
||||
return -EINVAL;
|
||||
|
||||
memset(&attr, 0, sizeof(attr));
|
||||
attr.link_create.prog_fd = prog_fd;
|
||||
attr.link_create.target_fd = target_fd;
|
||||
attr.link_create.attach_type = attach_type;
|
||||
attr.link_create.flags = OPTS_GET(opts, flags, 0);
|
||||
|
||||
if (iter_info_len) {
|
||||
attr.link_create.iter_info =
|
||||
ptr_to_u64(OPTS_GET(opts, iter_info, (void *)0));
|
||||
attr.link_create.iter_info_len = OPTS_GET(opts, iter_info_len, 0);
|
||||
attr.link_create.iter_info_len = iter_info_len;
|
||||
} else if (target_btf_id) {
|
||||
attr.link_create.target_btf_id = target_btf_id;
|
||||
}
|
||||
|
||||
return sys_bpf(BPF_LINK_CREATE, &attr, sizeof(attr));
|
||||
}
|
||||
|
|
|
@ -174,8 +174,9 @@ struct bpf_link_create_opts {
|
|||
__u32 flags;
|
||||
union bpf_iter_link_info *iter_info;
|
||||
__u32 iter_info_len;
|
||||
__u32 target_btf_id;
|
||||
};
|
||||
#define bpf_link_create_opts__last_field iter_info_len
|
||||
#define bpf_link_create_opts__last_field target_btf_id
|
||||
|
||||
LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
|
||||
enum bpf_attach_type attach_type,
|
||||
|
|
|
@ -9390,9 +9390,11 @@ static struct bpf_link *attach_iter(const struct bpf_sec_def *sec,
|
|||
}
|
||||
|
||||
static struct bpf_link *
|
||||
bpf_program__attach_fd(struct bpf_program *prog, int target_fd,
|
||||
bpf_program__attach_fd(struct bpf_program *prog, int target_fd, int btf_id,
|
||||
const char *target_name)
|
||||
{
|
||||
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts,
|
||||
.target_btf_id = btf_id);
|
||||
enum bpf_attach_type attach_type;
|
||||
char errmsg[STRERR_BUFSIZE];
|
||||
struct bpf_link *link;
|
||||
|
@ -9410,7 +9412,7 @@ bpf_program__attach_fd(struct bpf_program *prog, int target_fd,
|
|||
link->detach = &bpf_link__detach_fd;
|
||||
|
||||
attach_type = bpf_program__get_expected_attach_type(prog);
|
||||
link_fd = bpf_link_create(prog_fd, target_fd, attach_type, NULL);
|
||||
link_fd = bpf_link_create(prog_fd, target_fd, attach_type, &opts);
|
||||
if (link_fd < 0) {
|
||||
link_fd = -errno;
|
||||
free(link);
|
||||
|
@ -9426,19 +9428,51 @@ bpf_program__attach_fd(struct bpf_program *prog, int target_fd,
|
|||
struct bpf_link *
|
||||
bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd)
|
||||
{
|
||||
return bpf_program__attach_fd(prog, cgroup_fd, "cgroup");
|
||||
return bpf_program__attach_fd(prog, cgroup_fd, 0, "cgroup");
|
||||
}
|
||||
|
||||
struct bpf_link *
|
||||
bpf_program__attach_netns(struct bpf_program *prog, int netns_fd)
|
||||
{
|
||||
return bpf_program__attach_fd(prog, netns_fd, "netns");
|
||||
return bpf_program__attach_fd(prog, netns_fd, 0, "netns");
|
||||
}
|
||||
|
||||
struct bpf_link *bpf_program__attach_xdp(struct bpf_program *prog, int ifindex)
|
||||
{
|
||||
/* target_fd/target_ifindex use the same field in LINK_CREATE */
|
||||
return bpf_program__attach_fd(prog, ifindex, "xdp");
|
||||
return bpf_program__attach_fd(prog, ifindex, 0, "xdp");
|
||||
}
|
||||
|
||||
struct bpf_link *bpf_program__attach_freplace(struct bpf_program *prog,
|
||||
int target_fd,
|
||||
const char *attach_func_name)
|
||||
{
|
||||
int btf_id;
|
||||
|
||||
if (!!target_fd != !!attach_func_name) {
|
||||
pr_warn("prog '%s': supply none or both of target_fd and attach_func_name\n",
|
||||
prog->name);
|
||||
return ERR_PTR(-EINVAL);
|
||||
}
|
||||
|
||||
if (prog->type != BPF_PROG_TYPE_EXT) {
|
||||
pr_warn("prog '%s': only BPF_PROG_TYPE_EXT can attach as freplace",
|
||||
prog->name);
|
||||
return ERR_PTR(-EINVAL);
|
||||
}
|
||||
|
||||
if (target_fd) {
|
||||
btf_id = libbpf_find_prog_btf_id(attach_func_name, target_fd);
|
||||
if (btf_id < 0)
|
||||
return ERR_PTR(btf_id);
|
||||
|
||||
return bpf_program__attach_fd(prog, target_fd, btf_id, "freplace");
|
||||
} else {
|
||||
/* no target, so use raw_tracepoint_open for compatibility
|
||||
* with old kernels
|
||||
*/
|
||||
return bpf_program__attach_trace(prog);
|
||||
}
|
||||
}
|
||||
|
||||
struct bpf_link *
|
||||
|
|
|
@ -261,6 +261,9 @@ LIBBPF_API struct bpf_link *
|
|||
bpf_program__attach_netns(struct bpf_program *prog, int netns_fd);
|
||||
LIBBPF_API struct bpf_link *
|
||||
bpf_program__attach_xdp(struct bpf_program *prog, int ifindex);
|
||||
LIBBPF_API struct bpf_link *
|
||||
bpf_program__attach_freplace(struct bpf_program *prog,
|
||||
int target_fd, const char *attach_func_name);
|
||||
|
||||
struct bpf_map;
|
||||
|
||||
|
|
|
@ -304,6 +304,7 @@ LIBBPF_0.2.0 {
|
|||
global:
|
||||
bpf_prog_bind_map;
|
||||
bpf_prog_test_run_opts;
|
||||
bpf_program__attach_freplace;
|
||||
bpf_program__section_name;
|
||||
btf__add_array;
|
||||
btf__add_const;
|
||||
|
|
Loading…
Reference in New Issue