Commit Graph

61 Commits

Author SHA1 Message Date
Maciej Żenczykowski
cc40e9c110 Merge "remove spurious #include and OVERFLOW_COUNTERSET" 2022-12-20 12:13:43 +00:00
Maciej Żenczykowski
e504cc8754 Merge "Add option to skip map/prog based on build type" 2022-12-20 08:38:45 +00:00
Ryan Zuklie
8531cfe5dd 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: I5cd9725ad8f133c98c86e6ba620a266313055157
2022-12-19 14:08:53 -08:00
Maciej Żenczykowski
6776e3b02b remove spurious #include and OVERFLOW_COUNTERSET
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I8ad1b19905509862bdfa4e710da778ed0effb058
2022-12-19 09:59:25 -08:00
Maciej Żenczykowski
ec6505edd6 remove SKIP_IF_BPF_NOT_SUPPORTED
There is only user left, let us simply move the macro
definition into the resolv_integration_test.cpp file.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I6eb66abaa7f6392ab43300e5087e4277491e44c9
2022-12-18 19:48:54 +00:00
Maciej Żenczykowski
d6ba77ab89 remove no longer needed SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED macro
We can merge this now that:
  https://googleplex-android-review.git.corp.google.com/c/platform/system/netd/+/20284735
  'bpf_base_test - unconditionally run TestSocketFilterSetUp'
has been pulled into tm-mainline-prod.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I1b65cd41ce7b4eaab7f1fead2742c72e48294244
2022-12-18 16:37:32 +00:00
Maciej Żenczykowski
8da25a73a5 remove unused SKIP_IF_XDP_NOT_SUPPORTED macro
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I2480cbe958d2827440e5dd827b43a89f4b7a556c
2022-12-18 11:11:21 +00:00
Treehugger Robot
22d2158452 Merge "Add skeleton BPF ring buffer integration test" 2022-12-14 21:04:18 +00:00
Ryan Zuklie
ccd5eb9359 Add skeleton BPF ring buffer integration test
This adds a BPF program with a ring buffer and an associated test that
ensures the program and ring buffer can be loaded by the bpf loader. In
the following changes, this foundation will be used to test the user
space ring buffer wrapper class.

Unlike BpfMap, userspace can't (easily) write to the ring buffer so the
most accurate way to test a userspace ring buffer implementation is to
write to it from a BPF program.

Note: the BPF program in this change is never actually attached to a
cgroup, it is only loaded. The program will be executed manually using
BPF_PROG_RUN which is "side-effect free" (it can have side effects on
BPF maps, but not on Kernel state, e.g. dropping packets).

Bug: 246985031
Test: atest libbpf_android_test
Change-Id: Ib9c591218188f5f358a755c8854bc6f87d26af12
2022-12-12 10:02:18 -08:00
Ryan Zuklie
5a8738e7de Rename DEFINE_BPF_RINGBUF to DEFINE_BPF_RINGBUF_EXT
_EXT suffix is used for the all-args versions of these, such as the one
here.

Bug: 246985031
Test: build and flash on 4.19 and 5.10
Change-Id: Ibd76cbdc38f4d1f9df4f440147e5f18d9a2ea026
2022-12-08 17:18:54 -08:00
Ryan Zuklie
79ce874e8f Add macro to create BPF ring buffers.
BPF ring buffers are defined like maps, but cannot specify a key or
value size (attempting to do so is a verifier error). This change also
adds the type-safe methods for interacting with the map (output, reserve
and submit).

Bug: 246985031
Test: local run of Network Tracing w/ ring buffers
Change-Id: Ie8a47d987be6cb219fe7d73f2c61a56e3a3ab21a
2022-11-28 11:06:30 -08:00
Maciej Żenczykowski
91e4845301 Merge "Revert "add bpf_for_each_map_elem() helper definition"" 2022-11-07 23:36:35 +00:00
Maciej Żenczykowski
61e21fff0b Revert "add bpf_for_each_map_elem() helper definition"
This reverts commit 8711ae5494a3011f48543987fa08872d775df652.

Bug: 256991746
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I4835904f906422d5927feef82459ce1adcebc90c
2022-11-02 08:39:20 +00:00
Maciej Żenczykowski
203d5e0dcd trivialize SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED
Bug: 255652639
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I3732bbd7ad9e4e06c914206560268bd1ce8204f4
2022-10-26 03:05:32 +00:00
Chris Sabotta
87e48d453e Merge "Bug: b/255652639" 2022-10-25 23:40:44 +00:00
Chris Sabotta
33f1f3f74c Bug: b/255652639
Revert "remove SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED"

