Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Chenbo Feng | 061cec7 | 2019-03-01 15:07:24 -0800 | [diff] [blame] | 17 | package com.android.server.net; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 18 | |
Benedict Wong | 5cc3e6a | 2019-06-11 18:21:30 -0700 | [diff] [blame] | 19 | import static android.net.NetworkStats.INTERFACES_ALL; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 20 | import static android.net.NetworkStats.SET_ALL; |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 21 | import static android.net.NetworkStats.TAG_ALL; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 22 | import static android.net.NetworkStats.TAG_NONE; |
| 23 | import static android.net.NetworkStats.UID_ALL; |
Chenbo Feng | 6880d63 | 2018-12-22 13:20:31 -0800 | [diff] [blame] | 24 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 25 | import static com.android.server.NetworkManagementSocketTagger.kernelToTag; |
| 26 | |
Remi NGUYEN VAN | 088ff68 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 27 | import android.annotation.Nullable; |
Chenbo Feng | db91039 | 2019-02-27 19:07:39 -0800 | [diff] [blame] | 28 | import android.net.INetd; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 29 | import android.net.NetworkStats; |
Chenbo Feng | db91039 | 2019-02-27 19:07:39 -0800 | [diff] [blame] | 30 | import android.net.util.NetdService; |
| 31 | import android.os.RemoteException; |
Jeff Sharkey | 453dafa | 2012-02-27 17:42:34 -0800 | [diff] [blame] | 32 | import android.os.StrictMode; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 33 | import android.os.SystemClock; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 34 | |
Chenbo Feng | bce7364 | 2019-04-11 18:44:45 -0700 | [diff] [blame] | 35 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 36 | import com.android.internal.annotations.VisibleForTesting; |
Benedict Wong | c9511e7 | 2019-06-12 17:46:31 +0000 | [diff] [blame] | 37 | import com.android.internal.net.VpnInfo; |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 38 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 39 | import com.android.internal.util.ProcFileReader; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 40 | |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 41 | import libcore.io.IoUtils; |
| 42 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 43 | import java.io.File; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 44 | import java.io.FileInputStream; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 45 | import java.io.IOException; |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 46 | import java.net.ProtocolException; |
Remi NGUYEN VAN | 088ff68 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 47 | import java.util.Arrays; |
| 48 | import java.util.HashSet; |
| 49 | import java.util.Map; |
| 50 | import java.util.concurrent.ConcurrentHashMap; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Creates {@link NetworkStats} instances by parsing various {@code /proc/} |
| 54 | * files as needed. |
Ricky Wai | 0aa3aef | 2017-11-09 15:22:59 +0000 | [diff] [blame] | 55 | * |
| 56 | * @hide |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 57 | */ |
| 58 | public class NetworkStatsFactory { |
| 59 | private static final String TAG = "NetworkStatsFactory"; |
| 60 | |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 61 | private static final boolean USE_NATIVE_PARSING = true; |
| 62 | private static final boolean SANITY_CHECK_NATIVE = false; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 63 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 64 | /** Path to {@code /proc/net/xt_qtaguid/iface_stat_all}. */ |
| 65 | private final File mStatsXtIfaceAll; |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 66 | /** Path to {@code /proc/net/xt_qtaguid/iface_stat_fmt}. */ |
| 67 | private final File mStatsXtIfaceFmt; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 68 | /** Path to {@code /proc/net/xt_qtaguid/stats}. */ |
| 69 | private final File mStatsXtUid; |
| 70 | |
Benedict Wong | 51a770b | 2019-06-20 14:46:35 -0700 | [diff] [blame] | 71 | private final boolean mUseBpfStats; |
Chenbo Feng | 828f1b4 | 2017-11-20 17:03:59 -0800 | [diff] [blame] | 72 | |
Chenbo Feng | db91039 | 2019-02-27 19:07:39 -0800 | [diff] [blame] | 73 | private INetd mNetdService; |
| 74 | |
Benedict Wong | c9511e7 | 2019-06-12 17:46:31 +0000 | [diff] [blame] | 75 | /** |
| 76 | * Guards persistent data access in this class |
| 77 | * |
| 78 | * <p>In order to prevent deadlocks, critical sections protected by this lock SHALL NOT call out |
| 79 | * to other code that will acquire other locks within the system server. See b/134244752. |
| 80 | */ |
| 81 | private static final Object sPersistentDataLock = new Object(); |
Chenbo Feng | b29a616 | 2019-01-10 14:48:54 -0800 | [diff] [blame] | 82 | |
Benedict Wong | c9511e7 | 2019-06-12 17:46:31 +0000 | [diff] [blame] | 83 | /** Set containing info about active VPNs and their underlying networks. */ |
| 84 | private static volatile VpnInfo[] sVpnInfos = new VpnInfo[0]; |
| 85 | |
| 86 | // A persistent snapshot of cumulative stats since device start |
| 87 | @GuardedBy("sPersistentDataLock") |
| 88 | private NetworkStats mPersistSnapshot; |
| 89 | |
| 90 | // The persistent snapshot of tun and 464xlat adjusted stats since device start |
| 91 | @GuardedBy("sPersistentDataLock") |
| 92 | private NetworkStats mTunAnd464xlatAdjustedStats; |
| 93 | |
Remi NGUYEN VAN | 088ff68 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 94 | /** |
| 95 | * (Stacked interface) -> (base interface) association for all connected ifaces since boot. |
| 96 | * |
| 97 | * Because counters must never roll backwards, once a given interface is stacked on top of an |
| 98 | * underlying interface, the stacked interface can never be stacked on top of |
| 99 | * another interface. */ |
| 100 | private static final ConcurrentHashMap<String, String> sStackedIfaces |
| 101 | = new ConcurrentHashMap<>(); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 102 | |
| 103 | public static void noteStackedIface(String stackedIface, String baseIface) { |
Remi NGUYEN VAN | 088ff68 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 104 | if (stackedIface != null && baseIface != null) { |
| 105 | sStackedIfaces.put(stackedIface, baseIface); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
Benedict Wong | c9511e7 | 2019-06-12 17:46:31 +0000 | [diff] [blame] | 110 | * Set active VPN information for data usage migration purposes |
| 111 | * |
| 112 | * <p>Traffic on TUN-based VPNs inherently all appear to be originated from the VPN providing |
| 113 | * app's UID. This method is used to support migration of VPN data usage, ensuring data is |
| 114 | * accurately billed to the real owner of the traffic. |
| 115 | * |
| 116 | * @param vpnArray The snapshot of the currently-running VPNs. |
| 117 | */ |
| 118 | public static void updateVpnInfos(VpnInfo[] vpnArray) { |
| 119 | sVpnInfos = vpnArray.clone(); |
| 120 | } |
| 121 | |
| 122 | @VisibleForTesting |
| 123 | public static VpnInfo[] getVpnInfos() { |
| 124 | return sVpnInfos.clone(); |
| 125 | } |
| 126 | |
| 127 | /** |
Remi NGUYEN VAN | 088ff68 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 128 | * Get a set of interfaces containing specified ifaces and stacked interfaces. |
| 129 | * |
| 130 | * <p>The added stacked interfaces are ifaces stacked on top of the specified ones, or ifaces |
| 131 | * on which the specified ones are stacked. Stacked interfaces are those noted with |
| 132 | * {@link #noteStackedIface(String, String)}, but only interfaces noted before this method |
| 133 | * is called are guaranteed to be included. |
| 134 | */ |
Remi NGUYEN VAN | 9fb55e4 | 2018-02-27 16:47:22 +0900 | [diff] [blame] | 135 | public static String[] augmentWithStackedInterfaces(@Nullable String[] requiredIfaces) { |
Remi NGUYEN VAN | 088ff68 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 136 | if (requiredIfaces == NetworkStats.INTERFACES_ALL) { |
| 137 | return null; |
| 138 | } |
| 139 | |
| 140 | HashSet<String> relatedIfaces = new HashSet<>(Arrays.asList(requiredIfaces)); |
| 141 | // ConcurrentHashMap's EntrySet iterators are "guaranteed to traverse |
| 142 | // elements as they existed upon construction exactly once, and may |
| 143 | // (but are not guaranteed to) reflect any modifications subsequent to construction". |
| 144 | // This is enough here. |
| 145 | for (Map.Entry<String, String> entry : sStackedIfaces.entrySet()) { |
| 146 | if (relatedIfaces.contains(entry.getKey())) { |
| 147 | relatedIfaces.add(entry.getValue()); |
| 148 | } else if (relatedIfaces.contains(entry.getValue())) { |
| 149 | relatedIfaces.add(entry.getKey()); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
Remi NGUYEN VAN | 088ff68 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 152 | |
| 153 | String[] outArray = new String[relatedIfaces.size()]; |
| 154 | return relatedIfaces.toArray(outArray); |
| 155 | } |
| 156 | |
Remi NGUYEN VAN | 9fb55e4 | 2018-02-27 16:47:22 +0900 | [diff] [blame] | 157 | /** |
| 158 | * Applies 464xlat adjustments with ifaces noted with {@link #noteStackedIface(String, String)}. |
junyulai | c33ac0d | 2018-10-19 21:14:30 +0800 | [diff] [blame] | 159 | * @see NetworkStats#apply464xlatAdjustments(NetworkStats, NetworkStats, Map, boolean) |
Remi NGUYEN VAN | 9fb55e4 | 2018-02-27 16:47:22 +0900 | [diff] [blame] | 160 | */ |
| 161 | public static void apply464xlatAdjustments(NetworkStats baseTraffic, |
junyulai | c33ac0d | 2018-10-19 21:14:30 +0800 | [diff] [blame] | 162 | NetworkStats stackedTraffic, boolean useBpfStats) { |
| 163 | NetworkStats.apply464xlatAdjustments(baseTraffic, stackedTraffic, sStackedIfaces, |
| 164 | useBpfStats); |
Remi NGUYEN VAN | 9fb55e4 | 2018-02-27 16:47:22 +0900 | [diff] [blame] | 165 | } |
| 166 | |
Remi NGUYEN VAN | 088ff68 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 167 | @VisibleForTesting |
| 168 | public static void clearStackedIfaces() { |
| 169 | sStackedIfaces.clear(); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 172 | public NetworkStatsFactory() { |
Chenbo Feng | 6880d63 | 2018-12-22 13:20:31 -0800 | [diff] [blame] | 173 | this(new File("/proc/"), new File("/sys/fs/bpf/map_netd_app_uid_stats_map").exists()); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 176 | @VisibleForTesting |
Chenbo Feng | 828f1b4 | 2017-11-20 17:03:59 -0800 | [diff] [blame] | 177 | public NetworkStatsFactory(File procRoot, boolean useBpfStats) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 178 | mStatsXtIfaceAll = new File(procRoot, "net/xt_qtaguid/iface_stat_all"); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 179 | mStatsXtIfaceFmt = new File(procRoot, "net/xt_qtaguid/iface_stat_fmt"); |
| 180 | mStatsXtUid = new File(procRoot, "net/xt_qtaguid/stats"); |
Chenbo Feng | 828f1b4 | 2017-11-20 17:03:59 -0800 | [diff] [blame] | 181 | mUseBpfStats = useBpfStats; |
Chenbo Feng | b29a616 | 2019-01-10 14:48:54 -0800 | [diff] [blame] | 182 | mPersistSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), -1); |
Benedict Wong | c9511e7 | 2019-06-12 17:46:31 +0000 | [diff] [blame] | 183 | mTunAnd464xlatAdjustedStats = new NetworkStats(SystemClock.elapsedRealtime(), -1); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Chenbo Feng | dcc5678 | 2018-04-18 15:44:46 -0700 | [diff] [blame] | 186 | public NetworkStats readBpfNetworkStatsDev() throws IOException { |
| 187 | final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 6); |
| 188 | if (nativeReadNetworkStatsDev(stats) != 0) { |
| 189 | throw new IOException("Failed to parse bpf iface stats"); |
| 190 | } |
| 191 | return stats; |
| 192 | } |
| 193 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 194 | /** |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 195 | * Parse and return interface-level summary {@link NetworkStats} measured |
| 196 | * using {@code /proc/net/dev} style hooks, which may include non IP layer |
| 197 | * traffic. Values monotonically increase since device boot, and may include |
| 198 | * details about inactive interfaces. |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 199 | * |
| 200 | * @throws IllegalStateException when problem parsing stats. |
| 201 | */ |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 202 | public NetworkStats readNetworkStatsSummaryDev() throws IOException { |
Chenbo Feng | 93afbf2 | 2018-01-18 19:48:52 -0800 | [diff] [blame] | 203 | |
Chenbo Feng | dcc5678 | 2018-04-18 15:44:46 -0700 | [diff] [blame] | 204 | // Return xt_bpf stats if switched to bpf module. |
Chenbo Feng | 93afbf2 | 2018-01-18 19:48:52 -0800 | [diff] [blame] | 205 | if (mUseBpfStats) |
Chenbo Feng | dcc5678 | 2018-04-18 15:44:46 -0700 | [diff] [blame] | 206 | return readBpfNetworkStatsDev(); |
Chenbo Feng | 93afbf2 | 2018-01-18 19:48:52 -0800 | [diff] [blame] | 207 | |
Jeff Sharkey | 453dafa | 2012-02-27 17:42:34 -0800 | [diff] [blame] | 208 | final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads(); |
| 209 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 210 | final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 6); |
| 211 | final NetworkStats.Entry entry = new NetworkStats.Entry(); |
| 212 | |
Jeff Sharkey | 1d29a30 | 2012-02-27 18:08:01 -0800 | [diff] [blame] | 213 | ProcFileReader reader = null; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 214 | try { |
Jeff Sharkey | 1d29a30 | 2012-02-27 18:08:01 -0800 | [diff] [blame] | 215 | reader = new ProcFileReader(new FileInputStream(mStatsXtIfaceAll)); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 216 | |
Jeff Sharkey | 1d29a30 | 2012-02-27 18:08:01 -0800 | [diff] [blame] | 217 | while (reader.hasMoreData()) { |
| 218 | entry.iface = reader.nextString(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 219 | entry.uid = UID_ALL; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 220 | entry.set = SET_ALL; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 221 | entry.tag = TAG_NONE; |
| 222 | |
Jeff Sharkey | 1d29a30 | 2012-02-27 18:08:01 -0800 | [diff] [blame] | 223 | final boolean active = reader.nextInt() != 0; |
| 224 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 225 | // always include snapshot values |
Jeff Sharkey | 1d29a30 | 2012-02-27 18:08:01 -0800 | [diff] [blame] | 226 | entry.rxBytes = reader.nextLong(); |
| 227 | entry.rxPackets = reader.nextLong(); |
| 228 | entry.txBytes = reader.nextLong(); |
| 229 | entry.txPackets = reader.nextLong(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 230 | |
| 231 | // fold in active numbers, but only when active |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 232 | if (active) { |
Jeff Sharkey | 1d29a30 | 2012-02-27 18:08:01 -0800 | [diff] [blame] | 233 | entry.rxBytes += reader.nextLong(); |
| 234 | entry.rxPackets += reader.nextLong(); |
| 235 | entry.txBytes += reader.nextLong(); |
| 236 | entry.txPackets += reader.nextLong(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | stats.addValues(entry); |
Jeff Sharkey | 1d29a30 | 2012-02-27 18:08:01 -0800 | [diff] [blame] | 240 | reader.finishLine(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 241 | } |
Hugo Benichi | e1bb3a1 | 2017-04-07 15:20:56 +0900 | [diff] [blame] | 242 | } catch (NullPointerException|NumberFormatException e) { |
Neil Fuller | 038b20c | 2018-07-03 19:22:45 +0100 | [diff] [blame] | 243 | throw protocolExceptionWithCause("problem parsing stats", e); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 244 | } finally { |
| 245 | IoUtils.closeQuietly(reader); |
Jeff Sharkey | 453dafa | 2012-02-27 17:42:34 -0800 | [diff] [blame] | 246 | StrictMode.setThreadPolicy(savedPolicy); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 247 | } |
| 248 | return stats; |
| 249 | } |
| 250 | |
| 251 | /** |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 252 | * Parse and return interface-level summary {@link NetworkStats}. Designed |
| 253 | * to return only IP layer traffic. Values monotonically increase since |
| 254 | * device boot, and may include details about inactive interfaces. |
| 255 | * |
| 256 | * @throws IllegalStateException when problem parsing stats. |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 257 | */ |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 258 | public NetworkStats readNetworkStatsSummaryXt() throws IOException { |
Chenbo Feng | 93afbf2 | 2018-01-18 19:48:52 -0800 | [diff] [blame] | 259 | |
Chenbo Feng | dcc5678 | 2018-04-18 15:44:46 -0700 | [diff] [blame] | 260 | // Return xt_bpf stats if qtaguid module is replaced. |
Chenbo Feng | 93afbf2 | 2018-01-18 19:48:52 -0800 | [diff] [blame] | 261 | if (mUseBpfStats) |
Chenbo Feng | dcc5678 | 2018-04-18 15:44:46 -0700 | [diff] [blame] | 262 | return readBpfNetworkStatsDev(); |
Chenbo Feng | 93afbf2 | 2018-01-18 19:48:52 -0800 | [diff] [blame] | 263 | |
Jeff Sharkey | 453dafa | 2012-02-27 17:42:34 -0800 | [diff] [blame] | 264 | final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads(); |
| 265 | |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 266 | // return null when kernel doesn't support |
| 267 | if (!mStatsXtIfaceFmt.exists()) return null; |
| 268 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 269 | final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 6); |
| 270 | final NetworkStats.Entry entry = new NetworkStats.Entry(); |
| 271 | |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 272 | ProcFileReader reader = null; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 273 | try { |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 274 | // open and consume header line |
| 275 | reader = new ProcFileReader(new FileInputStream(mStatsXtIfaceFmt)); |
| 276 | reader.finishLine(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 277 | |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 278 | while (reader.hasMoreData()) { |
| 279 | entry.iface = reader.nextString(); |
| 280 | entry.uid = UID_ALL; |
| 281 | entry.set = SET_ALL; |
| 282 | entry.tag = TAG_NONE; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 283 | |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 284 | entry.rxBytes = reader.nextLong(); |
| 285 | entry.rxPackets = reader.nextLong(); |
| 286 | entry.txBytes = reader.nextLong(); |
| 287 | entry.txPackets = reader.nextLong(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 288 | |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 289 | stats.addValues(entry); |
| 290 | reader.finishLine(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 291 | } |
Hugo Benichi | e1bb3a1 | 2017-04-07 15:20:56 +0900 | [diff] [blame] | 292 | } catch (NullPointerException|NumberFormatException e) { |
Neil Fuller | 038b20c | 2018-07-03 19:22:45 +0100 | [diff] [blame] | 293 | throw protocolExceptionWithCause("problem parsing stats", e); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 294 | } finally { |
| 295 | IoUtils.closeQuietly(reader); |
Jeff Sharkey | 453dafa | 2012-02-27 17:42:34 -0800 | [diff] [blame] | 296 | StrictMode.setThreadPolicy(savedPolicy); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 297 | } |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 298 | return stats; |
| 299 | } |
| 300 | |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 301 | public NetworkStats readNetworkStatsDetail() throws IOException { |
Benedict Wong | 5cc3e6a | 2019-06-11 18:21:30 -0700 | [diff] [blame] | 302 | return readNetworkStatsDetail(UID_ALL, INTERFACES_ALL, TAG_ALL); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Benedict Wong | 51a770b | 2019-06-20 14:46:35 -0700 | [diff] [blame] | 305 | @GuardedBy("sPersistentDataLock") |
| 306 | private void requestSwapActiveStatsMapLocked() throws RemoteException { |
| 307 | // Ask netd to do a active map stats swap. When the binder call successfully returns, |
| 308 | // the system server should be able to safely read and clean the inactive map |
| 309 | // without race problem. |
| 310 | if (mNetdService == null) { |
| 311 | mNetdService = NetdService.getInstance(); |
| 312 | } |
| 313 | mNetdService.trafficSwapActiveStatsMap(); |
| 314 | } |
| 315 | |
Benedict Wong | 5cc3e6a | 2019-06-11 18:21:30 -0700 | [diff] [blame] | 316 | /** |
| 317 | * Reads the detailed UID stats based on the provided parameters |
| 318 | * |
| 319 | * @param limitUid the UID to limit this query to |
| 320 | * @param limitIfaces the interfaces to limit this query to. Use {@link |
| 321 | * NetworkStats.INTERFACES_ALL} to select all interfaces |
| 322 | * @param limitTag the tags to limit this query to |
| 323 | * @return the NetworkStats instance containing network statistics at the present time. |
| 324 | */ |
| 325 | public NetworkStats readNetworkStatsDetail( |
Benedict Wong | 5cc3e6a | 2019-06-11 18:21:30 -0700 | [diff] [blame] | 326 | int limitUid, String[] limitIfaces, int limitTag) throws IOException { |
Benedict Wong | c9511e7 | 2019-06-12 17:46:31 +0000 | [diff] [blame] | 327 | // In order to prevent deadlocks, anything protected by this lock MUST NOT call out to other |
| 328 | // code that will acquire other locks within the system server. See b/134244752. |
| 329 | synchronized (sPersistentDataLock) { |
| 330 | // Take a reference. If this gets swapped out, we still have the old reference. |
| 331 | final VpnInfo[] vpnArray = sVpnInfos; |
| 332 | // Take a defensive copy. mPersistSnapshot is mutated in some cases below |
| 333 | final NetworkStats prev = mPersistSnapshot.clone(); |
| 334 | |
| 335 | if (USE_NATIVE_PARSING) { |
| 336 | final NetworkStats stats = |
| 337 | new NetworkStats(SystemClock.elapsedRealtime(), 0 /* initialSize */); |
| 338 | if (mUseBpfStats) { |
Chenbo Feng | db91039 | 2019-02-27 19:07:39 -0800 | [diff] [blame] | 339 | try { |
| 340 | requestSwapActiveStatsMapLocked(); |
| 341 | } catch (RemoteException e) { |
| 342 | throw new IOException(e); |
| 343 | } |
| 344 | // Stats are always read from the inactive map, so they must be read after the |
| 345 | // swap |
Chenbo Feng | bce7364 | 2019-04-11 18:44:45 -0700 | [diff] [blame] | 346 | if (nativeReadNetworkStatsDetail(stats, mStatsXtUid.getAbsolutePath(), UID_ALL, |
Benedict Wong | c9511e7 | 2019-06-12 17:46:31 +0000 | [diff] [blame] | 347 | INTERFACES_ALL, TAG_ALL, mUseBpfStats) != 0) { |
Chenbo Feng | bce7364 | 2019-04-11 18:44:45 -0700 | [diff] [blame] | 348 | throw new IOException("Failed to parse network stats"); |
| 349 | } |
Benedict Wong | c9511e7 | 2019-06-12 17:46:31 +0000 | [diff] [blame] | 350 | |
| 351 | // BPF stats are incremental; fold into mPersistSnapshot. |
Chenbo Feng | bce7364 | 2019-04-11 18:44:45 -0700 | [diff] [blame] | 352 | mPersistSnapshot.setElapsedRealtime(stats.getElapsedRealtime()); |
| 353 | mPersistSnapshot.combineAllValues(stats); |
Benedict Wong | c9511e7 | 2019-06-12 17:46:31 +0000 | [diff] [blame] | 354 | } else { |
| 355 | if (nativeReadNetworkStatsDetail(stats, mStatsXtUid.getAbsolutePath(), UID_ALL, |
| 356 | INTERFACES_ALL, TAG_ALL, mUseBpfStats) != 0) { |
| 357 | throw new IOException("Failed to parse network stats"); |
| 358 | } |
| 359 | if (SANITY_CHECK_NATIVE) { |
| 360 | final NetworkStats javaStats = javaReadNetworkStatsDetail(mStatsXtUid, |
| 361 | UID_ALL, INTERFACES_ALL, TAG_ALL); |
| 362 | assertEquals(javaStats, stats); |
| 363 | } |
| 364 | |
| 365 | mPersistSnapshot = stats; |
Chenbo Feng | b29a616 | 2019-01-10 14:48:54 -0800 | [diff] [blame] | 366 | } |
Chenbo Feng | b29a616 | 2019-01-10 14:48:54 -0800 | [diff] [blame] | 367 | } else { |
Benedict Wong | c9511e7 | 2019-06-12 17:46:31 +0000 | [diff] [blame] | 368 | mPersistSnapshot = javaReadNetworkStatsDetail(mStatsXtUid, UID_ALL, INTERFACES_ALL, |
| 369 | TAG_ALL); |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 370 | } |
Benedict Wong | c9511e7 | 2019-06-12 17:46:31 +0000 | [diff] [blame] | 371 | |
| 372 | NetworkStats adjustedStats = adjustForTunAnd464Xlat(mPersistSnapshot, prev, vpnArray); |
| 373 | |
| 374 | // Filter return values |
| 375 | adjustedStats.filter(limitUid, limitIfaces, limitTag); |
| 376 | return adjustedStats; |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
Benedict Wong | c9511e7 | 2019-06-12 17:46:31 +0000 | [diff] [blame] | 380 | @GuardedBy("sPersistentDataLock") |
| 381 | private NetworkStats adjustForTunAnd464Xlat( |
| 382 | NetworkStats uidDetailStats, NetworkStats previousStats, VpnInfo[] vpnArray) { |
| 383 | // Calculate delta from last snapshot |
| 384 | final NetworkStats delta = uidDetailStats.subtract(previousStats); |
| 385 | |
| 386 | // Apply 464xlat adjustments before VPN adjustments. If VPNs are using v4 on a v6 only |
| 387 | // network, the overhead is their fault. |
| 388 | // No locking here: apply464xlatAdjustments behaves fine with an add-only |
| 389 | // ConcurrentHashMap. |
| 390 | delta.apply464xlatAdjustments(sStackedIfaces, mUseBpfStats); |
| 391 | |
| 392 | // Migrate data usage over a VPN to the TUN network. |
| 393 | for (VpnInfo info : vpnArray) { |
| 394 | delta.migrateTun(info.ownerUid, info.vpnIface, info.underlyingIfaces); |
| 395 | } |
| 396 | |
| 397 | // Filter out debug entries as that may lead to over counting. |
| 398 | delta.filterDebugEntries(); |
| 399 | |
| 400 | // Update mTunAnd464xlatAdjustedStats with migrated delta. |
| 401 | mTunAnd464xlatAdjustedStats.combineAllValues(delta); |
| 402 | mTunAnd464xlatAdjustedStats.setElapsedRealtime(uidDetailStats.getElapsedRealtime()); |
| 403 | |
| 404 | return mTunAnd464xlatAdjustedStats.clone(); |
| 405 | } |
| 406 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 407 | /** |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 408 | * Parse and return {@link NetworkStats} with UID-level details. Values are |
| 409 | * expected to monotonically increase since device boot. |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 410 | */ |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 411 | @VisibleForTesting |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 412 | public static NetworkStats javaReadNetworkStatsDetail(File detailPath, int limitUid, |
| 413 | String[] limitIfaces, int limitTag) |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 414 | throws IOException { |
Jeff Sharkey | 453dafa | 2012-02-27 17:42:34 -0800 | [diff] [blame] | 415 | final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads(); |
| 416 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 417 | final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 24); |
| 418 | final NetworkStats.Entry entry = new NetworkStats.Entry(); |
| 419 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 420 | int idx = 1; |
| 421 | int lastIdx = 1; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 422 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 423 | ProcFileReader reader = null; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 424 | try { |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 425 | // open and consume header line |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 426 | reader = new ProcFileReader(new FileInputStream(detailPath)); |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 427 | reader.finishLine(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 428 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 429 | while (reader.hasMoreData()) { |
| 430 | idx = reader.nextInt(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 431 | if (idx != lastIdx + 1) { |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 432 | throw new ProtocolException( |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 433 | "inconsistent idx=" + idx + " after lastIdx=" + lastIdx); |
| 434 | } |
| 435 | lastIdx = idx; |
| 436 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 437 | entry.iface = reader.nextString(); |
| 438 | entry.tag = kernelToTag(reader.nextString()); |
| 439 | entry.uid = reader.nextInt(); |
| 440 | entry.set = reader.nextInt(); |
| 441 | entry.rxBytes = reader.nextLong(); |
| 442 | entry.rxPackets = reader.nextLong(); |
| 443 | entry.txBytes = reader.nextLong(); |
| 444 | entry.txPackets = reader.nextLong(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 445 | |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 446 | if ((limitIfaces == null || ArrayUtils.contains(limitIfaces, entry.iface)) |
| 447 | && (limitUid == UID_ALL || limitUid == entry.uid) |
| 448 | && (limitTag == TAG_ALL || limitTag == entry.tag)) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 449 | stats.addValues(entry); |
| 450 | } |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 451 | |
| 452 | reader.finishLine(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 453 | } |
Hugo Benichi | e1bb3a1 | 2017-04-07 15:20:56 +0900 | [diff] [blame] | 454 | } catch (NullPointerException|NumberFormatException e) { |
Neil Fuller | 038b20c | 2018-07-03 19:22:45 +0100 | [diff] [blame] | 455 | throw protocolExceptionWithCause("problem parsing idx " + idx, e); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 456 | } finally { |
| 457 | IoUtils.closeQuietly(reader); |
Jeff Sharkey | 453dafa | 2012-02-27 17:42:34 -0800 | [diff] [blame] | 458 | StrictMode.setThreadPolicy(savedPolicy); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 459 | } |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 460 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 461 | return stats; |
| 462 | } |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 463 | |
| 464 | public void assertEquals(NetworkStats expected, NetworkStats actual) { |
| 465 | if (expected.size() != actual.size()) { |
| 466 | throw new AssertionError( |
| 467 | "Expected size " + expected.size() + ", actual size " + actual.size()); |
| 468 | } |
| 469 | |
| 470 | NetworkStats.Entry expectedRow = null; |
| 471 | NetworkStats.Entry actualRow = null; |
| 472 | for (int i = 0; i < expected.size(); i++) { |
| 473 | expectedRow = expected.getValues(i, expectedRow); |
| 474 | actualRow = actual.getValues(i, actualRow); |
| 475 | if (!expectedRow.equals(actualRow)) { |
| 476 | throw new AssertionError( |
| 477 | "Expected row " + i + ": " + expectedRow + ", actual row " + actualRow); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Parse statistics from file into given {@link NetworkStats} object. Values |
| 484 | * are expected to monotonically increase since device boot. |
| 485 | */ |
| 486 | @VisibleForTesting |
Chenbo Feng | 828f1b4 | 2017-11-20 17:03:59 -0800 | [diff] [blame] | 487 | public static native int nativeReadNetworkStatsDetail(NetworkStats stats, String path, |
| 488 | int limitUid, String[] limitIfaces, int limitTag, boolean useBpfStats); |
Chenbo Feng | dcc5678 | 2018-04-18 15:44:46 -0700 | [diff] [blame] | 489 | |
| 490 | @VisibleForTesting |
| 491 | public static native int nativeReadNetworkStatsDev(NetworkStats stats); |
Neil Fuller | 038b20c | 2018-07-03 19:22:45 +0100 | [diff] [blame] | 492 | |
| 493 | private static ProtocolException protocolExceptionWithCause(String message, Throwable cause) { |
| 494 | ProtocolException pe = new ProtocolException(message); |
| 495 | pe.initCause(cause); |
| 496 | return pe; |
| 497 | } |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 498 | } |