^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Common eBPF ELF object loading operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) 2015 Huawei Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #ifndef __LIBBPF_LIBBPF_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define __LIBBPF_LIBBPF_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <sys/types.h> // for size_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "libbpf_common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #ifdef __cplusplus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) extern "C" {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) enum libbpf_errno {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) __LIBBPF_ERRNO__START = 4000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /* Something wrong in libelf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) LIBBPF_ERRNO__RELOC, /* Relocation failed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) LIBBPF_ERRNO__PROG2BIG, /* Program too big */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) __LIBBPF_ERRNO__END,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) enum libbpf_print_level {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) LIBBPF_WARN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) LIBBPF_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) LIBBPF_DEBUG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) const char *, va_list ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /* Hide internal to user */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct bpf_object;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct bpf_object_open_attr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) const char *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) enum bpf_prog_type prog_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct bpf_object_open_opts {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* size of this struct, for forward/backward compatiblity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) size_t sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /* object name override, if provided:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * - for object open from file, this will override setting object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * name from file path's base name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * - for object open from memory buffer, this will specify an object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * name and will override default "<addr>-<buf-size>" name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) const char *object_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* parse map definitions non-strictly, allowing extra attributes/data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) bool relaxed_maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /* DEPRECATED: handle CO-RE relocations non-strictly, allowing failures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * Value is ignored. Relocations always are processed non-strictly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * Non-relocatable instructions are replaced with invalid ones to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * prevent accidental errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) bool relaxed_core_relocs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /* maps that set the 'pinning' attribute in their definition will have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * their pin_path attribute set to a file in this directory, and be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) const char *pin_root_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) __u32 attach_prog_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* Additional kernel config content that augments and overrides
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * system Kconfig for CONFIG_xxx externs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) const char *kconfig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #define bpf_object_open_opts__last_field kconfig
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) LIBBPF_API struct bpf_object *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) LIBBPF_API struct bpf_object *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) const struct bpf_object_open_opts *opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* deprecated bpf_object__open variants */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) LIBBPF_API struct bpf_object *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) const char *name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) LIBBPF_API struct bpf_object *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) bpf_object__open_xattr(struct bpf_object_open_attr *attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) enum libbpf_pin_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) LIBBPF_PIN_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) LIBBPF_PIN_BY_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* pin_maps and unpin_maps can both be called with a NULL path, in which case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * they will use the pin_path attribute of each map (and ignore all maps that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * don't have a pin_path set).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) const char *path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) const char *path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) const char *path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) LIBBPF_API void bpf_object__close(struct bpf_object *object);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct bpf_object_load_attr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct bpf_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) int log_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) const char *target_btf_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* Load/unload object into/from kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) LIBBPF_API int bpf_object__load(struct bpf_object *obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct btf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) LIBBPF_API struct bpf_program *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) bpf_object__find_program_by_title(const struct bpf_object *obj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) const char *title);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) LIBBPF_API struct bpf_program *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) bpf_object__find_program_by_name(const struct bpf_object *obj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) const char *name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) #define bpf_object__for_each_safe(pos, tmp) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) for ((pos) = bpf_object__next(NULL), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) (tmp) = bpf_object__next(pos); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) (pos) != NULL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) (pos) = (tmp), (tmp) = bpf_object__next(tmp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) bpf_object_clear_priv_t clear_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) LIBBPF_API int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) enum bpf_attach_type *expected_attach_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) LIBBPF_API int libbpf_attach_type_by_name(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) enum bpf_attach_type *attach_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) enum bpf_attach_type attach_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /* Accessors of bpf_program */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct bpf_program;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) LIBBPF_API struct bpf_program *bpf_program__next(struct bpf_program *prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) const struct bpf_object *obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) #define bpf_object__for_each_program(pos, obj) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) for ((pos) = bpf_program__next(NULL, (obj)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) (pos) != NULL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) (pos) = bpf_program__next((pos), (obj)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) LIBBPF_API struct bpf_program *bpf_program__prev(struct bpf_program *prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) const struct bpf_object *obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) bpf_program_clear_priv_t clear_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) __u32 ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) LIBBPF_API LIBBPF_DEPRECATED("BPF program title is confusing term; please use bpf_program__section_name() instead")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /* returns program size in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) LIBBPF_API size_t bpf_program__size(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) __u32 kern_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) const char *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) int instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) const char *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) int instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct bpf_link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) LIBBPF_API int bpf_link__detach(struct bpf_link *link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) LIBBPF_API struct bpf_link *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) bpf_program__attach(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) LIBBPF_API struct bpf_link *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) bpf_program__attach_perf_event(struct bpf_program *prog, int pfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) LIBBPF_API struct bpf_link *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) bpf_program__attach_kprobe(struct bpf_program *prog, bool retprobe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) const char *func_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) LIBBPF_API struct bpf_link *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) bpf_program__attach_uprobe(struct bpf_program *prog, bool retprobe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) pid_t pid, const char *binary_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) size_t func_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) LIBBPF_API struct bpf_link *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) bpf_program__attach_tracepoint(struct bpf_program *prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) const char *tp_category,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) const char *tp_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) LIBBPF_API struct bpf_link *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) const char *tp_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) LIBBPF_API struct bpf_link *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) bpf_program__attach_trace(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) LIBBPF_API struct bpf_link *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) bpf_program__attach_lsm(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) LIBBPF_API struct bpf_link *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) LIBBPF_API struct bpf_link *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) bpf_program__attach_netns(struct bpf_program *prog, int netns_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) LIBBPF_API struct bpf_link *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) bpf_program__attach_xdp(struct bpf_program *prog, int ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) LIBBPF_API struct bpf_link *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) bpf_program__attach_freplace(struct bpf_program *prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) int target_fd, const char *attach_func_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct bpf_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct bpf_iter_attach_opts {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) size_t sz; /* size of this struct for forward/backward compatibility */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) union bpf_iter_link_info *link_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) __u32 link_info_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) #define bpf_iter_attach_opts__last_field link_info_len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) LIBBPF_API struct bpf_link *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) bpf_program__attach_iter(struct bpf_program *prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) const struct bpf_iter_attach_opts *opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct bpf_insn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * Libbpf allows callers to adjust BPF programs before being loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * into kernel. One program in an object file can be transformed into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * multiple variants to be attached to different hooks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * form an API for this purpose.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * - bpf_program_prep_t:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * Defines a 'preprocessor', which is a caller defined function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * passed to libbpf through bpf_program__set_prep(), and will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * called before program is loaded. The processor should adjust
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * the program one time for each instance according to the instance id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * passed to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * - bpf_program__set_prep:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * Attaches a preprocessor to a BPF program. The number of instances
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * that should be created is also passed through this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * - bpf_program__nth_fd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * After the program is loaded, get resulting FD of a given instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * of the BPF program.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * If bpf_program__set_prep() is not used, the program would be loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * without adjustment during bpf_object__load(). The program has only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * one instance. In this case bpf_program__fd(prog) is equal to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * bpf_program__nth_fd(prog, 0).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct bpf_prog_prep_result {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * If not NULL, load new instruction array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * If set to NULL, don't load this instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) struct bpf_insn *new_insn_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) int new_insn_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) /* If not NULL, result FD is written to it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) int *pfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * Parameters of bpf_program_prep_t:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * - prog: The bpf_program being loaded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * - n: Index of instance being generated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * - insns: BPF instructions array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * - insns_cnt:Number of instructions in insns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * - res: Output parameter, result of transformation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * Return value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * - Zero: pre-processing success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * - Non-zero: pre-processing error, stop loading.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) struct bpf_insn *insns, int insns_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) struct bpf_prog_prep_result *res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) bpf_program_prep_t prep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * Adjust type of BPF program. Default is kprobe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) LIBBPF_API int bpf_program__set_struct_ops(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) LIBBPF_API int bpf_program__set_extension(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) LIBBPF_API int bpf_program__set_sk_lookup(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) LIBBPF_API enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) enum bpf_prog_type type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) LIBBPF_API enum bpf_attach_type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) bpf_program__get_expected_attach_type(struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) LIBBPF_API void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) bpf_program__set_expected_attach_type(struct bpf_program *prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) enum bpf_attach_type type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) LIBBPF_API int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) const char *attach_func_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) LIBBPF_API bool bpf_program__is_struct_ops(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) LIBBPF_API bool bpf_program__is_extension(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) LIBBPF_API bool bpf_program__is_sk_lookup(const struct bpf_program *prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * No need for __attribute__((packed)), all members of 'bpf_map_def'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * are all aligned. In addition, using __attribute__((packed))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * would trigger a -Wpacked warning message, and lead to an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * if -Werror is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) struct bpf_map_def {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) unsigned int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) unsigned int key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) unsigned int value_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) unsigned int max_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) unsigned int map_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * so no need to worry about a name clash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) LIBBPF_API struct bpf_map *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) LIBBPF_API int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * Get bpf_map through the offset of corresponding struct bpf_map_def
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * in the BPF object file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) LIBBPF_API struct bpf_map *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) LIBBPF_API struct bpf_map *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) #define bpf_object__for_each_map(pos, obj) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) for ((pos) = bpf_map__next(NULL, (obj)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) (pos) != NULL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) (pos) = bpf_map__next((pos), (obj)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) #define bpf_map__for_each bpf_object__for_each_map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) LIBBPF_API struct bpf_map *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) /* get/set map FD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) /* get map definition */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) LIBBPF_API const struct bpf_map_def *bpf_map__def(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) /* get map name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) /* get/set map type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) /* get/set map size (max_entries) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) /* get/set map flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) /* get/set map NUMA node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) /* get/set map key size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) /* get/set map value size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) /* get map key/value BTF type IDs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) /* get/set map if_index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) bpf_map_clear_priv_t clear_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) const void *data, size_t size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) LIBBPF_API const char *bpf_map__get_pin_path(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) LIBBPF_API long libbpf_get_error(const void *ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) struct bpf_prog_load_attr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) const char *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) enum bpf_prog_type prog_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) enum bpf_attach_type expected_attach_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) int ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) int log_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) int prog_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) struct bpf_object **pobj, int *prog_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct bpf_object **pobj, int *prog_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) struct xdp_link_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) __u32 prog_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) __u32 drv_prog_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) __u32 hw_prog_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) __u32 skb_prog_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) __u8 attach_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) struct bpf_xdp_set_link_opts {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) size_t sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) int old_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) size_t :0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) #define bpf_xdp_set_link_opts__last_field old_fd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) LIBBPF_API int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) const struct bpf_xdp_set_link_opts *opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) size_t info_size, __u32 flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) /* Ring buffer APIs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) struct ring_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) struct ring_buffer_opts {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) size_t sz; /* size of this struct, for forward/backward compatiblity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) #define ring_buffer_opts__last_field sz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) LIBBPF_API struct ring_buffer *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) const struct ring_buffer_opts *opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) ring_buffer_sample_fn sample_cb, void *ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) /* Perf buffer APIs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) struct perf_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) void *data, __u32 size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) /* common use perf buffer options */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) struct perf_buffer_opts {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) /* if specified, sample_cb is called for each sample */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) perf_buffer_sample_fn sample_cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) /* if specified, lost_cb is called for each batch of lost samples */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) perf_buffer_lost_fn lost_cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) /* ctx is provided to sample_cb and lost_cb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) void *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) LIBBPF_API struct perf_buffer *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) perf_buffer__new(int map_fd, size_t page_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) const struct perf_buffer_opts *opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) enum bpf_perf_event_ret {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) LIBBPF_PERF_EVENT_DONE = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) LIBBPF_PERF_EVENT_ERROR = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) LIBBPF_PERF_EVENT_CONT = -2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) struct perf_event_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) typedef enum bpf_perf_event_ret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) /* raw perf buffer options, giving most power and control */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) struct perf_buffer_raw_opts {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) /* perf event attrs passed directly into perf_event_open() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) struct perf_event_attr *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) /* raw event callback */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) perf_buffer_event_fn event_cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) /* ctx is provided to event_cb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) void *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) /* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) * max_entries of given PERF_EVENT_ARRAY map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) int cpu_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) /* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) int *cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) /* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) int *map_keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) LIBBPF_API struct perf_buffer *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) perf_buffer__new_raw(int map_fd, size_t page_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) const struct perf_buffer_raw_opts *opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) typedef enum bpf_perf_event_ret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) void *private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) LIBBPF_API enum bpf_perf_event_ret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) void **copy_mem, size_t *copy_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) bpf_perf_event_print_t fn, void *private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) struct bpf_prog_linfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) struct bpf_prog_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) LIBBPF_API struct bpf_prog_linfo *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) bpf_prog_linfo__new(const struct bpf_prog_info *info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) LIBBPF_API const struct bpf_line_info *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) __u64 addr, __u32 func_idx, __u32 nr_skip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) LIBBPF_API const struct bpf_line_info *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) __u32 insn_off, __u32 nr_skip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) * Probe for supported system features
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) * Note that running many of these probes in a short amount of time can cause
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) * the kernel to reach the maximal size of lockable memory allowed for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) * user, causing subsequent probes to fail. In this case, the caller may want
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) * to adjust that limit with setrlimit().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) __u32 ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) enum bpf_prog_type prog_type, __u32 ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) LIBBPF_API bool bpf_probe_large_insn_limit(__u32 ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) * Get bpf_prog_info in continuous memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) * struct bpf_prog_info has multiple arrays. The user has option to choose
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) * arrays to fetch from kernel. The following APIs provide an uniform way to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) * fetch these data. All arrays in bpf_prog_info are stored in a single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) * continuous memory region. This makes it easy to store the info in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) * file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) * Before writing bpf_prog_info_linear to files, it is necessary to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) * translate pointers in bpf_prog_info to offsets. Helper functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) * are introduced to switch between pointers and offsets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) * Examples:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) * # To fetch map_ids and prog_tags:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) * __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) * (1UL << BPF_PROG_INFO_PROG_TAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) * struct bpf_prog_info_linear *info_linear =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) * bpf_program__get_prog_info_linear(fd, arrays);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) * # To save data in file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) * bpf_program__bpil_addr_to_offs(info_linear);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) * # To read data from file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) * read(f, info_linear, <proper_size>);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) * bpf_program__bpil_offs_to_addr(info_linear);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) enum bpf_prog_info_array {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) BPF_PROG_INFO_FIRST_ARRAY = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) BPF_PROG_INFO_JITED_INSNS = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) BPF_PROG_INFO_XLATED_INSNS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) BPF_PROG_INFO_MAP_IDS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) BPF_PROG_INFO_JITED_KSYMS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) BPF_PROG_INFO_JITED_FUNC_LENS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) BPF_PROG_INFO_FUNC_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) BPF_PROG_INFO_LINE_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) BPF_PROG_INFO_JITED_LINE_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) BPF_PROG_INFO_PROG_TAGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) BPF_PROG_INFO_LAST_ARRAY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) struct bpf_prog_info_linear {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) /* size of struct bpf_prog_info, when the tool is compiled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) __u32 info_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) /* total bytes allocated for data, round up to 8 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) __u32 data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) /* which arrays are included in data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) __u64 arrays;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) struct bpf_prog_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) __u8 data[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) LIBBPF_API struct bpf_prog_info_linear *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) bpf_program__get_prog_info_linear(int fd, __u64 arrays);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) LIBBPF_API void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) LIBBPF_API void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) * A helper function to get the number of possible CPUs before looking up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) * per-CPU maps. Negative errno is returned on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) * Example usage:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) * int ncpus = libbpf_num_possible_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) * if (ncpus < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) * // error handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) * }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) * long values[ncpus];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) * bpf_map_lookup_elem(per_cpu_map_fd, key, values);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) LIBBPF_API int libbpf_num_possible_cpus(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) struct bpf_map_skeleton {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) struct bpf_map **map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) void **mmaped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) struct bpf_prog_skeleton {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) struct bpf_program **prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) struct bpf_link **link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) struct bpf_object_skeleton {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) size_t sz; /* size of this struct, for forward/backward compatibility */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) size_t data_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) struct bpf_object **obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) int map_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) int map_skel_sz; /* sizeof(struct bpf_skeleton_map) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) struct bpf_map_skeleton *maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) int prog_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) int prog_skel_sz; /* sizeof(struct bpf_skeleton_prog) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) struct bpf_prog_skeleton *progs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) LIBBPF_API int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) bpf_object__open_skeleton(struct bpf_object_skeleton *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) const struct bpf_object_open_opts *opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) enum libbpf_tristate {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) TRI_NO = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) TRI_YES = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) TRI_MODULE = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) #ifdef __cplusplus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) } /* extern "C" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) #endif /* __LIBBPF_LIBBPF_H */