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