blob: abd0ff6796de0b66b9cbde18ffef8c57dd5c9d66 [file] [log] [blame]
Chenbo Fengf43bf812017-12-15 18:27:22 -08001/*
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 Colittif9c654c2018-03-01 18:02:15 +090031#ifdef LOG_TAG
32#undef LOG_TAG
33#endif
34
35#define LOG_TAG "BpfNetworkStats"
36
Chenbo Fengf43bf812017-12-15 18:27:22 -080037namespace android {
38namespace bpf {
39
40static const char* BPF_IFACE_STATS = "/proc/net/dev";
41
Chenbo Feng7e974052018-02-28 22:57:21 -080042// The limit for stats received by a unknown interface;
43static const uint64_t MAX_UNKNOWN_IFACE_BYTES = 100*1000;
44
Chenbo Feng06c73642018-03-05 04:10:38 -080045static constexpr uint32_t BPF_OPEN_FLAGS = BPF_F_RDONLY;
Lorenzo Colittif9c654c2018-03-01 18:02:15 +090046
Chenbo Fengf43bf812017-12-15 18:27:22 -080047int bpfGetUidStatsInternal(uid_t uid, Stats* stats, const base::unique_fd& map_fd) {
48 struct StatsKey curKey, nextKey;
49 curKey = NONEXISTENT_STATSKEY;
50 while (bpf::getNextMapKey(map_fd, &curKey, &nextKey) != -1) {
51 curKey = nextKey;
52 if (curKey.uid == uid) {
53 StatsValue statsEntry;
54 if (bpf::findMapEntry(map_fd, &curKey, &statsEntry) < 0) {
55 return -errno;
56 }
Chenbo Fengeac6c472018-02-05 15:06:23 -080057 stats->rxPackets += statsEntry.rxPackets;
58 stats->txPackets += statsEntry.txPackets;
59 stats->rxBytes += statsEntry.rxBytes;
60 stats->txBytes += statsEntry.txBytes;
Chenbo Fengf43bf812017-12-15 18:27:22 -080061 }
62 }
63 // Return errno if getNextMapKey return error before hit to the end of the map.
64 if (errno != ENOENT) return -errno;
65 return 0;
66}
67
68int bpfGetUidStats(uid_t uid, Stats* stats) {
Lorenzo Colittif9c654c2018-03-01 18:02:15 +090069 base::unique_fd uidStatsMap(bpf::mapRetrieve(UID_STATS_MAP_PATH, BPF_OPEN_FLAGS));
Chenbo Fengf43bf812017-12-15 18:27:22 -080070 if (uidStatsMap < 0) {
71 int ret = -errno;
Lorenzo Colittif9c654c2018-03-01 18:02:15 +090072 ALOGE("Opening map fd from %s failed: %s", UID_STATS_MAP_PATH, strerror(errno));
Chenbo Fengf43bf812017-12-15 18:27:22 -080073 return ret;
74 }
75 return bpfGetUidStatsInternal(uid, stats, uidStatsMap);
76}
77
78// TODO: The iface stats read from proc/net/dev contains additional L2 header.
79// Need to adjust the byte length read depend on the packets number before
80// return.
81// Bug: b/72111305
82int bpfGetIfaceStatsInternal(const char* iface, Stats* stats, const char* file) {
83 std::string content;
84 if (!android::base::ReadFileToString(file, &content)) {
85 ALOGE("Cannot read iface stats from: %s", file);
86 return -errno;
87 }
88 std::istringstream stream(content);
89 for (std::string ifaceLine; std::getline(stream, ifaceLine);) {
90 const char* buffer = android::base::Trim(ifaceLine).c_str();
91 char cur_iface[IFNAMSIZ];
92 uint64_t rxBytes, rxPackets, txBytes, txPackets;
93 // Typical iface stats read to parse:
94 // interface rxbytes rxpackets errs drop fifo frame compressed multicast txbytes txpackets \
95 // errs drop fifo colls carrier compressed
96 // lo: 13470483181 57249790 0 0 0 0 0 0 13470483181 57249790 0 0 0 0 0 0
97 int matched = sscanf(buffer,
98 "%[^ :]: %" SCNu64 " %" SCNu64
99 " %*lu %*lu %*lu %*lu %*lu %*lu "
100 "%" SCNu64 " %" SCNu64 "",
101 cur_iface, &rxBytes, &rxPackets, &txBytes, &txPackets);
102 if (matched >= 5) {
103 if (!iface || !strcmp(iface, cur_iface)) {
104 stats->rxBytes += rxBytes;
105 stats->rxPackets += rxPackets;
106 stats->txBytes += txBytes;
107 stats->txPackets += txPackets;
108 }
109 }
110 }
111 stats->tcpRxPackets = -1;
112 stats->tcpTxPackets = -1;
113
114 return 0;
115}
116
117int bpfGetIfaceStats(const char* iface, Stats* stats) {
118 return bpfGetIfaceStatsInternal(iface, stats, BPF_IFACE_STATS);
119}
120
121stats_line populateStatsEntry(const StatsKey& statsKey, const StatsValue& statsEntry,
122 const char* ifname) {
123 stats_line newLine;
124 strlcpy(newLine.iface, ifname, sizeof(newLine.iface));
125 newLine.uid = statsKey.uid;
126 newLine.set = statsKey.counterSet;
127 newLine.tag = statsKey.tag;
Chenbo Fengeac6c472018-02-05 15:06:23 -0800128 newLine.rxPackets = statsEntry.rxPackets;
129 newLine.txPackets = statsEntry.txPackets;
130 newLine.rxBytes = statsEntry.rxBytes;
131 newLine.txBytes = statsEntry.txBytes;
Chenbo Fengf43bf812017-12-15 18:27:22 -0800132 return newLine;
133}
134
Chenbo Feng7e974052018-02-28 22:57:21 -0800135int getIfaceNameFromMap(const base::unique_fd& ifaceMapFd, const base::unique_fd& statsMapFd,
136 char *ifname, StatsKey &curKey, uint64_t *unknownIfaceBytesTotal) {
137 if (bpf::findMapEntry(ifaceMapFd, &curKey.ifaceIndex, ifname) < 0) {
138 StatsValue statsEntry;
139 if (bpf::findMapEntry(statsMapFd, &curKey, &statsEntry) < 0) return -errno;
140 *unknownIfaceBytesTotal += (statsEntry.rxBytes + statsEntry.txBytes);
141 if (*unknownIfaceBytesTotal >= MAX_UNKNOWN_IFACE_BYTES) {
142 ALOGE("Unknown name for ifindex %d with more than %" PRIu64 " bytes of traffic",
143 curKey.ifaceIndex, *unknownIfaceBytesTotal);
144 }
145 return -ENODEV;
146 }
147 return 0;
148}
149
Chenbo Fengf43bf812017-12-15 18:27:22 -0800150int parseBpfUidStatsDetail(std::vector<stats_line>* lines,
151 const std::vector<std::string>& limitIfaces, int limitUid,
Chenbo Feng7e974052018-02-28 22:57:21 -0800152 const base::unique_fd& statsMapFd, const base::unique_fd& ifaceMapFd) {
Chenbo Fengf43bf812017-12-15 18:27:22 -0800153 struct StatsKey curKey, nextKey;
154 curKey = NONEXISTENT_STATSKEY;
Chenbo Feng7e974052018-02-28 22:57:21 -0800155 uint64_t unknownIfaceBytesTotal = 0;
156 while (bpf::getNextMapKey(statsMapFd, &curKey, &nextKey) != -1) {
Chenbo Fengf43bf812017-12-15 18:27:22 -0800157 curKey = nextKey;
158 char ifname[IFNAMSIZ];
159 // The data entry in uid map that stores removed uid stats use 0 as the
160 // iface. Just skip when seen.
Chenbo Feng7e974052018-02-28 22:57:21 -0800161 if (curKey.ifaceIndex == 0 ||
162 getIfaceNameFromMap(ifaceMapFd, statsMapFd, ifname, curKey, &unknownIfaceBytesTotal)) {
163 continue;
164 }
Chenbo Fengf43bf812017-12-15 18:27:22 -0800165 std::string ifnameStr(ifname);
166 if (limitIfaces.size() > 0 &&
167 std::find(limitIfaces.begin(), limitIfaces.end(), ifnameStr) == limitIfaces.end()) {
168 // Nothing matched; skip this line.
169 continue;
170 }
171 if (limitUid != UID_ALL && limitUid != int(curKey.uid)) continue;
172 StatsValue statsEntry;
Chenbo Feng7e974052018-02-28 22:57:21 -0800173 if (bpf::findMapEntry(statsMapFd, &curKey, &statsEntry) < 0) {
Chenbo Fengf43bf812017-12-15 18:27:22 -0800174 int ret = -errno;
175 ALOGE("get map statsEntry failed: %s", strerror(errno));
176 return ret;
177 }
178 lines->push_back(populateStatsEntry(curKey, statsEntry, ifname));
179 }
180 return 0;
181}
182
183int parseBpfTagStatsDetail(std::vector<stats_line>* lines,
184 const std::vector<std::string>& limitIfaces, int limitTag, int limitUid,
Chenbo Feng7e974052018-02-28 22:57:21 -0800185 const base::unique_fd& statsMapFd, const base::unique_fd& ifaceMapFd) {
Chenbo Fengf43bf812017-12-15 18:27:22 -0800186 struct StatsKey curKey, nextKey;
187 curKey = NONEXISTENT_STATSKEY;
Chenbo Feng7e974052018-02-28 22:57:21 -0800188 uint64_t unknownIfaceBytesTotal = 0;
189 while (bpf::getNextMapKey(statsMapFd, &curKey, &nextKey) != -1) {
Chenbo Fengf43bf812017-12-15 18:27:22 -0800190 curKey = nextKey;
Chenbo Feng7e974052018-02-28 22:57:21 -0800191 char ifname[IFNAMSIZ];
192 if (getIfaceNameFromMap(ifaceMapFd, statsMapFd, ifname, curKey, &unknownIfaceBytesTotal)) {
193 continue;
194 }
Chenbo Fengf43bf812017-12-15 18:27:22 -0800195 std::string ifnameStr(ifname);
196 if (limitIfaces.size() > 0 &&
197 std::find(limitIfaces.begin(), limitIfaces.end(), ifnameStr) == limitIfaces.end()) {
198 // Nothing matched; skip this line.
199 continue;
200 }
201 if ((limitTag != TAG_ALL && uint32_t(limitTag) != (curKey.tag)) ||
202 (limitUid != UID_ALL && uint32_t(limitUid) != curKey.uid))
203 continue;
204 StatsValue statsEntry;
Chenbo Feng7e974052018-02-28 22:57:21 -0800205 if (bpf::findMapEntry(statsMapFd, &curKey, &statsEntry) < 0) return -errno;
Chenbo Fengf43bf812017-12-15 18:27:22 -0800206 lines->push_back(populateStatsEntry(curKey, statsEntry, ifname));
207 }
208 if (errno != ENOENT) return -errno;
209 return 0;
210}
211
212int parseBpfNetworkStatsDetail(std::vector<stats_line>* lines,
213 const std::vector<std::string>& limitIfaces, int limitTag,
214 int limitUid) {
Lorenzo Colittif9c654c2018-03-01 18:02:15 +0900215 base::unique_fd tagStatsMap(bpf::mapRetrieve(TAG_STATS_MAP_PATH, BPF_OPEN_FLAGS));
Chenbo Fengf43bf812017-12-15 18:27:22 -0800216 int ret = 0;
217 if (tagStatsMap < 0) {
218 ret = -errno;
219 ALOGE("get tagStats map fd failed: %s", strerror(errno));
220 return ret;
221 }
Chenbo Feng7e974052018-02-28 22:57:21 -0800222 base::unique_fd ifaceIndexNameMap(bpf::mapRetrieve(IFACE_INDEX_NAME_MAP_PATH, BPF_OPEN_FLAGS));
223 if (ifaceIndexNameMap < 0) {
224 ret = -errno;
225 ALOGE("get ifaceIndexName map fd failed: %s", strerror(errno));
226 return ret;
227 }
228 ret = parseBpfTagStatsDetail(lines, limitIfaces, limitTag, limitUid, tagStatsMap,
229 ifaceIndexNameMap);
Chenbo Fengf43bf812017-12-15 18:27:22 -0800230 if (ret) return ret;
231
232 if (limitTag == TAG_ALL) {
Lorenzo Colittif9c654c2018-03-01 18:02:15 +0900233 base::unique_fd uidStatsMap(bpf::mapRetrieve(UID_STATS_MAP_PATH, BPF_OPEN_FLAGS));
Chenbo Fengf43bf812017-12-15 18:27:22 -0800234 if (uidStatsMap < 0) {
235 ret = -errno;
Lorenzo Colittif9c654c2018-03-01 18:02:15 +0900236 ALOGE("Opening map fd from %s failed: %s", UID_STATS_MAP_PATH, strerror(errno));
Chenbo Fengf43bf812017-12-15 18:27:22 -0800237 return ret;
238 }
Chenbo Feng7e974052018-02-28 22:57:21 -0800239 ret = parseBpfUidStatsDetail(lines, limitIfaces, limitUid, uidStatsMap, ifaceIndexNameMap);
Chenbo Fengf43bf812017-12-15 18:27:22 -0800240 }
241 return ret;
242}
243
244uint64_t combineUidTag(const uid_t uid, const uint32_t tag) {
245 return (uint64_t)uid << 32 | tag;
246}
247
248// This function get called when the system_server decided to clean up the
249// tagStatsMap after it gethered the information of taggged socket stats. The
250// function go through all the entry in tagStatsMap and remove all the entry
251// for which the tag no longer exists.
252int cleanStatsMapInternal(const base::unique_fd& cookieTagMap, const base::unique_fd& tagStatsMap) {
253 uint64_t curCookie = 0;
254 uint64_t nextCookie = 0;
255 int res;
256 UidTag tmp_uidtag;
257 std::unordered_set<uint64_t> uidTagSet;
258 StatsKey curKey, nextKey;
259
260 // Find all the uid, tag pair exist in cookieTagMap.
261 while (bpf::getNextMapKey(cookieTagMap, &curCookie, &nextCookie) != -1) {
262 curCookie = nextCookie;
263 res = bpf::findMapEntry(cookieTagMap, &curCookie, &tmp_uidtag);
264 if (res < 0) {
265 // might be a concurrent delete, continue to check other entries.
266 continue;
267 }
268 uint64_t uidTag = combineUidTag(tmp_uidtag.uid, tmp_uidtag.tag);
269 uidTagSet.insert(uidTag);
270 }
271
272 // Find all the entries in tagStatsMap where the key is not in the set of
273 // uid, tag pairs found above.
274 curKey = NONEXISTENT_STATSKEY;
275 std::vector<StatsKey> keyList;
276 while (bpf::getNextMapKey(tagStatsMap, &curKey, &nextKey) != -1) {
277 curKey = nextKey;
278 uint64_t uidTag = combineUidTag(curKey.uid, curKey.tag);
279 if (uidTagSet.find(uidTag) == uidTagSet.end()) {
280 keyList.push_back(curKey);
281 }
282 }
283
284 // Delete the entries
285 int size = keyList.size();
286 while (!keyList.empty()) {
287 StatsKey key = keyList.back();
288 keyList.pop_back();
289 res = bpf::deleteMapEntry(tagStatsMap, &key);
290 if (res < 0 && errno != ENOENT) {
291 res = -errno;
292 ALOGE("Failed to delete data(uid=%u, tag=%u): %s\n", key.uid, key.tag, strerror(errno));
293 return res;
294 }
295 }
296 ALOGD("finish clean up, %d stats entry cleaned", size);
297 return 0;
298}
299
300int cleanStatsMap() {
Lorenzo Colittif9c654c2018-03-01 18:02:15 +0900301 base::unique_fd cookieTagMap(bpf::mapRetrieve(COOKIE_UID_MAP_PATH, BPF_OPEN_FLAGS));
Chenbo Fengf43bf812017-12-15 18:27:22 -0800302 int ret = 0;
303 if (cookieTagMap < 0) {
304 ret = -errno;
305 ALOGE("get cookieTag map fd failed: %s", strerror(errno));
306 return ret;
307 }
308
Lorenzo Colittif9c654c2018-03-01 18:02:15 +0900309 base::unique_fd tagStatsMap(bpf::mapRetrieve(TAG_STATS_MAP_PATH, BPF_OPEN_FLAGS));
Chenbo Fengf43bf812017-12-15 18:27:22 -0800310 if (tagStatsMap < 0) {
311 ret = -errno;
312 ALOGE("get tagStats map fd failed: %s", strerror(errno));
313 return ret;
314 }
315
316 return cleanStatsMapInternal(cookieTagMap, tagStatsMap);
317}
318
319} // namespace bpf
320} // namespace android