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