linux/tools/lib/bpf/libbpf.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
   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 */
  10#ifndef __LIBBPF_LIBBPF_H
  11#define __LIBBPF_LIBBPF_H
  12
  13#include <stdarg.h>
  14#include <stdio.h>
  15#include <stdint.h>
  16#include <stdbool.h>
  17#include <sys/types.h>  // for size_t
  18#include <linux/bpf.h>
  19
  20#ifdef __cplusplus
  21extern "C" {
  22#endif
  23
  24#ifndef LIBBPF_API
  25#define LIBBPF_API __attribute__((visibility("default")))
  26#endif
  27
  28enum libbpf_errno {
  29        __LIBBPF_ERRNO__START = 4000,
  30
  31        /* Something wrong in libelf */
  32        LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
  33        LIBBPF_ERRNO__FORMAT,   /* BPF object format invalid */
  34        LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
  35        LIBBPF_ERRNO__ENDIAN,   /* Endian mismatch */
  36        LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
  37        LIBBPF_ERRNO__RELOC,    /* Relocation failed */
  38        LIBBPF_ERRNO__LOAD,     /* Load program failure for unknown reason */
  39        LIBBPF_ERRNO__VERIFY,   /* Kernel verifier blocks program loading */
  40        LIBBPF_ERRNO__PROG2BIG, /* Program too big */
  41        LIBBPF_ERRNO__KVER,     /* Incorrect kernel version */
  42        LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
  43        LIBBPF_ERRNO__WRNGPID,  /* Wrong pid in netlink message */
  44        LIBBPF_ERRNO__INVSEQ,   /* Invalid netlink sequence */
  45        LIBBPF_ERRNO__NLPARSE,  /* netlink parsing error */
  46        __LIBBPF_ERRNO__END,
  47};
  48
  49LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
  50
  51enum libbpf_print_level {
  52        LIBBPF_WARN,
  53        LIBBPF_INFO,
  54        LIBBPF_DEBUG,
  55};
  56
  57typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
  58                                 const char *, va_list ap);
  59
  60LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
  61
  62/* Hide internal to user */
  63struct bpf_object;
  64
  65struct bpf_object_open_attr {
  66        const char *file;
  67        enum bpf_prog_type prog_type;
  68};
  69
  70/* Helper macro to declare and initialize libbpf options struct
  71 *
  72 * This dance with uninitialized declaration, followed by memset to zero,
  73 * followed by assignment using compound literal syntax is done to preserve
  74 * ability to use a nice struct field initialization syntax and **hopefully**
  75 * have all the padding bytes initialized to zero. It's not guaranteed though,
  76 * when copying literal, that compiler won't copy garbage in literal's padding
  77 * bytes, but that's the best way I've found and it seems to work in practice.
  78 *
  79 * Macro declares opts struct of given type and name, zero-initializes,
  80 * including any extra padding, it with memset() and then assigns initial
  81 * values provided by users in struct initializer-syntax as varargs.
  82 */
  83#define DECLARE_LIBBPF_OPTS(TYPE, NAME, ...)                                \
  84        struct TYPE NAME = ({                                               \
  85                memset(&NAME, 0, sizeof(struct TYPE));                      \
  86                (struct TYPE) {                                             \
  87                        .sz = sizeof(struct TYPE),                          \
  88                        __VA_ARGS__                                         \
  89                };                                                          \
  90        })
  91
  92struct bpf_object_open_opts {
  93        /* size of this struct, for forward/backward compatiblity */
  94        size_t sz;
  95        /* object name override, if provided:
  96         * - for object open from file, this will override setting object
  97         *   name from file path's base name;
  98         * - for object open from memory buffer, this will specify an object
  99         *   name and will override default "<addr>-<buf-size>" name;
 100         */
 101        const char *object_name;
 102        /* parse map definitions non-strictly, allowing extra attributes/data */
 103        bool relaxed_maps;
 104        /* process CO-RE relocations non-strictly, allowing them to fail */
 105        bool relaxed_core_relocs;
 106        /* maps that set the 'pinning' attribute in their definition will have
 107         * their pin_path attribute set to a file in this directory, and be
 108         * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
 109         */
 110        const char *pin_root_path;
 111};
 112#define bpf_object_open_opts__last_field pin_root_path
 113
 114LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
 115LIBBPF_API struct bpf_object *
 116bpf_object__open_file(const char *path, struct bpf_object_open_opts *opts);
 117LIBBPF_API struct bpf_object *
 118bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
 119                     struct bpf_object_open_opts *opts);
 120
 121/* deprecated bpf_object__open variants */
 122LIBBPF_API struct bpf_object *
 123bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz,
 124                        const char *name);
 125LIBBPF_API struct bpf_object *
 126bpf_object__open_xattr(struct bpf_object_open_attr *attr);
 127
 128int bpf_object__section_size(const struct bpf_object *obj, const char *name,
 129                             __u32 *size);
 130int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
 131                                __u32 *off);
 132
 133enum libbpf_pin_type {
 134        LIBBPF_PIN_NONE,
 135        /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
 136        LIBBPF_PIN_BY_NAME,
 137};
 138
 139/* pin_maps and unpin_maps can both be called with a NULL path, in which case
 140 * they will use the pin_path attribute of each map (and ignore all maps that
 141 * don't have a pin_path set).
 142 */
 143LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
 144LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
 145                                      const char *path);
 146LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
 147                                        const char *path);
 148LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
 149                                          const char *path);
 150LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
 151LIBBPF_API void bpf_object__close(struct bpf_object *object);
 152
 153struct bpf_object_load_attr {
 154        struct bpf_object *obj;
 155        int log_level;
 156        const char *target_btf_path;
 157};
 158
 159/* Load/unload object into/from kernel */
 160LIBBPF_API int bpf_object__load(struct bpf_object *obj);
 161LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr);
 162LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
 163LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
 164LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
 165
 166struct btf;
 167LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
 168LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
 169
 170LIBBPF_API struct bpf_program *
 171bpf_object__find_program_by_title(const struct bpf_object *obj,
 172                                  const char *title);
 173
 174LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
 175#define bpf_object__for_each_safe(pos, tmp)                     \
 176        for ((pos) = bpf_object__next(NULL),            \
 177                (tmp) = bpf_object__next(pos);          \
 178             (pos) != NULL;                             \
 179             (pos) = (tmp), (tmp) = bpf_object__next(tmp))
 180
 181typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
 182LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
 183                                    bpf_object_clear_priv_t clear_priv);
 184LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog);
 185
 186LIBBPF_API int
 187libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
 188                         enum bpf_attach_type *expected_attach_type);
 189LIBBPF_API int libbpf_attach_type_by_name(const char *name,
 190                                          enum bpf_attach_type *attach_type);
 191
 192/* Accessors of bpf_program */
 193struct bpf_program;
 194LIBBPF_API struct bpf_program *bpf_program__next(struct bpf_program *prog,
 195                                                 const struct bpf_object *obj);
 196
 197#define bpf_object__for_each_program(pos, obj)          \
 198        for ((pos) = bpf_program__next(NULL, (obj));    \
 199             (pos) != NULL;                             \
 200             (pos) = bpf_program__next((pos), (obj)))
 201
 202LIBBPF_API struct bpf_program *bpf_program__prev(struct bpf_program *prog,
 203                                                 const struct bpf_object *obj);
 204
 205typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
 206
 207LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
 208                                     bpf_program_clear_priv_t clear_priv);
 209
 210LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
 211LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
 212                                         __u32 ifindex);
 213
 214LIBBPF_API const char *bpf_program__title(const struct bpf_program *prog,
 215                                          bool needs_copy);
 216
 217/* returns program size in bytes */
 218LIBBPF_API size_t bpf_program__size(const struct bpf_program *prog);
 219
 220LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license,
 221                                 __u32 kern_version);
 222LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
 223LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
 224                                         const char *path,
 225                                         int instance);
 226LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
 227                                           const char *path,
 228                                           int instance);
 229LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
 230LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
 231LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
 232
 233struct bpf_link;
 234
 235LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
 236
 237LIBBPF_API struct bpf_link *
 238bpf_program__attach_perf_event(struct bpf_program *prog, int pfd);
 239LIBBPF_API struct bpf_link *
 240bpf_program__attach_kprobe(struct bpf_program *prog, bool retprobe,
 241                           const char *func_name);
 242LIBBPF_API struct bpf_link *
 243bpf_program__attach_uprobe(struct bpf_program *prog, bool retprobe,
 244                           pid_t pid, const char *binary_path,
 245                           size_t func_offset);
 246LIBBPF_API struct bpf_link *
 247bpf_program__attach_tracepoint(struct bpf_program *prog,
 248                               const char *tp_category,
 249                               const char *tp_name);
 250LIBBPF_API struct bpf_link *
 251bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
 252                                   const char *tp_name);
 253
 254struct bpf_insn;
 255
 256/*
 257 * Libbpf allows callers to adjust BPF programs before being loaded
 258 * into kernel. One program in an object file can be transformed into
 259 * multiple variants to be attached to different hooks.
 260 *
 261 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
 262 * form an API for this purpose.
 263 *
 264 * - bpf_program_prep_t:
 265 *   Defines a 'preprocessor', which is a caller defined function
 266 *   passed to libbpf through bpf_program__set_prep(), and will be
 267 *   called before program is loaded. The processor should adjust
 268 *   the program one time for each instance according to the instance id
 269 *   passed to it.
 270 *
 271 * - bpf_program__set_prep:
 272 *   Attaches a preprocessor to a BPF program. The number of instances
 273 *   that should be created is also passed through this function.
 274 *
 275 * - bpf_program__nth_fd:
 276 *   After the program is loaded, get resulting FD of a given instance
 277 *   of the BPF program.
 278 *
 279 * If bpf_program__set_prep() is not used, the program would be loaded
 280 * without adjustment during bpf_object__load(). The program has only
 281 * one instance. In this case bpf_program__fd(prog) is equal to
 282 * bpf_program__nth_fd(prog, 0).
 283 */
 284
 285struct bpf_prog_prep_result {
 286        /*
 287         * If not NULL, load new instruction array.
 288         * If set to NULL, don't load this instance.
 289         */
 290        struct bpf_insn *new_insn_ptr;
 291        int new_insn_cnt;
 292
 293        /* If not NULL, result FD is written to it. */
 294        int *pfd;
 295};
 296
 297/*
 298 * Parameters of bpf_program_prep_t:
 299 *  - prog:     The bpf_program being loaded.
 300 *  - n:        Index of instance being generated.
 301 *  - insns:    BPF instructions array.
 302 *  - insns_cnt:Number of instructions in insns.
 303 *  - res:      Output parameter, result of transformation.
 304 *
 305 * Return value:
 306 *  - Zero:     pre-processing success.
 307 *  - Non-zero: pre-processing error, stop loading.
 308 */
 309typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
 310                                  struct bpf_insn *insns, int insns_cnt,
 311                                  struct bpf_prog_prep_result *res);
 312
 313LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
 314                                     bpf_program_prep_t prep);
 315
 316LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n);
 317
 318/*
 319 * Adjust type of BPF program. Default is kprobe.
 320 */
 321LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
 322LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
 323LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
 324LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
 325LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
 326LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
 327LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
 328LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
 329
 330LIBBPF_API enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog);
 331LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
 332                                      enum bpf_prog_type type);
 333
 334LIBBPF_API enum bpf_attach_type
 335bpf_program__get_expected_attach_type(struct bpf_program *prog);
 336LIBBPF_API void
 337bpf_program__set_expected_attach_type(struct bpf_program *prog,
 338                                      enum bpf_attach_type type);
 339
 340LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog);
 341LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog);
 342LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog);
 343LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog);
 344LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog);
 345LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog);
 346LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
 347LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
 348
 349/*
 350 * No need for __attribute__((packed)), all members of 'bpf_map_def'
 351 * are all aligned.  In addition, using __attribute__((packed))
 352 * would trigger a -Wpacked warning message, and lead to an error
 353 * if -Werror is set.
 354 */
 355struct bpf_map_def {
 356        unsigned int type;
 357        unsigned int key_size;
 358        unsigned int value_size;
 359        unsigned int max_entries;
 360        unsigned int map_flags;
 361};
 362
 363/*
 364 * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel,
 365 * so no need to worry about a name clash.
 366 */
 367struct bpf_map;
 368LIBBPF_API struct bpf_map *
 369bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
 370
 371LIBBPF_API int
 372bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
 373
 374/*
 375 * Get bpf_map through the offset of corresponding struct bpf_map_def
 376 * in the BPF object file.
 377 */
 378LIBBPF_API struct bpf_map *
 379bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
 380
 381LIBBPF_API struct bpf_map *
 382bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
 383#define bpf_object__for_each_map(pos, obj)              \
 384        for ((pos) = bpf_map__next(NULL, (obj));        \
 385             (pos) != NULL;                             \
 386             (pos) = bpf_map__next((pos), (obj)))
 387#define bpf_map__for_each bpf_object__for_each_map
 388
 389LIBBPF_API struct bpf_map *
 390bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
 391
 392LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
 393LIBBPF_API const struct bpf_map_def *bpf_map__def(const struct bpf_map *map);
 394LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
 395LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
 396LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
 397
 398typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
 399LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
 400                                 bpf_map_clear_priv_t clear_priv);
 401LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
 402LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
 403LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
 404LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);
 405LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
 406LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
 407LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
 408LIBBPF_API const char *bpf_map__get_pin_path(const struct bpf_map *map);
 409LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
 410LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
 411LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
 412
 413LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
 414
 415LIBBPF_API long libbpf_get_error(const void *ptr);
 416
 417struct bpf_prog_load_attr {
 418        const char *file;
 419        enum bpf_prog_type prog_type;
 420        enum bpf_attach_type expected_attach_type;
 421        int ifindex;
 422        int log_level;
 423        int prog_flags;
 424};
 425
 426LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
 427                                   struct bpf_object **pobj, int *prog_fd);
 428LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type,
 429                             struct bpf_object **pobj, int *prog_fd);
 430
 431struct xdp_link_info {
 432        __u32 prog_id;
 433        __u32 drv_prog_id;
 434        __u32 hw_prog_id;
 435        __u32 skb_prog_id;
 436        __u8 attach_mode;
 437};
 438
 439LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
 440LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
 441LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
 442                                     size_t info_size, __u32 flags);
 443
 444struct perf_buffer;
 445
 446typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
 447                                      void *data, __u32 size);
 448typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
 449
 450/* common use perf buffer options */
 451struct perf_buffer_opts {
 452        /* if specified, sample_cb is called for each sample */
 453        perf_buffer_sample_fn sample_cb;
 454        /* if specified, lost_cb is called for each batch of lost samples */
 455        perf_buffer_lost_fn lost_cb;
 456        /* ctx is provided to sample_cb and lost_cb */
 457        void *ctx;
 458};
 459
 460LIBBPF_API struct perf_buffer *
 461perf_buffer__new(int map_fd, size_t page_cnt,
 462                 const struct perf_buffer_opts *opts);
 463
 464enum bpf_perf_event_ret {
 465        LIBBPF_PERF_EVENT_DONE  = 0,
 466        LIBBPF_PERF_EVENT_ERROR = -1,
 467        LIBBPF_PERF_EVENT_CONT  = -2,
 468};
 469
 470struct perf_event_header;
 471
 472typedef enum bpf_perf_event_ret
 473(*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
 474
 475/* raw perf buffer options, giving most power and control */
 476struct perf_buffer_raw_opts {
 477        /* perf event attrs passed directly into perf_event_open() */
 478        struct perf_event_attr *attr;
 479        /* raw event callback */
 480        perf_buffer_event_fn event_cb;
 481        /* ctx is provided to event_cb */
 482        void *ctx;
 483        /* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
 484         * max_entries of given PERF_EVENT_ARRAY map)
 485         */
 486        int cpu_cnt;
 487        /* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
 488        int *cpus;
 489        /* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
 490        int *map_keys;
 491};
 492
 493LIBBPF_API struct perf_buffer *
 494perf_buffer__new_raw(int map_fd, size_t page_cnt,
 495                     const struct perf_buffer_raw_opts *opts);
 496
 497LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
 498LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
 499
 500typedef enum bpf_perf_event_ret
 501        (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
 502                                  void *private_data);
 503LIBBPF_API enum bpf_perf_event_ret
 504bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
 505                           void **copy_mem, size_t *copy_size,
 506                           bpf_perf_event_print_t fn, void *private_data);
 507
 508struct nlattr;
 509typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
 510int libbpf_netlink_open(unsigned int *nl_pid);
 511int libbpf_nl_get_link(int sock, unsigned int nl_pid,
 512                       libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie);
 513int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
 514                        libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie);
 515int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
 516                        libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie);
 517int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
 518                         libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie);
 519
 520struct bpf_prog_linfo;
 521struct bpf_prog_info;
 522
 523LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
 524LIBBPF_API struct bpf_prog_linfo *
 525bpf_prog_linfo__new(const struct bpf_prog_info *info);
 526LIBBPF_API const struct bpf_line_info *
 527bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
 528                                __u64 addr, __u32 func_idx, __u32 nr_skip);
 529LIBBPF_API const struct bpf_line_info *
 530bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
 531                      __u32 insn_off, __u32 nr_skip);
 532
 533/*
 534 * Probe for supported system features
 535 *
 536 * Note that running many of these probes in a short amount of time can cause
 537 * the kernel to reach the maximal size of lockable memory allowed for the
 538 * user, causing subsequent probes to fail. In this case, the caller may want
 539 * to adjust that limit with setrlimit().
 540 */
 541LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type,
 542                                    __u32 ifindex);
 543LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
 544LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id,
 545                                 enum bpf_prog_type prog_type, __u32 ifindex);
 546
 547/*
 548 * Get bpf_prog_info in continuous memory
 549 *
 550 * struct bpf_prog_info has multiple arrays. The user has option to choose
 551 * arrays to fetch from kernel. The following APIs provide an uniform way to
 552 * fetch these data. All arrays in bpf_prog_info are stored in a single
 553 * continuous memory region. This makes it easy to store the info in a
 554 * file.
 555 *
 556 * Before writing bpf_prog_info_linear to files, it is necessary to
 557 * translate pointers in bpf_prog_info to offsets. Helper functions
 558 * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr()
 559 * are introduced to switch between pointers and offsets.
 560 *
 561 * Examples:
 562 *   # To fetch map_ids and prog_tags:
 563 *   __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) |
 564 *           (1UL << BPF_PROG_INFO_PROG_TAGS);
 565 *   struct bpf_prog_info_linear *info_linear =
 566 *           bpf_program__get_prog_info_linear(fd, arrays);
 567 *
 568 *   # To save data in file
 569 *   bpf_program__bpil_addr_to_offs(info_linear);
 570 *   write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
 571 *
 572 *   # To read data from file
 573 *   read(f, info_linear, <proper_size>);
 574 *   bpf_program__bpil_offs_to_addr(info_linear);
 575 */
 576enum bpf_prog_info_array {
 577        BPF_PROG_INFO_FIRST_ARRAY = 0,
 578        BPF_PROG_INFO_JITED_INSNS = 0,
 579        BPF_PROG_INFO_XLATED_INSNS,
 580        BPF_PROG_INFO_MAP_IDS,
 581        BPF_PROG_INFO_JITED_KSYMS,
 582        BPF_PROG_INFO_JITED_FUNC_LENS,
 583        BPF_PROG_INFO_FUNC_INFO,
 584        BPF_PROG_INFO_LINE_INFO,
 585        BPF_PROG_INFO_JITED_LINE_INFO,
 586        BPF_PROG_INFO_PROG_TAGS,
 587        BPF_PROG_INFO_LAST_ARRAY,
 588};
 589
 590struct bpf_prog_info_linear {
 591        /* size of struct bpf_prog_info, when the tool is compiled */
 592        __u32                   info_len;
 593        /* total bytes allocated for data, round up to 8 bytes */
 594        __u32                   data_len;
 595        /* which arrays are included in data */
 596        __u64                   arrays;
 597        struct bpf_prog_info    info;
 598        __u8                    data[];
 599};
 600
 601LIBBPF_API struct bpf_prog_info_linear *
 602bpf_program__get_prog_info_linear(int fd, __u64 arrays);
 603
 604LIBBPF_API void
 605bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
 606
 607LIBBPF_API void
 608bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
 609
 610/*
 611 * A helper function to get the number of possible CPUs before looking up
 612 * per-CPU maps. Negative errno is returned on failure.
 613 *
 614 * Example usage:
 615 *
 616 *     int ncpus = libbpf_num_possible_cpus();
 617 *     if (ncpus < 0) {
 618 *          // error handling
 619 *     }
 620 *     long values[ncpus];
 621 *     bpf_map_lookup_elem(per_cpu_map_fd, key, values);
 622 *
 623 */
 624LIBBPF_API int libbpf_num_possible_cpus(void);
 625
 626#ifdef __cplusplus
 627} /* extern "C" */
 628#endif
 629
 630#endif /* __LIBBPF_LIBBPF_H */
 631