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 391 name = elf_strptr(obj->efile.elf, 392 obj->efile.strtabidx, 393 sym.st_name); 394 if (!name) { 395 pr_warning("failed to get sym name string for prog %s\n", 396 prog->section_name); 397 return -LIBBPF_ERRNO__LIBELF; 398 } 399 } 400 401 if (!name) { 402 pr_warning("failed to find sym for prog %s\n", 403 prog->section_name); 404 return -EINVAL; 405 } 406 407 prog->name = strdup(name); 408 if (!prog->name) { 409 pr_warning("failed to allocate memory for prog sym %s\n", 410 name); 411 return -ENOMEM; 412 } 413 } 414 415 return 0; 416 } 417 418 static struct bpf_object *bpf_object__new(const char *path, 419 void *obj_buf, 420 size_t obj_buf_sz) 421 { 422 struct bpf_object *obj; 423 424 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1); 425 if (!obj) { 426 pr_warning("alloc memory failed for %s\n", path); 427 return ERR_PTR(-ENOMEM); 428 } 429 430 strcpy(obj->path, path); 431 obj->efile.fd = -1; 432 433 /* 434 * Caller of this function should also calls 435 * bpf_object__elf_finish() after data collection to return 436 * obj_buf to user. If not, we should duplicate the buffer to 437 * avoid user freeing them before elf finish. 438 */ 439 obj->efile.obj_buf = obj_buf; 440 obj->efile.obj_buf_sz = obj_buf_sz; 441 obj->efile.maps_shndx = -1; 442 443 obj->loaded = false; 444 445 INIT_LIST_HEAD(&obj->list); 446 list_add(&obj->list, &bpf_objects_list); 447 return obj; 448 } 449 450 static void bpf_object__elf_finish(struct bpf_object *obj) 451 { 452 if (!obj_elf_valid(obj)) 453 return; 454 455 if (obj->efile.elf) { 456 elf_end(obj->efile.elf); 457 obj->efile.elf = NULL; 458 } 459 obj->efile.symbols = NULL; 460 461 zfree(&obj->efile.reloc); 462 obj->efile.nr_reloc = 0; 463 zclose(obj->efile.fd); 464 obj->efile.obj_buf = NULL; 465 obj->efile.obj_buf_sz = 0; 466 } 467 468 static int bpf_object__elf_init(struct bpf_object *obj) 469 { 470 int err = 0; 471 GElf_Ehdr *ep; 472 473 if (obj_elf_valid(obj)) { 474 pr_warning("elf init: internal error\n"); 475 return -LIBBPF_ERRNO__LIBELF; 476 } 477 478 if (obj->efile.obj_buf_sz > 0) { 479 /* 480 * obj_buf should have been validated by 481 * bpf_object__open_buffer(). 482 */ 483 obj->efile.elf = elf_memory(obj->efile.obj_buf, 484 obj->efile.obj_buf_sz); 485 } else { 486 obj->efile.fd = open(obj->path, O_RDONLY); 487 if (obj->efile.fd < 0) { 488 pr_warning("failed to open %s: %s\n", obj->path, 489 strerror(errno)); 490 return -errno; 491 } 492 493 obj->efile.elf = elf_begin(obj->efile.fd, 494 LIBBPF_ELF_C_READ_MMAP, 495 NULL); 496 } 497 498 if (!obj->efile.elf) { 499 pr_warning("failed to open %s as ELF file\n", 500 obj->path); 501 err = -LIBBPF_ERRNO__LIBELF; 502 goto errout; 503 } 504 505 if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) { 506 pr_warning("failed to get EHDR from %s\n", 507 obj->path); 508 err = -LIBBPF_ERRNO__FORMAT; 509 goto errout; 510 } 511 ep = &obj->efile.ehdr; 512 513 /* Old LLVM set e_machine to EM_NONE */ 514 if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) { 515 pr_warning("%s is not an eBPF object file\n", 516 obj->path); 517 err = -LIBBPF_ERRNO__FORMAT; 518 goto errout; 519 } 520 521 return 0; 522 errout: 523 bpf_object__elf_finish(obj); 524 return err; 525 } 526 527 static int 528 bpf_object__check_endianness(struct bpf_object *obj) 529 { 530 static unsigned int const endian = 1; 531 532 switch (obj->efile.ehdr.e_ident[EI_DATA]) { 533 case ELFDATA2LSB: 534 /* We are big endian, BPF obj is little endian. */ 535 if (*(unsigned char const *)&endian != 1) 536 goto mismatch; 537 break; 538 539 case ELFDATA2MSB: 540 /* We are little endian, BPF obj is big endian. */ 541 if (*(unsigned char const *)&endian != 0) 542 goto mismatch; 543 break; 544 default: 545 return -LIBBPF_ERRNO__ENDIAN; 546 } 547 548 return 0; 549 550 mismatch: 551 pr_warning("Error: endianness mismatch.\n"); 552 return -LIBBPF_ERRNO__ENDIAN; 553 } 554 555 static int 556 bpf_object__init_license(struct bpf_object *obj, 557 void *data, size_t size) 558 { 559 memcpy(obj->license, data, 560 min(size, sizeof(obj->license) - 1)); 561 pr_debug("license of %s is %s\n", obj->path, obj->license); 562 return 0; 563 } 564 565 static int 566 bpf_object__init_kversion(struct bpf_object *obj, 567 void *data, size_t size) 568 { 569 u32 kver; 570 571 if (size != sizeof(kver)) { 572 pr_warning("invalid kver section in %s\n", obj->path); 573 return -LIBBPF_ERRNO__FORMAT; 574 } 575 memcpy(&kver, data, sizeof(kver)); 576 obj->kern_version = kver; 577 pr_debug("kernel version of %s is %x\n", obj->path, 578 obj->kern_version); 579 return 0; 580 } 581 582 static int 583 bpf_object__validate_maps(struct bpf_object *obj) 584 { 585 int i; 586 587 /* 588 * If there's only 1 map, the only error case should have been 589 * catched in bpf_object__init_maps(). 590 */ 591 if (!obj->maps || !obj->nr_maps || (obj->nr_maps == 1)) 592 return 0; 593 594 for (i = 1; i < obj->nr_maps; i++) { 595 const struct bpf_map *a = &obj->maps[i - 1]; 596 const struct bpf_map *b = &obj->maps[i]; 597 598 if (b->offset - a->offset < sizeof(struct bpf_map_def)) { 599 pr_warning("corrupted map section in %s: map \"%s\" too small\n", 600 obj->path, a->name); 601 return -EINVAL; 602 } 603 } 604 return 0; 605 } 606 607 static int compare_bpf_map(const void *_a, const void *_b) 608 { 609 const struct bpf_map *a = _a; 610 const struct bpf_map *b = _b; 611 612 return a->offset - b->offset; 613 } 614 615 static int 616 bpf_object__init_maps(struct bpf_object *obj) 617 { 618 int i, map_idx, nr_maps = 0; 619 Elf_Scn *scn; 620 Elf_Data *data; 621 Elf_Data *symbols = obj->efile.symbols; 622 623 if (obj->efile.maps_shndx < 0) 624 return -EINVAL; 625 if (!symbols) 626 return -EINVAL; 627 628 scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx); 629 if (scn) 630 data = elf_getdata(scn, NULL); 631 if (!scn || !data) { 632 pr_warning("failed to get Elf_Data from map section %d\n", 633 obj->efile.maps_shndx); 634 return -EINVAL; 635 } 636 637 /* 638 * Count number of maps. Each map has a name. 639 * Array of maps is not supported: only the first element is 640 * considered. 641 * 642 * TODO: Detect array of map and report error. 643 */ 644 for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) { 645 GElf_Sym sym; 646 647 if (!gelf_getsym(symbols, i, &sym)) 648 continue; 649 if (sym.st_shndx != obj->efile.maps_shndx) 650 continue; 651 nr_maps++; 652 } 653 654 /* Alloc obj->maps and fill nr_maps. */ 655 pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path, 656 nr_maps, data->d_size); 657 658 if (!nr_maps) 659 return 0; 660 661 obj->maps = calloc(nr_maps, sizeof(obj->maps[0])); 662 if (!obj->maps) { 663 pr_warning("alloc maps for object failed\n"); 664 return -ENOMEM; 665 } 666 obj->nr_maps = nr_maps; 667 668 /* 669 * fill all fd with -1 so won't close incorrect 670 * fd (fd=0 is stdin) when failure (zclose won't close 671 * negative fd)). 672 */ 673 for (i = 0; i < nr_maps; i++) 674 obj->maps[i].fd = -1; 675 676 /* 677 * Fill obj->maps using data in "maps" section. 678 */ 679 for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) { 680 GElf_Sym sym; 681 const char *map_name; 682 struct bpf_map_def *def; 683 684 if (!gelf_getsym(symbols, i, &sym)) 685 continue; 686 if (sym.st_shndx != obj->efile.maps_shndx) 687 continue; 688 689 map_name = elf_strptr(obj->efile.elf, 690 obj->efile.strtabidx, 691 sym.st_name); 692 obj->maps[map_idx].offset = sym.st_value; 693 if (sym.st_value + sizeof(struct bpf_map_def) > data->d_size) { 694 pr_warning("corrupted maps section in %s: last map \"%s\" too small\n", 695 obj->path, map_name); 696 return -EINVAL; 697 } 698 699 obj->maps[map_idx].name = strdup(map_name); 700 if (!obj->maps[map_idx].name) { 701 pr_warning("failed to alloc map name\n"); 702 return -ENOMEM; 703 } 704 pr_debug("map %d is \"%s\"\n", map_idx, 705 obj->maps[map_idx].name); 706 def = (struct bpf_map_def *)(data->d_buf + sym.st_value); 707 obj->maps[map_idx].def = *def; 708 map_idx++; 709 } 710 711 qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map); 712 return bpf_object__validate_maps(obj); 713 } 714 715 static int bpf_object__elf_collect(struct bpf_object *obj) 716 { 717 Elf *elf = obj->efile.elf; 718 GElf_Ehdr *ep = &obj->efile.ehdr; 719 Elf_Scn *scn = NULL; 720 int idx = 0, err = 0; 721 722 /* Elf is corrupted/truncated, avoid calling elf_strptr. */ 723 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) { 724 pr_warning("failed to get e_shstrndx from %s\n", 725 obj->path); 726 return -LIBBPF_ERRNO__FORMAT; 727 } 728 729 while ((scn = elf_nextscn(elf, scn)) != NULL) { 730 char *name; 731 GElf_Shdr sh; 732 Elf_Data *data; 733 734 idx++; 735 if (gelf_getshdr(scn, &sh) != &sh) { 736 pr_warning("failed to get section header from %s\n", 737 obj->path); 738 err = -LIBBPF_ERRNO__FORMAT; 739 goto out; 740 } 741 742 name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name); 743 if (!name) { 744 pr_warning("failed to get section name from %s\n", 745 obj->path); 746 err = -LIBBPF_ERRNO__FORMAT; 747 goto out; 748 } 749 750 data = elf_getdata(scn, 0); 751 if (!data) { 752 pr_warning("failed to get section data from %s(%s)\n", 753 name, obj->path); 754 err = -LIBBPF_ERRNO__FORMAT; 755 goto out; 756 } 757 pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n", 758 name, (unsigned long)data->d_size, 759 (int)sh.sh_link, (unsigned long)sh.sh_flags, 760 (int)sh.sh_type); 761 762 if (strcmp(name, "license") == 0) 763 err = bpf_object__init_license(obj, 764 data->d_buf, 765 data->d_size); 766 else if (strcmp(name, "version") == 0) 767 err = bpf_object__init_kversion(obj, 768 data->d_buf, 769 data->d_size); 770 else if (strcmp(name, "maps") == 0) 771 obj->efile.maps_shndx = idx; 772 else if (sh.sh_type == SHT_SYMTAB) { 773 if (obj->efile.symbols) { 774 pr_warning("bpf: multiple SYMTAB in %s\n", 775 obj->path); 776 err = -LIBBPF_ERRNO__FORMAT; 777 } else { 778 obj->efile.symbols = data; 779 obj->efile.strtabidx = sh.sh_link; 780 } 781 } else if ((sh.sh_type == SHT_PROGBITS) && 782 (sh.sh_flags & SHF_EXECINSTR) && 783 (data->d_size > 0)) { 784 err = bpf_object__add_program(obj, data->d_buf, 785 data->d_size, name, idx); 786 if (err) { 787 char errmsg[STRERR_BUFSIZE]; 788 789 strerror_r(-err, errmsg, sizeof(errmsg)); 790 pr_warning("failed to alloc program %s (%s): %s", 791 name, obj->path, errmsg); 792 } 793 } else if (sh.sh_type == SHT_REL) { 794 void *reloc = obj->efile.reloc; 795 int nr_reloc = obj->efile.nr_reloc + 1; 796 797 reloc = realloc(reloc, 798 sizeof(*obj->efile.reloc) * nr_reloc); 799 if (!reloc) { 800 pr_warning("realloc failed\n"); 801 err = -ENOMEM; 802 } else { 803 int n = nr_reloc - 1; 804 805 obj->efile.reloc = reloc; 806 obj->efile.nr_reloc = nr_reloc; 807 808 obj->efile.reloc[n].shdr = sh; 809 obj->efile.reloc[n].data = data; 810 } 811 } 812 if (err) 813 goto out; 814 } 815 816 if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) { 817 pr_warning("Corrupted ELF file: index of strtab invalid\n"); 818 return LIBBPF_ERRNO__FORMAT; 819 } 820 if (obj->efile.maps_shndx >= 0) { 821 err = bpf_object__init_maps(obj); 822 if (err) 823 goto out; 824 } 825 err = bpf_object__init_prog_names(obj); 826 out: 827 return err; 828 } 829 830 static struct bpf_program * 831 bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx) 832 { 833 struct bpf_program *prog; 834 size_t i; 835 836 for (i = 0; i < obj->nr_programs; i++) { 837 prog = &obj->programs[i]; 838 if (prog->idx == idx) 839 return prog; 840 } 841 return NULL; 842 } 843 844 static int 845 bpf_program__collect_reloc(struct bpf_program *prog, 846 size_t nr_maps, GElf_Shdr *shdr, 847 Elf_Data *data, Elf_Data *symbols, 848 int maps_shndx, struct bpf_map *maps) 849 { 850 int i, nrels; 851 852 pr_debug("collecting relocating info for: '%s'\n", 853 prog->section_name); 854 nrels = shdr->sh_size / shdr->sh_entsize; 855 856 prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels); 857 if (!prog->reloc_desc) { 858 pr_warning("failed to alloc memory in relocation\n"); 859 return -ENOMEM; 860 } 861 prog->nr_reloc = nrels; 862 863 for (i = 0; i < nrels; i++) { 864 GElf_Sym sym; 865 GElf_Rel rel; 866 unsigned int insn_idx; 867 struct bpf_insn *insns = prog->insns; 868 size_t map_idx; 869 870 if (!gelf_getrel(data, i, &rel)) { 871 pr_warning("relocation: failed to get %d reloc\n", i); 872 return -LIBBPF_ERRNO__FORMAT; 873 } 874 875 if (!gelf_getsym(symbols, 876 GELF_R_SYM(rel.r_info), 877 &sym)) { 878 pr_warning("relocation: symbol %"PRIx64" not found\n", 879 GELF_R_SYM(rel.r_info)); 880 return -LIBBPF_ERRNO__FORMAT; 881 } 882 883 if (sym.st_shndx != maps_shndx) { 884 pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n", 885 prog->section_name, sym.st_shndx); 886 return -LIBBPF_ERRNO__RELOC; 887 } 888 889 insn_idx = rel.r_offset / sizeof(struct bpf_insn); 890 pr_debug("relocation: insn_idx=%u\n", insn_idx); 891 892 if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) { 893 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n", 894 insn_idx, insns[insn_idx].code); 895 return -LIBBPF_ERRNO__RELOC; 896 } 897 898 /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */ 899 for (map_idx = 0; map_idx < nr_maps; map_idx++) { 900 if (maps[map_idx].offset == sym.st_value) { 901 pr_debug("relocation: find map %zd (%s) for insn %u\n", 902 map_idx, maps[map_idx].name, insn_idx); 903 break; 904 } 905 } 906 907 if (map_idx >= nr_maps) { 908 pr_warning("bpf relocation: map_idx %d large than %d\n", 909 (int)map_idx, (int)nr_maps - 1); 910 return -LIBBPF_ERRNO__RELOC; 911 } 912 913 prog->reloc_desc[i].insn_idx = insn_idx; 914 prog->reloc_desc[i].map_idx = map_idx; 915 } 916 return 0; 917 } 918 919 static int 920 bpf_object__create_maps(struct bpf_object *obj) 921 { 922 unsigned int i; 923 924 for (i = 0; i < obj->nr_maps; i++) { 925 struct bpf_map_def *def = &obj->maps[i].def; 926 int *pfd = &obj->maps[i].fd; 927 928 *pfd = bpf_create_map_name(def->type, 929 obj->maps[i].name, 930 def->key_size, 931 def->value_size, 932 def->max_entries, 933 0); 934 if (*pfd < 0) { 935 size_t j; 936 int err = *pfd; 937 938 pr_warning("failed to create map (name: '%s'): %s\n", 939 obj->maps[i].name, 940 strerror(errno)); 941 for (j = 0; j < i; j++) 942 zclose(obj->maps[j].fd); 943 return err; 944 } 945 pr_debug("create map %s: fd=%d\n", obj->maps[i].name, *pfd); 946 } 947 948 return 0; 949 } 950 951 static int 952 bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj) 953 { 954 int i; 955 956 if (!prog || !prog->reloc_desc) 957 return 0; 958 959 for (i = 0; i < prog->nr_reloc; i++) { 960 int insn_idx, map_idx; 961 struct bpf_insn *insns = prog->insns; 962 963 insn_idx = prog->reloc_desc[i].insn_idx; 964 map_idx = prog->reloc_desc[i].map_idx; 965 966 if (insn_idx >= (int)prog->insns_cnt) { 967 pr_warning("relocation out of range: '%s'\n", 968 prog->section_name); 969 return -LIBBPF_ERRNO__RELOC; 970 } 971 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD; 972 insns[insn_idx].imm = obj->maps[map_idx].fd; 973 } 974 975 zfree(&prog->reloc_desc); 976 prog->nr_reloc = 0; 977 return 0; 978 } 979 980 981 static int 982 bpf_object__relocate(struct bpf_object *obj) 983 { 984 struct bpf_program *prog; 985 size_t i; 986 int err; 987 988 for (i = 0; i < obj->nr_programs; i++) { 989 prog = &obj->programs[i]; 990 991 err = bpf_program__relocate(prog, obj); 992 if (err) { 993 pr_warning("failed to relocate '%s'\n", 994 prog->section_name); 995 return err; 996 } 997 } 998 return 0; 999 } 1000 1001 static int bpf_object__collect_reloc(struct bpf_object *obj) 1002 { 1003 int i, err; 1004 1005 if (!obj_elf_valid(obj)) { 1006 pr_warning("Internal error: elf object is closed\n"); 1007 return -LIBBPF_ERRNO__INTERNAL; 1008 } 1009 1010 for (i = 0; i < obj->efile.nr_reloc; i++) { 1011 GElf_Shdr *shdr = &obj->efile.reloc[i].shdr; 1012 Elf_Data *data = obj->efile.reloc[i].data; 1013 int idx = shdr->sh_info; 1014 struct bpf_program *prog; 1015 size_t nr_maps = obj->nr_maps; 1016 1017 if (shdr->sh_type != SHT_REL) { 1018 pr_warning("internal error at %d\n", __LINE__); 1019 return -LIBBPF_ERRNO__INTERNAL; 1020 } 1021 1022 prog = bpf_object__find_prog_by_idx(obj, idx); 1023 if (!prog) { 1024 pr_warning("relocation failed: no %d section\n", 1025 idx); 1026 return -LIBBPF_ERRNO__RELOC; 1027 } 1028 1029 err = bpf_program__collect_reloc(prog, nr_maps, 1030 shdr, data, 1031 obj->efile.symbols, 1032 obj->efile.maps_shndx, 1033 obj->maps); 1034 if (err) 1035 return err; 1036 } 1037 return 0; 1038 } 1039 1040 static int 1041 load_program(enum bpf_prog_type type, const char *name, struct bpf_insn *insns, 1042 int insns_cnt, char *license, u32 kern_version, int *pfd) 1043 { 1044 int ret; 1045 char *log_buf; 1046 1047 if (!insns || !insns_cnt) 1048 return -EINVAL; 1049 1050 log_buf = malloc(BPF_LOG_BUF_SIZE); 1051 if (!log_buf) 1052 pr_warning("Alloc log buffer for bpf loader error, continue without log\n"); 1053 1054 ret = bpf_load_program_name(type, name, insns, insns_cnt, license, 1055 kern_version, log_buf, BPF_LOG_BUF_SIZE); 1056 1057 if (ret >= 0) { 1058 *pfd = ret; 1059 ret = 0; 1060 goto out; 1061 } 1062 1063 ret = -LIBBPF_ERRNO__LOAD; 1064 pr_warning("load bpf program failed: %s\n", strerror(errno)); 1065 1066 if (log_buf && log_buf[0] != '\0') { 1067 ret = -LIBBPF_ERRNO__VERIFY; 1068 pr_warning("-- BEGIN DUMP LOG ---\n"); 1069 pr_warning("\n%s\n", log_buf); 1070 pr_warning("-- END LOG --\n"); 1071 } else if (insns_cnt >= BPF_MAXINSNS) { 1072 pr_warning("Program too large (%d insns), at most %d insns\n", 1073 insns_cnt, BPF_MAXINSNS); 1074 ret = -LIBBPF_ERRNO__PROG2BIG; 1075 } else { 1076 /* Wrong program type? */ 1077 if (type != BPF_PROG_TYPE_KPROBE) { 1078 int fd; 1079 1080 fd = bpf_load_program_name(BPF_PROG_TYPE_KPROBE, name, 1081 insns, insns_cnt, license, 1082 kern_version, NULL, 0); 1083 if (fd >= 0) { 1084 close(fd); 1085 ret = -LIBBPF_ERRNO__PROGTYPE; 1086 goto out; 1087 } 1088 } 1089 1090 if (log_buf) 1091 ret = -LIBBPF_ERRNO__KVER; 1092 } 1093 1094 out: 1095 free(log_buf); 1096 return ret; 1097 } 1098 1099 static int 1100 bpf_program__load(struct bpf_program *prog, 1101 char *license, u32 kern_version) 1102 { 1103 int err = 0, fd, i; 1104 1105 if (prog->instances.nr < 0 || !prog->instances.fds) { 1106 if (prog->preprocessor) { 1107 pr_warning("Internal error: can't load program '%s'\n", 1108 prog->section_name); 1109 return -LIBBPF_ERRNO__INTERNAL; 1110 } 1111 1112 prog->instances.fds = malloc(sizeof(int)); 1113 if (!prog->instances.fds) { 1114 pr_warning("Not enough memory for BPF fds\n"); 1115 return -ENOMEM; 1116 } 1117 prog->instances.nr = 1; 1118 prog->instances.fds[0] = -1; 1119 } 1120 1121 if (!prog->preprocessor) { 1122 if (prog->instances.nr != 1) { 1123 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n", 1124 prog->section_name, prog->instances.nr); 1125 } 1126 err = load_program(prog->type, prog->name, prog->insns, 1127 prog->insns_cnt, license, kern_version, &fd); 1128 if (!err) 1129 prog->instances.fds[0] = fd; 1130 goto out; 1131 } 1132 1133 for (i = 0; i < prog->instances.nr; i++) { 1134 struct bpf_prog_prep_result result; 1135 bpf_program_prep_t preprocessor = prog->preprocessor; 1136 1137 bzero(&result, sizeof(result)); 1138 err = preprocessor(prog, i, prog->insns, 1139 prog->insns_cnt, &result); 1140 if (err) { 1141 pr_warning("Preprocessing the %dth instance of program '%s' failed\n", 1142 i, prog->section_name); 1143 goto out; 1144 } 1145 1146 if (!result.new_insn_ptr || !result.new_insn_cnt) { 1147 pr_debug("Skip loading the %dth instance of program '%s'\n", 1148 i, prog->section_name); 1149 prog->instances.fds[i] = -1; 1150 if (result.pfd) 1151 *result.pfd = -1; 1152 continue; 1153 } 1154 1155 err = load_program(prog->type, prog->name, 1156 result.new_insn_ptr, 1157 result.new_insn_cnt, 1158 license, kern_version, &fd); 1159 1160 if (err) { 1161 pr_warning("Loading the %dth instance of program '%s' failed\n", 1162 i, prog->section_name); 1163 goto out; 1164 } 1165 1166 if (result.pfd) 1167 *result.pfd = fd; 1168 prog->instances.fds[i] = fd; 1169 } 1170 out: 1171 if (err) 1172 pr_warning("failed to load program '%s'\n", 1173 prog->section_name); 1174 zfree(&prog->insns); 1175 prog->insns_cnt = 0; 1176 return err; 1177 } 1178 1179 static int 1180 bpf_object__load_progs(struct bpf_object *obj) 1181 { 1182 size_t i; 1183 int err; 1184 1185 for (i = 0; i < obj->nr_programs; i++) { 1186 err = bpf_program__load(&obj->programs[i], 1187 obj->license, 1188 obj->kern_version); 1189 if (err) 1190 return err; 1191 } 1192 return 0; 1193 } 1194 1195 static int bpf_object__validate(struct bpf_object *obj) 1196 { 1197 if (obj->kern_version == 0) { 1198 pr_warning("%s doesn't provide kernel version\n", 1199 obj->path); 1200 return -LIBBPF_ERRNO__KVERSION; 1201 } 1202 return 0; 1203 } 1204 1205 static struct bpf_object * 1206 __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz) 1207 { 1208 struct bpf_object *obj; 1209 int err; 1210 1211 if (elf_version(EV_CURRENT) == EV_NONE) { 1212 pr_warning("failed to init libelf for %s\n", path); 1213 return ERR_PTR(-LIBBPF_ERRNO__LIBELF); 1214 } 1215 1216 obj = bpf_object__new(path, obj_buf, obj_buf_sz); 1217 if (IS_ERR(obj)) 1218 return obj; 1219 1220 CHECK_ERR(bpf_object__elf_init(obj), err, out); 1221 CHECK_ERR(bpf_object__check_endianness(obj), err, out); 1222 CHECK_ERR(bpf_object__elf_collect(obj), err, out); 1223 CHECK_ERR(bpf_object__collect_reloc(obj), err, out); 1224 CHECK_ERR(bpf_object__validate(obj), err, out); 1225 1226 bpf_object__elf_finish(obj); 1227 return obj; 1228 out: 1229 bpf_object__close(obj); 1230 return ERR_PTR(err); 1231 } 1232 1233 struct bpf_object *bpf_object__open(const char *path) 1234 { 1235 /* param validation */ 1236 if (!path) 1237 return NULL; 1238 1239 pr_debug("loading %s\n", path); 1240 1241 return __bpf_object__open(path, NULL, 0); 1242 } 1243 1244 struct bpf_object *bpf_object__open_buffer(void *obj_buf, 1245 size_t obj_buf_sz, 1246 const char *name) 1247 { 1248 char tmp_name[64]; 1249 1250 /* param validation */ 1251 if (!obj_buf || obj_buf_sz <= 0) 1252 return NULL; 1253 1254 if (!name) { 1255 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx", 1256 (unsigned long)obj_buf, 1257 (unsigned long)obj_buf_sz); 1258 tmp_name[sizeof(tmp_name) - 1] = '\0'; 1259 name = tmp_name; 1260 } 1261 pr_debug("loading object '%s' from buffer\n", 1262 name); 1263 1264 return __bpf_object__open(name, obj_buf, obj_buf_sz); 1265 } 1266 1267 int bpf_object__unload(struct bpf_object *obj) 1268 { 1269 size_t i; 1270 1271 if (!obj) 1272 return -EINVAL; 1273 1274 for (i = 0; i < obj->nr_maps; i++) 1275 zclose(obj->maps[i].fd); 1276 1277 for (i = 0; i < obj->nr_programs; i++) 1278 bpf_program__unload(&obj->programs[i]); 1279 1280 return 0; 1281 } 1282 1283 int bpf_object__load(struct bpf_object *obj) 1284 { 1285 int err; 1286 1287 if (!obj) 1288 return -EINVAL; 1289 1290 if (obj->loaded) { 1291 pr_warning("object should not be loaded twice\n"); 1292 return -EINVAL; 1293 } 1294 1295 obj->loaded = true; 1296 1297 CHECK_ERR(bpf_object__create_maps(obj), err, out); 1298 CHECK_ERR(bpf_object__relocate(obj), err, out); 1299 CHECK_ERR(bpf_object__load_progs(obj), err, out); 1300 1301 return 0; 1302 out: 1303 bpf_object__unload(obj); 1304 pr_warning("failed to load object '%s'\n", obj->path); 1305 return err; 1306 } 1307 1308 static int check_path(const char *path) 1309 { 1310 struct statfs st_fs; 1311 char *dname, *dir; 1312 int err = 0; 1313 1314 if (path == NULL) 1315 return -EINVAL; 1316 1317 dname = strdup(path); 1318 if (dname == NULL) 1319 return -ENOMEM; 1320 1321 dir = dirname(dname); 1322 if (statfs(dir, &st_fs)) { 1323 pr_warning("failed to statfs %s: %s\n", dir, strerror(errno)); 1324 err = -errno; 1325 } 1326 free(dname); 1327 1328 if (!err && st_fs.f_type != BPF_FS_MAGIC) { 1329 pr_warning("specified path %s is not on BPF FS\n", path); 1330 err = -EINVAL; 1331 } 1332 1333 return err; 1334 } 1335 1336 int bpf_program__pin_instance(struct bpf_program *prog, const char *path, 1337 int instance) 1338 { 1339 int err; 1340 1341 err = check_path(path); 1342 if (err) 1343 return err; 1344 1345 if (prog == NULL) { 1346 pr_warning("invalid program pointer\n"); 1347 return -EINVAL; 1348 } 1349 1350 if (instance < 0 || instance >= prog->instances.nr) { 1351 pr_warning("invalid prog instance %d of prog %s (max %d)\n", 1352 instance, prog->section_name, prog->instances.nr); 1353 return -EINVAL; 1354 } 1355 1356 if (bpf_obj_pin(prog->instances.fds[instance], path)) { 1357 pr_warning("failed to pin program: %s\n", strerror(errno)); 1358 return -errno; 1359 } 1360 pr_debug("pinned program '%s'\n", path); 1361 1362 return 0; 1363 } 1364 1365 static int make_dir(const char *path) 1366 { 1367 int err = 0; 1368 1369 if (mkdir(path, 0700) && errno != EEXIST) 1370 err = -errno; 1371 1372 if (err) 1373 pr_warning("failed to mkdir %s: %s\n", path, strerror(-err)); 1374 return err; 1375 } 1376 1377 int bpf_program__pin(struct bpf_program *prog, const char *path) 1378 { 1379 int i, err; 1380 1381 err = check_path(path); 1382 if (err) 1383 return err; 1384 1385 if (prog == NULL) { 1386 pr_warning("invalid program pointer\n"); 1387 return -EINVAL; 1388 } 1389 1390 if (prog->instances.nr <= 0) { 1391 pr_warning("no instances of prog %s to pin\n", 1392 prog->section_name); 1393 return -EINVAL; 1394 } 1395 1396 err = make_dir(path); 1397 if (err) 1398 return err; 1399 1400 for (i = 0; i < prog->instances.nr; i++) { 1401 char buf[PATH_MAX]; 1402 int len; 1403 1404 len = snprintf(buf, PATH_MAX, "%s/%d", path, i); 1405 if (len < 0) 1406 return -EINVAL; 1407 else if (len >= PATH_MAX) 1408 return -ENAMETOOLONG; 1409 1410 err = bpf_program__pin_instance(prog, buf, i); 1411 if (err) 1412 return err; 1413 } 1414 1415 return 0; 1416 } 1417 1418 int bpf_map__pin(struct bpf_map *map, const char *path) 1419 { 1420 int err; 1421 1422 err = check_path(path); 1423 if (err) 1424 return err; 1425 1426 if (map == NULL) { 1427 pr_warning("invalid map pointer\n"); 1428 return -EINVAL; 1429 } 1430 1431 if (bpf_obj_pin(map->fd, path)) { 1432 pr_warning("failed to pin map: %s\n", strerror(errno)); 1433 return -errno; 1434 } 1435 1436 pr_debug("pinned map '%s'\n", path); 1437 return 0; 1438 } 1439 1440 int bpf_object__pin(struct bpf_object *obj, const char *path) 1441 { 1442 struct bpf_program *prog; 1443 struct bpf_map *map; 1444 int err; 1445 1446 if (!obj) 1447 return -ENOENT; 1448 1449 if (!obj->loaded) { 1450 pr_warning("object not yet loaded; load it first\n"); 1451 return -ENOENT; 1452 } 1453 1454 err = make_dir(path); 1455 if (err) 1456 return err; 1457 1458 bpf_map__for_each(map, obj) { 1459 char buf[PATH_MAX]; 1460 int len; 1461 1462 len = snprintf(buf, PATH_MAX, "%s/%s", path, 1463 bpf_map__name(map)); 1464 if (len < 0) 1465 return -EINVAL; 1466 else if (len >= PATH_MAX) 1467 return -ENAMETOOLONG; 1468 1469 err = bpf_map__pin(map, buf); 1470 if (err) 1471 return err; 1472 } 1473 1474 bpf_object__for_each_program(prog, obj) { 1475 char buf[PATH_MAX]; 1476 int len; 1477 1478 len = snprintf(buf, PATH_MAX, "%s/%s", path, 1479 prog->section_name); 1480 if (len < 0) 1481 return -EINVAL; 1482 else if (len >= PATH_MAX) 1483 return -ENAMETOOLONG; 1484 1485 err = bpf_program__pin(prog, buf); 1486 if (err) 1487 return err; 1488 } 1489 1490 return 0; 1491 } 1492 1493 void bpf_object__close(struct bpf_object *obj) 1494 { 1495 size_t i; 1496 1497 if (!obj) 1498 return; 1499 1500 if (obj->clear_priv) 1501 obj->clear_priv(obj, obj->priv); 1502 1503 bpf_object__elf_finish(obj); 1504 bpf_object__unload(obj); 1505 1506 for (i = 0; i < obj->nr_maps; i++) { 1507 zfree(&obj->maps[i].name); 1508 if (obj->maps[i].clear_priv) 1509 obj->maps[i].clear_priv(&obj->maps[i], 1510 obj->maps[i].priv); 1511 obj->maps[i].priv = NULL; 1512 obj->maps[i].clear_priv = NULL; 1513 } 1514 zfree(&obj->maps); 1515 obj->nr_maps = 0; 1516 1517 if (obj->programs && obj->nr_programs) { 1518 for (i = 0; i < obj->nr_programs; i++) 1519 bpf_program__exit(&obj->programs[i]); 1520 } 1521 zfree(&obj->programs); 1522 1523 list_del(&obj->list); 1524 free(obj); 1525 } 1526 1527 struct bpf_object * 1528 bpf_object__next(struct bpf_object *prev) 1529 { 1530 struct bpf_object *next; 1531 1532 if (!prev) 1533 next = list_first_entry(&bpf_objects_list, 1534 struct bpf_object, 1535 list); 1536 else 1537 next = list_next_entry(prev, list); 1538 1539 /* Empty list is noticed here so don't need checking on entry. */ 1540 if (&next->list == &bpf_objects_list) 1541 return NULL; 1542 1543 return next; 1544 } 1545 1546 const char *bpf_object__name(struct bpf_object *obj) 1547 { 1548 return obj ? obj->path : ERR_PTR(-EINVAL); 1549 } 1550 1551 unsigned int bpf_object__kversion(struct bpf_object *obj) 1552 { 1553 return obj ? obj->kern_version : 0; 1554 } 1555 1556 int bpf_object__set_priv(struct bpf_object *obj, void *priv, 1557 bpf_object_clear_priv_t clear_priv) 1558 { 1559 if (obj->priv && obj->clear_priv) 1560 obj->clear_priv(obj, obj->priv); 1561 1562 obj->priv = priv; 1563 obj->clear_priv = clear_priv; 1564 return 0; 1565 } 1566 1567 void *bpf_object__priv(struct bpf_object *obj) 1568 { 1569 return obj ? obj->priv : ERR_PTR(-EINVAL); 1570 } 1571 1572 struct bpf_program * 1573 bpf_program__next(struct bpf_program *prev, struct bpf_object *obj) 1574 { 1575 size_t idx; 1576 1577 if (!obj->programs) 1578 return NULL; 1579 /* First handler */ 1580 if (prev == NULL) 1581 return &obj->programs[0]; 1582 1583 if (prev->obj != obj) { 1584 pr_warning("error: program handler doesn't match object\n"); 1585 return NULL; 1586 } 1587 1588 idx = (prev - obj->programs) + 1; 1589 if (idx >= obj->nr_programs) 1590 return NULL; 1591 return &obj->programs[idx]; 1592 } 1593 1594 int bpf_program__set_priv(struct bpf_program *prog, void *priv, 1595 bpf_program_clear_priv_t clear_priv) 1596 { 1597 if (prog->priv && prog->clear_priv) 1598 prog->clear_priv(prog, prog->priv); 1599 1600 prog->priv = priv; 1601 prog->clear_priv = clear_priv; 1602 return 0; 1603 } 1604 1605 void *bpf_program__priv(struct bpf_program *prog) 1606 { 1607 return prog ? prog->priv : ERR_PTR(-EINVAL); 1608 } 1609 1610 const char *bpf_program__title(struct bpf_program *prog, bool needs_copy) 1611 { 1612 const char *title; 1613 1614 title = prog->section_name; 1615 if (needs_copy) { 1616 title = strdup(title); 1617 if (!title) { 1618 pr_warning("failed to strdup program title\n"); 1619 return ERR_PTR(-ENOMEM); 1620 } 1621 } 1622 1623 return title; 1624 } 1625 1626 int bpf_program__fd(struct bpf_program *prog) 1627 { 1628 return bpf_program__nth_fd(prog, 0); 1629 } 1630 1631 int bpf_program__set_prep(struct bpf_program *prog, int nr_instances, 1632 bpf_program_prep_t prep) 1633 { 1634 int *instances_fds; 1635 1636 if (nr_instances <= 0 || !prep) 1637 return -EINVAL; 1638 1639 if (prog->instances.nr > 0 || prog->instances.fds) { 1640 pr_warning("Can't set pre-processor after loading\n"); 1641 return -EINVAL; 1642 } 1643 1644 instances_fds = malloc(sizeof(int) * nr_instances); 1645 if (!instances_fds) { 1646 pr_warning("alloc memory failed for fds\n"); 1647 return -ENOMEM; 1648 } 1649 1650 /* fill all fd with -1 */ 1651 memset(instances_fds, -1, sizeof(int) * nr_instances); 1652 1653 prog->instances.nr = nr_instances; 1654 prog->instances.fds = instances_fds; 1655 prog->preprocessor = prep; 1656 return 0; 1657 } 1658 1659 int bpf_program__nth_fd(struct bpf_program *prog, int n) 1660 { 1661 int fd; 1662 1663 if (n >= prog->instances.nr || n < 0) { 1664 pr_warning("Can't get the %dth fd from program %s: only %d instances\n", 1665 n, prog->section_name, prog->instances.nr); 1666 return -EINVAL; 1667 } 1668 1669 fd = prog->instances.fds[n]; 1670 if (fd < 0) { 1671 pr_warning("%dth instance of program '%s' is invalid\n", 1672 n, prog->section_name); 1673 return -ENOENT; 1674 } 1675 1676 return fd; 1677 } 1678 1679 void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type) 1680 { 1681 prog->type = type; 1682 } 1683 1684 static bool bpf_program__is_type(struct bpf_program *prog, 1685 enum bpf_prog_type type) 1686 { 1687 return prog ? (prog->type == type) : false; 1688 } 1689 1690 #define BPF_PROG_TYPE_FNS(NAME, TYPE) \ 1691 int bpf_program__set_##NAME(struct bpf_program *prog) \ 1692 { \ 1693 if (!prog) \ 1694 return -EINVAL; \ 1695 bpf_program__set_type(prog, TYPE); \ 1696 return 0; \ 1697 } \ 1698 \ 1699 bool bpf_program__is_##NAME(struct bpf_program *prog) \ 1700 { \ 1701 return bpf_program__is_type(prog, TYPE); \ 1702 } \ 1703 1704 BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER); 1705 BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE); 1706 BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS); 1707 BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT); 1708 BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT); 1709 BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP); 1710 BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT); 1711 1712 int bpf_map__fd(struct bpf_map *map) 1713 { 1714 return map ? map->fd : -EINVAL; 1715 } 1716 1717 const struct bpf_map_def *bpf_map__def(struct bpf_map *map) 1718 { 1719 return map ? &map->def : ERR_PTR(-EINVAL); 1720 } 1721 1722 const char *bpf_map__name(struct bpf_map *map) 1723 { 1724 return map ? map->name : NULL; 1725 } 1726 1727 int bpf_map__set_priv(struct bpf_map *map, void *priv, 1728 bpf_map_clear_priv_t clear_priv) 1729 { 1730 if (!map) 1731 return -EINVAL; 1732 1733 if (map->priv) { 1734 if (map->clear_priv) 1735 map->clear_priv(map, map->priv); 1736 } 1737 1738 map->priv = priv; 1739 map->clear_priv = clear_priv; 1740 return 0; 1741 } 1742 1743 void *bpf_map__priv(struct bpf_map *map) 1744 { 1745 return map ? map->priv : ERR_PTR(-EINVAL); 1746 } 1747 1748 struct bpf_map * 1749 bpf_map__next(struct bpf_map *prev, struct bpf_object *obj) 1750 { 1751 size_t idx; 1752 struct bpf_map *s, *e; 1753 1754 if (!obj || !obj->maps) 1755 return NULL; 1756 1757 s = obj->maps; 1758 e = obj->maps + obj->nr_maps; 1759 1760 if (prev == NULL) 1761 return s; 1762 1763 if ((prev < s) || (prev >= e)) { 1764 pr_warning("error in %s: map handler doesn't belong to object\n", 1765 __func__); 1766 return NULL; 1767 } 1768 1769 idx = (prev - obj->maps) + 1; 1770 if (idx >= obj->nr_maps) 1771 return NULL; 1772 return &obj->maps[idx]; 1773 } 1774 1775 struct bpf_map * 1776 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name) 1777 { 1778 struct bpf_map *pos; 1779 1780 bpf_map__for_each(pos, obj) { 1781 if (pos->name && !strcmp(pos->name, name)) 1782 return pos; 1783 } 1784 return NULL; 1785 } 1786 1787 struct bpf_map * 1788 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset) 1789 { 1790 int i; 1791 1792 for (i = 0; i < obj->nr_maps; i++) { 1793 if (obj->maps[i].offset == offset) 1794 return &obj->maps[i]; 1795 } 1796 return ERR_PTR(-ENOENT); 1797 } 1798 1799 long libbpf_get_error(const void *ptr) 1800 { 1801 if (IS_ERR(ptr)) 1802 return PTR_ERR(ptr); 1803 return 0; 1804 } 1805 1806 int bpf_prog_load(const char *file, enum bpf_prog_type type, 1807 struct bpf_object **pobj, int *prog_fd) 1808 { 1809 struct bpf_program *prog; 1810 struct bpf_object *obj; 1811 int err; 1812 1813 obj = bpf_object__open(file); 1814 if (IS_ERR(obj)) 1815 return -ENOENT; 1816 1817 prog = bpf_program__next(NULL, obj); 1818 if (!prog) { 1819 bpf_object__close(obj); 1820 return -ENOENT; 1821 } 1822 1823 bpf_program__set_type(prog, type); 1824 err = bpf_object__load(obj); 1825 if (err) { 1826 bpf_object__close(obj); 1827 return -EINVAL; 1828 } 1829 1830 *pobj = obj; 1831 *prog_fd = bpf_program__fd(prog); 1832 return 0; 1833 } 1834