From 499d3cac7365f60bf8446b7ddee3ccbe3b1dd83d Mon Sep 17 00:00:00 2001 From: Hungming Chen Date: Tue, 9 Mar 2021 16:03:48 +0800 Subject: [PATCH] [NFCT.TETHER.14] Clear the BPF maps in BpfCoordinator ctor Needed while the system server has recovered from crash. Test: atest TetheringCoverageTests Change-Id: Idbed4887f5396537f0d0df97de8b482266dffbb7 --- .../apishim/api31/BpfCoordinatorShimImpl.java | 34 +++++++++++++++++ .../tethering/BpfCoordinator.java | 1 + .../tethering/BpfCoordinatorTest.java | 38 +++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/Tethering/apishim/31/com/android/networkstack/tethering/apishim/api31/BpfCoordinatorShimImpl.java b/Tethering/apishim/31/com/android/networkstack/tethering/apishim/api31/BpfCoordinatorShimImpl.java index 4dc1c51f68..03d24433d2 100644 --- a/Tethering/apishim/31/com/android/networkstack/tethering/apishim/api31/BpfCoordinatorShimImpl.java +++ b/Tethering/apishim/31/com/android/networkstack/tethering/apishim/api31/BpfCoordinatorShimImpl.java @@ -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 diff --git a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java index 985328fe60..7c5271678f 100644 --- a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java +++ b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java @@ -224,6 +224,7 @@ public class BpfCoordinator { maybeSchedulePollingStats(); }; + // TODO: add BpfMap retrieving function. @VisibleForTesting public abstract static class Dependencies { /** Get handler. */ diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/BpfCoordinatorTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/BpfCoordinatorTest.java index 1270e50c42..64ae983505 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/BpfCoordinatorTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/BpfCoordinatorTest.java @@ -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();