[CLATJ#20] ClatdCoordinator: stop bpf for clat

Remove eBPF offload at clat stopping if possible.

Bug: 212345928
Test: build only
because need to test with clatd launched

Change-Id: I6ace77694c0e6fe68d16e80e3c8198aa41385fd2
This commit is contained in:
Hungming Chen
2022-01-17 15:03:36 +08:00
parent b1d3ccbeb6
commit fb676b58e0
2 changed files with 58 additions and 0 deletions

View File

@@ -303,6 +303,25 @@ static jint com_android_server_connectivity_ClatCoordinator_maybeStartBpf(
return 0; // TODO: return forked clatd pid.
}
// TODO: stop clatd and rename to .._stopClatd.
static void com_android_server_connectivity_ClatCoordinator_maybeStopBpf(JNIEnv* env, jobject clazz,
jstring iface, jstring pfx96,
jstring v4, jstring v6,
jint pid /* unused */) {
ScopedUtfChars ifaceStr(env, iface);
ScopedUtfChars pfx96Str(env, pfx96);
ScopedUtfChars v4Str(env, v4);
ScopedUtfChars v6Str(env, v6);
if (!net::clat::initMaps()) {
net::clat::ClatdTracker tracker = {};
if (!initTracker(ifaceStr.c_str(), pfx96Str.c_str(), v4Str.c_str(), v6Str.c_str(),
&tracker)) {
net::clat::maybeStopBpf(tracker);
}
}
}
/*
* JNI registration.
*/
@@ -329,6 +348,9 @@ static const JNINativeMethod gMethods[] = {
"(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Ljava/lang/"
"String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I",
(void*)com_android_server_connectivity_ClatCoordinator_maybeStartBpf},
{"native_maybeStopBpf",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V",
(void*)com_android_server_connectivity_ClatCoordinator_maybeStopBpf},
};
int register_android_server_connectivity_ClatCoordinator(JNIEnv* env) {

View File

@@ -74,6 +74,12 @@ public class ClatCoordinator {
private final Dependencies mDeps;
@Nullable
private String mIface = null;
@Nullable
private String mNat64Prefix = null;
@Nullable
private String mXlatLocalAddress4 = null;
@Nullable
private String mXlatLocalAddress6 = null;
private int mPid = INVALID_PID;
@VisibleForTesting
@@ -171,6 +177,14 @@ public class ClatCoordinator {
@NonNull String v4, @NonNull String v6) throws IOException {
return native_maybeStartBpf(tunfd, readsock6, writesock6, iface, pfx96, v4, v6);
}
/**
* Maybe stop bpf.
*/
public void maybeStopBpf(String iface, String pfx96, String v4, String v6, int pid)
throws IOException {
native_maybeStopBpf(iface, pfx96, v4, v6, pid);
}
}
@VisibleForTesting
@@ -317,6 +331,10 @@ public class ClatCoordinator {
try {
mDeps.maybeStartBpf(tunFd.getFileDescriptor(), readSock6.getFileDescriptor(),
writeSock6.getFileDescriptor(), iface, pfx96, v4, v6);
mIface = iface;
mNat64Prefix = pfx96;
mXlatLocalAddress4 = v4;
mXlatLocalAddress6 = v6;
} catch (IOException e) {
throw new IOException("Error start bpf on " + iface + ": " + e);
}
@@ -325,6 +343,22 @@ public class ClatCoordinator {
return null;
}
/**
* Stop clatd
*/
public void clatStop() throws IOException {
mDeps.maybeStopBpf(mIface, mNat64Prefix, mXlatLocalAddress4, mXlatLocalAddress6,
mPid /* unused */);
// TODO: remove setIptablesDropRule
Log.i(TAG, "clatd on " + mIface + " stopped");
mIface = null;
mNat64Prefix = null;
mXlatLocalAddress4 = null;
mXlatLocalAddress6 = null;
}
private static native String native_selectIpv4Address(String v4addr, int prefixlen)
throws IOException;
private static native String native_generateIpv6Address(String iface, String v4,
@@ -341,4 +375,6 @@ public class ClatCoordinator {
private static native int native_maybeStartBpf(FileDescriptor tunfd, FileDescriptor readsock6,
FileDescriptor writesock6, String iface, String pfx96, String v4, String v6)
throws IOException;
private static native void native_maybeStopBpf(String iface, String pfx96, String v4,
String v6, int pid) throws IOException;
}