This seems to be the most common usecase.
Test: TreeHugger
Flag: EXEMPT PURE_REFACTOR
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I2de4d067b2f20680539cbddb31555864bcf957b1
Maciej Żenczykowskicommited 2025-09-12 10:14:12 -07:00
There's been some reports that this can cause process termination due
to gc/sleep logic thinking the thread is stuck for >4s.
While this implies that RCU is stuck for >4s, maybe this is something
that can happen on lower power devices.
It seems prudent to (at least temporarily) revert back to the old
non-CriticalNative behaviour.
Test: TreeHugger
Flag: EXEMPT mainline, bugfix
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I56b2802132f252a02411a9a8f762d98ab2f336ad
Maciej Żenczykowskicommited 2025-09-11 13:01:08 -07:00
Mainline M-2025-11 drops support for android R,
thus making the new minimum Android 12 / S (api 31)
Once we get this to pass tests, we can merge it,
then we need to bump min sdk to 31, then we can
probably get rid of the dlsym stuff and just link it directly.
Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com
Change-Id: I9d68ea9f4aa180b3ec24d252bcd49593e19cf475
Maciej Żenczykowskicommited 2025-08-26 09:56:44 -07:00
Convert BpfMap native methods to use @FastNative to reduce JNI overhead.
Refactor synchronizeKernelRCU() to be @CriticalNative for maximum
performance by changing its error handling to return an error code
instead of throwing directly from native code.
(In practice we don't expect it to *ever* fail)
FYI:
@FastNative and @CriticalNative are Android-specific annotations
that signal to the ART runtime to use a lower-overhead JNI transition,
resulting in better performance.
A standard JNI call tells the Garbage Collector (GC) that the thread
is transitioning to a 'Native' state. This is a signal to the GC that
the thread might be busy for a while and shouldn't be suspended arbitrarily.
@FastNative is for short, quick functions:
This provides a moderately faster JNI call (thread is still 'Runnable').
The native function signature remains the same as a standard JNI method,
and it can still access the JNIEnv* to interact with Java objects
or throw exceptions.
@CriticalNative is for short, quick & trivial functions:
This provides the fastest possible JNI transition and is intended
for simple, performance-critical leaf functions.
It comes with strict requirements:
- The native method cannot throw exceptions, create Java objects,
or make any other JNI calls.
- All arguments and the return type must be primitive, ie:
jboolean [bool]
jbyte [s8]
jchar [u16]
jshort [s16]
jint [s32]
jlong [s64]
jfloat [32-bit]
jdouble [64-bit]
void
Java objects like String or byte[] are not allowed.
Because of these restrictions, the native C++ function signature
must not include JNIEnv* or jobject/jclass parameters.
(This likely also means it must be 'static')
Test: TreeHugger
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9b6107b26086df2e87218ff9274593e9f2eb6b0b
Maciej Żenczykowskicommited 2025-08-12 15:28:05 -07:00
AServiceManager_waitForService sytem APIs currently only callable
via dlsym(): b/376759605. When built with sdk_version current,
the NDK stubs for "sdk" variants only include NDK APIs,
excluding #systemapi APIs.
Flag: EXEMPT Mechanical refactors
Test: atest ServiceManagerWrapperIntegrationTest
Change-Id: I48f690d771c7905e79e463617ded772f753cc854
Mark Chienauthored and Patrick Rohrcommited 2025-08-06 10:44:39 -07:00
This adds a JNI function to retrieve an interface's driver name via
ethtool API, which will be used to identify USB NCM interfaces.
Unfortunately, SIOCETHTOOL (ETHTOOL_GDRVINFO in particular) does not
allow passing in the interface index, meaning that this operation is
racy, i.e. the interface might have been replaced when
getDriverNameForInterface() is called.
SICOETHTOOL is slowly being replaced by a netlink alternative that
allows querying by interface index but, unfortunately, does not support
ETHTOOL_GDRVINFO yet.
Test: verified manually; automated test will be added shortly.
Flag: EXEMPT mainline
Bug: 1000001
Change-Id: I8bec47e36a607e7d8fe2fac2eebcd8dff0360a02
Additionally, when calling isEthernet(null), throw NPE instead of
SIGSEGV.
Test: New test in this patch
Change-Id: Ie80b860d4befe65a1777d0ebeb18d1807c6375d0
Chalard Jeanauthored and Patrick Rohrcommited 2025-03-19 08:24:45 -07:00
Ignore this error code during DEL operations.
It is returned when the specific thing to be deleted
is not found and is therefore a valid response.
Gets rid of 30+ logs worth of:
E TcUtils : NLMSG_ERROR message return error: -2
during netd startup (cleanup phase).
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie542f8ff418dbed604eea5b31b87f9b974ccdbcb
Maciej Żenczykowskicommited 2025-02-06 12:56:09 -08:00
Several extra callbacks are sent after the first timer expiration
notification. This occurs because data from the FileDescriptor
must be read after the timer expires. Otherwise, expiration
callbacks will continue to be sent, notifying of unread data.
This change also adds TFD_NONBLOCK to timerfd creation to ensure
reading safety.
Test: Manually verify that the callback is sent only once
Change-Id: I30559ed93d0a742b98a58d33d83723f7a155fbd9
The timerfd delivers timer expiration notifications via a file
descriptor, which can be used to implement delayed callbacks.
Create a JNI class to interface with the timerfd system calls.
Bug: 366373064
Test: m
Change-Id: I0ae7a4cf618838e614b2fa4523b55a4d0e8f74e2
Generated via:
git mv staticlibs/native/bpf_headers bpf/headers
cd bpf/headers/include
git mv bpf/bpf_helpers.h .
git mv bpf/bpf_map_def.h .
manually fix up bpf/bpf_map_def.h include path
bpf_helpers.h is only used by bpf programs so the 'bpf/'
prefix is spurious.
bpf_map_def.h is only used by bpf programs and the two
bpfloaders, which effectively means the 'bpf/' prefix isn't
useful.
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic3fc4e354a15821c27c23b7dbc019daad9f6ad85
Maciej Żenczykowskicommited 2024-08-21 13:18:59 -07:00
cat'ing this text file from a terminal results in
shell prompt being in the middle of the screen - fix it.
Test: N/A
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iabc6225e2698b13013be2c630f0c415c8832817f
Maciej Żenczykowskicommited 2024-08-20 19:52:18 -07:00
Step 3 of moving bpf related stuff into a common directory,
will ease development.
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9ec90e2f58c68e17d6e83b48b8dea2cdf31a3694
Maciej Żenczykowskicommited 2024-08-20 19:51:50 -07:00
Since all mainline shipped bpf .o files have BTF loading disabled,
we might as well make it the default.
Especially since currently neither the platform nor the mainline
bpf loaders even support BTF and thus don't even parse these settings.
We do need to keep it around to prevent a boot time regression on
(at least) Android U.
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ibc6f9d7168c1d61c435de7e641cc0aaa5db4603a
Maciej Żenczykowskicommited 2024-08-17 02:06:51 +00:00
Afterwards:
git grep 'DEFAULT_BPFLOADER_(MIN|MAX)_VER'
comes up empty
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9c7736c2c247987263e40c336bdea3a135cfda9a
Maciej Żenczykowskicommited 2024-08-15 16:15:13 -07:00
This hasn't ever been useful.
Afterwards:
git grep 'DEFAULT_BPF_PROG_(SELINUX_CONTEXT|PIN_SUBDIR)'
comes up empty
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I7694d0bb3aa4e424247da7f79251412ff549ce63
Maciej Żenczykowskicommited 2024-08-15 15:21:15 -07:00
Both the loader and all the bpf .o's are in the same apex, they're thus
built from the same source and have the same struct bpf_map_def
definition, and this is no longer required.
We do still need to populate the field in the .o's though,
since offload.o and test.o are still read by the Android S
platform bpfloader.
Afterwards:
git grep 'DEFAULT_SIZEOF_BPF_MAP_DEF|sizeOfBpfMapDef'
is empty
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I152151e3713b1002f603f298dae060decb4c19fe
Maciej Żenczykowskicommited 2024-08-15 15:17:22 -07:00
Both the loader and all the bpf .o's are in the same apex, they're thus
built from the same source and have the same struct bpf_prog_def
definition, and this is no longer required.
We do still need to populate the field in the .o's though,
since offload.o and test.o are still read by the Android S
platform bpfloader.
Afterwards:
git grep 'DEFAULT_SIZEOF_BPF_PROG_DEF|sizeOfBpfProgDef'
is empty
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ibb82f5ffda7ff5404eb3217067a9d7bcec34ce94
Maciej Żenczykowskicommited 2024-08-15 15:04:39 -07:00
This allows subclasses to override the destructor and perform additional cleanup.
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic962202ad7406759dacd7371b87ef92f0249ab84
Maciej Żenczykowskicommited 2024-07-24 19:17:54 +00:00
Currently there's no mechanism to subscribe to changes in a BpfRingbuf,
and the existing wait() method can only block on a single bpf
ringbuffer.
Hence, add methods to interface with epoll_ctl() system call.
Also in code review of previous patchsets it came to light
that the constructor is needlessly 'private', thus change it
to 'protected' so that it can be subclassed.
Bug: 350094404
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I4d9b841f5e984c42acab629a723e69920c00b356
Isak Evaldssonauthored and Maciej Żenczykowskicommited 2024-07-24 07:31:05 +00:00
This reverts commit 231598bc92.
This is targetting mainline release M-2024-09.
Note: left the failure message in netbpfload/netbpfload.33rc
to mention 'netbpfload-failed' and not 'bpfloader-failed'.
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iaf99360a215b1a74bda457ed55b7f56a5a3f2b48
Maciej Żenczykowskicommited 2024-06-21 15:46:42 +00:00
As currently implemented, it is not safe to open (and mmap) the
same kernel -> userspace ringbuffer multiple times.
Note: it's likely, that as implemented, even a single instance
is not thread safe without external locking.
Test: TreeHugger, atest bpf_existence_test bpf_module_test bpf-time-in-state-tests libbpf_android_test
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iea6908746a58e7412c0ce89da29a764aefeaef47
Maciej Żenczykowskicommited 2024-06-15 03:58:24 -07:00
the clat verifyPerms() checker only wants to check the
existence and accessibility of the maps.
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I3079ab316bcd88370d774fec5e88c1b5ff438266
Maciej Żenczykowskicommited 2024-06-15 03:54:09 -07:00
Per commit description of https://android-review.git.corp.google.com/c/platform/packages/modules/Connectivity/+/3109108 "Use SingleWriterBpfMap in BpfNetMaps":
- firewall.cpp writes to sConfigurationMap, sUidOwnerMap, and
sDataSaverEnabledMap, but that code only runs in tests because
it requires root privileges.
So either those maps need to not use SingleWriterBpfMap
in the BpfNetMaps (which would affect performance),
or we need to prevent the tests from grabbing the locks,
or the tests will (presumably) fail.
This avoids grabbing the locks from the test, even though
this is absolutely terrible and likely breaks things...
But in general the entire firewall.cpp file (and libconnectivity_native_test_utils target) are just one
large modularization violation in need of major escalation...
Test via:
atest resolv_{gold,stress,integration,unit}_test
Without this patch:
resolv_gold_test: Passed: 16
resolv_integration_test: Passed: 53, Failed: 2, Ignored: 118, Assumption Failed: 0 (Completed With ERRORS)
resolv_stress_test: Passed: 2
resolv_unit_test: Passed: 124, Failed: 2
4 tests failed
--------------
ResolverMultinetworkTest#DnsWithVpn
PrivateDns/TransportParameterizedTest#BlockDnsQuery/DoT
CallbackTest#tagSocketCallback
ResolvGetAddrInfoTest#InvalidParameters
With this patch:
resolv_gold_test: Passed: 16
resolv_integration_test: Passed: 178
resolv_stress_test: Passed: 2
resolv_unit_test: Passed: 126
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie97d015ef66c3864a23484e7ed190fe7d091af5c
Maciej Żenczykowskicommited 2024-06-14 17:35:29 -07:00
The java bpfmap fd cache has some implications:
- this won't protect against two SingleWriterBpfMap instantiations
for the same map (path) in the same process
- we don't release any locks on BpfMap destruction.
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ibb0b1f57974c307ea5e0fa699ce8d598c59f456e
Maciej Żenczykowskicommited 2024-06-14 15:56:01 -07:00
Only the non-CRITICAL test@mainline.o is loaded from netbpfload
on T and U < QPR3, everything else is loaded by platform bpfloader
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic07a57cf93851d3a1411b62c97d8ce057507360b
Maciej Żenczykowskicommited 2024-06-14 15:09:41 -07:00
Per inspection of:
https://source.corp.google.com/h/prodkernel/kernel/upstream/torvalds/linux-2.6/+/master:kernel/bpf/syscall.c;drc=2884dc7d08d98a89d8d65121524bb7533183a63a;l=5786
bpf system call returns -EINVAL on unknown operations:
static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
{
...
switch (cmd) {
...
case BPF_OBJ_GET_INFO_BY_FD:
err = bpf_obj_get_info_by_fd(&attr, uattr.user);
break;
..
default:
err = -EINVAL;
break;
}
return err;
}
Equivalent logic is present in ACK 4.9-Q:
SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
This is what should thus be returned on 4.9-T devices.
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I124686ca360513f45e17afba23efa87acf1ce480
Maciej Żenczykowskicommited 2024-06-14 14:25:27 -07:00
and add an accessor to grab an exclusive lock on a R/W map open
(such a map could be accessed with a write-through cache)
Note: we can't grab a flock as that would occupy the full inode,
and all bpfmaps are actually (currently) the *same* anonymous inode.
As such we actually grab a lock on a range (a single byte),
the offset being determined by the unique bpf map id.
We include some very simple, but sufficient, correctness tests
in the critical boot path: this is to prevent any surprises
caused by kernel implementation changes.
$ adb root && sleep 1 && adb wait-for-device shell grep OFDLCK /proc/locks
id: OFDLCK ADVISORY [WRITE|READ] pid blkmaj:min:inode min_offset max_offset
11: OFDLCK ADVISORY READ -1 00:0e:1048 36 36
14: OFDLCK ADVISORY READ -1 00:0e:1048 35 35
15: OFDLCK ADVISORY READ -1 00:0e:1048 41 41
16: OFDLCK ADVISORY READ -1 00:0e:1048 40 40
22: OFDLCK ADVISORY READ -1 00:0e:1048 24 24
23: OFDLCK ADVISORY READ -1 00:0e:1048 17 17
24: OFDLCK ADVISORY READ -1 00:0e:1048 16 16
25: OFDLCK ADVISORY READ -1 00:0e:1048 13 13
OFDLCK probably means 'Open File Descriptor LoCK' since an OFDLCK
is associated with (held by) the file descriptor and not a process/pid,
on the given (anonymous in this case) block device + inode.
Where READ=shared and WRITE=exclusive.
There are (as yet) no exclusive locks held post boot.
The pid field is unfortunately always -1 (and cannot be manually set).
The 00:0e:1048 (or at least the inode portion) is random
(likely depends on boot ordering)
The final two fields (min and max offset) are the bpf map id.
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I208e3450da3fe4689ad5fd578539f401f25a4fef
Maciej Żenczykowskicommited 2024-06-14 10:51:25 +00:00
when the program is missing due to too old kernel.
Program presence is already enforced by CRITICAL annotation and bpfloader.
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic5f53636c833e65c611a7b8069f28c088c249158
Maciej Żenczykowskicommited 2024-04-26 22:48:30 +00:00