Commit Graph

96 Commits

Author SHA1 Message Date
Maciej Żenczykowski
4782ab1b9e KernelUtils.h: explicitly do not support x32
x86 has 2 obvious ABIs:
32-bit registers/pointers/system calls (i386/i486/i586/i686)
64-bit registers/pointers/system calls (amd64 / x86_64)

but there also exists a third:
the memory optimizing hybrid 'x32' which uses 64-bit registers,
with 32-bit pointers, and 64-bit system calls (with minor tweaks).

(there is also technically an aarch64/arm64-ilp32 ABI mirroring x32)

Note: It would probably be trivial to add support if we wanted to.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I82138c5bafbc3bd37cad98eeb79441701d94c386
2023-05-21 02:33:55 +00:00
Maciej Żenczykowski
1c4ba307f4 Merge "bpfloader: ignore_on_(arch) support" 2023-05-20 01:04:21 +00:00
Maciej Żenczykowski
2998627ddd KernelUtils.h - add describeArch()
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I0ae4b5fd8cf9baf04b738743d9e52f94a35aaf21
2023-05-20 00:45:27 +00:00
Maciej Żenczykowski
6d3224e286 bpfloader: ignore_on_(arch) support
Test: TreeHugger
Bug: 283523051
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I73768a67b50bb82ebb13e20c6ba6425e1e4236c7
2023-05-19 20:59:40 +00:00
Treehugger Robot
8273756bcc Merge "KernelUtils.h - add arch query helpers" 2023-05-19 07:32:57 +00:00
Patrick Rohr
93312c998e bpf_headers: Make bpf_headers available to libsysutils
So KernelUtils.h can be used in NetlinkEvent.cpp.

Test: builds
Change-Id: If7a6a28e735ee19dc6b7a72330a97a8a01bcecd5
2023-05-18 14:39:14 -07:00
Patrick Rohr
85a86cb908 BpfRingbuf: Clean up memory access
Consumer pos is only written to by userspace (except for
initialization), so reading from it does not require a memory barrier.

This change also moves producer pos to be acquired before anything else
and marks start_ptr as volatile, so it is only read once.

Test: atest BpfRingbufTest
Change-Id: I228578ef4b42c1732646f0ae928b9a0aa2445304
2023-05-18 14:08:37 -07:00
Patrick Rohr
52954ff505 BpfRingbuf: Add support for 32 bit kernels
This change adds support for 32 bit kernels by capping mProducerPos to
4 bytes. mConsumerPos (which is written to by userspace) continues to
use 8 bytes. (This can be done because an entire page is allocated for
mConsumerPos to control access permissions). In a 32 bit kernel, that
means that the top order bits are just ignored. In addition, comparisons
in userspace only use the bottom 4 bytes to be bitness agnostic.

Test: atest BpfRingbufTest
Change-Id: I7fe6d9000a151512785f1aa2a53fa97d31967d19
2023-05-18 14:08:37 -07:00
Maciej Żenczykowski
5b2bcc508a KernelUtils.h - add arch query helpers
see bionic/docs/defines.md:
http://cs/h/android/platform/superproject/+/master:bionic/docs/defines.md?l=57

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I818da16ad4dd1837ef3cdcc552557119eafaf6af
2023-05-18 17:17:29 +00:00
Maciej Żenczykowski
0d6edeef83 Merge "BpfRingbuf: support 32 bit userspace" 2023-05-18 17:08:15 +00:00
Treehugger Robot
cee4bd691e Merge "bpf_headers: add kernel bitness utility functions to KernelUtils" 2023-05-18 00:28:51 +00:00
Maciej Żenczykowski
7d2b91e7a3 Merge "bpf: Copy comment about bpffs labelling into bpf_helpers.h" 2023-05-17 22:40:57 +00:00
Patrick Rohr
311f8b325e BpfRingbuf: support 32 bit userspace
Both consumer and producer position are wrapped in an std::atomic and
extended to uint64_t to enforce atomic access.

