Commit graph android_system_bpf/libbpf_android/include
Message SHA1
Merge "Add option to skip map/prog based on build type"
Maciej Żenczykowski   authored and   Gerrit Code Review   commited 2022-12-20 08:38:45 +00:00
4fba4f7a22
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: I981e3447b40751926cbfd1692e13772ec582d3d4

Ryan Zuklie   commited 2022-12-19 14:39:31 -08:00
ce89f50035
bpfloader - fix includes
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I3c46875a34b744c9ff0af2b52b63ecbc9db5c15e

Maciej Żenczykowski   commited 2022-12-19 19:14:33 +00:00
300c51fd4f
Merge "bpfloader: pass whole struct Location to loadProg()"
Maciej Żenczykowski   authored and   Gerrit Code Review   commited 2022-12-10 10:24:43 +00:00
b909fed62a
bpfloader: pass whole struct Location to loadProg()
Simplify the loadProg() interface by passing struct Location instead
of passing its fields as separate arguments. Move struct Location into
libbpf_android.h to accommodate the change.

Change-Id: I39834b2645d38ba4c2eb5ea901a3da0f56a1912c
Signed-off-by: Connor O'Brien <connoro@google.com>

Connor O'Brien   commited 2022-12-09 15:07:37 -08:00
6c0ce9f17a
Bpfloader - minimal fs_bpf_loader support
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I8090de18b50a717f77d3fddab62a611888aa1521

Maciej Żenczykowski   commited 2022-12-02 13:51:25 +00:00
9a2093d38c
bpfloader: support 'shared' maps and per-map/program selinux context
allow bpf .o files to specify desired selinux context

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

Maciej Żenczykowski   commited 2022-06-21 02:23:09 -07:00
4181713963
bpfloader: also load from /vendor/etc/bpf/
Allow vendors to use bpf programs, but limit to tracepoints
for now (other types of programs, for instance skfilter, aren't
safe to expose, because the kernel gives us limited ways to
control which resources can have BPF programs attached, and
some shared resources only support a single BPF program at an
attach point).

Bug: 140330870
Bug: 162057235
Test: install bpf program to /vendor/etc/bpf/ and use it.
Test: atest libbpf_load_test
Change-Id: I6c876fe52739c38db73689ffd784167e7d35d58a

