1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
4 * Common eBPF ELF object loading operations.
8 * Copyright (C) 2015 Huawei Inc.
10 #ifndef __LIBBPF_LIBBPF_H
11 #define __LIBBPF_LIBBPF_H
17 #include <sys/types.h> // for size_t
18 #include <linux/bpf.h>
20 #include "libbpf_common.h"
21 #include "libbpf_legacy.h"
28 __LIBBPF_ERRNO__START = 4000,
30 /* Something wrong in libelf */
31 LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
32 LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */
33 LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
34 LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */
35 LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
36 LIBBPF_ERRNO__RELOC, /* Relocation failed */
37 LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
38 LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */
39 LIBBPF_ERRNO__PROG2BIG, /* Program too big */
40 LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
41 LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
42 LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */
43 LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */
44 LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */
48 LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
50 enum libbpf_print_level {
56 typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
57 const char *, va_list ap);
59 LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
61 /* Hide internal to user */
64 struct bpf_object_open_attr {
66 enum bpf_prog_type prog_type;
69 struct bpf_object_open_opts {
70 /* size of this struct, for forward/backward compatiblity */
72 /* object name override, if provided:
73 * - for object open from file, this will override setting object
74 * name from file path's base name;
75 * - for object open from memory buffer, this will specify an object
76 * name and will override default "<addr>-<buf-size>" name;
78 const char *object_name;
79 /* parse map definitions non-strictly, allowing extra attributes/data */
81 /* DEPRECATED: handle CO-RE relocations non-strictly, allowing failures.
82 * Value is ignored. Relocations always are processed non-strictly.
83 * Non-relocatable instructions are replaced with invalid ones to
84 * prevent accidental errors.
86 bool relaxed_core_relocs;
87 /* maps that set the 'pinning' attribute in their definition will have
88 * their pin_path attribute set to a file in this directory, and be
89 * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
91 const char *pin_root_path;
93 /* Additional kernel config content that augments and overrides
94 * system Kconfig for CONFIG_xxx externs.
97 /* Path to the custom BTF to be used for BPF CO-RE relocations.
98 * This custom BTF completely replaces the use of vmlinux BTF
99 * for the purpose of CO-RE relocations.
100 * NOTE: any other BPF feature (e.g., fentry/fexit programs,
101 * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.
103 const char *btf_custom_path;
105 #define bpf_object_open_opts__last_field btf_custom_path
107 LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
108 LIBBPF_API struct bpf_object *
109 bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
110 LIBBPF_API struct bpf_object *
111 bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
112 const struct bpf_object_open_opts *opts);
114 /* deprecated bpf_object__open variants */
115 LIBBPF_API struct bpf_object *
116 bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz,
118 LIBBPF_API struct bpf_object *
119 bpf_object__open_xattr(struct bpf_object_open_attr *attr);
121 enum libbpf_pin_type {
123 /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
127 /* pin_maps and unpin_maps can both be called with a NULL path, in which case
128 * they will use the pin_path attribute of each map (and ignore all maps that
129 * don't have a pin_path set).
131 LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
132 LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
134 LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
136 LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
138 LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
139 LIBBPF_API void bpf_object__close(struct bpf_object *object);
141 struct bpf_object_load_attr {
142 struct bpf_object *obj;
144 const char *target_btf_path;
147 /* Load/unload object into/from kernel */
148 LIBBPF_API int bpf_object__load(struct bpf_object *obj);
149 LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr);
150 LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
152 LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
153 LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
154 LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);
157 LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
158 LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
160 LIBBPF_API struct bpf_program *
161 bpf_object__find_program_by_title(const struct bpf_object *obj,
163 LIBBPF_API struct bpf_program *
164 bpf_object__find_program_by_name(const struct bpf_object *obj,
167 LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
168 #define bpf_object__for_each_safe(pos, tmp) \
169 for ((pos) = bpf_object__next(NULL), \
170 (tmp) = bpf_object__next(pos); \
172 (pos) = (tmp), (tmp) = bpf_object__next(tmp))
174 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
175 LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
176 bpf_object_clear_priv_t clear_priv);
177 LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog);
180 libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
181 enum bpf_attach_type *expected_attach_type);
182 LIBBPF_API int libbpf_attach_type_by_name(const char *name,
183 enum bpf_attach_type *attach_type);
184 LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
185 enum bpf_attach_type attach_type);
187 /* Accessors of bpf_program */
189 LIBBPF_API struct bpf_program *bpf_program__next(struct bpf_program *prog,
190 const struct bpf_object *obj);
192 #define bpf_object__for_each_program(pos, obj) \
193 for ((pos) = bpf_program__next(NULL, (obj)); \
195 (pos) = bpf_program__next((pos), (obj)))
197 LIBBPF_API struct bpf_program *bpf_program__prev(struct bpf_program *prog,
198 const struct bpf_object *obj);
200 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
202 LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
203 bpf_program_clear_priv_t clear_priv);
205 LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
206 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
209 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
210 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
211 LIBBPF_API LIBBPF_DEPRECATED("BPF program title is confusing term; please use bpf_program__section_name() instead")
212 const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy);
213 LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
214 LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
216 /* returns program size in bytes */
217 LIBBPF_API size_t bpf_program__size(const struct bpf_program *prog);
219 LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license,
221 LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
222 LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
225 LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
228 LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
229 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
230 LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
234 LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
235 LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
236 LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
237 LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
238 LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
239 LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
240 struct bpf_program *prog);
241 LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
242 LIBBPF_API int bpf_link__detach(struct bpf_link *link);
243 LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
245 LIBBPF_API struct bpf_link *
246 bpf_program__attach(struct bpf_program *prog);
248 struct bpf_perf_event_opts {
249 /* size of this struct, for forward/backward compatiblity */
251 /* custom user-provided value fetchable through bpf_get_attach_cookie() */
254 #define bpf_perf_event_opts__last_field bpf_cookie
256 LIBBPF_API struct bpf_link *
257 bpf_program__attach_perf_event(struct bpf_program *prog, int pfd);
259 LIBBPF_API struct bpf_link *
260 bpf_program__attach_perf_event_opts(struct bpf_program *prog, int pfd,
261 const struct bpf_perf_event_opts *opts);
263 struct bpf_kprobe_opts {
264 /* size of this struct, for forward/backward compatiblity */
266 /* custom user-provided value fetchable through bpf_get_attach_cookie() */
268 /* function's offset to install kprobe to */
269 unsigned long offset;
270 /* kprobe is return probe */
274 #define bpf_kprobe_opts__last_field retprobe
276 LIBBPF_API struct bpf_link *
277 bpf_program__attach_kprobe(struct bpf_program *prog, bool retprobe,
278 const char *func_name);
279 LIBBPF_API struct bpf_link *
280 bpf_program__attach_kprobe_opts(struct bpf_program *prog,
281 const char *func_name,
282 const struct bpf_kprobe_opts *opts);
284 struct bpf_uprobe_opts {
285 /* size of this struct, for forward/backward compatiblity */
287 /* offset of kernel reference counted USDT semaphore, added in
288 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
290 size_t ref_ctr_offset;
291 /* custom user-provided value fetchable through bpf_get_attach_cookie() */
293 /* uprobe is return probe, invoked at function return time */
297 #define bpf_uprobe_opts__last_field retprobe
299 LIBBPF_API struct bpf_link *
300 bpf_program__attach_uprobe(struct bpf_program *prog, bool retprobe,
301 pid_t pid, const char *binary_path,
303 LIBBPF_API struct bpf_link *
304 bpf_program__attach_uprobe_opts(struct bpf_program *prog, pid_t pid,
305 const char *binary_path, size_t func_offset,
306 const struct bpf_uprobe_opts *opts);
308 struct bpf_tracepoint_opts {
309 /* size of this struct, for forward/backward compatiblity */
311 /* custom user-provided value fetchable through bpf_get_attach_cookie() */
314 #define bpf_tracepoint_opts__last_field bpf_cookie
316 LIBBPF_API struct bpf_link *
317 bpf_program__attach_tracepoint(struct bpf_program *prog,
318 const char *tp_category,
319 const char *tp_name);
320 LIBBPF_API struct bpf_link *
321 bpf_program__attach_tracepoint_opts(struct bpf_program *prog,
322 const char *tp_category,
324 const struct bpf_tracepoint_opts *opts);
326 LIBBPF_API struct bpf_link *
327 bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
328 const char *tp_name);
329 LIBBPF_API struct bpf_link *
330 bpf_program__attach_trace(struct bpf_program *prog);
331 LIBBPF_API struct bpf_link *
332 bpf_program__attach_lsm(struct bpf_program *prog);
333 LIBBPF_API struct bpf_link *
334 bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd);
335 LIBBPF_API struct bpf_link *
336 bpf_program__attach_netns(struct bpf_program *prog, int netns_fd);
337 LIBBPF_API struct bpf_link *
338 bpf_program__attach_xdp(struct bpf_program *prog, int ifindex);
339 LIBBPF_API struct bpf_link *
340 bpf_program__attach_freplace(struct bpf_program *prog,
341 int target_fd, const char *attach_func_name);
345 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map);
347 struct bpf_iter_attach_opts {
348 size_t sz; /* size of this struct for forward/backward compatibility */
349 union bpf_iter_link_info *link_info;
352 #define bpf_iter_attach_opts__last_field link_info_len
354 LIBBPF_API struct bpf_link *
355 bpf_program__attach_iter(struct bpf_program *prog,
356 const struct bpf_iter_attach_opts *opts);
361 * Libbpf allows callers to adjust BPF programs before being loaded
362 * into kernel. One program in an object file can be transformed into
363 * multiple variants to be attached to different hooks.
365 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
366 * form an API for this purpose.
368 * - bpf_program_prep_t:
369 * Defines a 'preprocessor', which is a caller defined function
370 * passed to libbpf through bpf_program__set_prep(), and will be
371 * called before program is loaded. The processor should adjust
372 * the program one time for each instance according to the instance id
375 * - bpf_program__set_prep:
376 * Attaches a preprocessor to a BPF program. The number of instances
377 * that should be created is also passed through this function.
379 * - bpf_program__nth_fd:
380 * After the program is loaded, get resulting FD of a given instance
381 * of the BPF program.
383 * If bpf_program__set_prep() is not used, the program would be loaded
384 * without adjustment during bpf_object__load(). The program has only
385 * one instance. In this case bpf_program__fd(prog) is equal to
386 * bpf_program__nth_fd(prog, 0).
389 struct bpf_prog_prep_result {
391 * If not NULL, load new instruction array.
392 * If set to NULL, don't load this instance.
394 struct bpf_insn *new_insn_ptr;
397 /* If not NULL, result FD is written to it. */
402 * Parameters of bpf_program_prep_t:
403 * - prog: The bpf_program being loaded.
404 * - n: Index of instance being generated.
405 * - insns: BPF instructions array.
406 * - insns_cnt:Number of instructions in insns.
407 * - res: Output parameter, result of transformation.
410 * - Zero: pre-processing success.
411 * - Non-zero: pre-processing error, stop loading.
413 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
414 struct bpf_insn *insns, int insns_cnt,
415 struct bpf_prog_prep_result *res);
417 LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
418 bpf_program_prep_t prep);
420 LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n);
423 * Adjust type of BPF program. Default is kprobe.
425 LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
426 LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
427 LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
428 LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
429 LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog);
430 LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
431 LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
432 LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
433 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
434 LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
435 LIBBPF_API int bpf_program__set_struct_ops(struct bpf_program *prog);
436 LIBBPF_API int bpf_program__set_extension(struct bpf_program *prog);
437 LIBBPF_API int bpf_program__set_sk_lookup(struct bpf_program *prog);
439 LIBBPF_API enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog);
440 LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
441 enum bpf_prog_type type);
443 LIBBPF_API enum bpf_attach_type
444 bpf_program__get_expected_attach_type(const struct bpf_program *prog);
446 bpf_program__set_expected_attach_type(struct bpf_program *prog,
447 enum bpf_attach_type type);
450 bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
451 const char *attach_func_name);
453 LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog);
454 LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog);
455 LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog);
456 LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog);
457 LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog);
458 LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog);
459 LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog);
460 LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
461 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
462 LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
463 LIBBPF_API bool bpf_program__is_struct_ops(const struct bpf_program *prog);
464 LIBBPF_API bool bpf_program__is_extension(const struct bpf_program *prog);
465 LIBBPF_API bool bpf_program__is_sk_lookup(const struct bpf_program *prog);
468 * No need for __attribute__((packed)), all members of 'bpf_map_def'
469 * are all aligned. In addition, using __attribute__((packed))
470 * would trigger a -Wpacked warning message, and lead to an error
475 unsigned int key_size;
476 unsigned int value_size;
477 unsigned int max_entries;
478 unsigned int map_flags;
482 * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel,
483 * so no need to worry about a name clash.
485 LIBBPF_API struct bpf_map *
486 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
489 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
492 * Get bpf_map through the offset of corresponding struct bpf_map_def
493 * in the BPF object file.
495 LIBBPF_API struct bpf_map *
496 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
498 LIBBPF_API struct bpf_map *
499 bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
500 #define bpf_object__for_each_map(pos, obj) \
501 for ((pos) = bpf_map__next(NULL, (obj)); \
503 (pos) = bpf_map__next((pos), (obj)))
504 #define bpf_map__for_each bpf_object__for_each_map
506 LIBBPF_API struct bpf_map *
507 bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
510 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
511 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
512 /* get map definition */
513 LIBBPF_API const struct bpf_map_def *bpf_map__def(const struct bpf_map *map);
515 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
516 /* get/set map type */
517 LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
518 LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
519 /* get/set map size (max_entries) */
520 LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
521 LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
522 LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
523 /* get/set map flags */
524 LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
525 LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
526 /* get/set map NUMA node */
527 LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
528 LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
529 /* get/set map key size */
530 LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
531 LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
532 /* get/set map value size */
533 LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
534 LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
535 /* get map key/value BTF type IDs */
536 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
537 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
538 /* get/set map if_index */
539 LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
540 LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
542 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
543 LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
544 bpf_map_clear_priv_t clear_priv);
545 LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
546 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
547 const void *data, size_t size);
548 LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
549 LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);
550 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
551 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
552 LIBBPF_API const char *bpf_map__get_pin_path(const struct bpf_map *map);
553 LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
554 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
555 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
556 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
558 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
559 LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
561 LIBBPF_API long libbpf_get_error(const void *ptr);
563 struct bpf_prog_load_attr {
565 enum bpf_prog_type prog_type;
566 enum bpf_attach_type expected_attach_type;
572 LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
573 struct bpf_object **pobj, int *prog_fd);
574 LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type,
575 struct bpf_object **pobj, int *prog_fd);
577 /* XDP related API */
578 struct xdp_link_info {
586 struct bpf_xdp_set_link_opts {
591 #define bpf_xdp_set_link_opts__last_field old_fd
593 LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
594 LIBBPF_API int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
595 const struct bpf_xdp_set_link_opts *opts);
596 LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
597 LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
598 size_t info_size, __u32 flags);
601 enum bpf_tc_attach_point {
602 BPF_TC_INGRESS = 1 << 0,
603 BPF_TC_EGRESS = 1 << 1,
604 BPF_TC_CUSTOM = 1 << 2,
607 #define BPF_TC_PARENT(a, b) \
608 ((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
611 BPF_TC_F_REPLACE = 1 << 0,
617 enum bpf_tc_attach_point attach_point;
621 #define bpf_tc_hook__last_field parent
632 #define bpf_tc_opts__last_field priority
634 LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
635 LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
636 LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
637 struct bpf_tc_opts *opts);
638 LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
639 const struct bpf_tc_opts *opts);
640 LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
641 struct bpf_tc_opts *opts);
643 /* Ring buffer APIs */
646 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
648 struct ring_buffer_opts {
649 size_t sz; /* size of this struct, for forward/backward compatiblity */
652 #define ring_buffer_opts__last_field sz
654 LIBBPF_API struct ring_buffer *
655 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
656 const struct ring_buffer_opts *opts);
657 LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
658 LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
659 ring_buffer_sample_fn sample_cb, void *ctx);
660 LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
661 LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
662 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
664 /* Perf buffer APIs */
667 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
668 void *data, __u32 size);
669 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
671 /* common use perf buffer options */
672 struct perf_buffer_opts {
673 /* if specified, sample_cb is called for each sample */
674 perf_buffer_sample_fn sample_cb;
675 /* if specified, lost_cb is called for each batch of lost samples */
676 perf_buffer_lost_fn lost_cb;
677 /* ctx is provided to sample_cb and lost_cb */
681 LIBBPF_API struct perf_buffer *
682 perf_buffer__new(int map_fd, size_t page_cnt,
683 const struct perf_buffer_opts *opts);
685 enum bpf_perf_event_ret {
686 LIBBPF_PERF_EVENT_DONE = 0,
687 LIBBPF_PERF_EVENT_ERROR = -1,
688 LIBBPF_PERF_EVENT_CONT = -2,
691 struct perf_event_header;
693 typedef enum bpf_perf_event_ret
694 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
696 /* raw perf buffer options, giving most power and control */
697 struct perf_buffer_raw_opts {
698 /* perf event attrs passed directly into perf_event_open() */
699 struct perf_event_attr *attr;
700 /* raw event callback */
701 perf_buffer_event_fn event_cb;
702 /* ctx is provided to event_cb */
704 /* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
705 * max_entries of given PERF_EVENT_ARRAY map)
708 /* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
710 /* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
714 LIBBPF_API struct perf_buffer *
715 perf_buffer__new_raw(int map_fd, size_t page_cnt,
716 const struct perf_buffer_raw_opts *opts);
718 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
719 LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
720 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
721 LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
722 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
723 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
724 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
726 typedef enum bpf_perf_event_ret
727 (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
729 LIBBPF_API enum bpf_perf_event_ret
730 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
731 void **copy_mem, size_t *copy_size,
732 bpf_perf_event_print_t fn, void *private_data);
734 struct bpf_prog_linfo;
735 struct bpf_prog_info;
737 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
738 LIBBPF_API struct bpf_prog_linfo *
739 bpf_prog_linfo__new(const struct bpf_prog_info *info);
740 LIBBPF_API const struct bpf_line_info *
741 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
742 __u64 addr, __u32 func_idx, __u32 nr_skip);
743 LIBBPF_API const struct bpf_line_info *
744 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
745 __u32 insn_off, __u32 nr_skip);
748 * Probe for supported system features
750 * Note that running many of these probes in a short amount of time can cause
751 * the kernel to reach the maximal size of lockable memory allowed for the
752 * user, causing subsequent probes to fail. In this case, the caller may want
753 * to adjust that limit with setrlimit().
755 LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type,
757 LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
758 LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id,
759 enum bpf_prog_type prog_type, __u32 ifindex);
760 LIBBPF_API bool bpf_probe_large_insn_limit(__u32 ifindex);
763 * Get bpf_prog_info in continuous memory
765 * struct bpf_prog_info has multiple arrays. The user has option to choose
766 * arrays to fetch from kernel. The following APIs provide an uniform way to
767 * fetch these data. All arrays in bpf_prog_info are stored in a single
768 * continuous memory region. This makes it easy to store the info in a
771 * Before writing bpf_prog_info_linear to files, it is necessary to
772 * translate pointers in bpf_prog_info to offsets. Helper functions
773 * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr()
774 * are introduced to switch between pointers and offsets.
777 * # To fetch map_ids and prog_tags:
778 * __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) |
779 * (1UL << BPF_PROG_INFO_PROG_TAGS);
780 * struct bpf_prog_info_linear *info_linear =
781 * bpf_program__get_prog_info_linear(fd, arrays);
783 * # To save data in file
784 * bpf_program__bpil_addr_to_offs(info_linear);
785 * write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
787 * # To read data from file
788 * read(f, info_linear, <proper_size>);
789 * bpf_program__bpil_offs_to_addr(info_linear);
791 enum bpf_prog_info_array {
792 BPF_PROG_INFO_FIRST_ARRAY = 0,
793 BPF_PROG_INFO_JITED_INSNS = 0,
794 BPF_PROG_INFO_XLATED_INSNS,
795 BPF_PROG_INFO_MAP_IDS,
796 BPF_PROG_INFO_JITED_KSYMS,
797 BPF_PROG_INFO_JITED_FUNC_LENS,
798 BPF_PROG_INFO_FUNC_INFO,
799 BPF_PROG_INFO_LINE_INFO,
800 BPF_PROG_INFO_JITED_LINE_INFO,
801 BPF_PROG_INFO_PROG_TAGS,
802 BPF_PROG_INFO_LAST_ARRAY,
805 struct bpf_prog_info_linear {
806 /* size of struct bpf_prog_info, when the tool is compiled */
808 /* total bytes allocated for data, round up to 8 bytes */
810 /* which arrays are included in data */
812 struct bpf_prog_info info;
816 LIBBPF_API struct bpf_prog_info_linear *
817 bpf_program__get_prog_info_linear(int fd, __u64 arrays);
820 bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
823 bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
826 * A helper function to get the number of possible CPUs before looking up
827 * per-CPU maps. Negative errno is returned on failure.
831 * int ncpus = libbpf_num_possible_cpus();
835 * long values[ncpus];
836 * bpf_map_lookup_elem(per_cpu_map_fd, key, values);
839 LIBBPF_API int libbpf_num_possible_cpus(void);
841 struct bpf_map_skeleton {
843 struct bpf_map **map;
847 struct bpf_prog_skeleton {
849 struct bpf_program **prog;
850 struct bpf_link **link;
853 struct bpf_object_skeleton {
854 size_t sz; /* size of this struct, for forward/backward compatibility */
860 struct bpf_object **obj;
863 int map_skel_sz; /* sizeof(struct bpf_skeleton_map) */
864 struct bpf_map_skeleton *maps;
867 int prog_skel_sz; /* sizeof(struct bpf_skeleton_prog) */
868 struct bpf_prog_skeleton *progs;
872 bpf_object__open_skeleton(struct bpf_object_skeleton *s,
873 const struct bpf_object_open_opts *opts);
874 LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
875 LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
876 LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
877 LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
879 struct gen_loader_opts {
880 size_t sz; /* size of this struct, for forward/backward compatiblity */
887 #define gen_loader_opts__last_field insns_sz
888 LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
889 struct gen_loader_opts *opts);
891 enum libbpf_tristate {
897 struct bpf_linker_opts {
898 /* size of this struct, for forward/backward compatiblity */
901 #define bpf_linker_opts__last_field sz
903 struct bpf_linker_file_opts {
904 /* size of this struct, for forward/backward compatiblity */
907 #define bpf_linker_file_opts__last_field sz
911 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
912 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
913 const char *filename,
914 const struct bpf_linker_file_opts *opts);
915 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
916 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
922 #endif /* __LIBBPF_LIBBPF_H */