From 91ef9d38e30aaefc18d25a4794a1e5982a76015f Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Wed, 26 Feb 2025 17:33:27 +0800 Subject: [PATCH] Enable early native network creation for local network This is needed by tethering local network feature, given that the IpServer needs to setup forwarding after native network creation and before signaling network availability. Test: atest CtsNetTestCases:android.net.cts.NetworkAgentTest \ ConnectivityCoverageTests:android.net.EthernetTetheringTest \ ConnectivityCoverageTests:android.net.connectivity.com.android.server.ConnectivityServiceTest \ --update-device Bug: 349487600 Change-Id: I2bc2ec355e01be661b587406bd127dff11665047 --- .../android/server/ConnectivityService.java | 12 ++++----- .../server/ConnectivityServiceTest.java | 26 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/service/src/com/android/server/ConnectivityService.java b/service/src/com/android/server/ConnectivityService.java index 2c6390f1a0..99b9eca0a1 100644 --- a/service/src/com/android/server/ConnectivityService.java +++ b/service/src/com/android/server/ConnectivityService.java @@ -5405,12 +5405,12 @@ public class ConnectivityService extends IConnectivityManager.Stub { } @VisibleForTesting - protected static boolean shouldCreateNetworksImmediately() { + protected static boolean shouldCreateNetworksImmediately(@NonNull NetworkCapabilities caps) { // The feature of creating the networks immediately was slated for U, but race conditions // detected late required this was flagged off. // TODO : enable this in a Mainline update or in V, and re-enable the test for this // in NetworkAgentTest. - return false; + return caps.hasCapability(NET_CAPABILITY_LOCAL_NETWORK); } private static boolean shouldCreateNativeNetwork(@NonNull NetworkAgentInfo nai, @@ -5419,12 +5419,12 @@ public class ConnectivityService extends IConnectivityManager.Stub { if (state == NetworkInfo.State.CONNECTED) return true; if (state != NetworkInfo.State.CONNECTING) { // TODO: throw if no WTFs are observed in the field. - if (shouldCreateNetworksImmediately()) { + if (shouldCreateNetworksImmediately(nai.getCapsNoCopy())) { Log.wtf(TAG, "Uncreated network in invalid state: " + state); } return false; } - return nai.isVPN() || shouldCreateNetworksImmediately(); + return nai.isVPN() || shouldCreateNetworksImmediately(nai.getCapsNoCopy()); } private static boolean shouldDestroyNativeNetwork(@NonNull NetworkAgentInfo nai) { @@ -12048,7 +12048,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { // interfaces and routing rules have been added, DNS servers programmed, etc. // For VPNs, this must be done before the capabilities are updated, because as soon as // that happens, UIDs are routed to the network. - if (shouldCreateNetworksImmediately()) { + if (shouldCreateNetworksImmediately(networkAgent.getCapsNoCopy())) { applyInitialLinkProperties(networkAgent); } @@ -12073,7 +12073,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { networkAgent.getAndSetNetworkCapabilities(networkAgent.networkCapabilities); handlePerNetworkPrivateDnsConfig(networkAgent, mDnsManager.getPrivateDnsConfig()); - if (!shouldCreateNetworksImmediately()) { + if (!shouldCreateNetworksImmediately(networkAgent.getCapsNoCopy())) { applyInitialLinkProperties(networkAgent); } else { // The network was created when the agent registered, and the LinkProperties are diff --git a/tests/unit/java/com/android/server/ConnectivityServiceTest.java b/tests/unit/java/com/android/server/ConnectivityServiceTest.java index 19a41d88e9..c4944b6de0 100755 --- a/tests/unit/java/com/android/server/ConnectivityServiceTest.java +++ b/tests/unit/java/com/android/server/ConnectivityServiceTest.java @@ -4040,7 +4040,7 @@ public class ConnectivityServiceTest { mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, callbacks); - if (mService.shouldCreateNetworksImmediately()) { + if (mService.shouldCreateNetworksImmediately(mWiFiAgent.getNetworkCapabilities())) { assertEquals("onNetworkCreated", eventOrder.poll(TIMEOUT_MS, TimeUnit.MILLISECONDS)); } else { assertNull(eventOrder.poll()); @@ -4053,7 +4053,7 @@ public class ConnectivityServiceTest { // connected. // TODO: fix this bug, file the request before connecting, and remove the waitForIdle. mWiFiAgent.connectWithoutInternet(); - if (!mService.shouldCreateNetworksImmediately()) { + if (!mService.shouldCreateNetworksImmediately(mWiFiAgent.getNetworkCapabilities())) { assertEquals("onNetworkCreated", eventOrder.poll(TIMEOUT_MS, TimeUnit.MILLISECONDS)); } else { waitForIdle(); @@ -7941,8 +7941,8 @@ public class ConnectivityServiceTest { // Simple connection with initial LP should have updated ifaces. mCellAgent.connect(false); waitForIdle(); - List allNetworks = mService.shouldCreateNetworksImmediately() - ? cellAndWifi() : onlyCell(); + List allNetworks = mService.shouldCreateNetworksImmediately( + mCellAgent.getNetworkCapabilities()) ? cellAndWifi() : onlyCell(); expectNotifyNetworkStatus(allNetworks, onlyCell(), MOBILE_IFNAME); reset(mStatsManager); @@ -8254,7 +8254,7 @@ public class ConnectivityServiceTest { mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR); final int netId = mCellAgent.getNetwork().netId; waitForIdle(); - if (mService.shouldCreateNetworksImmediately()) { + if (mService.shouldCreateNetworksImmediately(mCellAgent.getNetworkCapabilities())) { verify(mMockDnsResolver, times(1)).createNetworkCache(netId); } else { verify(mMockDnsResolver, never()).setResolverConfiguration(any()); @@ -8274,7 +8274,7 @@ public class ConnectivityServiceTest { mCellAgent.sendLinkProperties(cellLp); mCellAgent.connect(false); waitForIdle(); - if (!mService.shouldCreateNetworksImmediately()) { + if (!mService.shouldCreateNetworksImmediately(mCellAgent.getNetworkCapabilities())) { // CS tells dnsresolver about the empty DNS config for this network. verify(mMockDnsResolver, times(1)).createNetworkCache(netId); } @@ -8397,7 +8397,7 @@ public class ConnectivityServiceTest { mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR); final int netId = mCellAgent.getNetwork().netId; waitForIdle(); - if (mService.shouldCreateNetworksImmediately()) { + if (mService.shouldCreateNetworksImmediately(mCellAgent.getNetworkCapabilities())) { verify(mMockDnsResolver, times(1)).createNetworkCache(netId); } else { verify(mMockDnsResolver, never()).setResolverConfiguration(any()); @@ -8420,7 +8420,7 @@ public class ConnectivityServiceTest { mCellAgent.sendLinkProperties(cellLp); mCellAgent.connect(false); waitForIdle(); - if (!mService.shouldCreateNetworksImmediately()) { + if (!mService.shouldCreateNetworksImmediately(mCellAgent.getNetworkCapabilities())) { verify(mMockDnsResolver, times(1)).createNetworkCache(netId); } verify(mMockDnsResolver, atLeastOnce()).setResolverConfiguration( @@ -16065,13 +16065,13 @@ public class ConnectivityServiceTest { final TestNetworkAgentWrapper workAgent = makeEnterpriseNetworkAgent(profileNetworkPreference.getPreferenceEnterpriseId()); - if (mService.shouldCreateNetworksImmediately()) { + if (mService.shouldCreateNetworksImmediately(workAgent.getNetworkCapabilities())) { expectNativeNetworkCreated(workAgent.getNetwork().netId, INetd.PERMISSION_SYSTEM, null /* iface */, inOrder); } if (connectWorkProfileAgentAhead) { workAgent.connect(false); - if (!mService.shouldCreateNetworksImmediately()) { + if (!mService.shouldCreateNetworksImmediately(workAgent.getNetworkCapabilities())) { expectNativeNetworkCreated(workAgent.getNetwork().netId, INetd.PERMISSION_SYSTEM, null /* iface */, inOrder); } @@ -16114,7 +16114,7 @@ public class ConnectivityServiceTest { if (!connectWorkProfileAgentAhead) { workAgent.connect(false); - if (!mService.shouldCreateNetworksImmediately()) { + if (!mService.shouldCreateNetworksImmediately(workAgent.getNetworkCapabilities())) { inOrder.verify(mMockNetd).networkCreate( nativeNetworkConfigPhysical(workAgent.getNetwork().netId, INetd.PERMISSION_SYSTEM)); @@ -18825,7 +18825,7 @@ public class ConnectivityServiceTest { } private void verifyMtuSetOnWifiInterfaceOnlyUpToT(int mtu) throws Exception { - if (!mService.shouldCreateNetworksImmediately()) { + if (!mService.shouldCreateNetworksImmediately(mWiFiAgent.getNetworkCapabilities())) { verify(mMockNetd, times(1)).interfaceSetMtu(WIFI_IFNAME, mtu); } else { verify(mMockNetd, never()).interfaceSetMtu(eq(WIFI_IFNAME), anyInt()); @@ -18833,7 +18833,7 @@ public class ConnectivityServiceTest { } private void verifyMtuSetOnWifiInterfaceOnlyStartingFromU(int mtu) throws Exception { - if (mService.shouldCreateNetworksImmediately()) { + if (mService.shouldCreateNetworksImmediately(mWiFiAgent.getNetworkCapabilities())) { verify(mMockNetd, times(1)).interfaceSetMtu(WIFI_IFNAME, mtu); } else { verify(mMockNetd, never()).interfaceSetMtu(eq(WIFI_IFNAME), anyInt());