Merge changes from topic "tc-police"
* changes:
Move logging helper to own file
Add tc police support in TcUtils.java
Add tc police support to jni lib
Add functionality to attach tc-police action to ingress qdisc
Add tests for adding / deleting bpf filter
Add support to attach / replace / detach clsact qdisc to libtcutils
Add tests for libtcutils
Fix kernelversion.h
diff --git a/staticlibs/framework/com/android/net/module/util/NetworkStatsUtils.java b/staticlibs/framework/com/android/net/module/util/NetworkStatsUtils.java
index 28ff770..c4d415e 100644
--- a/staticlibs/framework/com/android/net/module/util/NetworkStatsUtils.java
+++ b/staticlibs/framework/com/android/net/module/util/NetworkStatsUtils.java
@@ -16,12 +16,23 @@
package com.android.net.module.util;
+import android.app.usage.NetworkStats;
+
+import com.android.internal.annotations.VisibleForTesting;
+
/**
* Various utilities used for NetworkStats related code.
*
* @hide
*/
public class NetworkStatsUtils {
+ // These constants must be synced with the definition in android.net.NetworkStats.
+ // TODO: update to formal APIs once all downstreams have these APIs.
+ private static final int SET_ALL = -1;
+ private static final int METERED_ALL = -1;
+ private static final int ROAMING_ALL = -1;
+ private static final int DEFAULT_NETWORK_ALL = -1;
+
/**
* Safely multiple a value by a rational.
* <p>
@@ -88,4 +99,77 @@
if (low > high) throw new IllegalArgumentException("low(" + low + ") > high(" + high + ")");
return amount < low ? low : (amount > high ? high : amount);
}
+
+ /**
+ * Convert structure from android.app.usage.NetworkStats to android.net.NetworkStats.
+ */
+ public static android.net.NetworkStats fromPublicNetworkStats(
+ NetworkStats publiceNetworkStats) {
+ android.net.NetworkStats stats = new android.net.NetworkStats(0L, 0);
+ while (publiceNetworkStats.hasNextBucket()) {
+ NetworkStats.Bucket bucket = new NetworkStats.Bucket();
+ publiceNetworkStats.getNextBucket(bucket);
+ final android.net.NetworkStats.Entry entry = fromBucket(bucket);
+ stats = stats.addEntry(entry);
+ }
+ return stats;
+ }
+
+ @VisibleForTesting
+ static android.net.NetworkStats.Entry fromBucket(NetworkStats.Bucket bucket) {
+ return new android.net.NetworkStats.Entry(
+ null /* IFACE_ALL */, bucket.getUid(), convertBucketState(bucket.getState()),
+ convertBucketTag(bucket.getTag()), convertBucketMetered(bucket.getMetered()),
+ convertBucketRoaming(bucket.getRoaming()),
+ convertBucketDefaultNetworkStatus(bucket.getDefaultNetworkStatus()),
+ bucket.getRxBytes(), bucket.getRxPackets(),
+ bucket.getTxBytes(), bucket.getTxPackets(), 0 /* operations */);
+ }
+
+ private static int convertBucketState(int networkStatsSet) {
+ switch (networkStatsSet) {
+ case NetworkStats.Bucket.STATE_ALL: return SET_ALL;
+ case NetworkStats.Bucket.STATE_DEFAULT: return android.net.NetworkStats.SET_DEFAULT;
+ case NetworkStats.Bucket.STATE_FOREGROUND:
+ return android.net.NetworkStats.SET_FOREGROUND;
+ }
+ return 0;
+ }
+
+ private static int convertBucketTag(int tag) {
+ switch (tag) {
+ case NetworkStats.Bucket.TAG_NONE: return android.net.NetworkStats.TAG_NONE;
+ }
+ return tag;
+ }
+
+ private static int convertBucketMetered(int metered) {
+ switch (metered) {
+ case NetworkStats.Bucket.METERED_ALL: return METERED_ALL;
+ case NetworkStats.Bucket.METERED_NO: return android.net.NetworkStats.METERED_NO;
+ case NetworkStats.Bucket.METERED_YES: return android.net.NetworkStats.METERED_YES;
+ }
+ return 0;
+ }
+
+ private static int convertBucketRoaming(int roaming) {
+ switch (roaming) {
+ case NetworkStats.Bucket.ROAMING_ALL: return ROAMING_ALL;
+ case NetworkStats.Bucket.ROAMING_NO: return android.net.NetworkStats.ROAMING_NO;
+ case NetworkStats.Bucket.ROAMING_YES: return android.net.NetworkStats.ROAMING_YES;
+ }
+ return 0;
+ }
+
+ private static int convertBucketDefaultNetworkStatus(int defaultNetworkStatus) {
+ switch (defaultNetworkStatus) {
+ case NetworkStats.Bucket.DEFAULT_NETWORK_ALL:
+ return DEFAULT_NETWORK_ALL;
+ case NetworkStats.Bucket.DEFAULT_NETWORK_NO:
+ return android.net.NetworkStats.DEFAULT_NETWORK_NO;
+ case NetworkStats.Bucket.DEFAULT_NETWORK_YES:
+ return android.net.NetworkStats.DEFAULT_NETWORK_YES;
+ }
+ return 0;
+ }
}
diff --git a/staticlibs/native/bpf_headers/Android.bp b/staticlibs/native/bpf_headers/Android.bp
index 8426dce..af8ec73 100644
--- a/staticlibs/native/bpf_headers/Android.bp
+++ b/staticlibs/native/bpf_headers/Android.bp
@@ -37,6 +37,7 @@
],
visibility: [
"//bootable/libbootloader/vts",
+ "//cts/tests/tests/net/native",
"//frameworks/base/services/core/jni",
"//frameworks/native/libs/cputimeinstate",
"//frameworks/native/services/gpuservice",
diff --git a/staticlibs/native/nettestutils/Android.bp b/staticlibs/native/nettestutils/Android.bp
new file mode 100644
index 0000000..4fa367d
--- /dev/null
+++ b/staticlibs/native/nettestutils/Android.bp
@@ -0,0 +1,28 @@
+// 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.
+
+cc_library_static {
+ name: "libnettestutils",
+ export_include_dirs: ["include"],
+ srcs: ["DumpService.cpp"],
+
+ shared_libs: [
+ "libbinder",
+ "libutils",
+ ],
+ cflags: [
+ "-Werror",
+ "-Wall",
+ ],
+}
diff --git a/staticlibs/native/nettestutils/DumpService.cpp b/staticlibs/native/nettestutils/DumpService.cpp
new file mode 100644
index 0000000..ba3d77e
--- /dev/null
+++ b/staticlibs/native/nettestutils/DumpService.cpp
@@ -0,0 +1,57 @@
+/*
+ * 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 "nettestutils/DumpService.h"
+
+#include <android-base/file.h>
+
+#include <sstream>
+#include <thread>
+
+android::status_t dumpService(const android::sp<android::IBinder>& binder,
+ const std::vector<std::string>& args,
+ std::vector<std::string>& outputLines) {
+ if (!outputLines.empty()) return -EUCLEAN;
+
+ android::base::unique_fd localFd, remoteFd;
+ if (!Pipe(&localFd, &remoteFd)) return -errno;
+
+ android::Vector<android::String16> str16Args;
+ for (const auto& arg : args) {
+ str16Args.push(android::String16(arg.c_str()));
+ }
+ android::status_t ret;
+ // dump() blocks until another thread has consumed all its output.
+ std::thread dumpThread =
+ std::thread([&ret, binder, remoteFd{std::move(remoteFd)}, str16Args]() {
+ ret = binder->dump(remoteFd, str16Args);
+ });
+
+ std::string dumpContent;
+ if (!android::base::ReadFdToString(localFd.get(), &dumpContent)) {
+ return -errno;
+ }
+ dumpThread.join();
+ if (ret != android::OK) return ret;
+
+ std::stringstream dumpStream(std::move(dumpContent));
+ std::string line;
+ while (std::getline(dumpStream, line)) {
+ outputLines.push_back(line);
+ }
+
+ return android::OK;
+}
diff --git a/staticlibs/native/nettestutils/include/nettestutils/DumpService.h b/staticlibs/native/nettestutils/include/nettestutils/DumpService.h
new file mode 100644
index 0000000..2a72181
--- /dev/null
+++ b/staticlibs/native/nettestutils/include/nettestutils/DumpService.h
@@ -0,0 +1,23 @@
+/*
+ * 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 <binder/Binder.h>
+
+#include <vector>
+
+android::status_t dumpService(const android::sp<android::IBinder>& binder,
+ const std::vector<std::string>& args,
+ std::vector<std::string>& outputLines);
diff --git a/staticlibs/tests/unit/src/com/android/net/module/util/NetworkStatsUtilsTest.kt b/staticlibs/tests/unit/src/com/android/net/module/util/NetworkStatsUtilsTest.kt
index ff839fe..2785ea9 100644
--- a/staticlibs/tests/unit/src/com/android/net/module/util/NetworkStatsUtilsTest.kt
+++ b/staticlibs/tests/unit/src/com/android/net/module/util/NetworkStatsUtilsTest.kt
@@ -16,12 +16,16 @@
package com.android.net.module.util
+import android.net.NetworkStats
+import android.text.TextUtils
import androidx.test.filters.SmallTest
import androidx.test.runner.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
+import org.mockito.Mockito.doReturn
+import org.mockito.Mockito.mock
@RunWith(AndroidJUnit4::class)
@SmallTest
@@ -71,4 +75,68 @@
assertEquals(11, NetworkStatsUtils.constrain(11, 11, 11))
assertEquals(11, NetworkStatsUtils.constrain(1, 11, 11))
}
+
+ @Test
+ fun testBucketToEntry() {
+ val bucket = makeMockBucket(android.app.usage.NetworkStats.Bucket.UID_ALL,
+ android.app.usage.NetworkStats.Bucket.TAG_NONE,
+ android.app.usage.NetworkStats.Bucket.STATE_DEFAULT,
+ android.app.usage.NetworkStats.Bucket.METERED_YES,
+ android.app.usage.NetworkStats.Bucket.ROAMING_NO,
+ android.app.usage.NetworkStats.Bucket.DEFAULT_NETWORK_ALL, 1024, 8, 2048, 12)
+ val entry = NetworkStatsUtils.fromBucket(bucket)
+ val expectedEntry = NetworkStats.Entry(null /* IFACE_ALL */, NetworkStats.UID_ALL,
+ NetworkStats.SET_DEFAULT, NetworkStats.TAG_NONE, NetworkStats.METERED_YES,
+ NetworkStats.ROAMING_NO, NetworkStats.DEFAULT_NETWORK_ALL, 1024, 8, 2048, 12,
+ 0 /* operations */)
+
+ // TODO: Use assertEquals once all downstreams accept null iface in
+ // NetworkStats.Entry#equals.
+ assertEntryEquals(expectedEntry, entry)
+ }
+
+ private fun makeMockBucket(
+ uid: Int,
+ tag: Int,
+ state: Int,
+ metered: Int,
+ roaming: Int,
+ defaultNetwork: Int,
+ rxBytes: Long,
+ rxPackets: Long,
+ txBytes: Long,
+ txPackets: Long
+ ): android.app.usage.NetworkStats.Bucket {
+ val ret: android.app.usage.NetworkStats.Bucket =
+ mock(android.app.usage.NetworkStats.Bucket::class.java)
+ doReturn(uid).`when`(ret).getUid()
+ doReturn(tag).`when`(ret).getTag()
+ doReturn(state).`when`(ret).getState()
+ doReturn(metered).`when`(ret).getMetered()
+ doReturn(roaming).`when`(ret).getRoaming()
+ doReturn(defaultNetwork).`when`(ret).getDefaultNetworkStatus()
+ doReturn(rxBytes).`when`(ret).getRxBytes()
+ doReturn(rxPackets).`when`(ret).getRxPackets()
+ doReturn(txBytes).`when`(ret).getTxBytes()
+ doReturn(txPackets).`when`(ret).getTxPackets()
+ return ret
+ }
+
+ /**
+ * Assert that the two {@link NetworkStats.Entry} are equals.
+ */
+ private fun assertEntryEquals(left: NetworkStats.Entry, right: NetworkStats.Entry) {
+ TextUtils.equals(left.iface, right.iface)
+ assertEquals(left.uid, right.uid)
+ assertEquals(left.set, right.set)
+ assertEquals(left.tag, right.tag)
+ assertEquals(left.metered, right.metered)
+ assertEquals(left.roaming, right.roaming)
+ assertEquals(left.defaultNetwork, right.defaultNetwork)
+ assertEquals(left.rxBytes, right.rxBytes)
+ assertEquals(left.rxPackets, right.rxPackets)
+ assertEquals(left.txBytes, right.txBytes)
+ assertEquals(left.txPackets, right.txPackets)
+ assertEquals(left.operations, right.operations)
+ }
}
\ No newline at end of file
diff --git a/staticlibs/testutils/Android.bp b/staticlibs/testutils/Android.bp
index 1be64c1..133c983 100644
--- a/staticlibs/testutils/Android.bp
+++ b/staticlibs/testutils/Android.bp
@@ -79,6 +79,6 @@
"host/**/*.kt",
],
libs: ["tradefed"],
- test_suites: ["device-tests", "general-tests", "cts", "mts"],
+ test_suites: ["device-tests", "general-tests", "cts", "mts-networking"],
data: [":ConnectivityChecker"],
}