Commit 129b9c5e authored by Kui-Feng Lee's avatar Kui-Feng Lee Committed by Andrii Nakryiko

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: default avatarKui-Feng Lee <kuifeng@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220510205923.3206889-5-kuifeng@fb.com
parent 2fcc8241
...@@ -863,6 +863,14 @@ int bpf_link_create(int prog_fd, int target_fd, ...@@ -863,6 +863,14 @@ int bpf_link_create(int prog_fd, int target_fd,
if (!OPTS_ZEROED(opts, kprobe_multi)) if (!OPTS_ZEROED(opts, kprobe_multi))
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
break; break;
case BPF_TRACE_FENTRY:
case BPF_TRACE_FEXIT:
case BPF_MODIFY_RETURN:
case BPF_LSM_MAC:
attr.link_create.tracing.cookie = OPTS_GET(opts, tracing.cookie, 0);
if (!OPTS_ZEROED(opts, tracing))
return libbpf_err(-EINVAL);
break;
default: default:
if (!OPTS_ZEROED(opts, flags)) if (!OPTS_ZEROED(opts, flags))
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
......
...@@ -420,6 +420,9 @@ struct bpf_link_create_opts { ...@@ -420,6 +420,9 @@ struct bpf_link_create_opts {
const unsigned long *addrs; const unsigned long *addrs;
const __u64 *cookies; const __u64 *cookies;
} kprobe_multi; } kprobe_multi;
struct {
__u64 cookie;
} tracing;
}; };
size_t :0; size_t :0;
}; };
......
...@@ -11568,12 +11568,17 @@ static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf ...@@ -11568,12 +11568,17 @@ static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf
} }
/* Common logic for all BPF program types that attach to a btf_id */ /* Common logic for all BPF program types that attach to a btf_id */
static struct bpf_link *bpf_program__attach_btf_id(const struct bpf_program *prog) static struct bpf_link *bpf_program__attach_btf_id(const struct bpf_program *prog,
const struct bpf_trace_opts *opts)
{ {
LIBBPF_OPTS(bpf_link_create_opts, link_opts);
char errmsg[STRERR_BUFSIZE]; char errmsg[STRERR_BUFSIZE];
struct bpf_link *link; struct bpf_link *link;
int prog_fd, pfd; int prog_fd, pfd;
if (!OPTS_VALID(opts, bpf_trace_opts))
return libbpf_err_ptr(-EINVAL);
prog_fd = bpf_program__fd(prog); prog_fd = bpf_program__fd(prog);
if (prog_fd < 0) { if (prog_fd < 0) {
pr_warn("prog '%s': can't attach before loaded\n", prog->name); pr_warn("prog '%s': can't attach before loaded\n", prog->name);
...@@ -11586,7 +11591,8 @@ static struct bpf_link *bpf_program__attach_btf_id(const struct bpf_program *pro ...@@ -11586,7 +11591,8 @@ static struct bpf_link *bpf_program__attach_btf_id(const struct bpf_program *pro
link->detach = &bpf_link__detach_fd; link->detach = &bpf_link__detach_fd;
/* libbpf is smart enough to redirect to BPF_RAW_TRACEPOINT_OPEN on old kernels */ /* libbpf is smart enough to redirect to BPF_RAW_TRACEPOINT_OPEN on old kernels */
pfd = bpf_link_create(prog_fd, 0, bpf_program__expected_attach_type(prog), NULL); link_opts.tracing.cookie = OPTS_GET(opts, cookie, 0);
pfd = bpf_link_create(prog_fd, 0, bpf_program__expected_attach_type(prog), &link_opts);
if (pfd < 0) { if (pfd < 0) {
pfd = -errno; pfd = -errno;
free(link); free(link);
...@@ -11600,12 +11606,18 @@ static struct bpf_link *bpf_program__attach_btf_id(const struct bpf_program *pro ...@@ -11600,12 +11606,18 @@ static struct bpf_link *bpf_program__attach_btf_id(const struct bpf_program *pro
struct bpf_link *bpf_program__attach_trace(const struct bpf_program *prog) struct bpf_link *bpf_program__attach_trace(const struct bpf_program *prog)
{ {
return bpf_program__attach_btf_id(prog); return bpf_program__attach_btf_id(prog, NULL);
}
struct bpf_link *bpf_program__attach_trace_opts(const struct bpf_program *prog,
const struct bpf_trace_opts *opts)
{
return bpf_program__attach_btf_id(prog, opts);
} }
struct bpf_link *bpf_program__attach_lsm(const struct bpf_program *prog) struct bpf_link *bpf_program__attach_lsm(const struct bpf_program *prog)
{ {
return bpf_program__attach_btf_id(prog); return bpf_program__attach_btf_id(prog, NULL);
} }
static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link) static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link)
......
...@@ -603,8 +603,20 @@ bpf_program__attach_tracepoint_opts(const struct bpf_program *prog, ...@@ -603,8 +603,20 @@ bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_raw_tracepoint(const struct bpf_program *prog, bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
const char *tp_name); const char *tp_name);
struct bpf_trace_opts {
/* size of this struct, for forward/backward compatibility */
size_t sz;
/* custom user-provided value fetchable through bpf_get_attach_cookie() */
__u64 cookie;
};
#define bpf_trace_opts__last_field cookie
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_trace(const struct bpf_program *prog); bpf_program__attach_trace(const struct bpf_program *prog);
LIBBPF_API struct bpf_link *
bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_lsm(const struct bpf_program *prog); bpf_program__attach_lsm(const struct bpf_program *prog);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
......
...@@ -447,6 +447,7 @@ LIBBPF_0.8.0 { ...@@ -447,6 +447,7 @@ LIBBPF_0.8.0 {
bpf_object__destroy_subskeleton; bpf_object__destroy_subskeleton;
bpf_object__open_subskeleton; bpf_object__open_subskeleton;
bpf_program__attach_kprobe_multi_opts; bpf_program__attach_kprobe_multi_opts;
bpf_program__attach_trace_opts;
bpf_program__attach_usdt; bpf_program__attach_usdt;
libbpf_register_prog_handler; libbpf_register_prog_handler;
libbpf_unregister_prog_handler; libbpf_unregister_prog_handler;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment