Merge "[SP32] Try to get 1.1 OffloadControl hardware if available"

This commit is contained in:
Junyu Lai
2021-06-22 20:31:18 +00:00
committed by Gerrit Code Review
6 changed files with 123 additions and 39 deletions

View File

@@ -26,6 +26,7 @@ import static android.net.NetworkStats.UID_TETHERING;
import static android.net.netstats.provider.NetworkStatsProvider.QUOTA_UNLIMITED;
import static android.provider.Settings.Global.TETHER_OFFLOAD_DISABLED;
import static com.android.networkstack.tethering.OffloadHardwareInterface.OFFLOAD_HAL_VERSION_NONE;
import static com.android.networkstack.tethering.TetheringConfiguration.DEFAULT_TETHER_OFFLOAD_POLL_INTERVAL_MS;
import android.annotation.NonNull;
@@ -96,7 +97,8 @@ public class OffloadController {
private final SharedLog mLog;
private final HashMap<String, LinkProperties> mDownstreams;
private boolean mConfigInitialized;
private boolean mControlInitialized;
@OffloadHardwareInterface.OffloadHalVersion
private int mControlHalVersion;
private LinkProperties mUpstreamLinkProperties;
// The complete set of offload-exempt prefixes passed in via Tethering from
// all upstream and downstream sources.
@@ -179,7 +181,7 @@ public class OffloadController {
}
}
mControlInitialized = mHwInterface.initOffloadControl(
mControlHalVersion = mHwInterface.initOffloadControl(
// OffloadHardwareInterface guarantees that these callback
// methods are called on the handler passed to it, which is the
// same as mHandler, as coordinated by the setup in Tethering.
@@ -278,7 +280,7 @@ public class OffloadController {
updateStatsForCurrentUpstream();
mUpstreamLinkProperties = null;
mHwInterface.stopOffloadControl();
mControlInitialized = false;
mControlHalVersion = OFFLOAD_HAL_VERSION_NONE;
mConfigInitialized = false;
if (mHandler.hasCallbacks(mScheduledPollingTask)) {
mHandler.removeCallbacks(mScheduledPollingTask);
@@ -287,7 +289,7 @@ public class OffloadController {
}
private boolean started() {
return mConfigInitialized && mControlInitialized;
return mConfigInitialized && mControlHalVersion != OFFLOAD_HAL_VERSION_NONE;
}
@VisibleForTesting
@@ -696,6 +698,8 @@ public class OffloadController {
}
final boolean isStarted = started();
pw.println("Offload HALs " + (isStarted ? "started" : "not started"));
pw.println("Offload Control HAL version: "
+ OffloadHardwareInterface.halVerToString(mControlHalVersion));
LinkProperties lp = mUpstreamLinkProperties;
String upstream = (lp != null) ? lp.getInterfaceName() : null;
pw.println("Current upstream: " + upstream);

View File

@@ -20,6 +20,7 @@ import static android.net.netlink.StructNlMsgHdr.NLM_F_DUMP;
import static android.net.netlink.StructNlMsgHdr.NLM_F_REQUEST;
import static android.net.util.TetheringUtils.uint16;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.hardware.tetheroffload.config.V1_0.IOffloadConfig;
import android.hardware.tetheroffload.control.V1_0.IOffloadControl;
@@ -38,12 +39,15 @@ import android.os.RemoteException;
import android.system.ErrnoException;
import android.system.Os;
import android.system.OsConstants;
import android.util.Pair;
import com.android.internal.annotations.VisibleForTesting;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.ByteBuffer;
@@ -82,6 +86,37 @@ public class OffloadHardwareInterface {
private final SharedLog mLog;
private final Dependencies mDeps;
private IOffloadControl mOffloadControl;
// TODO: Use major-minor version control to prevent from defining new constants.
static final int OFFLOAD_HAL_VERSION_NONE = 0;
static final int OFFLOAD_HAL_VERSION_1_0 = 1;
static final int OFFLOAD_HAL_VERSION_1_1 = 2;
/** @hide */
@Retention(RetentionPolicy.SOURCE)
@IntDef(prefix = "OFFLOAD_HAL_VERSION_", value = {
OFFLOAD_HAL_VERSION_NONE,
OFFLOAD_HAL_VERSION_1_0,
OFFLOAD_HAL_VERSION_1_1
})
public @interface OffloadHalVersion {}
@OffloadHalVersion
private int mOffloadControlVersion = OFFLOAD_HAL_VERSION_NONE;
@NonNull
static String halVerToString(int version) {
switch(version) {
case OFFLOAD_HAL_VERSION_1_0:
return "1.0";
case OFFLOAD_HAL_VERSION_1_1:
return "1.1";
case OFFLOAD_HAL_VERSION_NONE:
return "None";
default:
throw new IllegalArgumentException("Unsupported version int " + version);
}
}
private TetheringOffloadCallback mTetheringOffloadCallback;
private ControlCallback mControlCallback;
@@ -167,13 +202,30 @@ public class OffloadHardwareInterface {
}
}
public IOffloadControl getOffloadControl() {
@NonNull
public Pair<IOffloadControl, Integer> getOffloadControl() {
IOffloadControl hal = null;
int version = OFFLOAD_HAL_VERSION_NONE;
try {
return IOffloadControl.getService(true /*retry*/);
} catch (RemoteException | NoSuchElementException e) {
mLog.e("tethering offload control not supported: " + e);
return null;
hal = android.hardware.tetheroffload.control
.V1_1.IOffloadControl.getService(true /*retry*/);
version = OFFLOAD_HAL_VERSION_1_1;
} catch (NoSuchElementException e) {
// Unsupported by device.
} catch (RemoteException e) {
mLog.e("Unable to get offload control " + OFFLOAD_HAL_VERSION_1_1);
}
if (hal == null) {
try {
hal = IOffloadControl.getService(true /*retry*/);
version = OFFLOAD_HAL_VERSION_1_0;
} catch (NoSuchElementException e) {
// Unsupported by device.
} catch (RemoteException e) {
mLog.e("Unable to get offload control " + OFFLOAD_HAL_VERSION_1_0);
}
}
return new Pair<IOffloadControl, Integer>(hal, version);
}
public NativeHandle createConntrackSocket(final int groups) {
@@ -304,16 +356,25 @@ public class OffloadHardwareInterface {
}
}
/** Initialize the tethering offload HAL. */
public boolean initOffloadControl(ControlCallback controlCb) {
/**
* Initialize the tethering offload HAL.
*
* @return one of {@code OFFLOAD_HAL_VERSION_*} represents the HAL version, or
* {@link #OFFLOAD_HAL_VERSION_NONE} if failed.
*/
public int initOffloadControl(ControlCallback controlCb) {
mControlCallback = controlCb;
if (mOffloadControl == null) {
mOffloadControl = mDeps.getOffloadControl();
final Pair<IOffloadControl, Integer> halAndVersion = mDeps.getOffloadControl();
mOffloadControl = halAndVersion.first;
mOffloadControlVersion = halAndVersion.second;
if (mOffloadControl == null) {
mLog.e("tethering IOffloadControl.getService() returned null");
return false;
return OFFLOAD_HAL_VERSION_NONE;
}
mLog.i("tethering offload control version "
+ halVerToString(mOffloadControlVersion) + " is supported.");
}
final String logmsg = String.format("initOffloadControl(%s)",
@@ -331,11 +392,11 @@ public class OffloadHardwareInterface {
});
} catch (RemoteException e) {
record(logmsg, e);
return false;
return OFFLOAD_HAL_VERSION_NONE;
}
record(logmsg, results);
return results.mSuccess;
return results.mSuccess ? mOffloadControlVersion : OFFLOAD_HAL_VERSION_NONE;
}
/** Stop IOffloadControl. */