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