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_insn; 169 170 /* 171 * Libbpf allows callers to adjust BPF programs before being loaded 172 * into kernel. One program in an object file can be transformed into 173 * multiple variants to be attached to different hooks. 174 * 175 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd 176 * form an API for this purpose. 177 * 178 * - bpf_program_prep_t: 179 * Defines a 'preprocessor', which is a caller defined function 180 * passed to libbpf through bpf_program__set_prep(), and will be 181 * called before program is loaded. The processor should adjust 182 * the program one time for each instance according to the instance id 183 * passed to it. 184 * 185 * - bpf_program__set_prep: 186 * Attaches a preprocessor to a BPF program. The number of instances 187 * that should be created is also passed through this function. 188 * 189 * - bpf_program__nth_fd: 190 * After the program is loaded, get resulting FD of a given instance 191 * of the BPF program. 192 * 193 * If bpf_program__set_prep() is not used, the program would be loaded 194 * without adjustment during bpf_object__load(). The program has only 195 * one instance. In this case bpf_program__fd(prog) is equal to 196 * bpf_program__nth_fd(prog, 0). 197 */ 198 199 struct bpf_prog_prep_result { 200 /* 201 * If not NULL, load new instruction array. 202 * If set to NULL, don't load this instance. 203 */ 204 struct bpf_insn *new_insn_ptr; 205 int new_insn_cnt; 206 207 /* If not NULL, result FD is written to it. */ 208 int *pfd; 209 }; 210 211 /* 212 * Parameters of bpf_program_prep_t: 213 * - prog: The bpf_program being loaded. 214 * - n: Index of instance being generated. 215 * - insns: BPF instructions array. 216 * - insns_cnt:Number of instructions in insns. 217 * - res: Output parameter, result of transformation. 218 * 219 * Return value: 220 * - Zero: pre-processing success. 221 * - Non-zero: pre-processing error, stop loading. 222 */ 223 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n, 224 struct bpf_insn *insns, int insns_cnt, 225 struct bpf_prog_prep_result *res); 226 227 LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance, 228 bpf_program_prep_t prep); 229 230 LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n); 231 232 /* 233 * Adjust type of BPF program. Default is kprobe. 234 */ 235 LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog); 236 LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog); 237 LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog); 238 LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog); 239 LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog); 240 LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog); 241 LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog); 242 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog); 243 LIBBPF_API void bpf_program__set_type(struct bpf_program *prog, 244 enum bpf_prog_type type); 245 LIBBPF_API void 246 bpf_program__set_expected_attach_type(struct bpf_program *prog, 247 enum bpf_attach_type type); 248 249 LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog); 250 LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog); 251 LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog); 252 LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog); 253 LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog); 254 LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog); 255 LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog); 256 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog); 257 258 /* 259 * No need for __attribute__((packed)), all members of 'bpf_map_def' 260 * are all aligned. In addition, using __attribute__((packed)) 261 * would trigger a -Wpacked warning message, and lead to an error 262 * if -Werror is set. 263 */ 264 struct bpf_map_def { 265 unsigned int type; 266 unsigned int key_size; 267 unsigned int value_size; 268 unsigned int max_entries; 269 unsigned int map_flags; 270 }; 271 272 /* 273 * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel, 274 * so no need to worry about a name clash. 275 */ 276 struct bpf_map; 277 LIBBPF_API struct bpf_map * 278 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name); 279 280 LIBBPF_API int 281 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name); 282 283 /* 284 * Get bpf_map through the offset of corresponding struct bpf_map_def 285 * in the BPF object file. 286 */ 287 LIBBPF_API struct bpf_map * 288 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset); 289 290 LIBBPF_API struct bpf_map * 291 bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj); 292 #define bpf_object__for_each_map(pos, obj) \ 293 for ((pos) = bpf_map__next(NULL, (obj)); \ 294 (pos) != NULL; \ 295 (pos) = bpf_map__next((pos), (obj))) 296 #define bpf_map__for_each bpf_object__for_each_map 297 298 LIBBPF_API struct bpf_map * 299 bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj); 300 301 LIBBPF_API int bpf_map__fd(const struct bpf_map *map); 302 LIBBPF_API const struct bpf_map_def *bpf_map__def(const struct bpf_map *map); 303 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map); 304 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map); 305 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map); 306 307 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *); 308 LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv, 309 bpf_map_clear_priv_t clear_priv); 310 LIBBPF_API void *bpf_map__priv(const struct bpf_map *map); 311 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd); 312 LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries); 313 LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map); 314 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map); 315 LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex); 316 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path); 317 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path); 318 319 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd); 320 321 LIBBPF_API long libbpf_get_error(const void *ptr); 322 323 struct bpf_prog_load_attr { 324 const char *file; 325 enum bpf_prog_type prog_type; 326 enum bpf_attach_type expected_attach_type; 327 int ifindex; 328 int log_level; 329 int prog_flags; 330 }; 331 332 LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, 333 struct bpf_object **pobj, int *prog_fd); 334 LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type, 335 struct bpf_object **pobj, int *prog_fd); 336 337 LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags); 338 LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags); 339 340 enum bpf_perf_event_ret { 341 LIBBPF_PERF_EVENT_DONE = 0, 342 LIBBPF_PERF_EVENT_ERROR = -1, 343 LIBBPF_PERF_EVENT_CONT = -2, 344 }; 345 346 struct perf_event_header; 347 typedef enum bpf_perf_event_ret 348 (*bpf_perf_event_print_t)(struct perf_event_header *hdr, 349 void *private_data); 350 LIBBPF_API enum bpf_perf_event_ret 351 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size, 352 void **copy_mem, size_t *copy_size, 353 bpf_perf_event_print_t fn, void *private_data); 354 355 struct nlattr; 356 typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb); 357 int libbpf_netlink_open(unsigned int *nl_pid); 358 int libbpf_nl_get_link(int sock, unsigned int nl_pid, 359 libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie); 360 int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex, 361 libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie); 362 int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex, 363 libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie); 364 int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle, 365 libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie); 366 367 struct bpf_prog_linfo; 368 struct bpf_prog_info; 369 370 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo); 371 LIBBPF_API struct bpf_prog_linfo * 372 bpf_prog_linfo__new(const struct bpf_prog_info *info); 373 LIBBPF_API const struct bpf_line_info * 374 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo, 375 __u64 addr, __u32 func_idx, __u32 nr_skip); 376 LIBBPF_API const struct bpf_line_info * 377 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo, 378 __u32 insn_off, __u32 nr_skip); 379 380 /* 381 * Probe for supported system features 382 * 383 * Note that running many of these probes in a short amount of time can cause 384 * the kernel to reach the maximal size of lockable memory allowed for the 385 * user, causing subsequent probes to fail. In this case, the caller may want 386 * to adjust that limit with setrlimit(). 387 */ 388 LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type, 389 __u32 ifindex); 390 LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex); 391 LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id, 392 enum bpf_prog_type prog_type, __u32 ifindex); 393 394 /* 395 * Get bpf_prog_info in continuous memory 396 * 397 * struct bpf_prog_info has multiple arrays. The user has option to choose 398 * arrays to fetch from kernel. The following APIs provide an uniform way to 399 * fetch these data. All arrays in bpf_prog_info are stored in a single 400 * continuous memory region. This makes it easy to store the info in a 401 * file. 402 * 403 * Before writing bpf_prog_info_linear to files, it is necessary to 404 * translate pointers in bpf_prog_info to offsets. Helper functions 405 * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr() 406 * are introduced to switch between pointers and offsets. 407 * 408 * Examples: 409 * # To fetch map_ids and prog_tags: 410 * __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) | 411 * (1UL << BPF_PROG_INFO_PROG_TAGS); 412 * struct bpf_prog_info_linear *info_linear = 413 * bpf_program__get_prog_info_linear(fd, arrays); 414 * 415 * # To save data in file 416 * bpf_program__bpil_addr_to_offs(info_linear); 417 * write(f, info_linear, sizeof(*info_linear) + info_linear->data_len); 418 * 419 * # To read data from file 420 * read(f, info_linear, <proper_size>); 421 * bpf_program__bpil_offs_to_addr(info_linear); 422 */ 423 enum bpf_prog_info_array { 424 BPF_PROG_INFO_FIRST_ARRAY = 0, 425 BPF_PROG_INFO_JITED_INSNS = 0, 426 BPF_PROG_INFO_XLATED_INSNS, 427 BPF_PROG_INFO_MAP_IDS, 428 BPF_PROG_INFO_JITED_KSYMS, 429 BPF_PROG_INFO_JITED_FUNC_LENS, 430 BPF_PROG_INFO_FUNC_INFO, 431 BPF_PROG_INFO_LINE_INFO, 432 BPF_PROG_INFO_JITED_LINE_INFO, 433 BPF_PROG_INFO_PROG_TAGS, 434 BPF_PROG_INFO_LAST_ARRAY, 435 }; 436 437 struct bpf_prog_info_linear { 438 /* size of struct bpf_prog_info, when the tool is compiled */ 439 __u32 info_len; 440 /* total bytes allocated for data, round up to 8 bytes */ 441 __u32 data_len; 442 /* which arrays are included in data */ 443 __u64 arrays; 444 struct bpf_prog_info info; 445 __u8 data[]; 446 }; 447 448 LIBBPF_API struct bpf_prog_info_linear * 449 bpf_program__get_prog_info_linear(int fd, __u64 arrays); 450 451 LIBBPF_API void 452 bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear); 453 454 LIBBPF_API void 455 bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear); 456 457 /* 458 * A helper function to get the number of possible CPUs before looking up 459 * per-CPU maps. Negative errno is returned on failure. 460 * 461 * Example usage: 462 * 463 * int ncpus = libbpf_num_possible_cpus(); 464 * if (ncpus < 0) { 465 * // error handling 466 * } 467 * long values[ncpus]; 468 * bpf_map_lookup_elem(per_cpu_map_fd, key, values); 469 * 470 */ 471 LIBBPF_API int libbpf_num_possible_cpus(void); 472 473 #ifdef __cplusplus 474 } /* extern "C" */ 475 #endif 476 477 #endif /* __LIBBPF_LIBBPF_H */ 478