Make NetworkScore system API

Bug: 146583853
Test: FrameworksNetTests
Change-Id: Ie7b70750ef0e17141080f4266dea6155c3601569
This commit is contained in:
Chalard Jean
2020-01-22 16:47:43 +09:00
parent 6a38d3aa4b
commit 79eb7ba87b

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,11 +28,11 @@ 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.NetworkScore;
import android.net.NetworkSpecifier; import android.net.NetworkSpecifier;
import android.net.StringNetworkSpecifier; import android.net.StringNetworkSpecifier;
import android.net.ip.IIpClient; import android.net.ip.IIpClient;
@@ -52,7 +51,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;
@@ -68,6 +66,9 @@ public class EthernetNetworkFactory extends NetworkFactory {
private final static int NETWORK_SCORE = 70; private final static int NETWORK_SCORE = 70;
private static final String NETWORK_TYPE = "Ethernet"; private static final String NETWORK_TYPE = "Ethernet";
private final static NetworkScore NULL_SCORE = new NetworkScore.Builder()
.setRange(NetworkScore.RANGE_CLOSE)
.build();
private final ConcurrentHashMap<String, NetworkInterfaceState> mTrackingInterfaces = private final ConcurrentHashMap<String, NetworkInterfaceState> mTrackingInterfaces =
new ConcurrentHashMap<>(); new ConcurrentHashMap<>();
@@ -221,12 +222,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 +248,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 +376,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 +385,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);
} }
@@ -421,23 +420,23 @@ public class EthernetNetworkFactory extends NetworkFactory {
* This function is called with the purpose of assigning and updating the network score of * This function is called with the purpose of assigning and updating the network score of
* the member NetworkAgent. * the member NetworkAgent.
*/ */
private int getNetworkScore() { private NetworkScore getNetworkScore() {
// never set the network score below 0. // never set the network score below 0.
if (!mLinkUp) { if (!mLinkUp) {
return 0; return NULL_SCORE;
} }
int[] transportTypes = mCapabilities.getTransportTypes(); int[] transportTypes = mCapabilities.getTransportTypes();
if (transportTypes.length < 1) { if (transportTypes.length < 1) {
Log.w(TAG, "Network interface '" + mLinkProperties.getInterfaceName() + "' has no " Log.w(TAG, "Network interface '" + mLinkProperties.getInterfaceName() + "' has no "
+ "transport type associated with it. Score set to zero"); + "transport type associated with it. Score set to zero");
return 0; return NULL_SCORE;
} }
TransportInfo transportInfo = sTransports.get(transportTypes[0], /* if dne */ null); TransportInfo transportInfo = sTransports.get(transportTypes[0], /* if dne */ null);
if (transportInfo != null) { if (transportInfo != null) {
return transportInfo.mScore; return new NetworkScore.Builder().setLegacyScore(transportInfo.mScore).build();
} }
return 0; return NULL_SCORE;
} }
private void start() { private void start() {
@@ -446,11 +445,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 +465,16 @@ 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, // STOPSHIP : use the new constructor for NetworkAgent that takes a NetworkScore
NETWORK_TYPE, mNetworkInfo, mCapabilities, mLinkProperties, final NetworkAgentConfig config = new NetworkAgentConfig.Builder()
getNetworkScore(), mNetworkFactory.getSerialNumber()) { .setLegacyType(mLegacyType)
.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 +484,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 +529,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 +541,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 +552,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 +602,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() + ", "