Clear counters and delete tag data from NetworkStatsService.

Currently, NetworkStatsService deletes tag data by calling
NetworkManagementSocketTagger, which then calls into libcutils
"qtaguid" code. Instead, make NetworkStatsService call into
libcutils directly and delete the NetworkManagementSocketTagger
code.

In the future, this will make it easier for NetworkStatsService
to perform this operation by calling directly into BpfNetMaps.

Because the unit test does not yet have working JNI code, provide
an internal TagStatsDeleter interface that can be mocked out via
the Dependencies class. This is a bit ugly but it will be deleted
as soon as the code uses BpfNetMaps directly.

Delete NetworkManagementSocketTagger#setKernelCounterSet since it
was replaced in aosp/1958917.

Also remove unused includes and make formatting changes suggested
by clang-format.

Test: m
Test: atest NetworkStatsServiceTest
Test: atest NetworkUsageStatsTest
Test: atest TrafficStatsTest
Test: stats deleted when CtsUsageStatsTestCases completes and test APK is uninstalled
Change-Id: I62987000afc185199821580232bfb7668c8e301e
This commit is contained in:
Lorenzo Colitti
2022-01-27 15:37:43 +09:00
parent a522e3ddd1
commit a8ac57607e
2 changed files with 26 additions and 8 deletions

View File

@@ -59,7 +59,6 @@ import static android.text.format.DateUtils.SECOND_IN_MILLIS;
import static com.android.net.module.util.NetworkCapabilitiesUtils.getDisplayTransport; import static com.android.net.module.util.NetworkCapabilitiesUtils.getDisplayTransport;
import static com.android.net.module.util.NetworkStatsUtils.LIMIT_GLOBAL_ALERT; import static com.android.net.module.util.NetworkStatsUtils.LIMIT_GLOBAL_ALERT;
import static com.android.server.NetworkManagementSocketTagger.resetKernelUidStats;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
@@ -121,6 +120,7 @@ import android.provider.Settings.Global;
import android.service.NetworkInterfaceProto; import android.service.NetworkInterfaceProto;
import android.service.NetworkStatsServiceDumpProto; import android.service.NetworkStatsServiceDumpProto;
import android.system.ErrnoException; import android.system.ErrnoException;
import android.system.Os;
import android.telephony.PhoneStateListener; import android.telephony.PhoneStateListener;
import android.telephony.SubscriptionPlan; import android.telephony.SubscriptionPlan;
import android.text.TextUtils; import android.text.TextUtils;
@@ -546,6 +546,10 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
return null; return null;
} }
} }
public TagStatsDeleter getTagStatsDeleter() {
return NetworkStatsService::nativeDeleteTagData;
}
} }
/** /**
@@ -1801,7 +1805,10 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
// Clear kernel stats associated with UID // Clear kernel stats associated with UID
for (int uid : uids) { for (int uid : uids) {
resetKernelUidStats(uid); final int ret = mDeps.getTagStatsDeleter().deleteTagData(uid);
if (ret < 0) {
Log.w(TAG, "problem clearing counters for uid " + uid + ": " + Os.strerror(-ret));
}
} }
} }
@@ -2380,4 +2387,12 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
private static native long nativeGetTotalStat(int type); private static native long nativeGetTotalStat(int type);
private static native long nativeGetIfaceStat(String iface, int type); private static native long nativeGetIfaceStat(String iface, int type);
private static native long nativeGetUidStat(int uid, int type); private static native long nativeGetUidStat(int uid, int type);
// TODO: use BpfNetMaps to delete tag data and remove this.
@VisibleForTesting
interface TagStatsDeleter {
int deleteTagData(int uid);
}
private static native int nativeDeleteTagData(int uid);
} }

View File

@@ -16,20 +16,18 @@
#define LOG_TAG "NetworkStatsNative" #define LOG_TAG "NetworkStatsNative"
#include <cutils/qtaguid.h>
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <inttypes.h> #include <inttypes.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "core_jni_helpers.h"
#include <jni.h> #include <jni.h>
#include <nativehelper/ScopedUtfChars.h> #include <nativehelper/ScopedUtfChars.h>
#include <utils/misc.h> #include <sys/stat.h>
#include <sys/types.h>
#include <utils/Log.h> #include <utils/Log.h>
#include <utils/misc.h>
#include "android-base/unique_fd.h"
#include "bpf/BpfUtils.h" #include "bpf/BpfUtils.h"
#include "netdbpf/BpfNetworkStats.h" #include "netdbpf/BpfNetworkStats.h"
@@ -104,10 +102,15 @@ static jlong getUidStat(JNIEnv* env, jclass clazz, jint uid, jint type) {
} }
} }
static int deleteTagData(JNIEnv* /* env */, jclass /* clazz */, jint uid) {
return qtaguid_deleteTagData(0, uid);
}
static const JNINativeMethod gMethods[] = { static const JNINativeMethod gMethods[] = {
{"nativeGetTotalStat", "(I)J", (void*)getTotalStat}, {"nativeGetTotalStat", "(I)J", (void*)getTotalStat},
{"nativeGetIfaceStat", "(Ljava/lang/String;I)J", (void*)getIfaceStat}, {"nativeGetIfaceStat", "(Ljava/lang/String;I)J", (void*)getIfaceStat},
{"nativeGetUidStat", "(II)J", (void*)getUidStat}, {"nativeGetUidStat", "(II)J", (void*)getUidStat},
{"nativeDeleteTagData", "(I)I", (void*)deleteTagData},
}; };
int register_android_server_net_NetworkStatsService(JNIEnv* env) { int register_android_server_net_NetworkStatsService(JNIEnv* env) {