Commit graph

295 commits

Author SHA1 Message Date
Maciej Żenczykowski
672b0e7aae 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
2020-02-12 04:15:29 -08:00
Maciej Żenczykowski
524deefc88 improve logging in case of bpf load failure
(we don't want to truncate this output, since it can be very useful
for debugging failures)

Test: builds, more useful ebpf load failure logs
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I817853f33b7f59296a041fb0d26ca3ae502b29da
2020-02-11 19:12:34 -08:00
Maciej Żenczykowski
06caf87004 '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
2020-02-11 16:43:15 -08:00
Maciej Żenczykowski
c3a640db94 introduce isBpfSupported() helper
Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9e2a7e049746ad193eea01e7c9d6786d27728c72
2020-02-11 15:05:27 -08:00
Maciej Żenczykowski
8bd5f39348 fix getBpfSupportLevel() cache wrt. multi-threading
In case it happens to get called twice in parallel on
startup.  It's never a mistake to do extra calls to the
uncached version.

Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I2605ed2ebc4ee331b2ca6c9fa915203d70415d7f
2020-02-11 11:20:35 -08:00
Maciej Żenczykowski
61588e0e64 Merge "make getBpfSupportLevel() cached" 2020-02-11 19:08:03 +00:00
Maciej Żenczykowski
fd90bc8ba1 make getBpfSupportLevel() cached
Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I6948934c88ce8268ad38e8c0061807a81f5ea4bb
2020-02-10 19:30:06 -08:00
Bernie Innocenti
43d2538871 More missing calls to Result::ok() in libbpf_android tests
Test: m checkbuild
Change-Id: Idb70b975e3c3bcbaa78cb493ec8f66b9b625ef74
2020-02-10 07:28:13 +09:00
Tom Cherry
2cc5aeb816 Merge "Drop base:: in front of ErrnoErrorf" 2020-02-06 19:14:15 +00:00
Treehugger Robot
4a480c057e Merge "Convert system/bpf to Result::ok()" 2020-02-06 13:33:12 +00:00
Bernie Innocenti
5953dee87d Convert system/bpf to Result::ok()
No functionality changes, this is a mechanical cleanup.

Test: m
Change-Id: Ia37f81c9861be0da83a91663a9d32b6b753a527c
2020-02-06 04:26:17 +09:00
Maciej Żenczykowski
51b71237cc bpfloader - add dependency on offload.o
Test: builds, atest, /system/etc/bpf/offload.o exists
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I043369f5e0ae4315dc8db0cf70dc8806e4997fe4
2020-02-05 03:57:57 -08:00
Tom Cherry
a7146db6fb Drop base:: in front of ErrnoErrorf
ErrnoErrorf() is now a #define, so it cannot be qualified.

Test: build
Change-Id: I4c70a234ae6459ca071975b912353ee2501988c2
2020-02-04 15:27:30 -08:00
Maciej Żenczykowski
089fb876e4 Merge "Set /proc/sys/net/core/bpf_jit_{enable,kallsyms} to 1" 2020-02-01 01:26:01 +00:00
Maciej Żenczykowski
ef2dc8e0a9 Merge changes from topic "b129773125"
* changes:
  make BpfMap.reset() harder to use
  BpfLoadTest.cpp - construct BpfMap from path not fd
  BpfMap - add pinned path based constructors
2020-01-30 15:24:02 +00:00
Treehugger Robot
7ddcfb5d21 Merge "BpfMapTest.cpp - construct BpfMap from type/size/flags and not from fd" 2020-01-30 09:46:32 +00:00
Maciej Żenczykowski
4f6573959b 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
2020-01-30 08:59:53 +00:00
Maciej Żenczykowski
cd5c3023ce BpfLoadTest.cpp - construct BpfMap from path not fd
switch over to the newer pinned path using constructor

Test: build, atest
  mMapFd no longer shows up in this file
Bug: 129773125
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie70149ffd5a541c80b35804ffd38df07f28b509b
2020-01-30 08:59:44 +00:00
Maciej Żenczykowski
dfd941f0fd 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
2020-01-30 08:50:58 +00:00
Maciej Żenczykowski
7c9a984f1d BpfMapTest.cpp - construct BpfMap from type/size/flags and not from fd
Test: build, atest
  mMapFd no longer shows up in this file
Bug: 129773125
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie49f0027d0f04565426ed800b079800b364dd049
2020-01-29 22:43:10 -08:00
Maciej Żenczykowski
a391148649 Set /proc/sys/net/core/bpf_jit_{enable,kallsyms} to 1
bpf_jit_enable = 1 is mostly a no-op since on most future
devices it will be force enabled by BPF_JIT_ALWAYS_ON

