Commit graph android_packages_modules_Co.../bpf/syscall_wrappers
Message SHA1
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
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
BpfMap.h: consume() on 5.4+ use 'lookup & delete' kernel api
Reduces consume() from 3 (getNextKey, readValue, deleteValue)
to 2 (getNextKey, readAndDeleteValue) system calls per item.

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

Maciej Żenczykowski   commited 2025-08-11 15:12:52 -07:00
69513313aa
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
Add support to ctx in runProgram.
Programs of type raw_tp need the input to be passed in the ctx
instead of the data.

Test: with ag/33648187
Change-Id: I79ac0126d91c9eafd847a0c8709a47a0113a9430

Alessio Cosenza   commited 2025-05-29 16:10:22 +00:00
69a9bbcee0
BpfSyscallWrappers.h: fix to pass 'attr' by address instead of by const reference
(shot in the dark)

checking attr.info.info_len in line 265 could probably be optimized out if we pass by const reference, since one could assume it is still sizeof(info).

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

Maciej Żenczykowski   commited 2025-05-29 02:43:55 -07:00
f53f0f0e21
BpfSyscallWrapper: add bpfGetFd{Jit,Xlat}ProgLen
Available in Linux Kernel 4.14+

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I3e5a43ceece5826fc6019bd11df2bc5315fe405c

Maciej Żenczykowski   commited 2025-02-14 11:12:31 -08:00
fb98f05e7c
Use unique_fd for usableProgram
Makes 2 lines out of 4.

Test: TH
Change-Id: I5e2d047105065a189e6393a8f5fcb75ff42922ec

Patrick Rohr   commited 2024-11-22 11:01:58 -08:00
c8a9dc2f6e
Get rid of BPF_FD_JUST_USE_INT
borrowed_fd defines implicit conversion constructors for both int and
unique_fd.

Test: TH
Change-Id: Ie41c747f4afea050b18d721956bcbbca0446bdc7

Patrick Rohr   commited 2024-11-22 10:36:27 -08:00
99ace23829
bpf: git mv staticlibs/native/bpf_syscall_wrappers bpf/syscall_wrappers
Step 3 of moving bpf related stuff into a common directory,
will ease development.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9ec90e2f58c68e17d6e83b48b8dea2cdf31a3694

Maciej Żenczykowski   commited 2024-08-20 19:51:50 -07:00
d33d63701c