Merge "Use BpfDump.toBase64EncodedString to dump raw map"

This commit is contained in:
Nucca Chen
2022-07-06 12:04:54 +00:00
committed by Gerrit Code Review
2 changed files with 12 additions and 21 deletions

View File

@@ -51,7 +51,6 @@ import android.system.ErrnoException;
import android.system.OsConstants; import android.system.OsConstants;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.ArraySet; import android.util.ArraySet;
import android.util.Base64;
import android.util.Log; import android.util.Log;
import android.util.SparseArray; import android.util.SparseArray;
@@ -61,6 +60,7 @@ import androidx.annotation.Nullable;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.IndentingPrintWriter; import com.android.internal.util.IndentingPrintWriter;
import com.android.modules.utils.build.SdkLevel; 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.BpfMap;
import com.android.net.module.util.CollectionUtils; import com.android.net.module.util.CollectionUtils;
import com.android.net.module.util.InterfaceParams; 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_STATS = "--stats";
private static final String DUMPSYS_RAWMAP_ARG_UPSTREAM4 = "--upstream4"; 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. */ /** The names of all the BPF counters defined in bpf_tethering.h. */
public static final String[] sBpfCounterNames = getBpfCounterNames(); public static final String[] sBpfCounterNames = getBpfCounterNames();
@@ -1080,18 +1077,6 @@ public class BpfCoordinator {
} }
} }
private <K extends Struct, V extends Struct> 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 <K extends Struct, V extends Struct> void dumpRawMap(BpfMap<K, V> map, private <K extends Struct, V extends Struct> void dumpRawMap(BpfMap<K, V> map,
IndentingPrintWriter pw) throws ErrnoException { IndentingPrintWriter pw) throws ErrnoException {
if (map == null) { if (map == null) {
@@ -1102,14 +1087,20 @@ public class BpfCoordinator {
pw.println("No entries"); pw.println("No entries");
return; 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. * Dump raw BPF map into the base64 encoded strings "<base64 key>,<base64 value>".
* Only allow to dump one map path once. * Allow to dump only one map path once. For test only.
* Format: *
* Usage:
* $ dumpsys tethering bpfRawMap --<map name> * $ dumpsys tethering bpfRawMap --<map name>
*
* Output:
* <base64 encoded key #1>,<base64 encoded value #1>
* <base64 encoded key #2>,<base64 encoded value #2>
* ..
*/ */
public void dumpRawMap(@NonNull IndentingPrintWriter pw, @Nullable String[] args) { public void dumpRawMap(@NonNull IndentingPrintWriter pw, @Nullable String[] args) {
// TODO: consider checking the arg order that <map name> is after "bpfRawMap". Probably // TODO: consider checking the arg order that <map name> is after "bpfRawMap". Probably

View File

@@ -32,6 +32,7 @@ import static android.system.OsConstants.IPPROTO_IP;
import static android.system.OsConstants.IPPROTO_IPV6; import static android.system.OsConstants.IPPROTO_IPV6;
import static android.system.OsConstants.IPPROTO_UDP; 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.ConnectivityUtils.isIPv6ULA;
import static com.android.net.module.util.HexDump.dumpHexString; import static com.android.net.module.util.HexDump.dumpHexString;
import static com.android.net.module.util.NetworkStackConstants.ETHER_TYPE_IPV4; 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_TETHERING_RAWMAP_ARG = "bpfRawMap";
private static final String DUMPSYS_RAWMAP_ARG_STATS = "--stats"; private static final String DUMPSYS_RAWMAP_ARG_STATS = "--stats";
private static final String DUMPSYS_RAWMAP_ARG_UPSTREAM4 = "--upstream4"; private static final String DUMPSYS_RAWMAP_ARG_UPSTREAM4 = "--upstream4";
private static final String BASE64_DELIMITER = ",";
private static final String LINE_DELIMITER = "\\n"; private static final String LINE_DELIMITER = "\\n";
// version=6, traffic class=0x0, flowlabel=0x0; // version=6, traffic class=0x0, flowlabel=0x0;