This reverts commit 319d8468ad90d33307ce764e7f6130587ce81419.

Reason for revert: Build breakage, (First bad build: 9217822)

Change-Id: I0db2bdb68006f9379bb149f7de9fa81ace501c3d
2022-10-25 23:38:32 +00:00
Maciej Żenczykowski
713c112d23 Merge "remove SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED" 2022-10-25 22:10:11 +00:00
Maciej Żenczykowski
5897a805ea remove SKIP_IF_BPF_SUPPORTED
as this does not appear to be used anywhere anymore

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I6905c6fdb4daf48df07e758481368d441637ad13
2022-10-25 09:27:08 +00:00
Maciej Żenczykowski
52605df5fd remove SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED
as this does not appear to be used anywhere anymore

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I95939b0b6c66f41f776ca31ea7178bb30241b81f
2022-10-25 09:24:59 +00:00
Maciej Żenczykowski
6f9830c342 Disallow pre-T use of userspace writable bpf map arrays with more than 1 element.
Bug: 254543135
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I11ea5edaebf08f3492ad79f059afa98b50f80df3
2022-10-21 00:04:03 +00:00
Maciej Żenczykowski
dd3fe1d750 add DEFINE_BPF_MAP_RO for output maps which userspace should never modify
Bug: 254543135
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I94c5e68e8941a264ea3671f75e3ad9c37ded8894
2022-10-20 04:06:37 +00:00
Maciej Żenczykowski
c5dcf2c8ea add bpf_for_each_map_elem() helper definition
See 5.12+ kernel's include/uapi/linux/bpf.h

(note that since bpf is 64-bit, int and long are the same thing)

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib340a619645ad5ab8e41ff76cb7b51671f496bc5
2022-09-27 23:14:51 +00:00
Maciej Żenczykowski
30a1808c1b Bump up BPFLOADER_MIN_VER for platform bpf.o's
Non platform ones (ie. shipped via mainline) already have
the value manually specified.

The goal here is to be able to do some bpf.o validity checking
in the bpfloader based on the bpfloader_min_ver.

Such validity checking really only makes sense for mainline
shipped bpf.o's which might need to load on older bpfloaders.

Hence we no longer want platform bpf.o's to have minver == 0.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9e2f670c48e30818689a42bc515eb49e86f3cbea
2022-07-25 18:04:34 +00:00
Maciej Żenczykowski
eb4194e852 BpfLoader v0.21-v0.27 don't support map uid at the new location
Bug: 239807354
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: If55ab36c1434e28d293135a97b87351de9f39686
2022-07-21 13:36:50 +00:00
Maciej Żenczykowski
a2469f36fc bpf_map_def: rename uid to 'zero', add new uid field
Bug: 239807354
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I13dce79dc11956c1903645a442bd00bd6d499a32
2022-07-21 13:35:45 +00:00
Maciej Żenczykowski
72e19c51fa Bpf maps & programs: support DEFAULT_BPF_(MAP|PROG)_(UID|SELINUX_CONTEXT|PIN_SUBDIR)
Sometimes you just want to change the setting for all maps and/or
programs in a specific .c file...

Bug: 190523685
Bug: 236925089
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I3ba6b3a8dadf18c1436b21feb1ee03db446e6aba
2022-07-18 08:59:50 -07:00
Maciej Żenczykowski
b6338cec03 define BPFLOADER_OBJ_AT_VER_VERSION as 25
BpfLoader v0.25+ support loading obj@ver.o files
as if they were obj.o.  This allows different
versions of the .o per bpfloader version.

This is useful because BTF enabled bpf.o files
are incompatible with BpfLoader < 0.10.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I2067b1c54c36842c0baa779f37a904b75b9dfb6d
2022-07-15 13:00:25 -07:00
Treehugger Robot
e182ece901 Merge "refactor common logic into abortOnKeyOrValueSizeMismatch" 2022-06-23 19:53:41 +00:00
Maciej Żenczykowski
96ffe3733c refactor common logic into abortOnKeyOrValueSizeMismatch
Per request on:
  https://googleplex-android-review.googlesource.com/c/platform/frameworks/libs/net/+/18992756

While we're at it let's temporarily remove the check in .reset(fd)
  if (bpfGetFdMapFlags(mMapFd) != 0) abort(); // TODO: fix for BpfMapRO

