Commit 12a8654b authored by Alexei Starovoitov's avatar Alexei Starovoitov Committed by Daniel Borkmann

libbpf: Add support for prog_tracing

Cleanup libbpf from expected_attach_type == attach_btf_id hack
and introduce BPF_PROG_TYPE_TRACING.
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20191030223212.953010-3-ast@kernel.org
parent f1b9509c
...@@ -173,6 +173,7 @@ enum bpf_prog_type { ...@@ -173,6 +173,7 @@ enum bpf_prog_type {
BPF_PROG_TYPE_CGROUP_SYSCTL, BPF_PROG_TYPE_CGROUP_SYSCTL,
BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE,
BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_PROG_TYPE_CGROUP_SOCKOPT,
BPF_PROG_TYPE_TRACING,
}; };
enum bpf_attach_type { enum bpf_attach_type {
...@@ -199,6 +200,7 @@ enum bpf_attach_type { ...@@ -199,6 +200,7 @@ enum bpf_attach_type {
BPF_CGROUP_UDP6_RECVMSG, BPF_CGROUP_UDP6_RECVMSG,
BPF_CGROUP_GETSOCKOPT, BPF_CGROUP_GETSOCKOPT,
BPF_CGROUP_SETSOCKOPT, BPF_CGROUP_SETSOCKOPT,
BPF_TRACE_RAW_TP,
__MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
}; };
......
...@@ -228,9 +228,10 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, ...@@ -228,9 +228,10 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, sizeof(attr));
attr.prog_type = load_attr->prog_type; attr.prog_type = load_attr->prog_type;
attr.expected_attach_type = load_attr->expected_attach_type; attr.expected_attach_type = load_attr->expected_attach_type;
if (attr.prog_type == BPF_PROG_TYPE_RAW_TRACEPOINT) if (attr.prog_type == BPF_PROG_TYPE_TRACING)
/* expected_attach_type is ignored for tracing progs */ attr.attach_btf_id = load_attr->attach_btf_id;
attr.attach_btf_id = attr.expected_attach_type; else
attr.prog_ifindex = load_attr->prog_ifindex;
attr.insn_cnt = (__u32)load_attr->insns_cnt; attr.insn_cnt = (__u32)load_attr->insns_cnt;
attr.insns = ptr_to_u64(load_attr->insns); attr.insns = ptr_to_u64(load_attr->insns);
attr.license = ptr_to_u64(load_attr->license); attr.license = ptr_to_u64(load_attr->license);
...@@ -245,7 +246,6 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, ...@@ -245,7 +246,6 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
} }
attr.kern_version = load_attr->kern_version; attr.kern_version = load_attr->kern_version;
attr.prog_ifindex = load_attr->prog_ifindex;
attr.prog_btf_fd = load_attr->prog_btf_fd; attr.prog_btf_fd = load_attr->prog_btf_fd;
attr.func_info_rec_size = load_attr->func_info_rec_size; attr.func_info_rec_size = load_attr->func_info_rec_size;
attr.func_info_cnt = load_attr->func_info_cnt; attr.func_info_cnt = load_attr->func_info_cnt;
......
...@@ -78,7 +78,10 @@ struct bpf_load_program_attr { ...@@ -78,7 +78,10 @@ struct bpf_load_program_attr {
size_t insns_cnt; size_t insns_cnt;
const char *license; const char *license;
__u32 kern_version; __u32 kern_version;
union {
__u32 prog_ifindex; __u32 prog_ifindex;
__u32 attach_btf_id;
};
__u32 prog_btf_fd; __u32 prog_btf_fd;
__u32 func_info_rec_size; __u32 func_info_rec_size;
const void *func_info; const void *func_info;
......
...@@ -188,6 +188,7 @@ struct bpf_program { ...@@ -188,6 +188,7 @@ struct bpf_program {
bpf_program_clear_priv_t clear_priv; bpf_program_clear_priv_t clear_priv;
enum bpf_attach_type expected_attach_type; enum bpf_attach_type expected_attach_type;
__u32 attach_btf_id;
void *func_info; void *func_info;
__u32 func_info_rec_size; __u32 func_info_rec_size;
__u32 func_info_cnt; __u32 func_info_cnt;
...@@ -3446,6 +3447,7 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, ...@@ -3446,6 +3447,7 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
load_attr.line_info_cnt = prog->line_info_cnt; load_attr.line_info_cnt = prog->line_info_cnt;
load_attr.log_level = prog->log_level; load_attr.log_level = prog->log_level;
load_attr.prog_flags = prog->prog_flags; load_attr.prog_flags = prog->prog_flags;
load_attr.attach_btf_id = prog->attach_btf_id;
retry_load: retry_load:
log_buf = malloc(log_buf_size); log_buf = malloc(log_buf_size);
...@@ -3607,6 +3609,8 @@ bpf_object__load_progs(struct bpf_object *obj, int log_level) ...@@ -3607,6 +3609,8 @@ bpf_object__load_progs(struct bpf_object *obj, int log_level)
return 0; return 0;
} }
static int libbpf_attach_btf_id_by_name(const char *name, __u32 *btf_id);
static struct bpf_object * static struct bpf_object *
__bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz, __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
struct bpf_object_open_opts *opts) struct bpf_object_open_opts *opts)
...@@ -3656,6 +3660,7 @@ __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz, ...@@ -3656,6 +3660,7 @@ __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
bpf_object__for_each_program(prog, obj) { bpf_object__for_each_program(prog, obj) {
enum bpf_prog_type prog_type; enum bpf_prog_type prog_type;
enum bpf_attach_type attach_type; enum bpf_attach_type attach_type;
__u32 btf_id;
err = libbpf_prog_type_by_name(prog->section_name, &prog_type, err = libbpf_prog_type_by_name(prog->section_name, &prog_type,
&attach_type); &attach_type);
...@@ -3667,6 +3672,12 @@ __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz, ...@@ -3667,6 +3672,12 @@ __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
bpf_program__set_type(prog, prog_type); bpf_program__set_type(prog, prog_type);
bpf_program__set_expected_attach_type(prog, attach_type); bpf_program__set_expected_attach_type(prog, attach_type);
if (prog_type == BPF_PROG_TYPE_TRACING) {
err = libbpf_attach_btf_id_by_name(prog->section_name, &btf_id);
if (err)
goto out;
prog->attach_btf_id = btf_id;
}
} }
return obj; return obj;
...@@ -4518,6 +4529,7 @@ BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT); ...@@ -4518,6 +4529,7 @@ BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT); BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT);
BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP); BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT); BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
BPF_PROG_TYPE_FNS(tracing, BPF_PROG_TYPE_TRACING);
enum bpf_attach_type enum bpf_attach_type
bpf_program__get_expected_attach_type(struct bpf_program *prog) bpf_program__get_expected_attach_type(struct bpf_program *prog)
...@@ -4546,7 +4558,8 @@ void bpf_program__set_expected_attach_type(struct bpf_program *prog, ...@@ -4546,7 +4558,8 @@ void bpf_program__set_expected_attach_type(struct bpf_program *prog,
BPF_PROG_SEC_IMPL(string, ptype, eatype, 1, 0, eatype) BPF_PROG_SEC_IMPL(string, ptype, eatype, 1, 0, eatype)
/* Programs that use BTF to identify attach point */ /* Programs that use BTF to identify attach point */
#define BPF_PROG_BTF(string, ptype) BPF_PROG_SEC_IMPL(string, ptype, 0, 0, 1, 0) #define BPF_PROG_BTF(string, ptype, eatype) \
BPF_PROG_SEC_IMPL(string, ptype, eatype, 0, 1, 0)
/* Programs that can be attached but attach type can't be identified by section /* Programs that can be attached but attach type can't be identified by section
* name. Kept for backward compatibility. * name. Kept for backward compatibility.
...@@ -4573,7 +4586,8 @@ static const struct { ...@@ -4573,7 +4586,8 @@ static const struct {
BPF_PROG_SEC("tp/", BPF_PROG_TYPE_TRACEPOINT), BPF_PROG_SEC("tp/", BPF_PROG_TYPE_TRACEPOINT),
BPF_PROG_SEC("raw_tracepoint/", BPF_PROG_TYPE_RAW_TRACEPOINT), BPF_PROG_SEC("raw_tracepoint/", BPF_PROG_TYPE_RAW_TRACEPOINT),
BPF_PROG_SEC("raw_tp/", BPF_PROG_TYPE_RAW_TRACEPOINT), BPF_PROG_SEC("raw_tp/", BPF_PROG_TYPE_RAW_TRACEPOINT),
BPF_PROG_BTF("tp_btf/", BPF_PROG_TYPE_RAW_TRACEPOINT), BPF_PROG_BTF("tp_btf/", BPF_PROG_TYPE_TRACING,
BPF_TRACE_RAW_TP),
BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP), BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP),
BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT), BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT),
BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN), BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN),
...@@ -4678,37 +4692,56 @@ int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, ...@@ -4678,37 +4692,56 @@ int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
continue; continue;
*prog_type = section_names[i].prog_type; *prog_type = section_names[i].prog_type;
*expected_attach_type = section_names[i].expected_attach_type; *expected_attach_type = section_names[i].expected_attach_type;
if (section_names[i].is_attach_btf) { return 0;
}
pr_warn("failed to guess program type based on ELF section name '%s'\n", name);
type_names = libbpf_get_type_names(false);
if (type_names != NULL) {
pr_info("supported section(type) names are:%s\n", type_names);
free(type_names);
}
return -ESRCH;
}
#define BTF_PREFIX "btf_trace_"
static int libbpf_attach_btf_id_by_name(const char *name, __u32 *btf_id)
{
struct btf *btf = bpf_core_find_kernel_btf(); struct btf *btf = bpf_core_find_kernel_btf();
char raw_tp_btf_name[128] = "btf_trace_"; char raw_tp_btf_name[128] = BTF_PREFIX;
char *dst = raw_tp_btf_name + sizeof("btf_trace_") - 1; char *dst = raw_tp_btf_name + sizeof(BTF_PREFIX) - 1;
int ret; int ret, i, err = -EINVAL;
if (IS_ERR(btf)) { if (IS_ERR(btf)) {
pr_warn("vmlinux BTF is not found\n"); pr_warn("vmlinux BTF is not found\n");
return -EINVAL; return -EINVAL;
} }
if (!name)
goto out;
for (i = 0; i < ARRAY_SIZE(section_names); i++) {
if (!section_names[i].is_attach_btf)
continue;
if (strncmp(name, section_names[i].sec, section_names[i].len))
continue;
/* prepend "btf_trace_" prefix per kernel convention */ /* prepend "btf_trace_" prefix per kernel convention */
strncat(dst, name + section_names[i].len, strncat(dst, name + section_names[i].len,
sizeof(raw_tp_btf_name) - sizeof("btf_trace_")); sizeof(raw_tp_btf_name) - sizeof(BTF_PREFIX));
ret = btf__find_by_name(btf, raw_tp_btf_name); ret = btf__find_by_name(btf, raw_tp_btf_name);
btf__free(btf);
if (ret <= 0) { if (ret <= 0) {
pr_warn("%s is not found in vmlinux BTF\n", dst); pr_warn("%s is not found in vmlinux BTF\n", dst);
return -EINVAL; goto out;
}
*expected_attach_type = ret;
}
return 0;
} }
pr_warn("failed to guess program type based on ELF section name '%s'\n", name); *btf_id = ret;
type_names = libbpf_get_type_names(false); err = 0;
if (type_names != NULL) { goto out;
pr_info("supported section(type) names are:%s\n", type_names);
free(type_names);
} }
pr_warn("failed to identify btf_id based on ELF section name '%s'\n", name);
return -ESRCH; err = -ESRCH;
out:
btf__free(btf);
return err;
} }
int libbpf_attach_type_by_name(const char *name, int libbpf_attach_type_by_name(const char *name,
......
...@@ -307,6 +307,7 @@ LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog); ...@@ -307,6 +307,7 @@ LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog); LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog); LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog); LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
LIBBPF_API enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog); LIBBPF_API enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog);
LIBBPF_API void bpf_program__set_type(struct bpf_program *prog, LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
...@@ -326,6 +327,7 @@ LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog); ...@@ -326,6 +327,7 @@ LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog);
LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog); LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog);
LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog); LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog); LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
/* /*
* No need for __attribute__((packed)), all members of 'bpf_map_def' * No need for __attribute__((packed)), all members of 'bpf_map_def'
......
...@@ -197,4 +197,6 @@ LIBBPF_0.0.6 { ...@@ -197,4 +197,6 @@ LIBBPF_0.0.6 {
bpf_object__open_mem; bpf_object__open_mem;
bpf_program__get_expected_attach_type; bpf_program__get_expected_attach_type;
bpf_program__get_type; bpf_program__get_type;
bpf_program__is_tracing;
bpf_program__set_tracing;
} LIBBPF_0.0.5; } LIBBPF_0.0.5;
...@@ -102,6 +102,7 @@ probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns, ...@@ -102,6 +102,7 @@ probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns,
case BPF_PROG_TYPE_FLOW_DISSECTOR: case BPF_PROG_TYPE_FLOW_DISSECTOR:
case BPF_PROG_TYPE_CGROUP_SYSCTL: case BPF_PROG_TYPE_CGROUP_SYSCTL:
case BPF_PROG_TYPE_CGROUP_SOCKOPT: case BPF_PROG_TYPE_CGROUP_SOCKOPT:
case BPF_PROG_TYPE_TRACING:
default: default:
break; break;
} }
......
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