Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -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 | |
| 17 | #define LOG_TAG "BpfUtils" |
| 18 | |
Bernie Innocenti | 26ffded | 2018-10-19 15:41:53 +0900 | [diff] [blame] | 19 | #include "bpf/BpfUtils.h" |
| 20 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 21 | #include <elf.h> |
| 22 | #include <inttypes.h> |
| 23 | #include <linux/bpf.h> |
| 24 | #include <linux/if_ether.h> |
| 25 | #include <linux/in.h> |
Chenbo Feng | 9cd8f14 | 2018-12-04 16:54:56 -0800 | [diff] [blame] | 26 | #include <linux/pfkeyv2.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/mman.h> |
Chenbo Feng | 0a1a9a1 | 2019-04-09 12:05:04 -0700 | [diff] [blame] | 30 | #include <sys/resource.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 31 | #include <sys/socket.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <sys/utsname.h> |
| 34 | #include <sstream> |
| 35 | #include <string> |
| 36 | |
| 37 | #include <android-base/properties.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 38 | #include <android-base/unique_fd.h> |
Bernie Innocenti | 26ffded | 2018-10-19 15:41:53 +0900 | [diff] [blame] | 39 | #include <log/log.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 40 | #include <netdutils/MemBlock.h> |
| 41 | #include <netdutils/Slice.h> |
Suren Baghdasaryan | 9217ccb | 2018-12-19 17:29:13 -0800 | [diff] [blame] | 42 | #include <processgroup/processgroup.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 43 | |
| 44 | using android::base::GetUintProperty; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 45 | using android::base::unique_fd; |
| 46 | using android::netdutils::MemBlock; |
| 47 | using android::netdutils::Slice; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 48 | |
Chenbo Feng | 9cd8f14 | 2018-12-04 16:54:56 -0800 | [diff] [blame] | 49 | // The buffer size for the buffer that records program loading logs, needs to be large enough for |
| 50 | // the largest kernel program. |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 51 | |
| 52 | namespace android { |
| 53 | namespace bpf { |
| 54 | |
| 55 | /* The bpf_attr is a union which might have a much larger size then the struct we are using, while |
| 56 | * The inline initializer only reset the field we are using and leave the reset of the memory as |
| 57 | * is. The bpf kernel code will performs a much stricter check to ensure all unused field is 0. So |
| 58 | * this syscall will normally fail with E2BIG if we don't do a memset to bpf_attr. |
| 59 | */ |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 60 | |
| 61 | int bpf(int cmd, Slice bpfAttr) { |
| 62 | return syscall(__NR_bpf, cmd, bpfAttr.base(), bpfAttr.size()); |
| 63 | } |
| 64 | |
| 65 | int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size, uint32_t max_entries, |
| 66 | uint32_t map_flags) { |
| 67 | bpf_attr attr; |
| 68 | memset(&attr, 0, sizeof(attr)); |
| 69 | attr.map_type = map_type; |
| 70 | attr.key_size = key_size; |
| 71 | attr.value_size = value_size; |
| 72 | attr.max_entries = max_entries; |
| 73 | attr.map_flags = map_flags; |
| 74 | |
| 75 | return bpf(BPF_MAP_CREATE, Slice(&attr, sizeof(attr))); |
| 76 | } |
| 77 | |
| 78 | int writeToMapEntry(const base::unique_fd& map_fd, void* key, void* value, uint64_t flags) { |
| 79 | bpf_attr attr; |
| 80 | memset(&attr, 0, sizeof(attr)); |
| 81 | attr.map_fd = map_fd.get(); |
| 82 | attr.key = ptr_to_u64(key); |
| 83 | attr.value = ptr_to_u64(value); |
| 84 | attr.flags = flags; |
| 85 | |
| 86 | return bpf(BPF_MAP_UPDATE_ELEM, Slice(&attr, sizeof(attr))); |
| 87 | } |
| 88 | |
| 89 | int findMapEntry(const base::unique_fd& map_fd, void* key, void* value) { |
| 90 | bpf_attr attr; |
| 91 | memset(&attr, 0, sizeof(attr)); |
| 92 | attr.map_fd = map_fd.get(); |
| 93 | attr.key = ptr_to_u64(key); |
| 94 | attr.value = ptr_to_u64(value); |
| 95 | |
| 96 | return bpf(BPF_MAP_LOOKUP_ELEM, Slice(&attr, sizeof(attr))); |
| 97 | } |
| 98 | |
| 99 | int deleteMapEntry(const base::unique_fd& map_fd, void* key) { |
| 100 | bpf_attr attr; |
| 101 | memset(&attr, 0, sizeof(attr)); |
| 102 | attr.map_fd = map_fd.get(); |
| 103 | attr.key = ptr_to_u64(key); |
| 104 | |
| 105 | return bpf(BPF_MAP_DELETE_ELEM, Slice(&attr, sizeof(attr))); |
| 106 | } |
| 107 | |
| 108 | int getNextMapKey(const base::unique_fd& map_fd, void* key, void* next_key) { |
| 109 | bpf_attr attr; |
| 110 | memset(&attr, 0, sizeof(attr)); |
| 111 | attr.map_fd = map_fd.get(); |
| 112 | attr.key = ptr_to_u64(key); |
| 113 | attr.next_key = ptr_to_u64(next_key); |
| 114 | |
| 115 | return bpf(BPF_MAP_GET_NEXT_KEY, Slice(&attr, sizeof(attr))); |
| 116 | } |
| 117 | |
| 118 | int getFirstMapKey(const base::unique_fd& map_fd, void* firstKey) { |
| 119 | bpf_attr attr; |
| 120 | memset(&attr, 0, sizeof(attr)); |
| 121 | attr.map_fd = map_fd.get(); |
| 122 | attr.key = 0; |
| 123 | attr.next_key = ptr_to_u64(firstKey); |
| 124 | |
| 125 | return bpf(BPF_MAP_GET_NEXT_KEY, Slice(&attr, sizeof(attr))); |
| 126 | } |
| 127 | |
| 128 | int bpfProgLoad(bpf_prog_type prog_type, Slice bpf_insns, const char* license, |
| 129 | uint32_t kern_version, Slice bpf_log) { |
| 130 | bpf_attr attr; |
| 131 | memset(&attr, 0, sizeof(attr)); |
| 132 | attr.prog_type = prog_type; |
| 133 | attr.insns = ptr_to_u64(bpf_insns.base()); |
| 134 | attr.insn_cnt = bpf_insns.size() / sizeof(struct bpf_insn); |
| 135 | attr.license = ptr_to_u64((void*)license); |
| 136 | attr.log_buf = ptr_to_u64(bpf_log.base()); |
| 137 | attr.log_size = bpf_log.size(); |
| 138 | attr.log_level = DEFAULT_LOG_LEVEL; |
| 139 | attr.kern_version = kern_version; |
| 140 | int ret = bpf(BPF_PROG_LOAD, Slice(&attr, sizeof(attr))); |
| 141 | |
| 142 | if (ret < 0) { |
| 143 | std::string prog_log = netdutils::toString(bpf_log); |
| 144 | std::istringstream iss(prog_log); |
| 145 | for (std::string line; std::getline(iss, line);) { |
| 146 | ALOGE("%s", line.c_str()); |
| 147 | } |
| 148 | } |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | int bpfFdPin(const base::unique_fd& map_fd, const char* pathname) { |
| 153 | bpf_attr attr; |
| 154 | memset(&attr, 0, sizeof(attr)); |
| 155 | attr.pathname = ptr_to_u64((void*)pathname); |
| 156 | attr.bpf_fd = map_fd.get(); |
| 157 | |
| 158 | return bpf(BPF_OBJ_PIN, Slice(&attr, sizeof(attr))); |
| 159 | } |
| 160 | |
Chenbo Feng | c1dd764 | 2018-12-22 11:41:20 -0800 | [diff] [blame] | 161 | int bpfFdGet(const char* pathname, uint32_t flag) { |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 162 | bpf_attr attr; |
| 163 | memset(&attr, 0, sizeof(attr)); |
| 164 | attr.pathname = ptr_to_u64((void*)pathname); |
| 165 | attr.file_flags = flag; |
| 166 | return bpf(BPF_OBJ_GET, Slice(&attr, sizeof(attr))); |
| 167 | } |
| 168 | |
Chenbo Feng | c1dd764 | 2018-12-22 11:41:20 -0800 | [diff] [blame] | 169 | int mapRetrieve(const char* pathname, uint32_t flag) { |
| 170 | return bpfFdGet(pathname, flag); |
| 171 | } |
| 172 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 173 | int attachProgram(bpf_attach_type type, uint32_t prog_fd, uint32_t cg_fd) { |
| 174 | bpf_attr attr; |
| 175 | memset(&attr, 0, sizeof(attr)); |
| 176 | attr.target_fd = cg_fd; |
| 177 | attr.attach_bpf_fd = prog_fd; |
| 178 | attr.attach_type = type; |
| 179 | |
| 180 | return bpf(BPF_PROG_ATTACH, Slice(&attr, sizeof(attr))); |
| 181 | } |
| 182 | |
| 183 | int detachProgram(bpf_attach_type type, uint32_t cg_fd) { |
| 184 | bpf_attr attr; |
| 185 | memset(&attr, 0, sizeof(attr)); |
| 186 | attr.target_fd = cg_fd; |
| 187 | attr.attach_type = type; |
| 188 | |
| 189 | return bpf(BPF_PROG_DETACH, Slice(&attr, sizeof(attr))); |
| 190 | } |
| 191 | |
| 192 | uint64_t getSocketCookie(int sockFd) { |
| 193 | uint64_t sock_cookie; |
| 194 | socklen_t cookie_len = sizeof(sock_cookie); |
| 195 | int res = getsockopt(sockFd, SOL_SOCKET, SO_COOKIE, &sock_cookie, &cookie_len); |
| 196 | if (res < 0) { |
| 197 | res = -errno; |
| 198 | ALOGE("Failed to get socket cookie: %s\n", strerror(errno)); |
| 199 | errno = -res; |
| 200 | // 0 is an invalid cookie. See sock_gen_cookie. |
| 201 | return NONEXISTENT_COOKIE; |
| 202 | } |
| 203 | return sock_cookie; |
| 204 | } |
| 205 | |
Chenbo Feng | 9cd8f14 | 2018-12-04 16:54:56 -0800 | [diff] [blame] | 206 | int synchronizeKernelRCU() { |
| 207 | // This is a temporary hack for network stats map swap on devices running |
| 208 | // 4.9 kernels. The kernel code of socket release on pf_key socket will |
| 209 | // explicitly call synchronize_rcu() which is exactly what we need. |
| 210 | int pfSocket = socket(AF_KEY, SOCK_RAW | SOCK_CLOEXEC, PF_KEY_V2); |
| 211 | |
| 212 | if (pfSocket < 0) { |
| 213 | int ret = -errno; |
| 214 | ALOGE("create PF_KEY socket failed: %s", strerror(errno)); |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | // When closing socket, synchronize_rcu() gets called in sock_release(). |
| 219 | if (close(pfSocket)) { |
| 220 | int ret = -errno; |
| 221 | ALOGE("failed to close the PF_KEY socket: %s", strerror(errno)); |
| 222 | return ret; |
| 223 | } |
| 224 | return 0; |
| 225 | } |
| 226 | |
Chenbo Feng | 0a1a9a1 | 2019-04-09 12:05:04 -0700 | [diff] [blame] | 227 | int setrlimitForTest() { |
| 228 | // Set the memory rlimit for the test process if the default MEMLOCK rlimit is not enough. |
| 229 | struct rlimit limit = { |
| 230 | .rlim_cur = TEST_LIMIT, |
| 231 | .rlim_max = TEST_LIMIT, |
| 232 | }; |
| 233 | int res = setrlimit(RLIMIT_MEMLOCK, &limit); |
| 234 | if (res) { |
| 235 | ALOGE("Failed to set the default MEMLOCK rlimit: %s", strerror(errno)); |
| 236 | } |
| 237 | return res; |
| 238 | } |
| 239 | |
Chenbo Feng | 79b7e61 | 2018-12-11 12:24:23 -0800 | [diff] [blame] | 240 | std::string BpfLevelToString(BpfLevel bpfLevel) { |
| 241 | switch (bpfLevel) { |
| 242 | case BpfLevel::NONE: return "NONE_SUPPORT"; |
| 243 | case BpfLevel::BASIC: return "BPF_LEVEL_BASIC"; |
| 244 | case BpfLevel::EXTENDED: return "BPF_LEVEL_EXTENDED"; |
| 245 | // No default statement. We want to see errors of the form: |
| 246 | // "enumeration value 'BPF_LEVEL_xxx' not handled in switch [-Werror,-Wswitch]". |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | BpfLevel getBpfSupportLevel() { |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 251 | struct utsname buf; |
| 252 | int kernel_version_major; |
| 253 | int kernel_version_minor; |
| 254 | |
Maciej Żenczykowski | 1b5491d | 2019-11-15 00:54:05 +0000 | [diff] [blame] | 255 | uint64_t api_level = GetUintProperty<uint64_t>("ro.product.first_api_level", 0); |
| 256 | if (api_level == 0) { |
| 257 | ALOGE("Cannot determine initial API level of the device"); |
| 258 | api_level = GetUintProperty<uint64_t>("ro.build.version.sdk", 0); |
| 259 | } |
| 260 | |
| 261 | // Check if the device is shipped originally with android P. |
| 262 | if (api_level < MINIMUM_API_REQUIRED) return BpfLevel::NONE; |
| 263 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 264 | int ret = uname(&buf); |
Maciej Żenczykowski | 1b5491d | 2019-11-15 00:54:05 +0000 | [diff] [blame] | 265 | if (ret) { |
| 266 | return BpfLevel::NONE; |
| 267 | } |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 268 | char dummy; |
| 269 | ret = sscanf(buf.release, "%d.%d%c", &kernel_version_major, &kernel_version_minor, &dummy); |
Maciej Żenczykowski | 1b5491d | 2019-11-15 00:54:05 +0000 | [diff] [blame] | 270 | // Check the device kernel version |
Chenbo Feng | 79b7e61 | 2018-12-11 12:24:23 -0800 | [diff] [blame] | 271 | if (ret < 2) return BpfLevel::NONE; |
Maciej Żenczykowski | 1b5491d | 2019-11-15 00:54:05 +0000 | [diff] [blame] | 272 | if (kernel_version_major > 4 || (kernel_version_major == 4 && kernel_version_minor >= 14)) |
Chenbo Feng | 79b7e61 | 2018-12-11 12:24:23 -0800 | [diff] [blame] | 273 | return BpfLevel::EXTENDED; |
Maciej Żenczykowski | 1b5491d | 2019-11-15 00:54:05 +0000 | [diff] [blame] | 274 | if (kernel_version_major == 4 && kernel_version_minor >= 9) return BpfLevel::BASIC; |
Chenbo Feng | 79b7e61 | 2018-12-11 12:24:23 -0800 | [diff] [blame] | 275 | |
Maciej Żenczykowski | 1b5491d | 2019-11-15 00:54:05 +0000 | [diff] [blame] | 276 | return BpfLevel::NONE; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 279 | } // namespace bpf |
| 280 | } // namespace android |