Move bpf_helpers and add BPF_CORE_READ macros
Subsystems (6)
PATCH 1/6 selftests/bpf: undo GCC-specific bpf_helpers.h changes
In reply to Andrii NakryikoHaving GCC provide its own bpf-helper.h is not the right approach and is going to be changed. Undo bpf_helpers.h change before moving bpf_helpers.h into libbpf.
--- tools/testing/selftests/bpf/bpf_helpers.h | 8 -------- 1 file changed, 8 deletions(-)
tools/testing/selftests/bpf/bpf_helpers.h +0 -8
@@ -13,8 +13,6 @@ ##__VA_ARGS__); \ }) -#ifdef __clang__ -/* helper macro to place programs, maps, license in * different sections in elf_bpf file. Section names * are interpreted by elf_bpf loader
@@ -258,12 +256,6 @@ struct bpf_map_def {unsigned int numa_node; }; -#else - -#include <bpf-helpers.h> - -#endif -#define BPF_ANNOTATE_KV_PAIR (name, type_key, type_val) \ struct ____btf_map_##name { \ type_key key; \
Signature · 1 line
2.17.1
PATCH 2/6 libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Andrii NakryikoMake bpf_helpers.h and bpf_endian.h official part of libbpf. Ensure they are installed along the other libbpf headers.
--- tools/lib/bpf/Makefile | 4 +- tools/lib/bpf/bpf_endian.h | 72 +++++ tools/lib/bpf/bpf_helpers.h | 527 ++++++++++++++++++++++++++++++++++++ 3 files changed, 602 insertions(+), 1 deletion(-) create mode 100644 tools/lib/bpf/bpf_endian.h create mode 100644 tools/lib/bpf/bpf_helpers.h
tools/lib/bpf/Makefile +3 -1
@@ -240,7 +240,9 @@ install_headers: $(call do_install,libbpf.h,$(prefix)/include/bpf,644); \ $(call do_install,btf.h,$(prefix)/include/bpf,644); \ $(call do_install,libbpf_util.h,$(prefix)/include/bpf,644); \ - $(call do_install,xsk.h,$(prefix)/include/bpf,644); + $(call do_install,xsk.h,$(prefix)/include/bpf,644); \ + $(call do_install,bpf_helpers.h,$(prefix)/include/bpf,644); \ + $(call do_install,bpf_endian.h,$(prefix)/include/bpf,644); install_pkgconfig: $(PC_FILE) $(call QUIET_INSTALL, $(PC_FILE)) \
tools/lib/bpf/bpf_endian.h new file +72 -0
72 lines not on this page · raw message
tools/lib/bpf/bpf_helpers.h new file +527 -0
527 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 4/6 libbpf: add BPF_CORE_READ/BPF_CORE_READ_INTO helpers
In reply to Andrii NakryikoAdd few macros simplifying BCC-like multi-level probe reads, while also emitting CO-RE relocations for each read.
--- tools/lib/bpf/bpf_helpers.h | 151 +++++++++++++++++++++++++++++++++++- 1 file changed, 147 insertions(+), 4 deletions(-)
tools/lib/bpf/bpf_helpers.h +147 -4
@@ -19,6 +19,10 @@*/ #define SEC (NAME ) __attribute__((section(NAME), used)) +#ifndef __always_inline +#define __always_inline __attribute__((always_inline)) +#endif +/* helper functions called from eBPF programs written in C */ static void *(*bpf_map_lookup_elem)(void *map,const void *key) = (void *) BPF_FUNC_map_lookup_elem;
@@ -505,7 +509,7 @@ struct pt_regs;#endif /* -* BPF_CORE_READ abstracts away bpf_probe_read() call and captures offset +* bpf_core_read() abstracts away bpf_probe_read() call and captures field * relocation for source address using __builtin_preserve_access_index() * built-in, provided by Clang. *
@@ -520,8 +524,147 @@ struct pt_regs;* actual field offset, based on target kernel BTF type that matches original * (local) BTF, used to record relocation. */ -#define BPF_CORE_READ (dst, src) \ - bpf_probe_read((dst), sizeof(*(src)), \ - __builtin_preserve_access_index(src)) +#define bpf_core_read (dst, sz, src) \ + bpf_probe_read(dst, sz, \ + (const void *)__builtin_preserve_access_index(src)) + +/* +* bpf_core_read_str() is a thin wrapper around bpf_probe_read_str() +* additionally emitting BPF CO-RE field relocation for specified source +* argument. +*/ +#define bpf_core_read_str (dst, sz, src) \ + bpf_probe_read_str(dst, sz, \ + (const void *)__builtin_preserve_access_index(src)) + +#define ___concat (a, b) a ## b +#define ___apply (fn, n) ___concat(fn, n) +#define ___nth (_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, __11,N , ...) N + +/* return number of provided arguments; used for switch-based variadic macro +* definitions (see ___last, ___arrow, etc below) +*/ +#define ___narg (...) ___nth(_, ##__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) +/* return 0 if no arguments are passed, N - otherwise; used for +* recursively-defined macros to specify termination (0) case, and generic +* (N) case (e.g., ___read_ptrs, ___core_read) +*/ +#define ___empty (...) ___nth(_, ##__VA_ARGS__, N, N, N, N, N, N, N, N, N, N, 0) + +#define ___last1 (x) x +#define ___last2 (a, x) x +#define ___last3 (a, b, x) x +#define ___last4 (a, b, c, x) x +#define ___last5 (a, b, c, d, x) x +#define ___last6 (a, b, c, d, e, x) x +#define ___last7 (a, b, c, d, e, f, x) x +#define ___last8 (a, b, c, d, e, f, g, x) x +#define ___last9 (a, b, c, d, e, f, g, h, x) x +#define ___last10 (a, b, c, d, e, f, g, h, i, x) x +#define ___last (...) ___apply(___last, ___narg(__VA_ARGS__))(__VA_ARGS__) + +#define ___nolast2 (a, _) a +#define ___nolast3 (a, b, _) a, b +#define ___nolast4 (a, b, c, _) a, b, c +#define ___nolast5 (a, b, c, d, _) a, b, c, d +#define ___nolast6 (a, b, c, d, e, _) a, b, c, d, e +#define ___nolast7 (a, b, c, d, e, f, _) a, b, c, d, e, f +#define ___nolast8 (a, b, c, d, e, f, g, _) a, b, c, d, e, f, g +#define ___nolast9 (a, b, c, d, e, f, g, h, _) a, b, c, d, e, f, g, h +#define ___nolast10 (a, b, c, d, e, f, g, h, i, _) a, b, c, d, e, f, g, h, i +#define ___nolast (...) ___apply(___nolast, ___narg(__VA_ARGS__))(__VA_ARGS__) + +#define ___arrow1 (a) a +#define ___arrow2 (a, b) a->b +#define ___arrow3 (a, b, c) a->b->c +#define ___arrow4 (a, b, c, d) a->b->c->d +#define ___arrow5 (a, b, c, d, e) a->b->c->d->e +#define ___arrow6 (a, b, c, d, e, f) a->b->c->d->e->f +#define ___arrow7 (a, b, c, d, e, f, g) a->b->c->d->e->f->g +#define ___arrow8 (a, b, c, d, e, f, g, h) a->b->c->d->e->f->g->h +#define ___arrow9 (a, b, c, d, e, f, g, h, i) a->b->c->d->e->f->g->h->i +#define ___arrow10 (a, b, c, d, e, f, g, h, i, j) a->b->c->d->e->f->g->h->i->j +#define ___arrow (...) ___apply(___arrow, ___narg(__VA_ARGS__))(__VA_ARGS__) + +#define ___type (...) typeof(___arrow(__VA_ARGS__)) + +#define ___read (read_fn, dst, src_type, src, accessor) \ + read_fn((void *)(dst), sizeof(*(dst)), &((src_type)(src))->accessor) + +/* "recursively" read a sequence of inner pointers using local __t var */ +#define ___rd_last (...) \ + ___read(bpf_core_read, &__t, \ + ___type(___nolast(__VA_ARGS__)), __t, ___last(__VA_ARGS__)); +#define ___rd_p0 (src) const void *__t = src; +#define ___rd_p1 (...) ___rd_p0(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__) +#define ___rd_p2 (...) ___rd_p1(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__) +#define ___rd_p3 (...) ___rd_p2(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__) +#define ___rd_p4 (...) ___rd_p3(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__) +#define ___rd_p5 (...) ___rd_p4(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__) +#define ___rd_p6 (...) ___rd_p5(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__) +#define ___rd_p7 (...) ___rd_p6(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__) +#define ___rd_p8 (...) ___rd_p7(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__) +#define ___rd_p9 (...) ___rd_p8(___nolast(__VA_ARGS__)) ___rd_last(__VA_ARGS__) +#define ___read_ptrs (src, ...) \ + ___apply(___rd_p, ___narg(__VA_ARGS__))(src, __VA_ARGS__) + +#define ___core_read0 (fn, dst, src, a) \ + ___read(fn, dst, ___type(src), src, a); +#define ___core_readN (fn, dst, src, ...) \ + ___read_ptrs(src, ___nolast(__VA_ARGS__)) \ + ___read(fn, dst, ___type(src, ___nolast(__VA_ARGS__)), __t, \ + ___last(__VA_ARGS__)); +#define ___core_read (fn, dst, src, a, ...) \ + ___apply(___core_read, ___empty(__VA_ARGS__))(fn, dst, \ + src, a, ##__VA_ARGS__) + +/* +* BPF_CORE_READ_INTO() is a more performance-conscious variant of +* BPF_CORE_READ(), in which final field is read into user-provided storage. +* See BPF_CORE_READ() below for more details on general usage. +*/ +#define BPF_CORE_READ_INTO (dst, src, a, ...) \ + ({ \ + ___core_read(bpf_core_read, dst, src, a, ##__VA_ARGS__) \ + }) + +/* +* BPF_CORE_READ_STR_INTO() does same "pointer chasing" as +* BPF_CORE_READ() for intermediate pointers, but then executes (and returns +* corresponding error code) bpf_core_read_str() for final string read. +*/ +#define BPF_CORE_READ_STR_INTO (dst, src, a, ...) \ + ({ \ + ___core_read(bpf_core_read_str, dst, src, a, ##__VA_ARGS__) \ + }) + +/* +* BPF_CORE_READ() is used to simplify BPF CO-RE relocatable read, especially +* when there are few pointer chasing steps. +* E.g., what in non-BPF world (or in BPF w/ BCC) would be something like: +* int x = s->a.b.c->d.e->f->g; +* can be succinctly achieved using BPF_CORE_READ as: +* int x = BPF_CORE_READ(s, a.b.c, d.e, f, g); +* +* BPF_CORE_READ will decompose above statement into 4 bpf_core_read (BPF +* CO-RE relocatable bpf_probe_read() wrapper) calls, logically equivalent to: +* 1. const void *__t = s->a.b.c; +* 2. __t = __t->d.e; +* 3. __t = __t->f; +* 4. return __t->g; +* +* Equivalence is logical, because there is a heavy type casting/preservation +* involved, as well as all the reads are happening through bpf_probe_read() +* calls using __builtin_preserve_access_index() to emit CO-RE relocations. +* +* N.B. Only up to 9 "field accessors" are supported, which should be more +* than enough for any practical purpose. +*/ +#define BPF_CORE_READ (src, a, ...) \ + ({ \ + ___type(src, a, ##__VA_ARGS__) __r; \ + BPF_CORE_READ_INTO(&__r, src, a, ##__VA_ARGS__); \ + __r; \ + })#endif
Signature · 1 line
2.17.1
cover Move bpf_helpers and add BPF_CORE_READ macros
This patch set make bpf_helpers.h and bpf_endian.h part of libbpf itself for consumption by user BPF programs, not just selftests. All the selftests are switched to use libbpf's bpf_helpers/bpf_endian and selftests' ones are removed.
As part of this patch set we also add BPF_CORE_READ variadic macros, that are simplifying BPF CO-RE reads, especially the ones that have to follow few pointers. E.g., what in non-BPF world (and when using BCC) would be:
int x = s->a->b.c->d; /* s, a, and b.c are pointers */
today would have to be written using explicit bpf_probe_read() calls as:
void *t; int x; bpf_probe_read(&t, sizeof(t), s->a); bpf_probe_read(&t, sizeof(t), ((struct b *)t)->b.c); bpf_probe_read(&x, sizeof(x), ((struct c *)t)->d);
This is super inconvenient and distracts from program logic a lot. Now, with added BPF_CORE_READ() macros, you can write the above as:
int x = BPF_CORE_READ(s, a, b.c, d);
Up to 9 levels of pointer chasing are supported, which should be enough for any practical purpose, hopefully, without adding too much boilerplate macro definitions (though there is admittedly some, given how variadic and recursive C macro have to be implemented).
There is also BPF_CORE_READ_INTO() variant, which relies on caller to allocate space for result:
int x; BPF_CORE_READ_INTO(&x, s, a, b.c, d);
Result of last bpf_probe_read() call in the chain of calls is the result of BPF_CORE_READ_INTO(). If any intermediate bpf_probe_read() aall fails, then all the subsequent ones will fail too, so this is sufficient to know whether overall "operation" succeeded or not. No short-circuiting of bpf_probe_read()s is done, though.
BPF_CORE_READ_STR_INTO() is added as well, which differs from BPF_CORE_READ_INTO() only in that last bpf_probe_read() call (to read final field after chasing pointers) is replaced with bpf_probe_read_str(). Result of bpf_probe_read_str() is returned as a result of BPF_CORE_READ_STR_INTO() macro itself, so that applications can track return code and/or length of read string.
Patch set outline:
- patch #1 undoes previously added GCC-specific bpf-helpers.h include;
- patch #2 adds bpf_helper.h and bpf_endian.h into libbpf sources;
- patch #3 adjusts selftests (Makefile only) and removes bpf_helpers.h/bpf_endian.h from seftests/bpf;
- patch #4 adds variadic BPF_CORE_READ() macro family, as described above;
- patch #5 fixes existing BPF CO-RE reloc selftest as it previously re-used BPF_CORE_READ() macro name with slightly different syntax;
- patch #6 adds tests to verify all possible levels of pointer nestedness for BPF_CORE_READ(), as well as correctness test for BPF_CORE_READ_STR_INTO().
Andrii Nakryiko (6):
selftests/bpf: undo GCC-specific bpf_helpers.h changes
libbpf: move bpf_helpers.h, bpf_endian.h into libbpf selftests/bpf: switch test to use libbpf's helpers
libbpf: add BPF_CORE_READ/BPF_CORE_READ_INTO helpers selftests/bpf: adjust CO-RE reloc tests for new BPF_CORE_READ macro selftests/bpf: add BPF_CORE_READ and BPF_CORE_READ_STR_INTO macro
tests
tools/lib/bpf/Makefile | 4 +-
.../selftests => lib}/bpf/bpf_endian.h | 0
.../selftests => lib}/bpf/bpf_helpers.h | 159 ++++++++++++++++--
tools/testing/selftests/bpf/Makefile | 2 +-
.../selftests/bpf/prog_tests/core_reloc.c | 8 +-
.../selftests/bpf/progs/core_reloc_types.h | 9 +
.../bpf/progs/test_core_reloc_arrays.c | 10 +-
.../bpf/progs/test_core_reloc_flavors.c | 8 +-
.../bpf/progs/test_core_reloc_ints.c | 18 +-
.../bpf/progs/test_core_reloc_kernel.c | 60 ++++++-
.../bpf/progs/test_core_reloc_misc.c | 8 +-
.../bpf/progs/test_core_reloc_mods.c | 18 +-
.../bpf/progs/test_core_reloc_nesting.c | 6 +-
.../bpf/progs/test_core_reloc_primitives.c | 12 +-
.../bpf/progs/test_core_reloc_ptr_as_arr.c | 4 +-
15 files changed, 273 insertions(+), 53 deletions(-)
rename tools/{testing/selftests => lib}/bpf/bpf_endian.h (100%)
rename tools/{testing/selftests => lib}/bpf/bpf_helpers.h (77%)Signature · 1 line
2.17.1
PATCH 3/6 selftests/bpf: switch test to use libbpf's helpers
Adjust selftest to use bpf_helpers.h and bpf_endian.h from libbpf. Delete bpf_helpers.h/bpf_endian.h, that are still part of selftests.
--- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/bpf_endian.h | 72 --- tools/testing/selftests/bpf/bpf_helpers.h | 527 ---------------------- 3 files changed, 1 insertion(+), 600 deletions(-) delete mode 100644 tools/testing/selftests/bpf/bpf_endian.h delete mode 100644 tools/testing/selftests/bpf/bpf_helpers.h
tools/testing/selftests/bpf/Makefile +1 -1
@@ -148,7 +148,7 @@ $(shell $(1) -v -E - </dev/null 2>&1 \ endef CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG)) BPF_CFLAGS = -I. -I./include/uapi -I../../../include/uapi \ - -I$(OUTPUT)/../usr/include -D__TARGET_ARCH_$(SRCARCH) + -I$(BPFDIR) -I$(OUTPUT)/../usr/include -D__TARGET_ARCH_$(SRCARCH) CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \ -Wno-compare-distinct-pointer-types
tools/testing/selftests/bpf/bpf_endian.h deleted +0 -72
72 lines not on this page · raw message
tools/testing/selftests/bpf/bpf_helpers.h deleted +0 -527
527 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 6/6 selftests/bpf: add BPF_CORE_READ and BPF_CORE_READ_STR_INTO macro tests
In reply to Andrii NakryikoValidate BPF_CORE_READ correctness and handling of up to 9 levels of nestedness using cyclic task->(group_leader->)*->tgid chains.
Also add a test of maximum-dpeth BPF_CORE_READ_STR_INTO() macro.
--- .../selftests/bpf/prog_tests/core_reloc.c | 8 ++- .../selftests/bpf/progs/core_reloc_types.h | 9 ++++ .../bpf/progs/test_core_reloc_kernel.c | 54 ++++++++++++++++++- 3 files changed, 68 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/core_reloc.c +6 -2
@@ -193,8 +193,12 @@ static struct core_reloc_test_case test_cases[] = { .btf_src_file = NULL, /* load from /lib/modules/$(uname -r) */ .input = "", .input_len = 0, - .output = "\1", /* true */ - .output_len = 1, + .output = STRUCT_TO_CHAR_PTR(core_reloc_kernel_output) { + .valid = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, }, + .comm ="test_progs\0\0\0\0\0" , + .comm_len = 11, + }, + .output_len = sizeof(struct core_reloc_kernel_output), },/* validate BPF program can use multiple flavors to match against
tools/testing/selftests/bpf/progs/core_reloc_types.h +9 -0
@@ -1,5 +1,14 @@#include <stdint.h> #include <stdbool.h> +/* +* KERNEL +*/ + +struct core_reloc_kernel_output { +int valid[10 ]; +char comm[16 ]; +int comm_len; +};/* * FLAVORS
tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c +53 -1
76 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 5/6 selftests/bpf: adjust CO-RE reloc tests for new BPF_CORE_READ macro
In reply to Andrii NakryikoGiven introduction of variadic BPF_CORE_READ with slightly different syntax and semantics, define CORE_READ, which is a thin wrapper around low-level bpf_core_read() macro, which in turn is just a wrapper around bpf_probe_read(). BPF_CORE_READ is higher-level variadic macro supporting multi-pointer reads and are tested separately.
--- .../bpf/progs/test_core_reloc_arrays.c | 10 ++++++---- .../bpf/progs/test_core_reloc_flavors.c | 8 +++++--- .../selftests/bpf/progs/test_core_reloc_ints.c | 18 ++++++++++-------- .../bpf/progs/test_core_reloc_kernel.c | 6 ++++-- .../selftests/bpf/progs/test_core_reloc_misc.c | 8 +++++--- .../selftests/bpf/progs/test_core_reloc_mods.c | 18 ++++++++++-------- .../bpf/progs/test_core_reloc_nesting.c | 6 ++++-- .../bpf/progs/test_core_reloc_primitives.c | 12 +++++++----- .../bpf/progs/test_core_reloc_ptr_as_arr.c | 4 +++- 9 files changed, 54 insertions(+), 36 deletions(-)
tools/testing/selftests/bpf/progs/test_core_reloc_arrays.c +6 -4
@@ -31,6 +31,8 @@ struct core_reloc_arrays {struct core_reloc_arrays_substruct d[1 ][2 ]; }; +#define CORE_READ (dst, src) bpf_core_read(dst, sizeof(*dst), src) + SEC("raw_tracepoint/sys_enter")int test_core_arrays (void *ctx) {
@@ -38,16 +40,16 @@ int test_core_arrays(void *ctx)struct core_reloc_arrays_output *out = (void *)&data.out;/* in->a[2] */ -if (BPF_CORE_READ (&out->a2, &in->a[2 ])) +if (CORE_READ (&out->a2, &in->a[2 ]))return 1 ;/* in->b[1][2][3] */ -if (BPF_CORE_READ (&out->b123, &in->b[1 ][2 ][3 ])) +if (CORE_READ (&out->b123, &in->b[1 ][2 ][3 ]))return 1 ;/* in->c[1].c */ -if (BPF_CORE_READ (&out->c1c, &in->c[1 ].c)) +if (CORE_READ (&out->c1c, &in->c[1 ].c))return 1 ;/* in->d[0][0].d */ -if (BPF_CORE_READ (&out->d00d, &in->d[0 ][0 ].d)) +if (CORE_READ (&out->d00d, &in->d[0 ][0 ].d))return 1 ;return 0 ;
tools/testing/selftests/bpf/progs/test_core_reloc_flavors.c +5 -3
@@ -39,6 +39,8 @@ struct core_reloc_flavors___weird { }; }; +#define CORE_READ (dst, src) bpf_core_read(dst, sizeof(*dst), src) + SEC("raw_tracepoint/sys_enter")int test_core_flavors (void *ctx) {
@@ -48,13 +50,13 @@ int test_core_flavors(void *ctx)struct core_reloc_flavors *out = (void *)&data.out;/* read a using weird layout */ -if (BPF_CORE_READ (&out->a, &in_weird->a)) +if (CORE_READ (&out->a, &in_weird->a))return 1 ;/* read b using reversed layout */ -if (BPF_CORE_READ (&out->b, &in_rev->b)) +if (CORE_READ (&out->b, &in_rev->b))return 1 ;/* read c using original layout */ -if (BPF_CORE_READ (&out->c, &in_orig->c)) +if (CORE_READ (&out->c, &in_orig->c))return 1 ;return 0 ;
tools/testing/selftests/bpf/progs/test_core_reloc_ints.c +10 -8
@@ -23,20 +23,22 @@ struct core_reloc_ints {int64_t s64_field; }; +#define CORE_READ (dst, src) bpf_core_read(dst, sizeof(*dst), src) + SEC("raw_tracepoint/sys_enter")int test_core_ints (void *ctx) {struct core_reloc_ints *in = (void *)&data.in;struct core_reloc_ints *out = (void *)&data.out; -if (BPF_CORE_READ (&out->u8_field, &in->u8_field) || -BPF_CORE_READ (&out->s8_field, &in->s8_field) || -BPF_CORE_READ (&out->u16_field, &in->u16_field) || -BPF_CORE_READ (&out->s16_field, &in->s16_field) || -BPF_CORE_READ (&out->u32_field, &in->u32_field) || -BPF_CORE_READ (&out->s32_field, &in->s32_field) || -BPF_CORE_READ (&out->u64_field, &in->u64_field) || -BPF_CORE_READ (&out->s64_field, &in->s64_field)) +if (CORE_READ (&out->u8_field, &in->u8_field) || +CORE_READ (&out->s8_field, &in->s8_field) || +CORE_READ (&out->u16_field, &in->u16_field) || +CORE_READ (&out->s16_field, &in->s16_field) || +CORE_READ (&out->u32_field, &in->u32_field) || +CORE_READ (&out->s32_field, &in->s32_field) || +CORE_READ (&out->u64_field, &in->u64_field) || +CORE_READ (&out->s64_field, &in->s64_field))return 1 ;return 0 ;
tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c +4 -2
@@ -17,6 +17,8 @@ struct task_struct {int tgid; }; +#define CORE_READ (dst, src) bpf_core_read(dst, sizeof(*dst), src) + SEC("raw_tracepoint/sys_enter")int test_core_kernel (void *ctx) {
@@ -24,8 +26,8 @@ int test_core_kernel(void *ctx)uint64_t pid_tgid =bpf_get_current_pid_tgid ();int pid, tgid; -if (BPF_CORE_READ (&pid, &task->pid) || -BPF_CORE_READ (&tgid, &task->tgid)) +if (CORE_READ (&pid, &task->pid) || +CORE_READ (&tgid, &task->tgid))return 1 ;/* validate pid + tgid matches */
tools/testing/selftests/bpf/progs/test_core_reloc_misc.c +5 -3
@@ -32,6 +32,8 @@ struct core_reloc_misc_extensible {int b; }; +#define CORE_READ (dst, src) bpf_core_read(dst, sizeof(*dst), src) + SEC("raw_tracepoint/sys_enter")int test_core_misc (void *ctx) {
@@ -41,15 +43,15 @@ int test_core_misc(void *ctx)struct core_reloc_misc_output *out = (void *)&data.out;/* record two different relocations with the same accessor string */ -if (BPF_CORE_READ (&out->a, &in_a->a1) ||/* accessor: 0:0 */ -BPF_CORE_READ (&out->b, &in_b->b1))/* accessor: 0:0 */ +if (CORE_READ (&out->a, &in_a->a1) ||/* accessor: 0:0 */ +CORE_READ (&out->b, &in_b->b1))/* accessor: 0:0 */ return 1 ;/* Validate relocations capture array-only accesses for structs with * fixed header, but with potentially extendable tail. This will read * first 4 bytes of 2nd element of in_ext array of potentially * variably sized struct core_reloc_misc_extensible. */ -if (BPF_CORE_READ (&out->c, &in_ext[2 ]))/* accessor: 2 */ +if (CORE_READ (&out->c, &in_ext[2 ]))/* accessor: 2 */ return 1 ;return 0 ;
tools/testing/selftests/bpf/progs/test_core_reloc_mods.c +10 -8
@@ -41,20 +41,22 @@ struct core_reloc_mods {core_reloc_mods_substruct_t h; }; +#define CORE_READ (dst, src) bpf_core_read(dst, sizeof(*dst), src) + SEC("raw_tracepoint/sys_enter")int test_core_mods (void *ctx) {struct core_reloc_mods *in = (void *)&data.in;struct core_reloc_mods_output *out = (void *)&data.out; -if (BPF_CORE_READ (&out->a, &in->a) || -BPF_CORE_READ (&out->b, &in->b) || -BPF_CORE_READ (&out->c, &in->c) || -BPF_CORE_READ (&out->d, &in->d) || -BPF_CORE_READ (&out->e, &in->e[2 ]) || -BPF_CORE_READ (&out->f, &in->f[1 ]) || -BPF_CORE_READ (&out->g, &in->g.x) || -BPF_CORE_READ (&out->h, &in->h.y)) +if (CORE_READ (&out->a, &in->a) || +CORE_READ (&out->b, &in->b) || +CORE_READ (&out->c, &in->c) || +CORE_READ (&out->d, &in->d) || +CORE_READ (&out->e, &in->e[2 ]) || +CORE_READ (&out->f, &in->f[1 ]) || +CORE_READ (&out->g, &in->g.x) || +CORE_READ (&out->h, &in->h.y))return 1 ;return 0 ;
tools/testing/selftests/bpf/progs/test_core_reloc_nesting.c +4 -2
@@ -30,15 +30,17 @@ struct core_reloc_nesting { } b; }; +#define CORE_READ (dst, src) bpf_core_read(dst, sizeof(*dst), src) + SEC("raw_tracepoint/sys_enter")int test_core_nesting (void *ctx) {struct core_reloc_nesting *in = (void *)&data.in;struct core_reloc_nesting *out = (void *)&data.out; -if (BPF_CORE_READ (&out->a.a.a, &in->a.a.a)) +if (CORE_READ (&out->a.a.a, &in->a.a.a))return 1 ; -if (BPF_CORE_READ (&out->b.b.b, &in->b.b.b)) +if (CORE_READ (&out->b.b.b, &in->b.b.b))return 1 ;return 0 ;
tools/testing/selftests/bpf/progs/test_core_reloc_primitives.c +7 -5
@@ -25,17 +25,19 @@ struct core_reloc_primitives {int (*f)(const char *); }; +#define CORE_READ (dst, src) bpf_core_read(dst, sizeof(*dst), src) + SEC("raw_tracepoint/sys_enter")int test_core_primitives (void *ctx) {struct core_reloc_primitives *in = (void *)&data.in;struct core_reloc_primitives *out = (void *)&data.out; -if (BPF_CORE_READ (&out->a, &in->a) || -BPF_CORE_READ (&out->b, &in->b) || -BPF_CORE_READ (&out->c, &in->c) || -BPF_CORE_READ (&out->d, &in->d) || -BPF_CORE_READ (&out->f, &in->f)) +if (CORE_READ (&out->a, &in->a) || +CORE_READ (&out->b, &in->b) || +CORE_READ (&out->c, &in->c) || +CORE_READ (&out->d, &in->d) || +CORE_READ (&out->f, &in->f))return 1 ;return 0 ;
tools/testing/selftests/bpf/progs/test_core_reloc_ptr_as_arr.c +3 -1
@@ -16,13 +16,15 @@ struct core_reloc_ptr_as_arr {int a; }; +#define CORE_READ (dst, src) bpf_core_read(dst, sizeof(*dst), src) + SEC("raw_tracepoint/sys_enter")int test_core_ptr_as_arr (void *ctx) {struct core_reloc_ptr_as_arr *in = (void *)&data.in;struct core_reloc_ptr_as_arr *out = (void *)&data.out; -if (BPF_CORE_READ (&out->a, &in[2 ].a)) +if (CORE_READ (&out->a, &in[2 ].a))return 1 ;return 0 ;
Signature · 1 line
2.17.1
Re: [PATCH bpf-next 1/6] selftests/bpf: undo GCC-specific bpf_helpers.h changes
In reply to Andrii NakryikoQuoted 6 lines · Andrii Nakryiko
Having GCC provide its own
bpf-helper.his not the right approach and is going to be changed. Undobpf_helpers.hchange before movingbpf_helpers.hinto libbpf.Signed-off-by Andrii Nakryiko <[email protected]>
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Andrii NakryikoQuoted 5 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.Signed-off-by Andrii Nakryiko <[email protected]>
Can we merge/rearrange 2/6 and 3/6, so they is a git-rename instead of many +++ and ---?
Thanks,
Song
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
Quoted 10 lines · Song Liu
Quoted 5 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.Signed-off-by Andrii Nakryiko <[email protected]>Can we merge/rearrange 2/6 and 3/6, so they is a git-rename instead of many +++ and ---?
I arranged them that way because of Github sync. We don't sync selftests/bpf changes to Github, and it causes more churn if commits have a mix of libbpf and selftests changes.
I didn't modify bpf_helpers.h/bpf_endian.h between those patches, so don't worry about reviewing contents ;)
Quoted 3 lines
Thanks,
Song
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
Quoted 12 lines · Andrii Nakryiko
Quoted 10 lines · Song Liu
Quoted 5 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.Signed-off-by Andrii Nakryiko <[email protected]>Can we merge/rearrange 2/6 and 3/6, so they is a git-rename instead of many +++ and ---?
Quoted 6 lines
I arranged them that way because of Github sync. We don't sync selftests/bpf changes to Github, and it causes more churn if commits have a mix of libbpf and selftests changes.
I didn't modify
bpf_helpers.h/bpf_endian.hbetween those patches, so don't worry about reviewing contents ;)
I thought we were over this :/ Please merge the patches.
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Andrii NakryikoQuoted 11 lines · Andrii Nakryiko
Quoted 6 lines · Song Liu
Quoted 4 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by Andrii Nakryiko <[email protected]>Can we merge/rearrange 2/6 and 3/6, so they is a git-rename instead of many +++ and ---?
I arranged them that way because of Github sync. We don't sync selftests/bpf changes to Github, and it causes more churn if commits have a mix of libbpf and selftests changes.
Aha, I missed this point.
Quoted 2 lines
I didn't modify
bpf_helpers.h/bpf_endian.hbetween those patches, so don't worry about reviewing contents ;)
Well, we need to be careful here. As headers in a library should be more stable than headers shipped with the code.
Here, I am a little concerned with the fact that we added BPF_CORE_READ() to libbpf, and then changed its syntax. This is within one release, so it is mostly OK.
Thanks,
Song
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Jakub KicinskiQuoted 17 lines · Jakub Kicinski
Quoted 12 lines · Andrii Nakryiko
Quoted 10 lines · Song Liu
Quoted 5 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.Signed-off-by Andrii Nakryiko <[email protected]>Can we merge/rearrange 2/6 and 3/6, so they is a git-rename instead of many +++ and ---?
Quoted 8 lines
Quoted 6 lines
I arranged them that way because of Github sync. We don't sync selftests/bpf changes to Github, and it causes more churn if commits have a mix of libbpf and selftests changes.
I didn't modify
bpf_helpers.h/bpf_endian.hbetween those patches, so don't worry about reviewing contents ;)I thought we were over this :/ Please merge the patches.
Andrii changed syntax for BPF_CORE_READ(). I guess that is new?
Thanks,
Song
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Song LiuQuoted 26 lines · Song Liu
Quoted 12 lines · Andrii Nakryiko
Quoted 6 lines · Song Liu
Quoted 4 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by Andrii Nakryiko <[email protected]>Can we merge/rearrange 2/6 and 3/6, so they is a git-rename instead of many +++ and ---?
I arranged them that way because of Github sync. We don't sync selftests/bpf changes to Github, and it causes more churn if commits have a mix of libbpf and selftests changes.
Aha, I missed this point.
Quoted 2 lines
I didn't modify
bpf_helpers.h/bpf_endian.hbetween those patches, so don't worry about reviewing contents ;)
Quoted 6 lines
Well, we need to be careful here. As headers in a library should be more stable than headers shipped with the code.
Here, I am a little concerned with the fact that we added
BPF_CORE_READ()to libbpf, and then changed its syntax. This is within one release, so it is mostly OK.
Well, I could bundle bpf_helpers move and fixing up selftests in one commit, but I think it just makes commit unnecessarily big and convoluted. BPF_CORE_READ in previous form was ever only used by selftests, so it was never "released" per se, so it seems fine to do it this way, but let me know if you disagree.
Quoted 4 lines
Thanks,
Song
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Jakub KicinskiQuoted 14 lines · Jakub Kicinski
Quoted 12 lines · Andrii Nakryiko
Quoted 10 lines · Song Liu
Quoted 5 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.Signed-off-by Andrii Nakryiko <[email protected]>Can we merge/rearrange 2/6 and 3/6, so they is a git-rename instead of many +++ and ---?
Quoted 8 lines
Quoted 6 lines
I arranged them that way because of Github sync. We don't sync selftests/bpf changes to Github, and it causes more churn if commits have a mix of libbpf and selftests changes.
I didn't modify
bpf_helpers.h/bpf_endian.hbetween those patches, so don't worry about reviewing contents ;)I thought we were over this :/ Please merge the patches.
I'll merge those two patches, our sync script can handle that now, though with a bit of human input. I'm not exactly sure on the "why" though, I think generally splitting libbpf changes and selftests changes is a good thing, no?
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Andrii NakryikoQuoted 38 lines · Andrii Nakryiko
Quoted 26 lines · Song Liu
Quoted 12 lines · Andrii Nakryiko
Quoted 6 lines · Song Liu
Quoted 4 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by Andrii Nakryiko <[email protected]>Can we merge/rearrange 2/6 and 3/6, so they is a git-rename instead of many +++ and ---?
I arranged them that way because of Github sync. We don't sync selftests/bpf changes to Github, and it causes more churn if commits have a mix of libbpf and selftests changes.
Aha, I missed this point.
Quoted 2 lines
I didn't modify
bpf_helpers.h/bpf_endian.hbetween those patches, so don't worry about reviewing contents ;)Quoted 6 lines
Well, we need to be careful here. As headers in a library should be more stable than headers shipped with the code.
Here, I am a little concerned with the fact that we added
BPF_CORE_READ()to libbpf, and then changed its syntax. This is within one release, so it is mostly OK.
Quoted 5 lines
Well, I could bundle
bpf_helpersmove and fixing up selftests in one commit, but I think it just makes commit unnecessarily big and convoluted.BPF_CORE_READin previous form was ever only used by selftests, so it was never "released" per se, so it seems fine to do it this way, but let me know if you disagree.
A better approach is to modify BPF_CORE_READ in selftests before moving it to libbpf. But I am ok with current approach as-is.
Song
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Song LiuQuoted 19 lines · Song Liu
Quoted 19 lines · Jakub Kicinski
Quoted 16 lines · Andrii Nakryiko
Quoted 10 lines · Song Liu
Quoted 5 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.Signed-off-by Andrii Nakryiko <[email protected]>Can we merge/rearrange 2/6 and 3/6, so they is a git-rename instead of many +++ and ---?
I arranged them that way because of Github sync. We don't sync selftests/bpf changes to Github, and it causes more churn if commits have a mix of libbpf and selftests changes.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
I didn't modify
bpf_helpers.h/bpf_endian.hbetween those patches, so don't worry about reviewing contents ;)I thought we were over this :/ Please merge the patches.
Andrii changed syntax for
BPF_CORE_READ(). I guess that is new?
I meant the battle to not split changes into harder to review, and less logical form based on what some random, never review upstream script can or cannot do :)
I was responding to the "don't worry about reviewing contents" - as you pointed out git would just generate a move..
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
Quoted 29 lines · Jakub Kicinski
Quoted 24 lines · Song Liu
Quoted 23 lines · Jakub Kicinski
Quoted 12 lines · Andrii Nakryiko
Quoted 10 lines · Song Liu
Quoted 5 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.Signed-off-by Andrii Nakryiko <[email protected]>Can we merge/rearrange 2/6 and 3/6, so they is a git-rename instead of many +++ and ---?
Quoted 6 lines
I arranged them that way because of Github sync. We don't sync selftests/bpf changes to Github, and it causes more churn if commits have a mix of libbpf and selftests changes.
I didn't modify
bpf_helpers.h/bpf_endian.hbetween those patches, so don't worry about reviewing contents ;)I thought we were over this :/ Please merge the patches.
Quoted 8 lines
Quoted 1 line
Andrii changed syntax for
BPF_CORE_READ(). I guess that is new?I meant the battle to not split changes into harder to review, and less logical form based on what some random, never review upstream script can or cannot do :)
I was responding to the "don't worry about reviewing contents" - as you pointed out git would just generate a move..
Yeah, I got your actual point a few minutes later.
Thanks for the clarification. :)
Song
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Andrii NakryikoQuoted 18 lines · Andrii Nakryiko
Quoted 17 lines · Jakub Kicinski
Quoted 16 lines · Andrii Nakryiko
Quoted 10 lines · Song Liu
Quoted 5 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.Signed-off-by Andrii Nakryiko <[email protected]>Can we merge/rearrange 2/6 and 3/6, so they is a git-rename instead of many +++ and ---?
I arranged them that way because of Github sync. We don't sync selftests/bpf changes to Github, and it causes more churn if commits have a mix of libbpf and selftests changes.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
I didn't modify
bpf_helpers.h/bpf_endian.hbetween those patches, so don't worry about reviewing contents ;)I thought we were over this :/ Please merge the patches.
I'll merge those two patches, our sync script can handle that now, though with a bit of human input. I'm not exactly sure on the "why" though
Awesome, thank you!
Quoted 2 lines
I think generally splitting libbpf changes and selftests changes is a good thing, no?
I'm not sure, here headers are moved to a better location. To me it seems like the logical change is move, rather than add X, remove X.
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Andrii Nakryikotools/lib/bpf/bpf_helpers.h line 257
Quoted 10 lines
tools/lib/bpf/bpf_helpers.h new file +9
-0+struct bpf_map_def { +unsigned int type; +unsigned int key_size; +unsigned int value_size; +unsigned int max_entries; +unsigned int map_flags; +unsigned int inner_map_idx; +unsigned int numa_node; +};
Didn't we agree on no new bpf_map_def ABI in libbpf, and that all additions should be BTF-based?
-Toke
Re: [PATCH bpf-next 1/6] selftests/bpf: undo GCC-specific bpf_helpers.h changes
In reply to Andrii NakryikoQuoted 42 lines
Am 30.09.2019 um 20:58 schrieb Andrii Nakryiko <[email protected]>:
Having GCC provide its own
bpf-helper.his not the right approach and is going to be changed. Undobpf_helpers.hchange before movingbpf_helpers.hinto libbpf.Signed-off-by Andrii Nakryiko <[email protected]>---
tools/testing/selftests/bpf/bpf_helpers.h| 8 --------1 file changed, 8 deletions(-)tools/testing/selftests/bpf/bpf_helpers.h +0
-8@@ -13,8 +13,6 @@ ##__VA_ARGS__); \ }) -#ifdef __clang__ -/* helper macro to place programs, maps, license in * different sections in elf_bpf file. Section names * are interpreted by elf_bpf loader @@ -258,12 +256,6 @@ struct bpf_map_def {unsigned int numa_node; }; -#else - -#include <bpf-helpers.h> - -#endif -#define BPF_ANNOTATE_KV_PAIR (name, type_key, type_val) \ struct ____btf_map_##name { \ type_key key; \Signature · 1 line
2.17.1
RE: [PATCH bpf-next 4/6] libbpf: add BPF_CORE_READ/BPF_CORE_READ_INTO helpers
In reply to Andrii NakryikoQuoted 4 lines · Andrii Nakryiko
Add few macros simplifying BCC-like multi-level probe reads, while also emitting CO-RE relocations for each read.
Signed-off-by Andrii Nakryiko <[email protected]>
LGTM.
RE: [PATCH bpf-next 1/6] selftests/bpf: undo GCC-specific bpf_helpers.h changes
In reply to Andrii NakryikoQuoted 38 lines · Andrii Nakryiko
Having GCC provide its own
bpf-helper.his not the right approach and is going to be changed. Undobpf_helpers.hchange before movingbpf_helpers.hinto libbpf.Signed-off-by Andrii Nakryiko <[email protected]>--- tools/testing/selftests/bpf/bpf_helpers.h | 8 -------- 1 file changed, 8 deletions(-)tools/testing/selftests/bpf/bpf_helpers.h +0
-8@@ -13,8 +13,6 @@ ##__VA_ARGS__); \ }) -#ifdef __clang__ -/* helper macro to place programs, maps, license in * different sections in elf_bpf file. Section names * are interpreted by elf_bpf loader @@ -258,12 +256,6 @@ struct bpf_map_def {unsigned int numa_node; }; -#else - -#include <bpf-helpers.h> - -#endif -#define BPF_ANNOTATE_KV_PAIR (name, type_key, type_val) \ struct ____btf_map_##name { \ type_key key; \Signature · 1 line
2.17.1
RE: [PATCH bpf-next 5/6] selftests/bpf: adjust CO-RE reloc tests for new BPF_CORE_READ macro
In reply to Andrii NakryikoQuoted 7 lines · Andrii Nakryiko
Given introduction of variadic
BPF_CORE_READwith slightly different syntax and semantics, defineCORE_READ, which is a thin wrapper around low-levelbpf_core_read()macro, which in turn is just a wrapper aroundbpf_probe_read().BPF_CORE_READis higher-level variadic macro supporting multi-pointer reads and are tested separately.Signed-off-by Andrii Nakryiko <[email protected]>
Quoted 12 lines
--- .../bpf/progs/test_core_reloc_arrays.c | 10 ++++++---- .../bpf/progs/test_core_reloc_flavors.c | 8 +++++--- .../selftests/bpf/progs/test_core_reloc_ints.c | 18 ++++++++++-------- .../bpf/progs/test_core_reloc_kernel.c | 6 ++++-- .../selftests/bpf/progs/test_core_reloc_misc.c | 8 +++++--- .../selftests/bpf/progs/test_core_reloc_mods.c | 18 ++++++++++-------- .../bpf/progs/test_core_reloc_nesting.c | 6 ++++-- .../bpf/progs/test_core_reloc_primitives.c | 12 +++++++----- .../bpf/progs/test_core_reloc_ptr_as_arr.c | 4 +++- 9 files changed, 54 insertions(+), 36 deletions(-)
Starting to get many layers of macros here but makes sense here.
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Toke Høiland-JørgensenQuoted 14 lines · Toke Høiland-Jørgensen
tools/lib/bpf/bpf_helpers.h line 257
Quoted 10 lines
tools/lib/bpf/bpf_helpers.h new file +9
-0+struct bpf_map_def { +unsigned int type; +unsigned int key_size; +unsigned int value_size; +unsigned int max_entries; +unsigned int map_flags; +unsigned int inner_map_idx; +unsigned int numa_node; +};Didn't we agree on no new
bpf_map_defABI in libbpf, and that all additions should be BTF-based?
Quoted 2 lines
-Toke
We use libbpf on pre BTF kernels so in this case I think it makes sense to add these fields. Having inner_map_idx there should allow us to remove some other things we have sitting around.
.John
RE: [PATCH bpf-next 6/6] selftests/bpf: add BPF_CORE_READ and BPF_CORE_READ_STR_INTO macro tests
In reply to Andrii NakryikoQuoted 7 lines · Andrii Nakryiko
Validate
BPF_CORE_READcorrectness and handling of up to 9 levels of nestedness using cyclic task->(group_leader->)*->tgid chains.Also add a test of maximum-dpeth
BPF_CORE_READ_STR_INTO()macro.Signed-off-by Andrii Nakryiko <[email protected]>---
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Toke Høiland-JørgensenQuoted 14 lines · Toke Høiland-Jørgensen
tools/lib/bpf/bpf_helpers.h line 257
Quoted 11 lines
tools/lib/bpf/bpf_helpers.h new file +9
-0+struct bpf_map_def { +unsigned int type; +unsigned int key_size; +unsigned int value_size; +unsigned int max_entries; +unsigned int map_flags; +unsigned int inner_map_idx; +unsigned int numa_node; +};Didn't we agree on no new
bpf_map_defABI in libbpf, and that all additions should be BTF-based?
Oh yes, we did, sorry, this is an oversight. I really appreciate you pointing this out ;)
I'll go over bpf_helpers.h carefully and will double-check if we don't have any other custom selftests-only stuff left there.
Quoted 3 lines
-Toke
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to John FastabendQuoted 16 lines · John Fastabend
Quoted 14 lines · Toke Høiland-Jørgensen
tools/lib/bpf/bpf_helpers.h line 257
Quoted 10 lines
tools/lib/bpf/bpf_helpers.h new file +9
-0+struct bpf_map_def { +unsigned int type; +unsigned int key_size; +unsigned int value_size; +unsigned int max_entries; +unsigned int map_flags; +unsigned int inner_map_idx; +unsigned int numa_node; +};Didn't we agree on no new
bpf_map_defABI in libbpf, and that all additions should be BTF-based?
Quoted 6 lines
Quoted 2 lines
-Toke
We use libbpf on pre BTF kernels so in this case I think it makes sense to add these fields. Having
inner_map_idxthere should allow us to remove some other things we have sitting around.
We had a discussion about supporting non-BTF and non-standard BPF map definition before and it's still on my TODO list to go and do a proof of concept how that can look like and what libbpf changes we need to make. Right now libbpf doesn't support those new fields anyway, so we shouldn't add them to public API.
Quoted 2 lines
.John
Re: [PATCH bpf-next 4/6] libbpf: add BPF_CORE_READ/BPF_CORE_READ_INTO helpers
In reply to Andrii NakryikoQuoted 48 lines · Andrii Nakryiko
Add few macros simplifying BCC-like multi-level probe reads, while also emitting CO-RE relocations for each read.
Signed-off-by Andrii Nakryiko <[email protected]>---
tools/lib/bpf/bpf_helpers.h| 151 +++++++++++++++++++++++++++++++++++-1 file changed, 147 insertions(+), 4 deletions(-)tools/lib/bpf/bpf_helpers.h +4
-0@@ -19,6 +19,10 @@*/ #define SEC (NAME ) __attribute__((section(NAME), used)) +#ifndef __always_inline +#define __always_inline __attribute__((always_inline)) +#endif +/* helper functions called from eBPF programs written in C */ static void *(*bpf_map_lookup_elem)(void *map, const void *key) =
(void *) BPF_FUNC_map_lookup_elem;tools/lib/bpf/bpf_helpers.h +7
-4@@ -505,7 +509,7 @@ struct pt_regs;#endif /* -* BPF_CORE_READ abstracts away bpf_probe_read() call and captures offset +* bpf_core_read() abstracts away bpf_probe_read() call and captures field * relocation for source address using __builtin_preserve_access_index() * built-in, provided by Clang. * @@ -520,8 +524,147 @@ struct pt_regs;* actual field offset, based on target kernel BTF type that matches original * (local) BTF, used to record relocation. */ -#define BPF_CORE_READ (dst, src) \ - bpf_probe_read((dst), sizeof(*(src)), \ - __builtin_preserve_access_index(src)) +#define bpf_core_read (dst, sz, src) \ + bpf_probe_read(dst, sz, \ + (const void *)__builtin_preserve_access_index(src)) + +/* +* bpf_core_read_str() is a thin wrapper around bpf_probe_read_str()
tools/lib/bpf/bpf_helpers.h struct pt_regs; · line 542
Quoted 10 lines
tools/lib/bpf/bpf_helpers.h +10
-0+* additionally emitting BPF CO-RE field relocation for specified source +* argument. +*/ +#define bpf_core_read_str (dst, sz, src) \ + bpf_probe_read_str(dst, sz, \ + (const void *)__builtin_preserve_access_index(src)) + +#define ___concat (a, b) a ## b +#define ___apply (fn, n) ___concat(fn, n) +#define ___nth (_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, __11,N , ...) N
We are adding many marcos with simple names: ___apply(), ___nth. So I worry they may conflict with macro definitions from other libraries. Shall we hide them in .c files or prefix/postfix them with _libbpf or something?
Thanks,
Song
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Andrii NakryikoQuoted 115 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.Signed-off-by Andrii Nakryiko <[email protected]>---
tools/lib/bpf/Makefile | 4 +- tools/lib/bpf/bpf_endian.h | 72 +++++ tools/lib/bpf/bpf_helpers.h | 527 ++++++++++++++++++++++++++++++++++++ 3 files changed, 602 insertions(+), 1 deletion(-)create mode 100644
tools/lib/bpf/bpf_endian.h
create mode 100644tools/lib/bpf/bpf_helpers.htools/lib/bpf/Makefile +3
-1@@ -240,7 +240,9 @@ install_headers: $(call do_install,libbpf.h,$(prefix)/include/bpf,644); \ $(call do_install,btf.h,$(prefix)/include/bpf,644); \ $(call do_install,libbpf_util.h,$(prefix)/include/bpf,644); \ - $(call do_install,xsk.h,$(prefix)/include/bpf,644); + $(call do_install,xsk.h,$(prefix)/include/bpf,644); \ + $(call do_install,bpf_helpers.h,$(prefix)/include/bpf,644); \ + $(call do_install,bpf_endian.h,$(prefix)/include/bpf,644); install_pkgconfig: $(PC_FILE) $(call QUIET_INSTALL, $(PC_FILE)) \tools/lib/bpf/bpf_endian.h new file +72
-0@@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +#ifndef __BPF_ENDIAN__ +#define __BPF_ENDIAN__ + +#include <linux/stddef.h> +#include <linux/swab.h> + +/* LLVM's BPF target selects the endianness of the CPU +* it compiles on, or the user specifies (bpfel/bpfeb), +* respectively. The used __BYTE_ORDER__ is defined by +* the compiler, we cannot rely on __BYTE_ORDER from +* libc headers, since it doesn't reflect the actual +* requested byte order. +* +* Note, LLVM's BPF target has different __builtin_bswapX() +* semantics. It does map to BPF_ALU | BPF_END | BPF_TO_BE +* in bpfel and bpfeb case, which means below, that we map +* to cpu_to_be16(). We could use it unconditionally in BPF +* case, but better not rely on it, so that this header here +* can be used from application and BPF program side, which +* use different targets. +*/ +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +# define __bpf_ntohs (x) __builtin_bswap16(x) +# define __bpf_htons (x) __builtin_bswap16(x) +# define __bpf_constant_ntohs (x) ___constant_swab16(x) +# define __bpf_constant_htons (x) ___constant_swab16(x) +# define __bpf_ntohl (x) __builtin_bswap32(x) +# define __bpf_htonl (x) __builtin_bswap32(x) +# define __bpf_constant_ntohl (x) ___constant_swab32(x) +# define __bpf_constant_htonl (x) ___constant_swab32(x) +# define __bpf_be64_to_cpu (x) __builtin_bswap64(x) +# define __bpf_cpu_to_be64 (x) __builtin_bswap64(x) +# define __bpf_constant_be64_to_cpu (x) ___constant_swab64(x) +# define __bpf_constant_cpu_to_be64 (x) ___constant_swab64(x) +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +# define __bpf_ntohs (x) (x) +# define __bpf_htons (x) (x) +# define __bpf_constant_ntohs (x) (x) +# define __bpf_constant_htons (x) (x) +# define __bpf_ntohl (x) (x) +# define __bpf_htonl (x) (x) +# define __bpf_constant_ntohl (x) (x) +# define __bpf_constant_htonl (x) (x) +# define __bpf_be64_to_cpu (x) (x) +# define __bpf_cpu_to_be64 (x) (x) +# define __bpf_constant_be64_to_cpu (x) (x) +# define __bpf_constant_cpu_to_be64 (x) (x) +#else +# error "Fix your compiler's __BYTE_ORDER__?!" +#endif + +#define bpf_htons (x) \ + (__builtin_constant_p(x) ? \ + __bpf_constant_htons(x) : __bpf_htons(x)) +#define bpf_ntohs (x) \ + (__builtin_constant_p(x) ? \ + __bpf_constant_ntohs(x) : __bpf_ntohs(x)) +#define bpf_htonl (x) \ + (__builtin_constant_p(x) ? \ + __bpf_constant_htonl(x) : __bpf_htonl(x)) +#define bpf_ntohl (x) \ + (__builtin_constant_p(x) ? \ + __bpf_constant_ntohl(x) : __bpf_ntohl(x)) +#define bpf_cpu_to_be64 (x) \ + (__builtin_constant_p(x) ? \ + __bpf_constant_cpu_to_be64(x) : __bpf_cpu_to_be64(x)) +#define bpf_be64_to_cpu (x) \ + (__builtin_constant_p(x) ? \ + __bpf_constant_be64_to_cpu(x) : __bpf_be64_to_cpu(x)) + +#endif /* __BPF_ENDIAN__ */ tools/lib/bpf/bpf_helpers.h new file +0
-0
tools/lib/bpf/bpf_helpers.h line 6
Quoted 6 lines
tools/lib/bpf/bpf_helpers.h new file +6
-0@@ -0,0 +1,527 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +#ifndef __BPF_HELPERS__ +#define __BPF_HELPERS__ + +#define __uint (name, val) int (*name)[val] +#define __type (name, val) val *name
Similar to the concern with 4/6, maybe we should rename/prefix/postfix these two macros?
Thanks,
Song
Song Liu
Acked-byRe: [PATCH bpf-next 6/6] selftests/bpf: add BPF_CORE_READ and BPF_CORE_READ_STR_INTO macro tests
In reply to John FastabendQuoted 8 lines · John Fastabend
Quoted 3 lines · Andrii Nakryiko
Validate
BPF_CORE_READcorrectness and handling of up to 9 levels of nestedness using cyclic task->(group_leader->)*->tgid chains.
Quoted 6 lines
Quoted 4 lines
Also add a test of maximum-dpeth
BPF_CORE_READ_STR_INTO()macro.Signed-off-by Andrii Nakryiko <[email protected]>---
Acked-by John Fastabend <[email protected]>
Re: [PATCH bpf-next 4/6] libbpf: add BPF_CORE_READ/BPF_CORE_READ_INTO helpers
In reply to Song LiuQuoted 55 lines · Song Liu
Quoted 55 lines · Andrii Nakryiko
Add few macros simplifying BCC-like multi-level probe reads, while also emitting CO-RE relocations for each read.
Signed-off-by Andrii Nakryiko <[email protected]>---
tools/lib/bpf/bpf_helpers.h| 151 +++++++++++++++++++++++++++++++++++-1 file changed, 147 insertions(+), 4 deletions(-)tools/lib/bpf/bpf_helpers.h +4
-0@@ -19,6 +19,10 @@*/ #define SEC (NAME ) __attribute__((section(NAME), used)) +#ifndef __always_inline +#define __always_inline __attribute__((always_inline)) +#endif +/* helper functions called from eBPF programs written in C */ static void *(*bpf_map_lookup_elem)(void *map, const void *key) =
(void *) BPF_FUNC_map_lookup_elem;tools/lib/bpf/bpf_helpers.h +13
-4@@ -505,7 +509,7 @@ struct pt_regs;#endif /* -* BPF_CORE_READ abstracts away bpf_probe_read() call and captures offset +* bpf_core_read() abstracts away bpf_probe_read() call and captures field * relocation for source address using __builtin_preserve_access_index() * built-in, provided by Clang. * @@ -520,8 +524,147 @@ struct pt_regs;* actual field offset, based on target kernel BTF type that matches original * (local) BTF, used to record relocation. */ -#define BPF_CORE_READ (dst, src) \ - bpf_probe_read((dst), sizeof(*(src)), \ - __builtin_preserve_access_index(src)) +#define bpf_core_read (dst, sz, src) \ + bpf_probe_read(dst, sz, \ + (const void *)__builtin_preserve_access_index(src)) + +/* +* bpf_core_read_str() is a thin wrapper around bpf_probe_read_str() +* additionally emitting BPF CO-RE field relocation for specified source +* argument. +*/ +#define bpf_core_read_str (dst, sz, src) \ + bpf_probe_read_str(dst, sz, \ + (const void *)__builtin_preserve_access_index(src))
Quoted 8 lines
tools/lib/bpf/bpf_helpers.h struct pt_regs; · line 542
Quoted 4 lines
tools/lib/bpf/bpf_helpers.h +4
-0+ +#define ___concat (a, b) a ## b +#define ___apply (fn, n) ___concat(fn, n) +#define ___nth (_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, __11,N , ...) NWe are adding many marcos with simple names:
___apply(),___nth. So I worry they may conflict with macro definitions from other libraries. Shall we hide them in .c files or prefix/postfix them with_libbpfor something?
Keep in mind, this is the header that's included from BPF code.
They are prefixed with three underscores, I was hoping it's good enough to avoid accidental conflicts. It's unlikely someone will have macros with the same names **in BPF-side code**.
Prefixing with _libbpf is an option, but it will make it super ugly and hard to follow (I've spent a bunch of time to even get it to the current state), so I'd like to avoid that.
Quoted 4 lines
Thanks,
Song
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Song LiuQuoted 17 lines · Song Liu
Quoted 17 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.Signed-off-by Andrii Nakryiko <[email protected]>---
tools/lib/bpf/Makefile | 4 +- tools/lib/bpf/bpf_endian.h | 72 +++++ tools/lib/bpf/bpf_helpers.h | 527 ++++++++++++++++++++++++++++++++++++ 3 files changed, 602 insertions(+), 1 deletion(-)create mode 100644
tools/lib/bpf/bpf_endian.h
create mode 100644tools/lib/bpf/bpf_helpers.h
[...]
Quoted 8 lines
Quoted 8 lines
tools/lib/bpf/bpf_endian.h new file +1
-0+#endif /* __BPF_ENDIAN__ */ tools/lib/bpf/bpf_helpers.h new file +1
-0@@ -0,0 +1,527 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
Quoted 8 lines
tools/lib/bpf/bpf_helpers.h line 6
Quoted 5 lines
tools/lib/bpf/bpf_helpers.h new file +5
-0+#ifndef __BPF_HELPERS__ +#define __BPF_HELPERS__ + +#define __uint (name, val) int (*name)[val] +#define __type (name, val) val *nameSimilar to the concern with 4/6, maybe we should rename/prefix/postfix these two macros?
Those were specifically named so they are as clear and clean in user code as possible, it was an explicit goal. Ideally they would be "uint" and "type", but that's pushing it too far :)
Quoted 4 lines
Thanks,
Song
Re: [PATCH bpf-next 5/6] selftests/bpf: adjust CO-RE reloc tests for new BPF_CORE_READ macro
In reply to John FastabendQuoted 22 lines · John Fastabend
Quoted 7 lines · Andrii Nakryiko
Given introduction of variadic
BPF_CORE_READwith slightly different syntax and semantics, defineCORE_READ, which is a thin wrapper around low-levelbpf_core_read()macro, which in turn is just a wrapper aroundbpf_probe_read().BPF_CORE_READis higher-level variadic macro supporting multi-pointer reads and are tested separately.Signed-off-by Andrii Nakryiko <[email protected]>Quoted 12 lines
--- .../bpf/progs/test_core_reloc_arrays.c | 10 ++++++---- .../bpf/progs/test_core_reloc_flavors.c | 8 +++++--- .../selftests/bpf/progs/test_core_reloc_ints.c | 18 ++++++++++-------- .../bpf/progs/test_core_reloc_kernel.c | 6 ++++-- .../selftests/bpf/progs/test_core_reloc_misc.c | 8 +++++--- .../selftests/bpf/progs/test_core_reloc_mods.c | 18 ++++++++++-------- .../bpf/progs/test_core_reloc_nesting.c | 6 ++++-- .../bpf/progs/test_core_reloc_primitives.c | 12 +++++++----- .../bpf/progs/test_core_reloc_ptr_as_arr.c | 4 +++- 9 files changed, 54 insertions(+), 36 deletions(-)
Quoted 1 line
Starting to get many layers of macros here but makes sense here.
Yeah, a bit. I was considering to either switch to bpf_core_read() with explicit sizeof or making bpf_core_read() deriving sizeof(), but didn't because:
1. wanted to keep bpf_core_read() a direct "substitute" for bpf_probe_read()
2. figured one copy-pasted #define for each of few files is small enough price for much more readable tests
Quoted 2 lines
Acked-by John Fastabend <[email protected]>
Thanks for review!
Re: [PATCH bpf-next 4/6] libbpf: add BPF_CORE_READ/BPF_CORE_READ_INTO helpers
In reply to Andrii NakryikoQuoted 69 lines · Andrii Nakryiko
Quoted 55 lines · Song Liu
Quoted 55 lines · Andrii Nakryiko
Add few macros simplifying BCC-like multi-level probe reads, while also emitting CO-RE relocations for each read.
Signed-off-by Andrii Nakryiko <[email protected]>---
tools/lib/bpf/bpf_helpers.h| 151 +++++++++++++++++++++++++++++++++++-1 file changed, 147 insertions(+), 4 deletions(-)tools/lib/bpf/bpf_helpers.h +4
-0@@ -19,6 +19,10 @@*/ #define SEC (NAME ) __attribute__((section(NAME), used)) +#ifndef __always_inline +#define __always_inline __attribute__((always_inline)) +#endif +/* helper functions called from eBPF programs written in C */ static void *(*bpf_map_lookup_elem)(void *map, const void *key) =
(void *) BPF_FUNC_map_lookup_elem;tools/lib/bpf/bpf_helpers.h +13
-4@@ -505,7 +509,7 @@ struct pt_regs;#endif /* -* BPF_CORE_READ abstracts away bpf_probe_read() call and captures offset +* bpf_core_read() abstracts away bpf_probe_read() call and captures field * relocation for source address using __builtin_preserve_access_index() * built-in, provided by Clang. * @@ -520,8 +524,147 @@ struct pt_regs;* actual field offset, based on target kernel BTF type that matches original * (local) BTF, used to record relocation. */ -#define BPF_CORE_READ (dst, src) \ - bpf_probe_read((dst), sizeof(*(src)), \ - __builtin_preserve_access_index(src)) +#define bpf_core_read (dst, sz, src) \ + bpf_probe_read(dst, sz, \ + (const void *)__builtin_preserve_access_index(src)) + +/* +* bpf_core_read_str() is a thin wrapper around bpf_probe_read_str() +* additionally emitting BPF CO-RE field relocation for specified source +* argument. +*/ +#define bpf_core_read_str (dst, sz, src) \ + bpf_probe_read_str(dst, sz, \ + (const void *)__builtin_preserve_access_index(src))Quoted 8 lines
tools/lib/bpf/bpf_helpers.h struct pt_regs; · line 542
Quoted 4 lines
tools/lib/bpf/bpf_helpers.h +4
-0+ +#define ___concat (a, b) a ## b +#define ___apply (fn, n) ___concat(fn, n) +#define ___nth (_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, __11,N , ...) NWe are adding many marcos with simple names:
___apply(),___nth. So I worry they may conflict with macro definitions from other libraries. Shall we hide them in .c files or prefix/postfix them with_libbpfor something?
Quoted 5 lines
Keep in mind, this is the header that's included from BPF code.
They are prefixed with three underscores, I was hoping it's good enough to avoid accidental conflicts. It's unlikely someone will have macros with the same names **in BPF-side code**.
BPF side code would include kernel headers. So there are many headers to conflict with. And we won't know until somebody want to trace certain kernel structure.
Quoted 3 lines
Prefixing with
_libbpfis an option, but it will make it super ugly and hard to follow (I've spent a bunch of time to even get it to the current state), so I'd like to avoid that.
BPF programs will not use these marcos directly, so I feel it is OK to pay the pain of _libbpf prefix, as it is contained within this file.
Thanks,
Song
Re: [PATCH bpf-next 5/6] selftests/bpf: adjust CO-RE reloc tests for new BPF_CORE_READ macro
In reply to Andrii NakryikoQuoted 12 lines · Andrii Nakryiko
Given introduction of variadic
BPF_CORE_READwith slightly different syntax and semantics, defineCORE_READ, which is a thin wrapper around low-levelbpf_core_read()macro, which in turn is just a wrapper aroundbpf_probe_read().BPF_CORE_READis higher-level variadic macro supporting multi-pointer reads and are tested separately.Signed-off-by Andrii Nakryiko <[email protected]>
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Andrii NakryikoQuoted 24 lines · Andrii Nakryiko
Quoted 16 lines · John Fastabend
Quoted 14 lines · Toke Høiland-Jørgensen
tools/lib/bpf/bpf_helpers.h line 257
Quoted 10 lines
tools/lib/bpf/bpf_helpers.h new file +9
-0+struct bpf_map_def { +unsigned int type; +unsigned int key_size; +unsigned int value_size; +unsigned int max_entries; +unsigned int map_flags; +unsigned int inner_map_idx; +unsigned int numa_node; +};Didn't we agree on no new
bpf_map_defABI in libbpf, and that all additions should be BTF-based?Quoted 6 lines
Quoted 2 lines
-Toke
We use libbpf on pre BTF kernels so in this case I think it makes sense to add these fields. Having
inner_map_idxthere should allow us to remove some other things we have sitting around.
Quoted 5 lines
We had a discussion about supporting non-BTF and non-standard BPF map definition before and it's still on my TODO list to go and do a proof of concept how that can look like and what libbpf changes we need to make. Right now libbpf doesn't support those new fields anyway, so we shouldn't add them to public API.
I'm carrying around a patch for perf_event_open_probe() to get it working without BTF I'll send that out today/tomorrow. It seems enough to get all the basic prog load, maps reused/pinned, etc at least for my use case.
Quoted 3 lines
Quoted 3 lines
.John
Re: [PATCH bpf-next 4/6] libbpf: add BPF_CORE_READ/BPF_CORE_READ_INTO helpers
In reply to Song LiuQuoted 72 lines · Song Liu
Quoted 72 lines · Andrii Nakryiko
Quoted 55 lines · Song Liu
Quoted 55 lines · Andrii Nakryiko
Add few macros simplifying BCC-like multi-level probe reads, while also emitting CO-RE relocations for each read.
Signed-off-by Andrii Nakryiko <[email protected]>---
tools/lib/bpf/bpf_helpers.h| 151 +++++++++++++++++++++++++++++++++++-1 file changed, 147 insertions(+), 4 deletions(-)tools/lib/bpf/bpf_helpers.h +4
-0@@ -19,6 +19,10 @@*/ #define SEC (NAME ) __attribute__((section(NAME), used)) +#ifndef __always_inline +#define __always_inline __attribute__((always_inline)) +#endif +/* helper functions called from eBPF programs written in C */ static void *(*bpf_map_lookup_elem)(void *map, const void *key) =
(void *) BPF_FUNC_map_lookup_elem;tools/lib/bpf/bpf_helpers.h +13
-4@@ -505,7 +509,7 @@ struct pt_regs;#endif /* -* BPF_CORE_READ abstracts away bpf_probe_read() call and captures offset +* bpf_core_read() abstracts away bpf_probe_read() call and captures field * relocation for source address using __builtin_preserve_access_index() * built-in, provided by Clang. * @@ -520,8 +524,147 @@ struct pt_regs;* actual field offset, based on target kernel BTF type that matches original * (local) BTF, used to record relocation. */ -#define BPF_CORE_READ (dst, src) \ - bpf_probe_read((dst), sizeof(*(src)), \ - __builtin_preserve_access_index(src)) +#define bpf_core_read (dst, sz, src) \ + bpf_probe_read(dst, sz, \ + (const void *)__builtin_preserve_access_index(src)) + +/* +* bpf_core_read_str() is a thin wrapper around bpf_probe_read_str() +* additionally emitting BPF CO-RE field relocation for specified source +* argument. +*/ +#define bpf_core_read_str (dst, sz, src) \ + bpf_probe_read_str(dst, sz, \ + (const void *)__builtin_preserve_access_index(src))Quoted 8 lines
tools/lib/bpf/bpf_helpers.h struct pt_regs; · line 542
Quoted 4 lines
tools/lib/bpf/bpf_helpers.h +4
-0+ +#define ___concat (a, b) a ## b +#define ___apply (fn, n) ___concat(fn, n) +#define ___nth (_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, __11,N , ...) NWe are adding many marcos with simple names:
___apply(),___nth. So I worry they may conflict with macro definitions from other libraries. Shall we hide them in .c files or prefix/postfix them with_libbpfor something?Keep in mind, this is the header that's included from BPF code.
Quoted 7 lines
Quoted 3 lines
They are prefixed with three underscores, I was hoping it's good enough to avoid accidental conflicts. It's unlikely someone will have macros with the same names **in BPF-side code**.
BPF side code would include kernel headers. So there are many headers to conflict with. And we won't know until somebody want to trace certain kernel structure.
We have all the kernel sources at our disposal, there's no need to guess :) There is no instance of ___apply, ___concat, ___nth, ___arrow, ___last, ___nolast, or ___type, not even speaking about other more specific names. There are currently two instances of "____last_____" used in a string. And I'm certainly not afraid that user code can use triple-underscored identifier with exact those names and complain about bpf_helpers.h :)
Quoted 7 lines
Quoted 4 lines
Prefixing with
_libbpfis an option, but it will make it super ugly and hard to follow (I've spent a bunch of time to even get it to the current state), so I'd like to avoid that.BPF programs will not use these marcos directly, so I feel it is OK to pay the pain of
_libbpfprefix, as it is contained within this file.
I disagree, having more convoluted multi-line macros will eventually lead to a stupid mistake or typo, which could have been spotted otherwise. Plus, if user screws up (which is inevitable anyway) usage of these macro, he will be presented with long and incomprehensible chain of macro with more obscure names than necessary. If you think that internal names don't matter, ask anyone who had to decipher compilation errors involving C++ standard library templates. I'd be OK with that if there was a real risk of name conflict, but I disagree with the premise.
Quoted 6 lines
Thanks,
Song
Daniel Borkmann
maintainerRe: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Andrii NakryikoQuoted 4 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.Signed-off-by Andrii Nakryiko <[email protected]>
[...]
Quoted 65 lines
new file mode 100644
index 000000000000..fbe28008450ftools/lib/bpf/bpf_endian.h new file +60
-0@@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +#ifndef __BPF_ENDIAN__ +#define __BPF_ENDIAN__ + +#include <linux/stddef.h> +#include <linux/swab.h> + +/* LLVM's BPF target selects the endianness of the CPU +* it compiles on, or the user specifies (bpfel/bpfeb), +* respectively. The used __BYTE_ORDER__ is defined by +* the compiler, we cannot rely on __BYTE_ORDER from +* libc headers, since it doesn't reflect the actual +* requested byte order. +* +* Note, LLVM's BPF target has different __builtin_bswapX() +* semantics. It does map to BPF_ALU | BPF_END | BPF_TO_BE +* in bpfel and bpfeb case, which means below, that we map +* to cpu_to_be16(). We could use it unconditionally in BPF +* case, but better not rely on it, so that this header here +* can be used from application and BPF program side, which +* use different targets. +*/ +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +# define __bpf_ntohs (x) __builtin_bswap16(x) +# define __bpf_htons (x) __builtin_bswap16(x) +# define __bpf_constant_ntohs (x) ___constant_swab16(x) +# define __bpf_constant_htons (x) ___constant_swab16(x) +# define __bpf_ntohl (x) __builtin_bswap32(x) +# define __bpf_htonl (x) __builtin_bswap32(x) +# define __bpf_constant_ntohl (x) ___constant_swab32(x) +# define __bpf_constant_htonl (x) ___constant_swab32(x) +# define __bpf_be64_to_cpu (x) __builtin_bswap64(x) +# define __bpf_cpu_to_be64 (x) __builtin_bswap64(x) +# define __bpf_constant_be64_to_cpu (x) ___constant_swab64(x) +# define __bpf_constant_cpu_to_be64 (x) ___constant_swab64(x) +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +# define __bpf_ntohs (x) (x) +# define __bpf_htons (x) (x) +# define __bpf_constant_ntohs (x) (x) +# define __bpf_constant_htons (x) (x) +# define __bpf_ntohl (x) (x) +# define __bpf_htonl (x) (x) +# define __bpf_constant_ntohl (x) (x) +# define __bpf_constant_htonl (x) (x) +# define __bpf_be64_to_cpu (x) (x) +# define __bpf_cpu_to_be64 (x) (x) +# define __bpf_constant_be64_to_cpu (x) (x) +# define __bpf_constant_cpu_to_be64 (x) (x) +#else +# error "Fix your compiler's __BYTE_ORDER__?!" +#endif + +#define bpf_htons (x) \ + (__builtin_constant_p(x) ? \ + __bpf_constant_htons(x) : __bpf_htons(x)) +#define bpf_ntohs (x) \ + (__builtin_constant_p(x) ? \ + __bpf_constant_ntohs(x) : __bpf_ntohs(x)) +#define bpf_htonl (x) \ + (__builtin_constant_p(x) ? \
tools/lib/bpf/bpf_endian.h line 70
Quoted 10 lines
tools/lib/bpf/bpf_endian.h new file +10
-0+ __bpf_constant_htonl(x) : __bpf_htonl(x)) +#define bpf_ntohl (x) \ + (__builtin_constant_p(x) ? \ + __bpf_constant_ntohl(x) : __bpf_ntohl(x)) +#define bpf_cpu_to_be64 (x) \ + (__builtin_constant_p(x) ? \ + __bpf_constant_cpu_to_be64(x) : __bpf_cpu_to_be64(x)) +#define bpf_be64_to_cpu (x) \ + (__builtin_constant_p(x) ? \ + __bpf_constant_be64_to_cpu(x) : __bpf_be64_to_cpu(x))
Nit: if we move this into a public API for libbpf, we should probably also define for sake of consistency a bpf_cpu_to_be{64,32,16}() and bpf_be{64,32,16}_to_cpu() and have the latter two point to the existing bpf_hton{l,s}() and bpf_ntoh{l,s}() macros.
Quoted 12 lines
tools/lib/bpf/bpf_endian.h new file +1
-0+#endif /* __BPF_ENDIAN__ */ tools/lib/bpf/bpf_helpers.h new file +4
-0@@ -0,0 +1,527 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +#ifndef __BPF_HELPERS__ +#define __BPF_HELPERS__ +
tools/lib/bpf/bpf_helpers.h line 14
Quoted 10 lines
tools/lib/bpf/bpf_helpers.h new file +10
-0+#define __uint (name, val) int (*name)[val] +#define __type (name, val) val *name + +/* helper macro to print out debug messages */ +#define bpf_printk (fmt, ...) \ +({ \ + char ____fmt[] = fmt; \ + bpf_trace_printk(____fmt, sizeof(____fmt), \ + ##__VA_ARGS__); \ +})
Did you double check if by now via .rodata we can have the fmt as const char * and add __attribute__ ((format (printf, 1, 2))) to it? If that works we should avoid having to copy the string as above in the API.
tools/lib/bpf/bpf_helpers.h line 22
Quoted 7 lines
tools/lib/bpf/bpf_helpers.h new file +7
-0+/* helper macro to place programs, maps, license in +* different sections in elf_bpf file. Section names +* are interpreted by elf_bpf loader +*/ +#define SEC (NAME ) __attribute__((section(NAME), used)) + +/* helper functions called from eBPF programs written in C */
As mentioned earlier, the whole helper function description below should get a big cleanup in here when moved into libbpf API.
Quoted 4 lines
tools/lib/bpf/bpf_helpers.h new file +4
-0+static void *(*bpf_map_lookup_elem)(void *map,const void *key) = + (void *) BPF_FUNC_map_lookup_elem; +static int (*bpf_map_update_elem)(void *map,const void *key,const void *value, + unsigned long long flags) =
[...]
tools/lib/bpf/bpf_helpers.h line 244
Quoted 11 lines
tools/lib/bpf/bpf_helpers.h new file +11
-0+ +/* llvm builtin functions that eBPF C program may use to +* emit BPF_LD_ABS and BPF_LD_IND instructions +*/ +struct sk_buff ; +unsigned long long load_byte (void *skb, +unsigned long long off) asm("llvm.bpf.load.byte" ); +unsigned long long load_half (void *skb, +unsigned long long off) asm("llvm.bpf.load.half" ); +unsigned long long load_word (void *skb, +unsigned long long off) asm("llvm.bpf.load.word" );
These should be removed from the public API, no point in adding legacy/ deprecated stuff in there.
Quoted 11 lines
tools/lib/bpf/bpf_helpers.h new file +11
-0+/* a helper structure used by eBPF C program +* to describe map attributes to elf_bpf loader +*/ +struct bpf_map_def { +unsigned int type; +unsigned int key_size; +unsigned int value_size; +unsigned int max_entries; +unsigned int map_flags; +unsigned int inner_map_idx; +unsigned int numa_node;
tools/lib/bpf/bpf_helpers.h line 266
Quoted 10 lines
tools/lib/bpf/bpf_helpers.h new file +10
-0+}; + +#define BPF_ANNOTATE_KV_PAIR (name, type_key, type_val) \ + struct ____btf_map_##name { \ + type_key key; \ + type_val value; \ + }; \ + struct ____btf_map_##name \ + __attribute__ ((section(".maps." #name), used)) \ + ____btf_map_##name = { }
Same here.
tools/lib/bpf/bpf_helpers.h line 269
Quoted 2 lines
tools/lib/bpf/bpf_helpers.h new file +2
-0+static int (*bpf_skb_load_bytes)(void *ctx,int off,void *to,int len) = + (void *) BPF_FUNC_skb_load_bytes;
[...]
Given we already move bpf_endian.h to a separate header, I'd do the same for all tracing specifics as well, e.g. bpf_tracing.h.
Quoted 214 lines
tools/lib/bpf/bpf_helpers.h new file +211
-0+/* Scan the ARCH passed in from ARCH env variable (see Makefile) */ +#if defined(__TARGET_ARCH_x86) +#define bpf_target_x86 +#define bpf_target_defined +#elif defined(__TARGET_ARCH_s390) +#define bpf_target_s390 +#define bpf_target_defined +#elif defined(__TARGET_ARCH_arm) +#define bpf_target_arm +#define bpf_target_defined +#elif defined(__TARGET_ARCH_arm64) +#define bpf_target_arm64 +#define bpf_target_defined +#elif defined(__TARGET_ARCH_mips) +#define bpf_target_mips +#define bpf_target_defined +#elif defined(__TARGET_ARCH_powerpc) +#define bpf_target_powerpc +#define bpf_target_defined +#elif defined(__TARGET_ARCH_sparc) +#define bpf_target_sparc +#define bpf_target_defined +#else +#undef bpf_target_defined +#endif + +/* Fall back to what the compiler says */ +#ifndef bpf_target_defined +#if defined(__x86_64__) +#define bpf_target_x86 +#elif defined(__s390__) +#define bpf_target_s390 +#elif defined(__arm__) +#define bpf_target_arm +#elif defined(__aarch64__) +#define bpf_target_arm64 +#elif defined(__mips__) +#define bpf_target_mips +#elif defined(__powerpc__) +#define bpf_target_powerpc +#elif defined(__sparc__) +#define bpf_target_sparc +#endif +#endif + +#if defined(bpf_target_x86) + +#ifdef __KERNEL__ +#define PT_REGS_PARM1 (x) ((x)->di) +#define PT_REGS_PARM2 (x) ((x)->si) +#define PT_REGS_PARM3 (x) ((x)->dx) +#define PT_REGS_PARM4 (x) ((x)->cx) +#define PT_REGS_PARM5 (x) ((x)->r8) +#define PT_REGS_RET (x) ((x)->sp) +#define PT_REGS_FP (x) ((x)->bp) +#define PT_REGS_RC (x) ((x)->ax) +#define PT_REGS_SP (x) ((x)->sp) +#define PT_REGS_IP (x) ((x)->ip) +#else +#ifdef __i386__ +/* i386 kernel is built with -mregparm=3 */ +#define PT_REGS_PARM1 (x) ((x)->eax) +#define PT_REGS_PARM2 (x) ((x)->edx) +#define PT_REGS_PARM3 (x) ((x)->ecx) +#define PT_REGS_PARM4 (x) 0 +#define PT_REGS_PARM5 (x) 0 +#define PT_REGS_RET (x) ((x)->esp) +#define PT_REGS_FP (x) ((x)->ebp) +#define PT_REGS_RC (x) ((x)->eax) +#define PT_REGS_SP (x) ((x)->esp) +#define PT_REGS_IP (x) ((x)->eip) +#else +#define PT_REGS_PARM1 (x) ((x)->rdi) +#define PT_REGS_PARM2 (x) ((x)->rsi) +#define PT_REGS_PARM3 (x) ((x)->rdx) +#define PT_REGS_PARM4 (x) ((x)->rcx) +#define PT_REGS_PARM5 (x) ((x)->r8) +#define PT_REGS_RET (x) ((x)->rsp) +#define PT_REGS_FP (x) ((x)->rbp) +#define PT_REGS_RC (x) ((x)->rax) +#define PT_REGS_SP (x) ((x)->rsp) +#define PT_REGS_IP (x) ((x)->rip) +#endif +#endif + +#elif defined(bpf_target_s390) + +/* s390 provides user_pt_regs instead of struct pt_regs to userspace */ +struct pt_regs ; +#define PT_REGS_S390 const volatile user_pt_regs +#define PT_REGS_PARM1 (x) (((PT_REGS_S390 *)(x))->gprs[2]) +#define PT_REGS_PARM2 (x) (((PT_REGS_S390 *)(x))->gprs[3]) +#define PT_REGS_PARM3 (x) (((PT_REGS_S390 *)(x))->gprs[4]) +#define PT_REGS_PARM4 (x) (((PT_REGS_S390 *)(x))->gprs[5]) +#define PT_REGS_PARM5 (x) (((PT_REGS_S390 *)(x))->gprs[6]) +#define PT_REGS_RET (x) (((PT_REGS_S390 *)(x))->gprs[14]) +/* Works only with CONFIG_FRAME_POINTER */ +#define PT_REGS_FP (x) (((PT_REGS_S390 *)(x))->gprs[11]) +#define PT_REGS_RC (x) (((PT_REGS_S390 *)(x))->gprs[2]) +#define PT_REGS_SP (x) (((PT_REGS_S390 *)(x))->gprs[15]) +#define PT_REGS_IP (x) (((PT_REGS_S390 *)(x))->psw.addr) + +#elif defined(bpf_target_arm) + +#define PT_REGS_PARM1 (x) ((x)->uregs[0]) +#define PT_REGS_PARM2 (x) ((x)->uregs[1]) +#define PT_REGS_PARM3 (x) ((x)->uregs[2]) +#define PT_REGS_PARM4 (x) ((x)->uregs[3]) +#define PT_REGS_PARM5 (x) ((x)->uregs[4]) +#define PT_REGS_RET (x) ((x)->uregs[14]) +#define PT_REGS_FP (x) ((x)->uregs[11])/* Works only with CONFIG_FRAME_POINTER */ +#define PT_REGS_RC (x) ((x)->uregs[0]) +#define PT_REGS_SP (x) ((x)->uregs[13]) +#define PT_REGS_IP (x) ((x)->uregs[12]) + +#elif defined(bpf_target_arm64) + +/* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */ +struct pt_regs ; +#define PT_REGS_ARM64 const volatile struct user_pt_regs +#define PT_REGS_PARM1 (x) (((PT_REGS_ARM64 *)(x))->regs[0]) +#define PT_REGS_PARM2 (x) (((PT_REGS_ARM64 *)(x))->regs[1]) +#define PT_REGS_PARM3 (x) (((PT_REGS_ARM64 *)(x))->regs[2]) +#define PT_REGS_PARM4 (x) (((PT_REGS_ARM64 *)(x))->regs[3]) +#define PT_REGS_PARM5 (x) (((PT_REGS_ARM64 *)(x))->regs[4]) +#define PT_REGS_RET (x) (((PT_REGS_ARM64 *)(x))->regs[30]) +/* Works only with CONFIG_FRAME_POINTER */ +#define PT_REGS_FP (x) (((PT_REGS_ARM64 *)(x))->regs[29]) +#define PT_REGS_RC (x) (((PT_REGS_ARM64 *)(x))->regs[0]) +#define PT_REGS_SP (x) (((PT_REGS_ARM64 *)(x))->sp) +#define PT_REGS_IP (x) (((PT_REGS_ARM64 *)(x))->pc) + +#elif defined(bpf_target_mips) + +#define PT_REGS_PARM1 (x) ((x)->regs[4]) +#define PT_REGS_PARM2 (x) ((x)->regs[5]) +#define PT_REGS_PARM3 (x) ((x)->regs[6]) +#define PT_REGS_PARM4 (x) ((x)->regs[7]) +#define PT_REGS_PARM5 (x) ((x)->regs[8]) +#define PT_REGS_RET (x) ((x)->regs[31]) +#define PT_REGS_FP (x) ((x)->regs[30])/* Works only with CONFIG_FRAME_POINTER */ +#define PT_REGS_RC (x) ((x)->regs[1]) +#define PT_REGS_SP (x) ((x)->regs[29]) +#define PT_REGS_IP (x) ((x)->cp0_epc) + +#elif defined(bpf_target_powerpc) + +#define PT_REGS_PARM1 (x) ((x)->gpr[3]) +#define PT_REGS_PARM2 (x) ((x)->gpr[4]) +#define PT_REGS_PARM3 (x) ((x)->gpr[5]) +#define PT_REGS_PARM4 (x) ((x)->gpr[6]) +#define PT_REGS_PARM5 (x) ((x)->gpr[7]) +#define PT_REGS_RC (x) ((x)->gpr[3]) +#define PT_REGS_SP (x) ((x)->sp) +#define PT_REGS_IP (x) ((x)->nip) + +#elif defined(bpf_target_sparc) + +#define PT_REGS_PARM1 (x) ((x)->u_regs[UREG_I0]) +#define PT_REGS_PARM2 (x) ((x)->u_regs[UREG_I1]) +#define PT_REGS_PARM3 (x) ((x)->u_regs[UREG_I2]) +#define PT_REGS_PARM4 (x) ((x)->u_regs[UREG_I3]) +#define PT_REGS_PARM5 (x) ((x)->u_regs[UREG_I4]) +#define PT_REGS_RET (x) ((x)->u_regs[UREG_I7]) +#define PT_REGS_RC (x) ((x)->u_regs[UREG_I0]) +#define PT_REGS_SP (x) ((x)->u_regs[UREG_FP]) + +/* Should this also be a bpf_target check for the sparc case? */ +#if defined(__arch64__) +#define PT_REGS_IP (x) ((x)->tpc) +#else +#define PT_REGS_IP (x) ((x)->pc) +#endif + +#endif + +#if defined(bpf_target_powerpc) +#define BPF_KPROBE_READ_RET_IP (ip, ctx) ({ (ip) = (ctx)->link; }) +#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP +#elif defined(bpf_target_sparc) +#define BPF_KPROBE_READ_RET_IP (ip, ctx) ({ (ip) = PT_REGS_RET(ctx); }) +#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP +#else +#define BPF_KPROBE_READ_RET_IP (ip, ctx) ({ \ + bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); }) +#define BPF_KRETPROBE_READ_RET_IP (ip, ctx) ({ \ + bpf_probe_read(&(ip), sizeof(ip), \ + (void *)(PT_REGS_FP(ctx) + sizeof(ip))); }) +#endif + +/* +* BPF_CORE_READ abstracts away bpf_probe_read() call and captures offset +* relocation for source address using __builtin_preserve_access_index() +* built-in, provided by Clang. +* +* __builtin_preserve_access_index() takes as an argument an expression of +* taking an address of a field within struct/union. It makes compiler emit +* a relocation, which records BTF type ID describing root struct/union and an +* accessor string which describes exact embedded field that was used to take +* an address. See detailed description of this relocation format and +* semantics in comments to struct bpf_offset_reloc in libbpf_internal.h. +* +* This relocation allows libbpf to adjust BPF instruction to use correct +* actual field offset, based on target kernel BTF type that matches original +* (local) BTF, used to record relocation. +*/ +#define BPF_CORE_READ (dst, src) \ + bpf_probe_read((dst), sizeof(*(src)), \ + __builtin_preserve_access_index(src)) + +#endif Signature · 1 line
2.17.1
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
Quoted 17 lines · Daniel Borkmann
Quoted 4 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.Signed-off-by Andrii Nakryiko <[email protected]>[...]
Quoted 9 lines
new file mode 100644
index 000000000000..fbe28008450ftools/lib/bpf/bpf_endian.h new file +4
-0@@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +#ifndef __BPF_ENDIAN__ +#define __BPF_ENDIAN__ +
[...]
Quoted 11 lines
tools/lib/bpf/bpf_endian.h line 70
Quoted 6 lines
tools/lib/bpf/bpf_endian.h new file +6
-0+#define bpf_cpu_to_be64 (x) \ + (__builtin_constant_p(x) ? \ + __bpf_constant_cpu_to_be64(x) : __bpf_cpu_to_be64(x)) +#define bpf_be64_to_cpu (x) \ + (__builtin_constant_p(x) ? \ + __bpf_constant_be64_to_cpu(x) : __bpf_be64_to_cpu(x))Nit: if we move this into a public API for libbpf, we should probably also define for sake of consistency a bpf_cpu_to_be{64,32,16}() and bpf_be{64,32,16}_to_cpu() and have the latter two point to the existing bpf_hton{l,s}() and bpf_ntoh{l,s}() macros.
I think these deserve better tests before we add more stuff, both from BPF-side and userland-side (as they are supposed to be includeable from both, right)? I'm hesitant adding new unfamiliar macro/builtins without tests, but I don't want to get distracted with that right now, especially this patch set already becomes bigger than I'd hope.
Given we are talking about adding new stuff which is not breaking change, we can add it later after we move this as is, agree?
Quoted 20 lines
Quoted 20 lines
tools/lib/bpf/bpf_endian.h new file +1
-0+#endif /* __BPF_ENDIAN__ */ tools/lib/bpf/bpf_helpers.h new file +11
-0@@ -0,0 +1,527 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +#ifndef __BPF_HELPERS__ +#define __BPF_HELPERS__ + +#define __uint (name, val) int (*name)[val] +#define __type (name, val) val *name + +/* helper macro to print out debug messages */ +#define bpf_printk (fmt, ...) \ +({ \ + char ____fmt[] = fmt; \
Quoted 8 lines
tools/lib/bpf/bpf_helpers.h line 14
Quoted 3 lines
tools/lib/bpf/bpf_helpers.h new file +3
-0+bpf_trace_printk (____fmt,sizeof (____fmt), \ + ##__VA_ARGS__); \ +})Did you double check if by now via .rodata we can have the fmt as const char * and add
__attribute__((format (printf, 1, 2))) to it? If that works we should avoid having to copy the string as above in the API.
This doesn't work still, unfortunately. Eventually, though, we'll be able to essentially nop it out with direct call to bpf_trace_printk, so I'm not concerned about future API burden.
Quoted 11 lines
tools/lib/bpf/bpf_helpers.h line 22
Quoted 8 lines
tools/lib/bpf/bpf_helpers.h new file +7
-0+/* helper macro to place programs, maps, license in +* different sections in elf_bpf file. Section names +* are interpreted by elf_bpf loader +*/ +#define SEC (NAME ) __attribute__((section(NAME), used)) + +/* helper functions called from eBPF programs written in C */ As mentioned earlier, the whole helper function description below should get a big cleanup in here when moved into libbpf API.
Right, I just recalled that today, sorry I didn't do it for this version.
There were two things you mentioned on that thread that you wanted to clean up:
1. using __u32 instead int and stuff like that
2. using macro to hide some of the ugliness of (void *) = BPF_FUNC_xxx
So with 1) I'm concerned that we can't just assume that __u32 is always going to be defined. Also we need bpf_helpers.h to be usable both with including system headers, as well as auto-generated vmlinux.h. In first case, I don't think we can assume that typedef is always defined, in latter case we can't really define it on our own. So I think we should just keep it as int, unsigned long long, etc. Thoughts?
For 2), I'm doing that right now, but it's not that much cleaner, let's see.
Am I missing something else?
Quoted 6 lines
Quoted 5 lines
tools/lib/bpf/bpf_helpers.h new file +4
-0+static void *(*bpf_map_lookup_elem)(void *map,const void *key) = + (void *) BPF_FUNC_map_lookup_elem; +static int (*bpf_map_update_elem)(void *map,const void *key,const void *value, + unsigned long long flags) =[...]
Quoted 14 lines
tools/lib/bpf/bpf_helpers.h line 244
Quoted 11 lines
tools/lib/bpf/bpf_helpers.h new file +11
-0+ +/* llvm builtin functions that eBPF C program may use to +* emit BPF_LD_ABS and BPF_LD_IND instructions +*/ +struct sk_buff ; +unsigned long long load_byte (void *skb, +unsigned long long off) asm("llvm.bpf.load.byte" ); +unsigned long long load_half (void *skb, +unsigned long long off) asm("llvm.bpf.load.half" ); +unsigned long long load_word (void *skb, +unsigned long long off) asm("llvm.bpf.load.word" );These should be removed from the public API, no point in adding legacy/ deprecated stuff in there.
Oh, cool, never knew what that stuff is anyway :)
Quoted 16 lines
Quoted 16 lines
tools/lib/bpf/bpf_helpers.h new file +15
-0+/* a helper structure used by eBPF C program +* to describe map attributes to elf_bpf loader +*/ +struct bpf_map_def { +unsigned int type; +unsigned int key_size; +unsigned int value_size; +unsigned int max_entries; +unsigned int map_flags; +unsigned int inner_map_idx; +unsigned int numa_node; +}; + +#define BPF_ANNOTATE_KV_PAIR (name, type_key, type_val) \ + struct ____btf_map_##name { \
Quoted 8 lines
tools/lib/bpf/bpf_helpers.h line 266
Quoted 6 lines
tools/lib/bpf/bpf_helpers.h new file +6
-0+type_key key; \ +type_val value; \ + }; \ + struct ____btf_map_##name \ + __attribute__ ((section(".maps." #name), used)) \ + ____btf_map_##name = { }Same here.
We still use it in few selftests, I'll move it into selftests-only header.
Quoted 7 lines
tools/lib/bpf/bpf_helpers.h line 269
Quoted 3 lines
tools/lib/bpf/bpf_helpers.h new file +2
-0+static int (*bpf_skb_load_bytes)(void *ctx,int off,void *to,int len) = + (void *) BPF_FUNC_skb_load_bytes;[...]
Given we already move
bpf_endian.hto a separate header, I'd do the same for all tracing specifics as well, e.g.bpf_tracing.h.
You mean all the stuff below, right? I'll extract it into separate header, no problem.
What about CO-RE stuff. It's not strictly for tracing, so does it make sense to keep it here?
Quoted 6 lines
Quoted 6 lines
tools/lib/bpf/bpf_helpers.h new file +5
-0+/* Scan the ARCH passed in from ARCH env variable (see Makefile) */ +#if defined(__TARGET_ARCH_x86) +#define bpf_target_x86 +#define bpf_target_defined +#elif defined(__TARGET_ARCH_s390)
[...]
Quoted 3 lines
Quoted 3 lines
--
2.17.1
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
Quoted 10 lines · Andrii Nakryiko
Quoted 8 lines · Daniel Borkmann
Quoted 4 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.Signed-off-by Andrii Nakryiko <[email protected]>[...]
[...]
Quoted 18 lines
Quoted 3 lines
As mentioned earlier, the whole helper function description below should get a big cleanup in here when moved into libbpf API.
Right, I just recalled that today, sorry I didn't do it for this version.
There were two things you mentioned on that thread that you wanted to clean up:
1. using__u32instead int and stuff like that
2. using macro to hide some of the ugliness of (void *) =BPF_FUNC_xxxSo with 1) I'm concerned that we can't just assume that
__u32is always going to be defined. Also we needbpf_helpers.hto be usable both with including system headers, as well as auto-generatedvmlinux.h. In first case, I don't think we can assume that typedef is always defined, in latter case we can't really define it on our own. So I think we should just keep it as int, unsigned long long, etc. Thoughts?
Quoted 3 lines
For 2), I'm doing that right now, but it's not that much cleaner, let's see.
Am I missing something else?
Ok, so this doesn't work with just
#define BPF_FUNC(NAME, ...) (*NAME)(__VA_ARGS__) __attribute__((unused)) = (void *) BPF_FUNC_##NAME
because helper is called bpf_map_update_elem(), but enum value is BPF_FUNC_map_update_elem (without bpf_ prefix). So one way to do this would be to have bpf_ prepended to function name by macro:
static int BPF_FUNC(map_update_elem, ....);
But this is super confusing, because this definition becomes basically un-greppable. I think we should just keep it as is, or go all the way in for super-verbose
static int BPF_FUNC(BPF_FUNC_map_update_elem, bpf_map_elem, ...);
I hate both, honestly.
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
tools/lib/bpf/bpf_helpers.h new file +4
-0+static void *(*bpf_map_lookup_elem)(void *map,const void *key) = + (void *) BPF_FUNC_map_lookup_elem; +static int (*bpf_map_update_elem)(void *map,const void *key,const void *value, + unsigned long long flags) =[...]
Quoted 1 line
+
[...]
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
--
2.17.1
Re: [PATCH bpf-next 4/6] libbpf: add BPF_CORE_READ/BPF_CORE_READ_INTO helpers
In reply to Andrii NakryikoQuoted 85 lines · Andrii Nakryiko
Quoted 72 lines · Song Liu
Quoted 72 lines · Andrii Nakryiko
Quoted 55 lines · Song Liu
Quoted 55 lines · Andrii Nakryiko
Add few macros simplifying BCC-like multi-level probe reads, while also emitting CO-RE relocations for each read.
Signed-off-by Andrii Nakryiko <[email protected]>---
tools/lib/bpf/bpf_helpers.h| 151 +++++++++++++++++++++++++++++++++++-1 file changed, 147 insertions(+), 4 deletions(-)tools/lib/bpf/bpf_helpers.h +4
-0@@ -19,6 +19,10 @@*/ #define SEC (NAME ) __attribute__((section(NAME), used)) +#ifndef __always_inline +#define __always_inline __attribute__((always_inline)) +#endif +/* helper functions called from eBPF programs written in C */ static void *(*bpf_map_lookup_elem)(void *map, const void *key) =
(void *) BPF_FUNC_map_lookup_elem;tools/lib/bpf/bpf_helpers.h +13
-4@@ -505,7 +509,7 @@ struct pt_regs;#endif /* -* BPF_CORE_READ abstracts away bpf_probe_read() call and captures offset +* bpf_core_read() abstracts away bpf_probe_read() call and captures field * relocation for source address using __builtin_preserve_access_index() * built-in, provided by Clang. * @@ -520,8 +524,147 @@ struct pt_regs;* actual field offset, based on target kernel BTF type that matches original * (local) BTF, used to record relocation. */ -#define BPF_CORE_READ (dst, src) \ - bpf_probe_read((dst), sizeof(*(src)), \ - __builtin_preserve_access_index(src)) +#define bpf_core_read (dst, sz, src) \ + bpf_probe_read(dst, sz, \ + (const void *)__builtin_preserve_access_index(src)) + +/* +* bpf_core_read_str() is a thin wrapper around bpf_probe_read_str() +* additionally emitting BPF CO-RE field relocation for specified source +* argument. +*/ +#define bpf_core_read_str (dst, sz, src) \ + bpf_probe_read_str(dst, sz, \ + (const void *)__builtin_preserve_access_index(src))Quoted 8 lines
tools/lib/bpf/bpf_helpers.h struct pt_regs; · line 542
Quoted 4 lines
tools/lib/bpf/bpf_helpers.h +4
-0+ +#define ___concat (a, b) a ## b +#define ___apply (fn, n) ___concat(fn, n) +#define ___nth (_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, __11,N , ...) NWe are adding many marcos with simple names:
___apply(),___nth. So I worry they may conflict with macro definitions from other libraries. Shall we hide them in .c files or prefix/postfix them with_libbpfor something?Keep in mind, this is the header that's included from BPF code.
Quoted 7 lines
Quoted 3 lines
They are prefixed with three underscores, I was hoping it's good enough to avoid accidental conflicts. It's unlikely someone will have macros with the same names **in BPF-side code**.
BPF side code would include kernel headers. So there are many headers to conflict with. And we won't know until somebody want to trace certain kernel structure.
Quoted 7 lines
We have all the kernel sources at our disposal, there's no need to guess :) There is no instance of
___apply,___concat,___nth,___arrow,___last,___nolast, or___type, not even speaking about other more specific names. There are currently two instances of "____last_____" used in a string. And I'm certainly not afraid that user code can use triple-underscored identifier with exact those names and complain aboutbpf_helpers.h:)
I worry more about _future_ conflicts, that someone may add ___apply to some kernel header file and break some BPF programs. Since these BPF programs are not in-tree, it is very difficult to test them properly. We have had name conflicts from other libraries, so I hope we don't create more ourselves.
Thanks,
Song
Re: [PATCH bpf-next 4/6] libbpf: add BPF_CORE_READ/BPF_CORE_READ_INTO helpers
Quoted 94 lines · Song Liu
Quoted 86 lines · Andrii Nakryiko
Quoted 72 lines · Song Liu
Quoted 72 lines · Andrii Nakryiko
Quoted 55 lines · Song Liu
Quoted 55 lines · Andrii Nakryiko
Add few macros simplifying BCC-like multi-level probe reads, while also emitting CO-RE relocations for each read.
Signed-off-by Andrii Nakryiko <[email protected]>---
tools/lib/bpf/bpf_helpers.h| 151 +++++++++++++++++++++++++++++++++++-1 file changed, 147 insertions(+), 4 deletions(-)tools/lib/bpf/bpf_helpers.h +4
-0@@ -19,6 +19,10 @@*/ #define SEC (NAME ) __attribute__((section(NAME), used)) +#ifndef __always_inline +#define __always_inline __attribute__((always_inline)) +#endif +/* helper functions called from eBPF programs written in C */ static void *(*bpf_map_lookup_elem)(void *map, const void *key) =
(void *) BPF_FUNC_map_lookup_elem;tools/lib/bpf/bpf_helpers.h +13
-4@@ -505,7 +509,7 @@ struct pt_regs;#endif /* -* BPF_CORE_READ abstracts away bpf_probe_read() call and captures offset +* bpf_core_read() abstracts away bpf_probe_read() call and captures field * relocation for source address using __builtin_preserve_access_index() * built-in, provided by Clang. * @@ -520,8 +524,147 @@ struct pt_regs;* actual field offset, based on target kernel BTF type that matches original * (local) BTF, used to record relocation. */ -#define BPF_CORE_READ (dst, src) \ - bpf_probe_read((dst), sizeof(*(src)), \ - __builtin_preserve_access_index(src)) +#define bpf_core_read (dst, sz, src) \ + bpf_probe_read(dst, sz, \ + (const void *)__builtin_preserve_access_index(src)) + +/* +* bpf_core_read_str() is a thin wrapper around bpf_probe_read_str() +* additionally emitting BPF CO-RE field relocation for specified source +* argument. +*/ +#define bpf_core_read_str (dst, sz, src) \ + bpf_probe_read_str(dst, sz, \ + (const void *)__builtin_preserve_access_index(src))Quoted 8 lines
tools/lib/bpf/bpf_helpers.h struct pt_regs; · line 542
Quoted 4 lines
tools/lib/bpf/bpf_helpers.h +4
-0+ +#define ___concat (a, b) a ## b +#define ___apply (fn, n) ___concat(fn, n) +#define ___nth (_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, __11,N , ...) NWe are adding many marcos with simple names:
___apply(),___nth. So I worry they may conflict with macro definitions from other libraries. Shall we hide them in .c files or prefix/postfix them with_libbpfor something?Keep in mind, this is the header that's included from BPF code.
Quoted 7 lines
Quoted 3 lines
They are prefixed with three underscores, I was hoping it's good enough to avoid accidental conflicts. It's unlikely someone will have macros with the same names **in BPF-side code**.
BPF side code would include kernel headers. So there are many headers to conflict with. And we won't know until somebody want to trace certain kernel structure.
Quoted 7 lines
We have all the kernel sources at our disposal, there's no need to guess :) There is no instance of
___apply,___concat,___nth,___arrow,___last,___nolast, or___type, not even speaking about other more specific names. There are currently two instances of "____last_____" used in a string. And I'm certainly not afraid that user code can use triple-underscored identifier with exact those names and complain aboutbpf_helpers.h:)
Quoted 1 line
I worry more about _future_ conflicts, that someone may add
___applyto
You can say the same about pretty much any name that user might use, that's just the fact of life with C language without namespaces. I don't think that justifies usage of obscure names.
Look at SEC macro, for instance. It's also an enum value in drivers/sbus/char/oradax.c, but it might some day end up in one of driver's headers. This is probably not a reason to rename it, though.
Quoted 4 lines
some kernel header file and break some BPF programs. Since these BPF programs are not in-tree, it is very difficult to test them properly. We have had name conflicts from other libraries, so I hope we don't create more ourselves.
Let's agree to come back to this problem when and if we ever encounter it. All those ___xxx macro are internal and users shouldn't rely on them, which means if we ever get a real conflict, we'll be able to rename them to avoid the conflict.
Quoted 3 lines
Thanks,
Song
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Andrii NakryikoQuoted 16 lines · Andrii Nakryiko
Quoted 14 lines · Toke Høiland-Jørgensen
tools/lib/bpf/bpf_helpers.h line 257
Quoted 11 lines
tools/lib/bpf/bpf_helpers.h new file +9
-0+struct bpf_map_def { +unsigned int type; +unsigned int key_size; +unsigned int value_size; +unsigned int max_entries; +unsigned int map_flags; +unsigned int inner_map_idx; +unsigned int numa_node; +};Didn't we agree on no new
bpf_map_defABI in libbpf, and that all additions should be BTF-based?
Quoted 4 lines
Oh yes, we did, sorry, this is an oversight. I really appreciate you pointing this out ;)
I'll go overbpf_helpers.hcarefully and will double-check if we don't have any other custom selftests-only stuff left there.
Great, thanks!
-Toke
Re: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Andrii NakryikoQuoted 24 lines · Andrii Nakryiko
Quoted 16 lines · John Fastabend
Quoted 14 lines · Toke Høiland-Jørgensen
tools/lib/bpf/bpf_helpers.h line 257
Quoted 10 lines
tools/lib/bpf/bpf_helpers.h new file +9
-0+struct bpf_map_def { +unsigned int type; +unsigned int key_size; +unsigned int value_size; +unsigned int max_entries; +unsigned int map_flags; +unsigned int inner_map_idx; +unsigned int numa_node; +};Didn't we agree on no new
bpf_map_defABI in libbpf, and that all additions should be BTF-based?Quoted 6 lines
Quoted 2 lines
-Toke
We use libbpf on pre BTF kernels so in this case I think it makes sense to add these fields. Having
inner_map_idxthere should allow us to remove some other things we have sitting around.
Quoted 5 lines
We had a discussion about supporting non-BTF and non-standard BPF map definition before and it's still on my TODO list to go and do a proof of concept how that can look like and what libbpf changes we need to make. Right now libbpf doesn't support those new fields anyway, so we shouldn't add them to public API.
This was the thread; the context was libbpf support in iproute2:
https://lore.kernel.org/netdev/[email protected]/
Basically, we agreed that rather than adding more fields to bpf_map_def in libbpf itself, we'd support BTF definitions natively, and provide applications the right callbacks to support custom formats as they see fit.
-Toke
Daniel Borkmann
maintainerRe: [PATCH bpf-next 2/6] libbpf: move bpf_helpers.h, bpf_endian.h into libbpf
In reply to Andrii NakryikoQuoted 32 lines · Andrii Nakryiko
Quoted 16 lines · Daniel Borkmann
Quoted 4 lines · Andrii Nakryiko
Make
bpf_helpers.handbpf_endian.hofficial part of libbpf. Ensure they are installed along the other libbpf headers.Signed-off-by Andrii Nakryiko <[email protected]>[...]
Quoted 9 lines
new file mode 100644
index 000000000000..fbe28008450ftools/lib/bpf/bpf_endian.h new file +4
-0@@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +#ifndef __BPF_ENDIAN__ +#define __BPF_ENDIAN__ +[...]
Quoted 11 lines
tools/lib/bpf/bpf_endian.h line 70
Quoted 6 lines
tools/lib/bpf/bpf_endian.h new file +6
-0+#define bpf_cpu_to_be64 (x) \ + (__builtin_constant_p(x) ? \ + __bpf_constant_cpu_to_be64(x) : __bpf_cpu_to_be64(x)) +#define bpf_be64_to_cpu (x) \ + (__builtin_constant_p(x) ? \ + __bpf_constant_be64_to_cpu(x) : __bpf_be64_to_cpu(x))Nit: if we move this into a public API for libbpf, we should probably also define for sake of consistency a bpf_cpu_to_be{64,32,16}() and bpf_be{64,32,16}_to_cpu() and have the latter two point to the existing bpf_hton{l,s}() and bpf_ntoh{l,s}() macros.
Quoted 8 lines
I think these deserve better tests before we add more stuff, both from BPF-side and userland-side (as they are supposed to be includeable from both, right)? I'm hesitant adding new unfamiliar macro/builtins without tests, but I don't want to get distracted with that right now, especially this patch set already becomes bigger than I'd hope.
Given we are talking about adding new stuff which is not breaking change, we can add it later after we move this as is, agree?
Sure, we can add it later, I only meant to say adding things like define bpf_cpu_to_be32 pointing to bpf_htonl and so on, so no new extra helpers, but just to make it more consistent API wise and from usability side.
Quoted 49 lines
Quoted 19 lines
Quoted 19 lines
tools/lib/bpf/bpf_endian.h new file +1
-0+#endif /* __BPF_ENDIAN__ */ tools/lib/bpf/bpf_helpers.h new file +11
-0@@ -0,0 +1,527 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +#ifndef __BPF_HELPERS__ +#define __BPF_HELPERS__ + +#define __uint (name, val) int (*name)[val] +#define __type (name, val) val *name + +/* helper macro to print out debug messages */ +#define bpf_printk (fmt, ...) \ +({ \ + char ____fmt[] = fmt; \Quoted 8 lines
tools/lib/bpf/bpf_helpers.h line 14
Quoted 3 lines
tools/lib/bpf/bpf_helpers.h new file +3
-0+bpf_trace_printk (____fmt,sizeof (____fmt), \ + ##__VA_ARGS__); \ +})Did you double check if by now via .rodata we can have the fmt as const char * and add
__attribute__((format (printf, 1, 2))) to it? If that works we should avoid having to copy the string as above in the API.This doesn't work still, unfortunately. Eventually, though, we'll be able to essentially nop it out with direct call to
bpf_trace_printk, so I'm not concerned about future API burden.Quoted 10 lines
tools/lib/bpf/bpf_helpers.h line 22
Quoted 7 lines
tools/lib/bpf/bpf_helpers.h new file +7
-0+/* helper macro to place programs, maps, license in +* different sections in elf_bpf file. Section names +* are interpreted by elf_bpf loader +*/ +#define SEC (NAME ) __attribute__((section(NAME), used)) + +/* helper functions called from eBPF programs written in C */ As mentioned earlier, the whole helper function description below should get a big cleanup in here when moved into libbpf API.
Right, I just recalled that today, sorry I didn't do it for this version.
There were two things you mentioned on that thread that you wanted to clean up:
1. using__u32instead int and stuff like that
2. using macro to hide some of the ugliness of (void *) =BPF_FUNC_xxx
Quoted 7 lines
So with 1) I'm concerned that we can't just assume that
__u32is always going to be defined. Also we needbpf_helpers.hto be usable both with including system headers, as well as auto-generatedvmlinux.h. In first case, I don't think we can assume that typedef is always defined, in latter case we can't really define it on our own. So I think we should just keep it as int, unsigned long long, etc. Thoughts?
Long time ago in Cilium I tried to clean up the mess a bit [0] for most of the helpers we're using. I was using types from stdint.h actually, haven't heard of any issues so far.
The __u* types which you mentioned is shipped in linux/types.h, these are typically used in uapi headers like linux/bpf.h and match kernel side u* counterpart, so they should generally be available as well as an alternative.
[0] https://github.com/cilium/cilium/blob/master/bpf/include/bpf/api.h
Quoted 50 lines
For 2), I'm doing that right now, but it's not that much cleaner, let's see.
Am I missing something else?
Quoted 5 lines
Quoted 4 lines
tools/lib/bpf/bpf_helpers.h new file +4
-0+static void *(*bpf_map_lookup_elem)(void *map,const void *key) = + (void *) BPF_FUNC_map_lookup_elem; +static int (*bpf_map_update_elem)(void *map,const void *key,const void *value, + unsigned long long flags) =[...]
Quoted 14 lines
tools/lib/bpf/bpf_helpers.h line 244
Quoted 11 lines
tools/lib/bpf/bpf_helpers.h new file +11
-0+ +/* llvm builtin functions that eBPF C program may use to +* emit BPF_LD_ABS and BPF_LD_IND instructions +*/ +struct sk_buff ; +unsigned long long load_byte (void *skb, +unsigned long long off) asm("llvm.bpf.load.byte" ); +unsigned long long load_half (void *skb, +unsigned long long off) asm("llvm.bpf.load.half" ); +unsigned long long load_word (void *skb, +unsigned long long off) asm("llvm.bpf.load.word" );These should be removed from the public API, no point in adding legacy/ deprecated stuff in there.
Oh, cool, never knew what that stuff is anyway :)
Quoted 15 lines
Quoted 15 lines
tools/lib/bpf/bpf_helpers.h new file +15
-0+/* a helper structure used by eBPF C program +* to describe map attributes to elf_bpf loader +*/ +struct bpf_map_def { +unsigned int type; +unsigned int key_size; +unsigned int value_size; +unsigned int max_entries; +unsigned int map_flags; +unsigned int inner_map_idx; +unsigned int numa_node; +}; + +#define BPF_ANNOTATE_KV_PAIR (name, type_key, type_val) \ + struct ____btf_map_##name { \Quoted 8 lines
tools/lib/bpf/bpf_helpers.h line 266
Quoted 6 lines
tools/lib/bpf/bpf_helpers.h new file +6
-0+type_key key; \ +type_val value; \ + }; \ + struct ____btf_map_##name \ + __attribute__ ((section(".maps." #name), used)) \ + ____btf_map_##name = { }Same here.
Quoted 1 line
We still use it in few selftests, I'll move it into selftests-only header.
Yes, all the legacy stuff we don't want exposed in libbpf as API should be kept in samples or selftests only header until it's fully converted one day where we can remove that temporary header entirely.
Quoted 9 lines
Quoted 6 lines
tools/lib/bpf/bpf_helpers.h line 269
Quoted 2 lines
tools/lib/bpf/bpf_helpers.h new file +2
-0+static int (*bpf_skb_load_bytes)(void *ctx,int off,void *to,int len) = + (void *) BPF_FUNC_skb_load_bytes;[...]
Given we already move
bpf_endian.hto a separate header, I'd do the same for all tracing specifics as well, e.g.bpf_tracing.h.You mean all the stuff below, right? I'll extract it into separate header, no problem.
Sounds good. We should probably get this more in order as well if it becomes API. My worry is that potentially PT_REGS_* names may clash in future, but this cleanup can be done in a second step as long as its done in this development cycle and not officially released yet.
Quoted 2 lines
What about CO-RE stuff. It's not strictly for tracing, so does it make sense to keep it here?
Yes, as it's core and has a use-case beyond tracing, I'd keep it here.
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
tools/lib/bpf/bpf_helpers.h new file +5
-0+/* Scan the ARCH passed in from ARCH env variable (see Makefile) */ +#if defined(__TARGET_ARCH_x86) +#define bpf_target_x86 +#define bpf_target_defined +#elif defined(__TARGET_ARCH_s390)[...]
Re: [PATCH bpf-next 4/6] libbpf: add BPF_CORE_READ/BPF_CORE_READ_INTO helpers
In reply to Andrii NakryikoQuoted 114 lines · Andrii Nakryiko
Quoted 94 lines · Song Liu
Quoted 86 lines · Andrii Nakryiko
Quoted 72 lines · Song Liu
Quoted 72 lines · Andrii Nakryiko
Quoted 55 lines · Song Liu
Quoted 55 lines · Andrii Nakryiko
Add few macros simplifying BCC-like multi-level probe reads, while also emitting CO-RE relocations for each read.
Signed-off-by Andrii Nakryiko <[email protected]>---
tools/lib/bpf/bpf_helpers.h| 151 +++++++++++++++++++++++++++++++++++-1 file changed, 147 insertions(+), 4 deletions(-)tools/lib/bpf/bpf_helpers.h +4
-0@@ -19,6 +19,10 @@*/ #define SEC (NAME ) __attribute__((section(NAME), used)) +#ifndef __always_inline +#define __always_inline __attribute__((always_inline)) +#endif +/* helper functions called from eBPF programs written in C */ static void *(*bpf_map_lookup_elem)(void *map, const void *key) =
(void *)BPF_FUNC_map_lookup_elem;tools/lib/bpf/bpf_helpers.h +13
-4@@ -505,7 +509,7 @@ struct pt_regs;#endif /* -* BPF_CORE_READ abstracts away bpf_probe_read() call and captures offset +* bpf_core_read() abstracts away bpf_probe_read() call and captures field * relocation for source address using __builtin_preserve_access_index() * built-in, provided by Clang. * @@ -520,8 +524,147 @@ struct pt_regs;* actual field offset, based on target kernel BTF type that matches original * (local) BTF, used to record relocation. */ -#define BPF_CORE_READ (dst, src) \ - bpf_probe_read((dst), sizeof(*(src)), \ - __builtin_preserve_access_index(src)) +#define bpf_core_read (dst, sz, src) \ + bpf_probe_read(dst, sz, \ + (const void *)__builtin_preserve_access_index(src)) + +/* +* bpf_core_read_str() is a thin wrapper around bpf_probe_read_str() +* additionally emitting BPF CO-RE field relocation for specified source +* argument. +*/ +#define bpf_core_read_str (dst, sz, src) \ + bpf_probe_read_str(dst, sz, \ + (const void *)__builtin_preserve_access_index(src))Quoted 8 lines
tools/lib/bpf/bpf_helpers.h struct pt_regs; · line 542
Quoted 4 lines
tools/lib/bpf/bpf_helpers.h +4
-0+ +#define ___concat (a, b) a ## b +#define ___apply (fn, n) ___concat(fn, n) +#define ___nth (_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, __11,N , ...) NWe are adding many marcos with simple names:
___apply(),___nth. So I worry they may conflict with macro definitions from other libraries. Shall we hide them in .c files or prefix/postfix them with_libbpfor something?Keep in mind, this is the header that's included from BPF code.
Quoted 7 lines
Quoted 3 lines
They are prefixed with three underscores, I was hoping it's good enough to avoid accidental conflicts. It's unlikely someone will have macros with the same names **in BPF-side code**.
BPF side code would include kernel headers. So there are many headers to conflict with. And we won't know until somebody want to trace certain kernel structure.
Quoted 7 lines
We have all the kernel sources at our disposal, there's no need to guess :) There is no instance of
___apply,___concat,___nth,___arrow,___last,___nolast, or___type, not even speaking about other more specific names. There are currently two instances of "____last_____" used in a string. And I'm certainly not afraid that user code can use triple-underscored identifier with exact those names and complain aboutbpf_helpers.h:)Quoted 1 line
I worry more about _future_ conflicts, that someone may add
___applytoYou can say the same about pretty much any name that user might use, that's just the fact of life with C language without namespaces. I don't think that justifies usage of obscure names.
Look at SEC macro, for instance. It's also an enum value in
drivers/sbus/char/oradax.c, but it might some day end up in one of driver's headers. This is probably not a reason to rename it, though.Quoted 4 lines
some kernel header file and break some BPF programs. Since these BPF programs are not in-tree, it is very difficult to test them properly. We have had name conflicts from other libraries, so I hope we don't create more ourselves.
Quoted 4 lines
Let's agree to come back to this problem when and if we ever encounter it. All those
___xxxmacro are internal and users shouldn't rely on them, which means if we ever get a real conflict, we'll be able to rename them to avoid the conflict.
Well, if this really happens, we will have to fix them.
I won't block this set just for this. If you insist, let's keep these as-is.
Thanks,
Song