See resulting assembly instructions for aosp_cf_x86_phone-userdebug:

frameworks/libs/net/common/native/bpf_headers/include/bpf/BpfRingbuf.h:212
  2d:   8b 41 18                mov    0x18(%ecx),%eax
external/libcxx/include/atomic:926 (discriminator 4)
  30:   f3 0f 7e 00             movq   (%eax),%xmm0

This will not work on a 32 bit kernel (so we might want to explicitly
check for that in the test).

Test: atest BpfRingbufTest
Change-Id: I7fec435cbe49d392363d05f7e702d82ecacb9e91
2023-05-17 15:39:38 -07:00
Patrick Rohr
2f5c1159cc bpf: Copy comment about bpffs labelling into bpf_helpers.h
I found this comment in aosp/2103424 helpful in figuring out the correct
selinux domain for a newly added map.

Test: documentation only change
Change-Id: I96c06ff33605c8ed3f2ae6e8810bbcdc8e66b51b
2023-05-17 22:38:22 +00:00
Patrick Rohr
432291a334 bpf_headers: add kernel bitness utility functions to KernelUtils
These were copied from
system/core/libsysutils/src/NetlinkEvent.cpp and the following changes
were applied:
- Mark isUserspace64bit as constexpr.
- Added __unused annotations
- Fixed a typo (s/meaningul/meaningful)

