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