Steven Moreland   commited 2022-02-09 00:59:37 +00:00
0f10f3fd9f
[NETD-BPF#14] Move BPF map definition and utils to frameworks/libs/net/
Move BPF map definition and utilities to a common place that easy to be
referenced from both mainline module and platform code.

Bug: 202086915
Test: m; flash; boot
Test: cd system/netd/ && atest
Test: cd packages/modules/Connectivity && atest
Test: m gpuservice_unittest libtimeinstate_test bpf_module_test
      CtsAppOpsTestCases libbpf_load_test VtsBootconfigTest
      vts_test_binary_bpf_module bpf_benchmark libbpf_load_test
      libbpf_android_test
Change-Id: Ib15cf78c2da97bff835fb406c866676eec77c013

Ken Chen   authored and   Maciej Żenczykowski   commited 2021-12-04 03:42:55 +00:00
b1d4888ec2
[NETD-BPF#10] Add WaitForProgsLoaded.h
Move waitForProgsLoaded() from libbpf_android.h to a single header file.
A transitional commit of separating headers, utilities from bfploader.

Bug: 202086915
Test: build; flash; cd system/netd; atest
Change-Id: Ief1bcd09e973f952c29859a7a8fba92c8626e32d

Ken Chen   commited 2021-12-03 08:16:24 +00:00
5b0fbc192e
[NETD_BPF#8] Move BpfUtils.cpp to BpfUtils.h
Functions in BpfUtils.cpp are trivial, they can be static inlined
in BpfUtils.h.

Bug: 202086915
Test: m; flash; boot; cd system/netd && atest
Test: m gpuservice_unittest libtimeinstate_test bpf_module_test
      CtsAppOpsTestCases libmeminfo_test VtsBootconfigTest
      vts_test_binary_bpf_module
Change-Id: Ie1ece23b6fc9a4db5fc95930209a10da1e528cb5

Ken Chen   commited 2021-11-21 10:59:39 +08:00
02c2536103
BpfUtils.h - switch to GTEST_SKIP in all 4 SKIP macros
Test: TreeHugger, atest libbpf_android_test netd_unit_test netd_integration_test
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I3acb47a432378f257418f81b26516e18d6ff3780

Maciej Żenczykowski   commited 2021-11-11 11:54:02 -08:00
5460527c74
expose readSectionUint() for testing
Bug: 190519702
Test: atest bpf_module_test, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I772c918d162440de6cc492b2faeafe03340fca11

Maciej Żenczykowski   commited 2021-07-06 01:55:33 -07:00
7ed94efeda
BpfUtils.h - add back SKIP_IF_BPF_NOT_SUPPORTED macro
Needs to be used by dns mainline module tests on R devices (Pixel 2).

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

Maciej Żenczykowski   commited 2021-03-11 19:52:30 -08:00
67fa2073ff
KVER macro - support 4.9.256+ kernel
Instead of doing 16/8/8 bits for major/minor/subver macro,
switch to 8/8/16 bits, since this gives plenty of space:
  - major versions are bumped every few years, so 256 last forever
  - minor versions are bumped every few months (~5-6 per year),
    so 256 lasts for decades, additionally Linus Torvalds doesn't
    like big numbers, and eventually bumps the major instead
  - sub versions are the problematic ones, because they're bumped
    on every LTS security release, however even at one release per day,
    16 bits lasts for 180 years

Note: before this change 4.9.256 was treated as equivalent to 4.10.0.

Luckily all our tests were only ever (by chance) against the LTS release
bases (ie. 4.9.0, 4.14.0, 4.19.0, 5.4.0) and thus we wouldn't have hit
an actually failure until 4.9.(5*256) === 4.14.0 which would have caused
the bpfloader to attempt to load bpf code requiring 4.14 on a 4.9 kernel,
and resulted in failure of device to successfully boot.

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

Maciej Żenczykowski   commited 2021-02-11 16:05:21 -08:00
30af4b5487
BpfMap - fix a clang warning: misc-unconventional-assign-operator
//system/bpf/libbpf_android:libbpf_android_test clang-tidy BpfMapTest.cpp
system/bpf/libbpf_android/include/bpf/BpfMap.h:138:5: warning: operator=() should return 'BpfMap&' [misc-unconventional-assign-operator]
    void operator=(BpfMap<Key, Value>&& other) noexcept {
    ^

Test: builds without warnings
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I0bb0d42f58bba58c7b9220f062453d827438e85a

Maciej Żenczykowski   commited 2021-01-23 18:30:07 +00:00
8ed7c0f9b9
move waitForProgsLoaded() to header file
Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I2f0035f2622ca2a8c10d29caff912f3f895f2607

Maciej Żenczykowski   commited 2021-01-21 17:39:33 -08:00
cdf85a3148
header file cleanup
Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I03920e51bab3cb704f156ea9c62a0a38fd65a3a9

Maciej Żenczykowski   commited 2021-01-21 14:24:25 -08:00
23c436f957
add SKIP_IF_XDP_NOT_SUPPORTED macro
Test: builds, atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ia954983565250faff547e81a53ec0df5fc26827e

Maciej Żenczykowski   commited 2021-01-20 16:24:02 -08:00
a36bed395f
remove isBpfSupported()
Test: builds, atest, TreeHugger
Bug: 167500195
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ifb4b4ced25ac444fe913c6392c0c8d57b2d9c58e

Maciej Żenczykowski   commited 2021-01-19 23:48:01 -08:00
916d2395cc
remove BpfLevel and getBpfSupportLevel in favour of isAtLeastKernelVersion
Test: builds, atest, TreeHugger
Bug: 167500195
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I50dbed1843a8c2802e1b416281e193ae7d282a99

Maciej Żenczykowski   commited 2021-01-19 23:48:01 -08:00
dcb5228f2c
put tethering bpf maps & programs in /sys/fs/bpf/tethering/...
This makes it harder for system and mainline bpf programs to
conflict with each other, thus enabling easier mainline module
updates.

Test: builds and boots, atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic1b69bb1ddc4a10bd4861dadbd6b97e2520c532d

Maciej Żenczykowski   commited 2021-01-20 05:17:14 +00:00
d8a4578f30
Merge changes Ic378d0e5,Ib3a8696a
* changes:
  remove unused enum value BpfLevel::NONE
  remove unused function BpfLevelToString()

Treehugger Robot   authored and   Gerrit Code Review   commited 2021-01-20 03:38:42 +00:00
da5d7ec6a7
remove unused enum value BpfLevel::NONE
Test: builds, atest, TreeHugger
Bug: 167500195
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic378d0e5a579abf44985a3353a21885796c297bb

Maciej Żenczykowski   commited 2021-01-20 01:36:55 +00:00
2337d47db3
remove unused function BpfLevelToString()
Test: builds, atest, TreeHugger
Bug: 167500195
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib3a8696a9653e5fbb022c98c26cfff623fadb3b4

Maciej Żenczykowski   commited 2021-01-20 01:36:16 +00:00
5947162cc5
remove unused macros SKIP_IF_BPF{,_NOT}_SUPPORTED
Test: builds, atest, TreeHugger
Bug: 167500195
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic740b1102fcf735516123eee2c6a3d5caa0892f9

Maciej Żenczykowski   commited 2021-01-20 01:15:04 +00:00
b8e9af4b10
Use the module bpf_syscall_wrappers for BPF system calls
Use the versioned header library of BPF system calls and remove the
stubs.

Test: atest BpfMapTest
Change-Id: Ib178b02b7cfebaae7681609ae7c40a49cda47eeb

Hungming Chen   commited 2021-01-13 14:34:41 +08:00
a46f217d73
rename BPF_FD_TO_INT() macro to BPF_FD_TO_U32()
Cause that's what it actually does...

Bug: 173167302
Test: atest BpfMapTest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I8445cc4fe68f24bb3b26a678b7731b0c518e80f9

Maciej Żenczykowski   commited 2020-11-30 17:06:58 -08:00
38fb4300fb
Fix BPF_FD_JUST_USE_INT macro
Bug: 173167302
Test: atest BpfMapTest
Change-Id: I70b58e23cc5b04a187de8d55ddc3f2b367718b88

markchien   commited 2020-12-01 01:28:20 +08:00
26ca77305d
Allow to just use integer for bpf fd
Bug: 173167302
Test: bpf_module_test
      clatd_test
      libnetdbpf_test
      netd_integration_test
      netd_unit_test
      netdutils_test

Change-Id: I2643222e1eb2c5686d7f64eac8d9c4666a235c69

markchien   authored and   Mark Chien   commited 2020-11-13 04:21:13 +00:00
90b02a072e
implement support for functions which may optionally fail to load
This is useful for critical functions with fallbacks, but
may even be useful for non-critical functions, where a function
in the middle of the file may fail to load, but you still want
other (later) functions to be attempted.

Critical applies to the entire .c file (or to be more correct to
the entire resulting .o).  Optional applies to a specific section
of that .o (ie. a specific individual function).

This new optional attribute is necessary to be able to declare
a .c/.o file critical even if *some* of the individual functions
might fail to load due to missing kernel patches.

(Note: we currently have no way to specify a map as optional)

Critical guarantees that all non-optional programs, and all maps,
have been created, pinned, chowned, and chmoded successfully
(or that they already existed).

For an example of use see:
  system/netd/bpf_progs/offload.c

(while at it also add retrieveProgram() and mapRetrieve{RW,RO,WO}()
 helpers to BpfUtils.h)

Test: builds, atest, see paired netd change for extra details
Bug: 150040815
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I50b292c061b05fc8f4b4b8574f128345c45c78db

Maciej Żenczykowski   commited 2020-06-17 10:44:19 +00:00
aa295c8355
Warn loudly and do not set bpf.progs_loaded property on critical bpf load failure.
Reasoning: critical load failures for netd bpf programs will just result
in unpredictable behaviour later on.  For example netd/systemserver
crash loops.

Test: builds
Bug: 150040815
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iefa01e60cd0a9a223e96411726a199bfb4857a5a

Maciej Żenczykowski   commited 2020-06-15 08:01:50 +00:00
89515d979a
BpfMap.h - fix cert-oop54-cpp compiler warning
Fixes:
  system/bpf/libbpf_android/include/bpf/BpfMap.h:132:10: warning: operator=() does not handle self-assignment properly [cert-oop54-cpp]
      void operator=(const BpfMap<Key, Value>& other) {
           ^

Matches the self-assignment check from
  https://clang.llvm.org/extra/clang-tidy/checks/bugprone-unhandled-self-assignment.html

This isn't needed here, since the code was already correct, but
it does prevent a pointless newfd = dup(fd); close(fd); sequence.

Test: builds
Bug: 153035880
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ia57f95d4ab180783c97db7e6f8d929f822c8958d

Maciej Żenczykowski   commited 2020-04-24 08:15:00 -07:00
54a7e769ae
BpfMap: Specify zero to be default map_flags in constructor
Bug: 149963652
Test: atest
Change-Id: I0a9311bdadffc0fda75f153b3e2617fb082d3ddb

Hungming Chen   commited 2020-03-05 16:03:57 +08:00
6161ff246f
BpfMap: override copy assignment operator
Test: build, atest
Bug: 149963652
Change-Id: I864ca00762a4e5c910454dd9dc023b74c67065d6

Hungming Chen   authored and   Maciej Żenczykowski   commited 2020-03-04 23:11:28 +00:00
4008da93e8
Merge "BpfMap - make clear() multithread safe"
Treehugger Robot   authored and   Gerrit Code Review   commited 2020-02-20 13:05:14 +00:00
e9e8305a36
add support for marking ebpf programs as being only for some kernel versions
Some ebpf code cannot be loaded on too old kernels.

Sometimes we want a different - more advanced - version of an ebpf program
to be loaded on a newer kernel.

Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9e93e7246951916e6d60544575337a7a19c82886

Maciej Żenczykowski   commited 2020-02-19 15:46:23 -08:00
07375e26a3
BpfMap - make clear() multithread safe
(while at it also remove 3 spurious this-> prefixes)

Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I2419926b555734d7f5e052fb9e0686c52bdb32d0

Maciej Żenczykowski   commited 2020-02-19 13:29:02 -08:00
b7176fb6af
add more precise bpf levels
This is to allow fixing a broken OffloadUtils netd test, but this will
also be useful for future support to make programs per kernel version.

We make the string versions of the enum more readable, because there
is exactly one user:

adb shell dumpsys netd | egrep -i bpf
  BPF module status: enabled
  BPF support level: Extended [4.14]
  ...

Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I843bd7355703a78acc8343ca7f647ce9366927eb

Maciej Żenczykowski   commited 2020-02-18 04:46:58 +00:00
8a09a5ad1c
BpfUtils.h - add SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED
(being moved from //system/netd/tests/bpf_base_test.cpp)

Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I7a38eff6d86a1bcd6dde193fd360d198bd57a900

Maciej Żenczykowski   commited 2020-02-12 04:15:29 -08:00
672b0e7aae
'static inline' -> 'inline'
Per Bernie: "in C++, inline implies internal linkage, you can drop static"

Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I139af1d826d4982a1480fbee433fd9a743509786

Maciej Żenczykowski   commited 2020-02-11 16:43:15 -08:00
06caf87004
introduce isBpfSupported() helper
Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9e2a7e049746ad193eea01e7c9d6786d27728c72

Maciej Żenczykowski   commited 2020-02-11 15:05:27 -08:00
c3a640db94
More missing calls to Result::ok() in libbpf_android tests
Test: m checkbuild
Change-Id: Idb70b975e3c3bcbaa78cb493ec8f66b9b625ef74

Bernie Innocenti   commited 2020-02-10 07:28:13 +09:00
43d2538871
Merge "Drop base:: in front of ErrnoErrorf"
Tom Cherry   authored and   Gerrit Code Review   commited 2020-02-06 19:14:15 +00:00
2cc5aeb816
Convert system/bpf to Result::ok()
No functionality changes, this is a mechanical cleanup.

Test: m
Change-Id: Ia37f81c9861be0da83a91663a9d32b6b753a527c

Bernie Innocenti   commited 2020-02-06 04:26:17 +09:00
5953dee87d
Drop base:: in front of ErrnoErrorf
ErrnoErrorf() is now a #define, so it cannot be qualified.

Test: build
Change-Id: I4c70a234ae6459ca071975b912353ee2501988c2

Tom Cherry   commited 2020-02-04 15:27:30 -08:00
a7146db6fb
make BpfMap.reset() harder to use
Long term we don't even want reset() to exist.

It's simply not useful since only the bpfloader can create maps,
and thus all we can do is construct from pre-existing pinned bpf
path - and we should never destroy/unpin/delete these.

Similarly the move constructor is probably spurious.

Basically the only real use for reset() is in tests,
and even there the overall utility is not clear.

Hence, in the mean time, make it harder to use incorrectly,
and harder to use in general.

To do that we get rid of reset() -> you must call reset(-1)
and we eliminate the ability to call reset(unique_fd)
which has unclear fd-leaking semantics.

Also remove a spot where it is called spuriously.

Test: build, atest
Bug: 129773125
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ia018224aca0005ab68809b350595485ccd3f1bd3

Maciej Żenczykowski   commited 2020-01-30 08:59:53 +00:00
4f6573959b
BpfMap - add pinned path based constructors
(and remove the old straight from fd constructor)

Note that BpfMapRO is not yet truly compile time read-only,
since it still has methods that can modify things (although those
modifications will of course fail due to the map fd being r/o).

Test: build, atest with followup fixes
Bug: 129773125
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I70b511e3f0a62cd4d9d5a923e658b1086337ec4c

Maciej Żenczykowski   commited 2020-01-30 08:50:58 +00:00
dfd941f0fd
BpfUtils - remove some spurious things
Test: builds
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I4ffa11220794d64c324d9de8890ba27cac986710

Maciej Żenczykowski   commited 2020-01-24 22:00:10 +00:00
974e0ed197
libbpf_android: remove libnetdutils dep
Remove libnetdutils dependency because bpf is being used for things other
than networking these days, and we don't want to make libnetdutils
vendor-available in the future. libbase provides an alternative type now.

Bug: 140330870
Test: atest libbpf_android_test netd_integration_test netd_unit_test
    libnetdbpf_test bpf_module_test
Change-Id: I72ae8cd7f58a49bfc7dcb914a332a4c4bad5dea5

Steven Moreland   commited 2020-01-21 10:47:33 -08:00
e7cd2a72d0