Bootstrap

Android12上实现双以太网卡共存同时访问外网

具体实现如下:

修改main 表优先级到9999, 作用:eth0 eth1 访问

不去teardown 低分数网线

diff --git a/service/src/com/android/server/ConnectivityService.java b/service/src/com/android/server/ConnectivityService.java
index 418e9e3..72d15d5 100644
--- a/service/src/com/android/server/ConnectivityService.java
+++ b/service/src/com/android/server/ConnectivityService.java
@@ -291,8 +291,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
     private static final String REQUEST_ARG = "requests";
 
     private static final boolean DBG = true;
-    private static final boolean DDBG = Log.isLoggable(TAG, Log.DEBUG);
-    private static final boolean VDBG = Log.isLoggable(TAG, Log.VERBOSE);
+    private static final boolean DDBG = true;    //Log.isLoggable(TAG, Log.DEBUG);
+    private static final boolean VDBG = true;    //Log.isLoggable(TAG, Log.VERBOSE);
 
     private static final boolean LOGD_BLOCKED_NETWORKINFO = true;
 
@@ -1587,7 +1587,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
         handleAlwaysOnNetworkRequest(mDefaultMobileDataRequest,
                 ConnectivitySettingsManager.MOBILE_DATA_ALWAYS_ON, true /* defaultValue */);
         handleAlwaysOnNetworkRequest(mDefaultWifiRequest,
-                ConnectivitySettingsManager.WIFI_ALWAYS_REQUESTED, false /* defaultValue */);
+                ConnectivitySettingsManager.WIFI_ALWAYS_REQUESTED, true/* defaultValue */);
         final boolean vehicleAlwaysRequested = mResources.get().getBoolean(
                 R.bool.config_vehicleInternalNetworkAlwaysRequested);
         handleAlwaysOnNetworkRequest(mDefaultVehicleRequest, vehicleAlwaysRequested);
@@ -3797,6 +3797,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
         // 2. If the network was inactive and there are now requests, unset inactive.
         // 3. If this network is unneeded (which implies it is not lingering), and there is at least
         //    one lingered request, set inactive.
+        /****
         nai.updateInactivityTimer();
         if (nai.isInactive() && nai.numForegroundNetworkRequests() > 0) {
             if (DBG) log("Unsetting inactive " + nai.toShortString());
@@ -3811,6 +3812,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
             logNetworkEvent(nai, NetworkEvent.NETWORK_LINGER);
             return true;
         }
+        ****/
+        loge("wade skip updateInactivityState.");
         return false;
     }
 
@@ -6853,7 +6856,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
             return;
         }
         mNetworkOffers.add(noi);
-        issueNetworkNeeds(noi);
+        //issueNetworkNeeds(noi);
     }
 
     private void handleUnregisterNetworkOffer(@NonNull final NetworkOfferInfo noi) {
@@ -7780,6 +7783,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
     }
 
     private void teardownUnneededNetwork(NetworkAgentInfo nai) {
+        /**
         if (nai.numRequestNetworkRequests() != 0) {
             for (int i = 0; i < nai.numNetworkRequests(); i++) {
                 NetworkRequest nr = nai.requestAt(i);
@@ -7790,6 +7794,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
             }
         }
         nai.disconnect();
+        **/
+        loge("wade skip teardownUnneededNetwork.");
     }
 
     private void handleLingerComplete(NetworkAgentInfo oldNetwork) {
@@ -8113,6 +8119,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
                 // Remove default networking if disallowed for managed default requests.
                 bestNetwork = mNoServiceNetwork;
             }
+            loge("wade bestNetwork = " + bestNetwork + "bestRequest = " + bestRequest);
             if (nri.getSatisfier() != bestNetwork) {
                 // bestNetwork may be null if no network can satisfy this request.
                 changes.addRequestReassignment(new NetworkReassignment.RequestReassignment(
@@ -8150,7 +8157,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
             log(changes.toString()); // Shorter form, only one line of log
         }
         applyNetworkReassignment(changes, now);
-        issueNetworkNeeds();
+        //issueNetworkNeeds();
     }
 
     private void applyNetworkReassignment(@NonNull final NetworkReassignment changes,
diff --git a/server/RouteController.cpp b/server/RouteController.cpp
index ba305e6..d38aec5 100644
--- a/server/RouteController.cpp
+++ b/server/RouteController.cpp
@@ -721,6 +721,18 @@ int RouteController::configureDummyNetwork() {
     return 0;
 }
 
+// Add a new rule to look up the 'main' table, with the same selectors as the "default network"
+// rule, but with a lower priority. We will never create routes in the main table; it should only be
+// used for directly-connected routes implicitly created by the kernel when adding IP addresses.
+// This is necessary, for example, when adding a route through a directly-connected gateway: in
+// order to add the route, there must already be a directly-connected route that covers the gateway.
+[[nodiscard]] static int addDirectlyConnectedRule() {
+
+    return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
+                        0, 0/*, IIF_NONE, OIF_NONE, UID_ROOT, UID_ROOT*/);
+}
+
+
 // Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
 // relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
 // behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
@@ -1160,6 +1172,11 @@ int RouteController::Init(unsigned localNetId) {
     if (int ret = addUnreachableRule()) {
         return ret;
     }
+
+    if (int ret = addDirectlyConnectedRule()) {
+       return ret;
+    }
+
     // Don't complain if we can't add the dummy network, since not all devices support it.
     configureDummyNetwork();
 
diff --git a/server/RouteController.h b/server/RouteController.h
index 38d2d62..43ee148 100644
--- a/server/RouteController.h
+++ b/server/RouteController.h
@@ -30,6 +30,7 @@
 namespace android::net {
 
 // clang-format off
+const uint32_t RULE_PRIORITY_DIRECTLY_CONNECTED  = 9999;
 const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM     = 10000;
 const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF        = 11000;
 const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL     = 12000;

;