Commit graph android_packages_modules_Co.../bpf
Message SHA1
BpfMap: lower min allowed batch size
Vendor/OEM reports of some of their code failing to build due
to larger key/value sizes.

I'd make this check mainline only, but I can't seem to find
a way to actually make that (reliably) work.

So let's just lower the minimum from 256 to 64 - this should
I think still be enough for significant performance benefits
from using bulk operations.  Indeed probably even 10 would be
enough for that.

Bug: 442996907
Test: TreeHugger
Flag: EXEMPT mainline/trivial
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I69d53293dd5bb2174af7c814f531a08eba590310

Maciej Żenczykowski   commited 2025-09-11 13:01:08 -07:00
1476ec1141
NetBpfLoad: eliminate 'bpfCmdFixupIsNeeded'
It turns out this doesn't work anyway:

Both our Java and C++ higher level map opening logic on 4.14+
kernels will check key/value sizes match, which will fail due to
the kernel uapi mismatch.

(Note: that the fixup was only ever applied/enabled inside of
the NetBpfLoad process itself, not in any later .so/Java/JNI)

This removes the fixup and simply chooses to not even attempt to
load bpf.  This will result in the mainline bpf provided
maps and programs simply not existing.  This would/will cause
a failure of 'bpf_existence_test' - but who cares, so long
as things keep on booting.

We need to transition to libbpf, and clearly making this work
isn't worth the considerable effort it would be, since S ebpf
functionality (ie. tethering offload) hasn't worked on the
affected devices in years.

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

Maciej Żenczykowski   commited 2025-09-11 13:01:01 -07:00
db324dfbca
NetBpfLoad: move dir creation ahead of kernel uapi check
Test: TreeHugger
Flag: EXEMPT mainline/refactor
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I395d6f9abe0c0cbd8af662b1c94be16eeac4060b

Maciej Żenczykowski   commited 2025-09-11 12:04:56 -07:00
9611f93131
NetBpfLoad: reduce log spam
by joining two log lines into one with effectively the same info

(the kernel version is already printed during startup)

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

Maciej Żenczykowski   commited 2025-09-11 11:34:49 -07:00
ff505eaaea
NetBpfLoad: Fail boot if BTF load fails on 4.19+
libbpf ignores BTF load error but this causes boot failure only when BTF
is required for some reasons (e.g. SK_STORAGE).
This CL makes boot fails regardless of the necessity of BTF on 4.19+
kernel

Test: boot kernel on 4.14, 4.19, 5.10, 6.1
Flag: EXEMPT mainline
Change-Id: I0af1e7eedcbea803c22c064ff157700a01307691

Motomu Utsumi   commited 2025-09-04 04:59:00 -07:00
8f3cfccf27
tcpAccECN: Move static const variables to file scope
The symbol name of a static const variable defined within a function
includes a '.' and old kernel BTF ferifier rejects it.
"""
08-29 15:11:27.825     0     0 W         : c3   1287 NetBpfLoad: [377] STRUCT _rodata size=64 vlen=3
08-29 15:11:27.825     0     0 W         : c3   1287 NetBpfLoad: 	tcp_option type_id=343 bits_offset=0
08-29 15:11:27.825     0     0 W         : c3   1287 NetBpfLoad: 	schedcls_ingress_accecn_eth_.new_cnt type_id=346 bits_offset=128 Invalid name
"""

This verifier limitation was relaxed in later kernels by
https://lore.kernel.org/bpf/20230615145607.3469985-1-revest@chromium.org/

Test: Boot on 4.19 kernel && logcat
Bug: 440448408
Bug: 442710894
Flag: EXEMPT tcpAccECN program is not used
Change-Id: If3c993bb6abb1ff8688932d682d909d335f84ca2

