Add config for client-side traffic stats rate-limit cache.
This is a no-op change since the config is not used yet, and
the feature flag will be gradually rolled out instead of enabled
by default.
However, this change is necessary for follow-up changes to enable
toggling the feature and configuring its behavior through device
configuration flags.
This change includes:
1. Disable the service-side cache flag if the client-side flag
is enabled.
2. Since the apps don't have enough permission to check the
feature flag from DeviceConfig. Provides a binder interface
for returning per-UID config,
which only returns cache enabled if the SDK level is V+
or the app's target SDK is V+.
Test: atest FrameworksNetTests:android.net.connectivity.android.net.TrafficStatsTest \
FrameworksNetTests:android.net.connectivity.com.android.server.net.NetworkStatsServiceTest \
CtsNetTestCases:android.net.cts.NetworkStatsBinderTest \
FrameworksNetIntegrationTests:com.android.server.net.integrationtests.NetworkStatsIntegrationTest
Bug: 343260158
Change-Id: Id9761e4aea65fabe6466c875b787e178317a1da2
diff --git a/framework-t/src/android/net/INetworkStatsService.aidl b/framework-t/src/android/net/INetworkStatsService.aidl
index ce57a4a..b459a13 100644
--- a/framework-t/src/android/net/INetworkStatsService.aidl
+++ b/framework-t/src/android/net/INetworkStatsService.aidl
@@ -25,6 +25,7 @@
import android.net.UnderlyingNetworkInfo;
import android.net.netstats.IUsageCallback;
import android.net.netstats.StatsResult;
+import android.net.netstats.TrafficStatsRateLimitCacheConfig;
import android.net.netstats.provider.INetworkStatsProvider;
import android.net.netstats.provider.INetworkStatsProviderCallback;
import android.os.IBinder;
@@ -104,4 +105,7 @@
/** Clear TrafficStats rate-limit caches. */
void clearTrafficStatsRateLimitCaches();
+
+ /** Get rate-limit cache config. */
+ TrafficStatsRateLimitCacheConfig getRateLimitCacheConfig();
}
diff --git a/framework-t/src/android/net/netstats/TrafficStatsRateLimitCacheConfig.aidl b/framework-t/src/android/net/netstats/TrafficStatsRateLimitCacheConfig.aidl
new file mode 100644
index 0000000..cdf0b7c
--- /dev/null
+++ b/framework-t/src/android/net/netstats/TrafficStatsRateLimitCacheConfig.aidl
@@ -0,0 +1,42 @@
+/**
+ * Copyright (c) 2024, 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.
+ */
+
+package android.net.netstats;
+
+/**
+ * Configuration for the TrafficStats rate limit cache.
+ *
+ * @hide
+ */
+@JavaDerive(equals=true, toString=true)
+@JavaOnlyImmutable
+parcelable TrafficStatsRateLimitCacheConfig {
+
+ /**
+ * Whether the cache is enabled for V+ device or target Sdk V+ apps.
+ */
+ boolean isCacheEnabled;
+
+ /**
+ * The duration for which cache entries are valid, in milliseconds.
+ */
+ int expiryDurationMs;
+
+ /**
+ * The maximum number of entries to store in the cache.
+ */
+ int maxEntries;
+}
diff --git a/service-t/src/com/android/server/net/NetworkStatsService.java b/service-t/src/com/android/server/net/NetworkStatsService.java
index ba706d9..fb712a1 100644
--- a/service-t/src/com/android/server/net/NetworkStatsService.java
+++ b/service-t/src/com/android/server/net/NetworkStatsService.java
@@ -127,6 +127,7 @@
import android.net.netstats.IUsageCallback;
import android.net.netstats.NetworkStatsDataMigrationUtils;
import android.net.netstats.StatsResult;
+import android.net.netstats.TrafficStatsRateLimitCacheConfig;
import android.net.netstats.provider.INetworkStatsProvider;
import android.net.netstats.provider.INetworkStatsProviderCallback;
import android.net.netstats.provider.NetworkStatsProvider;
@@ -491,6 +492,9 @@
private final TrafficStatsRateLimitCache mTrafficStatsIfaceCache;
@Nullable
private final TrafficStatsRateLimitCache mTrafficStatsUidCache;
+ // A feature flag to control whether the client-side rate limit cache should be enabled.
+ static final String TRAFFICSTATS_CLIENT_RATE_LIMIT_CACHE_ENABLED_FLAG =
+ "trafficstats_client_rate_limit_cache_enabled_flag";
static final String TRAFFICSTATS_SERVICE_RATE_LIMIT_CACHE_ENABLED_FLAG =
"trafficstats_rate_limit_cache_enabled_flag";
static final String BROADCAST_NETWORK_STATS_UPDATED_RATE_LIMIT_ENABLED_FLAG =
@@ -498,6 +502,7 @@
private final boolean mIsTrafficStatsServiceRateLimitCacheEnabled;
private final int mTrafficStatsRateLimitCacheExpiryDuration;
private final int mTrafficStatsServiceRateLimitCacheMaxEntries;
+ private final TrafficStatsRateLimitCacheConfig mTrafficStatsRateLimitCacheClientSideConfig;
private final boolean mBroadcastNetworkStatsUpdatedRateLimitEnabled;
@@ -691,8 +696,13 @@
mEventLogger = null;
}
+ mTrafficStatsRateLimitCacheClientSideConfig =
+ mDeps.getTrafficStatsRateLimitCacheClientSideConfig(mContext);
+ // If the client side cache feature is enabled, disable the service side
+ // cache unconditionally.
mIsTrafficStatsServiceRateLimitCacheEnabled =
- mDeps.isTrafficStatsServiceRateLimitCacheEnabled(mContext);
+ mDeps.isTrafficStatsServiceRateLimitCacheEnabled(mContext,
+ mTrafficStatsRateLimitCacheClientSideConfig.isCacheEnabled);
mBroadcastNetworkStatsUpdatedRateLimitEnabled =
mDeps.enabledBroadcastNetworkStatsUpdatedRateLimiting(mContext);
mTrafficStatsRateLimitCacheExpiryDuration =
@@ -973,13 +983,42 @@
}
/**
- * Whether the service side cache is enabled for V+ device or target Sdk V+ apps.
+ * Get client side traffic stats rate-limit cache config.
*
* This method should only be called once in the constructor,
* to ensure that the code does not need to deal with flag values changing at runtime.
*/
- public boolean isTrafficStatsServiceRateLimitCacheEnabled(@NonNull Context ctx) {
- return DeviceConfigUtils.isTetheringFeatureNotChickenedOut(
+ @NonNull
+ public TrafficStatsRateLimitCacheConfig getTrafficStatsRateLimitCacheClientSideConfig(
+ @NonNull Context ctx) {
+ final TrafficStatsRateLimitCacheConfig config =
+ new TrafficStatsRateLimitCacheConfig.Builder()
+ .setIsCacheEnabled(DeviceConfigUtils.isTetheringFeatureEnabled(
+ ctx, TRAFFICSTATS_CLIENT_RATE_LIMIT_CACHE_ENABLED_FLAG))
+ .setExpiryDurationMs(getDeviceConfigPropertyInt(
+ NAMESPACE_TETHERING, TRAFFIC_STATS_CACHE_EXPIRY_DURATION_NAME,
+ DEFAULT_TRAFFIC_STATS_CACHE_EXPIRY_DURATION_MS))
+ .setMaxEntries(getDeviceConfigPropertyInt(
+ NAMESPACE_TETHERING,
+ TRAFFIC_STATS_SERVICE_CACHE_MAX_ENTRIES_NAME,
+ DEFAULT_TRAFFIC_STATS_SERVICE_CACHE_MAX_ENTRIES))
+ .build();
+ return config;
+ }
+
+ /**
+ * Determines whether the service-side rate-limiting cache is enabled.
+ *
+ * The cache is enabled for devices running Android V+ or apps targeting SDK V+
+ * if the `TRAFFICSTATS_SERVICE_RATE_LIMIT_CACHE_ENABLED_FLAG` feature flag
+ * is enabled and client-side caching is disabled.
+ *
+ * This method should only be called once in the constructor,
+ * to ensure that the code does not need to deal with flag values changing at runtime.
+ */
+ public boolean isTrafficStatsServiceRateLimitCacheEnabled(@NonNull Context ctx,
+ boolean clientCacheEnabled) {
+ return !clientCacheEnabled && DeviceConfigUtils.isTetheringFeatureNotChickenedOut(
ctx, TRAFFICSTATS_SERVICE_RATE_LIMIT_CACHE_ENABLED_FLAG);
}
@@ -2143,6 +2182,20 @@
}
/**
+ * Determines whether to use the client-side cache for traffic stats rate limiting.
+ *
+ * This is based on the cache enabled feature flag. If enabled, the client-side cache
+ * is used for V+ devices or callers with V+ target sdk.
+ *
+ * @param callingUid The UID of the app making the request.
+ * @return True if the client-side cache should be used, false otherwise.
+ */
+ private boolean useClientSideCache(int callingUid) {
+ return mTrafficStatsRateLimitCacheClientSideConfig.isCacheEnabled && (SdkLevel.isAtLeastV()
+ || mDeps.isChangeEnabled(ENABLE_TRAFFICSTATS_RATE_LIMIT_CACHE, callingUid));
+ }
+
+ /**
* Determines whether to use the service-side cache for traffic stats rate limiting.
*
* This is based on the cache enabled feature flag. If enabled, the service-side cache
@@ -2238,6 +2291,19 @@
}
}
+ @Override
+ public TrafficStatsRateLimitCacheConfig getRateLimitCacheConfig() {
+ // Build a per uid config for the client based on the checking result.
+ final TrafficStatsRateLimitCacheConfig config =
+ new TrafficStatsRateLimitCacheConfig.Builder()
+ .setIsCacheEnabled(useClientSideCache(Binder.getCallingUid()))
+ .setExpiryDurationMs(
+ mTrafficStatsRateLimitCacheClientSideConfig.expiryDurationMs)
+ .setMaxEntries(mTrafficStatsRateLimitCacheClientSideConfig.maxEntries)
+ .build();
+ return config;
+ }
+
private NetworkStats.Entry getProviderIfaceStats(@Nullable String iface) {
final NetworkStats providerSnapshot = getNetworkStatsFromProviders(STATS_PER_IFACE);
final HashSet<String> limitIfaces;
@@ -3014,6 +3080,9 @@
pw.print(TRAFFIC_STATS_SERVICE_CACHE_MAX_ENTRIES_NAME,
mTrafficStatsServiceRateLimitCacheMaxEntries);
pw.println();
+ pw.print("trafficstats.client.cache.config",
+ mTrafficStatsRateLimitCacheClientSideConfig);
+ pw.println();
pw.decreaseIndent();
diff --git a/staticlibs/testutils/devicetests/com/android/testutils/SetFeatureFlagsRule.kt b/staticlibs/testutils/devicetests/com/android/testutils/SetFeatureFlagsRule.kt
index d5e91c2..7b970d3 100644
--- a/staticlibs/testutils/devicetests/com/android/testutils/SetFeatureFlagsRule.kt
+++ b/staticlibs/testutils/devicetests/com/android/testutils/SetFeatureFlagsRule.kt
@@ -57,6 +57,7 @@
* @param enabled The desired state (true for enabled, false for disabled) of the feature flag.
*/
@Target(AnnotationTarget.FUNCTION)
+ @Repeatable
@Retention(AnnotationRetention.RUNTIME)
annotation class FeatureFlag(val name: String, val enabled: Boolean = true)
diff --git a/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java b/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java
index 3a52927..b528480 100644
--- a/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java
+++ b/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java
@@ -78,6 +78,7 @@
import static com.android.server.net.NetworkStatsService.NETSTATS_IMPORT_ATTEMPTS_COUNTER_NAME;
import static com.android.server.net.NetworkStatsService.NETSTATS_IMPORT_FALLBACKS_COUNTER_NAME;
import static com.android.server.net.NetworkStatsService.NETSTATS_IMPORT_SUCCESSES_COUNTER_NAME;
+import static com.android.server.net.NetworkStatsService.TRAFFICSTATS_CLIENT_RATE_LIMIT_CACHE_ENABLED_FLAG;
import static com.android.server.net.NetworkStatsService.TRAFFICSTATS_SERVICE_RATE_LIMIT_CACHE_ENABLED_FLAG;
import static com.android.testutils.DevSdkIgnoreRuleKt.SC_V2;
@@ -129,6 +130,7 @@
import android.net.TetheringManager;
import android.net.UnderlyingNetworkInfo;
import android.net.netstats.StatsResult;
+import android.net.netstats.TrafficStatsRateLimitCacheConfig;
import android.net.netstats.provider.INetworkStatsProviderCallback;
import android.net.wifi.WifiInfo;
import android.os.Build;
@@ -622,8 +624,9 @@
}
@Override
- public boolean isTrafficStatsServiceRateLimitCacheEnabled(Context ctx) {
- return mFeatureFlags.getOrDefault(
+ public boolean isTrafficStatsServiceRateLimitCacheEnabled(Context ctx,
+ boolean isClientCacheEnabled) {
+ return !isClientCacheEnabled && mFeatureFlags.getOrDefault(
TRAFFICSTATS_SERVICE_RATE_LIMIT_CACHE_ENABLED_FLAG, false);
}
@@ -644,6 +647,19 @@
}
@Override
+ public TrafficStatsRateLimitCacheConfig getTrafficStatsRateLimitCacheClientSideConfig(
+ @NonNull Context ctx) {
+ final TrafficStatsRateLimitCacheConfig config =
+ new TrafficStatsRateLimitCacheConfig.Builder()
+ .setIsCacheEnabled(mFeatureFlags.getOrDefault(
+ TRAFFICSTATS_CLIENT_RATE_LIMIT_CACHE_ENABLED_FLAG, false))
+ .setExpiryDurationMs(DEFAULT_TRAFFIC_STATS_CACHE_EXPIRY_DURATION_MS)
+ .setMaxEntries(DEFAULT_TRAFFIC_STATS_SERVICE_CACHE_MAX_ENTRIES)
+ .build();
+ return config;
+ }
+
+ @Override
public boolean isChangeEnabled(long changeId, int uid) {
return mCompatChanges.getOrDefault(changeId, true);
}
@@ -2454,6 +2470,44 @@
assertUidTotal(sTemplateWifi, UID_GREEN, 64L, 3L, 1024L, 8L, 0);
}
+ @FeatureFlag(name = TRAFFICSTATS_CLIENT_RATE_LIMIT_CACHE_ENABLED_FLAG, enabled = false)
+ @Test
+ public void testGetRateLimitCacheConfig_featureDisabled() {
+ mDeps.setChangeEnabled(ENABLE_TRAFFICSTATS_RATE_LIMIT_CACHE, false);
+ assertFalse(mService.getRateLimitCacheConfig().isCacheEnabled);
+ mDeps.setChangeEnabled(ENABLE_TRAFFICSTATS_RATE_LIMIT_CACHE, true);
+ assertFalse(mService.getRateLimitCacheConfig().isCacheEnabled);
+ }
+
+ @FeatureFlag(name = TRAFFICSTATS_CLIENT_RATE_LIMIT_CACHE_ENABLED_FLAG)
+ @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
+ @Test
+ public void testGetRateLimitCacheConfig_vOrAbove() {
+ mDeps.setChangeEnabled(ENABLE_TRAFFICSTATS_RATE_LIMIT_CACHE, false);
+ assertTrue(mService.getRateLimitCacheConfig().isCacheEnabled);
+ mDeps.setChangeEnabled(ENABLE_TRAFFICSTATS_RATE_LIMIT_CACHE, true);
+ assertTrue(mService.getRateLimitCacheConfig().isCacheEnabled);
+ }
+
+ @FeatureFlag(name = TRAFFICSTATS_CLIENT_RATE_LIMIT_CACHE_ENABLED_FLAG)
+ @DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
+ @Test
+ public void testGetRateLimitCacheConfig_belowV() {
+ mDeps.setChangeEnabled(ENABLE_TRAFFICSTATS_RATE_LIMIT_CACHE, false);
+ assertFalse(mService.getRateLimitCacheConfig().isCacheEnabled);
+ mDeps.setChangeEnabled(ENABLE_TRAFFICSTATS_RATE_LIMIT_CACHE, true);
+ assertTrue(mService.getRateLimitCacheConfig().isCacheEnabled);
+ }
+
+ @FeatureFlag(name = TRAFFICSTATS_CLIENT_RATE_LIMIT_CACHE_ENABLED_FLAG)
+ @FeatureFlag(name = TRAFFICSTATS_SERVICE_RATE_LIMIT_CACHE_ENABLED_FLAG)
+ @Test
+ public void testTrafficStatsRateLimitCache_clientCacheEnabledDisableServiceCache()
+ throws Exception {
+ mDeps.setChangeEnabled(ENABLE_TRAFFICSTATS_RATE_LIMIT_CACHE, true);
+ doTestTrafficStatsRateLimitCache(false /* expectCached */);
+ }
+
@FeatureFlag(name = TRAFFICSTATS_SERVICE_RATE_LIMIT_CACHE_ENABLED_FLAG, enabled = false)
@Test
public void testTrafficStatsRateLimitCache_disabledWithCompatChangeEnabled() throws Exception {