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" |
| 28 | #include "bpf/BpfNetworkStats.h" |
| 29 | #include "bpf/BpfUtils.h" |
| 30 | |
Lorenzo Colitti | f9c654c | 2018-03-01 18:02:15 +0900 | [diff] [blame] | 31 | #ifdef LOG_TAG |
| 32 | #undef LOG_TAG |
| 33 | #endif |
| 34 | |
| 35 | #define LOG_TAG "BpfNetworkStats" |
| 36 | |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 37 | namespace android { |
| 38 | namespace bpf { |
| 39 | |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 40 | |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 41 | // The limit for stats received by a unknown interface; |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 42 | static const int64_t MAX_UNKNOWN_IFACE_BYTES = 100*1000; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 43 | |
Chenbo Feng | 06c7364 | 2018-03-05 04:10:38 -0800 | [diff] [blame] | 44 | static constexpr uint32_t BPF_OPEN_FLAGS = BPF_F_RDONLY; |
Lorenzo Colitti | f9c654c | 2018-03-01 18:02:15 +0900 | [diff] [blame] | 45 | |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 46 | int bpfGetUidStatsInternal(uid_t uid, Stats* stats, const base::unique_fd& map_fd) { |
Chenbo Feng | 1651348 | 2018-03-15 17:59:58 -0700 | [diff] [blame] | 47 | struct StatsKey nonExistentKey = NONEXISTENT_STATSKEY; |
| 48 | struct StatsValue dummyValue; |
| 49 | auto processUidStats = [uid, stats](void *key, const base::unique_fd& map_fd) { |
| 50 | if (((StatsKey *) key)->uid != uid) { |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 51 | return BPF_CONTINUE; |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 52 | } |
Chenbo Feng | 1651348 | 2018-03-15 17:59:58 -0700 | [diff] [blame] | 53 | StatsValue statsEntry; |
| 54 | int ret = bpf::findMapEntry(map_fd, key, &statsEntry); |
| 55 | if (ret) return -errno; |
| 56 | stats->rxPackets += statsEntry.rxPackets; |
| 57 | stats->txPackets += statsEntry.txPackets; |
| 58 | stats->rxBytes += statsEntry.rxBytes; |
| 59 | stats->txBytes += statsEntry.txBytes; |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 60 | return BPF_CONTINUE; |
Chenbo Feng | 1651348 | 2018-03-15 17:59:58 -0700 | [diff] [blame] | 61 | }; |
| 62 | return bpfIterateMap(nonExistentKey, dummyValue, map_fd, processUidStats); |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | int bpfGetUidStats(uid_t uid, Stats* stats) { |
Lorenzo Colitti | f9c654c | 2018-03-01 18:02:15 +0900 | [diff] [blame] | 66 | base::unique_fd uidStatsMap(bpf::mapRetrieve(UID_STATS_MAP_PATH, BPF_OPEN_FLAGS)); |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 67 | if (uidStatsMap < 0) { |
| 68 | int ret = -errno; |
Lorenzo Colitti | f9c654c | 2018-03-01 18:02:15 +0900 | [diff] [blame] | 69 | ALOGE("Opening map fd from %s failed: %s", UID_STATS_MAP_PATH, strerror(errno)); |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 70 | return ret; |
| 71 | } |
| 72 | return bpfGetUidStatsInternal(uid, stats, uidStatsMap); |
| 73 | } |
| 74 | |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 75 | int bpfGetIfaceStatsInternal(const char* iface, Stats* stats, |
| 76 | const base::unique_fd& ifaceStatsMapFd, |
| 77 | const base::unique_fd& ifaceNameMapFd) { |
| 78 | uint32_t nonExistentKey = NONEXISTENT_IFACE_STATS_KEY; |
| 79 | struct StatsValue dummyValue; |
| 80 | int64_t unknownIfaceBytesTotal = 0; |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 81 | stats->tcpRxPackets = -1; |
| 82 | stats->tcpTxPackets = -1; |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 83 | auto processIfaceStats = [iface, stats, &ifaceNameMapFd, &unknownIfaceBytesTotal]( |
| 84 | void* key, const base::unique_fd& ifaceStatsMapFd) { |
| 85 | char ifname[IFNAMSIZ]; |
| 86 | int ifIndex = *(int *)key; |
| 87 | if (getIfaceNameFromMap(ifaceNameMapFd, ifaceStatsMapFd, ifIndex, ifname, &ifIndex, |
| 88 | &unknownIfaceBytesTotal)) { |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 89 | return BPF_CONTINUE; |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 90 | } |
| 91 | if (!iface || !strcmp(iface, ifname)) { |
| 92 | StatsValue statsEntry; |
| 93 | int ret = bpf::findMapEntry(ifaceStatsMapFd, &ifIndex, &statsEntry); |
| 94 | if (ret) return -errno; |
| 95 | stats->rxPackets += statsEntry.rxPackets; |
| 96 | stats->txPackets += statsEntry.txPackets; |
| 97 | stats->rxBytes += statsEntry.rxBytes; |
| 98 | stats->txBytes += statsEntry.txBytes; |
| 99 | } |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 100 | return BPF_CONTINUE; |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 101 | }; |
| 102 | return bpfIterateMap(nonExistentKey, dummyValue, ifaceStatsMapFd, processIfaceStats); |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | int bpfGetIfaceStats(const char* iface, Stats* stats) { |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 106 | base::unique_fd ifaceStatsMap(bpf::mapRetrieve(IFACE_STATS_MAP_PATH, BPF_OPEN_FLAGS)); |
| 107 | int ret; |
| 108 | if (ifaceStatsMap < 0) { |
| 109 | ret = -errno; |
| 110 | ALOGE("get ifaceStats map fd failed: %s", strerror(errno)); |
| 111 | return ret; |
| 112 | } |
| 113 | base::unique_fd ifaceIndexNameMap(bpf::mapRetrieve(IFACE_INDEX_NAME_MAP_PATH, BPF_OPEN_FLAGS)); |
| 114 | if (ifaceIndexNameMap < 0) { |
| 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 | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 136 | void maybeLogUnknownIface(int ifaceIndex, const base::unique_fd& statsMapFd, void* curKey, |
| 137 | int64_t* unknownIfaceBytesTotal) { |
| 138 | // Have we already logged an error? |
| 139 | if (*unknownIfaceBytesTotal == -1) { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | // Are we undercounting enough data to be worth logging? |
| 144 | StatsValue statsEntry; |
| 145 | if (bpf::findMapEntry(statsMapFd, curKey, &statsEntry) < 0) { |
| 146 | // No data is being undercounted. |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | *unknownIfaceBytesTotal += (statsEntry.rxBytes + statsEntry.txBytes); |
| 151 | if (*unknownIfaceBytesTotal >= MAX_UNKNOWN_IFACE_BYTES) { |
| 152 | ALOGE("Unknown name for ifindex %d with more than %" PRId64 " bytes of traffic", |
| 153 | ifaceIndex, *unknownIfaceBytesTotal); |
| 154 | *unknownIfaceBytesTotal = -1; |
| 155 | } |
| 156 | } |
| 157 | |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 158 | int getIfaceNameFromMap(const base::unique_fd& ifaceMapFd, const base::unique_fd& statsMapFd, |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 159 | uint32_t ifaceIndex, char* ifname, void* curKey, |
| 160 | int64_t* unknownIfaceBytesTotal) { |
| 161 | if (bpf::findMapEntry(ifaceMapFd, &ifaceIndex, ifname) < 0) { |
| 162 | maybeLogUnknownIface(ifaceIndex, statsMapFd, curKey, unknownIfaceBytesTotal); |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 163 | return -ENODEV; |
| 164 | } |
| 165 | return 0; |
| 166 | } |
| 167 | |
Chenbo Feng | 1651348 | 2018-03-15 17:59:58 -0700 | [diff] [blame] | 168 | int parseBpfNetworkStatsDetailInternal(std::vector<stats_line>* lines, |
| 169 | const std::vector<std::string>& limitIfaces, int limitTag, |
| 170 | int limitUid, const base::unique_fd& statsMapFd, |
| 171 | const base::unique_fd& ifaceMapFd) { |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 172 | int64_t unknownIfaceBytesTotal = 0; |
Chenbo Feng | 1651348 | 2018-03-15 17:59:58 -0700 | [diff] [blame] | 173 | struct StatsKey nonExistentKey = NONEXISTENT_STATSKEY; |
| 174 | struct StatsValue dummyValue; |
| 175 | auto processDetailUidStats = [lines, &limitIfaces, limitTag, limitUid, |
| 176 | &unknownIfaceBytesTotal, &ifaceMapFd] |
| 177 | (void* key, const base::unique_fd& statsMapFd) { |
| 178 | struct StatsKey curKey = * (struct StatsKey*)key; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 179 | char ifname[IFNAMSIZ]; |
Chenbo Feng | 33a4de1 | 2018-03-16 18:10:07 -0700 | [diff] [blame] | 180 | if (getIfaceNameFromMap(ifaceMapFd, statsMapFd, curKey.ifaceIndex, ifname, &curKey, |
| 181 | &unknownIfaceBytesTotal)) { |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 182 | return BPF_CONTINUE; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 183 | } |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 184 | std::string ifnameStr(ifname); |
| 185 | if (limitIfaces.size() > 0 && |
| 186 | std::find(limitIfaces.begin(), limitIfaces.end(), ifnameStr) == limitIfaces.end()) { |
| 187 | // Nothing matched; skip this line. |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 188 | return BPF_CONTINUE; |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 189 | } |
Chenbo Feng | 1651348 | 2018-03-15 17:59:58 -0700 | [diff] [blame] | 190 | if (limitTag != TAG_ALL && uint32_t(limitTag) != curKey.tag) { |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 191 | return BPF_CONTINUE; |
Chenbo Feng | 1651348 | 2018-03-15 17:59:58 -0700 | [diff] [blame] | 192 | } |
| 193 | if (limitUid != UID_ALL && uint32_t(limitUid) != curKey.uid) { |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 194 | return BPF_CONTINUE; |
Chenbo Feng | 1651348 | 2018-03-15 17:59:58 -0700 | [diff] [blame] | 195 | } |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 196 | StatsValue statsEntry; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 197 | if (bpf::findMapEntry(statsMapFd, &curKey, &statsEntry) < 0) return -errno; |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 198 | lines->push_back(populateStatsEntry(curKey, statsEntry, ifname)); |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 199 | return BPF_CONTINUE; |
Chenbo Feng | 1651348 | 2018-03-15 17:59:58 -0700 | [diff] [blame] | 200 | }; |
| 201 | return bpfIterateMap(nonExistentKey, dummyValue, statsMapFd, processDetailUidStats); |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | int parseBpfNetworkStatsDetail(std::vector<stats_line>* lines, |
| 205 | const std::vector<std::string>& limitIfaces, int limitTag, |
| 206 | int limitUid) { |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 207 | int ret = 0; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 208 | base::unique_fd ifaceIndexNameMap(bpf::mapRetrieve(IFACE_INDEX_NAME_MAP_PATH, BPF_OPEN_FLAGS)); |
| 209 | if (ifaceIndexNameMap < 0) { |
| 210 | ret = -errno; |
| 211 | ALOGE("get ifaceIndexName map fd failed: %s", strerror(errno)); |
| 212 | return ret; |
| 213 | } |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 214 | |
Chenbo Feng | 1651348 | 2018-03-15 17:59:58 -0700 | [diff] [blame] | 215 | // If the caller did not pass in TAG_NONE, read tag data. |
| 216 | if (limitTag != TAG_NONE) { |
| 217 | base::unique_fd tagStatsMap(bpf::mapRetrieve(TAG_STATS_MAP_PATH, BPF_OPEN_FLAGS)); |
| 218 | if (tagStatsMap < 0) { |
| 219 | ret = -errno; |
| 220 | ALOGE("get tagStats map fd failed: %s", strerror(errno)); |
| 221 | return ret; |
| 222 | } |
| 223 | ret = parseBpfNetworkStatsDetailInternal(lines, limitIfaces, limitTag, limitUid, |
| 224 | tagStatsMap, ifaceIndexNameMap); |
| 225 | if (ret) return ret; |
| 226 | } |
| 227 | |
| 228 | // If the caller did not pass in a specific tag (i.e., if limitTag is TAG_NONE(0) or |
| 229 | // TAG_ALL(-1)) read UID data. |
| 230 | if (limitTag == TAG_NONE || limitTag == TAG_ALL) { |
Lorenzo Colitti | f9c654c | 2018-03-01 18:02:15 +0900 | [diff] [blame] | 231 | base::unique_fd uidStatsMap(bpf::mapRetrieve(UID_STATS_MAP_PATH, BPF_OPEN_FLAGS)); |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 232 | if (uidStatsMap < 0) { |
| 233 | ret = -errno; |
Lorenzo Colitti | f9c654c | 2018-03-01 18:02:15 +0900 | [diff] [blame] | 234 | ALOGE("Opening map fd from %s failed: %s", UID_STATS_MAP_PATH, strerror(errno)); |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 235 | return ret; |
| 236 | } |
Chenbo Feng | 1651348 | 2018-03-15 17:59:58 -0700 | [diff] [blame] | 237 | ret = parseBpfNetworkStatsDetailInternal(lines, limitIfaces, limitTag, limitUid, |
| 238 | uidStatsMap, ifaceIndexNameMap); |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 239 | } |
| 240 | return ret; |
| 241 | } |
| 242 | |
Chenbo Feng | f4b812d | 2018-04-18 15:27:19 -0700 | [diff] [blame^] | 243 | int parseBpfNetworkStatsDevInternal(std::vector<stats_line>* lines, |
| 244 | const base::unique_fd& statsMapFd, |
| 245 | const base::unique_fd& ifaceMapFd) { |
| 246 | int64_t unknownIfaceBytesTotal = 0; |
| 247 | uint32_t nonExistentKey = NONEXISTENT_IFACE_STATS_KEY; |
| 248 | struct StatsValue dummyValue; |
| 249 | auto processDetailIfaceStats = [lines, &unknownIfaceBytesTotal, &ifaceMapFd]( |
| 250 | void* key, void* value, const base::unique_fd& statsMapFd) { |
| 251 | uint32_t ifIndex = *(uint32_t*)key; |
| 252 | char ifname[IFNAMSIZ]; |
| 253 | if (getIfaceNameFromMap(ifaceMapFd, statsMapFd, ifIndex, ifname, &ifIndex, |
| 254 | &unknownIfaceBytesTotal)) { |
| 255 | return BPF_CONTINUE; |
| 256 | } |
| 257 | StatsValue* statsEntry = (StatsValue*)value; |
| 258 | StatsKey fakeKey = { |
| 259 | .uid = (uint32_t)UID_ALL, .counterSet = (uint32_t)SET_ALL, .tag = (uint32_t)TAG_NONE}; |
| 260 | lines->push_back(populateStatsEntry(fakeKey, *statsEntry, ifname)); |
| 261 | return BPF_CONTINUE; |
| 262 | }; |
| 263 | return bpfIterateMapWithValue(nonExistentKey, dummyValue, statsMapFd, processDetailIfaceStats); |
| 264 | } |
| 265 | |
| 266 | int parseBpfNetworkStatsDev(std::vector<stats_line>* lines) { |
| 267 | int ret = 0; |
| 268 | base::unique_fd ifaceIndexNameMap(bpf::mapRetrieve(IFACE_INDEX_NAME_MAP_PATH, BPF_OPEN_FLAGS)); |
| 269 | if (ifaceIndexNameMap < 0) { |
| 270 | ret = -errno; |
| 271 | ALOGE("get ifaceIndexName map fd failed: %s", strerror(errno)); |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | base::unique_fd ifaceStatsMap(bpf::mapRetrieve(IFACE_STATS_MAP_PATH, BPF_OPEN_FLAGS)); |
| 276 | if (ifaceStatsMap < 0) { |
| 277 | ret = -errno; |
| 278 | ALOGE("get ifaceStats map fd failed: %s", strerror(errno)); |
| 279 | return ret; |
| 280 | } |
| 281 | return parseBpfNetworkStatsDevInternal(lines, ifaceStatsMap, ifaceIndexNameMap); |
| 282 | } |
| 283 | |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 284 | uint64_t combineUidTag(const uid_t uid, const uint32_t tag) { |
| 285 | return (uint64_t)uid << 32 | tag; |
| 286 | } |
| 287 | |
| 288 | // This function get called when the system_server decided to clean up the |
| 289 | // tagStatsMap after it gethered the information of taggged socket stats. The |
| 290 | // function go through all the entry in tagStatsMap and remove all the entry |
| 291 | // for which the tag no longer exists. |
| 292 | int cleanStatsMapInternal(const base::unique_fd& cookieTagMap, const base::unique_fd& tagStatsMap) { |
| 293 | uint64_t curCookie = 0; |
| 294 | uint64_t nextCookie = 0; |
| 295 | int res; |
| 296 | UidTag tmp_uidtag; |
| 297 | std::unordered_set<uint64_t> uidTagSet; |
| 298 | StatsKey curKey, nextKey; |
| 299 | |
| 300 | // Find all the uid, tag pair exist in cookieTagMap. |
| 301 | while (bpf::getNextMapKey(cookieTagMap, &curCookie, &nextCookie) != -1) { |
| 302 | curCookie = nextCookie; |
| 303 | res = bpf::findMapEntry(cookieTagMap, &curCookie, &tmp_uidtag); |
| 304 | if (res < 0) { |
| 305 | // might be a concurrent delete, continue to check other entries. |
| 306 | continue; |
| 307 | } |
| 308 | uint64_t uidTag = combineUidTag(tmp_uidtag.uid, tmp_uidtag.tag); |
| 309 | uidTagSet.insert(uidTag); |
| 310 | } |
| 311 | |
| 312 | // Find all the entries in tagStatsMap where the key is not in the set of |
| 313 | // uid, tag pairs found above. |
| 314 | curKey = NONEXISTENT_STATSKEY; |
| 315 | std::vector<StatsKey> keyList; |
| 316 | while (bpf::getNextMapKey(tagStatsMap, &curKey, &nextKey) != -1) { |
| 317 | curKey = nextKey; |
| 318 | uint64_t uidTag = combineUidTag(curKey.uid, curKey.tag); |
| 319 | if (uidTagSet.find(uidTag) == uidTagSet.end()) { |
| 320 | keyList.push_back(curKey); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // Delete the entries |
| 325 | int size = keyList.size(); |
| 326 | while (!keyList.empty()) { |
| 327 | StatsKey key = keyList.back(); |
| 328 | keyList.pop_back(); |
| 329 | res = bpf::deleteMapEntry(tagStatsMap, &key); |
| 330 | if (res < 0 && errno != ENOENT) { |
| 331 | res = -errno; |
| 332 | ALOGE("Failed to delete data(uid=%u, tag=%u): %s\n", key.uid, key.tag, strerror(errno)); |
| 333 | return res; |
| 334 | } |
| 335 | } |
| 336 | ALOGD("finish clean up, %d stats entry cleaned", size); |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | int cleanStatsMap() { |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 341 | base::unique_fd cookieTagMap(bpf::mapRetrieve(COOKIE_TAG_MAP_PATH, BPF_OPEN_FLAGS)); |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 342 | int ret = 0; |
| 343 | if (cookieTagMap < 0) { |
| 344 | ret = -errno; |
| 345 | ALOGE("get cookieTag map fd failed: %s", strerror(errno)); |
| 346 | return ret; |
| 347 | } |
| 348 | |
Lorenzo Colitti | f9c654c | 2018-03-01 18:02:15 +0900 | [diff] [blame] | 349 | base::unique_fd tagStatsMap(bpf::mapRetrieve(TAG_STATS_MAP_PATH, BPF_OPEN_FLAGS)); |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 350 | if (tagStatsMap < 0) { |
| 351 | ret = -errno; |
| 352 | ALOGE("get tagStats map fd failed: %s", strerror(errno)); |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | return cleanStatsMapInternal(cookieTagMap, tagStatsMap); |
| 357 | } |
| 358 | |
| 359 | } // namespace bpf |
| 360 | } // namespace android |