Loading core/java/android/net/NetworkStats.java +3 −12 Original line number Original line Diff line number Diff line Loading @@ -16,8 +16,6 @@ package android.net; package android.net; import static android.os.Process.CLAT_UID; import android.annotation.IntDef; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.Nullable; Loading Loading @@ -1069,11 +1067,10 @@ public final class NetworkStats implements Parcelable { * @param baseTraffic Traffic on the base interfaces. Will be mutated. * @param baseTraffic Traffic on the base interfaces. Will be mutated. * @param stackedTraffic Stats with traffic stacked on top of our ifaces. Will also be mutated. * @param stackedTraffic Stats with traffic stacked on top of our ifaces. Will also be mutated. * @param stackedIfaces Mapping ipv6if -> ipv4if interface where traffic is counted on both. * @param stackedIfaces Mapping ipv6if -> ipv4if interface where traffic is counted on both. * @param useBpfStats True if eBPF is in use. * @hide * @hide */ */ public static void apply464xlatAdjustments(NetworkStats baseTraffic, public static void apply464xlatAdjustments(NetworkStats baseTraffic, NetworkStats stackedTraffic, Map<String, String> stackedIfaces, boolean useBpfStats) { NetworkStats stackedTraffic, Map<String, String> stackedIfaces) { // For recycling // For recycling Entry entry = null; Entry entry = null; for (int i = 0; i < stackedTraffic.size; i++) { for (int i = 0; i < stackedTraffic.size; i++) { Loading @@ -1096,12 +1093,6 @@ public final class NetworkStats implements Parcelable { entry.txBytes += entry.txPackets * IPV4V6_HEADER_DELTA; entry.txBytes += entry.txPackets * IPV4V6_HEADER_DELTA; stackedTraffic.setValues(i, entry); stackedTraffic.setValues(i, entry); } } // Theoretically there should be no traffic accounted to the clat daemon's uid: // see ebpf program 'netd.c's early returns // and iptables '-m owner --uid-owner clat -j RETURN' rules prior to accounting // TODO: remove this - should definitely be safe once ebpf only. baseTraffic.removeUids(new int[] {CLAT_UID}); } } /** /** Loading @@ -1113,8 +1104,8 @@ public final class NetworkStats implements Parcelable { * @param stackedIfaces Mapping ipv6if -> ipv4if interface where traffic is counted on both. * @param stackedIfaces Mapping ipv6if -> ipv4if interface where traffic is counted on both. * @hide * @hide */ */ public void apply464xlatAdjustments(Map<String, String> stackedIfaces, boolean useBpfStats) { public void apply464xlatAdjustments(Map<String, String> stackedIfaces) { apply464xlatAdjustments(this, this, stackedIfaces, useBpfStats); apply464xlatAdjustments(this, this, stackedIfaces); } } /** /** Loading services/core/java/com/android/server/net/NetworkStatsFactory.java +4 −6 Original line number Original line Diff line number Diff line Loading @@ -152,12 +152,10 @@ public class NetworkStatsFactory { /** /** * Applies 464xlat adjustments with ifaces noted with {@link #noteStackedIface(String, String)}. * Applies 464xlat adjustments with ifaces noted with {@link #noteStackedIface(String, String)}. * @see NetworkStats#apply464xlatAdjustments(NetworkStats, NetworkStats, Map, boolean) * @see NetworkStats#apply464xlatAdjustments(NetworkStats, NetworkStats, Map) */ */ public void apply464xlatAdjustments(NetworkStats baseTraffic, public void apply464xlatAdjustments(NetworkStats baseTraffic, NetworkStats stackedTraffic) { NetworkStats stackedTraffic, boolean useBpfStats) { NetworkStats.apply464xlatAdjustments(baseTraffic, stackedTraffic, mStackedIfaces); NetworkStats.apply464xlatAdjustments(baseTraffic, stackedTraffic, mStackedIfaces, useBpfStats); } } public NetworkStatsFactory() { public NetworkStatsFactory() { Loading Loading @@ -380,7 +378,7 @@ public class NetworkStatsFactory { // network, the overhead is their fault. // network, the overhead is their fault. // No locking here: apply464xlatAdjustments behaves fine with an add-only // No locking here: apply464xlatAdjustments behaves fine with an add-only // ConcurrentHashMap. // ConcurrentHashMap. delta.apply464xlatAdjustments(mStackedIfaces, mUseBpfStats); delta.apply464xlatAdjustments(mStackedIfaces); // Migrate data usage over a VPN to the TUN network. // Migrate data usage over a VPN to the TUN network. for (VpnInfo info : vpnArray) { for (VpnInfo info : vpnArray) { Loading services/core/java/com/android/server/net/NetworkStatsService.java +2 −3 Original line number Original line Diff line number Diff line Loading @@ -1882,14 +1882,13 @@ public class NetworkStatsService extends INetworkStatsService.Stub { // fold tethering stats and operations into uid snapshot // fold tethering stats and operations into uid snapshot final NetworkStats tetherSnapshot = getNetworkStatsTethering(STATS_PER_UID); final NetworkStats tetherSnapshot = getNetworkStatsTethering(STATS_PER_UID); tetherSnapshot.filter(UID_ALL, ifaces, TAG_ALL); tetherSnapshot.filter(UID_ALL, ifaces, TAG_ALL); mStatsFactory.apply464xlatAdjustments(uidSnapshot, tetherSnapshot, mStatsFactory.apply464xlatAdjustments(uidSnapshot, tetherSnapshot); mUseBpfTrafficStats); uidSnapshot.combineAllValues(tetherSnapshot); uidSnapshot.combineAllValues(tetherSnapshot); // get a stale copy of uid stats snapshot provided by providers. // get a stale copy of uid stats snapshot provided by providers. final NetworkStats providerStats = getNetworkStatsFromProviders(STATS_PER_UID); final NetworkStats providerStats = getNetworkStatsFromProviders(STATS_PER_UID); providerStats.filter(UID_ALL, ifaces, TAG_ALL); providerStats.filter(UID_ALL, ifaces, TAG_ALL); mStatsFactory.apply464xlatAdjustments(uidSnapshot, providerStats, mUseBpfTrafficStats); mStatsFactory.apply464xlatAdjustments(uidSnapshot, providerStats); uidSnapshot.combineAllValues(providerStats); uidSnapshot.combineAllValues(providerStats); uidSnapshot.combineAllValues(mUidOperations); uidSnapshot.combineAllValues(mUidOperations); Loading tests/net/java/android/net/NetworkStatsTest.java +7 −17 Original line number Original line Diff line number Diff line Loading @@ -926,21 +926,14 @@ public class NetworkStatsTest { 3 /* txPackets */, 3 /* txPackets */, 0 /* operations */); 0 /* operations */); final NetworkStats statsXt = new NetworkStats(TEST_START, 3) final NetworkStats stats = new NetworkStats(TEST_START, 3) .insertEntry(appEntry) .insertEntry(rootUidEntry) .insertEntry(otherEntry); final NetworkStats statsEbpf = new NetworkStats(TEST_START, 3) .insertEntry(appEntry) .insertEntry(appEntry) .insertEntry(rootUidEntry) .insertEntry(rootUidEntry) .insertEntry(otherEntry); .insertEntry(otherEntry); statsXt.apply464xlatAdjustments(stackedIface, false); stats.apply464xlatAdjustments(stackedIface); statsEbpf.apply464xlatAdjustments(stackedIface, true); assertEquals(3, statsXt.size()); assertEquals(3, stats.size()); assertEquals(3, statsEbpf.size()); final NetworkStats.Entry expectedAppUid = new NetworkStats.Entry( final NetworkStats.Entry expectedAppUid = new NetworkStats.Entry( v4Iface, appUid, SET_DEFAULT, TAG_NONE, v4Iface, appUid, SET_DEFAULT, TAG_NONE, 30949510, 30949510, Loading @@ -955,12 +948,9 @@ public class NetworkStatsTest { 17607, 17607, 97, 97, 0); 0); assertEquals(expectedAppUid, statsXt.getValues(0, null)); assertEquals(expectedAppUid, stats.getValues(0, null)); assertEquals(expectedRootUid, statsXt.getValues(1, null)); assertEquals(expectedRootUid, stats.getValues(1, null)); assertEquals(otherEntry, statsXt.getValues(2, null)); assertEquals(otherEntry, stats.getValues(2, null)); assertEquals(expectedAppUid, statsEbpf.getValues(0, null)); assertEquals(expectedRootUid, statsEbpf.getValues(1, null)); assertEquals(otherEntry, statsEbpf.getValues(2, null)); } } @Test @Test Loading @@ -985,7 +975,7 @@ public class NetworkStatsTest { .insertEntry(secondEntry); .insertEntry(secondEntry); // Empty map: no adjustment // Empty map: no adjustment stats.apply464xlatAdjustments(new ArrayMap<>(), false); stats.apply464xlatAdjustments(new ArrayMap<>()); assertEquals(2, stats.size()); assertEquals(2, stats.size()); assertEquals(firstEntry, stats.getValues(0, null)); assertEquals(firstEntry, stats.getValues(0, null)); Loading tests/net/res/raw/xt_qtaguid_with_clat +0 −2 Original line number Original line Diff line number Diff line Loading @@ -41,5 +41,3 @@ idx iface acct_tag_hex uid_tag_int cnt_set rx_bytes rx_packets tx_bytes tx_packe 41 dummy0 0x0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 41 dummy0 0x0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 42 lo 0x0 0 0 1288 16 1288 16 0 0 532 8 756 8 0 0 532 8 756 8 42 lo 0x0 0 0 1288 16 1288 16 0 0 532 8 756 8 0 0 532 8 756 8 43 lo 0x0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 43 lo 0x0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 44 wlan0 0x0 1029 0 0 0 312046 5113 0 0 0 0 0 0 306544 5046 3230 38 2272 29 45 wlan0 0x0 1029 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 No newline at end of file Loading
core/java/android/net/NetworkStats.java +3 −12 Original line number Original line Diff line number Diff line Loading @@ -16,8 +16,6 @@ package android.net; package android.net; import static android.os.Process.CLAT_UID; import android.annotation.IntDef; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.Nullable; Loading Loading @@ -1069,11 +1067,10 @@ public final class NetworkStats implements Parcelable { * @param baseTraffic Traffic on the base interfaces. Will be mutated. * @param baseTraffic Traffic on the base interfaces. Will be mutated. * @param stackedTraffic Stats with traffic stacked on top of our ifaces. Will also be mutated. * @param stackedTraffic Stats with traffic stacked on top of our ifaces. Will also be mutated. * @param stackedIfaces Mapping ipv6if -> ipv4if interface where traffic is counted on both. * @param stackedIfaces Mapping ipv6if -> ipv4if interface where traffic is counted on both. * @param useBpfStats True if eBPF is in use. * @hide * @hide */ */ public static void apply464xlatAdjustments(NetworkStats baseTraffic, public static void apply464xlatAdjustments(NetworkStats baseTraffic, NetworkStats stackedTraffic, Map<String, String> stackedIfaces, boolean useBpfStats) { NetworkStats stackedTraffic, Map<String, String> stackedIfaces) { // For recycling // For recycling Entry entry = null; Entry entry = null; for (int i = 0; i < stackedTraffic.size; i++) { for (int i = 0; i < stackedTraffic.size; i++) { Loading @@ -1096,12 +1093,6 @@ public final class NetworkStats implements Parcelable { entry.txBytes += entry.txPackets * IPV4V6_HEADER_DELTA; entry.txBytes += entry.txPackets * IPV4V6_HEADER_DELTA; stackedTraffic.setValues(i, entry); stackedTraffic.setValues(i, entry); } } // Theoretically there should be no traffic accounted to the clat daemon's uid: // see ebpf program 'netd.c's early returns // and iptables '-m owner --uid-owner clat -j RETURN' rules prior to accounting // TODO: remove this - should definitely be safe once ebpf only. baseTraffic.removeUids(new int[] {CLAT_UID}); } } /** /** Loading @@ -1113,8 +1104,8 @@ public final class NetworkStats implements Parcelable { * @param stackedIfaces Mapping ipv6if -> ipv4if interface where traffic is counted on both. * @param stackedIfaces Mapping ipv6if -> ipv4if interface where traffic is counted on both. * @hide * @hide */ */ public void apply464xlatAdjustments(Map<String, String> stackedIfaces, boolean useBpfStats) { public void apply464xlatAdjustments(Map<String, String> stackedIfaces) { apply464xlatAdjustments(this, this, stackedIfaces, useBpfStats); apply464xlatAdjustments(this, this, stackedIfaces); } } /** /** Loading
services/core/java/com/android/server/net/NetworkStatsFactory.java +4 −6 Original line number Original line Diff line number Diff line Loading @@ -152,12 +152,10 @@ public class NetworkStatsFactory { /** /** * Applies 464xlat adjustments with ifaces noted with {@link #noteStackedIface(String, String)}. * Applies 464xlat adjustments with ifaces noted with {@link #noteStackedIface(String, String)}. * @see NetworkStats#apply464xlatAdjustments(NetworkStats, NetworkStats, Map, boolean) * @see NetworkStats#apply464xlatAdjustments(NetworkStats, NetworkStats, Map) */ */ public void apply464xlatAdjustments(NetworkStats baseTraffic, public void apply464xlatAdjustments(NetworkStats baseTraffic, NetworkStats stackedTraffic) { NetworkStats stackedTraffic, boolean useBpfStats) { NetworkStats.apply464xlatAdjustments(baseTraffic, stackedTraffic, mStackedIfaces); NetworkStats.apply464xlatAdjustments(baseTraffic, stackedTraffic, mStackedIfaces, useBpfStats); } } public NetworkStatsFactory() { public NetworkStatsFactory() { Loading Loading @@ -380,7 +378,7 @@ public class NetworkStatsFactory { // network, the overhead is their fault. // network, the overhead is their fault. // No locking here: apply464xlatAdjustments behaves fine with an add-only // No locking here: apply464xlatAdjustments behaves fine with an add-only // ConcurrentHashMap. // ConcurrentHashMap. delta.apply464xlatAdjustments(mStackedIfaces, mUseBpfStats); delta.apply464xlatAdjustments(mStackedIfaces); // Migrate data usage over a VPN to the TUN network. // Migrate data usage over a VPN to the TUN network. for (VpnInfo info : vpnArray) { for (VpnInfo info : vpnArray) { Loading
services/core/java/com/android/server/net/NetworkStatsService.java +2 −3 Original line number Original line Diff line number Diff line Loading @@ -1882,14 +1882,13 @@ public class NetworkStatsService extends INetworkStatsService.Stub { // fold tethering stats and operations into uid snapshot // fold tethering stats and operations into uid snapshot final NetworkStats tetherSnapshot = getNetworkStatsTethering(STATS_PER_UID); final NetworkStats tetherSnapshot = getNetworkStatsTethering(STATS_PER_UID); tetherSnapshot.filter(UID_ALL, ifaces, TAG_ALL); tetherSnapshot.filter(UID_ALL, ifaces, TAG_ALL); mStatsFactory.apply464xlatAdjustments(uidSnapshot, tetherSnapshot, mStatsFactory.apply464xlatAdjustments(uidSnapshot, tetherSnapshot); mUseBpfTrafficStats); uidSnapshot.combineAllValues(tetherSnapshot); uidSnapshot.combineAllValues(tetherSnapshot); // get a stale copy of uid stats snapshot provided by providers. // get a stale copy of uid stats snapshot provided by providers. final NetworkStats providerStats = getNetworkStatsFromProviders(STATS_PER_UID); final NetworkStats providerStats = getNetworkStatsFromProviders(STATS_PER_UID); providerStats.filter(UID_ALL, ifaces, TAG_ALL); providerStats.filter(UID_ALL, ifaces, TAG_ALL); mStatsFactory.apply464xlatAdjustments(uidSnapshot, providerStats, mUseBpfTrafficStats); mStatsFactory.apply464xlatAdjustments(uidSnapshot, providerStats); uidSnapshot.combineAllValues(providerStats); uidSnapshot.combineAllValues(providerStats); uidSnapshot.combineAllValues(mUidOperations); uidSnapshot.combineAllValues(mUidOperations); Loading
tests/net/java/android/net/NetworkStatsTest.java +7 −17 Original line number Original line Diff line number Diff line Loading @@ -926,21 +926,14 @@ public class NetworkStatsTest { 3 /* txPackets */, 3 /* txPackets */, 0 /* operations */); 0 /* operations */); final NetworkStats statsXt = new NetworkStats(TEST_START, 3) final NetworkStats stats = new NetworkStats(TEST_START, 3) .insertEntry(appEntry) .insertEntry(rootUidEntry) .insertEntry(otherEntry); final NetworkStats statsEbpf = new NetworkStats(TEST_START, 3) .insertEntry(appEntry) .insertEntry(appEntry) .insertEntry(rootUidEntry) .insertEntry(rootUidEntry) .insertEntry(otherEntry); .insertEntry(otherEntry); statsXt.apply464xlatAdjustments(stackedIface, false); stats.apply464xlatAdjustments(stackedIface); statsEbpf.apply464xlatAdjustments(stackedIface, true); assertEquals(3, statsXt.size()); assertEquals(3, stats.size()); assertEquals(3, statsEbpf.size()); final NetworkStats.Entry expectedAppUid = new NetworkStats.Entry( final NetworkStats.Entry expectedAppUid = new NetworkStats.Entry( v4Iface, appUid, SET_DEFAULT, TAG_NONE, v4Iface, appUid, SET_DEFAULT, TAG_NONE, 30949510, 30949510, Loading @@ -955,12 +948,9 @@ public class NetworkStatsTest { 17607, 17607, 97, 97, 0); 0); assertEquals(expectedAppUid, statsXt.getValues(0, null)); assertEquals(expectedAppUid, stats.getValues(0, null)); assertEquals(expectedRootUid, statsXt.getValues(1, null)); assertEquals(expectedRootUid, stats.getValues(1, null)); assertEquals(otherEntry, statsXt.getValues(2, null)); assertEquals(otherEntry, stats.getValues(2, null)); assertEquals(expectedAppUid, statsEbpf.getValues(0, null)); assertEquals(expectedRootUid, statsEbpf.getValues(1, null)); assertEquals(otherEntry, statsEbpf.getValues(2, null)); } } @Test @Test Loading @@ -985,7 +975,7 @@ public class NetworkStatsTest { .insertEntry(secondEntry); .insertEntry(secondEntry); // Empty map: no adjustment // Empty map: no adjustment stats.apply464xlatAdjustments(new ArrayMap<>(), false); stats.apply464xlatAdjustments(new ArrayMap<>()); assertEquals(2, stats.size()); assertEquals(2, stats.size()); assertEquals(firstEntry, stats.getValues(0, null)); assertEquals(firstEntry, stats.getValues(0, null)); Loading
tests/net/res/raw/xt_qtaguid_with_clat +0 −2 Original line number Original line Diff line number Diff line Loading @@ -41,5 +41,3 @@ idx iface acct_tag_hex uid_tag_int cnt_set rx_bytes rx_packets tx_bytes tx_packe 41 dummy0 0x0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 41 dummy0 0x0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 42 lo 0x0 0 0 1288 16 1288 16 0 0 532 8 756 8 0 0 532 8 756 8 42 lo 0x0 0 0 1288 16 1288 16 0 0 532 8 756 8 0 0 532 8 756 8 43 lo 0x0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 43 lo 0x0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 44 wlan0 0x0 1029 0 0 0 312046 5113 0 0 0 0 0 0 306544 5046 3230 38 2272 29 45 wlan0 0x0 1029 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 No newline at end of file