Merge changes Ieb8645ac,I6466ec14,I87deb82b,I995b108e,Ib6521459 am: f4c10e8bc2

am: 48f59a0fdf

Change-Id: I960e94b03b29282ae2b03f78a19ed2692bd88e05
This commit is contained in:
Varun Anand
2019-06-17 10:33:53 -07:00
committed by android-build-merger
6 changed files with 373 additions and 227 deletions

View File

@@ -16,6 +16,7 @@
package com.android.server.net;
import static android.net.NetworkStats.INTERFACES_ALL;
import static android.net.NetworkStats.SET_ALL;
import static android.net.NetworkStats.TAG_ALL;
import static android.net.NetworkStats.TAG_NONE;
@@ -33,6 +34,7 @@ import android.os.SystemClock;
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.ProcFileReader;
@@ -70,11 +72,25 @@ public class NetworkStatsFactory {
private INetd mNetdService;
// A persistent Snapshot since device start for eBPF stats
@GuardedBy("mPersistSnapshot")
private final NetworkStats mPersistSnapshot;
/**
* Guards persistent data access in this class
*
* <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();
/** Set containing info about active VPNs and their underlying networks. */
private static volatile VpnInfo[] sVpnInfos = new VpnInfo[0];
// A persistent snapshot of cumulative stats since device start
@GuardedBy("sPersistentDataLock")
private NetworkStats mPersistSnapshot;
// The persistent snapshot of tun and 464xlat adjusted stats since device start
@GuardedBy("sPersistentDataLock")
private NetworkStats mTunAnd464xlatAdjustedStats;
// TODO: only do adjustments in NetworkStatsService and remove this.
/**
* (Stacked interface) -> (base interface) association for all connected ifaces since boot.
*
@@ -90,6 +106,24 @@ public class NetworkStatsFactory {
}
}
/**
* Set active VPN information for data usage migration purposes
*
* <p>Traffic on TUN-based VPNs inherently all appear to be originated from the VPN providing
* app's UID. This method is used to support migration of VPN data usage, ensuring data is
* accurately billed to the real owner of the traffic.
*
* @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();
}
/**
* Get a set of interfaces containing specified ifaces and stacked interfaces.
*
@@ -146,6 +180,7 @@ public class NetworkStatsFactory {
mStatsXtUid = new File(procRoot, "net/xt_qtaguid/stats");
mUseBpfStats = useBpfStats;
mPersistSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), -1);
mTunAnd464xlatAdjustedStats = new NetworkStats(SystemClock.elapsedRealtime(), -1);
}
public NetworkStats readBpfNetworkStatsDev() throws IOException {
@@ -264,22 +299,24 @@ public class NetworkStatsFactory {
}
public NetworkStats readNetworkStatsDetail() throws IOException {
return readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
return readNetworkStatsDetail(UID_ALL, INTERFACES_ALL, TAG_ALL);
}
public NetworkStats readNetworkStatsDetail(int limitUid, String[] limitIfaces, int limitTag,
NetworkStats lastStats) throws IOException {
final NetworkStats stats =
readNetworkStatsDetailInternal(limitUid, limitIfaces, limitTag, lastStats);
// No locking here: apply464xlatAdjustments behaves fine with an add-only ConcurrentHashMap.
// TODO: remove this and only apply adjustments in NetworkStatsService.
stats.apply464xlatAdjustments(sStackedIfaces, mUseBpfStats);
return stats;
/**
* Reads the detailed UID stats based on the provided parameters
*
* @param limitUid the UID to limit this query to
* @param limitIfaces the interfaces to limit this query to. Use {@link
* NetworkStats.INTERFACES_ALL} to select all interfaces
* @param limitTag the tags to limit this query to
* @return the NetworkStats instance containing network statistics at the present time.
*/
public NetworkStats readNetworkStatsDetail(
int limitUid, @Nullable String[] limitIfaces, int limitTag) throws IOException {
return readNetworkStatsDetailInternal(limitUid, limitIfaces, limitTag);
}
@GuardedBy("mPersistSnapshot")
@GuardedBy("sPersistentDataLock")
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
@@ -292,19 +329,20 @@ public class NetworkStatsFactory {
}
}
// TODO: delete the lastStats parameter
private NetworkStats readNetworkStatsDetailInternal(int limitUid, String[] limitIfaces,
int limitTag, NetworkStats lastStats) throws IOException {
if (USE_NATIVE_PARSING) {
final NetworkStats stats;
if (lastStats != null) {
stats = lastStats;
stats.setElapsedRealtime(SystemClock.elapsedRealtime());
} else {
stats = new NetworkStats(SystemClock.elapsedRealtime(), -1);
}
if (mUseBpfStats) {
synchronized (mPersistSnapshot) {
private NetworkStats readNetworkStatsDetailInternal(
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) {
// Take a reference. If this gets swapped out, we still have the old reference.
final VpnInfo[] vpnArray = sVpnInfos;
// Take a defensive copy. mPersistSnapshot is mutated in some cases below
final NetworkStats prev = mPersistSnapshot.clone();
if (USE_NATIVE_PARSING) {
final NetworkStats stats =
new NetworkStats(SystemClock.elapsedRealtime(), 0 /* initialSize */);
if (mUseBpfStats) {
try {
requestSwapActiveStatsMapLocked();
} catch (RemoteException e) {
@@ -313,32 +351,66 @@ public class NetworkStatsFactory {
// Stats are always read from the inactive map, so they must be read after the
// swap
if (nativeReadNetworkStatsDetail(stats, mStatsXtUid.getAbsolutePath(), UID_ALL,
null, TAG_ALL, mUseBpfStats) != 0) {
INTERFACES_ALL, TAG_ALL, mUseBpfStats) != 0) {
throw new IOException("Failed to parse network stats");
}
// BPF stats are incremental; fold into mPersistSnapshot.
mPersistSnapshot.setElapsedRealtime(stats.getElapsedRealtime());
mPersistSnapshot.combineAllValues(stats);
NetworkStats result = mPersistSnapshot.clone();
result.filter(limitUid, limitIfaces, limitTag);
return result;
} else {
if (nativeReadNetworkStatsDetail(stats, mStatsXtUid.getAbsolutePath(), UID_ALL,
INTERFACES_ALL, TAG_ALL, mUseBpfStats) != 0) {
throw new IOException("Failed to parse network stats");
}
if (SANITY_CHECK_NATIVE) {
final NetworkStats javaStats = javaReadNetworkStatsDetail(mStatsXtUid,
UID_ALL, INTERFACES_ALL, TAG_ALL);
assertEquals(javaStats, stats);
}
mPersistSnapshot = stats;
}
} else {
if (nativeReadNetworkStatsDetail(stats, mStatsXtUid.getAbsolutePath(), limitUid,
limitIfaces, limitTag, mUseBpfStats) != 0) {
throw new IOException("Failed to parse network stats");
}
if (SANITY_CHECK_NATIVE) {
final NetworkStats javaStats = javaReadNetworkStatsDetail(mStatsXtUid, limitUid,
limitIfaces, limitTag);
assertEquals(javaStats, stats);
}
return stats;
mPersistSnapshot = javaReadNetworkStatsDetail(mStatsXtUid, UID_ALL, INTERFACES_ALL,
TAG_ALL);
}
} else {
return javaReadNetworkStatsDetail(mStatsXtUid, limitUid, limitIfaces, limitTag);
NetworkStats adjustedStats = adjustForTunAnd464Xlat(mPersistSnapshot, prev, vpnArray);
// Filter return values
adjustedStats.filter(limitUid, limitIfaces, limitTag);
return adjustedStats;
}
}
@GuardedBy("sPersistentDataLock")
private NetworkStats adjustForTunAnd464Xlat(
NetworkStats uidDetailStats, NetworkStats previousStats, VpnInfo[] vpnArray) {
// Calculate delta from last snapshot
final NetworkStats delta = uidDetailStats.subtract(previousStats);
// Apply 464xlat adjustments before VPN adjustments. If VPNs are using v4 on a v6 only
// network, the overhead is their fault.
// No locking here: apply464xlatAdjustments behaves fine with an add-only
// ConcurrentHashMap.
delta.apply464xlatAdjustments(sStackedIfaces, mUseBpfStats);
// Migrate data usage over a VPN to the TUN network.
for (VpnInfo info : vpnArray) {
delta.migrateTun(info.ownerUid, info.vpnIface, info.underlyingIfaces);
}
// Filter out debug entries as that may lead to over counting.
delta.filterDebugEntries();
// Update mTunAnd464xlatAdjustedStats with migrated delta.
mTunAnd464xlatAdjustedStats.combineAllValues(delta);
mTunAnd464xlatAdjustedStats.setElapsedRealtime(uidDetailStats.getElapsedRealtime());
return mTunAnd464xlatAdjustedStats.clone();
}
/**
* Parse and return {@link NetworkStats} with UID-level details. Values are
* expected to monotonically increase since device boot.

View File

@@ -39,7 +39,6 @@ import android.util.Slog;
import android.util.SparseArray;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.net.VpnInfo;
import java.util.concurrent.atomic.AtomicInteger;
@@ -104,9 +103,9 @@ class NetworkStatsObservers {
public void updateStats(NetworkStats xtSnapshot, NetworkStats uidSnapshot,
ArrayMap<String, NetworkIdentitySet> activeIfaces,
ArrayMap<String, NetworkIdentitySet> activeUidIfaces,
VpnInfo[] vpnArray, long currentTime) {
long currentTime) {
StatsContext statsContext = new StatsContext(xtSnapshot, uidSnapshot, activeIfaces,
activeUidIfaces, vpnArray, currentTime);
activeUidIfaces, currentTime);
getHandler().sendMessage(mHandler.obtainMessage(MSG_UPDATE_STATS, statsContext));
}
@@ -354,7 +353,7 @@ class NetworkStatsObservers {
// thread will update it. We pass a null VPN array because usage is aggregated by uid
// for this snapshot, so VPN traffic can't be reattributed to responsible apps.
mRecorder.recordSnapshotLocked(statsContext.mXtSnapshot, statsContext.mActiveIfaces,
null /* vpnArray */, statsContext.mCurrentTime);
statsContext.mCurrentTime);
}
/**
@@ -396,7 +395,7 @@ class NetworkStatsObservers {
// thread will update it. We pass the VPN info so VPN traffic is reattributed to
// responsible apps.
mRecorder.recordSnapshotLocked(statsContext.mUidSnapshot, statsContext.mActiveUidIfaces,
statsContext.mVpnArray, statsContext.mCurrentTime);
statsContext.mCurrentTime);
}
/**
@@ -427,18 +426,16 @@ class NetworkStatsObservers {
NetworkStats mUidSnapshot;
ArrayMap<String, NetworkIdentitySet> mActiveIfaces;
ArrayMap<String, NetworkIdentitySet> mActiveUidIfaces;
VpnInfo[] mVpnArray;
long mCurrentTime;
StatsContext(NetworkStats xtSnapshot, NetworkStats uidSnapshot,
ArrayMap<String, NetworkIdentitySet> activeIfaces,
ArrayMap<String, NetworkIdentitySet> activeUidIfaces,
VpnInfo[] vpnArray, long currentTime) {
long currentTime) {
mXtSnapshot = xtSnapshot;
mUidSnapshot = uidSnapshot;
mActiveIfaces = activeIfaces;
mActiveUidIfaces = activeUidIfaces;
mVpnArray = vpnArray;
mCurrentTime = currentTime;
}
}

View File

@@ -23,7 +23,6 @@ import static android.text.format.DateUtils.YEAR_IN_MILLIS;
import static com.android.internal.util.Preconditions.checkNotNull;
import android.annotation.Nullable;
import android.net.NetworkStats;
import android.net.NetworkStats.NonMonotonicObserver;
import android.net.NetworkStatsHistory;
@@ -37,14 +36,13 @@ import android.util.MathUtils;
import android.util.Slog;
import android.util.proto.ProtoOutputStream;
import com.android.internal.net.VpnInfo;
import com.android.internal.util.FileRotator;
import com.android.internal.util.IndentingPrintWriter;
import libcore.io.IoUtils;
import com.google.android.collect.Sets;
import libcore.io.IoUtils;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
@@ -202,18 +200,12 @@ public class NetworkStatsRecorder {
}
/**
* Record any delta that occurred since last {@link NetworkStats} snapshot,
* using the given {@link Map} to identify network interfaces. First
* snapshot is considered bootstrap, and is not counted as delta.
*
* @param vpnArray Optional info about the currently active VPN, if any. This is used to
* redistribute traffic from the VPN app to the underlying responsible apps.
* This should always be set to null if the provided snapshot is aggregated
* across all UIDs (e.g. contains UID_ALL buckets), regardless of VPN state.
* Record any delta that occurred since last {@link NetworkStats} snapshot, using the given
* {@link Map} to identify network interfaces. First snapshot is considered bootstrap, and is
* not counted as delta.
*/
public void recordSnapshotLocked(NetworkStats snapshot,
Map<String, NetworkIdentitySet> ifaceIdent, @Nullable VpnInfo[] vpnArray,
long currentTimeMillis) {
Map<String, NetworkIdentitySet> ifaceIdent, long currentTimeMillis) {
final HashSet<String> unknownIfaces = Sets.newHashSet();
// skip recording when snapshot missing
@@ -232,12 +224,6 @@ public class NetworkStatsRecorder {
final long end = currentTimeMillis;
final long start = end - delta.getElapsedRealtime();
if (vpnArray != null) {
for (VpnInfo info : vpnArray) {
delta.migrateTun(info.ownerUid, info.vpnIface, info.primaryUnderlyingIface);
}
}
NetworkStats.Entry entry = null;
for (int i = 0; i < delta.size(); i++) {
entry = delta.getValues(i, entry);

View File

@@ -131,7 +131,6 @@ 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;
@@ -267,10 +266,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
@GuardedBy("mStatsLock")
private Network[] mDefaultNetworks = new Network[0];
/** Set containing info about active VPNs and their underlying networks. */
@GuardedBy("mStatsLock")
private VpnInfo[] mVpnInfos = new VpnInfo[0];
private final DropBoxNonMonotonicObserver mNonMonotonicObserver =
new DropBoxNonMonotonicObserver();
@@ -864,7 +859,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
@Override
public void forceUpdateIfaces(
Network[] defaultNetworks,
VpnInfo[] vpnArray,
NetworkState[] networkStates,
String activeIface) {
checkNetworkStackPermission(mContext);
@@ -872,7 +866,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
final long token = Binder.clearCallingIdentity();
try {
updateIfaces(defaultNetworks, vpnArray, networkStates, activeIface);
updateIfaces(defaultNetworks, networkStates, activeIface);
} finally {
Binder.restoreCallingIdentity(token);
}
@@ -1138,13 +1132,11 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
private void updateIfaces(
Network[] defaultNetworks,
VpnInfo[] vpnArray,
NetworkState[] networkStates,
String activeIface) {
synchronized (mStatsLock) {
mWakeLock.acquire();
try {
mVpnInfos = vpnArray;
mActiveIface = activeIface;
updateIfacesLocked(defaultNetworks, networkStates);
} finally {
@@ -1154,10 +1146,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
}
/**
* Inspect all current {@link NetworkState} to derive mapping from {@code
* iface} to {@link NetworkStatsHistory}. When multiple {@link NetworkInfo}
* are active on a single {@code iface}, they are combined under a single
* {@link NetworkIdentitySet}.
* Inspect all current {@link NetworkState} to derive mapping from {@code iface} to {@link
* NetworkStatsHistory}. When multiple {@link NetworkInfo} are active on a single {@code iface},
* they are combined under a single {@link NetworkIdentitySet}.
*/
@GuardedBy("mStatsLock")
private void updateIfacesLocked(Network[] defaultNetworks, NetworkState[] states) {
@@ -1274,27 +1265,24 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
// For xt/dev, we pass a null VPN array because usage is aggregated by UID, so VPN traffic
// can't be reattributed to responsible apps.
Trace.traceBegin(TRACE_TAG_NETWORK, "recordDev");
mDevRecorder.recordSnapshotLocked(
devSnapshot, mActiveIfaces, null /* vpnArray */, currentTime);
mDevRecorder.recordSnapshotLocked(devSnapshot, mActiveIfaces, currentTime);
Trace.traceEnd(TRACE_TAG_NETWORK);
Trace.traceBegin(TRACE_TAG_NETWORK, "recordXt");
mXtRecorder.recordSnapshotLocked(
xtSnapshot, mActiveIfaces, null /* vpnArray */, currentTime);
mXtRecorder.recordSnapshotLocked(xtSnapshot, mActiveIfaces, currentTime);
Trace.traceEnd(TRACE_TAG_NETWORK);
// For per-UID stats, pass the VPN info so VPN traffic is reattributed to responsible apps.
VpnInfo[] vpnArray = mVpnInfos;
Trace.traceBegin(TRACE_TAG_NETWORK, "recordUid");
mUidRecorder.recordSnapshotLocked(uidSnapshot, mActiveUidIfaces, vpnArray, currentTime);
mUidRecorder.recordSnapshotLocked(uidSnapshot, mActiveUidIfaces, currentTime);
Trace.traceEnd(TRACE_TAG_NETWORK);
Trace.traceBegin(TRACE_TAG_NETWORK, "recordUidTag");
mUidTagRecorder.recordSnapshotLocked(uidSnapshot, mActiveUidIfaces, vpnArray, currentTime);
mUidTagRecorder.recordSnapshotLocked(uidSnapshot, mActiveUidIfaces, currentTime);
Trace.traceEnd(TRACE_TAG_NETWORK);
// We need to make copies of member fields that are sent to the observer to avoid
// a race condition between the service handler thread and the observer's
mStatsObservers.updateStats(xtSnapshot, uidSnapshot, new ArrayMap<>(mActiveIfaces),
new ArrayMap<>(mActiveUidIfaces), vpnArray, currentTime);
new ArrayMap<>(mActiveUidIfaces), currentTime);
}
/**
@@ -1667,8 +1655,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
*/
private NetworkStats getNetworkStatsUidDetail(String[] ifaces)
throws RemoteException {
// TODO: remove 464xlat adjustments from NetworkStatsFactory and apply all at once here.
final NetworkStats uidSnapshot = mNetworkManager.getNetworkStatsUidDetail(UID_ALL,
ifaces);