Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bd737985 authored by Junyu Lai's avatar Junyu Lai Committed by Gerrit Code Review
Browse files

Merge "Ignore ipv6 tx traffic on clat uid."

parents 94d92dc3 f6ebf4ae
Loading
Loading
Loading
Loading
+15 −11
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.net;

import static android.os.Process.CLAT_UID;

import android.annotation.UnsupportedAppUsage;
import android.os.Parcel;
import android.os.Parcelable;
@@ -828,13 +830,15 @@ public class NetworkStats implements Parcelable {
     *
     * <p>For 464xlat traffic, xt_qtaguid sees every IPv4 packet twice, once as a native IPv4
     * packet on the stacked interface, and once as translated to an IPv6 packet on the
     * base interface. For correct stats accounting on the base interface, every 464xlat
     * packet needs to be subtracted from the root UID on the base interface both for tx
     * and rx traffic (http://b/12249687, http:/b/33681750).
     * base interface. For correct stats accounting on the base interface, if using xt_qtaguid,
     * every rx 464xlat packet needs to be subtracted from the root UID on the base interface
     * (http://b/12249687, http:/b/33681750), and every tx 464xlat packet which was counted onto
     * clat uid should be ignored.
     *
     * As for eBPF, the per uid stats is collected by different hook, the rx packets on base
     * interface will not be counted. Thus, the adjustment on root uid is only needed in tx
     * direction.
     * interface will not be counted. Thus, the adjustment on root uid is not needed. However, the
     * tx traffic counted in the same way xt_qtaguid does, so the traffic on clat uid still
     * needs to be ignored.
     *
     * <p>This method will behave fine if {@code stackedIfaces} is an non-synchronized but add-only
     * {@code ConcurrentHashMap}
@@ -862,17 +866,14 @@ public class NetworkStats implements Parcelable {
            if (baseIface == null) {
                continue;
            }
            // Subtract any 464lat traffic seen for the root UID on the current base interface.
            // However, for eBPF, the per uid stats is collected by different hook, the rx packets
            // on base interface will not be counted. Thus, the adjustment on root uid is only
            // needed in tx direction.
            // Subtract xt_qtaguid 464lat rx traffic seen for the root UID on the current base
            // interface. As for eBPF, the per uid stats is collected by different hook, the rx
            // packets on base interface will not be counted.
            adjust.iface = baseIface;
            if (!useBpfStats) {
                adjust.rxBytes = -(entry.rxBytes + entry.rxPackets * IPV4V6_HEADER_DELTA);
                adjust.rxPackets = -entry.rxPackets;
            }
            adjust.txBytes = -(entry.txBytes + entry.txPackets * IPV4V6_HEADER_DELTA);
            adjust.txPackets = -entry.txPackets;
            adjustments.combineValues(adjust);

            // For 464xlat traffic, per uid stats only counts the bytes of the native IPv4 packet
@@ -884,6 +885,9 @@ public class NetworkStats implements Parcelable {
            stackedTraffic.setValues(i, entry);
        }

        // Traffic on clat uid is v6 tx traffic that is already counted with app uid on the stacked
        // v4 interface, so it needs to be removed to avoid double-counting.
        baseTraffic.removeUids(new int[] {CLAT_UID});
        baseTraffic.combineAllValues(adjustments);
    }

+6 −0
Original line number Diff line number Diff line
@@ -108,6 +108,12 @@ public class Process {
     */
    public static final int NFC_UID = 1027;

    /**
     * Defines the UID/GID for the clatd process.
     * @hide
     * */
    public static final int CLAT_UID = 1029;

    /**
     * Defines the UID/GID for the Bluetooth service process.
     * @hide
+5 −7
Original line number Diff line number Diff line
@@ -832,25 +832,23 @@ public class NetworkStatsTest {
                0 /* operations */);

        // Traffic measured for the root uid on the base interface if eBPF is in use.
        // Incorrectly includes appEntry's bytes and packets, plus IPv4-IPv6 translation
        // overhead (20 bytes per packet), only for TX traffic.
        final NetworkStats.Entry ebpfRootUidEntry = new NetworkStats.Entry(
                baseIface, rootUid, SET_DEFAULT, TAG_NONE,
                163577 /* rxBytes */,
                187 /* rxPackets */,
                1169942 /* txBytes */,
                13902 /* txPackets */,
                17607 /* txBytes */,
                97 /* txPackets */,
                0 /* operations */);

        // Traffic measured for the root uid on the base interface if xt_qtaguid is in use.
        // Incorrectly includes appEntry's bytes and packets, plus IPv4-IPv6 translation
        // overhead (20 bytes per packet), in both directions.
        // overhead (20 bytes per packet), in rx direction.
        final NetworkStats.Entry xtRootUidEntry = new NetworkStats.Entry(
                baseIface, rootUid, SET_DEFAULT, TAG_NONE,
                31113087 /* rxBytes */,
                22588 /* rxPackets */,
                1169942 /* txBytes */,
                13902 /* txPackets */,
                17607 /* txBytes */,
                97 /* txPackets */,
                0 /* operations */);

        final NetworkStats.Entry otherEntry = new NetworkStats.Entry(
+14 −3
Original line number Diff line number Diff line
@@ -159,7 +159,7 @@ public class NetworkStatsFactoryTest {
        assertStatsEntry(stats, "v4-wlan0", 1000, SET_DEFAULT, 0x0, 30812L, 2310L);
        assertStatsEntry(stats, "v4-wlan0", 10102, SET_DEFAULT, 0x0, 10022L, 3330L);
        assertStatsEntry(stats, "v4-wlan0", 10060, SET_DEFAULT, 0x0, 9532772L, 254112L);
        assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, 15229L, 5766L);
        assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, 15229L, 0L);
        assertStatsEntry(stats, "wlan0", 1000, SET_DEFAULT, 0x0, 6126L, 2013L);
        assertStatsEntry(stats, "wlan0", 10013, SET_DEFAULT, 0x0, 0L, 144L);
        assertStatsEntry(stats, "wlan0", 10018, SET_DEFAULT, 0x0, 5980263L, 167667L);
@@ -170,6 +170,8 @@ public class NetworkStatsFactoryTest {
        assertStatsEntry(stats, "dummy0", 0, SET_DEFAULT, 0x0, 0L, 168L);
        assertStatsEntry(stats, "lo", 0, SET_DEFAULT, 0x0, 1288L, 1288L);

        assertNoStatsEntry(stats, "wlan0", 1029, SET_DEFAULT, 0x0);

        NetworkStatsFactory.clearStackedIfaces();
    }

@@ -191,12 +193,12 @@ public class NetworkStatsFactoryTest {
        // Stats snapshot before the download
        stats = parseDetailedStats(R.raw.xt_qtaguid_with_clat_100mb_download_before);
        assertStatsEntry(stats, "v4-wlan0", 10106, SET_FOREGROUND, 0x0, appRxBytesBefore, 5199872L);
        assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, rootRxBytesBefore, 647888L);
        assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, rootRxBytesBefore, 0L);

        // Stats snapshot after the download
        stats = parseDetailedStats(R.raw.xt_qtaguid_with_clat_100mb_download_after);
        assertStatsEntry(stats, "v4-wlan0", 10106, SET_FOREGROUND, 0x0, appRxBytesAfter, 7867488L);
        assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, rootRxBytesAfter, 647587L);
        assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, rootRxBytesAfter, 0L);

        NetworkStatsFactory.clearStackedIfaces();
    }
