Make DNS cache lifecycle management explicit

1. ConnectivityService calls netd binder to create/destroy network directly.
2. Call dnsresolver binder to create/destroy cache after create/destroy network.
3. Remove unused network create/destroy methods in NetworkManagementService.

Bug: 129453995
Test: atest FrameworksNetTests

Merged-In: I388e208143c38b89bcbb0589de393250024d59aa
(cherry picked from commit 204ca13e63f063f044ac4ad3b96f08b473fe59df)

Change-Id: I4d3dfd9305b60a724aa2dc38448948d8e710c932
This commit is contained in:
Lorenzo Colitti
2019-04-25 18:06:28 -07:00
committed by Luke Huang
parent 72d5c3fc4e
commit d44040df36
3 changed files with 43 additions and 28 deletions

View File

@@ -3071,11 +3071,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
// fallback network the default or requested a new network from the
// NetworkFactories, so network traffic isn't interrupted for an unnecessarily
// long time.
try {
mNetd.networkDestroy(nai.network.netId);
} catch (RemoteException | ServiceSpecificException e) {
loge("Exception destroying network: " + e);
}
destroyNativeNetwork(nai);
mDnsManager.removeNetwork(nai.network);
}
synchronized (mNetworkForNetId) {
@@ -3083,6 +3079,35 @@ public class ConnectivityService extends IConnectivityManager.Stub
}
}
private boolean createNativeNetwork(@NonNull NetworkAgentInfo networkAgent) {
try {
// This should never fail. Specifying an already in use NetID will cause failure.
if (networkAgent.isVPN()) {
mNetd.networkCreateVpn(networkAgent.network.netId,
(networkAgent.networkMisc == null
|| !networkAgent.networkMisc.allowBypass));
} else {
mNetd.networkCreatePhysical(networkAgent.network.netId,
getNetworkPermission(networkAgent.networkCapabilities));
}
mDnsResolver.createNetworkCache(networkAgent.network.netId);
return true;
} catch (RemoteException | ServiceSpecificException e) {
loge("Error creating network " + networkAgent.network.netId + ": "
+ e.getMessage());
return false;
}
}
private void destroyNativeNetwork(@NonNull NetworkAgentInfo networkAgent) {
try {
mNetd.networkDestroy(networkAgent.network.netId);
mDnsResolver.destroyNetworkCache(networkAgent.network.netId);
} catch (RemoteException | ServiceSpecificException e) {
loge("Exception destroying network: " + e);
}
}
// If this method proves to be too slow then we can maintain a separate
// pendingIntent => NetworkRequestInfo map.
// This method assumes that every non-null PendingIntent maps to exactly 1 NetworkRequestInfo.
@@ -6476,21 +6501,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
// A network that has just connected has zero requests and is thus a foreground network.
networkAgent.networkCapabilities.addCapability(NET_CAPABILITY_FOREGROUND);
try {
// This should never fail. Specifying an already in use NetID will cause failure.
if (networkAgent.isVPN()) {
mNMS.createVirtualNetwork(networkAgent.network.netId,
(networkAgent.networkMisc == null ||
!networkAgent.networkMisc.allowBypass));
} else {
mNMS.createPhysicalNetwork(networkAgent.network.netId,
getNetworkPermission(networkAgent.networkCapabilities));
}
} catch (Exception e) {
loge("Error creating network " + networkAgent.network.netId + ": "
+ e.getMessage());
return;
}
if (!createNativeNetwork(networkAgent)) return;
networkAgent.created = true;
}

View File

@@ -263,12 +263,6 @@ public class DnsManager {
}
public void removeNetwork(Network network) {
try {
mDnsResolver.clearResolverConfiguration(network.netId);
} catch (RemoteException | ServiceSpecificException e) {
Slog.e(TAG, "Error clearing DNS configuration: " + e);
return;
}
mPrivateDnsMap.remove(network.netId);
mPrivateDnsValidationMap.remove(network.netId);
}

View File

@@ -4894,7 +4894,10 @@ public class ConnectivityServiceTest {
mCellNetworkAgent.sendLinkProperties(cellLp);
mCellNetworkAgent.connect(false);
waitForIdle();
// CS tells netd about the empty DNS config for this network.
verify(mMockDnsResolver, times(1)).createNetworkCache(
eq(mCellNetworkAgent.getNetwork().netId));
// CS tells dnsresolver about the empty DNS config for this network.
verify(mMockDnsResolver, atLeastOnce()).setResolverConfiguration(any());
reset(mMockDnsResolver);
@@ -4978,6 +4981,8 @@ public class ConnectivityServiceTest {
mCellNetworkAgent.sendLinkProperties(cellLp);
mCellNetworkAgent.connect(false);
waitForIdle();
verify(mMockDnsResolver, times(1)).createNetworkCache(
eq(mCellNetworkAgent.getNetwork().netId));
verify(mMockDnsResolver, atLeastOnce()).setResolverConfiguration(
mResolverParamsParcelCaptor.capture());
ResolverParamsParcel resolvrParams = mResolverParamsParcelCaptor.getValue();
@@ -5851,12 +5856,17 @@ public class ConnectivityServiceTest {
cellLp.addRoute(new RouteInfo(myIpv6, null, MOBILE_IFNAME));
reset(mNetworkManagementService);
reset(mMockDnsResolver);
reset(mMockNetd);
when(mNetworkManagementService.getInterfaceConfig(CLAT_PREFIX + MOBILE_IFNAME))
.thenReturn(getClatInterfaceConfig(myIpv4));
// Connect with ipv6 link properties. Expect prefix discovery to be started.
mCellNetworkAgent.sendLinkProperties(cellLp);
mCellNetworkAgent.connect(true);
verify(mMockNetd, times(1)).networkCreatePhysical(eq(cellNetId), anyInt());
verify(mMockDnsResolver, times(1)).createNetworkCache(eq(cellNetId));
networkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
verify(mMockDnsResolver, times(1)).startPrefix64Discovery(cellNetId);
@@ -6048,7 +6058,7 @@ public class ConnectivityServiceTest {
verify(mNetworkManagementService, times(0)).removeIdleTimer(eq(MOBILE_IFNAME));
verify(mMockNetd, times(1)).networkDestroy(eq(mCellNetworkAgent.getNetwork().netId));
verify(mMockDnsResolver, times(1))
.clearResolverConfiguration(eq(mCellNetworkAgent.getNetwork().netId));
.destroyNetworkCache(eq(mCellNetworkAgent.getNetwork().netId));
// Disconnect wifi
ConditionVariable cv = waitForConnectivityBroadcasts(1);