David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 10 | #ifndef __LIBBPF_LIBBPF_H |
| 11 | #define __LIBBPF_LIBBPF_H |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 13 | #include <stdarg.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | #ifndef LIBBPF_API |
| 25 | #define LIBBPF_API __attribute__((visibility("default"))) |
| 26 | #endif |
| 27 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 28 | enum 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 */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 45 | LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 46 | __LIBBPF_ERRNO__END, |
| 47 | }; |
| 48 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 49 | LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 50 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 51 | enum libbpf_print_level { |
| 52 | LIBBPF_WARN, |
| 53 | LIBBPF_INFO, |
| 54 | LIBBPF_DEBUG, |
| 55 | }; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 56 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 57 | typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level, |
| 58 | const char *, va_list ap); |
| 59 | |
| 60 | LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 61 | |
| 62 | /* Hide internal to user */ |
| 63 | struct bpf_object; |
| 64 | |
| 65 | struct bpf_object_open_attr { |
| 66 | const char *file; |
| 67 | enum bpf_prog_type prog_type; |
| 68 | }; |
| 69 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 70 | LIBBPF_API struct bpf_object *bpf_object__open(const char *path); |
| 71 | LIBBPF_API struct bpf_object * |
| 72 | bpf_object__open_xattr(struct bpf_object_open_attr *attr); |
| 73 | struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr, |
| 74 | int flags); |
| 75 | LIBBPF_API struct bpf_object *bpf_object__open_buffer(void *obj_buf, |
| 76 | size_t obj_buf_sz, |
| 77 | const char *name); |
| 78 | int bpf_object__section_size(const struct bpf_object *obj, const char *name, |
| 79 | __u32 *size); |
| 80 | int bpf_object__variable_offset(const struct bpf_object *obj, const char *name, |
| 81 | __u32 *off); |
| 82 | LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path); |
| 83 | LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj, |
| 84 | const char *path); |
| 85 | LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj, |
| 86 | const char *path); |
| 87 | LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj, |
| 88 | const char *path); |
| 89 | LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path); |
| 90 | LIBBPF_API void bpf_object__close(struct bpf_object *object); |
| 91 | |
| 92 | struct bpf_object_load_attr { |
| 93 | struct bpf_object *obj; |
| 94 | int log_level; |
| 95 | const char *target_btf_path; |
| 96 | }; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 97 | |
| 98 | /* Load/unload object into/from kernel */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 99 | LIBBPF_API int bpf_object__load(struct bpf_object *obj); |
| 100 | LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr); |
| 101 | LIBBPF_API int bpf_object__unload(struct bpf_object *obj); |
| 102 | LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj); |
| 103 | LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 104 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 105 | struct btf; |
| 106 | LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj); |
| 107 | LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 108 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 109 | LIBBPF_API struct bpf_program * |
| 110 | bpf_object__find_program_by_title(const struct bpf_object *obj, |
| 111 | const char *title); |
| 112 | |
| 113 | LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 114 | #define bpf_object__for_each_safe(pos, tmp) \ |
| 115 | for ((pos) = bpf_object__next(NULL), \ |
| 116 | (tmp) = bpf_object__next(pos); \ |
| 117 | (pos) != NULL; \ |
| 118 | (pos) = (tmp), (tmp) = bpf_object__next(tmp)) |
| 119 | |
| 120 | typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 121 | LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv, |
| 122 | bpf_object_clear_priv_t clear_priv); |
| 123 | LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 124 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 125 | LIBBPF_API int |
| 126 | libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, |
| 127 | enum bpf_attach_type *expected_attach_type); |
| 128 | LIBBPF_API int libbpf_attach_type_by_name(const char *name, |
| 129 | enum bpf_attach_type *attach_type); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 130 | |
| 131 | /* Accessors of bpf_program */ |
| 132 | struct bpf_program; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 133 | LIBBPF_API struct bpf_program *bpf_program__next(struct bpf_program *prog, |
| 134 | const struct bpf_object *obj); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 135 | |
| 136 | #define bpf_object__for_each_program(pos, obj) \ |
| 137 | for ((pos) = bpf_program__next(NULL, (obj)); \ |
| 138 | (pos) != NULL; \ |
| 139 | (pos) = bpf_program__next((pos), (obj))) |
| 140 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 141 | LIBBPF_API struct bpf_program *bpf_program__prev(struct bpf_program *prog, |
| 142 | const struct bpf_object *obj); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 143 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 144 | typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 145 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 146 | LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv, |
| 147 | bpf_program_clear_priv_t clear_priv); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 148 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 149 | LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog); |
| 150 | LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog, |
| 151 | __u32 ifindex); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 152 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 153 | LIBBPF_API const char *bpf_program__title(const struct bpf_program *prog, |
| 154 | bool needs_copy); |
| 155 | |
| 156 | LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license, |
| 157 | __u32 kern_version); |
| 158 | LIBBPF_API int bpf_program__fd(const struct bpf_program *prog); |
| 159 | LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog, |
| 160 | const char *path, |
| 161 | int instance); |
| 162 | LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog, |
| 163 | const char *path, |
| 164 | int instance); |
| 165 | LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path); |
| 166 | LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path); |
| 167 | LIBBPF_API void bpf_program__unload(struct bpf_program *prog); |
| 168 | |
| 169 | struct bpf_link; |
| 170 | |
| 171 | LIBBPF_API int bpf_link__destroy(struct bpf_link *link); |
| 172 | |
| 173 | LIBBPF_API struct bpf_link * |
| 174 | bpf_program__attach_perf_event(struct bpf_program *prog, int pfd); |
| 175 | LIBBPF_API struct bpf_link * |
| 176 | bpf_program__attach_kprobe(struct bpf_program *prog, bool retprobe, |
| 177 | const char *func_name); |
| 178 | LIBBPF_API struct bpf_link * |
| 179 | bpf_program__attach_uprobe(struct bpf_program *prog, bool retprobe, |
| 180 | pid_t pid, const char *binary_path, |
| 181 | size_t func_offset); |
| 182 | LIBBPF_API struct bpf_link * |
| 183 | bpf_program__attach_tracepoint(struct bpf_program *prog, |
| 184 | const char *tp_category, |
| 185 | const char *tp_name); |
| 186 | LIBBPF_API struct bpf_link * |
| 187 | bpf_program__attach_raw_tracepoint(struct bpf_program *prog, |
| 188 | const char *tp_name); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 189 | |
| 190 | struct bpf_insn; |
| 191 | |
| 192 | /* |
| 193 | * Libbpf allows callers to adjust BPF programs before being loaded |
| 194 | * into kernel. One program in an object file can be transformed into |
| 195 | * multiple variants to be attached to different hooks. |
| 196 | * |
| 197 | * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd |
| 198 | * form an API for this purpose. |
| 199 | * |
| 200 | * - bpf_program_prep_t: |
| 201 | * Defines a 'preprocessor', which is a caller defined function |
| 202 | * passed to libbpf through bpf_program__set_prep(), and will be |
| 203 | * called before program is loaded. The processor should adjust |
| 204 | * the program one time for each instance according to the instance id |
| 205 | * passed to it. |
| 206 | * |
| 207 | * - bpf_program__set_prep: |
| 208 | * Attaches a preprocessor to a BPF program. The number of instances |
| 209 | * that should be created is also passed through this function. |
| 210 | * |
| 211 | * - bpf_program__nth_fd: |
| 212 | * After the program is loaded, get resulting FD of a given instance |
| 213 | * of the BPF program. |
| 214 | * |
| 215 | * If bpf_program__set_prep() is not used, the program would be loaded |
| 216 | * without adjustment during bpf_object__load(). The program has only |
| 217 | * one instance. In this case bpf_program__fd(prog) is equal to |
| 218 | * bpf_program__nth_fd(prog, 0). |
| 219 | */ |
| 220 | |
| 221 | struct bpf_prog_prep_result { |
| 222 | /* |
| 223 | * If not NULL, load new instruction array. |
| 224 | * If set to NULL, don't load this instance. |
| 225 | */ |
| 226 | struct bpf_insn *new_insn_ptr; |
| 227 | int new_insn_cnt; |
| 228 | |
| 229 | /* If not NULL, result FD is written to it. */ |
| 230 | int *pfd; |
| 231 | }; |
| 232 | |
| 233 | /* |
| 234 | * Parameters of bpf_program_prep_t: |
| 235 | * - prog: The bpf_program being loaded. |
| 236 | * - n: Index of instance being generated. |
| 237 | * - insns: BPF instructions array. |
| 238 | * - insns_cnt:Number of instructions in insns. |
| 239 | * - res: Output parameter, result of transformation. |
| 240 | * |
| 241 | * Return value: |
| 242 | * - Zero: pre-processing success. |
| 243 | * - Non-zero: pre-processing error, stop loading. |
| 244 | */ |
| 245 | typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n, |
| 246 | struct bpf_insn *insns, int insns_cnt, |
| 247 | struct bpf_prog_prep_result *res); |
| 248 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 249 | LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance, |
| 250 | bpf_program_prep_t prep); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 251 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 252 | LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 253 | |
| 254 | /* |
| 255 | * Adjust type of BPF program. Default is kprobe. |
| 256 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 257 | LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog); |
| 258 | LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog); |
| 259 | LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog); |
| 260 | LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog); |
| 261 | LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog); |
| 262 | LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog); |
| 263 | LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog); |
| 264 | LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog); |
| 265 | LIBBPF_API void bpf_program__set_type(struct bpf_program *prog, |
| 266 | enum bpf_prog_type type); |
| 267 | LIBBPF_API void |
| 268 | bpf_program__set_expected_attach_type(struct bpf_program *prog, |
| 269 | enum bpf_attach_type type); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 270 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 271 | LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog); |
| 272 | LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog); |
| 273 | LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog); |
| 274 | LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog); |
| 275 | LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog); |
| 276 | LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog); |
| 277 | LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog); |
| 278 | LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 279 | |
| 280 | /* |
| 281 | * No need for __attribute__((packed)), all members of 'bpf_map_def' |
| 282 | * are all aligned. In addition, using __attribute__((packed)) |
| 283 | * would trigger a -Wpacked warning message, and lead to an error |
| 284 | * if -Werror is set. |
| 285 | */ |
| 286 | struct bpf_map_def { |
| 287 | unsigned int type; |
| 288 | unsigned int key_size; |
| 289 | unsigned int value_size; |
| 290 | unsigned int max_entries; |
| 291 | unsigned int map_flags; |
| 292 | }; |
| 293 | |
| 294 | /* |
| 295 | * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel, |
| 296 | * so no need to worry about a name clash. |
| 297 | */ |
| 298 | struct bpf_map; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 299 | LIBBPF_API struct bpf_map * |
| 300 | bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name); |
| 301 | |
| 302 | LIBBPF_API int |
| 303 | bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 304 | |
| 305 | /* |
| 306 | * Get bpf_map through the offset of corresponding struct bpf_map_def |
| 307 | * in the BPF object file. |
| 308 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 309 | LIBBPF_API struct bpf_map * |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 310 | bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset); |
| 311 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 312 | LIBBPF_API struct bpf_map * |
| 313 | bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj); |
| 314 | #define bpf_object__for_each_map(pos, obj) \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 315 | for ((pos) = bpf_map__next(NULL, (obj)); \ |
| 316 | (pos) != NULL; \ |
| 317 | (pos) = bpf_map__next((pos), (obj))) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 318 | #define bpf_map__for_each bpf_object__for_each_map |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 319 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 320 | LIBBPF_API struct bpf_map * |
| 321 | bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj); |
| 322 | |
| 323 | LIBBPF_API int bpf_map__fd(const struct bpf_map *map); |
| 324 | LIBBPF_API const struct bpf_map_def *bpf_map__def(const struct bpf_map *map); |
| 325 | LIBBPF_API const char *bpf_map__name(const struct bpf_map *map); |
| 326 | LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map); |
| 327 | LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 328 | |
| 329 | typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 330 | LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv, |
| 331 | bpf_map_clear_priv_t clear_priv); |
| 332 | LIBBPF_API void *bpf_map__priv(const struct bpf_map *map); |
| 333 | LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd); |
| 334 | LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries); |
| 335 | LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map); |
| 336 | LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map); |
| 337 | LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex); |
| 338 | LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path); |
| 339 | LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 340 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 341 | LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd); |
| 342 | |
| 343 | LIBBPF_API long libbpf_get_error(const void *ptr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 344 | |
| 345 | struct bpf_prog_load_attr { |
| 346 | const char *file; |
| 347 | enum bpf_prog_type prog_type; |
| 348 | enum bpf_attach_type expected_attach_type; |
| 349 | int ifindex; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 350 | int log_level; |
| 351 | int prog_flags; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 352 | }; |
| 353 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 354 | LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, |
| 355 | struct bpf_object **pobj, int *prog_fd); |
| 356 | LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type, |
| 357 | struct bpf_object **pobj, int *prog_fd); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 358 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 359 | LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags); |
| 360 | LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags); |
| 361 | |
| 362 | struct perf_buffer; |
| 363 | |
| 364 | typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu, |
| 365 | void *data, __u32 size); |
| 366 | typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt); |
| 367 | |
| 368 | /* common use perf buffer options */ |
| 369 | struct perf_buffer_opts { |
| 370 | /* if specified, sample_cb is called for each sample */ |
| 371 | perf_buffer_sample_fn sample_cb; |
| 372 | /* if specified, lost_cb is called for each batch of lost samples */ |
| 373 | perf_buffer_lost_fn lost_cb; |
| 374 | /* ctx is provided to sample_cb and lost_cb */ |
| 375 | void *ctx; |
| 376 | }; |
| 377 | |
| 378 | LIBBPF_API struct perf_buffer * |
| 379 | perf_buffer__new(int map_fd, size_t page_cnt, |
| 380 | const struct perf_buffer_opts *opts); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 381 | |
| 382 | enum bpf_perf_event_ret { |
| 383 | LIBBPF_PERF_EVENT_DONE = 0, |
| 384 | LIBBPF_PERF_EVENT_ERROR = -1, |
| 385 | LIBBPF_PERF_EVENT_CONT = -2, |
| 386 | }; |
| 387 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 388 | struct perf_event_header; |
| 389 | |
| 390 | typedef enum bpf_perf_event_ret |
| 391 | (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event); |
| 392 | |
| 393 | /* raw perf buffer options, giving most power and control */ |
| 394 | struct perf_buffer_raw_opts { |
| 395 | /* perf event attrs passed directly into perf_event_open() */ |
| 396 | struct perf_event_attr *attr; |
| 397 | /* raw event callback */ |
| 398 | perf_buffer_event_fn event_cb; |
| 399 | /* ctx is provided to event_cb */ |
| 400 | void *ctx; |
| 401 | /* if cpu_cnt == 0, open all on all possible CPUs (up to the number of |
| 402 | * max_entries of given PERF_EVENT_ARRAY map) |
| 403 | */ |
| 404 | int cpu_cnt; |
| 405 | /* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */ |
| 406 | int *cpus; |
| 407 | /* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */ |
| 408 | int *map_keys; |
| 409 | }; |
| 410 | |
| 411 | LIBBPF_API struct perf_buffer * |
| 412 | perf_buffer__new_raw(int map_fd, size_t page_cnt, |
| 413 | const struct perf_buffer_raw_opts *opts); |
| 414 | |
| 415 | LIBBPF_API void perf_buffer__free(struct perf_buffer *pb); |
| 416 | LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms); |
| 417 | |
| 418 | typedef enum bpf_perf_event_ret |
| 419 | (*bpf_perf_event_print_t)(struct perf_event_header *hdr, |
| 420 | void *private_data); |
| 421 | LIBBPF_API enum bpf_perf_event_ret |
| 422 | bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size, |
| 423 | void **copy_mem, size_t *copy_size, |
| 424 | bpf_perf_event_print_t fn, void *private_data); |
| 425 | |
| 426 | struct nlattr; |
| 427 | typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb); |
| 428 | int libbpf_netlink_open(unsigned int *nl_pid); |
| 429 | int libbpf_nl_get_link(int sock, unsigned int nl_pid, |
| 430 | libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie); |
| 431 | int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex, |
| 432 | libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie); |
| 433 | int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex, |
| 434 | libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie); |
| 435 | int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle, |
| 436 | libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie); |
| 437 | |
| 438 | struct bpf_prog_linfo; |
| 439 | struct bpf_prog_info; |
| 440 | |
| 441 | LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo); |
| 442 | LIBBPF_API struct bpf_prog_linfo * |
| 443 | bpf_prog_linfo__new(const struct bpf_prog_info *info); |
| 444 | LIBBPF_API const struct bpf_line_info * |
| 445 | bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo, |
| 446 | __u64 addr, __u32 func_idx, __u32 nr_skip); |
| 447 | LIBBPF_API const struct bpf_line_info * |
| 448 | bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo, |
| 449 | __u32 insn_off, __u32 nr_skip); |
| 450 | |
| 451 | /* |
| 452 | * Probe for supported system features |
| 453 | * |
| 454 | * Note that running many of these probes in a short amount of time can cause |
| 455 | * the kernel to reach the maximal size of lockable memory allowed for the |
| 456 | * user, causing subsequent probes to fail. In this case, the caller may want |
| 457 | * to adjust that limit with setrlimit(). |
| 458 | */ |
| 459 | LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type, |
| 460 | __u32 ifindex); |
| 461 | LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex); |
| 462 | LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id, |
| 463 | enum bpf_prog_type prog_type, __u32 ifindex); |
| 464 | |
| 465 | /* |
| 466 | * Get bpf_prog_info in continuous memory |
| 467 | * |
| 468 | * struct bpf_prog_info has multiple arrays. The user has option to choose |
| 469 | * arrays to fetch from kernel. The following APIs provide an uniform way to |
| 470 | * fetch these data. All arrays in bpf_prog_info are stored in a single |
| 471 | * continuous memory region. This makes it easy to store the info in a |
| 472 | * file. |
| 473 | * |
| 474 | * Before writing bpf_prog_info_linear to files, it is necessary to |
| 475 | * translate pointers in bpf_prog_info to offsets. Helper functions |
| 476 | * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr() |
| 477 | * are introduced to switch between pointers and offsets. |
| 478 | * |
| 479 | * Examples: |
| 480 | * # To fetch map_ids and prog_tags: |
| 481 | * __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) | |
| 482 | * (1UL << BPF_PROG_INFO_PROG_TAGS); |
| 483 | * struct bpf_prog_info_linear *info_linear = |
| 484 | * bpf_program__get_prog_info_linear(fd, arrays); |
| 485 | * |
| 486 | * # To save data in file |
| 487 | * bpf_program__bpil_addr_to_offs(info_linear); |
| 488 | * write(f, info_linear, sizeof(*info_linear) + info_linear->data_len); |
| 489 | * |
| 490 | * # To read data from file |
| 491 | * read(f, info_linear, <proper_size>); |
| 492 | * bpf_program__bpil_offs_to_addr(info_linear); |
| 493 | */ |
| 494 | enum bpf_prog_info_array { |
| 495 | BPF_PROG_INFO_FIRST_ARRAY = 0, |
| 496 | BPF_PROG_INFO_JITED_INSNS = 0, |
| 497 | BPF_PROG_INFO_XLATED_INSNS, |
| 498 | BPF_PROG_INFO_MAP_IDS, |
| 499 | BPF_PROG_INFO_JITED_KSYMS, |
| 500 | BPF_PROG_INFO_JITED_FUNC_LENS, |
| 501 | BPF_PROG_INFO_FUNC_INFO, |
| 502 | BPF_PROG_INFO_LINE_INFO, |
| 503 | BPF_PROG_INFO_JITED_LINE_INFO, |
| 504 | BPF_PROG_INFO_PROG_TAGS, |
| 505 | BPF_PROG_INFO_LAST_ARRAY, |
| 506 | }; |
| 507 | |
| 508 | struct bpf_prog_info_linear { |
| 509 | /* size of struct bpf_prog_info, when the tool is compiled */ |
| 510 | __u32 info_len; |
| 511 | /* total bytes allocated for data, round up to 8 bytes */ |
| 512 | __u32 data_len; |
| 513 | /* which arrays are included in data */ |
| 514 | __u64 arrays; |
| 515 | struct bpf_prog_info info; |
| 516 | __u8 data[]; |
| 517 | }; |
| 518 | |
| 519 | LIBBPF_API struct bpf_prog_info_linear * |
| 520 | bpf_program__get_prog_info_linear(int fd, __u64 arrays); |
| 521 | |
| 522 | LIBBPF_API void |
| 523 | bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear); |
| 524 | |
| 525 | LIBBPF_API void |
| 526 | bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear); |
| 527 | |
| 528 | /* |
| 529 | * A helper function to get the number of possible CPUs before looking up |
| 530 | * per-CPU maps. Negative errno is returned on failure. |
| 531 | * |
| 532 | * Example usage: |
| 533 | * |
| 534 | * int ncpus = libbpf_num_possible_cpus(); |
| 535 | * if (ncpus < 0) { |
| 536 | * // error handling |
| 537 | * } |
| 538 | * long values[ncpus]; |
| 539 | * bpf_map_lookup_elem(per_cpu_map_fd, key, values); |
| 540 | * |
| 541 | */ |
| 542 | LIBBPF_API int libbpf_num_possible_cpus(void); |
| 543 | |
| 544 | #ifdef __cplusplus |
| 545 | } /* extern "C" */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 546 | #endif |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 547 | |
| 548 | #endif /* __LIBBPF_LIBBPF_H */ |