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
This commit is contained in:
Hungming Chen
2022-10-26 10:17:36 +08:00
parent de5fb7370b
commit 771bc597b6
3 changed files with 36 additions and 8 deletions

View File

@@ -403,6 +403,18 @@ public class BpfCoordinator {
return null; 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 @VisibleForTesting
@@ -1287,13 +1299,15 @@ public class BpfCoordinator {
} }
private void dumpCounters(@NonNull IndentingPrintWriter pw) { private void dumpCounters(@NonNull IndentingPrintWriter pw) {
if (!mDeps.isAtLeastS()) { try (IBpfMap<S32, S32> map = mDeps.getBpfErrorMap()) {
pw.println("No counter support"); if (map == null) {
return; pw.println("No error counter support");
} return;
try (IBpfMap<S32, S32> map = new BpfMap<>(TETHER_ERROR_MAP_PATH, BpfMap.BPF_F_RDONLY, }
S32.class, S32.class)) { if (map.isEmpty()) {
pw.println("<empty>");
return;
}
map.forEach((k, v) -> { map.forEach((k, v) -> {
String counterName; String counterName;
try { try {
@@ -1307,7 +1321,7 @@ public class BpfCoordinator {
if (v.val > 0) pw.println(String.format("%s: %d", counterName, v.val)); if (v.val > 0) pw.println(String.format("%s: %d", counterName, v.val));
}); });
} catch (ErrnoException | IOException e) { } catch (ErrnoException | IOException e) {
pw.println("Error dumping counter map: " + e); pw.println("Error dumping error counter map: " + e);
} }
} }

View File

@@ -99,6 +99,7 @@ import com.android.net.module.util.BpfMap;
import com.android.net.module.util.InterfaceParams; import com.android.net.module.util.InterfaceParams;
import com.android.net.module.util.NetworkStackConstants; import com.android.net.module.util.NetworkStackConstants;
import com.android.net.module.util.SharedLog; 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.Tether4Key;
import com.android.net.module.util.bpf.Tether4Value; import com.android.net.module.util.bpf.Tether4Value;
import com.android.net.module.util.bpf.TetherStatsKey; import com.android.net.module.util.bpf.TetherStatsKey;
@@ -197,6 +198,7 @@ public class IpServerTest {
@Mock private BpfMap<TetherStatsKey, TetherStatsValue> mBpfStatsMap; @Mock private BpfMap<TetherStatsKey, TetherStatsValue> mBpfStatsMap;
@Mock private BpfMap<TetherLimitKey, TetherLimitValue> mBpfLimitMap; @Mock private BpfMap<TetherLimitKey, TetherLimitValue> mBpfLimitMap;
@Mock private BpfMap<TetherDevKey, TetherDevValue> mBpfDevMap; @Mock private BpfMap<TetherDevKey, TetherDevValue> mBpfDevMap;
@Mock private BpfMap<S32, S32> mBpfErrorMap;
@Captor private ArgumentCaptor<DhcpServingParamsParcel> mDhcpParamsCaptor; @Captor private ArgumentCaptor<DhcpServingParamsParcel> mDhcpParamsCaptor;
@@ -360,6 +362,11 @@ public class IpServerTest {
public BpfMap<TetherDevKey, TetherDevValue> getBpfDevMap() { public BpfMap<TetherDevKey, TetherDevValue> getBpfDevMap() {
return mBpfDevMap; return mBpfDevMap;
} }
@Nullable
public BpfMap<S32, S32> getBpfErrorMap() {
return mBpfErrorMap;
}
}; };
mBpfCoordinator = spy(new BpfCoordinator(mBpfDeps)); mBpfCoordinator = spy(new BpfCoordinator(mBpfDeps));

View File

@@ -99,6 +99,7 @@ import com.android.net.module.util.IBpfMap;
import com.android.net.module.util.InterfaceParams; import com.android.net.module.util.InterfaceParams;
import com.android.net.module.util.NetworkStackConstants; import com.android.net.module.util.NetworkStackConstants;
import com.android.net.module.util.SharedLog; 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.Tether4Key;
import com.android.net.module.util.bpf.Tether4Value; import com.android.net.module.util.bpf.Tether4Value;
import com.android.net.module.util.bpf.TetherStatsKey; import com.android.net.module.util.bpf.TetherStatsKey;
@@ -365,6 +366,7 @@ public class BpfCoordinatorTest {
@Mock private IBpfMap<TetherDownstream6Key, Tether6Value> mBpfDownstream6Map; @Mock private IBpfMap<TetherDownstream6Key, Tether6Value> mBpfDownstream6Map;
@Mock private IBpfMap<TetherUpstream6Key, Tether6Value> mBpfUpstream6Map; @Mock private IBpfMap<TetherUpstream6Key, Tether6Value> mBpfUpstream6Map;
@Mock private IBpfMap<TetherDevKey, TetherDevValue> mBpfDevMap; @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. // Late init since methods must be called by the thread that created this object.
private TestableNetworkStatsProviderCbBinder mTetherStatsProviderCb; private TestableNetworkStatsProviderCbBinder mTetherStatsProviderCb;
@@ -457,6 +459,11 @@ public class BpfCoordinatorTest {
public IBpfMap<TetherDevKey, TetherDevValue> getBpfDevMap() { public IBpfMap<TetherDevKey, TetherDevValue> getBpfDevMap() {
return mBpfDevMap; return mBpfDevMap;
} }
@Nullable
public IBpfMap<S32, S32> getBpfErrorMap() {
return mBpfErrorMap;
}
}); });
@Before public void setUp() { @Before public void setUp() {