From 0aef9341d30fc842c4d75ee8fbae5a00911d04aa Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Mon, 11 Aug 2014 15:22:51 -0700 Subject: [PATCH] Hack and ship: NetworkStats edition. Some devices use clatd for catching raw IPv4 traffic when running on a pure-IPv6 carrier network. In those situations, the per-UID stats are accounted against the clat iface, so framework users need to combine both the "base" and "stacked" iface usage together. This also means that policy rules (like restricting background data or battery saver) need to apply to the stacked ifaces. Finally, we need to massage stats data slightly: -- Currently xt_qtaguid double-counts the clatd traffic *leaving* the device; both against the original UID on the clat iface, and against UID 0 on the final egress interface. -- All clatd traffic *arriving* at the device is missing the extra IPv6 packet header overhead when accounted against the final UID. Bug: 12249687, 15459248, 16296564 Change-Id: I0ee59d96831f52782de7a980e4cce9b061902fff --- core/java/android/net/LinkProperties.java | 9 +++++--- .../android/server/ConnectivityService.java | 23 +++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/core/java/android/net/LinkProperties.java b/core/java/android/net/LinkProperties.java index 8be5cf8bd1..47b74ab4d2 100644 --- a/core/java/android/net/LinkProperties.java +++ b/core/java/android/net/LinkProperties.java @@ -16,6 +16,7 @@ package android.net; +import android.annotation.NonNull; import android.net.ProxyInfo; import android.os.Parcelable; import android.os.Parcel; @@ -24,7 +25,6 @@ import android.text.TextUtils; import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; - import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Collection; @@ -480,10 +480,13 @@ public final class LinkProperties implements Parcelable { * Returns all the links stacked on top of this link. * @hide */ - public List getStackedLinks() { + public @NonNull List getStackedLinks() { + if (mStackedLinks.isEmpty()) { + return Collections.EMPTY_LIST; + } List stacked = new ArrayList(); for (LinkProperties link : mStackedLinks.values()) { - stacked.add(new LinkProperties(link)); + stacked.add(new LinkProperties(link)); } return Collections.unmodifiableList(stacked); } diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index f9baccdaee..08cb7914b7 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -119,7 +119,9 @@ import android.util.Xml; import com.android.internal.R; import com.android.internal.annotations.GuardedBy; +import com.android.internal.app.IBatteryStats; import com.android.internal.net.LegacyVpnInfo; +import com.android.internal.net.NetworkStatsFactory; import com.android.internal.net.VpnConfig; import com.android.internal.net.VpnProfile; import com.android.internal.telephony.DctConstants; @@ -4476,12 +4478,23 @@ public class ConnectivityService extends IConnectivityManager.Stub { // TODO - read the tcp buffer size config string from somewhere // updateNetworkSettings(); } - // notify battery stats service about this network + + // Notify battery stats service about this network, both the normal + // interface and any stacked links. try { - BatteryStatsService.getService().noteNetworkInterfaceType( - newNetwork.linkProperties.getInterfaceName(), - newNetwork.networkInfo.getType()); - } catch (RemoteException e) { } + final IBatteryStats bs = BatteryStatsService.getService(); + final int type = newNetwork.networkInfo.getType(); + + final String baseIface = newNetwork.linkProperties.getInterfaceName(); + bs.noteNetworkInterfaceType(baseIface, type); + for (LinkProperties stacked : newNetwork.linkProperties.getStackedLinks()) { + final String stackedIface = stacked.getInterfaceName(); + bs.noteNetworkInterfaceType(stackedIface, type); + NetworkStatsFactory.noteStackedIface(stackedIface, baseIface); + } + } catch (RemoteException ignored) { + } + notifyNetworkCallbacks(newNetwork, ConnectivityManager.CALLBACK_AVAILABLE); } else { if (DBG && newNetwork.networkRequests.size() != 0) {