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

Commit 3df7f596 authored by Paul Hu's avatar Paul Hu
Browse files

Adjust the timing of VPN type/profile/algorithms metrics collection

This adjustment is a prerequisite for a subsequent change that
will reset VPN metrics data, ensuring that these metrics are
consistently captured at the correct point in the VPN lifecycle.
This helps to prevent stale or incorrect data from being used in
metrics reporting, especially after a metrics reset.

Bug: 306313287
Test: atest FrameworksVpnTests
Flag: android.net.platform.flags.collect_vpn_metrics
Change-Id: Iaee4415caf738f9c57b769ec4d533004cc88b39a
parent 642ef43d
Loading
Loading
Loading
Loading
+14 −12
Original line number Diff line number Diff line
@@ -2299,10 +2299,10 @@ public class Vpn {
        return success;
    }

    private void setMtu(int mtu) {
    private void setMtuAndMetrics(int mtu) {
        synchronized (Vpn.this) {
            mConfig.mtu = mtu;
            if (isVpnMetricsLoggable()) {
            if (mVpnConnectivityMetrics != null) {
                mVpnConnectivityMetrics.setMtu(mtu);
            }
        }
@@ -2311,7 +2311,7 @@ public class Vpn {
    private void setUnderlyingNetworksAndMetrics(@NonNull Network[] networks) {
        synchronized (Vpn.this) {
            mConfig.underlyingNetworks = networks;
            if (isVpnMetricsLoggable()) {
            if (mVpnConnectivityMetrics != null) {
                mVpnConnectivityMetrics.setUnderlyingNetwork(mConfig.underlyingNetworks);
            }
        }
@@ -3002,7 +3002,7 @@ public class Vpn {
            // The update on VPN and the IPsec tunnel will be done when migration is fully complete
            // in onChildMigrated
            mIkeConnectionInfo = ikeConnectionInfo;
            if (isVpnMetricsLoggable()) {
            if (mVpnConnectivityMetrics != null) {
                mVpnConnectivityMetrics.setServerIpProtocol(ikeConnectionInfo.getRemoteAddress());
            }
        }
@@ -3068,13 +3068,20 @@ public class Vpn {
                    // Ignore stale runner.
                    if (mVpnRunner != this) return;

                    if (mVpnConnectivityMetrics != null) {
                        mVpnConnectivityMetrics.setVpnType(VpnManager.TYPE_VPN_PLATFORM);
                        mVpnConnectivityMetrics.setVpnProfileType(mProfile.toVpnProfile().type);
                        mVpnConnectivityMetrics.setAllowedAlgorithms(
                                mProfile.getAllowedAlgorithms());
                    }

                    mInterface = interfaceName;
                    setMtu(vpnMtu);
                    setMtuAndMetrics(vpnMtu);
                    mConfig.interfaze = mInterface;

                    mConfig.addresses.clear();
                    mConfig.addresses.addAll(internalAddresses);
                    if (isVpnMetricsLoggable()) {
                    if (mVpnConnectivityMetrics != null) {
                        mVpnConnectivityMetrics.setVpnNetworkIpProtocol(mConfig.addresses);
                    }

@@ -3179,7 +3186,7 @@ public class Vpn {
                    final LinkProperties oldLp = makeLinkProperties();

                    setUnderlyingNetworksAndMetrics(new Network[] {network});
                    setMtu(calculateVpnMtu());
                    setMtuAndMetrics(calculateVpnMtu());
                    final LinkProperties newLp = makeLinkProperties();

                    // If MTU is < 1280, IPv6 addresses will be removed. If there are no addresses
@@ -4284,11 +4291,6 @@ public class Vpn {
            config.allowBypass = profile.isBypassable;
            config.disallowedApplications = getAppExclusionList(mPackage);
            mConfig = config;
            if (mVpnConnectivityMetrics != null) {
                mVpnConnectivityMetrics.setVpnType(VpnManager.TYPE_VPN_PLATFORM);
                mVpnConnectivityMetrics.setVpnProfileType(profile.type);
                mVpnConnectivityMetrics.setAllowedAlgorithms(profile.getAllowedAlgorithms());
            }

            switch (profile.type) {
                case VpnProfile.TYPE_IKEV2_IPSEC_USER_PASS:
+7 −7
Original line number Diff line number Diff line
@@ -2063,13 +2063,6 @@ public class VpnTest extends VpnTestBase {
    private Vpn startLegacyVpn(final Vpn vpn, final VpnProfile vpnProfile) throws Exception {
        setMockedUsers(PRIMARY_USER);
        vpn.startLegacyVpn(vpnProfile);
        if (vpnProfile.type == VpnProfile.TYPE_IKEV2_IPSEC_USER_PASS
                || vpnProfile.type == VpnProfile.TYPE_IKEV2_IPSEC_PSK) {
            verify(mVpnConnectivityMetrics).setAllowedAlgorithms(
                    Ikev2VpnProfile.DEFAULT_ALGORITHMS);
        }
        verify(mVpnConnectivityMetrics).setVpnType(VpnManager.TYPE_VPN_PLATFORM);
        verify(mVpnConnectivityMetrics).setVpnProfileType(vpnProfile.type);
        return vpn;
    }

@@ -2220,6 +2213,13 @@ public class VpnTest extends VpnTestBase {
                any(), any(), anyString(), ncCaptor.capture(), lpCaptor.capture(),
                any(), nacCaptor.capture(), any(), any());
        verify(mIkeSessionWrapper).setUnderpinnedNetwork(TEST_NETWORK);
        if (vpnProfile.type == VpnProfile.TYPE_IKEV2_IPSEC_USER_PASS
                || vpnProfile.type == VpnProfile.TYPE_IKEV2_IPSEC_PSK) {
            verify(mVpnConnectivityMetrics).setAllowedAlgorithms(
                    Ikev2VpnProfile.DEFAULT_ALGORITHMS);
        }
        verify(mVpnConnectivityMetrics).setVpnType(VpnManager.TYPE_VPN_PLATFORM);
        verify(mVpnConnectivityMetrics).setVpnProfileType(vpnProfile.type);
        verify(mVpnConnectivityMetrics).setUnderlyingNetwork(any());
        verify(mVpnConnectivityMetrics).setVpnNetworkIpProtocol(argThat(addresses ->
                CollectionUtils.all(List.of(