blob: d8eb6b6e48089194d517db9e7d5514c824aa24fc [file] [log] [blame]
Chenbo Fengf2759682017-10-10 17:31:57 -07001/*
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
Chenbo Fengc10a8a42017-12-15 13:56:33 -080017#define LOG_TAG "TrafficController"
Chenbo Feng33cc1032017-10-23 15:16:37 -070018#include <inttypes.h>
Chenbo Fengf2759682017-10-10 17:31:57 -070019#include <linux/bpf.h>
20#include <linux/if_ether.h>
21#include <linux/in.h>
22#include <linux/inet_diag.h>
Chenbo Feng116d0552017-12-04 17:25:19 -080023#include <linux/netlink.h>
24#include <linux/sock_diag.h>
Chenbo Fengf2759682017-10-10 17:31:57 -070025#include <linux/unistd.h>
Chenbo Fengc10a8a42017-12-15 13:56:33 -080026#include <net/if.h>
Chenbo Fengf2759682017-10-10 17:31:57 -070027#include <stdlib.h>
28#include <string.h>
29#include <sys/socket.h>
Chenbo Fengc10a8a42017-12-15 13:56:33 -080030#include <sys/stat.h>
Chenbo Feng05393d82018-01-09 15:18:43 -080031#include <sys/types.h>
Chenbo Feng33cc1032017-10-23 15:16:37 -070032#include <sys/utsname.h>
Chenbo Feng05393d82018-01-09 15:18:43 -080033#include <sys/wait.h>
Chenbo Feng89c12f12018-03-21 10:29:18 -070034#include <mutex>
Chenbo Fengc10a8a42017-12-15 13:56:33 -080035#include <unordered_set>
36#include <vector>
37
38#include <android-base/stringprintf.h>
Chenbo Fengef297172018-03-26 10:53:33 -070039#include <android-base/strings.h>
Chenbo Fengc10a8a42017-12-15 13:56:33 -080040#include <android-base/unique_fd.h>
Chenbo Feng05393d82018-01-09 15:18:43 -080041#include <logwrap/logwrap.h>
Chenbo Fengc10a8a42017-12-15 13:56:33 -080042#include <netdutils/StatusOr.h>
Chenbo Fengf2759682017-10-10 17:31:57 -070043
Chenbo Feng116d0552017-12-04 17:25:19 -080044#include <netdutils/Misc.h>
45#include <netdutils/Syscalls.h>
Chenbo Fengf2759682017-10-10 17:31:57 -070046#include "TrafficController.h"
Chenbo Feng4f6c2372018-04-26 10:37:55 -070047#include "bpf/BpfMap.h"
Chenbo Feng89c12f12018-03-21 10:29:18 -070048#include "bpf/bpf_shared.h"
Chenbo Feng116d0552017-12-04 17:25:19 -080049
Chenbo Fengef297172018-03-26 10:53:33 -070050#include "DumpWriter.h"
Chenbo Feng89c12f12018-03-21 10:29:18 -070051#include "FirewallController.h"
Chenbo Feng7e974052018-02-28 22:57:21 -080052#include "InterfaceController.h"
Chenbo Feng116d0552017-12-04 17:25:19 -080053#include "NetlinkListener.h"
Chenbo Feng33cc1032017-10-23 15:16:37 -070054#include "qtaguid/qtaguid.h"
Chenbo Fengf2759682017-10-10 17:31:57 -070055
Chenbo Fengc10a8a42017-12-15 13:56:33 -080056using namespace android::bpf;
Chenbo Fengf2759682017-10-10 17:31:57 -070057
58namespace android {
59namespace net {
60
Chenbo Fengc10a8a42017-12-15 13:56:33 -080061using base::StringPrintf;
62using base::unique_fd;
Chenbo Fengef297172018-03-26 10:53:33 -070063using base::Join;
Chenbo Feng116d0552017-12-04 17:25:19 -080064using netdutils::extract;
65using netdutils::Slice;
66using netdutils::sSyscalls;
Chenbo Fengc10a8a42017-12-15 13:56:33 -080067using netdutils::Status;
68using netdutils::statusFromErrno;
69using netdutils::StatusOr;
Chenbo Feng116d0552017-12-04 17:25:19 -080070using netdutils::status::ok;
Chenbo Fengc10a8a42017-12-15 13:56:33 -080071
Chenbo Feng116d0552017-12-04 17:25:19 -080072constexpr int kSockDiagMsgType = SOCK_DIAG_BY_FAMILY;
73constexpr int kSockDiagDoneMsgType = NLMSG_DONE;
74
75StatusOr<std::unique_ptr<NetlinkListenerInterface>> makeSkDestroyListener() {
76 const auto& sys = sSyscalls.get();
77 ASSIGN_OR_RETURN(auto event, sys.eventfd(0, EFD_CLOEXEC));
78 const int domain = AF_NETLINK;
79 const int type = SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK;
80 const int protocol = NETLINK_INET_DIAG;
81 ASSIGN_OR_RETURN(auto sock, sys.socket(domain, type, protocol));
82
83 sockaddr_nl addr = {
84 .nl_family = AF_NETLINK,
85 .nl_groups = 1 << (SKNLGRP_INET_TCP_DESTROY - 1) | 1 << (SKNLGRP_INET_UDP_DESTROY - 1) |
86 1 << (SKNLGRP_INET6_TCP_DESTROY - 1) | 1 << (SKNLGRP_INET6_UDP_DESTROY - 1)};
87 RETURN_IF_NOT_OK(sys.bind(sock, addr));
88
89 const sockaddr_nl kernel = {.nl_family = AF_NETLINK};
90 RETURN_IF_NOT_OK(sys.connect(sock, kernel));
91
92 std::unique_ptr<NetlinkListenerInterface> listener =
93 std::make_unique<NetlinkListener>(std::move(event), std::move(sock));
94
95 return listener;
96}
97
Chenbo Feng89c12f12018-03-21 10:29:18 -070098Status changeOwnerAndMode(const char* path, gid_t group, const char* debugName, bool netdOnly) {
99 int ret = chown(path, AID_ROOT, group);
100 if (ret != 0) return statusFromErrno(errno, StringPrintf("change %s group failed", debugName));
101
102 if (netdOnly) {
103 ret = chmod(path, S_IRWXU);
104 } else {
105 // Allow both netd and system server to obtain map fd from the path.
106 // chmod doesn't grant permission to all processes in that group to
107 // read/write the bpf map. They still need correct sepolicy to
108 // read/write the map.
109 ret = chmod(path, S_IRWXU | S_IRGRP);
110 }
111 if (ret != 0) return statusFromErrno(errno, StringPrintf("change %s mode failed", debugName));
112 return netdutils::status::ok;
113}
114
115TrafficController::TrafficController() {
Chenbo Fengf43bf812017-12-15 18:27:22 -0800116 ebpfSupported = hasBpfSupport();
Chenbo Feng89c12f12018-03-21 10:29:18 -0700117}
118
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700119Status initialOwnerMap(BpfMap<uint32_t, uint8_t>& map) {
120 // Check and delete all the entries from the map in case it is a runtime
121 // restart
122 const auto deleteAllEntries = [](const uint32_t& key, BpfMap<uint32_t, uint8_t>& map) {
123 Status res = map.deleteValue(key);
124 if (!isOk(res) && (res.code() == ENOENT)) {
125 ALOGE("Failed to delete data(uid=%u): %s\n", key, strerror(res.code()));
126 }
127 return netdutils::status::ok;
128 };
129 // It is safe to delete from this map because nothing will concurrently iterate over it:
130 // - Nothing in netd will iterate over it because we're holding mOwnerMatchMutex.
131 // - Nothing outside netd iterates over it.
132 map.iterate(deleteAllEntries);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700133 uint32_t mapSettingKey = UID_MAP_ENABLED;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700134 uint8_t defaultMapState = 0;
135 return map.writeValue(mapSettingKey, defaultMapState, BPF_NOEXIST);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700136}
137
138Status TrafficController::initMaps() {
Chenbo Fengef1cab32018-04-13 19:50:49 -0700139 std::lock_guard<std::mutex> ownerMapGuard(mOwnerMatchMutex);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700140 RETURN_IF_NOT_OK(
141 mCookieTagMap.getOrCreate(COOKIE_UID_MAP_SIZE, COOKIE_TAG_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700142
143 RETURN_IF_NOT_OK(changeOwnerAndMode(COOKIE_TAG_MAP_PATH, AID_NET_BW_ACCT, "CookieTagMap",
144 false));
145
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700146 RETURN_IF_NOT_OK(mUidCounterSetMap.getOrCreate(UID_COUNTERSET_MAP_SIZE, UID_COUNTERSET_MAP_PATH,
147 BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700148 RETURN_IF_NOT_OK(changeOwnerAndMode(UID_COUNTERSET_MAP_PATH, AID_NET_BW_ACCT,
149 "UidCounterSetMap", false));
150
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700151 RETURN_IF_NOT_OK(
152 mUidStatsMap.getOrCreate(UID_STATS_MAP_SIZE, UID_STATS_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700153 RETURN_IF_NOT_OK(changeOwnerAndMode(UID_STATS_MAP_PATH, AID_NET_BW_STATS, "UidStatsMap",
154 false));
155
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700156 RETURN_IF_NOT_OK(
157 mTagStatsMap.getOrCreate(TAG_STATS_MAP_SIZE, TAG_STATS_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700158 RETURN_IF_NOT_OK(changeOwnerAndMode(TAG_STATS_MAP_PATH, AID_NET_BW_STATS, "TagStatsMap",
159 false));
160
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700161 RETURN_IF_NOT_OK(mIfaceIndexNameMap.getOrCreate(IFACE_INDEX_NAME_MAP_SIZE,
162 IFACE_INDEX_NAME_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700163 RETURN_IF_NOT_OK(changeOwnerAndMode(IFACE_INDEX_NAME_MAP_PATH, AID_NET_BW_STATS,
164 "IfaceIndexNameMap", false));
165
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700166 RETURN_IF_NOT_OK(
167 mDozableUidMap.getOrCreate(UID_OWNER_MAP_SIZE, DOZABLE_UID_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700168 RETURN_IF_NOT_OK(changeOwnerAndMode(DOZABLE_UID_MAP_PATH, AID_ROOT, "DozableUidMap", true));
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700169 RETURN_IF_NOT_OK(initialOwnerMap(mDozableUidMap));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700170
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700171 RETURN_IF_NOT_OK(
172 mStandbyUidMap.getOrCreate(UID_OWNER_MAP_SIZE, STANDBY_UID_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700173 RETURN_IF_NOT_OK(changeOwnerAndMode(STANDBY_UID_MAP_PATH, AID_ROOT, "StandbyUidMap", true));
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700174 RETURN_IF_NOT_OK(initialOwnerMap(mStandbyUidMap));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700175
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700176 RETURN_IF_NOT_OK(mPowerSaveUidMap.getOrCreate(UID_OWNER_MAP_SIZE, POWERSAVE_UID_MAP_PATH,
177 BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700178 RETURN_IF_NOT_OK(changeOwnerAndMode(POWERSAVE_UID_MAP_PATH, AID_ROOT, "PowerSaveUidMap", true));
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700179 RETURN_IF_NOT_OK(initialOwnerMap(mPowerSaveUidMap));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700180
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700181 RETURN_IF_NOT_OK(
182 mIfaceStatsMap.getOrCreate(IFACE_STATS_MAP_SIZE, IFACE_STATS_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700183 RETURN_IF_NOT_OK(changeOwnerAndMode(IFACE_STATS_MAP_PATH, AID_NET_BW_STATS, "IfaceStatsMap",
184 false));
185 return netdutils::status::ok;
186}
187
188Status TrafficController::start() {
189
Chenbo Fengf43bf812017-12-15 18:27:22 -0800190 if (!ebpfSupported) {
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800191 return netdutils::status::ok;
Chenbo Fengf2759682017-10-10 17:31:57 -0700192 }
193
194 /* When netd restart from a crash without total system reboot, the program
195 * is still attached to the cgroup, detach it so the program can be freed
196 * and we can load and attach new program into the target cgroup.
197 *
198 * TODO: Scrape existing socket when run-time restart and clean up the map
199 * if the socket no longer exist
200 */
201
Chenbo Feng89c12f12018-03-21 10:29:18 -0700202 RETURN_IF_NOT_OK(initMaps());
Chenbo Feng7e974052018-02-28 22:57:21 -0800203
204 // Fetch the list of currently-existing interfaces. At this point NetlinkHandler is
205 // already running, so it will call addInterface() when any new interface appears.
206 std::map<std::string, uint32_t> ifacePairs;
207 ASSIGN_OR_RETURN(ifacePairs, InterfaceController::getIfaceList());
208 for (const auto& ifacePair:ifacePairs) {
209 addInterface(ifacePair.first.c_str(), ifacePair.second);
210 }
211
Chenbo Feng5ed17992018-03-13 21:30:49 -0700212
Chenbo Feng116d0552017-12-04 17:25:19 -0800213 auto result = makeSkDestroyListener();
214 if (!isOk(result)) {
215 ALOGE("Unable to create SkDestroyListener: %s", toString(result).c_str());
216 } else {
217 mSkDestroyListener = std::move(result.value());
218 }
219 // Rx handler extracts nfgenmsg looks up and invokes registered dispatch function.
220 const auto rxHandler = [this](const nlmsghdr&, const Slice msg) {
221 inet_diag_msg diagmsg = {};
222 if (extract(msg, diagmsg) < sizeof(inet_diag_msg)) {
223 ALOGE("unrecognized netlink message: %s", toString(msg).c_str());
224 return;
225 }
226 uint64_t sock_cookie = static_cast<uint64_t>(diagmsg.id.idiag_cookie[0]) |
227 (static_cast<uint64_t>(diagmsg.id.idiag_cookie[1]) << 32);
228
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700229 mCookieTagMap.deleteValue(sock_cookie);
Chenbo Feng116d0552017-12-04 17:25:19 -0800230 };
231 expectOk(mSkDestroyListener->subscribe(kSockDiagMsgType, rxHandler));
232
233 // In case multiple netlink message comes in as a stream, we need to handle the rxDone message
234 // properly.
235 const auto rxDoneHandler = [](const nlmsghdr&, const Slice msg) {
236 // Ignore NLMSG_DONE messages
237 inet_diag_msg diagmsg = {};
238 extract(msg, diagmsg);
239 };
240 expectOk(mSkDestroyListener->subscribe(kSockDiagDoneMsgType, rxDoneHandler));
241
Chenbo Feng05393d82018-01-09 15:18:43 -0800242 int* status = nullptr;
243
244 std::vector<const char*> prog_args{
245 "/system/bin/bpfloader",
246 };
Chenbo Feng89c12f12018-03-21 10:29:18 -0700247 int ret = access(BPF_INGRESS_PROG_PATH, R_OK);
Chenbo Feng05393d82018-01-09 15:18:43 -0800248 if (ret != 0 && errno == ENOENT) {
249 prog_args.push_back((char*)"-i");
250 }
251 ret = access(BPF_EGRESS_PROG_PATH, R_OK);
252 if (ret != 0 && errno == ENOENT) {
253 prog_args.push_back((char*)"-e");
254 }
Chenbo Feng5ed17992018-03-13 21:30:49 -0700255 ret = access(XT_BPF_INGRESS_PROG_PATH, R_OK);
256 if (ret != 0 && errno == ENOENT) {
257 prog_args.push_back((char*)"-p");
258 }
259 ret = access(XT_BPF_EGRESS_PROG_PATH, R_OK);
260 if (ret != 0 && errno == ENOENT) {
261 prog_args.push_back((char*)"-m");
262 }
Chenbo Feng05393d82018-01-09 15:18:43 -0800263
264 if (prog_args.size() == 1) {
Chenbo Feng5ed17992018-03-13 21:30:49 -0700265 // all program are loaded already.
Chenbo Feng05393d82018-01-09 15:18:43 -0800266 return netdutils::status::ok;
267 }
268
269 prog_args.push_back(nullptr);
270 ret = android_fork_execvp(prog_args.size(), (char**)prog_args.data(), status, false, true);
271 if (ret) {
272 ret = errno;
273 ALOGE("failed to execute %s: %s", prog_args[0], strerror(errno));
274 return statusFromErrno(ret, "run bpf loader failed");
275 }
276 return netdutils::status::ok;
Chenbo Fengf2759682017-10-10 17:31:57 -0700277}
278
Chenbo Fengf2759682017-10-10 17:31:57 -0700279int TrafficController::tagSocket(int sockFd, uint32_t tag, uid_t uid) {
Chenbo Fengbc6d4702018-04-11 18:27:19 -0700280 if (!ebpfSupported) {
281 if (legacy_tagSocket(sockFd, tag, uid)) return -errno;
282 return 0;
283 }
Chenbo Feng33cc1032017-10-23 15:16:37 -0700284
Chenbo Fengf2759682017-10-10 17:31:57 -0700285 uint64_t sock_cookie = getSocketCookie(sockFd);
Chenbo Fengef1cab32018-04-13 19:50:49 -0700286 if (sock_cookie == NONEXISTENT_COOKIE) return -errno;
Chenbo Fengf2759682017-10-10 17:31:57 -0700287 UidTag newKey = {.uid = (uint32_t)uid, .tag = tag};
288
289 // Update the tag information of a socket to the cookieUidMap. Use BPF_ANY
290 // flag so it will insert a new entry to the map if that value doesn't exist
291 // yet. And update the tag if there is already a tag stored. Since the eBPF
292 // program in kernel only read this map, and is protected by rcu read lock. It
293 // should be fine to cocurrently update the map while eBPF program is running.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700294 Status res = mCookieTagMap.writeValue(sock_cookie, newKey, BPF_ANY);
295 if (!isOk(res)) {
296 ALOGE("Failed to tag the socket: %s, fd: %d", strerror(res.code()),
297 mCookieTagMap.getMap().get());
Chenbo Fengf2759682017-10-10 17:31:57 -0700298 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700299 return -res.code();
Chenbo Fengf2759682017-10-10 17:31:57 -0700300}
301
302int TrafficController::untagSocket(int sockFd) {
Chenbo Fengbc6d4702018-04-11 18:27:19 -0700303 if (!ebpfSupported) {
304 if (legacy_untagSocket(sockFd)) return -errno;
305 return 0;
306 }
Chenbo Fengf2759682017-10-10 17:31:57 -0700307 uint64_t sock_cookie = getSocketCookie(sockFd);
308
Chenbo Fengef1cab32018-04-13 19:50:49 -0700309 if (sock_cookie == NONEXISTENT_COOKIE) return -errno;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700310 Status res = mCookieTagMap.deleteValue(sock_cookie);
311 if (!isOk(res)) {
312 ALOGE("Failed to untag socket: %s\n", strerror(res.code()));
Chenbo Fengf2759682017-10-10 17:31:57 -0700313 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700314 return -res.code();
Chenbo Fengf2759682017-10-10 17:31:57 -0700315}
316
317int TrafficController::setCounterSet(int counterSetNum, uid_t uid) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700318 if (counterSetNum < 0 || counterSetNum >= OVERFLOW_COUNTERSET) return -EINVAL;
319 Status res;
Chenbo Fengbc6d4702018-04-11 18:27:19 -0700320 if (!ebpfSupported) {
321 if (legacy_setCounterSet(counterSetNum, uid)) return -errno;
322 return 0;
323 }
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800324
325 // The default counter set for all uid is 0, so deleting the current counterset for that uid
326 // will automatically set it to 0.
Chenbo Fengf2759682017-10-10 17:31:57 -0700327 if (counterSetNum == 0) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700328 Status res = mUidCounterSetMap.deleteValue(uid);
329 if (isOk(res) || (!isOk(res) && res.code() == ENOENT)) {
Chenbo Fengf2759682017-10-10 17:31:57 -0700330 return 0;
331 } else {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700332 ALOGE("Failed to delete the counterSet: %s\n", strerror(res.code()));
333 return -res.code();
Chenbo Fengf2759682017-10-10 17:31:57 -0700334 }
335 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700336 uint8_t tmpCounterSetNum = (uint8_t)counterSetNum;
337 res = mUidCounterSetMap.writeValue(uid, tmpCounterSetNum, BPF_ANY);
338 if (!isOk(res)) {
339 ALOGE("Failed to set the counterSet: %s, fd: %d", strerror(res.code()),
340 mUidCounterSetMap.getMap().get());
341 return -res.code();
Chenbo Fengf2759682017-10-10 17:31:57 -0700342 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700343 return 0;
Chenbo Fengf2759682017-10-10 17:31:57 -0700344}
345
346int TrafficController::deleteTagData(uint32_t tag, uid_t uid) {
Chenbo Feng33cc1032017-10-23 15:16:37 -0700347
Chenbo Fengbc6d4702018-04-11 18:27:19 -0700348 if (!ebpfSupported) {
349 if (legacy_deleteTagData(tag, uid)) return -errno;
350 return 0;
351 }
Chenbo Feng33cc1032017-10-23 15:16:37 -0700352
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800353 // First we go through the cookieTagMap to delete the target uid tag combination. Or delete all
Chenbo Fengef1cab32018-04-13 19:50:49 -0700354 // the tags related to the uid if the tag is 0.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700355 const auto deleteMatchedCookieEntries = [uid, tag](const uint64_t& key, const UidTag& value,
356 BpfMap<uint64_t, UidTag>& map) {
357 if (value.uid == uid && (value.tag == tag || tag == 0)) {
358 Status res = map.deleteValue(key);
359 if (isOk(res) || (res.code() == ENOENT)) {
360 return netdutils::status::ok;
Chenbo Fengf2759682017-10-10 17:31:57 -0700361 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700362 ALOGE("Failed to delete data(cookie = %" PRIu64 "): %s\n", key, strerror(res.code()));
Chenbo Fengf2759682017-10-10 17:31:57 -0700363 }
Chenbo Fengef1cab32018-04-13 19:50:49 -0700364 // Move forward to next cookie in the map.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700365 return netdutils::status::ok;
Chenbo Fengef1cab32018-04-13 19:50:49 -0700366 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700367 mCookieTagMap.iterateWithValue(deleteMatchedCookieEntries);
Chenbo Fengf2759682017-10-10 17:31:57 -0700368 // Now we go through the Tag stats map and delete the data entry with correct uid and tag
Chenbo Fengef1cab32018-04-13 19:50:49 -0700369 // combination. Or all tag stats under that uid if the target tag is 0.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700370 const auto deleteMatchedUidTagEntries = [uid, tag](const StatsKey& key,
371 BpfMap<StatsKey, StatsValue>& map) {
372 if (key.uid == uid && (key.tag == tag || tag == 0)) {
373 Status res = map.deleteValue(key);
374 if (isOk(res) || (res.code() == ENOENT)) {
Chenbo Fengef1cab32018-04-13 19:50:49 -0700375 //Entry is deleted, use the current key to get a new nextKey;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700376 return netdutils::status::ok;
Chenbo Fengf2759682017-10-10 17:31:57 -0700377 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700378 ALOGE("Failed to delete data(uid=%u, tag=%u): %s\n", key.uid, key.tag,
379 strerror(res.code()));
Chenbo Fengf2759682017-10-10 17:31:57 -0700380 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700381 return netdutils::status::ok;
Chenbo Fengef1cab32018-04-13 19:50:49 -0700382 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700383 mTagStatsMap.iterate(deleteMatchedUidTagEntries);
Chenbo Fengf2759682017-10-10 17:31:57 -0700384 // If the tag is not zero, we already deleted all the data entry required. If tag is 0, we also
Chenbo Fenged37fea2017-12-13 19:35:01 -0800385 // need to delete the stats stored in uidStatsMap and counterSet map.
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800386 if (tag != 0) return 0;
387
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700388 Status res = mUidCounterSetMap.deleteValue(uid);
389 if (!isOk(res) && res.code() != ENOENT) {
390 ALOGE("Failed to delete counterSet data(uid=%u, tag=%u): %s\n", uid, tag,
391 strerror(res.code()));
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800392 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700393 mUidStatsMap.iterate(deleteMatchedUidTagEntries);
394 return 0;
Chenbo Fengf2759682017-10-10 17:31:57 -0700395}
396
Chenbo Feng7e974052018-02-28 22:57:21 -0800397int TrafficController::addInterface(const char* name, uint32_t ifaceIndex) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700398 if (!ebpfSupported) return 0;
Chenbo Feng7e974052018-02-28 22:57:21 -0800399
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700400 IfaceValue iface;
Chenbo Feng7e974052018-02-28 22:57:21 -0800401 if (ifaceIndex == 0) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700402 ALOGE("Unknown interface %s(%d)", name, ifaceIndex);
Chenbo Feng7e974052018-02-28 22:57:21 -0800403 return -1;
404 }
405
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700406 strlcpy(iface.name, name, sizeof(IfaceValue));
407 Status res = mIfaceIndexNameMap.writeValue(ifaceIndex, iface, BPF_ANY);
408 if (!isOk(res)) {
409 ALOGE("Failed to add iface %s(%d): %s", name, ifaceIndex, strerror(res.code()));
410 return -res.code();
Chenbo Feng7e974052018-02-28 22:57:21 -0800411 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700412 return 0;
Chenbo Feng7e974052018-02-28 22:57:21 -0800413}
414
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700415Status TrafficController::updateOwnerMapEntry(BpfMap<uint32_t, uint8_t>& map, uid_t uid,
416 FirewallRule rule, FirewallType type) {
Chenbo Feng89c12f12018-03-21 10:29:18 -0700417 if (uid == UID_MAP_ENABLED) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700418 return statusFromErrno(-EINVAL, "This uid is reserved for map state");
Chenbo Feng89c12f12018-03-21 10:29:18 -0700419 }
420
421 if ((rule == ALLOW && type == WHITELIST) || (rule == DENY && type == BLACKLIST)) {
422 uint8_t flag = (type == WHITELIST) ? BPF_PASS : BPF_DROP;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700423 RETURN_IF_NOT_OK(map.writeValue(uid, flag, BPF_ANY));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700424 } else if ((rule == ALLOW && type == BLACKLIST) || (rule == DENY && type == WHITELIST)) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700425 RETURN_IF_NOT_OK(map.deleteValue(uid));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700426 } else {
427 //Cannot happen.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700428 return statusFromErrno(-EINVAL, "");
Chenbo Feng89c12f12018-03-21 10:29:18 -0700429 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700430 return netdutils::status::ok;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700431}
432
433int TrafficController::changeUidOwnerRule(ChildChain chain, uid_t uid, FirewallRule rule,
434 FirewallType type) {
435 std::lock_guard<std::mutex> guard(mOwnerMatchMutex);
436 if (!ebpfSupported) {
437 ALOGE("bpf is not set up, should use iptables rule");
438 return -ENOSYS;
439 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700440 Status res;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700441 switch (chain) {
442 case DOZABLE:
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700443 res = updateOwnerMapEntry(mDozableUidMap, uid, rule, type);
444 break;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700445 case STANDBY:
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700446 res = updateOwnerMapEntry(mStandbyUidMap, uid, rule, type);
447 break;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700448 case POWERSAVE:
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700449 res = updateOwnerMapEntry(mPowerSaveUidMap, uid, rule, type);
450 break;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700451 case NONE:
452 default:
453 return -EINVAL;
454 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700455 if (!isOk(res)) {
456 ALOGE("change uid(%u) rule of %d failed: %s, rule: %d, type: %d", uid, chain,
457 res.msg().c_str(), rule, type);
458 return -res.code();
459 }
460 return 0;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700461}
462
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700463Status TrafficController::replaceUidsInMap(BpfMap<uint32_t, uint8_t>& map,
464 const std::vector<int32_t>& uids, FirewallRule rule,
465 FirewallType type) {
Chenbo Feng89c12f12018-03-21 10:29:18 -0700466 std::set<int32_t> uidSet(uids.begin(), uids.end());
467 std::vector<uint32_t> uidsToDelete;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700468 auto getUidsToDelete = [&uidsToDelete, &uidSet](const uint32_t& key,
469 const BpfMap<uint32_t, uint8_t>&) {
470 if (key != UID_MAP_ENABLED && uidSet.find((int32_t)key) == uidSet.end()) {
471 uidsToDelete.push_back(key);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700472 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700473 return netdutils::status::ok;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700474 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700475 RETURN_IF_NOT_OK(map.iterate(getUidsToDelete));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700476
477 for(auto uid : uidsToDelete) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700478 RETURN_IF_NOT_OK(map.deleteValue(uid));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700479 }
480
481 for (auto uid : uids) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700482 RETURN_IF_NOT_OK(updateOwnerMapEntry(map, uid, rule, type));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700483 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700484 return netdutils::status::ok;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700485}
486
487int TrafficController::replaceUidOwnerMap(const std::string& name, bool isWhitelist,
488 const std::vector<int32_t>& uids) {
489 std::lock_guard<std::mutex> guard(mOwnerMatchMutex);
490 FirewallRule rule;
491 FirewallType type;
492 if (isWhitelist) {
493 type = WHITELIST;
494 rule = ALLOW;
495 } else {
496 type = BLACKLIST;
497 rule = DENY;
498 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700499 Status res;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700500 if (!name.compare(FirewallController::LOCAL_DOZABLE)) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700501 res = replaceUidsInMap(mDozableUidMap, uids, rule, type);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700502 } else if (!name.compare(FirewallController::LOCAL_STANDBY)) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700503 res = replaceUidsInMap(mStandbyUidMap, uids, rule, type);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700504 } else if (!name.compare(FirewallController::LOCAL_POWERSAVE)) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700505 res = replaceUidsInMap(mPowerSaveUidMap, uids, rule, type);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700506 } else {
507 ALOGE("unknown chain name: %s", name.c_str());
508 return -EINVAL;
509 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700510 if (!isOk(res)) {
511 ALOGE("Failed to clean up chain: %s: %s", name.c_str(), res.msg().c_str());
512 return -res.code();
Chenbo Feng89c12f12018-03-21 10:29:18 -0700513 }
514 return 0;
515}
516
517int TrafficController::toggleUidOwnerMap(ChildChain chain, bool enable) {
518 std::lock_guard<std::mutex> guard(mOwnerMatchMutex);
519 uint32_t keyUid = UID_MAP_ENABLED;
520 uint8_t mapState = enable ? 1 : 0;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700521 Status res;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700522 switch (chain) {
523 case DOZABLE:
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700524 res = mDozableUidMap.writeValue(keyUid, mapState, BPF_EXIST);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700525 break;
526 case STANDBY:
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700527 res = mStandbyUidMap.writeValue(keyUid, mapState, BPF_EXIST);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700528 break;
529 case POWERSAVE:
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700530 res = mPowerSaveUidMap.writeValue(keyUid, mapState, BPF_EXIST);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700531 break;
532 default:
533 return -EINVAL;
534 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700535 if (!isOk(res)) {
536 ALOGE("Failed to toggleUidOwnerMap(%d): %s", chain, res.msg().c_str());
Chenbo Feng89c12f12018-03-21 10:29:18 -0700537 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700538 return -res.code();
Chenbo Feng89c12f12018-03-21 10:29:18 -0700539}
540
Chenbo Feng07d43fe2017-12-21 14:38:51 -0800541bool TrafficController::checkBpfStatsEnable() {
542 return ebpfSupported;
543}
544
Chenbo Fengef297172018-03-26 10:53:33 -0700545std::string getProgramStatus(const char *path) {
546 int ret = access(path, R_OK);
547 if (ret == 0) {
548 return StringPrintf("OK");
549 }
550 if (ret != 0 && errno == ENOENT) {
551 return StringPrintf("program is missing at: %s", path);
552 }
553 return StringPrintf("check Program %s error: %s", path, strerror(errno));
554}
555
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700556std::string getMapStatus(const base::unique_fd& map_fd, const char* path) {
Chenbo Fengef297172018-03-26 10:53:33 -0700557 if (map_fd.get() < 0) {
558 return StringPrintf("map fd lost");
559 }
560 if (access(path, F_OK) != 0) {
561 return StringPrintf("map not pinned to location: %s", path);
562 }
563 return StringPrintf("OK");
564}
565
566void dumpBpfMap(std::string mapName, DumpWriter& dw, const std::string& header) {
567 dw.blankline();
568 dw.println("%s:", mapName.c_str());
569 if(!header.empty()) {
570 dw.println(header.c_str());
571 }
572}
573
574const String16 TrafficController::DUMP_KEYWORD = String16("trafficcontroller");
575
576void TrafficController::dump(DumpWriter& dw, bool verbose) {
Chenbo Fengef1cab32018-04-13 19:50:49 -0700577 std::lock_guard<std::mutex> ownerMapGuard(mOwnerMatchMutex);
Chenbo Fengef297172018-03-26 10:53:33 -0700578 dw.incIndent();
579 dw.println("TrafficController");
580
581 dw.incIndent();
582 dw.println("BPF module status: %s", ebpfSupported? "ON" : "OFF");
583
584 if (!ebpfSupported)
585 return;
586
587 dw.blankline();
588 dw.println("mCookieTagMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700589 getMapStatus(mCookieTagMap.getMap(), COOKIE_TAG_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700590 dw.println("mUidCounterSetMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700591 getMapStatus(mUidCounterSetMap.getMap(), UID_COUNTERSET_MAP_PATH).c_str());
592 dw.println("mUidStatsMap status: %s",
593 getMapStatus(mUidStatsMap.getMap(), UID_STATS_MAP_PATH).c_str());
594 dw.println("mTagStatsMap status: %s",
595 getMapStatus(mTagStatsMap.getMap(), TAG_STATS_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700596 dw.println("mIfaceIndexNameMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700597 getMapStatus(mIfaceIndexNameMap.getMap(), IFACE_INDEX_NAME_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700598 dw.println("mIfaceStatsMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700599 getMapStatus(mIfaceStatsMap.getMap(), IFACE_STATS_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700600 dw.println("mDozableUidMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700601 getMapStatus(mDozableUidMap.getMap(), DOZABLE_UID_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700602 dw.println("mStandbyUidMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700603 getMapStatus(mStandbyUidMap.getMap(), STANDBY_UID_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700604 dw.println("mPowerSaveUidMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700605 getMapStatus(mPowerSaveUidMap.getMap(), POWERSAVE_UID_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700606
607 dw.blankline();
608 dw.println("Cgroup ingress program status: %s",
609 getProgramStatus(BPF_INGRESS_PROG_PATH).c_str());
610 dw.println("Cgroup egress program status: %s", getProgramStatus(BPF_EGRESS_PROG_PATH).c_str());
611 dw.println("xt_bpf ingress program status: %s",
612 getProgramStatus(XT_BPF_INGRESS_PROG_PATH).c_str());
613 dw.println("xt_bpf egress program status: %s",
614 getProgramStatus(XT_BPF_EGRESS_PROG_PATH).c_str());
615
616 if(!verbose) return;
617
618 dw.blankline();
619 dw.println("BPF map content:");
620
621 dw.incIndent();
622
623 // Print CookieTagMap content.
624 dumpBpfMap("mCookieTagMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700625 const auto printCookieTagInfo = [&dw](const uint64_t& key, const UidTag& value,
626 const BpfMap<uint64_t, UidTag>&) {
627 dw.println("cookie=%" PRIu64 " tag=0x%x uid=%u", key, value.tag, value.uid);
628 return netdutils::status::ok;
Chenbo Fengef297172018-03-26 10:53:33 -0700629 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700630 Status res = mCookieTagMap.iterateWithValue(printCookieTagInfo);
631 if (!isOk(res)) {
632 dw.println("mCookieTagMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700633 }
634
635 // Print UidCounterSetMap Content
636 dumpBpfMap("mUidCounterSetMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700637 const auto printUidInfo = [&dw](const uint32_t& key, const uint8_t& value,
638 const BpfMap<uint32_t, uint8_t>&) {
639 dw.println("%u %u", key, value);
640 return netdutils::status::ok;
Chenbo Fengef297172018-03-26 10:53:33 -0700641 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700642 res = mUidCounterSetMap.iterateWithValue(printUidInfo);
643 if (!isOk(res)) {
644 dw.println("mUidCounterSetMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700645 }
646
647 // Print uidStatsMap content
648 std::string statsHeader = StringPrintf("ifaceIndex ifaceName tag_hex uid_int cnt_set rxBytes"
649 " rxPackets txBytes txPackets");
650 dumpBpfMap("mUidStatsMap", dw, statsHeader);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700651 const auto printStatsInfo = [&dw, this](const StatsKey& key, const StatsValue& value,
652 const BpfMap<StatsKey, StatsValue>&) {
653 uint32_t ifIndex = key.ifaceIndex;
654 auto ifname = mIfaceIndexNameMap.readValue(ifIndex);
655 if (!isOk(ifname)) {
656 strlcpy(ifname.value().name, "unknown", sizeof(IfaceValue));
Chenbo Fengef297172018-03-26 10:53:33 -0700657 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700658 dw.println("%u %s 0x%x %u %u %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, ifIndex,
659 ifname.value().name, key.tag, key.uid, key.counterSet, value.rxBytes,
660 value.rxPackets, value.txBytes, value.txPackets);
661 return netdutils::status::ok;
Chenbo Fengef297172018-03-26 10:53:33 -0700662 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700663 res = mUidStatsMap.iterateWithValue(printStatsInfo);
664 if (!isOk(res)) {
665 dw.println("mUidStatsMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700666 }
667
668 // Print TagStatsMap content.
669 dumpBpfMap("mTagStatsMap", dw, statsHeader);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700670 res = mTagStatsMap.iterateWithValue(printStatsInfo);
671 if (!isOk(res)) {
672 dw.println("mTagStatsMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700673 }
674
675 // Print ifaceIndexToNameMap content.
676 dumpBpfMap("mIfaceIndexNameMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700677 const auto printIfaceNameInfo = [&dw](const uint32_t& key, const IfaceValue& value,
678 const BpfMap<uint32_t, IfaceValue>&) {
679 const char* ifname = value.name;
680 dw.println("ifaceIndex=%u ifaceName=%s", key, ifname);
681 return netdutils::status::ok;
Chenbo Fengef297172018-03-26 10:53:33 -0700682 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700683 res = mIfaceIndexNameMap.iterateWithValue(printIfaceNameInfo);
684 if (!isOk(res)) {
685 dw.println("mIfaceIndexNameMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700686 }
687
688 // Print ifaceStatsMap content
689 std::string ifaceStatsHeader = StringPrintf("ifaceIndex ifaceName rxBytes rxPackets txBytes"
690 " txPackets");
691 dumpBpfMap("mIfaceStatsMap:", dw, ifaceStatsHeader);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700692 const auto printIfaceStatsInfo = [&dw, this](const uint32_t& key, const StatsValue& value,
693 const BpfMap<uint32_t, StatsValue>&) {
694 auto ifname = mIfaceIndexNameMap.readValue(key);
695 if (!isOk(ifname)) {
696 strlcpy(ifname.value().name, "unknown", sizeof(IfaceValue));
Chenbo Fengef297172018-03-26 10:53:33 -0700697 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700698 dw.println("%u %s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, key, ifname.value().name,
699 value.rxBytes, value.rxPackets, value.txBytes, value.txPackets);
700 return netdutils::status::ok;
Chenbo Fengef297172018-03-26 10:53:33 -0700701 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700702 res = mIfaceStatsMap.iterateWithValue(printIfaceStatsInfo);
703 if (!isOk(res)) {
704 dw.println("mIfaceStatsMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700705 }
706
707 // Print owner match uid maps
Chenbo Fengef297172018-03-26 10:53:33 -0700708 dumpBpfMap("mDozableUidMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700709 res = mDozableUidMap.iterateWithValue(printUidInfo);
710 if (!isOk(res)) {
711 dw.println("mDozableUidMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700712 }
713
714 dumpBpfMap("mStandbyUidMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700715 res = mStandbyUidMap.iterateWithValue(printUidInfo);
716 if (!isOk(res)) {
717 dw.println("mDozableUidMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700718 }
719
720 dumpBpfMap("mPowerSaveUidMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700721 res = mPowerSaveUidMap.iterateWithValue(printUidInfo);
722 if (!isOk(res)) {
723 dw.println("mDozableUidMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700724 }
725
726 dw.decIndent();
727
728 dw.decIndent();
729
730 dw.decIndent();
731
732}
733
Chenbo Fengf2759682017-10-10 17:31:57 -0700734} // namespace net
735} // namespace android