Skip to content
Snippets Groups Projects
Commit 78c0d088 authored by Mark Chien's avatar Mark Chien Committed by Gerrit Code Review
Browse files

Merge "Random select started prefix range" into main

parents ed353e90 c0b0f616
No related branches found
No related tags found
No related merge requests found
...@@ -189,7 +189,10 @@ public class PrivateAddressCoordinator { ...@@ -189,7 +189,10 @@ public class PrivateAddressCoordinator {
return cachedAddress; return cachedAddress;
} }
for (IpPrefix prefixRange : mTetheringPrefixes) { final int prefixIndex = getStartedPrefixIndex();
for (int i = 0; i < mTetheringPrefixes.size(); i++) {
final IpPrefix prefixRange = mTetheringPrefixes.get(
(prefixIndex + i) % mTetheringPrefixes.size());
final LinkAddress newAddress = chooseDownstreamAddress(prefixRange); final LinkAddress newAddress = chooseDownstreamAddress(prefixRange);
if (newAddress != null) { if (newAddress != null) {
mDownstreams.add(ipServer); mDownstreams.add(ipServer);
...@@ -202,6 +205,28 @@ public class PrivateAddressCoordinator { ...@@ -202,6 +205,28 @@ public class PrivateAddressCoordinator {
return null; return null;
} }
private int getStartedPrefixIndex() {
if (!mConfig.isRandomPrefixBaseEnabled()) return 0;
final int random = getRandomInt() & 0xffffff;
// This is to select the starting prefix range (/8, /12, or /16) instead of the actual
// LinkAddress. To avoid complex operations in the selection logic and make the selected
// rate approximate consistency with that /8 is around 2^4 times of /12 and /12 is around
// 2^4 times of /16, we simply define a map between the value and the prefix value like
// this:
//
// Value 0 ~ 0xffff (65536/16777216 = 0.39%) -> 192.168.0.0/16
// Value 0x10000 ~ 0xfffff (983040/16777216 = 5.86%) -> 172.16.0.0/12
// Value 0x100000 ~ 0xffffff (15728640/16777216 = 93.7%) -> 10.0.0.0/8
if (random > 0xfffff) {
return 2;
} else if (random > 0xffff) {
return 1;
} else {
return 0;
}
}
private int getPrefixBaseAddress(final IpPrefix prefix) { private int getPrefixBaseAddress(final IpPrefix prefix) {
return inet4AddressToIntHTH((Inet4Address) prefix.getAddress()); return inet4AddressToIntHTH((Inet4Address) prefix.getAddress());
} }
......
...@@ -130,6 +130,8 @@ public class TetheringConfiguration { ...@@ -130,6 +130,8 @@ public class TetheringConfiguration {
public static final String TETHER_ENABLE_WEAR_TETHERING = public static final String TETHER_ENABLE_WEAR_TETHERING =
"tether_enable_wear_tethering"; "tether_enable_wear_tethering";
public static final String TETHER_FORCE_RANDOM_PREFIX_BASE_SELECTION =
"tether_force_random_prefix_base_selection";
/** /**
* Default value that used to periodic polls tether offload stats from tethering offload HAL * Default value that used to periodic polls tether offload stats from tethering offload HAL
* to make the data warnings work. * to make the data warnings work.
...@@ -171,6 +173,7 @@ public class TetheringConfiguration { ...@@ -171,6 +173,7 @@ public class TetheringConfiguration {
private final int mP2pLeasesSubnetPrefixLength; private final int mP2pLeasesSubnetPrefixLength;
private final boolean mEnableWearTethering; private final boolean mEnableWearTethering;
private final boolean mRandomPrefixBase;
private final int mUsbTetheringFunction; private final int mUsbTetheringFunction;
protected final ContentResolver mContentResolver; protected final ContentResolver mContentResolver;
...@@ -288,6 +291,8 @@ public class TetheringConfiguration { ...@@ -288,6 +291,8 @@ public class TetheringConfiguration {
mEnableWearTethering = shouldEnableWearTethering(ctx); mEnableWearTethering = shouldEnableWearTethering(ctx);
mRandomPrefixBase = mDeps.isFeatureEnabled(ctx, TETHER_FORCE_RANDOM_PREFIX_BASE_SELECTION);
configLog.log(toString()); configLog.log(toString());
} }
...@@ -376,6 +381,10 @@ public class TetheringConfiguration { ...@@ -376,6 +381,10 @@ public class TetheringConfiguration {
return mEnableWearTethering; return mEnableWearTethering;
} }
public boolean isRandomPrefixBaseEnabled() {
return mRandomPrefixBase;
}
/** Does the dumping.*/ /** Does the dumping.*/
public void dump(PrintWriter pw) { public void dump(PrintWriter pw) {
pw.print("activeDataSubId: "); pw.print("activeDataSubId: ");
...@@ -426,6 +435,9 @@ public class TetheringConfiguration { ...@@ -426,6 +435,9 @@ public class TetheringConfiguration {
pw.print("mUsbTetheringFunction: "); pw.print("mUsbTetheringFunction: ");
pw.println(isUsingNcm() ? "NCM" : "RNDIS"); pw.println(isUsingNcm() ? "NCM" : "RNDIS");
pw.print("mRandomPrefixBase: ");
pw.println(mRandomPrefixBase);
} }
/** Returns the string representation of this object.*/ /** Returns the string representation of this object.*/
......
...@@ -30,6 +30,7 @@ import static com.android.networkstack.tethering.util.PrefixUtils.asIpPrefix; ...@@ -30,6 +30,7 @@ import static com.android.networkstack.tethering.util.PrefixUtils.asIpPrefix;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset; import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
...@@ -104,6 +105,7 @@ public final class PrivateAddressCoordinatorTest { ...@@ -104,6 +105,7 @@ public final class PrivateAddressCoordinatorTest {
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mConnectivityMgr); when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mConnectivityMgr);
when(mConnectivityMgr.getAllNetworks()).thenReturn(mAllNetworks); when(mConnectivityMgr.getAllNetworks()).thenReturn(mAllNetworks);
when(mConfig.shouldEnableWifiP2pDedicatedIp()).thenReturn(false); when(mConfig.shouldEnableWifiP2pDedicatedIp()).thenReturn(false);
when(mConfig.isRandomPrefixBaseEnabled()).thenReturn(false);
setUpIpServers(); setUpIpServers();
mPrivateAddressCoordinator = spy(new PrivateAddressCoordinator(mContext, mConfig)); mPrivateAddressCoordinator = spy(new PrivateAddressCoordinator(mContext, mConfig));
} }
...@@ -572,4 +574,41 @@ public final class PrivateAddressCoordinatorTest { ...@@ -572,4 +574,41 @@ public final class PrivateAddressCoordinatorTest {
assertEquals("Wrong local hotspot prefix: ", new LinkAddress("192.168.134.5/24"), assertEquals("Wrong local hotspot prefix: ", new LinkAddress("192.168.134.5/24"),
localHotspotAddress); localHotspotAddress);
} }
@Test
public void testStartedPrefixRange() throws Exception {
when(mConfig.isRandomPrefixBaseEnabled()).thenReturn(true);
startedPrefixBaseTest("192.168.0.0/16", 0);
startedPrefixBaseTest("192.168.0.0/16", 1);
startedPrefixBaseTest("192.168.0.0/16", 0xffff);
startedPrefixBaseTest("172.16.0.0/12", 0x10000);
startedPrefixBaseTest("172.16.0.0/12", 0x11111);
startedPrefixBaseTest("172.16.0.0/12", 0xfffff);
startedPrefixBaseTest("10.0.0.0/8", 0x100000);
startedPrefixBaseTest("10.0.0.0/8", 0x1fffff);
startedPrefixBaseTest("10.0.0.0/8", 0xffffff);
startedPrefixBaseTest("192.168.0.0/16", 0x1000000);
}
private void startedPrefixBaseTest(final String expected, final int randomIntForPrefixBase)
throws Exception {
mPrivateAddressCoordinator = spy(new PrivateAddressCoordinator(mContext, mConfig));
when(mPrivateAddressCoordinator.getRandomInt()).thenReturn(randomIntForPrefixBase);
final LinkAddress address = requestDownstreamAddress(mHotspotIpServer,
CONNECTIVITY_SCOPE_GLOBAL, false /* useLastAddress */);
final IpPrefix prefixBase = new IpPrefix(expected);
assertTrue(address + " is not part of " + prefixBase,
prefixBase.containsPrefix(asIpPrefix(address)));
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment