blob: f9bc9096e8e9e1e2aa948f2dadcb3b30f0f84c48 [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"
18
Chenbo Feng33cc1032017-10-23 15:16:37 -070019#include <inttypes.h>
Chenbo Fengf2759682017-10-10 17:31:57 -070020#include <linux/bpf.h>
21#include <linux/if_ether.h>
22#include <linux/in.h>
23#include <linux/inet_diag.h>
Chenbo Feng116d0552017-12-04 17:25:19 -080024#include <linux/netlink.h>
25#include <linux/sock_diag.h>
Chenbo Fengf2759682017-10-10 17:31:57 -070026#include <linux/unistd.h>
Chenbo Fengc10a8a42017-12-15 13:56:33 -080027#include <net/if.h>
Chenbo Fengf2759682017-10-10 17:31:57 -070028#include <stdlib.h>
29#include <string.h>
30#include <sys/socket.h>
Chenbo Fengc10a8a42017-12-15 13:56:33 -080031#include <sys/stat.h>
Chenbo Feng33cc1032017-10-23 15:16:37 -070032#include <sys/utsname.h>
Chenbo Fengc10a8a42017-12-15 13:56:33 -080033#include <unordered_set>
34#include <vector>
35
36#include <android-base/stringprintf.h>
37#include <android-base/unique_fd.h>
38#include <netdutils/StatusOr.h>
Chenbo Fengf2759682017-10-10 17:31:57 -070039
Chenbo Feng116d0552017-12-04 17:25:19 -080040#include <netdutils/Misc.h>
41#include <netdutils/Syscalls.h>
Chenbo Fengf2759682017-10-10 17:31:57 -070042#include "BpfProgSets.h"
Chenbo Fengf2759682017-10-10 17:31:57 -070043#include "TrafficController.h"
Chenbo Fengc10a8a42017-12-15 13:56:33 -080044#include "bpf/BpfUtils.h"
Chenbo Feng116d0552017-12-04 17:25:19 -080045
46#include "NetlinkListener.h"
Chenbo Feng33cc1032017-10-23 15:16:37 -070047#include "qtaguid/qtaguid.h"
Chenbo Fengf2759682017-10-10 17:31:57 -070048
Chenbo Fengc10a8a42017-12-15 13:56:33 -080049using namespace android::bpf;
Chenbo Fengf2759682017-10-10 17:31:57 -070050using namespace android::net::bpf_prog;
51
52namespace android {
53namespace net {
54
Chenbo Fengc10a8a42017-12-15 13:56:33 -080055using base::StringPrintf;
56using base::unique_fd;
Chenbo Feng116d0552017-12-04 17:25:19 -080057using netdutils::extract;
58using netdutils::Slice;
59using netdutils::sSyscalls;
Chenbo Fengc10a8a42017-12-15 13:56:33 -080060using netdutils::Status;
61using netdutils::statusFromErrno;
62using netdutils::StatusOr;
Chenbo Feng116d0552017-12-04 17:25:19 -080063using netdutils::status::ok;
Chenbo Fengc10a8a42017-12-15 13:56:33 -080064
65Status TrafficController::loadAndAttachProgram(bpf_attach_type type, const char* path,
66 const char* name, base::unique_fd& cg_fd) {
67 base::unique_fd fd;
68 int ret = access(path, R_OK);
69 if (ret == 0) {
70 // The program already exist and we can access it.
71 return netdutils::status::ok;
72 }
73
74 if (errno != ENOENT) {
75 // The program exist but we cannot access it.
76 return statusFromErrno(errno, StringPrintf("Cannot access %s at path: %s", name, path));
77 }
78
79 // Program does not exist yet. Load, attach and pin it.
80 if (type == BPF_CGROUP_INET_EGRESS) {
81 fd.reset(loadEgressProg(mCookieTagMap.get(), mUidStatsMap.get(), mTagStatsMap.get(),
82 mUidCounterSetMap.get()));
83 } else {
84 fd.reset(loadIngressProg(mCookieTagMap.get(), mUidStatsMap.get(), mTagStatsMap.get(),
85 mUidCounterSetMap.get()));
86 }
87 if (fd < 0) {
88 return statusFromErrno(errno, StringPrintf("load %s failed", name));
89 }
90
91 ret = attachProgram(type, fd, cg_fd);
92 if (ret) {
93 return statusFromErrno(errno, StringPrintf("%s attach failed", name));
94 }
95
96 ret = mapPin(fd, path);
97 if (ret) {
98 return statusFromErrno(errno, StringPrintf("Pin %s as file failed(%s)", name, path));
99 }
100 return netdutils::status::ok;
101}
102
Chenbo Feng116d0552017-12-04 17:25:19 -0800103constexpr int kSockDiagMsgType = SOCK_DIAG_BY_FAMILY;
104constexpr int kSockDiagDoneMsgType = NLMSG_DONE;
105
106StatusOr<std::unique_ptr<NetlinkListenerInterface>> makeSkDestroyListener() {
107 const auto& sys = sSyscalls.get();
108 ASSIGN_OR_RETURN(auto event, sys.eventfd(0, EFD_CLOEXEC));
109 const int domain = AF_NETLINK;
110 const int type = SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK;
111 const int protocol = NETLINK_INET_DIAG;
112 ASSIGN_OR_RETURN(auto sock, sys.socket(domain, type, protocol));
113
114 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 =
124 std::make_unique<NetlinkListener>(std::move(event), std::move(sock));
125
126 return listener;
127}
128
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800129Status TrafficController::start() {
Chenbo Fengf43bf812017-12-15 18:27:22 -0800130 ebpfSupported = hasBpfSupport();
131 if (!ebpfSupported) {
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800132 return netdutils::status::ok;
Chenbo Fengf2759682017-10-10 17:31:57 -0700133 }
134
135 /* When netd restart from a crash without total system reboot, the program
136 * is still attached to the cgroup, detach it so the program can be freed
137 * and we can load and attach new program into the target cgroup.
138 *
139 * TODO: Scrape existing socket when run-time restart and clean up the map
140 * if the socket no longer exist
141 */
142
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800143 ALOGI("START to load TrafficController");
144 base::unique_fd cg_fd(open(CGROUP_ROOT_PATH, O_DIRECTORY | O_RDONLY | O_CLOEXEC));
Chenbo Fengf2759682017-10-10 17:31:57 -0700145 if (cg_fd < 0) {
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800146 return statusFromErrno(errno, "Failed to open the cgroup directory");
Chenbo Fengf2759682017-10-10 17:31:57 -0700147 }
148
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800149 ASSIGN_OR_RETURN(mCookieTagMap,
150 setUpBPFMap(sizeof(uint64_t), sizeof(struct UidTag), COOKIE_UID_MAP_SIZE,
151 COOKIE_UID_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Fengf2759682017-10-10 17:31:57 -0700152
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800153 // Allow both netd and system server to obtain map fd from the path. Chown the group to
154 // net_bw_acct does not grant all process in that group the permission to access bpf maps. They
155 // still need correct sepolicy to read/write the map. And only system_server and netd have that
156 // permission for now.
Chenbo Fengf43bf812017-12-15 18:27:22 -0800157 int ret = chown(COOKIE_UID_MAP_PATH, AID_ROOT, AID_NET_BW_ACCT);
Chenbo Fengf2759682017-10-10 17:31:57 -0700158 if (ret) {
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800159 return statusFromErrno(errno, "change cookieTagMap group failed.");
160 }
161 ret = chmod(COOKIE_UID_MAP_PATH, S_IRWXU | S_IRGRP | S_IWGRP);
162 if (ret) {
163 return statusFromErrno(errno, "change cookieTagMap mode failed.");
Chenbo Fengf2759682017-10-10 17:31:57 -0700164 }
165
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800166 ASSIGN_OR_RETURN(mUidCounterSetMap,
167 setUpBPFMap(sizeof(uint32_t), sizeof(uint32_t), UID_COUNTERSET_MAP_SIZE,
168 UID_COUNTERSET_MAP_PATH, BPF_MAP_TYPE_HASH));
169 // Only netd can access the file.
170 ret = chmod(UID_COUNTERSET_MAP_PATH, S_IRWXU);
Chenbo Fengf2759682017-10-10 17:31:57 -0700171 if (ret) {
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800172 return statusFromErrno(errno, "change uidCounterSetMap mode failed.");
Chenbo Fengf2759682017-10-10 17:31:57 -0700173 }
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800174
175 ASSIGN_OR_RETURN(mUidStatsMap,
Chenbo Fengf43bf812017-12-15 18:27:22 -0800176 setUpBPFMap(sizeof(struct StatsKey), sizeof(struct StatsValue),
177 UID_STATS_MAP_SIZE, UID_STATS_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800178 // Change the file mode of pinned map so both netd and system server can get the map fd
179 // from it.
180 ret = chown(UID_STATS_MAP_PATH, AID_ROOT, AID_NET_BW_ACCT);
181 if (ret) {
182 return statusFromErrno(errno, "change uidStatsMap group failed.");
183 }
184 ret = chmod(UID_STATS_MAP_PATH, S_IRWXU | S_IRGRP | S_IWGRP);
185 if (ret) {
186 return statusFromErrno(errno, "change uidStatsMap mode failed.");
187 }
188
189 ASSIGN_OR_RETURN(mTagStatsMap,
Chenbo Fengf43bf812017-12-15 18:27:22 -0800190 setUpBPFMap(sizeof(struct StatsKey), sizeof(struct StatsValue),
191 TAG_STATS_MAP_SIZE, TAG_STATS_MAP_PATH, BPF_MAP_TYPE_HASH));
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800192 // Change the file mode of pinned map so both netd and system server can get the map fd
193 // from the path.
194 ret = chown(TAG_STATS_MAP_PATH, AID_ROOT, AID_NET_BW_STATS);
195 if (ret) {
196 return statusFromErrno(errno, "change tagStatsMap group failed.");
197 }
198 ret = chmod(TAG_STATS_MAP_PATH, S_IRWXU | S_IRGRP | S_IWGRP);
199 if (ret) {
200 return statusFromErrno(errno, "change tagStatsMap mode failed.");
201 }
202
Chenbo Feng116d0552017-12-04 17:25:19 -0800203 auto result = makeSkDestroyListener();
204 if (!isOk(result)) {
205 ALOGE("Unable to create SkDestroyListener: %s", toString(result).c_str());
206 } else {
207 mSkDestroyListener = std::move(result.value());
208 }
209 // Rx handler extracts nfgenmsg looks up and invokes registered dispatch function.
210 const auto rxHandler = [this](const nlmsghdr&, const Slice msg) {
211 inet_diag_msg diagmsg = {};
212 if (extract(msg, diagmsg) < sizeof(inet_diag_msg)) {
213 ALOGE("unrecognized netlink message: %s", toString(msg).c_str());
214 return;
215 }
216 uint64_t sock_cookie = static_cast<uint64_t>(diagmsg.id.idiag_cookie[0]) |
217 (static_cast<uint64_t>(diagmsg.id.idiag_cookie[1]) << 32);
218
219 deleteMapEntry(mCookieTagMap, &sock_cookie);
220 };
221 expectOk(mSkDestroyListener->subscribe(kSockDiagMsgType, rxHandler));
222
223 // In case multiple netlink message comes in as a stream, we need to handle the rxDone message
224 // properly.
225 const auto rxDoneHandler = [](const nlmsghdr&, const Slice msg) {
226 // Ignore NLMSG_DONE messages
227 inet_diag_msg diagmsg = {};
228 extract(msg, diagmsg);
229 };
230 expectOk(mSkDestroyListener->subscribe(kSockDiagDoneMsgType, rxDoneHandler));
231
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800232 RETURN_IF_NOT_OK(loadAndAttachProgram(BPF_CGROUP_INET_INGRESS, BPF_INGRESS_PROG_PATH,
233 "Ingress_prog", cg_fd));
234 return loadAndAttachProgram(BPF_CGROUP_INET_EGRESS, BPF_EGRESS_PROG_PATH, "egress_prog", cg_fd);
Chenbo Fengf2759682017-10-10 17:31:57 -0700235}
236
Chenbo Fengf2759682017-10-10 17:31:57 -0700237int TrafficController::tagSocket(int sockFd, uint32_t tag, uid_t uid) {
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800238 if (legacy_tagSocket(sockFd, tag, uid)) return -errno;
239 if (!ebpfSupported) return 0;
Chenbo Feng33cc1032017-10-23 15:16:37 -0700240
Chenbo Fengf2759682017-10-10 17:31:57 -0700241 uint64_t sock_cookie = getSocketCookie(sockFd);
242 if (sock_cookie == INET_DIAG_NOCOOKIE) return -errno;
243 UidTag newKey = {.uid = (uint32_t)uid, .tag = tag};
244
245 // Update the tag information of a socket to the cookieUidMap. Use BPF_ANY
246 // flag so it will insert a new entry to the map if that value doesn't exist
247 // yet. And update the tag if there is already a tag stored. Since the eBPF
248 // program in kernel only read this map, and is protected by rcu read lock. It
249 // should be fine to cocurrently update the map while eBPF program is running.
250 int res = writeToMapEntry(mCookieTagMap, &sock_cookie, &newKey, BPF_ANY);
251 if (res < 0) {
252 res = -errno;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800253 ALOGE("Failed to tag the socket: %s, fd: %d", strerror(errno), mCookieTagMap.get());
Chenbo Fengf2759682017-10-10 17:31:57 -0700254 }
255
256 return res;
257}
258
259int TrafficController::untagSocket(int sockFd) {
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800260 if (legacy_untagSocket(sockFd)) return -errno;
Chenbo Feng33cc1032017-10-23 15:16:37 -0700261 if (!ebpfSupported) return 0;
Chenbo Fengf2759682017-10-10 17:31:57 -0700262 uint64_t sock_cookie = getSocketCookie(sockFd);
263
264 if (sock_cookie == INET_DIAG_NOCOOKIE) return -errno;
265 int res = deleteMapEntry(mCookieTagMap, &sock_cookie);
266 if (res) {
267 res = -errno;
268 ALOGE("Failed to untag socket: %s\n", strerror(errno));
269 }
270 return res;
271}
272
273int TrafficController::setCounterSet(int counterSetNum, uid_t uid) {
274 if (counterSetNum < 0 || counterSetNum >= COUNTERSETS_LIMIT) return -EINVAL;
Chenbo Fengf2759682017-10-10 17:31:57 -0700275 int res;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800276 if (legacy_setCounterSet(counterSetNum, uid)) return -errno;
277 if (!ebpfSupported) return 0;
278
279 // The default counter set for all uid is 0, so deleting the current counterset for that uid
280 // will automatically set it to 0.
Chenbo Fengf2759682017-10-10 17:31:57 -0700281 if (counterSetNum == 0) {
282 res = deleteMapEntry(mUidCounterSetMap, &uid);
283 if (res == 0 || (res == -1 && errno == ENOENT)) {
284 return 0;
285 } else {
286 ALOGE("Failed to delete the counterSet: %s\n", strerror(errno));
287 return -errno;
288 }
289 }
290
291 res = writeToMapEntry(mUidCounterSetMap, &uid, &counterSetNum, BPF_ANY);
292 if (res < 0) {
293 res = -errno;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800294 ALOGE("Failed to set the counterSet: %s, fd: %d", strerror(errno), mUidCounterSetMap.get());
Chenbo Fengf2759682017-10-10 17:31:57 -0700295 }
296 return res;
297}
298
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800299// TODO: Add a lock for delete tag Data so when several request for different uid comes in, they do
300// not race with each other.
Chenbo Fengf2759682017-10-10 17:31:57 -0700301int TrafficController::deleteTagData(uint32_t tag, uid_t uid) {
302 int res = 0;
Chenbo Feng33cc1032017-10-23 15:16:37 -0700303
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800304 if (legacy_deleteTagData(tag, uid)) return -errno;
Chenbo Feng33cc1032017-10-23 15:16:37 -0700305 if (!ebpfSupported) return 0;
306
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800307 uint64_t curCookie = NONEXIST_COOKIE;
Chenbo Fengf2759682017-10-10 17:31:57 -0700308 uint64_t nextCookie = 0;
309 UidTag tmp_uidtag;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800310 std::vector<uint64_t> cookieList;
311 // First we go through the cookieTagMap to delete the target uid tag combination. Or delete all
312 // the tags related to the uid if the tag is 0, we start the map iteration with a cookie of
313 // INET_DIAG_NOCOOKIE because it's guaranteed that that will not be in the map.
314 while (getNextMapKey(mCookieTagMap, &curCookie, &nextCookie) != -1) {
315 res = findMapEntry(mCookieTagMap, &nextCookie, &tmp_uidtag);
Chenbo Fengf2759682017-10-10 17:31:57 -0700316 if (res < 0) {
317 res = -errno;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800318 ALOGE("Failed to get tag info(cookie = %" PRIu64 ": %s\n", nextCookie, strerror(errno));
319 // Continue to look for next entry.
320 curCookie = nextCookie;
321 continue;
Chenbo Fengf2759682017-10-10 17:31:57 -0700322 }
323
324 if (tmp_uidtag.uid == uid && (tmp_uidtag.tag == tag || tag == 0)) {
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800325 res = deleteMapEntry(mCookieTagMap, &nextCookie);
326 if (res < 0 && errno != ENOENT) {
Chenbo Fengf2759682017-10-10 17:31:57 -0700327 res = -errno;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800328 ALOGE("Failed to delete data(cookie = %" PRIu64 "): %s\n", nextCookie,
329 strerror(errno));
Chenbo Fengf2759682017-10-10 17:31:57 -0700330 }
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800331 } else {
332 // Move forward to next cookie in the map.
Chenbo Fengf2759682017-10-10 17:31:57 -0700333 curCookie = nextCookie;
334 }
335 }
336
337 // Now we go through the Tag stats map and delete the data entry with correct uid and tag
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800338 // combination. Or all tag stats under that uid if the target tag is 0. The initial key is
339 // set to the nonexist_statskey because it will never be in the map, and thus getNextMapKey will
340 // return 0 and set nextKey to the first key in the map.
Chenbo Fengf2759682017-10-10 17:31:57 -0700341 struct StatsKey curKey, nextKey;
Chenbo Fengf43bf812017-12-15 18:27:22 -0800342 curKey = android::bpf::NONEXISTENT_STATSKEY;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800343 while (getNextMapKey(mTagStatsMap, &curKey, &nextKey) != -1) {
344 if (nextKey.uid == uid && (nextKey.tag == tag || tag == 0)) {
345 res = deleteMapEntry(mTagStatsMap, &nextKey);
346 if (res < 0 && errno != ENOENT) {
347 // Skip the current entry if unexpected error happened.
348 ALOGE("Failed to delete data(uid=%u, tag=%u): %s\n", nextKey.uid, nextKey.tag,
349 strerror(errno));
350 curKey = nextKey;
Chenbo Fengf2759682017-10-10 17:31:57 -0700351 }
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800352 } else {
Chenbo Fengf2759682017-10-10 17:31:57 -0700353 curKey = nextKey;
354 }
355 }
356
357 // 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 -0800358 // need to delete the stats stored in uidStatsMap and counterSet map.
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800359 if (tag != 0) return 0;
360
361 res = deleteMapEntry(mUidCounterSetMap, &uid);
362 if (res < 0 && errno != ENOENT) {
363 ALOGE("Failed to delete counterSet data(uid=%u, tag=%u): %s\n", uid, tag, strerror(errno));
364 }
365
366 // For the uid stats deleted from the map, move them into a special
367 // removed uid entry. The removed uid is stored in uid 0, tag 0 and
368 // counterSet as COUNTERSETS_LIMIT.
369 StatsKey removedStatsKey = {0, 0, COUNTERSETS_LIMIT, 0};
Chenbo Fengf43bf812017-12-15 18:27:22 -0800370 StatsValue removedStatsTotal = {};
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800371 res = findMapEntry(mUidStatsMap, &removedStatsKey, &removedStatsTotal);
372 if (res < 0 && errno != ENOENT) {
373 ALOGE("Failed to get stats of removed uid: %s", strerror(errno));
374 }
375
Chenbo Fengf43bf812017-12-15 18:27:22 -0800376 curKey = android::bpf::NONEXISTENT_STATSKEY;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800377 while (getNextMapKey(mUidStatsMap, &curKey, &nextKey) != -1) {
378 if (nextKey.uid == uid) {
Chenbo Fengf43bf812017-12-15 18:27:22 -0800379 StatsValue old_stats = {};
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800380 res = findMapEntry(mUidStatsMap, &nextKey, &old_stats);
381 if (res < 0) {
382 if (errno != ENOENT) {
383 // if errno is ENOENT Somebody else deleted nextKey. Lookup the next key from
384 // curKey. If we have other error. Skip this key to avoid an infinite loop.
385 curKey = nextKey;
386 }
387 continue;
Chenbo Fengf2759682017-10-10 17:31:57 -0700388 }
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800389 res = deleteMapEntry(mUidStatsMap, &nextKey);
390 if (res < 0 && errno != ENOENT) {
391 ALOGE("Failed to delete data(uid=%u, tag=%u): %s\n", nextKey.uid, nextKey.tag,
392 strerror(errno));
393 curKey = nextKey;
394 continue;
395 }
396 removedStatsTotal.rxTcpPackets += old_stats.rxTcpPackets;
397 removedStatsTotal.rxTcpBytes += old_stats.rxTcpBytes;
398 removedStatsTotal.txTcpPackets += old_stats.txTcpPackets;
399 removedStatsTotal.txTcpBytes += old_stats.txTcpBytes;
400 removedStatsTotal.rxUdpPackets += old_stats.rxUdpPackets;
401 removedStatsTotal.rxUdpBytes += old_stats.rxUdpBytes;
402 removedStatsTotal.txUdpPackets += old_stats.txUdpPackets;
403 removedStatsTotal.txUdpBytes += old_stats.txUdpBytes;
404 removedStatsTotal.rxOtherPackets += old_stats.rxOtherPackets;
405 removedStatsTotal.rxOtherBytes += old_stats.rxOtherBytes;
406 removedStatsTotal.txOtherPackets += old_stats.txOtherPackets;
407 removedStatsTotal.txOtherBytes += old_stats.txOtherBytes;
408 } else {
Chenbo Fengf2759682017-10-10 17:31:57 -0700409 curKey = nextKey;
410 }
411 }
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800412
413 res = writeToMapEntry(mUidStatsMap, &removedStatsKey, &removedStatsTotal, BPF_ANY);
414 if (res) {
415 res = -errno;
416 ALOGE("Failed to add deleting stats to removed uid: %s", strerror(errno));
417 }
Chenbo Fengf2759682017-10-10 17:31:57 -0700418 return res;
419}
420
Chenbo Feng07d43fe2017-12-21 14:38:51 -0800421bool TrafficController::checkBpfStatsEnable() {
422 return ebpfSupported;
423}
424
Chenbo Fengf2759682017-10-10 17:31:57 -0700425} // namespace net
426} // namespace android