Commit graph

37 commits

Author SHA1 Message Date
Hungming Chen
4b8e982fae Load BPF programs from system and mainline folder
Test: flash and boot successfully
Change-Id: Ia1a713024686c36e34f7cf525a284abc00fe56e2
2020-09-17 11:38:53 +08:00
Maciej Żenczykowski
07b874481f bpfloader: sleep 20 seconds on failure
(should make it easier to observe logs via adb)

Test: atest, TreeHugger
Bug: 150040815
Bug: 160546011
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I20d6ede4ddf516a91c707aa08cf3a92aed39b03b
2020-07-15 01:44:12 -07:00
Maciej Żenczykowski
2a775a423a Revert "start bpfloader asynchronously"
This reverts commit 838b81de8f.

Test: build, atest, TreeHugger
Bug: 150040815
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I7447ef782bab90208046a4e5698301ab0e08da56
2020-06-23 21:07:30 -07:00
Maciej Żenczykowski
838b81de8f start bpfloader asynchronously
(probably a loss for reliability, but possibly a win for device boot time)

Test: build, atest, TreeHugger
Bug: 150040815
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Id62009a9f888ce2092b8a1e7aef24ae4224109d5
2020-06-23 21:06:19 -07:00
Maciej Żenczykowski
6bd207e96b launch bpfloader earlier, and reboot the device on bpfloader failure
should eliminate netd crash loops due to boottime bpfloader failure

Test: built and booted on cuttlefish, atest of various net tests
Bug: 150040815
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: If11d491d78958ff18290e826011d2593f58d217d
2020-06-22 08:29:08 +00:00
Maciej Żenczykowski
89515d979a 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
2020-06-15 08:01:50 +00:00
Maciej Żenczykowski
567dc56b57 only set 'bpf.progs_loaded' property on eBPF capable devices...
... which also means waitForProgsLoaded() has to be a no-op on pre-bpf devices.

This is more consistent: it's weird to have bpf.progs_loaded set on a 4.4 device.

Test: builds, atest
Bug: 150040815
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iddabcf56c187ea518dd2e4d8db451b2f6902aa8e
2020-06-13 22:41:48 -07:00
Maciej Żenczykowski
cd994cd637 Remove bpfloader dependency on netd's ebpf programs.
This is backwards.  netd should require them,
and they should require bpfloader, not the other way round.

Test: build, atest, ls -lZ /sys/fs/bpf /system/etc/bpf still shows all maps/progs
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iecd95cb69eaad97979af163e63489309826546f6
2020-02-19 10:31:54 -08:00
Connor O'Brien
8fa2949910 bpfloader: add dependency on time_in_state.o
Include time_in_state.o in device builds, causing time in state
programs & maps to be loaded.

Bug: 138317993
Test: make bpfloader; check that time_in_state.o is built
Change-Id: I4849bda645d7a02e3f889068987a79ce3f78d138
Signed-off-by: Connor O'Brien <connoro@google.com>
2020-02-18 16:08:28 -08:00
Maciej Żenczykowski
669f7388df grant bpfloader CAP_CHOWN
so that it can change the uid/gid of pinned bpf progs and maps

Test: build, atest
Bug: 149434314
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Id23f9caaddf620d4e99d970523f0a9768826e0d7
2020-02-13 13:08:40 -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
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
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
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
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
Treehugger Robot
4389649d8a Merge "bpfloader: Try to load even if bpf.progs_loaded is set" 2019-06-13 20:55:07 +00:00
Joel Fernandes
2d8176917f bpfloader: Increase rlimit to 64MB
The rlimit was recently set to 8MB for bpfloader in
Change Id Ib97d1d304f4ea9a6215afed76e1c1e28f1415221

This could be causing intermittent failures to create maps, let us
increase it to 64MB for bpfloader.

Bug: 134558982

Change-Id: Iabb2e6c7d9ea3843ae28f2c465756e5aacdf1904
Signed-off-by: Joel Fernandes <joelaf@google.com>
2019-06-13 20:38:18 +00:00
Joel Fernandes
2409604601 bpfloader: Try to load even if bpf.progs_loaded is set
Currently the bpfloader does not try to load programs if
bpf.progs_loaded is set. However there is no harm in trying to reload
programs because the loader is designed to reuse previously created maps
and programs if they were created. On the other hand, not trying to load
if property is set is harmful since the property prevents retries. So
prevent this from happening.

