Commit Graph

85 Commits

Author SHA1 Message Date
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
e8dceddeec enable NETLINK_EXT_ACK extended error reporting
(we don't yet do anything with it, but at least in strace we should
now see more detailed errors from the kernel)

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I6a34a0e7e866bbde41daef23870ad90dceee6ded
2022-06-10 21:10:15 +00:00
Maciej Żenczykowski
0b2781b5ec add ALOGW/ALOGI/ALOGD to logging.h
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I99c7892f10f89a81e0c18838328aa7f945a3817a
2022-06-10 19:00:07 +00:00
Maciej Żenczykowski
023ad6a087 fix sign of error to be positive when passed to strerror()
Currently we see:
  E TcUtils : NLMSG_ERROR message return error: -2
  E ConnectivityService: TcUtils.tcFilterAddDevIngressPolice(ifaceIndex=6, PRIO_POLICE, ETH_P_ALL, rateInBytesPerSecond=2500000, bpfProgPath=/sys/fs/bpf/netd_shared/prog_netd_schedact_ingress_account) failure:
  E ConnectivityService: java.io.IOException: com_android_net_module_util_TcUtils_tcFilterAddDevIngressPolice error: : Unknown error -2

Bug: 231495412
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib49171e13d06082a37cbb12def1285d8875b5279
2022-06-09 22:35:13 +00: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
Maciej Żenczykowski
28e5347154 BpfMap: cache bpf map file descriptors
We switch back to int from ParcelFileDescriptor,
and eliminate all calls to close().  Bpf Map FDs
now live till process exit.

Bug: 230880517
Test: TreeHugger, atest com.android.networkstack.tethering.BpfMapTest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I89b6dc88ea56cb1e50695f8daf54ed79bce3fba2
2022-05-19 01:26:55 -07:00
Hungming Chen
b411547dd2 BpfMap: wrap native fd with ParcelFileDescriptor to avoid fd leak
ParcelFileDescriptor has implemented finalize(). Wrap native fd into
ParcelFileDescriptor which helps to release fd automatically.

Bug: 230880517
Test: manual test
Steps:
1. Connect to IPv6 only wifi and clat maps are created
$ adb shell cmd wifi set-wifi-enabled enabled
05-12 13:53:41.182  1793  2031 W BpfMap  : open /sys/fs/bpf/net_shared/map_clatd_clat_ingress6_map..: 493
05-12 13:53:41.182  1793  2031 W BpfMap  : open /sys/fs/bpf/net_shared/map_clatd_clat_egress4_map..: 546

$ adb shell ls -all proc/1793/fd | grep bpf
.. system system 64 2022-05-12 13:55:35 .. 493 -> anon_inode:bpf-map
.. system system 64 2022-05-12 13:55:35 .. 546 -> anon_inode:bpf-map

$ adb shell dumpsys connectivity
Forwarding rules:
  BPF ingress map: iif nat64Prefix v6Addr -> v4Addr oif
    47 /64:ff9b::/96 /2a00:79e1:abc:6f02:6efd:1d4b:f05e:25bd -> /192.0.0.4 54
  BPF egress map: iif v4Addr -> v6Addr nat64Prefix oif
    54 /192.0.0.4 -> /2a00:79e1:abc:6f02:6efd:1d4b:f05e:25bd /64:ff9b::/96 47 ether

2. Disconnect from IPv6 only wifi, force GC and clat map fds are released
$ adb shell cmd wifi set-wifi-enabled disabled
$ adb shell kill -10 1793
$ adb shell ls -all proc/1793/fd | grep bpf
(fd 493 and 546 are removed)

Change-Id: I26bbafbd73eccab6f4ae2c71690ecad12bbef7df
2022-05-12 15:41:19 +08:00
Patrick Rohr
ba48d10969 Make libtcutils visible to netd tests
Test: TH
Change-Id: I71943cfd2fb72dca8a7ffd5d063860b89be47ca1
2022-03-30 09:38:06 +02:00
Hungming Chen
556c8010c9 TcUtils: add tcQdiscAddDevClsact
Support tc command:
$ tc qdisc add dev .. clsact

