Write Data Saver setting to BPF map

The information is needed by modules who want to know whether a
specific UID is blocked by Data Saver feature.

1. Add a one-element map data_saver_enabled_map.
2. Update current data saver setting to the map.

Bug: 288340533
Test: atest FrameworksNetTests:android.net.connectivity.com.android.serv
er.BpfNetMapsTest
Test: atest bpf_existence_test

Change-Id: I981da4b569247c33cba2d365cb6f2691f673474e
This commit is contained in:
Ken Chen
2023-10-20 13:02:14 +08:00
parent 6b134f18f4
commit 243301748e
7 changed files with 123 additions and 0 deletions

View File

@@ -17,6 +17,9 @@
package com.android.server;
import static android.net.BpfNetMapsConstants.CURRENT_STATS_MAP_CONFIGURATION_KEY;
import static android.net.BpfNetMapsConstants.DATA_SAVER_ENABLED_KEY;
import static android.net.BpfNetMapsConstants.DATA_SAVER_DISABLED;
import static android.net.BpfNetMapsConstants.DATA_SAVER_ENABLED;
import static android.net.BpfNetMapsConstants.DOZABLE_MATCH;
import static android.net.BpfNetMapsConstants.HAPPY_BOX_MATCH;
import static android.net.BpfNetMapsConstants.IIF_MATCH;
@@ -141,6 +144,7 @@ public final class BpfNetMapsTest {
private final IBpfMap<S32, U8> mUidPermissionMap = new TestBpfMap<>(S32.class, U8.class);
private final IBpfMap<CookieTagMapKey, CookieTagMapValue> mCookieTagMap =
spy(new TestBpfMap<>(CookieTagMapKey.class, CookieTagMapValue.class));
private final IBpfMap<S32, U8> mDataSaverEnabledMap = new TestBpfMap<>(S32.class, U8.class);
@Before
public void setUp() throws Exception {
@@ -155,6 +159,8 @@ public final class BpfNetMapsTest {
BpfNetMaps.setUidOwnerMapForTest(mUidOwnerMap);
BpfNetMaps.setUidPermissionMapForTest(mUidPermissionMap);
BpfNetMaps.setCookieTagMapForTest(mCookieTagMap);
BpfNetMaps.setDataSaverEnabledMapForTest(mDataSaverEnabledMap);
mDataSaverEnabledMap.updateEntry(DATA_SAVER_ENABLED_KEY, new U8(DATA_SAVER_DISABLED));
mBpfNetMaps = new BpfNetMaps(mContext, mNetd, mDeps);
}
@@ -1155,6 +1161,21 @@ public final class BpfNetMapsTest {
assertDumpContains(getDump(), "cookie=123 tag=0x789 uid=456");
}
private void doTestDumpDataSaverConfig(final short value, final boolean expected)
throws Exception {
mDataSaverEnabledMap.updateEntry(DATA_SAVER_ENABLED_KEY, new U8(value));
assertDumpContains(getDump(),
"sDataSaverEnabledMap: " + expected);
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.S_V2)
public void testDumpDataSaverConfig() throws Exception {
doTestDumpDataSaverConfig(DATA_SAVER_DISABLED, false);
doTestDumpDataSaverConfig(DATA_SAVER_ENABLED, true);
doTestDumpDataSaverConfig((short) 2, true);
}
@Test
public void testGetUids() throws ErrnoException {
final int uid0 = TEST_UIDS[0];
@@ -1183,4 +1204,23 @@ public final class BpfNetMapsTest {
assertThrows(expected,
() -> mBpfNetMaps.getUidsWithAllowRuleOnAllowListChain(FIREWALL_CHAIN_OEM_DENY_1));
}
@Test
@IgnoreAfter(Build.VERSION_CODES.S_V2)
public void testSetDataSaverEnabledBeforeT() {
for (boolean enable : new boolean[] {true, false}) {
assertThrows(UnsupportedOperationException.class,
() -> mBpfNetMaps.setDataSaverEnabled(enable));
}
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.S_V2)
public void testSetDataSaverEnabled() throws Exception {
for (boolean enable : new boolean[] {true, false}) {
mBpfNetMaps.setDataSaverEnabled(enable);
assertEquals(enable ? DATA_SAVER_ENABLED : DATA_SAVER_DISABLED,
mDataSaverEnabledMap.getValue(DATA_SAVER_ENABLED_KEY).val);
}
}
}