Motomu Utsumi   authored and   Maciej Żenczykowski   commited 2025-09-04 04:50:13 -07:00
937c57a2f8
tcpAccECN: Fix counter order in tcp option
Counter order in AccECN options is the following

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Kind = 174   |  Length = 11  |          EE1B field           |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| EE1B (cont'd) |           ECEB field                          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                  EE0B field                   |             Order 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Test: TH
Bug: 440448408
Flag: EXEMPT tcpAccECN program is not attached
Change-Id: I7d76d40f7499e44c89c8241919a0d3c416b0ce00

Motomu Utsumi   commited 2025-08-29 16:27:29 +09:00
2977062223
tcpAccECN: Fix to increment ceb counter
Before this CL, if the ip_ecn is 0b11, program returns without updating
byte_count->ceb

Test: TH
Bug: 440448408
Flag: EXEMPT tcpAccECN program is not used
Change-Id: I37aeddcb6807c43787b4f3ca0db26f47ffe190df

Motomu Utsumi   commited 2025-08-28 17:49:54 +09:00
de403d30e4
bpf/progs/tcpAccECN.c: use static const for another local constant
Likely not needed for kernel verifier happiness,
since even 1 'static' constant prevents .rodata from being
entirely empty/automatic and thus BTF-less.

Trivial follow up to go/ag/35341088

Bug: 441356399
Bug: 441436401
Test: TreeHugger
Flag: EXEMPT mainline/bug fix
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I396d1a4f2d47225707348f7c7dc3548cbba125a0

Maciej Żenczykowski   commited 2025-08-27 23:01:24 -07:00
4203f5008e
bpf/progs/tcpAccECN.c: use static const for local constants
loading BTF fails on 5.10 for tcpAccECN due to https://lore.kernel.org/bpf/20210119153519.3901963-1-yhs@fb.com

We're hitting the same problem on 5.10:
  ...
  NetBpfLoad: [381] DATASEC .rodata size=40 vlen=0 vlen == 0
  ...
  NetBpfLoad: libbpf: Error loading .BTF into kernel: -22. BTF is optional, ignoring.
  NetBpfLoad: libbpf: map 'sk_storage': failed to create: Invalid argument(-22)
  NetBpfLoad: libbpf: failed to load object '/apex/com.android.tethering/etc/bpf/mainline/netd.o'
  NetBpfLoad: Failed to load object: /apex/com.android.tethering/etc/bpf/mainline/netd.o, libbpf: 1
  NetBpfLoad: === CRITICAL FAILURE LOADING BPF PROGRAMS ===

so BTF fails, but SK_STORAGE *requires* BTF.

This is a kernel BTF verified failure fixed in later kernels.

step 1. Adjust the bpf helper definitions to more closely match kernel uabi in include/uapi/linux/bpf.h:
 * long bpf_sock_ops_cb_flags_set(struct bpf_sock_ops *bpf_sock, int argval)
 * long bpf_reserve_hdr_opt(struct bpf_sock_ops *skops, u32 len, u64 flags)
 * long bpf_store_hdr_opt(struct bpf_sock_ops *skops, const void *from, u32 len, u64 flags)

(some for consistency, but what matters is the 'const' addition)

Dump current .rodata via:
  adb pull /apex/com.android.tethering/etc/bpf/mainline/netd.o && llvm-objdump -s -j .rodata netd.o
  m netd.o && llvm-objdump -s -j .rodata /android/out/soong/.intermediates/packages/modules/Connectivity/bpf/progs/netd.o/android_common/obj/unstripped/packages/modules/Connectivity/bpf/progs/netd.o

.../netd.o:  file format elf64-bpf
Contents of section .rodata:
 0000 ae0b0000 00000000 00000000 00000000  ................
 0010 00000000 00000000 01000000 00000000  ................
 0020 01000000 00000000                    ........

which lines up with:

  #define CUSTOM_TCP_OPTION_KIND 174  // 0xAE
  #define CUSTOM_TCP_OPTION_SIZE 11   // 0x0B
  #define TCPHDR_SYN 0x02

            struct {
                __u8 kind;
                __u8 length;
                __u8 data[CUSTOM_TCP_OPTION_SIZE - 2];
            } __attribute__((packed)) tcp_option = {
                .kind = CUSTOM_TCP_OPTION_KIND,    // 0xAE
                .length = CUSTOM_TCP_OPTION_SIZE,  // 0x0B
                .data = {0},                       // 9 bytes
            };                                     // 5 byte alignment pad

and

  typedef struct{
    uint64_t ceb;
    uint64_t e0b;
    uint64_t e1b;
  } EcnByteCounters;
  STRUCT_SIZE(EcnByteCounters, 8 * 3);  <-- 24 bytes

  {
                        EcnByteCounters new_cnt = {
                            .ceb = 0,
                            .e0b = 1,
                            .e1b = 1,
                        };
                        bpf_l4s_accecn_byte_map_update_elem(&flow_key, &new_cnt, 0);
  }

Which matches the final 24 bytes.

So we convert those to 'static const' which hopefully will get them debug info.
(following the comment in the last line of the above linked patch)

Bug: 441356399
Bug: 441436401
Test: TreeHugger
Flag: EXEMPT mainline/bug fix
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I7d871c54ecccf2abc7834fde3c63c073cad29004

Maciej Żenczykowski   commited 2025-08-27 14:35:43 -07:00
157e8cd78b
tcpAccECN: increase min required kernel to 6.1
Appear to be failing on pixel 7 U (which is 5.10)

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

Maciej Żenczykowski   commited 2025-08-27 13:06:28 -07:00
74d3460f5d
Merge "bpf_existence_test: remove support for Android R" into main
Maciej Żenczykowski   authored and   Android (Google) Code Review   commited 2025-08-27 04:22:27 -07:00
717a18c65c
Merge "bpf: build tcpAccECN into netd.o" into main
Maciej Żenczykowski   authored and   Android (Google) Code Review   commited 2025-08-26 13:00:18 -07:00
b21ed8fb38
Merge "netbpfload.35rc: grant capability SYSLOG if available" into main
Maciej Żenczykowski   authored and   Android (Google) Code Review   commited 2025-08-26 11:32:13 -07:00
439770c5e2
bpf: build tcpAccECN into netd.o
Note that currently bpfloader is *still* always <25Q4 (and thus <26Q2).

With this change:
  $ adb shell find /sys/fs/bpf/ | egrep -i tcp
is empty.

With the 26Q2 changed to 25Q3 it is not:
  $ adbz shell find /sys/fs/bpf/ | egrep -i tcp
  /sys/fs/bpf/netd_shared/prog_tcpAccECN_schedcls_egress_accecn_rawip
  /sys/fs/bpf/netd_shared/prog_tcpAccECN_schedcls_ingress_accecn_rawip
  /sys/fs/bpf/netd_shared/prog_tcpAccECN_schedcls_egress_accecn_eth
  /sys/fs/bpf/netd_shared/prog_tcpAccECN_schedcls_ingress_accecn_eth
  /sys/fs/bpf/netd_shared/prog_tcpAccECN_sockops_accecn_option
  /sys/fs/bpf/netd_shared/map_tcpAccECN_l4s_accecn_mss_map
  /sys/fs/bpf/netd_shared/map_tcpAccECN_l4s_accecn_byte_map
  /sys/fs/bpf/netd_shared/map_tcpAccECN_l4s_accecn_ce_map

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

Maciej Żenczykowski   commited 2025-08-26 09:59:20 -07:00
23057f4db5
bpf_existence_test: remove support for Android R
As of M-2025-11 mainline is only being pushed to S+

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

Maciej Żenczykowski   commited 2025-08-26 09:56:45 -07:00
1780108641
tcpAccECN: Add to the dependency of the tethering apex
This CL also sorts bpf targets in Android.bp

Test: m
Bug: 440448408
Flag: EXEMPT tcpAccECN is not used
Change-Id: I4a662870333f3da02d4d6cfcb3fca8c0af452d34

Motomu Utsumi   commited 2025-08-26 10:21:53 +09:00
95ab63a373
Merge "NetBpfLoad: use macro for mainline beta flag" into main
Maciej Żenczykowski   authored and   Android (Google) Code Review   commited 2025-08-25 10:54:37 -07:00
c528cbf687
Merge "tcpAccECN: remove duplicated definitions of load_word and load_half" into main
Maciej Żenczykowski   authored and   Android (Google) Code Review   commited 2025-08-25 10:52:10 -07:00
bfd1b36ea0
Merge "tcpAccECN: add build target" into main
Maciej Żenczykowski   authored and   Android (Google) Code Review   commited 2025-08-25 10:51:00 -07:00
96694e36ca
Merge "tcpAccECN: update DEFINE_BPF_PROG to follow the current syntax" into main
Maciej Żenczykowski   authored and   Android (Google) Code Review   commited 2025-08-25 10:50:16 -07:00
0562b55916
Merge "tcpAccECN: remove CRITICAL" into main
Maciej Żenczykowski   authored and   Android (Google) Code Review   commited 2025-08-25 10:49:28 -07:00
e77c943066
Merge "tcpAccECN: define required macros" into main
Maciej Żenczykowski   authored and   Android (Google) Code Review   commited 2025-08-25 10:49:01 -07:00
c36345cfdf
Merge "tcpAccECN: remove carriage returns" into main
Maciej Żenczykowski   authored and   Android (Google) Code Review   commited 2025-08-25 10:48:05 -07:00
23215028c4
Merge "NetBpfLoad: Add default map flag for BPF_MAP_TYPE_LRU_HASH" into main
Maciej Żenczykowski   authored and   Android (Google) Code Review   commited 2025-08-25 10:47:16 -07:00
2de52a3d0b
NetBpfLoad: use macro for mainline beta flag
Flag: com.android.tethering.readonly.flags.use_libbpf
Bug: 428792764
Test: Enable/Disable flag, boot, adb shell logcat
Change-Id: I40e6124f6c96cf09ce50f38d786f15847ec6f69d

Motomu Utsumi   commited 2025-08-25 20:54:37 +09:00
aa8717db44
tcpAccECN: remove duplicated definitions of load_word and load_half
bpf_helpers.h has the same definitions

Test: m tcpAccECN.o
Bug: 440448408
Flag: EXEMPT tcpAccECN is not used
Change-Id: Ie5b47bdcbc2aa8d4563cf3ec0806f8dfc6861801

Motomu Utsumi   commited 2025-08-25 16:10:56 +09:00
5cb1506a75
tcpAccECN: add build target
Test: m tcpAccECN.o
Bug: 440448408
Flag: EXEMPT tcpAccECN is not used
Change-Id: Id8f5e3cfb55b3b698b496dbb4beee857a5a4c87a

Motomu Utsumi   commited 2025-08-25 16:02:04 +09:00
86a3a4a7ca
tcpAccECN: update DEFINE_BPF_PROG to follow the current syntax
This CL also sets the min kver to 5.10.
skb_tcp_flags field in bpf_sock_ops was introduced on 5.10 kernel

Test: TH
Bug: 440448408
Flag: EXEMPT tcpAccECN is not used
Change-Id: I71696e0bf3b36296c5f2e7fd5b692406d96ddf97

Motomu Utsumi   commited 2025-08-25 13:04:48 +09:00
bf24fdbcc2
tcpAccECN: remove CRITICAL
CRITICAL section was removed by ag/33605170

Test: TH
Bug: 440448408
Flag: EXEMPT tcpAccECN is not used
Change-Id: I6de89edd7547ede1ecdd18b5ee80fb9bc1977915

Motomu Utsumi   commited 2025-08-25 12:55:14 +09:00
9d5f30d8d6
tcpAccECN: define required macros
Test: TH
Bug: 440448408
Flag: EXEMPT tcpAccECN is not used
Change-Id: I9fbbd03d5bf2d6955082dfdb6f7e9279ad840b61

Motomu Utsumi   commited 2025-08-25 12:55:14 +09:00
4f585bdfac
tcpAccECN: remove carriage returns
$ sed -i 's/\r$//g' bpf/progs/tcpAccECN.c
$ sed -i 's/\r$//g' bpf/progs/tcpAccECN.h

Test: TH
Bug: 440448408
Flag: EXEMPT tcpAccECN is not used
Change-Id: Iffde87607e95920dc839e6901e0ba40912f83a49

Motomu Utsumi   commited 2025-08-25 12:55:14 +09:00
c2c0572ae8
NetBpfLoad: Add default map flag for BPF_MAP_TYPE_LRU_HASH
Test: TH
Bug: 440448408
Flag: EXEMPT mainline
Change-Id: I46cec4837528e1f3e66738e2d9ae85e3ea012f98

Motomu Utsumi   commited 2025-08-25 12:55:10 +09:00
aadd755e55
NetBpfLoad: use libbpf based on mainline beta flag
This CL did not change the binary and library size

Before this CL
"""
$adb shell ls -l /apex/com.android.tethering/bin/netbpfload
-rwxr-x--- 1 root root 102744 1970-01-01 09:00 /apex/com.android.tethering/bin/netbpfload
$adb shell ls -1 /apex/com.android.tethering/lib64 | wc -l
21
"""

After this CL
"""
$adb shell ls -l /apex/com.android.tethering/bin/netbpfload
-rwxr-x--- 1 root root 102744 1970-01-01 09:00 /apex/com.android.tethering/bin/netbpfload
$adb shell ls -1 /apex/com.android.tethering/lib64 | wc -l
21
"""

Flag: com.android.tethering.readonly.flags.use_libbpf
Bug: 428792764
Test: Enable/Disable flag, boot, adb shell logcat
Change-Id: Ie8d94ab00d8b32f5062265c6b9696feb77a0f0b1

Motomu Utsumi   commited 2025-08-22 22:46:24 +09:00
5a3103ba1e
Merge "NetBpfLoad: expose kernel bug BPF map to NetworkStack" into main
Maciej Żenczykowski   authored and   Android (Google) Code Review   commited 2025-08-20 01:32:12 -07:00
cf94760d68
NetBpfLoad: expose kernel bug BPF map to NetworkStack
This change grants the NetworkStack process read access to the existing
'map_kernel_bugs' BPF map. This map, created by NetBpfLoad,
allows the kernel to signal known bugs to userspace.

The primary modification is applying the correct permissions
('chown'/'chmod') so the NetworkStack can query the map.

The map is pinned to `/sys/fs/bpf/tethering/map_kernel_bugs` and its
permissions are set (root:network_stack, 0640) for access by network_stack
```
$ adb shell ls -lZ /sys/fs/bpf/tethering/
total 0
-r--r----- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 map_kernel_bugs
-rw-rw---- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 map_offload_tether_dev_map
-rw-rw---- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 map_offload_tether_downstream4_map
-rw-rw---- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 map_offload_tether_downstream64_map
-rw-rw---- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 map_offload_tether_downstream6_map
-r--r----- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 map_offload_tether_error_map
-rw-rw---- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 map_offload_tether_limit_map
-rw-rw---- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 map_offload_tether_stats_map
-rw-rw---- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 map_offload_tether_upstream4_map
-rw-rw---- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 map_offload_tether_upstream6_map
-rw-rw---- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 map_test_bitmap
-rw-rw---- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 map_test_tether2_downstream6_map
-rw-rw---- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 map_test_tether3_downstream6_map
-rw-rw---- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 map_test_tether_downstream6_map
-r--r----- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 prog_offload_schedcls_tether_downstream4_ether
-r--r----- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 prog_offload_schedcls_tether_downstream4_rawip
-r--r----- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 prog_offload_schedcls_tether_downstream6_ether
-r--r----- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 prog_offload_schedcls_tether_downstream6_rawip
-r--r----- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 prog_offload_schedcls_tether_upstream4_ether
-r--r----- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 prog_offload_schedcls_tether_upstream4_rawip
-r--r----- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 prog_offload_schedcls_tether_upstream6_ether
-r--r----- 1 root network_stack u:object_r:fs_bpf_tethering:s0  0 2025-08-18 04:04 prog_offload_schedcls_tether_upstream6_rawip
```

Flag: EXEMPT mainline
Bug: 430428550
Test: Manually check `dumpsys tethering bpf` after using bpftool on the map
Change-Id: Ie0fdaeea85cc9de45a42fd15a714a49ec9f3049d

Jimi Chen   commited 2025-08-19 22:44:49 -07:00
579e85b8d1
netd_updatable_unit_test: set min_sdk_version=33
netd_updatable was added during T mainline effort.

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

Maciej Żenczykowski   commited 2025-08-18 15:59:14 -07:00
a11e26a539
libbpf_android_test: set min_sdk_version=30
Not clear what the true minimum for this should be...
30/R? 31/S? 33/T?

So let's just err on the side of 30/R for now.

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

Maciej Żenczykowski   commited 2025-08-18 15:18:35 -07:00
e18a04463d
NetBpfLoad: disable BTF when BPF command fixup is needed.
Should fix observed failure on a Xiaomi S device with 4.19 kernel:
  I NetBpfLoad: NetBpfLoad v0.42 (/apex/com.android.tethering/bin/netbpfload) api:31/31 kver:413009d (64-on-aarch64) libbpf: v1.4 uid:1000 rc:10
  I NetBpfLoad: Tethering APEX version 360911260
  E NetBpfLoad: bpfGetNextMapId(final 15) returned 0 errno 2
  W NetBpfLoad: Enabling bpfCmdFixupIsNeeded.
  ...
  W NetBpfLoad: libbpf: BTF loading error: -2
  E NetBpfLoad: btf__load_into_kernel failed, errno: 2
  E NetBpfLoad: Failed to create maps: (ret=-2) in /apex/com.android.tethering/etc/bpf/mainline/offload.o
  E NetBpfLoad: Failed to load object: /apex/com.android.tethering/etc/bpf/mainline/offload.o, ret: No such file or directory
  E NetBpfLoad: === CRITICAL FAILURE LOADING BPF PROGRAMS ===

(errno 2 is ENOENT)

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

Maciej Żenczykowski   commited 2025-08-18 12:56:44 -07:00
6f3b412e9b
NetBpfLoad: Update build constraints & comments.
Corrected the comment in Android.bp regarding the target SDK version. In NetBpfLoad.cpp, added specific handling for RiscV builds to account for its undefined minimum API level. Added an error to ensure NetBpfLoad is always compiled within an APEX. Clarified that `minSupportedKernelVer` must be 0 for APEX builds to enable runtime kernel version enforcement.

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

Maciej Żenczykowski   commited 2025-08-15 14:58:10 -07:00
95e4958769
Disable API level and kernel version static asserts for RISC-V.
(riscv isn't released, and thus is 10000)

Should fix:
  Broken build 13940537 on git_main-without-vendor on aosp_cf_riscv64_phone-aosp_current-userdebug

FAILED: ...
packages/modules/Connectivity/bpf/loader/NetBpfLoad.cpp:91:15: error: static assertion failed due to requirement '10000 == 30': NetBpfLoad must be compiled for 30/R
   91 | static_assert(__ANDROID_API__ == 30, "NetBpfLoad must be compiled for 30/R");
      |               ^~~~~~~~~~~~~~~~~~~~~
<built-in>:500:25: note: expanded from macro '__ANDROID_API__'
  500 | #define __ANDROID_API__ __ANDROID_MIN_SDK_VERSION__
      |                         ^
<built-in>:499:37: note: expanded from macro '__ANDROID_MIN_SDK_VERSION__'
  499 | #define __ANDROID_MIN_SDK_VERSION__ 10000
      |                                     ^
1 error generated.

Bug: 438908493
Test: TreeHugger
Flag: EXEMPT mainline & compile fix for unreleased RiscV
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I50ba1c3159c277fb8becb68220ba0885cf30e996

Maciej Żenczykowski   commited 2025-08-15 05:30:23 -07:00
8c3023ad3b
netbpfload.35rc: grant capability SYSLOG if available
(needed for eBPF CO-RE ksyms relocation resolution)

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

Maciej Żenczykowski   commited 2025-08-14 20:10:43 -07:00
586562308f
KernelUtils: automatically assume 5.10 kernel on platform
We're adding a lot of 'isAtLeastKernelVersion(x,y,z)' checks,
that would ideally compile time optimize out whenever possible.

Note: we should be able to bump the '__ANDROID_API__ == 30' assert
in NetBpfLoad to 31/S any day now, since technically tip-of-tree
only goes into M-2025-11 which requires S.  But none of the
prep work for that is ready yet.

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

Maciej Żenczykowski   commited 2025-08-14 20:10:43 -07:00
66d7b632b0
BpfMap.h: on 5.10+ use bulk bpf map api
for:
  forAll(f(K))
  forAll(f(K,V))
  consume(f(K,V))

Test: TreeHugger
  ct_m_boot && a_test BpfBitmapTest libnetworkstats_test libbpf_android_test netd_updatable_unit_test bpf_existence_test
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I6b4985796db0aa48ad16c153179cfd37682bb1c2

Maciej Żenczykowski   commited 2025-08-14 20:10:43 -07:00
7bfdb000c6
BpfSyscallWrappers: add helpers for BPF_MAP_LOOKUP{,_AND_DELETE}_BATCH
Test: TreeHugger
Flag: EXEMPT mainline/noop
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie7019b7c830c11f0886de990d03f22ec43f30e06

Maciej Żenczykowski   commited 2025-08-14 20:10:43 -07:00
a943e2bdde
BpfNetworkStats: introduce poor man's ifindex2name() cache
Uses 16KiB ram, only caches ifindex < 1024, but in practice
that should usually be the case, as these are assigned
in sequential order by the kernel.

Note that in progs/netd.c:86 we have:
  DEFINE_BPF_MAP_NO_NETD(iface_index_name_map, HASH, uint32_t, IfaceValue, 1000)
so stuff doesn't really work right once we go past 1000-ish
tracked interfaces...

(and worst case we're no worse off then we are now...)

Benefit should be significant as all our bpf map stats
dumping logic has to convert from ifindex to ifname
in order to strcmp against the interface we're trying to match,
ie. that's the first thing 3 of the 4 cases we support
do at the very top of their innermost loop.

This effectively eliminates 1 bpf() lookup systemcall, thus
getting us down to 2 (getNextKey + readValue/readValueAndDelete)

Note: the tests work because they're consistent in how they
map ifindex/ifname so they do not require cache clearing.

I believe we don't have to worry about threading, as
we're being called while traversing/consuming maps.
Thus I believe there must be locking external to us.

Also writes should be *super* uncommon, so even if this
were to run multithreaded, races should happen very rarely.
If they did, we'd just write in the same value twice (since
the mapping from ifindex->ifname should rarely if ever change)

Worst case once in a blue moon we'd fail to match an interface
that had *just* shown up (because of reading only half the name),
and we'd lose some stats...

(and since the entire value fits in 16 bytes, and thus in a single
cache line, in practice most cpus will probably write combine
and thus avoid a half write/read anyway, both x86-64 and aarch64
can potentially even do a 16-byte read/write in 1 instruction...
although unclear if that will happen, but worst case should be 2)

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

Maciej Żenczykowski   commited 2025-08-12 20:39:20 -07:00
6bd25b5fd4
Merge "bpf: Adding AccECN receiver support to TCP connections" into main
Maciej Żenczykowski   authored and   Android (Google) Code Review   commited 2025-08-12 18:44:48 -07:00
c125d064f0
bpf: Adding AccECN receiver support to TCP connections
To support L4S receiver functionality on TCP connections, AccECN
is implemented using eBPF programs. Eth and rawip packets are
handled by separate pairs of ingress and egress programs.
(cherry picked from https://partner-android-review.googlesource.com/q/commit:075c29658a91da2b0e9d36d871bd2403b74a4f69)

Flag: EXEMPT tcpAccECN is not used
Change-Id: Ib0a11880b3f2b1ea04e188fe33366cae9eabe898

k1.reddy   authored and   Maciej Żenczykowski   commited 2025-08-12 18:44:01 -07:00
1191b84fb4
KernelUtils: make failure to call & parse uname abort
In practice this is already the case... If uname() execution
fails, or we fail to parse the result, we won't pass the
4.9+ required check in NetBpfLoad, which is required on S+,
and mainline no longer supports anything <S anyway.

We don't require subversion as technically "4.9" (ie. 4.9.0)
is enough for almost all usecases, although in practice I would
expect there will always be an LTS subversion present.

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

Maciej Żenczykowski   commited 2025-08-11 21:27:04 -07:00
e9517aedd3
KernelUtils: mark kernelVer const
(this was basically an oversight, nothing should be setting this)

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

Maciej Żenczykowski   commited 2025-08-11 20:58:15 -07:00
168c6c90ab