Merge "Separate events usage for starting socket keepalives"

This commit is contained in:
Chiachang Wang
2023-04-17 02:32:00 +00:00
committed by Gerrit Code Review
4 changed files with 50 additions and 41 deletions

View File

@@ -281,9 +281,8 @@ public abstract class NetworkAgent {
* *
* arg1 = the hardware slot number of the keepalive to start * arg1 = the hardware slot number of the keepalive to start
* arg2 = interval in seconds * arg2 = interval in seconds
* obj = AutomaticKeepaliveInfo object * obj = KeepalivePacketData object describing the data to be sent
* *
* Also used internally by ConnectivityService / KeepaliveTracker, with different semantics.
* @hide * @hide
*/ */
public static final int CMD_START_SOCKET_KEEPALIVE = BASE + 11; public static final int CMD_START_SOCKET_KEEPALIVE = BASE + 11;
@@ -435,6 +434,14 @@ public abstract class NetworkAgent {
*/ */
public static final int CMD_DSCP_POLICY_STATUS = BASE + 28; public static final int CMD_DSCP_POLICY_STATUS = BASE + 28;
/**
* Sent by the NetworkAgent to ConnectivityService to notify that this network is expected to be
* replaced within the specified time by a similar network.
* arg1 = timeout in milliseconds
* @hide
*/
public static final int EVENT_UNREGISTER_AFTER_REPLACEMENT = BASE + 29;
/** /**
* DSCP policy was successfully added. * DSCP policy was successfully added.
*/ */
@@ -477,27 +484,6 @@ public abstract class NetworkAgent {
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface DscpPolicyStatus {} public @interface DscpPolicyStatus {}
/**
* Sent by the NetworkAgent to ConnectivityService to notify that this network is expected to be
* replaced within the specified time by a similar network.
* arg1 = timeout in milliseconds
* @hide
*/
public static final int EVENT_UNREGISTER_AFTER_REPLACEMENT = BASE + 29;
/**
* Sent by AutomaticOnOffKeepaliveTracker periodically (when relevant) to trigger monitor
* automatic keepalive request.
*
* NATT keepalives have an automatic mode where the system only sends keepalive packets when
* TCP sockets are open over a VPN. The system will check periodically for presence of
* such open sockets, and this message is what triggers the re-evaluation.
*
* obj = A Binder object associated with the keepalive.
* @hide
*/
public static final int CMD_MONITOR_AUTOMATIC_KEEPALIVE = BASE + 30;
private static NetworkInfo getLegacyNetworkInfo(final NetworkAgentConfig config) { private static NetworkInfo getLegacyNetworkInfo(final NetworkAgentConfig config) {
final NetworkInfo ni = new NetworkInfo(config.legacyType, config.legacySubType, final NetworkInfo ni = new NetworkInfo(config.legacyType, config.legacySubType,
config.legacyTypeName, config.legacySubTypeName); config.legacyTypeName, config.legacySubTypeName);

View File

@@ -464,7 +464,11 @@ public class ConnectivityService extends IConnectivityManager.Stub
private String mCurrentTcpBufferSizes; private String mCurrentTcpBufferSizes;
private static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames( private static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames(
new Class[] { ConnectivityService.class, NetworkAgent.class, NetworkAgentInfo.class }); new Class[] {
ConnectivityService.class,
NetworkAgent.class,
NetworkAgentInfo.class,
AutomaticOnOffKeepaliveTracker.class });
private enum ReapUnvalidatedNetworks { private enum ReapUnvalidatedNetworks {
// Tear down networks that have no chance (e.g. even if validated) of becoming // Tear down networks that have no chance (e.g. even if validated) of becoming
@@ -5598,12 +5602,13 @@ public class ConnectivityService extends IConnectivityManager.Stub
handleConfigureAlwaysOnNetworks(); handleConfigureAlwaysOnNetworks();
break; break;
} }
// Sent by KeepaliveTracker to process an app request on the state machine thread. // Sent by AutomaticOnOffKeepaliveTracker to process an app request on the
case NetworkAgent.CMD_START_SOCKET_KEEPALIVE: { // handler thread.
case AutomaticOnOffKeepaliveTracker.CMD_REQUEST_START_KEEPALIVE: {
mKeepaliveTracker.handleStartKeepalive(msg); mKeepaliveTracker.handleStartKeepalive(msg);
break; break;
} }
case NetworkAgent.CMD_MONITOR_AUTOMATIC_KEEPALIVE: { case AutomaticOnOffKeepaliveTracker.CMD_MONITOR_AUTOMATIC_KEEPALIVE: {
final AutomaticOnOffKeepalive ki = final AutomaticOnOffKeepalive ki =
mKeepaliveTracker.getKeepaliveForBinder((IBinder) msg.obj); mKeepaliveTracker.getKeepaliveForBinder((IBinder) msg.obj);
if (null == ki) return; // The callback was unregistered before the alarm fired if (null == ki) return; // The callback was unregistered before the alarm fired

View File

@@ -16,7 +16,6 @@
package com.android.server.connectivity; package com.android.server.connectivity;
import static android.net.NetworkAgent.CMD_START_SOCKET_KEEPALIVE;
import static android.net.SocketKeepalive.ERROR_INVALID_SOCKET; import static android.net.SocketKeepalive.ERROR_INVALID_SOCKET;
import static android.net.SocketKeepalive.MIN_INTERVAL_SEC; import static android.net.SocketKeepalive.MIN_INTERVAL_SEC;
import static android.net.SocketKeepalive.SUCCESS_PAUSED; import static android.net.SocketKeepalive.SUCCESS_PAUSED;
@@ -40,7 +39,6 @@ import android.net.INetd;
import android.net.ISocketKeepaliveCallback; import android.net.ISocketKeepaliveCallback;
import android.net.MarkMaskParcel; import android.net.MarkMaskParcel;
import android.net.Network; import android.net.Network;
import android.net.NetworkAgent;
import android.net.SocketKeepalive.InvalidSocketException; import android.net.SocketKeepalive.InvalidSocketException;
import android.os.FileUtils; import android.os.FileUtils;
import android.os.Handler; import android.os.Handler;
@@ -94,6 +92,29 @@ public class AutomaticOnOffKeepaliveTracker {
private static final int ADJUST_TCP_POLLING_DELAY_MS = 2000; private static final int ADJUST_TCP_POLLING_DELAY_MS = 2000;
private static final String AUTOMATIC_ON_OFF_KEEPALIVE_VERSION = private static final String AUTOMATIC_ON_OFF_KEEPALIVE_VERSION =
"automatic_on_off_keepalive_version"; "automatic_on_off_keepalive_version";
// ConnectivityService parses message constants from itself and AutomaticOnOffKeepaliveTracker
// with MessageUtils for debugging purposes, and crashes if some messages have the same values.
private static final int BASE = 2000;
/**
* Sent by AutomaticOnOffKeepaliveTracker periodically (when relevant) to trigger monitor
* automatic keepalive request.
*
* NATT keepalives have an automatic mode where the system only sends keepalive packets when
* TCP sockets are open over a VPN. The system will check periodically for presence of
* such open sockets, and this message is what triggers the re-evaluation.
*
* obj = A Binder object associated with the keepalive.
*/
public static final int CMD_MONITOR_AUTOMATIC_KEEPALIVE = BASE + 1;
/**
* Sent by AutomaticOnOffKeepaliveTracker to ConnectivityService to start a keepalive.
*
* obj = AutomaticKeepaliveInfo object
*/
public static final int CMD_REQUEST_START_KEEPALIVE = BASE + 2;
/** /**
* States for {@code #AutomaticOnOffKeepalive}. * States for {@code #AutomaticOnOffKeepalive}.
* *
@@ -202,7 +223,7 @@ public class AutomaticOnOffKeepaliveTracker {
throw new InvalidSocketException(ERROR_INVALID_SOCKET, e); throw new InvalidSocketException(ERROR_INVALID_SOCKET, e);
} }
mAlarmListener = () -> mConnectivityServiceHandler.obtainMessage( mAlarmListener = () -> mConnectivityServiceHandler.obtainMessage(
NetworkAgent.CMD_MONITOR_AUTOMATIC_KEEPALIVE, mCallback.asBinder()) CMD_MONITOR_AUTOMATIC_KEEPALIVE, mCallback.asBinder())
.sendToTarget(); .sendToTarget();
} else { } else {
mAutomaticOnOffState = STATE_ALWAYS_ON; mAutomaticOnOffState = STATE_ALWAYS_ON;
@@ -482,9 +503,8 @@ public class AutomaticOnOffKeepaliveTracker {
+ "" + dstAddrString + ":" + dstPort + "" + dstAddrString + ":" + dstPort
+ " auto=" + autoKi + " auto=" + autoKi
+ " underpinned=" + underpinnedNetwork); + " underpinned=" + underpinnedNetwork);
mConnectivityServiceHandler.obtainMessage(NetworkAgent.CMD_START_SOCKET_KEEPALIVE, mConnectivityServiceHandler.obtainMessage(CMD_REQUEST_START_KEEPALIVE, autoKi)
// TODO : move ConnectivityService#encodeBool to a static lib. .sendToTarget();
automaticOnOffKeepalives ? 1 : 0, 0, autoKi).sendToTarget();
} catch (InvalidSocketException e) { } catch (InvalidSocketException e) {
mKeepaliveTracker.notifyErrorCallback(cb, e.error); mKeepaliveTracker.notifyErrorCallback(cb, e.error);
} }
@@ -517,9 +537,8 @@ public class AutomaticOnOffKeepaliveTracker {
+ "" + dstAddrString + ":" + dstPort + "" + dstAddrString + ":" + dstPort
+ " auto=" + autoKi + " auto=" + autoKi
+ " underpinned=" + underpinnedNetwork); + " underpinned=" + underpinnedNetwork);
mConnectivityServiceHandler.obtainMessage(NetworkAgent.CMD_START_SOCKET_KEEPALIVE, mConnectivityServiceHandler.obtainMessage(CMD_REQUEST_START_KEEPALIVE, autoKi)
// TODO : move ConnectivityService#encodeBool to a static lib. .sendToTarget();
automaticOnOffKeepalives ? 1 : 0, 0, autoKi).sendToTarget();
} catch (InvalidSocketException e) { } catch (InvalidSocketException e) {
mKeepaliveTracker.notifyErrorCallback(cb, e.error); mKeepaliveTracker.notifyErrorCallback(cb, e.error);
} }
@@ -547,7 +566,7 @@ public class AutomaticOnOffKeepaliveTracker {
final AutomaticOnOffKeepalive autoKi = new AutomaticOnOffKeepalive(ki, final AutomaticOnOffKeepalive autoKi = new AutomaticOnOffKeepalive(ki,
false /* autoOnOff, tcp keepalives are never auto on/off */, false /* autoOnOff, tcp keepalives are never auto on/off */,
null /* underpinnedNetwork, tcp keepalives do not refer to this */); null /* underpinnedNetwork, tcp keepalives do not refer to this */);
mConnectivityServiceHandler.obtainMessage(CMD_START_SOCKET_KEEPALIVE, autoKi) mConnectivityServiceHandler.obtainMessage(CMD_REQUEST_START_KEEPALIVE, autoKi)
.sendToTarget(); .sendToTarget();
} catch (InvalidSocketException e) { } catch (InvalidSocketException e) {
mKeepaliveTracker.notifyErrorCallback(cb, e.error); mKeepaliveTracker.notifyErrorCallback(cb, e.error);

View File

@@ -47,7 +47,6 @@ import android.net.LinkProperties;
import android.net.MarkMaskParcel; import android.net.MarkMaskParcel;
import android.net.NattKeepalivePacketData; import android.net.NattKeepalivePacketData;
import android.net.Network; import android.net.Network;
import android.net.NetworkAgent;
import android.net.NetworkCapabilities; import android.net.NetworkCapabilities;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.os.Binder; import android.os.Binder;
@@ -268,11 +267,11 @@ public class AutomaticOnOffKeepaliveTrackerTest {
@Override @Override
public void handleMessage(@NonNull final Message msg) { public void handleMessage(@NonNull final Message msg) {
switch (msg.what) { switch (msg.what) {
case NetworkAgent.CMD_START_SOCKET_KEEPALIVE: case AutomaticOnOffKeepaliveTracker.CMD_REQUEST_START_KEEPALIVE:
Log.d(TAG, "Test handler received CMD_START_SOCKET_KEEPALIVE : " + msg); Log.d(TAG, "Test handler received CMD_REQUEST_START_KEEPALIVE : " + msg);
mAOOKeepaliveTracker.handleStartKeepalive(msg); mAOOKeepaliveTracker.handleStartKeepalive(msg);
break; break;
case NetworkAgent.CMD_MONITOR_AUTOMATIC_KEEPALIVE: case AutomaticOnOffKeepaliveTracker.CMD_MONITOR_AUTOMATIC_KEEPALIVE:
Log.d(TAG, "Test handler received CMD_MONITOR_AUTOMATIC_KEEPALIVE : " + msg); Log.d(TAG, "Test handler received CMD_MONITOR_AUTOMATIC_KEEPALIVE : " + msg);
mLastAutoKi = mAOOKeepaliveTracker.getKeepaliveForBinder((IBinder) msg.obj); mLastAutoKi = mAOOKeepaliveTracker.getKeepaliveForBinder((IBinder) msg.obj);
break; break;