Commit graph android_packages_modules_Co.../bpf/loader/NetBpfLoad.cpp
Message SHA1
NetBpfLoad: Add method to prepare map load by libbpf
Bug: 428792764
Flag: EXEMPT no-op
Test: TH
Change-Id: I74bb7895756df19be0d095269a490d3da54787bd

Motomu Utsumi   commited 2025-07-24 13:57:50 +09:00
f1b1afb341
NetBpfLoad: Extract building prog pin loc to a helper function
Bug: 428792764
Flag: EXEMPT no-op
Test: TH
Change-Id: Ib79e90f81fcfeb7767850a49b9f8bd7b9e475647

Motomu Utsumi   commited 2025-07-24 13:57:35 +09:00
f8977e924d
NetBpfLoad: Extract building map pin loc to a helper function
Bug: 428792764
Flag: EXEMPT no-op
Test: TH
Change-Id: I4e66c7127f210a2264b370e079c1fb7b44958886

Motomu Utsumi   commited 2025-07-24 13:08:34 +09:00
bf407f6bc4
NetBpfLoad: Add program name to struct codeSection.
Bug: 428792764
Flag: EXEMPT no-op
Test: TH
Change-Id: I3820e2464337778d7597cc4e1842fd0d8eb11045

Motomu Utsumi   commited 2025-07-23 12:04:50 +09:00
8645b6e0dc
NetBpfLoad: Extract map type sanitization into a helper function
Bug: 428792764
Flag: EXEMPT logging only change
Test: TH
Change-Id: Ibb1f1ea1ccaacd6e49ae525ddee398ff405b3d9f

Motomu Utsumi   commited 2025-07-18 10:14:37 +09:00
c80abe1bce
NetBpfLoad: Extract prog validation into a helper function
Bug: 428792764
Flag: EXEMPT no-op
Test: TH
Change-Id: I1007f4f365580ccc75015a85c3ef545bfddc7f24

Motomu Utsumi   commited 2025-07-18 10:11:32 +09:00
1d25bb35f2
NetBpfLoad: Extract prog pinning logic into a helper function
Bug: 428792764
Flag: EXEMPT no-op
Test: TH
Change-Id: I51c25918bec3951afca3401de8e2f0ff62ced684

Motomu Utsumi   commited 2025-07-18 10:11:26 +09:00
597f3afeb4
NetBpfLoad: Move selinux_context logging for prog
This is to prepare for splitting out the method for pinning a prog.

Bug: 428792764
Flag: EXEMPT logging only change
Test: TH
Change-Id: Iedfc8f3941f8e8b0e70e4dfbc47520b29d332c3e

Motomu Utsumi   commited 2025-07-18 10:11:21 +09:00
1e0675c0ce
NetBpfLoad: Extract map pinning logic into a helper function
Bug: 428792764
Flag: EXEMPT no-op
Test: TH
Change-Id: I4124756f07f5f0a3be9946b8a97faf7669f1615f

Motomu Utsumi   commited 2025-07-18 10:11:15 +09:00
77b0b25376
NetBpfLoad: Move selinux_context logging for map
This is to prepare for splitting out the method for pinning a map.

Bug: 428792764
Flag: EXEMPT logging only change
Test: TH
Change-Id: Iebaeea3150a99e85c25bb106e74ce3fc7f9ddb50

Motomu Utsumi   commited 2025-07-18 10:11:09 +09:00
4e024762a0
NetBpfLoad: Add libbpf-style map definitions for libbpf support
This updates the BPF map macros so programs can be loaded by both the
existing code and the libbpf.
The macros now generate ".maps" section in addition to the
".android_maps" section.

To prevent name collisions, the legacy map definitions are suffixed with
_def. This CL also adds the readMapNames method to get map names from
the ".android_maps" section, allowing the existing code path to continue
working.

Bug: 428792764
Flag: EXEMPT no-op
Test: TH
Change-Id: Ibe6f100b5684bc85d2cf9c100f2431740fa516ca

Motomu Utsumi   commited 2025-07-18 10:11:02 +09:00
e5dcaf78e3
NetBpfLoad: Remove unnecessary reuse flag in map creation
The reuse boolean was set to true only in a code path that
immediately called abort().

Bug: 428792764
Test: TH
Flag: EXEMPT no-op
Change-Id: I157e9f09c87eedc82c24d4ec821dcb132a389e31

