[CLATJ#22] ClatCoordinator: stop clatd

provide clatd stop function which stops clatd by pid.

Bug: 212345928
Test: flash and boot
Run "atest ClatCoordinatorTest" in a follow commit.

Change-Id: Icd9c4f9038bf75113fbc5608f213145e58a061d9
This commit is contained in:
Hungming Chen
2021-12-25 21:50:49 +08:00
parent 9625caa8e5
commit 9d11ddfd5c
2 changed files with 27 additions and 14 deletions

View File

@@ -24,6 +24,7 @@
#include <nativehelper/JNIHelp.h> #include <nativehelper/JNIHelp.h>
#include <net/if.h> #include <net/if.h>
#include <spawn.h> #include <spawn.h>
#include <sys/wait.h>
#include <string> #include <string>
#include <netjniutils/netjniutils.h> #include <netjniutils/netjniutils.h>
@@ -425,16 +426,20 @@ static jint com_android_server_connectivity_ClatCoordinator_startClatd(
return pid; return pid;
} }
// TODO: stop clatd and rename to .._stopClatd. static void com_android_server_connectivity_ClatCoordinator_stopClatd(JNIEnv* env, jobject clazz,
static void com_android_server_connectivity_ClatCoordinator_maybeStopBpf(JNIEnv* env, jobject clazz,
jstring iface, jstring pfx96, jstring iface, jstring pfx96,
jstring v4, jstring v6, jstring v4, jstring v6,
jint pid /* unused */) { jint pid) {
ScopedUtfChars ifaceStr(env, iface); ScopedUtfChars ifaceStr(env, iface);
ScopedUtfChars pfx96Str(env, pfx96); ScopedUtfChars pfx96Str(env, pfx96);
ScopedUtfChars v4Str(env, v4); ScopedUtfChars v4Str(env, v4);
ScopedUtfChars v6Str(env, v6); ScopedUtfChars v6Str(env, v6);
if (pid <= 0) {
jniThrowExceptionFmt(env, "java/io/IOException", "Invalid pid");
return;
}
if (!net::clat::initMaps()) { if (!net::clat::initMaps()) {
net::clat::ClatdTracker tracker = {}; net::clat::ClatdTracker tracker = {};
if (!initTracker(ifaceStr.c_str(), pfx96Str.c_str(), v4Str.c_str(), v6Str.c_str(), if (!initTracker(ifaceStr.c_str(), pfx96Str.c_str(), v4Str.c_str(), v6Str.c_str(),
@@ -442,6 +447,9 @@ static void com_android_server_connectivity_ClatCoordinator_maybeStopBpf(JNIEnv*
net::clat::maybeStopBpf(tracker); net::clat::maybeStopBpf(tracker);
} }
} }
kill(pid, SIGTERM);
waitpid(pid, nullptr, 0); // Should we block in JNI?
} }
/* /*
@@ -470,9 +478,9 @@ static const JNINativeMethod gMethods[] = {
"(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Ljava/lang/" "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Ljava/lang/"
"String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I", "String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I",
(void*)com_android_server_connectivity_ClatCoordinator_startClatd}, (void*)com_android_server_connectivity_ClatCoordinator_startClatd},
{"native_maybeStopBpf", {"native_stopClatd",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V",
(void*)com_android_server_connectivity_ClatCoordinator_maybeStopBpf}, (void*)com_android_server_connectivity_ClatCoordinator_stopClatd},
}; };
int register_android_server_connectivity_ClatCoordinator(JNIEnv* env) { int register_android_server_connectivity_ClatCoordinator(JNIEnv* env) {

View File

@@ -179,11 +179,11 @@ public class ClatCoordinator {
} }
/** /**
* Maybe stop bpf. * Stop clatd.
*/ */
public void maybeStopBpf(String iface, String pfx96, String v4, String v6, int pid) public void stopClatd(String iface, String pfx96, String v4, String v6, int pid)
throws IOException { throws IOException {
native_maybeStopBpf(iface, pfx96, v4, v6, pid); native_stopClatd(iface, pfx96, v4, v6, pid);
} }
} }
@@ -349,8 +349,12 @@ public class ClatCoordinator {
* Stop clatd * Stop clatd
*/ */
public void clatStop() throws IOException { public void clatStop() throws IOException {
mDeps.maybeStopBpf(mIface, mNat64Prefix, mXlatLocalAddress4, mXlatLocalAddress6, if (mPid == INVALID_PID) {
mPid /* unused */); throw new IOException("Clatd has not started");
}
Log.i(TAG, "Stopping clatd pid=" + mPid + " on " + mIface);
mDeps.stopClatd(mIface, mNat64Prefix, mXlatLocalAddress4, mXlatLocalAddress6, mPid);
// TODO: remove setIptablesDropRule // TODO: remove setIptablesDropRule
Log.i(TAG, "clatd on " + mIface + " stopped"); Log.i(TAG, "clatd on " + mIface + " stopped");
@@ -359,6 +363,7 @@ public class ClatCoordinator {
mNat64Prefix = null; mNat64Prefix = null;
mXlatLocalAddress4 = null; mXlatLocalAddress4 = null;
mXlatLocalAddress6 = null; mXlatLocalAddress6 = null;
mPid = INVALID_PID;
} }
private static native String native_selectIpv4Address(String v4addr, int prefixlen) private static native String native_selectIpv4Address(String v4addr, int prefixlen)
@@ -377,6 +382,6 @@ public class ClatCoordinator {
private static native int native_startClatd(FileDescriptor tunfd, FileDescriptor readsock6, private static native int native_startClatd(FileDescriptor tunfd, FileDescriptor readsock6,
FileDescriptor writesock6, String iface, String pfx96, String v4, String v6) FileDescriptor writesock6, String iface, String pfx96, String v4, String v6)
throws IOException; throws IOException;
private static native void native_maybeStopBpf(String iface, String pfx96, String v4, private static native void native_stopClatd(String iface, String pfx96, String v4, String v6,
String v6, int pid) throws IOException; int pid) throws IOException;
} }