Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <inttypes.h> |
| 18 | #include <net/if.h> |
| 19 | #include <string.h> |
| 20 | #include <unordered_set> |
| 21 | |
| 22 | #include <utils/Log.h> |
| 23 | #include <utils/misc.h> |
| 24 | |
| 25 | #include "android-base/file.h" |
| 26 | #include "android-base/strings.h" |
| 27 | #include "android-base/unique_fd.h" |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 28 | #include "bpf/BpfMap.h" |
Chenbo Feng | d6104d1 | 2018-10-16 20:29:29 -0700 | [diff] [blame] | 29 | #include "netdbpf/BpfNetworkStats.h" |
| 30 | #include "netdbpf/bpf_shared.h" |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 31 | |
Lorenzo Colitti | f9c654c | 2018-03-01 18:02:15 +0900 | [diff] [blame] | 32 | #ifdef LOG_TAG |
| 33 | #undef LOG_TAG |
| 34 | #endif |
| 35 | |
| 36 | #define LOG_TAG "BpfNetworkStats" |
| 37 | |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 38 | namespace android { |
| 39 | namespace bpf { |
| 40 | |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 41 | using base::Result; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 42 | |
Chenbo Feng | 873ae14 | 2019-04-10 12:26:06 -0700 | [diff] [blame] | 43 | // The target map for stats reading should be the inactive map, which is oppsite |
| 44 | // from the config value. |
| 45 | static constexpr char const* STATS_MAP_PATH[] = {STATS_MAP_B_PATH, STATS_MAP_A_PATH}; |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 46 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 47 | int bpfGetUidStatsInternal(uid_t uid, Stats* stats, |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 48 | const BpfMap<uint32_t, StatsValue>& appUidStatsMap) { |
| 49 | auto statsEntry = appUidStatsMap.readValue(uid); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 50 | if (statsEntry.ok()) { |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 51 | stats->rxPackets = statsEntry.value().rxPackets; |
| 52 | stats->txPackets = statsEntry.value().txPackets; |
| 53 | stats->rxBytes = statsEntry.value().rxBytes; |
| 54 | stats->txBytes = statsEntry.value().txBytes; |
| 55 | } |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 56 | return (statsEntry.ok() || statsEntry.error().code() == ENOENT) ? 0 |
| 57 | : -statsEntry.error().code(); |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | int bpfGetUidStats(uid_t uid, Stats* stats) { |
Maciej Żenczykowski | f879a7f | 2020-01-20 03:18:06 -0800 | [diff] [blame] | 61 | BpfMapRO<uint32_t, StatsValue> appUidStatsMap(APP_UID_STATS_MAP_PATH); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 62 | |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 63 | if (!appUidStatsMap.isValid()) { |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 64 | int ret = -errno; |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 65 | ALOGE("Opening appUidStatsMap(%s) failed: %s", APP_UID_STATS_MAP_PATH, strerror(errno)); |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 66 | return ret; |
| 67 | } |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 68 | return bpfGetUidStatsInternal(uid, stats, appUidStatsMap); |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 71 | int bpfGetIfaceStatsInternal(const char* iface, Stats* stats, |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 72 | const BpfMap<uint32_t, StatsValue>& ifaceStatsMap, |
| 73 | const BpfMap<uint32_t, IfaceValue>& ifaceNameMap) { |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 74 | int64_t unknownIfaceBytesTotal = 0; |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 75 | stats->tcpRxPackets = -1; |
| 76 | stats->tcpTxPackets = -1; |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 77 | const auto processIfaceStats = |
| 78 | [iface, stats, &ifaceNameMap, &unknownIfaceBytesTotal]( |
| 79 | const uint32_t& key, |
| 80 | const BpfMap<uint32_t, StatsValue>& ifaceStatsMap) -> Result<void> { |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 81 | char ifname[IFNAMSIZ]; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 82 | if (getIfaceNameFromMap(ifaceNameMap, ifaceStatsMap, key, ifname, key, |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 83 | &unknownIfaceBytesTotal)) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 84 | return Result<void>(); |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 85 | } |
| 86 | if (!iface || !strcmp(iface, ifname)) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 87 | Result<StatsValue> statsEntry = ifaceStatsMap.readValue(key); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 88 | if (!statsEntry.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 89 | return statsEntry.error(); |
| 90 | } |
| 91 | stats->rxPackets += statsEntry.value().rxPackets; |
| 92 | stats->txPackets += statsEntry.value().txPackets; |
| 93 | stats->rxBytes += statsEntry.value().rxBytes; |
| 94 | stats->txBytes += statsEntry.value().txBytes; |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 95 | } |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 96 | return Result<void>(); |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 97 | }; |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 98 | auto res = ifaceStatsMap.iterate(processIfaceStats); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 99 | return res.ok() ? 0 : -res.error().code(); |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | int bpfGetIfaceStats(const char* iface, Stats* stats) { |
Maciej Żenczykowski | f879a7f | 2020-01-20 03:18:06 -0800 | [diff] [blame] | 103 | BpfMapRO<uint32_t, StatsValue> ifaceStatsMap(IFACE_STATS_MAP_PATH); |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 104 | int ret; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 105 | if (!ifaceStatsMap.isValid()) { |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 106 | ret = -errno; |
| 107 | ALOGE("get ifaceStats map fd failed: %s", strerror(errno)); |
| 108 | return ret; |
| 109 | } |
Maciej Żenczykowski | f879a7f | 2020-01-20 03:18:06 -0800 | [diff] [blame] | 110 | BpfMapRO<uint32_t, IfaceValue> ifaceIndexNameMap(IFACE_INDEX_NAME_MAP_PATH); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 111 | if (!ifaceIndexNameMap.isValid()) { |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 112 | ret = -errno; |
| 113 | ALOGE("get ifaceIndexName map fd failed: %s", strerror(errno)); |
| 114 | return ret; |
| 115 | } |
| 116 | return bpfGetIfaceStatsInternal(iface, stats, ifaceStatsMap, ifaceIndexNameMap); |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | stats_line populateStatsEntry(const StatsKey& statsKey, const StatsValue& statsEntry, |
| 120 | const char* ifname) { |
| 121 | stats_line newLine; |
| 122 | strlcpy(newLine.iface, ifname, sizeof(newLine.iface)); |
Chenbo Feng | f4b812d | 2018-04-18 15:27:19 -0700 | [diff] [blame] | 123 | newLine.uid = (int32_t)statsKey.uid; |
| 124 | newLine.set = (int32_t)statsKey.counterSet; |
| 125 | newLine.tag = (int32_t)statsKey.tag; |
Chenbo Feng | eac6c47 | 2018-02-05 15:06:23 -0800 | [diff] [blame] | 126 | newLine.rxPackets = statsEntry.rxPackets; |
| 127 | newLine.txPackets = statsEntry.txPackets; |
| 128 | newLine.rxBytes = statsEntry.rxBytes; |
| 129 | newLine.txBytes = statsEntry.txBytes; |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 130 | return newLine; |
| 131 | } |
| 132 | |
Chenbo Feng | 1651348 | 2018-03-15 17:59:58 -0700 | [diff] [blame] | 133 | int parseBpfNetworkStatsDetailInternal(std::vector<stats_line>* lines, |
| 134 | const std::vector<std::string>& limitIfaces, int limitTag, |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 135 | int limitUid, const BpfMap<StatsKey, StatsValue>& statsMap, |
| 136 | const BpfMap<uint32_t, IfaceValue>& ifaceMap) { |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 137 | int64_t unknownIfaceBytesTotal = 0; |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 138 | const auto processDetailUidStats = |
| 139 | [lines, &limitIfaces, &limitTag, &limitUid, &unknownIfaceBytesTotal, &ifaceMap]( |
| 140 | const StatsKey& key, |
| 141 | const BpfMap<StatsKey, StatsValue>& statsMap) -> Result<void> { |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 142 | char ifname[IFNAMSIZ]; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 143 | if (getIfaceNameFromMap(ifaceMap, statsMap, key.ifaceIndex, ifname, key, |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 144 | &unknownIfaceBytesTotal)) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 145 | return Result<void>(); |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 146 | } |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 147 | std::string ifnameStr(ifname); |
| 148 | if (limitIfaces.size() > 0 && |
| 149 | std::find(limitIfaces.begin(), limitIfaces.end(), ifnameStr) == limitIfaces.end()) { |
| 150 | // Nothing matched; skip this line. |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 151 | return Result<void>(); |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 152 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 153 | if (limitTag != TAG_ALL && uint32_t(limitTag) != key.tag) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 154 | return Result<void>(); |
Chenbo Feng | 1651348 | 2018-03-15 17:59:58 -0700 | [diff] [blame] | 155 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 156 | if (limitUid != UID_ALL && uint32_t(limitUid) != key.uid) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 157 | return Result<void>(); |
Chenbo Feng | 1651348 | 2018-03-15 17:59:58 -0700 | [diff] [blame] | 158 | } |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 159 | Result<StatsValue> statsEntry = statsMap.readValue(key); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 160 | if (!statsEntry.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 161 | return base::ResultError(statsEntry.error().message(), statsEntry.error().code()); |
| 162 | } |
| 163 | lines->push_back(populateStatsEntry(key, statsEntry.value(), ifname)); |
| 164 | return Result<void>(); |
Chenbo Feng | 1651348 | 2018-03-15 17:59:58 -0700 | [diff] [blame] | 165 | }; |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 166 | Result<void> res = statsMap.iterate(processDetailUidStats); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 167 | if (!res.ok()) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 168 | ALOGE("failed to iterate per uid Stats map for detail traffic stats: %s", |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 169 | strerror(res.error().code())); |
| 170 | return -res.error().code(); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 171 | } |
junyulai | a130ac2 | 2018-11-09 16:12:29 +0800 | [diff] [blame] | 172 | |
| 173 | // Since eBPF use hash map to record stats, network stats collected from |
| 174 | // eBPF will be out of order. And the performance of findIndexHinted in |
| 175 | // NetworkStats will also be impacted. |
| 176 | // |
| 177 | // Furthermore, since the StatsKey contains iface index, the network stats |
| 178 | // reported to framework would create items with the same iface, uid, tag |
| 179 | // and set, which causes NetworkStats maps wrong item to subtract. |
| 180 | // |
| 181 | // Thus, the stats needs to be properly sorted and grouped before reported. |
| 182 | groupNetworkStats(lines); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 183 | return 0; |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | int parseBpfNetworkStatsDetail(std::vector<stats_line>* lines, |
| 187 | const std::vector<std::string>& limitIfaces, int limitTag, |
| 188 | int limitUid) { |
Maciej Żenczykowski | f879a7f | 2020-01-20 03:18:06 -0800 | [diff] [blame] | 189 | BpfMapRO<uint32_t, IfaceValue> ifaceIndexNameMap(IFACE_INDEX_NAME_MAP_PATH); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 190 | if (!ifaceIndexNameMap.isValid()) { |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 191 | int ret = -errno; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 192 | ALOGE("get ifaceIndexName map fd failed: %s", strerror(errno)); |
| 193 | return ret; |
| 194 | } |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 195 | |
Maciej Żenczykowski | f879a7f | 2020-01-20 03:18:06 -0800 | [diff] [blame] | 196 | BpfMapRO<uint32_t, uint8_t> configurationMap(CONFIGURATION_MAP_PATH); |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 197 | if (!configurationMap.isValid()) { |
| 198 | int ret = -errno; |
| 199 | ALOGE("get configuration map fd failed: %s", strerror(errno)); |
| 200 | return ret; |
| 201 | } |
| 202 | auto configuration = configurationMap.readValue(CURRENT_STATS_MAP_CONFIGURATION_KEY); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 203 | if (!configuration.ok()) { |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 204 | ALOGE("Cannot read the old configuration from map: %s", |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 205 | configuration.error().message().c_str()); |
| 206 | return -configuration.error().code(); |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 207 | } |
| 208 | const char* statsMapPath = STATS_MAP_PATH[configuration.value()]; |
Maciej Żenczykowski | f879a7f | 2020-01-20 03:18:06 -0800 | [diff] [blame] | 209 | BpfMap<StatsKey, StatsValue> statsMap(statsMapPath); |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 210 | if (!statsMap.isValid()) { |
| 211 | int ret = -errno; |
| 212 | ALOGE("get stats map fd failed: %s, path: %s", strerror(errno), statsMapPath); |
| 213 | return ret; |
Chenbo Feng | 1651348 | 2018-03-15 17:59:58 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Chenbo Feng | 873ae14 | 2019-04-10 12:26:06 -0700 | [diff] [blame] | 216 | // It is safe to read and clear the old map now since the |
| 217 | // networkStatsFactory should call netd to swap the map in advance already. |
| 218 | int ret = parseBpfNetworkStatsDetailInternal(lines, limitIfaces, limitTag, limitUid, statsMap, |
| 219 | ifaceIndexNameMap); |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 220 | if (ret) { |
| 221 | ALOGE("parse detail network stats failed: %s", strerror(errno)); |
| 222 | return ret; |
| 223 | } |
| 224 | |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 225 | Result<void> res = statsMap.clear(); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 226 | if (!res.ok()) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 227 | ALOGE("Clean up current stats map failed: %s", strerror(res.error().code())); |
| 228 | return -res.error().code(); |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return 0; |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Chenbo Feng | f4b812d | 2018-04-18 15:27:19 -0700 | [diff] [blame] | 234 | int parseBpfNetworkStatsDevInternal(std::vector<stats_line>* lines, |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 235 | const BpfMap<uint32_t, StatsValue>& statsMap, |
| 236 | const BpfMap<uint32_t, IfaceValue>& ifaceMap) { |
Chenbo Feng | f4b812d | 2018-04-18 15:27:19 -0700 | [diff] [blame] | 237 | int64_t unknownIfaceBytesTotal = 0; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 238 | const auto processDetailIfaceStats = [lines, &unknownIfaceBytesTotal, &ifaceMap, &statsMap]( |
| 239 | const uint32_t& key, const StatsValue& value, |
| 240 | const BpfMap<uint32_t, StatsValue>&) { |
Chenbo Feng | f4b812d | 2018-04-18 15:27:19 -0700 | [diff] [blame] | 241 | char ifname[IFNAMSIZ]; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 242 | if (getIfaceNameFromMap(ifaceMap, statsMap, key, ifname, key, &unknownIfaceBytesTotal)) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 243 | return Result<void>(); |
Chenbo Feng | f4b812d | 2018-04-18 15:27:19 -0700 | [diff] [blame] | 244 | } |
Chenbo Feng | f4b812d | 2018-04-18 15:27:19 -0700 | [diff] [blame] | 245 | StatsKey fakeKey = { |
Nick Desaulniers | a1aae6c | 2019-10-07 17:43:50 -0700 | [diff] [blame] | 246 | .uid = (uint32_t)UID_ALL, |
| 247 | .tag = (uint32_t)TAG_NONE, |
| 248 | .counterSet = (uint32_t)SET_ALL, |
| 249 | }; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 250 | lines->push_back(populateStatsEntry(fakeKey, value, ifname)); |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 251 | return Result<void>(); |
Chenbo Feng | f4b812d | 2018-04-18 15:27:19 -0700 | [diff] [blame] | 252 | }; |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 253 | Result<void> res = statsMap.iterateWithValue(processDetailIfaceStats); |
Bernie Innocenti | 615fd74 | 2020-02-06 03:55:09 +0900 | [diff] [blame] | 254 | if (!res.ok()) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 255 | ALOGE("failed to iterate per uid Stats map for detail traffic stats: %s", |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 256 | strerror(res.error().code())); |
| 257 | return -res.error().code(); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 258 | } |
junyulai | a130ac2 | 2018-11-09 16:12:29 +0800 | [diff] [blame] | 259 | |
| 260 | groupNetworkStats(lines); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 261 | return 0; |
Chenbo Feng | f4b812d | 2018-04-18 15:27:19 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | int parseBpfNetworkStatsDev(std::vector<stats_line>* lines) { |
| 265 | int ret = 0; |
Maciej Żenczykowski | f879a7f | 2020-01-20 03:18:06 -0800 | [diff] [blame] | 266 | BpfMapRO<uint32_t, IfaceValue> ifaceIndexNameMap(IFACE_INDEX_NAME_MAP_PATH); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 267 | if (!ifaceIndexNameMap.isValid()) { |
Chenbo Feng | f4b812d | 2018-04-18 15:27:19 -0700 | [diff] [blame] | 268 | ret = -errno; |
| 269 | ALOGE("get ifaceIndexName map fd failed: %s", strerror(errno)); |
| 270 | return ret; |
| 271 | } |
| 272 | |
Maciej Żenczykowski | f879a7f | 2020-01-20 03:18:06 -0800 | [diff] [blame] | 273 | BpfMapRO<uint32_t, StatsValue> ifaceStatsMap(IFACE_STATS_MAP_PATH); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 274 | if (!ifaceStatsMap.isValid()) { |
Chenbo Feng | f4b812d | 2018-04-18 15:27:19 -0700 | [diff] [blame] | 275 | ret = -errno; |
| 276 | ALOGE("get ifaceStats map fd failed: %s", strerror(errno)); |
| 277 | return ret; |
| 278 | } |
| 279 | return parseBpfNetworkStatsDevInternal(lines, ifaceStatsMap, ifaceIndexNameMap); |
| 280 | } |
| 281 | |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 282 | uint64_t combineUidTag(const uid_t uid, const uint32_t tag) { |
| 283 | return (uint64_t)uid << 32 | tag; |
| 284 | } |
| 285 | |
junyulai | a130ac2 | 2018-11-09 16:12:29 +0800 | [diff] [blame] | 286 | void groupNetworkStats(std::vector<stats_line>* lines) { |
| 287 | if (lines->size() <= 1) return; |
| 288 | std::sort(lines->begin(), lines->end()); |
| 289 | |
| 290 | // Similar to std::unique(), but aggregates the duplicates rather than discarding them. |
| 291 | size_t nextOutput = 0; |
| 292 | for (size_t i = 1; i < lines->size(); i++) { |
| 293 | if (lines->at(nextOutput) == lines->at(i)) { |
| 294 | lines->at(nextOutput) += lines->at(i); |
| 295 | } else { |
| 296 | nextOutput++; |
| 297 | if (nextOutput != i) { |
| 298 | lines->at(nextOutput) = lines->at(i); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | if (lines->size() != nextOutput + 1) { |
| 304 | lines->resize(nextOutput + 1); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // True if lhs equals to rhs, only compare iface, uid, tag and set. |
| 309 | bool operator==(const stats_line& lhs, const stats_line& rhs) { |
| 310 | return ((lhs.uid == rhs.uid) && (lhs.tag == rhs.tag) && (lhs.set == rhs.set) && |
| 311 | !strncmp(lhs.iface, rhs.iface, sizeof(lhs.iface))); |
| 312 | } |
| 313 | |
| 314 | // True if lhs is smaller then rhs, only compare iface, uid, tag and set. |
| 315 | bool operator<(const stats_line& lhs, const stats_line& rhs) { |
| 316 | int ret = strncmp(lhs.iface, rhs.iface, sizeof(lhs.iface)); |
| 317 | if (ret != 0) return ret < 0; |
| 318 | if (lhs.uid < rhs.uid) return true; |
| 319 | if (lhs.uid > rhs.uid) return false; |
| 320 | if (lhs.tag < rhs.tag) return true; |
| 321 | if (lhs.tag > rhs.tag) return false; |
| 322 | if (lhs.set < rhs.set) return true; |
| 323 | if (lhs.set > rhs.set) return false; |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | stats_line& stats_line::operator=(const stats_line& rhs) { |
Luca Stefani | 28e8472 | 2019-09-01 21:11:02 +0200 | [diff] [blame] | 328 | if (this == &rhs) return *this; |
| 329 | |
junyulai | a130ac2 | 2018-11-09 16:12:29 +0800 | [diff] [blame] | 330 | strlcpy(iface, rhs.iface, sizeof(iface)); |
| 331 | uid = rhs.uid; |
| 332 | set = rhs.set; |
| 333 | tag = rhs.tag; |
| 334 | rxPackets = rhs.rxPackets; |
| 335 | txPackets = rhs.txPackets; |
| 336 | rxBytes = rhs.rxBytes; |
| 337 | txBytes = rhs.txBytes; |
| 338 | return *this; |
| 339 | } |
| 340 | |
| 341 | stats_line& stats_line::operator+=(const stats_line& rhs) { |
| 342 | rxPackets += rhs.rxPackets; |
| 343 | txPackets += rhs.txPackets; |
| 344 | rxBytes += rhs.rxBytes; |
| 345 | txBytes += rhs.txBytes; |
| 346 | return *this; |
| 347 | } |
| 348 | |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 349 | } // namespace bpf |
| 350 | } // namespace android |