Convert the Ethernet factory to the new network agent

This is a combination of ag/10147368 and ag/10438581.
ag/10147368 implemented this conversion together with
using the planned NetworkScore API, while ag/10438581
reverted the parts that had to do with NetworkScore.
This is just a squash of these two patches and it
brings the factory in line with the code in rvc-dev.

Test: FrameworksNetTests NetworkStackTests EthernetTetheringTest
Change-Id: Ib214fbbd042f2c0ebd01cf5b0ec33d922629677e
Merged-In: Ie7b70750ef0e17141080f4266dea6155c3601569
This commit is contained in:
Chalard Jean
2020-01-22 16:47:43 +09:00
parent dd397f58c7
commit 86aac0daec

View File

@@ -16,7 +16,6 @@
package com.android.server.ethernet; package com.android.server.ethernet;
import static android.net.ConnectivityManager.TYPE_ETHERNET;
import static android.net.shared.LinkPropertiesParcelableUtil.toStableParcelable; import static android.net.shared.LinkPropertiesParcelableUtil.toStableParcelable;
import static com.android.internal.util.Preconditions.checkNotNull; import static com.android.internal.util.Preconditions.checkNotNull;
@@ -29,10 +28,9 @@ import android.net.IpConfiguration.IpAssignment;
import android.net.IpConfiguration.ProxySettings; import android.net.IpConfiguration.ProxySettings;
import android.net.LinkProperties; import android.net.LinkProperties;
import android.net.NetworkAgent; import android.net.NetworkAgent;
import android.net.NetworkAgentConfig;
import android.net.NetworkCapabilities; import android.net.NetworkCapabilities;
import android.net.NetworkFactory; import android.net.NetworkFactory;
import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState;
import android.net.NetworkRequest; import android.net.NetworkRequest;
import android.net.NetworkSpecifier; import android.net.NetworkSpecifier;
import android.net.StringNetworkSpecifier; import android.net.StringNetworkSpecifier;
@@ -52,7 +50,6 @@ import android.util.SparseArray;
import com.android.internal.util.IndentingPrintWriter; import com.android.internal.util.IndentingPrintWriter;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.lang.Math;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.Objects; import java.util.Objects;
@@ -221,12 +218,12 @@ public class EthernetNetworkFactory extends NetworkFactory {
NetworkInterfaceState network = null; NetworkInterfaceState network = null;
if (!TextUtils.isEmpty(requestedIface)) { if (!TextUtils.isEmpty(requestedIface)) {
NetworkInterfaceState n = mTrackingInterfaces.get(requestedIface); NetworkInterfaceState n = mTrackingInterfaces.get(requestedIface);
if (n != null && n.statisified(request.networkCapabilities)) { if (n != null && n.satisfied(request.networkCapabilities)) {
network = n; network = n;
} }
} else { } else {
for (NetworkInterfaceState n : mTrackingInterfaces.values()) { for (NetworkInterfaceState n : mTrackingInterfaces.values()) {
if (n.statisified(request.networkCapabilities) && n.mLinkUp) { if (n.satisfied(request.networkCapabilities) && n.mLinkUp) {
network = n; network = n;
break; break;
} }
@@ -247,8 +244,8 @@ public class EthernetNetworkFactory extends NetworkFactory {
private final NetworkCapabilities mCapabilities; private final NetworkCapabilities mCapabilities;
private final Handler mHandler; private final Handler mHandler;
private final Context mContext; private final Context mContext;
private final NetworkInfo mNetworkInfo;
private final NetworkFactory mNetworkFactory; private final NetworkFactory mNetworkFactory;
private final int mLegacyType;
private static String sTcpBufferSizes = null; // Lazy initialized. private static String sTcpBufferSizes = null; // Lazy initialized.
@@ -375,9 +372,7 @@ public class EthernetNetworkFactory extends NetworkFactory {
} }
mHwAddress = hwAddress; mHwAddress = hwAddress;
mNetworkInfo = new NetworkInfo(legacyType, 0, NETWORK_TYPE, ""); mLegacyType = legacyType;
mNetworkInfo.setExtraInfo(mHwAddress);
mNetworkInfo.setIsAvailable(true);
} }
void setIpConfig(IpConfiguration ipConfig) { void setIpConfig(IpConfiguration ipConfig) {
@@ -386,12 +381,12 @@ public class EthernetNetworkFactory extends NetworkFactory {
return; return;
} }
this.mIpConfig = ipConfig; this.mIpConfig = ipConfig;
if (mNetworkInfo.getDetailedState() != DetailedState.DISCONNECTED) { if (mNetworkAgent != null) {
restart(); restart();
} }
} }
boolean statisified(NetworkCapabilities requestedCapabilities) { boolean satisfied(NetworkCapabilities requestedCapabilities) {
return requestedCapabilities.satisfiedByNetworkCapabilities(mCapabilities); return requestedCapabilities.satisfiedByNetworkCapabilities(mCapabilities);
} }
@@ -446,11 +441,9 @@ public class EthernetNetworkFactory extends NetworkFactory {
return; return;
} }
if (DBG) { if (DBG) {
Log.d(TAG, String.format("starting IpClient(%s): mNetworkInfo=%s", name, Log.d(TAG, String.format("Starting Ethernet IpClient(%s)", name));
mNetworkInfo));
} }
mNetworkInfo.setDetailedState(DetailedState.OBTAINING_IPADDR, null, mHwAddress);
mIpClientCallback = new IpClientCallbacksImpl(); mIpClientCallback = new IpClientCallbacksImpl();
IpClientUtil.makeIpClient(mContext, name, mIpClientCallback); IpClientUtil.makeIpClient(mContext, name, mIpClientCallback);
mIpClientCallback.awaitIpClientStart(); mIpClientCallback.awaitIpClientStart();
@@ -468,13 +461,15 @@ public class EthernetNetworkFactory extends NetworkFactory {
return; return;
} }
mLinkProperties = linkProperties; mLinkProperties = linkProperties;
mNetworkInfo.setDetailedState(DetailedState.CONNECTED, null, mHwAddress);
mNetworkInfo.setIsAvailable(true);
// Create our NetworkAgent. // Create our NetworkAgent.
mNetworkAgent = new NetworkAgent(mHandler.getLooper(), mContext, final NetworkAgentConfig config = new NetworkAgentConfig.Builder()
NETWORK_TYPE, mNetworkInfo, mCapabilities, mLinkProperties, .setLegacyType(mLegacyType)
getNetworkScore(), mNetworkFactory.getSerialNumber()) { .setLegacyTypeName(NETWORK_TYPE)
.build();
mNetworkAgent = new NetworkAgent(mContext, mHandler.getLooper(),
NETWORK_TYPE, mCapabilities, mLinkProperties,
getNetworkScore(), config, mNetworkFactory.getProvider()) {
public void unwanted() { public void unwanted() {
if (this == mNetworkAgent) { if (this == mNetworkAgent) {
stop(); stop();
@@ -484,6 +479,9 @@ public class EthernetNetworkFactory extends NetworkFactory {
} // Otherwise, we've already called stop. } // Otherwise, we've already called stop.
} }
}; };
mNetworkAgent.register();
mNetworkAgent.setLegacyExtraInfo(mHwAddress);
mNetworkAgent.setConnected();
} }
void onIpLayerStopped(LinkProperties linkProperties) { void onIpLayerStopped(LinkProperties linkProperties) {
@@ -526,16 +524,11 @@ public class EthernetNetworkFactory extends NetworkFactory {
} }
mIpClientCallback = null; mIpClientCallback = null;
// ConnectivityService will only forget our NetworkAgent if we send it a NetworkInfo object
// with a state of DISCONNECTED or SUSPENDED. So we can't simply clear our NetworkInfo here:
// that sets the state to IDLE, and ConnectivityService will still think we're connected.
//
mNetworkInfo.setDetailedState(DetailedState.DISCONNECTED, null, mHwAddress);
if (mNetworkAgent != null) { if (mNetworkAgent != null) {
updateAgent(); mNetworkAgent.unregister();
mNetworkAgent = null; mNetworkAgent = null;
} }
clear(); mLinkProperties.clear();
} }
private void updateAgent() { private void updateAgent() {
@@ -543,11 +536,9 @@ public class EthernetNetworkFactory extends NetworkFactory {
if (DBG) { if (DBG) {
Log.i(TAG, "Updating mNetworkAgent with: " + Log.i(TAG, "Updating mNetworkAgent with: " +
mCapabilities + ", " + mCapabilities + ", " +
mNetworkInfo + ", " +
mLinkProperties); mLinkProperties);
} }
mNetworkAgent.sendNetworkCapabilities(mCapabilities); mNetworkAgent.sendNetworkCapabilities(mCapabilities);
mNetworkAgent.sendNetworkInfo(mNetworkInfo);
mNetworkAgent.sendLinkProperties(mLinkProperties); mNetworkAgent.sendLinkProperties(mLinkProperties);
// As a note, getNetworkScore() is fairly expensive to calculate. This is fine for now // As a note, getNetworkScore() is fairly expensive to calculate. This is fine for now
@@ -556,12 +547,6 @@ public class EthernetNetworkFactory extends NetworkFactory {
mNetworkAgent.sendNetworkScore(getNetworkScore()); mNetworkAgent.sendNetworkScore(getNetworkScore());
} }
private void clear() {
mLinkProperties.clear();
mNetworkInfo.setDetailedState(DetailedState.IDLE, null, null);
mNetworkInfo.setIsAvailable(false);
}
private static void provisionIpClient(IIpClient ipClient, IpConfiguration config, private static void provisionIpClient(IIpClient ipClient, IpConfiguration config,
String tcpBufferSizes) { String tcpBufferSizes) {
if (config.getProxySettings() == ProxySettings.STATIC || if (config.getProxySettings() == ProxySettings.STATIC ||
@@ -612,7 +597,6 @@ public class EthernetNetworkFactory extends NetworkFactory {
+ "iface: " + name + ", " + "iface: " + name + ", "
+ "up: " + mLinkUp + ", " + "up: " + mLinkUp + ", "
+ "hwAddress: " + mHwAddress + ", " + "hwAddress: " + mHwAddress + ", "
+ "networkInfo: " + mNetworkInfo + ", "
+ "networkCapabilities: " + mCapabilities + ", " + "networkCapabilities: " + mCapabilities + ", "
+ "networkAgent: " + mNetworkAgent + ", " + "networkAgent: " + mNetworkAgent + ", "
+ "score: " + getNetworkScore() + ", " + "score: " + getNetworkScore() + ", "