Move error count BPF map initialization into class Dependencies
The preparation for testing BpfCoordinator#dump().
Used to wrap error count BPF map access to avoid SYS_SECCOMP on some
test device. Ex: cuttlefish.
https://android-build.googleplex.com/builds/tests/view?invocationId=I01200010101524265&testResultId=TR03228338750686157
Bug: 255466740
Test: atest BpfCoordinatorTest
Change-Id: Id4405b70bc6d500b35943b4f81b88e7515fff3b0
diff --git a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java
index ebc9d26..6a5089d 100644
--- a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java
+++ b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java
@@ -403,6 +403,18 @@
return null;
}
}
+
+ /** Get error BPF map. */
+ @Nullable public IBpfMap<S32, S32> getBpfErrorMap() {
+ if (!isAtLeastS()) return null;
+ try {
+ return new BpfMap<>(TETHER_ERROR_MAP_PATH,
+ BpfMap.BPF_F_RDONLY, S32.class, S32.class);
+ } catch (ErrnoException e) {
+ Log.e(TAG, "Cannot create error map: " + e);
+ return null;
+ }
+ }
}
@VisibleForTesting
@@ -1287,13 +1299,15 @@
}
private void dumpCounters(@NonNull IndentingPrintWriter pw) {
- if (!mDeps.isAtLeastS()) {
- pw.println("No counter support");
- return;
- }
- try (IBpfMap<S32, S32> map = new BpfMap<>(TETHER_ERROR_MAP_PATH, BpfMap.BPF_F_RDONLY,
- S32.class, S32.class)) {
-
+ try (IBpfMap<S32, S32> map = mDeps.getBpfErrorMap()) {
+ if (map == null) {
+ pw.println("No error counter support");
+ return;
+ }
+ if (map.isEmpty()) {
+ pw.println("<empty>");
+ return;
+ }
map.forEach((k, v) -> {
String counterName;
try {
@@ -1307,7 +1321,7 @@
if (v.val > 0) pw.println(String.format("%s: %d", counterName, v.val));
});
} catch (ErrnoException | IOException e) {
- pw.println("Error dumping counter map: " + e);
+ pw.println("Error dumping error counter map: " + e);
}
}
diff --git a/Tethering/tests/unit/src/android/net/ip/IpServerTest.java b/Tethering/tests/unit/src/android/net/ip/IpServerTest.java
index 5f4454b..0f3159d 100644
--- a/Tethering/tests/unit/src/android/net/ip/IpServerTest.java
+++ b/Tethering/tests/unit/src/android/net/ip/IpServerTest.java
@@ -99,6 +99,7 @@
import com.android.net.module.util.InterfaceParams;
import com.android.net.module.util.NetworkStackConstants;
import com.android.net.module.util.SharedLog;
+import com.android.net.module.util.Struct.S32;
import com.android.net.module.util.bpf.Tether4Key;
import com.android.net.module.util.bpf.Tether4Value;
import com.android.net.module.util.bpf.TetherStatsKey;
@@ -197,6 +198,7 @@
@Mock private BpfMap<TetherStatsKey, TetherStatsValue> mBpfStatsMap;
@Mock private BpfMap<TetherLimitKey, TetherLimitValue> mBpfLimitMap;
@Mock private BpfMap<TetherDevKey, TetherDevValue> mBpfDevMap;
+ @Mock private BpfMap<S32, S32> mBpfErrorMap;
@Captor private ArgumentCaptor<DhcpServingParamsParcel> mDhcpParamsCaptor;
@@ -360,6 +362,11 @@
public BpfMap<TetherDevKey, TetherDevValue> getBpfDevMap() {
return mBpfDevMap;
}
+
+ @Nullable
+ public BpfMap<S32, S32> getBpfErrorMap() {
+ return mBpfErrorMap;
+ }
};
mBpfCoordinator = spy(new BpfCoordinator(mBpfDeps));
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 bbca565..620ab62 100644
--- a/Tethering/tests/unit/src/com/android/networkstack/tethering/BpfCoordinatorTest.java
+++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/BpfCoordinatorTest.java
@@ -99,6 +99,7 @@
import com.android.net.module.util.InterfaceParams;
import com.android.net.module.util.NetworkStackConstants;
import com.android.net.module.util.SharedLog;
+import com.android.net.module.util.Struct.S32;
import com.android.net.module.util.bpf.Tether4Key;
import com.android.net.module.util.bpf.Tether4Value;
import com.android.net.module.util.bpf.TetherStatsKey;
@@ -365,6 +366,7 @@
@Mock private IBpfMap<TetherDownstream6Key, Tether6Value> mBpfDownstream6Map;
@Mock private IBpfMap<TetherUpstream6Key, Tether6Value> mBpfUpstream6Map;
@Mock private IBpfMap<TetherDevKey, TetherDevValue> mBpfDevMap;
+ @Mock private IBpfMap<S32, S32> mBpfErrorMap;
// Late init since methods must be called by the thread that created this object.
private TestableNetworkStatsProviderCbBinder mTetherStatsProviderCb;
@@ -457,6 +459,11 @@
public IBpfMap<TetherDevKey, TetherDevValue> getBpfDevMap() {
return mBpfDevMap;
}
+
+ @Nullable
+ public IBpfMap<S32, S32> getBpfErrorMap() {
+ return mBpfErrorMap;
+ }
});
@Before public void setUp() {