Skip to content

Commit 939f0c4

Browse files
tohojokernel-patches-bot
authored andcommitted
From: Toke Høiland-Jørgensen <toke@redhat.com>
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> --- tools/lib/bpf/bpf.c | 1 + tools/lib/bpf/bpf.h | 3 ++- tools/lib/bpf/libbpf.c | 36 +++++++++++++++++++++++++++++++----- tools/lib/bpf/libbpf.h | 3 +++ tools/lib/bpf/libbpf.map | 1 + 5 files changed, 38 insertions(+), 6 deletions(-)
1 parent 20fe3a6 commit 939f0c4

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

tools/lib/bpf/bpf.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ int bpf_link_create(int prog_fd, int target_fd,
599599
attr.link_create.iter_info =
600600
ptr_to_u64(OPTS_GET(opts, iter_info, (void *)0));
601601
attr.link_create.iter_info_len = OPTS_GET(opts, iter_info_len, 0);
602+
attr.link_create.target_btf_id = OPTS_GET(opts, target_btf_id, 0);
602603

603604
return sys_bpf(BPF_LINK_CREATE, &attr, sizeof(attr));
604605
}

tools/lib/bpf/bpf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ struct bpf_link_create_opts {
174174
__u32 flags;
175175
union bpf_iter_link_info *iter_info;
176176
__u32 iter_info_len;
177+
__u32 target_btf_id;
177178
};
178-
#define bpf_link_create_opts__last_field iter_info_len
179+
#define bpf_link_create_opts__last_field target_btf_id
179180

180181
LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
181182
enum bpf_attach_type attach_type,

tools/lib/bpf/libbpf.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9390,9 +9390,11 @@ static struct bpf_link *attach_iter(const struct bpf_sec_def *sec,
93909390
}
93919391

93929392
static struct bpf_link *
9393-
bpf_program__attach_fd(struct bpf_program *prog, int target_fd,
9393+
bpf_program__attach_fd(struct bpf_program *prog, int target_fd, int btf_id,
93949394
const char *target_name)
93959395
{
9396+
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts,
9397+
.target_btf_id = btf_id);
93969398
enum bpf_attach_type attach_type;
93979399
char errmsg[STRERR_BUFSIZE];
93989400
struct bpf_link *link;
@@ -9410,7 +9412,7 @@ bpf_program__attach_fd(struct bpf_program *prog, int target_fd,
94109412
link->detach = &bpf_link__detach_fd;
94119413

94129414
attach_type = bpf_program__get_expected_attach_type(prog);
9413-
link_fd = bpf_link_create(prog_fd, target_fd, attach_type, NULL);
9415+
link_fd = bpf_link_create(prog_fd, target_fd, attach_type, &opts);
94149416
if (link_fd < 0) {
94159417
link_fd = -errno;
94169418
free(link);
@@ -9426,19 +9428,43 @@ bpf_program__attach_fd(struct bpf_program *prog, int target_fd,
94269428
struct bpf_link *
94279429
bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd)
94289430
{
9429-
return bpf_program__attach_fd(prog, cgroup_fd, "cgroup");
9431+
return bpf_program__attach_fd(prog, cgroup_fd, 0, "cgroup");
94309432
}
94319433

94329434
struct bpf_link *
94339435
bpf_program__attach_netns(struct bpf_program *prog, int netns_fd)
94349436
{
9435-
return bpf_program__attach_fd(prog, netns_fd, "netns");
9437+
return bpf_program__attach_fd(prog, netns_fd, 0, "netns");
94369438
}
94379439

94389440
struct bpf_link *bpf_program__attach_xdp(struct bpf_program *prog, int ifindex)
94399441
{
94409442
/* target_fd/target_ifindex use the same field in LINK_CREATE */
9441-
return bpf_program__attach_fd(prog, ifindex, "xdp");
9443+
return bpf_program__attach_fd(prog, ifindex, 0, "xdp");
9444+
}
9445+
9446+
struct bpf_link *bpf_program__attach_freplace(struct bpf_program *prog,
9447+
int target_fd,
9448+
const char *attach_func_name)
9449+
{
9450+
int btf_id;
9451+
9452+
if (!!target_fd != !!attach_func_name || prog->type != BPF_PROG_TYPE_EXT)
9453+
return ERR_PTR(-EINVAL);
9454+
9455+
if (target_fd) {
9456+
9457+
btf_id = libbpf_find_prog_btf_id(attach_func_name, target_fd);
9458+
if (btf_id < 0)
9459+
return ERR_PTR(btf_id);
9460+
9461+
return bpf_program__attach_fd(prog, target_fd, btf_id, "freplace");
9462+
} else {
9463+
/* no target, so use raw_tracepoint_open for compatibility
9464+
* with old kernels
9465+
*/
9466+
return bpf_program__attach_trace(prog);
9467+
}
94429468
}
94439469

94449470
struct bpf_link *

tools/lib/bpf/libbpf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ LIBBPF_API struct bpf_link *
261261
bpf_program__attach_netns(struct bpf_program *prog, int netns_fd);
262262
LIBBPF_API struct bpf_link *
263263
bpf_program__attach_xdp(struct bpf_program *prog, int ifindex);
264+
LIBBPF_API struct bpf_link *
265+
bpf_program__attach_freplace(struct bpf_program *prog,
266+
int target_fd, const char *attach_func_name);
264267

265268
struct bpf_map;
266269

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ LIBBPF_0.1.0 {
303303
LIBBPF_0.2.0 {
304304
global:
305305
bpf_prog_bind_map;
306+
bpf_program__attach_freplace;
306307
bpf_program__section_name;
307308
perf_buffer__buffer_cnt;
308309
perf_buffer__buffer_fd;

0 commit comments

Comments
 (0)