1 // SPDX-License-Identifier: GPL-2.0
13 #include <linux/bpf.h>
14 #include <linux/filter.h>
15 #include <linux/perf_event.h>
16 #include <linux/netlink.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/types.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/syscall.h>
22 #include <sys/ioctl.h>
31 #define DEBUGFS "/sys/kernel/debug/tracing/"
33 static char license[128];
34 static int kern_version;
35 static bool processed_sec[128];
36 char bpf_log_buf[BPF_LOG_BUF_SIZE];
38 int prog_fd[MAX_PROGS];
39 int event_fd[MAX_PROGS];
41 int prog_array_fd = -1;
43 struct bpf_map_data map_data[MAX_MAPS];
44 int map_data_count = 0;
46 static int populate_prog_array(const char *event, int prog_fd)
48 int ind = atoi(event), err;
50 err = bpf_map_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
52 printf("failed to store prog_fd in prog_array\n");
58 static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
60 bool is_socket = strncmp(event, "socket", 6) == 0;
61 bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
62 bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
63 bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0;
64 bool is_xdp = strncmp(event, "xdp", 3) == 0;
65 bool is_perf_event = strncmp(event, "perf_event", 10) == 0;
66 bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0;
67 bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0;
68 bool is_sockops = strncmp(event, "sockops", 7) == 0;
69 bool is_sk_skb = strncmp(event, "sk_skb", 6) == 0;
70 size_t insns_cnt = size / sizeof(struct bpf_insn);
71 enum bpf_prog_type prog_type;
74 struct perf_event_attr attr = {};
76 attr.type = PERF_TYPE_TRACEPOINT;
77 attr.sample_type = PERF_SAMPLE_RAW;
78 attr.sample_period = 1;
79 attr.wakeup_events = 1;
82 prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
83 } else if (is_kprobe || is_kretprobe) {
84 prog_type = BPF_PROG_TYPE_KPROBE;
85 } else if (is_tracepoint) {
86 prog_type = BPF_PROG_TYPE_TRACEPOINT;
88 prog_type = BPF_PROG_TYPE_XDP;
89 } else if (is_perf_event) {
90 prog_type = BPF_PROG_TYPE_PERF_EVENT;
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;
95 } else if (is_sockops) {
96 prog_type = BPF_PROG_TYPE_SOCK_OPS;
97 } else if (is_sk_skb) {
98 prog_type = BPF_PROG_TYPE_SK_SKB;
100 printf("Unknown event '%s'\n", event);
104 fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version,
105 bpf_log_buf, BPF_LOG_BUF_SIZE);
107 printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf);
111 prog_fd[prog_cnt++] = fd;
113 if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk)
116 if (is_socket || is_sockops || is_sk_skb) {
124 if (!isdigit(*event)) {
125 printf("invalid prog number\n");
128 return populate_prog_array(event, fd);
131 if (is_kprobe || is_kretprobe) {
138 printf("event name cannot be empty\n");
143 return populate_prog_array(event, fd);
145 snprintf(buf, sizeof(buf),
146 "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events",
147 is_kprobe ? 'p' : 'r', event, event);
150 printf("failed to create kprobe '%s' error '%s'\n",
151 event, strerror(errno));
155 strcpy(buf, DEBUGFS);
156 strcat(buf, "events/kprobes/");
159 } else if (is_tracepoint) {
163 printf("event name cannot be empty\n");
166 strcpy(buf, DEBUGFS);
167 strcat(buf, "events/");
172 efd = open(buf, O_RDONLY, 0);
174 printf("failed to open event %s\n", event);
178 err = read(efd, buf, sizeof(buf));
179 if (err < 0 || err >= sizeof(buf)) {
180 printf("read from '%s' failed '%s'\n", event, strerror(errno));
190 efd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
192 printf("event %d fd %d err %s\n", id, efd, strerror(errno));
195 event_fd[prog_cnt - 1] = efd;
196 ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
197 ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
202 static int load_maps(struct bpf_map_data *maps, int nr_maps,
203 fixup_map_cb fixup_map)
207 for (i = 0; i < nr_maps; i++) {
209 fixup_map(&maps[i], i);
210 /* Allow userspace to assign map FD prior to creation */
211 if (maps[i].fd != -1) {
212 map_fd[i] = maps[i].fd;
217 numa_node = maps[i].def.map_flags & BPF_F_NUMA_NODE ?
218 maps[i].def.numa_node : -1;
220 if (maps[i].def.type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
221 maps[i].def.type == BPF_MAP_TYPE_HASH_OF_MAPS) {
222 int inner_map_fd = map_fd[maps[i].def.inner_map_idx];
224 map_fd[i] = bpf_create_map_in_map_node(maps[i].def.type,
225 maps[i].def.key_size,
227 maps[i].def.max_entries,
228 maps[i].def.map_flags,
231 map_fd[i] = bpf_create_map_node(maps[i].def.type,
232 maps[i].def.key_size,
233 maps[i].def.value_size,
234 maps[i].def.max_entries,
235 maps[i].def.map_flags,
239 printf("failed to create a map: %d %s\n",
240 errno, strerror(errno));
243 maps[i].fd = map_fd[i];
245 if (maps[i].def.type == BPF_MAP_TYPE_PROG_ARRAY)
246 prog_array_fd = map_fd[i];
251 static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname,
252 GElf_Shdr *shdr, Elf_Data **data)
256 scn = elf_getscn(elf, i);
260 if (gelf_getshdr(scn, shdr) != shdr)
263 *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
264 if (!*shname || !shdr->sh_size)
267 *data = elf_getdata(scn, 0);
268 if (!*data || elf_getdata(scn, *data) != NULL)
274 static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
275 GElf_Shdr *shdr, struct bpf_insn *insn,
276 struct bpf_map_data *maps, int nr_maps)
280 nrels = shdr->sh_size / shdr->sh_entsize;
282 for (i = 0; i < nrels; i++) {
285 unsigned int insn_idx;
289 gelf_getrel(data, i, &rel);
291 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
293 gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym);
295 if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
296 printf("invalid relo for insn[%d].code 0x%x\n",
297 insn_idx, insn[insn_idx].code);
300 insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
302 /* Match FD relocation against recorded map_data[] offset */
303 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
304 if (maps[map_idx].elf_offset == sym.st_value) {
310 insn[insn_idx].imm = maps[map_idx].fd;
312 printf("invalid relo for insn[%d] no map_data match\n",
321 static int cmp_symbols(const void *l, const void *r)
323 const GElf_Sym *lsym = (const GElf_Sym *)l;
324 const GElf_Sym *rsym = (const GElf_Sym *)r;
326 if (lsym->st_value < rsym->st_value)
328 else if (lsym->st_value > rsym->st_value)
334 static int load_elf_maps_section(struct bpf_map_data *maps, int maps_shndx,
335 Elf *elf, Elf_Data *symbols, int strtabidx)
337 int map_sz_elf, map_sz_copy;
338 bool validate_zero = false;
350 /* Get data for maps section via elf index */
351 scn = elf_getscn(elf, maps_shndx);
353 data_maps = elf_getdata(scn, NULL);
354 if (!scn || !data_maps) {
355 printf("Failed to get Elf_Data from maps section %d\n",
360 /* For each map get corrosponding symbol table entry */
361 sym = calloc(MAX_MAPS+1, sizeof(GElf_Sym));
362 for (i = 0, nr_maps = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
363 assert(nr_maps < MAX_MAPS+1);
364 if (!gelf_getsym(symbols, i, &sym[nr_maps]))
366 if (sym[nr_maps].st_shndx != maps_shndx)
368 /* Only increment iif maps section */
372 /* Align to map_fd[] order, via sort on offset in sym.st_value */
373 qsort(sym, nr_maps, sizeof(GElf_Sym), cmp_symbols);
375 /* Keeping compatible with ELF maps section changes
376 * ------------------------------------------------
377 * The program size of struct bpf_map_def is known by loader
378 * code, but struct stored in ELF file can be different.
380 * Unfortunately sym[i].st_size is zero. To calculate the
381 * struct size stored in the ELF file, assume all struct have
382 * the same size, and simply divide with number of map
385 map_sz_elf = data_maps->d_size / nr_maps;
386 map_sz_copy = sizeof(struct bpf_map_def);
387 if (map_sz_elf < map_sz_copy) {
389 * Backward compat, loading older ELF file with
390 * smaller struct, keeping remaining bytes zero.
392 map_sz_copy = map_sz_elf;
393 } else if (map_sz_elf > map_sz_copy) {
395 * Forward compat, loading newer ELF file with larger
396 * struct with unknown features. Assume zero means
397 * feature not used. Thus, validate rest of struct
400 validate_zero = true;
403 /* Memcpy relevant part of ELF maps data to loader maps */
404 for (i = 0; i < nr_maps; i++) {
405 unsigned char *addr, *end;
406 struct bpf_map_def *def;
407 const char *map_name;
410 map_name = elf_strptr(elf, strtabidx, sym[i].st_name);
411 maps[i].name = strdup(map_name);
413 printf("strdup(%s): %s(%d)\n", map_name,
414 strerror(errno), errno);
419 /* Symbol value is offset into ELF maps section data area */
420 offset = sym[i].st_value;
421 def = (struct bpf_map_def *)(data_maps->d_buf + offset);
422 maps[i].elf_offset = offset;
423 memset(&maps[i].def, 0, sizeof(struct bpf_map_def));
424 memcpy(&maps[i].def, def, map_sz_copy);
426 /* Verify no newer features were requested */
428 addr = (unsigned char*) def + map_sz_copy;
429 end = (unsigned char*) def + map_sz_elf;
430 for (; addr < end; addr++) {
443 static int do_load_bpf_file(const char *path, fixup_map_cb fixup_map)
445 int fd, i, ret, maps_shndx = -1, strtabidx = -1;
448 GElf_Shdr shdr, shdr_prog;
449 Elf_Data *data, *data_prog, *data_maps = NULL, *symbols = NULL;
450 char *shname, *shname_prog;
453 /* reset global variables */
455 memset(license, 0, sizeof(license));
456 memset(processed_sec, 0, sizeof(processed_sec));
458 if (elf_version(EV_CURRENT) == EV_NONE)
461 fd = open(path, O_RDONLY, 0);
465 elf = elf_begin(fd, ELF_C_READ, NULL);
470 if (gelf_getehdr(elf, &ehdr) != &ehdr)
473 /* clear all kprobes */
474 i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events");
476 /* scan over all elf sections to get license and map info */
477 for (i = 1; i < ehdr.e_shnum; i++) {
479 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
482 if (0) /* helpful for llvm debugging */
483 printf("section %d:%s data %p size %zd link %d flags %d\n",
484 i, shname, data->d_buf, data->d_size,
485 shdr.sh_link, (int) shdr.sh_flags);
487 if (strcmp(shname, "license") == 0) {
488 processed_sec[i] = true;
489 memcpy(license, data->d_buf, data->d_size);
490 } else if (strcmp(shname, "version") == 0) {
491 processed_sec[i] = true;
492 if (data->d_size != sizeof(int)) {
493 printf("invalid size of version section %zd\n",
497 memcpy(&kern_version, data->d_buf, sizeof(int));
498 } else if (strcmp(shname, "maps") == 0) {
503 for (j = 0; j < MAX_MAPS; j++)
505 } else if (shdr.sh_type == SHT_SYMTAB) {
506 strtabidx = shdr.sh_link;
514 printf("missing SHT_SYMTAB section\n");
519 nr_maps = load_elf_maps_section(map_data, maps_shndx,
520 elf, symbols, strtabidx);
522 printf("Error: Failed loading ELF maps (errno:%d):%s\n",
523 nr_maps, strerror(-nr_maps));
527 if (load_maps(map_data, nr_maps, fixup_map))
529 map_data_count = nr_maps;
531 processed_sec[maps_shndx] = true;
534 /* process all relo sections, and rewrite bpf insns for maps */
535 for (i = 1; i < ehdr.e_shnum; i++) {
536 if (processed_sec[i])
539 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
542 if (shdr.sh_type == SHT_REL) {
543 struct bpf_insn *insns;
545 /* locate prog sec that need map fixup (relocations) */
546 if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
547 &shdr_prog, &data_prog))
550 if (shdr_prog.sh_type != SHT_PROGBITS ||
551 !(shdr_prog.sh_flags & SHF_EXECINSTR))
554 insns = (struct bpf_insn *) data_prog->d_buf;
555 processed_sec[i] = true; /* relo section */
557 if (parse_relo_and_apply(data, symbols, &shdr, insns,
564 for (i = 1; i < ehdr.e_shnum; i++) {
566 if (processed_sec[i])
569 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
572 if (memcmp(shname, "kprobe/", 7) == 0 ||
573 memcmp(shname, "kretprobe/", 10) == 0 ||
574 memcmp(shname, "tracepoint/", 11) == 0 ||
575 memcmp(shname, "xdp", 3) == 0 ||
576 memcmp(shname, "perf_event", 10) == 0 ||
577 memcmp(shname, "socket", 6) == 0 ||
578 memcmp(shname, "cgroup/", 7) == 0 ||
579 memcmp(shname, "sockops", 7) == 0 ||
580 memcmp(shname, "sk_skb", 6) == 0) {
581 ret = load_and_attach(shname, data->d_buf,
594 int load_bpf_file(char *path)
596 return do_load_bpf_file(path, NULL);
599 int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map)
601 return do_load_bpf_file(path, fixup_map);
604 void read_trace_pipe(void)
608 trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
613 static char buf[4096];
616 sz = read(trace_fd, buf, sizeof(buf));
624 #define MAX_SYMS 300000
625 static struct ksym syms[MAX_SYMS];
628 static int ksym_cmp(const void *p1, const void *p2)
630 return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
633 int load_kallsyms(void)
635 FILE *f = fopen("/proc/kallsyms", "r");
636 char func[256], buf[256];
645 if (!fgets(buf, sizeof(buf), f))
647 if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
651 syms[i].addr = (long) addr;
652 syms[i].name = strdup(func);
656 qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
660 struct ksym *ksym_search(long key)
662 int start = 0, end = sym_cnt;
665 while (start < end) {
666 size_t mid = start + (end - start) / 2;
668 result = key - syms[mid].addr;
677 if (start >= 1 && syms[start - 1].addr < key &&
678 key < syms[start].addr)
680 return &syms[start - 1];
682 /* out of range. return _stext */
686 int set_link_xdp_fd(int ifindex, int fd, __u32 flags)
688 struct sockaddr_nl sa;
689 int sock, seq = 0, len, ret = -1;
691 struct nlattr *nla, *nla_xdp;
694 struct ifinfomsg ifinfo;
698 struct nlmsgerr *err;
700 memset(&sa, 0, sizeof(sa));
701 sa.nl_family = AF_NETLINK;
703 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
705 printf("open netlink socket: %s\n", strerror(errno));
709 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
710 printf("bind to netlink: %s\n", strerror(errno));
714 memset(&req, 0, sizeof(req));
715 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
716 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
717 req.nh.nlmsg_type = RTM_SETLINK;
718 req.nh.nlmsg_pid = 0;
719 req.nh.nlmsg_seq = ++seq;
720 req.ifinfo.ifi_family = AF_UNSPEC;
721 req.ifinfo.ifi_index = ifindex;
723 /* started nested attribute for XDP */
724 nla = (struct nlattr *)(((char *)&req)
725 + NLMSG_ALIGN(req.nh.nlmsg_len));
726 nla->nla_type = NLA_F_NESTED | 43/*IFLA_XDP*/;
727 nla->nla_len = NLA_HDRLEN;
730 nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
731 nla_xdp->nla_type = 1/*IFLA_XDP_FD*/;
732 nla_xdp->nla_len = NLA_HDRLEN + sizeof(int);
733 memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd));
734 nla->nla_len += nla_xdp->nla_len;
736 /* if user passed in any flags, add those too */
738 nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
739 nla_xdp->nla_type = 3/*IFLA_XDP_FLAGS*/;
740 nla_xdp->nla_len = NLA_HDRLEN + sizeof(flags);
741 memcpy((char *)nla_xdp + NLA_HDRLEN, &flags, sizeof(flags));
742 nla->nla_len += nla_xdp->nla_len;
745 req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len);
747 if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) {
748 printf("send to netlink: %s\n", strerror(errno));
752 len = recv(sock, buf, sizeof(buf), 0);
754 printf("recv from netlink: %s\n", strerror(errno));
758 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
759 nh = NLMSG_NEXT(nh, len)) {
760 if (nh->nlmsg_pid != getpid()) {
761 printf("Wrong pid %d, expected %d\n",
762 nh->nlmsg_pid, getpid());
765 if (nh->nlmsg_seq != seq) {
766 printf("Wrong seq %d, expected %d\n",
770 switch (nh->nlmsg_type) {
772 err = (struct nlmsgerr *)NLMSG_DATA(nh);
775 printf("nlmsg error %s\n", strerror(-err->error));