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

Commit 77c17be6 authored by markchien's avatar markchien
Browse files

[Tether06] Migrate tether offload controller into module

The tether offload JNI library in this patch still have many dependencies
with internal libraries. Will have follow up changes to cut the
dependencies to let it be a unbundled library.

Bug: 136040414
Test: -build, flash, boot
      -atest TetheringTests
      -atest FrameworksNetTests

Change-Id: Iacf8e0b94135e35672de3ee77c474ee39a00c591
parent 0549e43f
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ java_defaults {
        "androidx.annotation_annotation",
        "netd_aidl_interface-java",
        "networkstack-aidl-interfaces-java",
        "android.hardware.tetheroffload.control-V1.0-java",
        "tethering-client",
    ],
    manifest: "AndroidManifestBase.xml",
@@ -38,11 +39,39 @@ android_library {
    defaults: ["TetheringAndroidLibraryDefaults"],
}

cc_library_shared {
    name: "libtetheroffloadjni",
    srcs: [
        "jni/com_android_server_connectivity_tethering_OffloadHardwareInterface.cpp",
    ],
    shared_libs: [
        "libnativehelper",
        "libcutils",
        "android.hardware.tetheroffload.config@1.0",
    ],
    static_libs: [
        "liblog",
        "libbase",
        "libhidlbase",
        "libutils",
    ],

    cflags: [
        "-Wall",
        "-Werror",
        "-Wno-unused-parameter",
        "-Wthread-safety",
    ],
}