Bug: 134558982
Change-Id: I61dfb8827ca37c9a1ff4e0912ca96f06a36c37d6
Signed-off-by: Joel Fernandes <joelaf@google.com>
2019-06-13 14:44:02 -04:00
Chenbo Feng
b20d28df6a Skip bpf object loading on unsupported devices
The bpfloader should not try to load bpf objects on unsupported devices
since devices with 4.4 kernel can have bpf maps but not bpf pogram
support. Creating bpf maps on those devices would mislead the framework
code to use an unsupported code path and crash system server.

Bug: 130687855
Test: cuttlefish boot test for 4.4 kernel.
Change-Id: I43ffc01e1099ee3effaa6ba30aa78080d5bec72f
2019-04-17 21:40:39 +00:00
Chenbo Feng
ee121646cf Set bpfloader MEMLOCK rlimit to at least 8MB
Since bpfloader is only used to create the bpf maps and load bpf
programs on android devices. It is okay to set the MEMLOCK rlimit of
bpfloader to the proper size needed by bpf maps regardless of the global
device values. It helps partner to adopt the bpf feature without
changing the device configuration.

Bug: 119279144
Bug: 129246448
Test: remove device specific rlimit and build and verify bpf maps
created.

Change-Id: Ib97d1d304f4ea9a6215afed76e1c1e28f1415221
2019-04-09 12:04:33 -07:00
Maciej Żenczykowski
7a3a5ba1ad bpfloader depends on clatd.o
Test: builds
Bug: 65674744
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I5c48acc6aed81c38650564d2fb42e6e816b4b726
2019-03-26 18:11:01 -07:00
Lorenzo Colitti
ac600729b8 Remove bpfloader dependency on netd.
This was necessary when the netd BPF program lived in system/bpf,
but it's no longer necessary.

Test: m
Bug: 65674744
Change-Id: I1fcf7e41276341b1258adf9d3795e81b6139f84f
2019-03-26 13:52:15 +09:00
Connor O'Brien
c551a76f45 Remove time_in_state.o from build
The data collected by this program currently has no consumers, so
there's no need to load it or include it in the bpfloader build yet.

Test: make bpfloader
Change-Id: I206d94e3da34dc3b81bdbfdd4a00b05cfb624f48
Signed-off-by: Connor O'Brien <connoro@google.com>
2019-03-22 16:19:25 -07:00
Connor O'Brien
c8885ccf3f Add time_in_state.o to build
bpfloader now has CAP_SYS_ADMIN, which allows it to load programs like
time_in_state.o that use eBPF's tracepoint support.

Test: make bpfloader
Test: ls /sys/fs/bpf/ shows time_in_state progs and maps are created
Bug: 112334572
Bug: 78498733
Change-Id: Iedff50381c9ee2b3bdfc6c01b2e2ccaeaf0917d5
Signed-off-by: Connor O'Brien <connoro@google.com>
2019-01-23 12:09:19 -08:00
Joel Fernandes
d3ec871017 Set bpf.progs_loaded property when programs finish loading
The property bpf.progs_loaded will be used by services expecting BPF
programs to be loaded and wait before they are loaded.

Bug: 112334572

Change-Id: I19daa0caa84e2659e8790f0dd551d95ab824e13b
Signed-off-by: Joel Fernandes <joelaf@google.com>
2019-01-11 09:02:11 -05:00
Joel Fernandes
6e1341ef20 bpfloader: Make it an init service
With this, bpfloader does not need to be started by netd and will load
at boot time.

Bug: 112334572
Change-Id: Iad777ed0a5bb5995dbf2fc176a220292a4c5e41f
Signed-off-by: Joel Fernandes <joelaf@google.com>
2019-01-07 20:17:42 -05:00
Chenbo Feng
c1dd7648b1 New bpfloader netd kernel program
Change the netd bpf program to the new format. Adding map definition and
necessary helper functions to the kernel program code. Move the netd bpf
programs out of bpfloader to a new directory.

