blob: b1f1985c976377572ebfda8cce8e572c13618177 [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 Feng116d0552017-12-04 17:25:19 -080048
Chenbo Fengef297172018-03-26 10:53:33 -070049#include "DumpWriter.h"
Chenbo Feng89c12f12018-03-21 10:29:18 -070050#include "FirewallController.h"
Chenbo Feng7e974052018-02-28 22:57:21 -080051#include "InterfaceController.h"
Chenbo Feng116d0552017-12-04 17:25:19 -080052#include "NetlinkListener.h"
Chenbo Feng33cc1032017-10-23 15:16:37 -070053#include "qtaguid/qtaguid.h"
Chenbo Fengf2759682017-10-10 17:31:57 -070054
Bernie Innocenti7e25ec02018-07-02 19:32:17 +090055using namespace android::bpf; // NOLINT(google-build-using-namespace): grandfathered
Chenbo Fengf2759682017-10-10 17:31:57 -070056
57namespace android {
58namespace net {
59
Chenbo Fengc10a8a42017-12-15 13:56:33 -080060using base::StringPrintf;
61using base::unique_fd;
Chenbo Fengef297172018-03-26 10:53:33 -070062using base::Join;
Chenbo Feng116d0552017-12-04 17:25:19 -080063using netdutils::extract;
64using netdutils::Slice;
65using netdutils::sSyscalls;
Chenbo Fengc10a8a42017-12-15 13:56:33 -080066using netdutils::Status;
67using netdutils::statusFromErrno;
68using netdutils::StatusOr;
Chenbo Feng116d0552017-12-04 17:25:19 -080069using netdutils::status::ok;
Chenbo Fengc10a8a42017-12-15 13:56:33 -080070
Chenbo Feng116d0552017-12-04 17:25:19 -080071constexpr int kSockDiagMsgType = SOCK_DIAG_BY_FAMILY;
72constexpr int kSockDiagDoneMsgType = NLMSG_DONE;
73
Chenbo Feng703798e2018-06-15 17:07:59 -070074#define UID_MATCH_MSG_TRANS(result, target, match) \
75 do { \
76 if (match & target) { \
77 result.append(StringPrintf(" %s", #target)); \
78 match &= ~target; \
79 } \
80 } while (0)
81
82const std::string uidMatchTypeToString(uint8_t match) {
83 std::string matchType;
84 UID_MATCH_MSG_TRANS(matchType, HAPPY_BOX_MATCH, match);
85 UID_MATCH_MSG_TRANS(matchType, PENALTY_BOX_MATCH, match);
86 UID_MATCH_MSG_TRANS(matchType, DOZABLE_MATCH, match);
87 UID_MATCH_MSG_TRANS(matchType, STANDBY_MATCH, match);
88 UID_MATCH_MSG_TRANS(matchType, POWERSAVE_MATCH, match);
89 if (match) {
90 return StringPrintf("Unknown match: %u", match);
91 }
92 return matchType;
93}
94
Chenbo Feng49586642018-08-30 18:01:53 -070095StatusOr<std::unique_ptr<NetlinkListenerInterface>> TrafficController::makeSkDestroyListener() {
Chenbo Feng116d0552017-12-04 17:25:19 -080096 const auto& sys = sSyscalls.get();
97 ASSIGN_OR_RETURN(auto event, sys.eventfd(0, EFD_CLOEXEC));
98 const int domain = AF_NETLINK;
99 const int type = SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK;
100 const int protocol = NETLINK_INET_DIAG;
101 ASSIGN_OR_RETURN(auto sock, sys.socket(domain, type, protocol));
102
Lorenzo Colitti436efdb2018-07-26 17:20:57 +0900103 // TODO: if too many sockets are closed too quickly, we can overflow the socket buffer, and
104 // some entries in mCookieTagMap will not be freed. In order to fix this we would need to
105 // periodically dump all sockets and remove the tag entries for sockets that have been closed.
106 // For now, set a large-enough buffer that we can close hundreds of sockets without getting
107 // ENOBUFS and leaking mCookieTagMap entries.
108 int rcvbuf = 512 * 1024;
109 auto ret = sys.setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
110 if (!ret.ok()) {
111 ALOGW("Failed to set SkDestroyListener buffer size to %d: %s", rcvbuf, ret.msg().c_str());
112 }
113
Chenbo Feng116d0552017-12-04 17:25:19 -0800114 sockaddr_nl addr = {
115 .nl_family = AF_NETLINK,
116 .nl_groups = 1 << (SKNLGRP_INET_TCP_DESTROY - 1) | 1 << (SKNLGRP_INET_UDP_DESTROY - 1) |
117 1 << (SKNLGRP_INET6_TCP_DESTROY - 1) | 1 << (SKNLGRP_INET6_UDP_DESTROY - 1)};
118 RETURN_IF_NOT_OK(sys.bind(sock, addr));
119
120 const sockaddr_nl kernel = {.nl_family = AF_NETLINK};
121 RETURN_IF_NOT_OK(sys.connect(sock, kernel));
122
123 std::unique_ptr<NetlinkListenerInterface> listener =
Chenbo Feng46296702018-08-23 12:20:22 -0700124 std::make_unique<NetlinkListener>(std::move(event), std::move(sock), "SkDestroyListen");
Chenbo Feng116d0552017-12-04 17:25:19 -0800125
126 return listener;
127}
128
Chenbo Feng89c12f12018-03-21 10:29:18 -0700129Status changeOwnerAndMode(const char* path, gid_t group, const char* debugName, bool netdOnly) {
130 int ret = chown(path, AID_ROOT, group);
131 if (ret != 0) return statusFromErrno(errno, StringPrintf("change %s group failed", debugName));
132
133 if (netdOnly) {
134 ret = chmod(path, S_IRWXU);
135 } else {
136 // Allow both netd and system server to obtain map fd from the path.
137 // chmod doesn't grant permission to all processes in that group to
138 // read/write the bpf map. They still need correct sepolicy to
139 // read/write the map.
140 ret = chmod(path, S_IRWXU | S_IRGRP);
141 }
142 if (ret != 0) return statusFromErrno(errno, StringPrintf("change %s mode failed", debugName));
143 return netdutils::status::ok;
144}
145
146TrafficController::TrafficController() {
Chenbo Fengf43bf812017-12-15 18:27:22 -0800147 ebpfSupported = hasBpfSupport();
Chenbo Feng89c12f12018-03-21 10:29:18 -0700148}
149
Chenbo Feng89c12f12018-03-21 10:29:18 -0700150Status TrafficController::initMaps() {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900151 std::lock_guard ownerMapGuard(mOwnerMatchMutex);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700152 RETURN_IF_NOT_OK(
153 mCookieTagMap.getOrCreate(COOKIE_UID_MAP_SIZE, COOKIE_TAG_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700154
155 RETURN_IF_NOT_OK(changeOwnerAndMode(COOKIE_TAG_MAP_PATH, AID_NET_BW_ACCT, "CookieTagMap",
156 false));
157
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700158 RETURN_IF_NOT_OK(mUidCounterSetMap.getOrCreate(UID_COUNTERSET_MAP_SIZE, UID_COUNTERSET_MAP_PATH,
159 BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700160 RETURN_IF_NOT_OK(changeOwnerAndMode(UID_COUNTERSET_MAP_PATH, AID_NET_BW_ACCT,
161 "UidCounterSetMap", false));
162
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700163 RETURN_IF_NOT_OK(
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700164 mAppUidStatsMap.getOrCreate(UID_STATS_MAP_SIZE, APP_UID_STATS_MAP_PATH, BPF_MAP_TYPE_HASH));
165 RETURN_IF_NOT_OK(
166 changeOwnerAndMode(APP_UID_STATS_MAP_PATH, AID_NET_BW_STATS, "AppUidStatsMap", false));
167
168 RETURN_IF_NOT_OK(
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700169 mUidStatsMap.getOrCreate(UID_STATS_MAP_SIZE, UID_STATS_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700170 RETURN_IF_NOT_OK(changeOwnerAndMode(UID_STATS_MAP_PATH, AID_NET_BW_STATS, "UidStatsMap",
171 false));
172
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700173 RETURN_IF_NOT_OK(
174 mTagStatsMap.getOrCreate(TAG_STATS_MAP_SIZE, TAG_STATS_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700175 RETURN_IF_NOT_OK(changeOwnerAndMode(TAG_STATS_MAP_PATH, AID_NET_BW_STATS, "TagStatsMap",
176 false));
177
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700178 RETURN_IF_NOT_OK(mIfaceIndexNameMap.getOrCreate(IFACE_INDEX_NAME_MAP_SIZE,
179 IFACE_INDEX_NAME_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700180 RETURN_IF_NOT_OK(changeOwnerAndMode(IFACE_INDEX_NAME_MAP_PATH, AID_NET_BW_STATS,
181 "IfaceIndexNameMap", false));
182
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700183 RETURN_IF_NOT_OK(
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700184 mIfaceStatsMap.getOrCreate(IFACE_STATS_MAP_SIZE, IFACE_STATS_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700185 RETURN_IF_NOT_OK(changeOwnerAndMode(IFACE_STATS_MAP_PATH, AID_NET_BW_STATS, "IfaceStatsMap",
186 false));
Chenbo Feng95892f32018-06-07 14:52:02 -0700187
Chenbo Feng703798e2018-06-15 17:07:59 -0700188 RETURN_IF_NOT_OK(mConfigurationMap.getOrCreate(CONFIGURATION_MAP_SIZE, CONFIGURATION_MAP_PATH,
189 BPF_MAP_TYPE_HASH));
190 RETURN_IF_NOT_OK(
191 changeOwnerAndMode(CONFIGURATION_MAP_PATH, AID_ROOT, "ConfigurationMap", true));
192 uint32_t key = CONFIGURATION_KEY;
193 uint8_t initialConfiguration = DEFAULT_CONFIG;
194 RETURN_IF_NOT_OK(mConfigurationMap.writeValue(key, initialConfiguration, BPF_ANY));
195
196 RETURN_IF_NOT_OK(
197 mUidOwnerMap.getOrCreate(UID_OWNER_MAP_SIZE, UID_OWNER_MAP_PATH, BPF_MAP_TYPE_HASH));
198 RETURN_IF_NOT_OK(changeOwnerAndMode(UID_OWNER_MAP_PATH, AID_ROOT, "UidOwnerMap", true));
Bernie Innocenti6f9fd902018-10-11 20:50:23 +0900199 RETURN_IF_NOT_OK(mUidOwnerMap.clear());
Chenbo Feng89c12f12018-03-21 10:29:18 -0700200 return netdutils::status::ok;
201}
202
Chenbo Fengb2d213d2018-12-04 11:31:00 -0800203static Status attachProgramToCgroup(const char* programPath, const int cgroupFd,
204 bpf_attach_type type) {
205 unique_fd cgroupProg(bpfFdGet(programPath, 0));
206 if (cgroupProg == -1) {
207 int ret = errno;
208 ALOGE("Failed to get program from %s: %s", programPath, strerror(ret));
209 return statusFromErrno(ret, "cgroup program get failed");
210 }
211 if (android::bpf::attachProgram(type, cgroupProg, cgroupFd)) {
212 int ret = errno;
213 ALOGE("Program from %s attach failed: %s", programPath, strerror(ret));
214 return statusFromErrno(ret, "program attach failed");
215 }
216 return netdutils::status::ok;
217}
218
219static Status initPrograms() {
220 unique_fd cg_fd(open(CGROUP_ROOT_PATH, O_DIRECTORY | O_RDONLY | O_CLOEXEC));
221 if (cg_fd == -1) {
222 int ret = errno;
223 ALOGE("Failed to open the cgroup directory: %s", strerror(ret));
224 return statusFromErrno(ret, "Open the cgroup directory failed");
225 }
226 RETURN_IF_NOT_OK(attachProgramToCgroup(BPF_EGRESS_PROG_PATH, cg_fd, BPF_CGROUP_INET_EGRESS));
227 RETURN_IF_NOT_OK(attachProgramToCgroup(BPF_INGRESS_PROG_PATH, cg_fd, BPF_CGROUP_INET_INGRESS));
228 return netdutils::status::ok;
229}
230
Chenbo Feng89c12f12018-03-21 10:29:18 -0700231Status TrafficController::start() {
232
Chenbo Fengf43bf812017-12-15 18:27:22 -0800233 if (!ebpfSupported) {
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800234 return netdutils::status::ok;
Chenbo Fengf2759682017-10-10 17:31:57 -0700235 }
236
Chenbo Fengb2d213d2018-12-04 11:31:00 -0800237 int* status = nullptr;
238
239 std::vector<const char*> prog_args{
240 "/system/bin/bpfloader",
241 };
242
243 prog_args.push_back(nullptr);
244 int ret = android_fork_execvp(prog_args.size(), (char**) prog_args.data(), status, false, true);
245 if (ret) {
246 ret = errno;
247 ALOGE("failed to execute %s: %s", prog_args[0], strerror(errno));
248 return statusFromErrno(ret, "run bpf loader failed");
249 }
Bernie Innocenti6f9fd902018-10-11 20:50:23 +0900250 /* When netd restarts from a crash without total system reboot, the program
Chenbo Fengf2759682017-10-10 17:31:57 -0700251 * is still attached to the cgroup, detach it so the program can be freed
252 * and we can load and attach new program into the target cgroup.
253 *
254 * TODO: Scrape existing socket when run-time restart and clean up the map
255 * if the socket no longer exist
256 */
257
Chenbo Feng89c12f12018-03-21 10:29:18 -0700258 RETURN_IF_NOT_OK(initMaps());
Chenbo Feng7e974052018-02-28 22:57:21 -0800259
Chenbo Fengb2d213d2018-12-04 11:31:00 -0800260 RETURN_IF_NOT_OK(initPrograms());
261
Chenbo Feng7e974052018-02-28 22:57:21 -0800262 // Fetch the list of currently-existing interfaces. At this point NetlinkHandler is
263 // already running, so it will call addInterface() when any new interface appears.
264 std::map<std::string, uint32_t> ifacePairs;
265 ASSIGN_OR_RETURN(ifacePairs, InterfaceController::getIfaceList());
266 for (const auto& ifacePair:ifacePairs) {
267 addInterface(ifacePair.first.c_str(), ifacePair.second);
268 }
269
Chenbo Feng116d0552017-12-04 17:25:19 -0800270 auto result = makeSkDestroyListener();
271 if (!isOk(result)) {
272 ALOGE("Unable to create SkDestroyListener: %s", toString(result).c_str());
273 } else {
274 mSkDestroyListener = std::move(result.value());
275 }
276 // Rx handler extracts nfgenmsg looks up and invokes registered dispatch function.
277 const auto rxHandler = [this](const nlmsghdr&, const Slice msg) {
278 inet_diag_msg diagmsg = {};
279 if (extract(msg, diagmsg) < sizeof(inet_diag_msg)) {
Bernie Innocenti6f9fd902018-10-11 20:50:23 +0900280 ALOGE("Unrecognized netlink message: %s", toString(msg).c_str());
Chenbo Feng116d0552017-12-04 17:25:19 -0800281 return;
282 }
283 uint64_t sock_cookie = static_cast<uint64_t>(diagmsg.id.idiag_cookie[0]) |
284 (static_cast<uint64_t>(diagmsg.id.idiag_cookie[1]) << 32);
285
Bernie Innocenti6f9fd902018-10-11 20:50:23 +0900286 Status s = mCookieTagMap.deleteValue(sock_cookie);
Chenbo Feng1d4c5db2018-10-18 15:50:46 -0700287 if (!isOk(s) && s.code() != ENOENT) {
Bernie Innocenti6f9fd902018-10-11 20:50:23 +0900288 ALOGE("Failed to delete cookie %" PRIx64 ": %s", sock_cookie, toString(s).c_str());
289 return;
290 }
Chenbo Feng116d0552017-12-04 17:25:19 -0800291 };
292 expectOk(mSkDestroyListener->subscribe(kSockDiagMsgType, rxHandler));
293
294 // In case multiple netlink message comes in as a stream, we need to handle the rxDone message
295 // properly.
296 const auto rxDoneHandler = [](const nlmsghdr&, const Slice msg) {
297 // Ignore NLMSG_DONE messages
298 inet_diag_msg diagmsg = {};
299 extract(msg, diagmsg);
300 };
301 expectOk(mSkDestroyListener->subscribe(kSockDiagDoneMsgType, rxDoneHandler));
302
Chenbo Feng05393d82018-01-09 15:18:43 -0800303 return netdutils::status::ok;
Chenbo Fengf2759682017-10-10 17:31:57 -0700304}
305
Chenbo Fengf2759682017-10-10 17:31:57 -0700306int TrafficController::tagSocket(int sockFd, uint32_t tag, uid_t uid) {
Chenbo Fengbc6d4702018-04-11 18:27:19 -0700307 if (!ebpfSupported) {
308 if (legacy_tagSocket(sockFd, tag, uid)) return -errno;
309 return 0;
310 }
Chenbo Feng33cc1032017-10-23 15:16:37 -0700311
Chenbo Fengf2759682017-10-10 17:31:57 -0700312 uint64_t sock_cookie = getSocketCookie(sockFd);
Chenbo Fengef1cab32018-04-13 19:50:49 -0700313 if (sock_cookie == NONEXISTENT_COOKIE) return -errno;
Chenbo Fengf2759682017-10-10 17:31:57 -0700314 UidTag newKey = {.uid = (uint32_t)uid, .tag = tag};
315
316 // Update the tag information of a socket to the cookieUidMap. Use BPF_ANY
317 // flag so it will insert a new entry to the map if that value doesn't exist
318 // yet. And update the tag if there is already a tag stored. Since the eBPF
319 // program in kernel only read this map, and is protected by rcu read lock. It
320 // should be fine to cocurrently update the map while eBPF program is running.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700321 Status res = mCookieTagMap.writeValue(sock_cookie, newKey, BPF_ANY);
322 if (!isOk(res)) {
323 ALOGE("Failed to tag the socket: %s, fd: %d", strerror(res.code()),
324 mCookieTagMap.getMap().get());
Chenbo Fengf2759682017-10-10 17:31:57 -0700325 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700326 return -res.code();
Chenbo Fengf2759682017-10-10 17:31:57 -0700327}
328
329int TrafficController::untagSocket(int sockFd) {
Chenbo Fengbc6d4702018-04-11 18:27:19 -0700330 if (!ebpfSupported) {
331 if (legacy_untagSocket(sockFd)) return -errno;
332 return 0;
333 }
Chenbo Fengf2759682017-10-10 17:31:57 -0700334 uint64_t sock_cookie = getSocketCookie(sockFd);
335
Chenbo Fengef1cab32018-04-13 19:50:49 -0700336 if (sock_cookie == NONEXISTENT_COOKIE) return -errno;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700337 Status res = mCookieTagMap.deleteValue(sock_cookie);
338 if (!isOk(res)) {
339 ALOGE("Failed to untag socket: %s\n", strerror(res.code()));
Chenbo Fengf2759682017-10-10 17:31:57 -0700340 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700341 return -res.code();
Chenbo Fengf2759682017-10-10 17:31:57 -0700342}
343
344int TrafficController::setCounterSet(int counterSetNum, uid_t uid) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700345 if (counterSetNum < 0 || counterSetNum >= OVERFLOW_COUNTERSET) return -EINVAL;
346 Status res;
Chenbo Fengbc6d4702018-04-11 18:27:19 -0700347 if (!ebpfSupported) {
348 if (legacy_setCounterSet(counterSetNum, uid)) return -errno;
349 return 0;
350 }
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800351
352 // The default counter set for all uid is 0, so deleting the current counterset for that uid
353 // will automatically set it to 0.
Chenbo Fengf2759682017-10-10 17:31:57 -0700354 if (counterSetNum == 0) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700355 Status res = mUidCounterSetMap.deleteValue(uid);
356 if (isOk(res) || (!isOk(res) && res.code() == ENOENT)) {
Chenbo Fengf2759682017-10-10 17:31:57 -0700357 return 0;
358 } else {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700359 ALOGE("Failed to delete the counterSet: %s\n", strerror(res.code()));
360 return -res.code();
Chenbo Fengf2759682017-10-10 17:31:57 -0700361 }
362 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700363 uint8_t tmpCounterSetNum = (uint8_t)counterSetNum;
364 res = mUidCounterSetMap.writeValue(uid, tmpCounterSetNum, BPF_ANY);
365 if (!isOk(res)) {
366 ALOGE("Failed to set the counterSet: %s, fd: %d", strerror(res.code()),
367 mUidCounterSetMap.getMap().get());
368 return -res.code();
Chenbo Fengf2759682017-10-10 17:31:57 -0700369 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700370 return 0;
Chenbo Fengf2759682017-10-10 17:31:57 -0700371}
372
373int TrafficController::deleteTagData(uint32_t tag, uid_t uid) {
Chenbo Fengbc6d4702018-04-11 18:27:19 -0700374 if (!ebpfSupported) {
375 if (legacy_deleteTagData(tag, uid)) return -errno;
376 return 0;
377 }
Chenbo Feng33cc1032017-10-23 15:16:37 -0700378
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800379 // First we go through the cookieTagMap to delete the target uid tag combination. Or delete all
Chenbo Fengef1cab32018-04-13 19:50:49 -0700380 // the tags related to the uid if the tag is 0.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700381 const auto deleteMatchedCookieEntries = [uid, tag](const uint64_t& key, const UidTag& value,
382 BpfMap<uint64_t, UidTag>& map) {
383 if (value.uid == uid && (value.tag == tag || tag == 0)) {
384 Status res = map.deleteValue(key);
385 if (isOk(res) || (res.code() == ENOENT)) {
386 return netdutils::status::ok;
Chenbo Fengf2759682017-10-10 17:31:57 -0700387 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700388 ALOGE("Failed to delete data(cookie = %" PRIu64 "): %s\n", key, strerror(res.code()));
Chenbo Fengf2759682017-10-10 17:31:57 -0700389 }
Chenbo Fengef1cab32018-04-13 19:50:49 -0700390 // Move forward to next cookie in the map.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700391 return netdutils::status::ok;
Chenbo Fengef1cab32018-04-13 19:50:49 -0700392 };
Bernie Innocenti6f9fd902018-10-11 20:50:23 +0900393 mCookieTagMap.iterateWithValue(deleteMatchedCookieEntries).ignoreError();
Chenbo Fengf2759682017-10-10 17:31:57 -0700394 // 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 -0700395 // combination. Or all tag stats under that uid if the target tag is 0.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700396 const auto deleteMatchedUidTagEntries = [uid, tag](const StatsKey& key,
397 BpfMap<StatsKey, StatsValue>& map) {
398 if (key.uid == uid && (key.tag == tag || tag == 0)) {
399 Status res = map.deleteValue(key);
400 if (isOk(res) || (res.code() == ENOENT)) {
Chenbo Fengef1cab32018-04-13 19:50:49 -0700401 //Entry is deleted, use the current key to get a new nextKey;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700402 return netdutils::status::ok;
Chenbo Fengf2759682017-10-10 17:31:57 -0700403 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700404 ALOGE("Failed to delete data(uid=%u, tag=%u): %s\n", key.uid, key.tag,
405 strerror(res.code()));
Chenbo Fengf2759682017-10-10 17:31:57 -0700406 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700407 return netdutils::status::ok;
Chenbo Fengef1cab32018-04-13 19:50:49 -0700408 };
Bernie Innocenti6f9fd902018-10-11 20:50:23 +0900409 mTagStatsMap.iterate(deleteMatchedUidTagEntries).ignoreError();
Chenbo Fengf2759682017-10-10 17:31:57 -0700410 // 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 -0800411 // need to delete the stats stored in uidStatsMap and counterSet map.
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800412 if (tag != 0) return 0;
413
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700414 Status res = mUidCounterSetMap.deleteValue(uid);
415 if (!isOk(res) && res.code() != ENOENT) {
416 ALOGE("Failed to delete counterSet data(uid=%u, tag=%u): %s\n", uid, tag,
417 strerror(res.code()));
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800418 }
Bernie Innocenti6f9fd902018-10-11 20:50:23 +0900419 mUidStatsMap.iterate(deleteMatchedUidTagEntries).ignoreError();
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700420
421 auto deleteAppUidStatsEntry = [uid](const uint32_t& key, BpfMap<uint32_t, StatsValue>& map) {
422 if (key == uid) {
423 Status res = map.deleteValue(key);
424 if (isOk(res) || (res.code() == ENOENT)) {
425 return netdutils::status::ok;
426 }
427 ALOGE("Failed to delete data(uid=%u): %s", key, strerror(res.code()));
428 }
429 return netdutils::status::ok;
430 };
Bernie Innocenti6f9fd902018-10-11 20:50:23 +0900431 mAppUidStatsMap.iterate(deleteAppUidStatsEntry).ignoreError();
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700432 return 0;
Chenbo Fengf2759682017-10-10 17:31:57 -0700433}
434
Chenbo Feng7e974052018-02-28 22:57:21 -0800435int TrafficController::addInterface(const char* name, uint32_t ifaceIndex) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700436 if (!ebpfSupported) return 0;
Chenbo Feng7e974052018-02-28 22:57:21 -0800437
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700438 IfaceValue iface;
Chenbo Feng7e974052018-02-28 22:57:21 -0800439 if (ifaceIndex == 0) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700440 ALOGE("Unknown interface %s(%d)", name, ifaceIndex);
Chenbo Feng7e974052018-02-28 22:57:21 -0800441 return -1;
442 }
443
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700444 strlcpy(iface.name, name, sizeof(IfaceValue));
445 Status res = mIfaceIndexNameMap.writeValue(ifaceIndex, iface, BPF_ANY);
446 if (!isOk(res)) {
447 ALOGE("Failed to add iface %s(%d): %s", name, ifaceIndex, strerror(res.code()));
448 return -res.code();
Chenbo Feng7e974052018-02-28 22:57:21 -0800449 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700450 return 0;
Chenbo Feng7e974052018-02-28 22:57:21 -0800451}
452
Chenbo Feng703798e2018-06-15 17:07:59 -0700453Status TrafficController::updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule,
454 FirewallType type) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900455 std::lock_guard guard(mOwnerMatchMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700456 if ((rule == ALLOW && type == WHITELIST) || (rule == DENY && type == BLACKLIST)) {
Chenbo Feng703798e2018-06-15 17:07:59 -0700457 RETURN_IF_NOT_OK(addMatch(mUidOwnerMap, uid, match));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700458 } else if ((rule == ALLOW && type == BLACKLIST) || (rule == DENY && type == WHITELIST)) {
Chenbo Feng703798e2018-06-15 17:07:59 -0700459 RETURN_IF_NOT_OK(removeMatch(mUidOwnerMap, uid, match));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700460 } else {
461 //Cannot happen.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700462 return statusFromErrno(-EINVAL, "");
Chenbo Feng89c12f12018-03-21 10:29:18 -0700463 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700464 return netdutils::status::ok;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700465}
466
Chenbo Feng703798e2018-06-15 17:07:59 -0700467UidOwnerMatchType TrafficController::jumpOpToMatch(BandwidthController::IptJumpOp jumpHandling) {
Chenbo Feng95892f32018-06-07 14:52:02 -0700468 switch (jumpHandling) {
469 case BandwidthController::IptJumpReject:
Chenbo Feng703798e2018-06-15 17:07:59 -0700470 return PENALTY_BOX_MATCH;
Chenbo Feng95892f32018-06-07 14:52:02 -0700471 case BandwidthController::IptJumpReturn:
Chenbo Feng703798e2018-06-15 17:07:59 -0700472 return HAPPY_BOX_MATCH;
Chenbo Feng95892f32018-06-07 14:52:02 -0700473 case BandwidthController::IptJumpNoAdd:
474 return NO_MATCH;
475 }
476}
477
Chenbo Feng703798e2018-06-15 17:07:59 -0700478Status TrafficController::removeMatch(BpfMap<uint32_t, uint8_t>& map, uint32_t uid,
479 UidOwnerMatchType match) {
480 auto oldMatch = map.readValue(uid);
481 if (isOk(oldMatch)) {
482 uint8_t newMatch = oldMatch.value() & ~match;
483 if (newMatch == 0) {
484 RETURN_IF_NOT_OK(map.deleteValue(uid));
485 } else {
486 RETURN_IF_NOT_OK(map.writeValue(uid, newMatch, BPF_ANY));
487 }
488 } else {
489 return statusFromErrno(ENOENT, StringPrintf("uid: %u does not exist in map", uid));
490 }
491 return netdutils::status::ok;
492}
493
494Status TrafficController::addMatch(BpfMap<uint32_t, uint8_t>& map, uint32_t uid,
495 UidOwnerMatchType match) {
496 auto oldMatch = map.readValue(uid);
497 if (isOk(oldMatch)) {
498 uint8_t newMatch = oldMatch.value() | match;
Bernie Innocenti6f9fd902018-10-11 20:50:23 +0900499 RETURN_IF_NOT_OK(map.writeValue((uint32_t) uid, newMatch, BPF_ANY));
Chenbo Feng703798e2018-06-15 17:07:59 -0700500 } else {
501 RETURN_IF_NOT_OK(map.writeValue(uid, match, BPF_ANY));
502 }
503 return netdutils::status::ok;
504}
505
506Status TrafficController::updateUidOwnerMap(const std::vector<std::string>& appStrUids,
507 BandwidthController::IptJumpOp jumpHandling,
508 BandwidthController::IptOp op) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900509 std::lock_guard guard(mOwnerMatchMutex);
Chenbo Feng703798e2018-06-15 17:07:59 -0700510 UidOwnerMatchType match = jumpOpToMatch(jumpHandling);
511 if (match == NO_MATCH) {
512 return statusFromErrno(
513 EINVAL, StringPrintf("invalid IptJumpOp: %d, command: %d", jumpHandling, match));
Chenbo Feng95892f32018-06-07 14:52:02 -0700514 }
515 for (const auto& appStrUid : appStrUids) {
516 char* endPtr;
517 long uid = strtol(appStrUid.c_str(), &endPtr, 10);
518 if ((errno == ERANGE && (uid == LONG_MAX || uid == LONG_MIN)) ||
519 (endPtr == appStrUid.c_str()) || (*endPtr != '\0')) {
520 return statusFromErrno(errno, "invalid uid string:" + appStrUid);
521 }
522
Chenbo Feng95892f32018-06-07 14:52:02 -0700523 if (op == BandwidthController::IptOpDelete) {
Chenbo Feng703798e2018-06-15 17:07:59 -0700524 RETURN_IF_NOT_OK(removeMatch(mUidOwnerMap, uid, match));
Chenbo Feng95892f32018-06-07 14:52:02 -0700525 } else if (op == BandwidthController::IptOpInsert) {
Chenbo Feng703798e2018-06-15 17:07:59 -0700526 RETURN_IF_NOT_OK(addMatch(mUidOwnerMap, uid, match));
Chenbo Feng95892f32018-06-07 14:52:02 -0700527 } else {
528 // Cannot happen.
Chenbo Feng703798e2018-06-15 17:07:59 -0700529 return statusFromErrno(EINVAL, StringPrintf("invalid IptOp: %d, %d", op, match));
Chenbo Feng95892f32018-06-07 14:52:02 -0700530 }
531 }
532 return netdutils::status::ok;
533}
534
Chenbo Feng89c12f12018-03-21 10:29:18 -0700535int TrafficController::changeUidOwnerRule(ChildChain chain, uid_t uid, FirewallRule rule,
536 FirewallType type) {
Chenbo Feng89c12f12018-03-21 10:29:18 -0700537 if (!ebpfSupported) {
538 ALOGE("bpf is not set up, should use iptables rule");
539 return -ENOSYS;
540 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700541 Status res;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700542 switch (chain) {
543 case DOZABLE:
Chenbo Feng703798e2018-06-15 17:07:59 -0700544 res = updateOwnerMapEntry(DOZABLE_MATCH, uid, rule, type);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700545 break;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700546 case STANDBY:
Chenbo Feng703798e2018-06-15 17:07:59 -0700547 res = updateOwnerMapEntry(STANDBY_MATCH, uid, rule, type);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700548 break;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700549 case POWERSAVE:
Chenbo Feng703798e2018-06-15 17:07:59 -0700550 res = updateOwnerMapEntry(POWERSAVE_MATCH, uid, rule, type);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700551 break;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700552 case NONE:
553 default:
554 return -EINVAL;
555 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700556 if (!isOk(res)) {
557 ALOGE("change uid(%u) rule of %d failed: %s, rule: %d, type: %d", uid, chain,
558 res.msg().c_str(), rule, type);
559 return -res.code();
560 }
561 return 0;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700562}
563
Chenbo Feng703798e2018-06-15 17:07:59 -0700564Status TrafficController::replaceUidsInMap(const UidOwnerMatchType match,
565 const std::vector<int32_t>& uids) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900566 std::lock_guard guard(mOwnerMatchMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700567 std::set<int32_t> uidSet(uids.begin(), uids.end());
568 std::vector<uint32_t> uidsToDelete;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700569 auto getUidsToDelete = [&uidsToDelete, &uidSet](const uint32_t& key,
570 const BpfMap<uint32_t, uint8_t>&) {
Chenbo Feng703798e2018-06-15 17:07:59 -0700571 if (uidSet.find((int32_t) key) == uidSet.end()) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700572 uidsToDelete.push_back(key);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700573 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700574 return netdutils::status::ok;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700575 };
Chenbo Feng703798e2018-06-15 17:07:59 -0700576 RETURN_IF_NOT_OK(mUidOwnerMap.iterate(getUidsToDelete));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700577
578 for(auto uid : uidsToDelete) {
Chenbo Feng703798e2018-06-15 17:07:59 -0700579 RETURN_IF_NOT_OK(removeMatch(mUidOwnerMap, uid, match));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700580 }
581
582 for (auto uid : uids) {
Chenbo Feng703798e2018-06-15 17:07:59 -0700583 RETURN_IF_NOT_OK(addMatch(mUidOwnerMap, uid, match));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700584 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700585 return netdutils::status::ok;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700586}
587
588int TrafficController::replaceUidOwnerMap(const std::string& name, bool isWhitelist,
589 const std::vector<int32_t>& uids) {
Chenbo Feng89c12f12018-03-21 10:29:18 -0700590 FirewallRule rule;
591 FirewallType type;
592 if (isWhitelist) {
593 type = WHITELIST;
594 rule = ALLOW;
595 } else {
596 type = BLACKLIST;
597 rule = DENY;
598 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700599 Status res;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700600 if (!name.compare(FirewallController::LOCAL_DOZABLE)) {
Chenbo Feng703798e2018-06-15 17:07:59 -0700601 res = replaceUidsInMap(DOZABLE_MATCH, uids);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700602 } else if (!name.compare(FirewallController::LOCAL_STANDBY)) {
Chenbo Feng703798e2018-06-15 17:07:59 -0700603 res = replaceUidsInMap(STANDBY_MATCH, uids);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700604 } else if (!name.compare(FirewallController::LOCAL_POWERSAVE)) {
Chenbo Feng703798e2018-06-15 17:07:59 -0700605 res = replaceUidsInMap(POWERSAVE_MATCH, uids);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700606 } else {
607 ALOGE("unknown chain name: %s", name.c_str());
608 return -EINVAL;
609 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700610 if (!isOk(res)) {
611 ALOGE("Failed to clean up chain: %s: %s", name.c_str(), res.msg().c_str());
612 return -res.code();
Chenbo Feng89c12f12018-03-21 10:29:18 -0700613 }
614 return 0;
615}
616
617int TrafficController::toggleUidOwnerMap(ChildChain chain, bool enable) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900618 std::lock_guard guard(mOwnerMatchMutex);
Chenbo Feng703798e2018-06-15 17:07:59 -0700619 uint32_t key = CONFIGURATION_KEY;
620 auto oldConfiguration = mConfigurationMap.readValue(key);
621 if (!isOk(oldConfiguration)) {
622 ALOGE("Cannot read the old configuration from map: %s",
623 oldConfiguration.status().msg().c_str());
624 return -oldConfiguration.status().code();
625 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700626 Status res;
Chenbo Feng703798e2018-06-15 17:07:59 -0700627 BpfConfig newConfiguration;
628 uint8_t match;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700629 switch (chain) {
630 case DOZABLE:
Chenbo Feng703798e2018-06-15 17:07:59 -0700631 match = DOZABLE_MATCH;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700632 break;
633 case STANDBY:
Chenbo Feng703798e2018-06-15 17:07:59 -0700634 match = STANDBY_MATCH;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700635 break;
636 case POWERSAVE:
Chenbo Feng703798e2018-06-15 17:07:59 -0700637 match = POWERSAVE_MATCH;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700638 break;
639 default:
640 return -EINVAL;
641 }
Chenbo Feng703798e2018-06-15 17:07:59 -0700642 newConfiguration =
643 enable ? (oldConfiguration.value() | match) : (oldConfiguration.value() & (~match));
644 res = mConfigurationMap.writeValue(key, newConfiguration, BPF_EXIST);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700645 if (!isOk(res)) {
646 ALOGE("Failed to toggleUidOwnerMap(%d): %s", chain, res.msg().c_str());
Chenbo Feng89c12f12018-03-21 10:29:18 -0700647 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700648 return -res.code();
Chenbo Feng89c12f12018-03-21 10:29:18 -0700649}
650
Chenbo Feng07d43fe2017-12-21 14:38:51 -0800651bool TrafficController::checkBpfStatsEnable() {
652 return ebpfSupported;
653}
654
Chenbo Fengef297172018-03-26 10:53:33 -0700655std::string getProgramStatus(const char *path) {
656 int ret = access(path, R_OK);
657 if (ret == 0) {
658 return StringPrintf("OK");
659 }
660 if (ret != 0 && errno == ENOENT) {
661 return StringPrintf("program is missing at: %s", path);
662 }
663 return StringPrintf("check Program %s error: %s", path, strerror(errno));
664}
665
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700666std::string getMapStatus(const base::unique_fd& map_fd, const char* path) {
Chenbo Fengef297172018-03-26 10:53:33 -0700667 if (map_fd.get() < 0) {
668 return StringPrintf("map fd lost");
669 }
670 if (access(path, F_OK) != 0) {
671 return StringPrintf("map not pinned to location: %s", path);
672 }
673 return StringPrintf("OK");
674}
675
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900676void dumpBpfMap(const std::string& mapName, DumpWriter& dw, const std::string& header) {
Chenbo Fengef297172018-03-26 10:53:33 -0700677 dw.blankline();
678 dw.println("%s:", mapName.c_str());
Erik Klineb31fd692018-06-06 20:50:11 +0900679 if (!header.empty()) {
680 dw.println(header);
Chenbo Fengef297172018-03-26 10:53:33 -0700681 }
682}
683
684const String16 TrafficController::DUMP_KEYWORD = String16("trafficcontroller");
685
686void TrafficController::dump(DumpWriter& dw, bool verbose) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900687 std::lock_guard ownerMapGuard(mOwnerMatchMutex);
Erik Klineb31fd692018-06-06 20:50:11 +0900688 ScopedIndent indentTop(dw);
Chenbo Fengef297172018-03-26 10:53:33 -0700689 dw.println("TrafficController");
690
Erik Klineb31fd692018-06-06 20:50:11 +0900691 ScopedIndent indentPreBpfModule(dw);
Chenbo Fengef297172018-03-26 10:53:33 -0700692 dw.println("BPF module status: %s", ebpfSupported? "ON" : "OFF");
693
Erik Klineb31fd692018-06-06 20:50:11 +0900694 if (!ebpfSupported) {
Chenbo Fengef297172018-03-26 10:53:33 -0700695 return;
Erik Klineb31fd692018-06-06 20:50:11 +0900696 }
Chenbo Fengef297172018-03-26 10:53:33 -0700697
698 dw.blankline();
699 dw.println("mCookieTagMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700700 getMapStatus(mCookieTagMap.getMap(), COOKIE_TAG_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700701 dw.println("mUidCounterSetMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700702 getMapStatus(mUidCounterSetMap.getMap(), UID_COUNTERSET_MAP_PATH).c_str());
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700703 dw.println("mAppUidStatsMap status: %s",
704 getMapStatus(mAppUidStatsMap.getMap(), APP_UID_STATS_MAP_PATH).c_str());
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700705 dw.println("mUidStatsMap status: %s",
706 getMapStatus(mUidStatsMap.getMap(), UID_STATS_MAP_PATH).c_str());
707 dw.println("mTagStatsMap status: %s",
708 getMapStatus(mTagStatsMap.getMap(), TAG_STATS_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700709 dw.println("mIfaceIndexNameMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700710 getMapStatus(mIfaceIndexNameMap.getMap(), IFACE_INDEX_NAME_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700711 dw.println("mIfaceStatsMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700712 getMapStatus(mIfaceStatsMap.getMap(), IFACE_STATS_MAP_PATH).c_str());
Chenbo Feng703798e2018-06-15 17:07:59 -0700713 dw.println("mConfigurationMap status: %s",
714 getMapStatus(mConfigurationMap.getMap(), CONFIGURATION_MAP_PATH).c_str());
715 dw.println("mUidOwnerMap status: %s",
716 getMapStatus(mUidOwnerMap.getMap(), UID_OWNER_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700717
718 dw.blankline();
719 dw.println("Cgroup ingress program status: %s",
720 getProgramStatus(BPF_INGRESS_PROG_PATH).c_str());
721 dw.println("Cgroup egress program status: %s", getProgramStatus(BPF_EGRESS_PROG_PATH).c_str());
722 dw.println("xt_bpf ingress program status: %s",
723 getProgramStatus(XT_BPF_INGRESS_PROG_PATH).c_str());
724 dw.println("xt_bpf egress program status: %s",
725 getProgramStatus(XT_BPF_EGRESS_PROG_PATH).c_str());
Chenbo Feng95892f32018-06-07 14:52:02 -0700726 dw.println("xt_bpf bandwidth whitelist program status: %s",
727 getProgramStatus(XT_BPF_WHITELIST_PROG_PATH).c_str());
728 dw.println("xt_bpf bandwidth blacklist program status: %s",
729 getProgramStatus(XT_BPF_BLACKLIST_PROG_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700730
Erik Klineb31fd692018-06-06 20:50:11 +0900731 if (!verbose) {
732 return;
733 }
Chenbo Fengef297172018-03-26 10:53:33 -0700734
735 dw.blankline();
736 dw.println("BPF map content:");
737
Erik Klineb31fd692018-06-06 20:50:11 +0900738 ScopedIndent indentForMapContent(dw);
Chenbo Fengef297172018-03-26 10:53:33 -0700739
740 // Print CookieTagMap content.
741 dumpBpfMap("mCookieTagMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700742 const auto printCookieTagInfo = [&dw](const uint64_t& key, const UidTag& value,
743 const BpfMap<uint64_t, UidTag>&) {
744 dw.println("cookie=%" PRIu64 " tag=0x%x uid=%u", key, value.tag, value.uid);
745 return netdutils::status::ok;
Chenbo Fengef297172018-03-26 10:53:33 -0700746 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700747 Status res = mCookieTagMap.iterateWithValue(printCookieTagInfo);
748 if (!isOk(res)) {
749 dw.println("mCookieTagMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700750 }
751
752 // Print UidCounterSetMap Content
753 dumpBpfMap("mUidCounterSetMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700754 const auto printUidInfo = [&dw](const uint32_t& key, const uint8_t& value,
755 const BpfMap<uint32_t, uint8_t>&) {
756 dw.println("%u %u", key, value);
757 return netdutils::status::ok;
Chenbo Fengef297172018-03-26 10:53:33 -0700758 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700759 res = mUidCounterSetMap.iterateWithValue(printUidInfo);
760 if (!isOk(res)) {
761 dw.println("mUidCounterSetMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700762 }
763
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700764 // Print AppUidStatsMap content
765 std::string appUidStatsHeader = StringPrintf("uid rxBytes rxPackets txBytes txPackets");
766 dumpBpfMap("mAppUidStatsMap:", dw, appUidStatsHeader);
767 auto printAppUidStatsInfo = [&dw](const uint32_t& key, const StatsValue& value,
768 const BpfMap<uint32_t, StatsValue>&) {
769 dw.println("%u %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, key, value.rxBytes,
770 value.rxPackets, value.txBytes, value.txPackets);
771 return netdutils::status::ok;
772 };
773 res = mAppUidStatsMap.iterateWithValue(printAppUidStatsInfo);
774 if (!res.ok()) {
775 dw.println("mAppUidStatsMap print end with error: %s", res.msg().c_str());
776 }
777
Chenbo Fengef297172018-03-26 10:53:33 -0700778 // Print uidStatsMap content
779 std::string statsHeader = StringPrintf("ifaceIndex ifaceName tag_hex uid_int cnt_set rxBytes"
780 " rxPackets txBytes txPackets");
781 dumpBpfMap("mUidStatsMap", dw, statsHeader);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700782 const auto printStatsInfo = [&dw, this](const StatsKey& key, const StatsValue& value,
783 const BpfMap<StatsKey, StatsValue>&) {
784 uint32_t ifIndex = key.ifaceIndex;
785 auto ifname = mIfaceIndexNameMap.readValue(ifIndex);
786 if (!isOk(ifname)) {
787 strlcpy(ifname.value().name, "unknown", sizeof(IfaceValue));
Chenbo Fengef297172018-03-26 10:53:33 -0700788 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700789 dw.println("%u %s 0x%x %u %u %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, ifIndex,
790 ifname.value().name, key.tag, key.uid, key.counterSet, value.rxBytes,
791 value.rxPackets, value.txBytes, value.txPackets);
792 return netdutils::status::ok;
Chenbo Fengef297172018-03-26 10:53:33 -0700793 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700794 res = mUidStatsMap.iterateWithValue(printStatsInfo);
795 if (!isOk(res)) {
796 dw.println("mUidStatsMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700797 }
798
799 // Print TagStatsMap content.
800 dumpBpfMap("mTagStatsMap", dw, statsHeader);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700801 res = mTagStatsMap.iterateWithValue(printStatsInfo);
802 if (!isOk(res)) {
803 dw.println("mTagStatsMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700804 }
805
806 // Print ifaceIndexToNameMap content.
807 dumpBpfMap("mIfaceIndexNameMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700808 const auto printIfaceNameInfo = [&dw](const uint32_t& key, const IfaceValue& value,
809 const BpfMap<uint32_t, IfaceValue>&) {
810 const char* ifname = value.name;
811 dw.println("ifaceIndex=%u ifaceName=%s", key, ifname);
812 return netdutils::status::ok;
Chenbo Fengef297172018-03-26 10:53:33 -0700813 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700814 res = mIfaceIndexNameMap.iterateWithValue(printIfaceNameInfo);
815 if (!isOk(res)) {
816 dw.println("mIfaceIndexNameMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700817 }
818
819 // Print ifaceStatsMap content
820 std::string ifaceStatsHeader = StringPrintf("ifaceIndex ifaceName rxBytes rxPackets txBytes"
821 " txPackets");
822 dumpBpfMap("mIfaceStatsMap:", dw, ifaceStatsHeader);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700823 const auto printIfaceStatsInfo = [&dw, this](const uint32_t& key, const StatsValue& value,
824 const BpfMap<uint32_t, StatsValue>&) {
825 auto ifname = mIfaceIndexNameMap.readValue(key);
826 if (!isOk(ifname)) {
827 strlcpy(ifname.value().name, "unknown", sizeof(IfaceValue));
Chenbo Fengef297172018-03-26 10:53:33 -0700828 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700829 dw.println("%u %s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, key, ifname.value().name,
830 value.rxBytes, value.rxPackets, value.txBytes, value.txPackets);
831 return netdutils::status::ok;
Chenbo Fengef297172018-03-26 10:53:33 -0700832 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700833 res = mIfaceStatsMap.iterateWithValue(printIfaceStatsInfo);
834 if (!isOk(res)) {
835 dw.println("mIfaceStatsMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700836 }
837
Chenbo Feng703798e2018-06-15 17:07:59 -0700838 dw.blankline();
Chenbo Fengef297172018-03-26 10:53:33 -0700839
Chenbo Feng703798e2018-06-15 17:07:59 -0700840 uint32_t key = CONFIGURATION_KEY;
841 auto configuration = mConfigurationMap.readValue(key);
842 if (isOk(configuration)) {
843 dw.println("current configuration: %d", configuration.value());
844 } else {
845 dw.println("mConfigurationMap read with error: %s", configuration.status().msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700846 }
Chenbo Feng703798e2018-06-15 17:07:59 -0700847 dumpBpfMap("mUidOwnerMap", dw, "");
848 const auto printUidMatchInfo = [&dw](const uint32_t& key, const uint8_t& value,
849 const BpfMap<uint32_t, uint8_t>&) {
850 dw.println("%u %s", key, uidMatchTypeToString(value).c_str());
851 return netdutils::status::ok;
852 };
853 res = mUidOwnerMap.iterateWithValue(printUidMatchInfo);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700854 if (!isOk(res)) {
Chenbo Feng703798e2018-06-15 17:07:59 -0700855 dw.println("mUidOwnerMap print end with error: %s", res.msg().c_str());
Chenbo Feng95892f32018-06-07 14:52:02 -0700856 }
Chenbo Fengef297172018-03-26 10:53:33 -0700857}
858
Chenbo Fengf2759682017-10-10 17:31:57 -0700859} // namespace net
860} // namespace android