]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - tools/lib/bpf/libbpf.c
cda652a1288052c07179e2a28df75228a2000aaa
[mirror_ubuntu-bionic-kernel.git] / tools / lib / bpf / libbpf.c
1 /*
2 * Common eBPF ELF object loading operations.
3 *
4 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
5 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
6 * Copyright (C) 2015 Huawei Inc.
7 * Copyright (C) 2017 Nicira, Inc.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation;
12 * version 2.1 of the License (not later!)
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this program; if not, see <http://www.gnu.org/licenses>
21 */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <libgen.h>
27 #include <inttypes.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <asm/unistd.h>
33 #include <linux/err.h>
34 #include <linux/kernel.h>
35 #include <linux/bpf.h>
36 #include <linux/list.h>
37 #include <linux/limits.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <sys/vfs.h>
41 #include <libelf.h>
42 #include <gelf.h>
43
44 #include "libbpf.h"
45 #include "bpf.h"
46
47 #ifndef EM_BPF
48 #define EM_BPF 247
49 #endif
50
51 #ifndef BPF_FS_MAGIC
52 #define BPF_FS_MAGIC 0xcafe4a11
53 #endif
54
55 #define __printf(a, b) __attribute__((format(printf, a, b)))
56
57 __printf(1, 2)
58 static int __base_pr(const char *format, ...)
59 {
60 va_list args;
61 int err;
62
63 va_start(args, format);
64 err = vfprintf(stderr, format, args);
65 va_end(args);
66 return err;
67 }
68
69 static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
70 static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
71 static __printf(1, 2) libbpf_print_fn_t __pr_debug;
72
73 #define __pr(func, fmt, ...) \
74 do { \
75 if ((func)) \
76 (func)("libbpf: " fmt, ##__VA_ARGS__); \
77 } while (0)
78
79 #define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
80 #define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
81 #define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
82
83 void libbpf_set_print(libbpf_print_fn_t warn,
84 libbpf_print_fn_t info,
85 libbpf_print_fn_t debug)
86 {
87 __pr_warning = warn;
88 __pr_info = info;
89 __pr_debug = debug;
90 }
91
92 #define STRERR_BUFSIZE 128
93
94 #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
95 #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
96 #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
97
98 static const char *libbpf_strerror_table[NR_ERRNO] = {
99 [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
100 [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
101 [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
102 [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
103 [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
104 [ERRCODE_OFFSET(RELOC)] = "Relocation failed",
105 [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
106 [ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
107 [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
108 [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
109 };
110
111 int libbpf_strerror(int err, char *buf, size_t size)
112 {
113 if (!buf || !size)
114 return -1;
115
116 err = err > 0 ? err : -err;
117
118 if (err < __LIBBPF_ERRNO__START) {
119 int ret;
120
121 ret = strerror_r(err, buf, size);
122 buf[size - 1] = '\0';
123 return ret;
124 }
125
126 if (err < __LIBBPF_ERRNO__END) {
127 const char *msg;
128
129 msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
130 snprintf(buf, size, "%s", msg);
131 buf[size - 1] = '\0';
132 return 0;
133 }
134
135 snprintf(buf, size, "Unknown libbpf error %d", err);
136 buf[size - 1] = '\0';
137 return -1;
138 }
139
140 #define CHECK_ERR(action, err, out) do { \
141 err = action; \
142 if (err) \
143 goto out; \
144 } while(0)
145
146
147 /* Copied from tools/perf/util/util.h */
148 #ifndef zfree
149 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
150 #endif
151
152 #ifndef zclose
153 # define zclose(fd) ({ \
154 int ___err = 0; \
155 if ((fd) >= 0) \
156 ___err = close((fd)); \
157 fd = -1; \
158 ___err; })
159 #endif
160
161 #ifdef HAVE_LIBELF_MMAP_SUPPORT
162 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
163 #else
164 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
165 #endif
166
167 /*
168 * bpf_prog should be a better name but it has been used in
169 * linux/filter.h.
170 */
171 struct bpf_program {
172 /* Index in elf obj file, for relocation use. */
173 int idx;
174 char *name;
175 char *section_name;
176 struct bpf_insn *insns;
177 size_t insns_cnt;
178 enum bpf_prog_type type;
179
180 struct {
181 int insn_idx;
182 int map_idx;
183 } *reloc_desc;
184 int nr_reloc;
185
186 struct {
187 int nr;
188 int *fds;
189 } instances;
190 bpf_program_prep_t preprocessor;
191
192 struct bpf_object *obj;
193 void *priv;
194 bpf_program_clear_priv_t clear_priv;
195 };
196
197 struct bpf_map {
198 int fd;
199 char *name;
200 size_t offset;
201 struct bpf_map_def def;
202 void *priv;
203 bpf_map_clear_priv_t clear_priv;
204 };
205
206 static LIST_HEAD(bpf_objects_list);
207
208 struct bpf_object {
209 char license[64];
210 u32 kern_version;
211
212 struct bpf_program *programs;
213 size_t nr_programs;
214 struct bpf_map *maps;
215 size_t nr_maps;
216
217 bool loaded;
218
219 /*
220 * Information when doing elf related work. Only valid if fd
221 * is valid.
222 */
223 struct {
224 int fd;
225 void *obj_buf;
226 size_t obj_buf_sz;
227 Elf *elf;
228 GElf_Ehdr ehdr;
229 Elf_Data *symbols;
230 size_t strtabidx;
231 struct {
232 GElf_Shdr shdr;
233 Elf_Data *data;
234 } *reloc;
235 int nr_reloc;
236 int maps_shndx;
237 } efile;
238 /*
239 * All loaded bpf_object is linked in a list, which is
240 * hidden to caller. bpf_objects__<func> handlers deal with
241 * all objects.
242 */
243 struct list_head list;
244
245 void *priv;
246 bpf_object_clear_priv_t clear_priv;
247
248 char path[];
249 };
250 #define obj_elf_valid(o) ((o)->efile.elf)
251
252 static void bpf_program__unload(struct bpf_program *prog)
253 {
254 int i;
255
256 if (!prog)
257 return;
258
259 /*
260 * If the object is opened but the program was never loaded,
261 * it is possible that prog->instances.nr == -1.
262 */
263 if (prog->instances.nr > 0) {
264 for (i = 0; i < prog->instances.nr; i++)
265 zclose(prog->instances.fds[i]);
266 } else if (prog->instances.nr != -1) {
267 pr_warning("Internal error: instances.nr is %d\n",
268 prog->instances.nr);
269 }
270
271 prog->instances.nr = -1;
272 zfree(&prog->instances.fds);
273 }
274
275 static void bpf_program__exit(struct bpf_program *prog)
276 {
277 if (!prog)
278 return;
279
280 if (prog->clear_priv)
281 prog->clear_priv(prog, prog->priv);
282
283 prog->priv = NULL;
284 prog->clear_priv = NULL;
285
286 bpf_program__unload(prog);
287 zfree(&prog->name);
288 zfree(&prog->section_name);
289 zfree(&prog->insns);
290 zfree(&prog->reloc_desc);
291
292 prog->nr_reloc = 0;
293 prog->insns_cnt = 0;
294 prog->idx = -1;
295 }
296
297 static int
298 bpf_program__init(void *data, size_t size, char *section_name, int idx,
299 struct bpf_program *prog)
300 {
301 if (size < sizeof(struct bpf_insn)) {
302 pr_warning("corrupted section '%s'\n", section_name);
303 return -EINVAL;
304 }
305
306 bzero(prog, sizeof(*prog));
307
308 prog->section_name = strdup(section_name);
309 if (!prog->section_name) {
310 pr_warning("failed to alloc name for prog under section %s\n",
311 section_name);
312 goto errout;
313 }
314
315 prog->insns = malloc(size);
316 if (!prog->insns) {
317 pr_warning("failed to alloc insns for prog under section %s\n",
318 section_name);
319 goto errout;
320 }
321 prog->insns_cnt = size / sizeof(struct bpf_insn);
322 memcpy(prog->insns, data,
323 prog->insns_cnt * sizeof(struct bpf_insn));
324 prog->idx = idx;
325 prog->instances.fds = NULL;
326 prog->instances.nr = -1;
327 prog->type = BPF_PROG_TYPE_KPROBE;
328
329 return 0;
330 errout:
331 bpf_program__exit(prog);
332 return -ENOMEM;
333 }
334
335 static int
336 bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
337 char *section_name, int idx)
338 {
339 struct bpf_program prog, *progs;
340 int nr_progs, err;
341
342 err = bpf_program__init(data, size, section_name, idx, &prog);
343 if (err)
344 return err;
345
346 progs = obj->programs;
347 nr_progs = obj->nr_programs;
348
349 progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
350 if (!progs) {
351 /*
352 * In this case the original obj->programs
353 * is still valid, so don't need special treat for
354 * bpf_close_object().
355 */
356 pr_warning("failed to alloc a new program under section '%s'\n",
357 section_name);
358 bpf_program__exit(&prog);
359 return -ENOMEM;
360 }
361
362 pr_debug("found program %s\n", prog.section_name);
363 obj->programs = progs;
364 obj->nr_programs = nr_progs + 1;
365 prog.obj = obj;
366 progs[nr_progs] = prog;
367 return 0;
368 }
369
370 static int
371 bpf_object__init_prog_names(struct bpf_object *obj)
372 {
373 Elf_Data *symbols = obj->efile.symbols;
374 struct bpf_program *prog;
375 size_t pi, si;
376
377 for (pi = 0; pi < obj->nr_programs; pi++) {
378 char *name = NULL;
379
380 prog = &obj->programs[pi];
381
382 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
383 si++) {
384 GElf_Sym sym;
385
386 if (!gelf_getsym(symbols, si, &sym))
387 continue;
388 if (sym.st_shndx != prog->idx)
389 continue;
390 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
391 continue;
392
393 name = elf_strptr(obj->efile.elf,
394 obj->efile.strtabidx,
395 sym.st_name);
396 if (!name) {
397 pr_warning("failed to get sym name string for prog %s\n",
398 prog->section_name);
399 return -LIBBPF_ERRNO__LIBELF;
400 }
401 }
402
403 if (!name) {
404 pr_warning("failed to find sym for prog %s\n",
405 prog->section_name);
406 return -EINVAL;
407 }
408
409 prog->name = strdup(name);
410 if (!prog->name) {
411 pr_warning("failed to allocate memory for prog sym %s\n",
412 name);
413 return -ENOMEM;
414 }
415 }
416
417 return 0;
418 }
419
420 static struct bpf_object *bpf_object__new(const char *path,
421 void *obj_buf,
422 size_t obj_buf_sz)
423 {
424 struct bpf_object *obj;
425
426 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
427 if (!obj) {
428 pr_warning("alloc memory failed for %s\n", path);
429 return ERR_PTR(-ENOMEM);
430 }
431
432 strcpy(obj->path, path);
433 obj->efile.fd = -1;
434
435 /*
436 * Caller of this function should also calls
437 * bpf_object__elf_finish() after data collection to return
438 * obj_buf to user. If not, we should duplicate the buffer to
439 * avoid user freeing them before elf finish.
440 */
441 obj->efile.obj_buf = obj_buf;
442 obj->efile.obj_buf_sz = obj_buf_sz;
443 obj->efile.maps_shndx = -1;
444
445 obj->loaded = false;
446
447 INIT_LIST_HEAD(&obj->list);
448 list_add(&obj->list, &bpf_objects_list);
449 return obj;
450 }
451
452 static void bpf_object__elf_finish(struct bpf_object *obj)
453 {
454 if (!obj_elf_valid(obj))
455 return;
456
457 if (obj->efile.elf) {
458 elf_end(obj->efile.elf);
459 obj->efile.elf = NULL;
460 }
461 obj->efile.symbols = NULL;
462
463 zfree(&obj->efile.reloc);
464 obj->efile.nr_reloc = 0;
465 zclose(obj->efile.fd);
466 obj->efile.obj_buf = NULL;
467 obj->efile.obj_buf_sz = 0;
468 }
469
470 static int bpf_object__elf_init(struct bpf_object *obj)
471 {
472 int err = 0;
473 GElf_Ehdr *ep;
474
475 if (obj_elf_valid(obj)) {
476 pr_warning("elf init: internal error\n");
477 return -LIBBPF_ERRNO__LIBELF;
478 }
479
480 if (obj->efile.obj_buf_sz > 0) {
481 /*
482 * obj_buf should have been validated by
483 * bpf_object__open_buffer().
484 */
485 obj->efile.elf = elf_memory(obj->efile.obj_buf,
486 obj->efile.obj_buf_sz);
487 } else {
488 obj->efile.fd = open(obj->path, O_RDONLY);
489 if (obj->efile.fd < 0) {
490 pr_warning("failed to open %s: %s\n", obj->path,
491 strerror(errno));
492 return -errno;
493 }
494
495 obj->efile.elf = elf_begin(obj->efile.fd,
496 LIBBPF_ELF_C_READ_MMAP,
497 NULL);
498 }
499
500 if (!obj->efile.elf) {
501 pr_warning("failed to open %s as ELF file\n",
502 obj->path);
503 err = -LIBBPF_ERRNO__LIBELF;
504 goto errout;
505 }
506
507 if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
508 pr_warning("failed to get EHDR from %s\n",
509 obj->path);
510 err = -LIBBPF_ERRNO__FORMAT;
511 goto errout;
512 }
513 ep = &obj->efile.ehdr;
514
515 /* Old LLVM set e_machine to EM_NONE */
516 if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) {
517 pr_warning("%s is not an eBPF object file\n",
518 obj->path);
519 err = -LIBBPF_ERRNO__FORMAT;
520 goto errout;
521 }
522
523 return 0;
524 errout:
525 bpf_object__elf_finish(obj);
526 return err;
527 }
528
529 static int
530 bpf_object__check_endianness(struct bpf_object *obj)
531 {
532 static unsigned int const endian = 1;
533
534 switch (obj->efile.ehdr.e_ident[EI_DATA]) {
535 case ELFDATA2LSB:
536 /* We are big endian, BPF obj is little endian. */
537 if (*(unsigned char const *)&endian != 1)
538 goto mismatch;
539 break;
540
541 case ELFDATA2MSB:
542 /* We are little endian, BPF obj is big endian. */
543 if (*(unsigned char const *)&endian != 0)
544 goto mismatch;
545 break;
546 default:
547 return -LIBBPF_ERRNO__ENDIAN;
548 }
549
550 return 0;
551
552 mismatch:
553 pr_warning("Error: endianness mismatch.\n");
554 return -LIBBPF_ERRNO__ENDIAN;
555 }
556
557 static int
558 bpf_object__init_license(struct bpf_object *obj,
559 void *data, size_t size)
560 {
561 memcpy(obj->license, data,
562 min(size, sizeof(obj->license) - 1));
563 pr_debug("license of %s is %s\n", obj->path, obj->license);
564 return 0;
565 }
566
567 static int
568 bpf_object__init_kversion(struct bpf_object *obj,
569 void *data, size_t size)
570 {
571 u32 kver;
572
573 if (size != sizeof(kver)) {
574 pr_warning("invalid kver section in %s\n", obj->path);
575 return -LIBBPF_ERRNO__FORMAT;
576 }
577 memcpy(&kver, data, sizeof(kver));
578 obj->kern_version = kver;
579 pr_debug("kernel version of %s is %x\n", obj->path,
580 obj->kern_version);
581 return 0;
582 }
583
584 static int compare_bpf_map(const void *_a, const void *_b)
585 {
586 const struct bpf_map *a = _a;
587 const struct bpf_map *b = _b;
588
589 return a->offset - b->offset;
590 }
591
592 static int
593 bpf_object__init_maps(struct bpf_object *obj)
594 {
595 int i, map_idx, map_def_sz, nr_maps = 0;
596 Elf_Scn *scn;
597 Elf_Data *data;
598 Elf_Data *symbols = obj->efile.symbols;
599
600 if (obj->efile.maps_shndx < 0)
601 return -EINVAL;
602 if (!symbols)
603 return -EINVAL;
604
605 scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
606 if (scn)
607 data = elf_getdata(scn, NULL);
608 if (!scn || !data) {
609 pr_warning("failed to get Elf_Data from map section %d\n",
610 obj->efile.maps_shndx);
611 return -EINVAL;
612 }
613
614 /*
615 * Count number of maps. Each map has a name.
616 * Array of maps is not supported: only the first element is
617 * considered.
618 *
619 * TODO: Detect array of map and report error.
620 */
621 for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
622 GElf_Sym sym;
623
624 if (!gelf_getsym(symbols, i, &sym))
625 continue;
626 if (sym.st_shndx != obj->efile.maps_shndx)
627 continue;
628 nr_maps++;
629 }
630
631 /* Alloc obj->maps and fill nr_maps. */
632 pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
633 nr_maps, data->d_size);
634
635 if (!nr_maps)
636 return 0;
637
638 /* Assume equally sized map definitions */
639 map_def_sz = data->d_size / nr_maps;
640 if (!data->d_size || (data->d_size % nr_maps) != 0) {
641 pr_warning("unable to determine map definition size "
642 "section %s, %d maps in %zd bytes\n",
643 obj->path, nr_maps, data->d_size);
644 return -EINVAL;
645 }
646
647 obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
648 if (!obj->maps) {
649 pr_warning("alloc maps for object failed\n");
650 return -ENOMEM;
651 }
652 obj->nr_maps = nr_maps;
653
654 /*
655 * fill all fd with -1 so won't close incorrect
656 * fd (fd=0 is stdin) when failure (zclose won't close
657 * negative fd)).
658 */
659 for (i = 0; i < nr_maps; i++)
660 obj->maps[i].fd = -1;
661
662 /*
663 * Fill obj->maps using data in "maps" section.
664 */
665 for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
666 GElf_Sym sym;
667 const char *map_name;
668 struct bpf_map_def *def;
669
670 if (!gelf_getsym(symbols, i, &sym))
671 continue;
672 if (sym.st_shndx != obj->efile.maps_shndx)
673 continue;
674
675 map_name = elf_strptr(obj->efile.elf,
676 obj->efile.strtabidx,
677 sym.st_name);
678 obj->maps[map_idx].offset = sym.st_value;
679 if (sym.st_value + map_def_sz > data->d_size) {
680 pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
681 obj->path, map_name);
682 return -EINVAL;
683 }
684
685 obj->maps[map_idx].name = strdup(map_name);
686 if (!obj->maps[map_idx].name) {
687 pr_warning("failed to alloc map name\n");
688 return -ENOMEM;
689 }
690 pr_debug("map %d is \"%s\"\n", map_idx,
691 obj->maps[map_idx].name);
692 def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
693 /*
694 * If the definition of the map in the object file fits in
695 * bpf_map_def, copy it. Any extra fields in our version
696 * of bpf_map_def will default to zero as a result of the
697 * calloc above.
698 */
699 if (map_def_sz <= sizeof(struct bpf_map_def)) {
700 memcpy(&obj->maps[map_idx].def, def, map_def_sz);
701 } else {
702 /*
703 * Here the map structure being read is bigger than what
704 * we expect, truncate if the excess bits are all zero.
705 * If they are not zero, reject this map as
706 * incompatible.
707 */
708 char *b;
709 for (b = ((char *)def) + sizeof(struct bpf_map_def);
710 b < ((char *)def) + map_def_sz; b++) {
711 if (*b != 0) {
712 pr_warning("maps section in %s: \"%s\" "
713 "has unrecognized, non-zero "
714 "options\n",
715 obj->path, map_name);
716 return -EINVAL;
717 }
718 }
719 memcpy(&obj->maps[map_idx].def, def,
720 sizeof(struct bpf_map_def));
721 }
722 map_idx++;
723 }
724
725 qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map);
726 return 0;
727 }
728
729 static int bpf_object__elf_collect(struct bpf_object *obj)
730 {
731 Elf *elf = obj->efile.elf;
732 GElf_Ehdr *ep = &obj->efile.ehdr;
733 Elf_Scn *scn = NULL;
734 int idx = 0, err = 0;
735
736 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
737 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
738 pr_warning("failed to get e_shstrndx from %s\n",
739 obj->path);
740 return -LIBBPF_ERRNO__FORMAT;
741 }
742
743 while ((scn = elf_nextscn(elf, scn)) != NULL) {
744 char *name;
745 GElf_Shdr sh;
746 Elf_Data *data;
747
748 idx++;
749 if (gelf_getshdr(scn, &sh) != &sh) {
750 pr_warning("failed to get section header from %s\n",
751 obj->path);
752 err = -LIBBPF_ERRNO__FORMAT;
753 goto out;
754 }
755
756 name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
757 if (!name) {
758 pr_warning("failed to get section name from %s\n",
759 obj->path);
760 err = -LIBBPF_ERRNO__FORMAT;
761 goto out;
762 }
763
764 data = elf_getdata(scn, 0);
765 if (!data) {
766 pr_warning("failed to get section data from %s(%s)\n",
767 name, obj->path);
768 err = -LIBBPF_ERRNO__FORMAT;
769 goto out;
770 }
771 pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
772 name, (unsigned long)data->d_size,
773 (int)sh.sh_link, (unsigned long)sh.sh_flags,
774 (int)sh.sh_type);
775
776 if (strcmp(name, "license") == 0)
777 err = bpf_object__init_license(obj,
778 data->d_buf,
779 data->d_size);
780 else if (strcmp(name, "version") == 0)
781 err = bpf_object__init_kversion(obj,
782 data->d_buf,
783 data->d_size);
784 else if (strcmp(name, "maps") == 0)
785 obj->efile.maps_shndx = idx;
786 else if (sh.sh_type == SHT_SYMTAB) {
787 if (obj->efile.symbols) {
788 pr_warning("bpf: multiple SYMTAB in %s\n",
789 obj->path);
790 err = -LIBBPF_ERRNO__FORMAT;
791 } else {
792 obj->efile.symbols = data;
793 obj->efile.strtabidx = sh.sh_link;
794 }
795 } else if ((sh.sh_type == SHT_PROGBITS) &&
796 (sh.sh_flags & SHF_EXECINSTR) &&
797 (data->d_size > 0)) {
798 err = bpf_object__add_program(obj, data->d_buf,
799 data->d_size, name, idx);
800 if (err) {
801 char errmsg[STRERR_BUFSIZE];
802
803 strerror_r(-err, errmsg, sizeof(errmsg));
804 pr_warning("failed to alloc program %s (%s): %s",
805 name, obj->path, errmsg);
806 }
807 } else if (sh.sh_type == SHT_REL) {
808 void *reloc = obj->efile.reloc;
809 int nr_reloc = obj->efile.nr_reloc + 1;
810
811 reloc = realloc(reloc,
812 sizeof(*obj->efile.reloc) * nr_reloc);
813 if (!reloc) {
814 pr_warning("realloc failed\n");
815 err = -ENOMEM;
816 } else {
817 int n = nr_reloc - 1;
818
819 obj->efile.reloc = reloc;
820 obj->efile.nr_reloc = nr_reloc;
821
822 obj->efile.reloc[n].shdr = sh;
823 obj->efile.reloc[n].data = data;
824 }
825 }
826 if (err)
827 goto out;
828 }
829
830 if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
831 pr_warning("Corrupted ELF file: index of strtab invalid\n");
832 return LIBBPF_ERRNO__FORMAT;
833 }
834 if (obj->efile.maps_shndx >= 0) {
835 err = bpf_object__init_maps(obj);
836 if (err)
837 goto out;
838 }
839 err = bpf_object__init_prog_names(obj);
840 out:
841 return err;
842 }
843
844 static struct bpf_program *
845 bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
846 {
847 struct bpf_program *prog;
848 size_t i;
849
850 for (i = 0; i < obj->nr_programs; i++) {
851 prog = &obj->programs[i];
852 if (prog->idx == idx)
853 return prog;
854 }
855 return NULL;
856 }
857
858 static int
859 bpf_program__collect_reloc(struct bpf_program *prog,
860 size_t nr_maps, GElf_Shdr *shdr,
861 Elf_Data *data, Elf_Data *symbols,
862 int maps_shndx, struct bpf_map *maps)
863 {
864 int i, nrels;
865
866 pr_debug("collecting relocating info for: '%s'\n",
867 prog->section_name);
868 nrels = shdr->sh_size / shdr->sh_entsize;
869
870 prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
871 if (!prog->reloc_desc) {
872 pr_warning("failed to alloc memory in relocation\n");
873 return -ENOMEM;
874 }
875 prog->nr_reloc = nrels;
876
877 for (i = 0; i < nrels; i++) {
878 GElf_Sym sym;
879 GElf_Rel rel;
880 unsigned int insn_idx;
881 struct bpf_insn *insns = prog->insns;
882 size_t map_idx;
883
884 if (!gelf_getrel(data, i, &rel)) {
885 pr_warning("relocation: failed to get %d reloc\n", i);
886 return -LIBBPF_ERRNO__FORMAT;
887 }
888
889 if (!gelf_getsym(symbols,
890 GELF_R_SYM(rel.r_info),
891 &sym)) {
892 pr_warning("relocation: symbol %"PRIx64" not found\n",
893 GELF_R_SYM(rel.r_info));
894 return -LIBBPF_ERRNO__FORMAT;
895 }
896
897 if (sym.st_shndx != maps_shndx) {
898 pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
899 prog->section_name, sym.st_shndx);
900 return -LIBBPF_ERRNO__RELOC;
901 }
902
903 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
904 pr_debug("relocation: insn_idx=%u\n", insn_idx);
905
906 if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
907 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
908 insn_idx, insns[insn_idx].code);
909 return -LIBBPF_ERRNO__RELOC;
910 }
911
912 /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
913 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
914 if (maps[map_idx].offset == sym.st_value) {
915 pr_debug("relocation: find map %zd (%s) for insn %u\n",
916 map_idx, maps[map_idx].name, insn_idx);
917 break;
918 }
919 }
920
921 if (map_idx >= nr_maps) {
922 pr_warning("bpf relocation: map_idx %d large than %d\n",
923 (int)map_idx, (int)nr_maps - 1);
924 return -LIBBPF_ERRNO__RELOC;
925 }
926
927 prog->reloc_desc[i].insn_idx = insn_idx;
928 prog->reloc_desc[i].map_idx = map_idx;
929 }
930 return 0;
931 }
932
933 static int
934 bpf_object__create_maps(struct bpf_object *obj)
935 {
936 unsigned int i;
937
938 for (i = 0; i < obj->nr_maps; i++) {
939 struct bpf_map_def *def = &obj->maps[i].def;
940 int *pfd = &obj->maps[i].fd;
941
942 *pfd = bpf_create_map_name(def->type,
943 obj->maps[i].name,
944 def->key_size,
945 def->value_size,
946 def->max_entries,
947 def->map_flags);
948 if (*pfd < 0) {
949 size_t j;
950 int err = *pfd;
951
952 pr_warning("failed to create map (name: '%s'): %s\n",
953 obj->maps[i].name,
954 strerror(errno));
955 for (j = 0; j < i; j++)
956 zclose(obj->maps[j].fd);
957 return err;
958 }
959 pr_debug("create map %s: fd=%d\n", obj->maps[i].name, *pfd);
960 }
961
962 return 0;
963 }
964
965 static int
966 bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
967 {
968 int i;
969
970 if (!prog || !prog->reloc_desc)
971 return 0;
972
973 for (i = 0; i < prog->nr_reloc; i++) {
974 int insn_idx, map_idx;
975 struct bpf_insn *insns = prog->insns;
976
977 insn_idx = prog->reloc_desc[i].insn_idx;
978 map_idx = prog->reloc_desc[i].map_idx;
979
980 if (insn_idx >= (int)prog->insns_cnt) {
981 pr_warning("relocation out of range: '%s'\n",
982 prog->section_name);
983 return -LIBBPF_ERRNO__RELOC;
984 }
985 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
986 insns[insn_idx].imm = obj->maps[map_idx].fd;
987 }
988
989 zfree(&prog->reloc_desc);
990 prog->nr_reloc = 0;
991 return 0;
992 }
993
994
995 static int
996 bpf_object__relocate(struct bpf_object *obj)
997 {
998 struct bpf_program *prog;
999 size_t i;
1000 int err;
1001
1002 for (i = 0; i < obj->nr_programs; i++) {
1003 prog = &obj->programs[i];
1004
1005 err = bpf_program__relocate(prog, obj);
1006 if (err) {
1007 pr_warning("failed to relocate '%s'\n",
1008 prog->section_name);
1009 return err;
1010 }
1011 }
1012 return 0;
1013 }
1014
1015 static int bpf_object__collect_reloc(struct bpf_object *obj)
1016 {
1017 int i, err;
1018
1019 if (!obj_elf_valid(obj)) {
1020 pr_warning("Internal error: elf object is closed\n");
1021 return -LIBBPF_ERRNO__INTERNAL;
1022 }
1023
1024 for (i = 0; i < obj->efile.nr_reloc; i++) {
1025 GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
1026 Elf_Data *data = obj->efile.reloc[i].data;
1027 int idx = shdr->sh_info;
1028 struct bpf_program *prog;
1029 size_t nr_maps = obj->nr_maps;
1030
1031 if (shdr->sh_type != SHT_REL) {
1032 pr_warning("internal error at %d\n", __LINE__);
1033 return -LIBBPF_ERRNO__INTERNAL;
1034 }
1035
1036 prog = bpf_object__find_prog_by_idx(obj, idx);
1037 if (!prog) {
1038 pr_warning("relocation failed: no %d section\n",
1039 idx);
1040 return -LIBBPF_ERRNO__RELOC;
1041 }
1042
1043 err = bpf_program__collect_reloc(prog, nr_maps,
1044 shdr, data,
1045 obj->efile.symbols,
1046 obj->efile.maps_shndx,
1047 obj->maps);
1048 if (err)
1049 return err;
1050 }
1051 return 0;
1052 }
1053
1054 static int
1055 load_program(enum bpf_prog_type type, const char *name, struct bpf_insn *insns,
1056 int insns_cnt, char *license, u32 kern_version, int *pfd)
1057 {
1058 int ret;
1059 char *log_buf;
1060
1061 if (!insns || !insns_cnt)
1062 return -EINVAL;
1063
1064 log_buf = malloc(BPF_LOG_BUF_SIZE);
1065 if (!log_buf)
1066 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
1067
1068 ret = bpf_load_program_name(type, name, insns, insns_cnt, license,
1069 kern_version, log_buf, BPF_LOG_BUF_SIZE);
1070
1071 if (ret >= 0) {
1072 *pfd = ret;
1073 ret = 0;
1074 goto out;
1075 }
1076
1077 ret = -LIBBPF_ERRNO__LOAD;
1078 pr_warning("load bpf program failed: %s\n", strerror(errno));
1079
1080 if (log_buf && log_buf[0] != '\0') {
1081 ret = -LIBBPF_ERRNO__VERIFY;
1082 pr_warning("-- BEGIN DUMP LOG ---\n");
1083 pr_warning("\n%s\n", log_buf);
1084 pr_warning("-- END LOG --\n");
1085 } else if (insns_cnt >= BPF_MAXINSNS) {
1086 pr_warning("Program too large (%d insns), at most %d insns\n",
1087 insns_cnt, BPF_MAXINSNS);
1088 ret = -LIBBPF_ERRNO__PROG2BIG;
1089 } else {
1090 /* Wrong program type? */
1091 if (type != BPF_PROG_TYPE_KPROBE) {
1092 int fd;
1093
1094 fd = bpf_load_program_name(BPF_PROG_TYPE_KPROBE, name,
1095 insns, insns_cnt, license,
1096 kern_version, NULL, 0);
1097 if (fd >= 0) {
1098 close(fd);
1099 ret = -LIBBPF_ERRNO__PROGTYPE;
1100 goto out;
1101 }
1102 }
1103
1104 if (log_buf)
1105 ret = -LIBBPF_ERRNO__KVER;
1106 }
1107
1108 out:
1109 free(log_buf);
1110 return ret;
1111 }
1112
1113 static int
1114 bpf_program__load(struct bpf_program *prog,
1115 char *license, u32 kern_version)
1116 {
1117 int err = 0, fd, i;
1118
1119 if (prog->instances.nr < 0 || !prog->instances.fds) {
1120 if (prog->preprocessor) {
1121 pr_warning("Internal error: can't load program '%s'\n",
1122 prog->section_name);
1123 return -LIBBPF_ERRNO__INTERNAL;
1124 }
1125
1126 prog->instances.fds = malloc(sizeof(int));
1127 if (!prog->instances.fds) {
1128 pr_warning("Not enough memory for BPF fds\n");
1129 return -ENOMEM;
1130 }
1131 prog->instances.nr = 1;
1132 prog->instances.fds[0] = -1;
1133 }
1134
1135 if (!prog->preprocessor) {
1136 if (prog->instances.nr != 1) {
1137 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
1138 prog->section_name, prog->instances.nr);
1139 }
1140 err = load_program(prog->type, prog->name, prog->insns,
1141 prog->insns_cnt, license, kern_version, &fd);
1142 if (!err)
1143 prog->instances.fds[0] = fd;
1144 goto out;
1145 }
1146
1147 for (i = 0; i < prog->instances.nr; i++) {
1148 struct bpf_prog_prep_result result;
1149 bpf_program_prep_t preprocessor = prog->preprocessor;
1150
1151 bzero(&result, sizeof(result));
1152 err = preprocessor(prog, i, prog->insns,
1153 prog->insns_cnt, &result);
1154 if (err) {
1155 pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
1156 i, prog->section_name);
1157 goto out;
1158 }
1159
1160 if (!result.new_insn_ptr || !result.new_insn_cnt) {
1161 pr_debug("Skip loading the %dth instance of program '%s'\n",
1162 i, prog->section_name);
1163 prog->instances.fds[i] = -1;
1164 if (result.pfd)
1165 *result.pfd = -1;
1166 continue;
1167 }
1168
1169 err = load_program(prog->type, prog->name,
1170 result.new_insn_ptr,
1171 result.new_insn_cnt,
1172 license, kern_version, &fd);
1173
1174 if (err) {
1175 pr_warning("Loading the %dth instance of program '%s' failed\n",
1176 i, prog->section_name);
1177 goto out;
1178 }
1179
1180 if (result.pfd)
1181 *result.pfd = fd;
1182 prog->instances.fds[i] = fd;
1183 }
1184 out:
1185 if (err)
1186 pr_warning("failed to load program '%s'\n",
1187 prog->section_name);
1188 zfree(&prog->insns);
1189 prog->insns_cnt = 0;
1190 return err;
1191 }
1192
1193 static int
1194 bpf_object__load_progs(struct bpf_object *obj)
1195 {
1196 size_t i;
1197 int err;
1198
1199 for (i = 0; i < obj->nr_programs; i++) {
1200 err = bpf_program__load(&obj->programs[i],
1201 obj->license,
1202 obj->kern_version);
1203 if (err)
1204 return err;
1205 }
1206 return 0;
1207 }
1208
1209 static int bpf_object__validate(struct bpf_object *obj)
1210 {
1211 if (obj->kern_version == 0) {
1212 pr_warning("%s doesn't provide kernel version\n",
1213 obj->path);
1214 return -LIBBPF_ERRNO__KVERSION;
1215 }
1216 return 0;
1217 }
1218
1219 static struct bpf_object *
1220 __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
1221 {
1222 struct bpf_object *obj;
1223 int err;
1224
1225 if (elf_version(EV_CURRENT) == EV_NONE) {
1226 pr_warning("failed to init libelf for %s\n", path);
1227 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
1228 }
1229
1230 obj = bpf_object__new(path, obj_buf, obj_buf_sz);
1231 if (IS_ERR(obj))
1232 return obj;
1233
1234 CHECK_ERR(bpf_object__elf_init(obj), err, out);
1235 CHECK_ERR(bpf_object__check_endianness(obj), err, out);
1236 CHECK_ERR(bpf_object__elf_collect(obj), err, out);
1237 CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
1238 CHECK_ERR(bpf_object__validate(obj), err, out);
1239
1240 bpf_object__elf_finish(obj);
1241 return obj;
1242 out:
1243 bpf_object__close(obj);
1244 return ERR_PTR(err);
1245 }
1246
1247 struct bpf_object *bpf_object__open(const char *path)
1248 {
1249 /* param validation */
1250 if (!path)
1251 return NULL;
1252
1253 pr_debug("loading %s\n", path);
1254
1255 return __bpf_object__open(path, NULL, 0);
1256 }
1257
1258 struct bpf_object *bpf_object__open_buffer(void *obj_buf,
1259 size_t obj_buf_sz,
1260 const char *name)
1261 {
1262 char tmp_name[64];
1263
1264 /* param validation */
1265 if (!obj_buf || obj_buf_sz <= 0)
1266 return NULL;
1267
1268 if (!name) {
1269 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
1270 (unsigned long)obj_buf,
1271 (unsigned long)obj_buf_sz);
1272 tmp_name[sizeof(tmp_name) - 1] = '\0';
1273 name = tmp_name;
1274 }
1275 pr_debug("loading object '%s' from buffer\n",
1276 name);
1277
1278 return __bpf_object__open(name, obj_buf, obj_buf_sz);
1279 }
1280
1281 int bpf_object__unload(struct bpf_object *obj)
1282 {
1283 size_t i;
1284
1285 if (!obj)
1286 return -EINVAL;
1287
1288 for (i = 0; i < obj->nr_maps; i++)
1289 zclose(obj->maps[i].fd);
1290
1291 for (i = 0; i < obj->nr_programs; i++)
1292 bpf_program__unload(&obj->programs[i]);
1293
1294 return 0;
1295 }
1296
1297 int bpf_object__load(struct bpf_object *obj)
1298 {
1299 int err;
1300
1301 if (!obj)
1302 return -EINVAL;
1303
1304 if (obj->loaded) {
1305 pr_warning("object should not be loaded twice\n");
1306 return -EINVAL;
1307 }
1308
1309 obj->loaded = true;
1310
1311 CHECK_ERR(bpf_object__create_maps(obj), err, out);
1312 CHECK_ERR(bpf_object__relocate(obj), err, out);
1313 CHECK_ERR(bpf_object__load_progs(obj), err, out);
1314
1315 return 0;
1316 out:
1317 bpf_object__unload(obj);
1318 pr_warning("failed to load object '%s'\n", obj->path);
1319 return err;
1320 }
1321
1322 static int check_path(const char *path)
1323 {
1324 struct statfs st_fs;
1325 char *dname, *dir;
1326 int err = 0;
1327
1328 if (path == NULL)
1329 return -EINVAL;
1330
1331 dname = strdup(path);
1332 if (dname == NULL)
1333 return -ENOMEM;
1334
1335 dir = dirname(dname);
1336 if (statfs(dir, &st_fs)) {
1337 pr_warning("failed to statfs %s: %s\n", dir, strerror(errno));
1338 err = -errno;
1339 }
1340 free(dname);
1341
1342 if (!err && st_fs.f_type != BPF_FS_MAGIC) {
1343 pr_warning("specified path %s is not on BPF FS\n", path);
1344 err = -EINVAL;
1345 }
1346
1347 return err;
1348 }
1349
1350 int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
1351 int instance)
1352 {
1353 int err;
1354
1355 err = check_path(path);
1356 if (err)
1357 return err;
1358
1359 if (prog == NULL) {
1360 pr_warning("invalid program pointer\n");
1361 return -EINVAL;
1362 }
1363
1364 if (instance < 0 || instance >= prog->instances.nr) {
1365 pr_warning("invalid prog instance %d of prog %s (max %d)\n",
1366 instance, prog->section_name, prog->instances.nr);
1367 return -EINVAL;
1368 }
1369
1370 if (bpf_obj_pin(prog->instances.fds[instance], path)) {
1371 pr_warning("failed to pin program: %s\n", strerror(errno));
1372 return -errno;
1373 }
1374 pr_debug("pinned program '%s'\n", path);
1375
1376 return 0;
1377 }
1378
1379 static int make_dir(const char *path)
1380 {
1381 int err = 0;
1382
1383 if (mkdir(path, 0700) && errno != EEXIST)
1384 err = -errno;
1385
1386 if (err)
1387 pr_warning("failed to mkdir %s: %s\n", path, strerror(-err));
1388 return err;
1389 }
1390
1391 int bpf_program__pin(struct bpf_program *prog, const char *path)
1392 {
1393 int i, err;
1394
1395 err = check_path(path);
1396 if (err)
1397 return err;
1398
1399 if (prog == NULL) {
1400 pr_warning("invalid program pointer\n");
1401 return -EINVAL;
1402 }
1403
1404 if (prog->instances.nr <= 0) {
1405 pr_warning("no instances of prog %s to pin\n",
1406 prog->section_name);
1407 return -EINVAL;
1408 }
1409
1410 err = make_dir(path);
1411 if (err)
1412 return err;
1413
1414 for (i = 0; i < prog->instances.nr; i++) {
1415 char buf[PATH_MAX];
1416 int len;
1417
1418 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
1419 if (len < 0)
1420 return -EINVAL;
1421 else if (len >= PATH_MAX)
1422 return -ENAMETOOLONG;
1423
1424 err = bpf_program__pin_instance(prog, buf, i);
1425 if (err)
1426 return err;
1427 }
1428
1429 return 0;
1430 }
1431
1432 int bpf_map__pin(struct bpf_map *map, const char *path)
1433 {
1434 int err;
1435
1436 err = check_path(path);
1437 if (err)
1438 return err;
1439
1440 if (map == NULL) {
1441 pr_warning("invalid map pointer\n");
1442 return -EINVAL;
1443 }
1444
1445 if (bpf_obj_pin(map->fd, path)) {
1446 pr_warning("failed to pin map: %s\n", strerror(errno));
1447 return -errno;
1448 }
1449
1450 pr_debug("pinned map '%s'\n", path);
1451 return 0;
1452 }
1453
1454 int bpf_object__pin(struct bpf_object *obj, const char *path)
1455 {
1456 struct bpf_program *prog;
1457 struct bpf_map *map;
1458 int err;
1459
1460 if (!obj)
1461 return -ENOENT;
1462
1463 if (!obj->loaded) {
1464 pr_warning("object not yet loaded; load it first\n");
1465 return -ENOENT;
1466 }
1467
1468 err = make_dir(path);
1469 if (err)
1470 return err;
1471
1472 bpf_map__for_each(map, obj) {
1473 char buf[PATH_MAX];
1474 int len;
1475
1476 len = snprintf(buf, PATH_MAX, "%s/%s", path,
1477 bpf_map__name(map));
1478 if (len < 0)
1479 return -EINVAL;
1480 else if (len >= PATH_MAX)
1481 return -ENAMETOOLONG;
1482
1483 err = bpf_map__pin(map, buf);
1484 if (err)
1485 return err;
1486 }
1487
1488 bpf_object__for_each_program(prog, obj) {
1489 char buf[PATH_MAX];
1490 int len;
1491
1492 len = snprintf(buf, PATH_MAX, "%s/%s", path,
1493 prog->section_name);
1494 if (len < 0)
1495 return -EINVAL;
1496 else if (len >= PATH_MAX)
1497 return -ENAMETOOLONG;
1498
1499 err = bpf_program__pin(prog, buf);
1500 if (err)
1501 return err;
1502 }
1503
1504 return 0;
1505 }
1506
1507 void bpf_object__close(struct bpf_object *obj)
1508 {
1509 size_t i;
1510
1511 if (!obj)
1512 return;
1513
1514 if (obj->clear_priv)
1515 obj->clear_priv(obj, obj->priv);
1516
1517 bpf_object__elf_finish(obj);
1518 bpf_object__unload(obj);
1519
1520 for (i = 0; i < obj->nr_maps; i++) {
1521 zfree(&obj->maps[i].name);
1522 if (obj->maps[i].clear_priv)
1523 obj->maps[i].clear_priv(&obj->maps[i],
1524 obj->maps[i].priv);
1525 obj->maps[i].priv = NULL;
1526 obj->maps[i].clear_priv = NULL;
1527 }
1528 zfree(&obj->maps);
1529 obj->nr_maps = 0;
1530
1531 if (obj->programs && obj->nr_programs) {
1532 for (i = 0; i < obj->nr_programs; i++)
1533 bpf_program__exit(&obj->programs[i]);
1534 }
1535 zfree(&obj->programs);
1536
1537 list_del(&obj->list);
1538 free(obj);
1539 }
1540
1541 struct bpf_object *
1542 bpf_object__next(struct bpf_object *prev)
1543 {
1544 struct bpf_object *next;
1545
1546 if (!prev)
1547 next = list_first_entry(&bpf_objects_list,
1548 struct bpf_object,
1549 list);
1550 else
1551 next = list_next_entry(prev, list);
1552
1553 /* Empty list is noticed here so don't need checking on entry. */
1554 if (&next->list == &bpf_objects_list)
1555 return NULL;
1556
1557 return next;
1558 }
1559
1560 const char *bpf_object__name(struct bpf_object *obj)
1561 {
1562 return obj ? obj->path : ERR_PTR(-EINVAL);
1563 }
1564
1565 unsigned int bpf_object__kversion(struct bpf_object *obj)
1566 {
1567 return obj ? obj->kern_version : 0;
1568 }
1569
1570 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
1571 bpf_object_clear_priv_t clear_priv)
1572 {
1573 if (obj->priv && obj->clear_priv)
1574 obj->clear_priv(obj, obj->priv);
1575
1576 obj->priv = priv;
1577 obj->clear_priv = clear_priv;
1578 return 0;
1579 }
1580
1581 void *bpf_object__priv(struct bpf_object *obj)
1582 {
1583 return obj ? obj->priv : ERR_PTR(-EINVAL);
1584 }
1585
1586 struct bpf_program *
1587 bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
1588 {
1589 size_t idx;
1590
1591 if (!obj->programs)
1592 return NULL;
1593 /* First handler */
1594 if (prev == NULL)
1595 return &obj->programs[0];
1596
1597 if (prev->obj != obj) {
1598 pr_warning("error: program handler doesn't match object\n");
1599 return NULL;
1600 }
1601
1602 idx = (prev - obj->programs) + 1;
1603 if (idx >= obj->nr_programs)
1604 return NULL;
1605 return &obj->programs[idx];
1606 }
1607
1608 int bpf_program__set_priv(struct bpf_program *prog, void *priv,
1609 bpf_program_clear_priv_t clear_priv)
1610 {
1611 if (prog->priv && prog->clear_priv)
1612 prog->clear_priv(prog, prog->priv);
1613
1614 prog->priv = priv;
1615 prog->clear_priv = clear_priv;
1616 return 0;
1617 }
1618
1619 void *bpf_program__priv(struct bpf_program *prog)
1620 {
1621 return prog ? prog->priv : ERR_PTR(-EINVAL);
1622 }
1623
1624 const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
1625 {
1626 const char *title;
1627
1628 title = prog->section_name;
1629 if (needs_copy) {
1630 title = strdup(title);
1631 if (!title) {
1632 pr_warning("failed to strdup program title\n");
1633 return ERR_PTR(-ENOMEM);
1634 }
1635 }
1636
1637 return title;
1638 }
1639
1640 int bpf_program__fd(struct bpf_program *prog)
1641 {
1642 return bpf_program__nth_fd(prog, 0);
1643 }
1644
1645 int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
1646 bpf_program_prep_t prep)
1647 {
1648 int *instances_fds;
1649
1650 if (nr_instances <= 0 || !prep)
1651 return -EINVAL;
1652
1653 if (prog->instances.nr > 0 || prog->instances.fds) {
1654 pr_warning("Can't set pre-processor after loading\n");
1655 return -EINVAL;
1656 }
1657
1658 instances_fds = malloc(sizeof(int) * nr_instances);
1659 if (!instances_fds) {
1660 pr_warning("alloc memory failed for fds\n");
1661 return -ENOMEM;
1662 }
1663
1664 /* fill all fd with -1 */
1665 memset(instances_fds, -1, sizeof(int) * nr_instances);
1666
1667 prog->instances.nr = nr_instances;
1668 prog->instances.fds = instances_fds;
1669 prog->preprocessor = prep;
1670 return 0;
1671 }
1672
1673 int bpf_program__nth_fd(struct bpf_program *prog, int n)
1674 {
1675 int fd;
1676
1677 if (n >= prog->instances.nr || n < 0) {
1678 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
1679 n, prog->section_name, prog->instances.nr);
1680 return -EINVAL;
1681 }
1682
1683 fd = prog->instances.fds[n];
1684 if (fd < 0) {
1685 pr_warning("%dth instance of program '%s' is invalid\n",
1686 n, prog->section_name);
1687 return -ENOENT;
1688 }
1689
1690 return fd;
1691 }
1692
1693 void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
1694 {
1695 prog->type = type;
1696 }
1697
1698 static bool bpf_program__is_type(struct bpf_program *prog,
1699 enum bpf_prog_type type)
1700 {
1701 return prog ? (prog->type == type) : false;
1702 }
1703
1704 #define BPF_PROG_TYPE_FNS(NAME, TYPE) \
1705 int bpf_program__set_##NAME(struct bpf_program *prog) \
1706 { \
1707 if (!prog) \
1708 return -EINVAL; \
1709 bpf_program__set_type(prog, TYPE); \
1710 return 0; \
1711 } \
1712 \
1713 bool bpf_program__is_##NAME(struct bpf_program *prog) \
1714 { \
1715 return bpf_program__is_type(prog, TYPE); \
1716 } \
1717
1718 BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
1719 BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
1720 BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
1721 BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
1722 BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
1723 BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
1724 BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
1725
1726 int bpf_map__fd(struct bpf_map *map)
1727 {
1728 return map ? map->fd : -EINVAL;
1729 }
1730
1731 const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
1732 {
1733 return map ? &map->def : ERR_PTR(-EINVAL);
1734 }
1735
1736 const char *bpf_map__name(struct bpf_map *map)
1737 {
1738 return map ? map->name : NULL;
1739 }
1740
1741 int bpf_map__set_priv(struct bpf_map *map, void *priv,
1742 bpf_map_clear_priv_t clear_priv)
1743 {
1744 if (!map)
1745 return -EINVAL;
1746
1747 if (map->priv) {
1748 if (map->clear_priv)
1749 map->clear_priv(map, map->priv);
1750 }
1751
1752 map->priv = priv;
1753 map->clear_priv = clear_priv;
1754 return 0;
1755 }
1756
1757 void *bpf_map__priv(struct bpf_map *map)
1758 {
1759 return map ? map->priv : ERR_PTR(-EINVAL);
1760 }
1761
1762 struct bpf_map *
1763 bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
1764 {
1765 size_t idx;
1766 struct bpf_map *s, *e;
1767
1768 if (!obj || !obj->maps)
1769 return NULL;
1770
1771 s = obj->maps;
1772 e = obj->maps + obj->nr_maps;
1773
1774 if (prev == NULL)
1775 return s;
1776
1777 if ((prev < s) || (prev >= e)) {
1778 pr_warning("error in %s: map handler doesn't belong to object\n",
1779 __func__);
1780 return NULL;
1781 }
1782
1783 idx = (prev - obj->maps) + 1;
1784 if (idx >= obj->nr_maps)
1785 return NULL;
1786 return &obj->maps[idx];
1787 }
1788
1789 struct bpf_map *
1790 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
1791 {
1792 struct bpf_map *pos;
1793
1794 bpf_map__for_each(pos, obj) {
1795 if (pos->name && !strcmp(pos->name, name))
1796 return pos;
1797 }
1798 return NULL;
1799 }
1800
1801 struct bpf_map *
1802 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
1803 {
1804 int i;
1805
1806 for (i = 0; i < obj->nr_maps; i++) {
1807 if (obj->maps[i].offset == offset)
1808 return &obj->maps[i];
1809 }
1810 return ERR_PTR(-ENOENT);
1811 }
1812
1813 long libbpf_get_error(const void *ptr)
1814 {
1815 if (IS_ERR(ptr))
1816 return PTR_ERR(ptr);
1817 return 0;
1818 }
1819
1820 int bpf_prog_load(const char *file, enum bpf_prog_type type,
1821 struct bpf_object **pobj, int *prog_fd)
1822 {
1823 struct bpf_program *prog;
1824 struct bpf_object *obj;
1825 int err;
1826
1827 obj = bpf_object__open(file);
1828 if (IS_ERR(obj))
1829 return -ENOENT;
1830
1831 prog = bpf_program__next(NULL, obj);
1832 if (!prog) {
1833 bpf_object__close(obj);
1834 return -ENOENT;
1835 }
1836
1837 bpf_program__set_type(prog, type);
1838 err = bpf_object__load(obj);
1839 if (err) {
1840 bpf_object__close(obj);
1841 return -EINVAL;
1842 }
1843
1844 *pobj = obj;
1845 *prog_fd = bpf_program__fd(prog);
1846 return 0;
1847 }