aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/bpf/libbpf.h
diff options
context:
space:
mode:
authorSong Liu <songliubraving@fb.com>2019-03-12 01:30:38 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2019-03-19 15:52:06 -0400
commit34be16466d4dc06f3d604dafbcdb3327b72e78da (patch)
treeac000724283e4cf8052a9604b71c8f6885d53c2e /tools/lib/bpf/libbpf.h
parent71184c6ab7e60fd59d8dbc8fed62a1c753dc4934 (diff)
tools lib bpf: Introduce bpf_program__get_prog_info_linear()
Currently, bpf_prog_info includes 9 arrays. The user has the option to fetch any combination of these arrays. However, this requires a lot of handling. This work becomes more tricky when we need to store bpf_prog_info to a file, because these arrays are allocated independently. This patch introduces 'struct bpf_prog_info_linear', which stores arrays of bpf_prog_info in continuous memory. Helper functions are introduced to unify the work to get different sets of bpf_prog_info. Specifically, bpf_program__get_prog_info_linear() allows the user to select which arrays to fetch, and handles details for the user. Please see the comments right before 'enum bpf_prog_info_array' for more details and examples. Signed-off-by: Song Liu <songliubraving@fb.com> Reviewed-by: Jiri Olsa <jolsa@kernel.org> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lkml.kernel.org/r/ce92c091-e80d-a0c1-4aa0-987706c42b20@iogearbox.net Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Alexei Starovoitov <ast@kernel.org> Cc: kernel-team@fb.com Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stanislav Fomichev <sdf@google.com> Link: http://lkml.kernel.org/r/20190312053051.2690567-3-songliubraving@fb.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/lib/bpf/libbpf.h')
-rw-r--r--tools/lib/bpf/libbpf.h63
1 files changed, 63 insertions, 0 deletions
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index aa1521a51687..c70785cc8ef5 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -378,6 +378,69 @@ LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
378LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id, 378LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id,
379 enum bpf_prog_type prog_type, __u32 ifindex); 379 enum bpf_prog_type prog_type, __u32 ifindex);
380 380
381/*
382 * Get bpf_prog_info in continuous memory
383 *
384 * struct bpf_prog_info has multiple arrays. The user has option to choose
385 * arrays to fetch from kernel. The following APIs provide an uniform way to
386 * fetch these data. All arrays in bpf_prog_info are stored in a single
387 * continuous memory region. This makes it easy to store the info in a
388 * file.
389 *
390 * Before writing bpf_prog_info_linear to files, it is necessary to
391 * translate pointers in bpf_prog_info to offsets. Helper functions
392 * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr()
393 * are introduced to switch between pointers and offsets.
394 *
395 * Examples:
396 * # To fetch map_ids and prog_tags:
397 * __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) |
398 * (1UL << BPF_PROG_INFO_PROG_TAGS);
399 * struct bpf_prog_info_linear *info_linear =
400 * bpf_program__get_prog_info_linear(fd, arrays);
401 *
402 * # To save data in file
403 * bpf_program__bpil_addr_to_offs(info_linear);
404 * write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
405 *
406 * # To read data from file
407 * read(f, info_linear, <proper_size>);
408 * bpf_program__bpil_offs_to_addr(info_linear);
409 */
410enum bpf_prog_info_array {
411 BPF_PROG_INFO_FIRST_ARRAY = 0,
412 BPF_PROG_INFO_JITED_INSNS = 0,
413 BPF_PROG_INFO_XLATED_INSNS,
414 BPF_PROG_INFO_MAP_IDS,
415 BPF_PROG_INFO_JITED_KSYMS,
416 BPF_PROG_INFO_JITED_FUNC_LENS,
417 BPF_PROG_INFO_FUNC_INFO,
418 BPF_PROG_INFO_LINE_INFO,
419 BPF_PROG_INFO_JITED_LINE_INFO,
420 BPF_PROG_INFO_PROG_TAGS,
421 BPF_PROG_INFO_LAST_ARRAY,
422};
423
424struct bpf_prog_info_linear {
425 /* size of struct bpf_prog_info, when the tool is compiled */
426 __u32 info_len;
427 /* total bytes allocated for data, round up to 8 bytes */
428 __u32 data_len;
429 /* which arrays are included in data */
430 __u64 arrays;
431 struct bpf_prog_info info;
432 __u8 data[];
433};
434
435LIBBPF_API struct bpf_prog_info_linear *
436bpf_program__get_prog_info_linear(int fd, __u64 arrays);
437
438LIBBPF_API void
439bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
440
441LIBBPF_API void
442bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
443
381#ifdef __cplusplus 444#ifdef __cplusplus
382} /* extern "C" */ 445} /* extern "C" */
383#endif 446#endif