Add code-generated BPF object skeleton support
Subsystems (7)
cover Add code-generated BPF object skeleton support
This patch set introduces an alternative and complimentary to existing libbpf API interface for working with BPF objects, maps, programs, and global data from userspace side. This approach is relying on code generation. bpftool produces a struct (a.k.a. skeleton) tailored and specific to provided BPF object file. It includes hard-coded fields and data structures for every map, program, link, and global data present.
Altogether this approach significantly reduces amount of userspace boilerplate code required to open, load, attach, and work with BPF objects. It improves attach/detach story, by providing pre-allocated space for bpf_links, and ensuring they are properly detached on shutdown. It allows to do away with by name/title lookups of maps and programs, because libbpf's skeleton API, in conjunction with generated code from bpftool, is filling in hard-coded fields with actual pointers to corresponding struct bpf_map/bpf_program/bpf_link.
Also, thanks to BPF array mmap() support, working with global data (variables) from userspace is now as natural as it is from BPF side: each variable is just a struct field inside skeleton struct. Furthermore, this allows to have a natural way for userspace to pre-initialize global data (including previously impossible to initialize .rodata) by just assigning values to the same per-variable fields. Libbpf will carefully take into account this initialization image, will use it to pre-populate BPF maps at creation time, and will re-mmap() BPF map's contents at exactly the same userspace memory address such that it can continue working with all the same pointers without any interruptions. If kernel doesn't support mmap(), global data will still be successfully initialized, but after map creation global data structures inside skeleton will be NULL-ed out. This allows userspace application to gracefully handle lack of mmap() support, if necessary.
As a demonstration of BPF CO-RE, libbpf tracing APIs, and skeleton approach working together, runqslower tool (originally BCC-based and distributed along with BCC) is added under samples directory. It's a complete tool with 100% feature parity with its BCC-based counterpart. But it doesn't require neither Python, nor Clang/LLVM runtime. It's pre-compiled and can be distributed to target machine in a compact binary form.
A bunch of selftests are also converted to using skeletons, demonstrating significant simplification of userspace part of test and reduction in amount of code necessary.
Andrii Nakryiko (16):
libbpf: don't require root for bpf_object__open()
libbpf: add generic bpf_program__attach()
libbpf: move non-public APIs from libbpf.h to libbpf_internal.h
libbpf: add BPF_EMBED_OBJ macro for embedding BPF .o files
libbpf: expose field/var declaration emitting API internally
libbpf: expose BPF program's function name
libbpf: refactor global data map initialization
libbpf: postpone BTF ID finding for TRACING programs to load phase
libbpf: reduce log level of supported section names dump
libbpf: add experimental BPF object skeleton support
bpftool: add skeleton codegen command
libbpf/samples: add runqslower sample to libbpf samples selftests/bpf: add BPF skeletons selftests and convert attach_probe.c selftests/bpf: convert few more selftest to skeletons selftests/bpf: add test validating data section to struct convertion
layout
bpftool: add gen skeleton BASH completions
tools/bpf/bpftool/bash-completion/bpftool | 11 + tools/bpf/bpftool/gen.c | 482 +++++++++++++++ tools/bpf/bpftool/main.c | 3 +- tools/bpf/bpftool/main.h | 1 + tools/bpf/bpftool/net.c | 1 + tools/lib/bpf/btf_dump.c | 61 +- tools/lib/bpf/libbpf.c | 583 ++++++++++++++---- tools/lib/bpf/libbpf.h | 63 +- tools/lib/bpf/libbpf.map | 7 + tools/lib/bpf/libbpf_internal.h | 63 ++ tools/lib/bpf/samples/runqslower/.gitignore | 2 + tools/lib/bpf/samples/runqslower/Makefile | 50 ++ .../bpf/samples/runqslower/runqslower.bpf.c | 105 ++++ tools/lib/bpf/samples/runqslower/runqslower.c | 189 ++++++ tools/lib/bpf/samples/runqslower/runqslower.h | 13 + tools/testing/selftests/bpf/.gitignore | 2 + tools/testing/selftests/bpf/Makefile | 36 +- .../selftests/bpf/prog_tests/attach_probe.c | 154 +---- .../selftests/bpf/prog_tests/fentry_fexit.c | 105 ++-- .../selftests/bpf/prog_tests/fentry_test.c | 72 +-- tools/testing/selftests/bpf/prog_tests/mmap.c | 58 +- .../selftests/bpf/prog_tests/probe_user.c | 6 +- .../selftests/bpf/prog_tests/rdonly_maps.c | 11 +- .../selftests/bpf/prog_tests/skeleton.c | 47 ++ .../bpf/prog_tests/stacktrace_build_id.c | 79 +-- .../bpf/prog_tests/stacktrace_build_id_nmi.c | 84 +-- .../selftests/bpf/progs/test_attach_probe.c | 34 +- .../selftests/bpf/progs/test_skeleton.c | 36 ++ 28 files changed, 1759 insertions(+), 599 deletions(-) create mode 100644 tools/bpf/bpftool/gen.c create mode 100644 tools/lib/bpf/samples/runqslower/.gitignore create mode 100644 tools/lib/bpf/samples/runqslower/Makefile create mode 100644 tools/lib/bpf/samples/runqslower/runqslower.bpf.c create mode 100644 tools/lib/bpf/samples/runqslower/runqslower.c create mode 100644 tools/lib/bpf/samples/runqslower/runqslower.h create mode 100644 tools/testing/selftests/bpf/prog_tests/skeleton.c create mode 100644 tools/testing/selftests/bpf/progs/test_skeleton.c
Signature · 1 line
2.17.1
RFC PATCH 1/16 libbpf: don't require root for bpf_object__open()
Reorganize bpf_object__open and bpf_object__load steps such that bpf_object__open doesn't need root access. This was previously done for feature probing and BTF sanitization. This doesn't have to happen on open, though, so move all those steps into the load phase.
This is important, because it makes it possible for tools like bpftool, to just open BPF object file and inspect their contents: programs, maps, BTF, etc. For such operations it is prohibitive to require root access. On the other hand, there is a lot of custom libbpf logic in those steps, so its best avoided for tools to reimplement all that on their own.
--- tools/lib/bpf/libbpf.c | 83 +++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 42 deletions(-)
tools/lib/bpf/libbpf.c +41 -42
@@ -101,13 +101,6 @@ void libbpf_print(enum libbpf_print_level level, const char *format, ...)#define STRERR_BUFSIZE 128 -#define CHECK_ERR (action, err, out) do { \ - err = action; \ - if (err) \ - goto out; \ -} while (0) - -/* Copied from tools/perf/util/util.h */ #ifndef zfree# define zfree (ptr) ({ free(*ptr); *ptr = NULL; })
@@ -864,8 +857,7 @@ bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type, def->value_size = data->d_size; def->max_entries =1 ; def->map_flags = type ==LIBBPF_MAP_RODATA ?BPF_F_RDONLY_PROG :0 ; -if (obj->caps.array_mmap) - def->map_flags |=BPF_F_MMAPABLE ; + def->map_flags |=BPF_F_MMAPABLE ;pr_debug ("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n" , map_name, map->sec_idx, map->sec_offset, def->map_flags);
@@ -888,8 +880,6 @@ static int bpf_object__init_global_data_maps(struct bpf_object *obj) {int err; -if (!obj->caps.global_data) -return 0 ;/* * Populate obj->maps with libbpf internal maps. */
@@ -1393,10 +1383,11 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,return 0 ; } -static int bpf_object__init_maps (struct bpf_object *obj,bool relaxed_maps, -const char *pin_root_path) +static int bpf_object__init_maps (struct bpf_object *obj, +struct bpf_object_open_opts *opts) { -bool strict = !relaxed_maps; +const char *pin_root_path =OPTS_GET (opts, pin_root_path,NULL ); +bool strict = !OPTS_GET (opts, relaxed_maps, false);int err; err =bpf_object__init_user_maps (obj, strict);
@@ -1592,8 +1583,7 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)return 0 ; } -static int bpf_object__elf_collect (struct bpf_object *obj,bool relaxed_maps, -const char *pin_root_path) +static int bpf_object__elf_collect (struct bpf_object *obj) {Elf *elf = obj->efile.elf;GElf_Ehdr *ep = &obj->efile.ehdr;
@@ -1728,14 +1718,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj, bool relaxed_maps,pr_warn ("Corrupted ELF file: index of strtab invalid\n" );return -LIBBPF_ERRNO__FORMAT ; } - err =bpf_object__init_btf (obj, btf_data, btf_ext_data); -if (!err) - err =bpf_object__init_maps (obj, relaxed_maps, pin_root_path); -if (!err) - err =bpf_object__sanitize_and_load_btf (obj); -if (!err) - err =bpf_object__init_prog_names (obj); -return err; +return bpf_object__init_btf (obj, btf_data, btf_ext_data); } static struct bpf_program *
@@ -1875,11 +1858,6 @@ static int bpf_program__record_reloc(struct bpf_program *prog,pr_warn ("bad data relo against section %u\n" , shdr_idx);return -LIBBPF_ERRNO__RELOC ; } -if (!obj->caps.global_data) { -pr_warn ("relocation: kernel does not support global \'%s\' variable access in insns[%d]\n" , - name, insn_idx); -return -LIBBPF_ERRNO__RELOC ; - }for (map_idx =0 ; map_idx < nr_maps; map_idx++) { map = &obj->maps[map_idx];if (map->libbpf_type != type)
@@ -3917,12 +3895,10 @@ static struct bpf_object * __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz, struct bpf_object_open_opts *opts) { - const char *pin_root_path; struct bpf_program *prog;struct bpf_object *obj;const char *obj_name;char tmp_name[64 ]; -bool relaxed_maps;__u32 attach_prog_fd;int err;
@@ -3952,16 +3928,16 @@ __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,return obj; obj->relaxed_core_relocs =OPTS_GET (opts, relaxed_core_relocs, false); - relaxed_maps =OPTS_GET (opts, relaxed_maps, false); - pin_root_path =OPTS_GET (opts, pin_root_path,NULL ); attach_prog_fd =OPTS_GET (opts, attach_prog_fd,0 ); -CHECK_ERR (bpf_object__elf_init (obj), err, out); -CHECK_ERR (bpf_object__check_endianness (obj), err, out); -CHECK_ERR (bpf_object__probe_caps (obj), err, out); -CHECK_ERR (bpf_object__elf_collect (obj, relaxed_maps, pin_root_path), - err, out); -CHECK_ERR (bpf_object__collect_reloc (obj), err, out); + err =bpf_object__elf_init (obj); + err = err ? :bpf_object__check_endianness (obj); + err = err ? :bpf_object__elf_collect (obj); + err = err ? :bpf_object__init_maps (obj, opts); + err = err ? :bpf_object__init_prog_names (obj); + err = err ? :bpf_object__collect_reloc (obj); +if (err) + goto out;bpf_object__elf_finish (obj);bpf_object__for_each_program (prog, obj) {
@@ -4079,6 +4055,24 @@ int bpf_object__unload(struct bpf_object *obj)return 0 ; } +static int bpf_object__sanitize_maps (struct bpf_object *obj) +{ +struct bpf_map *m; + +bpf_object__for_each_map (m, obj) { +if (!bpf_map__is_internal (m)) +continue ; +if (!obj->caps.global_data) { +pr_warn ("kernel doesn't support global data\n" ); +return -ENOTSUP ; + } +if (!obj->caps.array_mmap) + m->def.map_flags ^=BPF_F_MMAPABLE ; + } + +return 0 ; +} +int bpf_object__load_xattr (struct bpf_object_load_attr *attr) {struct bpf_object *obj;
@@ -4097,9 +4091,14 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr) obj->loaded = true; -CHECK_ERR (bpf_object__create_maps (obj), err, out); -CHECK_ERR (bpf_object__relocate (obj, attr->target_btf_path), err, out); -CHECK_ERR (bpf_object__load_progs (obj, attr->log_level), err, out); + err =bpf_object__probe_caps (obj); + err = err ? :bpf_object__sanitize_and_load_btf (obj); + err = err ? :bpf_object__sanitize_maps (obj); + err = err ? :bpf_object__create_maps (obj); + err = err ? :bpf_object__relocate (obj, attr->target_btf_path); + err = err ? :bpf_object__load_progs (obj, attr->log_level); +if (err) + goto out;return 0 ; out:
Signature · 1 line
2.17.1
RFC PATCH 2/16 libbpf: add generic bpf_program__attach()
In reply to Andrii NakryikoGeneralize BPF program attaching and allow libbpf to auto-detect type (and extra parameters, where applicable) and attach supported BPF program types based on program sections. Currently this is supported for:
- kprobe/kretprobe;
- tracepoint;
- raw tracepoint;
- tracing programs (typed raw TP/fentry/fexit).
More types support can be trivially added within this framework.
--- tools/lib/bpf/libbpf.c | 181 ++++++++++++++---- tools/lib/bpf/libbpf.h | 2 + tools/lib/bpf/libbpf.map | 6 + .../selftests/bpf/prog_tests/probe_user.c | 6 +- 4 files changed, 157 insertions(+), 38 deletions(-)
tools/lib/bpf/libbpf.c +147 -34
@@ -4971,7 +4971,28 @@ void bpf_program__set_expected_attach_type(struct bpf_program *prog,*/ #define BPF_APROG_COMPAT (string, ptype) BPF_PROG_SEC(string, ptype) -static const struct { +#define SEC_DEF (sec_pfx, ptype, ...) { \ + .sec = sec_pfx, \ + .len = sizeof(sec_pfx) - 1, \ + .prog_type = BPF_PROG_TYPE_##ptype, \ + __VA_ARGS__ \ +} + +struct bpf_sec_def ; + +typedef struct bpf_link *(*attach_fn_t )(const struct bpf_sec_def *sec, +struct bpf_program *prog); + +static struct bpf_link *attach_kprobe (const struct bpf_sec_def *sec, +struct bpf_program *prog); +static struct bpf_link *attach_tp (const struct bpf_sec_def *sec, +struct bpf_program *prog); +static struct bpf_link *attach_raw_tp (const struct bpf_sec_def *sec, +struct bpf_program *prog); +static struct bpf_link *attach_trace (const struct bpf_sec_def *sec, +struct bpf_program *prog); + +struct bpf_sec_def {const char *sec;size_t len;enum bpf_prog_type prog_type;
@@ -4979,24 +5000,39 @@ static const struct {bool is_attachable;bool is_attach_btf;enum bpf_attach_type attach_type; -} section_names[] = { +attach_fn_t attach_fn; +}; + +static const struct bpf_sec_def section_defs[] = {BPF_PROG_SEC ("socket" ,BPF_PROG_TYPE_SOCKET_FILTER ), -BPF_PROG_SEC ("kprobe/" ,BPF_PROG_TYPE_KPROBE ), +SEC_DEF ("kprobe/" ,KPROBE , + .attach_fn = attach_kprobe),BPF_PROG_SEC ("uprobe/" ,BPF_PROG_TYPE_KPROBE ), -BPF_PROG_SEC ("kretprobe/" ,BPF_PROG_TYPE_KPROBE ), +SEC_DEF ("kretprobe/" ,KPROBE , + .attach_fn = attach_kprobe),BPF_PROG_SEC ("uretprobe/" ,BPF_PROG_TYPE_KPROBE ),BPF_PROG_SEC ("classifier" ,BPF_PROG_TYPE_SCHED_CLS ),BPF_PROG_SEC ("action" ,BPF_PROG_TYPE_SCHED_ACT ), -BPF_PROG_SEC ("tracepoint/" ,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_tp/" ,BPF_PROG_TYPE_RAW_TRACEPOINT ), -BPF_PROG_BTF ("tp_btf/" ,BPF_PROG_TYPE_TRACING , -BPF_TRACE_RAW_TP ), -BPF_PROG_BTF ("fentry/" ,BPF_PROG_TYPE_TRACING , -BPF_TRACE_FENTRY ), -BPF_PROG_BTF ("fexit/" ,BPF_PROG_TYPE_TRACING , -BPF_TRACE_FEXIT ), +SEC_DEF ("tracepoint/" ,TRACEPOINT , + .attach_fn = attach_tp), +SEC_DEF ("tp/" ,TRACEPOINT , + .attach_fn = attach_tp), +SEC_DEF ("raw_tracepoint/" ,RAW_TRACEPOINT , + .attach_fn = attach_raw_tp), +SEC_DEF ("raw_tp/" ,RAW_TRACEPOINT , + .attach_fn = attach_raw_tp), +SEC_DEF ("tp_btf/" ,TRACING , + .expected_attach_type = BPF_TRACE_RAW_TP, + .is_attach_btf = true, + .attach_fn = attach_trace), +SEC_DEF ("fentry/" ,TRACING , + .expected_attach_type = BPF_TRACE_FENTRY, + .is_attach_btf = true, + .attach_fn = attach_trace), +SEC_DEF ("fexit/" ,TRACING , + .expected_attach_type = BPF_TRACE_FEXIT, + .is_attach_btf = true, + .attach_fn = attach_trace),BPF_PROG_SEC ("xdp" ,BPF_PROG_TYPE_XDP ),BPF_PROG_SEC ("perf_event" ,BPF_PROG_TYPE_PERF_EVENT ),BPF_PROG_SEC ("lwt_in" ,BPF_PROG_TYPE_LWT_IN ),
@@ -5058,12 +5094,26 @@ static const struct {#undef BPF_APROG_SEC#undef BPF_EAPROG_SEC#undef BPF_APROG_COMPAT +#undef SEC_DEF#define MAX_TYPE_NAME_SIZE 32 +static const struct bpf_sec_def *find_sec_def (const char *sec_name) +{ +int i, n =ARRAY_SIZE (section_defs); + +for (i =0 ; i < n; i++) { +if (strncmp (sec_name, + section_defs[i].sec, section_defs[i].len)) +continue ; +return §ion_defs[i]; + } +return NULL ; +} +static char *libbpf_get_type_names (bool attach_type) { -int i, len =ARRAY_SIZE (section_names) *MAX_TYPE_NAME_SIZE ; +int i, len =ARRAY_SIZE (section_defs) *MAX_TYPE_NAME_SIZE ;char *buf; buf =malloc (len);
@@ -5072,16 +5122,16 @@ static char *libbpf_get_type_names(bool attach_type) buf[0 ] ='\0' ;/* Forge string buf with all available names */ -for (i =0 ; i <ARRAY_SIZE (section_names); i++) { -if (attach_type && !section_names[i].is_attachable) +for (i =0 ; i <ARRAY_SIZE (section_defs); i++) { +if (attach_type && !section_defs[i].is_attachable)continue ; -if (strlen (buf) +strlen (section_names[i].sec) +2 > len) { +if (strlen (buf) +strlen (section_defs[i].sec) +2 > len) {free (buf);return NULL ; }strcat (buf," " ); -strcat (buf, section_names[i].sec); +strcat (buf, section_defs[i].sec); }return buf;
@@ -5090,19 +5140,19 @@ static char *libbpf_get_type_names(bool attach_type)int libbpf_prog_type_by_name (const char *name,enum bpf_prog_type *prog_type,enum bpf_attach_type *expected_attach_type) { +const struct bpf_sec_def *sec_def;char *type_names; -int i;if (!name)return -EINVAL ; -for (i =0 ; i <ARRAY_SIZE (section_names); i++) { -if (strncmp (name, section_names[i].sec, section_names[i].len)) -continue ; - *prog_type = section_names[i].prog_type; - *expected_attach_type = section_names[i].expected_attach_type; + sec_def =find_sec_def (name); +if (sec_def) { + *prog_type = sec_def->prog_type; + *expected_attach_type = sec_def->expected_attach_type;return 0 ; } +pr_warn ("failed to guess program type from ELF section '%s'\n" , name); type_names =libbpf_get_type_names (false);if (type_names !=NULL ) {
@@ -5185,16 +5235,16 @@ static int libbpf_find_attach_btf_id(const char *name,if (!name)return -EINVAL ; -for (i =0 ; i <ARRAY_SIZE (section_names); i++) { -if (!section_names[i].is_attach_btf) +for (i =0 ; i <ARRAY_SIZE (section_defs); i++) { +if (!section_defs[i].is_attach_btf)continue ; -if (strncmp (name, section_names[i].sec, section_names[i].len)) +if (strncmp (name, section_defs[i].sec, section_defs[i].len))continue ;if (attach_prog_fd) - err =libbpf_find_prog_btf_id (name + section_names[i].len, + err =libbpf_find_prog_btf_id (name + section_defs[i].len, attach_prog_fd);else - err =libbpf_find_vmlinux_btf_id (name + section_names[i].len, + err =libbpf_find_vmlinux_btf_id (name + section_defs[i].len, attach_type);if (err <=0 )pr_warn ("%s is not found in vmlinux BTF\n" , name);
@@ -5213,12 +5263,12 @@ int libbpf_attach_type_by_name(const char *name,if (!name)return -EINVAL ; -for (i =0 ; i <ARRAY_SIZE (section_names); i++) { -if (strncmp (name, section_names[i].sec, section_names[i].len)) +for (i =0 ; i <ARRAY_SIZE (section_defs); i++) { +if (strncmp (name, section_defs[i].sec, section_defs[i].len))continue ; -if (!section_names[i].is_attachable) +if (!section_defs[i].is_attachable)return -EINVAL ; - *attach_type = section_names[i].attach_type; + *attach_type = section_defs[i].attach_type;return 0 ; }pr_warn ("failed to guess attach type based on ELF section name '%s'\n" , name);
@@ -5678,6 +5728,18 @@ struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,return link; } +static struct bpf_link *attach_kprobe (const struct bpf_sec_def *sec, +struct bpf_program *prog) +{ +const char *func_name; +bool retprobe; + + func_name =bpf_program__title (prog, false) + sec->len; + retprobe =strcmp (sec->sec,"kretprobe/" ) ==0 ; + +return bpf_program__attach_kprobe (prog, retprobe, func_name); +} + struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog, bool retprobe, pid_t pid, const char *binary_path,
@@ -5790,6 +5852,32 @@ struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog,return link; } +static struct bpf_link *attach_tp (const struct bpf_sec_def *sec, +struct bpf_program *prog) +{ +char *sec_name, *tp_cat, *tp_name; +struct bpf_link *link; + + sec_name =strdup (bpf_program__title (prog, false)); +if (!sec_name) +return ERR_PTR (-ENOMEM ); + +/* extract "tp/<category>/<name>" */ + tp_cat = sec_name + sec->len; + tp_name =strchr (tp_cat,'/' ); +if (!tp_name) { + link =ERR_PTR (-EINVAL ); + goto out; + } + *tp_name ='\0' ; + tp_name++; + + link =bpf_program__attach_tracepoint (prog, tp_cat, tp_name); +out: +free (sec_name); +return link; +} +static int bpf_link__destroy_fd (struct bpf_link *link) {struct bpf_link_fd *l = (void *)link;
@@ -5829,6 +5917,14 @@ struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog,return (struct bpf_link *)link; } +static struct bpf_link *attach_raw_tp (const struct bpf_sec_def *sec, +struct bpf_program *prog) +{ +const char *tp_name =bpf_program__title (prog, false) + sec->len; + +return bpf_program__attach_raw_tracepoint (prog, tp_name); +} +struct bpf_link *bpf_program__attach_trace (struct bpf_program *prog) {char errmsg[STRERR_BUFSIZE ];
@@ -5860,6 +5956,23 @@ struct bpf_link *bpf_program__attach_trace(struct bpf_program *prog)return (struct bpf_link *)link; } +static struct bpf_link *attach_trace (const struct bpf_sec_def *sec, +struct bpf_program *prog) +{ +return bpf_program__attach_trace (prog); +} + +struct bpf_link *bpf_program__attach (struct bpf_program *prog) +{ +const struct bpf_sec_def *sec_def; + + sec_def =find_sec_def (bpf_program__title (prog, false)); +if (!sec_def || !sec_def->attach_fn) +return ERR_PTR (-ESRCH ); + +return sec_def->attach_fn (sec_def, prog); +} + enum bpf_perf_event_ret bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size, void **copy_mem, size_t *copy_size,
tools/lib/bpf/libbpf.h +2 -0
@@ -237,6 +237,8 @@ struct bpf_link; LIBBPF_API int bpf_link__destroy(struct bpf_link *link); +LIBBPF_API struct bpf_link * +bpf_program__attach (struct bpf_program *prog); LIBBPF_API struct bpf_link *bpf_program__attach_perf_event (struct bpf_program *prog,int pfd); LIBBPF_API struct bpf_link *
tools/lib/bpf/libbpf.map +6 -0
@@ -203,8 +203,14 @@ LIBBPF_0.0.6 { bpf_program__get_expected_attach_type; bpf_program__get_type; bpf_program__is_tracing; + bpf_program__name; bpf_program__set_tracing; bpf_program__size; btf__find_by_name_kind; libbpf_find_vmlinux_btf_id; } LIBBPF_0.0.5; + +LIBBPF_0.0.7 { + global: + bpf_program__attach; +} LIBBPF_0.0.6;
tools/testing/selftests/bpf/prog_tests/probe_user.c +2 -4
@@ -3,8 +3,7 @@void test_probe_user (void ) { -#define kprobe_name "__sys_connect" -const char *prog_name ="kprobe/" kprobe_name; +const char *prog_name ="kprobe/__sys_connect" ;const char *obj_file ="./test_probe_user.o" ; DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, );int err, results_map_fd, sock_fd, duration =0 ;
@@ -33,8 +32,7 @@ void test_probe_user(void) "err %d\n", results_map_fd)) goto cleanup; - kprobe_link =bpf_program__attach_kprobe (kprobe_prog, false, - kprobe_name); + kprobe_link =bpf_program__attach (kprobe_prog);if (CHECK (IS_ERR (kprobe_link),"attach_kprobe" ,"err %ld\n" ,PTR_ERR (kprobe_link))) { kprobe_link =NULL ;
Signature · 1 line
2.17.1
RFC PATCH 3/16 libbpf: move non-public APIs from libbpf.h to libbpf_internal.h
In reply to Andrii NakryikoFew libbpf APIs are not public but currently exposed through libbpf.h to be used by bpftool. Move them to libbpf_internal.h, where intent of being non-stable and non-public is much more obvious.
--- tools/bpf/bpftool/net.c | 1 + tools/lib/bpf/libbpf.h | 17 ----------------- tools/lib/bpf/libbpf.map | 1 - tools/lib/bpf/libbpf_internal.h | 17 +++++++++++++++++ 4 files changed, 18 insertions(+), 18 deletions(-)
tools/bpf/bpftool/net.c +1 -0
@@ -18,6 +18,7 @@#include <bpf.h> #include <nlattr.h> +#include "libbpf_internal.h" #include "main.h" #include "netlink_dumper.h"
tools/lib/bpf/libbpf.h +0 -17
@@ -126,11 +126,6 @@ bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz, LIBBPF_API struct bpf_object *bpf_object__open_xattr (struct bpf_object_open_attr *attr); -int bpf_object__section_size (const struct bpf_object *obj,const char *name, -__u32 *size); -int bpf_object__variable_offset (const struct bpf_object *obj,const char *name, -__u32 *off); -enum libbpf_pin_type {LIBBPF_PIN_NONE ,/* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
@@ -514,18 +509,6 @@ bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size, void **copy_mem, size_t *copy_size, bpf_perf_event_print_t fn, void *private_data); -struct nlattr ; -typedef int (*libbpf_dump_nlmsg_t )(void *cookie,void *msg,struct nlattr **tb); -int libbpf_netlink_open (unsigned int *nl_pid); -int libbpf_nl_get_link (int sock,unsigned int nl_pid, -libbpf_dump_nlmsg_t dump_link_nlmsg,void *cookie); -int libbpf_nl_get_class (int sock,unsigned int nl_pid,int ifindex, -libbpf_dump_nlmsg_t dump_class_nlmsg,void *cookie); -int libbpf_nl_get_qdisc (int sock,unsigned int nl_pid,int ifindex, -libbpf_dump_nlmsg_t dump_qdisc_nlmsg,void *cookie); -int libbpf_nl_get_filter (int sock,unsigned int nl_pid,int ifindex,int handle, -libbpf_dump_nlmsg_t dump_filter_nlmsg,void *cookie); -struct bpf_prog_linfo ;struct bpf_prog_info ;
tools/lib/bpf/libbpf.map +0 -1
@@ -203,7 +203,6 @@ LIBBPF_0.0.6 { bpf_program__get_expected_attach_type; bpf_program__get_type; bpf_program__is_tracing; - bpf_program__name; bpf_program__set_tracing; bpf_program__size; btf__find_by_name_kind;
tools/lib/bpf/libbpf_internal.h +17 -0
@@ -98,6 +98,23 @@ static inline bool libbpf_validate_opts(const char *opts,int libbpf__load_raw_btf (const char *raw_types,size_t types_len,const char *str_sec,size_t str_len); +int bpf_object__section_size (const struct bpf_object *obj,const char *name, +__u32 *size); +int bpf_object__variable_offset (const struct bpf_object *obj,const char *name, +__u32 *off); + +struct nlattr ; +typedef int (*libbpf_dump_nlmsg_t )(void *cookie,void *msg,struct nlattr **tb); +int libbpf_netlink_open (unsigned int *nl_pid); +int libbpf_nl_get_link (int sock,unsigned int nl_pid, +libbpf_dump_nlmsg_t dump_link_nlmsg,void *cookie); +int libbpf_nl_get_class (int sock,unsigned int nl_pid,int ifindex, +libbpf_dump_nlmsg_t dump_class_nlmsg,void *cookie); +int libbpf_nl_get_qdisc (int sock,unsigned int nl_pid,int ifindex, +libbpf_dump_nlmsg_t dump_qdisc_nlmsg,void *cookie); +int libbpf_nl_get_filter (int sock,unsigned int nl_pid,int ifindex,int handle, +libbpf_dump_nlmsg_t dump_filter_nlmsg,void *cookie); +struct btf_ext_info {/* * info points to the individual info section (e.g. func_info and
Signature · 1 line
2.17.1
RFC PATCH 4/16 libbpf: add BPF_EMBED_OBJ macro for embedding BPF .o files
In reply to Andrii NakryikoAdd a convenience macro BPF_EMBED_OBJ, which allows to embed other files (typically used to embed BPF .o files) into a hosting userspace programs. To C program it is exposed as struct bpf_embed_data, containing a pointer to raw data and its size in bytes.
--- tools/lib/bpf/libbpf.h | 35 +++++++++++++++++++ .../selftests/bpf/prog_tests/attach_probe.c | 23 ++---------- 2 files changed, 38 insertions(+), 20 deletions(-)
tools/lib/bpf/libbpf.h +35 -0
@@ -615,6 +615,41 @@ bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);*/ LIBBPF_API int libbpf_num_possible_cpus(void); +struct bpf_embed_data { +void *data; +size_t size; +}; + +#define BPF_EMBED_OBJ_DECLARE (NAME ) \ +extern struct bpf_embed_data NAME##_embed; \ +extern char NAME##_data[]; \ +extern char NAME##_data_end[]; + +#define __BPF_EMBED_OBJ (NAME ,PATH ,SZ ,ASM_TYPE ) \ +asm ( \ +" .pushsection \".rodata\", \"a\", @progbits \n" \ +" .global "#NAME"_data \n" \ +#NAME"_data: \n" \ +" .incbin \"" PATH "\" \n" \ +" .global "#NAME"_data_end \n" \ +#NAME"_data_end: \n" \ +" .global "#NAME"_embed \n" \ +" .type "#NAME"_embed, @object \n" \ +" .size "#NAME"_size, "#SZ" \n" \ +" .align 8, \n" \ +#NAME"_embed: \n" \ +" "ASM_TYPE" "#NAME"_data \n" \ +" "ASM_TYPE" "#NAME"_data_end - "#NAME"_data \n" \ +" .popsection \n" \ +); \ +BPF_EMBED_OBJ_DECLARE(NAME) + +#if __SIZEOF_POINTER__ ==4 +#define BPF_EMBED_OBJ (NAME ,PATH ) __BPF_EMBED_OBJ(NAME, PATH, 8, ".long") +#else +#define BPF_EMBED_OBJ (NAME ,PATH ) __BPF_EMBED_OBJ(NAME, PATH, 16, ".quad") +#endif + #ifdef __cplusplus }/* extern "C" */ #endif
tools/testing/selftests/bpf/prog_tests/attach_probe.c +3 -20
@@ -1,24 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> -#define EMBED_FILE (NAME ,PATH ) \ -asm ( \ -" .pushsection \".rodata\", \"a\", @progbits \n" \ -" .global "#NAME"_data \n" \ -#NAME"_data: \n" \ -" .incbin \"" PATH "\" \n" \ -#NAME"_data_end: \n" \ -" .global "#NAME"_size \n" \ -" .type "#NAME"_size, @object \n" \ -" .size "#NAME"_size, 4 \n" \ -" .align 4, \n" \ -#NAME"_size: \n" \ -" .int "#NAME"_data_end - "#NAME"_data \n" \ -" .popsection \n" \ -); \ -extern char NAME##_data[]; \ -extern int NAME##_size; -ssize_t get_base_addr () {size_t start;char buf[256 ];
@@ -39,7 +21,7 @@ ssize_t get_base_addr() {return -EINVAL ; } -EMBED_FILE (probe,"test_attach_probe.o" ); +BPF_EMBED_OBJ (probe,"test_attach_probe.o" );void test_attach_probe (void ) {
@@ -73,7 +55,8 @@ void test_attach_probe(void) uprobe_offset = (size_t )&get_base_addr - base_addr;/* open object */ - obj =bpf_object__open_mem (probe_data, probe_size, &open_opts); + obj =bpf_object__open_mem (probe_embed.data, probe_embed.size, + &open_opts);if (CHECK (IS_ERR (obj),"obj_open_mem" ,"err %ld\n" ,PTR_ERR (obj)))return ;
Signature · 1 line
2.17.1
RFC PATCH 5/16 libbpf: expose field/var declaration emitting API internally
In reply to Andrii NakryikoExpose useful function emitting field/variable declaration compilable C syntax of a provided BTF type. This is going to be used by bpftool when emitting data section layout as a struct. This is not a trivial algorithm, so better to expose it as internal API and let bpftool harness existing libbpf code. As part of making this API useful in a stand-alone fashion, move initialization of some of the internal btf_dumper state to early phase.
Also expose useful btf_align_of() function returning alignment of a given BTF type. This is also used for data section layout-compatible struct definition, specifically to determine necessary extra padding between fields/variables.
--- tools/lib/bpf/btf_dump.c | 61 ++++++++++++++++----------------- tools/lib/bpf/libbpf_internal.h | 6 ++++ 2 files changed, 36 insertions(+), 31 deletions(-)
tools/lib/bpf/btf_dump.c +30 -31
@@ -116,6 +116,8 @@ static void btf_dump_printf(const struct btf_dump *d, const char *fmt, ...)va_end (args); } +static int btf_dump_mark_referenced (struct btf_dump *d); + struct btf_dump *btf_dump__new(const struct btf *btf, const struct btf_ext *btf_ext, const struct btf_dump_opts *opts,
@@ -137,18 +139,39 @@ struct btf_dump *btf_dump__new(const struct btf *btf,if (IS_ERR (d->type_names)) { err =PTR_ERR (d->type_names); d->type_names =NULL ; -btf_dump__free (d); -return ERR_PTR (err); } d->ident_names =hashmap__new (str_hash_fn, str_equal_fn,NULL );if (IS_ERR (d->ident_names)) { err =PTR_ERR (d->ident_names); d->ident_names =NULL ; -btf_dump__free (d); -return ERR_PTR (err); + goto err; + } + d->type_states =calloc (1 +btf__get_nr_types (d->btf), +sizeof (d->type_states[0 ])); +if (!d->type_states) { + err = -ENOMEM ; + goto err; + } + d->cached_names =calloc (1 +btf__get_nr_types (d->btf), +sizeof (d->cached_names[0 ])); +if (!d->cached_names) { + err = -ENOMEM ; + goto err; } +/* VOID is special */ + d->type_states[0 ].order_state =ORDERED ; + d->type_states[0 ].emit_state =EMITTED ; + +/* eagerly determine referenced types for anon enums */ + err =btf_dump_mark_referenced (d); +if (err) + goto err; +return d; +err: +btf_dump__free (d); +return ERR_PTR (err); }void btf_dump__free (struct btf_dump *d)
@@ -175,7 +198,6 @@ void btf_dump__free(struct btf_dump *d)free (d); } -static int btf_dump_mark_referenced (struct btf_dump *d);static int btf_dump_order_type (struct btf_dump *d,__u32 id,bool through_ptr);static void btf_dump_emit_type (struct btf_dump *d,__u32 id,__u32 cont_id);
@@ -202,27 +224,6 @@ int btf_dump__dump_type(struct btf_dump *d, __u32 id)if (id >btf__get_nr_types (d->btf))return -EINVAL ; -/* type states are lazily allocated, as they might not be needed */ -if (!d->type_states) { - d->type_states =calloc (1 +btf__get_nr_types (d->btf), -sizeof (d->type_states[0 ])); -if (!d->type_states) -return -ENOMEM ; - d->cached_names =calloc (1 +btf__get_nr_types (d->btf), -sizeof (d->cached_names[0 ])); -if (!d->cached_names) -return -ENOMEM ; - -/* VOID is special */ - d->type_states[0 ].order_state =ORDERED ; - d->type_states[0 ].emit_state =EMITTED ; - -/* eagerly determine referenced types for anon enums */ - err =btf_dump_mark_referenced (d); -if (err) -return err; - } - d->emit_queue_cnt =0 ; err =btf_dump_order_type (d, id, false);if (err <0 )
@@ -565,8 +566,6 @@ struct id_stack {int cnt; }; -static void btf_dump_emit_type_decl (struct btf_dump *d,__u32 id, -const char *fname,int lvl);static void btf_dump_emit_type_chain (struct btf_dump *d,struct id_stack *decl_stack,const char *fname,int lvl);
@@ -752,7 +751,7 @@ static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id) } } -static int btf_align_of (const struct btf *btf,__u32 id) +int btf_align_of (const struct btf *btf,__u32 id) {const struct btf_type *t =btf__type_by_id (btf, id);__u16 kind =btf_kind (t);
@@ -1051,8 +1050,8 @@ static int btf_dump_push_decl_stack_id(struct btf_dump *d, __u32 id)* of a stack frame. Some care is required to "pop" stack frames after * processing type declaration chain. */ -static void btf_dump_emit_type_decl (struct btf_dump *d,__u32 id, -const char *fname,int lvl) +void btf_dump_emit_type_decl (struct btf_dump *d,__u32 id, +const char *fname,int lvl) {struct id_stack decl_stack;const struct btf_type *t;
tools/lib/bpf/libbpf_internal.h +6 -0
@@ -98,6 +98,12 @@ static inline bool libbpf_validate_opts(const char *opts,int libbpf__load_raw_btf (const char *raw_types,size_t types_len,const char *str_sec,size_t str_len); +struct btf_dump ; + +void btf_dump_emit_type_decl (struct btf_dump *d,__u32 id,const char *fname, +int lvl); +int btf_align_of (const struct btf *btf,__u32 id); +int bpf_object__section_size (const struct bpf_object *obj,const char *name,__u32 *size); int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
Signature · 1 line
2.17.1
RFC PATCH 6/16 libbpf: expose BPF program's function name
In reply to Andrii NakryikoAdd APIs to get BPF program function name, as opposed to bpf_program__title(), which returns BPF program function's section name. Function name has a benefit of being a valid C identifier and uniquely identifies a specific BPF program, while section name can be duplicated across multiple independent BPF programs.
Add also bpf_object__find_program_by_name(), similar to bpf_object__find_program_by_title(), to facilitate looking up BPF programs by their C function names.
Convert one of selftests to new API for look up.
--- tools/lib/bpf/libbpf.c | 28 +++++++++++++++---- tools/lib/bpf/libbpf.h | 9 ++++-- tools/lib/bpf/libbpf.map | 2 ++ .../selftests/bpf/prog_tests/rdonly_maps.c | 11 +++----- 4 files changed, 36 insertions(+), 14 deletions(-)
tools/lib/bpf/libbpf.c +23 -5
@@ -209,8 +209,8 @@ static const char * const libbpf_type_to_btf_name[] = { };struct bpf_map { -int fd;char *name; +int fd;int sec_idx;size_t sec_offset;int map_ifindex;
@@ -1384,7 +1384,7 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict, }static int bpf_object__init_maps (struct bpf_object *obj, -struct bpf_object_open_opts *opts) +const struct bpf_object_open_opts *opts) {const char *pin_root_path =OPTS_GET (opts, pin_root_path,NULL );bool strict = !OPTS_GET (opts, relaxed_maps, false);
@@ -1748,6 +1748,19 @@ bpf_object__find_program_by_title(const struct bpf_object *obj,return NULL ; } +struct bpf_program * +bpf_object__find_program_by_name (const struct bpf_object *obj, +const char *name) +{ +struct bpf_program *prog; + +bpf_object__for_each_program (prog, obj) { +if (!strcmp (prog->name, name)) +return prog; + } +return NULL ; +} +static bool bpf_object__shndx_is_data (const struct bpf_object *obj,int shndx) {
@@ -3893,7 +3906,7 @@ static int libbpf_find_attach_btf_id(const char *name, __u32 attach_prog_fd);static struct bpf_object *__bpf_object__open (const char *path,const void *obj_buf,size_t obj_buf_sz, -struct bpf_object_open_opts *opts) +const struct bpf_object_open_opts *opts) {struct bpf_program *prog;struct bpf_object *obj;
@@ -4002,7 +4015,7 @@ struct bpf_object *bpf_object__open(const char *path) }struct bpf_object * -bpf_object__open_file (const char *path,struct bpf_object_open_opts *opts) +bpf_object__open_file (const char *path,const struct bpf_object_open_opts *opts) {if (!path)return ERR_PTR (-EINVAL );
@@ -4014,7 +4027,7 @@ bpf_object__open_file(const char *path, struct bpf_object_open_opts *opts)struct bpf_object *bpf_object__open_mem (const void *obj_buf,size_t obj_buf_sz, -struct bpf_object_open_opts *opts) +const struct bpf_object_open_opts *opts) {if (!obj_buf || obj_buf_sz ==0 )return ERR_PTR (-EINVAL );
@@ -4819,6 +4832,11 @@ void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex) prog->prog_ifindex = ifindex; } +const char *bpf_program__name (const struct bpf_program *prog) +{ +return prog->name; +} +const char *bpf_program__title (const struct bpf_program *prog,bool needs_copy) {const char *title;
tools/lib/bpf/libbpf.h +7 -2
@@ -114,10 +114,10 @@ struct bpf_object_open_opts { LIBBPF_API struct bpf_object *bpf_object__open(const char *path); LIBBPF_API struct bpf_object * -bpf_object__open_file (const char *path,struct bpf_object_open_opts *opts); +bpf_object__open_file (const char *path,const struct bpf_object_open_opts *opts); LIBBPF_API struct bpf_object *bpf_object__open_mem (const void *obj_buf,size_t obj_buf_sz, -struct bpf_object_open_opts *opts); +const struct bpf_object_open_opts *opts);/* deprecated bpf_object__open variants */ LIBBPF_API struct bpf_object *
@@ -156,6 +156,7 @@ struct bpf_object_load_attr { LIBBPF_API int bpf_object__load(struct bpf_object *obj); LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr); LIBBPF_API int bpf_object__unload(struct bpf_object *obj); + LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj); LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
@@ -166,6 +167,9 @@ LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj); LIBBPF_API struct bpf_program *bpf_object__find_program_by_title (const struct bpf_object *obj,const char *title); +LIBBPF_API struct bpf_program * +bpf_object__find_program_by_name (const struct bpf_object *obj, +const char *name); LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);#define bpf_object__for_each_safe (pos, tmp) \
@@ -209,6 +213,7 @@ LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog); LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,__u32 ifindex); +LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog); LIBBPF_API const char *bpf_program__title(const struct bpf_program *prog,bool needs_copy);
tools/lib/bpf/libbpf.map +2 -0
@@ -211,5 +211,7 @@ LIBBPF_0.0.6 { LIBBPF_0.0.7 { global: + bpf_object__find_program_by_name; bpf_program__attach; + bpf_program__name; } LIBBPF_0.0.6;
tools/testing/selftests/bpf/prog_tests/rdonly_maps.c +4 -7
@@ -16,14 +16,11 @@ struct rdonly_map_subtest {void test_rdonly_maps (void ) { -const char *prog_name_skip_loop ="raw_tracepoint/sys_enter:skip_loop" ; -const char *prog_name_part_loop ="raw_tracepoint/sys_enter:part_loop" ; -const char *prog_name_full_loop ="raw_tracepoint/sys_enter:full_loop" ;const char *file ="test_rdonly_maps.o" ;struct rdonly_map_subtest subtests[] = { - {"skip loop" , prog_name_skip_loop,0 ,0 }, - {"part loop" , prog_name_part_loop,3 ,2 +3 +4 }, - {"full loop" , prog_name_full_loop,4 ,2 +3 +4 +5 }, + {"skip loop" ,"skip_loop" ,0 ,0 }, + {"part loop" ,"part_loop" ,3 ,2 +3 +4 }, + {"full loop" ,"full_loop" ,4 ,2 +3 +4 +5 }, };int i, err, zero =0 , duration =0 ;struct bpf_link *link =NULL ;
@@ -50,7 +47,7 @@ void test_rdonly_maps(void)if (!test__start_subtest (t->subtest_name))continue ; - prog =bpf_object__find_program_by_title (obj, t->prog_name); + prog =bpf_object__find_program_by_name (obj, t->prog_name);if (CHECK (!prog,"find_prog" ,"prog '%s' not found\n" , t->prog_name)) goto cleanup;
Signature · 1 line
2.17.1
RFC PATCH 7/16 libbpf: refactor global data map initialization
In reply to Andrii NakryikoRefactor global data map initialization to use anonymous mmap()-ed memory instead of malloc()-ed one. This allows to do a transparent re-mmap()-ing of already existing memory address to point to BPF map's memory after bpf_object__load() step (done in follow up patch). This choreographed setup allows to have a nice and unsurprising way to pre-initialize read-only (and r/w as well) maps by user and after BPF map creation keep working with mmap()-ed contents of this map. All in a way that doesn't require user code to update any pointers: the illusion of working with memory contents is preserved before and after actual BPF map instantiation.
Selftests and runqslower example demonstrate this feature in follow up patches.
--- tools/lib/bpf/libbpf.c | 92 +++++++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 37 deletions(-)
tools/lib/bpf/libbpf.c +55 -37
@@ -221,16 +221,12 @@ struct bpf_map {void *priv;bpf_map_clear_priv_t clear_priv;enum libbpf_map_type libbpf_type; +void *mmaped;char *pin_path;bool pinned;bool reused; }; -struct bpf_secdata { -void *rodata; -void *data; -}; -static LIST_HEAD (bpf_objects_list);struct bpf_object {
@@ -243,7 +239,6 @@ struct bpf_object {struct bpf_map *maps;size_t nr_maps;size_t maps_cap; -struct bpf_secdata sections;bool loaded;bool has_pseudo_calls;
@@ -828,13 +823,24 @@ static struct bpf_map *bpf_object__add_map(struct bpf_object *obj)return &obj->maps[obj->nr_maps++]; } +static size_t bpf_map_mmap_sz (const struct bpf_map *map) +{ +long page_sz =sysconf (_SC_PAGE_SIZE); +size_t map_sz; + + map_sz =roundup (map->def.value_size,8 ) * map->def.max_entries; + map_sz =roundup (map_sz, page_sz); +return map_sz; +} +static int bpf_object__init_internal_map (struct bpf_object *obj,enum libbpf_map_type type, -int sec_idx,Elf_Data *data,void **data_buff) +int sec_idx,Elf_Data *data) {char map_name[BPF_OBJ_NAME_LEN ];struct bpf_map_def *def;struct bpf_map *map; +int err; map =bpf_object__add_map (obj);if (IS_ERR (map))
@@ -862,16 +868,20 @@ bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,pr_debug ("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n" , map_name, map->sec_idx, map->sec_offset, def->map_flags); -if (data_buff) { - *data_buff =malloc (data->d_size); -if (!*data_buff) { -zfree (&map->name); -pr_warn ("failed to alloc map content buffer\n" ); -return -ENOMEM ; - } -memcpy (*data_buff, data->d_buf, data->d_size); + map->mmaped =mmap (NULL ,bpf_map_mmap_sz (map),PROT_READ |PROT_WRITE , +MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); +if (map->mmaped ==MAP_FAILED ) { + err = -errno; + map->mmaped =NULL ; +pr_warn ("failed to alloc map '%s' content buffer: %d\n" , + map->name, err); +zfree (&map->name); +return err; } +if (type !=LIBBPF_MAP_BSS ) +memcpy (map->mmaped, data->d_buf, data->d_size); +pr_debug ("map %td is \"%s\"\n" , map - obj->maps, map->name);return 0 ; }
@@ -886,23 +896,21 @@ static int bpf_object__init_global_data_maps(struct bpf_object *obj)if (obj->efile.data_shndx >=0 ) { err =bpf_object__init_internal_map (obj,LIBBPF_MAP_DATA , obj->efile.data_shndx, - obj->efile.data, - &obj->sections.data); + obj->efile.data);if (err)return err; }if (obj->efile.rodata_shndx >=0 ) { err =bpf_object__init_internal_map (obj,LIBBPF_MAP_RODATA , obj->efile.rodata_shndx, - obj->efile.rodata, - &obj->sections.rodata); + obj->efile.rodata);if (err)return err; }if (obj->efile.bss_shndx >=0 ) { err =bpf_object__init_internal_map (obj,LIBBPF_MAP_BSS , obj->efile.bss_shndx, - obj->efile.bss,NULL ); + obj->efile.bss);if (err)return err; }
@@ -2291,27 +2299,32 @@ bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map) {char *cp, errmsg[STRERR_BUFSIZE ];int err, zero =0 ; -__u8 *data;/* Nothing to do here since kernel already zero-initializes .bss map. */ if (map->libbpf_type ==LIBBPF_MAP_BSS )return 0 ; - data = map->libbpf_type ==LIBBPF_MAP_DATA ? - obj->sections.data : obj->sections.rodata; + err =bpf_map_update_elem (map->fd, &zero, map->mmaped,0 ); +if (err) { + err = -errno; + cp =libbpf_strerror_r (err, errmsg,sizeof (errmsg)); +pr_warn ("Error setting initial map(%s) contents: %s\n" , + map->name, cp); +return err; + } - err =bpf_map_update_elem (map->fd, &zero, data,0 );/* Freeze .rodata map as read-only from syscall side. */ -if (!err && map->libbpf_type ==LIBBPF_MAP_RODATA ) { +if (map->libbpf_type ==LIBBPF_MAP_RODATA ) { err =bpf_map_freeze (map->fd);if (err) { - cp =libbpf_strerror_r (errno, errmsg,sizeof (errmsg)); + err = -errno; + cp =libbpf_strerror_r (err, errmsg,sizeof (errmsg));pr_warn ("Error freezing map(%s) as read-only: %s\n" , map->name, cp); - err =0 ; +return err; } } -return err; +return 0 ; } static int
@@ -4682,17 +4695,22 @@ void bpf_object__close(struct bpf_object *obj)btf_ext__free (obj->btf_ext);for (i =0 ; i < obj->nr_maps; i++) { -zfree (&obj->maps[i].name); -zfree (&obj->maps[i].pin_path); -if (obj->maps[i].clear_priv) - obj->maps[i].clear_priv (&obj->maps[i], - obj->maps[i].priv); - obj->maps[i].priv =NULL ; - obj->maps[i].clear_priv =NULL ; +struct bpf_map *map = &obj->maps[i]; + +if (map->clear_priv) + map->clear_priv (map, map->priv); + map->priv =NULL ; + map->clear_priv =NULL ; + +if (map->mmaped) { +munmap (map->mmaped,bpf_map_mmap_sz (map)); + map->mmaped =NULL ; + } + +zfree (&map->name); +zfree (&map->pin_path); } -zfree (&obj->sections.rodata); -zfree (&obj->sections.data);zfree (&obj->maps); obj->nr_maps =0 ;
Signature · 1 line
2.17.1
RFC PATCH 8/16 libbpf: postpone BTF ID finding for TRACING programs to load phase
In reply to Andrii NakryikoMove BTF ID determination for BPF_PROG_TYPE_TRACING programs to a load phase. Performing it at open step is inconvenient, because it prevents BPF skeleton generation on older host kernel, which doesn't contain BTF_KIND_FUNCs information in vmlinux BTF. This is a common set up, though, when, e.g., selftests are compiled on older host kernel, but the test program itself is executed in qemu VM with bleeding edge kernel. Having this BTF searching performed at load time allows to successfully use bpf_object__open() for codegen and inspection of BPF object file.
--- tools/lib/bpf/libbpf.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-)
tools/lib/bpf/libbpf.c +18 -19
@@ -3811,11 +3811,22 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,return ret; } -int -bpf_program__load (struct bpf_program *prog, -char *license,__u32 kern_version) +static int libbpf_find_attach_btf_id (const char *name, +enum bpf_attach_type attach_type, +__u32 attach_prog_fd); + +int bpf_program__load (struct bpf_program *prog,char *license,__u32 kern_ver) { -int err =0 , fd, i; +int err =0 , fd, i, btf_id; + +if (prog->type ==BPF_PROG_TYPE_TRACING ) { + btf_id =libbpf_find_attach_btf_id (prog->section_name, + prog->expected_attach_type, + prog->attach_prog_fd); +if (btf_id <=0 ) +return btf_id; + prog->attach_btf_id = btf_id; + }if (prog->instances.nr <0 || !prog->instances.fds) {if (prog->preprocessor) {
@@ -3839,7 +3850,7 @@ bpf_program__load(struct bpf_program *prog, prog->section_name, prog->instances.nr); } err =load_program (prog, prog->insns, prog->insns_cnt, - license, kern_version, &fd); + license, kern_ver, &fd);if (!err) prog->instances.fds[0 ] = fd; goto out;
@@ -3868,9 +3879,7 @@ bpf_program__load(struct bpf_program *prog, } err =load_program (prog, result.new_insn_ptr, - result.new_insn_cnt, - license, kern_version, &fd); - + result.new_insn_cnt, license, kern_ver, &fd);if (err) {pr_warn ("Loading the %dth instance of program '%s' failed\n" , i, prog->section_name);
@@ -3914,9 +3923,6 @@ bpf_object__load_progs(struct bpf_object *obj, int log_level)return 0 ; } -static int libbpf_find_attach_btf_id (const char *name, -enum bpf_attach_type attach_type, -__u32 attach_prog_fd);static struct bpf_object *__bpf_object__open (const char *path,const void *obj_buf,size_t obj_buf_sz,const struct bpf_object_open_opts *opts)
@@ -3980,15 +3986,8 @@ __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_expected_attach_type (prog, attach_type); -if (prog_type ==BPF_PROG_TYPE_TRACING ) { - err =libbpf_find_attach_btf_id (prog->section_name, - attach_type, - attach_prog_fd); -if (err <=0 ) - goto out; - prog->attach_btf_id = err; +if (prog_type ==BPF_PROG_TYPE_TRACING ) prog->attach_prog_fd = attach_prog_fd; - } }return obj;
Signature · 1 line
2.17.1
RFC PATCH 9/16 libbpf: reduce log level of supported section names dump
In reply to Andrii NakryikoIt's quite spammy. And now that bpf_object__open() is trying to determine program type from its section name, we are getting these verbose messages all the time. Reduce their log level to DEBUG.
--- tools/lib/bpf/libbpf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -1
@@ -5191,7 +5191,7 @@ int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,pr_warn ("failed to guess program type from ELF section '%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); +pr_debug ("supported section(type) names are:%s\n" , type_names);free (type_names); }
Signature · 1 line
2.17.1
RFC PATCH 10/16 libbpf: add experimental BPF object skeleton support
In reply to Andrii NakryikoAdd new set of APIs (currently experimental and not public), allowing to open/load/attach BPF object through BPF object skeleton, generated by bpftool for a specific BPF object file. All the xxx_skeleton() APIs wrap up corresponding bpf_object_xxx() APIs, but additionally also automate map/program lookups by name, global data initialization and mmap()-ing, etc. All this greatly improves and simplifies userspace usability of working with BPF programs. See follow up patches for examples.
--- tools/lib/bpf/libbpf.c | 162 ++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf_internal.h | 40 ++++++++ 2 files changed, 202 insertions(+)
tools/lib/bpf/libbpf.c +162 -0
@@ -6727,3 +6727,165 @@ int libbpf_num_possible_cpus(void)WRITE_ONCE (cpus, tmp_cpus);return tmp_cpus; } + +int bpf_object__open_skeleton (struct bpf_object_skeleton *s, +const struct bpf_object_open_opts *opts) +{ +DECLARE_LIBBPF_OPTS (bpf_object_open_opts, skel_opts, + .object_name = s->name, + ); +struct bpf_object *obj; +int i; + +/* Attempt to preserve opts->object_name, unless overriden by user +* explicitly. Overwriting object name for skeletons is discouraged, +* as it breaks global data maps, because they contain object name +* prefix as their own map name prefix. When skeleton is generated, +* bpftool is making an assumption that this name will stay the same. +*/ +if (opts) { +memcpy (&skel_opts, opts,sizeof (*opts)); +if (!opts->object_name) + skel_opts.object_name = s->name; + } + + obj =bpf_object__open_mem (s->data, s->data_sz, &skel_opts); +if (IS_ERR (obj)) { +pr_warn ("failed to initialize skeleton BPF object '%s': %ld\n" , + s->name,PTR_ERR (obj)); +return PTR_ERR (obj); + } + + *s->obj = obj; + +for (i =0 ; i < s->map_cnt; i++) { +struct bpf_map **map = s->maps[i].map; +const char *name = s->maps[i].name; +void **mmaped = s->maps[i].mmaped; + + *map =bpf_object__find_map_by_name (obj, name); +if (!*map) { +pr_warn ("failed to find skeleton map '%s'\n" , name); +return -ESRCH ; + } + +if (mmaped) + *mmaped = (*map)->mmaped; + } + +for (i =0 ; i < s->prog_cnt; i++) { +struct bpf_program **prog = s->progs[i].prog; +const char *name = s->progs[i].name; + + *prog =bpf_object__find_program_by_name (obj, name); +if (!*prog) { +pr_warn ("failed to find skeleton program '%s'\n" , name); +return -ESRCH ; + } + } + +return 0 ; +} + +int bpf_object__load_skeleton (struct bpf_object_skeleton *s,const void *opts) +{ +int i, err; + + err =bpf_object__load (*s->obj); +if (err) { +pr_warn ("failed to load BPF skeleton '%s': %d\n" , s->name, err); +return err; + } + +for (i =0 ; i < s->map_cnt; i++) { +struct bpf_map *map = *s->maps[i].map; +size_t mmap_sz =bpf_map_mmap_sz (map); +int prot, map_fd =bpf_map__fd (map); +void **mmaped = s->maps[i].mmaped; +void *remapped; + +if (!mmaped) +continue ; + +if (!(map->def.map_flags &BPF_F_MMAPABLE )) { + *mmaped =NULL ; +continue ; + } + +if (map->def.map_flags &BPF_F_RDONLY_PROG ) + prot =PROT_READ ; +else + prot =PROT_READ |PROT_WRITE ; + +/* Remap anonymous mmap()-ed "map initialization image" as +* a BPF map-backed mmap()-ed memory, but preserving the same +* memory address. This will cause kernel to change process' +* page table to point to a different piece of kernel memory, +* but from userspace point of view memory address (and its +* contents, being identical at this point) will stay the +* same. This mapping will be released by bpf_object__close() +* as per normal clean up procedure, so we don't need to worry +* about it from skeleton's clean up perspective. +*/ + remapped =mmap (*mmaped, mmap_sz, prot,MAP_SHARED |MAP_FIXED , + map_fd,0 ); +if (remapped ==MAP_FAILED ) { + err = -errno; + *mmaped =NULL ; +pr_warn ("failed to re-mmap() map '%s': %d\n" , +bpf_map__name (map), err); +return err; + } + } + +return 0 ; +} + +int bpf_object__attach_skeleton (struct bpf_object_skeleton *s,const void *opts) +{ +int i; + +for (i =0 ; i < s->prog_cnt; i++) { +struct bpf_program *prog = *s->progs[i].prog; +struct bpf_link **link = s->progs[i].link; +const struct bpf_sec_def *sec_def; +const char *sec_name =bpf_program__title (prog, false); + + sec_def =find_sec_def (sec_name); +if (!sec_def || !sec_def->attach_fn) +continue ; + + *link = sec_def->attach_fn (sec_def, prog); +if (IS_ERR (*link)) { +pr_warn ("failed to auto-attach program '%s': %ld\n" , +bpf_program__name (prog),PTR_ERR (*link)); +return PTR_ERR (*link); + } + } + +return 0 ; +} + +void bpf_object__detach_skeleton (struct bpf_object_skeleton *s) +{ +int i; + +for (i =0 ; i < s->prog_cnt; i++) { +struct bpf_link **link = s->progs[i].link; + +if (!IS_ERR_OR_NULL (*link)) +bpf_link__destroy (*link); + *link =NULL ; + } +} + +void bpf_object__destroy_skeleton (struct bpf_object_skeleton *s) +{ +if (s->progs) +bpf_object__detach_skeleton (s); +if (s->obj) +bpf_object__close (*s->obj); +free (s->maps); +free (s->progs); +free (s); +}
tools/lib/bpf/libbpf_internal.h +40 -0
@@ -237,4 +237,44 @@ struct bpf_field_reloc {enum bpf_field_info_kind kind; }; + +struct bpf_map_skeleton { +const char *name; +struct bpf_map **map; +void **mmaped; +}; + +struct bpf_prog_skeleton { +const char *name; +struct bpf_program **prog; +struct bpf_link **link; +}; + +struct bpf_object_skeleton { +size_t sz;/* size of this struct, for forward/backward compatibility */ + +const char *name; +void *data; +size_t data_sz; + +struct bpf_object **obj; + +int map_cnt; +int map_skel_sz;/* sizeof(struct bpf_skeleton_map) */ +struct bpf_map_skeleton *maps; + +int prog_cnt; +int prog_skel_sz;/* sizeof(struct bpf_skeleton_prog) */ +struct bpf_prog_skeleton *progs; +}; + +int bpf_object__open_skeleton (struct bpf_object_skeleton *s, +const struct bpf_object_open_opts *opts); +int bpf_object__load_skeleton (struct bpf_object_skeleton *s, +const void *opts); +int bpf_object__attach_skeleton (struct bpf_object_skeleton *s, +const void *opts); +void bpf_object__detach_skeleton (struct bpf_object_skeleton *s); +void bpf_object__destroy_skeleton (struct bpf_object_skeleton *s); +#endif /* __LIBBPF_LIBBPF_INTERNAL_H */
Signature · 1 line
2.17.1
RFC PATCH 11/16 bpftool: add skeleton codegen command
In reply to Andrii NakryikoAdd bpftool gen skeleton command, which takes in compiled BPF .o object file and dumps a BPF skeleton struct and related code to work with that skeleton. Skeleton itself is tailored to a specific structure of provided BPF object file, containing accessors (just plain struct fields) for every map and program, as well as dedicated space for bpf_links. If BPF program is using global variables, corresponding structure definitions of compatible memory layout are emitted as well, making it possible to initialize and subsequently read/update global variables values using simple and clear C syntax for accessing fields. This skeleton majorly improves usability of opening/loading/attaching of BPF object, as well as interacting with it throughout the lifetime of loaded BPF object.
Generated skeleton struct has the following structure:
struct <object-name> {
/* used by libbpf's skeleton API */
struct bpf_object_skeleton *skeleton;
/* bpf_object for libbpf APIs */
struct bpf_object *obj;
struct {
/* for every defined map in BPF object: */
struct bpf_map *<map-name>;
} maps;
struct {
/* for every program in BPF object: */
struct bpf_program *<program-name>;
} progs;
struct {
/* for every program in BPF object: */
struct bpf_link *<program-name>;
} links;
/* for every present global data section: */
struct <object-name>__<one of bss, data, or rodata> {
/* memory layout of corresponding data section,
* with every defined variable represented as a struct field
* with exactly the same type, but without const/volatile
* modifiers, e.g.:
*/
int *my_var_1;
...
} *<one of bss, data, or rodata>;
};This provides great usability improvements:
- no need to look up maps and programs by name, instead just my_obj->maps.my_map or my_obj->progs.my_prog would give necessary bpf_map/bpf_program pointers, which user can pass to existing libbpf APIs;
- pre-defined places for bpf_links, which will be automatically populated for program types that libbpf knows how to attach automatically (currently tracepoints, kprobe/kretprobe, raw tracepoint and tracing programs). On tearing down skeleton, all active bpf_links will be destroyed (meaning BPF programs will be detached, if they are attached). For cases in which libbpf doesn't know how to auto-attach BPF program, user can manually create link after loading skeleton and they will be auto-detached on skeleton destruction:
my_obj->links.my_fancy_prog = bpf_program__attach_cgroup_whatever( my_obj->progs.my_fancy_prog, <whatever extra param);
- it's extremely easy and convenient to work with global data from userspace now. Both for read-only and read/write variables, it's possible to pre-initialize them before skeleton is loaded:
skel = my_obj__open(raw_embed_data); my_obj->rodata->my_var = 123; my_obj__load(skel); /* 123 will be initialization value for my_var */
After load, if kernel supports mmap() for BPF arrays, user can still read (and write for .bss and .data) variables values, but at that point it will be directly mmap()-ed to BPF array, backing global variables. This allows to seamlessly exchange data with BPF side. From userspace program's POV, all the pointers and memory contents stay the same, but mapped kernel memory changes to point to created map.
If kernel doesn't yet support mmap() for BPF arrays, it's still possible to use those data section structs to pre-initialize .bss, .data, and .rodata, but after load their pointers will be reset to NULL, allowing user code to gracefully handle this condition, if necessary.
Given a lot of surface area, skeleton is kept as an experimental non-public API for now, until more feedback and real-world experience is collected.
runqslower example and selftests further help to demonstrate usability benefits of this approach.
--- tools/bpf/bpftool/gen.c | 482 +++++++++++++++++++++++++++++++++++++++ tools/bpf/bpftool/main.c | 1 + tools/bpf/bpftool/main.h | 1 + 3 files changed, 484 insertions(+) create mode 100644 tools/bpf/bpftool/gen.c
tools/bpf/bpftool/gen.c new file +482 -0
@@ -0,0 +1,482 @@ +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +/* Copyright (C) 2019 Facebook */ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include <ctype.h> +#include <errno.h> +#include <fcntl.h> +#include <linux/err.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <bpf.h> +#include <libbpf.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> + +#include "btf.h" +#include "libbpf_internal.h" +#include "json_writer.h" +#include "main.h" + + +#define MAX_OBJ_NAME_LEN 64 + +static void sanitize_identifier (char *name) +{ +int i; + +for (i =0 ; name[i]; i++) +if (!isalnum (name[i]) && name[i] !='_' ) + name[i] ='_' ; +} + +static bool str_has_suffix (const char *str,const char *suffix) +{ +size_t i, n1 =strlen (str), n2 =strlen (suffix); + +if (n1 < n2) +return false; + +for (i =0 ; i < n2; i++) { +if (str[n1 - i -1 ] != suffix[n2 - i -1 ]) +return false; + } + +return true; +} + +static void get_obj_name (char *name,const char *file) +{ +/* Using basename() GNU version which doesn't modify arg. */ +strncpy (name,basename (file),MAX_OBJ_NAME_LEN -1 ); + name[MAX_OBJ_NAME_LEN -1 ] ='\0' ; +if (str_has_suffix (name,".o" )) + name[strlen (name) -2 ] ='\0' ; +sanitize_identifier (name); +} + +static void get_header_guard (char *guard,const char *obj_name) +{ +int i; + +sprintf (guard,"__%s_EMBED_H__" , obj_name); +for (i =0 ; guard[i]; i++) + guard[i] =toupper (guard[i]); +} + +static const char *get_map_ident (const struct bpf_map *map) +{ +const char *name =bpf_map__name (map); + +if (!bpf_map__is_internal (map)) +return name; + +if (str_has_suffix (name,".data" )) +return "data" ; +else if (str_has_suffix (name,".rodata" )) +return "rodata" ; +else if (str_has_suffix (name,".bss" )) +return "bss" ; +else +return NULL ; +} + +static void codegen_btf_dump_printf (void *ct,const char *fmt,va_list args) +{ +vprintf (fmt, args); +} + +static int codegen_datasec_def (struct bpf_object *obj, +struct btf *btf, +struct btf_dump *d, +const struct btf_type *sec, +const char *obj_name) +{ +const char *sec_name =btf__name_by_offset (btf, sec->name_off); +const struct btf_var_secinfo * sec_var =btf_var_secinfos (sec); +const char *sec_ident; +int i, off =0 , pad_cnt =0 , vlen =btf_vlen (sec); + +if (strcmp (sec_name,".data" ) ==0 ) { + sec_ident ="data" ; + }else if (strcmp (sec_name,".bss" ) ==0 ) { + sec_ident ="bss" ; + }else if (strcmp (sec_name,".rodata" ) ==0 ) { + sec_ident ="rodata" ; + }else { +return 0 ; + } + +printf (" struct %s__%s {\n" , obj_name, sec_ident); +for (i =0 ; i < vlen; i++, sec_var++) { +const struct btf_type *var =btf__type_by_id (btf, sec_var->type); +const char *var_name =btf__name_by_offset (btf, var->name_off); +int align =btf_align_of (btf, var->type); +int need_off = sec_var->offset; +int align_off; +__u32 var_type_id = var->type; +const struct btf_type *t =btf__type_by_id (btf, var_type_id); + +while (btf_is_mod (t)) { + var_type_id = t->type; + t =btf__type_by_id (btf, var_type_id); + } + +if (off > need_off) { +p_err ("Something is wrong for %s's variable #%d: need offset %d, already at %d.\n" , + sec_name, i, need_off, off); +return -1 ; + } + + align_off = (off + align -1 ) / align * align; +if (align_off != need_off) { +printf ("\t\tchar __pad%d[%d];\n" , + pad_cnt, need_off - off); + pad_cnt++; + } + +printf ("\t\t" ); +btf_dump_emit_type_decl (d, var_type_id, var_name,2 ); +printf (";\n" ); + + off = sec_var->offset + sec_var->size; + } +printf (" } *%s;\n" , sec_ident); +return 0 ; +} + +static int codegen_datasecs (struct bpf_object *obj,const char *obj_name) +{ +struct btf_dump *d; +struct btf *btf =bpf_object__btf (obj); +int i, err =0 , n =btf__get_nr_types (btf); + + d =btf_dump__new (btf,NULL ,NULL , codegen_btf_dump_printf); +if (IS_ERR (d)) +return PTR_ERR (d); + + +for (i =1 ; i <= n; i++) { +const struct btf_type *t =btf__type_by_id (btf, i); + +if (!btf_is_datasec (t)) +continue ; + + err =codegen_datasec_def (obj, btf, d, t, obj_name); +if (err) + goto out; + } +out: +btf_dump__free (d); +return err; +} + +static int do_skeleton (int argc,char **argv) +{ +char header_guard[MAX_OBJ_NAME_LEN +sizeof ("__EMBED_H__" )]; +size_t i, map_cnt =0 , prog_cnt =0 ; +char obj_name[MAX_OBJ_NAME_LEN ]; +struct bpf_program *prog; +struct bpf_object *obj; +struct bpf_map *map; +const char *file; +struct btf *btf; +int err =-1 ; + +if (!REQ_ARGS (1 )) { +usage (); +return -1 ; + } + file =GET_ARG (); + +if (argc) { +p_err ("extra unknown arguments" ); +return -1 ; + } + + obj =bpf_object__open_file (file,NULL ); +if (IS_ERR (obj)) { +p_err ("failed to open BPF object file: %ld" ,PTR_ERR (obj)); +return -1 ; + } + +get_obj_name (obj_name, file); +get_header_guard (header_guard, obj_name); + +bpf_object__for_each_map (map, obj) { + map_cnt++; + } +bpf_object__for_each_program (prog, obj) { + prog_cnt++; + } + +printf ( +"/* THIS FILE IS AUTOGENERATED! */\n" +"#ifndef %2$s\n" +"#define %2$s\n" +"\n" +"#include <stdlib.h>\n" +"#include <libbpf.h>\n" +"#include <libbpf_internal.h>\n" +"\n" +"struct %1$s {\n" +" struct bpf_object_skeleton *skeleton;\n" +" struct bpf_object *obj;\n" , + obj_name, header_guard + ); + +if (map_cnt) { +printf ("\tstruct {\n" ); +bpf_object__for_each_map (map, obj) { +const char *name =get_map_ident (map); + +if (!name) { +p_err ("unrecognized internal map '%s'" , +bpf_map__name (map)); + goto out; + } +printf ("\t\tstruct bpf_map *%s;\n" , name); + } +printf ("\t} maps;\n" ); + } + +if (prog_cnt) { +printf ("\tstruct {\n" ); +bpf_object__for_each_program (prog, obj) { +printf ("\t\tstruct bpf_program *%s;\n" , +bpf_program__name (prog)); + } +printf ("\t} progs;\n" ); +printf ("\tstruct {\n" ); +bpf_object__for_each_program (prog, obj) { +printf ("\t\tstruct bpf_link *%s;\n" , +bpf_program__name (prog)); + } +printf ("\t} links;\n" ); + } + + btf =bpf_object__btf (obj); +if (btf) { + err =codegen_datasecs (obj, obj_name); +if (err) + goto out; + } + +printf ( +"};\n" +"\n" +"static inline struct bpf_object_skeleton *\n" +"%1$s__create_skeleton(struct %1$s *obj, struct bpf_embed_data *embed)\n" +"{\n" +" struct bpf_object_skeleton *s;\n" +"\n" +" s = calloc(1, sizeof(*s));\n" +" if (!s)\n" +" return NULL;\n" +"\n" +" s->sz = sizeof(*s);\n" +" s->name = \"%1$s\";\n" +" s->data = embed->data;\n" +" s->data_sz = embed->size;\n" +" s->obj = &obj->obj;\n" , + obj_name + ); +if (map_cnt) { +printf ( +"\n" +" /* maps */\n" +" s->map_cnt = %zu;\n" +" s->map_skel_sz = sizeof(*s->maps);\n" +" s->maps = calloc(s->map_cnt, s->map_skel_sz);\n" +" if (!s->maps)\n" +" goto err;\n" , + map_cnt + ); + i =0 ; +bpf_object__for_each_map (map, obj) { +printf ("\ts->maps[%zu].name = \"%s\";\n" , + i,bpf_map__name (map)); +printf ("\ts->maps[%zu].map = &obj->maps.%s;\n" , + i,get_map_ident (map)); +/* memory-mapped internal maps */ +if (bpf_map__is_internal (map) && + (bpf_map__def (map)->map_flags &BPF_F_MMAPABLE )) { +const char *ident =get_map_ident (map); + +if (!ident) { + err = -EINVAL ; +p_err ("unrecognized internal map '%s'\n" , +bpf_map__name (map)); + goto out; + } +printf ("\ts->maps[%zu].mmaped = (void **)&obj->%s;\n" , + i, ident); + } + i++; + } + } +if (prog_cnt) { +printf ( +"\n" +" /* programs */\n" +" s->prog_cnt = %zu;\n" +" s->prog_skel_sz = sizeof(*s->progs);\n" +" s->progs = calloc(s->prog_cnt, s->prog_skel_sz);\n" +" if (!s->progs)\n" +" goto err;\n" , + prog_cnt + ); + i =0 ; +bpf_object__for_each_program (prog, obj) { +printf ("\ts->progs[%zu].name = \"%s\";\n" , + i,bpf_program__name (prog)); +printf ("\ts->progs[%zu].prog = &obj->progs.%s;\n" , + i,bpf_program__name (prog)); +printf ("\ts->progs[%zu].link = &obj->links.%s;\n" , + i,bpf_program__name (prog)); + i++; + } + } +printf ( +"\n" +" return s;\n" +"err:\n" +" bpf_object__destroy_skeleton(s);\n" +" return NULL;\n" +"}\n" +"static void\n" +"%1$s__destroy(struct %1$s *obj)\n" +"{\n" +" if (!obj)\n" +" return;\n" +" if (obj->skeleton)\n" +" bpf_object__destroy_skeleton(obj->skeleton);\n" +" free(obj);\n" +"}\n" +"\n" +"static inline struct %1$s *\n" +"%1$s__open_opts(struct bpf_embed_data *embed, const struct bpf_object_open_opts *opts)\n" +"{\n" +" struct %1$s *obj;\n" +"\n" +" obj = calloc(1, sizeof(*obj));\n" +" if (!obj)\n" +" return NULL;\n" +"\n" +" obj->skeleton = %1$s__create_skeleton(obj, embed);\n" +" if (!obj->skeleton)\n" +" goto err;\n" +"\n" +" if (bpf_object__open_skeleton(obj->skeleton, opts))\n" +" goto err;\n" +"\n" +" return obj;\n" +"err:\n" +" %1$s__destroy(obj);\n" +" return NULL;\n" +"}\n" +"\n" +"static inline struct %1$s *\n" +"%1$s__open(struct bpf_embed_data *embed)\n" +"{\n" +" return %1$s__open_opts(embed, NULL);\n" +"}\n" +"\n" +"static inline int\n" +"%1$s__load_opts(struct %1$s *obj, const void *opts)\n" +"{\n" +" return bpf_object__load_skeleton(obj->skeleton, opts);\n" +"}\n" +"\n" +"static inline int\n" +"%1$s__load(struct %1$s *obj)\n" +"{\n" +" return %1$s__load_opts(obj, NULL);\n" +"}\n" +"\n" +"static inline struct %1$s *\n" +"%1$s__open_and_load_opts(\n" +" struct bpf_embed_data *embed,\n" +" const struct bpf_object_open_opts *open_opts,\n" +" const void *load_opts)\n" +"{\n" +" struct %1$s *obj;\n" +"\n" +" obj = %1$s__open_opts(embed, open_opts);\n" +" if (!obj)\n" +" return NULL;\n" +"\n" +" if (%1$s__load_opts(obj, load_opts)) {\n" +" %1$s__destroy(obj);\n" +" return NULL;\n" +" }\n" +"\n" +" return obj;\n" +"}\n" +"\n" +"static inline struct %1$s *\n" +"%1$s__open_and_load(struct bpf_embed_data *embed)\n" +"{\n" +" return %1$s__open_and_load_opts(embed, NULL, NULL);\n" +"}\n" +"\n" +"static inline int\n" +"%1$s__attach_opts(struct %1$s *obj, const void *opts)\n" +"{\n" +" return bpf_object__attach_skeleton(obj->skeleton, opts);\n" +"}\n" +"\n" +"static inline int\n" +"%1$s__attach(struct %1$s *obj)\n" +"{\n" +" return %1$s__attach_opts(obj, NULL);\n" +"}\n" +"\n" +"static inline void\n" +"%1$s__detach(struct %1$s *obj)\n" +"{\n" +" return bpf_object__detach_skeleton(obj->skeleton);\n" +"}\n" +"\n" +"#endif /* %2$s */\n" , + obj_name, header_guard + ); + err =0 ; +out: +bpf_object__close (obj); +return err; +} + +static int do_help (int argc,char **argv) +{ +if (json_output) { +jsonw_null (json_wtr); +return 0 ; + } + +fprintf (stderr, +"Usage: %1$s gen skeleton FILE\n" +" %1$s gen help\n" +"\n" +" " HELP_SPEC_OPTIONS "\n" +"" , + bin_name); + +return 0 ; +} + +static const struct cmd cmds[] = { + {"skeleton" , do_skeleton }, + {"help" , do_help }, + {0 } +}; + +int do_gen (int argc,char **argv) +{ +return cmd_select (cmds, argc, argv, do_help); +}
tools/bpf/bpftool/main.c +1 -0
@@ -227,6 +227,7 @@ static const struct cmd cmds[] = { { "net", do_net }, { "feature", do_feature }, { "btf", do_btf }, + { "gen", do_gen }, { "version", do_version }, { 0 } };
tools/bpf/bpftool/main.h +1 -0
@@ -155,6 +155,7 @@ int do_net(int argc, char **arg);int do_tracelog (int argc,char **arg);int do_feature (int argc,char **argv);int do_btf (int argc,char **argv); +int do_gen (int argc,char **argv);int parse_u32_arg (int *argc,char ***argv,__u32 *val,const char *what);int prog_parse_fd (int *argc,char ***argv);
Signature · 1 line
2.17.1
RFC PATCH 12/16 libbpf/samples: add runqslower sample to libbpf samples
In reply to Andrii NakryikoConvert one of BCC tools (runqslower, [0]) to BPF CO-RE + libbpf. It matches its BCC-based counterpart 1-to-1, supporting all the same parameters and functionality. It is added under samples/runqslower subdirectory inside libbpf source dir.
runqslower sample utilizes BPF skeleton, auto-generated from BPF object file, as well as memory-mapped interface to global (read-only, in this case) data.
[0] https://github.com/iovisor/bcc/blob/11bf5d02c895df9646c117c713082eb192825293/tools/runqslower.py
--- tools/lib/bpf/samples/runqslower/.gitignore | 2 + tools/lib/bpf/samples/runqslower/Makefile | 50 +++++ .../bpf/samples/runqslower/runqslower.bpf.c | 105 ++++++++++ tools/lib/bpf/samples/runqslower/runqslower.c | 189 ++++++++++++++++++ tools/lib/bpf/samples/runqslower/runqslower.h | 13 ++ 5 files changed, 359 insertions(+) create mode 100644 tools/lib/bpf/samples/runqslower/.gitignore create mode 100644 tools/lib/bpf/samples/runqslower/Makefile create mode 100644 tools/lib/bpf/samples/runqslower/runqslower.bpf.c create mode 100644 tools/lib/bpf/samples/runqslower/runqslower.c create mode 100644 tools/lib/bpf/samples/runqslower/runqslower.h
tools/lib/bpf/samples/runqslower/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +/.output +/runqslower
tools/lib/bpf/samples/runqslower/Makefile new file +50 -0
50 lines not on this page · raw message
tools/lib/bpf/samples/runqslower/runqslower.bpf.c new file +105 -0
105 lines not on this page · raw message
tools/lib/bpf/samples/runqslower/runqslower.c new file +189 -0
189 lines not on this page · raw message
tools/lib/bpf/samples/runqslower/runqslower.h new file +13 -0
@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +#ifndef __RUNQSLOWER_H +#define __RUNQSLOWER_H + +#define TASK_COMM_LEN 16 + +struct event { +char task[TASK_COMM_LEN ]; +__u64 delta_us; +pid_t pid; +}; + +#endif /* __RUNQSLOWER_H */
Signature · 1 line
2.17.1
RFC PATCH 13/16 selftests/bpf: add BPF skeletons selftests and convert attach_probe.c
In reply to Andrii NakryikoAdd BPF skeleton generation to selftest/bpf's Makefile. Convert attach_probe.c to use skeleton.
--- tools/testing/selftests/bpf/.gitignore | 2 + tools/testing/selftests/bpf/Makefile | 36 +++-- .../selftests/bpf/prog_tests/attach_probe.c | 135 +++++------------- .../selftests/bpf/progs/test_attach_probe.c | 34 ++--- 4 files changed, 74 insertions(+), 133 deletions(-)
tools/testing/selftests/bpf/.gitignore +2 -0
@@ -37,5 +37,7 @@ libbpf.so.* test_hashmap test_btf_dump xdping +*.skel.h /no_alu32 /bpf_gcc +/tools
tools/testing/selftests/bpf/Makefile +28 -8
@@ -3,10 +3,12 @@ include ../../../../scripts/Kbuild.include include ../../../scripts/Makefile.arch CURDIR := $(abspath .) -LIBDIR := $(abspath ../../../lib) +TOOLSDIR := $(abspath ../../..) +LIBDIR := $(TOOLSDIR)/lib BPFDIR := $(LIBDIR)/bpf -TOOLSDIR := $(abspath ../../../include) -APIDIR := $(TOOLSDIR)/uapi +TOOLSINCDIR := $(TOOLSDIR)/include +BPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool +APIDIR := $(TOOLSINCDIR)/uapi GENDIR := $(abspath ../../../../include/generated) GENHDR := $(GENDIR)/autoconf.h
@@ -19,7 +21,7 @@ LLC ?= llc LLVM_OBJCOPY ?= llvm-objcopy BPF_GCC ?= $(shell command -v bpf-gcc;) CFLAGS += -g -Wall -O2 $(GENFLAGS) -I$(APIDIR) -I$(LIBDIR) -I$(BPFDIR) \ - -I$(GENDIR) -I$(TOOLSDIR) -I$(CURDIR) \ + -I$(GENDIR) -I$(TOOLSINCDIR) -I$(CURDIR) \ -Dbpf_prog_load=bpf_prog_test_load \ -Dbpf_load_program=bpf_test_load_program LDLIBS += -lcap -lelf -lrt -lpthread
@@ -117,6 +119,12 @@ $(OUTPUT)/test_cgroup_attach: cgroup_helpers.c # force a rebuild of BPFOBJ when its dependencies are updated force: +DEFAULT_BPFTOOL := $(OUTPUT)/tools/usr/local/sbin/bpftool +BPFTOOL ?= $(DEFAULT_BPFTOOL) + +$(DEFAULT_BPFTOOL): force + $(MAKE) -C $(BPFTOOLDIR) DESTDIR=$(OUTPUT)/tools install + $(BPFOBJ): force $(MAKE) -C $(BPFDIR) OUTPUT=$(OUTPUT)/
@@ -180,6 +188,8 @@ define GCC_BPF_BUILD_RULE $(BPF_GCC) $3 $4 -O2 -c $1 -o $2 endef +SKEL_BLACKLIST := btf__% test_pinning_invalid.c + # Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on # $eval()) and pass control to DEFINE_TEST_RUNNER_RULES. # Parameters:
@@ -195,8 +205,11 @@ TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \ $$(filter %.c,$(TRUNNER_EXTRA_SOURCES))) TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES)) TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h -TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \ - $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))) +TRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c)) +TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, $$(TRUNNER_BPF_SRCS)) +TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \ + $$(filter-out $(SKEL_BLACKLIST), \ + $$(TRUNNER_BPF_SRCS))) # Evaluate rules now with extra TRUNNER_XXX variables above already defined $$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
@@ -226,6 +239,11 @@ $(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o: \ $$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@, \ $(TRUNNER_BPF_CFLAGS), \ $(TRUNNER_BPF_LDFLAGS)) + +$(TRUNNER_BPF_SKELS): $(TRUNNER_OUTPUT)/%.skel.h: \ + $(TRUNNER_OUTPUT)/%.o \ + | $(BPFTOOL) $(TRUNNER_OUTPUT) + $$(BPFTOOL) gen skeleton $$< > $$@ endif # ensure we set up tests.h header generation rule just once
@@ -245,6 +263,7 @@ $(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \ $(TRUNNER_TESTS_DIR)/%.c \ $(TRUNNER_EXTRA_HDRS) \ $(TRUNNER_BPF_OBJS) \ + $(TRUNNER_BPF_SKELS) \ $$(BPFOBJ) | $(TRUNNER_OUTPUT) cd $$(@D) && $$(CC) $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
@@ -255,9 +274,9 @@ $(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \ $$(BPFOBJ) | $(TRUNNER_OUTPUT) $$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@ +# only copy extra resources if in flavored build $(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT) ifneq ($2,) - # only copy extra resources if in flavored build cp -a $$^ $(TRUNNER_OUTPUT)/ endif
@@ -319,4 +338,5 @@ $(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT) EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) \ prog_tests/tests.h map_tests/tests.h verifier/tests.h \ - feature $(OUTPUT)/*.o $(OUTPUT)/no_alu32 $(OUTPUT)/bpf_gcc + feature $(OUTPUT)/*.o $(OUTPUT)/no_alu32 $(OUTPUT)/bpf_gcc \ + tools
tools/testing/selftests/bpf/prog_tests/attach_probe.c +32 -103
187 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_attach_probe.c +12 -22
@@ -5,46 +5,36 @@#include <linux/bpf.h> #include "bpf_helpers.h" -struct { - __uint(type, BPF_MAP_TYPE_ARRAY); - __uint(max_entries, 4); - __type(key, int); - __type(value, int); -} results_map SEC(".maps"); +int kprobe_res =0 ; +int kretprobe_res =0 ; +int uprobe_res =0 ; +int uretprobe_res =0 ; SEC("kprobe/sys_nanosleep") -int handle_sys_nanosleep_entry (struct pt_regs *ctx) +int handle_kprobe (struct pt_regs *ctx) { -const int key =0 , value =1 ; - -bpf_map_update_elem (&results_map, &key, &value,0 ); + kprobe_res =1 ;return 0 ; } SEC("kretprobe/sys_nanosleep") -int handle_sys_getpid_return (struct pt_regs *ctx) +int handle_kretprobe (struct pt_regs *ctx) { -const int key =1 , value =2 ; - -bpf_map_update_elem (&results_map, &key, &value,0 ); + kretprobe_res =2 ;return 0 ; } SEC("uprobe/trigger_func") -int handle_uprobe_entry (struct pt_regs *ctx) +int handle_uprobe (struct pt_regs *ctx) { -const int key =2 , value =3 ; - -bpf_map_update_elem (&results_map, &key, &value,0 ); + uprobe_res =3 ;return 0 ; } SEC("uretprobe/trigger_func") -int handle_uprobe_return (struct pt_regs *ctx) +int handle_uretprobe (struct pt_regs *ctx) { -const int key =3 , value =4 ; - -bpf_map_update_elem (&results_map, &key, &value,0 ); + uretprobe_res =4 ;return 0 ; }
Signature · 1 line
2.17.1
RFC PATCH 14/16 selftests/bpf: convert few more selftest to skeletons
In reply to Andrii NakryikoConvert few more selftests to use generated BPF skeletons as a demonstration on how to use it.
--- .../selftests/bpf/prog_tests/fentry_fexit.c | 105 ++++++------------ .../selftests/bpf/prog_tests/fentry_test.c | 72 +++++------- tools/testing/selftests/bpf/prog_tests/mmap.c | 58 ++++------ .../bpf/prog_tests/stacktrace_build_id.c | 79 +++++-------- .../bpf/prog_tests/stacktrace_build_id_nmi.c | 84 ++++++-------- 5 files changed, 149 insertions(+), 249 deletions(-)
tools/testing/selftests/bpf/prog_tests/fentry_fexit.c +37 -68
@@ -1,90 +1,59 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright (c) 2019 Facebook */ #include <test_progs.h> +#include "test_pkt_access.skel.h" +#include "fentry_test.skel.h" +#include "fexit_test.skel.h" + +BPF_EMBED_OBJ (pkt_access,"test_pkt_access.o" ); +BPF_EMBED_OBJ (fentry,"fentry_test.o" ); +BPF_EMBED_OBJ (fexit,"fexit_test.o" );void test_fentry_fexit (void ) { -struct bpf_prog_load_attr attr_fentry = { - .file ="./fentry_test.o" , - }; -struct bpf_prog_load_attr attr_fexit = { - .file ="./fexit_test.o" , - }; - -struct bpf_object *obj_fentry =NULL , *obj_fexit =NULL , *pkt_obj; -struct bpf_map *data_map_fentry, *data_map_fexit; -char fentry_name[] ="fentry/bpf_fentry_testX" ; -char fexit_name[] ="fexit/bpf_fentry_testX" ; -int err, pkt_fd, kfree_skb_fd, i; -struct bpf_link *link[12 ] = {}; -struct bpf_program *prog[12 ]; -__u32 duration, retval; -const int zero =0 ; -u64 result[12 ]; - - err =bpf_prog_load ("./test_pkt_access.o" ,BPF_PROG_TYPE_SCHED_CLS , - &pkt_obj, &pkt_fd); -if (CHECK (err,"prog_load sched cls" ,"err %d errno %d\n" , err, errno)) +struct test_pkt_access *pkt_skel =NULL ; +struct fentry_test *fentry_skel =NULL ; +struct fexit_test *fexit_skel =NULL ; +__u64 *fentry_res, *fexit_res; +__u32 duration =0 , retval; +int err, pkt_fd, i; + + pkt_skel =test_pkt_access__open_and_load (&pkt_access_embed); +if (CHECK (!pkt_skel,"pkt_skel_load" ,"pkt_access skeleton failed\n" ))return ; - err =bpf_prog_load_xattr (&attr_fentry, &obj_fentry, &kfree_skb_fd); -if (CHECK (err,"prog_load fail" ,"err %d errno %d\n" , err, errno)) + fentry_skel =fentry_test__open_and_load (&fentry_embed); +if (CHECK (!fentry_skel,"fentry_skel_load" ,"fentry skeleton failed\n" )) goto close_prog; - err =bpf_prog_load_xattr (&attr_fexit, &obj_fexit, &kfree_skb_fd); -if (CHECK (err,"prog_load fail" ,"err %d errno %d\n" , err, errno)) + fexit_skel =fexit_test__open_and_load (&fexit_embed); +if (CHECK (!fexit_skel,"fexit_skel_load" ,"fexit skeleton failed\n" )) goto close_prog; -for (i =0 ; i <6 ; i++) { - fentry_name[sizeof (fentry_name) -2 ] ='1' + i; - prog[i] =bpf_object__find_program_by_title (obj_fentry, fentry_name); -if (CHECK (!prog[i],"find_prog" ,"prog %s not found\n" , fentry_name)) - goto close_prog; - link[i] =bpf_program__attach_trace (prog[i]); -if (CHECK (IS_ERR (link[i]),"attach_trace" ,"failed to link\n" )) - goto close_prog; - } - data_map_fentry =bpf_object__find_map_by_name (obj_fentry,"fentry_t.bss" ); -if (CHECK (!data_map_fentry,"find_data_map" ,"data map not found\n" )) + err =fentry_test__attach (fentry_skel); +if (CHECK (err,"fentry_attach" ,"fentry attach failed: %d\n" , err)) goto close_prog; - -for (i =6 ; i <12 ; i++) { - fexit_name[sizeof (fexit_name) -2 ] ='1' + i -6 ; - prog[i] =bpf_object__find_program_by_title (obj_fexit, fexit_name); -if (CHECK (!prog[i],"find_prog" ,"prog %s not found\n" , fexit_name)) - goto close_prog; - link[i] =bpf_program__attach_trace (prog[i]); -if (CHECK (IS_ERR (link[i]),"attach_trace" ,"failed to link\n" )) - goto close_prog; - } - data_map_fexit =bpf_object__find_map_by_name (obj_fexit,"fexit_te.bss" ); -if (CHECK (!data_map_fexit,"find_data_map" ,"data map not found\n" )) + err =fexit_test__attach (fexit_skel); +if (CHECK (err,"fexit_attach" ,"fexit attach failed: %d\n" , err)) goto close_prog; + pkt_fd =bpf_program__fd (pkt_skel->progs.test_pkt_access); err =bpf_prog_test_run (pkt_fd,1 , &pkt_v6,sizeof (pkt_v6),NULL ,NULL , &retval, &duration);CHECK (err || retval,"ipv6" ,"err %d errno %d retval %d duration %d\n" , err, errno, retval, duration); - err =bpf_map_lookup_elem (bpf_map__fd (data_map_fentry), &zero, &result); -if (CHECK (err,"get_result" , -"failed to get output data: %d\n" , err)) - goto close_prog; - - err =bpf_map_lookup_elem (bpf_map__fd (data_map_fexit), &zero, result +6 ); -if (CHECK (err,"get_result" , -"failed to get output data: %d\n" , err)) - goto close_prog; - -for (i =0 ; i <12 ; i++) -if (CHECK (result[i] !=1 ,"result" ,"bpf_fentry_test%d failed err %ld\n" , - i %6 +1 , result[i])) - goto close_prog; + fentry_res = (__u64 *)fentry_skel->bss; + fexit_res = (__u64 *)fexit_skel->bss; +printf ("%lld\n" , fentry_skel->bss->test1_result); +for (i =0 ; i <6 ; i++) { +CHECK (fentry_res[i] !=1 ,"result" , +"fentry_test%d failed err %lld\n" , i +1 , fentry_res[i]); +CHECK (fexit_res[i] !=1 ,"result" , +"fexit_test%d failed err %lld\n" , i +1 , fexit_res[i]); + } close_prog: -for (i =0 ; i <12 ; i++) -if (!IS_ERR_OR_NULL (link[i])) -bpf_link__destroy (link[i]); -bpf_object__close (obj_fentry); -bpf_object__close (obj_fexit); -bpf_object__close (pkt_obj); +test_pkt_access__destroy (pkt_skel); +fentry_test__destroy (fentry_skel); +fexit_test__destroy (fexit_skel); }
tools/testing/selftests/bpf/prog_tests/fentry_test.c +27 -45
91 lines not on this page · raw message
tools/testing/selftests/bpf/prog_tests/mmap.c +23 -35
120 lines not on this page · raw message
tools/testing/selftests/bpf/prog_tests/stacktrace_build_id.c +28 -51
142 lines not on this page · raw message
tools/testing/selftests/bpf/prog_tests/stacktrace_build_id_nmi.c +34 -50
166 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC PATCH 15/16 selftests/bpf: add test validating data section to struct convertion layout
In reply to Andrii NakryikoAdd a simple selftests validating datasection-to-struct layour dumping. Global variables are constructed in such a way as to cause both natural and artificial padding (through custom alignment requirement).
--- .../selftests/bpf/prog_tests/skeleton.c | 47 +++++++++++++++++++ .../selftests/bpf/progs/test_skeleton.c | 36 ++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/skeleton.c create mode 100644 tools/testing/selftests/bpf/progs/test_skeleton.c
tools/testing/selftests/bpf/prog_tests/skeleton.c new file +47 -0
@@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> + +struct s { +int a; +long long b; +} __attribute__((packed)); + +#include "test_skeleton.skel.h" + +BPF_EMBED_OBJ (skeleton,"test_skeleton.o" ); + +void test_skeleton (void ) +{ +int duration =0 , err; +struct test_skeleton * skel; +struct test_skeleton__bss *bss; + + skel =test_skeleton__open_and_load (&skeleton_embed); +if (CHECK (!skel,"skel_open" ,"failed to open skeleton\n" )) +return ; + + bss = skel->bss; + bss->in1 =1 ; + bss->in2 =2 ; + bss->in3 =3 ; + bss->in4 =4 ; + bss->in5.a =5 ; + bss->in5.b =6 ; + + err =test_skeleton__attach (skel); +if (CHECK (err,"skel_attach" ,"skeleton attach failed: %d\n" , err)) + goto cleanup; + +/* trigger tracepoint */ +usleep (1 ); + +CHECK (bss->out1 !=1 ,"res1" ,"got %d != exp %d\n" , bss->out1,1 ); +CHECK (bss->out2 !=2 ,"res2" ,"got %lld != exp %d\n" , bss->out2,2 ); +CHECK (bss->out3 !=3 ,"res3" ,"got %d != exp %d\n" , (int )bss->out3,3 ); +CHECK (bss->out4 !=4 ,"res4" ,"got %lld != exp %d\n" , bss->out4,4 ); +CHECK (bss->out5.a !=5 ,"res5" ,"got %d != exp %d\n" , bss->out5.a,5 ); +CHECK (bss->out5.b !=6 ,"res6" ,"got %lld != exp %d\n" , bss->out5.b,6 ); + +cleanup: +test_skeleton__destroy (skel); +}
tools/testing/selftests/bpf/progs/test_skeleton.c new file +36 -0
@@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2017 Facebook + +#include <linux/bpf.h> +#include "bpf_helpers.h" + +struct s { +int a; +long long b; +} __attribute__((packed)); + +int in1 =0 ; +long long in2 =0 ; +char in3 ='\0' ; +long long in4 __attribute__((aligned(64))) = 0; +struct s in5 = {}; + +long long out2 =0 ; +struct s out5 = {}; +char out3 =0 ; +long long out4 =0 ; +int out1 =0 ; + + +SEC("raw_tp/sys_enter") +int handler (const void *ctx) +{ + out1 = in1; + out2 = in2; + out3 = in3; + out4 = in4; + out5 = in5; +return 0 ; +} + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.17.1
RFC PATCH 16/16 bpftool: add `gen skeleton` BASH completions
In reply to Andrii NakryikoAdd BASH completions for gen sub-command.
--- tools/bpf/bpftool/bash-completion/bpftool | 11 +++++++++++ tools/bpf/bpftool/main.c | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-)
tools/bpf/bpftool/bash-completion/bpftool +11 -0
@@ -716,6 +716,17 @@ _bpftool() ;; esac ;; + gen) + case $command in + skeleton) + _filedir + ;; + *) + [[ $prev == $object ]] && \ + COMPREPLY=( $( compgen -W 'skeleton help' -- "$cur" ) ) + ;; + esac + ;; cgroup) case $command in show|list|tree)
tools/bpf/bpftool/main.c +1 -1
@@ -58,7 +58,7 @@ static int do_help(int argc, char **argv)" %s batch file FILE\n" " %s version\n" "\n" -" OBJECT := { prog | map | cgroup | perf | net | feature | btf }\n" +" OBJECT := { prog | map | cgroup | perf | net | feature | btf | gen }\n" " " HELP_SPEC_OPTIONS "\n" "" , bin_name, bin_name, bin_name);
Signature · 1 line
2.17.1