[NS09] Implement the new ranking code

At this stage, this is turned off. Unit tests will be
in a followup change.

Test: In a followup
Bug: 167544279
Merged-In: I3c2563d4ae4e3715d0c6270344ba8f7ef067872f
Merged-In: I4448a3546fbc1a3dddf757982c031c5f39ba2889
Change-Id: I4448a3546fbc1a3dddf757982c031c5f39ba2889
  (cherry-picked from ag/14010222 with fixes)
This commit is contained in:
Chalard Jean
2021-03-29 17:03:59 +09:00
committed by Junyu Lai
parent e3d2481792
commit e4aeac6d75
10 changed files with 308 additions and 89 deletions

View File

@@ -303,7 +303,6 @@ import com.android.net.module.util.ArrayTrackRecord;
import com.android.net.module.util.CollectionUtils;
import com.android.server.ConnectivityService.ConnectivityDiagnosticsCallbackInfo;
import com.android.server.ConnectivityService.NetworkRequestInfo;
import com.android.server.connectivity.ConnectivityConstants;
import com.android.server.connectivity.MockableSystemProperties;
import com.android.server.connectivity.Nat464Xlat;
import com.android.server.connectivity.NetworkAgentInfo;
@@ -3074,8 +3073,9 @@ public class ConnectivityServiceTest {
}
NetworkCapabilities filter = new NetworkCapabilities();
filter.addTransportType(TRANSPORT_CELLULAR);
filter.addCapability(capability);
// Add NOT_VCN_MANAGED capability into filter unconditionally since some request will add
// Add NOT_VCN_MANAGED capability into filter unconditionally since some requests will add
// NOT_VCN_MANAGED automatically but not for NetworkCapabilities,
// see {@code NetworkCapabilities#deduceNotVcnManagedCapability} for more details.
filter.addCapability(NET_CAPABILITY_NOT_VCN_MANAGED);
@@ -3104,15 +3104,11 @@ public class ConnectivityServiceTest {
// Now bring in a higher scored network.
TestNetworkAgentWrapper testAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
// Rather than create a validated network which complicates things by registering it's
// own NetworkRequest during startup, just bump up the score to cancel out the
// unvalidated penalty.
testAgent.adjustScore(40);
// When testAgent connects, because of its 50 score (50 for cell + 40 adjustment score
// - 40 penalty for not being validated), it will beat the testFactory's offer, so
// the request will be removed.
testAgent.connect(false);
// When testAgent connects, because of its score (50 legacy int / cell transport)
// it will beat or equal the testFactory's offer, so the request will be removed.
// Note the agent as validated only if the capability is INTERNET, as it's the only case
// where it makes sense.
testAgent.connect(NET_CAPABILITY_INTERNET == capability /* validated */);
testAgent.addCapability(capability);
testFactory.expectRequestRemove();
testFactory.assertRequestCountEquals(0);
@@ -3126,17 +3122,18 @@ public class ConnectivityServiceTest {
testFactory.assertRequestCountEquals(0);
assertFalse(testFactory.getMyStartRequested());
// Make the test agent weak enough to have the exact same score as the
// factory (50 for cell + 40 adjustment -40 validation penalty - 5 adjustment). Make sure
// the factory doesn't see the request.
// If using legacy scores, make the test agent weak enough to have the exact same score as
// the factory (50 for cell - 5 adjustment). Make sure the factory doesn't see the request.
// If not using legacy score, this is a no-op and the "same score removes request" behavior
// has already been tested above.
testAgent.adjustScore(-5);
expectNoRequestChanged(testFactory);
assertFalse(testFactory.getMyStartRequested());
// Make the test agent weak enough to see the two requests (the one that was just sent,
// and either the default one or the one sent at the top of this test if the default
// won't be seen).
testAgent.adjustScore(-45);
// Make the test agent weak enough that the factory will see the two requests (the one that
// was just sent, and either the default one or the one sent at the top of this test if
// the default won't be seen).
testAgent.setScore(new NetworkScore.Builder().setLegacyInt(2).setExiting(true).build());
testFactory.expectRequestAdds(2);
testFactory.assertRequestCountEquals(2);
assertTrue(testFactory.getMyStartRequested());
@@ -3168,7 +3165,7 @@ public class ConnectivityServiceTest {
assertTrue(testFactory.getMyStartRequested());
// Adjust the agent score up again. Expect the request to be withdrawn.
testAgent.adjustScore(50);
testAgent.setScore(new NetworkScore.Builder().setLegacyInt(50).build());
testFactory.expectRequestRemove();
testFactory.assertRequestCountEquals(0);
assertFalse(testFactory.getMyStartRequested());
@@ -3208,25 +3205,39 @@ public class ConnectivityServiceTest {
// Skipping VALIDATED and CAPTIVE_PORTAL as they're disallowed.
}
@Ignore("Refactoring in progress b/184028345")
@Test
public void testRegisterIgnoringScore() throws Exception {
mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
mWiFiNetworkAgent.adjustScore(30); // score = 60 (wifi) + 30 = 90
mWiFiNetworkAgent.setScore(new NetworkScore.Builder().setLegacyInt(90).build());
mWiFiNetworkAgent.connect(true /* validated */);
// Make sure the factory sees the default network
final NetworkCapabilities filter = new NetworkCapabilities();
filter.addTransportType(TRANSPORT_CELLULAR);
filter.addCapability(NET_CAPABILITY_INTERNET);
filter.addCapability(NET_CAPABILITY_NOT_VCN_MANAGED);
final HandlerThread handlerThread = new HandlerThread("testNetworkFactoryRequests");
handlerThread.start();
final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(),
mServiceContext, "testFactory", filter, mCsHandlerThread);
testFactory.registerIgnoringScore();
testFactory.expectRequestAdd();
testFactory.register();
mWiFiNetworkAgent.adjustScore(20); // exceed the maximum score
expectNoRequestChanged(testFactory); // still seeing the request
final MockNetworkFactory testFactoryAll = new MockNetworkFactory(handlerThread.getLooper(),
mServiceContext, "testFactoryAll", filter, mCsHandlerThread);
testFactoryAll.registerIgnoringScore();
// The regular test factory should not see the request, because WiFi is stronger than cell.
expectNoRequestChanged(testFactory);
// With ignoringScore though the request is seen.
testFactoryAll.expectRequestAdd();
// The legacy int will be ignored anyway, set the only other knob to true
mWiFiNetworkAgent.setScore(new NetworkScore.Builder().setLegacyInt(110)
.setTransportPrimary(true).build());
expectNoRequestChanged(testFactory); // still not seeing the request
expectNoRequestChanged(testFactoryAll); // still seeing the request
mWiFiNetworkAgent.disconnect();
}
@@ -6075,7 +6086,8 @@ public class ConnectivityServiceTest {
// called again, it does. For example, connect Ethernet, but with a low score, such that it
// does not become the default network.
mEthernetNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET);
mEthernetNetworkAgent.adjustScore(-40);
mEthernetNetworkAgent.setScore(
new NetworkScore.Builder().setLegacyInt(30).setExiting(true).build());
mEthernetNetworkAgent.connect(false);
waitForIdle();
verify(mStatsManager).notifyNetworkStatus(any(List.class),
@@ -6950,8 +6962,6 @@ public class ConnectivityServiceTest {
callback.expectAvailableCallbacksUnvalidated(mMockVpn);
callback.assertNoCallback();
assertTrue(mMockVpn.getAgent().getScore() > mEthernetNetworkAgent.getScore());
assertEquals(ConnectivityConstants.VPN_DEFAULT_SCORE, mMockVpn.getAgent().getScore());
assertEquals(mMockVpn.getNetwork(), mCm.getActiveNetwork());
NetworkCapabilities nc = mCm.getNetworkCapabilities(mMockVpn.getNetwork());
@@ -11811,14 +11821,14 @@ public class ConnectivityServiceTest {
new LinkProperties(), oemPaidNc);
oemPaidAgent.connect(true);
// The oemPaidAgent has score 50 (default for cell) so it beats what the oemPaidFactory can
// The oemPaidAgent has score 50/cell transport, so it beats what the oemPaidFactory can
// provide, therefore it loses the request.
oemPaidFactory.expectRequestRemove();
oemPaidFactory.assertRequestCountEquals(0);
expectNoRequestChanged(internetFactory);
internetFactory.assertRequestCountEquals(0);
oemPaidAgent.adjustScore(-30);
oemPaidAgent.setScore(new NetworkScore.Builder().setLegacyInt(20).setExiting(true).build());
// Now the that the agent is weak, the oemPaidFactory can beat the existing network for the
// OEM_PAID request. The internet factory however can't beat a network that has OEM_PAID
// for the preference request, so it doesn't see the request.
@@ -11848,7 +11858,8 @@ public class ConnectivityServiceTest {
// Now WiFi connects and it's unmetered, but it's weaker than cell.
mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
mWiFiNetworkAgent.adjustScore(-30); // Not the best Internet network, but unmetered
mWiFiNetworkAgent.setScore(new NetworkScore.Builder().setLegacyInt(30).setExiting(true)
.build()); // Not the best Internet network, but unmetered
mWiFiNetworkAgent.connect(true);
// The OEM_PAID preference prefers an unmetered network to an OEM_PAID network, so

View File

@@ -43,7 +43,7 @@ class NetworkRankerTest {
val nais = scores.map { makeNai(true, it) }
val bestNetwork = nais[2] // The one with the top score
val someRequest = mock(NetworkRequest::class.java)
assertEquals(bestNetwork, ranker.getBestNetwork(someRequest, nais))
assertEquals(bestNetwork, ranker.getBestNetwork(someRequest, nais, bestNetwork))
}
@Test
@@ -52,20 +52,20 @@ class NetworkRankerTest {
makeNai(false, 60), makeNai(true, 23), makeNai(false, 68))
val bestNetwork = nais[1] // Top score that's satisfying
val someRequest = mock(NetworkRequest::class.java)
assertEquals(bestNetwork, ranker.getBestNetwork(someRequest, nais))
assertEquals(bestNetwork, ranker.getBestNetwork(someRequest, nais, nais[1]))
}
@Test
fun testNoMatch() {
val nais = listOf(makeNai(false, 20), makeNai(false, 50), makeNai(false, 90))
val someRequest = mock(NetworkRequest::class.java)
assertNull(ranker.getBestNetwork(someRequest, nais))
assertNull(ranker.getBestNetwork(someRequest, nais, null))
}
@Test
fun testEmpty() {
val someRequest = mock(NetworkRequest::class.java)
assertNull(ranker.getBestNetwork(someRequest, emptyList()))
assertNull(ranker.getBestNetwork(someRequest, emptyList(), null))
}
// Make sure the ranker is "stable" (as in stable sort), that is, it always returns the FIRST
@@ -75,10 +75,10 @@ class NetworkRankerTest {
val nais1 = listOf(makeNai(true, 30), makeNai(true, 30), makeNai(true, 30),
makeNai(true, 30), makeNai(true, 30), makeNai(true, 30))
val someRequest = mock(NetworkRequest::class.java)
assertEquals(nais1[0], ranker.getBestNetwork(someRequest, nais1))
assertEquals(nais1[0], ranker.getBestNetwork(someRequest, nais1, nais1[0]))
val nais2 = listOf(makeNai(true, 30), makeNai(true, 50), makeNai(true, 20),
makeNai(true, 50), makeNai(true, 50), makeNai(true, 40))
assertEquals(nais2[1], ranker.getBestNetwork(someRequest, nais2))
assertEquals(nais2[1], ranker.getBestNetwork(someRequest, nais2, nais2[1]))
}
}