blob: eafbb5e3e95b01030d6d93dbbb7ee80a8dc5b69e [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"
Chenbo Feng4f6c2372018-04-26 10:37:55 -070028#include "bpf/BpfMap.h"
Chenbo Fengf43bf812017-12-15 18:27:22 -080029#include "bpf/BpfNetworkStats.h"
Chenbo Fengf43bf812017-12-15 18:27:22 -080030
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
Chenbo Feng4f6c2372018-04-26 10:37:55 -070040using netdutils::Status;
Chenbo Feng7e974052018-02-28 22:57:21 -080041
Chenbo Feng06c73642018-03-05 04:10:38 -080042static constexpr uint32_t BPF_OPEN_FLAGS = BPF_F_RDONLY;
Lorenzo Colittif9c654c2018-03-01 18:02:15 +090043
Chenbo Feng4f6c2372018-04-26 10:37:55 -070044int bpfGetUidStatsInternal(uid_t uid, Stats* stats,
Chenbo Fengbc4a15f2018-05-11 19:15:15 -070045 const BpfMap<uint32_t, StatsValue>& appUidStatsMap) {
46 auto statsEntry = appUidStatsMap.readValue(uid);
47 if (isOk(statsEntry)) {
48 stats->rxPackets = statsEntry.value().rxPackets;
49 stats->txPackets = statsEntry.value().txPackets;
50 stats->rxBytes = statsEntry.value().rxBytes;
51 stats->txBytes = statsEntry.value().txBytes;
52 }
53 return -statsEntry.status().code();
Chenbo Fengf43bf812017-12-15 18:27:22 -080054}
55
56int bpfGetUidStats(uid_t uid, Stats* stats) {
Chenbo Fengbc4a15f2018-05-11 19:15:15 -070057 BpfMap<uint32_t, StatsValue> appUidStatsMap(
58 mapRetrieve(APP_UID_STATS_MAP_PATH, BPF_OPEN_FLAGS));
Chenbo Feng4f6c2372018-04-26 10:37:55 -070059
Chenbo Fengbc4a15f2018-05-11 19:15:15 -070060 if (!appUidStatsMap.isValid()) {
Chenbo Fengf43bf812017-12-15 18:27:22 -080061 int ret = -errno;
Chenbo Fengbc4a15f2018-05-11 19:15:15 -070062 ALOGE("Opening appUidStatsMap(%s) failed: %s", UID_STATS_MAP_PATH, strerror(errno));
Chenbo Fengf43bf812017-12-15 18:27:22 -080063 return ret;
64 }
Chenbo Fengbc4a15f2018-05-11 19:15:15 -070065 return bpfGetUidStatsInternal(uid, stats, appUidStatsMap);
Chenbo Fengf43bf812017-12-15 18:27:22 -080066}
67
Chenbo Feng33a4de12018-03-16 18:10:07 -070068int bpfGetIfaceStatsInternal(const char* iface, Stats* stats,
Chenbo Feng4f6c2372018-04-26 10:37:55 -070069 const BpfMap<uint32_t, StatsValue>& ifaceStatsMap,
70 const BpfMap<uint32_t, IfaceValue>& ifaceNameMap) {
Chenbo Feng33a4de12018-03-16 18:10:07 -070071 int64_t unknownIfaceBytesTotal = 0;
Chenbo Fengf43bf812017-12-15 18:27:22 -080072 stats->tcpRxPackets = -1;
73 stats->tcpTxPackets = -1;
Chenbo Feng4f6c2372018-04-26 10:37:55 -070074 const auto processIfaceStats = [iface, stats, &ifaceNameMap, &unknownIfaceBytesTotal]
75 (const uint32_t& key,
76 const BpfMap<uint32_t, StatsValue>& ifaceStatsMap) {
Chenbo Feng33a4de12018-03-16 18:10:07 -070077 char ifname[IFNAMSIZ];
Chenbo Feng4f6c2372018-04-26 10:37:55 -070078 if (getIfaceNameFromMap(ifaceNameMap, ifaceStatsMap, key, ifname, key,
Chenbo Feng33a4de12018-03-16 18:10:07 -070079 &unknownIfaceBytesTotal)) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -070080 return netdutils::status::ok;
Chenbo Feng33a4de12018-03-16 18:10:07 -070081 }
82 if (!iface || !strcmp(iface, ifname)) {
83 StatsValue statsEntry;
Chenbo Feng4f6c2372018-04-26 10:37:55 -070084 ASSIGN_OR_RETURN(statsEntry, ifaceStatsMap.readValue(key));
Chenbo Feng33a4de12018-03-16 18:10:07 -070085 stats->rxPackets += statsEntry.rxPackets;
86 stats->txPackets += statsEntry.txPackets;
87 stats->rxBytes += statsEntry.rxBytes;
88 stats->txBytes += statsEntry.txBytes;
89 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -070090 return netdutils::status::ok;
Chenbo Feng33a4de12018-03-16 18:10:07 -070091 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -070092 return -ifaceStatsMap.iterate(processIfaceStats).code();
Chenbo Fengf43bf812017-12-15 18:27:22 -080093}
94
95int bpfGetIfaceStats(const char* iface, Stats* stats) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -070096 BpfMap<uint32_t, StatsValue> ifaceStatsMap(mapRetrieve(IFACE_STATS_MAP_PATH, BPF_OPEN_FLAGS));
Chenbo Feng33a4de12018-03-16 18:10:07 -070097 int ret;
Chenbo Feng4f6c2372018-04-26 10:37:55 -070098 if (!ifaceStatsMap.isValid()) {
Chenbo Feng33a4de12018-03-16 18:10:07 -070099 ret = -errno;
100 ALOGE("get ifaceStats map fd failed: %s", strerror(errno));
101 return ret;
102 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700103 BpfMap<uint32_t, IfaceValue> ifaceIndexNameMap(
104 mapRetrieve(IFACE_INDEX_NAME_MAP_PATH, BPF_OPEN_FLAGS));
105 if (!ifaceIndexNameMap.isValid()) {
Chenbo Feng33a4de12018-03-16 18:10:07 -0700106 ret = -errno;
107 ALOGE("get ifaceIndexName map fd failed: %s", strerror(errno));
108 return ret;
109 }
110 return bpfGetIfaceStatsInternal(iface, stats, ifaceStatsMap, ifaceIndexNameMap);
Chenbo Fengf43bf812017-12-15 18:27:22 -0800111}
112
113stats_line populateStatsEntry(const StatsKey& statsKey, const StatsValue& statsEntry,
114 const char* ifname) {
115 stats_line newLine;
116 strlcpy(newLine.iface, ifname, sizeof(newLine.iface));
Chenbo Fengf4b812d2018-04-18 15:27:19 -0700117 newLine.uid = (int32_t)statsKey.uid;
118 newLine.set = (int32_t)statsKey.counterSet;
119 newLine.tag = (int32_t)statsKey.tag;
Chenbo Fengeac6c472018-02-05 15:06:23 -0800120 newLine.rxPackets = statsEntry.rxPackets;
121 newLine.txPackets = statsEntry.txPackets;
122 newLine.rxBytes = statsEntry.rxBytes;
123 newLine.txBytes = statsEntry.txBytes;
Chenbo Fengf43bf812017-12-15 18:27:22 -0800124 return newLine;
125}
126
Chenbo Feng16513482018-03-15 17:59:58 -0700127int parseBpfNetworkStatsDetailInternal(std::vector<stats_line>* lines,
128 const std::vector<std::string>& limitIfaces, int limitTag,
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700129 int limitUid, const BpfMap<StatsKey, StatsValue>& statsMap,
130 const BpfMap<uint32_t, IfaceValue>& ifaceMap) {
Chenbo Feng33a4de12018-03-16 18:10:07 -0700131 int64_t unknownIfaceBytesTotal = 0;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700132 const auto processDetailUidStats = [lines, &limitIfaces, &limitTag, &limitUid,
133 &unknownIfaceBytesTotal,
134 &ifaceMap](const StatsKey& key,
135 const BpfMap<StatsKey, StatsValue>& statsMap) {
Chenbo Feng7e974052018-02-28 22:57:21 -0800136 char ifname[IFNAMSIZ];
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700137 if (getIfaceNameFromMap(ifaceMap, statsMap, key.ifaceIndex, ifname, key,
Chenbo Feng33a4de12018-03-16 18:10:07 -0700138 &unknownIfaceBytesTotal)) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700139 return netdutils::status::ok;
Chenbo Feng7e974052018-02-28 22:57:21 -0800140 }
Chenbo Fengf43bf812017-12-15 18:27:22 -0800141 std::string ifnameStr(ifname);
142 if (limitIfaces.size() > 0 &&
143 std::find(limitIfaces.begin(), limitIfaces.end(), ifnameStr) == limitIfaces.end()) {
144 // Nothing matched; skip this line.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700145 return netdutils::status::ok;
Chenbo Fengf43bf812017-12-15 18:27:22 -0800146 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700147 if (limitTag != TAG_ALL && uint32_t(limitTag) != key.tag) {
148 return netdutils::status::ok;
Chenbo Feng16513482018-03-15 17:59:58 -0700149 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700150 if (limitUid != UID_ALL && uint32_t(limitUid) != key.uid) {
151 return netdutils::status::ok;
Chenbo Feng16513482018-03-15 17:59:58 -0700152 }
Chenbo Fengf43bf812017-12-15 18:27:22 -0800153 StatsValue statsEntry;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700154 ASSIGN_OR_RETURN(statsEntry, statsMap.readValue(key));
155 lines->push_back(populateStatsEntry(key, statsEntry, ifname));
156 return netdutils::status::ok;
Chenbo Feng16513482018-03-15 17:59:58 -0700157 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700158 Status res = statsMap.iterate(processDetailUidStats);
159 if (!isOk(res)) {
160 ALOGE("failed to iterate per uid Stats map for detail traffic stats: %s",
161 strerror(res.code()));
162 return -res.code();
163 }
164 return 0;
Chenbo Fengf43bf812017-12-15 18:27:22 -0800165}
166
167int parseBpfNetworkStatsDetail(std::vector<stats_line>* lines,
168 const std::vector<std::string>& limitIfaces, int limitTag,
169 int limitUid) {
Chenbo Fengf43bf812017-12-15 18:27:22 -0800170 int ret = 0;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700171 BpfMap<uint32_t, IfaceValue> ifaceIndexNameMap(
172 mapRetrieve(IFACE_INDEX_NAME_MAP_PATH, BPF_OPEN_FLAGS));
173 if (!ifaceIndexNameMap.isValid()) {
Chenbo Feng7e974052018-02-28 22:57:21 -0800174 ret = -errno;
175 ALOGE("get ifaceIndexName map fd failed: %s", strerror(errno));
176 return ret;
177 }
Chenbo Fengf43bf812017-12-15 18:27:22 -0800178
Chenbo Feng16513482018-03-15 17:59:58 -0700179 // If the caller did not pass in TAG_NONE, read tag data.
180 if (limitTag != TAG_NONE) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700181 BpfMap<StatsKey, StatsValue> tagStatsMap(mapRetrieve(TAG_STATS_MAP_PATH, BPF_OPEN_FLAGS));
182 if (!tagStatsMap.isValid()) {
Chenbo Feng16513482018-03-15 17:59:58 -0700183 ret = -errno;
184 ALOGE("get tagStats map fd failed: %s", strerror(errno));
185 return ret;
186 }
187 ret = parseBpfNetworkStatsDetailInternal(lines, limitIfaces, limitTag, limitUid,
188 tagStatsMap, ifaceIndexNameMap);
189 if (ret) return ret;
190 }
191
192 // If the caller did not pass in a specific tag (i.e., if limitTag is TAG_NONE(0) or
193 // TAG_ALL(-1)) read UID data.
194 if (limitTag == TAG_NONE || limitTag == TAG_ALL) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700195 BpfMap<StatsKey, StatsValue> uidStatsMap(mapRetrieve(UID_STATS_MAP_PATH, BPF_OPEN_FLAGS));
196 if (!uidStatsMap.isValid()) {
Chenbo Fengf43bf812017-12-15 18:27:22 -0800197 ret = -errno;
Lorenzo Colittif9c654c2018-03-01 18:02:15 +0900198 ALOGE("Opening map fd from %s failed: %s", UID_STATS_MAP_PATH, strerror(errno));
Chenbo Fengf43bf812017-12-15 18:27:22 -0800199 return ret;
200 }
Chenbo Feng16513482018-03-15 17:59:58 -0700201 ret = parseBpfNetworkStatsDetailInternal(lines, limitIfaces, limitTag, limitUid,
202 uidStatsMap, ifaceIndexNameMap);
Chenbo Fengf43bf812017-12-15 18:27:22 -0800203 }
204 return ret;
205}
206
Chenbo Fengf4b812d2018-04-18 15:27:19 -0700207int parseBpfNetworkStatsDevInternal(std::vector<stats_line>* lines,
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700208 const BpfMap<uint32_t, StatsValue>& statsMap,
209 const BpfMap<uint32_t, IfaceValue>& ifaceMap) {
Chenbo Fengf4b812d2018-04-18 15:27:19 -0700210 int64_t unknownIfaceBytesTotal = 0;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700211 const auto processDetailIfaceStats = [lines, &unknownIfaceBytesTotal, &ifaceMap, &statsMap](
212 const uint32_t& key, const StatsValue& value,
213 const BpfMap<uint32_t, StatsValue>&) {
Chenbo Fengf4b812d2018-04-18 15:27:19 -0700214 char ifname[IFNAMSIZ];
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700215 if (getIfaceNameFromMap(ifaceMap, statsMap, key, ifname, key, &unknownIfaceBytesTotal)) {
216 return netdutils::status::ok;
Chenbo Fengf4b812d2018-04-18 15:27:19 -0700217 }
Chenbo Fengf4b812d2018-04-18 15:27:19 -0700218 StatsKey fakeKey = {
219 .uid = (uint32_t)UID_ALL, .counterSet = (uint32_t)SET_ALL, .tag = (uint32_t)TAG_NONE};
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700220 lines->push_back(populateStatsEntry(fakeKey, value, ifname));
221 return netdutils::status::ok;
Chenbo Fengf4b812d2018-04-18 15:27:19 -0700222 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700223 Status res = statsMap.iterateWithValue(processDetailIfaceStats);
224 if (!isOk(res)) {
225 ALOGE("failed to iterate per uid Stats map for detail traffic stats: %s",
226 strerror(res.code()));
227 return -res.code();
228 }
229 return 0;
Chenbo Fengf4b812d2018-04-18 15:27:19 -0700230}
231
232int parseBpfNetworkStatsDev(std::vector<stats_line>* lines) {
233 int ret = 0;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700234 BpfMap<uint32_t, IfaceValue> ifaceIndexNameMap(
235 mapRetrieve(IFACE_INDEX_NAME_MAP_PATH, BPF_OPEN_FLAGS));
236 if (!ifaceIndexNameMap.isValid()) {
Chenbo Fengf4b812d2018-04-18 15:27:19 -0700237 ret = -errno;
238 ALOGE("get ifaceIndexName map fd failed: %s", strerror(errno));
239 return ret;
240 }
241
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700242 BpfMap<uint32_t, StatsValue> ifaceStatsMap(mapRetrieve(IFACE_STATS_MAP_PATH, BPF_OPEN_FLAGS));
243 if (!ifaceStatsMap.isValid()) {
Chenbo Fengf4b812d2018-04-18 15:27:19 -0700244 ret = -errno;
245 ALOGE("get ifaceStats map fd failed: %s", strerror(errno));
246 return ret;
247 }
248 return parseBpfNetworkStatsDevInternal(lines, ifaceStatsMap, ifaceIndexNameMap);
249}
250
Chenbo Fengf43bf812017-12-15 18:27:22 -0800251uint64_t combineUidTag(const uid_t uid, const uint32_t tag) {
252 return (uint64_t)uid << 32 | tag;
253}
254
Chenbo Fengf43bf812017-12-15 18:27:22 -0800255} // namespace bpf
256} // namespace android