Merge "switch SynchronizeKernelRCU back to jni - but from BpfMap" into main

This commit is contained in:
Maciej Żenczykowski 2023-11-09 17:40:53 +00:00 • committed by Gerrit Code Review
commit 2a0a856c17
3 changed files with 28 additions and 13 deletions

View file

@ -45,8 +45,6 @@ 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;
@ -327,19 +325,9 @@ public class BpfNetMaps {
*/
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
public int 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));
BpfMap.synchronizeKernelRCU();
} 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;

View file

@ -239,6 +239,11 @@ public class BpfMap<K extends Struct, V extends Struct> implements IBpfMap<K, V>
return Struct.parse(mValueClass, buffer);
}
/** Synchronize Kernel RCU */
public static void synchronizeKernelRCU() throws ErrnoException {
nativeSynchronizeKernelRCU();
}
private static native int nativeBpfFdGet(String path, int mode, int keySize, int valueSize)
throws ErrnoException, NullPointerException;
@ -260,4 +265,6 @@ public class BpfMap<K extends Struct, V extends Struct> implements IBpfMap<K, V>
private native boolean nativeFindMapEntry(int fd, byte[] key, byte[] value)
throws ErrnoException;
private static native void nativeSynchronizeKernelRCU() throws ErrnoException;
}

View file

@ -15,6 +15,8 @@
*/
#include <errno.h>
#include <linux/pfkeyv2.h>
#include <sys/socket.h>
#include <jni.h>
#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedLocalRef.h>
@ -117,6 +119,22 @@ static jboolean com_android_net_module_util_BpfMap_nativeFindMapEntry(JNIEnv *en
return throwIfNotEnoent(env, "nativeFindMapEntry", ret, errno);
}
static void com_android_net_module_util_BpfMap_nativeSynchronizeKernelRCU(JNIEnv *env,
jclass clazz) {
const int pfSocket = socket(AF_KEY, SOCK_RAW | SOCK_CLOEXEC, PF_KEY_V2);
if (pfSocket < 0) {
jniThrowErrnoException(env, "nativeSynchronizeKernelRCU:socket", errno);
return;
}
if (close(pfSocket)) {
jniThrowErrnoException(env, "nativeSynchronizeKernelRCU:close", errno);
return;
}
return;
}
/*
* JNI registration.
*/
@ -132,6 +150,8 @@ static const JNINativeMethod gMethods[] = {
(void*) com_android_net_module_util_BpfMap_nativeGetNextMapKey },
{ "nativeFindMapEntry", "(I[B[B)Z",
(void*) com_android_net_module_util_BpfMap_nativeFindMapEntry },
{ "nativeSynchronizeKernelRCU", "()V",
(void*) com_android_net_module_util_BpfMap_nativeSynchronizeKernelRCU },
};