Motomu Utsumi   commited 2025-07-18 10:10:47 +09:00
04de3498c7
NetBpfLoad: templatize readSectionByName
This is a cherry-pick of ag/34620710 to NetBpfLoad

Test: TH
Flag: EXEMPT no-op
Bug: 428792764
Change-Id: I651d7661e0a1a4dd4aa23a116ee2e211a31505f0

Motomu Utsumi   commited 2025-07-17 08:51:27 -07:00
99a7b7388e
NetBpfLoad: Refactor map/prog data copying to use a single memcpy
This is a no-op because the data in the ".android_maps"/"progs" section
is an array of struct bpf_map_def/bpf_prog_def.

Test: TH
Flag: EXEMPT no-op
Bug: 428792764
Change-Id: I80bd8c72c68b5359cf1b16ed5d4739a854169d7f

Motomu Utsumi   commited 2025-07-17 08:50:51 -07:00
8073ad8606
NetBpfLoad: less copies
This is a cherry-pick of ag/34620709 to NetBpfLoad

Test: TH
Flag: EXEMPT no-op
Bug: 428792764
Change-Id: Ia5aaa30cab71a2051b6c787824798958f2dee3f5

Motomu Utsumi   commited 2025-07-17 08:49:51 -07:00
1ae10a006e
NetBpfLoad: don't print multiline log_buf on user builds
These tend to be huge (because they can contain the full bpf disassembly)
but not very worthwhile, because they often result in logs being rate
limitted, and thus we miss out on the useful information.

This is particularly true for user builds on non-Google devices
installing the production signed tethering apex mainline module.

We're better off seeing more of the other log lines...

Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie25a60c2e602772ab0de2050e7964f0202ff2685

Maciej Żenczykowski   commited 2025-07-15 15:00:25 -07:00
f125992b27
bpf: remove zero uid hack
Not needed any more, since no more loader vs .o version skew.

Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I6c0b9ff2683809f3e732ec950bc91cf87040642f

Maciej Żenczykowski   commited 2025-07-15 14:46:10 -07:00
cb0433611e
bpf: rename 'maps' section to '.android_maps'
For libbpf compatibility we will want to have a 'normal' looking
.maps section & an android-only .android_maps section.

Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9114d27c408cb084626ec1151826ae722797296f

Maciej Żenczykowski   commited 2025-07-15 14:43:49 -07:00
b27d2137d6
NetBpfLoad: remove support for .o wide bpfloader_{min,max}_ver
This is not needed, as this is now simply hard coded (along with the
.o names themselves) inside of NetBpfLoad.cpp itself - see loadAllObjects().

Even if it wasn't, we're better off using the per-map & per-program
limits instead.  These will already cause all maps/programs to be skipped...

(The only reason to have a global per .o one was to solve
.o vs loader version skew issues, which we can no longer have)

Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I8e8f62ed3534d54dd3382df53637f76bb72dab5e

Maciej Żenczykowski   commited 2025-07-15 14:40:11 -07:00
c4a1cae939
Enable BTF for BPF map on 4.19+ kernel
Flag: EXEMPT mainline
Test: Boot on 4.14 and BTF is disabled
Test: Boot on 4.19 and BTF is enabled
Bug: 429278733
Change-Id: Icdb85891c3d70f1f1949c5b6f5f82ed6bb862873

Motomu Utsumi   commited 2025-07-03 15:42:32 +09:00
4a3b537580
NetBpfLoad: create/populate/pin kernel bugs bpf array
This is an array of two ints:
- index 0 should have value 0
  (a value of 1 implies a kernel uapi bug)
- index 1 should have value 123
  (a value of 0 implies bpf array writes at non-zero index fail)

Note that these bugs can only be present on Android S & Sv2,
as these kernel bugs are considered fatal on Android T+.

(btw. we need to do this write manually as we cannot write into
maps we pin and then reopen - due to sepolicy)

Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9b2ada7f3e299014d39461a8d0718396daf0c91a

Maciej Żenczykowski   commited 2025-06-17 01:49:23 -07:00
83d5ad1d85
NetBpfLoad: workaround for Xiaomi kernel uapi bug
See https://b.corp.google.com/issues/414570528#comment96
which can be summarized as
https://github.com/MiCode/Xiaomi_Kernel_OpenSource/blob/vayu-r-oss/include/uapi/linux/bpf.h has:

