Revert "Take all VPN underlying networks into account when migrating traffic for"

This reverts commit 917327b193.

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

Bug: 134244752
Change-Id: Ibdaad3a4cbf0d8ef1ed53cfab1e454b9b878bae9
This commit is contained in:
Benedict Wong
2019-06-06 17:08:02 -07:00
committed by Lorenzo Colitti
parent 055992b112
commit ac06c1022d
7 changed files with 153 additions and 410 deletions

View File

@@ -18,7 +18,6 @@ package android.net;
import static android.os.Process.CLAT_UID; import static android.os.Process.CLAT_UID;
import android.annotation.NonNull;
import android.annotation.UnsupportedAppUsage; import android.annotation.UnsupportedAppUsage;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
@@ -1176,217 +1175,133 @@ public class NetworkStats implements Parcelable {
/** /**
* VPN accounting. Move some VPN's underlying traffic to other UIDs that use tun0 iface. * VPN accounting. Move some VPN's underlying traffic to other UIDs that use tun0 iface.
* *
* <p>This method should only be called on delta NetworkStats. Do not call this method on a * This method should only be called on delta NetworkStats. Do not call this method on a
* snapshot {@link NetworkStats} object because the tunUid and/or the underlyingIface may change * snapshot {@link NetworkStats} object because the tunUid and/or the underlyingIface may
* over time. * change over time.
* *
* <p>This method performs adjustments for one active VPN package and one VPN iface at a time. * This method performs adjustments for one active VPN package and one VPN iface at a time.
*
* It is possible for the VPN software to use multiple underlying networks. This method
* only migrates traffic for the primary underlying network.
* *
* @param tunUid uid of the VPN application * @param tunUid uid of the VPN application
* @param tunIface iface of the vpn tunnel * @param tunIface iface of the vpn tunnel
* @param underlyingIfaces underlying network ifaces used by the VPN application * @param underlyingIface the primary underlying network iface used by the VPN application
* @return true if it successfully adjusts the accounting for VPN, false otherwise
*/ */
public void migrateTun(int tunUid, @NonNull String tunIface, public boolean migrateTun(int tunUid, String tunIface, String underlyingIface) {
@NonNull String[] underlyingIfaces) { Entry tunIfaceTotal = new Entry();
// Combined usage by all apps using VPN. Entry underlyingIfaceTotal = new Entry();
final Entry tunIfaceTotal = new Entry();
// Usage by VPN, grouped by its {@code underlyingIfaces}.
final Entry[] perInterfaceTotal = new Entry[underlyingIfaces.length];
// Usage by VPN, summed across all its {@code underlyingIfaces}.
final Entry underlyingIfacesTotal = new Entry();
for (int i = 0; i < perInterfaceTotal.length; i++) { tunAdjustmentInit(tunUid, tunIface, underlyingIface, tunIfaceTotal, underlyingIfaceTotal);
perInterfaceTotal[i] = new Entry();
}
tunAdjustmentInit(tunUid, tunIface, underlyingIfaces, tunIfaceTotal, perInterfaceTotal, // If tunIface < underlyingIface, it leaves the overhead traffic in the VPN app.
underlyingIfacesTotal); // If tunIface > underlyingIface, the VPN app doesn't get credit for data compression.
// If tunIface < underlyingIfacesTotal, it leaves the overhead traffic in the VPN app.
// If tunIface > underlyingIfacesTotal, the VPN app doesn't get credit for data compression.
// Negative stats should be avoided. // Negative stats should be avoided.
final Entry[] moved = Entry pool = tunGetPool(tunIfaceTotal, underlyingIfaceTotal);
addTrafficToApplications(tunUid, tunIface, underlyingIfaces, tunIfaceTotal, if (pool.isEmpty()) {
perInterfaceTotal, underlyingIfacesTotal); return true;
deductTrafficFromVpnApp(tunUid, underlyingIfaces, moved); }
Entry moved =
addTrafficToApplications(tunUid, tunIface, underlyingIface, tunIfaceTotal, pool);
deductTrafficFromVpnApp(tunUid, underlyingIface, moved);
if (!moved.isEmpty()) {
Slog.wtf(TAG, "Failed to deduct underlying network traffic from VPN package. Moved="
+ moved);
return false;
}
return true;
} }
/** /**
* Initializes the data used by the migrateTun() method. * Initializes the data used by the migrateTun() method.
* *
* <p>This is the first pass iteration which does the following work: * This is the first pass iteration which does the following work:
* * (1) Adds up all the traffic through the tunUid's underlyingIface
* <ul> * (both foreground and background).
* <li>Adds up all the traffic through the tunUid's underlyingIfaces (both foreground and * (2) Adds up all the traffic through tun0 excluding traffic from the vpn app itself.
* background).
* <li>Adds up all the traffic through tun0 excluding traffic from the vpn app itself.
* </ul>
*
* @param tunUid uid of the VPN application
* @param tunIface iface of the vpn tunnel
* @param underlyingIfaces underlying network ifaces used by the VPN application
* @param tunIfaceTotal output parameter; combined data usage by all apps using VPN
* @param perInterfaceTotal output parameter; data usage by VPN app, grouped by its {@code
* underlyingIfaces}
* @param underlyingIfacesTotal output parameter; data usage by VPN, summed across all of its
* {@code underlyingIfaces}
*/ */
private void tunAdjustmentInit(int tunUid, @NonNull String tunIface, private void tunAdjustmentInit(int tunUid, String tunIface, String underlyingIface,
@NonNull String[] underlyingIfaces, @NonNull Entry tunIfaceTotal, Entry tunIfaceTotal, Entry underlyingIfaceTotal) {
@NonNull Entry[] perInterfaceTotal, @NonNull Entry underlyingIfacesTotal) { Entry recycle = new Entry();
final Entry recycle = new Entry();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
getValues(i, recycle); getValues(i, recycle);
if (recycle.uid == UID_ALL) { if (recycle.uid == UID_ALL) {
throw new IllegalStateException( throw new IllegalStateException(
"Cannot adjust VPN accounting on an iface aggregated NetworkStats."); "Cannot adjust VPN accounting on an iface aggregated NetworkStats.");
} } if (recycle.set == SET_DBG_VPN_IN || recycle.set == SET_DBG_VPN_OUT) {
if (recycle.set == SET_DBG_VPN_IN || recycle.set == SET_DBG_VPN_OUT) {
throw new IllegalStateException( throw new IllegalStateException(
"Cannot adjust VPN accounting on a NetworkStats containing SET_DBG_VPN_*"); "Cannot adjust VPN accounting on a NetworkStats containing SET_DBG_VPN_*");
} }
if (recycle.tag != TAG_NONE) {
// TODO(b/123666283): Take all tags for tunUid into account. if (recycle.uid == tunUid && recycle.tag == TAG_NONE
continue; && Objects.equals(underlyingIface, recycle.iface)) {
underlyingIfaceTotal.add(recycle);
} }
if (recycle.uid == tunUid) { if (recycle.uid != tunUid && recycle.tag == TAG_NONE
// Add up traffic through tunUid's underlying interfaces. && Objects.equals(tunIface, recycle.iface)) {
for (int j = 0; j < underlyingIfaces.length; j++) {
if (Objects.equals(underlyingIfaces[j], recycle.iface)) {
perInterfaceTotal[j].add(recycle);
underlyingIfacesTotal.add(recycle);
break;
}
}
} else if (tunIface.equals(recycle.iface)) {
// Add up all tunIface traffic excluding traffic from the vpn app itself. // Add up all tunIface traffic excluding traffic from the vpn app itself.
tunIfaceTotal.add(recycle); tunIfaceTotal.add(recycle);
} }
} }
} }
/** private static Entry tunGetPool(Entry tunIfaceTotal, Entry underlyingIfaceTotal) {
* Distributes traffic across apps that are using given {@code tunIface}, and returns the total Entry pool = new Entry();
* traffic that should be moved off of {@code tunUid} grouped by {@code underlyingIfaces}. pool.rxBytes = Math.min(tunIfaceTotal.rxBytes, underlyingIfaceTotal.rxBytes);
* pool.rxPackets = Math.min(tunIfaceTotal.rxPackets, underlyingIfaceTotal.rxPackets);
* @param tunUid uid of the VPN application pool.txBytes = Math.min(tunIfaceTotal.txBytes, underlyingIfaceTotal.txBytes);
* @param tunIface iface of the vpn tunnel pool.txPackets = Math.min(tunIfaceTotal.txPackets, underlyingIfaceTotal.txPackets);
* @param underlyingIfaces underlying network ifaces used by the VPN application pool.operations = Math.min(tunIfaceTotal.operations, underlyingIfaceTotal.operations);
* @param tunIfaceTotal combined data usage across all apps using {@code tunIface} return pool;
* @param perInterfaceTotal data usage by VPN app, grouped by its {@code underlyingIfaces} }
* @param underlyingIfacesTotal data usage by VPN, summed across all of its {@code
* underlyingIfaces}
*/
private Entry[] addTrafficToApplications(int tunUid, @NonNull String tunIface,
@NonNull String[] underlyingIfaces, @NonNull Entry tunIfaceTotal,
@NonNull Entry[] perInterfaceTotal, @NonNull Entry underlyingIfacesTotal) {
// Traffic that should be moved off of each underlying interface for tunUid (see
// deductTrafficFromVpnApp below).
final Entry[] moved = new Entry[underlyingIfaces.length];
for (int i = 0; i < underlyingIfaces.length; i++) {
moved[i] = new Entry();
}
final Entry tmpEntry = new Entry(); private Entry addTrafficToApplications(int tunUid, String tunIface, String underlyingIface,
Entry tunIfaceTotal, Entry pool) {
Entry moved = new Entry();
Entry tmpEntry = new Entry();
tmpEntry.iface = underlyingIface;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if (!Objects.equals(iface[i], tunIface)) { // the vpn app is excluded from the redistribution but all moved traffic will be
// Consider only entries that go onto the VPN interface. // deducted from the vpn app (see deductTrafficFromVpnApp below).
continue; if (Objects.equals(iface[i], tunIface) && uid[i] != tunUid) {
} if (tunIfaceTotal.rxBytes > 0) {
if (uid[i] == tunUid) { tmpEntry.rxBytes = pool.rxBytes * rxBytes[i] / tunIfaceTotal.rxBytes;
// Exclude VPN app from the redistribution, as it can choose to create packet } else {
// streams by writing to itself. tmpEntry.rxBytes = 0;
continue;
}
tmpEntry.uid = uid[i];
tmpEntry.tag = tag[i];
tmpEntry.metered = metered[i];
tmpEntry.roaming = roaming[i];
tmpEntry.defaultNetwork = defaultNetwork[i];
// In a first pass, compute each UID's total share of data across all underlyingIfaces.
// This is computed on the basis of the share of each UID's usage over tunIface.
// TODO: Consider refactoring first pass into a separate helper method.
long totalRxBytes = 0;
if (tunIfaceTotal.rxBytes > 0) {
// Note - The multiplication below should not overflow since NetworkStatsService
// processes this every time device has transmitted/received amount equivalent to
// global threshold alert (~ 2MB) across all interfaces.
final long rxBytesAcrossUnderlyingIfaces =
underlyingIfacesTotal.rxBytes * rxBytes[i] / tunIfaceTotal.rxBytes;
// app must not be blamed for more than it consumed on tunIface
totalRxBytes = Math.min(rxBytes[i], rxBytesAcrossUnderlyingIfaces);
}
long totalRxPackets = 0;
if (tunIfaceTotal.rxPackets > 0) {
final long rxPacketsAcrossUnderlyingIfaces =
underlyingIfacesTotal.rxPackets * rxPackets[i] / tunIfaceTotal.rxPackets;
totalRxPackets = Math.min(rxPackets[i], rxPacketsAcrossUnderlyingIfaces);
}
long totalTxBytes = 0;
if (tunIfaceTotal.txBytes > 0) {
final long txBytesAcrossUnderlyingIfaces =
underlyingIfacesTotal.txBytes * txBytes[i] / tunIfaceTotal.txBytes;
totalTxBytes = Math.min(txBytes[i], txBytesAcrossUnderlyingIfaces);
}
long totalTxPackets = 0;
if (tunIfaceTotal.txPackets > 0) {
final long txPacketsAcrossUnderlyingIfaces =
underlyingIfacesTotal.txPackets * txPackets[i] / tunIfaceTotal.txPackets;
totalTxPackets = Math.min(txPackets[i], txPacketsAcrossUnderlyingIfaces);
}
long totalOperations = 0;
if (tunIfaceTotal.operations > 0) {
final long operationsAcrossUnderlyingIfaces =
underlyingIfacesTotal.operations * operations[i] / tunIfaceTotal.operations;
totalOperations = Math.min(operations[i], operationsAcrossUnderlyingIfaces);
}
// In a second pass, distribute these values across interfaces in the proportion that
// each interface represents of the total traffic of the underlying interfaces.
for (int j = 0; j < underlyingIfaces.length; j++) {
tmpEntry.iface = underlyingIfaces[j];
tmpEntry.rxBytes = 0;
// Reset 'set' to correct value since it gets updated when adding debug info below.
tmpEntry.set = set[i];
if (underlyingIfacesTotal.rxBytes > 0) {
tmpEntry.rxBytes =
totalRxBytes
* perInterfaceTotal[j].rxBytes
/ underlyingIfacesTotal.rxBytes;
} }
tmpEntry.rxPackets = 0; if (tunIfaceTotal.rxPackets > 0) {
if (underlyingIfacesTotal.rxPackets > 0) { tmpEntry.rxPackets = pool.rxPackets * rxPackets[i] / tunIfaceTotal.rxPackets;
tmpEntry.rxPackets = } else {
totalRxPackets tmpEntry.rxPackets = 0;
* perInterfaceTotal[j].rxPackets
/ underlyingIfacesTotal.rxPackets;
} }
tmpEntry.txBytes = 0; if (tunIfaceTotal.txBytes > 0) {
if (underlyingIfacesTotal.txBytes > 0) { tmpEntry.txBytes = pool.txBytes * txBytes[i] / tunIfaceTotal.txBytes;
tmpEntry.txBytes = } else {
totalTxBytes tmpEntry.txBytes = 0;
* perInterfaceTotal[j].txBytes
/ underlyingIfacesTotal.txBytes;
} }
tmpEntry.txPackets = 0; if (tunIfaceTotal.txPackets > 0) {
if (underlyingIfacesTotal.txPackets > 0) { tmpEntry.txPackets = pool.txPackets * txPackets[i] / tunIfaceTotal.txPackets;
tmpEntry.txPackets = } else {
totalTxPackets tmpEntry.txPackets = 0;
* perInterfaceTotal[j].txPackets
/ underlyingIfacesTotal.txPackets;
} }
tmpEntry.operations = 0; if (tunIfaceTotal.operations > 0) {
if (underlyingIfacesTotal.operations > 0) {
tmpEntry.operations = tmpEntry.operations =
totalOperations pool.operations * operations[i] / tunIfaceTotal.operations;
* perInterfaceTotal[j].operations } else {
/ underlyingIfacesTotal.operations; tmpEntry.operations = 0;
} }
tmpEntry.uid = uid[i];
tmpEntry.tag = tag[i];
tmpEntry.set = set[i];
tmpEntry.metered = metered[i];
tmpEntry.roaming = roaming[i];
tmpEntry.defaultNetwork = defaultNetwork[i];
combineValues(tmpEntry); combineValues(tmpEntry);
if (tag[i] == TAG_NONE) { if (tag[i] == TAG_NONE) {
moved[j].add(tmpEntry); moved.add(tmpEntry);
// Add debug info // Add debug info
tmpEntry.set = SET_DBG_VPN_IN; tmpEntry.set = SET_DBG_VPN_IN;
combineValues(tmpEntry); combineValues(tmpEntry);
@@ -1396,45 +1311,38 @@ public class NetworkStats implements Parcelable {
return moved; return moved;
} }
private void deductTrafficFromVpnApp( private void deductTrafficFromVpnApp(int tunUid, String underlyingIface, Entry moved) {
int tunUid, // Add debug info
@NonNull String[] underlyingIfaces, moved.uid = tunUid;
@NonNull Entry[] moved) { moved.set = SET_DBG_VPN_OUT;
for (int i = 0; i < underlyingIfaces.length; i++) { moved.tag = TAG_NONE;
// Add debug info moved.iface = underlyingIface;
moved[i].uid = tunUid; moved.metered = METERED_ALL;
moved[i].set = SET_DBG_VPN_OUT; moved.roaming = ROAMING_ALL;
moved[i].tag = TAG_NONE; moved.defaultNetwork = DEFAULT_NETWORK_ALL;
moved[i].iface = underlyingIfaces[i]; combineValues(moved);
moved[i].metered = METERED_ALL;
moved[i].roaming = ROAMING_ALL;
moved[i].defaultNetwork = DEFAULT_NETWORK_ALL;
combineValues(moved[i]);
// Caveat: if the vpn software uses tag, the total tagged traffic may be greater than // Caveat: if the vpn software uses tag, the total tagged traffic may be greater than
// the TAG_NONE traffic. // the TAG_NONE traffic.
// //
// Relies on the fact that the underlying traffic only has state ROAMING_NO and // Relies on the fact that the underlying traffic only has state ROAMING_NO and METERED_NO,
// METERED_NO, which should be the case as it comes directly from the /proc file. // which should be the case as it comes directly from the /proc file. We only blend in the
// We only blend in the roaming data after applying these adjustments, by checking the // roaming data after applying these adjustments, by checking the NetworkIdentity of the
// NetworkIdentity of the underlying iface. // underlying iface.
final int idxVpnBackground = findIndex(underlyingIfaces[i], tunUid, SET_DEFAULT, int idxVpnBackground = findIndex(underlyingIface, tunUid, SET_DEFAULT, TAG_NONE,
TAG_NONE, METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO); METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO);
if (idxVpnBackground != -1) { if (idxVpnBackground != -1) {
// Note - tunSubtract also updates moved[i]; whatever traffic that's left is removed tunSubtract(idxVpnBackground, this, moved);
// from foreground usage. }
tunSubtract(idxVpnBackground, this, moved[i]);
}
final int idxVpnForeground = findIndex(underlyingIfaces[i], tunUid, SET_FOREGROUND, int idxVpnForeground = findIndex(underlyingIface, tunUid, SET_FOREGROUND, TAG_NONE,
TAG_NONE, METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO); METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO);
if (idxVpnForeground != -1) { if (idxVpnForeground != -1) {
tunSubtract(idxVpnForeground, this, moved[i]); tunSubtract(idxVpnForeground, this, moved);
}
} }
} }
private static void tunSubtract(int i, @NonNull NetworkStats left, @NonNull Entry right) { private static void tunSubtract(int i, NetworkStats left, Entry right) {
long rxBytes = Math.min(left.rxBytes[i], right.rxBytes); long rxBytes = Math.min(left.rxBytes[i], right.rxBytes);
left.rxBytes[i] -= rxBytes; left.rxBytes[i] -= rxBytes;
right.rxBytes -= rxBytes; right.rxBytes -= rxBytes;

View File

@@ -19,8 +19,6 @@ package com.android.internal.net;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import java.util.Arrays;
/** /**
* A lightweight container used to carry information of the ongoing VPN. * A lightweight container used to carry information of the ongoing VPN.
* Internal use only.. * Internal use only..
@@ -30,14 +28,14 @@ import java.util.Arrays;
public class VpnInfo implements Parcelable { public class VpnInfo implements Parcelable {
public int ownerUid; public int ownerUid;
public String vpnIface; public String vpnIface;
public String[] underlyingIfaces; public String primaryUnderlyingIface;
@Override @Override
public String toString() { public String toString() {
return "VpnInfo{" return "VpnInfo{"
+ "ownerUid=" + ownerUid + "ownerUid=" + ownerUid
+ ", vpnIface='" + vpnIface + '\'' + ", vpnIface='" + vpnIface + '\''
+ ", underlyingIfaces='" + Arrays.toString(underlyingIfaces) + '\'' + ", primaryUnderlyingIface='" + primaryUnderlyingIface + '\''
+ '}'; + '}';
} }
@@ -50,7 +48,7 @@ public class VpnInfo implements Parcelable {
public void writeToParcel(Parcel dest, int flags) { public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(ownerUid); dest.writeInt(ownerUid);
dest.writeString(vpnIface); dest.writeString(vpnIface);
dest.writeStringArray(underlyingIfaces); dest.writeString(primaryUnderlyingIface);
} }
public static final Parcelable.Creator<VpnInfo> CREATOR = new Parcelable.Creator<VpnInfo>() { public static final Parcelable.Creator<VpnInfo> CREATOR = new Parcelable.Creator<VpnInfo>() {
@@ -59,7 +57,7 @@ public class VpnInfo implements Parcelable {
VpnInfo info = new VpnInfo(); VpnInfo info = new VpnInfo();
info.ownerUid = source.readInt(); info.ownerUid = source.readInt();
info.vpnIface = source.readString(); info.vpnIface = source.readString();
info.underlyingIfaces = source.readStringArray(); info.primaryUnderlyingIface = source.readString();
return info; return info;
} }

View File

@@ -19,22 +19,13 @@ package android.net;
import com.google.caliper.BeforeExperiment; import com.google.caliper.BeforeExperiment;
import com.google.caliper.Param; import com.google.caliper.Param;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class NetworkStatsBenchmark { public class NetworkStatsBenchmark {
private static final String[] UNDERLYING_IFACES = {"wlan0", "rmnet0"}; private static final String UNDERLYING_IFACE = "wlan0";
private static final String TUN_IFACE = "tun0"; private static final String TUN_IFACE = "tun0";
private static final int TUN_UID = 999999999; private static final int TUN_UID = 999999999;
@Param({"100", "1000"}) @Param({"100", "1000"})
private int mSize; private int mSize;
/**
* Should not be more than the length of {@link #UNDERLYING_IFACES}.
*/
@Param({"1", "2"})
private int mNumUnderlyingIfaces;
private NetworkStats mNetworkStats; private NetworkStats mNetworkStats;
@BeforeExperiment @BeforeExperiment
@@ -42,10 +33,8 @@ public class NetworkStatsBenchmark {
mNetworkStats = new NetworkStats(0, mSize + 2); mNetworkStats = new NetworkStats(0, mSize + 2);
int uid = 0; int uid = 0;
NetworkStats.Entry recycle = new NetworkStats.Entry(); NetworkStats.Entry recycle = new NetworkStats.Entry();
final List<String> allIfaces = getAllIfacesForBenchmark(); // also contains TUN_IFACE.
final int totalIfaces = allIfaces.size();
for (int i = 0; i < mSize; i++) { for (int i = 0; i < mSize; i++) {
recycle.iface = allIfaces.get(i % totalIfaces); recycle.iface = (i < mSize / 2) ? TUN_IFACE : UNDERLYING_IFACE;
recycle.uid = uid; recycle.uid = uid;
recycle.set = i % 2; recycle.set = i % 2;
recycle.tag = NetworkStats.TAG_NONE; recycle.tag = NetworkStats.TAG_NONE;
@@ -59,39 +48,22 @@ public class NetworkStatsBenchmark {
uid++; uid++;
} }
} }
recycle.iface = UNDERLYING_IFACE;
for (int i = 0; i < mNumUnderlyingIfaces; i++) { recycle.uid = TUN_UID;
recycle.iface = UNDERLYING_IFACES[i]; recycle.set = NetworkStats.SET_FOREGROUND;
recycle.uid = TUN_UID; recycle.tag = NetworkStats.TAG_NONE;
recycle.set = NetworkStats.SET_FOREGROUND; recycle.rxBytes = 90000 * mSize;
recycle.tag = NetworkStats.TAG_NONE; recycle.rxPackets = 40 * mSize;
recycle.rxBytes = 90000 * mSize; recycle.txBytes = 180000 * mSize;
recycle.rxPackets = 40 * mSize; recycle.txPackets = 1200 * mSize;
recycle.txBytes = 180000 * mSize; recycle.operations = 0;
recycle.txPackets = 1200 * mSize; mNetworkStats.addValues(recycle);
recycle.operations = 0;
mNetworkStats.addValues(recycle);
}
}
private String[] getVpnUnderlyingIfaces() {
return Arrays.copyOf(UNDERLYING_IFACES, mNumUnderlyingIfaces);
}
/**
* Same as {@link #getVpnUnderlyingIfaces}, but also contains {@link #TUN_IFACE}.
*/
private List<String> getAllIfacesForBenchmark() {
List<String> ifaces = new ArrayList<>();
ifaces.add(TUN_IFACE);
ifaces.addAll(Arrays.asList(getVpnUnderlyingIfaces()));
return ifaces;
} }
public void timeMigrateTun(int reps) { public void timeMigrateTun(int reps) {
for (int i = 0; i < reps; i++) { for (int i = 0; i < reps; i++) {
NetworkStats stats = mNetworkStats.clone(); NetworkStats stats = mNetworkStats.clone();
stats.migrateTun(TUN_UID, TUN_IFACE, getVpnUnderlyingIfaces()); stats.migrateTun(TUN_UID, TUN_IFACE, UNDERLYING_IFACE);
} }
} }

View File

@@ -4383,7 +4383,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
/** /**
* @return VPN information for accounting, or null if we can't retrieve all required * @return VPN information for accounting, or null if we can't retrieve all required
* information, e.g underlying ifaces. * information, e.g primary underlying iface.
*/ */
@Nullable @Nullable
private VpnInfo createVpnInfo(Vpn vpn) { private VpnInfo createVpnInfo(Vpn vpn) {
@@ -4395,24 +4395,17 @@ public class ConnectivityService extends IConnectivityManager.Stub
// see VpnService.setUnderlyingNetworks()'s javadoc about how to interpret // see VpnService.setUnderlyingNetworks()'s javadoc about how to interpret
// the underlyingNetworks list. // the underlyingNetworks list.
if (underlyingNetworks == null) { if (underlyingNetworks == null) {
NetworkAgentInfo defaultNai = getDefaultNetwork(); NetworkAgentInfo defaultNetwork = getDefaultNetwork();
if (defaultNai != null && defaultNai.linkProperties != null) { if (defaultNetwork != null && defaultNetwork.linkProperties != null) {
underlyingNetworks = new Network[] { defaultNai.network }; info.primaryUnderlyingIface = getDefaultNetwork().linkProperties.getInterfaceName();
}
} else if (underlyingNetworks.length > 0) {
LinkProperties linkProperties = getLinkProperties(underlyingNetworks[0]);
if (linkProperties != null) {
info.primaryUnderlyingIface = linkProperties.getInterfaceName();
} }
} }
if (underlyingNetworks != null && underlyingNetworks.length > 0) { return info.primaryUnderlyingIface == null ? null : info;
List<String> interfaces = new ArrayList<>();
for (Network network : underlyingNetworks) {
LinkProperties lp = getLinkProperties(network);
if (lp != null) {
interfaces.add(lp.getInterfaceName());
}
}
if (!interfaces.isEmpty()) {
info.underlyingIfaces = interfaces.toArray(new String[interfaces.size()]);
}
}
return info.underlyingIfaces == null ? null : info;
} }
/** /**

View File

@@ -41,10 +41,10 @@ import com.android.internal.net.VpnInfo;
import com.android.internal.util.FileRotator; import com.android.internal.util.FileRotator;
import com.android.internal.util.IndentingPrintWriter; import com.android.internal.util.IndentingPrintWriter;
import com.google.android.collect.Sets;
import libcore.io.IoUtils; import libcore.io.IoUtils;
import com.google.android.collect.Sets;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.File; import java.io.File;
@@ -234,7 +234,7 @@ public class NetworkStatsRecorder {
if (vpnArray != null) { if (vpnArray != null) {
for (VpnInfo info : vpnArray) { for (VpnInfo info : vpnArray) {
delta.migrateTun(info.ownerUid, info.vpnIface, info.underlyingIfaces); delta.migrateTun(info.ownerUid, info.vpnIface, info.primaryUnderlyingIface);
} }
} }

View File

@@ -569,7 +569,7 @@ public class NetworkStatsTest {
.addValues(underlyingIface, tunUid, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, .addValues(underlyingIface, tunUid, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
DEFAULT_NETWORK_NO, 0L, 0L, 0L, 0L, 0L); DEFAULT_NETWORK_NO, 0L, 0L, 0L, 0L, 0L);
delta.migrateTun(tunUid, tunIface, new String[] {underlyingIface}); assertTrue(delta.toString(), delta.migrateTun(tunUid, tunIface, underlyingIface));
assertEquals(20, delta.size()); assertEquals(20, delta.size());
// tunIface and TEST_IFACE entries are not changed. // tunIface and TEST_IFACE entries are not changed.
@@ -650,7 +650,7 @@ public class NetworkStatsTest {
.addValues(underlyingIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, .addValues(underlyingIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
DEFAULT_NETWORK_NO, 75500L, 37L, 130000L, 70L, 0L); DEFAULT_NETWORK_NO, 75500L, 37L, 130000L, 70L, 0L);
delta.migrateTun(tunUid, tunIface, new String[]{underlyingIface}); assertTrue(delta.migrateTun(tunUid, tunIface, underlyingIface));
assertEquals(9, delta.size()); assertEquals(9, delta.size());
// tunIface entries should not be changed. // tunIface entries should not be changed.

View File

@@ -927,7 +927,7 @@ public class NetworkStatsServiceTest {
// 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).
expectDefaultSettings(); expectDefaultSettings();
NetworkState[] networkStates = new NetworkState[] {buildWifiState(), buildVpnState()}; NetworkState[] networkStates = new NetworkState[] {buildWifiState(), buildVpnState()};
VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(new String[] {TEST_IFACE})}; VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(TEST_IFACE)};
expectNetworkStatsUidDetail(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats());
expectBandwidthControlCheck(); expectBandwidthControlCheck();
@@ -947,10 +947,8 @@ public class NetworkStatsServiceTest {
expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3) expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
.addValues(TUN_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1000L, 100L, 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, 500L, 50L, 500L, 50L, 1L) .addValues(TUN_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 500L, 50L, 500L, 50L, 1L)
// VPN received 1650 bytes over WiFi in background (SET_DEFAULT). .addValues(
.addValues(TEST_IFACE, UID_VPN, SET_DEFAULT, TAG_NONE, 1650L, 150L, 0L, 0L, 1L) TEST_IFACE, UID_VPN, SET_DEFAULT, TAG_NONE, 1650L, 150L, 1650L, 150L, 2L));
// VPN sent 1650 bytes over WiFi in foreground (SET_FOREGROUND).
.addValues(TEST_IFACE, UID_VPN, SET_FOREGROUND, TAG_NONE, 0L, 0L, 1650L, 150L, 1L));
forcePollAndWaitForIdle(); forcePollAndWaitForIdle();
@@ -964,7 +962,7 @@ public class NetworkStatsServiceTest {
// 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).
expectDefaultSettings(); expectDefaultSettings();
NetworkState[] networkStates = new NetworkState[] {buildWifiState(), buildVpnState()}; NetworkState[] networkStates = new NetworkState[] {buildWifiState(), buildVpnState()};
VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(new String[] {TEST_IFACE})}; VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(TEST_IFACE)};
expectNetworkStatsUidDetail(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats());
expectBandwidthControlCheck(); expectBandwidthControlCheck();
@@ -994,132 +992,6 @@ public class NetworkStatsServiceTest {
assertUidTotal(sTemplateWifi, UID_VPN, 0L, 0L, 0L, 0L, 0); assertUidTotal(sTemplateWifi, UID_VPN, 0L, 0L, 0L, 0L, 0);
} }
@Test
public void vpnWithTwoUnderlyingIfaces_packetDuplication() throws Exception {
// WiFi and Cell networks are connected and VPN is using WiFi (which has TEST_IFACE) and
// 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.
expectDefaultSettings();
NetworkState[] networkStates =
new NetworkState[] {
buildWifiState(), buildMobile4gState(TEST_IFACE2), buildVpnState()
};
VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(new String[] {TEST_IFACE, TEST_IFACE2})};
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/received by UID_RED and UID_BLUE over VPN.
// VPN sent/received 4400 bytes (400 packets) over both WiFi and Cell (8800 bytes in total).
// Of 8800 bytes over WiFi/Cell, expect:
// - 500 bytes rx/tx each over WiFi/Cell attributed to both UID_RED and UID_BLUE.
// - 1200 bytes rx/tx each over WiFi/Cell for VPN_UID.
incrementCurrentTime(HOUR_IN_MILLIS);
expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 4)
.addValues(TUN_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1000L, 100L, 1000L, 100L, 2L)
.addValues(TUN_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 1000L, 100L, 1000L, 100L, 2L)
.addValues(TEST_IFACE, UID_VPN, SET_DEFAULT, TAG_NONE, 2200L, 200L, 2200L, 200L, 2L)
.addValues(
TEST_IFACE2, UID_VPN, SET_DEFAULT, TAG_NONE, 2200L, 200L, 2200L, 200L, 2L));
forcePollAndWaitForIdle();
assertUidTotal(sTemplateWifi, UID_RED, 500L, 50L, 500L, 50L, 1);
assertUidTotal(sTemplateWifi, UID_BLUE, 500L, 50L, 500L, 50L, 1);
assertUidTotal(sTemplateWifi, UID_VPN, 1200L, 100L, 1200L, 100L, 2);
assertUidTotal(buildTemplateMobileWildcard(), UID_RED, 500L, 50L, 500L, 50L, 1);
assertUidTotal(buildTemplateMobileWildcard(), UID_BLUE, 500L, 50L, 500L, 50L, 1);
assertUidTotal(buildTemplateMobileWildcard(), UID_VPN, 1200L, 100L, 1200L, 100L, 2);
}
@Test
public void vpnWithTwoUnderlyingIfaces_splitTraffic() throws Exception {
// WiFi and Cell networks are connected and VPN is using WiFi (which has TEST_IFACE) and
// 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.
expectDefaultSettings();
NetworkState[] networkStates =
new NetworkState[] {
buildWifiState(), buildMobile4gState(TEST_IFACE2), buildVpnState()
};
VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(new String[] {TEST_IFACE, TEST_IFACE2})};
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/received by UID_RED over VPN.
// VPN sent/received 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
// rx/tx.
// For UID_VPN, expect 60 bytes attributed over WiFi and 40 bytes over Cell for both rx/tx.
incrementCurrentTime(HOUR_IN_MILLIS);
expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
.addValues(TUN_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1000L, 100L, 1000L, 100L, 2L)
.addValues(TEST_IFACE, UID_VPN, SET_DEFAULT, TAG_NONE, 660L, 60L, 660L, 60L, 1L)
.addValues(TEST_IFACE2, UID_VPN, SET_DEFAULT, TAG_NONE, 440L, 40L, 440L, 40L, 1L));
forcePollAndWaitForIdle();
assertUidTotal(sTemplateWifi, UID_RED, 600L, 60L, 600L, 60L, 1);
assertUidTotal(sTemplateWifi, UID_VPN, 60L, 0L, 60L, 0L, 1);
assertUidTotal(buildTemplateMobileWildcard(), UID_RED, 400L, 40L, 400L, 40L, 1);
assertUidTotal(buildTemplateMobileWildcard(), UID_VPN, 40L, 0L, 40L, 0L, 1);
}
@Test
public void vpnWithTwoUnderlyingIfaces_splitTrafficWithCompression() throws Exception {
// WiFi and Cell networks are connected and VPN is using WiFi (which has TEST_IFACE) and
// 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.
expectDefaultSettings();
NetworkState[] networkStates =
new NetworkState[] {
buildWifiState(), buildMobile4gState(TEST_IFACE2), buildVpnState()
};
VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(new String[] {TEST_IFACE, TEST_IFACE2})};
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:
// 1000 bytes (100 packets) were sent/received by UID_RED over VPN.
// VPN sent/received 600 bytes (60 packets) over WiFi and 200 bytes (20 packets) over Cell.
// For UID_RED, expect 600 bytes attributed over WiFi and 200 bytes over Cell for both
// rx/tx.
// UID_VPN gets nothing attributed to it (avoiding negative stats).
incrementCurrentTime(HOUR_IN_MILLIS);
expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 4)
.addValues(TUN_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1000L, 100L, 1000L, 100L, 1L)
.addValues(TEST_IFACE, UID_VPN, SET_DEFAULT, TAG_NONE, 600L, 60L, 600L, 60L, 0L)
.addValues(TEST_IFACE2, UID_VPN, SET_DEFAULT, TAG_NONE, 200L, 20L, 200L, 20L, 0L));
forcePollAndWaitForIdle();
assertUidTotal(sTemplateWifi, UID_RED, 600L, 60L, 600L, 60L, 0);
assertUidTotal(sTemplateWifi, UID_VPN, 0L, 0L, 0L, 0L, 0);
assertUidTotal(buildTemplateMobileWildcard(), UID_RED, 200L, 20L, 200L, 20L, 0);
assertUidTotal(buildTemplateMobileWildcard(), UID_VPN, 0L, 0L, 0L, 0L, 0);
}
@Test @Test
public void vpnWithIncorrectUnderlyingIface() throws Exception { public void vpnWithIncorrectUnderlyingIface() throws Exception {
// WiFi and Cell networks are connected and VPN is using Cell (which has TEST_IFACE2), // WiFi and Cell networks are connected and VPN is using Cell (which has TEST_IFACE2),
@@ -1129,7 +1001,7 @@ public class NetworkStatsServiceTest {
new NetworkState[] { new NetworkState[] {
buildWifiState(), buildMobile4gState(TEST_IFACE2), buildVpnState() buildWifiState(), buildMobile4gState(TEST_IFACE2), buildVpnState()
}; };
VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(new String[] {TEST_IFACE})}; VpnInfo[] vpnInfos = new VpnInfo[] {createVpnInfo(TEST_IFACE)};
expectNetworkStatsUidDetail(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats());
expectBandwidthControlCheck(); expectBandwidthControlCheck();
@@ -1510,11 +1382,11 @@ public class NetworkStatsServiceTest {
return new NetworkState(info, prop, new NetworkCapabilities(), VPN_NETWORK, null, null); return new NetworkState(info, prop, new NetworkCapabilities(), VPN_NETWORK, null, null);
} }
private static VpnInfo createVpnInfo(String[] underlyingIfaces) { private static VpnInfo createVpnInfo(String underlyingIface) {
VpnInfo info = new VpnInfo(); VpnInfo info = new VpnInfo();
info.ownerUid = UID_VPN; info.ownerUid = UID_VPN;
info.vpnIface = TUN_IFACE; info.vpnIface = TUN_IFACE;
info.underlyingIfaces = underlyingIfaces; info.primaryUnderlyingIface = underlyingIface;
return info; return info;
} }