[NFCT.TETHER.14] Clear the BPF maps in BpfCoordinator ctor am: 499d3cac73 am: 129077bd58

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/1623669

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I6d09c8f877c717670cf2e1462ec0146b1a0106dd
This commit is contained in:
Hungming Chen
2021-03-12 01:37:00 +00:00
committed by Automerger Merge Worker
3 changed files with 73 additions and 0 deletions

View File

@@ -84,12 +84,46 @@ public class BpfCoordinatorShimImpl
public BpfCoordinatorShimImpl(@NonNull final Dependencies deps) {
mLog = deps.getSharedLog().forSubComponent(TAG);
mBpfDownstream4Map = deps.getBpfDownstream4Map();
mBpfUpstream4Map = deps.getBpfUpstream4Map();
mBpfDownstream6Map = deps.getBpfDownstream6Map();
mBpfUpstream6Map = deps.getBpfUpstream6Map();
mBpfStatsMap = deps.getBpfStatsMap();
mBpfLimitMap = deps.getBpfLimitMap();
// Clear the stubs of the maps for handling the system service crash if any.
// Doesn't throw the exception and clear the stubs as many as possible.
try {
if (mBpfDownstream4Map != null) mBpfDownstream4Map.clear();
} catch (ErrnoException e) {
mLog.e("Could not clear mBpfDownstream4Map: " + e);
}
try {
if (mBpfUpstream4Map != null) mBpfUpstream4Map.clear();
} catch (ErrnoException e) {
mLog.e("Could not clear mBpfUpstream4Map: " + e);
}
try {
if (mBpfDownstream6Map != null) mBpfDownstream6Map.clear();
} catch (ErrnoException e) {
mLog.e("Could not clear mBpfDownstream6Map: " + e);
}
try {
if (mBpfUpstream6Map != null) mBpfUpstream6Map.clear();
} catch (ErrnoException e) {
mLog.e("Could not clear mBpfUpstream6Map: " + e);
}
try {
if (mBpfStatsMap != null) mBpfStatsMap.clear();
} catch (ErrnoException e) {
mLog.e("Could not clear mBpfStatsMap: " + e);
}
try {
if (mBpfLimitMap != null) mBpfLimitMap.clear();
} catch (ErrnoException e) {
mLog.e("Could not clear mBpfLimitMap: " + e);
}
}
@Override

View File

@@ -224,6 +224,7 @@ public class BpfCoordinator {
maybeSchedulePollingStats();
};
// TODO: add BpfMap<TetherDownstream64Key, TetherDownstream64Value> retrieving function.
@VisibleForTesting
public abstract static class Dependencies {
/** Get handler. */

View File

@@ -150,6 +150,12 @@ public class BpfCoordinatorTest {
// BpfMap#getValue treats that the entry is not found as no error.
return mMap.get(key);
}
@Override
public void clear() throws ErrnoException {
// TODO: consider using mocked #getFirstKey and #deleteEntry to implement.
mMap.clear();
}
};
@Mock private NetworkStatsManager mStatsManager;
@@ -986,6 +992,24 @@ public class BpfCoordinatorTest {
checkBpfDisabled();
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.R)
public void testBpfDisabledbyNoBpfDownstream4Map() throws Exception {
setupFunctioningNetdInterface();
doReturn(null).when(mDeps).getBpfDownstream4Map();
checkBpfDisabled();
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.R)
public void testBpfDisabledbyNoBpfUpstream4Map() throws Exception {
setupFunctioningNetdInterface();
doReturn(null).when(mDeps).getBpfUpstream4Map();
checkBpfDisabled();
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.R)
public void testBpfDisabledbyNoBpfStatsMap() throws Exception {
@@ -1004,6 +1028,20 @@ public class BpfCoordinatorTest {
checkBpfDisabled();
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.R)
public void testBpfMapClear() throws Exception {
setupFunctioningNetdInterface();
final BpfCoordinator coordinator = makeBpfCoordinator();
verify(mBpfDownstream4Map).clear();
verify(mBpfUpstream4Map).clear();
verify(mBpfDownstream6Map).clear();
verify(mBpfUpstream6Map).clear();
verify(mBpfStatsMap).clear();
verify(mBpfLimitMap).clear();
}
@Test
public void testTetheringConfigSetPollingInterval() throws Exception {
setupFunctioningNetdInterface();