@@ -252,6 +254,15 @@ public class NetworkStatsFactoryTest {
        assertEquals("unexpected txBytes", txBytes, entry.txBytes);
    }

    private static void assertNoStatsEntry(NetworkStats stats, String iface, int uid, int set,
            int tag) {
        final int i = stats.findIndex(iface, uid, set, tag, METERED_NO, ROAMING_NO,
                DEFAULT_NETWORK_NO);
        if (i >= 0) {
            fail("unexpected NetworkStats entry at " + i);
        }
    }

    private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set,
            int tag, long rxBytes, long rxPackets, long txBytes, long txPackets) {
        final int i = stats.findIndex(iface, uid, set, tag, METERED_NO, ROAMING_NO,
+3 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ idx iface acct_tag_hex uid_tag_int cnt_set rx_bytes rx_packets tx_bytes tx_packe
7 v4-wlan0 0x0 10060 1 1448660 1041 31192 753 1448660 1041 0 0 0 0 31192 753 0 0 0 0
8 v4-wlan0 0x0 10102 0 9702 16 2870 23 9702 16 0 0 0 0 2870 23 0 0 0 0
9 v4-wlan0 0x0 10102 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
10 wlan0 0x0 0 0 11058671 7892 312046 5113 11043898 7811 13117 61 1656 20 306544 5046 3230 38 2272 29
10 wlan0 0x0 0 0 11058671 7892 0 0 11043898 7811 13117 61 1656 20 0 0 0 0 0 0
11 wlan0 0x0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
12 wlan0 0x0 1000 0 6126 13 2013 16 5934 11 192 2 0 0 1821 14 192 2 0 0
13 wlan0 0x0 1000 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
@@ -41,3 +41,5 @@ 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
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
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