Test: dumpsys netd trafficcontroller
Bug: 112334572
Change-Id: I9287285d188e966193532b1522b5d3e67e32e930
2019-01-07 20:17:42 -05:00
Chih-Hung Hsieh
18d8bfcfe9 Remove unused using decls.
Bug: 121209395
Test: make with WITH_TIDY=1 DEFAULT_GLOBAL_TIDY_CHECKS=-*,misc-unused-using-decls
Change-Id: I42d89c904c562ea2860510844c139b93ebee4637
2018-12-18 15:02:11 -08:00
Joel Fernandes
d76a2004a6 Initial checkin for relocatable ELF loader and link with bcc
* Wrote a relocatable ELF loader from scratch. The loader library loads
all objects in /system/etc/bpf/, parses and fixes up eBPF instructions
based on relocation information, creates maps and programs and pins
them. A single API call without arguments is made which results in this
operation. The API has all the information it needs from the filesystem
and the ELF objects, so there is no configuration stored in the code
calling the loader API essentially making it zero conf. Initially this
will be used by time_in_state statistics collection using tracepoints.
In the future, netd's eBPF C code should be rewritten to use this
infrastructure and the old ELF loader can be gotten rid off. This is the
first step.

* Link libbpf with libbpf_android which will come from the external/ bcc
project. This will be used for tracepoint and perf eBPF support. In the
future it can be used for other things as kernel eBPF support advances.

This patch will be merged only after bcc is cloned into external/ to
prevent build breakage.

Bug: 112334572
Change-Id: Ic0fd9504e18031132d40bb627c5e44058d59c9f8
Signed-off-by: Joel Fernandes <joelaf@google.com>
2018-12-17 22:27:56 +00:00
Yi Kong
4a74569453 Use size_t instead of unsigned int
Addresses follow-up review on commit fc3c769.

Test: m checkbuild
Change-Id: I79cd341c425ebe27cc66584540398fc98e94be50
2018-12-16 18:23:19 -08:00
Yi Kong
cb8e0a08ea Fix sign-compare warning
system/bpf/bpfloader/BpfLoader.cpp:79:23: error: comparison of integers of different signs: 'int' and 'std::__1::vector<android::bpf::BpfMapInfo, std::__1::allocator<android::bpf::BpfMapInfo> >::size_type' (aka 'unsigned long') [-Werror,-Wsign-compare]
    for (int i = 0; i < mapPatterns.size(); i++) {
                    ~ ^ ~~~~~~~~~~~~~~~~~~`

Test: m checkbuild
Change-Id: If2891bfc7655aea34499975763f72e0a25b32289
2018-12-14 14:31:42 -08:00
Chenbo Feng
1f20ad3d78 Code style clean up
Apply the git clang format on the bpf project to make sure the existing
code compliance with the standard.

Test: project compiles and bpfMapTest pass.
Bug: 112334572
Change-Id: I4774aa2a197be2c671f6fb307c1fc9b71edfeeec
2018-11-28 11:31:36 -08:00
Chenbo Feng
79a4da2d1e Rename libbpf to libbpf_android
Rename the bpf support library in android to libbpf_android to make
it compatible with external bcc project. Fix the Android.bp file to
prevent build break.

Bug: 112334572
Test: No build breakage.
Change-Id: I24ae8aaf8ca8afdded5e46d21f0deec68d6030da
2018-11-08 11:32:31 -08:00
Chenbo Feng
4c9e9ecc57 Fix the build problem with bpf project
Add the necessary Android.bp file and fix the compile error for the new
bpf project.

Fix the run time restart false alarm in bpf loader.

Bug: 112334572

Test: device boot and dumpsys netd show eBPF object okay, runtime
      restart netd and no bpfloader error showed in logcat.
Change-Id: Iae193bda45e50b7d22479086d6403e3c4fb185c4
2018-10-16 21:17:12 -07:00
Chenbo Feng
75b410bdf1 Initial checkin for bpf project
Copied the files from system/netd into system/bpf. No modification is
made to the files.

Test: this doesn't build since the Android.bp file is missing
Bug: 112334572

Change-Id: Ia090a429dd416e07c46766784fb3dd2037201525
2018-10-16 20:38:03 -07:00