• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.net;
18 
19 import static com.android.net.module.util.NetworkStatsUtils.multiplySafeByRational;
20 
21 import android.annotation.FlaggedApi;
22 import android.annotation.IntDef;
23 import android.annotation.NonNull;
24 import android.annotation.Nullable;
25 import android.annotation.SystemApi;
26 import android.compat.annotation.UnsupportedAppUsage;
27 import android.os.Build;
28 import android.os.Parcel;
29 import android.os.Parcelable;
30 import android.os.Process;
31 import android.os.SystemClock;
32 import android.text.TextUtils;
33 import android.util.SparseBooleanArray;
34 
35 import com.android.internal.annotations.VisibleForTesting;
36 import com.android.modules.utils.build.SdkLevel;
37 import com.android.net.flags.Flags;
38 import com.android.net.module.util.CollectionUtils;
39 
40 import libcore.util.EmptyArray;
41 
42 import java.io.CharArrayWriter;
43 import java.io.PrintWriter;
44 import java.lang.annotation.Retention;
45 import java.lang.annotation.RetentionPolicy;
46 import java.util.Arrays;
47 import java.util.HashSet;
48 import java.util.Iterator;
49 import java.util.List;
50 import java.util.Map;
51 import java.util.Objects;
52 import java.util.function.Function;
53 import java.util.function.Predicate;
54 
55 /**
56  * Collection of active network statistics. Can contain summary details across
57  * all interfaces, or details with per-UID granularity. Internally stores data
58  * as a large table, closely matching {@code /proc/} data format. This structure
59  * optimizes for rapid in-memory comparison, but consider using
60  * {@link NetworkStatsHistory} when persisting.
61  *
62  * @hide
63  */
64 // @NotThreadSafe
65 @SystemApi
66 public final class NetworkStats implements Parcelable, Iterable<NetworkStats.Entry> {
67     private static final String TAG = "NetworkStats";
68 
69     /**
70      * {@link #iface} value when interface details unavailable.
71      * @hide
72      */
73     @Nullable public static final String IFACE_ALL = null;
74 
75     /**
76      * Virtual network interface for video telephony. This is for VT data usage counting
77      * purpose.
78      */
79     public static final String IFACE_VT = "vt_data0";
80 
81     /** {@link #uid} value when UID details unavailable. */
82     public static final int UID_ALL = -1;
83     /** Special UID value for data usage by tethering. */
84     public static final int UID_TETHERING = -5;
85 
86     /**
87      * {@link #tag} value matching any tag.
88      * @hide
89      */
90     // TODO: Rename TAG_ALL to TAG_ANY.
91     public static final int TAG_ALL = -1;
92     /** {@link #set} value for all sets combined, not including debug sets. */
93     public static final int SET_ALL = -1;
94     /** {@link #set} value where background data is accounted. */
95     public static final int SET_DEFAULT = 0;
96     /** {@link #set} value where foreground data is accounted. */
97     public static final int SET_FOREGROUND = 1;
98     /**
99      * All {@link #set} value greater than SET_DEBUG_START are debug {@link #set} values.
100      * @hide
101      */
102     public static final int SET_DEBUG_START = 1000;
103     /**
104      * Debug {@link #set} value when the VPN stats are moved in.
105      * @hide
106      */
107     public static final int SET_DBG_VPN_IN = 1001;
108     /**
109      * Debug {@link #set} value when the VPN stats are moved out of a vpn UID.
110      * @hide
111      */
112     public static final int SET_DBG_VPN_OUT = 1002;
113 
114     /** @hide */
115     @Retention(RetentionPolicy.SOURCE)
116     @IntDef(prefix = { "SET_" }, value = {
117             SET_ALL,
118             SET_DEFAULT,
119             SET_FOREGROUND,
120     })
121     public @interface State {
122     }
123 
124     /**
125      * Include all interfaces when filtering
126      * @hide
127      */
128     public @Nullable static final String[] INTERFACES_ALL = null;
129 
130     /** {@link #tag} value for total data across all tags. */
131     public static final int TAG_NONE = 0;
132 
133     /** {@link #metered} value to account for all metered states. */
134     public static final int METERED_ALL = -1;
135     /** {@link #metered} value where native, unmetered data is accounted. */
136     public static final int METERED_NO = 0;
137     /** {@link #metered} value where metered data is accounted. */
138     public static final int METERED_YES = 1;
139 
140     /** @hide */
141     @Retention(RetentionPolicy.SOURCE)
142     @IntDef(prefix = { "METERED_" }, value = {
143             METERED_ALL,
144             METERED_NO,
145             METERED_YES
146     })
147     public @interface Meteredness {
148     }
149 
150 
151     /** {@link #roaming} value to account for all roaming states. */
152     public static final int ROAMING_ALL = -1;
153     /** {@link #roaming} value where native, non-roaming data is accounted. */
154     public static final int ROAMING_NO = 0;
155     /** {@link #roaming} value where roaming data is accounted. */
156     public static final int ROAMING_YES = 1;
157 
158     /** @hide */
159     @Retention(RetentionPolicy.SOURCE)
160     @IntDef(prefix = { "ROAMING_" }, value = {
161             ROAMING_ALL,
162             ROAMING_NO,
163             ROAMING_YES
164     })
165     public @interface Roaming {
166     }
167 
168     /** {@link #onDefaultNetwork} value to account for all default network states. */
169     public static final int DEFAULT_NETWORK_ALL = -1;
170     /** {@link #onDefaultNetwork} value to account for usage while not the default network. */
171     public static final int DEFAULT_NETWORK_NO = 0;
172     /** {@link #onDefaultNetwork} value to account for usage while the default network. */
173     public static final int DEFAULT_NETWORK_YES = 1;
174 
175     /** @hide */
176     @Retention(RetentionPolicy.SOURCE)
177     @IntDef(prefix = { "DEFAULT_NETWORK_" }, value = {
178             DEFAULT_NETWORK_ALL,
179             DEFAULT_NETWORK_NO,
180             DEFAULT_NETWORK_YES
181     })
182     public @interface DefaultNetwork {
183     }
184 
185     /**
186      * Denotes a request for stats at the interface level.
187      * @hide
188      */
189     public static final int STATS_PER_IFACE = 0;
190     /**
191      * Denotes a request for stats at the interface and UID level.
192      * @hide
193      */
194     public static final int STATS_PER_UID = 1;
195 
196     /** @hide */
197     @Retention(RetentionPolicy.SOURCE)
198     @IntDef(prefix = { "STATS_PER_" }, value = {
199             STATS_PER_IFACE,
200             STATS_PER_UID
201     })
202     public @interface StatsType {
203     }
204 
205     private static final String CLATD_INTERFACE_PREFIX = "v4-";
206     // Delta between IPv4 header (20b) and IPv6 header (40b).
207     // Used for correct stats accounting on clatd interfaces.
208     private static final int IPV4V6_HEADER_DELTA = 20;
209 
210     // TODO: move fields to "mVariable" notation
211 
212     /**
213      * {@link SystemClock#elapsedRealtime()} timestamp in milliseconds when this data was
214      * generated.
215      * It's a timestamps delta when {@link #subtract()},
216      * {@code INetworkStatsSession#getSummaryForAllUid()} methods are used.
217      */
218     private long elapsedRealtime;
219     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
220     private int size;
221     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
222     private int capacity;
223     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
224     private String[] iface;
225     @UnsupportedAppUsage
226     private int[] uid;
227     @UnsupportedAppUsage
228     private int[] set;
229     @UnsupportedAppUsage
230     private int[] tag;
231     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
232     private int[] metered;
233     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
234     private int[] roaming;
235     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
236     private int[] defaultNetwork;
237     @UnsupportedAppUsage
238     private long[] rxBytes;
239     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
240     private long[] rxPackets;
241     @UnsupportedAppUsage
242     private long[] txBytes;
243     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
244     private long[] txPackets;
245     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
246     private long[] operations;
247 
248     /**
249      * Basic element of network statistics. Contains the number of packets and number of bytes
250      * transferred on both directions in a given set of conditions. See
251      * {@link Entry#Entry(String, int, int, int, int, int, int, long, long, long, long, long)}.
252      *
253      * @hide
254      */
255     @SystemApi
256     public static class Entry {
257         /** @hide */
258         @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
259         public String iface;
260         /** @hide */
261         @UnsupportedAppUsage
262         public int uid;
263         /** @hide */
264         @UnsupportedAppUsage
265         public int set;
266         /** @hide */
267         @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
268         public int tag;
269         /**
270          * Note that this is only populated w/ the default value when read from /proc or written
271          * to disk. We merge in the correct value when reporting this value to clients of
272          * getSummary().
273          * @hide
274          */
275         public int metered;
276         /**
277          * Note that this is only populated w/ the default value when read from /proc or written
278          * to disk. We merge in the correct value when reporting this value to clients of
279          * getSummary().
280          * @hide
281          */
282         public int roaming;
283         /**
284          * Note that this is only populated w/ the default value when read from /proc or written
285          * to disk. We merge in the correct value when reporting this value to clients of
286          * getSummary().
287          * @hide
288          */
289         public int defaultNetwork;
290         /** @hide */
291         @UnsupportedAppUsage
292         public long rxBytes;
293         /** @hide */
294         @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
295         public long rxPackets;
296         /** @hide */
297         @UnsupportedAppUsage
298         public long txBytes;
299         /** @hide */
300         @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
301         public long txPackets;
302         /** @hide */
303         @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
304         public long operations;
305 
306         /** @hide */
307         @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Entry()308         public Entry() {
309             this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
310                     DEFAULT_NETWORK_NO, 0L, 0L, 0L, 0L, 0L);
311         }
312 
313         /**
314          * Construct a {@link Entry} object by giving statistics of packet and byte transferred on
315          * both direction, and associated with a set of given conditions.
316          *
317          * @param iface interface name of this {@link Entry}. Or null if not specified.
318          * @param uid uid of this {@link Entry}. {@link #UID_TETHERING} if this {@link Entry} is
319          *            for tethering. Or {@link #UID_ALL} if this {@link NetworkStats} is only
320          *            counting iface stats.
321          * @param set usage state of this {@link Entry}.
322          * @param tag tag of this {@link Entry}.
323          * @param metered metered state of this {@link Entry}.
324          * @param roaming roaming state of this {@link Entry}.
325          * @param defaultNetwork default network status of this {@link Entry}.
326          * @param rxBytes Number of bytes received for this {@link Entry}. Statistics should
327          *                represent the contents of IP packets, including IP headers.
328          * @param rxPackets Number of packets received for this {@link Entry}. Statistics should
329          *                  represent the contents of IP packets, including IP headers.
330          * @param txBytes Number of bytes transmitted for this {@link Entry}. Statistics should
331          *                represent the contents of IP packets, including IP headers.
332          * @param txPackets Number of bytes transmitted for this {@link Entry}. Statistics should
333          *                  represent the contents of IP packets, including IP headers.
334          * @param operations count of network operations performed for this {@link Entry}. This can
335          *                   be used to derive bytes-per-operation.
336          */
Entry(@ullable String iface, int uid, @State int set, int tag, @Meteredness int metered, @Roaming int roaming, @DefaultNetwork int defaultNetwork, long rxBytes, long rxPackets, long txBytes, long txPackets, long operations)337         public Entry(@Nullable String iface, int uid, @State int set, int tag,
338                 @Meteredness int metered, @Roaming int roaming, @DefaultNetwork int defaultNetwork,
339                 long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
340             this.iface = iface;
341             this.uid = uid;
342             this.set = set;
343             this.tag = tag;
344             this.metered = metered;
345             this.roaming = roaming;
346             this.defaultNetwork = defaultNetwork;
347             this.rxBytes = rxBytes;
348             this.rxPackets = rxPackets;
349             this.txBytes = txBytes;
350             this.txPackets = txPackets;
351             this.operations = operations;
352         }
353 
354         /** @hide */
isNegative()355         public boolean isNegative() {
356             return rxBytes < 0 || rxPackets < 0 || txBytes < 0 || txPackets < 0 || operations < 0;
357         }
358 
359         /** @hide */
isEmpty()360         public boolean isEmpty() {
361             return rxBytes == 0 && rxPackets == 0 && txBytes == 0 && txPackets == 0
362                     && operations == 0;
363         }
364 
365         /** @hide */
add(Entry another)366         public void add(Entry another) {
367             this.rxBytes += another.rxBytes;
368             this.rxPackets += another.rxPackets;
369             this.txBytes += another.txBytes;
370             this.txPackets += another.txPackets;
371             this.operations += another.operations;
372         }
373 
374         /**
375          * @return interface name of this entry.
376          * @hide
377          */
getIface()378         @Nullable public String getIface() {
379             return iface;
380         }
381 
382         /**
383          * @return the uid of this entry.
384          */
getUid()385         public int getUid() {
386             return uid;
387         }
388 
389         /**
390          * @return the set state of this entry.
391          */
getSet()392         @State public int getSet() {
393             return set;
394         }
395 
396         /**
397          * @return the tag value of this entry.
398          */
getTag()399         public int getTag() {
400             return tag;
401         }
402 
403         /**
404          * @return the metered state.
405          */
406         @Meteredness
getMetered()407         public int getMetered() {
408             return metered;
409         }
410 
411         /**
412          * @return the roaming state.
413          */
414         @Roaming
getRoaming()415         public int getRoaming() {
416             return roaming;
417         }
418 
419         /**
420          * @return the default network state.
421          */
422         @DefaultNetwork
getDefaultNetwork()423         public int getDefaultNetwork() {
424             return defaultNetwork;
425         }
426 
427         /**
428          * @return the number of received bytes.
429          */
getRxBytes()430         public long getRxBytes() {
431             return rxBytes;
432         }
433 
434         /**
435          * @return the number of received packets.
436          */
getRxPackets()437         public long getRxPackets() {
438             return rxPackets;
439         }
440 
441         /**
442          * @return the number of transmitted bytes.
443          */
getTxBytes()444         public long getTxBytes() {
445             return txBytes;
446         }
447 
448         /**
449          * @return the number of transmitted packets.
450          */
getTxPackets()451         public long getTxPackets() {
452             return txPackets;
453         }
454 
455         /**
456          * @return the count of network operations performed for this entry.
457          */
getOperations()458         public long getOperations() {
459             return operations;
460         }
461 
462         /**
463          * Set Key fields for this entry.
464          *
465          * @return this object.
466          * @hide
467          */
setKeys(@ullable String iface, int uid, @State int set, int tag, @Meteredness int metered, @Roaming int roaming, @DefaultNetwork int defaultNetwork)468         private Entry setKeys(@Nullable String iface, int uid, @State int set,
469                 int tag, @Meteredness int metered, @Roaming int roaming,
470                 @DefaultNetwork int defaultNetwork) {
471             this.iface = iface;
472             this.uid = uid;
473             this.set = set;
474             this.tag = tag;
475             this.metered = metered;
476             this.roaming = roaming;
477             this.defaultNetwork = defaultNetwork;
478             return this;
479         }
480 
481         /**
482          * Set Value fields for this entry.
483          *
484          * @return this object.
485          * @hide
486          */
setValues(long rxBytes, long rxPackets, long txBytes, long txPackets, long operations)487         private Entry setValues(long rxBytes, long rxPackets, long txBytes, long txPackets,
488                 long operations) {
489             this.rxBytes = rxBytes;
490             this.rxPackets = rxPackets;
491             this.txBytes = txBytes;
492             this.txPackets = txPackets;
493             this.operations = operations;
494             return this;
495         }
496 
497         @Override
toString()498         public String toString() {
499             final StringBuilder builder = new StringBuilder();
500             builder.append("iface=").append(iface);
501             builder.append(" uid=").append(uid);
502             builder.append(" set=").append(setToString(set));
503             builder.append(" tag=").append(tagToString(tag));
504             builder.append(" metered=").append(meteredToString(metered));
505             builder.append(" roaming=").append(roamingToString(roaming));
506             builder.append(" defaultNetwork=").append(defaultNetworkToString(defaultNetwork));
507             builder.append(" rxBytes=").append(rxBytes);
508             builder.append(" rxPackets=").append(rxPackets);
509             builder.append(" txBytes=").append(txBytes);
510             builder.append(" txPackets=").append(txPackets);
511             builder.append(" operations=").append(operations);
512             return builder.toString();
513         }
514 
515         /** @hide */
516         @Override
equals(@ullable Object o)517         public boolean equals(@Nullable Object o) {
518             if (o instanceof Entry) {
519                 final Entry e = (Entry) o;
520                 return uid == e.uid && set == e.set && tag == e.tag && metered == e.metered
521                         && roaming == e.roaming && defaultNetwork == e.defaultNetwork
522                         && rxBytes == e.rxBytes && rxPackets == e.rxPackets
523                         && txBytes == e.txBytes && txPackets == e.txPackets
524                         && operations == e.operations && TextUtils.equals(iface, e.iface);
525             }
526             return false;
527         }
528 
529         /** @hide */
530         @Override
hashCode()531         public int hashCode() {
532             return Objects.hash(uid, set, tag, metered, roaming, defaultNetwork, iface);
533         }
534     }
535 
NetworkStats(long elapsedRealtime, int initialSize)536     public NetworkStats(long elapsedRealtime, int initialSize) {
537         this.elapsedRealtime = elapsedRealtime;
538         this.size = 0;
539         if (initialSize > 0) {
540             this.capacity = initialSize;
541             this.iface = new String[initialSize];
542             this.uid = new int[initialSize];
543             this.set = new int[initialSize];
544             this.tag = new int[initialSize];
545             this.metered = new int[initialSize];
546             this.roaming = new int[initialSize];
547             this.defaultNetwork = new int[initialSize];
548             this.rxBytes = new long[initialSize];
549             this.rxPackets = new long[initialSize];
550             this.txBytes = new long[initialSize];
551             this.txPackets = new long[initialSize];
552             this.operations = new long[initialSize];
553         } else {
554             // Special case for use by NetworkStatsFactory to start out *really* empty.
555             clear();
556         }
557     }
558 
559     /** @hide */
560     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
NetworkStats(Parcel parcel)561     public NetworkStats(Parcel parcel) {
562         elapsedRealtime = parcel.readLong();
563         size = parcel.readInt();
564         capacity = parcel.readInt();
565         iface = parcel.createStringArray();
566         uid = parcel.createIntArray();
567         set = parcel.createIntArray();
568         tag = parcel.createIntArray();
569         metered = parcel.createIntArray();
570         roaming = parcel.createIntArray();
571         defaultNetwork = parcel.createIntArray();
572         rxBytes = parcel.createLongArray();
573         rxPackets = parcel.createLongArray();
574         txBytes = parcel.createLongArray();
575         txPackets = parcel.createLongArray();
576         operations = parcel.createLongArray();
577     }
578 
579     @Override
writeToParcel(@onNull Parcel dest, int flags)580     public void writeToParcel(@NonNull Parcel dest, int flags) {
581         dest.writeLong(elapsedRealtime);
582         dest.writeInt(size);
583         dest.writeInt(capacity);
584         dest.writeStringArray(iface);
585         dest.writeIntArray(uid);
586         dest.writeIntArray(set);
587         dest.writeIntArray(tag);
588         dest.writeIntArray(metered);
589         dest.writeIntArray(roaming);
590         dest.writeIntArray(defaultNetwork);
591         dest.writeLongArray(rxBytes);
592         dest.writeLongArray(rxPackets);
593         dest.writeLongArray(txBytes);
594         dest.writeLongArray(txPackets);
595         dest.writeLongArray(operations);
596     }
597 
598     /**
599      * @hide
600      */
601     @Override
clone()602     public NetworkStats clone() {
603         final NetworkStats clone = new NetworkStats(elapsedRealtime, size);
604         NetworkStats.Entry entry = null;
605         for (int i = 0; i < size; i++) {
606             entry = getValues(i, entry);
607             clone.insertEntry(entry);
608         }
609         return clone;
610     }
611 
612     /**
613      * Clear all data stored in this object.
614      * @hide
615      */
clear()616     public void clear() {
617         this.capacity = 0;
618         this.iface = EmptyArray.STRING;
619         this.uid = EmptyArray.INT;
620         this.set = EmptyArray.INT;
621         this.tag = EmptyArray.INT;
622         this.metered = EmptyArray.INT;
623         this.roaming = EmptyArray.INT;
624         this.defaultNetwork = EmptyArray.INT;
625         this.rxBytes = EmptyArray.LONG;
626         this.rxPackets = EmptyArray.LONG;
627         this.txBytes = EmptyArray.LONG;
628         this.txPackets = EmptyArray.LONG;
629         this.operations = EmptyArray.LONG;
630     }
631 
632     /** @hide */
633     @VisibleForTesting
insertEntry( String iface, long rxBytes, long rxPackets, long txBytes, long txPackets)634     public NetworkStats insertEntry(
635             String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {
636         return insertEntry(
637                 iface, UID_ALL, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO,
638                 rxBytes, rxPackets, txBytes, txPackets, 0L);
639     }
640 
641     /** @hide */
642     @VisibleForTesting
insertEntry(String iface, int uid, int set, int tag, long rxBytes, long rxPackets, long txBytes, long txPackets, long operations)643     public NetworkStats insertEntry(String iface, int uid, int set, int tag, long rxBytes,
644             long rxPackets, long txBytes, long txPackets, long operations) {
645         return insertEntry(new Entry(
646                 iface, uid, set, tag,  METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO,
647                 rxBytes, rxPackets, txBytes, txPackets, operations));
648     }
649 
650     /** @hide */
651     @VisibleForTesting
insertEntry(String iface, int uid, int set, int tag, int metered, int roaming, int defaultNetwork, long rxBytes, long rxPackets, long txBytes, long txPackets, long operations)652     public NetworkStats insertEntry(String iface, int uid, int set, int tag, int metered,
653             int roaming, int defaultNetwork, long rxBytes, long rxPackets, long txBytes,
654             long txPackets, long operations) {
655         return insertEntry(new Entry(
656                 iface, uid, set, tag, metered, roaming, defaultNetwork, rxBytes, rxPackets,
657                 txBytes, txPackets, operations));
658     }
659 
660     /**
661      * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
662      * object can be recycled across multiple calls.
663      * @hide
664      */
insertEntry(Entry entry)665     public NetworkStats insertEntry(Entry entry) {
666         if (size >= capacity) {
667             final int newLength = Math.max(size, 10) * 3 / 2;
668             iface = Arrays.copyOf(iface, newLength);
669             uid = Arrays.copyOf(uid, newLength);
670             set = Arrays.copyOf(set, newLength);
671             tag = Arrays.copyOf(tag, newLength);
672             metered = Arrays.copyOf(metered, newLength);
673             roaming = Arrays.copyOf(roaming, newLength);
674             defaultNetwork = Arrays.copyOf(defaultNetwork, newLength);
675             rxBytes = Arrays.copyOf(rxBytes, newLength);
676             rxPackets = Arrays.copyOf(rxPackets, newLength);
677             txBytes = Arrays.copyOf(txBytes, newLength);
678             txPackets = Arrays.copyOf(txPackets, newLength);
679             operations = Arrays.copyOf(operations, newLength);
680             capacity = newLength;
681         }
682 
683         setValues(size, entry);
684         size++;
685 
686         return this;
687     }
688 
setValues(int i, Entry entry)689     private void setValues(int i, Entry entry) {
690         iface[i] = entry.iface;
691         uid[i] = entry.uid;
692         set[i] = entry.set;
693         tag[i] = entry.tag;
694         metered[i] = entry.metered;
695         roaming[i] = entry.roaming;
696         defaultNetwork[i] = entry.defaultNetwork;
697         rxBytes[i] = entry.rxBytes;
698         rxPackets[i] = entry.rxPackets;
699         txBytes[i] = entry.txBytes;
700         txPackets[i] = entry.txPackets;
701         operations[i] = entry.operations;
702     }
703 
704     /**
705      * Iterate over Entry objects.
706      *
707      * Return an iterator of this object that will iterate through all contained Entry objects.
708      *
709      * This iterator does not support concurrent modification and makes no guarantee of fail-fast
710      * behavior. If any method that can mutate the contents of this object is called while
711      * iteration is in progress, either inside the loop or in another thread, then behavior is
712      * undefined.
713      * The remove() method is not implemented and will throw UnsupportedOperationException.
714      * @hide
715      */
716     @SystemApi
iterator()717     @NonNull public Iterator<Entry> iterator() {
718         return new Iterator<Entry>() {
719             int mIndex = 0;
720 
721             @Override
722             public boolean hasNext() {
723                 return mIndex < size;
724             }
725 
726             @Override
727             public Entry next() {
728                 return getValues(mIndex++, null);
729             }
730         };
731     }
732 
733     /**
734      * Return specific stats entry.
735      * @hide
736      */
737     @UnsupportedAppUsage
getValues(int i, @Nullable Entry recycle)738     public Entry getValues(int i, @Nullable Entry recycle) {
739         final Entry entry = recycle != null ? recycle : new Entry();
740         entry.iface = iface[i];
741         entry.uid = uid[i];
742         entry.set = set[i];
743         entry.tag = tag[i];
744         entry.metered = metered[i];
745         entry.roaming = roaming[i];
746         entry.defaultNetwork = defaultNetwork[i];
747         entry.rxBytes = rxBytes[i];
748         entry.rxPackets = rxPackets[i];
749         entry.txBytes = txBytes[i];
750         entry.txPackets = txPackets[i];
751         entry.operations = operations[i];
752         return entry;
753     }
754 
755     /**
756      * If @{code dest} is not equal to @{code src}, copy entry from index @{code src} to index
757      * @{code dest}.
758      */
maybeCopyEntry(int dest, int src)759     private void maybeCopyEntry(int dest, int src) {
760         if (dest == src) return;
761         iface[dest] = iface[src];
762         uid[dest] = uid[src];
763         set[dest] = set[src];
764         tag[dest] = tag[src];
765         metered[dest] = metered[src];
766         roaming[dest] = roaming[src];
767         defaultNetwork[dest] = defaultNetwork[src];
768         rxBytes[dest] = rxBytes[src];
769         rxPackets[dest] = rxPackets[src];
770         txBytes[dest] = txBytes[src];
771         txPackets[dest] = txPackets[src];
772         operations[dest] = operations[src];
773     }
774 
775     /** @hide */
getElapsedRealtime()776     public long getElapsedRealtime() {
777         return elapsedRealtime;
778     }
779 
780     /** @hide */
setElapsedRealtime(long time)781     public void setElapsedRealtime(long time) {
782         elapsedRealtime = time;
783     }
784 
785     /**
786      * Return age of this {@link NetworkStats} object with respect to
787      * {@link SystemClock#elapsedRealtime()}.
788      * @hide
789      */
getElapsedRealtimeAge()790     public long getElapsedRealtimeAge() {
791         return SystemClock.elapsedRealtime() - elapsedRealtime;
792     }
793 
794     /** @hide */
795     @UnsupportedAppUsage
size()796     public int size() {
797         return size;
798     }
799 
800     /** @hide */
801     @VisibleForTesting
internalSize()802     public int internalSize() {
803         return capacity;
804     }
805 
806     /** @hide */
807     @Deprecated
combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets, long txBytes, long txPackets, long operations)808     public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
809             long txBytes, long txPackets, long operations) {
810         return combineValues(
811                 iface, uid, SET_DEFAULT, tag, rxBytes, rxPackets, txBytes,
812                 txPackets, operations);
813     }
814 
815     /** @hide */
combineValues(String iface, int uid, int set, int tag, long rxBytes, long rxPackets, long txBytes, long txPackets, long operations)816     public NetworkStats combineValues(String iface, int uid, int set, int tag,
817             long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
818         return combineValues(new Entry(
819                 iface, uid, set, tag, METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO,
820                 rxBytes, rxPackets, txBytes, txPackets, operations));
821     }
822 
823     /**
824      * Combine given values with an existing row, or create a new row if
825      * {@link #findIndex(String, int, int, int, int, int, int)} is unable to find match. Can
826      * also be used to subtract values from existing rows. This method mutates the referencing
827      * {@link NetworkStats} object.
828      *
829      * @param entry the {@link Entry} to combine.
830      * @return a reference to this mutated {@link NetworkStats} object.
831      * @hide
832      */
combineValues(@onNull Entry entry)833     public @NonNull NetworkStats combineValues(@NonNull Entry entry) {
834         final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag, entry.metered,
835                 entry.roaming, entry.defaultNetwork);
836         if (i == -1) {
837             // only create new entry when positive contribution
838             insertEntry(entry);
839         } else {
840             rxBytes[i] += entry.rxBytes;
841             rxPackets[i] += entry.rxPackets;
842             txBytes[i] += entry.txBytes;
843             txPackets[i] += entry.txPackets;
844             operations[i] += entry.operations;
845         }
846         return this;
847     }
848 
849     /**
850      * Adds multiple entries to a copy of this NetworkStats instance.
851      *
852      * @param entries The entries to add.
853      * @return A new NetworkStats instance with the added entries.
854      */
855     @FlaggedApi(Flags.FLAG_NETSTATS_ADD_ENTRIES)
addEntries(@onNull final List<Entry> entries)856     public @NonNull NetworkStats addEntries(@NonNull final List<Entry> entries) {
857         final NetworkStats newStats = this.clone();
858         for (final Entry entry : Objects.requireNonNull(entries)) {
859             newStats.combineValues(entry);
860         }
861         return newStats;
862     }
863 
864     /**
865      * Add given values with an existing row, or create a new row if
866      * {@link #findIndex(String, int, int, int, int, int, int)} is unable to find match. Can
867      * also be used to subtract values from existing rows.
868      *
869      * @param entry the {@link Entry} to add.
870      * @return a new constructed {@link NetworkStats} object that contains the result.
871      */
addEntry(@onNull Entry entry)872     public @NonNull NetworkStats addEntry(@NonNull Entry entry) {
873         return this.clone().combineValues(entry);
874     }
875 
876     /**
877      * Add the given {@link NetworkStats} objects.
878      *
879      * @return the sum of two objects.
880      */
add(@onNull NetworkStats another)881     public @NonNull NetworkStats add(@NonNull NetworkStats another) {
882         final NetworkStats ret = this.clone();
883         ret.combineAllValues(another);
884         return ret;
885     }
886 
887     /**
888      * Combine all values from another {@link NetworkStats} into this object.
889      * @hide
890      */
combineAllValues(@onNull NetworkStats another)891     public void combineAllValues(@NonNull NetworkStats another) {
892         NetworkStats.Entry entry = null;
893         for (int i = 0; i < another.size; i++) {
894             entry = another.getValues(i, entry);
895             combineValues(entry);
896         }
897     }
898 
899     /**
900      * Find first stats index that matches the requested parameters.
901      * @hide
902      */
findIndex(String iface, int uid, int set, int tag, int metered, int roaming, int defaultNetwork)903     public int findIndex(String iface, int uid, int set, int tag, int metered, int roaming,
904             int defaultNetwork) {
905         for (int i = 0; i < size; i++) {
906             if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
907                     && metered == this.metered[i] && roaming == this.roaming[i]
908                     && defaultNetwork == this.defaultNetwork[i]
909                     && Objects.equals(iface, this.iface[i])) {
910                 return i;
911             }
912         }
913         return -1;
914     }
915 
916     /**
917      * Find first stats index that matches the requested parameters, starting
918      * search around the hinted index as an optimization.
919      * @hide
920      */
921     @VisibleForTesting
findIndexHinted(String iface, int uid, int set, int tag, int metered, int roaming, int defaultNetwork, int hintIndex)922     public int findIndexHinted(String iface, int uid, int set, int tag, int metered, int roaming,
923             int defaultNetwork, int hintIndex) {
924         for (int offset = 0; offset < size; offset++) {
925             final int halfOffset = offset / 2;
926 
927             // search outwards from hint index, alternating forward and backward
928             final int i;
929             if (offset % 2 == 0) {
930                 i = (hintIndex + halfOffset) % size;
931             } else {
932                 i = (size + hintIndex - halfOffset - 1) % size;
933             }
934 
935             if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
936                     && metered == this.metered[i] && roaming == this.roaming[i]
937                     && defaultNetwork == this.defaultNetwork[i]
938                     && Objects.equals(iface, this.iface[i])) {
939                 return i;
940             }
941         }
942         return -1;
943     }
944 
945     /**
946      * Splice in {@link #operations} from the given {@link NetworkStats} based
947      * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface},
948      * since operation counts are at data layer.
949      * @hide
950      */
spliceOperationsFrom(NetworkStats stats)951     public void spliceOperationsFrom(NetworkStats stats) {
952         for (int i = 0; i < size; i++) {
953             final int j = stats.findIndex(iface[i], uid[i], set[i], tag[i], metered[i], roaming[i],
954                     defaultNetwork[i]);
955             if (j == -1) {
956                 operations[i] = 0;
957             } else {
958                 operations[i] = stats.operations[j];
959             }
960         }
961     }
962 
963     /**
964      * Return list of unique interfaces known by this data structure.
965      * @hide
966      */
getUniqueIfaces()967     public String[] getUniqueIfaces() {
968         final HashSet<String> ifaces = new HashSet<String>();
969         for (String iface : this.iface) {
970             if (iface != IFACE_ALL) {
971                 ifaces.add(iface);
972             }
973         }
974         return ifaces.toArray(new String[ifaces.size()]);
975     }
976 
977     /**
978      * Return list of unique UIDs known by this data structure.
979      * @hide
980      */
981     @UnsupportedAppUsage
getUniqueUids()982     public int[] getUniqueUids() {
983         final SparseBooleanArray uids = new SparseBooleanArray();
984         for (int uid : this.uid) {
985             uids.put(uid, true);
986         }
987 
988         final int size = uids.size();
989         final int[] result = new int[size];
990         for (int i = 0; i < size; i++) {
991             result[i] = uids.keyAt(i);
992         }
993         return result;
994     }
995 
996     /**
997      * Return total bytes represented by this snapshot object, usually used when
998      * checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
999      * @hide
1000      */
1001     @UnsupportedAppUsage
getTotalBytes()1002     public long getTotalBytes() {
1003         final Entry entry = getTotal(null);
1004         return entry.rxBytes + entry.txBytes;
1005     }
1006 
1007     /**
1008      * Return total of all fields represented by this snapshot object.
1009      * @hide
1010      */
1011     @UnsupportedAppUsage
getTotal(Entry recycle)1012     public Entry getTotal(Entry recycle) {
1013         return getTotal(recycle, null, UID_ALL, false);
1014     }
1015 
1016     /**
1017      * Return total of all fields represented by this snapshot object matching
1018      * the requested {@link #uid}.
1019      * @hide
1020      */
1021     @UnsupportedAppUsage
getTotal(Entry recycle, int limitUid)1022     public Entry getTotal(Entry recycle, int limitUid) {
1023         return getTotal(recycle, null, limitUid, false);
1024     }
1025 
1026     /**
1027      * Return total of all fields represented by this snapshot object matching
1028      * the requested {@link #iface}.
1029      * @hide
1030      */
getTotal(Entry recycle, HashSet<String> limitIface)1031     public Entry getTotal(Entry recycle, HashSet<String> limitIface) {
1032         return getTotal(recycle, limitIface, UID_ALL, false);
1033     }
1034 
1035     /** @hide */
1036     @UnsupportedAppUsage
getTotalIncludingTags(Entry recycle)1037     public Entry getTotalIncludingTags(Entry recycle) {
1038         return getTotal(recycle, null, UID_ALL, true);
1039     }
1040 
1041     /**
1042      * Return total of all fields represented by this snapshot object matching
1043      * the requested {@link #iface} and {@link #uid}.
1044      *
1045      * @param limitIface Set of {@link #iface} to include in total; or {@code
1046      *            null} to include all ifaces.
1047      */
getTotal( Entry recycle, HashSet<String> limitIface, int limitUid, boolean includeTags)1048     private Entry getTotal(
1049             Entry recycle, HashSet<String> limitIface, int limitUid, boolean includeTags) {
1050         final Entry entry = recycle != null ? recycle : new Entry();
1051 
1052         entry.iface = IFACE_ALL;
1053         entry.uid = limitUid;
1054         entry.set = SET_ALL;
1055         entry.tag = TAG_NONE;
1056         entry.metered = METERED_ALL;
1057         entry.roaming = ROAMING_ALL;
1058         entry.defaultNetwork = DEFAULT_NETWORK_ALL;
1059         entry.rxBytes = 0;
1060         entry.rxPackets = 0;
1061         entry.txBytes = 0;
1062         entry.txPackets = 0;
1063         entry.operations = 0;
1064 
1065         for (int i = 0; i < size; i++) {
1066             final boolean matchesUid = (limitUid == UID_ALL) || (limitUid == uid[i]);
1067             final boolean matchesIface = (limitIface == null) || (limitIface.contains(iface[i]));
1068 
1069             if (matchesUid && matchesIface) {
1070                 // skip specific tags, since already counted in TAG_NONE
1071                 if (tag[i] != TAG_NONE && !includeTags) continue;
1072 
1073                 entry.rxBytes += rxBytes[i];
1074                 entry.rxPackets += rxPackets[i];
1075                 entry.txBytes += txBytes[i];
1076                 entry.txPackets += txPackets[i];
1077                 entry.operations += operations[i];
1078             }
1079         }
1080         return entry;
1081     }
1082 
1083     /**
1084      * Fast path for battery stats.
1085      * @hide
1086      */
getTotalPackets()1087     public long getTotalPackets() {
1088         long total = 0;
1089         for (int i = size - 1; i >= 0; i--) {
1090             total += rxPackets[i] + txPackets[i];
1091         }
1092         return total;
1093     }
1094 
1095     /**
1096      * Subtract the given {@link NetworkStats}, effectively leaving the delta
1097      * between two snapshots in time. Assumes that statistics rows collect over
1098      * time, and that none of them have disappeared. This method does not mutate
1099      * the referencing object.
1100      *
1101      * @return the delta between two objects.
1102      */
subtract(@onNull NetworkStats right)1103     public @NonNull NetworkStats subtract(@NonNull NetworkStats right) {
1104         return subtract(this, right, null, null);
1105     }
1106 
1107     /**
1108      * Subtract the two given {@link NetworkStats} objects, returning the delta
1109      * between two snapshots in time. Assumes that statistics rows collect over
1110      * time, and that none of them have disappeared.
1111      * <p>
1112      * If counters have rolled backwards, they are clamped to {@code 0} and
1113      * reported to the given {@link NonMonotonicObserver}.
1114      * @hide
1115      */
subtract(NetworkStats left, NetworkStats right, NonMonotonicObserver<C> observer, C cookie)1116     public static <C> NetworkStats subtract(NetworkStats left, NetworkStats right,
1117             NonMonotonicObserver<C> observer, C cookie) {
1118         return subtract(left, right, observer, cookie, null);
1119     }
1120 
1121     /**
1122      * Subtract the two given {@link NetworkStats} objects, returning the delta
1123      * between two snapshots in time. Assumes that statistics rows collect over
1124      * time, and that none of them have disappeared.
1125      * <p>
1126      * If counters have rolled backwards, they are clamped to {@code 0} and
1127      * reported to the given {@link NonMonotonicObserver}.
1128      * <p>
1129      * If <var>recycle</var> is supplied, this NetworkStats object will be
1130      * reused (and returned) as the result if it is large enough to contain
1131      * the data.
1132      * @hide
1133      */
subtract(NetworkStats left, NetworkStats right, NonMonotonicObserver<C> observer, C cookie, NetworkStats recycle)1134     public static <C> NetworkStats subtract(NetworkStats left, NetworkStats right,
1135             NonMonotonicObserver<C> observer, C cookie, NetworkStats recycle) {
1136         long deltaRealtime = left.elapsedRealtime - right.elapsedRealtime;
1137         if (deltaRealtime < 0) {
1138             if (observer != null) {
1139                 observer.foundNonMonotonic(left, -1, right, -1, cookie);
1140             }
1141             deltaRealtime = 0;
1142         }
1143 
1144         // result will have our rows, and elapsed time between snapshots
1145         final Entry entry = new Entry();
1146         final NetworkStats result;
1147         if (recycle != null && recycle.capacity >= left.size) {
1148             result = recycle;
1149             result.size = 0;
1150             result.elapsedRealtime = deltaRealtime;
1151         } else {
1152             result = new NetworkStats(deltaRealtime, left.size);
1153         }
1154         for (int i = 0; i < left.size; i++) {
1155             entry.iface = left.iface[i];
1156             entry.uid = left.uid[i];
1157             entry.set = left.set[i];
1158             entry.tag = left.tag[i];
1159             entry.metered = left.metered[i];
1160             entry.roaming = left.roaming[i];
1161             entry.defaultNetwork = left.defaultNetwork[i];
1162             entry.rxBytes = left.rxBytes[i];
1163             entry.rxPackets = left.rxPackets[i];
1164             entry.txBytes = left.txBytes[i];
1165             entry.txPackets = left.txPackets[i];
1166             entry.operations = left.operations[i];
1167 
1168             // Find the remote row that matches and subtract.
1169             // The returned row must be uniquely matched.
1170             final int j = right.findIndexHinted(entry.iface, entry.uid, entry.set, entry.tag,
1171                     entry.metered, entry.roaming, entry.defaultNetwork, i);
1172             if (j != -1) {
1173                 // Found matching row, subtract remote value.
1174                 entry.rxBytes -= right.rxBytes[j];
1175                 entry.rxPackets -= right.rxPackets[j];
1176                 entry.txBytes -= right.txBytes[j];
1177                 entry.txPackets -= right.txPackets[j];
1178                 entry.operations -= right.operations[j];
1179             }
1180 
1181             if (entry.isNegative()) {
1182                 if (observer != null) {
1183                     observer.foundNonMonotonic(left, i, right, j, cookie);
1184                 }
1185                 entry.rxBytes = Math.max(entry.rxBytes, 0);
1186                 entry.rxPackets = Math.max(entry.rxPackets, 0);
1187                 entry.txBytes = Math.max(entry.txBytes, 0);
1188                 entry.txPackets = Math.max(entry.txPackets, 0);
1189                 entry.operations = Math.max(entry.operations, 0);
1190             }
1191 
1192             result.insertEntry(entry);
1193         }
1194 
1195         return result;
1196     }
1197 
1198     /**
1199      * Calculate and apply adjustments to captured statistics for 464xlat traffic.
1200      *
1201      * <p>This mutates stacked traffic stats, to account for IPv4/IPv6 header size difference.
1202      *
1203      * <p>UID stats, which are only accounted on the stacked interface, need to be increased
1204      * by 20 bytes/packet to account for translation overhead.
1205      *
1206      * <p>The potential additional overhead of 8 bytes/packet for ip fragments is ignored.
1207      *
1208      * <p>Interface stats need to sum traffic on both stacked and base interface because:
1209      *   - eBPF offloaded packets appear only on the stacked interface
1210      *   - Non-offloaded ingress packets appear only on the stacked interface
1211      *     (due to iptables raw PREROUTING drop rules)
1212      *   - Non-offloaded egress packets appear only on the stacked interface
1213      *     (due to ignoring traffic from clat daemon by uid match)
1214      * (and of course the 20 bytes/packet overhead needs to be applied to stacked interface stats)
1215      *
1216      * <p>This method will behave fine if {@code stackedIfaces} is an non-synchronized but add-only
1217      * {@code ConcurrentHashMap}
1218      * @param baseTraffic Traffic on the base interfaces. Will be mutated.
1219      * @param stackedTraffic Stats with traffic stacked on top of our ifaces. Will also be mutated.
1220      * @param stackedIfaces Mapping ipv6if -> ipv4if interface where traffic is counted on both.
1221      * @hide
1222      */
apply464xlatAdjustments(NetworkStats baseTraffic, NetworkStats stackedTraffic, Map<String, String> stackedIfaces)1223     public static void apply464xlatAdjustments(NetworkStats baseTraffic,
1224             NetworkStats stackedTraffic, Map<String, String> stackedIfaces) {
1225         // For recycling
1226         Entry entry = null;
1227         for (int i = 0; i < stackedTraffic.size; i++) {
1228             entry = stackedTraffic.getValues(i, entry);
1229             if (entry == null) continue;
1230             if (entry.iface == null) continue;
1231             if (!entry.iface.startsWith(CLATD_INTERFACE_PREFIX)) continue;
1232 
1233             // For 464xlat traffic, per uid stats only counts the bytes of the native IPv4 packet
1234             // sent on the stacked interface with prefix "v4-" and drops the IPv6 header size after
1235             // unwrapping. To account correctly for on-the-wire traffic, add the 20 additional bytes
1236             // difference for all packets (http://b/12249687, http:/b/33681750).
1237             //
1238             // Note: this doesn't account for LRO/GRO/GSO/TSO (ie. >mtu) traffic correctly, nor
1239             // does it correctly account for the 8 extra bytes in the IPv6 fragmentation header.
1240             //
1241             // While the ebpf code path does try to simulate proper post segmentation packet
1242             // counts, we have nothing of the sort of xt_qtaguid stats.
1243             entry.rxBytes += entry.rxPackets * IPV4V6_HEADER_DELTA;
1244             entry.txBytes += entry.txPackets * IPV4V6_HEADER_DELTA;
1245             stackedTraffic.setValues(i, entry);
1246         }
1247     }
1248 
1249     /**
1250      * Calculate and apply adjustments to captured statistics for 464xlat traffic counted twice.
1251      *
1252      * <p>This mutates the object this method is called on. Equivalent to calling
1253      * {@link #apply464xlatAdjustments(NetworkStats, NetworkStats, Map)} with {@code this} as
1254      * base and stacked traffic.
1255      * @param stackedIfaces Mapping ipv6if -> ipv4if interface where traffic is counted on both.
1256      * @hide
1257      */
apply464xlatAdjustments(Map<String, String> stackedIfaces)1258     public void apply464xlatAdjustments(Map<String, String> stackedIfaces) {
1259         apply464xlatAdjustments(this, this, stackedIfaces);
1260     }
1261 
1262     /**
1263      * Return total statistics grouped by {@link #iface}; doesn't mutate the
1264      * original structure.
1265      * @hide
1266      * @deprecated Use {@link #mapKeysNotNull(Function)} instead.
1267      */
1268     @Deprecated
groupedByIface()1269     public NetworkStats groupedByIface() {
1270         if (SdkLevel.isAtLeastV()) {
1271             throw new UnsupportedOperationException("groupedByIface is not supported");
1272         }
1273         // Keep backward compatibility where the method filtered out tagged stats and keep the
1274         // operation counts as 0. The method used to deal with uid snapshot where tagged and
1275         // non-tagged stats were mixed. And this method was also in Android O API list,
1276         // so it is possible OEM can access it.
1277         final Entry temp = new Entry();
1278         final NetworkStats mappedStats = this.mapKeysNotNull(entry -> entry.getTag() != TAG_NONE
1279                 ? null : temp.setKeys(entry.getIface(), UID_ALL,
1280                 SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL));
1281 
1282         for (int i = 0; i < mappedStats.size; i++) {
1283             mappedStats.operations[i] = 0L;
1284         }
1285         return mappedStats;
1286     }
1287 
1288     /**
1289      * Return total statistics grouped by {@link #uid}; doesn't mutate the
1290      * original structure.
1291      * @hide
1292      * @deprecated Use {@link #mapKeysNotNull(Function)} instead.
1293      */
1294     @Deprecated
groupedByUid()1295     public NetworkStats groupedByUid() {
1296         if (SdkLevel.isAtLeastV()) {
1297             throw new UnsupportedOperationException("groupedByUid is not supported");
1298         }
1299         // Keep backward compatibility where the method filtered out tagged stats. The method used
1300         // to deal with uid snapshot where tagged and non-tagged stats were mixed. And
1301         // this method is also in Android O API list, so it is possible OEM can access it.
1302         final Entry temp = new Entry();
1303         return this.mapKeysNotNull(entry ->  entry.getTag() != TAG_NONE
1304                 ? null : temp.setKeys(IFACE_ALL, entry.getUid(),
1305                 SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL));
1306     }
1307 
1308     /**
1309      * Remove all rows that match one of specified UIDs.
1310      * This mutates the original structure in place.
1311      * @hide
1312      */
removeUids(int[] uids)1313     public void removeUids(int[] uids) {
1314         filter(e -> !CollectionUtils.contains(uids, e.uid));
1315     }
1316 
1317     /**
1318      * Remove all rows that match one of specified UIDs.
1319      * @return the result object.
1320      * @hide
1321      */
1322     @NonNull
removeEmptyEntries()1323     public NetworkStats removeEmptyEntries() {
1324         final NetworkStats ret = this.clone();
1325         ret.filter(e -> e.rxBytes != 0 || e.rxPackets != 0 || e.txBytes != 0 || e.txPackets != 0
1326                 || e.operations != 0);
1327         return ret;
1328     }
1329 
1330     /**
1331      * Returns a copy of this NetworkStats, replacing iface with IFACE_ALL in all entries.
1332      *
1333      * @hide
1334      */
1335     @NonNull
withoutInterfaces()1336     public NetworkStats withoutInterfaces() {
1337         final Entry temp = new Entry();
1338         return mapKeysNotNull(entry -> temp.setKeys(IFACE_ALL, entry.getUid(), entry.getSet(),
1339                 entry.getTag(), entry.getMetered(), entry.getRoaming(), entry.getDefaultNetwork()));
1340     }
1341 
1342     /**
1343      * Returns a new NetworkStats object where entries are transformed.
1344      *
1345      * Note that because NetworkStats is more akin to a map than to a list,
1346      * the entries will be grouped after they are mapped by the key fields,
1347      * e.g. uid, set, tag, defaultNetwork.
1348      * Only the key returned by the function is used ; values will be forcefully
1349      * copied from the original entry. Entries that map to the same set of keys
1350      * will be added together.
1351      */
1352     @NonNull
mapKeysNotNull(@onNull Function<Entry, Entry> f)1353     private NetworkStats mapKeysNotNull(@NonNull Function<Entry, Entry> f) {
1354         final NetworkStats ret = new NetworkStats(0, 1);
1355         for (Entry e : this) {
1356             final NetworkStats.Entry transformed = f.apply(e);
1357             if (transformed == null) continue;
1358             if (transformed == e) {
1359                 throw new IllegalStateException("A new entry must be created.");
1360             }
1361             transformed.setValues(e.getRxBytes(), e.getRxPackets(), e.getTxBytes(),
1362                     e.getTxPackets(), e.getOperations());
1363             ret.combineValues(transformed);
1364         }
1365         return ret;
1366     }
1367 
1368     /**
1369      * Only keep entries that match all specified filters.
1370      *
1371      * <p>This mutates the original structure in place. After this method is called,
1372      * size is the number of matching entries, and capacity is the previous capacity.
1373      * @param limitUid UID to filter for, or {@link #UID_ALL}.
1374      * @param limitIfaces Interfaces to filter for, or {@link #INTERFACES_ALL}.
1375      * @param limitTag Tag to filter for, or {@link #TAG_ALL}.
1376      * @hide
1377      */
filter(int limitUid, String[] limitIfaces, int limitTag)1378     public void filter(int limitUid, String[] limitIfaces, int limitTag) {
1379         if (limitUid == UID_ALL && limitTag == TAG_ALL && limitIfaces == INTERFACES_ALL) {
1380             return;
1381         }
1382         filter(e -> (limitUid == UID_ALL || limitUid == e.uid)
1383                 && (limitTag == TAG_ALL || limitTag == e.tag)
1384                 && (limitIfaces == INTERFACES_ALL
1385                     || CollectionUtils.contains(limitIfaces, e.iface)));
1386     }
1387 
1388     /**
1389      * Only keep entries with {@link #set} value less than {@link #SET_DEBUG_START}.
1390      *
1391      * <p>This mutates the original structure in place.
1392      * @hide
1393      */
filterDebugEntries()1394     public void filterDebugEntries() {
1395         filter(e -> e.set < SET_DEBUG_START);
1396     }
1397 
filter(Predicate<Entry> predicate)1398     private void filter(Predicate<Entry> predicate) {
1399         Entry entry = new Entry();
1400         int nextOutputEntry = 0;
1401         for (int i = 0; i < size; i++) {
1402             entry = getValues(i, entry);
1403             if (predicate.test(entry)) {
1404                 if (nextOutputEntry != i) {
1405                     setValues(nextOutputEntry, entry);
1406                 }
1407                 nextOutputEntry++;
1408             }
1409         }
1410         size = nextOutputEntry;
1411     }
1412 
1413     /** @hide */
dump(String prefix, PrintWriter pw)1414     public void dump(String prefix, PrintWriter pw) {
1415         pw.print(prefix);
1416         pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
1417         for (int i = 0; i < size; i++) {
1418             pw.print(prefix);
1419             pw.print("  ["); pw.print(i); pw.print("]");
1420             pw.print(" iface="); pw.print(iface[i]);
1421             pw.print(" uid="); pw.print(uid[i]);
1422             pw.print(" set="); pw.print(setToString(set[i]));
1423             pw.print(" tag="); pw.print(tagToString(tag[i]));
1424             pw.print(" metered="); pw.print(meteredToString(metered[i]));
1425             pw.print(" roaming="); pw.print(roamingToString(roaming[i]));
1426             pw.print(" defaultNetwork="); pw.print(defaultNetworkToString(defaultNetwork[i]));
1427             pw.print(" rxBytes="); pw.print(rxBytes[i]);
1428             pw.print(" rxPackets="); pw.print(rxPackets[i]);
1429             pw.print(" txBytes="); pw.print(txBytes[i]);
1430             pw.print(" txPackets="); pw.print(txPackets[i]);
1431             pw.print(" operations="); pw.println(operations[i]);
1432         }
1433     }
1434 
1435     /**
1436      * Return text description of {@link #set} value.
1437      * @hide
1438      */
setToString(int set)1439     public static String setToString(int set) {
1440         switch (set) {
1441             case SET_ALL:
1442                 return "ALL";
1443             case SET_DEFAULT:
1444                 return "DEFAULT";
1445             case SET_FOREGROUND:
1446                 return "FOREGROUND";
1447             case SET_DBG_VPN_IN:
1448                 return "DBG_VPN_IN";
1449             case SET_DBG_VPN_OUT:
1450                 return "DBG_VPN_OUT";
1451             default:
1452                 return "UNKNOWN";
1453         }
1454     }
1455 
1456     /**
1457      * Return text description of {@link #set} value.
1458      * @hide
1459      */
setToCheckinString(int set)1460     public static String setToCheckinString(int set) {
1461         switch (set) {
1462             case SET_ALL:
1463                 return "all";
1464             case SET_DEFAULT:
1465                 return "def";
1466             case SET_FOREGROUND:
1467                 return "fg";
1468             case SET_DBG_VPN_IN:
1469                 return "vpnin";
1470             case SET_DBG_VPN_OUT:
1471                 return "vpnout";
1472             default:
1473                 return "unk";
1474         }
1475     }
1476 
1477     /**
1478      * @return true if the querySet matches the dataSet.
1479      * @hide
1480      */
setMatches(int querySet, int dataSet)1481     public static boolean setMatches(int querySet, int dataSet) {
1482         if (querySet == dataSet) {
1483             return true;
1484         }
1485         // SET_ALL matches all non-debugging sets.
1486         return querySet == SET_ALL && dataSet < SET_DEBUG_START;
1487     }
1488 
1489     /**
1490      * Return text description of {@link #tag} value.
1491      * @hide
1492      */
tagToString(int tag)1493     public static String tagToString(int tag) {
1494         return "0x" + Integer.toHexString(tag);
1495     }
1496 
1497     /**
1498      * Return text description of {@link #metered} value.
1499      * @hide
1500      */
meteredToString(int metered)1501     public static String meteredToString(int metered) {
1502         switch (metered) {
1503             case METERED_ALL:
1504                 return "ALL";
1505             case METERED_NO:
1506                 return "NO";
1507             case METERED_YES:
1508                 return "YES";
1509             default:
1510                 return "UNKNOWN";
1511         }
1512     }
1513 
1514     /**
1515      * Return text description of {@link #roaming} value.
1516      * @hide
1517      */
roamingToString(int roaming)1518     public static String roamingToString(int roaming) {
1519         switch (roaming) {
1520             case ROAMING_ALL:
1521                 return "ALL";
1522             case ROAMING_NO:
1523                 return "NO";
1524             case ROAMING_YES:
1525                 return "YES";
1526             default:
1527                 return "UNKNOWN";
1528         }
1529     }
1530 
1531     /**
1532      * Return text description of {@link #defaultNetwork} value.
1533      * @hide
1534      */
defaultNetworkToString(int defaultNetwork)1535     public static String defaultNetworkToString(int defaultNetwork) {
1536         switch (defaultNetwork) {
1537             case DEFAULT_NETWORK_ALL:
1538                 return "ALL";
1539             case DEFAULT_NETWORK_NO:
1540                 return "NO";
1541             case DEFAULT_NETWORK_YES:
1542                 return "YES";
1543             default:
1544                 return "UNKNOWN";
1545         }
1546     }
1547 
1548     /** @hide */
1549     @Override
toString()1550     public String toString() {
1551         final CharArrayWriter writer = new CharArrayWriter();
1552         dump("", new PrintWriter(writer));
1553         return writer.toString();
1554     }
1555 
1556     @Override
describeContents()1557     public int describeContents() {
1558         return 0;
1559     }
1560 
1561     public static final @NonNull Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
1562         @Override
1563         public NetworkStats createFromParcel(Parcel in) {
1564             return new NetworkStats(in);
1565         }
1566 
1567         @Override
1568         public NetworkStats[] newArray(int size) {
1569             return new NetworkStats[size];
1570         }
1571     };
1572 
1573     /** @hide */
1574     public interface NonMonotonicObserver<C> {
foundNonMonotonic( NetworkStats left, int leftIndex, NetworkStats right, int rightIndex, C cookie)1575         public void foundNonMonotonic(
1576                 NetworkStats left, int leftIndex, NetworkStats right, int rightIndex, C cookie);
foundNonMonotonic( NetworkStats stats, int statsIndex, C cookie)1577         public void foundNonMonotonic(
1578                 NetworkStats stats, int statsIndex, C cookie);
1579     }
1580 
1581     /**
1582      * VPN accounting. Move some VPN's underlying traffic to other UIDs that use tun0 iface.
1583      *
1584      * <p>This method should only be called on delta NetworkStats. Do not call this method on a
1585      * snapshot {@link NetworkStats} object because the tunUid and/or the underlyingIface may change
1586      * over time.
1587      *
1588      * <p>This method performs adjustments for one active VPN package and one VPN iface at a time.
1589      *
1590      * @param tunUid uid of the VPN application
1591      * @param tunIface iface of the vpn tunnel
1592      * @param underlyingIfaces underlying network ifaces used by the VPN application
1593      * @hide
1594      */
migrateTun(int tunUid, @NonNull String tunIface, @NonNull List<String> underlyingIfaces)1595     public void migrateTun(int tunUid, @NonNull String tunIface,
1596             @NonNull List<String> underlyingIfaces) {
1597         // Combined usage by all apps using VPN.
1598         final Entry tunIfaceTotal = new Entry();
1599         // Usage by VPN, grouped by its {@code underlyingIfaces}.
1600         final Entry[] perInterfaceTotal = new Entry[underlyingIfaces.size()];
1601         // Usage by VPN, summed across all its {@code underlyingIfaces}.
1602         final Entry underlyingIfacesTotal = new Entry();
1603 
1604         for (int i = 0; i < perInterfaceTotal.length; i++) {
1605             perInterfaceTotal[i] = new Entry();
1606         }
1607 
1608         tunAdjustmentInit(tunUid, tunIface, underlyingIfaces, tunIfaceTotal, perInterfaceTotal,
1609                 underlyingIfacesTotal);
1610 
1611         // If tunIface < underlyingIfacesTotal, it leaves the overhead traffic in the VPN app.
1612         // If tunIface > underlyingIfacesTotal, the VPN app doesn't get credit for data compression.
1613         // Negative stats should be avoided.
1614         final Entry[] moved =
1615                 addTrafficToApplications(tunUid, tunIface, underlyingIfaces, tunIfaceTotal,
1616                         perInterfaceTotal, underlyingIfacesTotal);
1617         deductTrafficFromVpnApp(tunUid, underlyingIfaces, moved);
1618     }
1619 
1620     /**
1621      * Initializes the data used by the migrateTun() method.
1622      *
1623      * <p>This is the first pass iteration which does the following work:
1624      *
1625      * <ul>
1626      *   <li>Adds up all the traffic through the tunUid's underlyingIfaces (both foreground and
1627      *       background).
1628      *   <li>Adds up all the traffic through tun0 excluding traffic from the vpn app itself.
1629      * </ul>
1630      *
1631      * @param tunUid uid of the VPN application
1632      * @param tunIface iface of the vpn tunnel
1633      * @param underlyingIfaces underlying network ifaces used by the VPN application
1634      * @param tunIfaceTotal output parameter; combined data usage by all apps using VPN
1635      * @param perInterfaceTotal output parameter; data usage by VPN app, grouped by its {@code
1636      *     underlyingIfaces}
1637      * @param underlyingIfacesTotal output parameter; data usage by VPN, summed across all of its
1638      *     {@code underlyingIfaces}
1639      */
tunAdjustmentInit(int tunUid, @NonNull String tunIface, @NonNull List<String> underlyingIfaces, @NonNull Entry tunIfaceTotal, @NonNull Entry[] perInterfaceTotal, @NonNull Entry underlyingIfacesTotal)1640     private void tunAdjustmentInit(int tunUid, @NonNull String tunIface,
1641             @NonNull List<String> underlyingIfaces, @NonNull Entry tunIfaceTotal,
1642             @NonNull Entry[] perInterfaceTotal, @NonNull Entry underlyingIfacesTotal) {
1643         final Entry recycle = new Entry();
1644         for (int i = 0; i < size; i++) {
1645             getValues(i, recycle);
1646             if (recycle.uid == UID_ALL) {
1647                 throw new IllegalStateException(
1648                         "Cannot adjust VPN accounting on an iface aggregated NetworkStats.");
1649             }
1650             if (recycle.set == SET_DBG_VPN_IN || recycle.set == SET_DBG_VPN_OUT) {
1651                 throw new IllegalStateException(
1652                         "Cannot adjust VPN accounting on a NetworkStats containing SET_DBG_VPN_*");
1653             }
1654             if (recycle.tag != TAG_NONE) {
1655                 // TODO(b/123666283): Take all tags for tunUid into account.
1656                 continue;
1657             }
1658 
1659             if (tunUid == Process.SYSTEM_UID) {
1660                 // Kernel-based VPN or VCN, traffic sent by apps on the VPN/VCN network
1661                 //
1662                 // Since the data is not UID-accounted on underlying networks, just use VPN/VCN
1663                 // network usage as ground truth. Encrypted traffic on the underlying networks will
1664                 // never be processed here because encrypted traffic on the underlying interfaces
1665                 // is not present in UID stats, and this method is only called on UID stats.
1666                 if (tunIface.equals(recycle.iface)) {
1667                     tunIfaceTotal.add(recycle);
1668                     underlyingIfacesTotal.add(recycle);
1669 
1670                     // In steady state, there should always be one network, but edge cases may
1671                     // result in the network being null (network lost), and thus no underlying
1672                     // ifaces is possible.
1673                     if (perInterfaceTotal.length > 0) {
1674                         // While platform VPNs and VCNs have exactly one underlying network, that
1675                         // network may have multiple interfaces (eg for 464xlat). This layer does
1676                         // not have the required information to identify which of the interfaces
1677                         // were used. Select "any" of the interfaces. Since overhead is already
1678                         // lost, this number is an approximation anyways.
1679                         perInterfaceTotal[0].add(recycle);
1680                     }
1681                 }
1682             } else if (recycle.uid == tunUid) {
1683                 // VpnService VPN, traffic sent by the VPN app over underlying networks
1684                 for (int j = 0; j < underlyingIfaces.size(); j++) {
1685                     if (Objects.equals(underlyingIfaces.get(j), recycle.iface)) {
1686                         perInterfaceTotal[j].add(recycle);
1687                         underlyingIfacesTotal.add(recycle);
1688                         break;
1689                     }
1690                 }
1691             } else if (tunIface.equals(recycle.iface)) {
1692                 // VpnService VPN; traffic sent by apps on the VPN network
1693                 tunIfaceTotal.add(recycle);
1694             }
1695         }
1696     }
1697 
1698     /**
1699      * Distributes traffic across apps that are using given {@code tunIface}, and returns the total
1700      * traffic that should be moved off of {@code tunUid} grouped by {@code underlyingIfaces}.
1701      *
1702      * @param tunUid uid of the VPN application
1703      * @param tunIface iface of the vpn tunnel
1704      * @param underlyingIfaces underlying network ifaces used by the VPN application
1705      * @param tunIfaceTotal combined data usage across all apps using {@code tunIface}
1706      * @param perInterfaceTotal data usage by VPN app, grouped by its {@code underlyingIfaces}
1707      * @param underlyingIfacesTotal data usage by VPN, summed across all of its {@code
1708      *     underlyingIfaces}
1709      */
addTrafficToApplications(int tunUid, @NonNull String tunIface, @NonNull List<String> underlyingIfaces, @NonNull Entry tunIfaceTotal, @NonNull Entry[] perInterfaceTotal, @NonNull Entry underlyingIfacesTotal)1710     private Entry[] addTrafficToApplications(int tunUid, @NonNull String tunIface,
1711             @NonNull List<String> underlyingIfaces, @NonNull Entry tunIfaceTotal,
1712             @NonNull Entry[] perInterfaceTotal, @NonNull Entry underlyingIfacesTotal) {
1713         // Traffic that should be moved off of each underlying interface for tunUid (see
1714         // deductTrafficFromVpnApp below).
1715         final Entry[] moved = new Entry[underlyingIfaces.size()];
1716         for (int i = 0; i < underlyingIfaces.size(); i++) {
1717             moved[i] = new Entry();
1718         }
1719 
1720         final Entry tmpEntry = new Entry();
1721         final int origSize = size;
1722         for (int i = 0; i < origSize; i++) {
1723             if (!Objects.equals(iface[i], tunIface)) {
1724                 // Consider only entries that go onto the VPN interface.
1725                 continue;
1726             }
1727 
1728             if (uid[i] == tunUid && tunUid != Process.SYSTEM_UID) {
1729                 // Exclude VPN app from the redistribution, as it can choose to create packet
1730                 // streams by writing to itself.
1731                 //
1732                 // However, for platform VPNs, do not exclude the system's usage of the VPN network,
1733                 // since it is never local-only, and never double counted
1734                 continue;
1735             }
1736             tmpEntry.uid = uid[i];
1737             tmpEntry.tag = tag[i];
1738             tmpEntry.metered = metered[i];
1739             tmpEntry.roaming = roaming[i];
1740             tmpEntry.defaultNetwork = defaultNetwork[i];
1741 
1742             // In a first pass, compute this entry's total share of data across all
1743             // underlyingIfaces. This is computed on the basis of the share of this entry's usage
1744             // over tunIface.
1745             // TODO: Consider refactoring first pass into a separate helper method.
1746             long totalRxBytes = 0;
1747             if (tunIfaceTotal.rxBytes > 0) {
1748                 // Note - The multiplication below should not overflow since NetworkStatsService
1749                 // processes this every time device has transmitted/received amount equivalent to
1750                 // global threshold alert (~ 2MB) across all interfaces.
1751                 final long rxBytesAcrossUnderlyingIfaces =
1752                         multiplySafeByRational(underlyingIfacesTotal.rxBytes,
1753                                 rxBytes[i], tunIfaceTotal.rxBytes);
1754                 // app must not be blamed for more than it consumed on tunIface
1755                 totalRxBytes = Math.min(rxBytes[i], rxBytesAcrossUnderlyingIfaces);
1756             }
1757             long totalRxPackets = 0;
1758             if (tunIfaceTotal.rxPackets > 0) {
1759                 final long rxPacketsAcrossUnderlyingIfaces =
1760                         multiplySafeByRational(underlyingIfacesTotal.rxPackets,
1761                                 rxPackets[i], tunIfaceTotal.rxPackets);
1762                 totalRxPackets = Math.min(rxPackets[i], rxPacketsAcrossUnderlyingIfaces);
1763             }
1764             long totalTxBytes = 0;
1765             if (tunIfaceTotal.txBytes > 0) {
1766                 final long txBytesAcrossUnderlyingIfaces =
1767                         multiplySafeByRational(underlyingIfacesTotal.txBytes,
1768                                 txBytes[i], tunIfaceTotal.txBytes);
1769                 totalTxBytes = Math.min(txBytes[i], txBytesAcrossUnderlyingIfaces);
1770             }
1771             long totalTxPackets = 0;
1772             if (tunIfaceTotal.txPackets > 0) {
1773                 final long txPacketsAcrossUnderlyingIfaces =
1774                         multiplySafeByRational(underlyingIfacesTotal.txPackets,
1775                                 txPackets[i], tunIfaceTotal.txPackets);
1776                 totalTxPackets = Math.min(txPackets[i], txPacketsAcrossUnderlyingIfaces);
1777             }
1778             long totalOperations = 0;
1779             if (tunIfaceTotal.operations > 0) {
1780                 final long operationsAcrossUnderlyingIfaces =
1781                         multiplySafeByRational(underlyingIfacesTotal.operations,
1782                                 operations[i], tunIfaceTotal.operations);
1783                 totalOperations = Math.min(operations[i], operationsAcrossUnderlyingIfaces);
1784             }
1785             // In a second pass, distribute these values across interfaces in the proportion that
1786             // each interface represents of the total traffic of the underlying interfaces.
1787             for (int j = 0; j < underlyingIfaces.size(); j++) {
1788                 tmpEntry.iface = underlyingIfaces.get(j);
1789                 tmpEntry.rxBytes = 0;
1790                 // Reset 'set' to correct value since it gets updated when adding debug info below.
1791                 tmpEntry.set = set[i];
1792                 if (underlyingIfacesTotal.rxBytes > 0) {
1793                     tmpEntry.rxBytes =
1794                             multiplySafeByRational(totalRxBytes,
1795                                     perInterfaceTotal[j].rxBytes,
1796                                     underlyingIfacesTotal.rxBytes);
1797                 }
1798                 tmpEntry.rxPackets = 0;
1799                 if (underlyingIfacesTotal.rxPackets > 0) {
1800                     tmpEntry.rxPackets =
1801                             multiplySafeByRational(totalRxPackets,
1802                                     perInterfaceTotal[j].rxPackets,
1803                                     underlyingIfacesTotal.rxPackets);
1804                 }
1805                 tmpEntry.txBytes = 0;
1806                 if (underlyingIfacesTotal.txBytes > 0) {
1807                     tmpEntry.txBytes =
1808                             multiplySafeByRational(totalTxBytes,
1809                                     perInterfaceTotal[j].txBytes,
1810                                     underlyingIfacesTotal.txBytes);
1811                 }
1812                 tmpEntry.txPackets = 0;
1813                 if (underlyingIfacesTotal.txPackets > 0) {
1814                     tmpEntry.txPackets =
1815                             multiplySafeByRational(totalTxPackets,
1816                                     perInterfaceTotal[j].txPackets,
1817                                     underlyingIfacesTotal.txPackets);
1818                 }
1819                 tmpEntry.operations = 0;
1820                 if (underlyingIfacesTotal.operations > 0) {
1821                     tmpEntry.operations =
1822                             multiplySafeByRational(totalOperations,
1823                                     perInterfaceTotal[j].operations,
1824                                     underlyingIfacesTotal.operations);
1825                 }
1826                 // tmpEntry now contains the migrated data of the i-th entry for the j-th underlying
1827                 // interface. Add that data usage to this object.
1828                 combineValues(tmpEntry);
1829                 if (tag[i] == TAG_NONE) {
1830                     // Add the migrated data to moved so it is deducted from the VPN app later.
1831                     moved[j].add(tmpEntry);
1832                     // Add debug info
1833                     tmpEntry.set = SET_DBG_VPN_IN;
1834                     combineValues(tmpEntry);
1835                 }
1836             }
1837         }
1838         return moved;
1839     }
1840 
deductTrafficFromVpnApp( int tunUid, @NonNull List<String> underlyingIfaces, @NonNull Entry[] moved)1841     private void deductTrafficFromVpnApp(
1842             int tunUid,
1843             @NonNull List<String> underlyingIfaces,
1844             @NonNull Entry[] moved) {
1845         if (tunUid == Process.SYSTEM_UID) {
1846             // No traffic recorded on a per-UID basis for in-kernel VPN/VCNs over underlying
1847             // networks; thus no traffic to deduct.
1848             return;
1849         }
1850 
1851         for (int i = 0; i < underlyingIfaces.size(); i++) {
1852             moved[i].uid = tunUid;
1853             // Add debug info
1854             moved[i].set = SET_DBG_VPN_OUT;
1855             moved[i].tag = TAG_NONE;
1856             moved[i].iface = underlyingIfaces.get(i);
1857             moved[i].metered = METERED_ALL;
1858             moved[i].roaming = ROAMING_ALL;
1859             moved[i].defaultNetwork = DEFAULT_NETWORK_ALL;
1860             combineValues(moved[i]);
1861 
1862             // Caveat: if the vpn software uses tag, the total tagged traffic may be greater than
1863             // the TAG_NONE traffic.
1864             //
1865             // Relies on the fact that the underlying traffic only has state ROAMING_NO and
1866             // METERED_NO, which should be the case as it comes directly from the /proc file.
1867             // We only blend in the roaming data after applying these adjustments, by checking the
1868             // NetworkIdentity of the underlying iface.
1869             final int idxVpnBackground = findIndex(underlyingIfaces.get(i), tunUid, SET_DEFAULT,
1870                             TAG_NONE, METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO);
1871             if (idxVpnBackground != -1) {
1872                 // Note - tunSubtract also updates moved[i]; whatever traffic that's left is removed
1873                 // from foreground usage.
1874                 tunSubtract(idxVpnBackground, this, moved[i]);
1875             }
1876 
1877             final int idxVpnForeground = findIndex(underlyingIfaces.get(i), tunUid, SET_FOREGROUND,
1878                             TAG_NONE, METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO);
1879             if (idxVpnForeground != -1) {
1880                 tunSubtract(idxVpnForeground, this, moved[i]);
1881             }
1882         }
1883     }
1884 
tunSubtract(int i, @NonNull NetworkStats left, @NonNull Entry right)1885     private static void tunSubtract(int i, @NonNull NetworkStats left, @NonNull Entry right) {
1886         long rxBytes = Math.min(left.rxBytes[i], right.rxBytes);
1887         left.rxBytes[i] -= rxBytes;
1888         right.rxBytes -= rxBytes;
1889 
1890         long rxPackets = Math.min(left.rxPackets[i], right.rxPackets);
1891         left.rxPackets[i] -= rxPackets;
1892         right.rxPackets -= rxPackets;
1893 
1894         long txBytes = Math.min(left.txBytes[i], right.txBytes);
1895         left.txBytes[i] -= txBytes;
1896         right.txBytes -= txBytes;
1897 
1898         long txPackets = Math.min(left.txPackets[i], right.txPackets);
1899         left.txPackets[i] -= txPackets;
1900         right.txPackets -= txPackets;
1901     }
1902 }
1903