From 264cac59bb8bc08c62e124e2b8a0e445f909e648 Mon Sep 17 00:00:00 2001 From: Hungming Chen Date: Thu, 23 Jun 2022 21:26:55 +0800 Subject: [PATCH] Use BpfDump.toBase64EncodedString to dump raw map Move base64 related bpf map function and constant to f/l/n. This is preparation for testing BPF map in ClatCoordinator. Test: atest EthernetTetheringTest Change-Id: Ieb1e0a0f128c9aa0d7c9f043e90eecf3c61252f5 --- .../tethering/BpfCoordinator.java | 31 +++++++------------ .../android/net/EthernetTetheringTest.java | 2 +- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java index c403548e3d..47de640563 100644 --- a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java +++ b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java @@ -51,7 +51,6 @@ import android.system.ErrnoException; import android.system.OsConstants; import android.text.TextUtils; import android.util.ArraySet; -import android.util.Base64; import android.util.Log; import android.util.SparseArray; @@ -61,6 +60,7 @@ import androidx.annotation.Nullable; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.IndentingPrintWriter; import com.android.modules.utils.build.SdkLevel; +import com.android.net.module.util.BpfDump; import com.android.net.module.util.BpfMap; import com.android.net.module.util.CollectionUtils; import com.android.net.module.util.InterfaceParams; @@ -125,9 +125,6 @@ public class BpfCoordinator { private static final String DUMPSYS_RAWMAP_ARG_STATS = "--stats"; private static final String DUMPSYS_RAWMAP_ARG_UPSTREAM4 = "--upstream4"; - // Using "," as a separator is safe because base64 characters are [0-9a-zA-Z/=+]. - private static final String DUMP_BASE64_DELIMITER = ","; - /** The names of all the BPF counters defined in bpf_tethering.h. */ public static final String[] sBpfCounterNames = getBpfCounterNames(); @@ -1078,18 +1075,6 @@ public class BpfCoordinator { } } - private String bpfMapEntryToBase64String( - final K key, final V value) { - final byte[] keyBytes = key.writeToBytes(); - final String keyBase64Str = Base64.encodeToString(keyBytes, Base64.DEFAULT) - .replace("\n", ""); - final byte[] valueBytes = value.writeToBytes(); - final String valueBase64Str = Base64.encodeToString(valueBytes, Base64.DEFAULT) - .replace("\n", ""); - - return keyBase64Str + DUMP_BASE64_DELIMITER + valueBase64Str; - } - private void dumpRawMap(BpfMap map, IndentingPrintWriter pw) throws ErrnoException { if (map == null) { @@ -1100,14 +1085,20 @@ public class BpfCoordinator { pw.println("No entries"); return; } - map.forEach((k, v) -> pw.println(bpfMapEntryToBase64String(k, v))); + map.forEach((k, v) -> pw.println(BpfDump.toBase64EncodedString(k, v))); } /** - * Dump raw BPF map in base64 encoded strings. For test only. - * Only allow to dump one map path once. - * Format: + * Dump raw BPF map into the base64 encoded strings ",". + * Allow to dump only one map path once. For test only. + * + * Usage: * $ dumpsys tethering bpfRawMap -- + * + * Output: + * , + * , + * .. */ public void dumpRawMap(@NonNull IndentingPrintWriter pw, @Nullable String[] args) { // TODO: consider checking the arg order that is after "bpfRawMap". Probably diff --git a/Tethering/tests/integration/src/android/net/EthernetTetheringTest.java b/Tethering/tests/integration/src/android/net/EthernetTetheringTest.java index 819936dddb..c49cf1ed76 100644 --- a/Tethering/tests/integration/src/android/net/EthernetTetheringTest.java +++ b/Tethering/tests/integration/src/android/net/EthernetTetheringTest.java @@ -32,6 +32,7 @@ import static android.system.OsConstants.IPPROTO_IP; import static android.system.OsConstants.IPPROTO_IPV6; import static android.system.OsConstants.IPPROTO_UDP; +import static com.android.net.module.util.BpfDump.BASE64_DELIMITER; import static com.android.net.module.util.ConnectivityUtils.isIPv6ULA; import static com.android.net.module.util.HexDump.dumpHexString; import static com.android.net.module.util.NetworkStackConstants.ETHER_TYPE_IPV4; @@ -156,7 +157,6 @@ public class EthernetTetheringTest { private static final String DUMPSYS_TETHERING_RAWMAP_ARG = "bpfRawMap"; private static final String DUMPSYS_RAWMAP_ARG_STATS = "--stats"; private static final String DUMPSYS_RAWMAP_ARG_UPSTREAM4 = "--upstream4"; - private static final String BASE64_DELIMITER = ","; private static final String LINE_DELIMITER = "\\n"; // version=6, traffic class=0x0, flowlabel=0x0;