diff --git a/service/Android.bp b/service/Android.bp index 38c0110ae3..82f64ba532 100644 --- a/service/Android.bp +++ b/service/Android.bp @@ -114,7 +114,6 @@ cc_library_shared { ], srcs: [ ":services.connectivity-netstats-jni-sources", - "jni/com_android_server_BpfNetMaps.cpp", "jni/com_android_server_connectivity_ClatCoordinator.cpp", "jni/com_android_server_ServiceManagerWrapper.cpp", "jni/com_android_server_TestNetworkService.cpp", diff --git a/service/jni/com_android_server_BpfNetMaps.cpp b/service/jni/com_android_server_BpfNetMaps.cpp deleted file mode 100644 index 29f6a606ff..0000000000 --- a/service/jni/com_android_server_BpfNetMaps.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "bpf/BpfUtils.h" - -#include -#include - -namespace android { - -static jint native_synchronizeKernelRCU(JNIEnv* env, jobject self) { - return -bpf::synchronizeKernelRCU(); -} - -/* - * JNI registration. - */ -// clang-format off -static const JNINativeMethod gMethods[] = { - /* name, signature, funcPtr */ - {"native_synchronizeKernelRCU", "()I", - (void*)native_synchronizeKernelRCU}, -}; -// clang-format on - -int register_com_android_server_BpfNetMaps(JNIEnv* env) { - return jniRegisterNativeMethods(env, "android/net/connectivity/com/android/server/BpfNetMaps", - gMethods, NELEM(gMethods)); -} - -}; // namespace android diff --git a/service/jni/onload.cpp b/service/jni/onload.cpp index 5e89ad0c80..bb70d4fdab 100644 --- a/service/jni/onload.cpp +++ b/service/jni/onload.cpp @@ -22,7 +22,6 @@ namespace android { int register_com_android_server_TestNetworkService(JNIEnv* env); -int register_com_android_server_BpfNetMaps(JNIEnv* env); int register_com_android_server_connectivity_ClatCoordinator(JNIEnv* env); int register_android_server_net_NetworkStatsFactory(JNIEnv* env); int register_android_server_net_NetworkStatsService(JNIEnv* env); @@ -44,10 +43,6 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void*) { } if (android::modules::sdklevel::IsAtLeastT()) { - if (register_com_android_server_BpfNetMaps(env) < 0) { - return JNI_ERR; - } - if (register_com_android_server_connectivity_ClatCoordinator(env) < 0) { return JNI_ERR; } diff --git a/service/src/com/android/server/BpfNetMaps.java b/service/src/com/android/server/BpfNetMaps.java index b7e928d016..93a2602a6b 100644 --- a/service/src/com/android/server/BpfNetMaps.java +++ b/service/src/com/android/server/BpfNetMaps.java @@ -45,6 +45,8 @@ import static android.system.OsConstants.EINVAL; import static android.system.OsConstants.ENODEV; import static android.system.OsConstants.ENOENT; import static android.system.OsConstants.EOPNOTSUPP; +import static android.system.OsConstants.SOCK_RAW; +import static android.system.OsConstants.SOCK_CLOEXEC; import static com.android.server.ConnectivityStatsLog.NETWORK_BPF_MAP_INFO; @@ -324,10 +326,26 @@ public class BpfNetMaps { } /** - * Call synchronize_rcu() + * Synchronously call in to kernel to synchronize_rcu() */ + @RequiresApi(Build.VERSION_CODES.TIRAMISU) public int synchronizeKernelRCU() { - return native_synchronizeKernelRCU(); + // See p/m/C's staticlibs/native/bpf_headers/include/bpf/BpfUtils.h + // for equivalent C implementation of this function. + try { + // When closing socket, kernel calls synchronize_rcu() + // from pf_key's sock_release(). + // Constants from //bionic/libc/include/sys/socket.h: AF_KEY=15 + // and kernel's include/uapi/linux/pfkeyv2.h: PF_KEY_V2=2 + Os.close(Os.socket(15 /*PF_KEY*/, SOCK_RAW | SOCK_CLOEXEC, 2)); + } catch (ErrnoException e) { + // socket() can only fail due to lack of privs (selinux) or OOM, + // close() always succeeds, but may return a pending error, + // however on a freshly opened socket that cannot happen. + // As such this failing is basically a build configuration error. + return -e.errno; + } + return 0; } /** @@ -1061,7 +1079,4 @@ public class BpfNetMaps { pw.decreaseIndent(); } } - - @RequiresApi(Build.VERSION_CODES.TIRAMISU) - private static native int native_synchronizeKernelRCU(); }