Brenden Blanco | 246b942 | 2015-06-05 11:15:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 PLUMgrid, Inc. |
| 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 | */ |
Nan Xiao | 667988c | 2017-08-28 11:44:19 +0800 | [diff] [blame] | 16 | #ifndef _GNU_SOURCE |
Colin Ian King | a12db19 | 2017-07-06 13:58:17 +0100 | [diff] [blame] | 17 | #define _GNU_SOURCE |
Nan Xiao | 667988c | 2017-08-28 11:44:19 +0800 | [diff] [blame] | 18 | #endif |
Brenden Blanco | 246b942 | 2015-06-05 11:15:27 -0700 | [diff] [blame] | 19 | |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 20 | #include <arpa/inet.h> |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 21 | #include <errno.h> |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
Teng Qin | 01b07ba | 2017-11-20 13:28:03 -0800 | [diff] [blame] | 23 | #include <inttypes.h> |
Brenden Blanco | 3069caa | 2016-08-01 18:12:11 -0700 | [diff] [blame] | 24 | #include <limits.h> |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 25 | #include <linux/bpf.h> |
Brenden Blanco | a934c90 | 2017-05-31 08:44:22 -0700 | [diff] [blame] | 26 | #include <linux/bpf_common.h> |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 27 | #include <linux/if_packet.h> |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 28 | #include <linux/perf_event.h> |
Brenden Blanco | a934c90 | 2017-05-31 08:44:22 -0700 | [diff] [blame] | 29 | #include <linux/pkt_cls.h> |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 30 | #include <linux/rtnetlink.h> |
Brenden Blanco | a934c90 | 2017-05-31 08:44:22 -0700 | [diff] [blame] | 31 | #include <linux/sched.h> |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 32 | #include <linux/unistd.h> |
| 33 | #include <linux/version.h> |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 34 | #include <net/ethernet.h> |
| 35 | #include <net/if.h> |
Brenden Blanco | fa07345 | 2017-05-30 17:35:53 -0700 | [diff] [blame] | 36 | #include <sched.h> |
Brenden Blanco | a934c90 | 2017-05-31 08:44:22 -0700 | [diff] [blame] | 37 | #include <stdbool.h> |
Brenden Blanco | bb7200c | 2015-06-04 18:01:42 -0700 | [diff] [blame] | 38 | #include <stdio.h> |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <sys/ioctl.h> |
Brenden Blanco | 4b4bd27 | 2015-11-30 10:54:47 -0800 | [diff] [blame] | 42 | #include <sys/resource.h> |
Derek | 35c2501 | 2017-01-22 20:58:23 -0800 | [diff] [blame] | 43 | #include <sys/stat.h> |
| 44 | #include <sys/types.h> |
Brenden Blanco | a934c90 | 2017-05-31 08:44:22 -0700 | [diff] [blame] | 45 | #include <unistd.h> |
Alexei Starovoitov | b1df37c | 2017-09-06 19:47:47 -0700 | [diff] [blame] | 46 | #include <linux/if_alg.h> |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 47 | |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 48 | #include "libbpf.h" |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 49 | #include "perf_reader.h" |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 50 | |
Brenden Blanco | f275d3d | 2015-07-06 23:41:23 -0700 | [diff] [blame] | 51 | // TODO: remove these defines when linux-libc-dev exports them properly |
| 52 | |
| 53 | #ifndef __NR_bpf |
Naveen N. Rao | 0006ad1 | 2016-04-29 16:42:58 +0530 | [diff] [blame] | 54 | #if defined(__powerpc64__) |
| 55 | #define __NR_bpf 361 |
Zvonko Kosic | 98121a3 | 2017-03-07 07:30:25 +0100 | [diff] [blame] | 56 | #elif defined(__s390x__) |
| 57 | #define __NR_bpf 351 |
Zhiyi Sun | 8e434b7 | 2016-12-06 16:21:37 +0800 | [diff] [blame] | 58 | #elif defined(__aarch64__) |
| 59 | #define __NR_bpf 280 |
Naveen N. Rao | 0006ad1 | 2016-04-29 16:42:58 +0530 | [diff] [blame] | 60 | #else |
Brenden Blanco | f275d3d | 2015-07-06 23:41:23 -0700 | [diff] [blame] | 61 | #define __NR_bpf 321 |
| 62 | #endif |
Naveen N. Rao | 0006ad1 | 2016-04-29 16:42:58 +0530 | [diff] [blame] | 63 | #endif |
Brenden Blanco | f275d3d | 2015-07-06 23:41:23 -0700 | [diff] [blame] | 64 | |
| 65 | #ifndef SO_ATTACH_BPF |
| 66 | #define SO_ATTACH_BPF 50 |
| 67 | #endif |
| 68 | |
| 69 | #ifndef PERF_EVENT_IOC_SET_BPF |
| 70 | #define PERF_EVENT_IOC_SET_BPF _IOW('$', 8, __u32) |
| 71 | #endif |
| 72 | |
| 73 | #ifndef PERF_FLAG_FD_CLOEXEC |
| 74 | #define PERF_FLAG_FD_CLOEXEC (1UL << 3) |
| 75 | #endif |
| 76 | |
Martin KaFai Lau | df36816 | 2017-10-19 12:46:48 -0700 | [diff] [blame] | 77 | #define min(x, y) ((x) < (y) ? (x) : (y)) |
| 78 | |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 79 | static int probe_perf_reader_page_cnt = 8; |
| 80 | |
Brenden Blanco | fa07345 | 2017-05-30 17:35:53 -0700 | [diff] [blame] | 81 | static uint64_t ptr_to_u64(void *ptr) |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 82 | { |
Brenden Blanco | fa07345 | 2017-05-30 17:35:53 -0700 | [diff] [blame] | 83 | return (uint64_t) (unsigned long) ptr; |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Martin KaFai Lau | df36816 | 2017-10-19 12:46:48 -0700 | [diff] [blame] | 86 | int bpf_create_map(enum bpf_map_type map_type, const char *name, |
| 87 | int key_size, int value_size, |
| 88 | int max_entries, int map_flags) |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 89 | { |
Martin KaFai Lau | df36816 | 2017-10-19 12:46:48 -0700 | [diff] [blame] | 90 | size_t name_len = name ? strlen(name) : 0; |
Brenden Blanco | fdc027c | 2015-09-03 11:49:54 -0700 | [diff] [blame] | 91 | union bpf_attr attr; |
| 92 | memset(&attr, 0, sizeof(attr)); |
| 93 | attr.map_type = map_type; |
| 94 | attr.key_size = key_size; |
| 95 | attr.value_size = value_size; |
| 96 | attr.max_entries = max_entries; |
Huapeng Zhou | de11d07 | 2016-12-06 18:10:38 -0800 | [diff] [blame] | 97 | attr.map_flags = map_flags; |
Martin KaFai Lau | df36816 | 2017-10-19 12:46:48 -0700 | [diff] [blame] | 98 | memcpy(attr.map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1)); |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 99 | |
Brenden Blanco | 4b4bd27 | 2015-11-30 10:54:47 -0800 | [diff] [blame] | 100 | int ret = syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); |
Martin KaFai Lau | df36816 | 2017-10-19 12:46:48 -0700 | [diff] [blame] | 101 | |
| 102 | if (ret < 0 && name_len && (errno == E2BIG || errno == EINVAL)) { |
| 103 | memset(attr.map_name, 0, BPF_OBJ_NAME_LEN); |
| 104 | ret = syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); |
| 105 | } |
| 106 | |
Brenden Blanco | 4b4bd27 | 2015-11-30 10:54:47 -0800 | [diff] [blame] | 107 | if (ret < 0 && errno == EPERM) { |
| 108 | // see note below about the rationale for this retry |
| 109 | |
| 110 | struct rlimit rl = {}; |
| 111 | if (getrlimit(RLIMIT_MEMLOCK, &rl) == 0) { |
| 112 | rl.rlim_max = RLIM_INFINITY; |
| 113 | rl.rlim_cur = rl.rlim_max; |
| 114 | if (setrlimit(RLIMIT_MEMLOCK, &rl) == 0) |
| 115 | ret = syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); |
| 116 | } |
| 117 | } |
| 118 | return ret; |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags) |
| 122 | { |
Brenden Blanco | fdc027c | 2015-09-03 11:49:54 -0700 | [diff] [blame] | 123 | union bpf_attr attr; |
| 124 | memset(&attr, 0, sizeof(attr)); |
| 125 | attr.map_fd = fd; |
| 126 | attr.key = ptr_to_u64(key); |
| 127 | attr.value = ptr_to_u64(value); |
| 128 | attr.flags = flags; |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 129 | |
| 130 | return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)); |
| 131 | } |
| 132 | |
| 133 | int bpf_lookup_elem(int fd, void *key, void *value) |
| 134 | { |
Brenden Blanco | fdc027c | 2015-09-03 11:49:54 -0700 | [diff] [blame] | 135 | union bpf_attr attr; |
| 136 | memset(&attr, 0, sizeof(attr)); |
| 137 | attr.map_fd = fd; |
| 138 | attr.key = ptr_to_u64(key); |
| 139 | attr.value = ptr_to_u64(value); |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 140 | |
| 141 | return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)); |
| 142 | } |
| 143 | |
| 144 | int bpf_delete_elem(int fd, void *key) |
| 145 | { |
Brenden Blanco | fdc027c | 2015-09-03 11:49:54 -0700 | [diff] [blame] | 146 | union bpf_attr attr; |
| 147 | memset(&attr, 0, sizeof(attr)); |
| 148 | attr.map_fd = fd; |
| 149 | attr.key = ptr_to_u64(key); |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 150 | |
| 151 | return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr)); |
| 152 | } |
| 153 | |
Teng Qin | db7fab5 | 2017-05-16 01:10:15 -0700 | [diff] [blame] | 154 | int bpf_get_first_key(int fd, void *key, size_t key_size) |
| 155 | { |
| 156 | union bpf_attr attr; |
| 157 | int i, res; |
| 158 | |
| 159 | memset(&attr, 0, sizeof(attr)); |
| 160 | attr.map_fd = fd; |
| 161 | attr.key = 0; |
| 162 | attr.next_key = ptr_to_u64(key); |
| 163 | |
| 164 | // 4.12 and above kernel supports passing NULL to BPF_MAP_GET_NEXT_KEY |
| 165 | // to get first key of the map. For older kernels, the call will fail. |
| 166 | res = syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr)); |
| 167 | if (res < 0 && errno == EFAULT) { |
| 168 | // Fall back to try to find a non-existing key. |
| 169 | static unsigned char try_values[3] = {0, 0xff, 0x55}; |
| 170 | attr.key = ptr_to_u64(key); |
| 171 | for (i = 0; i < 3; i++) { |
| 172 | memset(key, try_values[i], key_size); |
| 173 | // We want to check the existence of the key but we don't know the size |
| 174 | // of map's value. So we pass an invalid pointer for value, expect |
| 175 | // the call to fail and check if the error is ENOENT indicating the |
| 176 | // key doesn't exist. If we use NULL for the invalid pointer, it might |
Edward Betts | fdf9b08 | 2017-10-10 21:13:28 +0100 | [diff] [blame] | 177 | // trigger a page fault in kernel and affect performance. Hence we use |
Teng Qin | db7fab5 | 2017-05-16 01:10:15 -0700 | [diff] [blame] | 178 | // ~0 which will fail and return fast. |
| 179 | // This should fail since we pass an invalid pointer for value. |
Teng Qin | 9190ef5 | 2017-05-20 22:46:00 -0700 | [diff] [blame] | 180 | if (bpf_lookup_elem(fd, key, (void *)~0) >= 0) |
Teng Qin | db7fab5 | 2017-05-16 01:10:15 -0700 | [diff] [blame] | 181 | return -1; |
| 182 | // This means the key doesn't exist. |
| 183 | if (errno == ENOENT) |
| 184 | return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr)); |
| 185 | } |
| 186 | return -1; |
| 187 | } else { |
| 188 | return res; |
| 189 | } |
| 190 | } |
| 191 | |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 192 | int bpf_get_next_key(int fd, void *key, void *next_key) |
| 193 | { |
Brenden Blanco | fdc027c | 2015-09-03 11:49:54 -0700 | [diff] [blame] | 194 | union bpf_attr attr; |
| 195 | memset(&attr, 0, sizeof(attr)); |
| 196 | attr.map_fd = fd; |
| 197 | attr.key = ptr_to_u64(key); |
| 198 | attr.next_key = ptr_to_u64(next_key); |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 199 | |
| 200 | return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr)); |
| 201 | } |
| 202 | |
Teng Qin | 797669f | 2017-11-03 16:04:32 -0700 | [diff] [blame] | 203 | static void bpf_print_hints(int ret, char *log) |
Brendan Gregg | 3482637 | 2017-01-13 14:02:02 -0800 | [diff] [blame] | 204 | { |
Teng Qin | 797669f | 2017-11-03 16:04:32 -0700 | [diff] [blame] | 205 | if (ret < 0) |
| 206 | fprintf(stderr, "bpf: Failed to load program: %s\n", strerror(errno)); |
Brendan Gregg | 3482637 | 2017-01-13 14:02:02 -0800 | [diff] [blame] | 207 | if (log == NULL) |
| 208 | return; |
Teng Qin | 797669f | 2017-11-03 16:04:32 -0700 | [diff] [blame] | 209 | else |
| 210 | fprintf(stderr, "%s\n", log); |
| 211 | |
| 212 | if (ret >= 0) |
| 213 | return; |
Brendan Gregg | 3482637 | 2017-01-13 14:02:02 -0800 | [diff] [blame] | 214 | |
| 215 | // The following error strings will need maintenance to match LLVM. |
| 216 | |
| 217 | // stack busting |
| 218 | if (strstr(log, "invalid stack off=-") != NULL) { |
| 219 | fprintf(stderr, "HINT: Looks like you exceeded the BPF stack limit. " |
| 220 | "This can happen if you allocate too much local variable storage. " |
| 221 | "For example, if you allocated a 1 Kbyte struct (maybe for " |
| 222 | "BPF_PERF_OUTPUT), busting a max stack of 512 bytes.\n\n"); |
| 223 | } |
| 224 | |
| 225 | // didn't check NULL on map lookup |
| 226 | if (strstr(log, "invalid mem access 'map_value_or_null'") != NULL) { |
| 227 | fprintf(stderr, "HINT: The 'map_value_or_null' error can happen if " |
| 228 | "you dereference a pointer value from a map lookup without first " |
| 229 | "checking if that pointer is NULL.\n\n"); |
| 230 | } |
| 231 | |
| 232 | // lacking a bpf_probe_read |
| 233 | if (strstr(log, "invalid mem access 'inv'") != NULL) { |
| 234 | fprintf(stderr, "HINT: The invalid mem access 'inv' error can happen " |
| 235 | "if you try to dereference memory without first using " |
| 236 | "bpf_probe_read() to copy it to the BPF stack. Sometimes the " |
| 237 | "bpf_probe_read is automatic by the bcc rewriter, other times " |
| 238 | "you'll need to be explicit.\n\n"); |
| 239 | } |
| 240 | } |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 241 | #define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u)) |
| 242 | |
Martin KaFai Lau | 3c24ad9 | 2017-10-19 23:47:39 -0700 | [diff] [blame] | 243 | int bpf_obj_get_info(int prog_map_fd, void *info, uint32_t *info_len) |
Alexei Starovoitov | b1df37c | 2017-09-06 19:47:47 -0700 | [diff] [blame] | 244 | { |
| 245 | union bpf_attr attr; |
| 246 | int err; |
| 247 | |
| 248 | memset(&attr, 0, sizeof(attr)); |
| 249 | attr.info.bpf_fd = prog_map_fd; |
| 250 | attr.info.info_len = *info_len; |
| 251 | attr.info.info = ptr_to_u64(info); |
| 252 | |
| 253 | err = syscall(__NR_bpf, BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr)); |
| 254 | if (!err) |
| 255 | *info_len = attr.info.info_len; |
| 256 | |
| 257 | return err; |
| 258 | } |
| 259 | |
| 260 | int bpf_prog_compute_tag(const struct bpf_insn *insns, int prog_len, |
| 261 | unsigned long long *ptag) |
| 262 | { |
| 263 | struct sockaddr_alg alg = { |
| 264 | .salg_family = AF_ALG, |
| 265 | .salg_type = "hash", |
| 266 | .salg_name = "sha1", |
| 267 | }; |
| 268 | int shafd = socket(AF_ALG, SOCK_SEQPACKET, 0); |
| 269 | if (shafd < 0) { |
| 270 | fprintf(stderr, "sha1 socket not available %s\n", strerror(errno)); |
| 271 | return -1; |
| 272 | } |
| 273 | int ret = bind(shafd, (struct sockaddr *)&alg, sizeof(alg)); |
| 274 | if (ret < 0) { |
| 275 | fprintf(stderr, "sha1 bind fail %s\n", strerror(errno)); |
| 276 | close(shafd); |
| 277 | return ret; |
| 278 | } |
| 279 | int shafd2 = accept(shafd, NULL, 0); |
| 280 | if (shafd2 < 0) { |
| 281 | fprintf(stderr, "sha1 accept fail %s\n", strerror(errno)); |
| 282 | close(shafd); |
| 283 | return -1; |
| 284 | } |
| 285 | struct bpf_insn prog[prog_len / 8]; |
| 286 | bool map_ld_seen = false; |
| 287 | int i; |
| 288 | for (i = 0; i < prog_len / 8; i++) { |
| 289 | prog[i] = insns[i]; |
| 290 | if (insns[i].code == (BPF_LD | BPF_DW | BPF_IMM) && |
| 291 | insns[i].src_reg == BPF_PSEUDO_MAP_FD && |
| 292 | !map_ld_seen) { |
| 293 | prog[i].imm = 0; |
| 294 | map_ld_seen = true; |
| 295 | } else if (insns[i].code == 0 && map_ld_seen) { |
| 296 | prog[i].imm = 0; |
| 297 | map_ld_seen = false; |
| 298 | } else { |
| 299 | map_ld_seen = false; |
| 300 | } |
| 301 | } |
| 302 | ret = write(shafd2, prog, prog_len); |
| 303 | if (ret != prog_len) { |
| 304 | fprintf(stderr, "sha1 write fail %s\n", strerror(errno)); |
| 305 | close(shafd2); |
| 306 | close(shafd); |
| 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | union { |
| 311 | unsigned char sha[20]; |
| 312 | unsigned long long tag; |
| 313 | } u = {}; |
| 314 | ret = read(shafd2, u.sha, 20); |
| 315 | if (ret != 20) { |
| 316 | fprintf(stderr, "sha1 read fail %s\n", strerror(errno)); |
| 317 | close(shafd2); |
| 318 | close(shafd); |
| 319 | return -1; |
| 320 | } |
| 321 | *ptag = __builtin_bswap64(u.tag); |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | int bpf_prog_get_tag(int fd, unsigned long long *ptag) |
| 326 | { |
| 327 | char fmt[64]; |
| 328 | snprintf(fmt, sizeof(fmt), "/proc/self/fdinfo/%d", fd); |
| 329 | FILE * f = fopen(fmt, "r"); |
| 330 | if (!f) { |
| 331 | /* fprintf(stderr, "failed to open fdinfo %s\n", strerror(errno));*/ |
| 332 | return -1; |
| 333 | } |
| 334 | fgets(fmt, sizeof(fmt), f); // pos |
| 335 | fgets(fmt, sizeof(fmt), f); // flags |
| 336 | fgets(fmt, sizeof(fmt), f); // mnt_id |
| 337 | fgets(fmt, sizeof(fmt), f); // prog_type |
| 338 | fgets(fmt, sizeof(fmt), f); // prog_jited |
| 339 | fgets(fmt, sizeof(fmt), f); // prog_tag |
| 340 | fclose(f); |
| 341 | char *p = strchr(fmt, ':'); |
| 342 | if (!p) { |
| 343 | /* fprintf(stderr, "broken fdinfo %s\n", fmt);*/ |
| 344 | return -2; |
| 345 | } |
| 346 | unsigned long long tag = 0; |
| 347 | sscanf(p + 1, "%llx", &tag); |
| 348 | *ptag = tag; |
| 349 | return 0; |
| 350 | } |
| 351 | |
Martin KaFai Lau | df36816 | 2017-10-19 12:46:48 -0700 | [diff] [blame] | 352 | int bpf_prog_load(enum bpf_prog_type prog_type, const char *name, |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 353 | const struct bpf_insn *insns, int prog_len, |
Brenden Blanco | 759029f | 2015-07-29 15:47:51 -0700 | [diff] [blame] | 354 | const char *license, unsigned kern_version, |
Teng Qin | 797669f | 2017-11-03 16:04:32 -0700 | [diff] [blame] | 355 | int log_level, char *log_buf, unsigned log_buf_size) |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 356 | { |
Martin KaFai Lau | df36816 | 2017-10-19 12:46:48 -0700 | [diff] [blame] | 357 | size_t name_len = name ? strlen(name) : 0; |
Brenden Blanco | fdc027c | 2015-09-03 11:49:54 -0700 | [diff] [blame] | 358 | union bpf_attr attr; |
Teng Qin | 797669f | 2017-11-03 16:04:32 -0700 | [diff] [blame] | 359 | char *tmp_log_buf = NULL; |
| 360 | unsigned tmp_log_buf_size = 0; |
davidefdl | 2dece10 | 2016-09-12 12:00:37 -0700 | [diff] [blame] | 361 | int ret = 0; |
| 362 | |
Brenden Blanco | fdc027c | 2015-09-03 11:49:54 -0700 | [diff] [blame] | 363 | memset(&attr, 0, sizeof(attr)); |
Teng Qin | 797669f | 2017-11-03 16:04:32 -0700 | [diff] [blame] | 364 | |
Brenden Blanco | fdc027c | 2015-09-03 11:49:54 -0700 | [diff] [blame] | 365 | attr.prog_type = prog_type; |
Brenden Blanco | 7009b55 | 2015-05-26 11:48:17 -0700 | [diff] [blame] | 366 | attr.kern_version = kern_version; |
Teng Qin | 797669f | 2017-11-03 16:04:32 -0700 | [diff] [blame] | 367 | attr.license = ptr_to_u64((void *)license); |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 368 | |
Teng Qin | 797669f | 2017-11-03 16:04:32 -0700 | [diff] [blame] | 369 | attr.insns = ptr_to_u64((void *)insns); |
| 370 | attr.insn_cnt = prog_len / sizeof(struct bpf_insn); |
davidefdl | 2dece10 | 2016-09-12 12:00:37 -0700 | [diff] [blame] | 371 | if (attr.insn_cnt > BPF_MAXINSNS) { |
davidefdl | 2dece10 | 2016-09-12 12:00:37 -0700 | [diff] [blame] | 372 | errno = EINVAL; |
| 373 | fprintf(stderr, |
Martin KaFai Lau | df36816 | 2017-10-19 12:46:48 -0700 | [diff] [blame] | 374 | "bpf: %s. Program too large (%u insns), at most %d insns\n\n", |
davidefdl | 2dece10 | 2016-09-12 12:00:37 -0700 | [diff] [blame] | 375 | strerror(errno), attr.insn_cnt, BPF_MAXINSNS); |
Teng Qin | 797669f | 2017-11-03 16:04:32 -0700 | [diff] [blame] | 376 | return -1; |
davidefdl | 2dece10 | 2016-09-12 12:00:37 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Teng Qin | 797669f | 2017-11-03 16:04:32 -0700 | [diff] [blame] | 379 | attr.log_level = log_level; |
| 380 | if (attr.log_level > 0) { |
| 381 | if (log_buf_size > 0) { |
| 382 | // Use user-provided log buffer if availiable. |
| 383 | log_buf[0] = 0; |
| 384 | attr.log_buf = ptr_to_u64(log_buf); |
| 385 | attr.log_size = log_buf_size; |
| 386 | } else { |
| 387 | // Create and use temporary log buffer if user didn't provide one. |
| 388 | tmp_log_buf_size = LOG_BUF_SIZE; |
| 389 | tmp_log_buf = malloc(tmp_log_buf_size); |
| 390 | if (!tmp_log_buf) { |
| 391 | fprintf(stderr, "bpf: Failed to allocate temporary log buffer: %s\n\n", |
| 392 | strerror(errno)); |
| 393 | attr.log_level = 0; |
| 394 | } else { |
| 395 | tmp_log_buf[0] = 0; |
| 396 | attr.log_buf = ptr_to_u64(tmp_log_buf); |
| 397 | attr.log_size = tmp_log_buf_size; |
| 398 | } |
| 399 | } |
| 400 | } |
Huapeng Zhou | de11d07 | 2016-12-06 18:10:38 -0800 | [diff] [blame] | 401 | |
Teng Qin | 797669f | 2017-11-03 16:04:32 -0700 | [diff] [blame] | 402 | memcpy(attr.prog_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1)); |
| 403 | |
| 404 | ret = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); |
| 405 | // BPF object name is not supported on older Kernels. |
| 406 | // If we failed due to this, clear the name and try again. |
Martin KaFai Lau | df36816 | 2017-10-19 12:46:48 -0700 | [diff] [blame] | 407 | if (ret < 0 && name_len && (errno == E2BIG || errno == EINVAL)) { |
| 408 | memset(attr.prog_name, 0, BPF_OBJ_NAME_LEN); |
| 409 | ret = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); |
| 410 | } |
| 411 | |
Brenden Blanco | 4b4bd27 | 2015-11-30 10:54:47 -0800 | [diff] [blame] | 412 | if (ret < 0 && errno == EPERM) { |
| 413 | // When EPERM is returned, two reasons are possible: |
| 414 | // 1. user has no permissions for bpf() |
| 415 | // 2. user has insufficent rlimit for locked memory |
| 416 | // Unfortunately, there is no api to inspect the current usage of locked |
| 417 | // mem for the user, so an accurate calculation of how much memory to lock |
| 418 | // for this new program is difficult to calculate. As a hack, bump the limit |
| 419 | // to unlimited. If program load fails again, return the error. |
Brenden Blanco | 4b4bd27 | 2015-11-30 10:54:47 -0800 | [diff] [blame] | 420 | struct rlimit rl = {}; |
| 421 | if (getrlimit(RLIMIT_MEMLOCK, &rl) == 0) { |
| 422 | rl.rlim_max = RLIM_INFINITY; |
| 423 | rl.rlim_cur = rl.rlim_max; |
| 424 | if (setrlimit(RLIMIT_MEMLOCK, &rl) == 0) |
| 425 | ret = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); |
| 426 | } |
| 427 | } |
| 428 | |
Teng Qin | 797669f | 2017-11-03 16:04:32 -0700 | [diff] [blame] | 429 | // The load has failed. Handle log message. |
| 430 | if (ret < 0) { |
| 431 | // User has provided a log buffer. |
| 432 | if (log_buf_size) { |
| 433 | // If logging is not already enabled, enable it and do the syscall again. |
| 434 | if (attr.log_level == 0) { |
| 435 | attr.log_level = 1; |
| 436 | attr.log_buf = ptr_to_u64(log_buf); |
| 437 | attr.log_size = log_buf_size; |
| 438 | ret = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); |
| 439 | } |
| 440 | // Print the log message and return. |
| 441 | bpf_print_hints(ret, log_buf); |
| 442 | if (errno == ENOSPC) |
| 443 | fprintf(stderr, "bpf: log_buf size may be insufficient\n"); |
| 444 | goto return_result; |
davidefdl | 2dece10 | 2016-09-12 12:00:37 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Teng Qin | 797669f | 2017-11-03 16:04:32 -0700 | [diff] [blame] | 447 | // User did not provide log buffer. We will try to increase size of |
| 448 | // our temporary log buffer to get full error message. |
| 449 | if (tmp_log_buf) |
| 450 | free(tmp_log_buf); |
| 451 | tmp_log_buf_size = LOG_BUF_SIZE; |
Yonghong Song | e86b017 | 2017-11-14 10:20:07 -0800 | [diff] [blame] | 452 | if (attr.log_level == 0) |
| 453 | attr.log_level = 1; |
Teng Qin | 797669f | 2017-11-03 16:04:32 -0700 | [diff] [blame] | 454 | for (;;) { |
| 455 | tmp_log_buf = malloc(tmp_log_buf_size); |
| 456 | if (!tmp_log_buf) { |
| 457 | fprintf(stderr, "bpf: Failed to allocate temporary log buffer: %s\n\n", |
| 458 | strerror(errno)); |
| 459 | goto return_result; |
| 460 | } |
| 461 | tmp_log_buf[0] = 0; |
| 462 | attr.log_buf = ptr_to_u64(tmp_log_buf); |
| 463 | attr.log_size = tmp_log_buf_size; |
davidefdl | 2dece10 | 2016-09-12 12:00:37 -0700 | [diff] [blame] | 464 | |
Teng Qin | 797669f | 2017-11-03 16:04:32 -0700 | [diff] [blame] | 465 | ret = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); |
| 466 | if (ret < 0 && errno == ENOSPC) { |
| 467 | // Temporary buffer size is not enough. Double it and try again. |
| 468 | free(tmp_log_buf); |
| 469 | tmp_log_buf = NULL; |
| 470 | tmp_log_buf_size <<= 1; |
| 471 | } else { |
| 472 | break; |
| 473 | } |
| 474 | } |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 475 | } |
Teng Qin | 797669f | 2017-11-03 16:04:32 -0700 | [diff] [blame] | 476 | |
| 477 | // If log_level is not 0, either speficied by user or set due to error, |
| 478 | // print the log message. |
| 479 | if (attr.log_level > 0) { |
| 480 | if (log_buf) |
| 481 | bpf_print_hints(ret, log_buf); |
| 482 | else if (tmp_log_buf) |
| 483 | bpf_print_hints(ret, tmp_log_buf); |
| 484 | } |
| 485 | |
| 486 | return_result: |
| 487 | if (tmp_log_buf) |
| 488 | free(tmp_log_buf); |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 489 | return ret; |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | int bpf_open_raw_sock(const char *name) |
| 493 | { |
| 494 | struct sockaddr_ll sll; |
| 495 | int sock; |
| 496 | |
| 497 | sock = socket(PF_PACKET, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, htons(ETH_P_ALL)); |
| 498 | if (sock < 0) { |
Prashant Bhole | 5b08d38 | 2017-10-06 15:05:04 +0900 | [diff] [blame] | 499 | fprintf(stderr, "cannot create raw socket\n"); |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 500 | return -1; |
| 501 | } |
| 502 | |
Olivier Tilmans | 830d58d | 2017-11-21 23:26:27 +0100 | [diff] [blame^] | 503 | /* Do not bind on empty interface names */ |
| 504 | if (!name || *name == '\0') |
| 505 | return sock; |
| 506 | |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 507 | memset(&sll, 0, sizeof(sll)); |
| 508 | sll.sll_family = AF_PACKET; |
| 509 | sll.sll_ifindex = if_nametoindex(name); |
Prashant Bhole | e9fa659 | 2017-10-06 14:45:21 +0900 | [diff] [blame] | 510 | if (sll.sll_ifindex == 0) { |
| 511 | fprintf(stderr, "bpf: Resolving device name to index: %s\n", strerror(errno)); |
| 512 | close(sock); |
| 513 | return -1; |
| 514 | } |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 515 | sll.sll_protocol = htons(ETH_P_ALL); |
| 516 | if (bind(sock, (struct sockaddr *)&sll, sizeof(sll)) < 0) { |
Prashant Bhole | 5b08d38 | 2017-10-06 15:05:04 +0900 | [diff] [blame] | 517 | fprintf(stderr, "bind to %s: %s\n", name, strerror(errno)); |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 518 | close(sock); |
| 519 | return -1; |
| 520 | } |
| 521 | |
| 522 | return sock; |
| 523 | } |
| 524 | |
| 525 | int bpf_attach_socket(int sock, int prog) { |
Brenden Blanco | af95673 | 2015-06-09 13:58:42 -0700 | [diff] [blame] | 526 | return setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog, sizeof(prog)); |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 529 | static int bpf_attach_tracing_event(int progfd, const char *event_path, |
| 530 | struct perf_reader *reader, int pid, int cpu, int group_fd) { |
Martin KaFai Lau | a8e6696 | 2016-05-19 23:04:41 -0700 | [diff] [blame] | 531 | int efd, pfd; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 532 | ssize_t bytes; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 533 | char buf[256]; |
| 534 | struct perf_event_attr attr = {}; |
| 535 | |
| 536 | snprintf(buf, sizeof(buf), "%s/id", event_path); |
| 537 | efd = open(buf, O_RDONLY, 0); |
| 538 | if (efd < 0) { |
| 539 | fprintf(stderr, "open(%s): %s\n", buf, strerror(errno)); |
Martin KaFai Lau | a8e6696 | 2016-05-19 23:04:41 -0700 | [diff] [blame] | 540 | return -1; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | bytes = read(efd, buf, sizeof(buf)); |
| 544 | if (bytes <= 0 || bytes >= sizeof(buf)) { |
| 545 | fprintf(stderr, "read(%s): %s\n", buf, strerror(errno)); |
Martin KaFai Lau | a8e6696 | 2016-05-19 23:04:41 -0700 | [diff] [blame] | 546 | close(efd); |
| 547 | return -1; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 548 | } |
Martin KaFai Lau | a8e6696 | 2016-05-19 23:04:41 -0700 | [diff] [blame] | 549 | close(efd); |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 550 | buf[bytes] = '\0'; |
| 551 | attr.config = strtol(buf, NULL, 0); |
| 552 | attr.type = PERF_TYPE_TRACEPOINT; |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 553 | attr.sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_CALLCHAIN; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 554 | attr.sample_period = 1; |
| 555 | attr.wakeup_events = 1; |
| 556 | pfd = syscall(__NR_perf_event_open, &attr, pid, cpu, group_fd, PERF_FLAG_FD_CLOEXEC); |
| 557 | if (pfd < 0) { |
Brenden Blanco | 7468195 | 2016-01-28 14:18:46 -0800 | [diff] [blame] | 558 | fprintf(stderr, "perf_event_open(%s/id): %s\n", event_path, strerror(errno)); |
Martin KaFai Lau | a8e6696 | 2016-05-19 23:04:41 -0700 | [diff] [blame] | 559 | return -1; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 560 | } |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 561 | perf_reader_set_fd(reader, pfd); |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 562 | |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 563 | if (perf_reader_mmap(reader, attr.type, attr.sample_type) < 0) |
Martin KaFai Lau | a8e6696 | 2016-05-19 23:04:41 -0700 | [diff] [blame] | 564 | return -1; |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 565 | |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 566 | if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, progfd) < 0) { |
| 567 | perror("ioctl(PERF_EVENT_IOC_SET_BPF)"); |
Martin KaFai Lau | a8e6696 | 2016-05-19 23:04:41 -0700 | [diff] [blame] | 568 | return -1; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 569 | } |
| 570 | if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) { |
| 571 | perror("ioctl(PERF_EVENT_IOC_ENABLE)"); |
Martin KaFai Lau | a8e6696 | 2016-05-19 23:04:41 -0700 | [diff] [blame] | 572 | return -1; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame] | 575 | return 0; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Derek | 7174d93 | 2017-01-30 21:03:02 -0800 | [diff] [blame] | 578 | void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type, const char *ev_name, |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 579 | const char *fn_name, |
| 580 | pid_t pid, int cpu, int group_fd, |
Mauricio Vasquez B | d1324ac | 2017-05-17 20:26:47 -0500 | [diff] [blame] | 581 | perf_reader_cb cb, void *cb_cookie) |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 582 | { |
Martin KaFai Lau | a8e6696 | 2016-05-19 23:04:41 -0700 | [diff] [blame] | 583 | int kfd; |
Derek | 7174d93 | 2017-01-30 21:03:02 -0800 | [diff] [blame] | 584 | char buf[256]; |
Teng Qin | f4543fc | 2017-09-03 17:11:59 -0700 | [diff] [blame] | 585 | char event_alias[128]; |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 586 | struct perf_reader *reader = NULL; |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 587 | static char *event_type = "kprobe"; |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 588 | |
Teng Qin | 4b764de | 2017-04-03 22:10:46 -0700 | [diff] [blame] | 589 | reader = perf_reader_new(cb, NULL, NULL, cb_cookie, probe_perf_reader_page_cnt); |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 590 | if (!reader) |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame] | 591 | goto error; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 592 | |
Brenden Blanco | 68e2d14 | 2016-01-28 10:24:56 -0800 | [diff] [blame] | 593 | snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/%s_events", event_type); |
| 594 | kfd = open(buf, O_WRONLY | O_APPEND, 0); |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 595 | if (kfd < 0) { |
Brenden Blanco | 68e2d14 | 2016-01-28 10:24:56 -0800 | [diff] [blame] | 596 | fprintf(stderr, "open(%s): %s\n", buf, strerror(errno)); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame] | 597 | goto error; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Teng Qin | f4543fc | 2017-09-03 17:11:59 -0700 | [diff] [blame] | 600 | snprintf(event_alias, sizeof(event_alias), "%s_bcc_%d", ev_name, getpid()); |
Mauricio Vasquez B | d1324ac | 2017-05-17 20:26:47 -0500 | [diff] [blame] | 601 | snprintf(buf, sizeof(buf), "%c:%ss/%s %s", attach_type==BPF_PROBE_ENTRY ? 'p' : 'r', |
Teng Qin | f4543fc | 2017-09-03 17:11:59 -0700 | [diff] [blame] | 602 | event_type, event_alias, fn_name); |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 603 | if (write(kfd, buf, strlen(buf)) < 0) { |
Brenden Blanco | 7e71aef | 2015-09-09 18:28:21 -0700 | [diff] [blame] | 604 | if (errno == EINVAL) |
| 605 | fprintf(stderr, "check dmesg output for possible cause\n"); |
Martin KaFai Lau | a8e6696 | 2016-05-19 23:04:41 -0700 | [diff] [blame] | 606 | close(kfd); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame] | 607 | goto error; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 608 | } |
Martin KaFai Lau | a8e6696 | 2016-05-19 23:04:41 -0700 | [diff] [blame] | 609 | close(kfd); |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 610 | |
Teng Qin | f4543fc | 2017-09-03 17:11:59 -0700 | [diff] [blame] | 611 | snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/%ss/%s", event_type, event_alias); |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 612 | if (bpf_attach_tracing_event(progfd, buf, reader, pid, cpu, group_fd) < 0) |
| 613 | goto error; |
Teng Qin | f4543fc | 2017-09-03 17:11:59 -0700 | [diff] [blame] | 614 | |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 615 | return reader; |
| 616 | |
| 617 | error: |
| 618 | perf_reader_free(reader); |
| 619 | return NULL; |
| 620 | |
| 621 | } |
| 622 | |
Brenden Blanco | fa07345 | 2017-05-30 17:35:53 -0700 | [diff] [blame] | 623 | static int enter_mount_ns(int pid) { |
| 624 | struct stat self_stat, target_stat; |
| 625 | int self_fd = -1, target_fd = -1; |
| 626 | char buf[64]; |
| 627 | |
| 628 | if (pid < 0) |
| 629 | return -1; |
| 630 | |
| 631 | if ((size_t)snprintf(buf, sizeof(buf), "/proc/%d/ns/mnt", pid) >= sizeof(buf)) |
| 632 | return -1; |
| 633 | |
| 634 | self_fd = open("/proc/self/ns/mnt", O_RDONLY); |
| 635 | if (self_fd < 0) { |
| 636 | perror("open(/proc/self/ns/mnt)"); |
| 637 | return -1; |
| 638 | } |
| 639 | |
| 640 | target_fd = open(buf, O_RDONLY); |
| 641 | if (target_fd < 0) { |
| 642 | perror("open(/proc/<pid>/ns/mnt)"); |
| 643 | goto error; |
| 644 | } |
| 645 | |
| 646 | if (fstat(self_fd, &self_stat)) { |
| 647 | perror("fstat(self_fd)"); |
| 648 | goto error; |
| 649 | } |
| 650 | |
| 651 | if (fstat(target_fd, &target_stat)) { |
| 652 | perror("fstat(target_fd)"); |
| 653 | goto error; |
| 654 | } |
| 655 | |
| 656 | // both target and current ns are same, avoid setns and close all fds |
| 657 | if (self_stat.st_ino == target_stat.st_ino) |
| 658 | goto error; |
| 659 | |
| 660 | if (setns(target_fd, CLONE_NEWNS)) { |
| 661 | perror("setns(target)"); |
| 662 | goto error; |
| 663 | } |
| 664 | |
| 665 | close(target_fd); |
| 666 | return self_fd; |
| 667 | |
| 668 | error: |
| 669 | if (self_fd >= 0) |
| 670 | close(self_fd); |
| 671 | if (target_fd >= 0) |
| 672 | close(target_fd); |
| 673 | return -1; |
| 674 | } |
| 675 | |
| 676 | static void exit_mount_ns(int fd) { |
| 677 | if (fd < 0) |
| 678 | return; |
| 679 | |
| 680 | if (setns(fd, CLONE_NEWNS)) |
| 681 | perror("setns"); |
| 682 | } |
| 683 | |
Derek | 7174d93 | 2017-01-30 21:03:02 -0800 | [diff] [blame] | 684 | void * bpf_attach_uprobe(int progfd, enum bpf_probe_attach_type attach_type, const char *ev_name, |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 685 | const char *binary_path, uint64_t offset, |
| 686 | pid_t pid, int cpu, int group_fd, |
Mauricio Vasquez B | d1324ac | 2017-05-17 20:26:47 -0500 | [diff] [blame] | 687 | perf_reader_cb cb, void *cb_cookie) |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 688 | { |
Derek | 7174d93 | 2017-01-30 21:03:02 -0800 | [diff] [blame] | 689 | char buf[PATH_MAX]; |
Teng Qin | 0760b75 | 2017-09-03 19:07:59 -0700 | [diff] [blame] | 690 | char event_alias[PATH_MAX]; |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 691 | struct perf_reader *reader = NULL; |
| 692 | static char *event_type = "uprobe"; |
Teng Qin | 0760b75 | 2017-09-03 19:07:59 -0700 | [diff] [blame] | 693 | int res, kfd = -1, ns_fd = -1; |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 694 | |
Teng Qin | 4b764de | 2017-04-03 22:10:46 -0700 | [diff] [blame] | 695 | reader = perf_reader_new(cb, NULL, NULL, cb_cookie, probe_perf_reader_page_cnt); |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 696 | if (!reader) |
| 697 | goto error; |
| 698 | |
| 699 | snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/%s_events", event_type); |
| 700 | kfd = open(buf, O_WRONLY | O_APPEND, 0); |
| 701 | if (kfd < 0) { |
| 702 | fprintf(stderr, "open(%s): %s\n", buf, strerror(errno)); |
| 703 | goto error; |
| 704 | } |
| 705 | |
Teng Qin | 0760b75 | 2017-09-03 19:07:59 -0700 | [diff] [blame] | 706 | res = snprintf(event_alias, sizeof(event_alias), "%s_bcc_%d", ev_name, getpid()); |
| 707 | if (res < 0 || res >= sizeof(event_alias)) { |
| 708 | fprintf(stderr, "Event name (%s) is too long for buffer\n", ev_name); |
| 709 | goto error; |
| 710 | } |
| 711 | res = snprintf(buf, sizeof(buf), "%c:%ss/%s %s:0x%lx", attach_type==BPF_PROBE_ENTRY ? 'p' : 'r', |
| 712 | event_type, event_alias, binary_path, offset); |
| 713 | if (res < 0 || res >= sizeof(buf)) { |
| 714 | fprintf(stderr, "Event alias (%s) too long for buffer\n", event_alias); |
Derek | 7174d93 | 2017-01-30 21:03:02 -0800 | [diff] [blame] | 715 | goto error; |
| 716 | } |
kmjohansen | 4b87af0 | 2017-03-30 00:58:31 -0700 | [diff] [blame] | 717 | |
Brenden Blanco | fa07345 | 2017-05-30 17:35:53 -0700 | [diff] [blame] | 718 | ns_fd = enter_mount_ns(pid); |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 719 | if (write(kfd, buf, strlen(buf)) < 0) { |
| 720 | if (errno == EINVAL) |
| 721 | fprintf(stderr, "check dmesg output for possible cause\n"); |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 722 | goto error; |
| 723 | } |
| 724 | close(kfd); |
Brenden Blanco | fa07345 | 2017-05-30 17:35:53 -0700 | [diff] [blame] | 725 | exit_mount_ns(ns_fd); |
| 726 | ns_fd = -1; |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 727 | |
Teng Qin | 0760b75 | 2017-09-03 19:07:59 -0700 | [diff] [blame] | 728 | snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/%ss/%s", event_type, event_alias); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame] | 729 | if (bpf_attach_tracing_event(progfd, buf, reader, pid, cpu, group_fd) < 0) |
| 730 | goto error; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 731 | |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 732 | return reader; |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame] | 733 | |
| 734 | error: |
Teng Qin | 0760b75 | 2017-09-03 19:07:59 -0700 | [diff] [blame] | 735 | if (kfd >= 0) |
| 736 | close(kfd); |
Brenden Blanco | fa07345 | 2017-05-30 17:35:53 -0700 | [diff] [blame] | 737 | exit_mount_ns(ns_fd); |
Martin KaFai Lau | a8e6696 | 2016-05-19 23:04:41 -0700 | [diff] [blame] | 738 | perf_reader_free(reader); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame] | 739 | return NULL; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 742 | static int bpf_detach_probe(const char *ev_name, const char *event_type) |
| 743 | { |
Teng Qin | 0760b75 | 2017-09-03 19:07:59 -0700 | [diff] [blame] | 744 | int kfd, res; |
| 745 | char buf[PATH_MAX]; |
Brenden Blanco | 68e2d14 | 2016-01-28 10:24:56 -0800 | [diff] [blame] | 746 | snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/%s_events", event_type); |
| 747 | kfd = open(buf, O_WRONLY | O_APPEND, 0); |
Brenden Blanco | 839dd27 | 2015-06-11 12:35:55 -0700 | [diff] [blame] | 748 | if (kfd < 0) { |
Brenden Blanco | 68e2d14 | 2016-01-28 10:24:56 -0800 | [diff] [blame] | 749 | fprintf(stderr, "open(%s): %s\n", buf, strerror(errno)); |
Teng Qin | 0760b75 | 2017-09-03 19:07:59 -0700 | [diff] [blame] | 750 | goto error; |
Brenden Blanco | 839dd27 | 2015-06-11 12:35:55 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Teng Qin | 0760b75 | 2017-09-03 19:07:59 -0700 | [diff] [blame] | 753 | res = snprintf(buf, sizeof(buf), "-:%ss/%s_bcc_%d", event_type, ev_name, getpid()); |
| 754 | if (res < 0 || res >= sizeof(buf)) { |
| 755 | fprintf(stderr, "snprintf(%s): %d\n", ev_name, res); |
| 756 | goto error; |
| 757 | } |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 758 | if (write(kfd, buf, strlen(buf)) < 0) { |
Brenden Blanco | 68e2d14 | 2016-01-28 10:24:56 -0800 | [diff] [blame] | 759 | fprintf(stderr, "write(%s): %s\n", buf, strerror(errno)); |
Teng Qin | 0760b75 | 2017-09-03 19:07:59 -0700 | [diff] [blame] | 760 | goto error; |
Brenden Blanco | 839dd27 | 2015-06-11 12:35:55 -0700 | [diff] [blame] | 761 | } |
Brenden Blanco | 839dd27 | 2015-06-11 12:35:55 -0700 | [diff] [blame] | 762 | |
Teng Qin | 0760b75 | 2017-09-03 19:07:59 -0700 | [diff] [blame] | 763 | close(kfd); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame] | 764 | return 0; |
Teng Qin | 0760b75 | 2017-09-03 19:07:59 -0700 | [diff] [blame] | 765 | |
| 766 | error: |
| 767 | if (kfd >= 0) |
| 768 | close(kfd); |
| 769 | return -1; |
Brenden Blanco | 839dd27 | 2015-06-11 12:35:55 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 772 | int bpf_detach_kprobe(const char *ev_name) |
| 773 | { |
Teng Qin | f4543fc | 2017-09-03 17:11:59 -0700 | [diff] [blame] | 774 | return bpf_detach_probe(ev_name, "kprobe"); |
Brenden Blanco | 68e2d14 | 2016-01-28 10:24:56 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 777 | int bpf_detach_uprobe(const char *ev_name) |
| 778 | { |
| 779 | return bpf_detach_probe(ev_name, "uprobe"); |
Brenden Blanco | 68e2d14 | 2016-01-28 10:24:56 -0800 | [diff] [blame] | 780 | } |
| 781 | |
Derek | e4da6c2 | 2017-01-28 16:11:28 -0800 | [diff] [blame] | 782 | |
Sasha Goldshtein | 1198c3c | 2016-06-30 06:26:28 -0700 | [diff] [blame] | 783 | void * bpf_attach_tracepoint(int progfd, const char *tp_category, |
| 784 | const char *tp_name, int pid, int cpu, |
| 785 | int group_fd, perf_reader_cb cb, void *cb_cookie) { |
| 786 | char buf[256]; |
| 787 | struct perf_reader *reader = NULL; |
| 788 | |
Teng Qin | 4b764de | 2017-04-03 22:10:46 -0700 | [diff] [blame] | 789 | reader = perf_reader_new(cb, NULL, NULL, cb_cookie, probe_perf_reader_page_cnt); |
Sasha Goldshtein | 1198c3c | 2016-06-30 06:26:28 -0700 | [diff] [blame] | 790 | if (!reader) |
| 791 | goto error; |
| 792 | |
| 793 | snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/%s/%s", |
| 794 | tp_category, tp_name); |
| 795 | if (bpf_attach_tracing_event(progfd, buf, reader, pid, cpu, group_fd) < 0) |
| 796 | goto error; |
| 797 | |
| 798 | return reader; |
| 799 | |
| 800 | error: |
| 801 | perf_reader_free(reader); |
| 802 | return NULL; |
| 803 | } |
| 804 | |
| 805 | int bpf_detach_tracepoint(const char *tp_category, const char *tp_name) { |
| 806 | // Right now, there is nothing to do, but it's a good idea to encourage |
| 807 | // callers to detach anything they attach. |
| 808 | return 0; |
| 809 | } |
| 810 | |
Teng Qin | 4b764de | 2017-04-03 22:10:46 -0700 | [diff] [blame] | 811 | void * bpf_open_perf_buffer(perf_reader_raw_cb raw_cb, |
| 812 | perf_reader_lost_cb lost_cb, void *cb_cookie, |
| 813 | int pid, int cpu, int page_cnt) { |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame] | 814 | int pfd; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 815 | struct perf_event_attr attr = {}; |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame] | 816 | struct perf_reader *reader = NULL; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 817 | |
Teng Qin | 4b764de | 2017-04-03 22:10:46 -0700 | [diff] [blame] | 818 | reader = perf_reader_new(NULL, raw_cb, lost_cb, cb_cookie, page_cnt); |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 819 | if (!reader) |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame] | 820 | goto error; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 821 | |
Brenden Blanco | 0dd2441 | 2016-02-17 00:26:14 -0800 | [diff] [blame] | 822 | attr.config = 10;//PERF_COUNT_SW_BPF_OUTPUT; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 823 | attr.type = PERF_TYPE_SOFTWARE; |
| 824 | attr.sample_type = PERF_SAMPLE_RAW; |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame] | 825 | attr.sample_period = 1; |
| 826 | attr.wakeup_events = 1; |
| 827 | pfd = syscall(__NR_perf_event_open, &attr, pid, cpu, -1, PERF_FLAG_FD_CLOEXEC); |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 828 | if (pfd < 0) { |
Brenden Blanco | 0dd2441 | 2016-02-17 00:26:14 -0800 | [diff] [blame] | 829 | fprintf(stderr, "perf_event_open: %s\n", strerror(errno)); |
| 830 | fprintf(stderr, " (check your kernel for PERF_COUNT_SW_BPF_OUTPUT support, 4.4 or newer)\n"); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame] | 831 | goto error; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 832 | } |
| 833 | perf_reader_set_fd(reader, pfd); |
| 834 | |
| 835 | if (perf_reader_mmap(reader, attr.type, attr.sample_type) < 0) |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame] | 836 | goto error; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 837 | |
| 838 | if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) { |
| 839 | perror("ioctl(PERF_EVENT_IOC_ENABLE)"); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame] | 840 | goto error; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | return reader; |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame] | 844 | |
| 845 | error: |
| 846 | if (reader) |
| 847 | perf_reader_free(reader); |
| 848 | |
| 849 | return NULL; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 850 | } |
Jan RĂ¼th | e0724d7 | 2016-07-28 22:32:46 +0200 | [diff] [blame] | 851 | |
Brenden Blanco | fa07345 | 2017-05-30 17:35:53 -0700 | [diff] [blame] | 852 | static int invalid_perf_config(uint32_t type, uint64_t config) { |
Teng Qin | 9875221 | 2017-05-19 19:05:24 -0700 | [diff] [blame] | 853 | switch (type) { |
Teng Qin | 01b07ba | 2017-11-20 13:28:03 -0800 | [diff] [blame] | 854 | case PERF_TYPE_HARDWARE: |
| 855 | if (config >= PERF_COUNT_HW_MAX) { |
| 856 | fprintf(stderr, "HARDWARE perf event config out of range\n"); |
| 857 | goto is_invalid; |
| 858 | } |
| 859 | return 0; |
| 860 | case PERF_TYPE_SOFTWARE: |
| 861 | if (config >= PERF_COUNT_SW_MAX) { |
| 862 | fprintf(stderr, "SOFTWARE perf event config out of range\n"); |
| 863 | goto is_invalid; |
| 864 | } else if (config == 10 /* PERF_COUNT_SW_BPF_OUTPUT */) { |
| 865 | fprintf(stderr, "Unable to open or attach perf event for BPF_OUTPUT\n"); |
| 866 | goto is_invalid; |
| 867 | } |
| 868 | return 0; |
| 869 | case PERF_TYPE_HW_CACHE: |
| 870 | if (((config >> 16) >= PERF_COUNT_HW_CACHE_RESULT_MAX) || |
| 871 | (((config >> 8) & 0xff) >= PERF_COUNT_HW_CACHE_OP_MAX) || |
| 872 | ((config & 0xff) >= PERF_COUNT_HW_CACHE_MAX)) { |
| 873 | fprintf(stderr, "HW_CACHE perf event config out of range\n"); |
| 874 | goto is_invalid; |
| 875 | } |
| 876 | return 0; |
| 877 | case PERF_TYPE_TRACEPOINT: |
| 878 | case PERF_TYPE_BREAKPOINT: |
| 879 | fprintf(stderr, |
| 880 | "Unable to open or attach TRACEPOINT or BREAKPOINT events\n"); |
| 881 | goto is_invalid; |
| 882 | default: |
| 883 | return 0; |
Teng Qin | 9875221 | 2017-05-19 19:05:24 -0700 | [diff] [blame] | 884 | } |
Teng Qin | 01b07ba | 2017-11-20 13:28:03 -0800 | [diff] [blame] | 885 | is_invalid: |
| 886 | fprintf(stderr, "Invalid perf event type %" PRIu32 " config %" PRIu64 "\n", |
| 887 | type, config); |
| 888 | return 1; |
Teng Qin | 9875221 | 2017-05-19 19:05:24 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Brenden Blanco | 3069caa | 2016-08-01 18:12:11 -0700 | [diff] [blame] | 891 | int bpf_open_perf_event(uint32_t type, uint64_t config, int pid, int cpu) { |
| 892 | int fd; |
| 893 | struct perf_event_attr attr = {}; |
Jan RĂ¼th | e0724d7 | 2016-07-28 22:32:46 +0200 | [diff] [blame] | 894 | |
Teng Qin | 9875221 | 2017-05-19 19:05:24 -0700 | [diff] [blame] | 895 | if (invalid_perf_config(type, config)) { |
Teng Qin | 9875221 | 2017-05-19 19:05:24 -0700 | [diff] [blame] | 896 | return -1; |
| 897 | } |
| 898 | |
Brenden Blanco | 3069caa | 2016-08-01 18:12:11 -0700 | [diff] [blame] | 899 | attr.sample_period = LONG_MAX; |
| 900 | attr.type = type; |
| 901 | attr.config = config; |
| 902 | |
| 903 | fd = syscall(__NR_perf_event_open, &attr, pid, cpu, -1, PERF_FLAG_FD_CLOEXEC); |
| 904 | if (fd < 0) { |
| 905 | fprintf(stderr, "perf_event_open: %s\n", strerror(errno)); |
| 906 | return -1; |
| 907 | } |
| 908 | |
| 909 | if (ioctl(fd, PERF_EVENT_IOC_ENABLE, 0) < 0) { |
| 910 | perror("ioctl(PERF_EVENT_IOC_ENABLE)"); |
| 911 | close(fd); |
| 912 | return -1; |
| 913 | } |
| 914 | |
| 915 | return fd; |
| 916 | } |
Jan RĂ¼th | e0724d7 | 2016-07-28 22:32:46 +0200 | [diff] [blame] | 917 | |
Andy Gospodarek | 9f3cab7 | 2017-05-17 16:18:45 -0400 | [diff] [blame] | 918 | int bpf_attach_xdp(const char *dev_name, int progfd, uint32_t flags) { |
Jan RĂ¼th | e0724d7 | 2016-07-28 22:32:46 +0200 | [diff] [blame] | 919 | struct sockaddr_nl sa; |
| 920 | int sock, seq = 0, len, ret = -1; |
| 921 | char buf[4096]; |
| 922 | struct nlattr *nla, *nla_xdp; |
| 923 | struct { |
| 924 | struct nlmsghdr nh; |
| 925 | struct ifinfomsg ifinfo; |
| 926 | char attrbuf[64]; |
| 927 | } req; |
| 928 | struct nlmsghdr *nh; |
| 929 | struct nlmsgerr *err; |
Toshiaki Makita | bb9b92a | 2017-07-31 20:20:55 +0900 | [diff] [blame] | 930 | socklen_t addrlen; |
Jan RĂ¼th | e0724d7 | 2016-07-28 22:32:46 +0200 | [diff] [blame] | 931 | |
| 932 | memset(&sa, 0, sizeof(sa)); |
| 933 | sa.nl_family = AF_NETLINK; |
| 934 | |
| 935 | sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |
| 936 | if (sock < 0) { |
| 937 | fprintf(stderr, "bpf: opening a netlink socket: %s\n", strerror(errno)); |
| 938 | return -1; |
| 939 | } |
| 940 | |
| 941 | if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) { |
| 942 | fprintf(stderr, "bpf: bind to netlink: %s\n", strerror(errno)); |
| 943 | goto cleanup; |
| 944 | } |
| 945 | |
Toshiaki Makita | bb9b92a | 2017-07-31 20:20:55 +0900 | [diff] [blame] | 946 | addrlen = sizeof(sa); |
| 947 | if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) { |
| 948 | fprintf(stderr, "bpf: get sock name of netlink: %s\n", strerror(errno)); |
| 949 | goto cleanup; |
| 950 | } |
| 951 | |
| 952 | if (addrlen != sizeof(sa)) { |
| 953 | fprintf(stderr, "bpf: wrong netlink address length: %d\n", addrlen); |
| 954 | goto cleanup; |
| 955 | } |
| 956 | |
Jan RĂ¼th | e0724d7 | 2016-07-28 22:32:46 +0200 | [diff] [blame] | 957 | memset(&req, 0, sizeof(req)); |
| 958 | req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); |
| 959 | req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; |
| 960 | req.nh.nlmsg_type = RTM_SETLINK; |
| 961 | req.nh.nlmsg_pid = 0; |
| 962 | req.nh.nlmsg_seq = ++seq; |
| 963 | req.ifinfo.ifi_family = AF_UNSPEC; |
| 964 | req.ifinfo.ifi_index = if_nametoindex(dev_name); |
| 965 | if (req.ifinfo.ifi_index == 0) { |
| 966 | fprintf(stderr, "bpf: Resolving device name to index: %s\n", strerror(errno)); |
| 967 | goto cleanup; |
| 968 | } |
| 969 | |
| 970 | nla = (struct nlattr *)(((char *)&req) |
| 971 | + NLMSG_ALIGN(req.nh.nlmsg_len)); |
| 972 | nla->nla_type = NLA_F_NESTED | 43/*IFLA_XDP*/; |
| 973 | |
| 974 | nla_xdp = (struct nlattr *)((char *)nla + NLA_HDRLEN); |
Andy Gospodarek | 9f3cab7 | 2017-05-17 16:18:45 -0400 | [diff] [blame] | 975 | nla->nla_len = NLA_HDRLEN; |
Jan RĂ¼th | e0724d7 | 2016-07-28 22:32:46 +0200 | [diff] [blame] | 976 | |
| 977 | // we specify the FD passed over by the user |
| 978 | nla_xdp->nla_type = 1/*IFLA_XDP_FD*/; |
Arthur Gautier | fbd91e2 | 2017-04-28 21:39:58 +0000 | [diff] [blame] | 979 | nla_xdp->nla_len = NLA_HDRLEN + sizeof(progfd); |
Jan RĂ¼th | e0724d7 | 2016-07-28 22:32:46 +0200 | [diff] [blame] | 980 | memcpy((char *)nla_xdp + NLA_HDRLEN, &progfd, sizeof(progfd)); |
Andy Gospodarek | 9f3cab7 | 2017-05-17 16:18:45 -0400 | [diff] [blame] | 981 | nla->nla_len += nla_xdp->nla_len; |
| 982 | |
| 983 | // parse flags as passed by the user |
| 984 | if (flags) { |
| 985 | nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len); |
Gary Lin | db8353b | 2017-08-18 18:10:43 +0800 | [diff] [blame] | 986 | nla_xdp->nla_type = 3/*IFLA_XDP_FLAGS*/; |
Andy Gospodarek | 9f3cab7 | 2017-05-17 16:18:45 -0400 | [diff] [blame] | 987 | nla_xdp->nla_len = NLA_HDRLEN + sizeof(flags); |
| 988 | memcpy((char *)nla_xdp + NLA_HDRLEN, &flags, sizeof(flags)); |
| 989 | nla->nla_len += nla_xdp->nla_len; |
| 990 | } |
Jan RĂ¼th | e0724d7 | 2016-07-28 22:32:46 +0200 | [diff] [blame] | 991 | |
| 992 | req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len); |
| 993 | |
| 994 | if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) { |
| 995 | fprintf(stderr, "bpf: send to netlink: %s\n", strerror(errno)); |
| 996 | goto cleanup; |
| 997 | } |
| 998 | |
| 999 | len = recv(sock, buf, sizeof(buf), 0); |
| 1000 | if (len < 0) { |
| 1001 | fprintf(stderr, "bpf: recv from netlink: %s\n", strerror(errno)); |
| 1002 | goto cleanup; |
| 1003 | } |
| 1004 | |
| 1005 | for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); |
| 1006 | nh = NLMSG_NEXT(nh, len)) { |
Toshiaki Makita | bb9b92a | 2017-07-31 20:20:55 +0900 | [diff] [blame] | 1007 | if (nh->nlmsg_pid != sa.nl_pid) { |
Toshiaki Makita | 890c76a | 2017-07-31 20:20:55 +0900 | [diff] [blame] | 1008 | fprintf(stderr, "bpf: Wrong pid %u, expected %u\n", |
Toshiaki Makita | bb9b92a | 2017-07-31 20:20:55 +0900 | [diff] [blame] | 1009 | nh->nlmsg_pid, sa.nl_pid); |
Jan RĂ¼th | e0724d7 | 2016-07-28 22:32:46 +0200 | [diff] [blame] | 1010 | errno = EBADMSG; |
| 1011 | goto cleanup; |
| 1012 | } |
| 1013 | if (nh->nlmsg_seq != seq) { |
| 1014 | fprintf(stderr, "bpf: Wrong seq %d, expected %d\n", |
| 1015 | nh->nlmsg_seq, seq); |
| 1016 | errno = EBADMSG; |
| 1017 | goto cleanup; |
| 1018 | } |
| 1019 | switch (nh->nlmsg_type) { |
| 1020 | case NLMSG_ERROR: |
| 1021 | err = (struct nlmsgerr *)NLMSG_DATA(nh); |
| 1022 | if (!err->error) |
| 1023 | continue; |
| 1024 | fprintf(stderr, "bpf: nlmsg error %s\n", strerror(-err->error)); |
| 1025 | errno = -err->error; |
| 1026 | goto cleanup; |
| 1027 | case NLMSG_DONE: |
| 1028 | break; |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | ret = 0; |
| 1033 | |
| 1034 | cleanup: |
| 1035 | close(sock); |
| 1036 | return ret; |
| 1037 | } |
Teng Qin | 206b020 | 2016-10-18 16:06:57 -0700 | [diff] [blame] | 1038 | |
| 1039 | int bpf_attach_perf_event(int progfd, uint32_t ev_type, uint32_t ev_config, |
| 1040 | uint64_t sample_period, uint64_t sample_freq, |
| 1041 | pid_t pid, int cpu, int group_fd) { |
Teng Qin | 9875221 | 2017-05-19 19:05:24 -0700 | [diff] [blame] | 1042 | if (invalid_perf_config(ev_type, ev_config)) { |
Teng Qin | 206b020 | 2016-10-18 16:06:57 -0700 | [diff] [blame] | 1043 | return -1; |
| 1044 | } |
| 1045 | if (!((sample_period > 0) ^ (sample_freq > 0))) { |
| 1046 | fprintf( |
| 1047 | stderr, "Exactly one of sample_period / sample_freq should be set\n" |
| 1048 | ); |
| 1049 | return -1; |
| 1050 | } |
| 1051 | |
| 1052 | struct perf_event_attr attr = {}; |
| 1053 | attr.type = ev_type; |
| 1054 | attr.config = ev_config; |
| 1055 | attr.inherit = 1; |
| 1056 | if (sample_freq > 0) { |
| 1057 | attr.freq = 1; |
| 1058 | attr.sample_freq = sample_freq; |
| 1059 | } else { |
| 1060 | attr.sample_period = sample_period; |
| 1061 | } |
| 1062 | |
| 1063 | int fd = syscall( |
| 1064 | __NR_perf_event_open, &attr, pid, cpu, group_fd, PERF_FLAG_FD_CLOEXEC |
| 1065 | ); |
| 1066 | if (fd < 0) { |
| 1067 | perror("perf_event_open failed"); |
| 1068 | return -1; |
| 1069 | } |
| 1070 | if (ioctl(fd, PERF_EVENT_IOC_SET_BPF, progfd) != 0) { |
| 1071 | perror("ioctl(PERF_EVENT_IOC_SET_BPF) failed"); |
| 1072 | close(fd); |
| 1073 | return -1; |
| 1074 | } |
| 1075 | if (ioctl(fd, PERF_EVENT_IOC_ENABLE, 0) != 0) { |
| 1076 | perror("ioctl(PERF_EVENT_IOC_ENABLE) failed"); |
| 1077 | close(fd); |
| 1078 | return -1; |
| 1079 | } |
| 1080 | |
| 1081 | return fd; |
| 1082 | } |
| 1083 | |
Teng Qin | d682733 | 2017-05-23 16:35:11 -0700 | [diff] [blame] | 1084 | int bpf_close_perf_event_fd(int fd) { |
| 1085 | int res, error = 0; |
| 1086 | if (fd >= 0) { |
| 1087 | res = ioctl(fd, PERF_EVENT_IOC_DISABLE, 0); |
| 1088 | if (res != 0) { |
| 1089 | perror("ioctl(PERF_EVENT_IOC_DISABLE) failed"); |
| 1090 | error = res; |
| 1091 | } |
| 1092 | res = close(fd); |
| 1093 | if (res != 0) { |
| 1094 | perror("close perf event FD failed"); |
| 1095 | error = (res && !error) ? res : error; |
| 1096 | } |
| 1097 | } |
| 1098 | return error; |
Teng Qin | 206b020 | 2016-10-18 16:06:57 -0700 | [diff] [blame] | 1099 | } |
Huapeng Zhou | 37dcac0 | 2016-12-20 13:42:01 -0800 | [diff] [blame] | 1100 | |
| 1101 | int bpf_obj_pin(int fd, const char *pathname) |
| 1102 | { |
Brenden Blanco | fa07345 | 2017-05-30 17:35:53 -0700 | [diff] [blame] | 1103 | union bpf_attr attr; |
| 1104 | |
| 1105 | memset(&attr, 0, sizeof(attr)); |
| 1106 | attr.pathname = ptr_to_u64((void *)pathname); |
| 1107 | attr.bpf_fd = fd; |
Huapeng Zhou | 37dcac0 | 2016-12-20 13:42:01 -0800 | [diff] [blame] | 1108 | |
| 1109 | return syscall(__NR_bpf, BPF_OBJ_PIN, &attr, sizeof(attr)); |
| 1110 | } |
| 1111 | |
| 1112 | int bpf_obj_get(const char *pathname) |
| 1113 | { |
Brenden Blanco | fa07345 | 2017-05-30 17:35:53 -0700 | [diff] [blame] | 1114 | union bpf_attr attr; |
| 1115 | |
| 1116 | memset(&attr, 0, sizeof(attr)); |
| 1117 | attr.pathname = ptr_to_u64((void *)pathname); |
Huapeng Zhou | 37dcac0 | 2016-12-20 13:42:01 -0800 | [diff] [blame] | 1118 | |
| 1119 | return syscall(__NR_bpf, BPF_OBJ_GET, &attr, sizeof(attr)); |
| 1120 | } |
Martin KaFai Lau | 3c24ad9 | 2017-10-19 23:47:39 -0700 | [diff] [blame] | 1121 | |
| 1122 | int bpf_prog_get_next_id(uint32_t start_id, uint32_t *next_id) |
| 1123 | { |
| 1124 | union bpf_attr attr; |
| 1125 | int err; |
| 1126 | |
| 1127 | memset(&attr, 0, sizeof(attr)); |
| 1128 | attr.start_id = start_id; |
| 1129 | |
| 1130 | err = syscall(__NR_bpf, BPF_PROG_GET_NEXT_ID, &attr, sizeof(attr)); |
| 1131 | if (!err) |
| 1132 | *next_id = attr.next_id; |
| 1133 | |
| 1134 | return err; |
| 1135 | } |
| 1136 | |
| 1137 | int bpf_prog_get_fd_by_id(uint32_t id) |
| 1138 | { |
| 1139 | union bpf_attr attr; |
| 1140 | |
| 1141 | memset(&attr, 0, sizeof(attr)); |
| 1142 | attr.prog_id = id; |
| 1143 | |
| 1144 | return syscall(__NR_bpf, BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr)); |
| 1145 | } |
| 1146 | |
| 1147 | int bpf_map_get_fd_by_id(uint32_t id) |
| 1148 | { |
| 1149 | union bpf_attr attr; |
| 1150 | |
| 1151 | memset(&attr, 0, sizeof(attr)); |
| 1152 | attr.map_id = id; |
| 1153 | |
| 1154 | return syscall(__NR_bpf, BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr)); |
| 1155 | } |