Skip to content

Commit c6eb018

Browse files
Kui-Feng LeeKernel Patches Daemon
authored andcommitted
libbpf: Assign cookies to links in libbpf.
Add a cookie field to the attributes of bpf_link_create(). Add bpf_program__attach_trace_opts() to attach a cookie to a link. Signed-off-by: Kui-Feng Lee <[email protected]>
1 parent 7a26c64 commit c6eb018

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

tools/lib/bpf/bpf.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,13 @@ int bpf_link_create(int prog_fd, int target_fd,
863863
if (!OPTS_ZEROED(opts, kprobe_multi))
864864
return libbpf_err(-EINVAL);
865865
break;
866+
case BPF_TRACE_FENTRY:
867+
case BPF_TRACE_FEXIT:
868+
case BPF_MODIFY_RETURN:
869+
attr.link_create.tracing.cookie = OPTS_GET(opts, tracing.cookie, 0);
870+
if (!OPTS_ZEROED(opts, tracing))
871+
return libbpf_err(-EINVAL);
872+
break;
866873
default:
867874
if (!OPTS_ZEROED(opts, flags))
868875
return libbpf_err(-EINVAL);

tools/lib/bpf/bpf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,9 @@ struct bpf_link_create_opts {
410410
__u32 iter_info_len;
411411
__u32 target_btf_id;
412412
union {
413+
struct {
414+
__u64 cookie;
415+
} tracing;
413416
struct {
414417
__u64 bpf_cookie;
415418
} perf_event;

tools/lib/bpf/libbpf.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11262,6 +11262,44 @@ struct bpf_link *bpf_program__attach_trace(const struct bpf_program *prog)
1126211262
return bpf_program__attach_btf_id(prog);
1126311263
}
1126411264

11265+
struct bpf_link *bpf_program__attach_trace_opts(const struct bpf_program *prog,
11266+
const struct bpf_trace_opts *opts)
11267+
{
11268+
char errmsg[STRERR_BUFSIZE];
11269+
struct bpf_link *link;
11270+
int prog_fd, pfd;
11271+
LIBBPF_OPTS(bpf_link_create_opts, link_opts);
11272+
11273+
/* Fallback for cookie is 0. Old kernels don't create
11274+
* fentry/fexit links through LINK_CREATE.
11275+
*/
11276+
if (OPTS_GET(opts, cookie, 0))
11277+
return bpf_program__attach_trace(prog);
11278+
11279+
prog_fd = bpf_program__fd(prog);
11280+
if (prog_fd < 0) {
11281+
pr_warn("prog '%s': can't attach before loaded\n", prog->name);
11282+
return libbpf_err_ptr(-EINVAL);
11283+
}
11284+
11285+
link = calloc(1, sizeof(*link));
11286+
if (!link)
11287+
return libbpf_err_ptr(-ENOMEM);
11288+
link->detach = &bpf_link__detach_fd;
11289+
11290+
link_opts.tracing.cookie = OPTS_GET(opts, cookie, 0);
11291+
pfd = bpf_link_create(prog_fd, 0, prog->expected_attach_type, &link_opts);
11292+
if (pfd < 0) {
11293+
pfd = -errno;
11294+
free(link);
11295+
pr_warn("prog '%s': failed to attach: %s\n",
11296+
prog->name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
11297+
return libbpf_err_ptr(pfd);
11298+
}
11299+
link->fd = pfd;
11300+
return (struct bpf_link *)link;
11301+
}
11302+
1126511303
struct bpf_link *bpf_program__attach_lsm(const struct bpf_program *prog)
1126611304
{
1126711305
return bpf_program__attach_btf_id(prog);

tools/lib/bpf/libbpf.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,20 @@ bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
563563
LIBBPF_API struct bpf_link *
564564
bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
565565
const char *tp_name);
566+
567+
struct bpf_trace_opts {
568+
/* size of this struct, for forward/backward compatibility */
569+
size_t sz;
570+
/* custom user-provided value fetchable through bpf_get_attach_cookie() */
571+
__u64 cookie;
572+
};
573+
#define bpf_trace_opts__last_field cookie
574+
566575
LIBBPF_API struct bpf_link *
567576
bpf_program__attach_trace(const struct bpf_program *prog);
577+
LIBBPF_API struct bpf_link *
578+
bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);
579+
568580
LIBBPF_API struct bpf_link *
569581
bpf_program__attach_lsm(const struct bpf_program *prog);
570582
LIBBPF_API struct bpf_link *

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ LIBBPF_0.8.0 {
444444
global:
445445
bpf_object__destroy_subskeleton;
446446
bpf_object__open_subskeleton;
447+
bpf_program__attach_trace_opts;
447448
bpf_program__attach_usdt;
448449
libbpf_register_prog_handler;
449450
libbpf_unregister_prog_handler;

0 commit comments

Comments
 (0)