// Common defaults for compiling the actual APK.
java_defaults {
    name: "TetheringAppDefaults",
    platform_apis: true,
    privileged: true,
    jni_libs: [
        "libtetheroffloadjni",
    ],
    resource_dirs: [
        "res",
    ],
@@ -71,6 +100,8 @@ filegroup {
    name: "tethering-servicescore-srcs",
    srcs: [
        "src/com/android/server/connectivity/tethering/EntitlementManager.java",
        "src/com/android/server/connectivity/tethering/OffloadController.java",
        "src/com/android/server/connectivity/tethering/OffloadHardwareInterface.java",
        "src/com/android/server/connectivity/tethering/TetheringConfiguration.java",
        "src/com/android/server/connectivity/tethering/UpstreamNetworkMonitor.java",
    ],
@@ -88,3 +119,11 @@ filegroup {
        "src/android/net/util/PrefixUtils.java",
    ],
}

// This group would be removed when tethering migration is done.
filegroup {
    name: "tethering-jni-srcs",
    srcs: [
        "jni/com_android_server_connectivity_tethering_OffloadHardwareInterface.cpp",
    ],
}
+0 −0

File moved.

+14 −6
Original line number Diff line number Diff line
@@ -36,8 +36,8 @@ import android.net.netlink.NetlinkSocket;
import android.net.util.IpUtils;
import android.net.util.SharedLog;
import android.os.Handler;
import android.os.Looper;
import android.os.INetworkManagementService;
import android.os.Looper;
import android.os.RemoteException;
import android.os.SystemClock;
import android.provider.Settings;
@@ -60,7 +60,6 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * A class to encapsulate the business logic of programming the tethering
@@ -74,7 +73,7 @@ public class OffloadController {
    private static final String ANYIP = "0.0.0.0";
    private static final ForwardedStats EMPTY_STATS = new ForwardedStats();

    private static enum UpdateType { IF_NEEDED, FORCE };
    private enum UpdateType { IF_NEEDED, FORCE };

    private final Handler mHandler;
    private final OffloadHardwareInterface mHwInterface;
@@ -128,6 +127,7 @@ public class OffloadController {
        }
    }

    /** Start hardware offload. */
    public boolean start() {
        if (started()) return true;

@@ -235,6 +235,7 @@ public class OffloadController {
        return isStarted;
    }

    /** Stop hardware offload. */
    public void stop() {
        // Completely stops tethering offload. After this method is called, it is no longer safe to
        // call any HAL method, no callbacks from the hardware will be delivered, and any in-flight
@@ -258,7 +259,9 @@ public class OffloadController {
            // getTetherStats() is the only function in OffloadController that can be called from
            // a different thread. Do not attempt to update stats by querying the offload HAL
            // synchronously from a different thread than our Handler thread. http://b/64771555.
            Runnable updateStats = () -> { updateStatsForCurrentUpstream(); };
            Runnable updateStats = () -> {
                updateStatsForCurrentUpstream();
            };
            if (Looper.myLooper() == mHandler.getLooper()) {
                updateStats.run();
            } else {
@@ -358,6 +361,7 @@ public class OffloadController {
        }
    }

    /** Set current tethering upstream LinkProperties. */
    public void setUpstreamLinkProperties(LinkProperties lp) {
        if (!started() || Objects.equals(mUpstreamLinkProperties, lp)) return;

@@ -376,6 +380,7 @@ public class OffloadController {
        pushUpstreamParameters(prevUpstream);
    }

    /** Set local prefixes. */
    public void setLocalPrefixes(Set<IpPrefix> localPrefixes) {
        mExemptPrefixes = localPrefixes;

@@ -383,6 +388,7 @@ public class OffloadController {
        computeAndPushLocalPrefixes(UpdateType.IF_NEEDED);
    }

    /** Update current downstream LinkProperties. */
    public void notifyDownstreamLinkProperties(LinkProperties lp) {
        final String ifname = lp.getInterfaceName();
        final LinkProperties oldLp = mDownstreams.put(ifname, new LinkProperties(lp));
@@ -421,6 +427,7 @@ public class OffloadController {
        }
    }

    /** Remove downstream interface from offload hardware. */
    public void removeDownstreamInterface(String ifname) {
        final LinkProperties lp = mDownstreams.remove(ifname);
        if (lp == null) return;
@@ -545,6 +552,7 @@ public class OffloadController {
        return false;
    }

    /** Dump information. */
    public void dump(IndentingPrintWriter pw) {
        if (isOffloadDisabled()) {
            pw.println("Offload disabled");
+54 −26
Original line number Diff line number Diff line
@@ -23,9 +23,9 @@ import android.hardware.tetheroffload.control.V1_0.ITetheringOffloadCallback;
import android.hardware.tetheroffload.control.V1_0.NatTimeoutUpdate;
import android.hardware.tetheroffload.control.V1_0.NetworkProtocol;
import android.hardware.tetheroffload.control.V1_0.OffloadCallbackEvent;
import android.net.util.SharedLog;
import android.os.Handler;
import android.os.RemoteException;
import android.net.util.SharedLog;
import android.system.OsConstants;

import java.util.ArrayList;
@@ -55,18 +55,34 @@ public class OffloadHardwareInterface {
    private TetheringOffloadCallback mTetheringOffloadCallback;
    private ControlCallback mControlCallback;

    /** The callback to notify status of offload management process. */
    public static class ControlCallback {
        /** Offload started. */
        public void onStarted() {}
        /**
         * Offload stopped because an error has occurred in lower layer.
         */
        public void onStoppedError() {}
        /**
         * Offload stopped because the device has moved to a bearer on which hardware offload is
         * not supported. Subsequent calls to setUpstreamParameters and add/removeDownstream will
         * likely fail and cannot be presumed to be saved inside of the hardware management process.
         * Upon receiving #onSupportAvailable(), the caller should reprogram the hardware to begin
         * offload again.
         */
        public void onStoppedUnsupported() {}
        /** Indicate that offload is able to proivde support for this time. */
        public void onSupportAvailable() {}
        /** Offload stopped because of usage limit reached. */
        public void onStoppedLimitReached() {}

        /** Indicate to update NAT timeout. */
        public void onNatTimeoutUpdate(int proto,
                                       String srcAddr, int srcPort,
                                       String dstAddr, int dstPort) {}
    }

    /** The object which records Tx/Rx forwarded bytes. */
    public static class ForwardedStats {
        public long rxBytes;
        public long txBytes;
@@ -76,11 +92,13 @@ public class OffloadHardwareInterface {
            txBytes = 0;
        }

        /** Add Tx/Rx bytes. */
        public void add(ForwardedStats other) {
            rxBytes += other.rxBytes;
            txBytes += other.txBytes;
        }

        /** Returns the string representation of this object. */
        public String toString() {
            return String.format("rx:%s tx:%s", rxBytes, txBytes);
        }
@@ -91,14 +109,17 @@ public class OffloadHardwareInterface {
        mLog = log.forSubComponent(TAG);
    }

    /** Get default value indicating whether offload is supported. */
    public int getDefaultTetherOffloadDisabled() {
        return DEFAULT_TETHER_OFFLOAD_DISABLED;
    }

    /** Configure offload management process. */
    public boolean initOffloadConfig() {
        return configOffload();
    }

    /** Initialize the tethering offload HAL. */
    public boolean initOffloadControl(ControlCallback controlCb) {
        mControlCallback = controlCb;

@@ -125,8 +146,8 @@ public class OffloadHardwareInterface {
            mOffloadControl.initOffload(
                    mTetheringOffloadCallback,
                    (boolean success, String errMsg) -> {
                        results.success = success;
                        results.errMsg = errMsg;
                        results.mSuccess = success;
                        results.mErrMsg = errMsg;
                    });
        } catch (RemoteException e) {
            record(logmsg, e);
@@ -134,9 +155,10 @@ public class OffloadHardwareInterface {
        }

        record(logmsg, results);
        return results.success;
        return results.mSuccess;
    }

    /** Stop IOffloadControl. */
    public void stopOffloadControl() {
        if (mOffloadControl != null) {
            try {
@@ -154,6 +176,7 @@ public class OffloadHardwareInterface {
        mLog.log("stopOffloadControl()");
    }

    /** Get Tx/Rx usage from last query. */
    public ForwardedStats getForwardedStats(String upstream) {
        final String logmsg = String.format("getForwardedStats(%s)",  upstream);

@@ -174,6 +197,7 @@ public class OffloadHardwareInterface {
        return stats;
    }

    /** Set local prefixes to offload management process. */
    public boolean setLocalPrefixes(ArrayList<String> localPrefixes) {
        final String logmsg = String.format("setLocalPrefixes([%s])",
                String.join(",", localPrefixes));
@@ -182,8 +206,8 @@ public class OffloadHardwareInterface {
        try {
            mOffloadControl.setLocalPrefixes(localPrefixes,
                    (boolean success, String errMsg) -> {
                        results.success = success;
                        results.errMsg = errMsg;
                        results.mSuccess = success;
                        results.mErrMsg = errMsg;
                    });
        } catch (RemoteException e) {
            record(logmsg, e);
@@ -191,9 +215,10 @@ public class OffloadHardwareInterface {
        }

        record(logmsg, results);
        return results.success;
        return results.mSuccess;
    }

    /** Set data limit value to offload management process. */
    public boolean setDataLimit(String iface, long limit) {

        final String logmsg = String.format("setDataLimit(%s, %d)", iface, limit);
@@ -203,8 +228,8 @@ public class OffloadHardwareInterface {
            mOffloadControl.setDataLimit(
                    iface, limit,
                    (boolean success, String errMsg) -> {
                        results.success = success;
                        results.errMsg = errMsg;
                        results.mSuccess = success;
                        results.mErrMsg = errMsg;
                    });
        } catch (RemoteException e) {
            record(logmsg, e);
@@ -212,9 +237,10 @@ public class OffloadHardwareInterface {
        }

        record(logmsg, results);
        return results.success;
        return results.mSuccess;
    }

    /** Set upstream parameters to offload management process. */
    public boolean setUpstreamParameters(
            String iface, String v4addr, String v4gateway, ArrayList<String> v6gws) {
        iface = (iface != null) ? iface : NO_INTERFACE_NAME;
@@ -230,8 +256,8 @@ public class OffloadHardwareInterface {
            mOffloadControl.setUpstreamParameters(
                    iface, v4addr, v4gateway, v6gws,
                    (boolean success, String errMsg) -> {
                        results.success = success;
                        results.errMsg = errMsg;
                        results.mSuccess = success;
                        results.mErrMsg = errMsg;
                    });
        } catch (RemoteException e) {
            record(logmsg, e);
@@ -239,9 +265,10 @@ public class OffloadHardwareInterface {
        }

        record(logmsg, results);
        return results.success;
        return results.mSuccess;
    }

    /** Add downstream prefix to offload management process. */
    public boolean addDownstreamPrefix(String ifname, String prefix) {
        final String logmsg = String.format("addDownstreamPrefix(%s, %s)", ifname, prefix);

@@ -249,8 +276,8 @@ public class OffloadHardwareInterface {
        try {
            mOffloadControl.addDownstream(ifname, prefix,
                    (boolean success, String errMsg) -> {
                        results.success = success;
                        results.errMsg = errMsg;
                        results.mSuccess = success;
                        results.mErrMsg = errMsg;
                    });
        } catch (RemoteException e) {
            record(logmsg, e);
@@ -258,9 +285,10 @@ public class OffloadHardwareInterface {
        }

        record(logmsg, results);
        return results.success;
        return results.mSuccess;
    }

    /** Remove downstream prefix from offload management process. */
    public boolean removeDownstreamPrefix(String ifname, String prefix) {
        final String logmsg = String.format("removeDownstreamPrefix(%s, %s)", ifname, prefix);

@@ -268,8 +296,8 @@ public class OffloadHardwareInterface {
        try {
            mOffloadControl.removeDownstream(ifname, prefix,
                    (boolean success, String errMsg) -> {
                        results.success = success;
                        results.errMsg = errMsg;
                        results.mSuccess = success;
                        results.mErrMsg = errMsg;
                    });
        } catch (RemoteException e) {
            record(logmsg, e);
@@ -277,7 +305,7 @@ public class OffloadHardwareInterface {
        }

        record(logmsg, results);
        return results.success;
        return results.mSuccess;
    }

    private void record(String msg, Throwable t) {
@@ -286,7 +314,7 @@ public class OffloadHardwareInterface {

    private void record(String msg, CbResults results) {
        final String logmsg = msg + YIELDS + results;
        if (!results.success) {
        if (!results.mSuccess) {
            mLog.e(logmsg);
        } else {
            mLog.log(logmsg);
@@ -298,7 +326,7 @@ public class OffloadHardwareInterface {
        public final ControlCallback controlCb;
        public final SharedLog log;

        public TetheringOffloadCallback(Handler h, ControlCallback cb, SharedLog sharedLog) {
        TetheringOffloadCallback(Handler h, ControlCallback cb, SharedLog sharedLog) {
            handler = h;
            controlCb = cb;
            log = sharedLog;
@@ -352,15 +380,15 @@ public class OffloadHardwareInterface {
    }

    private static class CbResults {
        boolean success;
        String errMsg;
        boolean mSuccess;
        String mErrMsg;

        @Override
        public String toString() {
            if (success) {
            if (mSuccess) {
                return "ok";
            } else {
                return "fail: " + errMsg;
                return "fail: " + mErrMsg;
            }
        }
    }
+2 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ android_test {
    static_libs: [
        "androidx.test.rules",
        "frameworks-base-testutils",
        "net-tests-utils",
        "mockito-target-extended-minus-junit4",
        "TetheringApiCurrentLib",
        "testables",
@@ -46,6 +47,7 @@ filegroup {
    name: "tethering-tests-src",
    srcs: [
        "src/com/android/server/connectivity/tethering/EntitlementManagerTest.java",
        "src/com/android/server/connectivity/tethering/OffloadControllerTest.java",
        "src/com/android/server/connectivity/tethering/TetheringConfigurationTest.java",
        "src/com/android/server/connectivity/tethering/UpstreamNetworkMonitorTest.java",
        "src/android/net/dhcp/DhcpServingParamsParcelExtTest.java",
Loading