blob: 4426e581e151f8f662f7856df9521c7b787cf562 [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
74StatusOr<std::unique_ptr<NetlinkListenerInterface>> makeSkDestroyListener() {
75 const auto& sys = sSyscalls.get();
76 ASSIGN_OR_RETURN(auto event, sys.eventfd(0, EFD_CLOEXEC));
77 const int domain = AF_NETLINK;
78 const int type = SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK;
79 const int protocol = NETLINK_INET_DIAG;
80 ASSIGN_OR_RETURN(auto sock, sys.socket(domain, type, protocol));
81
82 sockaddr_nl addr = {
83 .nl_family = AF_NETLINK,
84 .nl_groups = 1 << (SKNLGRP_INET_TCP_DESTROY - 1) | 1 << (SKNLGRP_INET_UDP_DESTROY - 1) |
85 1 << (SKNLGRP_INET6_TCP_DESTROY - 1) | 1 << (SKNLGRP_INET6_UDP_DESTROY - 1)};
86 RETURN_IF_NOT_OK(sys.bind(sock, addr));
87
88 const sockaddr_nl kernel = {.nl_family = AF_NETLINK};
89 RETURN_IF_NOT_OK(sys.connect(sock, kernel));
90
91 std::unique_ptr<NetlinkListenerInterface> listener =
92 std::make_unique<NetlinkListener>(std::move(event), std::move(sock));
93
94 return listener;
95}
96
Chenbo Feng89c12f12018-03-21 10:29:18 -070097Status changeOwnerAndMode(const char* path, gid_t group, const char* debugName, bool netdOnly) {
98 int ret = chown(path, AID_ROOT, group);
99 if (ret != 0) return statusFromErrno(errno, StringPrintf("change %s group failed", debugName));
100
101 if (netdOnly) {
102 ret = chmod(path, S_IRWXU);
103 } else {
104 // Allow both netd and system server to obtain map fd from the path.
105 // chmod doesn't grant permission to all processes in that group to
106 // read/write the bpf map. They still need correct sepolicy to
107 // read/write the map.
108 ret = chmod(path, S_IRWXU | S_IRGRP);
109 }
110 if (ret != 0) return statusFromErrno(errno, StringPrintf("change %s mode failed", debugName));
111 return netdutils::status::ok;
112}
113
114TrafficController::TrafficController() {
Chenbo Fengf43bf812017-12-15 18:27:22 -0800115 ebpfSupported = hasBpfSupport();
Chenbo Feng89c12f12018-03-21 10:29:18 -0700116}
117
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700118Status initialOwnerMap(BpfMap<uint32_t, uint8_t>& map) {
Chenbo Feng95892f32018-06-07 14:52:02 -0700119 map.clear();
Chenbo Feng89c12f12018-03-21 10:29:18 -0700120 uint32_t mapSettingKey = UID_MAP_ENABLED;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700121 uint8_t defaultMapState = 0;
122 return map.writeValue(mapSettingKey, defaultMapState, BPF_NOEXIST);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700123}
124
125Status TrafficController::initMaps() {
Chenbo Fengef1cab32018-04-13 19:50:49 -0700126 std::lock_guard<std::mutex> ownerMapGuard(mOwnerMatchMutex);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700127 RETURN_IF_NOT_OK(
128 mCookieTagMap.getOrCreate(COOKIE_UID_MAP_SIZE, COOKIE_TAG_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700129
130 RETURN_IF_NOT_OK(changeOwnerAndMode(COOKIE_TAG_MAP_PATH, AID_NET_BW_ACCT, "CookieTagMap",
131 false));
132
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700133 RETURN_IF_NOT_OK(mUidCounterSetMap.getOrCreate(UID_COUNTERSET_MAP_SIZE, UID_COUNTERSET_MAP_PATH,
134 BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700135 RETURN_IF_NOT_OK(changeOwnerAndMode(UID_COUNTERSET_MAP_PATH, AID_NET_BW_ACCT,
136 "UidCounterSetMap", false));
137
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700138 RETURN_IF_NOT_OK(
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700139 mAppUidStatsMap.getOrCreate(UID_STATS_MAP_SIZE, APP_UID_STATS_MAP_PATH, BPF_MAP_TYPE_HASH));
140 RETURN_IF_NOT_OK(
141 changeOwnerAndMode(APP_UID_STATS_MAP_PATH, AID_NET_BW_STATS, "AppUidStatsMap", false));
142
143 RETURN_IF_NOT_OK(
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700144 mUidStatsMap.getOrCreate(UID_STATS_MAP_SIZE, UID_STATS_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700145 RETURN_IF_NOT_OK(changeOwnerAndMode(UID_STATS_MAP_PATH, AID_NET_BW_STATS, "UidStatsMap",
146 false));
147
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700148 RETURN_IF_NOT_OK(
149 mTagStatsMap.getOrCreate(TAG_STATS_MAP_SIZE, TAG_STATS_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700150 RETURN_IF_NOT_OK(changeOwnerAndMode(TAG_STATS_MAP_PATH, AID_NET_BW_STATS, "TagStatsMap",
151 false));
152
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700153 RETURN_IF_NOT_OK(mIfaceIndexNameMap.getOrCreate(IFACE_INDEX_NAME_MAP_SIZE,
154 IFACE_INDEX_NAME_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700155 RETURN_IF_NOT_OK(changeOwnerAndMode(IFACE_INDEX_NAME_MAP_PATH, AID_NET_BW_STATS,
156 "IfaceIndexNameMap", false));
157
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700158 RETURN_IF_NOT_OK(
159 mDozableUidMap.getOrCreate(UID_OWNER_MAP_SIZE, DOZABLE_UID_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700160 RETURN_IF_NOT_OK(changeOwnerAndMode(DOZABLE_UID_MAP_PATH, AID_ROOT, "DozableUidMap", true));
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700161 RETURN_IF_NOT_OK(initialOwnerMap(mDozableUidMap));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700162
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700163 RETURN_IF_NOT_OK(
164 mStandbyUidMap.getOrCreate(UID_OWNER_MAP_SIZE, STANDBY_UID_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700165 RETURN_IF_NOT_OK(changeOwnerAndMode(STANDBY_UID_MAP_PATH, AID_ROOT, "StandbyUidMap", true));
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700166 RETURN_IF_NOT_OK(initialOwnerMap(mStandbyUidMap));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700167
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700168 RETURN_IF_NOT_OK(mPowerSaveUidMap.getOrCreate(UID_OWNER_MAP_SIZE, POWERSAVE_UID_MAP_PATH,
169 BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700170 RETURN_IF_NOT_OK(changeOwnerAndMode(POWERSAVE_UID_MAP_PATH, AID_ROOT, "PowerSaveUidMap", true));
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700171 RETURN_IF_NOT_OK(initialOwnerMap(mPowerSaveUidMap));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700172
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700173 RETURN_IF_NOT_OK(
174 mIfaceStatsMap.getOrCreate(IFACE_STATS_MAP_SIZE, IFACE_STATS_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700175 RETURN_IF_NOT_OK(changeOwnerAndMode(IFACE_STATS_MAP_PATH, AID_NET_BW_STATS, "IfaceStatsMap",
176 false));
Chenbo Feng95892f32018-06-07 14:52:02 -0700177
178 RETURN_IF_NOT_OK(mBandwidthUidMap.getOrCreate(UID_OWNER_MAP_SIZE, BANDWIDTH_UID_MAP_PATH,
179 BPF_MAP_TYPE_HASH));
180 RETURN_IF_NOT_OK(changeOwnerAndMode(BANDWIDTH_UID_MAP_PATH, AID_ROOT, "BandwidthUidMap", true));
181 mBandwidthUidMap.clear();
Chenbo Feng89c12f12018-03-21 10:29:18 -0700182 return netdutils::status::ok;
183}
184
185Status TrafficController::start() {
186
Chenbo Fengf43bf812017-12-15 18:27:22 -0800187 if (!ebpfSupported) {
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800188 return netdutils::status::ok;
Chenbo Fengf2759682017-10-10 17:31:57 -0700189 }
190
191 /* When netd restart from a crash without total system reboot, the program
192 * is still attached to the cgroup, detach it so the program can be freed
193 * and we can load and attach new program into the target cgroup.
194 *
195 * TODO: Scrape existing socket when run-time restart and clean up the map
196 * if the socket no longer exist
197 */
198
Chenbo Feng89c12f12018-03-21 10:29:18 -0700199 RETURN_IF_NOT_OK(initMaps());
Chenbo Feng7e974052018-02-28 22:57:21 -0800200
201 // Fetch the list of currently-existing interfaces. At this point NetlinkHandler is
202 // already running, so it will call addInterface() when any new interface appears.
203 std::map<std::string, uint32_t> ifacePairs;
204 ASSIGN_OR_RETURN(ifacePairs, InterfaceController::getIfaceList());
205 for (const auto& ifacePair:ifacePairs) {
206 addInterface(ifacePair.first.c_str(), ifacePair.second);
207 }
208
Chenbo Feng5ed17992018-03-13 21:30:49 -0700209
Chenbo Feng116d0552017-12-04 17:25:19 -0800210 auto result = makeSkDestroyListener();
211 if (!isOk(result)) {
212 ALOGE("Unable to create SkDestroyListener: %s", toString(result).c_str());
213 } else {
214 mSkDestroyListener = std::move(result.value());
215 }
216 // Rx handler extracts nfgenmsg looks up and invokes registered dispatch function.
217 const auto rxHandler = [this](const nlmsghdr&, const Slice msg) {
218 inet_diag_msg diagmsg = {};
219 if (extract(msg, diagmsg) < sizeof(inet_diag_msg)) {
220 ALOGE("unrecognized netlink message: %s", toString(msg).c_str());
221 return;
222 }
223 uint64_t sock_cookie = static_cast<uint64_t>(diagmsg.id.idiag_cookie[0]) |
224 (static_cast<uint64_t>(diagmsg.id.idiag_cookie[1]) << 32);
225
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700226 mCookieTagMap.deleteValue(sock_cookie);
Chenbo Feng116d0552017-12-04 17:25:19 -0800227 };
228 expectOk(mSkDestroyListener->subscribe(kSockDiagMsgType, rxHandler));
229
230 // In case multiple netlink message comes in as a stream, we need to handle the rxDone message
231 // properly.
232 const auto rxDoneHandler = [](const nlmsghdr&, const Slice msg) {
233 // Ignore NLMSG_DONE messages
234 inet_diag_msg diagmsg = {};
235 extract(msg, diagmsg);
236 };
237 expectOk(mSkDestroyListener->subscribe(kSockDiagDoneMsgType, rxDoneHandler));
238
Chenbo Feng05393d82018-01-09 15:18:43 -0800239 int* status = nullptr;
240
241 std::vector<const char*> prog_args{
242 "/system/bin/bpfloader",
243 };
Chenbo Feng89c12f12018-03-21 10:29:18 -0700244 int ret = access(BPF_INGRESS_PROG_PATH, R_OK);
Chenbo Feng05393d82018-01-09 15:18:43 -0800245 if (ret != 0 && errno == ENOENT) {
246 prog_args.push_back((char*)"-i");
247 }
248 ret = access(BPF_EGRESS_PROG_PATH, R_OK);
249 if (ret != 0 && errno == ENOENT) {
250 prog_args.push_back((char*)"-e");
251 }
Chenbo Feng5ed17992018-03-13 21:30:49 -0700252 ret = access(XT_BPF_INGRESS_PROG_PATH, R_OK);
253 if (ret != 0 && errno == ENOENT) {
254 prog_args.push_back((char*)"-p");
255 }
256 ret = access(XT_BPF_EGRESS_PROG_PATH, R_OK);
257 if (ret != 0 && errno == ENOENT) {
258 prog_args.push_back((char*)"-m");
259 }
Chenbo Feng95892f32018-06-07 14:52:02 -0700260 ret = access(XT_BPF_WHITELIST_PROG_PATH, R_OK);
261 if (ret != 0 && errno == ENOENT) {
262 prog_args.push_back((char*)"-w");
263 }
264 ret = access(XT_BPF_BLACKLIST_PROG_PATH, R_OK);
265 if (ret != 0 && errno == ENOENT) {
266 prog_args.push_back((char*)"-b");
267 }
Chenbo Feng05393d82018-01-09 15:18:43 -0800268
269 if (prog_args.size() == 1) {
Chenbo Feng5ed17992018-03-13 21:30:49 -0700270 // all program are loaded already.
Chenbo Feng05393d82018-01-09 15:18:43 -0800271 return netdutils::status::ok;
272 }
273
274 prog_args.push_back(nullptr);
275 ret = android_fork_execvp(prog_args.size(), (char**)prog_args.data(), status, false, true);
276 if (ret) {
277 ret = errno;
278 ALOGE("failed to execute %s: %s", prog_args[0], strerror(errno));
279 return statusFromErrno(ret, "run bpf loader failed");
280 }
281 return netdutils::status::ok;
Chenbo Fengf2759682017-10-10 17:31:57 -0700282}
283
Chenbo Fengf2759682017-10-10 17:31:57 -0700284int TrafficController::tagSocket(int sockFd, uint32_t tag, uid_t uid) {
Chenbo Fengbc6d4702018-04-11 18:27:19 -0700285 if (!ebpfSupported) {
286 if (legacy_tagSocket(sockFd, tag, uid)) return -errno;
287 return 0;
288 }
Chenbo Feng33cc1032017-10-23 15:16:37 -0700289
Chenbo Fengf2759682017-10-10 17:31:57 -0700290 uint64_t sock_cookie = getSocketCookie(sockFd);
Chenbo Fengef1cab32018-04-13 19:50:49 -0700291 if (sock_cookie == NONEXISTENT_COOKIE) return -errno;
Chenbo Fengf2759682017-10-10 17:31:57 -0700292 UidTag newKey = {.uid = (uint32_t)uid, .tag = tag};
293
294 // Update the tag information of a socket to the cookieUidMap. Use BPF_ANY
295 // flag so it will insert a new entry to the map if that value doesn't exist
296 // yet. And update the tag if there is already a tag stored. Since the eBPF
297 // program in kernel only read this map, and is protected by rcu read lock. It
298 // should be fine to cocurrently update the map while eBPF program is running.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700299 Status res = mCookieTagMap.writeValue(sock_cookie, newKey, BPF_ANY);
300 if (!isOk(res)) {
301 ALOGE("Failed to tag the socket: %s, fd: %d", strerror(res.code()),
302 mCookieTagMap.getMap().get());
Chenbo Fengf2759682017-10-10 17:31:57 -0700303 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700304 return -res.code();
Chenbo Fengf2759682017-10-10 17:31:57 -0700305}
306
307int TrafficController::untagSocket(int sockFd) {
Chenbo Fengbc6d4702018-04-11 18:27:19 -0700308 if (!ebpfSupported) {
309 if (legacy_untagSocket(sockFd)) return -errno;
310 return 0;
311 }
Chenbo Fengf2759682017-10-10 17:31:57 -0700312 uint64_t sock_cookie = getSocketCookie(sockFd);
313
Chenbo Fengef1cab32018-04-13 19:50:49 -0700314 if (sock_cookie == NONEXISTENT_COOKIE) return -errno;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700315 Status res = mCookieTagMap.deleteValue(sock_cookie);
316 if (!isOk(res)) {
317 ALOGE("Failed to untag socket: %s\n", strerror(res.code()));
Chenbo Fengf2759682017-10-10 17:31:57 -0700318 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700319 return -res.code();
Chenbo Fengf2759682017-10-10 17:31:57 -0700320}
321
322int TrafficController::setCounterSet(int counterSetNum, uid_t uid) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700323 if (counterSetNum < 0 || counterSetNum >= OVERFLOW_COUNTERSET) return -EINVAL;
324 Status res;
Chenbo Fengbc6d4702018-04-11 18:27:19 -0700325 if (!ebpfSupported) {
326 if (legacy_setCounterSet(counterSetNum, uid)) return -errno;
327 return 0;
328 }
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800329
330 // The default counter set for all uid is 0, so deleting the current counterset for that uid
331 // will automatically set it to 0.
Chenbo Fengf2759682017-10-10 17:31:57 -0700332 if (counterSetNum == 0) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700333 Status res = mUidCounterSetMap.deleteValue(uid);
334 if (isOk(res) || (!isOk(res) && res.code() == ENOENT)) {
Chenbo Fengf2759682017-10-10 17:31:57 -0700335 return 0;
336 } else {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700337 ALOGE("Failed to delete the counterSet: %s\n", strerror(res.code()));
338 return -res.code();
Chenbo Fengf2759682017-10-10 17:31:57 -0700339 }
340 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700341 uint8_t tmpCounterSetNum = (uint8_t)counterSetNum;
342 res = mUidCounterSetMap.writeValue(uid, tmpCounterSetNum, BPF_ANY);
343 if (!isOk(res)) {
344 ALOGE("Failed to set the counterSet: %s, fd: %d", strerror(res.code()),
345 mUidCounterSetMap.getMap().get());
346 return -res.code();
Chenbo Fengf2759682017-10-10 17:31:57 -0700347 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700348 return 0;
Chenbo Fengf2759682017-10-10 17:31:57 -0700349}
350
351int TrafficController::deleteTagData(uint32_t tag, uid_t uid) {
Chenbo Feng33cc1032017-10-23 15:16:37 -0700352
Chenbo Fengbc6d4702018-04-11 18:27:19 -0700353 if (!ebpfSupported) {
354 if (legacy_deleteTagData(tag, uid)) return -errno;
355 return 0;
356 }
Chenbo Feng33cc1032017-10-23 15:16:37 -0700357
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800358 // First we go through the cookieTagMap to delete the target uid tag combination. Or delete all
Chenbo Fengef1cab32018-04-13 19:50:49 -0700359 // the tags related to the uid if the tag is 0.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700360 const auto deleteMatchedCookieEntries = [uid, tag](const uint64_t& key, const UidTag& value,
361 BpfMap<uint64_t, UidTag>& map) {
362 if (value.uid == uid && (value.tag == tag || tag == 0)) {
363 Status res = map.deleteValue(key);
364 if (isOk(res) || (res.code() == ENOENT)) {
365 return netdutils::status::ok;
Chenbo Fengf2759682017-10-10 17:31:57 -0700366 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700367 ALOGE("Failed to delete data(cookie = %" PRIu64 "): %s\n", key, strerror(res.code()));
Chenbo Fengf2759682017-10-10 17:31:57 -0700368 }
Chenbo Fengef1cab32018-04-13 19:50:49 -0700369 // Move forward to next cookie in the map.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700370 return netdutils::status::ok;
Chenbo Fengef1cab32018-04-13 19:50:49 -0700371 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700372 mCookieTagMap.iterateWithValue(deleteMatchedCookieEntries);
Chenbo Fengf2759682017-10-10 17:31:57 -0700373 // 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 -0700374 // combination. Or all tag stats under that uid if the target tag is 0.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700375 const auto deleteMatchedUidTagEntries = [uid, tag](const StatsKey& key,
376 BpfMap<StatsKey, StatsValue>& map) {
377 if (key.uid == uid && (key.tag == tag || tag == 0)) {
378 Status res = map.deleteValue(key);
379 if (isOk(res) || (res.code() == ENOENT)) {
Chenbo Fengef1cab32018-04-13 19:50:49 -0700380 //Entry is deleted, use the current key to get a new nextKey;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700381 return netdutils::status::ok;
Chenbo Fengf2759682017-10-10 17:31:57 -0700382 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700383 ALOGE("Failed to delete data(uid=%u, tag=%u): %s\n", key.uid, key.tag,
384 strerror(res.code()));
Chenbo Fengf2759682017-10-10 17:31:57 -0700385 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700386 return netdutils::status::ok;
Chenbo Fengef1cab32018-04-13 19:50:49 -0700387 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700388 mTagStatsMap.iterate(deleteMatchedUidTagEntries);
Chenbo Fengf2759682017-10-10 17:31:57 -0700389 // 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 -0800390 // need to delete the stats stored in uidStatsMap and counterSet map.
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800391 if (tag != 0) return 0;
392
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700393 Status res = mUidCounterSetMap.deleteValue(uid);
394 if (!isOk(res) && res.code() != ENOENT) {
395 ALOGE("Failed to delete counterSet data(uid=%u, tag=%u): %s\n", uid, tag,
396 strerror(res.code()));
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800397 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700398 mUidStatsMap.iterate(deleteMatchedUidTagEntries);
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700399
400 auto deleteAppUidStatsEntry = [uid](const uint32_t& key, BpfMap<uint32_t, StatsValue>& map) {
401 if (key == uid) {
402 Status res = map.deleteValue(key);
403 if (isOk(res) || (res.code() == ENOENT)) {
404 return netdutils::status::ok;
405 }
406 ALOGE("Failed to delete data(uid=%u): %s", key, strerror(res.code()));
407 }
408 return netdutils::status::ok;
409 };
410 mAppUidStatsMap.iterate(deleteAppUidStatsEntry);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700411 return 0;
Chenbo Fengf2759682017-10-10 17:31:57 -0700412}
413
Chenbo Feng7e974052018-02-28 22:57:21 -0800414int TrafficController::addInterface(const char* name, uint32_t ifaceIndex) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700415 if (!ebpfSupported) return 0;
Chenbo Feng7e974052018-02-28 22:57:21 -0800416
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700417 IfaceValue iface;
Chenbo Feng7e974052018-02-28 22:57:21 -0800418 if (ifaceIndex == 0) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700419 ALOGE("Unknown interface %s(%d)", name, ifaceIndex);
Chenbo Feng7e974052018-02-28 22:57:21 -0800420 return -1;
421 }
422
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700423 strlcpy(iface.name, name, sizeof(IfaceValue));
424 Status res = mIfaceIndexNameMap.writeValue(ifaceIndex, iface, BPF_ANY);
425 if (!isOk(res)) {
426 ALOGE("Failed to add iface %s(%d): %s", name, ifaceIndex, strerror(res.code()));
427 return -res.code();
Chenbo Feng7e974052018-02-28 22:57:21 -0800428 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700429 return 0;
Chenbo Feng7e974052018-02-28 22:57:21 -0800430}
431
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700432Status TrafficController::updateOwnerMapEntry(BpfMap<uint32_t, uint8_t>& map, uid_t uid,
433 FirewallRule rule, FirewallType type) {
Chenbo Feng89c12f12018-03-21 10:29:18 -0700434 if (uid == UID_MAP_ENABLED) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700435 return statusFromErrno(-EINVAL, "This uid is reserved for map state");
Chenbo Feng89c12f12018-03-21 10:29:18 -0700436 }
437
438 if ((rule == ALLOW && type == WHITELIST) || (rule == DENY && type == BLACKLIST)) {
439 uint8_t flag = (type == WHITELIST) ? BPF_PASS : BPF_DROP;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700440 RETURN_IF_NOT_OK(map.writeValue(uid, flag, BPF_ANY));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700441 } else if ((rule == ALLOW && type == BLACKLIST) || (rule == DENY && type == WHITELIST)) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700442 RETURN_IF_NOT_OK(map.deleteValue(uid));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700443 } else {
444 //Cannot happen.
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700445 return statusFromErrno(-EINVAL, "");
Chenbo Feng89c12f12018-03-21 10:29:18 -0700446 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700447 return netdutils::status::ok;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700448}
449
Chenbo Feng95892f32018-06-07 14:52:02 -0700450BandwithMatchType TrafficController::jumpOpToMatch(BandwidthController::IptJumpOp jumpHandling) {
451 switch (jumpHandling) {
452 case BandwidthController::IptJumpReject:
453 return BLACKLISTMATCH;
454 case BandwidthController::IptJumpReturn:
455 return WHITELISTMATCH;
456 case BandwidthController::IptJumpNoAdd:
457 return NO_MATCH;
458 }
459}
460
461Status TrafficController::updateBandwidthUidMap(const std::vector<std::string>& appStrUids,
462 BandwidthController::IptJumpOp jumpHandling,
463 BandwidthController::IptOp op) {
464 uint8_t command = jumpOpToMatch(jumpHandling);
465 if (command == NO_MATCH) {
466 return statusFromErrno(EINVAL, StringPrintf("invalid IptJumpOp: %d, command: %d",
467 jumpHandling, command));
468 }
469 for (const auto& appStrUid : appStrUids) {
470 char* endPtr;
471 long uid = strtol(appStrUid.c_str(), &endPtr, 10);
472 if ((errno == ERANGE && (uid == LONG_MAX || uid == LONG_MIN)) ||
473 (endPtr == appStrUid.c_str()) || (*endPtr != '\0')) {
474 return statusFromErrno(errno, "invalid uid string:" + appStrUid);
475 }
476
477 auto match = mBandwidthUidMap.readValue(uid);
478 if (op == BandwidthController::IptOpDelete) {
479 if(!isOk(match)) {
480 return statusFromErrno(EINVAL, StringPrintf("uid(%ld): %s does not exist in map",
481 uid, appStrUid.c_str()));
482 }
483 uint8_t newValue = match.value() & ~command;
484 if (newValue == 0) {
485 RETURN_IF_NOT_OK(mBandwidthUidMap.deleteValue((uint32_t)uid));
486 } else {
487 RETURN_IF_NOT_OK(mBandwidthUidMap.writeValue((uint32_t)uid, newValue, BPF_ANY));
488 }
489 } else if (op == BandwidthController::IptOpInsert) {
490 uint8_t newValue = command;
491 if (isOk(match)) {
492 newValue |= match.value();
493 }
494 RETURN_IF_NOT_OK(mBandwidthUidMap.writeValue((uint32_t)uid, newValue, BPF_ANY));
495 } else {
496 // Cannot happen.
497 return statusFromErrno(EINVAL, StringPrintf("invalid IptOp: %d, %d", op, command));
498 }
499 }
500 return netdutils::status::ok;
501}
502
Chenbo Feng89c12f12018-03-21 10:29:18 -0700503int TrafficController::changeUidOwnerRule(ChildChain chain, uid_t uid, FirewallRule rule,
504 FirewallType type) {
505 std::lock_guard<std::mutex> guard(mOwnerMatchMutex);
506 if (!ebpfSupported) {
507 ALOGE("bpf is not set up, should use iptables rule");
508 return -ENOSYS;
509 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700510 Status res;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700511 switch (chain) {
512 case DOZABLE:
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700513 res = updateOwnerMapEntry(mDozableUidMap, uid, rule, type);
514 break;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700515 case STANDBY:
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700516 res = updateOwnerMapEntry(mStandbyUidMap, uid, rule, type);
517 break;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700518 case POWERSAVE:
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700519 res = updateOwnerMapEntry(mPowerSaveUidMap, uid, rule, type);
520 break;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700521 case NONE:
522 default:
523 return -EINVAL;
524 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700525 if (!isOk(res)) {
526 ALOGE("change uid(%u) rule of %d failed: %s, rule: %d, type: %d", uid, chain,
527 res.msg().c_str(), rule, type);
528 return -res.code();
529 }
530 return 0;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700531}
532
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700533Status TrafficController::replaceUidsInMap(BpfMap<uint32_t, uint8_t>& map,
534 const std::vector<int32_t>& uids, FirewallRule rule,
535 FirewallType type) {
Chenbo Feng89c12f12018-03-21 10:29:18 -0700536 std::set<int32_t> uidSet(uids.begin(), uids.end());
537 std::vector<uint32_t> uidsToDelete;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700538 auto getUidsToDelete = [&uidsToDelete, &uidSet](const uint32_t& key,
539 const BpfMap<uint32_t, uint8_t>&) {
540 if (key != UID_MAP_ENABLED && uidSet.find((int32_t)key) == uidSet.end()) {
541 uidsToDelete.push_back(key);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700542 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700543 return netdutils::status::ok;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700544 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700545 RETURN_IF_NOT_OK(map.iterate(getUidsToDelete));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700546
547 for(auto uid : uidsToDelete) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700548 RETURN_IF_NOT_OK(map.deleteValue(uid));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700549 }
550
551 for (auto uid : uids) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700552 RETURN_IF_NOT_OK(updateOwnerMapEntry(map, uid, rule, type));
Chenbo Feng89c12f12018-03-21 10:29:18 -0700553 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700554 return netdutils::status::ok;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700555}
556
557int TrafficController::replaceUidOwnerMap(const std::string& name, bool isWhitelist,
558 const std::vector<int32_t>& uids) {
559 std::lock_guard<std::mutex> guard(mOwnerMatchMutex);
560 FirewallRule rule;
561 FirewallType type;
562 if (isWhitelist) {
563 type = WHITELIST;
564 rule = ALLOW;
565 } else {
566 type = BLACKLIST;
567 rule = DENY;
568 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700569 Status res;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700570 if (!name.compare(FirewallController::LOCAL_DOZABLE)) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700571 res = replaceUidsInMap(mDozableUidMap, uids, rule, type);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700572 } else if (!name.compare(FirewallController::LOCAL_STANDBY)) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700573 res = replaceUidsInMap(mStandbyUidMap, uids, rule, type);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700574 } else if (!name.compare(FirewallController::LOCAL_POWERSAVE)) {
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700575 res = replaceUidsInMap(mPowerSaveUidMap, uids, rule, type);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700576 } else {
577 ALOGE("unknown chain name: %s", name.c_str());
578 return -EINVAL;
579 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700580 if (!isOk(res)) {
581 ALOGE("Failed to clean up chain: %s: %s", name.c_str(), res.msg().c_str());
582 return -res.code();
Chenbo Feng89c12f12018-03-21 10:29:18 -0700583 }
584 return 0;
585}
586
587int TrafficController::toggleUidOwnerMap(ChildChain chain, bool enable) {
588 std::lock_guard<std::mutex> guard(mOwnerMatchMutex);
589 uint32_t keyUid = UID_MAP_ENABLED;
590 uint8_t mapState = enable ? 1 : 0;
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700591 Status res;
Chenbo Feng89c12f12018-03-21 10:29:18 -0700592 switch (chain) {
593 case DOZABLE:
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700594 res = mDozableUidMap.writeValue(keyUid, mapState, BPF_EXIST);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700595 break;
596 case STANDBY:
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700597 res = mStandbyUidMap.writeValue(keyUid, mapState, BPF_EXIST);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700598 break;
599 case POWERSAVE:
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700600 res = mPowerSaveUidMap.writeValue(keyUid, mapState, BPF_EXIST);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700601 break;
602 default:
603 return -EINVAL;
604 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700605 if (!isOk(res)) {
606 ALOGE("Failed to toggleUidOwnerMap(%d): %s", chain, res.msg().c_str());
Chenbo Feng89c12f12018-03-21 10:29:18 -0700607 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700608 return -res.code();
Chenbo Feng89c12f12018-03-21 10:29:18 -0700609}
610
Chenbo Feng07d43fe2017-12-21 14:38:51 -0800611bool TrafficController::checkBpfStatsEnable() {
612 return ebpfSupported;
613}
614
Chenbo Fengef297172018-03-26 10:53:33 -0700615std::string getProgramStatus(const char *path) {
616 int ret = access(path, R_OK);
617 if (ret == 0) {
618 return StringPrintf("OK");
619 }
620 if (ret != 0 && errno == ENOENT) {
621 return StringPrintf("program is missing at: %s", path);
622 }
623 return StringPrintf("check Program %s error: %s", path, strerror(errno));
624}
625
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700626std::string getMapStatus(const base::unique_fd& map_fd, const char* path) {
Chenbo Fengef297172018-03-26 10:53:33 -0700627 if (map_fd.get() < 0) {
628 return StringPrintf("map fd lost");
629 }
630 if (access(path, F_OK) != 0) {
631 return StringPrintf("map not pinned to location: %s", path);
632 }
633 return StringPrintf("OK");
634}
635
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900636void dumpBpfMap(const std::string& mapName, DumpWriter& dw, const std::string& header) {
Chenbo Fengef297172018-03-26 10:53:33 -0700637 dw.blankline();
638 dw.println("%s:", mapName.c_str());
Erik Klineb31fd692018-06-06 20:50:11 +0900639 if (!header.empty()) {
640 dw.println(header);
Chenbo Fengef297172018-03-26 10:53:33 -0700641 }
642}
643
644const String16 TrafficController::DUMP_KEYWORD = String16("trafficcontroller");
645
646void TrafficController::dump(DumpWriter& dw, bool verbose) {
Chenbo Fengef1cab32018-04-13 19:50:49 -0700647 std::lock_guard<std::mutex> ownerMapGuard(mOwnerMatchMutex);
Erik Klineb31fd692018-06-06 20:50:11 +0900648 ScopedIndent indentTop(dw);
Chenbo Fengef297172018-03-26 10:53:33 -0700649 dw.println("TrafficController");
650
Erik Klineb31fd692018-06-06 20:50:11 +0900651 ScopedIndent indentPreBpfModule(dw);
Chenbo Fengef297172018-03-26 10:53:33 -0700652 dw.println("BPF module status: %s", ebpfSupported? "ON" : "OFF");
653
Erik Klineb31fd692018-06-06 20:50:11 +0900654 if (!ebpfSupported) {
Chenbo Fengef297172018-03-26 10:53:33 -0700655 return;
Erik Klineb31fd692018-06-06 20:50:11 +0900656 }
Chenbo Fengef297172018-03-26 10:53:33 -0700657
658 dw.blankline();
659 dw.println("mCookieTagMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700660 getMapStatus(mCookieTagMap.getMap(), COOKIE_TAG_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700661 dw.println("mUidCounterSetMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700662 getMapStatus(mUidCounterSetMap.getMap(), UID_COUNTERSET_MAP_PATH).c_str());
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700663 dw.println("mAppUidStatsMap status: %s",
664 getMapStatus(mAppUidStatsMap.getMap(), APP_UID_STATS_MAP_PATH).c_str());
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700665 dw.println("mUidStatsMap status: %s",
666 getMapStatus(mUidStatsMap.getMap(), UID_STATS_MAP_PATH).c_str());
667 dw.println("mTagStatsMap status: %s",
668 getMapStatus(mTagStatsMap.getMap(), TAG_STATS_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700669 dw.println("mIfaceIndexNameMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700670 getMapStatus(mIfaceIndexNameMap.getMap(), IFACE_INDEX_NAME_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700671 dw.println("mIfaceStatsMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700672 getMapStatus(mIfaceStatsMap.getMap(), IFACE_STATS_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700673 dw.println("mDozableUidMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700674 getMapStatus(mDozableUidMap.getMap(), DOZABLE_UID_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700675 dw.println("mStandbyUidMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700676 getMapStatus(mStandbyUidMap.getMap(), STANDBY_UID_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700677 dw.println("mPowerSaveUidMap status: %s",
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700678 getMapStatus(mPowerSaveUidMap.getMap(), POWERSAVE_UID_MAP_PATH).c_str());
Chenbo Feng95892f32018-06-07 14:52:02 -0700679 dw.println("mBandwidthUidMap status: %s",
680 getMapStatus(mBandwidthUidMap.getMap(), BANDWIDTH_UID_MAP_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700681
682 dw.blankline();
683 dw.println("Cgroup ingress program status: %s",
684 getProgramStatus(BPF_INGRESS_PROG_PATH).c_str());
685 dw.println("Cgroup egress program status: %s", getProgramStatus(BPF_EGRESS_PROG_PATH).c_str());
686 dw.println("xt_bpf ingress program status: %s",
687 getProgramStatus(XT_BPF_INGRESS_PROG_PATH).c_str());
688 dw.println("xt_bpf egress program status: %s",
689 getProgramStatus(XT_BPF_EGRESS_PROG_PATH).c_str());
Chenbo Feng95892f32018-06-07 14:52:02 -0700690 dw.println("xt_bpf bandwidth whitelist program status: %s",
691 getProgramStatus(XT_BPF_WHITELIST_PROG_PATH).c_str());
692 dw.println("xt_bpf bandwidth blacklist program status: %s",
693 getProgramStatus(XT_BPF_BLACKLIST_PROG_PATH).c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700694
Erik Klineb31fd692018-06-06 20:50:11 +0900695 if (!verbose) {
696 return;
697 }
Chenbo Fengef297172018-03-26 10:53:33 -0700698
699 dw.blankline();
700 dw.println("BPF map content:");
701
Erik Klineb31fd692018-06-06 20:50:11 +0900702 ScopedIndent indentForMapContent(dw);
Chenbo Fengef297172018-03-26 10:53:33 -0700703
704 // Print CookieTagMap content.
705 dumpBpfMap("mCookieTagMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700706 const auto printCookieTagInfo = [&dw](const uint64_t& key, const UidTag& value,
707 const BpfMap<uint64_t, UidTag>&) {
708 dw.println("cookie=%" PRIu64 " tag=0x%x uid=%u", key, value.tag, value.uid);
709 return netdutils::status::ok;
Chenbo Fengef297172018-03-26 10:53:33 -0700710 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700711 Status res = mCookieTagMap.iterateWithValue(printCookieTagInfo);
712 if (!isOk(res)) {
713 dw.println("mCookieTagMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700714 }
715
716 // Print UidCounterSetMap Content
717 dumpBpfMap("mUidCounterSetMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700718 const auto printUidInfo = [&dw](const uint32_t& key, const uint8_t& value,
719 const BpfMap<uint32_t, uint8_t>&) {
720 dw.println("%u %u", key, value);
721 return netdutils::status::ok;
Chenbo Fengef297172018-03-26 10:53:33 -0700722 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700723 res = mUidCounterSetMap.iterateWithValue(printUidInfo);
724 if (!isOk(res)) {
725 dw.println("mUidCounterSetMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700726 }
727
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700728 // Print AppUidStatsMap content
729 std::string appUidStatsHeader = StringPrintf("uid rxBytes rxPackets txBytes txPackets");
730 dumpBpfMap("mAppUidStatsMap:", dw, appUidStatsHeader);
731 auto printAppUidStatsInfo = [&dw](const uint32_t& key, const StatsValue& value,
732 const BpfMap<uint32_t, StatsValue>&) {
733 dw.println("%u %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, key, value.rxBytes,
734 value.rxPackets, value.txBytes, value.txPackets);
735 return netdutils::status::ok;
736 };
737 res = mAppUidStatsMap.iterateWithValue(printAppUidStatsInfo);
738 if (!res.ok()) {
739 dw.println("mAppUidStatsMap print end with error: %s", res.msg().c_str());
740 }
741
Chenbo Fengef297172018-03-26 10:53:33 -0700742 // Print uidStatsMap content
743 std::string statsHeader = StringPrintf("ifaceIndex ifaceName tag_hex uid_int cnt_set rxBytes"
744 " rxPackets txBytes txPackets");
745 dumpBpfMap("mUidStatsMap", dw, statsHeader);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700746 const auto printStatsInfo = [&dw, this](const StatsKey& key, const StatsValue& value,
747 const BpfMap<StatsKey, StatsValue>&) {
748 uint32_t ifIndex = key.ifaceIndex;
749 auto ifname = mIfaceIndexNameMap.readValue(ifIndex);
750 if (!isOk(ifname)) {
751 strlcpy(ifname.value().name, "unknown", sizeof(IfaceValue));
Chenbo Fengef297172018-03-26 10:53:33 -0700752 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700753 dw.println("%u %s 0x%x %u %u %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, ifIndex,
754 ifname.value().name, key.tag, key.uid, key.counterSet, value.rxBytes,
755 value.rxPackets, value.txBytes, value.txPackets);
756 return netdutils::status::ok;
Chenbo Fengef297172018-03-26 10:53:33 -0700757 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700758 res = mUidStatsMap.iterateWithValue(printStatsInfo);
759 if (!isOk(res)) {
760 dw.println("mUidStatsMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700761 }
762
763 // Print TagStatsMap content.
764 dumpBpfMap("mTagStatsMap", dw, statsHeader);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700765 res = mTagStatsMap.iterateWithValue(printStatsInfo);
766 if (!isOk(res)) {
767 dw.println("mTagStatsMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700768 }
769
770 // Print ifaceIndexToNameMap content.
771 dumpBpfMap("mIfaceIndexNameMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700772 const auto printIfaceNameInfo = [&dw](const uint32_t& key, const IfaceValue& value,
773 const BpfMap<uint32_t, IfaceValue>&) {
774 const char* ifname = value.name;
775 dw.println("ifaceIndex=%u ifaceName=%s", key, ifname);
776 return netdutils::status::ok;
Chenbo Fengef297172018-03-26 10:53:33 -0700777 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700778 res = mIfaceIndexNameMap.iterateWithValue(printIfaceNameInfo);
779 if (!isOk(res)) {
780 dw.println("mIfaceIndexNameMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700781 }
782
783 // Print ifaceStatsMap content
784 std::string ifaceStatsHeader = StringPrintf("ifaceIndex ifaceName rxBytes rxPackets txBytes"
785 " txPackets");
786 dumpBpfMap("mIfaceStatsMap:", dw, ifaceStatsHeader);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700787 const auto printIfaceStatsInfo = [&dw, this](const uint32_t& key, const StatsValue& value,
788 const BpfMap<uint32_t, StatsValue>&) {
789 auto ifname = mIfaceIndexNameMap.readValue(key);
790 if (!isOk(ifname)) {
791 strlcpy(ifname.value().name, "unknown", sizeof(IfaceValue));
Chenbo Fengef297172018-03-26 10:53:33 -0700792 }
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700793 dw.println("%u %s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, key, ifname.value().name,
794 value.rxBytes, value.rxPackets, value.txBytes, value.txPackets);
795 return netdutils::status::ok;
Chenbo Fengef297172018-03-26 10:53:33 -0700796 };
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700797 res = mIfaceStatsMap.iterateWithValue(printIfaceStatsInfo);
798 if (!isOk(res)) {
799 dw.println("mIfaceStatsMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700800 }
801
802 // Print owner match uid maps
Chenbo Fengef297172018-03-26 10:53:33 -0700803 dumpBpfMap("mDozableUidMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700804 res = mDozableUidMap.iterateWithValue(printUidInfo);
805 if (!isOk(res)) {
806 dw.println("mDozableUidMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700807 }
808
809 dumpBpfMap("mStandbyUidMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700810 res = mStandbyUidMap.iterateWithValue(printUidInfo);
811 if (!isOk(res)) {
812 dw.println("mDozableUidMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700813 }
814
815 dumpBpfMap("mPowerSaveUidMap", dw, "");
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700816 res = mPowerSaveUidMap.iterateWithValue(printUidInfo);
817 if (!isOk(res)) {
818 dw.println("mDozableUidMap print end with error: %s", res.msg().c_str());
Chenbo Fengef297172018-03-26 10:53:33 -0700819 }
820
Chenbo Feng95892f32018-06-07 14:52:02 -0700821 dumpBpfMap("mBandwidthUidMap", dw, "");
822 res = mBandwidthUidMap.iterateWithValue(printUidInfo);
823 if (!isOk(res)) {
824 dw.println("mBandwidthUidMap print end with error: %s", res.msg().c_str());
825 }
Chenbo Fengef297172018-03-26 10:53:33 -0700826}
827
Chenbo Fengf2759682017-10-10 17:31:57 -0700828} // namespace net
829} // namespace android