linux/tools/lib/bpf/libbpf.c
<<
>>
Prefs
   1// SPDX-License-Identifier: LGPL-2.1
   2
   3/*
   4 * Common eBPF ELF object loading operations.
   5 *
   6 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
   7 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
   8 * Copyright (C) 2015 Huawei Inc.
   9 * Copyright (C) 2017 Nicira, Inc.
  10 *
  11 * This program is free software; you can redistribute it and/or
  12 * modify it under the terms of the GNU Lesser General Public
  13 * License as published by the Free Software Foundation;
  14 * version 2.1 of the License (not later!)
  15 *
  16 * This program is distributed in the hope that it will be useful,
  17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19 * GNU Lesser General Public License for more details.
  20 *
  21 * You should have received a copy of the GNU Lesser General Public
  22 * License along with this program; if not,  see <http://www.gnu.org/licenses>
  23 */
  24
  25#include <stdlib.h>
  26#include <stdio.h>
  27#include <stdarg.h>
  28#include <libgen.h>
  29#include <inttypes.h>
  30#include <string.h>
  31#include <unistd.h>
  32#include <fcntl.h>
  33#include <errno.h>
  34#include <perf-sys.h>
  35#include <asm/unistd.h>
  36#include <linux/err.h>
  37#include <linux/kernel.h>
  38#include <linux/bpf.h>
  39#include <linux/btf.h>
  40#include <linux/list.h>
  41#include <linux/limits.h>
  42#include <sys/stat.h>
  43#include <sys/types.h>
  44#include <sys/vfs.h>
  45#include <libelf.h>
  46#include <gelf.h>
  47
  48#include "libbpf.h"
  49#include "bpf.h"
  50#include "btf.h"
  51
  52#ifndef EM_BPF
  53#define EM_BPF 247
  54#endif
  55
  56#ifndef BPF_FS_MAGIC
  57#define BPF_FS_MAGIC            0xcafe4a11
  58#endif
  59
  60#define __printf(a, b)  __attribute__((format(printf, a, b)))
  61
  62__printf(1, 2)
  63static int __base_pr(const char *format, ...)
  64{
  65        va_list args;
  66        int err;
  67
  68        va_start(args, format);
  69        err = vfprintf(stderr, format, args);
  70        va_end(args);
  71        return err;
  72}
  73
  74static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
  75static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
  76static __printf(1, 2) libbpf_print_fn_t __pr_debug;
  77
  78#define __pr(func, fmt, ...)    \
  79do {                            \
  80        if ((func))             \
  81                (func)("libbpf: " fmt, ##__VA_ARGS__); \
  82} while (0)
  83
  84#define pr_warning(fmt, ...)    __pr(__pr_warning, fmt, ##__VA_ARGS__)
  85#define pr_info(fmt, ...)       __pr(__pr_info, fmt, ##__VA_ARGS__)
  86#define pr_debug(fmt, ...)      __pr(__pr_debug, fmt, ##__VA_ARGS__)
  87
  88void libbpf_set_print(libbpf_print_fn_t warn,
  89                      libbpf_print_fn_t info,
  90                      libbpf_print_fn_t debug)
  91{
  92        __pr_warning = warn;
  93        __pr_info = info;
  94        __pr_debug = debug;
  95}
  96
  97#define STRERR_BUFSIZE  128
  98
  99#define ERRNO_OFFSET(e)         ((e) - __LIBBPF_ERRNO__START)
 100#define ERRCODE_OFFSET(c)       ERRNO_OFFSET(LIBBPF_ERRNO__##c)
 101#define NR_ERRNO        (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
 102
 103static const char *libbpf_strerror_table[NR_ERRNO] = {
 104        [ERRCODE_OFFSET(LIBELF)]        = "Something wrong in libelf",
 105        [ERRCODE_OFFSET(FORMAT)]        = "BPF object format invalid",
 106        [ERRCODE_OFFSET(KVERSION)]      = "'version' section incorrect or lost",
 107        [ERRCODE_OFFSET(ENDIAN)]        = "Endian mismatch",
 108        [ERRCODE_OFFSET(INTERNAL)]      = "Internal error in libbpf",
 109        [ERRCODE_OFFSET(RELOC)]         = "Relocation failed",
 110        [ERRCODE_OFFSET(VERIFY)]        = "Kernel verifier blocks program loading",
 111        [ERRCODE_OFFSET(PROG2BIG)]      = "Program too big",
 112        [ERRCODE_OFFSET(KVER)]          = "Incorrect kernel version",
 113        [ERRCODE_OFFSET(PROGTYPE)]      = "Kernel doesn't support this program type",
 114        [ERRCODE_OFFSET(WRNGPID)]       = "Wrong pid in netlink message",
 115        [ERRCODE_OFFSET(INVSEQ)]        = "Invalid netlink sequence",
 116};
 117
 118int libbpf_strerror(int err, char *buf, size_t size)
 119{
 120        if (!buf || !size)
 121                return -1;
 122
 123        err = err > 0 ? err : -err;
 124
 125        if (err < __LIBBPF_ERRNO__START) {
 126                int ret;
 127
 128                ret = strerror_r(err, buf, size);
 129                buf[size - 1] = '\0';
 130                return ret;
 131        }
 132
 133        if (err < __LIBBPF_ERRNO__END) {
 134                const char *msg;
 135
 136                msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
 137                snprintf(buf, size, "%s", msg);
 138                buf[size - 1] = '\0';
 139                return 0;
 140        }
 141
 142        snprintf(buf, size, "Unknown libbpf error %d", err);
 143        buf[size - 1] = '\0';
 144        return -1;
 145}
 146
 147#define CHECK_ERR(action, err, out) do {        \
 148        err = action;                   \
 149        if (err)                        \
 150                goto out;               \
 151} while(0)
 152
 153
 154/* Copied from tools/perf/util/util.h */
 155#ifndef zfree
 156# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
 157#endif
 158
 159#ifndef zclose
 160# define zclose(fd) ({                  \
 161        int ___err = 0;                 \
 162        if ((fd) >= 0)                  \
 163                ___err = close((fd));   \
 164        fd = -1;                        \
 165        ___err; })
 166#endif
 167
 168#ifdef HAVE_LIBELF_MMAP_SUPPORT
 169# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
 170#else
 171# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
 172#endif
 173
 174/*
 175 * bpf_prog should be a better name but it has been used in
 176 * linux/filter.h.
 177 */
 178struct bpf_program {
 179        /* Index in elf obj file, for relocation use. */
 180        int idx;
 181        char *name;
 182        int prog_ifindex;
 183        char *section_name;
 184        struct bpf_insn *insns;
 185        size_t insns_cnt, main_prog_cnt;
 186        enum bpf_prog_type type;
 187
 188        struct reloc_desc {
 189                enum {
 190                        RELO_LD64,
 191                        RELO_CALL,
 192                } type;
 193                int insn_idx;
 194                union {
 195                        int map_idx;
 196                        int text_off;
 197                };
 198        } *reloc_desc;
 199        int nr_reloc;
 200
 201        struct {
 202                int nr;
 203                int *fds;
 204        } instances;
 205        bpf_program_prep_t preprocessor;
 206
 207        struct bpf_object *obj;
 208        void *priv;
 209        bpf_program_clear_priv_t clear_priv;
 210
 211        enum bpf_attach_type expected_attach_type;
 212};
 213
 214struct bpf_map {
 215        int fd;
 216        char *name;
 217        size_t offset;
 218        int map_ifindex;
 219        struct bpf_map_def def;
 220        __u32 btf_key_type_id;
 221        __u32 btf_value_type_id;
 222        void *priv;
 223        bpf_map_clear_priv_t clear_priv;
 224};
 225
 226static LIST_HEAD(bpf_objects_list);
 227
 228struct bpf_object {
 229        char license[64];
 230        u32 kern_version;
 231
 232        struct bpf_program *programs;
 233        size_t nr_programs;
 234        struct bpf_map *maps;
 235        size_t nr_maps;
 236
 237        bool loaded;
 238
 239        /*
 240         * Information when doing elf related work. Only valid if fd
 241         * is valid.
 242         */
 243        struct {
 244                int fd;
 245                void *obj_buf;
 246                size_t obj_buf_sz;
 247                Elf *elf;
 248                GElf_Ehdr ehdr;
 249                Elf_Data *symbols;
 250                size_t strtabidx;
 251                struct {
 252                        GElf_Shdr shdr;
 253                        Elf_Data *data;
 254                } *reloc;
 255                int nr_reloc;
 256                int maps_shndx;
 257                int text_shndx;
 258        } efile;
 259        /*
 260         * All loaded bpf_object is linked in a list, which is
 261         * hidden to caller. bpf_objects__<func> handlers deal with
 262         * all objects.
 263         */
 264        struct list_head list;
 265
 266        struct btf *btf;
 267
 268        void *priv;
 269        bpf_object_clear_priv_t clear_priv;
 270
 271        char path[];
 272};
 273#define obj_elf_valid(o)        ((o)->efile.elf)
 274
 275static void bpf_program__unload(struct bpf_program *prog)
 276{
 277        int i;
 278
 279        if (!prog)
 280                return;
 281
 282        /*
 283         * If the object is opened but the program was never loaded,
 284         * it is possible that prog->instances.nr == -1.
 285         */
 286        if (prog->instances.nr > 0) {
 287                for (i = 0; i < prog->instances.nr; i++)
 288                        zclose(prog->instances.fds[i]);
 289        } else if (prog->instances.nr != -1) {
 290                pr_warning("Internal error: instances.nr is %d\n",
 291                           prog->instances.nr);
 292        }
 293
 294        prog->instances.nr = -1;
 295        zfree(&prog->instances.fds);
 296}
 297
 298static void bpf_program__exit(struct bpf_program *prog)
 299{
 300        if (!prog)
 301                return;
 302
 303        if (prog->clear_priv)
 304                prog->clear_priv(prog, prog->priv);
 305
 306        prog->priv = NULL;
 307        prog->clear_priv = NULL;
 308
 309        bpf_program__unload(prog);
 310        zfree(&prog->name);
 311        zfree(&prog->section_name);
 312        zfree(&prog->insns);
 313        zfree(&prog->reloc_desc);
 314
 315        prog->nr_reloc = 0;
 316        prog->insns_cnt = 0;
 317        prog->idx = -1;
 318}
 319
 320static int
 321bpf_program__init(void *data, size_t size, char *section_name, int idx,
 322                  struct bpf_program *prog)
 323{
 324        if (size < sizeof(struct bpf_insn)) {
 325                pr_warning("corrupted section '%s'\n", section_name);
 326                return -EINVAL;
 327        }
 328
 329        bzero(prog, sizeof(*prog));
 330
 331        prog->section_name = strdup(section_name);
 332        if (!prog->section_name) {
 333                pr_warning("failed to alloc name for prog under section(%d) %s\n",
 334                           idx, section_name);
 335                goto errout;
 336        }
 337
 338        prog->insns = malloc(size);
 339        if (!prog->insns) {
 340                pr_warning("failed to alloc insns for prog under section %s\n",
 341                           section_name);
 342                goto errout;
 343        }
 344        prog->insns_cnt = size / sizeof(struct bpf_insn);
 345        memcpy(prog->insns, data,
 346               prog->insns_cnt * sizeof(struct bpf_insn));
 347        prog->idx = idx;
 348        prog->instances.fds = NULL;
 349        prog->instances.nr = -1;
 350        prog->type = BPF_PROG_TYPE_KPROBE;
 351
 352        return 0;
 353errout:
 354        bpf_program__exit(prog);
 355        return -ENOMEM;
 356}
 357
 358static int
 359bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
 360                        char *section_name, int idx)
 361{
 362        struct bpf_program prog, *progs;
 363        int nr_progs, err;
 364
 365        err = bpf_program__init(data, size, section_name, idx, &prog);
 366        if (err)
 367                return err;
 368
 369        progs = obj->programs;
 370        nr_progs = obj->nr_programs;
 371
 372        progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
 373        if (!progs) {
 374                /*
 375                 * In this case the original obj->programs
 376                 * is still valid, so don't need special treat for
 377                 * bpf_close_object().
 378                 */
 379                pr_warning("failed to alloc a new program under section '%s'\n",
 380                           section_name);
 381                bpf_program__exit(&prog);
 382                return -ENOMEM;
 383        }
 384
 385        pr_debug("found program %s\n", prog.section_name);
 386        obj->programs = progs;
 387        obj->nr_programs = nr_progs + 1;
 388        prog.obj = obj;
 389        progs[nr_progs] = prog;
 390        return 0;
 391}
 392
 393static int
 394bpf_object__init_prog_names(struct bpf_object *obj)
 395{
 396        Elf_Data *symbols = obj->efile.symbols;
 397        struct bpf_program *prog;
 398        size_t pi, si;
 399
 400        for (pi = 0; pi < obj->nr_programs; pi++) {
 401                const char *name = NULL;
 402
 403                prog = &obj->programs[pi];
 404                if (prog->idx == obj->efile.text_shndx) {
 405                        name = ".text";
 406                        goto skip_search;
 407                }
 408
 409                for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
 410                     si++) {
 411                        GElf_Sym sym;
 412
 413                        if (!gelf_getsym(symbols, si, &sym))
 414                                continue;
 415                        if (sym.st_shndx != prog->idx)
 416                                continue;
 417                        if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
 418                                continue;
 419
 420                        name = elf_strptr(obj->efile.elf,
 421                                          obj->efile.strtabidx,
 422                                          sym.st_name);
 423                        if (!name) {
 424                                pr_warning("failed to get sym name string for prog %s\n",
 425                                           prog->section_name);
 426                                return -LIBBPF_ERRNO__LIBELF;
 427                        }
 428                }
 429
 430                if (!name) {
 431                        pr_warning("failed to find sym for prog %s\n",
 432                                   prog->section_name);
 433                        return -EINVAL;
 434                }
 435skip_search:
 436                prog->name = strdup(name);
 437                if (!prog->name) {
 438                        pr_warning("failed to allocate memory for prog sym %s\n",
 439                                   name);
 440                        return -ENOMEM;
 441                }
 442        }
 443
 444        return 0;
 445}
 446
 447static struct bpf_object *bpf_object__new(const char *path,
 448                                          void *obj_buf,
 449                                          size_t obj_buf_sz)
 450{
 451        struct bpf_object *obj;
 452
 453        obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
 454        if (!obj) {
 455                pr_warning("alloc memory failed for %s\n", path);
 456                return ERR_PTR(-ENOMEM);
 457        }
 458
 459        strcpy(obj->path, path);
 460        obj->efile.fd = -1;
 461
 462        /*
 463         * Caller of this function should also calls
 464         * bpf_object__elf_finish() after data collection to return
 465         * obj_buf to user. If not, we should duplicate the buffer to
 466         * avoid user freeing them before elf finish.
 467         */
 468        obj->efile.obj_buf = obj_buf;
 469        obj->efile.obj_buf_sz = obj_buf_sz;
 470        obj->efile.maps_shndx = -1;
 471
 472        obj->loaded = false;
 473
 474        INIT_LIST_HEAD(&obj->list);
 475        list_add(&obj->list, &bpf_objects_list);
 476        return obj;
 477}
 478
 479static void bpf_object__elf_finish(struct bpf_object *obj)
 480{
 481        if (!obj_elf_valid(obj))
 482                return;
 483
 484        if (obj->efile.elf) {
 485                elf_end(obj->efile.elf);
 486                obj->efile.elf = NULL;
 487        }
 488        obj->efile.symbols = NULL;
 489
 490        zfree(&obj->efile.reloc);
 491        obj->efile.nr_reloc = 0;
 492        zclose(obj->efile.fd);
 493        obj->efile.obj_buf = NULL;
 494        obj->efile.obj_buf_sz = 0;
 495}
 496
 497static int bpf_object__elf_init(struct bpf_object *obj)
 498{
 499        int err = 0;
 500        GElf_Ehdr *ep;
 501
 502        if (obj_elf_valid(obj)) {
 503                pr_warning("elf init: internal error\n");
 504                return -LIBBPF_ERRNO__LIBELF;
 505        }
 506
 507        if (obj->efile.obj_buf_sz > 0) {
 508                /*
 509                 * obj_buf should have been validated by
 510                 * bpf_object__open_buffer().
 511                 */
 512                obj->efile.elf = elf_memory(obj->efile.obj_buf,
 513                                            obj->efile.obj_buf_sz);
 514        } else {
 515                obj->efile.fd = open(obj->path, O_RDONLY);
 516                if (obj->efile.fd < 0) {
 517                        pr_warning("failed to open %s: %s\n", obj->path,
 518                                        strerror(errno));
 519                        return -errno;
 520                }
 521
 522                obj->efile.elf = elf_begin(obj->efile.fd,
 523                                LIBBPF_ELF_C_READ_MMAP,
 524                                NULL);
 525        }
 526
 527        if (!obj->efile.elf) {
 528                pr_warning("failed to open %s as ELF file\n",
 529                                obj->path);
 530                err = -LIBBPF_ERRNO__LIBELF;
 531                goto errout;
 532        }
 533
 534        if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
 535                pr_warning("failed to get EHDR from %s\n",
 536                                obj->path);
 537                err = -LIBBPF_ERRNO__FORMAT;
 538                goto errout;
 539        }
 540        ep = &obj->efile.ehdr;
 541
 542        /* Old LLVM set e_machine to EM_NONE */
 543        if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) {
 544                pr_warning("%s is not an eBPF object file\n",
 545                        obj->path);
 546                err = -LIBBPF_ERRNO__FORMAT;
 547                goto errout;
 548        }
 549
 550        return 0;
 551errout:
 552        bpf_object__elf_finish(obj);
 553        return err;
 554}
 555
 556static int
 557bpf_object__check_endianness(struct bpf_object *obj)
 558{
 559        static unsigned int const endian = 1;
 560
 561        switch (obj->efile.ehdr.e_ident[EI_DATA]) {
 562        case ELFDATA2LSB:
 563                /* We are big endian, BPF obj is little endian. */
 564                if (*(unsigned char const *)&endian != 1)
 565                        goto mismatch;
 566                break;
 567
 568        case ELFDATA2MSB:
 569                /* We are little endian, BPF obj is big endian. */
 570                if (*(unsigned char const *)&endian != 0)
 571                        goto mismatch;
 572                break;
 573        default:
 574                return -LIBBPF_ERRNO__ENDIAN;
 575        }
 576
 577        return 0;
 578
 579mismatch:
 580        pr_warning("Error: endianness mismatch.\n");
 581        return -LIBBPF_ERRNO__ENDIAN;
 582}
 583
 584static int
 585bpf_object__init_license(struct bpf_object *obj,
 586                         void *data, size_t size)
 587{
 588        memcpy(obj->license, data,
 589               min(size, sizeof(obj->license) - 1));
 590        pr_debug("license of %s is %s\n", obj->path, obj->license);
 591        return 0;
 592}
 593
 594static int
 595bpf_object__init_kversion(struct bpf_object *obj,
 596                          void *data, size_t size)
 597{
 598        u32 kver;
 599
 600        if (size != sizeof(kver)) {
 601                pr_warning("invalid kver section in %s\n", obj->path);
 602                return -LIBBPF_ERRNO__FORMAT;
 603        }
 604        memcpy(&kver, data, sizeof(kver));
 605        obj->kern_version = kver;
 606        pr_debug("kernel version of %s is %x\n", obj->path,
 607                 obj->kern_version);
 608        return 0;
 609}
 610
 611static int compare_bpf_map(const void *_a, const void *_b)
 612{
 613        const struct bpf_map *a = _a;
 614        const struct bpf_map *b = _b;
 615
 616        return a->offset - b->offset;
 617}
 618
 619static int
 620bpf_object__init_maps(struct bpf_object *obj)
 621{
 622        int i, map_idx, map_def_sz, nr_maps = 0;
 623        Elf_Scn *scn;
 624        Elf_Data *data;
 625        Elf_Data *symbols = obj->efile.symbols;
 626
 627        if (obj->efile.maps_shndx < 0)
 628                return -EINVAL;
 629        if (!symbols)
 630                return -EINVAL;
 631
 632        scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
 633        if (scn)
 634                data = elf_getdata(scn, NULL);
 635        if (!scn || !data) {
 636                pr_warning("failed to get Elf_Data from map section %d\n",
 637                           obj->efile.maps_shndx);
 638                return -EINVAL;
 639        }
 640
 641        /*
 642         * Count number of maps. Each map has a name.
 643         * Array of maps is not supported: only the first element is
 644         * considered.
 645         *
 646         * TODO: Detect array of map and report error.
 647         */
 648        for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
 649                GElf_Sym sym;
 650
 651                if (!gelf_getsym(symbols, i, &sym))
 652                        continue;
 653                if (sym.st_shndx != obj->efile.maps_shndx)
 654                        continue;
 655                nr_maps++;
 656        }
 657
 658        /* Alloc obj->maps and fill nr_maps. */
 659        pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
 660                 nr_maps, data->d_size);
 661
 662        if (!nr_maps)
 663                return 0;
 664
 665        /* Assume equally sized map definitions */
 666        map_def_sz = data->d_size / nr_maps;
 667        if (!data->d_size || (data->d_size % nr_maps) != 0) {
 668                pr_warning("unable to determine map definition size "
 669                           "section %s, %d maps in %zd bytes\n",
 670                           obj->path, nr_maps, data->d_size);
 671                return -EINVAL;
 672        }
 673
 674        obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
 675        if (!obj->maps) {
 676                pr_warning("alloc maps for object failed\n");
 677                return -ENOMEM;
 678        }
 679        obj->nr_maps = nr_maps;
 680
 681        /*
 682         * fill all fd with -1 so won't close incorrect
 683         * fd (fd=0 is stdin) when failure (zclose won't close
 684         * negative fd)).
 685         */
 686        for (i = 0; i < nr_maps; i++)
 687                obj->maps[i].fd = -1;
 688
 689        /*
 690         * Fill obj->maps using data in "maps" section.
 691         */
 692        for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
 693                GElf_Sym sym;
 694                const char *map_name;
 695                struct bpf_map_def *def;
 696
 697                if (!gelf_getsym(symbols, i, &sym))
 698                        continue;
 699                if (sym.st_shndx != obj->efile.maps_shndx)
 700                        continue;
 701
 702                map_name = elf_strptr(obj->efile.elf,
 703                                      obj->efile.strtabidx,
 704                                      sym.st_name);
 705                obj->maps[map_idx].offset = sym.st_value;
 706                if (sym.st_value + map_def_sz > data->d_size) {
 707                        pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
 708                                   obj->path, map_name);
 709                        return -EINVAL;
 710                }
 711
 712                obj->maps[map_idx].name = strdup(map_name);
 713                if (!obj->maps[map_idx].name) {
 714                        pr_warning("failed to alloc map name\n");
 715                        return -ENOMEM;
 716                }
 717                pr_debug("map %d is \"%s\"\n", map_idx,
 718                         obj->maps[map_idx].name);
 719                def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
 720                /*
 721                 * If the definition of the map in the object file fits in
 722                 * bpf_map_def, copy it.  Any extra fields in our version
 723                 * of bpf_map_def will default to zero as a result of the
 724                 * calloc above.
 725                 */
 726                if (map_def_sz <= sizeof(struct bpf_map_def)) {
 727                        memcpy(&obj->maps[map_idx].def, def, map_def_sz);
 728                } else {
 729                        /*
 730                         * Here the map structure being read is bigger than what
 731                         * we expect, truncate if the excess bits are all zero.
 732                         * If they are not zero, reject this map as
 733                         * incompatible.
 734                         */
 735                        char *b;
 736                        for (b = ((char *)def) + sizeof(struct bpf_map_def);
 737                             b < ((char *)def) + map_def_sz; b++) {
 738                                if (*b != 0) {
 739                                        pr_warning("maps section in %s: \"%s\" "
 740                                                   "has unrecognized, non-zero "
 741                                                   "options\n",
 742                                                   obj->path, map_name);
 743                                        return -EINVAL;
 744                                }
 745                        }
 746                        memcpy(&obj->maps[map_idx].def, def,
 747                               sizeof(struct bpf_map_def));
 748                }
 749                map_idx++;
 750        }
 751
 752        qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map);
 753        return 0;
 754}
 755
 756static bool section_have_execinstr(struct bpf_object *obj, int idx)
 757{
 758        Elf_Scn *scn;
 759        GElf_Shdr sh;
 760
 761        scn = elf_getscn(obj->efile.elf, idx);
 762        if (!scn)
 763                return false;
 764
 765        if (gelf_getshdr(scn, &sh) != &sh)
 766                return false;
 767
 768        if (sh.sh_flags & SHF_EXECINSTR)
 769                return true;
 770
 771        return false;
 772}
 773
 774static int bpf_object__elf_collect(struct bpf_object *obj)
 775{
 776        Elf *elf = obj->efile.elf;
 777        GElf_Ehdr *ep = &obj->efile.ehdr;
 778        Elf_Scn *scn = NULL;
 779        int idx = 0, err = 0;
 780
 781        /* Elf is corrupted/truncated, avoid calling elf_strptr. */
 782        if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
 783                pr_warning("failed to get e_shstrndx from %s\n",
 784                           obj->path);
 785                return -LIBBPF_ERRNO__FORMAT;
 786        }
 787
 788        while ((scn = elf_nextscn(elf, scn)) != NULL) {
 789                char *name;
 790                GElf_Shdr sh;
 791                Elf_Data *data;
 792
 793                idx++;
 794                if (gelf_getshdr(scn, &sh) != &sh) {
 795                        pr_warning("failed to get section(%d) header from %s\n",
 796                                   idx, obj->path);
 797                        err = -LIBBPF_ERRNO__FORMAT;
 798                        goto out;
 799                }
 800
 801                name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
 802                if (!name) {
 803                        pr_warning("failed to get section(%d) name from %s\n",
 804                                   idx, obj->path);
 805                        err = -LIBBPF_ERRNO__FORMAT;
 806                        goto out;
 807                }
 808
 809                data = elf_getdata(scn, 0);
 810                if (!data) {
 811                        pr_warning("failed to get section(%d) data from %s(%s)\n",
 812                                   idx, name, obj->path);
 813                        err = -LIBBPF_ERRNO__FORMAT;
 814                        goto out;
 815                }
 816                pr_debug("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
 817                         idx, name, (unsigned long)data->d_size,
 818                         (int)sh.sh_link, (unsigned long)sh.sh_flags,
 819                         (int)sh.sh_type);
 820
 821                if (strcmp(name, "license") == 0)
 822                        err = bpf_object__init_license(obj,
 823                                                       data->d_buf,
 824                                                       data->d_size);
 825                else if (strcmp(name, "version") == 0)
 826                        err = bpf_object__init_kversion(obj,
 827                                                        data->d_buf,
 828                                                        data->d_size);
 829                else if (strcmp(name, "maps") == 0)
 830                        obj->efile.maps_shndx = idx;
 831                else if (strcmp(name, BTF_ELF_SEC) == 0) {
 832                        obj->btf = btf__new(data->d_buf, data->d_size,
 833                                            __pr_debug);
 834                        if (IS_ERR(obj->btf)) {
 835                                pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
 836                                           BTF_ELF_SEC, PTR_ERR(obj->btf));
 837                                obj->btf = NULL;
 838                        }
 839                } else if (sh.sh_type == SHT_SYMTAB) {
 840                        if (obj->efile.symbols) {
 841                                pr_warning("bpf: multiple SYMTAB in %s\n",
 842                                           obj->path);
 843                                err = -LIBBPF_ERRNO__FORMAT;
 844                        } else {
 845                                obj->efile.symbols = data;
 846                                obj->efile.strtabidx = sh.sh_link;
 847                        }
 848                } else if ((sh.sh_type == SHT_PROGBITS) &&
 849                           (sh.sh_flags & SHF_EXECINSTR) &&
 850                           (data->d_size > 0)) {
 851                        if (strcmp(name, ".text") == 0)
 852                                obj->efile.text_shndx = idx;
 853                        err = bpf_object__add_program(obj, data->d_buf,
 854                                                      data->d_size, name, idx);
 855                        if (err) {
 856                                char errmsg[STRERR_BUFSIZE];
 857
 858                                strerror_r(-err, errmsg, sizeof(errmsg));
 859                                pr_warning("failed to alloc program %s (%s): %s",
 860                                           name, obj->path, errmsg);
 861                        }
 862                } else if (sh.sh_type == SHT_REL) {
 863                        void *reloc = obj->efile.reloc;
 864                        int nr_reloc = obj->efile.nr_reloc + 1;
 865                        int sec = sh.sh_info; /* points to other section */
 866
 867                        /* Only do relo for section with exec instructions */
 868                        if (!section_have_execinstr(obj, sec)) {
 869                                pr_debug("skip relo %s(%d) for section(%d)\n",
 870                                         name, idx, sec);
 871                                continue;
 872                        }
 873
 874                        reloc = realloc(reloc,
 875                                        sizeof(*obj->efile.reloc) * nr_reloc);
 876                        if (!reloc) {
 877                                pr_warning("realloc failed\n");
 878                                err = -ENOMEM;
 879                        } else {
 880                                int n = nr_reloc - 1;
 881
 882                                obj->efile.reloc = reloc;
 883                                obj->efile.nr_reloc = nr_reloc;
 884
 885                                obj->efile.reloc[n].shdr = sh;
 886                                obj->efile.reloc[n].data = data;
 887                        }
 888                } else {
 889                        pr_debug("skip section(%d) %s\n", idx, name);
 890                }
 891                if (err)
 892                        goto out;
 893        }
 894
 895        if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
 896                pr_warning("Corrupted ELF file: index of strtab invalid\n");
 897                return LIBBPF_ERRNO__FORMAT;
 898        }
 899        if (obj->efile.maps_shndx >= 0) {
 900                err = bpf_object__init_maps(obj);
 901                if (err)
 902                        goto out;
 903        }
 904        err = bpf_object__init_prog_names(obj);
 905out:
 906        return err;
 907}
 908
 909static struct bpf_program *
 910bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
 911{
 912        struct bpf_program *prog;
 913        size_t i;
 914
 915        for (i = 0; i < obj->nr_programs; i++) {
 916                prog = &obj->programs[i];
 917                if (prog->idx == idx)
 918                        return prog;
 919        }
 920        return NULL;
 921}
 922
 923static int
 924bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
 925                           Elf_Data *data, struct bpf_object *obj)
 926{
 927        Elf_Data *symbols = obj->efile.symbols;
 928        int text_shndx = obj->efile.text_shndx;
 929        int maps_shndx = obj->efile.maps_shndx;
 930        struct bpf_map *maps = obj->maps;
 931        size_t nr_maps = obj->nr_maps;
 932        int i, nrels;
 933
 934        pr_debug("collecting relocating info for: '%s'\n",
 935                 prog->section_name);
 936        nrels = shdr->sh_size / shdr->sh_entsize;
 937
 938        prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
 939        if (!prog->reloc_desc) {
 940                pr_warning("failed to alloc memory in relocation\n");
 941                return -ENOMEM;
 942        }
 943        prog->nr_reloc = nrels;
 944
 945        for (i = 0; i < nrels; i++) {
 946                GElf_Sym sym;
 947                GElf_Rel rel;
 948                unsigned int insn_idx;
 949                struct bpf_insn *insns = prog->insns;
 950                size_t map_idx;
 951
 952                if (!gelf_getrel(data, i, &rel)) {
 953                        pr_warning("relocation: failed to get %d reloc\n", i);
 954                        return -LIBBPF_ERRNO__FORMAT;
 955                }
 956
 957                if (!gelf_getsym(symbols,
 958                                 GELF_R_SYM(rel.r_info),
 959                                 &sym)) {
 960                        pr_warning("relocation: symbol %"PRIx64" not found\n",
 961                                   GELF_R_SYM(rel.r_info));
 962                        return -LIBBPF_ERRNO__FORMAT;
 963                }
 964                pr_debug("relo for %lld value %lld name %d\n",
 965                         (long long) (rel.r_info >> 32),
 966                         (long long) sym.st_value, sym.st_name);
 967
 968                if (sym.st_shndx != maps_shndx && sym.st_shndx != text_shndx) {
 969                        pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
 970                                   prog->section_name, sym.st_shndx);
 971                        return -LIBBPF_ERRNO__RELOC;
 972                }
 973
 974                insn_idx = rel.r_offset / sizeof(struct bpf_insn);
 975                pr_debug("relocation: insn_idx=%u\n", insn_idx);
 976
 977                if (insns[insn_idx].code == (BPF_JMP | BPF_CALL)) {
 978                        if (insns[insn_idx].src_reg != BPF_PSEUDO_CALL) {
 979                                pr_warning("incorrect bpf_call opcode\n");
 980                                return -LIBBPF_ERRNO__RELOC;
 981                        }
 982                        prog->reloc_desc[i].type = RELO_CALL;
 983                        prog->reloc_desc[i].insn_idx = insn_idx;
 984                        prog->reloc_desc[i].text_off = sym.st_value;
 985                        continue;
 986                }
 987
 988                if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
 989                        pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
 990                                   insn_idx, insns[insn_idx].code);
 991                        return -LIBBPF_ERRNO__RELOC;
 992                }
 993
 994                /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
 995                for (map_idx = 0; map_idx < nr_maps; map_idx++) {
 996                        if (maps[map_idx].offset == sym.st_value) {
 997                                pr_debug("relocation: find map %zd (%s) for insn %u\n",
 998                                         map_idx, maps[map_idx].name, insn_idx);
 999                                break;
1000                        }
1001                }
1002
1003                if (map_idx >= nr_maps) {
1004                        pr_warning("bpf relocation: map_idx %d large than %d\n",
1005                                   (int)map_idx, (int)nr_maps - 1);
1006                        return -LIBBPF_ERRNO__RELOC;
1007                }
1008
1009                prog->reloc_desc[i].type = RELO_LD64;
1010                prog->reloc_desc[i].insn_idx = insn_idx;
1011                prog->reloc_desc[i].map_idx = map_idx;
1012        }
1013        return 0;
1014}
1015
1016static int bpf_map_find_btf_info(struct bpf_map *map, const struct btf *btf)
1017{
1018        const struct btf_type *container_type;
1019        const struct btf_member *key, *value;
1020        struct bpf_map_def *def = &map->def;
1021        const size_t max_name = 256;
1022        char container_name[max_name];
1023        __s64 key_size, value_size;
1024        __s32 container_id;
1025
1026        if (snprintf(container_name, max_name, "____btf_map_%s", map->name) ==
1027            max_name) {
1028                pr_warning("map:%s length of '____btf_map_%s' is too long\n",
1029                           map->name, map->name);
1030                return -EINVAL;
1031        }
1032
1033        container_id = btf__find_by_name(btf, container_name);
1034        if (container_id < 0) {
1035                pr_debug("map:%s container_name:%s cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?\n",
1036                         map->name, container_name);
1037                return container_id;
1038        }
1039
1040        container_type = btf__type_by_id(btf, container_id);
1041        if (!container_type) {
1042                pr_warning("map:%s cannot find BTF type for container_id:%u\n",
1043                           map->name, container_id);
1044                return -EINVAL;
1045        }
1046
1047        if (BTF_INFO_KIND(container_type->info) != BTF_KIND_STRUCT ||
1048            BTF_INFO_VLEN(container_type->info) < 2) {
1049                pr_warning("map:%s container_name:%s is an invalid container struct\n",
1050                           map->name, container_name);
1051                return -EINVAL;
1052        }
1053
1054        key = (struct btf_member *)(container_type + 1);
1055        value = key + 1;
1056
1057        key_size = btf__resolve_size(btf, key->type);
1058        if (key_size < 0) {
1059                pr_warning("map:%s invalid BTF key_type_size\n",
1060                           map->name);
1061                return key_size;
1062        }
1063
1064        if (def->key_size != key_size) {
1065                pr_warning("map:%s btf_key_type_size:%u != map_def_key_size:%u\n",
1066                           map->name, (__u32)key_size, def->key_size);
1067                return -EINVAL;
1068        }
1069
1070        value_size = btf__resolve_size(btf, value->type);
1071        if (value_size < 0) {
1072                pr_warning("map:%s invalid BTF value_type_size\n", map->name);
1073                return value_size;
1074        }
1075
1076        if (def->value_size != value_size) {
1077                pr_warning("map:%s btf_value_type_size:%u != map_def_value_size:%u\n",
1078                           map->name, (__u32)value_size, def->value_size);
1079                return -EINVAL;
1080        }
1081
1082        map->btf_key_type_id = key->type;
1083        map->btf_value_type_id = value->type;
1084
1085        return 0;
1086}
1087
1088static int
1089bpf_object__create_maps(struct bpf_object *obj)
1090{
1091        struct bpf_create_map_attr create_attr = {};
1092        unsigned int i;
1093        int err;
1094
1095        for (i = 0; i < obj->nr_maps; i++) {
1096                struct bpf_map *map = &obj->maps[i];
1097                struct bpf_map_def *def = &map->def;
1098                int *pfd = &map->fd;
1099
1100                create_attr.name = map->name;
1101                create_attr.map_ifindex = map->map_ifindex;
1102                create_attr.map_type = def->type;
1103                create_attr.map_flags = def->map_flags;
1104                create_attr.key_size = def->key_size;
1105                create_attr.value_size = def->value_size;
1106                create_attr.max_entries = def->max_entries;
1107                create_attr.btf_fd = 0;
1108                create_attr.btf_key_type_id = 0;
1109                create_attr.btf_value_type_id = 0;
1110
1111                if (obj->btf && !bpf_map_find_btf_info(map, obj->btf)) {
1112                        create_attr.btf_fd = btf__fd(obj->btf);
1113                        create_attr.btf_key_type_id = map->btf_key_type_id;
1114                        create_attr.btf_value_type_id = map->btf_value_type_id;
1115                }
1116
1117                *pfd = bpf_create_map_xattr(&create_attr);
1118                if (*pfd < 0 && create_attr.btf_key_type_id) {
1119                        pr_warning("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
1120                                   map->name, strerror(errno), errno);
1121                        create_attr.btf_fd = 0;
1122                        create_attr.btf_key_type_id = 0;
1123                        create_attr.btf_value_type_id = 0;
1124                        map->btf_key_type_id = 0;
1125                        map->btf_value_type_id = 0;
1126                        *pfd = bpf_create_map_xattr(&create_attr);
1127                }
1128
1129                if (*pfd < 0) {
1130                        size_t j;
1131
1132                        err = *pfd;
1133                        pr_warning("failed to create map (name: '%s'): %s\n",
1134                                   map->name,
1135                                   strerror(errno));
1136                        for (j = 0; j < i; j++)
1137                                zclose(obj->maps[j].fd);
1138                        return err;
1139                }
1140                pr_debug("create map %s: fd=%d\n", map->name, *pfd);
1141        }
1142
1143        return 0;
1144}
1145
1146static int
1147bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
1148                        struct reloc_desc *relo)
1149{
1150        struct bpf_insn *insn, *new_insn;
1151        struct bpf_program *text;
1152        size_t new_cnt;
1153
1154        if (relo->type != RELO_CALL)
1155                return -LIBBPF_ERRNO__RELOC;
1156
1157        if (prog->idx == obj->efile.text_shndx) {
1158                pr_warning("relo in .text insn %d into off %d\n",
1159                           relo->insn_idx, relo->text_off);
1160                return -LIBBPF_ERRNO__RELOC;
1161        }
1162
1163        if (prog->main_prog_cnt == 0) {
1164                text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx);
1165                if (!text) {
1166                        pr_warning("no .text section found yet relo into text exist\n");
1167                        return -LIBBPF_ERRNO__RELOC;
1168                }
1169                new_cnt = prog->insns_cnt + text->insns_cnt;
1170                new_insn = realloc(prog->insns, new_cnt * sizeof(*insn));
1171                if (!new_insn) {
1172                        pr_warning("oom in prog realloc\n");
1173                        return -ENOMEM;
1174                }
1175                memcpy(new_insn + prog->insns_cnt, text->insns,
1176                       text->insns_cnt * sizeof(*insn));
1177                prog->insns = new_insn;
1178                prog->main_prog_cnt = prog->insns_cnt;
1179                prog->insns_cnt = new_cnt;
1180                pr_debug("added %zd insn from %s to prog %s\n",
1181                         text->insns_cnt, text->section_name,
1182                         prog->section_name);
1183        }
1184        insn = &prog->insns[relo->insn_idx];
1185        insn->imm += prog->main_prog_cnt - relo->insn_idx;
1186        return 0;
1187}
1188
1189static int
1190bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
1191{
1192        int i, err;
1193
1194        if (!prog || !prog->reloc_desc)
1195                return 0;
1196
1197        for (i = 0; i < prog->nr_reloc; i++) {
1198                if (prog->reloc_desc[i].type == RELO_LD64) {
1199                        struct bpf_insn *insns = prog->insns;
1200                        int insn_idx, map_idx;
1201
1202                        insn_idx = prog->reloc_desc[i].insn_idx;
1203                        map_idx = prog->reloc_desc[i].map_idx;
1204
1205                        if (insn_idx >= (int)prog->insns_cnt) {
1206                                pr_warning("relocation out of range: '%s'\n",
1207                                           prog->section_name);
1208                                return -LIBBPF_ERRNO__RELOC;
1209                        }
1210                        insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
1211                        insns[insn_idx].imm = obj->maps[map_idx].fd;
1212                } else {
1213                        err = bpf_program__reloc_text(prog, obj,
1214                                                      &prog->reloc_desc[i]);
1215                        if (err)
1216                                return err;
1217                }
1218        }
1219
1220        zfree(&prog->reloc_desc);
1221        prog->nr_reloc = 0;
1222        return 0;
1223}
1224
1225
1226static int
1227bpf_object__relocate(struct bpf_object *obj)
1228{
1229        struct bpf_program *prog;
1230        size_t i;
1231        int err;
1232
1233        for (i = 0; i < obj->nr_programs; i++) {
1234                prog = &obj->programs[i];
1235
1236                err = bpf_program__relocate(prog, obj);
1237                if (err) {
1238                        pr_warning("failed to relocate '%s'\n",
1239                                   prog->section_name);
1240                        return err;
1241                }
1242        }
1243        return 0;
1244}
1245
1246static int bpf_object__collect_reloc(struct bpf_object *obj)
1247{
1248        int i, err;
1249
1250        if (!obj_elf_valid(obj)) {
1251                pr_warning("Internal error: elf object is closed\n");
1252                return -LIBBPF_ERRNO__INTERNAL;
1253        }
1254
1255        for (i = 0; i < obj->efile.nr_reloc; i++) {
1256                GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
1257                Elf_Data *data = obj->efile.reloc[i].data;
1258                int idx = shdr->sh_info;
1259                struct bpf_program *prog;
1260
1261                if (shdr->sh_type != SHT_REL) {
1262                        pr_warning("internal error at %d\n", __LINE__);
1263                        return -LIBBPF_ERRNO__INTERNAL;
1264                }
1265
1266                prog = bpf_object__find_prog_by_idx(obj, idx);
1267                if (!prog) {
1268                        pr_warning("relocation failed: no section(%d)\n", idx);
1269                        return -LIBBPF_ERRNO__RELOC;
1270                }
1271
1272                err = bpf_program__collect_reloc(prog,
1273                                                 shdr, data,
1274                                                 obj);
1275                if (err)
1276                        return err;
1277        }
1278        return 0;
1279}
1280
1281static int
1282load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type,
1283             const char *name, struct bpf_insn *insns, int insns_cnt,
1284             char *license, u32 kern_version, int *pfd, int prog_ifindex)
1285{
1286        struct bpf_load_program_attr load_attr;
1287        char *log_buf;
1288        int ret;
1289
1290        memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
1291        load_attr.prog_type = type;
1292        load_attr.expected_attach_type = expected_attach_type;
1293        load_attr.name = name;
1294        load_attr.insns = insns;
1295        load_attr.insns_cnt = insns_cnt;
1296        load_attr.license = license;
1297        load_attr.kern_version = kern_version;
1298        load_attr.prog_ifindex = prog_ifindex;
1299
1300        if (!load_attr.insns || !load_attr.insns_cnt)
1301                return -EINVAL;
1302
1303        log_buf = malloc(BPF_LOG_BUF_SIZE);
1304        if (!log_buf)
1305                pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
1306
1307        ret = bpf_load_program_xattr(&load_attr, log_buf, BPF_LOG_BUF_SIZE);
1308
1309        if (ret >= 0) {
1310                *pfd = ret;
1311                ret = 0;
1312                goto out;
1313        }
1314
1315        ret = -LIBBPF_ERRNO__LOAD;
1316        pr_warning("load bpf program failed: %s\n", strerror(errno));
1317
1318        if (log_buf && log_buf[0] != '\0') {
1319                ret = -LIBBPF_ERRNO__VERIFY;
1320                pr_warning("-- BEGIN DUMP LOG ---\n");
1321                pr_warning("\n%s\n", log_buf);
1322                pr_warning("-- END LOG --\n");
1323        } else if (load_attr.insns_cnt >= BPF_MAXINSNS) {
1324                pr_warning("Program too large (%zu insns), at most %d insns\n",
1325                           load_attr.insns_cnt, BPF_MAXINSNS);
1326                ret = -LIBBPF_ERRNO__PROG2BIG;
1327        } else {
1328                /* Wrong program type? */
1329                if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
1330                        int fd;
1331
1332                        load_attr.prog_type = BPF_PROG_TYPE_KPROBE;
1333                        load_attr.expected_attach_type = 0;
1334                        fd = bpf_load_program_xattr(&load_attr, NULL, 0);
1335                        if (fd >= 0) {
1336                                close(fd);
1337                                ret = -LIBBPF_ERRNO__PROGTYPE;
1338                                goto out;
1339                        }
1340                }
1341
1342                if (log_buf)
1343                        ret = -LIBBPF_ERRNO__KVER;
1344        }
1345
1346out:
1347        free(log_buf);
1348        return ret;
1349}
1350
1351static int
1352bpf_program__load(struct bpf_program *prog,
1353                  char *license, u32 kern_version)
1354{
1355        int err = 0, fd, i;
1356
1357        if (prog->instances.nr < 0 || !prog->instances.fds) {
1358                if (prog->preprocessor) {
1359                        pr_warning("Internal error: can't load program '%s'\n",
1360                                   prog->section_name);
1361                        return -LIBBPF_ERRNO__INTERNAL;
1362                }
1363
1364                prog->instances.fds = malloc(sizeof(int));
1365                if (!prog->instances.fds) {
1366                        pr_warning("Not enough memory for BPF fds\n");
1367                        return -ENOMEM;
1368                }
1369                prog->instances.nr = 1;
1370                prog->instances.fds[0] = -1;
1371        }
1372
1373        if (!prog->preprocessor) {
1374                if (prog->instances.nr != 1) {
1375                        pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
1376                                   prog->section_name, prog->instances.nr);
1377                }
1378                err = load_program(prog->type, prog->expected_attach_type,
1379                                   prog->name, prog->insns, prog->insns_cnt,
1380                                   license, kern_version, &fd,
1381                                   prog->prog_ifindex);
1382                if (!err)
1383                        prog->instances.fds[0] = fd;
1384                goto out;
1385        }
1386
1387        for (i = 0; i < prog->instances.nr; i++) {
1388                struct bpf_prog_prep_result result;
1389                bpf_program_prep_t preprocessor = prog->preprocessor;
1390
1391                bzero(&result, sizeof(result));
1392                err = preprocessor(prog, i, prog->insns,
1393                                   prog->insns_cnt, &result);
1394                if (err) {
1395                        pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
1396                                   i, prog->section_name);
1397                        goto out;
1398                }
1399
1400                if (!result.new_insn_ptr || !result.new_insn_cnt) {
1401                        pr_debug("Skip loading the %dth instance of program '%s'\n",
1402                                 i, prog->section_name);
1403                        prog->instances.fds[i] = -1;
1404                        if (result.pfd)
1405                                *result.pfd = -1;
1406                        continue;
1407                }
1408
1409                err = load_program(prog->type, prog->expected_attach_type,
1410                                   prog->name, result.new_insn_ptr,
1411                                   result.new_insn_cnt,
1412                                   license, kern_version, &fd,
1413                                   prog->prog_ifindex);
1414
1415                if (err) {
1416                        pr_warning("Loading the %dth instance of program '%s' failed\n",
1417                                        i, prog->section_name);
1418                        goto out;
1419                }
1420
1421                if (result.pfd)
1422                        *result.pfd = fd;
1423                prog->instances.fds[i] = fd;
1424        }
1425out:
1426        if (err)
1427                pr_warning("failed to load program '%s'\n",
1428                           prog->section_name);
1429        zfree(&prog->insns);
1430        prog->insns_cnt = 0;
1431        return err;
1432}
1433
1434static int
1435bpf_object__load_progs(struct bpf_object *obj)
1436{
1437        size_t i;
1438        int err;
1439
1440        for (i = 0; i < obj->nr_programs; i++) {
1441                if (obj->programs[i].idx == obj->efile.text_shndx)
1442                        continue;
1443                err = bpf_program__load(&obj->programs[i],
1444                                        obj->license,
1445                                        obj->kern_version);
1446                if (err)
1447                        return err;
1448        }
1449        return 0;
1450}
1451
1452static bool bpf_prog_type__needs_kver(enum bpf_prog_type type)
1453{
1454        switch (type) {
1455        case BPF_PROG_TYPE_SOCKET_FILTER:
1456        case BPF_PROG_TYPE_SCHED_CLS:
1457        case BPF_PROG_TYPE_SCHED_ACT:
1458        case BPF_PROG_TYPE_XDP:
1459        case BPF_PROG_TYPE_CGROUP_SKB:
1460        case BPF_PROG_TYPE_CGROUP_SOCK:
1461        case BPF_PROG_TYPE_LWT_IN:
1462        case BPF_PROG_TYPE_LWT_OUT:
1463        case BPF_PROG_TYPE_LWT_XMIT:
1464        case BPF_PROG_TYPE_LWT_SEG6LOCAL:
1465        case BPF_PROG_TYPE_SOCK_OPS:
1466        case BPF_PROG_TYPE_SK_SKB:
1467        case BPF_PROG_TYPE_CGROUP_DEVICE:
1468        case BPF_PROG_TYPE_SK_MSG:
1469        case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1470        case BPF_PROG_TYPE_LIRC_MODE2:
1471                return false;
1472        case BPF_PROG_TYPE_UNSPEC:
1473        case BPF_PROG_TYPE_KPROBE:
1474        case BPF_PROG_TYPE_TRACEPOINT:
1475        case BPF_PROG_TYPE_PERF_EVENT:
1476        case BPF_PROG_TYPE_RAW_TRACEPOINT:
1477        default:
1478                return true;
1479        }
1480}
1481
1482static int bpf_object__validate(struct bpf_object *obj, bool needs_kver)
1483{
1484        if (needs_kver && obj->kern_version == 0) {
1485                pr_warning("%s doesn't provide kernel version\n",
1486                           obj->path);
1487                return -LIBBPF_ERRNO__KVERSION;
1488        }
1489        return 0;
1490}
1491
1492static struct bpf_object *
1493__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz,
1494                   bool needs_kver)
1495{
1496        struct bpf_object *obj;
1497        int err;
1498
1499        if (elf_version(EV_CURRENT) == EV_NONE) {
1500                pr_warning("failed to init libelf for %s\n", path);
1501                return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
1502        }
1503
1504        obj = bpf_object__new(path, obj_buf, obj_buf_sz);
1505        if (IS_ERR(obj))
1506                return obj;
1507
1508        CHECK_ERR(bpf_object__elf_init(obj), err, out);
1509        CHECK_ERR(bpf_object__check_endianness(obj), err, out);
1510        CHECK_ERR(bpf_object__elf_collect(obj), err, out);
1511        CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
1512        CHECK_ERR(bpf_object__validate(obj, needs_kver), err, out);
1513
1514        bpf_object__elf_finish(obj);
1515        return obj;
1516out:
1517        bpf_object__close(obj);
1518        return ERR_PTR(err);
1519}
1520
1521struct bpf_object *bpf_object__open(const char *path)
1522{
1523        /* param validation */
1524        if (!path)
1525                return NULL;
1526
1527        pr_debug("loading %s\n", path);
1528
1529        return __bpf_object__open(path, NULL, 0, true);
1530}
1531
1532struct bpf_object *bpf_object__open_buffer(void *obj_buf,
1533                                           size_t obj_buf_sz,
1534                                           const char *name)
1535{
1536        char tmp_name[64];
1537
1538        /* param validation */
1539        if (!obj_buf || obj_buf_sz <= 0)
1540                return NULL;
1541
1542        if (!name) {
1543                snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
1544                         (unsigned long)obj_buf,
1545                         (unsigned long)obj_buf_sz);
1546                tmp_name[sizeof(tmp_name) - 1] = '\0';
1547                name = tmp_name;
1548        }
1549        pr_debug("loading object '%s' from buffer\n",
1550                 name);
1551
1552        return __bpf_object__open(name, obj_buf, obj_buf_sz, true);
1553}
1554
1555int bpf_object__unload(struct bpf_object *obj)
1556{
1557        size_t i;
1558
1559        if (!obj)
1560                return -EINVAL;
1561
1562        for (i = 0; i < obj->nr_maps; i++)
1563                zclose(obj->maps[i].fd);
1564
1565        for (i = 0; i < obj->nr_programs; i++)
1566                bpf_program__unload(&obj->programs[i]);
1567
1568        return 0;
1569}
1570
1571int bpf_object__load(struct bpf_object *obj)
1572{
1573        int err;
1574
1575        if (!obj)
1576                return -EINVAL;
1577
1578        if (obj->loaded) {
1579                pr_warning("object should not be loaded twice\n");
1580                return -EINVAL;
1581        }
1582
1583        obj->loaded = true;
1584
1585        CHECK_ERR(bpf_object__create_maps(obj), err, out);
1586        CHECK_ERR(bpf_object__relocate(obj), err, out);
1587        CHECK_ERR(bpf_object__load_progs(obj), err, out);
1588
1589        return 0;
1590out:
1591        bpf_object__unload(obj);
1592        pr_warning("failed to load object '%s'\n", obj->path);
1593        return err;
1594}
1595
1596static int check_path(const char *path)
1597{
1598        struct statfs st_fs;
1599        char *dname, *dir;
1600        int err = 0;
1601
1602        if (path == NULL)
1603                return -EINVAL;
1604
1605        dname = strdup(path);
1606        if (dname == NULL)
1607                return -ENOMEM;
1608
1609        dir = dirname(dname);
1610        if (statfs(dir, &st_fs)) {
1611                pr_warning("failed to statfs %s: %s\n", dir, strerror(errno));
1612                err = -errno;
1613        }
1614        free(dname);
1615
1616        if (!err && st_fs.f_type != BPF_FS_MAGIC) {
1617                pr_warning("specified path %s is not on BPF FS\n", path);
1618                err = -EINVAL;
1619        }
1620
1621        return err;
1622}
1623
1624int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
1625                              int instance)
1626{
1627        int err;
1628
1629        err = check_path(path);
1630        if (err)
1631                return err;
1632
1633        if (prog == NULL) {
1634                pr_warning("invalid program pointer\n");
1635                return -EINVAL;
1636        }
1637
1638        if (instance < 0 || instance >= prog->instances.nr) {
1639                pr_warning("invalid prog instance %d of prog %s (max %d)\n",
1640                           instance, prog->section_name, prog->instances.nr);
1641                return -EINVAL;
1642        }
1643
1644        if (bpf_obj_pin(prog->instances.fds[instance], path)) {
1645                pr_warning("failed to pin program: %s\n", strerror(errno));
1646                return -errno;
1647        }
1648        pr_debug("pinned program '%s'\n", path);
1649
1650        return 0;
1651}
1652
1653static int make_dir(const char *path)
1654{
1655        int err = 0;
1656
1657        if (mkdir(path, 0700) && errno != EEXIST)
1658                err = -errno;
1659
1660        if (err)
1661                pr_warning("failed to mkdir %s: %s\n", path, strerror(-err));
1662        return err;
1663}
1664
1665int bpf_program__pin(struct bpf_program *prog, const char *path)
1666{
1667        int i, err;
1668
1669        err = check_path(path);
1670        if (err)
1671                return err;
1672
1673        if (prog == NULL) {
1674                pr_warning("invalid program pointer\n");
1675                return -EINVAL;
1676        }
1677
1678        if (prog->instances.nr <= 0) {
1679                pr_warning("no instances of prog %s to pin\n",
1680                           prog->section_name);
1681                return -EINVAL;
1682        }
1683
1684        err = make_dir(path);
1685        if (err)
1686                return err;
1687
1688        for (i = 0; i < prog->instances.nr; i++) {
1689                char buf[PATH_MAX];
1690                int len;
1691
1692                len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
1693                if (len < 0)
1694                        return -EINVAL;
1695                else if (len >= PATH_MAX)
1696                        return -ENAMETOOLONG;
1697
1698                err = bpf_program__pin_instance(prog, buf, i);
1699                if (err)
1700                        return err;
1701        }
1702
1703        return 0;
1704}
1705
1706int bpf_map__pin(struct bpf_map *map, const char *path)
1707{
1708        int err;
1709
1710        err = check_path(path);
1711        if (err)
1712                return err;
1713
1714        if (map == NULL) {
1715                pr_warning("invalid map pointer\n");
1716                return -EINVAL;
1717        }
1718
1719        if (bpf_obj_pin(map->fd, path)) {
1720                pr_warning("failed to pin map: %s\n", strerror(errno));
1721                return -errno;
1722        }
1723
1724        pr_debug("pinned map '%s'\n", path);
1725        return 0;
1726}
1727
1728int bpf_object__pin(struct bpf_object *obj, const char *path)
1729{
1730        struct bpf_program *prog;
1731        struct bpf_map *map;
1732        int err;
1733
1734        if (!obj)
1735                return -ENOENT;
1736
1737        if (!obj->loaded) {
1738                pr_warning("object not yet loaded; load it first\n");
1739                return -ENOENT;
1740        }
1741
1742        err = make_dir(path);
1743        if (err)
1744                return err;
1745
1746        bpf_map__for_each(map, obj) {
1747                char buf[PATH_MAX];
1748                int len;
1749
1750                len = snprintf(buf, PATH_MAX, "%s/%s", path,
1751                               bpf_map__name(map));
1752                if (len < 0)
1753                        return -EINVAL;
1754                else if (len >= PATH_MAX)
1755                        return -ENAMETOOLONG;
1756
1757                err = bpf_map__pin(map, buf);
1758                if (err)
1759                        return err;
1760        }
1761
1762        bpf_object__for_each_program(prog, obj) {
1763                char buf[PATH_MAX];
1764                int len;
1765
1766                len = snprintf(buf, PATH_MAX, "%s/%s", path,
1767                               prog->section_name);
1768                if (len < 0)
1769                        return -EINVAL;
1770                else if (len >= PATH_MAX)
1771                        return -ENAMETOOLONG;
1772
1773                err = bpf_program__pin(prog, buf);
1774                if (err)
1775                        return err;
1776        }
1777
1778        return 0;
1779}
1780
1781void bpf_object__close(struct bpf_object *obj)
1782{
1783        size_t i;
1784
1785        if (!obj)
1786                return;
1787
1788        if (obj->clear_priv)
1789                obj->clear_priv(obj, obj->priv);
1790
1791        bpf_object__elf_finish(obj);
1792        bpf_object__unload(obj);
1793        btf__free(obj->btf);
1794
1795        for (i = 0; i < obj->nr_maps; i++) {
1796                zfree(&obj->maps[i].name);
1797                if (obj->maps[i].clear_priv)
1798                        obj->maps[i].clear_priv(&obj->maps[i],
1799                                                obj->maps[i].priv);
1800                obj->maps[i].priv = NULL;
1801                obj->maps[i].clear_priv = NULL;
1802        }
1803        zfree(&obj->maps);
1804        obj->nr_maps = 0;
1805
1806        if (obj->programs && obj->nr_programs) {
1807                for (i = 0; i < obj->nr_programs; i++)
1808                        bpf_program__exit(&obj->programs[i]);
1809        }
1810        zfree(&obj->programs);
1811
1812        list_del(&obj->list);
1813        free(obj);
1814}
1815
1816struct bpf_object *
1817bpf_object__next(struct bpf_object *prev)
1818{
1819        struct bpf_object *next;
1820
1821        if (!prev)
1822                next = list_first_entry(&bpf_objects_list,
1823                                        struct bpf_object,
1824                                        list);
1825        else
1826                next = list_next_entry(prev, list);
1827
1828        /* Empty list is noticed here so don't need checking on entry. */
1829        if (&next->list == &bpf_objects_list)
1830                return NULL;
1831
1832        return next;
1833}
1834
1835const char *bpf_object__name(struct bpf_object *obj)
1836{
1837        return obj ? obj->path : ERR_PTR(-EINVAL);
1838}
1839
1840unsigned int bpf_object__kversion(struct bpf_object *obj)
1841{
1842        return obj ? obj->kern_version : 0;
1843}
1844
1845int bpf_object__btf_fd(const struct bpf_object *obj)
1846{
1847        return obj->btf ? btf__fd(obj->btf) : -1;
1848}
1849
1850int bpf_object__set_priv(struct bpf_object *obj, void *priv,
1851                         bpf_object_clear_priv_t clear_priv)
1852{
1853        if (obj->priv && obj->clear_priv)
1854                obj->clear_priv(obj, obj->priv);
1855
1856        obj->priv = priv;
1857        obj->clear_priv = clear_priv;
1858        return 0;
1859}
1860
1861void *bpf_object__priv(struct bpf_object *obj)
1862{
1863        return obj ? obj->priv : ERR_PTR(-EINVAL);
1864}
1865
1866struct bpf_program *
1867bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
1868{
1869        size_t idx;
1870
1871        if (!obj->programs)
1872                return NULL;
1873        /* First handler */
1874        if (prev == NULL)
1875                return &obj->programs[0];
1876
1877        if (prev->obj != obj) {
1878                pr_warning("error: program handler doesn't match object\n");
1879                return NULL;
1880        }
1881
1882        idx = (prev - obj->programs) + 1;
1883        if (idx >= obj->nr_programs)
1884                return NULL;
1885        return &obj->programs[idx];
1886}
1887
1888int bpf_program__set_priv(struct bpf_program *prog, void *priv,
1889                          bpf_program_clear_priv_t clear_priv)
1890{
1891        if (prog->priv && prog->clear_priv)
1892                prog->clear_priv(prog, prog->priv);
1893
1894        prog->priv = priv;
1895        prog->clear_priv = clear_priv;
1896        return 0;
1897}
1898
1899void *bpf_program__priv(struct bpf_program *prog)
1900{
1901        return prog ? prog->priv : ERR_PTR(-EINVAL);
1902}
1903
1904const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
1905{
1906        const char *title;
1907
1908        title = prog->section_name;
1909        if (needs_copy) {
1910                title = strdup(title);
1911                if (!title) {
1912                        pr_warning("failed to strdup program title\n");
1913                        return ERR_PTR(-ENOMEM);
1914                }
1915        }
1916
1917        return title;
1918}
1919
1920int bpf_program__fd(struct bpf_program *prog)
1921{
1922        return bpf_program__nth_fd(prog, 0);
1923}
1924
1925int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
1926                          bpf_program_prep_t prep)
1927{
1928        int *instances_fds;
1929
1930        if (nr_instances <= 0 || !prep)
1931                return -EINVAL;
1932
1933        if (prog->instances.nr > 0 || prog->instances.fds) {
1934                pr_warning("Can't set pre-processor after loading\n");
1935                return -EINVAL;
1936        }
1937
1938        instances_fds = malloc(sizeof(int) * nr_instances);
1939        if (!instances_fds) {
1940                pr_warning("alloc memory failed for fds\n");
1941                return -ENOMEM;
1942        }
1943
1944        /* fill all fd with -1 */
1945        memset(instances_fds, -1, sizeof(int) * nr_instances);
1946
1947        prog->instances.nr = nr_instances;
1948        prog->instances.fds = instances_fds;
1949        prog->preprocessor = prep;
1950        return 0;
1951}
1952
1953int bpf_program__nth_fd(struct bpf_program *prog, int n)
1954{
1955        int fd;
1956
1957        if (n >= prog->instances.nr || n < 0) {
1958                pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
1959                           n, prog->section_name, prog->instances.nr);
1960                return -EINVAL;
1961        }
1962
1963        fd = prog->instances.fds[n];
1964        if (fd < 0) {
1965                pr_warning("%dth instance of program '%s' is invalid\n",
1966                           n, prog->section_name);
1967                return -ENOENT;
1968        }
1969
1970        return fd;
1971}
1972
1973void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
1974{
1975        prog->type = type;
1976}
1977
1978static bool bpf_program__is_type(struct bpf_program *prog,
1979                                 enum bpf_prog_type type)
1980{
1981        return prog ? (prog->type == type) : false;
1982}
1983
1984#define BPF_PROG_TYPE_FNS(NAME, TYPE)                   \
1985int bpf_program__set_##NAME(struct bpf_program *prog)   \
1986{                                                       \
1987        if (!prog)                                      \
1988                return -EINVAL;                         \
1989        bpf_program__set_type(prog, TYPE);              \
1990        return 0;                                       \
1991}                                                       \
1992                                                        \
1993bool bpf_program__is_##NAME(struct bpf_program *prog)   \
1994{                                                       \
1995        return bpf_program__is_type(prog, TYPE);        \
1996}                                                       \
1997
1998BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
1999BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
2000BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
2001BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
2002BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
2003BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT);
2004BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
2005BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
2006
2007void bpf_program__set_expected_attach_type(struct bpf_program *prog,
2008                                           enum bpf_attach_type type)
2009{
2010        prog->expected_attach_type = type;
2011}
2012
2013#define BPF_PROG_SEC_FULL(string, ptype, atype) \
2014        { string, sizeof(string) - 1, ptype, atype }
2015
2016#define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_FULL(string, ptype, 0)
2017
2018#define BPF_S_PROG_SEC(string, ptype) \
2019        BPF_PROG_SEC_FULL(string, BPF_PROG_TYPE_CGROUP_SOCK, ptype)
2020
2021#define BPF_SA_PROG_SEC(string, ptype) \
2022        BPF_PROG_SEC_FULL(string, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, ptype)
2023
2024static const struct {
2025        const char *sec;
2026        size_t len;
2027        enum bpf_prog_type prog_type;
2028        enum bpf_attach_type expected_attach_type;
2029} section_names[] = {
2030        BPF_PROG_SEC("socket",          BPF_PROG_TYPE_SOCKET_FILTER),
2031        BPF_PROG_SEC("kprobe/",         BPF_PROG_TYPE_KPROBE),
2032        BPF_PROG_SEC("kretprobe/",      BPF_PROG_TYPE_KPROBE),
2033        BPF_PROG_SEC("classifier",      BPF_PROG_TYPE_SCHED_CLS),
2034        BPF_PROG_SEC("action",          BPF_PROG_TYPE_SCHED_ACT),
2035        BPF_PROG_SEC("tracepoint/",     BPF_PROG_TYPE_TRACEPOINT),
2036        BPF_PROG_SEC("raw_tracepoint/", BPF_PROG_TYPE_RAW_TRACEPOINT),
2037        BPF_PROG_SEC("xdp",             BPF_PROG_TYPE_XDP),
2038        BPF_PROG_SEC("perf_event",      BPF_PROG_TYPE_PERF_EVENT),
2039        BPF_PROG_SEC("cgroup/skb",      BPF_PROG_TYPE_CGROUP_SKB),
2040        BPF_PROG_SEC("cgroup/sock",     BPF_PROG_TYPE_CGROUP_SOCK),
2041        BPF_PROG_SEC("cgroup/dev",      BPF_PROG_TYPE_CGROUP_DEVICE),
2042        BPF_PROG_SEC("lwt_in",          BPF_PROG_TYPE_LWT_IN),
2043        BPF_PROG_SEC("lwt_out",         BPF_PROG_TYPE_LWT_OUT),
2044        BPF_PROG_SEC("lwt_xmit",        BPF_PROG_TYPE_LWT_XMIT),
2045        BPF_PROG_SEC("sockops",         BPF_PROG_TYPE_SOCK_OPS),
2046        BPF_PROG_SEC("sk_skb",          BPF_PROG_TYPE_SK_SKB),
2047        BPF_PROG_SEC("sk_msg",          BPF_PROG_TYPE_SK_MSG),
2048        BPF_SA_PROG_SEC("cgroup/bind4", BPF_CGROUP_INET4_BIND),
2049        BPF_SA_PROG_SEC("cgroup/bind6", BPF_CGROUP_INET6_BIND),
2050        BPF_SA_PROG_SEC("cgroup/connect4", BPF_CGROUP_INET4_CONNECT),
2051        BPF_SA_PROG_SEC("cgroup/connect6", BPF_CGROUP_INET6_CONNECT),
2052        BPF_SA_PROG_SEC("cgroup/sendmsg4", BPF_CGROUP_UDP4_SENDMSG),
2053        BPF_SA_PROG_SEC("cgroup/sendmsg6", BPF_CGROUP_UDP6_SENDMSG),
2054        BPF_S_PROG_SEC("cgroup/post_bind4", BPF_CGROUP_INET4_POST_BIND),
2055        BPF_S_PROG_SEC("cgroup/post_bind6", BPF_CGROUP_INET6_POST_BIND),
2056};
2057
2058#undef BPF_PROG_SEC
2059#undef BPF_PROG_SEC_FULL
2060#undef BPF_S_PROG_SEC
2061#undef BPF_SA_PROG_SEC
2062
2063static int bpf_program__identify_section(struct bpf_program *prog)
2064{
2065        int i;
2066
2067        if (!prog->section_name)
2068                goto err;
2069
2070        for (i = 0; i < ARRAY_SIZE(section_names); i++)
2071                if (strncmp(prog->section_name, section_names[i].sec,
2072                            section_names[i].len) == 0)
2073                        return i;
2074
2075err:
2076        pr_warning("failed to guess program type based on section name %s\n",
2077                   prog->section_name);
2078
2079        return -1;
2080}
2081
2082int bpf_map__fd(struct bpf_map *map)
2083{
2084        return map ? map->fd : -EINVAL;
2085}
2086
2087const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
2088{
2089        return map ? &map->def : ERR_PTR(-EINVAL);
2090}
2091
2092const char *bpf_map__name(struct bpf_map *map)
2093{
2094        return map ? map->name : NULL;
2095}
2096
2097__u32 bpf_map__btf_key_type_id(const struct bpf_map *map)
2098{
2099        return map ? map->btf_key_type_id : 0;
2100}
2101
2102__u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
2103{
2104        return map ? map->btf_value_type_id : 0;
2105}
2106
2107int bpf_map__set_priv(struct bpf_map *map, void *priv,
2108                     bpf_map_clear_priv_t clear_priv)
2109{
2110        if (!map)
2111                return -EINVAL;
2112
2113        if (map->priv) {
2114                if (map->clear_priv)
2115                        map->clear_priv(map, map->priv);
2116        }
2117
2118        map->priv = priv;
2119        map->clear_priv = clear_priv;
2120        return 0;
2121}
2122
2123void *bpf_map__priv(struct bpf_map *map)
2124{
2125        return map ? map->priv : ERR_PTR(-EINVAL);
2126}
2127
2128struct bpf_map *
2129bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
2130{
2131        size_t idx;
2132        struct bpf_map *s, *e;
2133
2134        if (!obj || !obj->maps)
2135                return NULL;
2136
2137        s = obj->maps;
2138        e = obj->maps + obj->nr_maps;
2139
2140        if (prev == NULL)
2141                return s;
2142
2143        if ((prev < s) || (prev >= e)) {
2144                pr_warning("error in %s: map handler doesn't belong to object\n",
2145                           __func__);
2146                return NULL;
2147        }
2148
2149        idx = (prev - obj->maps) + 1;
2150        if (idx >= obj->nr_maps)
2151                return NULL;
2152        return &obj->maps[idx];
2153}
2154
2155struct bpf_map *
2156bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
2157{
2158        struct bpf_map *pos;
2159
2160        bpf_map__for_each(pos, obj) {
2161                if (pos->name && !strcmp(pos->name, name))
2162                        return pos;
2163        }
2164        return NULL;
2165}
2166
2167struct bpf_map *
2168bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
2169{
2170        int i;
2171
2172        for (i = 0; i < obj->nr_maps; i++) {
2173                if (obj->maps[i].offset == offset)
2174                        return &obj->maps[i];
2175        }
2176        return ERR_PTR(-ENOENT);
2177}
2178
2179long libbpf_get_error(const void *ptr)
2180{
2181        if (IS_ERR(ptr))
2182                return PTR_ERR(ptr);
2183        return 0;
2184}
2185
2186int bpf_prog_load(const char *file, enum bpf_prog_type type,
2187                  struct bpf_object **pobj, int *prog_fd)
2188{
2189        struct bpf_prog_load_attr attr;
2190
2191        memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
2192        attr.file = file;
2193        attr.prog_type = type;
2194        attr.expected_attach_type = 0;
2195
2196        return bpf_prog_load_xattr(&attr, pobj, prog_fd);
2197}
2198
2199int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
2200                        struct bpf_object **pobj, int *prog_fd)
2201{
2202        struct bpf_program *prog, *first_prog = NULL;
2203        enum bpf_attach_type expected_attach_type;
2204        enum bpf_prog_type prog_type;
2205        struct bpf_object *obj;
2206        struct bpf_map *map;
2207        int section_idx;
2208        int err;
2209
2210        if (!attr)
2211                return -EINVAL;
2212        if (!attr->file)
2213                return -EINVAL;
2214
2215        obj = __bpf_object__open(attr->file, NULL, 0,
2216                                 bpf_prog_type__needs_kver(attr->prog_type));
2217        if (IS_ERR_OR_NULL(obj))
2218                return -ENOENT;
2219
2220        bpf_object__for_each_program(prog, obj) {
2221                /*
2222                 * If type is not specified, try to guess it based on
2223                 * section name.
2224                 */
2225                prog_type = attr->prog_type;
2226                prog->prog_ifindex = attr->ifindex;
2227                expected_attach_type = attr->expected_attach_type;
2228                if (prog_type == BPF_PROG_TYPE_UNSPEC) {
2229                        section_idx = bpf_program__identify_section(prog);
2230                        if (section_idx < 0) {
2231                                bpf_object__close(obj);
2232                                return -EINVAL;
2233                        }
2234                        prog_type = section_names[section_idx].prog_type;
2235                        expected_attach_type =
2236                                section_names[section_idx].expected_attach_type;
2237                }
2238
2239                bpf_program__set_type(prog, prog_type);
2240                bpf_program__set_expected_attach_type(prog,
2241                                                      expected_attach_type);
2242
2243                if (prog->idx != obj->efile.text_shndx && !first_prog)
2244                        first_prog = prog;
2245        }
2246
2247        bpf_map__for_each(map, obj) {
2248                map->map_ifindex = attr->ifindex;
2249        }
2250
2251        if (!first_prog) {
2252                pr_warning("object file doesn't contain bpf program\n");
2253                bpf_object__close(obj);
2254                return -ENOENT;
2255        }
2256
2257        err = bpf_object__load(obj);
2258        if (err) {
2259                bpf_object__close(obj);
2260                return -EINVAL;
2261        }
2262
2263        *pobj = obj;
2264        *prog_fd = bpf_program__fd(first_prog);
2265        return 0;
2266}
2267
2268enum bpf_perf_event_ret
2269bpf_perf_event_read_simple(void *mem, unsigned long size,
2270                           unsigned long page_size, void **buf, size_t *buf_len,
2271                           bpf_perf_event_print_t fn, void *priv)
2272{
2273        volatile struct perf_event_mmap_page *header = mem;
2274        __u64 data_tail = header->data_tail;
2275        __u64 data_head = header->data_head;
2276        int ret = LIBBPF_PERF_EVENT_ERROR;
2277        void *base, *begin, *end;
2278
2279        asm volatile("" ::: "memory"); /* in real code it should be smp_rmb() */
2280        if (data_head == data_tail)
2281                return LIBBPF_PERF_EVENT_CONT;
2282
2283        base = ((char *)header) + page_size;
2284
2285        begin = base + data_tail % size;
2286        end = base + data_head % size;
2287
2288        while (begin != end) {
2289                struct perf_event_header *ehdr;
2290
2291                ehdr = begin;
2292                if (begin + ehdr->size > base + size) {
2293                        long len = base + size - begin;
2294
2295                        if (*buf_len < ehdr->size) {
2296                                free(*buf);
2297                                *buf = malloc(ehdr->size);
2298                                if (!*buf) {
2299                                        ret = LIBBPF_PERF_EVENT_ERROR;
2300                                        break;
2301                                }
2302                                *buf_len = ehdr->size;
2303                        }
2304
2305                        memcpy(*buf, begin, len);
2306                        memcpy(*buf + len, base, ehdr->size - len);
2307                        ehdr = (void *)*buf;
2308                        begin = base + ehdr->size - len;
2309                } else if (begin + ehdr->size == base + size) {
2310                        begin = base;
2311                } else {
2312                        begin += ehdr->size;
2313                }
2314
2315                ret = fn(ehdr, priv);
2316                if (ret != LIBBPF_PERF_EVENT_CONT)
2317                        break;
2318
2319                data_tail += ehdr->size;
2320        }
2321
2322        __sync_synchronize(); /* smp_mb() */
2323        header->data_tail = data_tail;
2324
2325        return ret;
2326}
2327