blob: e30b6de94f2ee393afc1f9feb23d0a461a46a7da [file] [log] [blame]
Alexei Starovoitov249b8122014-12-01 15:06:37 -08001#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 Starovoitovb896c4f2015-03-25 12:49:23 -070011#include <stdlib.h>
Alexei Starovoitov249b8122014-12-01 15:06:37 -080012#include <linux/bpf.h>
13#include <linux/filter.h>
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070014#include <linux/perf_event.h>
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -080015#include <linux/netlink.h>
16#include <linux/rtnetlink.h>
17#include <sys/types.h>
18#include <sys/socket.h>
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070019#include <sys/syscall.h>
20#include <sys/ioctl.h>
21#include <sys/mman.h>
22#include <poll.h>
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070023#include <ctype.h>
Alexei Starovoitov249b8122014-12-01 15:06:37 -080024#include "libbpf.h"
25#include "bpf_helpers.h"
26#include "bpf_load.h"
27
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070028#define DEBUGFS "/sys/kernel/debug/tracing/"
29
Alexei Starovoitov249b8122014-12-01 15:06:37 -080030static char license[128];
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070031static int kern_version;
Alexei Starovoitov249b8122014-12-01 15:06:37 -080032static bool processed_sec[128];
33int map_fd[MAX_MAPS];
34int prog_fd[MAX_PROGS];
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070035int event_fd[MAX_PROGS];
Alexei Starovoitov249b8122014-12-01 15:06:37 -080036int prog_cnt;
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070037int prog_array_fd = -1;
38
39static int populate_prog_array(const char *event, int prog_fd)
40{
41 int ind = atoi(event), err;
42
43 err = bpf_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
44 if (err < 0) {
45 printf("failed to store prog_fd in prog_array\n");
46 return -1;
47 }
48 return 0;
49}
Alexei Starovoitov249b8122014-12-01 15:06:37 -080050
51static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
52{
Alexei Starovoitov249b8122014-12-01 15:06:37 -080053 bool is_socket = strncmp(event, "socket", 6) == 0;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070054 bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
55 bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
Alexei Starovoitovc0766042016-04-06 18:43:29 -070056 bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0;
Brenden Blanco86af8b42016-07-19 12:16:51 -070057 bool is_xdp = strncmp(event, "xdp", 3) == 0;
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -070058 bool is_perf_event = strncmp(event, "perf_event", 10) == 0;
David Ahern4f2e7ae2016-12-01 08:48:07 -080059 bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0;
60 bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070061 enum bpf_prog_type prog_type;
62 char buf[256];
63 int fd, efd, err, id;
64 struct perf_event_attr attr = {};
Alexei Starovoitov249b8122014-12-01 15:06:37 -080065
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070066 attr.type = PERF_TYPE_TRACEPOINT;
67 attr.sample_type = PERF_SAMPLE_RAW;
68 attr.sample_period = 1;
69 attr.wakeup_events = 1;
70
71 if (is_socket) {
72 prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
73 } else if (is_kprobe || is_kretprobe) {
74 prog_type = BPF_PROG_TYPE_KPROBE;
Alexei Starovoitovc0766042016-04-06 18:43:29 -070075 } else if (is_tracepoint) {
76 prog_type = BPF_PROG_TYPE_TRACEPOINT;
Brenden Blanco86af8b42016-07-19 12:16:51 -070077 } else if (is_xdp) {
78 prog_type = BPF_PROG_TYPE_XDP;
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -070079 } else if (is_perf_event) {
80 prog_type = BPF_PROG_TYPE_PERF_EVENT;
David Ahern4f2e7ae2016-12-01 08:48:07 -080081 } else if (is_cgroup_skb) {
82 prog_type = BPF_PROG_TYPE_CGROUP_SKB;
83 } else if (is_cgroup_sk) {
84 prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070085 } else {
86 printf("Unknown event '%s'\n", event);
Alexei Starovoitov249b8122014-12-01 15:06:37 -080087 return -1;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070088 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -080089
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070090 fd = bpf_prog_load(prog_type, prog, size, license, kern_version);
91 if (fd < 0) {
92 printf("bpf_prog_load() err=%d\n%s", errno, bpf_log_buf);
93 return -1;
94 }
95
96 prog_fd[prog_cnt++] = fd;
97
David Ahern4f2e7ae2016-12-01 08:48:07 -080098 if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk)
Brenden Blanco86af8b42016-07-19 12:16:51 -070099 return 0;
100
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700101 if (is_socket) {
102 event += 6;
103 if (*event != '/')
104 return 0;
105 event++;
106 if (!isdigit(*event)) {
107 printf("invalid prog number\n");
108 return -1;
109 }
110 return populate_prog_array(event, fd);
111 }
112
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700113 if (is_kprobe || is_kretprobe) {
114 if (is_kprobe)
115 event += 7;
116 else
117 event += 10;
118
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700119 if (*event == 0) {
120 printf("event name cannot be empty\n");
121 return -1;
122 }
123
124 if (isdigit(*event))
125 return populate_prog_array(event, fd);
126
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700127 snprintf(buf, sizeof(buf),
128 "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events",
129 is_kprobe ? 'p' : 'r', event, event);
130 err = system(buf);
131 if (err < 0) {
132 printf("failed to create kprobe '%s' error '%s'\n",
133 event, strerror(errno));
134 return -1;
135 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700136
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700137 strcpy(buf, DEBUGFS);
138 strcat(buf, "events/kprobes/");
139 strcat(buf, event);
140 strcat(buf, "/id");
141 } else if (is_tracepoint) {
142 event += 11;
143
144 if (*event == 0) {
145 printf("event name cannot be empty\n");
146 return -1;
147 }
148 strcpy(buf, DEBUGFS);
149 strcat(buf, "events/");
150 strcat(buf, event);
151 strcat(buf, "/id");
152 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700153
154 efd = open(buf, O_RDONLY, 0);
155 if (efd < 0) {
156 printf("failed to open event %s\n", event);
157 return -1;
158 }
159
160 err = read(efd, buf, sizeof(buf));
161 if (err < 0 || err >= sizeof(buf)) {
162 printf("read from '%s' failed '%s'\n", event, strerror(errno));
163 return -1;
164 }
165
166 close(efd);
167
168 buf[err] = 0;
169 id = atoi(buf);
170 attr.config = id;
171
172 efd = perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
173 if (efd < 0) {
174 printf("event %d fd %d err %s\n", id, efd, strerror(errno));
175 return -1;
176 }
177 event_fd[prog_cnt - 1] = efd;
178 ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
179 ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
180
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800181 return 0;
182}
183
184static int load_maps(struct bpf_map_def *maps, int len)
185{
186 int i;
187
188 for (i = 0; i < len / sizeof(struct bpf_map_def); i++) {
189
190 map_fd[i] = bpf_create_map(maps[i].type,
191 maps[i].key_size,
192 maps[i].value_size,
Alexei Starovoitov89b97602016-03-07 21:57:20 -0800193 maps[i].max_entries,
194 maps[i].map_flags);
Alexei Starovoitov618ec9a72016-03-07 21:57:18 -0800195 if (map_fd[i] < 0) {
196 printf("failed to create a map: %d %s\n",
197 errno, strerror(errno));
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800198 return 1;
Alexei Starovoitov618ec9a72016-03-07 21:57:18 -0800199 }
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700200
201 if (maps[i].type == BPF_MAP_TYPE_PROG_ARRAY)
202 prog_array_fd = map_fd[i];
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800203 }
204 return 0;
205}
206
207static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname,
208 GElf_Shdr *shdr, Elf_Data **data)
209{
210 Elf_Scn *scn;
211
212 scn = elf_getscn(elf, i);
213 if (!scn)
214 return 1;
215
216 if (gelf_getshdr(scn, shdr) != shdr)
217 return 2;
218
219 *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
220 if (!*shname || !shdr->sh_size)
221 return 3;
222
223 *data = elf_getdata(scn, 0);
224 if (!*data || elf_getdata(scn, *data) != NULL)
225 return 4;
226
227 return 0;
228}
229
230static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
231 GElf_Shdr *shdr, struct bpf_insn *insn)
232{
233 int i, nrels;
234
235 nrels = shdr->sh_size / shdr->sh_entsize;
236
237 for (i = 0; i < nrels; i++) {
238 GElf_Sym sym;
239 GElf_Rel rel;
240 unsigned int insn_idx;
241
242 gelf_getrel(data, i, &rel);
243
244 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
245
246 gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym);
247
248 if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
249 printf("invalid relo for insn[%d].code 0x%x\n",
250 insn_idx, insn[insn_idx].code);
251 return 1;
252 }
253 insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
254 insn[insn_idx].imm = map_fd[sym.st_value / sizeof(struct bpf_map_def)];
255 }
256
257 return 0;
258}
259
260int load_bpf_file(char *path)
261{
262 int fd, i;
263 Elf *elf;
264 GElf_Ehdr ehdr;
265 GElf_Shdr shdr, shdr_prog;
266 Elf_Data *data, *data_prog, *symbols = NULL;
267 char *shname, *shname_prog;
268
269 if (elf_version(EV_CURRENT) == EV_NONE)
270 return 1;
271
272 fd = open(path, O_RDONLY, 0);
273 if (fd < 0)
274 return 1;
275
276 elf = elf_begin(fd, ELF_C_READ, NULL);
277
278 if (!elf)
279 return 1;
280
281 if (gelf_getehdr(elf, &ehdr) != &ehdr)
282 return 1;
283
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700284 /* clear all kprobes */
285 i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events");
286
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800287 /* scan over all elf sections to get license and map info */
288 for (i = 1; i < ehdr.e_shnum; i++) {
289
290 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
291 continue;
292
293 if (0) /* helpful for llvm debugging */
294 printf("section %d:%s data %p size %zd link %d flags %d\n",
295 i, shname, data->d_buf, data->d_size,
296 shdr.sh_link, (int) shdr.sh_flags);
297
298 if (strcmp(shname, "license") == 0) {
299 processed_sec[i] = true;
300 memcpy(license, data->d_buf, data->d_size);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700301 } else if (strcmp(shname, "version") == 0) {
302 processed_sec[i] = true;
303 if (data->d_size != sizeof(int)) {
304 printf("invalid size of version section %zd\n",
305 data->d_size);
306 return 1;
307 }
308 memcpy(&kern_version, data->d_buf, sizeof(int));
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800309 } else if (strcmp(shname, "maps") == 0) {
310 processed_sec[i] = true;
311 if (load_maps(data->d_buf, data->d_size))
312 return 1;
313 } else if (shdr.sh_type == SHT_SYMTAB) {
314 symbols = data;
315 }
316 }
317
318 /* load programs that need map fixup (relocations) */
319 for (i = 1; i < ehdr.e_shnum; i++) {
320
321 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
322 continue;
323 if (shdr.sh_type == SHT_REL) {
324 struct bpf_insn *insns;
325
326 if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
327 &shdr_prog, &data_prog))
328 continue;
329
Alexei Starovoitovdb6a71d2016-11-22 16:52:09 -0800330 if (shdr_prog.sh_type != SHT_PROGBITS ||
331 !(shdr_prog.sh_flags & SHF_EXECINSTR))
332 continue;
333
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800334 insns = (struct bpf_insn *) data_prog->d_buf;
335
336 processed_sec[shdr.sh_info] = true;
337 processed_sec[i] = true;
338
339 if (parse_relo_and_apply(data, symbols, &shdr, insns))
340 continue;
341
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700342 if (memcmp(shname_prog, "kprobe/", 7) == 0 ||
343 memcmp(shname_prog, "kretprobe/", 10) == 0 ||
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700344 memcmp(shname_prog, "tracepoint/", 11) == 0 ||
Brenden Blanco86af8b42016-07-19 12:16:51 -0700345 memcmp(shname_prog, "xdp", 3) == 0 ||
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -0700346 memcmp(shname_prog, "perf_event", 10) == 0 ||
David Ahern4f2e7ae2016-12-01 08:48:07 -0800347 memcmp(shname_prog, "socket", 6) == 0 ||
348 memcmp(shname_prog, "cgroup/", 7) == 0)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800349 load_and_attach(shname_prog, insns, data_prog->d_size);
350 }
351 }
352
353 /* load programs that don't use maps */
354 for (i = 1; i < ehdr.e_shnum; i++) {
355
356 if (processed_sec[i])
357 continue;
358
359 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
360 continue;
361
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700362 if (memcmp(shname, "kprobe/", 7) == 0 ||
363 memcmp(shname, "kretprobe/", 10) == 0 ||
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700364 memcmp(shname, "tracepoint/", 11) == 0 ||
Brenden Blanco86af8b42016-07-19 12:16:51 -0700365 memcmp(shname, "xdp", 3) == 0 ||
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -0700366 memcmp(shname, "perf_event", 10) == 0 ||
David Ahern4f2e7ae2016-12-01 08:48:07 -0800367 memcmp(shname, "socket", 6) == 0 ||
368 memcmp(shname, "cgroup/", 7) == 0)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800369 load_and_attach(shname, data->d_buf, data->d_size);
370 }
371
372 close(fd);
373 return 0;
374}
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700375
376void read_trace_pipe(void)
377{
378 int trace_fd;
379
380 trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
381 if (trace_fd < 0)
382 return;
383
384 while (1) {
385 static char buf[4096];
386 ssize_t sz;
387
388 sz = read(trace_fd, buf, sizeof(buf));
389 if (sz > 0) {
390 buf[sz] = 0;
391 puts(buf);
392 }
393 }
394}
Alexei Starovoitov3622e7e2016-03-07 21:57:19 -0800395
396#define MAX_SYMS 300000
397static struct ksym syms[MAX_SYMS];
398static int sym_cnt;
399
400static int ksym_cmp(const void *p1, const void *p2)
401{
402 return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
403}
404
405int load_kallsyms(void)
406{
407 FILE *f = fopen("/proc/kallsyms", "r");
408 char func[256], buf[256];
409 char symbol;
410 void *addr;
411 int i = 0;
412
413 if (!f)
414 return -ENOENT;
415
416 while (!feof(f)) {
417 if (!fgets(buf, sizeof(buf), f))
418 break;
419 if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
420 break;
421 if (!addr)
422 continue;
423 syms[i].addr = (long) addr;
424 syms[i].name = strdup(func);
425 i++;
426 }
427 sym_cnt = i;
428 qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
429 return 0;
430}
431
432struct ksym *ksym_search(long key)
433{
434 int start = 0, end = sym_cnt;
435 int result;
436
437 while (start < end) {
438 size_t mid = start + (end - start) / 2;
439
440 result = key - syms[mid].addr;
441 if (result < 0)
442 end = mid;
443 else if (result > 0)
444 start = mid + 1;
445 else
446 return &syms[mid];
447 }
448
449 if (start >= 1 && syms[start - 1].addr < key &&
450 key < syms[start].addr)
451 /* valid ksym */
452 return &syms[start - 1];
453
454 /* out of range. return _stext */
455 return &syms[0];
456}
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800457
458int set_link_xdp_fd(int ifindex, int fd)
459{
460 struct sockaddr_nl sa;
461 int sock, seq = 0, len, ret = -1;
462 char buf[4096];
463 struct nlattr *nla, *nla_xdp;
464 struct {
465 struct nlmsghdr nh;
466 struct ifinfomsg ifinfo;
467 char attrbuf[64];
468 } req;
469 struct nlmsghdr *nh;
470 struct nlmsgerr *err;
471
472 memset(&sa, 0, sizeof(sa));
473 sa.nl_family = AF_NETLINK;
474
475 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
476 if (sock < 0) {
477 printf("open netlink socket: %s\n", strerror(errno));
478 return -1;
479 }
480
481 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
482 printf("bind to netlink: %s\n", strerror(errno));
483 goto cleanup;
484 }
485
486 memset(&req, 0, sizeof(req));
487 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
488 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
489 req.nh.nlmsg_type = RTM_SETLINK;
490 req.nh.nlmsg_pid = 0;
491 req.nh.nlmsg_seq = ++seq;
492 req.ifinfo.ifi_family = AF_UNSPEC;
493 req.ifinfo.ifi_index = ifindex;
494 nla = (struct nlattr *)(((char *)&req)
495 + NLMSG_ALIGN(req.nh.nlmsg_len));
496 nla->nla_type = NLA_F_NESTED | 43/*IFLA_XDP*/;
497
498 nla_xdp = (struct nlattr *)((char *)nla + NLA_HDRLEN);
499 nla_xdp->nla_type = 1/*IFLA_XDP_FD*/;
500 nla_xdp->nla_len = NLA_HDRLEN + sizeof(int);
501 memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd));
502 nla->nla_len = NLA_HDRLEN + nla_xdp->nla_len;
503
504 req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len);
505
506 if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) {
507 printf("send to netlink: %s\n", strerror(errno));
508 goto cleanup;
509 }
510
511 len = recv(sock, buf, sizeof(buf), 0);
512 if (len < 0) {
513 printf("recv from netlink: %s\n", strerror(errno));
514 goto cleanup;
515 }
516
517 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
518 nh = NLMSG_NEXT(nh, len)) {
519 if (nh->nlmsg_pid != getpid()) {
520 printf("Wrong pid %d, expected %d\n",
521 nh->nlmsg_pid, getpid());
522 goto cleanup;
523 }
524 if (nh->nlmsg_seq != seq) {
525 printf("Wrong seq %d, expected %d\n",
526 nh->nlmsg_seq, seq);
527 goto cleanup;
528 }
529 switch (nh->nlmsg_type) {
530 case NLMSG_ERROR:
531 err = (struct nlmsgerr *)NLMSG_DATA(nh);
532 if (!err->error)
533 continue;
534 printf("nlmsg error %s\n", strerror(-err->error));
535 goto cleanup;
536 case NLMSG_DONE:
537 break;
538 }
539 }
540
541 ret = 0;
542
543cleanup:
544 close(sock);
545 return ret;
546}