Test: TreeHugger
Change-Id: I98abcb59418ab12b6e4de0f42a18ded4677ddbfc
2022-03-17 17:23:04 +08: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
c434f7fa6e Merge "BpfUtils.h - switch to GTEST_SKIP in all 4 SKIP macros" 2022-03-02 08:53:01 +00: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
Tyler Wear
e48ee4a864 Bpf Util Lib in Common Location
Add bpf lib to load and attach BPF programs in commonly accesible
location.

Bug: 179733303

Change-Id: I17b68abb14e816e7210bc603ffda44e10fb73d2f
2022-03-01 09:09:36 -08:00
Maciej Żenczykowski
4f8654ce07 Merge "Fix wrong nla_type for matchall" 2022-02-11 20:42:35 +00:00
Patrick Rohr
20dcaf5f70 Fix wrong nla_type for matchall
TCA_U32_ACT was missed when converting from the u32 filter to matchall.
Interestingly, cls_matchall does not seem to validate this type as the
kernel happily accepted this configuration (which absolutely should not
happen as TCA_U32_ACT is greater than __TCA_MATCHALL_MAX).

Bug: 218840346
Test: atest CtsNetTestCases:RateLimitTest
Change-Id: Ia24683cbd5fbd10084163db6e6a4415ec03f6f3f
2022-02-11 13:24:51 +01:00
Maciej Żenczykowski
62b3d9fbb2 Merge "expose bpf_syscall_wrappers to art apex" 2022-02-10 23:26:49 +00:00
Maciej Żenczykowski
4395ff6815 Merge "switch from libbpf_android to bpf_headers" 2022-02-10 19:03:33 +00:00
Maciej Żenczykowski
2645d2374b Merge "bpf_headers/bpf_syscall_wrappers - remove visibility restrictions" 2022-02-10 19:03:15 +00:00
Maciej Żenczykowski
a11e698749 expose bpf_syscall_wrappers to art apex
The art apex already has access to bpf_headers which includes bpf_syscall_wrappers,
but this extra line is needed to switch it away from needlessly linking libbpf_android.

Since these are header files only, this annotation is kind of pointless,
but it does turn out to be needed...

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9e4d3373cea488080304e31ed7c8708aec988933
2022-02-09 22:23:01 -08: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
Maciej Żenczykowski
b751aee9ab Merge "Add attach bpf with flags and detach single bpf" 2022-02-10 02:26:56 +00:00
KaiWen Zheng
cfe2f2a1c9 Add attach bpf with flags and detach single bpf
Attach bpf program to cgroup with flags, support 'BPF_F_ALLOW_OVERRIDE'
and 'BPF_F_ALLOW_MULTI'; corresponding to detach a single bpf program in
a cgroup.

Test: m
Signed-off-by: KaiWen Zheng <zhengkaiwen5@xiaomi.corp-partner.google.com>
Change-Id: I9a4d2dcb249c6c628e3f1287d7d5d1879b722bd6
2022-02-09 19:27:58 +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
Patrick Rohr
23077d5a49 Include libtcutils inside libnet_utils_device_common_bpfjni
This way, users of libnet_utils_device_common_bpfjni do not also have to
separately list the required libtcutils.

Test: build, boots
Change-Id: Id40863de83b6c40b79f38d638299626f7e025810
2022-02-01 03:07:46 +00:00
Bob Badour
ae85125761 [LSC] Add LOCAL_LICENSE_KINDS to frameworks/libs/net
Added SPDX-license-identifier-Apache-2.0 to:
  common/native/nettestutils/Android.bp

Bug: 68860345
Bug: 151177513
Bug: 151953481

Test: m all
Change-Id: If5fcd4735fe82f336227626a443bba1d74201722
2022-01-27 10:46:23 -08:00
Hungming Chen
51fecf19e2 libclat: Use space indentation instead of tab
Correct the indentation style of libclat in the follows
bpf_headers/Android.bp
bpf_syscall_wrappers/Android.bp

Test: build
Change-Id: I7cdd354141124fa8f7b4a864ddd937bef4676302
2022-01-25 20:09:09 +08:00
Maciej Żenczykowski
ba9eeabd14 Merge "Add BPF_ANNOTATE_KV_PAIR to BPF map definition macros" 2022-01-25 02:31:59 +00:00
Maciej Żenczykowski
4772d859a7 Merge changes from topic "configure_bpf_for_clat"
* changes:
  make bpf_headers visible to libclat
  make bpf_syscall_wrappers visible to libclat