It is required for Pixel 3 & co Linux 4.9 based devices, which
can only do JIT for some ebpf programs (and thus can't enable
BPF_JIT_ALWAYS_ON without some netd programs refusing to load)

We also set bpf_jit_kallsyms = 1 because it makes debugging
failures easier, but it is incompatible with bpf_jit_harden != 0.

We don't bother setting bpf_jit_harden because we both want
bpf_jit_kallsyms to work, and because the only entity allowed
to load ebpf programs is the bpfloader and it only loads trusted
(verified file system signed) prebuilt bpf programs.

Test: built and booted, verified settings
Bug: 140377409
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9b401ee7b01a2042da87ff48d548b11e0cf78efa
2020-01-30 06:23:08 +00:00
Treehugger Robot
083c0776d9 Merge "bpfloader - bump memlock rlimit to 1GiB" 2020-01-30 03:48:29 +00:00
Maciej Żenczykowski
730a3860c2 refactor bpf_map_def into a single header file
Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iafd6016f4f24cc8fa263939c67e1a096aa91b125
2020-01-29 08:41:26 +00:00
Maciej Żenczykowski
e1deaecda4 bpfloader - bump memlock rlimit to 1GiB
Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I5a1728fe29e510c06978a0c7c5cc90af3e9ea46d
2020-01-27 22:28:40 -08:00
Maciej Żenczykowski
3c057e6b7b Revert "Grant IPC_LOCK capability to bpfloader"
This reverts commit 77494d2b70.

This doesn't apparently work...

Kernel doesn't actually test capabilities, just blindly charges
against user's memlock limit:

//kernel/bpf/syscall.c:

static int bpf_charge_memlock(struct user_struct *user, u32 pages) {
  unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;

  if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
    atomic_long_sub(pages, &user->locked_vm);
    return -EPERM;
  }
  return 0;
}

Test: N/A, revert
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Icf6d6e8a36e4b9f3771a5ce80e25ef3644ff4e83
2020-01-27 15:33:15 +00:00
Maciej Żenczykowski
77494d2b70 Grant IPC_LOCK capability to bpfloader
(instead of explicit memlock limit)

This reduces chance for memory allocation failures.

Any bpfloader failure is a critical boot time failure
(since without eBPF initializing properly netd will crash and
device will enter crash loop).

Test: builds, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I5ed0118d63c53100e7431324914bf22e9870abfe
2020-01-27 01:08:03 -08:00
Maciej Żenczykowski
0bfbf665b9 bpf_helpers - add more const annotations
Keys and values passed in to lookup/update/delete are not modified.

Test: builds
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I222615af7695f41bfb59c872c7368829631c08a9
2020-01-24 18:53:10 -08:00
Maciej Żenczykowski
974e0ed197 BpfUtils - remove some spurious things
Test: builds
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I4ffa11220794d64c324d9de8890ba27cac986710
2020-01-24 22:00:10 +00:00
Steven Moreland
e7cd2a72d0 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
2020-01-21 10:47:33 -08:00
Maciej Żenczykowski
289742f537 BpfUtils - pass file descriptors as 'const unique_fd&'s not raw ints.
Test: builds, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Idde6d9c81ac9ba02aa5a42f9ca9ec72ef7cd5cad
2020-01-18 19:39:39 -08:00
Maciej Żenczykowski
79365da151 BpfMap/Utils - fix const-ness of key & value
When accessing maps keys are always const/read-only.
Similarly for the value when it is being stored in the map.

This eliminates four const_casts.

Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Icc57b46a8e9e81b5b06264260bfbf34a4ba9105d
2020-01-18 19:32:09 -08:00
Maciej Żenczykowski
b479fd6a9f BpfUtils - move trivial functions from .cpp to static inline's in .h
Test: builds, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I49d1ac6e196c5b1afbe460a7247d2ad38f571958
2020-01-17 09:07:15 +00:00
Maciej Żenczykowski
2a1be9c22e BpfUtils - use aggregate initializers for bpf()
Test: builds, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic8383e92973ff68a1b54299949121a31fb5c66d1
2020-01-16 02:19:22 -08:00
Steven Moreland
b572378120 TEST_MAPPING += libbpf_android_test
Note, this does not add libbpf_load_test as well because this test
requires adding a bpf program to the system image.

Bug: N/A
Test: `atest --test-mapping .`
Change-Id: I770ec4876e1cb93a526e99bcb5a234f964f46971
2020-01-15 08:28:44 +00:00
Maciej Żenczykowski
bc32ac641d BpfUtils - replace memset() with = {}
We can't specify which union member we want to initialize
or the compiler will be too smart and do a partial init,
but a = {} will still zero the entire struct.

