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

Commit e2686fec authored by Paul Hu's avatar Paul Hu
Browse files

Log VPN service events

Log VPN service events for connection establishment to collect
VPN metrics.

Flag: android.net.platform.flags.collect_vpn_metrics
Test: atest FrameworksVpnTests
Test: Switch between platform VPN and 3rd-party VPN manually.
Change-Id: I72ffa95ce9e5a8f29e15d4d6d330484a65fd08bd
parent 0e1e20c9
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -711,10 +711,6 @@ public class Vpn {
        return mEnableTeardown;
    }

    private boolean isVpnMetricsLoggable() {
        return mVpnConnectivityMetrics != null && mVpnConnectivityMetrics.isPlatformVpn();
    }

    /**
     * Update current state, dispatching event to listeners.
     */
@@ -735,7 +731,7 @@ public class Vpn {
            case CONNECTED:
                if (null != mNetworkAgent) {
                    mNetworkAgent.markConnected();
                    if (isVpnMetricsLoggable()) {
                    if (mVpnConnectivityMetrics != null) {
                        mVpnConnectivityMetrics.notifyVpnConnected();
                    }
                }
@@ -745,7 +741,7 @@ public class Vpn {
                if (null != mNetworkAgent) {
                    mNetworkAgent.unregister();
                    mNetworkAgent = null;
                    if (isVpnMetricsLoggable()) {
                    if (mVpnConnectivityMetrics != null) {
                        mVpnConnectivityMetrics.notifyVpnDisconnected();
                        // Clear the metrics since the NetworkAgent is disconnected.
                        mVpnConnectivityMetrics.resetMetrics();
@@ -1818,6 +1814,10 @@ public class Vpn {
            config.interfaze = mInterface;
            config.startTime = SystemClock.elapsedRealtime();
            mConfig = config;
            // Log VPN service events for connection establishment
            if (mVpnConnectivityMetrics != null) {
                mVpnConnectivityMetrics.setVpnType(VpnManager.TYPE_VPN_SERVICE);
            }

            // Set up forwarding and DNS rules.
            // First attempt to do a seamless handover that only changes the interface name and
+0 −13
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import static android.net.IpSecAlgorithm.AUTH_HMAC_SHA512;
import static android.net.IpSecAlgorithm.CRYPT_AES_CBC;
import static android.net.IpSecAlgorithm.CRYPT_AES_CTR;
import static android.net.VpnManager.TYPE_VPN_OEM;
import static android.net.VpnManager.TYPE_VPN_PLATFORM;

import android.net.ConnectivityManager;
import android.net.LinkAddress;
@@ -390,18 +389,6 @@ public class VpnConnectivityMetrics {
        return ipProtocol;
    }

    /**
     * Checks if the VPN associated with these metrics is a platform-managed VPN.
     * The determination is based on the internal {@code mVpnType} field, which
     * should be set during the VPN's configuration.
     *
     * @return {@code true} if the VPN type matches {@code TYPE_VPN_PLATFORM};
     *         {@code false} otherwise.
     */
    public boolean isPlatformVpn() {
        return mVpnType == TYPE_VPN_PLATFORM;
    }

    /**
     * Validates and corrects the internal VPN metrics to ensure the collected data fall within
     * acceptable ranges.
+2 −1
Original line number Diff line number Diff line
@@ -1517,6 +1517,7 @@ public class VpnTest extends VpnTestBase {
                eq(TEST_VPN_PKG),
                eq(null) /* attributionTag */,
                eq(null) /* message */);
        verify(mVpnConnectivityMetrics, times(1)).setVpnType(VpnManager.TYPE_VPN_SERVICE);
        // Call establish() twice with the same config, it should match seamless handover case and
        // startOp() shouldn't be called again.
        vpn.establish(config);
@@ -1526,6 +1527,7 @@ public class VpnTest extends VpnTestBase {
                eq(TEST_VPN_PKG),
                eq(null) /* attributionTag */,
                eq(null) /* message */);
        verify(mVpnConnectivityMetrics, times(2)).setVpnType(VpnManager.TYPE_VPN_SERVICE);
    }

    private void verifyVpnManagerEvent(String sessionKey, String category, int errorClass,
@@ -3336,7 +3338,6 @@ public class VpnTest extends VpnTestBase {
        verify(mConnectivityManager, times(1)).registerNetworkProvider(argThat(
                provider -> provider.getName().contains("VpnNetworkProvider")
        ));
        doReturn(true).when(mVpnConnectivityMetrics).isPlatformVpn();
        return vpn;
    }