Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 1 | /* |
| 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 Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 17 | #define LOG_TAG "TrafficController" |
Chenbo Feng | 33cc103 | 2017-10-23 15:16:37 -0700 | [diff] [blame] | 18 | #include <inttypes.h> |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 19 | #include <linux/bpf.h> |
| 20 | #include <linux/if_ether.h> |
| 21 | #include <linux/in.h> |
| 22 | #include <linux/inet_diag.h> |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 23 | #include <linux/netlink.h> |
| 24 | #include <linux/sock_diag.h> |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 25 | #include <linux/unistd.h> |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 26 | #include <net/if.h> |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/socket.h> |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 30 | #include <sys/stat.h> |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 31 | #include <sys/types.h> |
Chenbo Feng | 33cc103 | 2017-10-23 15:16:37 -0700 | [diff] [blame] | 32 | #include <sys/utsname.h> |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 33 | #include <sys/wait.h> |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 34 | #include <mutex> |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 35 | #include <unordered_set> |
| 36 | #include <vector> |
| 37 | |
| 38 | #include <android-base/stringprintf.h> |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 39 | #include <android-base/strings.h> |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 40 | #include <android-base/unique_fd.h> |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 41 | #include <logwrap/logwrap.h> |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 42 | #include <netdutils/StatusOr.h> |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 43 | |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 44 | #include <netdutils/Misc.h> |
| 45 | #include <netdutils/Syscalls.h> |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 46 | #include "TrafficController.h" |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 47 | #include "bpf/BpfMap.h" |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 48 | |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 49 | #include "DumpWriter.h" |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 50 | #include "FirewallController.h" |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 51 | #include "InterfaceController.h" |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 52 | #include "NetlinkListener.h" |
Chenbo Feng | 33cc103 | 2017-10-23 15:16:37 -0700 | [diff] [blame] | 53 | #include "qtaguid/qtaguid.h" |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 54 | |
Bernie Innocenti | 7e25ec0 | 2018-07-02 19:32:17 +0900 | [diff] [blame] | 55 | using namespace android::bpf; // NOLINT(google-build-using-namespace): grandfathered |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 56 | |
| 57 | namespace android { |
| 58 | namespace net { |
| 59 | |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 60 | using base::StringPrintf; |
| 61 | using base::unique_fd; |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 62 | using base::Join; |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 63 | using netdutils::extract; |
| 64 | using netdutils::Slice; |
| 65 | using netdutils::sSyscalls; |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 66 | using netdutils::Status; |
| 67 | using netdutils::statusFromErrno; |
| 68 | using netdutils::StatusOr; |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 69 | using netdutils::status::ok; |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 70 | |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 71 | constexpr int kSockDiagMsgType = SOCK_DIAG_BY_FAMILY; |
| 72 | constexpr int kSockDiagDoneMsgType = NLMSG_DONE; |
| 73 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 74 | #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 | |
| 82 | const 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 Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 95 | StatusOr<std::unique_ptr<NetlinkListenerInterface>> makeSkDestroyListener() { |
| 96 | 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 | |
| 103 | sockaddr_nl addr = { |
| 104 | .nl_family = AF_NETLINK, |
| 105 | .nl_groups = 1 << (SKNLGRP_INET_TCP_DESTROY - 1) | 1 << (SKNLGRP_INET_UDP_DESTROY - 1) | |
| 106 | 1 << (SKNLGRP_INET6_TCP_DESTROY - 1) | 1 << (SKNLGRP_INET6_UDP_DESTROY - 1)}; |
| 107 | RETURN_IF_NOT_OK(sys.bind(sock, addr)); |
| 108 | |
| 109 | const sockaddr_nl kernel = {.nl_family = AF_NETLINK}; |
| 110 | RETURN_IF_NOT_OK(sys.connect(sock, kernel)); |
| 111 | |
| 112 | std::unique_ptr<NetlinkListenerInterface> listener = |
| 113 | std::make_unique<NetlinkListener>(std::move(event), std::move(sock)); |
| 114 | |
| 115 | return listener; |
| 116 | } |
| 117 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 118 | Status changeOwnerAndMode(const char* path, gid_t group, const char* debugName, bool netdOnly) { |
| 119 | int ret = chown(path, AID_ROOT, group); |
| 120 | if (ret != 0) return statusFromErrno(errno, StringPrintf("change %s group failed", debugName)); |
| 121 | |
| 122 | if (netdOnly) { |
| 123 | ret = chmod(path, S_IRWXU); |
| 124 | } else { |
| 125 | // Allow both netd and system server to obtain map fd from the path. |
| 126 | // chmod doesn't grant permission to all processes in that group to |
| 127 | // read/write the bpf map. They still need correct sepolicy to |
| 128 | // read/write the map. |
| 129 | ret = chmod(path, S_IRWXU | S_IRGRP); |
| 130 | } |
| 131 | if (ret != 0) return statusFromErrno(errno, StringPrintf("change %s mode failed", debugName)); |
| 132 | return netdutils::status::ok; |
| 133 | } |
| 134 | |
| 135 | TrafficController::TrafficController() { |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 136 | ebpfSupported = hasBpfSupport(); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 139 | Status initialOwnerMap(BpfMap<uint32_t, uint8_t>& map) { |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 140 | map.clear(); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 141 | uint32_t mapSettingKey = CONFIGURATION_KEY; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 142 | uint8_t defaultMapState = 0; |
| 143 | return map.writeValue(mapSettingKey, defaultMapState, BPF_NOEXIST); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | Status TrafficController::initMaps() { |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 147 | std::lock_guard<std::mutex> ownerMapGuard(mOwnerMatchMutex); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 148 | RETURN_IF_NOT_OK( |
| 149 | mCookieTagMap.getOrCreate(COOKIE_UID_MAP_SIZE, COOKIE_TAG_MAP_PATH, BPF_MAP_TYPE_HASH)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 150 | |
| 151 | RETURN_IF_NOT_OK(changeOwnerAndMode(COOKIE_TAG_MAP_PATH, AID_NET_BW_ACCT, "CookieTagMap", |
| 152 | false)); |
| 153 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 154 | RETURN_IF_NOT_OK(mUidCounterSetMap.getOrCreate(UID_COUNTERSET_MAP_SIZE, UID_COUNTERSET_MAP_PATH, |
| 155 | BPF_MAP_TYPE_HASH)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 156 | RETURN_IF_NOT_OK(changeOwnerAndMode(UID_COUNTERSET_MAP_PATH, AID_NET_BW_ACCT, |
| 157 | "UidCounterSetMap", false)); |
| 158 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 159 | RETURN_IF_NOT_OK( |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 160 | mAppUidStatsMap.getOrCreate(UID_STATS_MAP_SIZE, APP_UID_STATS_MAP_PATH, BPF_MAP_TYPE_HASH)); |
| 161 | RETURN_IF_NOT_OK( |
| 162 | changeOwnerAndMode(APP_UID_STATS_MAP_PATH, AID_NET_BW_STATS, "AppUidStatsMap", false)); |
| 163 | |
| 164 | RETURN_IF_NOT_OK( |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 165 | mUidStatsMap.getOrCreate(UID_STATS_MAP_SIZE, UID_STATS_MAP_PATH, BPF_MAP_TYPE_HASH)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 166 | RETURN_IF_NOT_OK(changeOwnerAndMode(UID_STATS_MAP_PATH, AID_NET_BW_STATS, "UidStatsMap", |
| 167 | false)); |
| 168 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 169 | RETURN_IF_NOT_OK( |
| 170 | mTagStatsMap.getOrCreate(TAG_STATS_MAP_SIZE, TAG_STATS_MAP_PATH, BPF_MAP_TYPE_HASH)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 171 | RETURN_IF_NOT_OK(changeOwnerAndMode(TAG_STATS_MAP_PATH, AID_NET_BW_STATS, "TagStatsMap", |
| 172 | false)); |
| 173 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 174 | RETURN_IF_NOT_OK(mIfaceIndexNameMap.getOrCreate(IFACE_INDEX_NAME_MAP_SIZE, |
| 175 | IFACE_INDEX_NAME_MAP_PATH, BPF_MAP_TYPE_HASH)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 176 | RETURN_IF_NOT_OK(changeOwnerAndMode(IFACE_INDEX_NAME_MAP_PATH, AID_NET_BW_STATS, |
| 177 | "IfaceIndexNameMap", false)); |
| 178 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 179 | RETURN_IF_NOT_OK( |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 180 | mIfaceStatsMap.getOrCreate(IFACE_STATS_MAP_SIZE, IFACE_STATS_MAP_PATH, BPF_MAP_TYPE_HASH)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 181 | RETURN_IF_NOT_OK(changeOwnerAndMode(IFACE_STATS_MAP_PATH, AID_NET_BW_STATS, "IfaceStatsMap", |
| 182 | false)); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 183 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 184 | RETURN_IF_NOT_OK(mConfigurationMap.getOrCreate(CONFIGURATION_MAP_SIZE, CONFIGURATION_MAP_PATH, |
| 185 | BPF_MAP_TYPE_HASH)); |
| 186 | RETURN_IF_NOT_OK( |
| 187 | changeOwnerAndMode(CONFIGURATION_MAP_PATH, AID_ROOT, "ConfigurationMap", true)); |
| 188 | uint32_t key = CONFIGURATION_KEY; |
| 189 | uint8_t initialConfiguration = DEFAULT_CONFIG; |
| 190 | RETURN_IF_NOT_OK(mConfigurationMap.writeValue(key, initialConfiguration, BPF_ANY)); |
| 191 | |
| 192 | RETURN_IF_NOT_OK( |
| 193 | mUidOwnerMap.getOrCreate(UID_OWNER_MAP_SIZE, UID_OWNER_MAP_PATH, BPF_MAP_TYPE_HASH)); |
| 194 | RETURN_IF_NOT_OK(changeOwnerAndMode(UID_OWNER_MAP_PATH, AID_ROOT, "UidOwnerMap", true)); |
| 195 | mUidOwnerMap.clear(); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 196 | return netdutils::status::ok; |
| 197 | } |
| 198 | |
| 199 | Status TrafficController::start() { |
| 200 | |
Chenbo Feng | f43bf81 | 2017-12-15 18:27:22 -0800 | [diff] [blame] | 201 | if (!ebpfSupported) { |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 202 | return netdutils::status::ok; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* When netd restart from a crash without total system reboot, the program |
| 206 | * is still attached to the cgroup, detach it so the program can be freed |
| 207 | * and we can load and attach new program into the target cgroup. |
| 208 | * |
| 209 | * TODO: Scrape existing socket when run-time restart and clean up the map |
| 210 | * if the socket no longer exist |
| 211 | */ |
| 212 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 213 | RETURN_IF_NOT_OK(initMaps()); |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 214 | |
| 215 | // Fetch the list of currently-existing interfaces. At this point NetlinkHandler is |
| 216 | // already running, so it will call addInterface() when any new interface appears. |
| 217 | std::map<std::string, uint32_t> ifacePairs; |
| 218 | ASSIGN_OR_RETURN(ifacePairs, InterfaceController::getIfaceList()); |
| 219 | for (const auto& ifacePair:ifacePairs) { |
| 220 | addInterface(ifacePair.first.c_str(), ifacePair.second); |
| 221 | } |
| 222 | |
Chenbo Feng | 5ed1799 | 2018-03-13 21:30:49 -0700 | [diff] [blame] | 223 | |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 224 | auto result = makeSkDestroyListener(); |
| 225 | if (!isOk(result)) { |
| 226 | ALOGE("Unable to create SkDestroyListener: %s", toString(result).c_str()); |
| 227 | } else { |
| 228 | mSkDestroyListener = std::move(result.value()); |
| 229 | } |
| 230 | // Rx handler extracts nfgenmsg looks up and invokes registered dispatch function. |
| 231 | const auto rxHandler = [this](const nlmsghdr&, const Slice msg) { |
| 232 | inet_diag_msg diagmsg = {}; |
| 233 | if (extract(msg, diagmsg) < sizeof(inet_diag_msg)) { |
| 234 | ALOGE("unrecognized netlink message: %s", toString(msg).c_str()); |
| 235 | return; |
| 236 | } |
| 237 | uint64_t sock_cookie = static_cast<uint64_t>(diagmsg.id.idiag_cookie[0]) | |
| 238 | (static_cast<uint64_t>(diagmsg.id.idiag_cookie[1]) << 32); |
| 239 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 240 | mCookieTagMap.deleteValue(sock_cookie); |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 241 | }; |
| 242 | expectOk(mSkDestroyListener->subscribe(kSockDiagMsgType, rxHandler)); |
| 243 | |
| 244 | // In case multiple netlink message comes in as a stream, we need to handle the rxDone message |
| 245 | // properly. |
| 246 | const auto rxDoneHandler = [](const nlmsghdr&, const Slice msg) { |
| 247 | // Ignore NLMSG_DONE messages |
| 248 | inet_diag_msg diagmsg = {}; |
| 249 | extract(msg, diagmsg); |
| 250 | }; |
| 251 | expectOk(mSkDestroyListener->subscribe(kSockDiagDoneMsgType, rxDoneHandler)); |
| 252 | |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 253 | int* status = nullptr; |
| 254 | |
| 255 | std::vector<const char*> prog_args{ |
| 256 | "/system/bin/bpfloader", |
| 257 | }; |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 258 | |
| 259 | prog_args.push_back(nullptr); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 260 | int ret = android_fork_execvp(prog_args.size(), (char**) prog_args.data(), status, false, true); |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 261 | if (ret) { |
| 262 | ret = errno; |
| 263 | ALOGE("failed to execute %s: %s", prog_args[0], strerror(errno)); |
| 264 | return statusFromErrno(ret, "run bpf loader failed"); |
| 265 | } |
| 266 | return netdutils::status::ok; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 269 | int TrafficController::tagSocket(int sockFd, uint32_t tag, uid_t uid) { |
Chenbo Feng | bc6d470 | 2018-04-11 18:27:19 -0700 | [diff] [blame] | 270 | if (!ebpfSupported) { |
| 271 | if (legacy_tagSocket(sockFd, tag, uid)) return -errno; |
| 272 | return 0; |
| 273 | } |
Chenbo Feng | 33cc103 | 2017-10-23 15:16:37 -0700 | [diff] [blame] | 274 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 275 | uint64_t sock_cookie = getSocketCookie(sockFd); |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 276 | if (sock_cookie == NONEXISTENT_COOKIE) return -errno; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 277 | UidTag newKey = {.uid = (uint32_t)uid, .tag = tag}; |
| 278 | |
| 279 | // Update the tag information of a socket to the cookieUidMap. Use BPF_ANY |
| 280 | // flag so it will insert a new entry to the map if that value doesn't exist |
| 281 | // yet. And update the tag if there is already a tag stored. Since the eBPF |
| 282 | // program in kernel only read this map, and is protected by rcu read lock. It |
| 283 | // should be fine to cocurrently update the map while eBPF program is running. |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 284 | Status res = mCookieTagMap.writeValue(sock_cookie, newKey, BPF_ANY); |
| 285 | if (!isOk(res)) { |
| 286 | ALOGE("Failed to tag the socket: %s, fd: %d", strerror(res.code()), |
| 287 | mCookieTagMap.getMap().get()); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 288 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 289 | return -res.code(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | int TrafficController::untagSocket(int sockFd) { |
Chenbo Feng | bc6d470 | 2018-04-11 18:27:19 -0700 | [diff] [blame] | 293 | if (!ebpfSupported) { |
| 294 | if (legacy_untagSocket(sockFd)) return -errno; |
| 295 | return 0; |
| 296 | } |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 297 | uint64_t sock_cookie = getSocketCookie(sockFd); |
| 298 | |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 299 | if (sock_cookie == NONEXISTENT_COOKIE) return -errno; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 300 | Status res = mCookieTagMap.deleteValue(sock_cookie); |
| 301 | if (!isOk(res)) { |
| 302 | ALOGE("Failed to untag socket: %s\n", strerror(res.code())); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 303 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 304 | return -res.code(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | int TrafficController::setCounterSet(int counterSetNum, uid_t uid) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 308 | if (counterSetNum < 0 || counterSetNum >= OVERFLOW_COUNTERSET) return -EINVAL; |
| 309 | Status res; |
Chenbo Feng | bc6d470 | 2018-04-11 18:27:19 -0700 | [diff] [blame] | 310 | if (!ebpfSupported) { |
| 311 | if (legacy_setCounterSet(counterSetNum, uid)) return -errno; |
| 312 | return 0; |
| 313 | } |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 314 | |
| 315 | // The default counter set for all uid is 0, so deleting the current counterset for that uid |
| 316 | // will automatically set it to 0. |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 317 | if (counterSetNum == 0) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 318 | Status res = mUidCounterSetMap.deleteValue(uid); |
| 319 | if (isOk(res) || (!isOk(res) && res.code() == ENOENT)) { |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 320 | return 0; |
| 321 | } else { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 322 | ALOGE("Failed to delete the counterSet: %s\n", strerror(res.code())); |
| 323 | return -res.code(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 326 | uint8_t tmpCounterSetNum = (uint8_t)counterSetNum; |
| 327 | res = mUidCounterSetMap.writeValue(uid, tmpCounterSetNum, BPF_ANY); |
| 328 | if (!isOk(res)) { |
| 329 | ALOGE("Failed to set the counterSet: %s, fd: %d", strerror(res.code()), |
| 330 | mUidCounterSetMap.getMap().get()); |
| 331 | return -res.code(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 332 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 333 | return 0; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | int TrafficController::deleteTagData(uint32_t tag, uid_t uid) { |
Chenbo Feng | 33cc103 | 2017-10-23 15:16:37 -0700 | [diff] [blame] | 337 | |
Chenbo Feng | bc6d470 | 2018-04-11 18:27:19 -0700 | [diff] [blame] | 338 | if (!ebpfSupported) { |
| 339 | if (legacy_deleteTagData(tag, uid)) return -errno; |
| 340 | return 0; |
| 341 | } |
Chenbo Feng | 33cc103 | 2017-10-23 15:16:37 -0700 | [diff] [blame] | 342 | |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 343 | // First we go through the cookieTagMap to delete the target uid tag combination. Or delete all |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 344 | // the tags related to the uid if the tag is 0. |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 345 | const auto deleteMatchedCookieEntries = [uid, tag](const uint64_t& key, const UidTag& value, |
| 346 | BpfMap<uint64_t, UidTag>& map) { |
| 347 | if (value.uid == uid && (value.tag == tag || tag == 0)) { |
| 348 | Status res = map.deleteValue(key); |
| 349 | if (isOk(res) || (res.code() == ENOENT)) { |
| 350 | return netdutils::status::ok; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 351 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 352 | ALOGE("Failed to delete data(cookie = %" PRIu64 "): %s\n", key, strerror(res.code())); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 353 | } |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 354 | // Move forward to next cookie in the map. |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 355 | return netdutils::status::ok; |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 356 | }; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 357 | mCookieTagMap.iterateWithValue(deleteMatchedCookieEntries); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 358 | // Now we go through the Tag stats map and delete the data entry with correct uid and tag |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 359 | // combination. Or all tag stats under that uid if the target tag is 0. |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 360 | const auto deleteMatchedUidTagEntries = [uid, tag](const StatsKey& key, |
| 361 | BpfMap<StatsKey, StatsValue>& map) { |
| 362 | if (key.uid == uid && (key.tag == tag || tag == 0)) { |
| 363 | Status res = map.deleteValue(key); |
| 364 | if (isOk(res) || (res.code() == ENOENT)) { |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 365 | //Entry is deleted, use the current key to get a new nextKey; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 366 | return netdutils::status::ok; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 367 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 368 | ALOGE("Failed to delete data(uid=%u, tag=%u): %s\n", key.uid, key.tag, |
| 369 | strerror(res.code())); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 370 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 371 | return netdutils::status::ok; |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 372 | }; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 373 | mTagStatsMap.iterate(deleteMatchedUidTagEntries); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 374 | // If the tag is not zero, we already deleted all the data entry required. If tag is 0, we also |
Chenbo Feng | ed37fea | 2017-12-13 19:35:01 -0800 | [diff] [blame] | 375 | // need to delete the stats stored in uidStatsMap and counterSet map. |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 376 | if (tag != 0) return 0; |
| 377 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 378 | Status res = mUidCounterSetMap.deleteValue(uid); |
| 379 | if (!isOk(res) && res.code() != ENOENT) { |
| 380 | ALOGE("Failed to delete counterSet data(uid=%u, tag=%u): %s\n", uid, tag, |
| 381 | strerror(res.code())); |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 382 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 383 | mUidStatsMap.iterate(deleteMatchedUidTagEntries); |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 384 | |
| 385 | auto deleteAppUidStatsEntry = [uid](const uint32_t& key, BpfMap<uint32_t, StatsValue>& map) { |
| 386 | if (key == uid) { |
| 387 | Status res = map.deleteValue(key); |
| 388 | if (isOk(res) || (res.code() == ENOENT)) { |
| 389 | return netdutils::status::ok; |
| 390 | } |
| 391 | ALOGE("Failed to delete data(uid=%u): %s", key, strerror(res.code())); |
| 392 | } |
| 393 | return netdutils::status::ok; |
| 394 | }; |
| 395 | mAppUidStatsMap.iterate(deleteAppUidStatsEntry); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 396 | return 0; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 399 | int TrafficController::addInterface(const char* name, uint32_t ifaceIndex) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 400 | if (!ebpfSupported) return 0; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 401 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 402 | IfaceValue iface; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 403 | if (ifaceIndex == 0) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 404 | ALOGE("Unknown interface %s(%d)", name, ifaceIndex); |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 405 | return -1; |
| 406 | } |
| 407 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 408 | strlcpy(iface.name, name, sizeof(IfaceValue)); |
| 409 | Status res = mIfaceIndexNameMap.writeValue(ifaceIndex, iface, BPF_ANY); |
| 410 | if (!isOk(res)) { |
| 411 | ALOGE("Failed to add iface %s(%d): %s", name, ifaceIndex, strerror(res.code())); |
| 412 | return -res.code(); |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 413 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 414 | return 0; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 417 | Status TrafficController::updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule, |
| 418 | FirewallType type) { |
| 419 | std::lock_guard<std::mutex> guard(mOwnerMatchMutex); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 420 | if ((rule == ALLOW && type == WHITELIST) || (rule == DENY && type == BLACKLIST)) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 421 | RETURN_IF_NOT_OK(addMatch(mUidOwnerMap, uid, match)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 422 | } else if ((rule == ALLOW && type == BLACKLIST) || (rule == DENY && type == WHITELIST)) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 423 | RETURN_IF_NOT_OK(removeMatch(mUidOwnerMap, uid, match)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 424 | } else { |
| 425 | //Cannot happen. |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 426 | return statusFromErrno(-EINVAL, ""); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 427 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 428 | return netdutils::status::ok; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 431 | UidOwnerMatchType TrafficController::jumpOpToMatch(BandwidthController::IptJumpOp jumpHandling) { |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 432 | switch (jumpHandling) { |
| 433 | case BandwidthController::IptJumpReject: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 434 | return PENALTY_BOX_MATCH; |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 435 | case BandwidthController::IptJumpReturn: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 436 | return HAPPY_BOX_MATCH; |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 437 | case BandwidthController::IptJumpNoAdd: |
| 438 | return NO_MATCH; |
| 439 | } |
| 440 | } |
| 441 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 442 | Status TrafficController::removeMatch(BpfMap<uint32_t, uint8_t>& map, uint32_t uid, |
| 443 | UidOwnerMatchType match) { |
| 444 | auto oldMatch = map.readValue(uid); |
| 445 | if (isOk(oldMatch)) { |
| 446 | uint8_t newMatch = oldMatch.value() & ~match; |
| 447 | if (newMatch == 0) { |
| 448 | RETURN_IF_NOT_OK(map.deleteValue(uid)); |
| 449 | } else { |
| 450 | RETURN_IF_NOT_OK(map.writeValue(uid, newMatch, BPF_ANY)); |
| 451 | } |
| 452 | } else { |
| 453 | return statusFromErrno(ENOENT, StringPrintf("uid: %u does not exist in map", uid)); |
| 454 | } |
| 455 | return netdutils::status::ok; |
| 456 | } |
| 457 | |
| 458 | Status TrafficController::addMatch(BpfMap<uint32_t, uint8_t>& map, uint32_t uid, |
| 459 | UidOwnerMatchType match) { |
| 460 | auto oldMatch = map.readValue(uid); |
| 461 | if (isOk(oldMatch)) { |
| 462 | uint8_t newMatch = oldMatch.value() | match; |
| 463 | map.writeValue((uint32_t) uid, newMatch, BPF_ANY); |
| 464 | } else { |
| 465 | RETURN_IF_NOT_OK(map.writeValue(uid, match, BPF_ANY)); |
| 466 | } |
| 467 | return netdutils::status::ok; |
| 468 | } |
| 469 | |
| 470 | Status TrafficController::updateUidOwnerMap(const std::vector<std::string>& appStrUids, |
| 471 | BandwidthController::IptJumpOp jumpHandling, |
| 472 | BandwidthController::IptOp op) { |
| 473 | std::lock_guard<std::mutex> guard(mOwnerMatchMutex); |
| 474 | UidOwnerMatchType match = jumpOpToMatch(jumpHandling); |
| 475 | if (match == NO_MATCH) { |
| 476 | return statusFromErrno( |
| 477 | EINVAL, StringPrintf("invalid IptJumpOp: %d, command: %d", jumpHandling, match)); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 478 | } |
| 479 | for (const auto& appStrUid : appStrUids) { |
| 480 | char* endPtr; |
| 481 | long uid = strtol(appStrUid.c_str(), &endPtr, 10); |
| 482 | if ((errno == ERANGE && (uid == LONG_MAX || uid == LONG_MIN)) || |
| 483 | (endPtr == appStrUid.c_str()) || (*endPtr != '\0')) { |
| 484 | return statusFromErrno(errno, "invalid uid string:" + appStrUid); |
| 485 | } |
| 486 | |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 487 | if (op == BandwidthController::IptOpDelete) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 488 | RETURN_IF_NOT_OK(removeMatch(mUidOwnerMap, uid, match)); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 489 | } else if (op == BandwidthController::IptOpInsert) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 490 | RETURN_IF_NOT_OK(addMatch(mUidOwnerMap, uid, match)); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 491 | } else { |
| 492 | // Cannot happen. |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 493 | return statusFromErrno(EINVAL, StringPrintf("invalid IptOp: %d, %d", op, match)); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | return netdutils::status::ok; |
| 497 | } |
| 498 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 499 | int TrafficController::changeUidOwnerRule(ChildChain chain, uid_t uid, FirewallRule rule, |
| 500 | FirewallType type) { |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 501 | if (!ebpfSupported) { |
| 502 | ALOGE("bpf is not set up, should use iptables rule"); |
| 503 | return -ENOSYS; |
| 504 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 505 | Status res; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 506 | switch (chain) { |
| 507 | case DOZABLE: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 508 | res = updateOwnerMapEntry(DOZABLE_MATCH, uid, rule, type); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 509 | break; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 510 | case STANDBY: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 511 | res = updateOwnerMapEntry(STANDBY_MATCH, uid, rule, type); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 512 | break; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 513 | case POWERSAVE: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 514 | res = updateOwnerMapEntry(POWERSAVE_MATCH, uid, rule, type); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 515 | break; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 516 | case NONE: |
| 517 | default: |
| 518 | return -EINVAL; |
| 519 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 520 | if (!isOk(res)) { |
| 521 | ALOGE("change uid(%u) rule of %d failed: %s, rule: %d, type: %d", uid, chain, |
| 522 | res.msg().c_str(), rule, type); |
| 523 | return -res.code(); |
| 524 | } |
| 525 | return 0; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 528 | Status TrafficController::replaceUidsInMap(const UidOwnerMatchType match, |
| 529 | const std::vector<int32_t>& uids) { |
| 530 | std::lock_guard<std::mutex> guard(mOwnerMatchMutex); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 531 | std::set<int32_t> uidSet(uids.begin(), uids.end()); |
| 532 | std::vector<uint32_t> uidsToDelete; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 533 | auto getUidsToDelete = [&uidsToDelete, &uidSet](const uint32_t& key, |
| 534 | const BpfMap<uint32_t, uint8_t>&) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 535 | if (uidSet.find((int32_t) key) == uidSet.end()) { |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 536 | uidsToDelete.push_back(key); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 537 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 538 | return netdutils::status::ok; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 539 | }; |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 540 | RETURN_IF_NOT_OK(mUidOwnerMap.iterate(getUidsToDelete)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 541 | |
| 542 | for(auto uid : uidsToDelete) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 543 | RETURN_IF_NOT_OK(removeMatch(mUidOwnerMap, uid, match)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | for (auto uid : uids) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 547 | RETURN_IF_NOT_OK(addMatch(mUidOwnerMap, uid, match)); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 548 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 549 | return netdutils::status::ok; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | int TrafficController::replaceUidOwnerMap(const std::string& name, bool isWhitelist, |
| 553 | const std::vector<int32_t>& uids) { |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 554 | FirewallRule rule; |
| 555 | FirewallType type; |
| 556 | if (isWhitelist) { |
| 557 | type = WHITELIST; |
| 558 | rule = ALLOW; |
| 559 | } else { |
| 560 | type = BLACKLIST; |
| 561 | rule = DENY; |
| 562 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 563 | Status res; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 564 | if (!name.compare(FirewallController::LOCAL_DOZABLE)) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 565 | res = replaceUidsInMap(DOZABLE_MATCH, uids); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 566 | } else if (!name.compare(FirewallController::LOCAL_STANDBY)) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 567 | res = replaceUidsInMap(STANDBY_MATCH, uids); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 568 | } else if (!name.compare(FirewallController::LOCAL_POWERSAVE)) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 569 | res = replaceUidsInMap(POWERSAVE_MATCH, uids); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 570 | } else { |
| 571 | ALOGE("unknown chain name: %s", name.c_str()); |
| 572 | return -EINVAL; |
| 573 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 574 | if (!isOk(res)) { |
| 575 | ALOGE("Failed to clean up chain: %s: %s", name.c_str(), res.msg().c_str()); |
| 576 | return -res.code(); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 577 | } |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | int TrafficController::toggleUidOwnerMap(ChildChain chain, bool enable) { |
| 582 | std::lock_guard<std::mutex> guard(mOwnerMatchMutex); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 583 | uint32_t key = CONFIGURATION_KEY; |
| 584 | auto oldConfiguration = mConfigurationMap.readValue(key); |
| 585 | if (!isOk(oldConfiguration)) { |
| 586 | ALOGE("Cannot read the old configuration from map: %s", |
| 587 | oldConfiguration.status().msg().c_str()); |
| 588 | return -oldConfiguration.status().code(); |
| 589 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 590 | Status res; |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 591 | BpfConfig newConfiguration; |
| 592 | uint8_t match; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 593 | switch (chain) { |
| 594 | case DOZABLE: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 595 | match = DOZABLE_MATCH; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 596 | break; |
| 597 | case STANDBY: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 598 | match = STANDBY_MATCH; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 599 | break; |
| 600 | case POWERSAVE: |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 601 | match = POWERSAVE_MATCH; |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 602 | break; |
| 603 | default: |
| 604 | return -EINVAL; |
| 605 | } |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 606 | newConfiguration = |
| 607 | enable ? (oldConfiguration.value() | match) : (oldConfiguration.value() & (~match)); |
| 608 | res = mConfigurationMap.writeValue(key, newConfiguration, BPF_EXIST); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 609 | if (!isOk(res)) { |
| 610 | ALOGE("Failed to toggleUidOwnerMap(%d): %s", chain, res.msg().c_str()); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 611 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 612 | return -res.code(); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Chenbo Feng | 07d43fe | 2017-12-21 14:38:51 -0800 | [diff] [blame] | 615 | bool TrafficController::checkBpfStatsEnable() { |
| 616 | return ebpfSupported; |
| 617 | } |
| 618 | |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 619 | std::string getProgramStatus(const char *path) { |
| 620 | int ret = access(path, R_OK); |
| 621 | if (ret == 0) { |
| 622 | return StringPrintf("OK"); |
| 623 | } |
| 624 | if (ret != 0 && errno == ENOENT) { |
| 625 | return StringPrintf("program is missing at: %s", path); |
| 626 | } |
| 627 | return StringPrintf("check Program %s error: %s", path, strerror(errno)); |
| 628 | } |
| 629 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 630 | std::string getMapStatus(const base::unique_fd& map_fd, const char* path) { |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 631 | if (map_fd.get() < 0) { |
| 632 | return StringPrintf("map fd lost"); |
| 633 | } |
| 634 | if (access(path, F_OK) != 0) { |
| 635 | return StringPrintf("map not pinned to location: %s", path); |
| 636 | } |
| 637 | return StringPrintf("OK"); |
| 638 | } |
| 639 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 640 | void dumpBpfMap(const std::string& mapName, DumpWriter& dw, const std::string& header) { |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 641 | dw.blankline(); |
| 642 | dw.println("%s:", mapName.c_str()); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 643 | if (!header.empty()) { |
| 644 | dw.println(header); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
| 648 | const String16 TrafficController::DUMP_KEYWORD = String16("trafficcontroller"); |
| 649 | |
| 650 | void TrafficController::dump(DumpWriter& dw, bool verbose) { |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 651 | std::lock_guard<std::mutex> ownerMapGuard(mOwnerMatchMutex); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 652 | ScopedIndent indentTop(dw); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 653 | dw.println("TrafficController"); |
| 654 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 655 | ScopedIndent indentPreBpfModule(dw); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 656 | dw.println("BPF module status: %s", ebpfSupported? "ON" : "OFF"); |
| 657 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 658 | if (!ebpfSupported) { |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 659 | return; |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 660 | } |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 661 | |
| 662 | dw.blankline(); |
| 663 | dw.println("mCookieTagMap status: %s", |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 664 | getMapStatus(mCookieTagMap.getMap(), COOKIE_TAG_MAP_PATH).c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 665 | dw.println("mUidCounterSetMap status: %s", |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 666 | getMapStatus(mUidCounterSetMap.getMap(), UID_COUNTERSET_MAP_PATH).c_str()); |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 667 | dw.println("mAppUidStatsMap status: %s", |
| 668 | getMapStatus(mAppUidStatsMap.getMap(), APP_UID_STATS_MAP_PATH).c_str()); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 669 | dw.println("mUidStatsMap status: %s", |
| 670 | getMapStatus(mUidStatsMap.getMap(), UID_STATS_MAP_PATH).c_str()); |
| 671 | dw.println("mTagStatsMap status: %s", |
| 672 | getMapStatus(mTagStatsMap.getMap(), TAG_STATS_MAP_PATH).c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 673 | dw.println("mIfaceIndexNameMap status: %s", |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 674 | getMapStatus(mIfaceIndexNameMap.getMap(), IFACE_INDEX_NAME_MAP_PATH).c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 675 | dw.println("mIfaceStatsMap status: %s", |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 676 | getMapStatus(mIfaceStatsMap.getMap(), IFACE_STATS_MAP_PATH).c_str()); |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 677 | dw.println("mConfigurationMap status: %s", |
| 678 | getMapStatus(mConfigurationMap.getMap(), CONFIGURATION_MAP_PATH).c_str()); |
| 679 | dw.println("mUidOwnerMap status: %s", |
| 680 | getMapStatus(mUidOwnerMap.getMap(), UID_OWNER_MAP_PATH).c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 681 | |
| 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 Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 690 | 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 Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 694 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 695 | if (!verbose) { |
| 696 | return; |
| 697 | } |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 698 | |
| 699 | dw.blankline(); |
| 700 | dw.println("BPF map content:"); |
| 701 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 702 | ScopedIndent indentForMapContent(dw); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 703 | |
| 704 | // Print CookieTagMap content. |
| 705 | dumpBpfMap("mCookieTagMap", dw, ""); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 706 | 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 Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 710 | }; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 711 | Status res = mCookieTagMap.iterateWithValue(printCookieTagInfo); |
| 712 | if (!isOk(res)) { |
| 713 | dw.println("mCookieTagMap print end with error: %s", res.msg().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | // Print UidCounterSetMap Content |
| 717 | dumpBpfMap("mUidCounterSetMap", dw, ""); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 718 | 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 Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 722 | }; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 723 | res = mUidCounterSetMap.iterateWithValue(printUidInfo); |
| 724 | if (!isOk(res)) { |
| 725 | dw.println("mUidCounterSetMap print end with error: %s", res.msg().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 728 | // 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 Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 742 | // 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 Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 746 | 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 Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 752 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 753 | 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 Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 757 | }; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 758 | res = mUidStatsMap.iterateWithValue(printStatsInfo); |
| 759 | if (!isOk(res)) { |
| 760 | dw.println("mUidStatsMap print end with error: %s", res.msg().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | // Print TagStatsMap content. |
| 764 | dumpBpfMap("mTagStatsMap", dw, statsHeader); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 765 | res = mTagStatsMap.iterateWithValue(printStatsInfo); |
| 766 | if (!isOk(res)) { |
| 767 | dw.println("mTagStatsMap print end with error: %s", res.msg().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | // Print ifaceIndexToNameMap content. |
| 771 | dumpBpfMap("mIfaceIndexNameMap", dw, ""); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 772 | 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 Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 777 | }; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 778 | res = mIfaceIndexNameMap.iterateWithValue(printIfaceNameInfo); |
| 779 | if (!isOk(res)) { |
| 780 | dw.println("mIfaceIndexNameMap print end with error: %s", res.msg().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | // Print ifaceStatsMap content |
| 784 | std::string ifaceStatsHeader = StringPrintf("ifaceIndex ifaceName rxBytes rxPackets txBytes" |
| 785 | " txPackets"); |
| 786 | dumpBpfMap("mIfaceStatsMap:", dw, ifaceStatsHeader); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 787 | 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 Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 792 | } |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 793 | 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 Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 796 | }; |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 797 | res = mIfaceStatsMap.iterateWithValue(printIfaceStatsInfo); |
| 798 | if (!isOk(res)) { |
| 799 | dw.println("mIfaceStatsMap print end with error: %s", res.msg().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 802 | dw.blankline(); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 803 | |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 804 | uint32_t key = CONFIGURATION_KEY; |
| 805 | auto configuration = mConfigurationMap.readValue(key); |
| 806 | if (isOk(configuration)) { |
| 807 | dw.println("current configuration: %d", configuration.value()); |
| 808 | } else { |
| 809 | dw.println("mConfigurationMap read with error: %s", configuration.status().msg().c_str()); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 810 | } |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 811 | dumpBpfMap("mUidOwnerMap", dw, ""); |
| 812 | const auto printUidMatchInfo = [&dw](const uint32_t& key, const uint8_t& value, |
| 813 | const BpfMap<uint32_t, uint8_t>&) { |
| 814 | dw.println("%u %s", key, uidMatchTypeToString(value).c_str()); |
| 815 | return netdutils::status::ok; |
| 816 | }; |
| 817 | res = mUidOwnerMap.iterateWithValue(printUidMatchInfo); |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 818 | if (!isOk(res)) { |
Chenbo Feng | 703798e | 2018-06-15 17:07:59 -0700 | [diff] [blame] | 819 | dw.println("mUidOwnerMap print end with error: %s", res.msg().c_str()); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 820 | } |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 823 | } // namespace net |
| 824 | } // namespace android |