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