enum bpf_cmd {
  BPF_MAP_CREATE,          <-- UAPI 0
  BPF_MAP_LOOKUP_ELEM,     <-- UAPI 1
  BPF_MAP_UPDATE_ELEM,     <-- UAPI 2
  BPF_MAP_DELETE_ELEM,     <-- UAPI 3
  BPF_MAP_GET_NEXT_KEY,    <-- UAPI 4
  BPF_PROG_LOAD,           <-- UAPI 5
  BPF_OBJ_PIN,             <-- UAPI 6
  BPF_OBJ_GET,             <-- UAPI 7
  BPF_PROG_ATTACH,         <-- UAPI 8
  BPF_PROG_DETACH,         <-- UAPI 9
  BPF_GET_COMM_HASH,       <-- breaks Linux uapi (uses 10)
  BPF_PROG_TEST_RUN,       <-- UAPI 10 becomes 11 (aka. BPF_PROG_RUN)
  BPF_PROG_GET_NEXT_ID,    <-- UAPI 11 becomes 12
  BPF_MAP_GET_NEXT_ID,     <-- UAPI 12 becomes 13
  BPF_PROG_GET_FD_BY_ID,   <-- UAPI 13 becomes 14
  BPF_MAP_GET_FD_BY_ID,    <-- UAPI 14 becomes 15
  BPF_OBJ_GET_INFO_BY_FD,  <-- UAPI 15 becomes 16
};

this addition in the middle of the userspace api enum,
causes all following enum values to be greater by one.

In particular this increases the value of
BPF_OBJ_GET_INFO_BY_FD from 15 to 16, thus break it.

It also causes UAPI 12 (BPF_MAP_GET_NEXT_ID)
to actually execute BPF_PROG_GET_NEXT_ID (normally UAPI 13)

As such this can be detected via finding the index of
the last map (on buggy kernels this will actually find
the index of the last program), then creating a map,
then checking if the last map index has changed
(on buggy kernels this will not be the case because
we did not create another program).

Bug: 414570528
Test: TreeHugger
Flag: EXEMPT mainline
Change-Id: Iecad121ac624df2d123a13e90a46792bd567a4c6
Signed-off-by: Maciej Żenczykowski <maze@google.com>

Maciej Żenczykowski   commited 2025-06-10 02:43:28 -07:00
f7eb2bf2bc
NetBpfLoad: add self-check for bpfGetNext{Prog,Map}Id()
This detects some kernel bugs, particularly on Xiaomi devices.

Bug: 414570528
Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic099c53dc1ad5f01bfebfddd29b1024f5b936fe5

Maciej Żenczykowski   commited 2025-06-09 01:14:56 -07:00
18523cb5f3
NetBpfLoad: Add errno to log for map verification failures
We sometimes see issues when trying to load BPF maps, it's helpful to know
what errno was set.

Bug: 414570528
Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I8b78ee17ebad076d073d50bfd0f01440fe7187a2

Maciej Żenczykowski   commited 2025-05-29 01:25:26 -07:00
4b3937b7f3
Fix C++23 build: add missing #include.
Change-Id: I781654e989d609df375cdbe4fa4c2175c551e84d

Elliott Hughes   commited 2025-05-22 16:37:33 -04:00
cd7f3bffd6
NetBpfLoad: Remove ignore_on_* directives
`ignore_on_*` directives haven't been used in a while and require a special version of bpfloader. Remove them.

While doing so, pad the `map_def` and `prog_def` structs, to maintain existing size.

Note: The mainline shipped bpf .o files are now in a location
that no platform bpfloader ever looked/looks at.
As such maintaining .o format compatibility with older release
is not needed.

Test: TreeHugger
Flag: EXEMPT dead code removal / mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I485c9645bdece2ad27add08347688d0f1c93e5d2

Maciej Żenczykowski   commited 2025-05-20 15:02:15 -07:00
41d8d63356
Merge "NetBpfLoad: abort() on ignore_on_*" into main
Maciej Żenczykowski   authored and   Android (Google) Code Review   commited 2025-05-20 14:17:49 -07:00
aeb8fcc185
Merge "NetBpfLoad: abort if pin_subdir is specified" into main
Treehugger Robot   authored and   Android (Google) Code Review   commited 2025-05-20 14:17:43 -07:00
2be0eec88c
NetBpfLoad: abort() on ignore_on_*
should be unused