2022-01-24 19:14:35 +00:00
Maciej Żenczykowski
b7bc68fd76 Merge changes from topic "bpfinterfacemap"
* changes:
  Decouple "net-utils-framework-common" from "net-utils-device-common-bpf"
  Add visibility for use bpfmap by BpfInterfaceMapUpdater
2022-01-24 19:05:27 +00:00
Ken Chen
945c8b2638 Clean up bpf_headers visibility
The bpf_headers no need to explicitly specify visibility for
system/netd/libnetdbpf. The libnetdbpf depends on
bpf_connectivity_headers, which exports bpf_headers already.

Bug: 202086915
Test: m
Change-Id: I09a073b14dc74511ba9dc09a89ae6e89dff51f4d
2022-01-23 15:57:25 +08:00
markchien
32a6e9cbde Add visibility for use bpfmap by BpfInterfaceMapUpdater
Bug: 215095957
Test: m
Change-Id: Idbb8e3cbf520a29444ccdfd2d1c42553c886a7f0
2022-01-21 22:33:38 +08:00
Connor O'Brien
56875455c2 Add BPF_ANNOTATE_KV_PAIR to BPF map definition macros
BPF_ANNOTATE_KV_PAIR is used by libbpf to associate each map with its
BTF information. This is ignored for programs compiled without BTF
information.

Bug: 203823368
Test: build & boot; confirm all bpf progs load successfully
Test: compile time_in_state.o with -g; confirm BTF is loaded
Signed-off-by: Connor O'Brien <connoro@google.com>
Change-Id: I45f294945d48106d39d1d105ac4f49311459a19d
2022-01-20 21:22:24 -08:00
Maciej Żenczykowski
171da02e3f Merge changes from topic "tc-police"
* changes:
  Move logging helper to own file
  Add tc police support in TcUtils.java
  Add tc police support to jni lib
  Add functionality to attach tc-police action to ingress qdisc
  Add tests for adding / deleting bpf filter
  Add support to attach / replace / detach clsact qdisc to libtcutils
  Add tests for libtcutils
  Fix kernelversion.h
2022-01-21 01:52:42 +00:00
Ken Chen
76ee6106f5 Add libnettestutils as a networking test utility
The library provides a helper function that can retrieve dumpsys
results from a specific service. It can also take dumpsys arguments.

Bug: 29761778
Bug: 202086915
Test: test in Ib4aeb385ab80187feb39b659274a0aee20a4a5ed

Change-Id: Ib2ab8b6e4136bd3cbc52b025e53a1fa0c4e91709
2022-01-20 01:38:00 +08:00
Patrick Rohr
28c717a03e Move logging helper to own file
And rename to ALOGE to be in line with liblog.

Test: builds
Change-Id: I6c33f78a0649b273f75ebf05ea5ba3689beabc24
2022-01-19 14:54:37 +01:00
Patrick Rohr
eb3a8c734b Add tc police support to jni lib
Bug: 157552970
Test: TreeHugger
Change-Id: I63b4324bbb8d20b7d401921dad1f8e73ccc26a39
2022-01-19 14:20:27 +01:00
Patrick Rohr
e815a7462f Add functionality to attach tc-police action to ingress qdisc
This is used for go/bandwidth-limiting.

Bug: 157552970
Test: atest LibTcUtilsTest
Change-Id: Ic8e4d8dc016b14a6d4fc8ddbb3949941e9ef95af
2022-01-19 14:20:27 +01:00
Patrick Rohr
1f7ba259c3 Add tests for adding / deleting bpf filter
Bug: 202086915
Bug: 157552970
Test: atest LibTcUtilsTest
Change-Id: I8adcb3df29e4b3e0c558714f90b2840698e40cb9
2022-01-19 14:20:27 +01:00
Patrick Rohr
42b58ae836 Add support to attach / replace / detach clsact qdisc to libtcutils
Bug: 202086915
Bug: 157552970
Test: atest LibTcUtilsTest
Change-Id: I2f2e2f66acb7590e8951ac6e178c5893ba635597
2022-01-19 14:20:27 +01:00