Merge "Clatd: add dump raw BPF map support" into main am: 3812c394ba

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/2632311

Change-Id: I4844845e72cdb27ebb0d0d72c0e0770633ad6f56
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Maciej Żenczykowski 2024-05-31 08:14:18 +00:00 • committed by Automerger Merge Worker
commit 15bca40a64
5 changed files with 68 additions and 11 deletions

View file

@ -1362,7 +1362,7 @@ public class BpfCoordinator {
if (CollectionUtils.contains(args, DUMPSYS_RAWMAP_ARG_STATS)) {
try (IBpfMap<TetherStatsKey, TetherStatsValue> statsMap = mDeps.getBpfStatsMap()) {
BpfDump.dumpRawMap(statsMap, pw);
} catch (ErrnoException | IOException e) {
} catch (IOException e) {
pw.println("Error dumping stats map: " + e);
}
return;
@ -1370,7 +1370,7 @@ public class BpfCoordinator {
if (CollectionUtils.contains(args, DUMPSYS_RAWMAP_ARG_UPSTREAM4)) {
try (IBpfMap<Tether4Key, Tether4Value> upstreamMap = mDeps.getBpfUpstream4Map()) {
BpfDump.dumpRawMap(upstreamMap, pw);
} catch (ErrnoException | IOException e) {
} catch (IOException e) {
pw.println("Error dumping IPv4 map: " + e);
}
return;

View file

@ -403,6 +403,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
private static final String NETWORK_ARG = "networks";
private static final String REQUEST_ARG = "requests";
private static final String TRAFFICCONTROLLER_ARG = "trafficcontroller";
public static final String CLATEGRESS4RAWBPFMAP_ARG = "clatEgress4RawBpfMap";
public static final String CLATINGRESS6RAWBPFMAP_ARG = "clatIngress6RawBpfMap";
private static final boolean DBG = true;
private static final boolean DDBG = Log.isLoggable(TAG, Log.DEBUG);
@ -4141,6 +4143,12 @@ public class ConnectivityService extends IConnectivityManager.Stub
boolean verbose = !CollectionUtils.contains(args, SHORT_ARG);
dumpTrafficController(pw, fd, verbose);
return;
} else if (CollectionUtils.contains(args, CLATEGRESS4RAWBPFMAP_ARG)) {
dumpClatBpfRawMap(pw, true /* isEgress4Map */);
return;
} else if (CollectionUtils.contains(args, CLATINGRESS6RAWBPFMAP_ARG)) {
dumpClatBpfRawMap(pw, false /* isEgress4Map */);
return;
}
pw.println("NetworkProviders for:");
@ -4400,6 +4408,15 @@ public class ConnectivityService extends IConnectivityManager.Stub
}
}
private void dumpClatBpfRawMap(IndentingPrintWriter pw, boolean isEgress4Map) {
for (NetworkAgentInfo nai : networksSortedById()) {
if (nai.clatd != null) {
nai.clatd.dumpRawBpfMap(pw, isEgress4Map);
break;
}
}
}
private void dumpAllRequestInfoLogsToLogcat() {
try (PrintWriter logPw = new PrintWriter(new Writer() {
@Override

View file

@ -41,6 +41,7 @@ import androidx.annotation.RequiresApi;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.IndentingPrintWriter;
import com.android.net.module.util.BpfDump;
import com.android.net.module.util.BpfMap;
import com.android.net.module.util.IBpfMap;
import com.android.net.module.util.InterfaceParams;
@ -793,6 +794,29 @@ public class ClatCoordinator {
}
}
/**
* Dump raw BPF map into base64 encoded strings {@literal "<base64 key>,<base64 value>"}.
* Allow to dump only one map in each call. For test only.
*
* @param pw print writer.
* @param isEgress4Map whether to dump the egress4 map (true) or the ingress6 map (false).
*
* Usage:
* $ dumpsys connectivity {clatEgress4RawBpfMap|clatIngress6RawBpfMap}
*
* Output:
* {@literal <base64 encoded key #1>,<base64 encoded value #1>}
* {@literal <base64 encoded key #2>,<base64 encoded value #2>}
* ..
*/
public void dumpRawMap(@NonNull IndentingPrintWriter pw, boolean isEgress4Map) {
if (isEgress4Map) {
BpfDump.dumpRawMap(mEgressMap, pw);
} else {
BpfDump.dumpRawMap(mIngressMap, pw);
}
}
/**
* Dump the coordinator information.
*

View file

@ -622,6 +622,18 @@ public class Nat464Xlat {
}
}
/**
* Dump the raw BPF maps in 464XLAT
*
* @param pw print writer.
* @param isEgress4Map whether to dump the egress4 map (true) or the ingress6 map (false).
*/
public void dumpRawBpfMap(IndentingPrintWriter pw, boolean isEgress4Map) {
if (SdkLevel.isAtLeastT()) {
mClatCoordinator.dumpRawMap(pw, isEgress4Map);
}
}
@Override
public String toString() {
return "mBaseIface: " + mBaseIface + ", mIface: " + mIface + ", mState: " + mState;

View file

@ -84,16 +84,20 @@ public class BpfDump {
* Dump the BpfMap entries with base64 format encoding.
*/
public static <K extends Struct, V extends Struct> void dumpRawMap(IBpfMap<K, V> map,
PrintWriter pw) throws ErrnoException {
if (map == null) {
pw.println("BPF map is null");
return;
PrintWriter pw) {
try {
if (map == null) {
pw.println("BPF map is null");
return;
}
if (map.isEmpty()) {
pw.println("No entries");
return;
}
map.forEach((k, v) -> pw.println(toBase64EncodedString(k, v)));
} catch (ErrnoException e) {
pw.println("Map dump end with error: " + Os.strerror(e.errno));
}
if (map.isEmpty()) {
pw.println("No entries");
return;
}
map.forEach((k, v) -> pw.println(toBase64EncodedString(k, v)));
}
/**