Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | |
| 17 | #define LOG_TAG "TcUtils" |
| 18 | |
| 19 | #include "tcutils/tcutils.h" |
| 20 | |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 21 | #include "logging.h" |
Patrick Rohr | 66452f5 | 2023-05-17 11:35:08 -0700 | [diff] [blame] | 22 | #include "bpf/KernelUtils.h" |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 23 | |
Patrick Rohr | 6b84b40 | 2024-05-16 12:06:10 -0700 | [diff] [blame] | 24 | #include <BpfSyscallWrappers.h> |
Patrick Rohr | 1575424 | 2024-05-16 11:38:57 -0700 | [diff] [blame] | 25 | #include <android-base/scopeguard.h> |
Patrick Rohr | 6b84b40 | 2024-05-16 12:06:10 -0700 | [diff] [blame] | 26 | #include <android-base/unique_fd.h> |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 27 | #include <arpa/inet.h> |
| 28 | #include <cerrno> |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 29 | #include <cstring> |
| 30 | #include <libgen.h> |
| 31 | #include <linux/if_arp.h> |
| 32 | #include <linux/if_ether.h> |
| 33 | #include <linux/netlink.h> |
| 34 | #include <linux/pkt_cls.h> |
| 35 | #include <linux/pkt_sched.h> |
| 36 | #include <linux/rtnetlink.h> |
Patrick Rohr | e815a74 | 2022-01-17 10:37:40 +0100 | [diff] [blame] | 37 | #include <linux/tc_act/tc_bpf.h> |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 38 | #include <net/if.h> |
Patrick Rohr | 0c34e9a0 | 2022-01-17 13:59:09 +0100 | [diff] [blame] | 39 | #include <stdio.h> |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 40 | #include <sys/socket.h> |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 41 | #include <unistd.h> |
| 42 | #include <utility> |
| 43 | |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 44 | // The maximum length of TCA_BPF_NAME. Sync from net/sched/cls_bpf.c. |
| 45 | #define CLS_BPF_NAME_LEN 256 |
| 46 | |
| 47 | // Classifier name. See cls_bpf_ops in net/sched/cls_bpf.c. |
| 48 | #define CLS_BPF_KIND_NAME "bpf" |
| 49 | |
| 50 | namespace android { |
| 51 | namespace { |
| 52 | |
Patrick Rohr | 6b84b40 | 2024-05-16 12:06:10 -0700 | [diff] [blame] | 53 | using base::make_scope_guard; |
| 54 | using base::unique_fd; |
| 55 | |
Patrick Rohr | e815a74 | 2022-01-17 10:37:40 +0100 | [diff] [blame] | 56 | /** |
| 57 | * IngressPoliceFilterBuilder builds a nlmsg request equivalent to the following |
| 58 | * tc command: |
| 59 | * |
| 60 | * tc filter add dev .. ingress prio .. protocol .. matchall \ |
| 61 | * action police rate .. burst .. conform-exceed pipe/continue \ |
| 62 | * action bpf object-pinned .. \ |
| 63 | * drop |
| 64 | */ |
| 65 | class IngressPoliceFilterBuilder final { |
| 66 | // default mtu is 2047, so the cell logarithm factor (cell_log) is 3. |
| 67 | // 0x7FF >> 0x3FF x 2^1 >> 0x1FF x 2^2 >> 0xFF x 2^3 |
| 68 | static constexpr int RTAB_CELL_LOGARITHM = 3; |
| 69 | static constexpr size_t RTAB_SIZE = 256; |
| 70 | static constexpr unsigned TIME_UNITS_PER_SEC = 1000000; |
| 71 | |
| 72 | struct Request { |
| 73 | nlmsghdr n; |
| 74 | tcmsg t; |
| 75 | struct { |
| 76 | nlattr attr; |
| 77 | char str[NLMSG_ALIGN(sizeof("matchall"))]; |
| 78 | } kind; |
| 79 | struct { |
| 80 | nlattr attr; |
| 81 | struct { |
| 82 | nlattr attr; |
| 83 | struct { |
| 84 | nlattr attr; |
| 85 | struct { |
| 86 | nlattr attr; |
| 87 | char str[NLMSG_ALIGN(sizeof("police"))]; |
| 88 | } kind; |
| 89 | struct { |
| 90 | nlattr attr; |
| 91 | struct { |
| 92 | nlattr attr; |
| 93 | struct tc_police obj; |
| 94 | } police; |
| 95 | struct { |
| 96 | nlattr attr; |
| 97 | uint32_t u32[RTAB_SIZE]; |
| 98 | } rtab; |
| 99 | struct { |
| 100 | nlattr attr; |
| 101 | int32_t s32; |
| 102 | } notexceedact; |
| 103 | } opt; |
| 104 | } act1; |
| 105 | struct { |
| 106 | nlattr attr; |
| 107 | struct { |
| 108 | nlattr attr; |
| 109 | char str[NLMSG_ALIGN(sizeof("bpf"))]; |
| 110 | } kind; |
| 111 | struct { |
| 112 | nlattr attr; |
| 113 | struct { |
| 114 | nlattr attr; |
| 115 | uint32_t u32; |
| 116 | } fd; |
| 117 | struct { |
| 118 | nlattr attr; |
| 119 | char str[NLMSG_ALIGN(CLS_BPF_NAME_LEN)]; |
| 120 | } name; |
| 121 | struct { |
| 122 | nlattr attr; |
| 123 | struct tc_act_bpf obj; |
| 124 | } parms; |
| 125 | } opt; |
| 126 | } act2; |
| 127 | } acts; |
| 128 | } opt; |
| 129 | }; |
| 130 | |
| 131 | // class members |
| 132 | const unsigned mBurstInBytes; |
| 133 | const char *mBpfProgPath; |
Patrick Rohr | 6b84b40 | 2024-05-16 12:06:10 -0700 | [diff] [blame] | 134 | unique_fd mBpfFd; |
Patrick Rohr | e815a74 | 2022-01-17 10:37:40 +0100 | [diff] [blame] | 135 | Request mRequest; |
| 136 | |
| 137 | static double getTickInUsec() { |
| 138 | FILE *fp = fopen("/proc/net/psched", "re"); |
| 139 | if (!fp) { |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 140 | ALOGE("fopen(\"/proc/net/psched\"): %s", strerror(errno)); |
Patrick Rohr | e815a74 | 2022-01-17 10:37:40 +0100 | [diff] [blame] | 141 | return 0.0; |
| 142 | } |
Patrick Rohr | 6b84b40 | 2024-05-16 12:06:10 -0700 | [diff] [blame] | 143 | auto scopeGuard = make_scope_guard([fp] { fclose(fp); }); |
Patrick Rohr | e815a74 | 2022-01-17 10:37:40 +0100 | [diff] [blame] | 144 | |
| 145 | uint32_t t2us; |
| 146 | uint32_t us2t; |
| 147 | uint32_t clockRes; |
| 148 | const bool isError = |
| 149 | fscanf(fp, "%08x%08x%08x", &t2us, &us2t, &clockRes) != 3; |
| 150 | |
| 151 | if (isError) { |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 152 | ALOGE("fscanf(/proc/net/psched, \"%%08x%%08x%%08x\"): %s", |
Patrick Rohr | e815a74 | 2022-01-17 10:37:40 +0100 | [diff] [blame] | 153 | strerror(errno)); |
| 154 | return 0.0; |
| 155 | } |
| 156 | |
| 157 | const double clockFactor = |
| 158 | static_cast<double>(clockRes) / TIME_UNITS_PER_SEC; |
| 159 | return static_cast<double>(t2us) / static_cast<double>(us2t) * clockFactor; |
| 160 | } |
| 161 | |
| 162 | static inline const double kTickInUsec = getTickInUsec(); |
| 163 | |
| 164 | public: |
| 165 | // clang-format off |
| 166 | IngressPoliceFilterBuilder(int ifIndex, uint16_t prio, uint16_t proto, unsigned rateInBytesPerSec, |
| 167 | unsigned burstInBytes, const char* bpfProgPath) |
| 168 | : mBurstInBytes(burstInBytes), |
| 169 | mBpfProgPath(bpfProgPath), |
Patrick Rohr | e815a74 | 2022-01-17 10:37:40 +0100 | [diff] [blame] | 170 | mRequest{ |
| 171 | .n = { |
| 172 | .nlmsg_len = sizeof(mRequest), |
| 173 | .nlmsg_type = RTM_NEWTFILTER, |
| 174 | .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE, |
| 175 | }, |
| 176 | .t = { |
| 177 | .tcm_family = AF_UNSPEC, |
| 178 | .tcm_ifindex = ifIndex, |
| 179 | .tcm_handle = TC_H_UNSPEC, |
| 180 | .tcm_parent = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS), |
| 181 | .tcm_info = (static_cast<uint32_t>(prio) << 16) |
| 182 | | static_cast<uint32_t>(htons(proto)), |
| 183 | }, |
| 184 | .kind = { |
| 185 | .attr = { |
| 186 | .nla_len = sizeof(mRequest.kind), |
| 187 | .nla_type = TCA_KIND, |
| 188 | }, |
| 189 | .str = "matchall", |
| 190 | }, |
| 191 | .opt = { |
| 192 | .attr = { |
| 193 | .nla_len = sizeof(mRequest.opt), |
| 194 | .nla_type = TCA_OPTIONS, |
| 195 | }, |
| 196 | .acts = { |
| 197 | .attr = { |
| 198 | .nla_len = sizeof(mRequest.opt.acts), |
Patrick Rohr | 20dcaf5 | 2022-02-11 13:14:45 +0100 | [diff] [blame] | 199 | .nla_type = TCA_MATCHALL_ACT, |
Patrick Rohr | e815a74 | 2022-01-17 10:37:40 +0100 | [diff] [blame] | 200 | }, |
| 201 | .act1 = { |
| 202 | .attr = { |
| 203 | .nla_len = sizeof(mRequest.opt.acts.act1), |
| 204 | .nla_type = 1, // action priority |
| 205 | }, |
| 206 | .kind = { |
| 207 | .attr = { |
| 208 | .nla_len = sizeof(mRequest.opt.acts.act1.kind), |
| 209 | .nla_type = TCA_ACT_KIND, |
| 210 | }, |
| 211 | .str = "police", |
| 212 | }, |
| 213 | .opt = { |
| 214 | .attr = { |
| 215 | .nla_len = sizeof(mRequest.opt.acts.act1.opt), |
| 216 | .nla_type = TCA_ACT_OPTIONS | NLA_F_NESTED, |
| 217 | }, |
| 218 | .police = { |
| 219 | .attr = { |
| 220 | .nla_len = sizeof(mRequest.opt.acts.act1.opt.police), |
| 221 | .nla_type = TCA_POLICE_TBF, |
| 222 | }, |
| 223 | .obj = { |
| 224 | .action = TC_ACT_PIPE, |
| 225 | .burst = 0, |
| 226 | .rate = { |
| 227 | .cell_log = RTAB_CELL_LOGARITHM, |
| 228 | .linklayer = TC_LINKLAYER_ETHERNET, |
| 229 | .cell_align = -1, |
| 230 | .rate = rateInBytesPerSec, |
| 231 | }, |
| 232 | }, |
| 233 | }, |
| 234 | .rtab = { |
| 235 | .attr = { |
| 236 | .nla_len = sizeof(mRequest.opt.acts.act1.opt.rtab), |
| 237 | .nla_type = TCA_POLICE_RATE, |
| 238 | }, |
| 239 | .u32 = {}, |
| 240 | }, |
| 241 | .notexceedact = { |
| 242 | .attr = { |
| 243 | .nla_len = sizeof(mRequest.opt.acts.act1.opt.notexceedact), |
| 244 | .nla_type = TCA_POLICE_RESULT, |
| 245 | }, |
| 246 | .s32 = TC_ACT_UNSPEC, |
| 247 | }, |
| 248 | }, |
| 249 | }, |
| 250 | .act2 = { |
| 251 | .attr = { |
| 252 | .nla_len = sizeof(mRequest.opt.acts.act2), |
| 253 | .nla_type = 2, // action priority |
| 254 | }, |
| 255 | .kind = { |
| 256 | .attr = { |
| 257 | .nla_len = sizeof(mRequest.opt.acts.act2.kind), |
| 258 | .nla_type = TCA_ACT_KIND, |
| 259 | }, |
| 260 | .str = "bpf", |
| 261 | }, |
| 262 | .opt = { |
| 263 | .attr = { |
| 264 | .nla_len = sizeof(mRequest.opt.acts.act2.opt), |
| 265 | .nla_type = TCA_ACT_OPTIONS | NLA_F_NESTED, |
| 266 | }, |
| 267 | .fd = { |
| 268 | .attr = { |
| 269 | .nla_len = sizeof(mRequest.opt.acts.act2.opt.fd), |
| 270 | .nla_type = TCA_ACT_BPF_FD, |
| 271 | }, |
| 272 | .u32 = 0, // set during build() |
| 273 | }, |
| 274 | .name = { |
| 275 | .attr = { |
| 276 | .nla_len = sizeof(mRequest.opt.acts.act2.opt.name), |
| 277 | .nla_type = TCA_ACT_BPF_NAME, |
| 278 | }, |
| 279 | .str = "placeholder", |
| 280 | }, |
| 281 | .parms = { |
| 282 | .attr = { |
| 283 | .nla_len = sizeof(mRequest.opt.acts.act2.opt.parms), |
| 284 | .nla_type = TCA_ACT_BPF_PARMS, |
| 285 | }, |
| 286 | .obj = { |
| 287 | // default action to be executed when bpf prog |
| 288 | // returns TC_ACT_UNSPEC. |
| 289 | .action = TC_ACT_SHOT, |
| 290 | }, |
| 291 | }, |
| 292 | }, |
| 293 | }, |
| 294 | }, |
| 295 | }, |
| 296 | } { |
| 297 | // constructor body |
| 298 | } |
| 299 | // clang-format on |
| 300 | |
Patrick Rohr | e815a74 | 2022-01-17 10:37:40 +0100 | [diff] [blame] | 301 | constexpr unsigned getRequestSize() const { return sizeof(Request); } |
| 302 | |
| 303 | private: |
| 304 | unsigned calculateXmitTime(unsigned size) { |
| 305 | const uint32_t rate = mRequest.opt.acts.act1.opt.police.obj.rate.rate; |
| 306 | return (static_cast<double>(size) / static_cast<double>(rate)) * |
| 307 | TIME_UNITS_PER_SEC * kTickInUsec; |
| 308 | } |
| 309 | |
| 310 | void initBurstRate() { |
| 311 | mRequest.opt.acts.act1.opt.police.obj.burst = |
| 312 | calculateXmitTime(mBurstInBytes); |
| 313 | } |
| 314 | |
| 315 | // Calculates a table with 256 transmission times for different packet sizes |
| 316 | // (all the way up to MTU). RTAB_CELL_LOGARITHM is used as a scaling factor. |
| 317 | // In this case, MTU size is always 2048, so RTAB_CELL_LOGARITHM is always |
| 318 | // 3. Therefore, this function generates the transmission times for packets |
| 319 | // of size 1..256 x 2^3. |
| 320 | void initRateTable() { |
| 321 | for (unsigned i = 0; i < RTAB_SIZE; ++i) { |
| 322 | unsigned adjustedSize = (i + 1) << RTAB_CELL_LOGARITHM; |
| 323 | mRequest.opt.acts.act1.opt.rtab.u32[i] = calculateXmitTime(adjustedSize); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | int initBpfFd() { |
Patrick Rohr | 6b84b40 | 2024-05-16 12:06:10 -0700 | [diff] [blame] | 328 | mBpfFd.reset(bpf::retrieveProgram(mBpfProgPath)); |
| 329 | if (!mBpfFd.ok()) { |
Patrick Rohr | e815a74 | 2022-01-17 10:37:40 +0100 | [diff] [blame] | 330 | int error = errno; |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 331 | ALOGE("retrieveProgram failed: %d", error); |
Patrick Rohr | e815a74 | 2022-01-17 10:37:40 +0100 | [diff] [blame] | 332 | return -error; |
| 333 | } |
| 334 | |
Patrick Rohr | 6b84b40 | 2024-05-16 12:06:10 -0700 | [diff] [blame] | 335 | mRequest.opt.acts.act2.opt.fd.u32 = static_cast<uint32_t>(mBpfFd.get()); |
Patrick Rohr | e815a74 | 2022-01-17 10:37:40 +0100 | [diff] [blame] | 336 | snprintf(mRequest.opt.acts.act2.opt.name.str, |
| 337 | sizeof(mRequest.opt.acts.act2.opt.name.str), "%s:[*fsobj]", |
| 338 | basename(mBpfProgPath)); |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | public: |
| 344 | int build() { |
| 345 | if (kTickInUsec == 0.0) { |
| 346 | return -EINVAL; |
| 347 | } |
| 348 | |
| 349 | initBurstRate(); |
| 350 | initRateTable(); |
| 351 | return initBpfFd(); |
| 352 | } |
| 353 | |
| 354 | const Request *getRequest() const { |
| 355 | // Make sure to call build() before calling this function. Otherwise, the |
| 356 | // request will be invalid. |
| 357 | return &mRequest; |
| 358 | } |
| 359 | }; |
| 360 | |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 361 | const sockaddr_nl KERNEL_NLADDR = {AF_NETLINK, 0, 0, 0}; |
| 362 | const uint16_t NETLINK_REQUEST_FLAGS = NLM_F_REQUEST | NLM_F_ACK; |
| 363 | |
| 364 | int sendAndProcessNetlinkResponse(const void *req, int len) { |
| 365 | // TODO: use unique_fd instead of ScopeGuard |
Patrick Rohr | 6b84b40 | 2024-05-16 12:06:10 -0700 | [diff] [blame] | 366 | unique_fd fd(socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE)); |
| 367 | if (!fd.ok()) { |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 368 | int error = errno; |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 369 | ALOGE("socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE): %d", |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 370 | error); |
| 371 | return -error; |
| 372 | } |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 373 | |
| 374 | static constexpr int on = 1; |
| 375 | if (setsockopt(fd, SOL_NETLINK, NETLINK_CAP_ACK, &on, sizeof(on))) { |
| 376 | int error = errno; |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 377 | ALOGE("setsockopt(fd, SOL_NETLINK, NETLINK_CAP_ACK, 1): %d", error); |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 378 | return -error; |
| 379 | } |
| 380 | |
Maciej Żenczykowski | e8dcedd | 2022-06-09 23:27:32 +0000 | [diff] [blame] | 381 | if (setsockopt(fd, SOL_NETLINK, NETLINK_EXT_ACK, &on, sizeof(on))) { |
| 382 | int error = errno; |
| 383 | ALOGW("setsockopt(fd, SOL_NETLINK, NETLINK_EXT_ACK, 1): %d", error); |
| 384 | // will fail on 4.9 kernels so don't: return -error; |
| 385 | } |
| 386 | |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 387 | // this is needed to get valid strace netlink parsing, it allocates the pid |
| 388 | if (bind(fd, (const struct sockaddr *)&KERNEL_NLADDR, |
| 389 | sizeof(KERNEL_NLADDR))) { |
| 390 | int error = errno; |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 391 | ALOGE("bind(fd, {AF_NETLINK, 0, 0}: %d)", error); |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 392 | return -error; |
| 393 | } |
| 394 | |
| 395 | // we do not want to receive messages from anyone besides the kernel |
| 396 | if (connect(fd, (const struct sockaddr *)&KERNEL_NLADDR, |
| 397 | sizeof(KERNEL_NLADDR))) { |
| 398 | int error = errno; |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 399 | ALOGE("connect(fd, {AF_NETLINK, 0, 0}): %d", error); |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 400 | return -error; |
| 401 | } |
| 402 | |
| 403 | int rv = send(fd, req, len, 0); |
| 404 | |
| 405 | if (rv == -1) { |
| 406 | int error = errno; |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 407 | ALOGE("send(fd, req, len, 0) failed: %d", error); |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 408 | return -error; |
| 409 | } |
| 410 | |
| 411 | if (rv != len) { |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 412 | ALOGE("send(fd, req, len = %d, 0) returned invalid message size %d", len, |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 413 | rv); |
| 414 | return -EMSGSIZE; |
| 415 | } |
| 416 | |
| 417 | struct { |
| 418 | nlmsghdr h; |
| 419 | nlmsgerr e; |
| 420 | char buf[256]; |
| 421 | } resp = {}; |
| 422 | |
| 423 | rv = recv(fd, &resp, sizeof(resp), MSG_TRUNC); |
| 424 | |
| 425 | if (rv == -1) { |
| 426 | int error = errno; |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 427 | ALOGE("recv() failed: %d", error); |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 428 | return -error; |
| 429 | } |
| 430 | |
| 431 | if (rv < (int)NLMSG_SPACE(sizeof(struct nlmsgerr))) { |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 432 | ALOGE("recv() returned short packet: %d", rv); |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 433 | return -EBADMSG; |
| 434 | } |
| 435 | |
| 436 | if (resp.h.nlmsg_len != (unsigned)rv) { |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 437 | ALOGE("recv() returned invalid header length: %d != %d", |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 438 | resp.h.nlmsg_len, rv); |
| 439 | return -EBADMSG; |
| 440 | } |
| 441 | |
| 442 | if (resp.h.nlmsg_type != NLMSG_ERROR) { |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 443 | ALOGE("recv() did not return NLMSG_ERROR message: %d", |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 444 | resp.h.nlmsg_type); |
| 445 | return -ENOMSG; |
| 446 | } |
| 447 | |
| 448 | if (resp.e.error) { |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 449 | ALOGE("NLMSG_ERROR message return error: %d", resp.e.error); |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 450 | } |
| 451 | return resp.e.error; // returns 0 on success |
| 452 | } |
| 453 | |
| 454 | int hardwareAddressType(const char *interface) { |
Patrick Rohr | 6b84b40 | 2024-05-16 12:06:10 -0700 | [diff] [blame] | 455 | unique_fd fd(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 456 | if (!fd.ok()) |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 457 | return -errno; |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 458 | |
| 459 | struct ifreq ifr = {}; |
| 460 | // We use strncpy() instead of strlcpy() since kernel has to be able |
| 461 | // to handle non-zero terminated junk passed in by userspace anyway, |
| 462 | // and this way too long interface names (more than IFNAMSIZ-1 = 15 |
| 463 | // characters plus terminating NULL) will not get truncated to 15 |
| 464 | // characters and zero-terminated and thus potentially erroneously |
| 465 | // match a truncated interface if one were to exist. |
| 466 | strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)); |
| 467 | |
| 468 | if (ioctl(fd, SIOCGIFHWADDR, &ifr, sizeof(ifr))) { |
| 469 | return -errno; |
| 470 | } |
| 471 | return ifr.ifr_hwaddr.sa_family; |
| 472 | } |
| 473 | |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 474 | } // namespace |
| 475 | |
| 476 | int isEthernet(const char *iface, bool &isEthernet) { |
| 477 | int rv = hardwareAddressType(iface); |
| 478 | if (rv < 0) { |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 479 | ALOGE("Get hardware address type of interface %s failed: %s", iface, |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 480 | strerror(-rv)); |
Patrick Rohr | 27846ff | 2022-01-17 12:22:51 +0100 | [diff] [blame] | 481 | return rv; |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | // Backwards compatibility with pre-GKI kernels that use various custom |
| 485 | // ARPHRD_* for their cellular interface |
| 486 | switch (rv) { |
| 487 | // ARPHRD_PUREIP on at least some Mediatek Android kernels |
| 488 | // example: wembley with 4.19 kernel |
| 489 | case 520: |
| 490 | // in Linux 4.14+ rmnet support was upstreamed and ARHRD_RAWIP became 519, |
| 491 | // but it is 530 on at least some Qualcomm Android 4.9 kernels with rmnet |
| 492 | // example: Pixel 3 family |
| 493 | case 530: |
| 494 | // >5.4 kernels are GKI2.0 and thus upstream compatible, however 5.10 |
| 495 | // shipped with Android S, so (for safety) let's limit ourselves to |
| 496 | // >5.10, ie. 5.11+ as a guarantee we're on Android T+ and thus no |
| 497 | // longer need this non-upstream compatibility logic |
Maciej Żenczykowski | d7d3b03 | 2022-12-22 17:03:18 +0000 | [diff] [blame] | 498 | static bool is_pre_5_11_kernel = !bpf::isAtLeastKernelVersion(5, 11, 0); |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 499 | if (is_pre_5_11_kernel) |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | switch (rv) { |
| 504 | case ARPHRD_ETHER: |
| 505 | isEthernet = true; |
| 506 | return 0; |
| 507 | case ARPHRD_NONE: |
| 508 | case ARPHRD_PPP: |
| 509 | case ARPHRD_RAWIP: |
| 510 | isEthernet = false; |
| 511 | return 0; |
| 512 | default: |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 513 | ALOGE("Unknown hardware address type %d on interface %s", rv, iface); |
Patrick Rohr | 27846ff | 2022-01-17 12:22:51 +0100 | [diff] [blame] | 514 | return -EAFNOSUPPORT; |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
Patrick Rohr | 42b58ae | 2022-01-17 13:09:12 +0100 | [diff] [blame] | 518 | // ADD: nlMsgType=RTM_NEWQDISC nlMsgFlags=NLM_F_EXCL|NLM_F_CREATE |
| 519 | // REPLACE: nlMsgType=RTM_NEWQDISC nlMsgFlags=NLM_F_CREATE|NLM_F_REPLACE |
| 520 | // DEL: nlMsgType=RTM_DELQDISC nlMsgFlags=0 |
| 521 | int doTcQdiscClsact(int ifIndex, uint16_t nlMsgType, uint16_t nlMsgFlags) { |
| 522 | // This is the name of the qdisc we are attaching. |
| 523 | // Some hoop jumping to make this compile time constant with known size, |
| 524 | // so that the structure declaration is well defined at compile time. |
| 525 | #define CLSACT "clsact" |
| 526 | // sizeof() includes the terminating NULL |
| 527 | static constexpr size_t ASCIIZ_LEN_CLSACT = sizeof(CLSACT); |
| 528 | |
| 529 | const struct { |
| 530 | nlmsghdr n; |
| 531 | tcmsg t; |
| 532 | struct { |
| 533 | nlattr attr; |
| 534 | char str[NLMSG_ALIGN(ASCIIZ_LEN_CLSACT)]; |
| 535 | } kind; |
| 536 | } req = { |
| 537 | .n = |
| 538 | { |
| 539 | .nlmsg_len = sizeof(req), |
| 540 | .nlmsg_type = nlMsgType, |
| 541 | .nlmsg_flags = |
| 542 | static_cast<__u16>(NETLINK_REQUEST_FLAGS | nlMsgFlags), |
| 543 | }, |
| 544 | .t = |
| 545 | { |
| 546 | .tcm_family = AF_UNSPEC, |
| 547 | .tcm_ifindex = ifIndex, |
| 548 | .tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0), |
| 549 | .tcm_parent = TC_H_CLSACT, |
| 550 | }, |
| 551 | .kind = |
| 552 | { |
| 553 | .attr = |
| 554 | { |
| 555 | .nla_len = NLA_HDRLEN + ASCIIZ_LEN_CLSACT, |
| 556 | .nla_type = TCA_KIND, |
| 557 | }, |
| 558 | .str = CLSACT, |
| 559 | }, |
| 560 | }; |
| 561 | #undef CLSACT |
| 562 | |
| 563 | return sendAndProcessNetlinkResponse(&req, sizeof(req)); |
| 564 | } |
| 565 | |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 566 | // tc filter add dev .. in/egress prio 1 protocol ipv6/ip bpf object-pinned |
| 567 | // /sys/fs/bpf/... direct-action |
| 568 | int tcAddBpfFilter(int ifIndex, bool ingress, uint16_t prio, uint16_t proto, |
| 569 | const char *bpfProgPath) { |
Patrick Rohr | 6b84b40 | 2024-05-16 12:06:10 -0700 | [diff] [blame] | 570 | unique_fd bpfFd(bpf::retrieveProgram(bpfProgPath)); |
| 571 | if (!bpfFd.ok()) { |
Patrick Rohr | 28c717a | 2022-01-19 14:51:35 +0100 | [diff] [blame] | 572 | ALOGE("retrieveProgram failed: %d", errno); |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 573 | return -errno; |
| 574 | } |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 575 | |
| 576 | struct { |
| 577 | nlmsghdr n; |
| 578 | tcmsg t; |
| 579 | struct { |
| 580 | nlattr attr; |
| 581 | // The maximum classifier name length is defined in |
| 582 | // tcf_proto_ops in include/net/sch_generic.h. |
| 583 | char str[NLMSG_ALIGN(sizeof(CLS_BPF_KIND_NAME))]; |
| 584 | } kind; |
| 585 | struct { |
| 586 | nlattr attr; |
| 587 | struct { |
| 588 | nlattr attr; |
| 589 | __u32 u32; |
| 590 | } fd; |
| 591 | struct { |
| 592 | nlattr attr; |
| 593 | char str[NLMSG_ALIGN(CLS_BPF_NAME_LEN)]; |
| 594 | } name; |
| 595 | struct { |
| 596 | nlattr attr; |
| 597 | __u32 u32; |
| 598 | } flags; |
| 599 | } options; |
| 600 | } req = { |
| 601 | .n = |
| 602 | { |
| 603 | .nlmsg_len = sizeof(req), |
| 604 | .nlmsg_type = RTM_NEWTFILTER, |
| 605 | .nlmsg_flags = NETLINK_REQUEST_FLAGS | NLM_F_EXCL | NLM_F_CREATE, |
| 606 | }, |
| 607 | .t = |
| 608 | { |
| 609 | .tcm_family = AF_UNSPEC, |
| 610 | .tcm_ifindex = ifIndex, |
| 611 | .tcm_handle = TC_H_UNSPEC, |
| 612 | .tcm_parent = TC_H_MAKE(TC_H_CLSACT, ingress ? TC_H_MIN_INGRESS |
| 613 | : TC_H_MIN_EGRESS), |
| 614 | .tcm_info = |
| 615 | static_cast<__u32>((static_cast<uint16_t>(prio) << 16) | |
| 616 | htons(static_cast<uint16_t>(proto))), |
| 617 | }, |
| 618 | .kind = |
| 619 | { |
| 620 | .attr = |
| 621 | { |
| 622 | .nla_len = sizeof(req.kind), |
| 623 | .nla_type = TCA_KIND, |
| 624 | }, |
| 625 | .str = CLS_BPF_KIND_NAME, |
| 626 | }, |
| 627 | .options = |
| 628 | { |
| 629 | .attr = |
| 630 | { |
| 631 | .nla_len = sizeof(req.options), |
| 632 | .nla_type = NLA_F_NESTED | TCA_OPTIONS, |
| 633 | }, |
| 634 | .fd = |
| 635 | { |
| 636 | .attr = |
| 637 | { |
| 638 | .nla_len = sizeof(req.options.fd), |
| 639 | .nla_type = TCA_BPF_FD, |
| 640 | }, |
| 641 | .u32 = static_cast<__u32>(bpfFd), |
| 642 | }, |
| 643 | .name = |
| 644 | { |
| 645 | .attr = |
| 646 | { |
| 647 | .nla_len = sizeof(req.options.name), |
| 648 | .nla_type = TCA_BPF_NAME, |
| 649 | }, |
| 650 | // Visible via 'tc filter show', but |
| 651 | // is overwritten by strncpy below |
| 652 | .str = "placeholder", |
| 653 | }, |
| 654 | .flags = |
| 655 | { |
| 656 | .attr = |
| 657 | { |
| 658 | .nla_len = sizeof(req.options.flags), |
| 659 | .nla_type = TCA_BPF_FLAGS, |
| 660 | }, |
| 661 | .u32 = TCA_BPF_FLAG_ACT_DIRECT, |
| 662 | }, |
| 663 | }, |
| 664 | }; |
| 665 | |
| 666 | snprintf(req.options.name.str, sizeof(req.options.name.str), "%s:[*fsobj]", |
| 667 | basename(bpfProgPath)); |
| 668 | |
| 669 | int error = sendAndProcessNetlinkResponse(&req, sizeof(req)); |
| 670 | return error; |
| 671 | } |
| 672 | |
Patrick Rohr | e815a74 | 2022-01-17 10:37:40 +0100 | [diff] [blame] | 673 | // tc filter add dev .. ingress prio .. protocol .. matchall \ |
| 674 | // action police rate .. burst .. conform-exceed pipe/continue \ |
| 675 | // action bpf object-pinned .. \ |
| 676 | // drop |
| 677 | // |
| 678 | // TODO: tc-police does not do ECN marking, so in the future, we should consider |
| 679 | // adding a second tc-police filter at a lower priority that rate limits traffic |
| 680 | // at something like 0.8 times the global rate limit and ecn marks exceeding |
| 681 | // packets inside a bpf program (but does not drop them). |
| 682 | int tcAddIngressPoliceFilter(int ifIndex, uint16_t prio, uint16_t proto, |
| 683 | unsigned rateInBytesPerSec, |
| 684 | const char *bpfProgPath) { |
| 685 | // TODO: this value needs to be validated. |
| 686 | // TCP IW10 (initial congestion window) means servers will send 10 mtus worth |
| 687 | // of data on initial connect. |
| 688 | // If nic is LRO capable it could aggregate up to 64KiB, so again probably a |
| 689 | // bad idea to set burst below that, because ingress packets could get |
| 690 | // aggregated to 64KiB at the nic. |
| 691 | // I don't know, but I wonder whether we shouldn't just do 128KiB and not do |
| 692 | // any math. |
| 693 | static constexpr unsigned BURST_SIZE_IN_BYTES = 128 * 1024; // 128KiB |
| 694 | IngressPoliceFilterBuilder filter(ifIndex, prio, proto, rateInBytesPerSec, |
| 695 | BURST_SIZE_IN_BYTES, bpfProgPath); |
| 696 | const int error = filter.build(); |
| 697 | if (error) { |
| 698 | return error; |
| 699 | } |
| 700 | return sendAndProcessNetlinkResponse(filter.getRequest(), |
| 701 | filter.getRequestSize()); |
| 702 | } |
| 703 | |
Patrick Rohr | 776c40c | 2022-01-12 21:05:26 +0100 | [diff] [blame] | 704 | // tc filter del dev .. in/egress prio .. protocol .. |
| 705 | int tcDeleteFilter(int ifIndex, bool ingress, uint16_t prio, uint16_t proto) { |
| 706 | const struct { |
| 707 | nlmsghdr n; |
| 708 | tcmsg t; |
| 709 | } req = { |
| 710 | .n = |
| 711 | { |
| 712 | .nlmsg_len = sizeof(req), |
| 713 | .nlmsg_type = RTM_DELTFILTER, |
| 714 | .nlmsg_flags = NETLINK_REQUEST_FLAGS, |
| 715 | }, |
| 716 | .t = |
| 717 | { |
| 718 | .tcm_family = AF_UNSPEC, |
| 719 | .tcm_ifindex = ifIndex, |
| 720 | .tcm_handle = TC_H_UNSPEC, |
| 721 | .tcm_parent = TC_H_MAKE(TC_H_CLSACT, ingress ? TC_H_MIN_INGRESS |
| 722 | : TC_H_MIN_EGRESS), |
| 723 | .tcm_info = |
| 724 | static_cast<__u32>((static_cast<uint16_t>(prio) << 16) | |
| 725 | htons(static_cast<uint16_t>(proto))), |
| 726 | }, |
| 727 | }; |
| 728 | |
| 729 | return sendAndProcessNetlinkResponse(&req, sizeof(req)); |
| 730 | } |
| 731 | |
| 732 | } // namespace android |