Merge "Only stop/start clatd if necessary." into lmp-dev

This commit is contained in:
Lorenzo Colitti
2014-09-24 01:21:08 +00:00
committed by Android (Google) Code Review
2 changed files with 45 additions and 39 deletions

View File

@@ -4240,13 +4240,17 @@ public class ConnectivityService extends IConnectivityManager.Stub {
LinkProperties newLp = networkAgent.linkProperties; LinkProperties newLp = networkAgent.linkProperties;
int netId = networkAgent.network.netId; int netId = networkAgent.network.netId;
// The NetworkAgentInfo does not know whether clatd is running on its network or not. Before
// we do anything else, make sure its LinkProperties are accurate.
mClat.fixupLinkProperties(networkAgent, oldLp);
updateInterfaces(newLp, oldLp, netId); updateInterfaces(newLp, oldLp, netId);
updateMtu(newLp, oldLp); updateMtu(newLp, oldLp);
updateTcpBufferSizes(networkAgent);
// TODO - figure out what to do for clat // TODO - figure out what to do for clat
// for (LinkProperties lp : newLp.getStackedLinks()) { // for (LinkProperties lp : newLp.getStackedLinks()) {
// updateMtu(lp, null); // updateMtu(lp, null);
// } // }
updateTcpBufferSizes(networkAgent);
final boolean flushDns = updateRoutes(newLp, oldLp, netId); final boolean flushDns = updateRoutes(newLp, oldLp, netId);
updateDnses(newLp, oldLp, netId, flushDns); updateDnses(newLp, oldLp, netId, flushDns);
updateClat(newLp, oldLp, networkAgent); updateClat(newLp, oldLp, networkAgent);
@@ -4254,25 +4258,16 @@ public class ConnectivityService extends IConnectivityManager.Stub {
} }
private void updateClat(LinkProperties newLp, LinkProperties oldLp, NetworkAgentInfo na) { private void updateClat(LinkProperties newLp, LinkProperties oldLp, NetworkAgentInfo na) {
// Update 464xlat state. final boolean wasRunningClat = mClat.isRunningClat(na);
if (mClat.requiresClat(na)) { final boolean shouldRunClat = Nat464Xlat.requiresClat(na);
// If the connection was previously using clat, but is not using it now, stop the clat if (!wasRunningClat && shouldRunClat) {
// daemon. Normally, this happens automatically when the connection disconnects, but if // Start clatd. If it's already been started but is not running yet, this is a no-op.
// the disconnect is not reported, or if the connection's LinkProperties changed for
// some other reason (e.g., handoff changes the IP addresses on the link), it would
// still be running. If it's not running, then stopping it is a no-op.
if (Nat464Xlat.isRunningClat(oldLp) && !Nat464Xlat.isRunningClat(newLp)) {
mClat.stopClat();
}
// If the link requires clat to be running, then start the daemon now.
if (na.networkInfo.isConnected()) {
mClat.startClat(na); mClat.startClat(na);
} else { } else if (wasRunningClat && !shouldRunClat) {
mClat.stopClat(); mClat.stopClat();
} }
} }
}
private void updateInterfaces(LinkProperties newLp, LinkProperties oldLp, int netId) { private void updateInterfaces(LinkProperties newLp, LinkProperties oldLp, int netId) {
CompareResult<String> interfaceDiff = new CompareResult<String>(); CompareResult<String> interfaceDiff = new CompareResult<String>();

View File

@@ -89,17 +89,20 @@ public class Nat464Xlat extends BaseNetworkObserver {
* @param network the NetworkAgentInfo corresponding to the network. * @param network the NetworkAgentInfo corresponding to the network.
* @return true if the network requires clat, false otherwise. * @return true if the network requires clat, false otherwise.
*/ */
public boolean requiresClat(NetworkAgentInfo network) { public static boolean requiresClat(NetworkAgentInfo nai) {
int netType = network.networkInfo.getType(); final int netType = nai.networkInfo.getType();
LinkProperties lp = network.linkProperties; final boolean connected = nai.networkInfo.isConnected();
final boolean hasIPv4Address =
(nai.linkProperties != null) ? nai.linkProperties.hasIPv4Address() : false;
Slog.d(TAG, "requiresClat: netType=" + netType +
", connected=" + connected +
", hasIPv4Address=" + hasIPv4Address);
// Only support clat on mobile for now. // Only support clat on mobile for now.
Slog.d(TAG, "requiresClat: netType=" + netType + ", hasIPv4Address=" + return netType == TYPE_MOBILE && connected && !hasIPv4Address;
lp.hasIPv4Address());
return netType == TYPE_MOBILE && !lp.hasIPv4Address();
} }
public static boolean isRunningClat(LinkProperties lp) { public boolean isRunningClat(NetworkAgentInfo network) {
return lp != null && lp.getAllInterfaceNames().contains(CLAT_INTERFACE_NAME); return mNetworkMessenger == network.messenger;
} }
/** /**
@@ -149,14 +152,6 @@ public class Nat464Xlat extends BaseNetworkObserver {
} }
} }
public boolean isStarted() {
return mIsStarted;
}
public boolean isRunning() {
return mIsRunning;
}
private void updateConnectivityService() { private void updateConnectivityService() {
Message msg = mHandler.obtainMessage( Message msg = mHandler.obtainMessage(
NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED, mBaseLP); NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED, mBaseLP);
@@ -165,6 +160,21 @@ public class Nat464Xlat extends BaseNetworkObserver {
msg.sendToTarget(); msg.sendToTarget();
} }
// Copies the stacked clat link in oldLp, if any, to the LinkProperties in nai.
public void fixupLinkProperties(NetworkAgentInfo nai, LinkProperties oldLp) {
if (isRunningClat(nai) &&
nai.linkProperties != null &&
!nai.linkProperties.getAllInterfaceNames().contains(CLAT_INTERFACE_NAME)) {
Slog.d(TAG, "clatd running, updating NAI for " + nai.linkProperties.getInterfaceName());
for (LinkProperties stacked: oldLp.getStackedLinks()) {
if (CLAT_INTERFACE_NAME.equals(stacked.getInterfaceName())) {
nai.linkProperties.addStackedLink(stacked);
break;
}
}
}
}
@Override @Override
public void interfaceAdded(String iface) { public void interfaceAdded(String iface) {
if (iface.equals(CLAT_INTERFACE_NAME)) { if (iface.equals(CLAT_INTERFACE_NAME)) {
@@ -175,17 +185,18 @@ public class Nat464Xlat extends BaseNetworkObserver {
// Create the LinkProperties for the clat interface by fetching the // Create the LinkProperties for the clat interface by fetching the
// IPv4 address for the interface and adding an IPv4 default route, // IPv4 address for the interface and adding an IPv4 default route,
// then stack the LinkProperties on top of the link it's running on. // then stack the LinkProperties on top of the link it's running on.
try {
InterfaceConfiguration config = mNMService.getInterfaceConfig(iface);
LinkAddress clatAddress = config.getLinkAddress();
mLP.clear();
mLP.setInterfaceName(iface);
// Although the clat interface is a point-to-point tunnel, we don't // Although the clat interface is a point-to-point tunnel, we don't
// point the route directly at the interface because some apps don't // point the route directly at the interface because some apps don't
// understand routes without gateways (see, e.g., http://b/9597256 // understand routes without gateways (see, e.g., http://b/9597256
// http://b/9597516). Instead, set the next hop of the route to the // http://b/9597516). Instead, set the next hop of the route to the
// clat IPv4 address itself (for those apps, it doesn't matter what // clat IPv4 address itself (for those apps, it doesn't matter what
// the IP of the gateway is, only that there is one). // the IP of the gateway is, only that there is one).
try {
InterfaceConfiguration config = mNMService.getInterfaceConfig(iface);
LinkAddress clatAddress = config.getLinkAddress();
mLP.clear();
mLP.setInterfaceName(iface);
RouteInfo ipv4Default = new RouteInfo(new LinkAddress(Inet4Address.ANY, 0), RouteInfo ipv4Default = new RouteInfo(new LinkAddress(Inet4Address.ANY, 0),
clatAddress.getAddress(), iface); clatAddress.getAddress(), iface);
mLP.addRoute(ipv4Default); mLP.addRoute(ipv4Default);