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