[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20190930185855.4115372-1-andriin@fb.com>
Date: Mon, 30 Sep 2019 11:58:49 -0700
From: Andrii Nakryiko <andriin@...com>
To: <bpf@...r.kernel.org>, <netdev@...r.kernel.org>, <ast@...com>,
<daniel@...earbox.net>
CC: <andrii.nakryiko@...il.com>, <kernel-team@...com>,
Andrii Nakryiko <andriin@...com>
Subject: [PATCH bpf-next 0/6] 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%)
--
2.17.1
Powered by blists - more mailing lists