]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - samples/bpf/bpf_load.c
Merge tag 'mac80211-next-for-davem-2018-03-29' of git://git.kernel.org/pub/scm/linux...
[mirror_ubuntu-eoan-kernel.git] / samples / bpf / bpf_load.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
249b812d
AS
2#include <stdio.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <libelf.h>
7#include <gelf.h>
8#include <errno.h>
9#include <unistd.h>
10#include <string.h>
11#include <stdbool.h>
b896c4f9 12#include <stdlib.h>
249b812d
AS
13#include <linux/bpf.h>
14#include <linux/filter.h>
b896c4f9 15#include <linux/perf_event.h>
12d8bb64
MKL
16#include <linux/netlink.h>
17#include <linux/rtnetlink.h>
6387d011 18#include <linux/types.h>
12d8bb64
MKL
19#include <sys/types.h>
20#include <sys/socket.h>
b896c4f9
AS
21#include <sys/syscall.h>
22#include <sys/ioctl.h>
23#include <sys/mman.h>
24#include <poll.h>
5bacd780 25#include <ctype.h>
9fd63d05 26#include <assert.h>
249b812d 27#include "libbpf.h"
249b812d 28#include "bpf_load.h"
205c8ada 29#include "perf-sys.h"
249b812d 30
b896c4f9
AS
31#define DEBUGFS "/sys/kernel/debug/tracing/"
32
249b812d 33static char license[128];
b896c4f9 34static int kern_version;
249b812d 35static bool processed_sec[128];
d40fc181 36char bpf_log_buf[BPF_LOG_BUF_SIZE];
249b812d
AS
37int map_fd[MAX_MAPS];
38int prog_fd[MAX_PROGS];
b896c4f9 39int event_fd[MAX_PROGS];
249b812d 40int prog_cnt;
5bacd780
AS
41int prog_array_fd = -1;
42
156450d9
JDB
43struct bpf_map_data map_data[MAX_MAPS];
44int map_data_count = 0;
45
5bacd780
AS
46static int populate_prog_array(const char *event, int prog_fd)
47{
48 int ind = atoi(event), err;
49
d40fc181 50 err = bpf_map_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
5bacd780
AS
51 if (err < 0) {
52 printf("failed to store prog_fd in prog_array\n");
53 return -1;
54 }
55 return 0;
56}
249b812d
AS
57
58static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
59{
249b812d 60 bool is_socket = strncmp(event, "socket", 6) == 0;
b896c4f9
AS
61 bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
62 bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
c0766040 63 bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0;
86af8b41 64 bool is_xdp = strncmp(event, "xdp", 3) == 0;
1c47910e 65 bool is_perf_event = strncmp(event, "perf_event", 10) == 0;
4f2e7ae5
DA
66 bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0;
67 bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0;
40304b2a 68 bool is_sockops = strncmp(event, "sockops", 7) == 0;
69e8cc13 69 bool is_sk_skb = strncmp(event, "sk_skb", 6) == 0;
4c4c3c27 70 bool is_sk_msg = strncmp(event, "sk_msg", 6) == 0;
43371c83 71 size_t insns_cnt = size / sizeof(struct bpf_insn);
b896c4f9
AS
72 enum bpf_prog_type prog_type;
73 char buf[256];
74 int fd, efd, err, id;
75 struct perf_event_attr attr = {};
76
77 attr.type = PERF_TYPE_TRACEPOINT;
78 attr.sample_type = PERF_SAMPLE_RAW;
79 attr.sample_period = 1;
80 attr.wakeup_events = 1;
81
82 if (is_socket) {
83 prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
84 } else if (is_kprobe || is_kretprobe) {
85 prog_type = BPF_PROG_TYPE_KPROBE;
c0766040
AS
86 } else if (is_tracepoint) {
87 prog_type = BPF_PROG_TYPE_TRACEPOINT;
86af8b41
BB
88 } else if (is_xdp) {
89 prog_type = BPF_PROG_TYPE_XDP;
1c47910e
AS
90 } else if (is_perf_event) {
91 prog_type = BPF_PROG_TYPE_PERF_EVENT;
4f2e7ae5
DA
92 } else if (is_cgroup_skb) {
93 prog_type = BPF_PROG_TYPE_CGROUP_SKB;
94 } else if (is_cgroup_sk) {
95 prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
40304b2a
LB
96 } else if (is_sockops) {
97 prog_type = BPF_PROG_TYPE_SOCK_OPS;
69e8cc13
JF
98 } else if (is_sk_skb) {
99 prog_type = BPF_PROG_TYPE_SK_SKB;
4c4c3c27
JF
100 } else if (is_sk_msg) {
101 prog_type = BPF_PROG_TYPE_SK_MSG;
b896c4f9
AS
102 } else {
103 printf("Unknown event '%s'\n", event);
249b812d 104 return -1;
b896c4f9
AS
105 }
106
43371c83 107 fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version,
d40fc181 108 bpf_log_buf, BPF_LOG_BUF_SIZE);
5bacd780 109 if (fd < 0) {
d40fc181 110 printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf);
5bacd780
AS
111 return -1;
112 }
113
114 prog_fd[prog_cnt++] = fd;
115
4f2e7ae5 116 if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk)
86af8b41
BB
117 return 0;
118
4c4c3c27 119 if (is_socket || is_sockops || is_sk_skb || is_sk_msg) {
40304b2a
LB
120 if (is_socket)
121 event += 6;
122 else
123 event += 7;
5bacd780
AS
124 if (*event != '/')
125 return 0;
126 event++;
127 if (!isdigit(*event)) {
128 printf("invalid prog number\n");
129 return -1;
130 }
131 return populate_prog_array(event, fd);
132 }
133
b896c4f9
AS
134 if (is_kprobe || is_kretprobe) {
135 if (is_kprobe)
136 event += 7;
137 else
138 event += 10;
139
5bacd780
AS
140 if (*event == 0) {
141 printf("event name cannot be empty\n");
142 return -1;
143 }
144
145 if (isdigit(*event))
146 return populate_prog_array(event, fd);
147
b896c4f9
AS
148 snprintf(buf, sizeof(buf),
149 "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events",
150 is_kprobe ? 'p' : 'r', event, event);
151 err = system(buf);
152 if (err < 0) {
153 printf("failed to create kprobe '%s' error '%s'\n",
154 event, strerror(errno));
155 return -1;
156 }
249b812d 157
c0766040
AS
158 strcpy(buf, DEBUGFS);
159 strcat(buf, "events/kprobes/");
160 strcat(buf, event);
161 strcat(buf, "/id");
162 } else if (is_tracepoint) {
163 event += 11;
164
165 if (*event == 0) {
166 printf("event name cannot be empty\n");
167 return -1;
168 }
169 strcpy(buf, DEBUGFS);
170 strcat(buf, "events/");
171 strcat(buf, event);
172 strcat(buf, "/id");
173 }
b896c4f9
AS
174
175 efd = open(buf, O_RDONLY, 0);
176 if (efd < 0) {
177 printf("failed to open event %s\n", event);
178 return -1;
179 }
180
181 err = read(efd, buf, sizeof(buf));
182 if (err < 0 || err >= sizeof(buf)) {
183 printf("read from '%s' failed '%s'\n", event, strerror(errno));
184 return -1;
185 }
186
187 close(efd);
188
189 buf[err] = 0;
190 id = atoi(buf);
191 attr.config = id;
192
205c8ada 193 efd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
b896c4f9
AS
194 if (efd < 0) {
195 printf("event %d fd %d err %s\n", id, efd, strerror(errno));
196 return -1;
197 }
198 event_fd[prog_cnt - 1] = efd;
0ec9552b
YS
199 err = ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
200 if (err < 0) {
201 printf("ioctl PERF_EVENT_IOC_ENABLE failed err %s\n",
202 strerror(errno));
203 return -1;
204 }
205 err = ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
206 if (err < 0) {
207 printf("ioctl PERF_EVENT_IOC_SET_BPF failed err %s\n",
208 strerror(errno));
209 return -1;
210 }
b896c4f9 211
249b812d
AS
212 return 0;
213}
214
156450d9
JDB
215static int load_maps(struct bpf_map_data *maps, int nr_maps,
216 fixup_map_cb fixup_map)
249b812d 217{
ad17d0e6 218 int i, numa_node;
156450d9 219
5010e948 220 for (i = 0; i < nr_maps; i++) {
6979bcc7
JDB
221 if (fixup_map) {
222 fixup_map(&maps[i], i);
223 /* Allow userspace to assign map FD prior to creation */
224 if (maps[i].fd != -1) {
225 map_fd[i] = maps[i].fd;
226 continue;
227 }
228 }
249b812d 229
ad17d0e6
MKL
230 numa_node = maps[i].def.map_flags & BPF_F_NUMA_NODE ?
231 maps[i].def.numa_node : -1;
232
156450d9
JDB
233 if (maps[i].def.type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
234 maps[i].def.type == BPF_MAP_TYPE_HASH_OF_MAPS) {
235 int inner_map_fd = map_fd[maps[i].def.inner_map_idx];
fb30d4b7 236
ad17d0e6 237 map_fd[i] = bpf_create_map_in_map_node(maps[i].def.type,
88cda1c9 238 maps[i].name,
156450d9
JDB
239 maps[i].def.key_size,
240 inner_map_fd,
241 maps[i].def.max_entries,
ad17d0e6
MKL
242 maps[i].def.map_flags,
243 numa_node);
fb30d4b7 244 } else {
ad17d0e6 245 map_fd[i] = bpf_create_map_node(maps[i].def.type,
88cda1c9 246 maps[i].name,
ad17d0e6
MKL
247 maps[i].def.key_size,
248 maps[i].def.value_size,
249 maps[i].def.max_entries,
250 maps[i].def.map_flags,
251 numa_node);
fb30d4b7 252 }
618ec9a7
AS
253 if (map_fd[i] < 0) {
254 printf("failed to create a map: %d %s\n",
255 errno, strerror(errno));
249b812d 256 return 1;
618ec9a7 257 }
156450d9 258 maps[i].fd = map_fd[i];
5bacd780 259
156450d9 260 if (maps[i].def.type == BPF_MAP_TYPE_PROG_ARRAY)
5bacd780 261 prog_array_fd = map_fd[i];
249b812d
AS
262 }
263 return 0;
264}
265
266static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname,
267 GElf_Shdr *shdr, Elf_Data **data)
268{
269 Elf_Scn *scn;
270
271 scn = elf_getscn(elf, i);
272 if (!scn)
273 return 1;
274
275 if (gelf_getshdr(scn, shdr) != shdr)
276 return 2;
277
278 *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
279 if (!*shname || !shdr->sh_size)
280 return 3;
281
282 *data = elf_getdata(scn, 0);
283 if (!*data || elf_getdata(scn, *data) != NULL)
284 return 4;
285
286 return 0;
287}
288
289static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
156450d9
JDB
290 GElf_Shdr *shdr, struct bpf_insn *insn,
291 struct bpf_map_data *maps, int nr_maps)
249b812d
AS
292{
293 int i, nrels;
294
295 nrels = shdr->sh_size / shdr->sh_entsize;
296
297 for (i = 0; i < nrels; i++) {
298 GElf_Sym sym;
299 GElf_Rel rel;
300 unsigned int insn_idx;
156450d9
JDB
301 bool match = false;
302 int j, map_idx;
249b812d
AS
303
304 gelf_getrel(data, i, &rel);
305
306 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
307
308 gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym);
309
310 if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
311 printf("invalid relo for insn[%d].code 0x%x\n",
312 insn_idx, insn[insn_idx].code);
313 return 1;
314 }
315 insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
156450d9
JDB
316
317 /* Match FD relocation against recorded map_data[] offset */
318 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
319 if (maps[map_idx].elf_offset == sym.st_value) {
320 match = true;
321 break;
322 }
323 }
324 if (match) {
325 insn[insn_idx].imm = maps[map_idx].fd;
326 } else {
327 printf("invalid relo for insn[%d] no map_data match\n",
328 insn_idx);
329 return 1;
330 }
249b812d
AS
331 }
332
333 return 0;
334}
335
9fd63d05
MKL
336static int cmp_symbols(const void *l, const void *r)
337{
338 const GElf_Sym *lsym = (const GElf_Sym *)l;
339 const GElf_Sym *rsym = (const GElf_Sym *)r;
340
341 if (lsym->st_value < rsym->st_value)
342 return -1;
343 else if (lsym->st_value > rsym->st_value)
344 return 1;
345 else
346 return 0;
347}
348
156450d9
JDB
349static int load_elf_maps_section(struct bpf_map_data *maps, int maps_shndx,
350 Elf *elf, Elf_Data *symbols, int strtabidx)
9fd63d05 351{
156450d9
JDB
352 int map_sz_elf, map_sz_copy;
353 bool validate_zero = false;
354 Elf_Data *data_maps;
355 int i, nr_maps;
356 GElf_Sym *sym;
357 Elf_Scn *scn;
358 int copy_sz;
359
360 if (maps_shndx < 0)
361 return -EINVAL;
362 if (!symbols)
363 return -EINVAL;
364
365 /* Get data for maps section via elf index */
366 scn = elf_getscn(elf, maps_shndx);
367 if (scn)
368 data_maps = elf_getdata(scn, NULL);
369 if (!scn || !data_maps) {
370 printf("Failed to get Elf_Data from maps section %d\n",
371 maps_shndx);
372 return -EINVAL;
373 }
9fd63d05 374
156450d9
JDB
375 /* For each map get corrosponding symbol table entry */
376 sym = calloc(MAX_MAPS+1, sizeof(GElf_Sym));
377 for (i = 0, nr_maps = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
378 assert(nr_maps < MAX_MAPS+1);
379 if (!gelf_getsym(symbols, i, &sym[nr_maps]))
9fd63d05 380 continue;
156450d9 381 if (sym[nr_maps].st_shndx != maps_shndx)
9fd63d05 382 continue;
156450d9 383 /* Only increment iif maps section */
9fd63d05
MKL
384 nr_maps++;
385 }
386
156450d9
JDB
387 /* Align to map_fd[] order, via sort on offset in sym.st_value */
388 qsort(sym, nr_maps, sizeof(GElf_Sym), cmp_symbols);
389
390 /* Keeping compatible with ELF maps section changes
391 * ------------------------------------------------
392 * The program size of struct bpf_map_def is known by loader
393 * code, but struct stored in ELF file can be different.
394 *
395 * Unfortunately sym[i].st_size is zero. To calculate the
396 * struct size stored in the ELF file, assume all struct have
397 * the same size, and simply divide with number of map
398 * symbols.
399 */
400 map_sz_elf = data_maps->d_size / nr_maps;
401 map_sz_copy = sizeof(struct bpf_map_def);
402 if (map_sz_elf < map_sz_copy) {
403 /*
404 * Backward compat, loading older ELF file with
405 * smaller struct, keeping remaining bytes zero.
406 */
407 map_sz_copy = map_sz_elf;
408 } else if (map_sz_elf > map_sz_copy) {
409 /*
410 * Forward compat, loading newer ELF file with larger
411 * struct with unknown features. Assume zero means
412 * feature not used. Thus, validate rest of struct
413 * data is zero.
414 */
415 validate_zero = true;
416 }
9fd63d05 417
156450d9 418 /* Memcpy relevant part of ELF maps data to loader maps */
9fd63d05 419 for (i = 0; i < nr_maps; i++) {
156450d9
JDB
420 unsigned char *addr, *end;
421 struct bpf_map_def *def;
422 const char *map_name;
423 size_t offset;
424
425 map_name = elf_strptr(elf, strtabidx, sym[i].st_name);
426 maps[i].name = strdup(map_name);
427 if (!maps[i].name) {
9fd63d05
MKL
428 printf("strdup(%s): %s(%d)\n", map_name,
429 strerror(errno), errno);
156450d9
JDB
430 free(sym);
431 return -errno;
432 }
433
434 /* Symbol value is offset into ELF maps section data area */
435 offset = sym[i].st_value;
436 def = (struct bpf_map_def *)(data_maps->d_buf + offset);
437 maps[i].elf_offset = offset;
438 memset(&maps[i].def, 0, sizeof(struct bpf_map_def));
439 memcpy(&maps[i].def, def, map_sz_copy);
440
441 /* Verify no newer features were requested */
442 if (validate_zero) {
443 addr = (unsigned char*) def + map_sz_copy;
444 end = (unsigned char*) def + map_sz_elf;
445 for (; addr < end; addr++) {
446 if (*addr != 0) {
447 free(sym);
448 return -EFBIG;
449 }
450 }
9fd63d05
MKL
451 }
452 }
453
156450d9 454 free(sym);
5010e948 455 return nr_maps;
9fd63d05
MKL
456}
457
458static int do_load_bpf_file(const char *path, fixup_map_cb fixup_map)
249b812d 459{
9fd63d05 460 int fd, i, ret, maps_shndx = -1, strtabidx = -1;
249b812d
AS
461 Elf *elf;
462 GElf_Ehdr ehdr;
463 GElf_Shdr shdr, shdr_prog;
9fd63d05 464 Elf_Data *data, *data_prog, *data_maps = NULL, *symbols = NULL;
156450d9
JDB
465 char *shname, *shname_prog;
466 int nr_maps = 0;
249b812d 467
a734fb5d
MS
468 /* reset global variables */
469 kern_version = 0;
470 memset(license, 0, sizeof(license));
471 memset(processed_sec, 0, sizeof(processed_sec));
472
249b812d
AS
473 if (elf_version(EV_CURRENT) == EV_NONE)
474 return 1;
475
476 fd = open(path, O_RDONLY, 0);
477 if (fd < 0)
478 return 1;
479
480 elf = elf_begin(fd, ELF_C_READ, NULL);
481
482 if (!elf)
483 return 1;
484
485 if (gelf_getehdr(elf, &ehdr) != &ehdr)
486 return 1;
487
b896c4f9
AS
488 /* clear all kprobes */
489 i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events");
490
249b812d
AS
491 /* scan over all elf sections to get license and map info */
492 for (i = 1; i < ehdr.e_shnum; i++) {
493
494 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
495 continue;
496
497 if (0) /* helpful for llvm debugging */
498 printf("section %d:%s data %p size %zd link %d flags %d\n",
499 i, shname, data->d_buf, data->d_size,
500 shdr.sh_link, (int) shdr.sh_flags);
501
502 if (strcmp(shname, "license") == 0) {
503 processed_sec[i] = true;
504 memcpy(license, data->d_buf, data->d_size);
b896c4f9
AS
505 } else if (strcmp(shname, "version") == 0) {
506 processed_sec[i] = true;
507 if (data->d_size != sizeof(int)) {
508 printf("invalid size of version section %zd\n",
509 data->d_size);
510 return 1;
511 }
512 memcpy(&kern_version, data->d_buf, sizeof(int));
249b812d 513 } else if (strcmp(shname, "maps") == 0) {
156450d9
JDB
514 int j;
515
9fd63d05
MKL
516 maps_shndx = i;
517 data_maps = data;
156450d9
JDB
518 for (j = 0; j < MAX_MAPS; j++)
519 map_data[j].fd = -1;
249b812d 520 } else if (shdr.sh_type == SHT_SYMTAB) {
9fd63d05 521 strtabidx = shdr.sh_link;
249b812d
AS
522 symbols = data;
523 }
524 }
525
9fd63d05
MKL
526 ret = 1;
527
528 if (!symbols) {
529 printf("missing SHT_SYMTAB section\n");
530 goto done;
531 }
532
533 if (data_maps) {
156450d9
JDB
534 nr_maps = load_elf_maps_section(map_data, maps_shndx,
535 elf, symbols, strtabidx);
536 if (nr_maps < 0) {
537 printf("Error: Failed loading ELF maps (errno:%d):%s\n",
538 nr_maps, strerror(-nr_maps));
5010e948
JDB
539 ret = 1;
540 goto done;
541 }
156450d9 542 if (load_maps(map_data, nr_maps, fixup_map))
9fd63d05 543 goto done;
156450d9 544 map_data_count = nr_maps;
9fd63d05
MKL
545
546 processed_sec[maps_shndx] = true;
547 }
548
7bc57950 549 /* process all relo sections, and rewrite bpf insns for maps */
249b812d 550 for (i = 1; i < ehdr.e_shnum; i++) {
16ad1329
MS
551 if (processed_sec[i])
552 continue;
249b812d
AS
553
554 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
555 continue;
7bc57950 556
249b812d
AS
557 if (shdr.sh_type == SHT_REL) {
558 struct bpf_insn *insns;
559
7bc57950 560 /* locate prog sec that need map fixup (relocations) */
249b812d
AS
561 if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
562 &shdr_prog, &data_prog))
563 continue;
564
db6a71dd
AS
565 if (shdr_prog.sh_type != SHT_PROGBITS ||
566 !(shdr_prog.sh_flags & SHF_EXECINSTR))
567 continue;
568
249b812d 569 insns = (struct bpf_insn *) data_prog->d_buf;
7bc57950 570 processed_sec[i] = true; /* relo section */
249b812d 571
156450d9
JDB
572 if (parse_relo_and_apply(data, symbols, &shdr, insns,
573 map_data, nr_maps))
249b812d 574 continue;
249b812d
AS
575 }
576 }
577
7bc57950 578 /* load programs */
249b812d
AS
579 for (i = 1; i < ehdr.e_shnum; i++) {
580
581 if (processed_sec[i])
582 continue;
583
584 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
585 continue;
586
b896c4f9
AS
587 if (memcmp(shname, "kprobe/", 7) == 0 ||
588 memcmp(shname, "kretprobe/", 10) == 0 ||
c0766040 589 memcmp(shname, "tracepoint/", 11) == 0 ||
86af8b41 590 memcmp(shname, "xdp", 3) == 0 ||
1c47910e 591 memcmp(shname, "perf_event", 10) == 0 ||
4f2e7ae5 592 memcmp(shname, "socket", 6) == 0 ||
40304b2a 593 memcmp(shname, "cgroup/", 7) == 0 ||
69e8cc13 594 memcmp(shname, "sockops", 7) == 0 ||
4c4c3c27
JF
595 memcmp(shname, "sk_skb", 6) == 0 ||
596 memcmp(shname, "sk_msg", 6) == 0) {
f856e469
LB
597 ret = load_and_attach(shname, data->d_buf,
598 data->d_size);
599 if (ret != 0)
600 goto done;
601 }
249b812d
AS
602 }
603
9fd63d05
MKL
604 ret = 0;
605done:
249b812d 606 close(fd);
9fd63d05
MKL
607 return ret;
608}
609
610int load_bpf_file(char *path)
611{
612 return do_load_bpf_file(path, NULL);
613}
614
615int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map)
616{
617 return do_load_bpf_file(path, fixup_map);
249b812d 618}
b896c4f9
AS
619
620void read_trace_pipe(void)
621{
622 int trace_fd;
623
624 trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
625 if (trace_fd < 0)
626 return;
627
628 while (1) {
629 static char buf[4096];
630 ssize_t sz;
631
632 sz = read(trace_fd, buf, sizeof(buf));
633 if (sz > 0) {
634 buf[sz] = 0;
635 puts(buf);
636 }
637 }
638}
3622e7e4
AS
639
640#define MAX_SYMS 300000
641static struct ksym syms[MAX_SYMS];
642static int sym_cnt;
643
644static int ksym_cmp(const void *p1, const void *p2)
645{
646 return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
647}
648
649int load_kallsyms(void)
650{
651 FILE *f = fopen("/proc/kallsyms", "r");
652 char func[256], buf[256];
653 char symbol;
654 void *addr;
655 int i = 0;
656
657 if (!f)
658 return -ENOENT;
659
660 while (!feof(f)) {
661 if (!fgets(buf, sizeof(buf), f))
662 break;
663 if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
664 break;
665 if (!addr)
666 continue;
667 syms[i].addr = (long) addr;
668 syms[i].name = strdup(func);
669 i++;
670 }
671 sym_cnt = i;
672 qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
673 return 0;
674}
675
676struct ksym *ksym_search(long key)
677{
678 int start = 0, end = sym_cnt;
679 int result;
680
681 while (start < end) {
682 size_t mid = start + (end - start) / 2;
683
684 result = key - syms[mid].addr;
685 if (result < 0)
686 end = mid;
687 else if (result > 0)
688 start = mid + 1;
689 else
690 return &syms[mid];
691 }
692
693 if (start >= 1 && syms[start - 1].addr < key &&
694 key < syms[start].addr)
695 /* valid ksym */
696 return &syms[start - 1];
697
698 /* out of range. return _stext */
699 return &syms[0];
700}
12d8bb64 701