We'll add it back when the code is in better shape,
and read-only vs read-write state of the map is something
we actually *know*.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Id8d65bdc529872685b42656e638f22048fafb7f6
2022-06-23 13:20:35 +00:00
Maciej Żenczykowski
1a542aa48d bpf_helpers - BPFLOADER_T_VERSION is v0.19
Test: N/A
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I1298757926acfe299d4646e4e3f1ead3a0dfa806
2022-06-23 13:15:14 +00:00
Maciej Żenczykowski
53a144ea2e actually enable the use of new fields added in previous commit
(split in two to facilitate manual testing)

Bug: 218408035
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ifc00ed168231615819b88b232155e1fe6f9a8c71
2022-06-18 20:14:48 -07:00
Maciej Żenczykowski
d4e612b1e7 support per-map/prog selinux context and cross .o map sharing
Tested by making the map struct conditional on #define V18,
and only #defining that in the top line of netd.c,
this results in:

$ objdump -s -j size_of_bpf_map_def out/target/product/vsoc_x86_64/apex/com.android.tethering/etc/bpf/net_shared/clatd.o
out/target/product/vsoc_x86_64/apex/com.android.tethering/etc/bpf/net_shared/clatd.o:     file format elf64-little
Contents of section size_of_bpf_map_def:
 0000 30000000 00000000                    0.......

$ objdump -s -j size_of_bpf_prog_def out/target/product/vsoc_x86_64/apex/com.android.tethering/etc/bpf/net_shared/clatd.o
out/target/product/vsoc_x86_64/apex/com.android.tethering/etc/bpf/net_shared/clatd.o:     file format elf64-little
Contents of section size_of_bpf_prog_def:
 0000 1c000000 00000000                    ........

$ objdump -s -j size_of_bpf_map_def out/target/product/vsoc_x86_64/apex/com.android.tethering/etc/bpf/netd_shared/netd.o
out/target/product/vsoc_x86_64/apex/com.android.tethering/etc/bpf/netd_shared/netd.o:     file format elf64-little
Contents of section size_of_bpf_map_def:
 0000 74000000 00000000                    t.......

$ objdump -s -j size_of_bpf_prog_def out/target/product/vsoc_x86_64/apex/com.android.tethering/etc/bpf/netd_shared/netd.o
out/target/product/vsoc_x86_64/apex/com.android.tethering/etc/bpf/netd_shared/netd.o:     file format elf64-little
Contents of section size_of_bpf_prog_def:
 0000 5c000000 00000000                    \.......

$ echo $[0x00000030] $[0x00000074]
48 116

$ echo $[0x0000001c] $[0x0000005c]
28 92

and it still successfully boots.
So the struct extension infrastructure works as desired.

Bug: 218408035
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I8b55769e69dbf9580e844f2a50d48651fd9a0cff
2022-06-18 14:03:50 -07:00
Maciej Żenczykowski
2efccc5a0e BpfMap.h - hide dangerous stuff behind #ifdef BPF_MAP_MAKE_VISIBLE_FOR_TESTING
while we're at it:
  - replace 'unique_fd != -1' with unique_fd.ok() which is
    a test for fd.get() >= 0 and is thus effectively equivalent
  - make use of the fact that unique_fd.reset()
    takes care to save errno.

