Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <sys/types.h> |
| 3 | #include <sys/stat.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <libelf.h> |
| 6 | #include <gelf.h> |
| 7 | #include <errno.h> |
| 8 | #include <unistd.h> |
| 9 | #include <string.h> |
| 10 | #include <stdbool.h> |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 11 | #include <stdlib.h> |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 12 | #include <linux/bpf.h> |
| 13 | #include <linux/filter.h> |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 14 | #include <linux/perf_event.h> |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 15 | #include <linux/netlink.h> |
| 16 | #include <linux/rtnetlink.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/socket.h> |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 19 | #include <sys/syscall.h> |
| 20 | #include <sys/ioctl.h> |
| 21 | #include <sys/mman.h> |
| 22 | #include <poll.h> |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 23 | #include <ctype.h> |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 24 | #include "libbpf.h" |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 25 | #include "bpf_load.h" |
Joe Stringer | 205c8ad | 2016-12-08 18:46:19 -0800 | [diff] [blame] | 26 | #include "perf-sys.h" |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 27 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 28 | #define DEBUGFS "/sys/kernel/debug/tracing/" |
| 29 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 30 | static char license[128]; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 31 | static int kern_version; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 32 | static bool processed_sec[128]; |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 33 | char bpf_log_buf[BPF_LOG_BUF_SIZE]; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 34 | int map_fd[MAX_MAPS]; |
| 35 | int prog_fd[MAX_PROGS]; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 36 | int event_fd[MAX_PROGS]; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 37 | int prog_cnt; |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 38 | int prog_array_fd = -1; |
| 39 | |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 40 | struct bpf_map_def { |
| 41 | unsigned int type; |
| 42 | unsigned int key_size; |
| 43 | unsigned int value_size; |
| 44 | unsigned int max_entries; |
| 45 | unsigned int map_flags; |
| 46 | }; |
| 47 | |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 48 | static int populate_prog_array(const char *event, int prog_fd) |
| 49 | { |
| 50 | int ind = atoi(event), err; |
| 51 | |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 52 | err = bpf_map_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY); |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 53 | if (err < 0) { |
| 54 | printf("failed to store prog_fd in prog_array\n"); |
| 55 | return -1; |
| 56 | } |
| 57 | return 0; |
| 58 | } |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 59 | |
| 60 | static int load_and_attach(const char *event, struct bpf_insn *prog, int size) |
| 61 | { |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 62 | bool is_socket = strncmp(event, "socket", 6) == 0; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 63 | bool is_kprobe = strncmp(event, "kprobe/", 7) == 0; |
| 64 | bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0; |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 65 | bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 66 | bool is_xdp = strncmp(event, "xdp", 3) == 0; |
Alexei Starovoitov | 1c47910e | 2016-09-01 18:37:25 -0700 | [diff] [blame] | 67 | bool is_perf_event = strncmp(event, "perf_event", 10) == 0; |
David Ahern | 4f2e7ae | 2016-12-01 08:48:07 -0800 | [diff] [blame] | 68 | bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0; |
| 69 | bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0; |
Joe Stringer | 43371c8 | 2016-12-14 14:43:39 -0800 | [diff] [blame] | 70 | size_t insns_cnt = size / sizeof(struct bpf_insn); |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 71 | enum bpf_prog_type prog_type; |
| 72 | char buf[256]; |
| 73 | int fd, efd, err, id; |
| 74 | struct perf_event_attr attr = {}; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 75 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 76 | attr.type = PERF_TYPE_TRACEPOINT; |
| 77 | attr.sample_type = PERF_SAMPLE_RAW; |
| 78 | attr.sample_period = 1; |
| 79 | attr.wakeup_events = 1; |
| 80 | |
| 81 | if (is_socket) { |
| 82 | prog_type = BPF_PROG_TYPE_SOCKET_FILTER; |
| 83 | } else if (is_kprobe || is_kretprobe) { |
| 84 | prog_type = BPF_PROG_TYPE_KPROBE; |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 85 | } else if (is_tracepoint) { |
| 86 | prog_type = BPF_PROG_TYPE_TRACEPOINT; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 87 | } else if (is_xdp) { |
| 88 | prog_type = BPF_PROG_TYPE_XDP; |
Alexei Starovoitov | 1c47910e | 2016-09-01 18:37:25 -0700 | [diff] [blame] | 89 | } else if (is_perf_event) { |
| 90 | prog_type = BPF_PROG_TYPE_PERF_EVENT; |
David Ahern | 4f2e7ae | 2016-12-01 08:48:07 -0800 | [diff] [blame] | 91 | } else if (is_cgroup_skb) { |
| 92 | prog_type = BPF_PROG_TYPE_CGROUP_SKB; |
| 93 | } else if (is_cgroup_sk) { |
| 94 | prog_type = BPF_PROG_TYPE_CGROUP_SOCK; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 95 | } else { |
| 96 | printf("Unknown event '%s'\n", event); |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 97 | return -1; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 98 | } |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 99 | |
Joe Stringer | 43371c8 | 2016-12-14 14:43:39 -0800 | [diff] [blame] | 100 | fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version, |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 101 | bpf_log_buf, BPF_LOG_BUF_SIZE); |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 102 | if (fd < 0) { |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 103 | printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf); |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | prog_fd[prog_cnt++] = fd; |
| 108 | |
David Ahern | 4f2e7ae | 2016-12-01 08:48:07 -0800 | [diff] [blame] | 109 | if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk) |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 110 | return 0; |
| 111 | |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 112 | if (is_socket) { |
| 113 | event += 6; |
| 114 | if (*event != '/') |
| 115 | return 0; |
| 116 | event++; |
| 117 | if (!isdigit(*event)) { |
| 118 | printf("invalid prog number\n"); |
| 119 | return -1; |
| 120 | } |
| 121 | return populate_prog_array(event, fd); |
| 122 | } |
| 123 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 124 | if (is_kprobe || is_kretprobe) { |
| 125 | if (is_kprobe) |
| 126 | event += 7; |
| 127 | else |
| 128 | event += 10; |
| 129 | |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 130 | if (*event == 0) { |
| 131 | printf("event name cannot be empty\n"); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | if (isdigit(*event)) |
| 136 | return populate_prog_array(event, fd); |
| 137 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 138 | snprintf(buf, sizeof(buf), |
| 139 | "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events", |
| 140 | is_kprobe ? 'p' : 'r', event, event); |
| 141 | err = system(buf); |
| 142 | if (err < 0) { |
| 143 | printf("failed to create kprobe '%s' error '%s'\n", |
| 144 | event, strerror(errno)); |
| 145 | return -1; |
| 146 | } |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 147 | |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 148 | strcpy(buf, DEBUGFS); |
| 149 | strcat(buf, "events/kprobes/"); |
| 150 | strcat(buf, event); |
| 151 | strcat(buf, "/id"); |
| 152 | } else if (is_tracepoint) { |
| 153 | event += 11; |
| 154 | |
| 155 | if (*event == 0) { |
| 156 | printf("event name cannot be empty\n"); |
| 157 | return -1; |
| 158 | } |
| 159 | strcpy(buf, DEBUGFS); |
| 160 | strcat(buf, "events/"); |
| 161 | strcat(buf, event); |
| 162 | strcat(buf, "/id"); |
| 163 | } |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 164 | |
| 165 | efd = open(buf, O_RDONLY, 0); |
| 166 | if (efd < 0) { |
| 167 | printf("failed to open event %s\n", event); |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | err = read(efd, buf, sizeof(buf)); |
| 172 | if (err < 0 || err >= sizeof(buf)) { |
| 173 | printf("read from '%s' failed '%s'\n", event, strerror(errno)); |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | close(efd); |
| 178 | |
| 179 | buf[err] = 0; |
| 180 | id = atoi(buf); |
| 181 | attr.config = id; |
| 182 | |
Joe Stringer | 205c8ad | 2016-12-08 18:46:19 -0800 | [diff] [blame] | 183 | efd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0); |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 184 | if (efd < 0) { |
| 185 | printf("event %d fd %d err %s\n", id, efd, strerror(errno)); |
| 186 | return -1; |
| 187 | } |
| 188 | event_fd[prog_cnt - 1] = efd; |
| 189 | ioctl(efd, PERF_EVENT_IOC_ENABLE, 0); |
| 190 | ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd); |
| 191 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static int load_maps(struct bpf_map_def *maps, int len) |
| 196 | { |
| 197 | int i; |
| 198 | |
| 199 | for (i = 0; i < len / sizeof(struct bpf_map_def); i++) { |
| 200 | |
| 201 | map_fd[i] = bpf_create_map(maps[i].type, |
| 202 | maps[i].key_size, |
| 203 | maps[i].value_size, |
Alexei Starovoitov | 89b9760 | 2016-03-07 21:57:20 -0800 | [diff] [blame] | 204 | maps[i].max_entries, |
| 205 | maps[i].map_flags); |
Alexei Starovoitov | 618ec9a7 | 2016-03-07 21:57:18 -0800 | [diff] [blame] | 206 | if (map_fd[i] < 0) { |
| 207 | printf("failed to create a map: %d %s\n", |
| 208 | errno, strerror(errno)); |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 209 | return 1; |
Alexei Starovoitov | 618ec9a7 | 2016-03-07 21:57:18 -0800 | [diff] [blame] | 210 | } |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 211 | |
| 212 | if (maps[i].type == BPF_MAP_TYPE_PROG_ARRAY) |
| 213 | prog_array_fd = map_fd[i]; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 214 | } |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname, |
| 219 | GElf_Shdr *shdr, Elf_Data **data) |
| 220 | { |
| 221 | Elf_Scn *scn; |
| 222 | |
| 223 | scn = elf_getscn(elf, i); |
| 224 | if (!scn) |
| 225 | return 1; |
| 226 | |
| 227 | if (gelf_getshdr(scn, shdr) != shdr) |
| 228 | return 2; |
| 229 | |
| 230 | *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name); |
| 231 | if (!*shname || !shdr->sh_size) |
| 232 | return 3; |
| 233 | |
| 234 | *data = elf_getdata(scn, 0); |
| 235 | if (!*data || elf_getdata(scn, *data) != NULL) |
| 236 | return 4; |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols, |
| 242 | GElf_Shdr *shdr, struct bpf_insn *insn) |
| 243 | { |
| 244 | int i, nrels; |
| 245 | |
| 246 | nrels = shdr->sh_size / shdr->sh_entsize; |
| 247 | |
| 248 | for (i = 0; i < nrels; i++) { |
| 249 | GElf_Sym sym; |
| 250 | GElf_Rel rel; |
| 251 | unsigned int insn_idx; |
| 252 | |
| 253 | gelf_getrel(data, i, &rel); |
| 254 | |
| 255 | insn_idx = rel.r_offset / sizeof(struct bpf_insn); |
| 256 | |
| 257 | gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym); |
| 258 | |
| 259 | if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) { |
| 260 | printf("invalid relo for insn[%d].code 0x%x\n", |
| 261 | insn_idx, insn[insn_idx].code); |
| 262 | return 1; |
| 263 | } |
| 264 | insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD; |
| 265 | insn[insn_idx].imm = map_fd[sym.st_value / sizeof(struct bpf_map_def)]; |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | int load_bpf_file(char *path) |
| 272 | { |
| 273 | int fd, i; |
| 274 | Elf *elf; |
| 275 | GElf_Ehdr ehdr; |
| 276 | GElf_Shdr shdr, shdr_prog; |
| 277 | Elf_Data *data, *data_prog, *symbols = NULL; |
| 278 | char *shname, *shname_prog; |
| 279 | |
Mickaël Salaün | a734fb5 | 2017-02-08 21:27:43 +0100 | [diff] [blame^] | 280 | /* reset global variables */ |
| 281 | kern_version = 0; |
| 282 | memset(license, 0, sizeof(license)); |
| 283 | memset(processed_sec, 0, sizeof(processed_sec)); |
| 284 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 285 | if (elf_version(EV_CURRENT) == EV_NONE) |
| 286 | return 1; |
| 287 | |
| 288 | fd = open(path, O_RDONLY, 0); |
| 289 | if (fd < 0) |
| 290 | return 1; |
| 291 | |
| 292 | elf = elf_begin(fd, ELF_C_READ, NULL); |
| 293 | |
| 294 | if (!elf) |
| 295 | return 1; |
| 296 | |
| 297 | if (gelf_getehdr(elf, &ehdr) != &ehdr) |
| 298 | return 1; |
| 299 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 300 | /* clear all kprobes */ |
| 301 | i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events"); |
| 302 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 303 | /* scan over all elf sections to get license and map info */ |
| 304 | for (i = 1; i < ehdr.e_shnum; i++) { |
| 305 | |
| 306 | if (get_sec(elf, i, &ehdr, &shname, &shdr, &data)) |
| 307 | continue; |
| 308 | |
| 309 | if (0) /* helpful for llvm debugging */ |
| 310 | printf("section %d:%s data %p size %zd link %d flags %d\n", |
| 311 | i, shname, data->d_buf, data->d_size, |
| 312 | shdr.sh_link, (int) shdr.sh_flags); |
| 313 | |
| 314 | if (strcmp(shname, "license") == 0) { |
| 315 | processed_sec[i] = true; |
| 316 | memcpy(license, data->d_buf, data->d_size); |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 317 | } else if (strcmp(shname, "version") == 0) { |
| 318 | processed_sec[i] = true; |
| 319 | if (data->d_size != sizeof(int)) { |
| 320 | printf("invalid size of version section %zd\n", |
| 321 | data->d_size); |
| 322 | return 1; |
| 323 | } |
| 324 | memcpy(&kern_version, data->d_buf, sizeof(int)); |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 325 | } else if (strcmp(shname, "maps") == 0) { |
| 326 | processed_sec[i] = true; |
| 327 | if (load_maps(data->d_buf, data->d_size)) |
| 328 | return 1; |
| 329 | } else if (shdr.sh_type == SHT_SYMTAB) { |
| 330 | symbols = data; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /* load programs that need map fixup (relocations) */ |
| 335 | for (i = 1; i < ehdr.e_shnum; i++) { |
Mickaël Salaün | 16ad132 | 2017-02-08 21:27:42 +0100 | [diff] [blame] | 336 | if (processed_sec[i]) |
| 337 | continue; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 338 | |
| 339 | if (get_sec(elf, i, &ehdr, &shname, &shdr, &data)) |
| 340 | continue; |
| 341 | if (shdr.sh_type == SHT_REL) { |
| 342 | struct bpf_insn *insns; |
| 343 | |
| 344 | if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog, |
| 345 | &shdr_prog, &data_prog)) |
| 346 | continue; |
| 347 | |
Alexei Starovoitov | db6a71d | 2016-11-22 16:52:09 -0800 | [diff] [blame] | 348 | if (shdr_prog.sh_type != SHT_PROGBITS || |
| 349 | !(shdr_prog.sh_flags & SHF_EXECINSTR)) |
| 350 | continue; |
| 351 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 352 | insns = (struct bpf_insn *) data_prog->d_buf; |
| 353 | |
| 354 | processed_sec[shdr.sh_info] = true; |
| 355 | processed_sec[i] = true; |
| 356 | |
| 357 | if (parse_relo_and_apply(data, symbols, &shdr, insns)) |
| 358 | continue; |
| 359 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 360 | if (memcmp(shname_prog, "kprobe/", 7) == 0 || |
| 361 | memcmp(shname_prog, "kretprobe/", 10) == 0 || |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 362 | memcmp(shname_prog, "tracepoint/", 11) == 0 || |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 363 | memcmp(shname_prog, "xdp", 3) == 0 || |
Alexei Starovoitov | 1c47910e | 2016-09-01 18:37:25 -0700 | [diff] [blame] | 364 | memcmp(shname_prog, "perf_event", 10) == 0 || |
David Ahern | 4f2e7ae | 2016-12-01 08:48:07 -0800 | [diff] [blame] | 365 | memcmp(shname_prog, "socket", 6) == 0 || |
| 366 | memcmp(shname_prog, "cgroup/", 7) == 0) |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 367 | load_and_attach(shname_prog, insns, data_prog->d_size); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /* load programs that don't use maps */ |
| 372 | for (i = 1; i < ehdr.e_shnum; i++) { |
| 373 | |
| 374 | if (processed_sec[i]) |
| 375 | continue; |
| 376 | |
| 377 | if (get_sec(elf, i, &ehdr, &shname, &shdr, &data)) |
| 378 | continue; |
| 379 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 380 | if (memcmp(shname, "kprobe/", 7) == 0 || |
| 381 | memcmp(shname, "kretprobe/", 10) == 0 || |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 382 | memcmp(shname, "tracepoint/", 11) == 0 || |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 383 | memcmp(shname, "xdp", 3) == 0 || |
Alexei Starovoitov | 1c47910e | 2016-09-01 18:37:25 -0700 | [diff] [blame] | 384 | memcmp(shname, "perf_event", 10) == 0 || |
David Ahern | 4f2e7ae | 2016-12-01 08:48:07 -0800 | [diff] [blame] | 385 | memcmp(shname, "socket", 6) == 0 || |
| 386 | memcmp(shname, "cgroup/", 7) == 0) |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 387 | load_and_attach(shname, data->d_buf, data->d_size); |
| 388 | } |
| 389 | |
| 390 | close(fd); |
| 391 | return 0; |
| 392 | } |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 393 | |
| 394 | void read_trace_pipe(void) |
| 395 | { |
| 396 | int trace_fd; |
| 397 | |
| 398 | trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0); |
| 399 | if (trace_fd < 0) |
| 400 | return; |
| 401 | |
| 402 | while (1) { |
| 403 | static char buf[4096]; |
| 404 | ssize_t sz; |
| 405 | |
| 406 | sz = read(trace_fd, buf, sizeof(buf)); |
| 407 | if (sz > 0) { |
| 408 | buf[sz] = 0; |
| 409 | puts(buf); |
| 410 | } |
| 411 | } |
| 412 | } |
Alexei Starovoitov | 3622e7e | 2016-03-07 21:57:19 -0800 | [diff] [blame] | 413 | |
| 414 | #define MAX_SYMS 300000 |
| 415 | static struct ksym syms[MAX_SYMS]; |
| 416 | static int sym_cnt; |
| 417 | |
| 418 | static int ksym_cmp(const void *p1, const void *p2) |
| 419 | { |
| 420 | return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr; |
| 421 | } |
| 422 | |
| 423 | int load_kallsyms(void) |
| 424 | { |
| 425 | FILE *f = fopen("/proc/kallsyms", "r"); |
| 426 | char func[256], buf[256]; |
| 427 | char symbol; |
| 428 | void *addr; |
| 429 | int i = 0; |
| 430 | |
| 431 | if (!f) |
| 432 | return -ENOENT; |
| 433 | |
| 434 | while (!feof(f)) { |
| 435 | if (!fgets(buf, sizeof(buf), f)) |
| 436 | break; |
| 437 | if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3) |
| 438 | break; |
| 439 | if (!addr) |
| 440 | continue; |
| 441 | syms[i].addr = (long) addr; |
| 442 | syms[i].name = strdup(func); |
| 443 | i++; |
| 444 | } |
| 445 | sym_cnt = i; |
| 446 | qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp); |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | struct ksym *ksym_search(long key) |
| 451 | { |
| 452 | int start = 0, end = sym_cnt; |
| 453 | int result; |
| 454 | |
| 455 | while (start < end) { |
| 456 | size_t mid = start + (end - start) / 2; |
| 457 | |
| 458 | result = key - syms[mid].addr; |
| 459 | if (result < 0) |
| 460 | end = mid; |
| 461 | else if (result > 0) |
| 462 | start = mid + 1; |
| 463 | else |
| 464 | return &syms[mid]; |
| 465 | } |
| 466 | |
| 467 | if (start >= 1 && syms[start - 1].addr < key && |
| 468 | key < syms[start].addr) |
| 469 | /* valid ksym */ |
| 470 | return &syms[start - 1]; |
| 471 | |
| 472 | /* out of range. return _stext */ |
| 473 | return &syms[0]; |
| 474 | } |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 475 | |
| 476 | int set_link_xdp_fd(int ifindex, int fd) |
| 477 | { |
| 478 | struct sockaddr_nl sa; |
| 479 | int sock, seq = 0, len, ret = -1; |
| 480 | char buf[4096]; |
| 481 | struct nlattr *nla, *nla_xdp; |
| 482 | struct { |
| 483 | struct nlmsghdr nh; |
| 484 | struct ifinfomsg ifinfo; |
| 485 | char attrbuf[64]; |
| 486 | } req; |
| 487 | struct nlmsghdr *nh; |
| 488 | struct nlmsgerr *err; |
| 489 | |
| 490 | memset(&sa, 0, sizeof(sa)); |
| 491 | sa.nl_family = AF_NETLINK; |
| 492 | |
| 493 | sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |
| 494 | if (sock < 0) { |
| 495 | printf("open netlink socket: %s\n", strerror(errno)); |
| 496 | return -1; |
| 497 | } |
| 498 | |
| 499 | if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) { |
| 500 | printf("bind to netlink: %s\n", strerror(errno)); |
| 501 | goto cleanup; |
| 502 | } |
| 503 | |
| 504 | memset(&req, 0, sizeof(req)); |
| 505 | req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); |
| 506 | req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; |
| 507 | req.nh.nlmsg_type = RTM_SETLINK; |
| 508 | req.nh.nlmsg_pid = 0; |
| 509 | req.nh.nlmsg_seq = ++seq; |
| 510 | req.ifinfo.ifi_family = AF_UNSPEC; |
| 511 | req.ifinfo.ifi_index = ifindex; |
| 512 | nla = (struct nlattr *)(((char *)&req) |
| 513 | + NLMSG_ALIGN(req.nh.nlmsg_len)); |
| 514 | nla->nla_type = NLA_F_NESTED | 43/*IFLA_XDP*/; |
| 515 | |
| 516 | nla_xdp = (struct nlattr *)((char *)nla + NLA_HDRLEN); |
| 517 | nla_xdp->nla_type = 1/*IFLA_XDP_FD*/; |
| 518 | nla_xdp->nla_len = NLA_HDRLEN + sizeof(int); |
| 519 | memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd)); |
| 520 | nla->nla_len = NLA_HDRLEN + nla_xdp->nla_len; |
| 521 | |
| 522 | req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len); |
| 523 | |
| 524 | if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) { |
| 525 | printf("send to netlink: %s\n", strerror(errno)); |
| 526 | goto cleanup; |
| 527 | } |
| 528 | |
| 529 | len = recv(sock, buf, sizeof(buf), 0); |
| 530 | if (len < 0) { |
| 531 | printf("recv from netlink: %s\n", strerror(errno)); |
| 532 | goto cleanup; |
| 533 | } |
| 534 | |
| 535 | for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); |
| 536 | nh = NLMSG_NEXT(nh, len)) { |
| 537 | if (nh->nlmsg_pid != getpid()) { |
| 538 | printf("Wrong pid %d, expected %d\n", |
| 539 | nh->nlmsg_pid, getpid()); |
| 540 | goto cleanup; |
| 541 | } |
| 542 | if (nh->nlmsg_seq != seq) { |
| 543 | printf("Wrong seq %d, expected %d\n", |
| 544 | nh->nlmsg_seq, seq); |
| 545 | goto cleanup; |
| 546 | } |
| 547 | switch (nh->nlmsg_type) { |
| 548 | case NLMSG_ERROR: |
| 549 | err = (struct nlmsgerr *)NLMSG_DATA(nh); |
| 550 | if (!err->error) |
| 551 | continue; |
| 552 | printf("nlmsg error %s\n", strerror(-err->error)); |
| 553 | goto cleanup; |
| 554 | case NLMSG_DONE: |
| 555 | break; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | ret = 0; |
| 560 | |
| 561 | cleanup: |
| 562 | close(sock); |
| 563 | return ret; |
| 564 | } |