diff options
7 files changed, 89 insertions, 117 deletions
diff --git a/core/java/android/net/INetworkStatsService.aidl b/core/java/android/net/INetworkStatsService.aidl index e5f3d26667a0..9994f9f82b07 100644 --- a/core/java/android/net/INetworkStatsService.aidl +++ b/core/java/android/net/INetworkStatsService.aidl @@ -67,7 +67,8 @@ interface INetworkStatsService { void forceUpdateIfaces( in Network[] defaultNetworks, in NetworkState[] networkStates, - in String activeIface); + in String activeIface, + in VpnInfo[] vpnInfos); /** Force update of statistics. */ @UnsupportedAppUsage void forceUpdate(); diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index a71d46cbe886..f8c1b247dbb8 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -192,7 +192,6 @@ import com.android.server.net.BaseNetdEventCallback; import com.android.server.net.BaseNetworkObserver; import com.android.server.net.LockdownVpnTracker; import com.android.server.net.NetworkPolicyManagerInternal; -import com.android.server.net.NetworkStatsFactory; import com.android.server.utils.PriorityDump; import com.google.android.collect.Lists; @@ -6800,8 +6799,8 @@ public class ConnectivityService extends IConnectivityManager.Stub } /** - * Notify NetworkStatsService and NetworkStatsFactory that the set of active ifaces has changed, - * or that one of the active iface's trackedproperties has changed. + * Notify NetworkStatsService that the set of active ifaces has changed, or that one of the + * active iface's tracked properties has changed. */ private void notifyIfacesChangedForNetworkStats() { ensureRunningOnConnectivityServiceThread(); @@ -6811,16 +6810,12 @@ public class ConnectivityService extends IConnectivityManager.Stub activeIface = activeLinkProperties.getInterfaceName(); } - // CAUTION: Ordering matters between updateVpnInfos() and forceUpdateIfaces(), which - // triggers a new poll. Trigger the poll first to ensure a snapshot is taken before - // switching to the new state. This ensures that traffic does not get mis-attributed to - // incorrect apps (including VPN app). + final VpnInfo[] vpnInfos = getAllVpnInfo(); try { mStatsService.forceUpdateIfaces( - getDefaultNetworks(), getAllNetworkState(), activeIface); + getDefaultNetworks(), getAllNetworkState(), activeIface, vpnInfos); } catch (Exception ignored) { } - NetworkStatsFactory.updateVpnInfos(getAllVpnInfo()); } @Override diff --git a/services/core/java/com/android/server/net/NetworkStatsFactory.java b/services/core/java/com/android/server/net/NetworkStatsFactory.java index 9344ef71c9f7..3ca1803262e7 100644 --- a/services/core/java/com/android/server/net/NetworkStatsFactory.java +++ b/services/core/java/com/android/server/net/NetworkStatsFactory.java @@ -78,17 +78,17 @@ public class NetworkStatsFactory { * <p>In order to prevent deadlocks, critical sections protected by this lock SHALL NOT call out * to other code that will acquire other locks within the system server. See b/134244752. */ - private static final Object sPersistentDataLock = new Object(); + private final Object mPersistentDataLock = new Object(); /** Set containing info about active VPNs and their underlying networks. */ - private static volatile VpnInfo[] sVpnInfos = new VpnInfo[0]; + private volatile VpnInfo[] mVpnInfos = new VpnInfo[0]; // A persistent snapshot of cumulative stats since device start - @GuardedBy("sPersistentDataLock") + @GuardedBy("mPersistentDataLock") private NetworkStats mPersistSnapshot; // The persistent snapshot of tun and 464xlat adjusted stats since device start - @GuardedBy("sPersistentDataLock") + @GuardedBy("mPersistentDataLock") private NetworkStats mTunAnd464xlatAdjustedStats; /** @@ -97,12 +97,13 @@ public class NetworkStatsFactory { * Because counters must never roll backwards, once a given interface is stacked on top of an * underlying interface, the stacked interface can never be stacked on top of * another interface. */ - private static final ConcurrentHashMap<String, String> sStackedIfaces + private final ConcurrentHashMap<String, String> mStackedIfaces = new ConcurrentHashMap<>(); - public static void noteStackedIface(String stackedIface, String baseIface) { + /** Informs the factory of a new stacked interface. */ + public void noteStackedIface(String stackedIface, String baseIface) { if (stackedIface != null && baseIface != null) { - sStackedIfaces.put(stackedIface, baseIface); + mStackedIfaces.put(stackedIface, baseIface); } } @@ -115,13 +116,8 @@ public class NetworkStatsFactory { * * @param vpnArray The snapshot of the currently-running VPNs. */ - public static void updateVpnInfos(VpnInfo[] vpnArray) { - sVpnInfos = vpnArray.clone(); - } - - @VisibleForTesting - public static VpnInfo[] getVpnInfos() { - return sVpnInfos.clone(); + public void updateVpnInfos(VpnInfo[] vpnArray) { + mVpnInfos = vpnArray.clone(); } /** @@ -132,7 +128,7 @@ public class NetworkStatsFactory { * {@link #noteStackedIface(String, String)}, but only interfaces noted before this method * is called are guaranteed to be included. */ - public static String[] augmentWithStackedInterfaces(@Nullable String[] requiredIfaces) { + public String[] augmentWithStackedInterfaces(@Nullable String[] requiredIfaces) { if (requiredIfaces == NetworkStats.INTERFACES_ALL) { return null; } @@ -142,7 +138,7 @@ public class NetworkStatsFactory { // elements as they existed upon construction exactly once, and may // (but are not guaranteed to) reflect any modifications subsequent to construction". // This is enough here. - for (Map.Entry<String, String> entry : sStackedIfaces.entrySet()) { + for (Map.Entry<String, String> entry : mStackedIfaces.entrySet()) { if (relatedIfaces.contains(entry.getKey())) { relatedIfaces.add(entry.getValue()); } else if (relatedIfaces.contains(entry.getValue())) { @@ -158,17 +154,12 @@ public class NetworkStatsFactory { * Applies 464xlat adjustments with ifaces noted with {@link #noteStackedIface(String, String)}. * @see NetworkStats#apply464xlatAdjustments(NetworkStats, NetworkStats, Map, boolean) */ - public static void apply464xlatAdjustments(NetworkStats baseTraffic, + public void apply464xlatAdjustments(NetworkStats baseTraffic, NetworkStats stackedTraffic, boolean useBpfStats) { - NetworkStats.apply464xlatAdjustments(baseTraffic, stackedTraffic, sStackedIfaces, + NetworkStats.apply464xlatAdjustments(baseTraffic, stackedTraffic, mStackedIfaces, useBpfStats); } - @VisibleForTesting - public static void clearStackedIfaces() { - sStackedIfaces.clear(); - } - public NetworkStatsFactory() { this(new File("/proc/"), new File("/sys/fs/bpf/map_netd_app_uid_stats_map").exists()); } @@ -179,7 +170,7 @@ public class NetworkStatsFactory { mStatsXtIfaceFmt = new File(procRoot, "net/xt_qtaguid/iface_stat_fmt"); mStatsXtUid = new File(procRoot, "net/xt_qtaguid/stats"); mUseBpfStats = useBpfStats; - synchronized (sPersistentDataLock) { + synchronized (mPersistentDataLock) { mPersistSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), -1); mTunAnd464xlatAdjustedStats = new NetworkStats(SystemClock.elapsedRealtime(), -1); } @@ -304,7 +295,7 @@ public class NetworkStatsFactory { return readNetworkStatsDetail(UID_ALL, INTERFACES_ALL, TAG_ALL); } - @GuardedBy("sPersistentDataLock") + @GuardedBy("mPersistentDataLock") private void requestSwapActiveStatsMapLocked() throws RemoteException { // Ask netd to do a active map stats swap. When the binder call successfully returns, // the system server should be able to safely read and clean the inactive map @@ -328,9 +319,9 @@ public class NetworkStatsFactory { int limitUid, String[] limitIfaces, int limitTag) throws IOException { // In order to prevent deadlocks, anything protected by this lock MUST NOT call out to other // code that will acquire other locks within the system server. See b/134244752. - synchronized (sPersistentDataLock) { + synchronized (mPersistentDataLock) { // Take a reference. If this gets swapped out, we still have the old reference. - final VpnInfo[] vpnArray = sVpnInfos; + final VpnInfo[] vpnArray = mVpnInfos; // Take a defensive copy. mPersistSnapshot is mutated in some cases below final NetworkStats prev = mPersistSnapshot.clone(); @@ -379,7 +370,7 @@ public class NetworkStatsFactory { } } - @GuardedBy("sPersistentDataLock") + @GuardedBy("mPersistentDataLock") private NetworkStats adjustForTunAnd464Xlat( NetworkStats uidDetailStats, NetworkStats previousStats, VpnInfo[] vpnArray) { // Calculate delta from last snapshot @@ -389,7 +380,7 @@ public class NetworkStatsFactory { // network, the overhead is their fault. // No locking here: apply464xlatAdjustments behaves fine with an add-only // ConcurrentHashMap. - delta.apply464xlatAdjustments(sStackedIfaces, mUseBpfStats); + delta.apply464xlatAdjustments(mStackedIfaces, mUseBpfStats); // Migrate data usage over a VPN to the TUN network. for (VpnInfo info : vpnArray) { diff --git a/services/core/java/com/android/server/net/NetworkStatsService.java b/services/core/java/com/android/server/net/NetworkStatsService.java index e67b39137a4e..4f4377df0997 100644 --- a/services/core/java/com/android/server/net/NetworkStatsService.java +++ b/services/core/java/com/android/server/net/NetworkStatsService.java @@ -130,6 +130,7 @@ import android.util.proto.ProtoOutputStream; import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; +import com.android.internal.net.VpnInfo; import com.android.internal.util.ArrayUtils; import com.android.internal.util.DumpUtils; import com.android.internal.util.FileRotator; @@ -777,7 +778,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { public NetworkStats getDetailedUidStats(String[] requiredIfaces) { try { final String[] ifacesToQuery = - NetworkStatsFactory.augmentWithStackedInterfaces(requiredIfaces); + mStatsFactory.augmentWithStackedInterfaces(requiredIfaces); return getNetworkStatsUidDetail(ifacesToQuery); } catch (RemoteException e) { Log.wtf(TAG, "Error compiling UID stats", e); @@ -829,7 +830,8 @@ public class NetworkStatsService extends INetworkStatsService.Stub { public void forceUpdateIfaces( Network[] defaultNetworks, NetworkState[] networkStates, - String activeIface) { + String activeIface, + VpnInfo[] vpnInfos) { checkNetworkStackPermission(mContext); final long token = Binder.clearCallingIdentity(); @@ -838,6 +840,11 @@ public class NetworkStatsService extends INetworkStatsService.Stub { } finally { Binder.restoreCallingIdentity(token); } + + // Update the VPN underlying interfaces only after the poll is made and tun data has been + // migrated. Otherwise the migration would use the new interfaces instead of the ones that + // were current when the polled data was transferred. + mStatsFactory.updateVpnInfos(vpnInfos); } @Override @@ -1649,7 +1656,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { // fold tethering stats and operations into uid snapshot final NetworkStats tetherSnapshot = getNetworkStatsTethering(STATS_PER_UID); tetherSnapshot.filter(UID_ALL, ifaces, TAG_ALL); - NetworkStatsFactory.apply464xlatAdjustments(uidSnapshot, tetherSnapshot, + mStatsFactory.apply464xlatAdjustments(uidSnapshot, tetherSnapshot, mUseBpfTrafficStats); uidSnapshot.combineAllValues(tetherSnapshot); @@ -1660,7 +1667,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { final NetworkStats vtStats = telephonyManager.getVtDataUsage(STATS_PER_UID); if (vtStats != null) { vtStats.filter(UID_ALL, ifaces, TAG_ALL); - NetworkStatsFactory.apply464xlatAdjustments(uidSnapshot, vtStats, + mStatsFactory.apply464xlatAdjustments(uidSnapshot, vtStats, mUseBpfTrafficStats); uidSnapshot.combineAllValues(vtStats); } diff --git a/tests/net/java/com/android/server/ConnectivityServiceTest.java b/tests/net/java/com/android/server/ConnectivityServiceTest.java index b60dc2d6b759..5dcbba6fb5ca 100644 --- a/tests/net/java/com/android/server/ConnectivityServiceTest.java +++ b/tests/net/java/com/android/server/ConnectivityServiceTest.java @@ -205,7 +205,6 @@ import com.android.server.connectivity.Tethering; import com.android.server.connectivity.Vpn; import com.android.server.net.NetworkPinner; import com.android.server.net.NetworkPolicyManagerInternal; -import com.android.server.net.NetworkStatsFactory; import com.android.testutils.HandlerUtilsKt; import com.android.testutils.ThrowingConsumer; @@ -4882,11 +4881,8 @@ public class ConnectivityServiceTest { mCellNetworkAgent.sendLinkProperties(cellLp); waitForIdle(); verify(mStatsService, atLeastOnce()) - .forceUpdateIfaces( - eq(onlyCell), - any(NetworkState[].class), - eq(MOBILE_IFNAME)); - assertEquals(new VpnInfo[0], NetworkStatsFactory.getVpnInfos()); + .forceUpdateIfaces(eq(onlyCell), any(NetworkState[].class), eq(MOBILE_IFNAME), + eq(new VpnInfo[0])); reset(mStatsService); // Default network switch should update ifaces. @@ -4895,65 +4891,47 @@ public class ConnectivityServiceTest { waitForIdle(); assertEquals(wifiLp, mService.getActiveLinkProperties()); verify(mStatsService, atLeastOnce()) - .forceUpdateIfaces( - eq(onlyWifi), - any(NetworkState[].class), - eq(WIFI_IFNAME)); - assertEquals(new VpnInfo[0], NetworkStatsFactory.getVpnInfos()); + .forceUpdateIfaces(eq(onlyWifi), any(NetworkState[].class), eq(WIFI_IFNAME), + eq(new VpnInfo[0])); reset(mStatsService); // Disconnect should update ifaces. mWiFiNetworkAgent.disconnect(); waitForIdle(); verify(mStatsService, atLeastOnce()) - .forceUpdateIfaces( - eq(onlyCell), - any(NetworkState[].class), - eq(MOBILE_IFNAME)); - assertEquals(new VpnInfo[0], NetworkStatsFactory.getVpnInfos()); + .forceUpdateIfaces(eq(onlyCell), any(NetworkState[].class), + eq(MOBILE_IFNAME), eq(new VpnInfo[0])); reset(mStatsService); // Metered change should update ifaces mCellNetworkAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); waitForIdle(); verify(mStatsService, atLeastOnce()) - .forceUpdateIfaces( - eq(onlyCell), - any(NetworkState[].class), - eq(MOBILE_IFNAME)); - assertEquals(new VpnInfo[0], NetworkStatsFactory.getVpnInfos()); + .forceUpdateIfaces(eq(onlyCell), any(NetworkState[].class), eq(MOBILE_IFNAME), + eq(new VpnInfo[0])); reset(mStatsService); mCellNetworkAgent.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); waitForIdle(); verify(mStatsService, atLeastOnce()) - .forceUpdateIfaces( - eq(onlyCell), - any(NetworkState[].class), - eq(MOBILE_IFNAME)); - assertEquals(new VpnInfo[0], NetworkStatsFactory.getVpnInfos()); + .forceUpdateIfaces(eq(onlyCell), any(NetworkState[].class), eq(MOBILE_IFNAME), + eq(new VpnInfo[0])); reset(mStatsService); // Captive portal change shouldn't update ifaces mCellNetworkAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL); waitForIdle(); verify(mStatsService, never()) - .forceUpdateIfaces( - eq(onlyCell), - any(NetworkState[].class), - eq(MOBILE_IFNAME)); - assertEquals(new VpnInfo[0], NetworkStatsFactory.getVpnInfos()); + .forceUpdateIfaces(eq(onlyCell), any(NetworkState[].class), eq(MOBILE_IFNAME), + eq(new VpnInfo[0])); reset(mStatsService); // Roaming change should update ifaces mCellNetworkAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING); waitForIdle(); verify(mStatsService, atLeastOnce()) - .forceUpdateIfaces( - eq(onlyCell), - any(NetworkState[].class), - eq(MOBILE_IFNAME)); - assertEquals(new VpnInfo[0], NetworkStatsFactory.getVpnInfos()); + .forceUpdateIfaces(eq(onlyCell), any(NetworkState[].class), eq(MOBILE_IFNAME), + eq(new VpnInfo[0])); reset(mStatsService); } diff --git a/tests/net/java/com/android/server/net/NetworkStatsFactoryTest.java b/tests/net/java/com/android/server/net/NetworkStatsFactoryTest.java index 7329474db70f..a21f5095c746 100644 --- a/tests/net/java/com/android/server/net/NetworkStatsFactoryTest.java +++ b/tests/net/java/com/android/server/net/NetworkStatsFactoryTest.java @@ -79,8 +79,7 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { // related to networkStatsFactory is compiled to a minimal native library and loaded here. System.loadLibrary("networkstatsfactorytestjni"); mFactory = new NetworkStatsFactory(mTestProc, false); - NetworkStatsFactory.updateVpnInfos(new VpnInfo[0]); - NetworkStatsFactory.clearStackedIfaces(); + mFactory.updateVpnInfos(new VpnInfo[0]); } @After @@ -107,7 +106,7 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { @Test public void vpnRewriteTrafficThroughItself() throws Exception { VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(new String[] {TEST_IFACE})}; - NetworkStatsFactory.updateVpnInfos(vpnInfos); + mFactory.updateVpnInfos(vpnInfos); // create some traffic (assume 10 bytes of MTU for VPN interface and 1 byte encryption // overhead per packet): @@ -136,8 +135,8 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { @Test public void vpnWithClat() throws Exception { VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(new String[] {CLAT_PREFIX + TEST_IFACE})}; - NetworkStatsFactory.updateVpnInfos(vpnInfos); - NetworkStatsFactory.noteStackedIface(CLAT_PREFIX + TEST_IFACE, TEST_IFACE); + mFactory.updateVpnInfos(vpnInfos); + mFactory.noteStackedIface(CLAT_PREFIX + TEST_IFACE, TEST_IFACE); // create some traffic (assume 10 bytes of MTU for VPN interface and 1 byte encryption // overhead per packet): @@ -169,7 +168,7 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { @Test public void vpnWithOneUnderlyingIface() throws Exception { VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(new String[] {TEST_IFACE})}; - NetworkStatsFactory.updateVpnInfos(vpnInfos); + mFactory.updateVpnInfos(vpnInfos); // create some traffic (assume 10 bytes of MTU for VPN interface and 1 byte encryption // overhead per packet): @@ -193,7 +192,7 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { public void vpnWithOneUnderlyingIfaceAndOwnTraffic() throws Exception { // WiFi network is connected and VPN is using WiFi (which has TEST_IFACE). VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(new String[] {TEST_IFACE})}; - NetworkStatsFactory.updateVpnInfos(vpnInfos); + mFactory.updateVpnInfos(vpnInfos); // create some traffic (assume 10 bytes of MTU for VPN interface and 1 byte encryption // overhead per packet): @@ -221,7 +220,7 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { public void vpnWithOneUnderlyingIface_withCompression() throws Exception { // WiFi network is connected and VPN is using WiFi (which has TEST_IFACE). VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(new String[] {TEST_IFACE})}; - NetworkStatsFactory.updateVpnInfos(vpnInfos); + mFactory.updateVpnInfos(vpnInfos); // create some traffic (assume 10 bytes of MTU for VPN interface and 1 byte encryption // overhead per packet): @@ -244,7 +243,7 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { // Cell (which has TEST_IFACE2) and has declared both of them in its underlying network set. // Additionally, VPN is duplicating traffic across both WiFi and Cell. VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(new String[] {TEST_IFACE, TEST_IFACE2})}; - NetworkStatsFactory.updateVpnInfos(vpnInfos); + mFactory.updateVpnInfos(vpnInfos); // create some traffic (assume 10 bytes of MTU for VPN interface and 1 byte encryption // overhead per packet): @@ -270,7 +269,7 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { // Cell (which has TEST_IFACE2) and has declared both of them in its underlying network set. // Additionally, VPN is arbitrarily splitting traffic across WiFi and Cell. VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(new String[] {TEST_IFACE, TEST_IFACE2})}; - NetworkStatsFactory.updateVpnInfos(vpnInfos); + mFactory.updateVpnInfos(vpnInfos); // create some traffic (assume 10 bytes of MTU for VPN interface and 1 byte encryption // overhead per packet): @@ -297,7 +296,7 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { // Cell (which has TEST_IFACE2) and has declared both of them in its underlying network set. // Additionally, VPN is arbitrarily splitting compressed traffic across WiFi and Cell. VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(new String[] {TEST_IFACE, TEST_IFACE2})}; - NetworkStatsFactory.updateVpnInfos(vpnInfos); + mFactory.updateVpnInfos(vpnInfos); // create some traffic (assume 10 bytes of MTU for VPN interface: // 1000 bytes (100 packets) were sent/received by UID_RED over VPN. @@ -319,7 +318,7 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { // WiFi and Cell networks are connected and VPN is using Cell (which has TEST_IFACE2), // but has declared only WiFi (TEST_IFACE) in its underlying network set. VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(new String[] {TEST_IFACE})}; - NetworkStatsFactory.updateVpnInfos(vpnInfos); + mFactory.updateVpnInfos(vpnInfos); // create some traffic (assume 10 bytes of MTU for VPN interface and 1 byte encryption // overhead per packet): @@ -383,7 +382,7 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { @Test public void testDoubleClatAccountingSimple() throws Exception { - NetworkStatsFactory.noteStackedIface("v4-wlan0", "wlan0"); + mFactory.noteStackedIface("v4-wlan0", "wlan0"); // xt_qtaguid_with_clat_simple is a synthetic file that simulates // - 213 received 464xlat packets of size 200 bytes @@ -398,7 +397,7 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { @Test public void testDoubleClatAccounting() throws Exception { - NetworkStatsFactory.noteStackedIface("v4-wlan0", "wlan0"); + mFactory.noteStackedIface("v4-wlan0", "wlan0"); NetworkStats stats = parseDetailedStats(R.raw.xt_qtaguid_with_clat); assertEquals(42, stats.size()); @@ -419,8 +418,6 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { assertStatsEntry(stats, "lo", 0, SET_DEFAULT, 0x0, 1288L, 1288L); assertNoStatsEntry(stats, "wlan0", 1029, SET_DEFAULT, 0x0); - - NetworkStatsFactory.clearStackedIfaces(); } @Test @@ -435,7 +432,7 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { long rootRxBytesAfter = 1398634L; assertEquals("UID 0 traffic should be ~0", 4623, rootRxBytesAfter - rootRxBytesBefore); - NetworkStatsFactory.noteStackedIface("v4-wlan0", "wlan0"); + mFactory.noteStackedIface("v4-wlan0", "wlan0"); NetworkStats stats; // Stats snapshot before the download @@ -447,8 +444,6 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { stats = parseDetailedStats(R.raw.xt_qtaguid_with_clat_100mb_download_after); assertStatsEntry(stats, "v4-wlan0", 10106, SET_FOREGROUND, 0x0, appRxBytesAfter, 7867488L); assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, rootRxBytesAfter, 0L); - - NetworkStatsFactory.clearStackedIfaces(); } /** diff --git a/tests/net/java/com/android/server/net/NetworkStatsServiceTest.java b/tests/net/java/com/android/server/net/NetworkStatsServiceTest.java index 51dcc3cae219..1d29a824d10d 100644 --- a/tests/net/java/com/android/server/net/NetworkStatsServiceTest.java +++ b/tests/net/java/com/android/server/net/NetworkStatsServiceTest.java @@ -96,6 +96,7 @@ import androidx.test.InstrumentationRegistry; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; +import com.android.internal.net.VpnInfo; import com.android.internal.util.ArrayUtils; import com.android.internal.util.test.BroadcastInterceptingContext; import com.android.server.net.NetworkStatsService.NetworkStatsSettings; @@ -242,7 +243,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); - mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states)); + mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states), new VpnInfo[0]); // verify service has empty history for wifi assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0); @@ -285,7 +286,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); - mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states)); + mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states), new VpnInfo[0]); // verify service has empty history for wifi assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0); @@ -358,7 +359,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); - mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states)); + mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states), new VpnInfo[0]); // modify some number on wifi, and trigger poll event incrementCurrentTime(2 * HOUR_IN_MILLIS); @@ -398,7 +399,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); - mService.forceUpdateIfaces(NETWORKS_MOBILE, states, getActiveIface(states)); + mService.forceUpdateIfaces(NETWORKS_MOBILE, states, getActiveIface(states), new VpnInfo[0]); // create some traffic on first network incrementCurrentTime(HOUR_IN_MILLIS); @@ -432,7 +433,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L) .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L)); - mService.forceUpdateIfaces(NETWORKS_MOBILE, states, getActiveIface(states)); + mService.forceUpdateIfaces(NETWORKS_MOBILE, states, getActiveIface(states), new VpnInfo[0]); forcePollAndWaitForIdle(); @@ -471,7 +472,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); - mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states)); + mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states), new VpnInfo[0]); // create some traffic incrementCurrentTime(HOUR_IN_MILLIS); @@ -528,7 +529,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); - mService.forceUpdateIfaces(NETWORKS_MOBILE, states, getActiveIface(states)); + mService.forceUpdateIfaces(NETWORKS_MOBILE, states, getActiveIface(states), new VpnInfo[0]); // create some traffic incrementCurrentTime(HOUR_IN_MILLIS); @@ -554,7 +555,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L) .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)); - mService.forceUpdateIfaces(NETWORKS_MOBILE, states, getActiveIface(states)); + mService.forceUpdateIfaces(NETWORKS_MOBILE, states, getActiveIface(states), new VpnInfo[0]); forcePollAndWaitForIdle(); @@ -583,7 +584,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); - mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states)); + mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states), new VpnInfo[0]); // create some traffic for two apps incrementCurrentTime(HOUR_IN_MILLIS); @@ -640,7 +641,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); - mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states)); + mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states), new VpnInfo[0]); NetworkStats.Entry entry1 = new NetworkStats.Entry( TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L); @@ -683,7 +684,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); - mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states)); + mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states), new VpnInfo[0]); NetworkStats.Entry uidStats = new NetworkStats.Entry( TEST_IFACE, UID_BLUE, SET_DEFAULT, 0xF00D, 1024L, 8L, 512L, 4L, 0L); @@ -695,9 +696,12 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { "otherif", UID_BLUE, SET_DEFAULT, 0xF00D, 1024L, 8L, 512L, 4L, 0L); final String[] ifaceFilter = new String[] { TEST_IFACE }; + final String[] augmentedIfaceFilter = new String[] { stackedIface, TEST_IFACE }; incrementCurrentTime(HOUR_IN_MILLIS); expectDefaultSettings(); expectNetworkStatsSummary(buildEmptyStats()); + when(mStatsFactory.augmentWithStackedInterfaces(eq(ifaceFilter))) + .thenReturn(augmentedIfaceFilter); when(mStatsFactory.readNetworkStatsDetail(eq(UID_ALL), any(), eq(TAG_ALL))) .thenReturn(new NetworkStats(getElapsedRealtime(), 1) .addValues(uidStats)); @@ -708,15 +712,16 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { NetworkStats stats = mService.getDetailedUidStats(ifaceFilter); - // mNetManager#getNetworkStatsUidDetail(UID_ALL, INTERFACES_ALL) has following invocations: + // mStatsFactory#readNetworkStatsDetail() has the following invocations: // 1) NetworkStatsService#systemReady from #setUp. // 2) mService#forceUpdateIfaces in the test above. // // Additionally, we should have one call from the above call to mService#getDetailedUidStats - // with the augmented ifaceFilter + // with the augmented ifaceFilter. verify(mStatsFactory, times(2)).readNetworkStatsDetail(UID_ALL, INTERFACES_ALL, TAG_ALL); verify(mStatsFactory, times(1)).readNetworkStatsDetail( - eq(UID_ALL), eq(NetworkStatsFactory.augmentWithStackedInterfaces(ifaceFilter)), + eq(UID_ALL), + eq(augmentedIfaceFilter), eq(TAG_ALL)); assertTrue(ArrayUtils.contains(stats.getUniqueIfaces(), TEST_IFACE)); assertTrue(ArrayUtils.contains(stats.getUniqueIfaces(), stackedIface)); @@ -733,7 +738,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); - mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states)); + mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states), new VpnInfo[0]); // create some initial traffic incrementCurrentTime(HOUR_IN_MILLIS); @@ -789,7 +794,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); - mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states)); + mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states), new VpnInfo[0]); // create some initial traffic incrementCurrentTime(HOUR_IN_MILLIS); @@ -828,7 +833,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); - mService.forceUpdateIfaces(NETWORKS_MOBILE, states, getActiveIface(states)); + mService.forceUpdateIfaces(NETWORKS_MOBILE, states, getActiveIface(states), new VpnInfo[0]); // Create some traffic incrementCurrentTime(HOUR_IN_MILLIS); @@ -865,7 +870,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); - mService.forceUpdateIfaces(NETWORKS_MOBILE, states, getActiveIface(states)); + mService.forceUpdateIfaces(NETWORKS_MOBILE, states, getActiveIface(states), new VpnInfo[0]); // create some tethering traffic incrementCurrentTime(HOUR_IN_MILLIS); @@ -905,7 +910,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); - mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states)); + mService.forceUpdateIfaces(NETWORKS_WIFI, states, getActiveIface(states), new VpnInfo[0]); // verify service has empty history for wifi assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0); |