(see impl. in //system/libbase/include/android-base/unique_fd.h )

Bug: 236285127
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I6fb7bf28a2265ad84baa3c552b39c620cb3875fe
2022-06-16 19:25:30 +00:00
Maciej Żenczykowski
cf4b58f98f BpfMap - add BpfMapRO.init() support
Really we need to fix the inheritance to make
BpfMapRO the parent class of BpfMap:
but that's a far more difficult thing to do,
so in the short term we punt like this.

This makes BpfMapRO a little bit more usable,
and allows a slow transition across the codebase...

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I1c5112db70e9e523c113cba536fbe19422b4d3f3
2022-06-13 19:37:03 -07:00
Maciej Żenczykowski
4b1b4090de TEST_BPF_MAP -> BPF_MAP_MAKE_VISIBLE_FOR_TESTING
Per request of Lorenzo on:
  https://googleplex-android-review.git.corp.google.com/c/platform/frameworks/libs/net/+/18694820
  https://googleplex-android-review.git.corp.google.com/c/platform/packages/modules/Connectivity/+/18695355

Although he'd suggested 'BPF_MAP_TEST_ONLY',
but I think this is even clearer.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie1185a61e80abe94e19903f6aa66bb1ad29ce2f4
2022-06-13 19:37:03 -07:00
Maciej Żenczykowski
5c5fae7a8b BpfMap key/value access size verification
(beginning there of)

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie7cf8cd51b4e272fc76a281df6231ed27955ed3f
2022-06-03 21:23:39 +00:00
Maciej Żenczykowski
1a31e3e68e BpfMap: simplify .init() mMapFd assignment
this way is more obviously not calling dup_with_cloexec

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I4e1ce3b0a42ccea638332ae451e291e025d57895
2022-06-01 15:17:17 -07:00
Maciej Żenczykowski
ec3e848a13 Merge "BpfMap: introduce resetMap(type, entries, flags)" 2022-06-01 09:00:46 +00:00
Maciej Żenczykowski
1db4a793b1 BpfMap: introduce resetMap(type, entries, flags)
to replace less safe uses of BpfMap.reset(create(type, keysize, valuesize, entries, flags))

Meant to be used in tests only.

Bug:
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I869f1f935bcf5d00702c42bc46d6094ea552addc
2022-05-31 07:30:24 -07:00
Maciej Żenczykowski
9a276e68f8 Fix parsing of kernel version 5.18-rc1
Test: TreeHugger
Bug: 234125620
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I624927cacd2722f4fc61ef419de74254cecdd77b
2022-05-27 19:43:47 +00:00
Maciej Żenczykowski
70cd8e7de8 synchronizeKernelRCU - update comment
Test: N/A, comment only
Signed-of-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I839a457fdb8f5b95f60cb9cd41983fbcf3792768
2022-05-27 15:06:41 +00:00
Maciej Żenczykowski
0b3a990e72 Merge "define necessary bpf helper functions" 2022-05-26 10:15:19 +00:00
eric.yan
fa1baa1b4c define necessary bpf helper functions
add bpf helper functions: bpf_get_stackid; bpf_get_current_comm

Signed-off-by: eric.yan <eric.yan@oppo.com>
Change-Id: If0daccfe8504fc107ed8d07bef92ed40b101dbc5
2022-05-26 09:29:26 +08:00
Maciej Żenczykowski
eeb588721e cache kernelVersion(), make isAtLeastKernelVersion() cheaper
not perfect due to this being in a header file, so multiple copies
potentially exist, but it's really simple, and works nearly as well.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Id90c6933d57bc12f4dd640c8918fd0885c7474cf
2022-05-25 12:41:25 -07:00
Maciej Żenczykowski
6f49c95686 bpf_helpers.h - add #define's for S/T bpfloader versions
(by request from Lorenzo)

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I802d2aae8b103375d7cbe0ed5c35f0abf6c04ac9
2022-05-24 16:07:27 -07:00
Stephane Lee
4806feb3a5 Allow use of shared bpf headers by recovery
This will allow healthd to filter uevent packets, if needed

Bug: 203462310
Test: Ensure that healthd can build if it uses bpf filters
Change-Id: I97bfe9509a6934444f656508176495bba38381be
2022-03-09 14:16:41 -08:00
Ken Chen
0a31b342a1 BpfUtils.h - switch to GTEST_SKIP in all 4 SKIP macros
patch the missing commit I3acb47a432378f257418f81b26516e18d6ff3780.

Test: TreeHugger, atest libbpf_android_test netd_unit_test
netd_integration_test

Change-Id: I6b7114584759c0689e0d6b852387b93a799f663f
2022-03-02 03:19:49 +00:00
Maciej Żenczykowski
c805004f6d switch from libbpf_android to bpf_headers
Test: TreeHugger, atest libbpf_android_test
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I215ac3c5d4820bfef94a48024d511ea86012eb0f
2022-02-09 19:33:20 -08:00
Maciej Żenczykowski
6b1d684059 bpf_headers/bpf_syscall_wrappers - remove visibility restrictions
this doesn't appear to make sense now that it's available to vendors

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: If2d24fa8eccfb5c12832fb191a8560b024751db7
2022-02-09 19:33:20 -08:00
Steven Moreland
b0df1895d5 bpf_headers for vendor.
For waitForProgsLoaded.

Bug: 162057235
Test: build, load bpf program from vendor
Change-Id: Ib670aada0a98249fed21beb72879326286b2f4c2
2022-02-08 22:30:07 +00:00