Commit Graph

103 Commits

Author SHA1 Message Date
Maciej Żenczykowski
3a64568abb type safety for kernel version
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: If05c0c5383dceeb65964143f5574d2ee1a484907
2023-10-06 15:54:38 -07:00
Maciej Żenczykowski
6dec6e9374 type safety for bool ignore_on_{eng,user,userdebug}
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ia85e16d1a7f8d1b21e4ce4c306db6bbea06138e0
2023-10-06 14:57:12 -07:00
Maciej Żenczykowski
f2466ef4bb type safety for bool optional
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I6f10395ca6f1c73f3513031f561fdf1b1dedf451
2023-10-06 14:48:43 -07:00
Maciej Żenczykowski
a262bd3d1e type safety for bool shared
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I4883eab734f47522fec718f0709475f7e6dd0a53
2023-10-06 14:46:11 -07:00
Maciej Żenczykowski
10da6d4e61 move PRIVATE/SHARED/MANDATORY/OPTIONAL/{LOAD,IGNORE}_ON_* into core headers
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I7053ab46e73b763f64f36baf5e7ab64b62421045
2023-10-06 14:25:41 -07:00
Maciej Żenczykowski
1e32277fff BpfClassic.h - add missing commas
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ife32fa36808de13c5f37ace497395dcc381ca336
2023-10-06 13:50:13 -07:00
Maciej Żenczykowski
d5bf2061f3 more classic bpf macros
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I5c7fd55301cd5aa44502f8481923c86bc49ea914
2023-10-04 22:41:21 +00:00
Maciej Żenczykowski
73df7414f1 BpfClassic.h - more helpers
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie0eb8895c2d887b71ba886735ad106c1118ebeac
2023-10-02 20:03:41 +00:00
Maciej Żenczykowski
b900201ffe rename BPF2_REJECT_IF_ANY_BITS_SET -> BPF2_REJECT_IF_ANY_MASKED_BITS_SET
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I54a189f0f7b73917aa3fd049fe2eca387aaca250
2023-10-02 16:09:05 +00:00
Maciej Żenczykowski
7e2f53e762 reverse BpfMap & BpfMapRO inheritence to make sense
Bug: 235590615
Bug: 235907076
Bug: 286003437
Test: TreeHugger, m droid gpuservice_unittest libtimeinstate_test bpf_benchmark bpf_module_test libbpf_load_test && mma
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I020a898fe8b257040a100f146654d4a04b19f843
2023-09-29 00:44:33 +00:00
Maciej Żenczykowski
601d9f8b65 use different log.h header file
Test: TreeHugger
Bug: 299653233
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I1e5c7bfc86d2ad61c0bad3b5ef2add94b4ce1481
2023-09-21 22:11:43 +00:00
Maciej Żenczykowski
1ef814da95 Merge "BpfMap.h - hide getMap()" into main 2023-08-22 00:32:09 +00:00
Maciej Żenczykowski
12c0ea929c BpfMap.h - hide getMap()
This leaks (what should be) an internal only fd.

There's still multiple uses of getMap() left in:
  common/native/bpf_headers/BpfMapTest.cpp

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I87a36f3f935ba9148ae4456fc6bf14b0e04f6038
2023-08-21 19:53:04 +00:00
Maciej Żenczykowski
ef136ebfa1 Merge "BpfMap.h - add 3 using statements" into main 2023-08-21 17:20:17 +00:00
Maciej Żenczykowski
eb1eb69f86 BpfMap.h - add 3 using statements
in particular base::Result is needlessly verbose

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ide0de9acec79b9aeeaff7ec92270304907bd10d4
2023-08-20 23:33:46 +00:00
Maciej Żenczykowski
910cf4da9b BpfMap - slightly simplify isEmpty()
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9b93224ec2d4ff717d5e39c3ad50e63ac73f5f7c
2023-08-20 12:03:26 +00:00
Maciej Żenczykowski
2cd8b69005 BpfClassic - add three more macros
Example use case:
  BPF_LOAD_SKB_PROTOCOL,
  BPF2_ACCEPT_IF_EQUAL(ETH_P_ARP),
  BPF2_REJECT_IF_NOT_EQUAL(ETH_P_IP),
  BPF_LOAD_IPV4_BE16(frag_off),
  BPF2_REJECT_IF_ANY_BITS_SET(IP_MF | IP_OFFMASK),
  BPF_ACCEPT,
which is a sample program which accepts ARP plus non-fragmented IPv4.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ibe6f9f7941f11bc7b767a092d3d108fa0291f360
2023-07-22 14:32:47 +00:00
Maciej Żenczykowski
f1416b5494 enforce bpf ring buffer size is a power of two
While at it enforce a min/max of 4 kiB and 256 MiB.

A bpf ringbuffer must be an integer multiple of page size
(which is at least 4kiB), so 4kB is forced.

256 MiB upper limit is probably higher than it should be,
but it's really only here to avoid the u32/s32 conversion
mess that happens for 2+ GiB.

Test: TreeHugger
Bug: 279819914
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I2216d2273b0956a86c4222dd6ecf7293e0bafd41
2023-06-17 11:26:21 +00:00
Maciej Żenczykowski
8837bf21a5 add macro to disable BTF on user builds
Bug: 286369326
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I6a705ae99a12fb8464730dab1119f4ce2ef3f974
2023-06-09 06:06:36 +00:00
Maciej Żenczykowski
5bec8b44a9 force map key/value sizes < 1024/65536 bytes to match BpfMap.java
Test: TreeHugger
Bug: 286154283
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I31fb48aa032e22a83e3e9f0ac8d7f2b3253e995e
2023-06-07 06:59:40 +00:00
Maciej Żenczykowski
2129040ae4 KernelUtils.h - better static assert messages for impossible conditions
per Lorenzo's request during cherrypick to udc-dev on
https://googleplex-android-review.git.corp.google.com/c/platform/frameworks/libs/net/+/23391171

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib19f5b3b63a6978ac5a72b2be08a2f0ba6cdd712
2023-05-25 12:30:52 +00:00
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
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