Test: builds, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I0f9208be1be65cec35d848d37a0142d03e0919bf
2020-01-15 07:08:21 +00:00
Maciej Żenczykowski
c827aaa235 get rid of netdutils::Slice in BpfUtils.cpp
Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic17181be20480c6db0806cc8dafef97d61736ec3
2020-01-15 04:17:45 +00:00
Maciej Żenczykowski
0cb8b5ca7a Merge "libbpf_load_test: fix map location" 2020-01-15 02:29:18 +00:00
Maciej Żenczykowski
cd6f89bbb6 Merge "libbpf_android/BpfUtils - remove unused bpfProgLoad" 2020-01-14 23:44:11 +00:00
Treehugger Robot
22b82c47d6 Merge "libbpf_android: allow bpf w/o maps section" 2020-01-14 22:03:43 +00:00
Steven Moreland
cdeb9203af libbpf_load_test: fix map location
This test had bitrot.

Bug: N/A
Test: `atest libbpf_load_test`
Change-Id: I3480286e225d8dfbd0badd7bbb7cb77ff29ab0ea
2020-01-14 11:37:49 -08:00
Maciej Żenczykowski
c66f5c6bc1 libbpf_android/BpfUtils - remove unused bpfProgLoad
(due to switch to bpfloader from loading via netd)

Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic95cdfde26b8e7b905551a7fd2b465e0cf99bbce
2020-01-14 09:10:32 -08:00
Maciej Żenczykowski
d5f6cf505b Merge "remove network specific struct definitions from BpfUtils.h" 2020-01-14 09:03:11 +00:00
Steven Moreland
1362229a2e Merge "libbpf_android: waitForProgsLoaded" 2020-01-13 22:12:06 +00:00
Steven Moreland
87a1b19052 OWNERS: +smoreland +connoro -fengc -joelaf
Bug: N/A
Test: N/A
Change-Id: I33e5cb5a5e2b0bce588cb90c4d0b74da7cc8dace
2020-01-10 15:42:06 -08:00
Steven Moreland
4891e61341 libbpf_android: waitForProgsLoaded
Creating a function for this rather than having people copy/paste a loop
there.

It's very intentional that there is no timeout here. If bpfloader is
down, there is a serious problem, and we shouldn't let code move forward
in a state where it may not have been tested.

Bug: 140330870
Test: boot & use bpf/netd
Change-Id: I358d1fcf77ad5406e294dd57307bc411e2c8ad6a
2020-01-10 15:37:53 -08:00
Steven Moreland
c0905b4880 libbpf_android: allow bpf w/o maps section
Currently, if a bpf program object has no map section, this error is
propagated, and the program will fail to load. However, it is desirable
to have programs without any map.

Bug: 140330870
Test: load program w/ bpf w/o maps section
Change-Id: Ia53c699212a8cb962e3c2db562921880fef0cce0
2020-01-10 13:33:03 -08:00
Maciej Żenczykowski
841dd444dd remove network specific struct definitions from BpfUtils.h
This is part of 3 commits in 3 diff git repos, the main one is:
  https://android-review.googlesource.com/c/platform/system/netd/+/1200479
  "share eBPF struct definitions between ebpf and C++ netd"

The struct definitions are being moved to system/netd in:
  libnetdbpf/include/netdbpf/bpf_shared.h

(they also become typedefs and more consistent naming is used)

Test: builds
Bug: 146787904
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I0200bce35f910414b96d802cd316717b4307b950
2020-01-06 23:28:06 +00:00
Maciej Żenczykowski
be15fa1edd remove spurious equals comparators
These are unused by virtue of the code continuing to build.

C++20 has https://en.cppreference.com/w/cpp/language/default_comparisons
but still requires explicit declaration of them with '=default' annotation.

Test: builds
Bug: 146787904
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I751ad5592acc1ed4cf763d1ca82c301f2e233e56
2020-01-06 13:57:05 +00:00
Maciej Żenczykowski
480c70ac14 bpfloader - remove dead code
Test: builds, atest
Bug: 146787904
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iea667cf4adaccdfd8fbffae8960cbe243ae77d70
2019-12-30 04:12:36 -08:00
Steven Moreland
804bca0d66 libbpf_android: bpf_prog_load no log success error
errno is left unchanged on successful calls to this function, so it is
confusing to log it in the success path.

Fixes: 146171927
Test: looking at logs
Change-Id: Ia424dd865bd518d8c533e99fb1a64b02bc92e759
2019-12-12 17:24:00 -08:00