Merge "Add test for OffloadHardwareInterface" am: 37b52321ee am: 9f6a0496d2

Change-Id: I0e6939d5983c87f6e47d9f4cb398aa2e2ef00818
This commit is contained in:
Mark Chien
2020-05-13 09:13:59 +00:00
committed by Automerger Merge Worker
2 changed files with 288 additions and 51 deletions

View File

@@ -66,6 +66,7 @@ public class OffloadHardwareInterface {
private final Handler mHandler;
private final SharedLog mLog;
private final Dependencies mDeps;
private IOffloadControl mOffloadControl;
private TetheringOffloadCallback mTetheringOffloadCallback;
private ControlCallback mControlCallback;
@@ -126,8 +127,76 @@ public class OffloadHardwareInterface {
}
public OffloadHardwareInterface(Handler h, SharedLog log) {
this(h, log, new Dependencies(log));
}
OffloadHardwareInterface(Handler h, SharedLog log, Dependencies deps) {
mHandler = h;
mLog = log.forSubComponent(TAG);
mDeps = deps;
}
/** Capture OffloadHardwareInterface dependencies, for injection. */
static class Dependencies {
private final SharedLog mLog;
Dependencies(SharedLog log) {
mLog = log;
}
public IOffloadConfig getOffloadConfig() {
try {
return IOffloadConfig.getService(true /*retry*/);
} catch (RemoteException | NoSuchElementException e) {
mLog.e("getIOffloadConfig error " + e);
return null;
}
}
public IOffloadControl getOffloadControl() {
try {
return IOffloadControl.getService(true /*retry*/);
} catch (RemoteException | NoSuchElementException e) {
mLog.e("tethering offload control not supported: " + e);
return null;
}
}
public NativeHandle createConntrackSocket(final int groups) {
final FileDescriptor fd;
try {
fd = NetlinkSocket.forProto(OsConstants.NETLINK_NETFILTER);
} catch (ErrnoException e) {
mLog.e("Unable to create conntrack socket " + e);
return null;
}
final SocketAddress sockAddr = SocketUtils.makeNetlinkSocketAddress(0, groups);
try {
Os.bind(fd, sockAddr);
} catch (ErrnoException | SocketException e) {
mLog.e("Unable to bind conntrack socket for groups " + groups + " error: " + e);
try {
SocketUtils.closeSocket(fd);
} catch (IOException ie) {
// Nothing we can do here
}
return null;
}
try {
Os.connect(fd, sockAddr);
} catch (ErrnoException | SocketException e) {
mLog.e("connect to kernel fail for groups " + groups + " error: " + e);
try {
SocketUtils.closeSocket(fd);
} catch (IOException ie) {
// Nothing we can do here
}
return null;
}
return new NativeHandle(fd, true);
}
}
/** Get default value indicating whether offload is supported. */
@@ -141,13 +210,7 @@ public class OffloadHardwareInterface {
* share them with offload management process.
*/
public boolean initOffloadConfig() {
IOffloadConfig offloadConfig;
try {
offloadConfig = IOffloadConfig.getService(true /*retry*/);
} catch (RemoteException | NoSuchElementException e) {
mLog.e("getIOffloadConfig error " + e);
return false;
}
final IOffloadConfig offloadConfig = mDeps.getOffloadConfig();
if (offloadConfig == null) {
mLog.e("Could not find IOffloadConfig service");
return false;
@@ -159,11 +222,11 @@ public class OffloadHardwareInterface {
//
// h2 provides a file descriptor bound to the following netlink groups
// (NF_NETLINK_CONNTRACK_UPDATE | NF_NETLINK_CONNTRACK_DESTROY).
final NativeHandle h1 = createConntrackSocket(
final NativeHandle h1 = mDeps.createConntrackSocket(
NF_NETLINK_CONNTRACK_NEW | NF_NETLINK_CONNTRACK_DESTROY);
if (h1 == null) return false;
final NativeHandle h2 = createConntrackSocket(
final NativeHandle h2 = mDeps.createConntrackSocket(
NF_NETLINK_CONNTRACK_UPDATE | NF_NETLINK_CONNTRACK_DESTROY);
if (h2 == null) {
closeFdInNativeHandle(h1);
@@ -198,53 +261,12 @@ public class OffloadHardwareInterface {
}
}
private NativeHandle createConntrackSocket(final int groups) {
FileDescriptor fd;
try {
fd = NetlinkSocket.forProto(OsConstants.NETLINK_NETFILTER);
} catch (ErrnoException e) {
mLog.e("Unable to create conntrack socket " + e);
return null;
}
final SocketAddress sockAddr = SocketUtils.makeNetlinkSocketAddress(0, groups);
try {
Os.bind(fd, sockAddr);
} catch (ErrnoException | SocketException e) {
mLog.e("Unable to bind conntrack socket for groups " + groups + " error: " + e);
try {
SocketUtils.closeSocket(fd);
} catch (IOException ie) {
// Nothing we can do here
}
return null;
}
try {
Os.connect(fd, sockAddr);
} catch (ErrnoException | SocketException e) {
mLog.e("connect to kernel fail for groups " + groups + " error: " + e);
try {
SocketUtils.closeSocket(fd);
} catch (IOException ie) {
// Nothing we can do here
}
return null;
}
return new NativeHandle(fd, true);
}
/** Initialize the tethering offload HAL. */
public boolean initOffloadControl(ControlCallback controlCb) {
mControlCallback = controlCb;
if (mOffloadControl == null) {
try {
mOffloadControl = IOffloadControl.getService(true /*retry*/);
} catch (RemoteException | NoSuchElementException e) {
mLog.e("tethering offload control not supported: " + e);
return false;
}
mOffloadControl = mDeps.getOffloadControl();
if (mOffloadControl == null) {
mLog.e("tethering IOffloadControl.getService() returned null");
return false;