From 4a3463b538b8b54e21b4cfa2f9f66ea3eafb5cf5 Mon Sep 17 00:00:00 2001 From: Dmitry Gruzd Date: Sun, 14 Apr 2024 01:25:15 +0300 Subject: [PATCH 1/3] netbpfload: Disable reboot on failure This allows to reach the boot animation instead just oem logo --- netbpfload/netbpfload.rc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbpfload/netbpfload.rc b/netbpfload/netbpfload.rc index 14181dc6fc..c6fd63d578 100644 --- a/netbpfload/netbpfload.rc +++ b/netbpfload/netbpfload.rc @@ -81,6 +81,6 @@ service bpfloader /system/bin/netbpfload # 'cannot prove return value is 0 or 1' or 'unsupported / unknown operation / helper', # 'invalid bpf_context access', etc. # - reboot_on_failure reboot,bpfloader-failed + # reboot_on_failure reboot,bpfloader-failed # we're not really updatable, but want to be able to load bpf programs shipped in apexes updatable From 76d7cb00e84d0da29da9ea9d101a3b1f81d31a24 Mon Sep 17 00:00:00 2001 From: z3DD3r Date: Mon, 20 May 2024 13:27:25 -0300 Subject: [PATCH 2/3] Support no-bpf usecase ChonDoit: Squash and adapt to TD-r37 --- DnsResolver/DnsBpfHelper.cpp | 22 +++++++++++++++---- ...id_server_connectivity_ClatCoordinator.cpp | 2 +- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/DnsResolver/DnsBpfHelper.cpp b/DnsResolver/DnsBpfHelper.cpp index de8bef5860..c5256f5ff2 100644 --- a/DnsResolver/DnsBpfHelper.cpp +++ b/DnsResolver/DnsBpfHelper.cpp @@ -38,9 +38,19 @@ base::Result DnsBpfHelper::init() { return base::Error(EOPNOTSUPP); } - RETURN_IF_RESULT_NOT_OK(mConfigurationMap.init(CONFIGURATION_MAP_PATH)); - RETURN_IF_RESULT_NOT_OK(mUidOwnerMap.init(UID_OWNER_MAP_PATH)); - RETURN_IF_RESULT_NOT_OK(mDataSaverEnabledMap.init(DATA_SAVER_ENABLED_MAP_PATH)); + auto ret1 = mConfigurationMap.init(CONFIGURATION_MAP_PATH); + if (!ret1.ok()) { + LOG(ERROR) << __func__ << ": Failed mConfigurationMap.init"; + } + auto ret2 = mUidOwnerMap.init(UID_OWNER_MAP_PATH); + if (!ret2.ok()) { + LOG(ERROR) << __func__ << ": Failed mConfigurationMap.init"; + } + auto ret3 = mDataSaverEnabledMap.init(DATA_SAVER_ENABLED_MAP_PATH); + if (!ret3.ok()) { + LOG(ERROR) << __func__ << ": Failed mConfigurationMap.init"; + } + return {}; } @@ -49,12 +59,16 @@ base::Result DnsBpfHelper::isUidNetworkingBlocked(uid_t uid, bool metered) if (!mConfigurationMap.isValid() || !mUidOwnerMap.isValid()) { LOG(ERROR) << __func__ << ": BPF maps are not ready. Forgot to call ADnsHelper_init?"; - return base::Error(EUNATCH); + return false; } auto enabledRules = mConfigurationMap.readValue(UID_RULES_CONFIGURATION_KEY); RETURN_IF_RESULT_NOT_OK(enabledRules); + if (!enabledRules.ok()) { + return false; + } + auto value = mUidOwnerMap.readValue(uid); uint32_t uidRules = value.ok() ? value.value().rule : 0; diff --git a/service/jni/com_android_server_connectivity_ClatCoordinator.cpp b/service/jni/com_android_server_connectivity_ClatCoordinator.cpp index 787ef8fd69..d32383cade 100644 --- a/service/jni/com_android_server_connectivity_ClatCoordinator.cpp +++ b/service/jni/com_android_server_connectivity_ClatCoordinator.cpp @@ -143,7 +143,7 @@ static void verifyClatPerms() { return; } - if (fatal) abort(); + // if (fatal) abort(); } #undef V From 9edfb2a4dd1ea511f2ac86485fd010da474da904 Mon Sep 17 00:00:00 2001 From: z3DD3r Date: Sat, 20 Apr 2024 19:12:46 +0300 Subject: [PATCH 3/3] Make use of BpfMap and BpfNetMaps safe ChonDoit: Squash and adapt to TD-r37 --- .../libs/libnetworkstats/BpfNetworkStats.cpp | 41 +++ .../src/com/android/server/BpfNetMaps.java | 272 ++++++++++-------- 2 files changed, 201 insertions(+), 112 deletions(-) diff --git a/service-t/native/libs/libnetworkstats/BpfNetworkStats.cpp b/service-t/native/libs/libnetworkstats/BpfNetworkStats.cpp index 8d6994818b..e1ea85ed7a 100644 --- a/service-t/native/libs/libnetworkstats/BpfNetworkStats.cpp +++ b/service-t/native/libs/libnetworkstats/BpfNetworkStats.cpp @@ -53,6 +53,10 @@ int bpfGetUidStatsInternal(uid_t uid, StatsValue* stats, int bpfGetUidStats(uid_t uid, StatsValue* stats) { static BpfMapRO appUidStatsMap(APP_UID_STATS_MAP_PATH); + if (!appUidStatsMap.isValid() || !appUidStatsMap.isOk()) { + ALOGE("get map fd failed: %s", strerror(errno)); + return -errno; + } return bpfGetUidStatsInternal(uid, stats, appUidStatsMap); } @@ -85,7 +89,15 @@ int bpfGetIfaceStatsInternal(const char* iface, StatsValue* stats, int bpfGetIfaceStats(const char* iface, StatsValue* stats) { static BpfMapRO ifaceStatsMap(IFACE_STATS_MAP_PATH); + if (!ifaceStatsMap.isValid() || !ifaceStatsMap.isOk()) { + ALOGE("get map fd failed: %s", strerror(errno)); + return -errno; + } static BpfMapRO ifaceIndexNameMap(IFACE_INDEX_NAME_MAP_PATH); + if (!ifaceIndexNameMap.isValid() || !ifaceIndexNameMap.isOk()) { + ALOGE("get map fd failed: %s", strerror(errno)); + return -errno; + } return bpfGetIfaceStatsInternal(iface, stats, ifaceStatsMap, ifaceIndexNameMap); } @@ -102,6 +114,10 @@ int bpfGetIfIndexStatsInternal(uint32_t ifindex, StatsValue* stats, int bpfGetIfIndexStats(int ifindex, StatsValue* stats) { static BpfMapRO ifaceStatsMap(IFACE_STATS_MAP_PATH); + if (!ifaceStatsMap.isValid() || !ifaceStatsMap.isOk()) { + ALOGE("get map fd failed: %s", strerror(errno)); + return -errno; + } return bpfGetIfIndexStatsInternal(ifindex, stats, ifaceStatsMap); } @@ -167,9 +183,25 @@ int parseBpfNetworkStatsDetailInternal(std::vector& lines, int parseBpfNetworkStatsDetail(std::vector* lines) { static BpfMapRO ifaceIndexNameMap(IFACE_INDEX_NAME_MAP_PATH); + if (!ifaceIndexNameMap.isValid() || !ifaceIndexNameMap.isOk()) { + ALOGE("get map fd failed: %s", strerror(errno)); + return -errno; + } static BpfMapRO configurationMap(CONFIGURATION_MAP_PATH); + if (!configurationMap.isValid() || !configurationMap.isOk()) { + ALOGE("get map fd failed: %s", strerror(errno)); + return -errno; + } static BpfMap statsMapA(STATS_MAP_A_PATH); + if (!statsMapA.isValid() || !statsMapA.isOk()) { + ALOGE("get map fd failed: %s", strerror(errno)); + return -errno; + } static BpfMap statsMapB(STATS_MAP_B_PATH); + if (!statsMapB.isValid() || !statsMapB.isOk()) { + ALOGE("get map fd failed: %s", strerror(errno)); + return -errno; + } if (!configurationMap.isOk()) return -1; auto configuration = configurationMap.readValue(CURRENT_STATS_MAP_CONFIGURATION_KEY); if (!configuration.ok()) { @@ -244,7 +276,16 @@ int parseBpfNetworkStatsDevInternal(std::vector& lines, int parseBpfNetworkStatsDev(std::vector* lines) { static BpfMapRO ifaceIndexNameMap(IFACE_INDEX_NAME_MAP_PATH); + if (!ifaceIndexNameMap.isValid() || !ifaceIndexNameMap.isOk()) { + ALOGE("get map fd failed: %s", strerror(errno)); + return -errno; + } static BpfMapRO ifaceStatsMap(IFACE_STATS_MAP_PATH); + if (!ifaceStatsMap.isValid() || !ifaceStatsMap.isOk()) { + ALOGE("get map fd failed: %s", strerror(errno)); + return -errno; + } + return parseBpfNetworkStatsDevInternal(*lines, ifaceStatsMap, ifaceIndexNameMap); } diff --git a/service/src/com/android/server/BpfNetMaps.java b/service/src/com/android/server/BpfNetMaps.java index 9b2202b295..ba0e5e4844 100644 --- a/service/src/com/android/server/BpfNetMaps.java +++ b/service/src/com/android/server/BpfNetMaps.java @@ -243,14 +243,16 @@ private static void initBpfMaps() { sConfigurationMap = getConfigurationMap(); } try { - sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, - new U32(UID_RULES_DEFAULT_CONFIGURATION)); + if (sConfigurationMap != null) + sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, + new U32(UID_RULES_DEFAULT_CONFIGURATION)); } catch (ErrnoException e) { throw new IllegalStateException("Failed to initialize uid rules configuration", e); } try { - sConfigurationMap.updateEntry(CURRENT_STATS_MAP_CONFIGURATION_KEY, - new U32(STATS_SELECT_MAP_A)); + if (sConfigurationMap != null) + sConfigurationMap.updateEntry(CURRENT_STATS_MAP_CONFIGURATION_KEY, + new U32(STATS_SELECT_MAP_A)); } catch (ErrnoException e) { throw new IllegalStateException("Failed to initialize current stats configuration", e); } @@ -259,7 +261,8 @@ private static void initBpfMaps() { sUidOwnerMap = getUidOwnerMap(); } try { - sUidOwnerMap.clear(); + if (sUidOwnerMap != null) + sUidOwnerMap.clear(); } catch (ErrnoException e) { throw new IllegalStateException("Failed to initialize uid owner map", e); } @@ -276,7 +279,8 @@ private static void initBpfMaps() { sDataSaverEnabledMap = getDataSaverEnabledMap(); } try { - sDataSaverEnabledMap.updateEntry(DATA_SAVER_ENABLED_KEY, new U8(DATA_SAVER_DISABLED)); + if (sDataSaverEnabledMap != null) + sDataSaverEnabledMap.updateEntry(DATA_SAVER_ENABLED_KEY, new U8(DATA_SAVER_DISABLED)); } catch (ErrnoException e) { throw new IllegalStateException("Failed to initialize data saver configuration", e); } @@ -285,7 +289,8 @@ private static void initBpfMaps() { sIngressDiscardMap = getIngressDiscardMap(); } try { - sIngressDiscardMap.clear(); + if (sIngressDiscardMap != null) + sIngressDiscardMap.clear(); } catch (ErrnoException e) { throw new IllegalStateException("Failed to initialize ingress discard map", e); } @@ -382,24 +387,26 @@ private void throwIfPreT(final String msg) { private void removeRule(final int uid, final long match, final String caller) { try { - synchronized (sUidOwnerMap) { - final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new S32(uid)); + if (sUidOwnerMap != null) { + synchronized (sUidOwnerMap) { + final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new S32(uid)); - if (oldMatch == null) { - throw new ServiceSpecificException(ENOENT, - "sUidOwnerMap does not have entry for uid: " + uid); - } + if (oldMatch == null) { + throw new ServiceSpecificException(ENOENT, + "sUidOwnerMap does not have entry for uid: " + uid); + } - final UidOwnerValue newMatch = new UidOwnerValue( - (match == IIF_MATCH) ? 0 : oldMatch.iif, - oldMatch.rule & ~match - ); + final UidOwnerValue newMatch = new UidOwnerValue( + (match == IIF_MATCH) ? 0 : oldMatch.iif, + oldMatch.rule & ~match + ); - if (newMatch.rule == 0) { - sUidOwnerMap.deleteEntry(new S32(uid)); - } else { - sUidOwnerMap.updateEntry(new S32(uid), newMatch); - } + if (newMatch.rule == 0) { + sUidOwnerMap.deleteEntry(new S32(uid)); + } else { + sUidOwnerMap.updateEntry(new S32(uid), newMatch); + } + } } } catch (ErrnoException e) { throw new ServiceSpecificException(e.errno, @@ -414,22 +421,24 @@ private void addRule(final int uid, final long match, final int iif, final Strin } try { - synchronized (sUidOwnerMap) { - final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new S32(uid)); - - final UidOwnerValue newMatch; - if (oldMatch != null) { - newMatch = new UidOwnerValue( - (match == IIF_MATCH) ? iif : oldMatch.iif, - oldMatch.rule | match - ); - } else { - newMatch = new UidOwnerValue( - iif, - match - ); + if (sUidOwnerMap != null) { + synchronized (sUidOwnerMap) { + final UidOwnerValue oldMatch = sUidOwnerMap.getValue(new S32(uid)); + + final UidOwnerValue newMatch; + if (oldMatch != null) { + newMatch = new UidOwnerValue( + (match == IIF_MATCH) ? iif : oldMatch.iif, + oldMatch.rule | match + ); + } else { + newMatch = new UidOwnerValue( + iif, + match + ); + } + sUidOwnerMap.updateEntry(new S32(uid), newMatch); } - sUidOwnerMap.updateEntry(new S32(uid), newMatch); } } catch (ErrnoException e) { throw new ServiceSpecificException(e.errno, @@ -513,9 +522,11 @@ public void setChildChain(final int childChain, final boolean enable) { final long match = getMatchByFirewallChain(childChain); try { synchronized (sUidRulesConfigBpfMapLock) { - final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY); - final long newConfig = enable ? (config.val | match) : (config.val & ~match); - sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(newConfig)); + if (sConfigurationMap != null) { + final U32 config = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY); + final long newConfig = enable ? (config.val | match) : (config.val & ~match); + sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(newConfig)); + } } } catch (ErrnoException e) { throw new ServiceSpecificException(e.errno, @@ -538,7 +549,9 @@ public void setChildChain(final int childChain, final boolean enable) { @Deprecated @RequiresApi(Build.VERSION_CODES.TIRAMISU) public boolean isChainEnabled(final int childChain) { - return BpfNetMapsReader.isChainEnabled(sConfigurationMap, childChain); + if (sConfigurationMap != null) + return BpfNetMapsReader.isChainEnabled(sConfigurationMap, childChain); + return false; } private Set asSet(final int[] uids) { @@ -573,23 +586,25 @@ public void replaceUidChain(final int chain, final int[] uids) { final Set uidSet = asSet(uids); final Set uidSetToRemoveRule = new ArraySet<>(); try { - synchronized (sUidOwnerMap) { - sUidOwnerMap.forEach((uid, config) -> { - // config could be null if there is a concurrent entry deletion. - // http://b/220084230. But sUidOwnerMap update must be done while holding a - // lock, so this should not happen. - if (config == null) { - Log.wtf(TAG, "sUidOwnerMap entry was deleted while holding a lock"); - } else if (!uidSet.contains((int) uid.val) && (config.rule & match) != 0) { - uidSetToRemoveRule.add((int) uid.val); - } - }); + if (sUidOwnerMap != null) { + synchronized (sUidOwnerMap) { + sUidOwnerMap.forEach((uid, config) -> { + // config could be null if there is a concurrent entry deletion. + // http://b/220084230. But sUidOwnerMap update must be done while holding a + // lock, so this should not happen. + if (config == null) { + Log.wtf(TAG, "sUidOwnerMap entry was deleted while holding a lock"); + } else if (!uidSet.contains((int) uid.val) && (config.rule & match) != 0) { + uidSetToRemoveRule.add((int) uid.val); + } + }); - for (final int uid : uidSetToRemoveRule) { - removeRule(uid, match, "replaceUidChain"); - } - for (final int uid : uids) { - addRule(uid, match, "replaceUidChain"); + for (final int uid : uidSetToRemoveRule) { + removeRule(uid, match, "replaceUidChain"); + } + for (final int uid : uids) { + addRule(uid, match, "replaceUidChain"); + } } } } catch (ErrnoException | ServiceSpecificException e) { @@ -636,23 +651,27 @@ public void setUidRule(final int childChain, final int uid, final int firewallRu */ // TODO: Migrate the callers to use {@link BpfNetMapsReader#getUidRule} instead. public int getUidRule(final int childChain, final int uid) { - return BpfNetMapsReader.getUidRule(sUidOwnerMap, childChain, uid); + if (sUidOwnerMap != null) + return BpfNetMapsReader.getUidRule(sUidOwnerMap, childChain, uid); + return 0; } private Set getUidsMatchEnabled(final int childChain) throws ErrnoException { final long match = getMatchByFirewallChain(childChain); Set uids = new ArraySet<>(); - synchronized (sUidOwnerMap) { - sUidOwnerMap.forEach((uid, val) -> { - if (val == null) { - Log.wtf(TAG, "sUidOwnerMap entry was deleted while holding a lock"); - } else { - if ((val.rule & match) != 0) { - uids.add(uid.val); + if (sUidOwnerMap != null) { + synchronized (sUidOwnerMap) { + sUidOwnerMap.forEach((uid, val) -> { + if (val == null) { + Log.wtf(TAG, "sUidOwnerMap entry was deleted while holding a lock"); + } else { + if ((val.rule & match) != 0) { + uids.add(uid.val); + } } - } - }); - } + }); + } + } return uids; } @@ -801,13 +820,15 @@ public void swapActiveStatsMap() { if (sConfigurationMap == null) return; try { - synchronized (sCurrentStatsMapConfigLock) { - final long config = sConfigurationMap.getValue( - CURRENT_STATS_MAP_CONFIGURATION_KEY).val; - final long newConfig = (config == STATS_SELECT_MAP_A) - ? STATS_SELECT_MAP_B : STATS_SELECT_MAP_A; - sConfigurationMap.updateEntry(CURRENT_STATS_MAP_CONFIGURATION_KEY, - new U32(newConfig)); + if (sConfigurationMap != null) { + synchronized (sCurrentStatsMapConfigLock) { + final long config = sConfigurationMap.getValue( + CURRENT_STATS_MAP_CONFIGURATION_KEY).val; + final long newConfig = (config == STATS_SELECT_MAP_A) + ? STATS_SELECT_MAP_B : STATS_SELECT_MAP_A; + sConfigurationMap.updateEntry(CURRENT_STATS_MAP_CONFIGURATION_KEY, + new U32(newConfig)); + } } } catch (ErrnoException e) { throw new ServiceSpecificException(e.errno, "Failed to swap active stats map"); @@ -846,7 +867,8 @@ public void setNetPermForUids(final int permissions, final int[] uids) throws Re if (permissions == PERMISSION_UNINSTALLED || permissions == PERMISSION_INTERNET) { for (final int uid : uids) { try { - sUidPermissionMap.deleteEntry(new S32(uid)); + if (sUidPermissionMap != null) + sUidPermissionMap.deleteEntry(new S32(uid)); } catch (ErrnoException e) { Log.e(TAG, "Failed to remove uid " + uid + " from permission map: " + e); } @@ -856,7 +878,8 @@ public void setNetPermForUids(final int permissions, final int[] uids) throws Re for (final int uid : uids) { try { - sUidPermissionMap.updateEntry(new S32(uid), new U8((short) permissions)); + if (sUidPermissionMap != null) + sUidPermissionMap.updateEntry(new S32(uid), new U8((short) permissions)); } catch (ErrnoException e) { Log.e(TAG, "Failed to set permission " + permissions + " to uid " + uid + ": " + e); @@ -877,8 +900,12 @@ public void setDataSaverEnabled(boolean enable) { throwIfPreT("setDataSaverEnabled is not available on pre-T devices"); try { - final short config = enable ? DATA_SAVER_ENABLED : DATA_SAVER_DISABLED; - sDataSaverEnabledMap.updateEntry(DATA_SAVER_ENABLED_KEY, new U8(config)); + if (sDataSaverEnabledMap != null) { + if (sDataSaverEnabledMap != null) { + final short config = enable ? DATA_SAVER_ENABLED : DATA_SAVER_DISABLED; + sDataSaverEnabledMap.updateEntry(DATA_SAVER_ENABLED_KEY, new U8(config)); + } + } } catch (ErrnoException e) { throw new ServiceSpecificException(e.errno, "Unable to set data saver: " + Os.strerror(e.errno)); @@ -901,8 +928,9 @@ public void setIngressDiscardRule(final InetAddress address, final String iface) return; } try { - sIngressDiscardMap.updateEntry(new IngressDiscardKey(address), - new IngressDiscardValue(ifIndex, ifIndex)); + if (sIngressDiscardMap != null) + sIngressDiscardMap.updateEntry(new IngressDiscardKey(address), + new IngressDiscardValue(ifIndex, ifIndex)); } catch (ErrnoException e) { Log.e(TAG, "Failed to set ingress discard rule for " + address + "(" + iface + "), " + e); @@ -918,7 +946,8 @@ public void setIngressDiscardRule(final InetAddress address, final String iface) public void removeIngressDiscardRule(final InetAddress address) { throwIfPreT("removeIngressDiscardRule is not available on pre-T devices"); try { - sIngressDiscardMap.deleteEntry(new IngressDiscardKey(address)); + if (sIngressDiscardMap != null) + sIngressDiscardMap.deleteEntry(new IngressDiscardKey(address)); } catch (ErrnoException e) { Log.e(TAG, "Failed to remove ingress discard rule for " + address + ", " + e); } @@ -955,8 +984,12 @@ public int pullBpfMapInfoAtom(final int atomTag, final List data) { } try { - data.add(mDeps.buildStatsEvent(getMapSize(sCookieTagMap), getMapSize(sUidOwnerMap), - getMapSize(sUidPermissionMap))); + if (sUidOwnerMap != null && sCookieTagMap != null && sUidPermissionMap != null) { + data.add(mDeps.buildStatsEvent(getMapSize(sCookieTagMap), getMapSize(sUidOwnerMap), + getMapSize(sUidPermissionMap))); + } else { + return StatsManager.PULL_SKIP; + } } catch (ErrnoException e) { Log.e(TAG, "Failed to pull NETWORK_BPF_MAP_INFO atom: " + e); return StatsManager.PULL_SKIP; @@ -990,8 +1023,10 @@ private String permissionToString(int permissionMask) { private void dumpOwnerMatchConfig(final IndentingPrintWriter pw) { try { - final long match = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY).val; - pw.println("current ownerMatch configuration: " + match + " " + matchToString(match)); + if (sConfigurationMap != null) { + final long match = sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY).val; + pw.println("current ownerMatch configuration: " + match + " " + matchToString(match)); + } } catch (ErrnoException e) { pw.println("Failed to read ownerMatch configuration: " + e); } @@ -999,10 +1034,12 @@ private void dumpOwnerMatchConfig(final IndentingPrintWriter pw) { private void dumpCurrentStatsMapConfig(final IndentingPrintWriter pw) { try { - final long config = sConfigurationMap.getValue(CURRENT_STATS_MAP_CONFIGURATION_KEY).val; - final String currentStatsMap = - (config == STATS_SELECT_MAP_A) ? "SELECT_MAP_A" : "SELECT_MAP_B"; - pw.println("current statsMap configuration: " + config + " " + currentStatsMap); + if (sConfigurationMap != null) { + final long config = sConfigurationMap.getValue(CURRENT_STATS_MAP_CONFIGURATION_KEY).val; + final String currentStatsMap = + (config == STATS_SELECT_MAP_A) ? "SELECT_MAP_A" : "SELECT_MAP_B"; + pw.println("current statsMap configuration: " + config + " " + currentStatsMap); + } } catch (ErrnoException e) { pw.println("Falied to read current statsMap configuration: " + e); } @@ -1010,9 +1047,11 @@ private void dumpCurrentStatsMapConfig(final IndentingPrintWriter pw) { private void dumpDataSaverConfig(final IndentingPrintWriter pw) { try { - final short config = sDataSaverEnabledMap.getValue(DATA_SAVER_ENABLED_KEY).val; - // Any non-zero value converted from short to boolean is true by convention. - pw.println("sDataSaverEnabledMap: " + (config != DATA_SAVER_DISABLED)); + if (sDataSaverEnabledMap != null) { + final short config = sDataSaverEnabledMap.getValue(DATA_SAVER_ENABLED_KEY).val; + // Any non-zero value converted from short to boolean is true by convention. + pw.println("sDataSaverEnabledMap: " + (config != DATA_SAVER_DISABLED)); + } } catch (ErrnoException e) { pw.println("Failed to read data saver configuration: " + e); } @@ -1051,25 +1090,34 @@ public void dump(final IndentingPrintWriter pw, final FileDescriptor fd, boolean // NetworkStatsService also dumps CookieTagMap and NetworkStatsService is a right place // to dump CookieTagMap. But the TagSocketTest in CTS depends on this dump so the tests // need to be updated before remove the dump from BpfNetMaps. - BpfDump.dumpMap(sCookieTagMap, pw, "sCookieTagMap", - (key, value) -> "cookie=" + key.socketCookie - + " tag=0x" + Long.toHexString(value.tag) - + " uid=" + value.uid); - BpfDump.dumpMap(sUidOwnerMap, pw, "sUidOwnerMap", - (uid, match) -> { - if ((match.rule & IIF_MATCH) != 0) { - // TODO: convert interface index to interface name by IfaceIndexNameMap - return uid.val + " " + matchToString(match.rule) + " " + match.iif; - } else { - return uid.val + " " + matchToString(match.rule); - } - }); - BpfDump.dumpMap(sUidPermissionMap, pw, "sUidPermissionMap", - (uid, permission) -> uid.val + " " + permissionToString(permission.val)); - BpfDump.dumpMap(sIngressDiscardMap, pw, "sIngressDiscardMap", - (key, value) -> "[" + key.dstAddr + "]: " - + value.iif1 + "(" + mDeps.getIfName(value.iif1) + "), " - + value.iif2 + "(" + mDeps.getIfName(value.iif2) + ")"); + if (sCookieTagMap != null) { + BpfDump.dumpMap(sCookieTagMap, pw, "sCookieTagMap", + (key, value) -> "cookie=" + key.socketCookie + + " tag=0x" + Long.toHexString(value.tag) + + " uid=" + value.uid); + } + + if (sUidOwnerMap != null) { + BpfDump.dumpMap(sUidOwnerMap, pw, "sUidOwnerMap", + (uid, match) -> { + if ((match.rule & IIF_MATCH) != 0) { + // TODO: convert interface index to interface name by IfaceIndexNameMap + return uid.val + " " + matchToString(match.rule) + " " + match.iif; + } else { + return uid.val + " " + matchToString(match.rule); + } + }); + } + if (sUidPermissionMap != null) { + BpfDump.dumpMap(sUidPermissionMap, pw, "sUidPermissionMap", + (uid, permission) -> uid.val + " " + permissionToString(permission.val)); + } + if (sIngressDiscardMap != null) { + BpfDump.dumpMap(sIngressDiscardMap, pw, "sIngressDiscardMap", + (key, value) -> "[" + key.dstAddr + "]: " + + value.iif1 + "(" + mDeps.getIfName(value.iif1) + "), " + + value.iif2 + "(" + mDeps.getIfName(value.iif2) + ")"); + } dumpDataSaverConfig(pw); pw.decreaseIndent(); }