Test: TreeHugger
Flag: EXEMPT dead code
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ice72d940dd3492d2b9e14dd32390a49f87bd54d1

Maciej Żenczykowski   commited 2025-05-20 08:45:15 -07:00
e445d25af7
NetBpfLoad: crash if we are reusing a map.
This is to identify if it actually happens.

It is expected not to happen, because we now pin every map and are run only once on boot.

Test: TreeHugger
Flag: EXEMPT dead code
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie46ace40f20fb4b061a91cd53f8256caebdfa71f

Maciej Żenczykowski   commited 2025-05-20 06:41:18 -07:00
b71cd4f8cf
NetBpfLoad: abort if pin_subdir is specified
This code should be dead. Abort to see whether it is.

Test: TreeHugger
Flag: EXEMPT dead code
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib5309ff683339e70bc44e135578ede75548b022d

Maciej Żenczykowski   commited 2025-05-20 06:38:57 -07:00
10979ef9ec
NetBpfLoad: disallow 32-bit userspace on 6.13+ kernels
This is basically a no-op as 32-bit netd already fails on android-mainline @ 6.13 (we're now at 6.14, 6.15 is coming in a few weeks).
I know this because we had to fix ACK 6.12, and didn't port it forward.

Note: AFAIK TV is planning to release on 25Q2 and then 27Q2,
so this 26Q2+ doesn't actually affect them till a year later in 27Q2.

Modern arm cores are dropping 32-bit cpu instruction decode support.
This already happened on Pixel 8+ (Tensor G3+) and is soon coming
to the GCE cloud machines underpinning our cuttlefish VMs
(this is very much an industry wide trend for modern aarch64 cores,
I believe all new application cores since 2023 have been 64-bit only).

Pixel 6a is AFAIK the last pixel with 32-bit application support,
and that is reaching EOL approximately in July 2027.
(while most test coverage is on Pixel 6/6pro which EOLs in Oct 2026)

Note though that this isn't a great example: AFAIK all Pixels
have had 64-bit system userspace (which is what we care about here)
The transition happened in the final Nexus, the year before Pixel 1.

This means we're going to start losing *any* remaining vestiges
of test coverage that we still (might) have.

Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie8a5a5dafd2f0e20ef4651c3a69957fde374ae4b

Maciej Żenczykowski   commited 2025-05-17 06:00:03 -07:00
1da1945e26
NetBpfLoad: eliminate directory scanning
The full set of 5 .o's is known at build time, and basically almost
never changes (and indeed is likely to shrink, with clatd.o merged
into offload.o && dscpPolicy.c merged into netd.c).

We might as well just code this knowledge straight into the mainline
loader.

This also allows us to move all the files to an apex subdirectory
that old platform bpfloaders don't even look at, which eliminates
the need for any backward compatibility with them.

Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie3d27178c31a68ed7cf5173b5983ad3009a3cf94

Maciej Żenczykowski   commited 2025-05-15 01:38:16 -07:00
82ce2ca21c
NetBpfLoad: (noop) simplify /sys/fs/bpf dir creation
Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iee3b3a82d3ecb233956cedb2f81a591eed43dc69

Maciej Żenczykowski   commited 2025-05-13 01:59:07 -07:00
57de4bfef0
Merge "NetBpfLoad: Require Kernel 5.10 for Android 25Q4" into main
Maciej Żenczykowski   authored and   Android (Google) Code Review   commited 2025-05-06 09:27:48 -07:00
0d528a50c2
Merge "NetBpfLoad: remove spurious S+ check." into main
Treehugger Robot   authored and   Android (Google) Code Review   commited 2025-05-06 09:25:39 -07:00
119d6d1fb9
NetBpfLoad: Require Kernel 5.10 for Android 25Q4
Netd bpf requires 5.10 at least in 25Q4.
This CL enforces that.

Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I76e52b2a2702a2a6ce761a947a0749ea798778b6

Maciej Żenczykowski   commited 2025-05-06 05:28:23 -07:00
ec1115e46b
NetBpfLoad: remove spurious S+ check.
Since all mainline bpf code is now loaded by NetBpfLoad, compatibility with platform bpfloader simply doesn't matter any more.

Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Id96e3e6a4dac485f3e707cc9e6c4b05a4f9389d7

Maciej Żenczykowski   commited 2025-05-06 02:59:51 -07:00
66893bffe5
bpf: Add new SDK versions
Add 25Q3, 25Q4, 26Q1 and 26Q2 SDKs to the bpfloader helpers. The Q3 and
beyond versions are not finalized, so they are not being populated in
BpfUtils.h yet, but at least the logic is in place.

Test: TreeHugger
Flag: EXEMPT test code
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I6ea12e347462be2c8258a4fe5bc994b35d4065a5

Maciej Żenczykowski   commited 2025-05-06 02:55:33 -07:00
c5b9f5ee99
NetBpfLoad: relax version checks
Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie4ac3608d3f9599ffca40b0b2073f21efed7b17c

Maciej Żenczykowski   commited 2025-05-01 06:12:57 -07:00
cb55572675
Fix memory leak in NetBpfLoad
The lambda was capturing btf by value instead of reference. This meant
that btf__free was never called, because at the time of capture btf was
null.

This changes the lambda to capture btf by reference instead of by value.

Test: TH
Flag: EXEMPT mainline
Bug: 411323352
Change-Id: I6c6637b10f16d48f9ae5b702e44883046b46e252

Patrick Rohr   commited 2025-04-17 08:38:01 -07:00
03b974321b
NetBpfLoad: remove dead code
This code requires
  !isArm()
  isUserspace32bit()
  isAtLeastKernelVersion(6, 7, 0)
which implies
  isAtLeastKernelVersion(6, 2, 0)
meaning we match the condition in 1801,
and don't match the isArm() conditions in 1803/1806/1810,
thus we'd have already 'returned 1' in line 1815.

Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I234ba91b9bdbfeaf1077eac64248d3ebfe2f2cd2

Maciej Żenczykowski   commited 2025-04-15 13:48:19 -07:00
a627d77127
NetBpfLoad: don't fail on S on kernels with buggy bpf arrays
Bug: 404427531
Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib30a731d7faecfc7e52590e28350c3289fe26852

Maciej Żenczykowski   commited 2025-04-02 17:53:30 -07:00
b3ddc8afad
NetBpfLoad: allow 25Q3
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com
Change-Id: Iced07b6438b4702334aaf85d301987efefe153c6

Maciej Żenczykowski   commited 2025-03-25 13:05:44 -07:00
3ac88f375b
Merge "NetBpfLoad: 25Q2+ parse & verify config" into main
Patrick Rohr   authored and   Gerrit Code Review   commited 2025-03-21 12:16:53 -07:00
b6d94ad6f3
NetBpfLoad: 25Q2+ parse & verify config
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ibe7560998655dfe36b5cc071a3405650ad684935

Maciej Żenczykowski   commited 2025-03-20 23:47:59 -07:00
25e2622121
Skip enabling BTF on kernel older than 5.10
BTF_KIND_VAR and BTF_KIND_DATASEC were introduced in 5.2.
It should be possible to enable BTF on 4.18 or above since libbpf can
load BTF.
Also, current code should work on 5.4 but this is untested.
For now, this CL enables BTF on 5.10 or above.

Test: boot on 4.9, 4.19, 5.10
Test: adb logcat | grep NetBpfLoad
Change-Id: I3da4970464afcf9a76b1f2645ed79c44750bcd6a

Motomu Utsumi   commited 2025-03-21 15:42:45 +09:00
861f11c9db
Fix an issue caused by variable re-declaration in inner scope
Since btf is declared in inner scope, btf is null in outer scope and BTF
was always disabled.

This CL also makes NetBpfLoad buildable with -Wshadow flag

Test: adb log | grep NetBpfLoad
Change-Id: I511f21a3620ccfdadd797786e087d0d142c816ed

Motomu Utsumi   commited 2025-03-21 15:40:42 +09:00
62b1c88367
Fix unsigned num comparison with zero
Caught by -Wtautological-unsigned-zero-compare compiler flag

Test: m
Change-Id: Ibedc657a237ba29fd7a4452271f347cd3180f57c

Motomu Utsumi   commited 2025-03-21 11:00:10 +09:00
a6ffae202b
Skip enabling BTF on kernel older than 4.18
BPF_BTF_LOAD command was introduced in 4.18

Bug: 403381101
Test: boot on 4.9 kernel
Change-Id: Ibf2070750334417bd1052486c1f59a5d3f7fc20c

Motomu Utsumi   commited 2025-03-19 19:49:08 +09:00
6f1ceccf14