From: Toke Høiland-Jørgensen <toke@xxxxxxxxxx> This adds support for supplying a target fd and btf ID for the raw_tracepoint_open() BPF operation, using a new bpf_raw_tracepoint_opts structure. This can be used for attaching freplace programs to multiple destinations. Signed-off-by: Toke Høiland-Jørgensen <toke@xxxxxxxxxx> --- tools/lib/bpf/bpf.c | 33 ++++++++++++++++++++++++++++++++- tools/lib/bpf/bpf.h | 12 ++++++++++++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index a7329b671c41..6e7690d8ddfb 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -793,17 +793,48 @@ int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len) return err; } -int bpf_raw_tracepoint_open(const char *name, int prog_fd) +int bpf_raw_tracepoint_open_opts(const char *name, int prog_fd, + struct bpf_raw_tracepoint_opts *opts) { union bpf_attr attr; + size_t log_buf_sz; + __u32 log_level; + char *log_buf; + + if (!OPTS_VALID(opts, bpf_raw_tracepoint_opts)) + return -EINVAL; + + log_buf = OPTS_GET(opts, log_buf, NULL); + log_buf_sz = OPTS_GET(opts, log_buf_sz, 0); + log_level = OPTS_GET(opts, log_level, 0); + + if (!log_buf != !log_buf_sz || log_level > (4 | 2 | 1) || + (log_level && !log_buf)) + return -EINVAL; memset(&attr, 0, sizeof(attr)); attr.raw_tracepoint.name = ptr_to_u64(name); attr.raw_tracepoint.prog_fd = prog_fd; + attr.raw_tracepoint.tgt_prog_fd = OPTS_GET(opts, tgt_prog_fd, 0); + attr.raw_tracepoint.tgt_btf_id = OPTS_GET(opts, tgt_btf_id, 0); + + attr.raw_tracepoint.log_level = log_level; + if (log_level) { + attr.raw_tracepoint.log_buf = ptr_to_u64(log_buf); + attr.raw_tracepoint.log_size = log_buf_sz; + } else { + attr.raw_tracepoint.log_buf = ptr_to_u64(NULL); + attr.raw_tracepoint.log_size = 0; + } return sys_bpf(BPF_RAW_TRACEPOINT_OPEN, &attr, sizeof(attr)); } +int bpf_raw_tracepoint_open(const char *name, int prog_fd) +{ + return bpf_raw_tracepoint_open_opts(name, prog_fd, NULL); +} + int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size, bool do_log) { diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 1b6015b21ba8..b6b8ebb6ce65 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -227,7 +227,19 @@ LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len); LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags, __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt); +struct bpf_raw_tracepoint_opts { + size_t sz; /* size of this struct for forward/backward compatibility */ + __u32 log_level; + char *log_buf; + size_t log_buf_sz; + int tgt_prog_fd; /* target program to attach to */ + __u32 tgt_btf_id; /* BTF ID of target function */ +}; +#define bpf_raw_tracepoint_opts__last_field tgt_btf_id + LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd); +LIBBPF_API int bpf_raw_tracepoint_open_opts(const char *name, int prog_fd, + struct bpf_raw_tracepoint_opts *opts); LIBBPF_API int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size, bool do_log); LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 6544d2cd1ed6..f726805f5ec8 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -288,4 +288,5 @@ LIBBPF_0.1.0 { bpf_map__value_size; bpf_program__autoload; bpf_program__set_autoload; + bpf_raw_tracepoint_open_opts; } LIBBPF_0.0.9;