Test: TH
Change-Id: Ia0b8e6e02daca7e9b69f337a494d88eb1beffc22
2023-05-17 12:52:56 -07:00
Patrick Rohr
66452f566b bpf_headers: rename KernelVersion.h to KernelUtils.h
Test: TH
Change-Id: Ifacc159c19a8fcb64b571295b945fb9fca82496a
2023-05-17 11:43:02 -07:00
Maciej Żenczykowski
bc89547ed3 Merge "Remove mentions of bploader versions between S and T" 2023-04-27 19:37:36 +00:00
Treehugger Robot
82b6bd25f3 Merge "Bump the default bpfloader version for platform code." 2023-04-27 19:17:59 +00:00
Maciej Żenczykowski
9ee26f72eb Remove mentions of bploader versions between S and T
(they're only of historical interest at this point)

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I0e52efda62626091c3023c5fd3622ffcdcd00ee1
2023-04-26 23:17:07 +00:00
Maciej Żenczykowski
59934d185a Bump the default bpfloader version for platform code.
This is the default for BPFLOADER_MIN_VER if not otherwise specified.

This does not affect mainline code, see:
  cs/p:aosp-master file:packages/modules/Connectivity/bpf_progs define.*BPFLOADER_MIN_VER

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ice5b14c3b580653f8889a5292abaa89cf8434da1
2023-04-26 23:13:24 +00:00
Maciej Żenczykowski
1e697e5514 Added a new #define for U BpfLoader version
See http://cs/h/googleplex-android/platform/superproject/+/udc-dev:system/bpf/libbpf_android/Loader.cpp?l=40

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: If0d75a30f8432f62a1c56aef12efba644fb458d6
2023-04-26 23:06:38 +00:00
Gopal Krishna Shukla
2632888a10 bpf_headers: add bpf_probe_read_user
Change-Id: I2b106cf8e34ec97f1efa2dd735b70aab8e4e1b4e
2023-04-15 00:58:34 +05:30
Maciej Żenczykowski
fb914d8226 update comment about synchronizeKernelRCU() still working
Since it just came up again, and I just verified this still works

Test: N/A
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I292c94cf9af63488ed30a6ac367ac83c8912e97e
2023-04-04 22:24:31 +00:00
Steven Moreland
59e343367a bpf_headers: add bpf_probe_read_user_str
Bug: 269407130
Test: N/A
Change-Id: I735bffefd41ea3edcafd846041f8c237e860ee6c
2023-03-29 00:12:36 +00:00
Maciej Żenczykowski
aab04f1455 introduce BpfClassic.h
with helper macros for writing classic bpf code

Bug: 265591307
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ibffad51668303ddc5d6850409f9058dfc2894ef6
2023-03-10 23:46:42 +00:00
Maciej Żenczykowski
aa9063c3d5 improve getSocketCookie()
(and a few minor cleanups while at it)

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I657c3314fb0ad2519dc4d1b0743e1be6a81a887a
2023-03-03 01:56:47 +00:00
Ryan Zuklie
2669e24058 Add BpfRingbuf class to wrap ringbuf access
This implements the bare minimum to read from BPF ring buffers in
userspace. The implementation uses two shared memory regions with
atomic-like access. Currently, the class only guarantees that the
element type has the same size (later can add btf support).

The alternative would be to use libbpf or bcc, but since this is a small
header-only library that seemed like it could end up being more
difficulty than it was worth.

Bug: 246985031
Test: atest libbpf_android_test
Change-Id: I7f08e76db9cb9672ef66c629bccb3db63d3c2229
2023-01-23 08:28:50 -08:00
Ryan Zuklie
be2ff67dae Add macro support for ignore_on BPF maps.
This is similar to https://r.android.com/2374598 for maps.

Bug: 246985031
Test: build connectivity module
Change-Id: Id4c9f93b69e808d461b4554bf3fa591828635dd1
2023-01-19 17:24:10 -08:00
Ryan Zuklie
888bd2de67 Update bpf helpers to support new ignore_on fields
This updates the ringbuffer and program helpers to allow specifying
which platform builds they are ignored on. In order to use this, you
must specify a min loader version >= 0.32, so the helpers also add
options for setting this at the program/ringbuf level.

Bug: 246985031
Test: tethering build & install, full platform build & install
Change-Id: I6bf9f7945c3fbac8fd4e02b5805016ac275b7884
2023-01-05 18:00:55 -08:00
Treehugger Robot
d2819de4d7 Merge "bpf_helpers.h: add load_byte/half/word() llvm/clang asm magic" 2023-01-04 12:17:22 +00:00
Maciej Żenczykowski
d6aa6218e3 Merge "Use the .o-wide bpfloader min/max ver for maps & programs too" 2023-01-04 10:28:51 +00:00
Maciej Żenczykowski
3114700eba bpf_helpers.h: add load_byte/half/word() llvm/clang asm magic
see kernel's
  samples/bpf/bpf_helpers.h
or
  tools/testing/selftests/bpf/bpf_legacy.h

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Id331fb9abcd87df8bf87c5a86749acf0144ffbde
2022-12-27 12:31:27 +00:00
Maciej Żenczykowski
ff998720b6 Use the .o-wide bpfloader min/max ver for maps & programs too
This is basically a no-op, but will make future compatibility checks easier.

The programs/maps already won't load on a bpfloader outside of
this range, as the entire .o will be skipped.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I0669c7ff9e04e24a8da68aa821c9ad705a8f5a93
2022-12-27 12:07:03 +00:00
Maciej Żenczykowski
d7d3b03e13 dedup kernelversion.h
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie7a4dd46122e86bf808d0a3ac566075e1c3a2cd4
2022-12-24 17:17:14 +00:00
Maciej Żenczykowski
32be06f45f verify java map key/value struct size matches file descriptor
(this should avoid kernel reading/writing from out of bounds)

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I71fe71eee4e4e6a917477eef5fd2266439e803f3
2022-12-21 00:03:37 +00:00
Maciej Żenczykowski
cc40e9c110 Merge "remove spurious #include and OVERFLOW_COUNTERSET" 2022-12-20 12:13:43 +00:00
Maciej Żenczykowski
e504cc8754 Merge "Add option to skip map/prog based on build type" 2022-12-20 08:38:45 +00:00
Ryan Zuklie
8531cfe5dd Add option to skip map/prog based on build type
This feature allows skipping a program or map based on the type of the
build. This allows things like userdebug-only programs.

Bug: 246985031
Test: added test program and watched it skipped on userdebug
Change-Id: I5cd9725ad8f133c98c86e6ba620a266313055157
2022-12-19 14:08:53 -08:00
Maciej Żenczykowski
6776e3b02b remove spurious #include and OVERFLOW_COUNTERSET
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I8ad1b19905509862bdfa4e710da778ed0effb058
2022-12-19 09:59:25 -08:00
Maciej Żenczykowski
ec6505edd6 remove SKIP_IF_BPF_NOT_SUPPORTED
There is only user left, let us simply move the macro
definition into the resolv_integration_test.cpp file.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I6eb66abaa7f6392ab43300e5087e4277491e44c9
2022-12-18 19:48:54 +00:00
Maciej Żenczykowski
d6ba77ab89 remove no longer needed SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED macro
We can merge this now that:
  https://googleplex-android-review.git.corp.google.com/c/platform/system/netd/+/20284735
  'bpf_base_test - unconditionally run TestSocketFilterSetUp'
has been pulled into tm-mainline-prod.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I1b65cd41ce7b4eaab7f1fead2742c72e48294244
2022-12-18 16:37:32 +00:00
Maciej Żenczykowski
8da25a73a5 remove unused SKIP_IF_XDP_NOT_SUPPORTED macro
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I2480cbe958d2827440e5dd827b43a89f4b7a556c
2022-12-18 11:11:21 +00:00
Treehugger Robot
22d2158452 Merge "Add skeleton BPF ring buffer integration test" 2022-12-14 21:04:18 +00:00
Ryan Zuklie
ccd5eb9359 Add skeleton BPF ring buffer integration test
This adds a BPF program with a ring buffer and an associated test that
ensures the program and ring buffer can be loaded by the bpf loader. In
the following changes, this foundation will be used to test the user
space ring buffer wrapper class.

Unlike BpfMap, userspace can't (easily) write to the ring buffer so the
most accurate way to test a userspace ring buffer implementation is to
write to it from a BPF program.

Note: the BPF program in this change is never actually attached to a
cgroup, it is only loaded. The program will be executed manually using
BPF_PROG_RUN which is "side-effect free" (it can have side effects on
BPF maps, but not on Kernel state, e.g. dropping packets).

Bug: 246985031
Test: atest libbpf_android_test
Change-Id: Ib9c591218188f5f358a755c8854bc6f87d26af12
2022-12-12 10:02:18 -08:00
Ryan Zuklie
5a8738e7de Rename DEFINE_BPF_RINGBUF to DEFINE_BPF_RINGBUF_EXT
_EXT suffix is used for the all-args versions of these, such as the one
here.

Bug: 246985031
Test: build and flash on 4.19 and 5.10
Change-Id: Ibd76cbdc38f4d1f9df4f440147e5f18d9a2ea026
2022-12-08 17:18:54 -08:00
Ryan Zuklie
79ce874e8f Add macro to create BPF ring buffers.
BPF ring buffers are defined like maps, but cannot specify a key or
value size (attempting to do so is a verifier error). This change also
adds the type-safe methods for interacting with the map (output, reserve
and submit).

Bug: 246985031
Test: local run of Network Tracing w/ ring buffers
Change-Id: Ie8a47d987be6cb219fe7d73f2c61a56e3a3ab21a
2022-11-28 11:06:30 -08:00
Maciej Żenczykowski
91e4845301 Merge "Revert "add bpf_for_each_map_elem() helper definition"" 2022-11-07 23:36:35 +00:00
Maciej Żenczykowski
61e21fff0b Revert "add bpf_for_each_map_elem() helper definition"
This reverts commit 8711ae5494a3011f48543987fa08872d775df652.

Bug: 256991746
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I4835904f906422d5927feef82459ce1adcebc90c
2022-11-02 08:39:20 +00:00
Maciej Żenczykowski
203d5e0dcd trivialize SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED
Bug: 255652639
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I3732bbd7ad9e4e06c914206560268bd1ce8204f4
2022-10-26 03:05:32 +00:00
Chris Sabotta
87e48d453e Merge "Bug: b/255652639" 2022-10-25 23:40:44 +00:00