Revert "Addressing comments for http://ag/7700679."

This reverts commit a229333986.

Reason for revert: This change has been implicated in 4-way deadlocks as seen in b/134244752.

Bug: 134244752
Change-Id: I5fbb3443a39a21fc9d96442726cd10d20e8d61cd
This commit is contained in:
Benedict Wong
2019-06-06 17:06:39 -07:00
committed by Lorenzo Colitti
parent 54a08a686d
commit 1fbf261c01
3 changed files with 31 additions and 102 deletions

View File

@@ -1300,8 +1300,7 @@ public class NetworkStats implements Parcelable {
} }
final Entry tmpEntry = new Entry(); final Entry tmpEntry = new Entry();
final int origSize = size; for (int i = 0; i < size; i++) {
for (int i = 0; i < origSize; i++) {
if (!Objects.equals(iface[i], tunIface)) { if (!Objects.equals(iface[i], tunIface)) {
// Consider only entries that go onto the VPN interface. // Consider only entries that go onto the VPN interface.
continue; continue;
@@ -1317,9 +1316,8 @@ public class NetworkStats implements Parcelable {
tmpEntry.roaming = roaming[i]; tmpEntry.roaming = roaming[i];
tmpEntry.defaultNetwork = defaultNetwork[i]; tmpEntry.defaultNetwork = defaultNetwork[i];
// In a first pass, compute this entry's total share of data across all // In a first pass, compute each UID's total share of data across all underlyingIfaces.
// underlyingIfaces. This is computed on the basis of the share of this entry's usage // This is computed on the basis of the share of each UID's usage over tunIface.
// over tunIface.
// TODO: Consider refactoring first pass into a separate helper method. // TODO: Consider refactoring first pass into a separate helper method.
long totalRxBytes = 0; long totalRxBytes = 0;
if (tunIfaceTotal.rxBytes > 0) { if (tunIfaceTotal.rxBytes > 0) {
@@ -1396,11 +1394,9 @@ public class NetworkStats implements Parcelable {
* perInterfaceTotal[j].operations * perInterfaceTotal[j].operations
/ underlyingIfacesTotal.operations; / underlyingIfacesTotal.operations;
} }
// tmpEntry now contains the migrated data of the i-th entry for the j-th underlying
// interface. Add that data usage to this object.
combineValues(tmpEntry); combineValues(tmpEntry);
if (tag[i] == TAG_NONE) { if (tag[i] == TAG_NONE) {
// Add the migrated data to moved so it is deducted from the VPN app later.
moved[j].add(tmpEntry); moved[j].add(tmpEntry);
// Add debug info // Add debug info
tmpEntry.set = SET_DBG_VPN_IN; tmpEntry.set = SET_DBG_VPN_IN;
@@ -1416,8 +1412,8 @@ public class NetworkStats implements Parcelable {
@NonNull String[] underlyingIfaces, @NonNull String[] underlyingIfaces,
@NonNull Entry[] moved) { @NonNull Entry[] moved) {
for (int i = 0; i < underlyingIfaces.length; i++) { for (int i = 0; i < underlyingIfaces.length; i++) {
moved[i].uid = tunUid;
// Add debug info // Add debug info
moved[i].uid = tunUid;
moved[i].set = SET_DBG_VPN_OUT; moved[i].set = SET_DBG_VPN_OUT;
moved[i].tag = TAG_NONE; moved[i].tag = TAG_NONE;
moved[i].iface = underlyingIfaces[i]; moved[i].iface = underlyingIfaces[i];

View File

@@ -4396,7 +4396,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
// the underlyingNetworks list. // the underlyingNetworks list.
if (underlyingNetworks == null) { if (underlyingNetworks == null) {
NetworkAgentInfo defaultNai = getDefaultNetwork(); NetworkAgentInfo defaultNai = getDefaultNetwork();
if (defaultNai != null) { if (defaultNai != null && defaultNai.linkProperties != null) {
underlyingNetworks = new Network[] { defaultNai.network }; underlyingNetworks = new Network[] { defaultNai.network };
} }
} }
@@ -4404,7 +4404,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
List<String> interfaces = new ArrayList<>(); List<String> interfaces = new ArrayList<>();
for (Network network : underlyingNetworks) { for (Network network : underlyingNetworks) {
LinkProperties lp = getLinkProperties(network); LinkProperties lp = getLinkProperties(network);
if (lp != null && !TextUtils.isEmpty(lp.getInterfaceName())) { if (lp != null) {
interfaces.add(lp.getInterfaceName()); interfaces.add(lp.getInterfaceName());
} }
} }

View File

@@ -930,65 +930,6 @@ public class NetworkStatsServiceTest {
assertUidTotal(sTemplateImsi1, UID_TETHERING, 1920L, 14L, 384L, 2L, 0); assertUidTotal(sTemplateImsi1, UID_TETHERING, 1920L, 14L, 384L, 2L, 0);
} }
@Test
public void vpnRewriteTrafficThroughItself() throws Exception {
// WiFi network is connected and VPN is using WiFi (which has TEST_IFACE).
expectDefaultSettings();
NetworkState[] networkStates = new NetworkState[] {buildWifiState(), buildVpnState()};
VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(new String[] {TEST_IFACE})};
expectNetworkStatsUidDetail(buildEmptyStats());
expectBandwidthControlCheck();
mService.forceUpdateIfaces(
new Network[] {WIFI_NETWORK, VPN_NETWORK},
vpnInfos,
networkStates,
getActiveIface(networkStates));
// create some traffic (assume 10 bytes of MTU for VPN interface and 1 byte encryption
// overhead per packet):
//
// 1000 bytes (100 packets) were sent, and 2000 bytes (200 packets) were received by UID_RED
// over VPN.
// 500 bytes (50 packets) were sent, and 1000 bytes (100 packets) were received by UID_BLUE
// over VPN.
//
// VPN UID rewrites packets read from TUN back to TUN, plus some of its own traffic
// (100 bytes).
incrementCurrentTime(HOUR_IN_MILLIS);
expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 5)
.addValues(TUN_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 2000L, 200L, 1000L, 100L, 1L)
.addValues(TUN_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 1000L, 100L, 500L, 50L, 1L)
// VPN rewrites all the packets read from TUN + 100 additional bytes of VPN's
// own traffic.
.addValues(TUN_IFACE, UID_VPN, SET_DEFAULT, TAG_NONE, 0L, 0L, 1600L, 160L, 2L)
// VPN sent 1760 bytes over WiFi in foreground (SET_FOREGROUND) i.e. 1600
// bytes (160 packets) + 1 byte/packet overhead (=160 bytes).
.addValues(TEST_IFACE, UID_VPN, SET_FOREGROUND, TAG_NONE, 0L, 0L, 1760L, 176L, 1L)
// VPN received 3300 bytes over WiFi in background (SET_DEFAULT) i.e. 3000 bytes
// (300 packets) + 1 byte/packet encryption overhead (=300 bytes).
.addValues(TEST_IFACE, UID_VPN, SET_DEFAULT, TAG_NONE, 3300L, 300L, 0L, 0L, 1L));
forcePollAndWaitForIdle();
// Verify increased TUN usage by UID_VPN does not get attributed to other apps.
NetworkStats tunStats =
mService.getDetailedUidStats(new String[] {TUN_IFACE});
assertValues(
tunStats, TUN_IFACE, UID_RED, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL,
DEFAULT_NETWORK_ALL, 2000L, 200L, 1000L, 100L, 1);
assertValues(
tunStats, TUN_IFACE, UID_BLUE, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL,
DEFAULT_NETWORK_ALL, 1000L, 100L, 500L, 50L, 1);
assertValues(
tunStats, TUN_IFACE, UID_VPN, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL,
DEFAULT_NETWORK_ALL, 0L, 0L, 1600L, 160L, 2);
// Verify correct attribution over WiFi.
assertUidTotal(sTemplateWifi, UID_RED, 2000L, 200L, 1000L, 100L, 1);
assertUidTotal(sTemplateWifi, UID_BLUE, 1000L, 100L, 500L, 50L, 1);
assertUidTotal(sTemplateWifi, UID_VPN, 300L, 0L, 260L, 26L, 2);
}
@Test @Test
public void vpnWithOneUnderlyingIface() throws Exception { public void vpnWithOneUnderlyingIface() throws Exception {
// WiFi network is connected and VPN is using WiFi (which has TEST_IFACE). // WiFi network is connected and VPN is using WiFi (which has TEST_IFACE).
@@ -1005,29 +946,25 @@ public class NetworkStatsServiceTest {
getActiveIface(networkStates)); getActiveIface(networkStates));
// create some traffic (assume 10 bytes of MTU for VPN interface and 1 byte encryption // create some traffic (assume 10 bytes of MTU for VPN interface and 1 byte encryption
// overhead per packet): // overhead per packet):
// 1000 bytes (100 packets) were sent, and 2000 bytes (200 packets) were received by UID_RED // 1000 bytes (100 packets) were sent/received by UID_RED over VPN.
// over VPN. // 500 bytes (50 packets) were sent/received by UID_BLUE over VPN.
// 500 bytes (50 packets) were sent, and 1000 bytes (100 packets) were received by UID_BLUE // VPN sent/received 1650 bytes (150 packets) over WiFi.
// over VPN. // Of 1650 bytes over WiFi, expect 1000 bytes attributed to UID_RED, 500 bytes attributed to
// VPN sent 1650 bytes (150 packets), and received 3300 (300 packets) over WiFi. // UID_BLUE, and 150 bytes attributed to UID_VPN for both rx/tx traffic.
// Of 1650 bytes sent over WiFi, expect 1000 bytes attributed to UID_RED, 500 bytes
// attributed to UID_BLUE, and 150 bytes attributed to UID_VPN.
// Of 3300 bytes received over WiFi, expect 2000 bytes attributed to UID_RED, 1000 bytes
// attributed to UID_BLUE, and 300 bytes attributed to UID_VPN.
incrementCurrentTime(HOUR_IN_MILLIS); incrementCurrentTime(HOUR_IN_MILLIS);
expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3) expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
.addValues(TUN_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 2000L, 200L, 1000L, 100L, 1L) .addValues(TUN_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1000L, 100L, 1000L, 100L, 1L)
.addValues(TUN_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 1000L, 100L, 500L, 50L, 1L) .addValues(TUN_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 500L, 50L, 500L, 50L, 1L)
// VPN received 3300 bytes over WiFi in background (SET_DEFAULT). // VPN received 1650 bytes over WiFi in background (SET_DEFAULT).
.addValues(TEST_IFACE, UID_VPN, SET_DEFAULT, TAG_NONE, 3300L, 300L, 0L, 0L, 1L) .addValues(TEST_IFACE, UID_VPN, SET_DEFAULT, TAG_NONE, 1650L, 150L, 0L, 0L, 1L)
// VPN sent 1650 bytes over WiFi in foreground (SET_FOREGROUND). // VPN sent 1650 bytes over WiFi in foreground (SET_FOREGROUND).
.addValues(TEST_IFACE, UID_VPN, SET_FOREGROUND, TAG_NONE, 0L, 0L, 1650L, 150L, 1L)); .addValues(TEST_IFACE, UID_VPN, SET_FOREGROUND, TAG_NONE, 0L, 0L, 1650L, 150L, 1L));
forcePollAndWaitForIdle(); forcePollAndWaitForIdle();
assertUidTotal(sTemplateWifi, UID_RED, 2000L, 200L, 1000L, 100L, 1); assertUidTotal(sTemplateWifi, UID_RED, 1000L, 100L, 1000L, 100L, 1);
assertUidTotal(sTemplateWifi, UID_BLUE, 1000L, 100L, 500L, 50L, 1); assertUidTotal(sTemplateWifi, UID_BLUE, 500L, 50L, 500L, 50L, 1);
assertUidTotal(sTemplateWifi, UID_VPN, 300L, 0L, 150L, 0L, 2); assertUidTotal(sTemplateWifi, UID_VPN, 150L, 0L, 150L, 0L, 2);
} }
@Test @Test
@@ -1131,28 +1068,24 @@ public class NetworkStatsServiceTest {
getActiveIface(networkStates)); getActiveIface(networkStates));
// create some traffic (assume 10 bytes of MTU for VPN interface and 1 byte encryption // create some traffic (assume 10 bytes of MTU for VPN interface and 1 byte encryption
// overhead per packet): // overhead per packet):
// 1000 bytes (100 packets) were sent, and 500 bytes (50 packets) received by UID_RED over // 1000 bytes (100 packets) were sent/received by UID_RED over VPN.
// VPN. // VPN sent/received 660 bytes (60 packets) over WiFi and 440 bytes (40 packets) over Cell.
// VPN sent 660 bytes (60 packets) over WiFi and 440 bytes (40 packets) over Cell. // For UID_RED, expect 600 bytes attributed over WiFi and 400 bytes over Cell for both
// And, it received 330 bytes (30 packets) over WiFi and 220 bytes (20 packets) over Cell. // rx/tx.
// For UID_RED, expect 600 bytes attributed over WiFi and 400 bytes over Cell for sent (tx) // For UID_VPN, expect 60 bytes attributed over WiFi and 40 bytes over Cell for both rx/tx.
// traffic. For received (rx) traffic, expect 300 bytes over WiFi and 200 bytes over Cell.
//
// For UID_VPN, expect 60 bytes attributed over WiFi and 40 bytes over Cell for tx traffic.
// And, 30 bytes over WiFi and 20 bytes over Cell for rx traffic.
incrementCurrentTime(HOUR_IN_MILLIS); incrementCurrentTime(HOUR_IN_MILLIS);
expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3) expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
.addValues(TUN_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 500L, 50L, 1000L, 100L, 2L) .addValues(TUN_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1000L, 100L, 1000L, 100L, 2L)
.addValues(TEST_IFACE, UID_VPN, SET_DEFAULT, TAG_NONE, 330L, 30L, 660L, 60L, 1L) .addValues(TEST_IFACE, UID_VPN, SET_DEFAULT, TAG_NONE, 660L, 60L, 660L, 60L, 1L)
.addValues(TEST_IFACE2, UID_VPN, SET_DEFAULT, TAG_NONE, 220L, 20L, 440L, 40L, 1L)); .addValues(TEST_IFACE2, UID_VPN, SET_DEFAULT, TAG_NONE, 440L, 40L, 440L, 40L, 1L));
forcePollAndWaitForIdle(); forcePollAndWaitForIdle();
assertUidTotal(sTemplateWifi, UID_RED, 300L, 30L, 600L, 60L, 1); assertUidTotal(sTemplateWifi, UID_RED, 600L, 60L, 600L, 60L, 1);
assertUidTotal(sTemplateWifi, UID_VPN, 30L, 0L, 60L, 0L, 1); assertUidTotal(sTemplateWifi, UID_VPN, 60L, 0L, 60L, 0L, 1);
assertUidTotal(buildTemplateMobileWildcard(), UID_RED, 200L, 20L, 400L, 40L, 1); assertUidTotal(buildTemplateMobileWildcard(), UID_RED, 400L, 40L, 400L, 40L, 1);
assertUidTotal(buildTemplateMobileWildcard(), UID_VPN, 20L, 0L, 40L, 0L, 1); assertUidTotal(buildTemplateMobileWildcard(), UID_VPN, 40L, 0L, 40L, 0L, 1);
} }
@Test @Test