Linux Audio

Check our new training course

Loading...
v6.2
   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#include "libbpf_common.h"
  21#include "libbpf_legacy.h"
  22
  23#ifdef __cplusplus
  24extern "C" {
  25#endif
  26
  27LIBBPF_API __u32 libbpf_major_version(void);
  28LIBBPF_API __u32 libbpf_minor_version(void);
  29LIBBPF_API const char *libbpf_version_string(void);
  30
  31enum libbpf_errno {
  32	__LIBBPF_ERRNO__START = 4000,
  33
  34	/* Something wrong in libelf */
  35	LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
  36	LIBBPF_ERRNO__FORMAT,	/* BPF object format invalid */
  37	LIBBPF_ERRNO__KVERSION,	/* Incorrect or no 'version' section */
  38	LIBBPF_ERRNO__ENDIAN,	/* Endian mismatch */
  39	LIBBPF_ERRNO__INTERNAL,	/* Internal error in libbpf */
  40	LIBBPF_ERRNO__RELOC,	/* Relocation failed */
  41	LIBBPF_ERRNO__LOAD,	/* Load program failure for unknown reason */
  42	LIBBPF_ERRNO__VERIFY,	/* Kernel verifier blocks program loading */
  43	LIBBPF_ERRNO__PROG2BIG,	/* Program too big */
  44	LIBBPF_ERRNO__KVER,	/* Incorrect kernel version */
  45	LIBBPF_ERRNO__PROGTYPE,	/* Kernel doesn't support this program type */
  46	LIBBPF_ERRNO__WRNGPID,	/* Wrong pid in netlink message */
  47	LIBBPF_ERRNO__INVSEQ,	/* Invalid netlink sequence */
  48	LIBBPF_ERRNO__NLPARSE,	/* netlink parsing error */
  49	__LIBBPF_ERRNO__END,
  50};
  51
  52LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
  53
  54/**
  55 * @brief **libbpf_bpf_attach_type_str()** converts the provided attach type
  56 * value into a textual representation.
  57 * @param t The attach type.
  58 * @return Pointer to a static string identifying the attach type. NULL is
  59 * returned for unknown **bpf_attach_type** values.
  60 */
  61LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t);
  62
  63/**
  64 * @brief **libbpf_bpf_link_type_str()** converts the provided link type value
  65 * into a textual representation.
  66 * @param t The link type.
  67 * @return Pointer to a static string identifying the link type. NULL is
  68 * returned for unknown **bpf_link_type** values.
  69 */
  70LIBBPF_API const char *libbpf_bpf_link_type_str(enum bpf_link_type t);
  71
  72/**
  73 * @brief **libbpf_bpf_map_type_str()** converts the provided map type value
  74 * into a textual representation.
  75 * @param t The map type.
  76 * @return Pointer to a static string identifying the map type. NULL is
  77 * returned for unknown **bpf_map_type** values.
  78 */
  79LIBBPF_API const char *libbpf_bpf_map_type_str(enum bpf_map_type t);
  80
  81/**
  82 * @brief **libbpf_bpf_prog_type_str()** converts the provided program type
  83 * value into a textual representation.
  84 * @param t The program type.
  85 * @return Pointer to a static string identifying the program type. NULL is
  86 * returned for unknown **bpf_prog_type** values.
  87 */
  88LIBBPF_API const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t);
  89
  90enum libbpf_print_level {
  91        LIBBPF_WARN,
  92        LIBBPF_INFO,
  93        LIBBPF_DEBUG,
  94};
  95
  96typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
  97				 const char *, va_list ap);
  98
 
 
 
 
 
 
 
 
 
 
 
  99LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
 100
 101/* Hide internal to user */
 102struct bpf_object;
 103
 104struct bpf_object_open_opts {
 105	/* size of this struct, for forward/backward compatibility */
 106	size_t sz;
 107	/* object name override, if provided:
 108	 * - for object open from file, this will override setting object
 109	 *   name from file path's base name;
 110	 * - for object open from memory buffer, this will specify an object
 111	 *   name and will override default "<addr>-<buf-size>" name;
 112	 */
 113	const char *object_name;
 114	/* parse map definitions non-strictly, allowing extra attributes/data */
 115	bool relaxed_maps;
 116	/* maps that set the 'pinning' attribute in their definition will have
 117	 * their pin_path attribute set to a file in this directory, and be
 118	 * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
 119	 */
 120	const char *pin_root_path;
 121
 122	__u32 :32; /* stub out now removed attach_prog_fd */
 123
 124	/* Additional kernel config content that augments and overrides
 125	 * system Kconfig for CONFIG_xxx externs.
 126	 */
 127	const char *kconfig;
 128	/* Path to the custom BTF to be used for BPF CO-RE relocations.
 129	 * This custom BTF completely replaces the use of vmlinux BTF
 130	 * for the purpose of CO-RE relocations.
 131	 * NOTE: any other BPF feature (e.g., fentry/fexit programs,
 132	 * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.
 133	 */
 134	const char *btf_custom_path;
 135	/* Pointer to a buffer for storing kernel logs for applicable BPF
 136	 * commands. Valid kernel_log_size has to be specified as well and are
 137	 * passed-through to bpf() syscall. Keep in mind that kernel might
 138	 * fail operation with -ENOSPC error if provided buffer is too small
 139	 * to contain entire log output.
 140	 * See the comment below for kernel_log_level for interaction between
 141	 * log_buf and log_level settings.
 142	 *
 143	 * If specified, this log buffer will be passed for:
 144	 *   - each BPF progral load (BPF_PROG_LOAD) attempt, unless overriden
 145	 *     with bpf_program__set_log() on per-program level, to get
 146	 *     BPF verifier log output.
 147	 *   - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get
 148	 *     BTF sanity checking log.
 149	 *
 150	 * Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite
 151	 * previous contents, so if you need more fine-grained control, set
 152	 * per-program buffer with bpf_program__set_log_buf() to preserve each
 153	 * individual program's verification log. Keep using kernel_log_buf
 154	 * for BTF verification log, if necessary.
 155	 */
 156	char *kernel_log_buf;
 157	size_t kernel_log_size;
 158	/*
 159	 * Log level can be set independently from log buffer. Log_level=0
 160	 * means that libbpf will attempt loading BTF or program without any
 161	 * logging requested, but will retry with either its own or custom log
 162	 * buffer, if provided, and log_level=1 on any error.
 163	 * And vice versa, setting log_level>0 will request BTF or prog
 164	 * loading with verbose log from the first attempt (and as such also
 165	 * for successfully loaded BTF or program), and the actual log buffer
 166	 * could be either libbpf's own auto-allocated log buffer, if
 167	 * kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.
 168	 * If user didn't provide custom log buffer, libbpf will emit captured
 169	 * logs through its print callback.
 170	 */
 171	__u32 kernel_log_level;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 172
 173	size_t :0;
 174};
 175#define bpf_object_open_opts__last_field kernel_log_level
 176
 
 
 
 
 
 
 
 
 177LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
 178
 179/**
 180 * @brief **bpf_object__open_file()** creates a bpf_object by opening
 181 * the BPF ELF object file pointed to by the passed path and loading it
 182 * into memory.
 183 * @param path BPF object file path
 184 * @param opts options for how to load the bpf object, this parameter is
 185 * optional and can be set to NULL
 186 * @return pointer to the new bpf_object; or NULL is returned on error,
 187 * error code is stored in errno
 188 */
 189LIBBPF_API struct bpf_object *
 190bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
 191
 192/**
 193 * @brief **bpf_object__open_mem()** creates a bpf_object by reading
 194 * the BPF objects raw bytes from a memory buffer containing a valid
 195 * BPF ELF object file.
 196 * @param obj_buf pointer to the buffer containing ELF file bytes
 197 * @param obj_buf_sz number of bytes in the buffer
 198 * @param opts options for how to load the bpf object
 199 * @return pointer to the new bpf_object; or NULL is returned on error,
 200 * error code is stored in errno
 201 */
 202LIBBPF_API struct bpf_object *
 203bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
 204		     const struct bpf_object_open_opts *opts);
 205
 206/* Load/unload object into/from kernel */
 
 
 
 
 
 
 207LIBBPF_API int bpf_object__load(struct bpf_object *obj);
 208
 209LIBBPF_API void bpf_object__close(struct bpf_object *object);
 
 
 
 
 
 210
 211/* pin_maps and unpin_maps can both be called with a NULL path, in which case
 212 * they will use the pin_path attribute of each map (and ignore all maps that
 213 * don't have a pin_path set).
 
 
 
 
 
 
 
 214 */
 215LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
 
 
 
 
 
 
 
 
 
 
 
 
 216LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
 217				      const char *path);
 218LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
 219					const char *path);
 220LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
 221					  const char *path);
 222LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
 
 223
 224LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
 225LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
 226LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);
 227
 
 
 
 
 
 
 
 
 228struct btf;
 229LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
 230LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
 231
 232LIBBPF_API struct bpf_program *
 233bpf_object__find_program_by_name(const struct bpf_object *obj,
 234				 const char *name);
 235
 236LIBBPF_API int
 237libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
 238			 enum bpf_attach_type *expected_attach_type);
 239LIBBPF_API int libbpf_attach_type_by_name(const char *name,
 240					  enum bpf_attach_type *attach_type);
 241LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
 242					  enum bpf_attach_type attach_type);
 243
 244/* Accessors of bpf_program */
 245struct bpf_program;
 246
 247LIBBPF_API struct bpf_program *
 248bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);
 249
 250#define bpf_object__for_each_program(pos, obj)			\
 251	for ((pos) = bpf_object__next_program((obj), NULL);	\
 252	     (pos) != NULL;					\
 253	     (pos) = bpf_object__next_program((obj), (pos)))
 254
 255LIBBPF_API struct bpf_program *
 256bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);
 257
 258LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
 259					 __u32 ifindex);
 260
 261LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
 262LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
 263LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
 264LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
 265LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog);
 266LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach);
 267
 268struct bpf_insn;
 269
 270/**
 271 * @brief **bpf_program__insns()** gives read-only access to BPF program's
 272 * underlying BPF instructions.
 273 * @param prog BPF program for which to return instructions
 274 * @return a pointer to an array of BPF instructions that belong to the
 275 * specified BPF program
 276 *
 277 * Returned pointer is always valid and not NULL. Number of `struct bpf_insn`
 278 * pointed to can be fetched using **bpf_program__insn_cnt()** API.
 279 *
 280 * Keep in mind, libbpf can modify and append/delete BPF program's
 281 * instructions as it processes BPF object file and prepares everything for
 282 * uploading into the kernel. So depending on the point in BPF object
 283 * lifetime, **bpf_program__insns()** can return different sets of
 284 * instructions. As an example, during BPF object load phase BPF program
 285 * instructions will be CO-RE-relocated, BPF subprograms instructions will be
 286 * appended, ldimm64 instructions will have FDs embedded, etc. So instructions
 287 * returned before **bpf_object__load()** and after it might be quite
 288 * different.
 289 */
 290LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
 291
 292/**
 293 * @brief **bpf_program__set_insns()** can set BPF program's underlying
 294 * BPF instructions.
 295 *
 296 * WARNING: This is a very advanced libbpf API and users need to know
 297 * what they are doing. This should be used from prog_prepare_load_fn
 298 * callback only.
 299 *
 300 * @param prog BPF program for which to return instructions
 301 * @param new_insns a pointer to an array of BPF instructions
 302 * @param new_insn_cnt number of `struct bpf_insn`'s that form
 303 * specified BPF program
 304 * @return 0, on success; negative error code, otherwise
 305 */
 306LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog,
 307				      struct bpf_insn *new_insns, size_t new_insn_cnt);
 308
 309/**
 310 * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
 311 * that form specified BPF program.
 312 * @param prog BPF program for which to return number of BPF instructions
 313 *
 314 * See **bpf_program__insns()** documentation for notes on how libbpf can
 315 * change instructions and their count during different phases of
 316 * **bpf_object** lifetime.
 317 */
 318LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);
 319
 320LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
 321
 322/**
 323 * @brief **bpf_program__pin()** pins the BPF program to a file
 324 * in the BPF FS specified by a path. This increments the programs
 325 * reference count, allowing it to stay loaded after the process
 326 * which loaded it has exited.
 327 *
 328 * @param prog BPF program to pin, must already be loaded
 329 * @param path file path in a BPF file system
 330 * @return 0, on success; negative error code, otherwise
 331 */
 332LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
 333
 334/**
 335 * @brief **bpf_program__unpin()** unpins the BPF program from a file
 336 * in the BPFFS specified by a path. This decrements the programs
 337 * reference count.
 338 *
 339 * The file pinning the BPF program can also be unlinked by a different
 340 * process in which case this function will return an error.
 341 *
 342 * @param prog BPF program to unpin
 343 * @param path file path to the pin in a BPF file system
 344 * @return 0, on success; negative error code, otherwise
 345 */
 346LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
 347LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
 348
 349struct bpf_link;
 350
 351LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
 352LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
 353LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
 354/**
 355 * @brief **bpf_link__pin()** pins the BPF link to a file
 356 * in the BPF FS specified by a path. This increments the links
 357 * reference count, allowing it to stay loaded after the process
 358 * which loaded it has exited.
 359 *
 360 * @param link BPF link to pin, must already be loaded
 361 * @param path file path in a BPF file system
 362 * @return 0, on success; negative error code, otherwise
 363 */
 364
 365LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
 366
 367/**
 368 * @brief **bpf_link__unpin()** unpins the BPF link from a file
 369 * in the BPFFS specified by a path. This decrements the links
 370 * reference count.
 371 *
 372 * The file pinning the BPF link can also be unlinked by a different
 373 * process in which case this function will return an error.
 374 *
 375 * @param prog BPF program to unpin
 376 * @param path file path to the pin in a BPF file system
 377 * @return 0, on success; negative error code, otherwise
 378 */
 379LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
 380LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
 381					struct bpf_program *prog);
 382LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
 383LIBBPF_API int bpf_link__detach(struct bpf_link *link);
 384LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
 385
 386/**
 387 * @brief **bpf_program__attach()** is a generic function for attaching
 388 * a BPF program based on auto-detection of program type, attach type,
 389 * and extra paremeters, where applicable.
 390 *
 391 * @param prog BPF program to attach
 392 * @return Reference to the newly created BPF link; or NULL is returned on error,
 393 * error code is stored in errno
 394 *
 395 * This is supported for:
 396 *   - kprobe/kretprobe (depends on SEC() definition)
 397 *   - uprobe/uretprobe (depends on SEC() definition)
 398 *   - tracepoint
 399 *   - raw tracepoint
 400 *   - tracing programs (typed raw TP/fentry/fexit/fmod_ret)
 401 */
 402LIBBPF_API struct bpf_link *
 403bpf_program__attach(const struct bpf_program *prog);
 404
 405struct bpf_perf_event_opts {
 406	/* size of this struct, for forward/backward compatiblity */
 407	size_t sz;
 408	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
 409	__u64 bpf_cookie;
 
 
 
 410};
 411#define bpf_perf_event_opts__last_field bpf_cookie
 412
 413LIBBPF_API struct bpf_link *
 414bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
 415
 416LIBBPF_API struct bpf_link *
 417bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
 418				    const struct bpf_perf_event_opts *opts);
 419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 420struct bpf_kprobe_opts {
 421	/* size of this struct, for forward/backward compatiblity */
 422	size_t sz;
 423	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
 424	__u64 bpf_cookie;
 425	/* function's offset to install kprobe to */
 426	size_t offset;
 427	/* kprobe is return probe */
 428	bool retprobe;
 
 
 429	size_t :0;
 430};
 431#define bpf_kprobe_opts__last_field retprobe
 432
 433LIBBPF_API struct bpf_link *
 434bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
 435			   const char *func_name);
 436LIBBPF_API struct bpf_link *
 437bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
 438                                const char *func_name,
 439                                const struct bpf_kprobe_opts *opts);
 440
 441struct bpf_kprobe_multi_opts {
 442	/* size of this struct, for forward/backward compatibility */
 443	size_t sz;
 444	/* array of function symbols to attach */
 445	const char **syms;
 446	/* array of function addresses to attach */
 447	const unsigned long *addrs;
 448	/* array of user-provided values fetchable through bpf_get_attach_cookie */
 449	const __u64 *cookies;
 450	/* number of elements in syms/addrs/cookies arrays */
 451	size_t cnt;
 452	/* create return kprobes */
 453	bool retprobe;
 
 
 454	size_t :0;
 455};
 456
 457#define bpf_kprobe_multi_opts__last_field retprobe
 458
 459LIBBPF_API struct bpf_link *
 460bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
 461				      const char *pattern,
 462				      const struct bpf_kprobe_multi_opts *opts);
 463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 464struct bpf_ksyscall_opts {
 465	/* size of this struct, for forward/backward compatiblity */
 466	size_t sz;
 467	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
 468	__u64 bpf_cookie;
 469	/* attach as return probe? */
 470	bool retprobe;
 471	size_t :0;
 472};
 473#define bpf_ksyscall_opts__last_field retprobe
 474
 475/**
 476 * @brief **bpf_program__attach_ksyscall()** attaches a BPF program
 477 * to kernel syscall handler of a specified syscall. Optionally it's possible
 478 * to request to install retprobe that will be triggered at syscall exit. It's
 479 * also possible to associate BPF cookie (though options).
 480 *
 481 * Libbpf automatically will determine correct full kernel function name,
 482 * which depending on system architecture and kernel version/configuration
 483 * could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will
 484 * attach specified program using kprobe/kretprobe mechanism.
 485 *
 486 * **bpf_program__attach_ksyscall()** is an API counterpart of declarative
 487 * **SEC("ksyscall/<syscall>")** annotation of BPF programs.
 488 *
 489 * At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do
 490 * not handle all the calling convention quirks for mmap(), clone() and compat
 491 * syscalls. It also only attaches to "native" syscall interfaces. If host
 492 * system supports compat syscalls or defines 32-bit syscalls in 64-bit
 493 * kernel, such syscall interfaces won't be attached to by libbpf.
 494 *
 495 * These limitations may or may not change in the future. Therefore it is
 496 * recommended to use SEC("kprobe") for these syscalls or if working with
 497 * compat and 32-bit interfaces is required.
 498 *
 499 * @param prog BPF program to attach
 500 * @param syscall_name Symbolic name of the syscall (e.g., "bpf")
 501 * @param opts Additional options (see **struct bpf_ksyscall_opts**)
 502 * @return Reference to the newly created BPF link; or NULL is returned on
 503 * error, error code is stored in errno
 504 */
 505LIBBPF_API struct bpf_link *
 506bpf_program__attach_ksyscall(const struct bpf_program *prog,
 507			     const char *syscall_name,
 508			     const struct bpf_ksyscall_opts *opts);
 509
 510struct bpf_uprobe_opts {
 511	/* size of this struct, for forward/backward compatiblity */
 512	size_t sz;
 513	/* offset of kernel reference counted USDT semaphore, added in
 514	 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
 515	 */
 516	size_t ref_ctr_offset;
 517	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
 518	__u64 bpf_cookie;
 519	/* uprobe is return probe, invoked at function return time */
 520	bool retprobe;
 521	/* Function name to attach to.  Could be an unqualified ("abc") or library-qualified
 522	 * "abc@LIBXYZ" name.  To specify function entry, func_name should be set while
 523	 * func_offset argument to bpf_prog__attach_uprobe_opts() should be 0.  To trace an
 524	 * offset within a function, specify func_name and use func_offset argument to specify
 525	 * offset within the function.  Shared library functions must specify the shared library
 526	 * binary_path.
 527	 */
 528	const char *func_name;
 
 
 529	size_t :0;
 530};
 531#define bpf_uprobe_opts__last_field func_name
 532
 533/**
 534 * @brief **bpf_program__attach_uprobe()** attaches a BPF program
 535 * to the userspace function which is found by binary path and
 536 * offset. You can optionally specify a particular proccess to attach
 537 * to. You can also optionally attach the program to the function
 538 * exit instead of entry.
 539 *
 540 * @param prog BPF program to attach
 541 * @param retprobe Attach to function exit
 542 * @param pid Process ID to attach the uprobe to, 0 for self (own process),
 543 * -1 for all processes
 544 * @param binary_path Path to binary that contains the function symbol
 545 * @param func_offset Offset within the binary of the function symbol
 546 * @return Reference to the newly created BPF link; or NULL is returned on error,
 547 * error code is stored in errno
 548 */
 549LIBBPF_API struct bpf_link *
 550bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
 551			   pid_t pid, const char *binary_path,
 552			   size_t func_offset);
 553
 554/**
 555 * @brief **bpf_program__attach_uprobe_opts()** is just like
 556 * bpf_program__attach_uprobe() except with a options struct
 557 * for various configurations.
 558 *
 559 * @param prog BPF program to attach
 560 * @param pid Process ID to attach the uprobe to, 0 for self (own process),
 561 * -1 for all processes
 562 * @param binary_path Path to binary that contains the function symbol
 563 * @param func_offset Offset within the binary of the function symbol
 564 * @param opts Options for altering program attachment
 565 * @return Reference to the newly created BPF link; or NULL is returned on error,
 566 * error code is stored in errno
 567 */
 568LIBBPF_API struct bpf_link *
 569bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
 570				const char *binary_path, size_t func_offset,
 571				const struct bpf_uprobe_opts *opts);
 572
 573struct bpf_usdt_opts {
 574	/* size of this struct, for forward/backward compatibility */
 575	size_t sz;
 576	/* custom user-provided value accessible through usdt_cookie() */
 577	__u64 usdt_cookie;
 578	size_t :0;
 579};
 580#define bpf_usdt_opts__last_field usdt_cookie
 581
 582/**
 583 * @brief **bpf_program__attach_usdt()** is just like
 584 * bpf_program__attach_uprobe_opts() except it covers USDT (User-space
 585 * Statically Defined Tracepoint) attachment, instead of attaching to
 586 * user-space function entry or exit.
 587 *
 588 * @param prog BPF program to attach
 589 * @param pid Process ID to attach the uprobe to, 0 for self (own process),
 590 * -1 for all processes
 591 * @param binary_path Path to binary that contains provided USDT probe
 592 * @param usdt_provider USDT provider name
 593 * @param usdt_name USDT probe name
 594 * @param opts Options for altering program attachment
 595 * @return Reference to the newly created BPF link; or NULL is returned on error,
 596 * error code is stored in errno
 597 */
 598LIBBPF_API struct bpf_link *
 599bpf_program__attach_usdt(const struct bpf_program *prog,
 600			 pid_t pid, const char *binary_path,
 601			 const char *usdt_provider, const char *usdt_name,
 602			 const struct bpf_usdt_opts *opts);
 603
 604struct bpf_tracepoint_opts {
 605	/* size of this struct, for forward/backward compatiblity */
 606	size_t sz;
 607	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
 608	__u64 bpf_cookie;
 609};
 610#define bpf_tracepoint_opts__last_field bpf_cookie
 611
 612LIBBPF_API struct bpf_link *
 613bpf_program__attach_tracepoint(const struct bpf_program *prog,
 614			       const char *tp_category,
 615			       const char *tp_name);
 616LIBBPF_API struct bpf_link *
 617bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
 618				    const char *tp_category,
 619				    const char *tp_name,
 620				    const struct bpf_tracepoint_opts *opts);
 621
 
 
 
 
 
 
 
 622LIBBPF_API struct bpf_link *
 623bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
 624				   const char *tp_name);
 
 
 
 
 625
 626struct bpf_trace_opts {
 627	/* size of this struct, for forward/backward compatibility */
 628	size_t sz;
 629	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
 630	__u64 cookie;
 631};
 632#define bpf_trace_opts__last_field cookie
 633
 634LIBBPF_API struct bpf_link *
 635bpf_program__attach_trace(const struct bpf_program *prog);
 636LIBBPF_API struct bpf_link *
 637bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);
 638
 639LIBBPF_API struct bpf_link *
 640bpf_program__attach_lsm(const struct bpf_program *prog);
 641LIBBPF_API struct bpf_link *
 642bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
 643LIBBPF_API struct bpf_link *
 644bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
 645LIBBPF_API struct bpf_link *
 
 
 646bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
 647LIBBPF_API struct bpf_link *
 648bpf_program__attach_freplace(const struct bpf_program *prog,
 649			     int target_fd, const char *attach_func_name);
 650
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 651struct bpf_map;
 652
 653LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
 
 654
 655struct bpf_iter_attach_opts {
 656	size_t sz; /* size of this struct for forward/backward compatibility */
 657	union bpf_iter_link_info *link_info;
 658	__u32 link_info_len;
 659};
 660#define bpf_iter_attach_opts__last_field link_info_len
 661
 662LIBBPF_API struct bpf_link *
 663bpf_program__attach_iter(const struct bpf_program *prog,
 664			 const struct bpf_iter_attach_opts *opts);
 665
 666LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);
 667
 668/**
 669 * @brief **bpf_program__set_type()** sets the program
 670 * type of the passed BPF program.
 671 * @param prog BPF program to set the program type for
 672 * @param type program type to set the BPF map to have
 673 * @return error code; or 0 if no error. An error occurs
 674 * if the object is already loaded.
 675 *
 676 * This must be called before the BPF object is loaded,
 677 * otherwise it has no effect and an error is returned.
 678 */
 679LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,
 680				     enum bpf_prog_type type);
 681
 682LIBBPF_API enum bpf_attach_type
 683bpf_program__expected_attach_type(const struct bpf_program *prog);
 684
 685/**
 686 * @brief **bpf_program__set_expected_attach_type()** sets the
 687 * attach type of the passed BPF program. This is used for
 688 * auto-detection of attachment when programs are loaded.
 689 * @param prog BPF program to set the attach type for
 690 * @param type attach type to set the BPF map to have
 691 * @return error code; or 0 if no error. An error occurs
 692 * if the object is already loaded.
 693 *
 694 * This must be called before the BPF object is loaded,
 695 * otherwise it has no effect and an error is returned.
 696 */
 697LIBBPF_API int
 698bpf_program__set_expected_attach_type(struct bpf_program *prog,
 699				      enum bpf_attach_type type);
 700
 701LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
 702LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
 703
 704/* Per-program log level and log buffer getters/setters.
 705 * See bpf_object_open_opts comments regarding log_level and log_buf
 706 * interactions.
 707 */
 708LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);
 709LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);
 710LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
 711LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
 712
 713/**
 714 * @brief **bpf_program__set_attach_target()** sets BTF-based attach target
 715 * for supported BPF program types:
 716 *   - BTF-aware raw tracepoints (tp_btf);
 717 *   - fentry/fexit/fmod_ret;
 718 *   - lsm;
 719 *   - freplace.
 720 * @param prog BPF program to set the attach type for
 721 * @param type attach type to set the BPF map to have
 722 * @return error code; or 0 if no error occurred.
 723 */
 724LIBBPF_API int
 725bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
 726			       const char *attach_func_name);
 727
 728/**
 729 * @brief **bpf_object__find_map_by_name()** returns BPF map of
 730 * the given name, if it exists within the passed BPF object
 731 * @param obj BPF object
 732 * @param name name of the BPF map
 733 * @return BPF map instance, if such map exists within the BPF object;
 734 * or NULL otherwise.
 735 */
 736LIBBPF_API struct bpf_map *
 737bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
 738
 739LIBBPF_API int
 740bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
 741
 742LIBBPF_API struct bpf_map *
 743bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
 744
 745#define bpf_object__for_each_map(pos, obj)		\
 746	for ((pos) = bpf_object__next_map((obj), NULL);	\
 747	     (pos) != NULL;				\
 748	     (pos) = bpf_object__next_map((obj), (pos)))
 749#define bpf_map__for_each bpf_object__for_each_map
 750
 751LIBBPF_API struct bpf_map *
 752bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
 753
 754/**
 755 * @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create
 756 * BPF map during BPF object load phase.
 757 * @param map the BPF map instance
 758 * @param autocreate whether to create BPF map during BPF object load
 759 * @return 0 on success; -EBUSY if BPF object was already loaded
 760 *
 761 * **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating
 762 * BPF map. By default, libbpf will attempt to create every single BPF map
 763 * defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall
 764 * and fill in map FD in BPF instructions.
 765 *
 766 * This API allows to opt-out of this process for specific map instance. This
 767 * can be useful if host kernel doesn't support such BPF map type or used
 768 * combination of flags and user application wants to avoid creating such
 769 * a map in the first place. User is still responsible to make sure that their
 770 * BPF-side code that expects to use such missing BPF map is recognized by BPF
 771 * verifier as dead code, otherwise BPF verifier will reject such BPF program.
 772 */
 773LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);
 774LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);
 775
 776/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 777 * @brief **bpf_map__fd()** gets the file descriptor of the passed
 778 * BPF map
 779 * @param map the BPF map instance
 780 * @return the file descriptor; or -EINVAL in case of an error
 781 */
 782LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
 783LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
 784/* get map name */
 785LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
 786/* get/set map type */
 787LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
 788LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
 789/* get/set map size (max_entries) */
 790LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
 791LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
 792/* get/set map flags */
 793LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
 794LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
 795/* get/set map NUMA node */
 796LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
 797LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
 798/* get/set map key size */
 799LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
 800LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
 801/* get/set map value size */
 802LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 803LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
 804/* get map key/value BTF type IDs */
 805LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
 806LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
 807/* get/set map if_index */
 808LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
 809LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
 810/* get/set map map_extra flags */
 811LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
 812LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
 813
 814LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
 815					  const void *data, size_t size);
 816LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
 817
 818/**
 819 * @brief **bpf_map__is_internal()** tells the caller whether or not the
 820 * passed map is a special map created by libbpf automatically for things like
 821 * global variables, __ksym externs, Kconfig values, etc
 822 * @param map the bpf_map
 823 * @return true, if the map is an internal map; false, otherwise
 824 */
 825LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
 
 
 
 
 
 
 
 
 826LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
 
 
 
 
 
 
 
 827LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
 
 
 
 
 
 
 
 828LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
 
 
 
 
 
 
 
 
 
 
 
 
 
 829LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
 
 
 
 
 
 
 
 
 
 
 
 
 830LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
 831
 832LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
 833LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
 834
 835/**
 836 * @brief **bpf_map__lookup_elem()** allows to lookup BPF map value
 837 * corresponding to provided key.
 838 * @param map BPF map to lookup element in
 839 * @param key pointer to memory containing bytes of the key used for lookup
 840 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
 841 * @param value pointer to memory in which looked up value will be stored
 842 * @param value_sz size in byte of value data memory; it has to match BPF map
 843 * definition's **value_size**. For per-CPU BPF maps value size has to be
 844 * a product of BPF map value size and number of possible CPUs in the system
 845 * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
 846 * per-CPU values value size has to be aligned up to closest 8 bytes for
 847 * alignment reasons, so expected size is: `round_up(value_size, 8)
 848 * * libbpf_num_possible_cpus()`.
 849 * @flags extra flags passed to kernel for this operation
 850 * @return 0, on success; negative error, otherwise
 851 *
 852 * **bpf_map__lookup_elem()** is high-level equivalent of
 853 * **bpf_map_lookup_elem()** API with added check for key and value size.
 854 */
 855LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,
 856				    const void *key, size_t key_sz,
 857				    void *value, size_t value_sz, __u64 flags);
 858
 859/**
 860 * @brief **bpf_map__update_elem()** allows to insert or update value in BPF
 861 * map that corresponds to provided key.
 862 * @param map BPF map to insert to or update element in
 863 * @param key pointer to memory containing bytes of the key
 864 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
 865 * @param value pointer to memory containing bytes of the value
 866 * @param value_sz size in byte of value data memory; it has to match BPF map
 867 * definition's **value_size**. For per-CPU BPF maps value size has to be
 868 * a product of BPF map value size and number of possible CPUs in the system
 869 * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
 870 * per-CPU values value size has to be aligned up to closest 8 bytes for
 871 * alignment reasons, so expected size is: `round_up(value_size, 8)
 872 * * libbpf_num_possible_cpus()`.
 873 * @flags extra flags passed to kernel for this operation
 874 * @return 0, on success; negative error, otherwise
 875 *
 876 * **bpf_map__update_elem()** is high-level equivalent of
 877 * **bpf_map_update_elem()** API with added check for key and value size.
 878 */
 879LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map,
 880				    const void *key, size_t key_sz,
 881				    const void *value, size_t value_sz, __u64 flags);
 882
 883/**
 884 * @brief **bpf_map__delete_elem()** allows to delete element in BPF map that
 885 * corresponds to provided key.
 886 * @param map BPF map to delete element from
 887 * @param key pointer to memory containing bytes of the key
 888 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
 889 * @flags extra flags passed to kernel for this operation
 890 * @return 0, on success; negative error, otherwise
 891 *
 892 * **bpf_map__delete_elem()** is high-level equivalent of
 893 * **bpf_map_delete_elem()** API with added check for key size.
 894 */
 895LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map,
 896				    const void *key, size_t key_sz, __u64 flags);
 897
 898/**
 899 * @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value
 900 * corresponding to provided key and atomically delete it afterwards.
 901 * @param map BPF map to lookup element in
 902 * @param key pointer to memory containing bytes of the key used for lookup
 903 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
 904 * @param value pointer to memory in which looked up value will be stored
 905 * @param value_sz size in byte of value data memory; it has to match BPF map
 906 * definition's **value_size**. For per-CPU BPF maps value size has to be
 907 * a product of BPF map value size and number of possible CPUs in the system
 908 * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
 909 * per-CPU values value size has to be aligned up to closest 8 bytes for
 910 * alignment reasons, so expected size is: `round_up(value_size, 8)
 911 * * libbpf_num_possible_cpus()`.
 912 * @flags extra flags passed to kernel for this operation
 913 * @return 0, on success; negative error, otherwise
 914 *
 915 * **bpf_map__lookup_and_delete_elem()** is high-level equivalent of
 916 * **bpf_map_lookup_and_delete_elem()** API with added check for key and value size.
 917 */
 918LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
 919					       const void *key, size_t key_sz,
 920					       void *value, size_t value_sz, __u64 flags);
 921
 922/**
 923 * @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by
 924 * fetching next key that follows current key.
 925 * @param map BPF map to fetch next key from
 926 * @param cur_key pointer to memory containing bytes of current key or NULL to
 927 * fetch the first key
 928 * @param next_key pointer to memory to write next key into
 929 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
 930 * @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map;
 931 * negative error, otherwise
 932 *
 933 * **bpf_map__get_next_key()** is high-level equivalent of
 934 * **bpf_map_get_next_key()** API with added check for key size.
 935 */
 936LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map,
 937				     const void *cur_key, void *next_key, size_t key_sz);
 938
 939struct bpf_xdp_set_link_opts {
 940	size_t sz;
 941	int old_fd;
 942	size_t :0;
 943};
 944#define bpf_xdp_set_link_opts__last_field old_fd
 945
 946struct bpf_xdp_attach_opts {
 947	size_t sz;
 948	int old_prog_fd;
 949	size_t :0;
 950};
 951#define bpf_xdp_attach_opts__last_field old_prog_fd
 952
 953struct bpf_xdp_query_opts {
 954	size_t sz;
 955	__u32 prog_id;		/* output */
 956	__u32 drv_prog_id;	/* output */
 957	__u32 hw_prog_id;	/* output */
 958	__u32 skb_prog_id;	/* output */
 959	__u8 attach_mode;	/* output */
 
 
 960	size_t :0;
 961};
 962#define bpf_xdp_query_opts__last_field attach_mode
 963
 964LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,
 965			      const struct bpf_xdp_attach_opts *opts);
 966LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,
 967			      const struct bpf_xdp_attach_opts *opts);
 968LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);
 969LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);
 970
 971/* TC related API */
 972enum bpf_tc_attach_point {
 973	BPF_TC_INGRESS = 1 << 0,
 974	BPF_TC_EGRESS  = 1 << 1,
 975	BPF_TC_CUSTOM  = 1 << 2,
 976};
 977
 978#define BPF_TC_PARENT(a, b) 	\
 979	((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
 980
 981enum bpf_tc_flags {
 982	BPF_TC_F_REPLACE = 1 << 0,
 983};
 984
 985struct bpf_tc_hook {
 986	size_t sz;
 987	int ifindex;
 988	enum bpf_tc_attach_point attach_point;
 989	__u32 parent;
 990	size_t :0;
 991};
 992#define bpf_tc_hook__last_field parent
 993
 994struct bpf_tc_opts {
 995	size_t sz;
 996	int prog_fd;
 997	__u32 flags;
 998	__u32 prog_id;
 999	__u32 handle;
1000	__u32 priority;
1001	size_t :0;
1002};
1003#define bpf_tc_opts__last_field priority
1004
1005LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
1006LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
1007LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
1008			     struct bpf_tc_opts *opts);
1009LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
1010			     const struct bpf_tc_opts *opts);
1011LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
1012			    struct bpf_tc_opts *opts);
1013
1014/* Ring buffer APIs */
1015struct ring_buffer;
 
1016struct user_ring_buffer;
1017
1018typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
1019
1020struct ring_buffer_opts {
1021	size_t sz; /* size of this struct, for forward/backward compatiblity */
1022};
1023
1024#define ring_buffer_opts__last_field sz
1025
1026LIBBPF_API struct ring_buffer *
1027ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
1028		 const struct ring_buffer_opts *opts);
1029LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
1030LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
1031				ring_buffer_sample_fn sample_cb, void *ctx);
1032LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
1033LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
 
1034LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
1035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1036struct user_ring_buffer_opts {
1037	size_t sz; /* size of this struct, for forward/backward compatibility */
1038};
1039
1040#define user_ring_buffer_opts__last_field sz
1041
1042/* @brief **user_ring_buffer__new()** creates a new instance of a user ring
 
1043 * buffer.
1044 *
1045 * @param map_fd A file descriptor to a BPF_MAP_TYPE_USER_RINGBUF map.
1046 * @param opts Options for how the ring buffer should be created.
1047 * @return A user ring buffer on success; NULL and errno being set on a
1048 * failure.
1049 */
1050LIBBPF_API struct user_ring_buffer *
1051user_ring_buffer__new(int map_fd, const struct user_ring_buffer_opts *opts);
1052
1053/* @brief **user_ring_buffer__reserve()** reserves a pointer to a sample in the
 
1054 * user ring buffer.
1055 * @param rb A pointer to a user ring buffer.
1056 * @param size The size of the sample, in bytes.
1057 * @return A pointer to an 8-byte aligned reserved region of the user ring
1058 * buffer; NULL, and errno being set if a sample could not be reserved.
1059 *
1060 * This function is *not* thread safe, and callers must synchronize accessing
1061 * this function if there are multiple producers.  If a size is requested that
1062 * is larger than the size of the entire ring buffer, errno will be set to
1063 * E2BIG and NULL is returned. If the ring buffer could accommodate the size,
1064 * but currently does not have enough space, errno is set to ENOSPC and NULL is
1065 * returned.
1066 *
1067 * After initializing the sample, callers must invoke
1068 * **user_ring_buffer__submit()** to post the sample to the kernel. Otherwise,
1069 * the sample must be freed with **user_ring_buffer__discard()**.
1070 */
1071LIBBPF_API void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);
1072
1073/* @brief **user_ring_buffer__reserve_blocking()** reserves a record in the
 
1074 * ring buffer, possibly blocking for up to @timeout_ms until a sample becomes
1075 * available.
1076 * @param rb The user ring buffer.
1077 * @param size The size of the sample, in bytes.
1078 * @param timeout_ms The amount of time, in milliseconds, for which the caller
1079 * should block when waiting for a sample. -1 causes the caller to block
1080 * indefinitely.
1081 * @return A pointer to an 8-byte aligned reserved region of the user ring
1082 * buffer; NULL, and errno being set if a sample could not be reserved.
1083 *
1084 * This function is *not* thread safe, and callers must synchronize
1085 * accessing this function if there are multiple producers
1086 *
1087 * If **timeout_ms** is -1, the function will block indefinitely until a sample
1088 * becomes available. Otherwise, **timeout_ms** must be non-negative, or errno
1089 * is set to EINVAL, and NULL is returned. If **timeout_ms** is 0, no blocking
1090 * will occur and the function will return immediately after attempting to
1091 * reserve a sample.
1092 *
1093 * If **size** is larger than the size of the entire ring buffer, errno is set
1094 * to E2BIG and NULL is returned. If the ring buffer could accommodate
1095 * **size**, but currently does not have enough space, the caller will block
1096 * until at most **timeout_ms** has elapsed. If insufficient space is available
1097 * at that time, errno is set to ENOSPC, and NULL is returned.
1098 *
1099 * The kernel guarantees that it will wake up this thread to check if
1100 * sufficient space is available in the ring buffer at least once per
1101 * invocation of the **bpf_ringbuf_drain()** helper function, provided that at
1102 * least one sample is consumed, and the BPF program did not invoke the
1103 * function with BPF_RB_NO_WAKEUP. A wakeup may occur sooner than that, but the
1104 * kernel does not guarantee this. If the helper function is invoked with
1105 * BPF_RB_FORCE_WAKEUP, a wakeup event will be sent even if no sample is
1106 * consumed.
1107 *
1108 * When a sample of size **size** is found within **timeout_ms**, a pointer to
1109 * the sample is returned. After initializing the sample, callers must invoke
1110 * **user_ring_buffer__submit()** to post the sample to the ring buffer.
1111 * Otherwise, the sample must be freed with **user_ring_buffer__discard()**.
1112 */
1113LIBBPF_API void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,
1114						    __u32 size,
1115						    int timeout_ms);
1116
1117/* @brief **user_ring_buffer__submit()** submits a previously reserved sample
 
1118 * into the ring buffer.
1119 * @param rb The user ring buffer.
1120 * @param sample A reserved sample.
1121 *
1122 * It is not necessary to synchronize amongst multiple producers when invoking
1123 * this function.
1124 */
1125LIBBPF_API void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample);
1126
1127/* @brief **user_ring_buffer__discard()** discards a previously reserved sample.
 
1128 * @param rb The user ring buffer.
1129 * @param sample A reserved sample.
1130 *
1131 * It is not necessary to synchronize amongst multiple producers when invoking
1132 * this function.
1133 */
1134LIBBPF_API void user_ring_buffer__discard(struct user_ring_buffer *rb, void *sample);
1135
1136/* @brief **user_ring_buffer__free()** frees a ring buffer that was previously
 
1137 * created with **user_ring_buffer__new()**.
1138 * @param rb The user ring buffer being freed.
1139 */
1140LIBBPF_API void user_ring_buffer__free(struct user_ring_buffer *rb);
1141
1142/* Perf buffer APIs */
1143struct perf_buffer;
1144
1145typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
1146				      void *data, __u32 size);
1147typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
1148
1149/* common use perf buffer options */
1150struct perf_buffer_opts {
1151	size_t sz;
 
 
1152};
1153#define perf_buffer_opts__last_field sz
1154
1155/**
1156 * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
1157 * BPF_PERF_EVENT_ARRAY map
1158 * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
1159 * code to send data over to user-space
1160 * @param page_cnt number of memory pages allocated for each per-CPU buffer
1161 * @param sample_cb function called on each received data record
1162 * @param lost_cb function called when record loss has occurred
1163 * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
1164 * @return a new instance of struct perf_buffer on success, NULL on error with
1165 * *errno* containing an error code
1166 */
1167LIBBPF_API struct perf_buffer *
1168perf_buffer__new(int map_fd, size_t page_cnt,
1169		 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
1170		 const struct perf_buffer_opts *opts);
1171
1172enum bpf_perf_event_ret {
1173	LIBBPF_PERF_EVENT_DONE	= 0,
1174	LIBBPF_PERF_EVENT_ERROR	= -1,
1175	LIBBPF_PERF_EVENT_CONT	= -2,
1176};
1177
1178struct perf_event_header;
1179
1180typedef enum bpf_perf_event_ret
1181(*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
1182
1183/* raw perf buffer options, giving most power and control */
1184struct perf_buffer_raw_opts {
1185	size_t sz;
1186	long :0;
1187	long :0;
1188	/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
1189	 * max_entries of given PERF_EVENT_ARRAY map)
1190	 */
1191	int cpu_cnt;
1192	/* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
1193	int *cpus;
1194	/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
1195	int *map_keys;
1196};
1197#define perf_buffer_raw_opts__last_field map_keys
1198
1199struct perf_event_attr;
1200
1201LIBBPF_API struct perf_buffer *
1202perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1203		     perf_buffer_event_fn event_cb, void *ctx,
1204		     const struct perf_buffer_raw_opts *opts);
1205
1206LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
1207LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
1208LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
1209LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
1210LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
1211LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
1212LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
1213/**
1214 * @brief **perf_buffer__buffer()** returns the per-cpu raw mmap()'ed underlying
1215 * memory region of the ring buffer.
1216 * This ring buffer can be used to implement a custom events consumer.
1217 * The ring buffer starts with the *struct perf_event_mmap_page*, which
1218 * holds the ring buffer managment fields, when accessing the header
1219 * structure it's important to be SMP aware.
1220 * You can refer to *perf_event_read_simple* for a simple example.
1221 * @param pb the perf buffer structure
1222 * @param buf_idx the buffer index to retreive
1223 * @param buf (out) gets the base pointer of the mmap()'ed memory
1224 * @param buf_size (out) gets the size of the mmap()'ed region
1225 * @return 0 on success, negative error code for failure
1226 */
1227LIBBPF_API int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf,
1228				   size_t *buf_size);
1229
1230struct bpf_prog_linfo;
1231struct bpf_prog_info;
1232
1233LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
1234LIBBPF_API struct bpf_prog_linfo *
1235bpf_prog_linfo__new(const struct bpf_prog_info *info);
1236LIBBPF_API const struct bpf_line_info *
1237bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
1238				__u64 addr, __u32 func_idx, __u32 nr_skip);
1239LIBBPF_API const struct bpf_line_info *
1240bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
1241		      __u32 insn_off, __u32 nr_skip);
1242
1243/*
1244 * Probe for supported system features
1245 *
1246 * Note that running many of these probes in a short amount of time can cause
1247 * the kernel to reach the maximal size of lockable memory allowed for the
1248 * user, causing subsequent probes to fail. In this case, the caller may want
1249 * to adjust that limit with setrlimit().
1250 */
1251
1252/**
1253 * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports
1254 * BPF programs of a given type.
1255 * @param prog_type BPF program type to detect kernel support for
1256 * @param opts reserved for future extensibility, should be NULL
1257 * @return 1, if given program type is supported; 0, if given program type is
1258 * not supported; negative error code if feature detection failed or can't be
1259 * performed
1260 *
1261 * Make sure the process has required set of CAP_* permissions (or runs as
1262 * root) when performing feature checking.
1263 */
1264LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);
1265/**
1266 * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports
1267 * BPF maps of a given type.
1268 * @param map_type BPF map type to detect kernel support for
1269 * @param opts reserved for future extensibility, should be NULL
1270 * @return 1, if given map type is supported; 0, if given map type is
1271 * not supported; negative error code if feature detection failed or can't be
1272 * performed
1273 *
1274 * Make sure the process has required set of CAP_* permissions (or runs as
1275 * root) when performing feature checking.
1276 */
1277LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);
1278/**
1279 * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the
1280 * use of a given BPF helper from specified BPF program type.
1281 * @param prog_type BPF program type used to check the support of BPF helper
1282 * @param helper_id BPF helper ID (enum bpf_func_id) to check support for
1283 * @param opts reserved for future extensibility, should be NULL
1284 * @return 1, if given combination of program type and helper is supported; 0,
1285 * if the combination is not supported; negative error code if feature
1286 * detection for provided input arguments failed or can't be performed
1287 *
1288 * Make sure the process has required set of CAP_* permissions (or runs as
1289 * root) when performing feature checking.
1290 */
1291LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
1292				       enum bpf_func_id helper_id, const void *opts);
1293
1294/**
1295 * @brief **libbpf_num_possible_cpus()** is a helper function to get the
1296 * number of possible CPUs that the host kernel supports and expects.
1297 * @return number of possible CPUs; or error code on failure
1298 *
1299 * Example usage:
1300 *
1301 *     int ncpus = libbpf_num_possible_cpus();
1302 *     if (ncpus < 0) {
1303 *          // error handling
1304 *     }
1305 *     long values[ncpus];
1306 *     bpf_map_lookup_elem(per_cpu_map_fd, key, values);
1307 */
1308LIBBPF_API int libbpf_num_possible_cpus(void);
1309
1310struct bpf_map_skeleton {
1311	const char *name;
1312	struct bpf_map **map;
1313	void **mmaped;
 
1314};
1315
1316struct bpf_prog_skeleton {
1317	const char *name;
1318	struct bpf_program **prog;
1319	struct bpf_link **link;
1320};
1321
1322struct bpf_object_skeleton {
1323	size_t sz; /* size of this struct, for forward/backward compatibility */
1324
1325	const char *name;
1326	const void *data;
1327	size_t data_sz;
1328
1329	struct bpf_object **obj;
1330
1331	int map_cnt;
1332	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1333	struct bpf_map_skeleton *maps;
1334
1335	int prog_cnt;
1336	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1337	struct bpf_prog_skeleton *progs;
1338};
1339
1340LIBBPF_API int
1341bpf_object__open_skeleton(struct bpf_object_skeleton *s,
1342			  const struct bpf_object_open_opts *opts);
1343LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
1344LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
1345LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
1346LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
1347
1348struct bpf_var_skeleton {
1349	const char *name;
1350	struct bpf_map **map;
1351	void **addr;
1352};
1353
1354struct bpf_object_subskeleton {
1355	size_t sz; /* size of this struct, for forward/backward compatibility */
1356
1357	const struct bpf_object *obj;
1358
1359	int map_cnt;
1360	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1361	struct bpf_map_skeleton *maps;
1362
1363	int prog_cnt;
1364	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1365	struct bpf_prog_skeleton *progs;
1366
1367	int var_cnt;
1368	int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */
1369	struct bpf_var_skeleton *vars;
1370};
1371
1372LIBBPF_API int
1373bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);
1374LIBBPF_API void
1375bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);
1376
1377struct gen_loader_opts {
1378	size_t sz; /* size of this struct, for forward/backward compatiblity */
1379	const char *data;
1380	const char *insns;
1381	__u32 data_sz;
1382	__u32 insns_sz;
1383};
1384
1385#define gen_loader_opts__last_field insns_sz
1386LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
1387				      struct gen_loader_opts *opts);
1388
1389enum libbpf_tristate {
1390	TRI_NO = 0,
1391	TRI_YES = 1,
1392	TRI_MODULE = 2,
1393};
1394
1395struct bpf_linker_opts {
1396	/* size of this struct, for forward/backward compatiblity */
1397	size_t sz;
1398};
1399#define bpf_linker_opts__last_field sz
1400
1401struct bpf_linker_file_opts {
1402	/* size of this struct, for forward/backward compatiblity */
1403	size_t sz;
1404};
1405#define bpf_linker_file_opts__last_field sz
1406
1407struct bpf_linker;
1408
1409LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
1410LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
1411				    const char *filename,
1412				    const struct bpf_linker_file_opts *opts);
1413LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
1414LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
1415
1416/*
1417 * Custom handling of BPF program's SEC() definitions
1418 */
1419
1420struct bpf_prog_load_opts; /* defined in bpf.h */
1421
1422/* Called during bpf_object__open() for each recognized BPF program. Callback
1423 * can use various bpf_program__set_*() setters to adjust whatever properties
1424 * are necessary.
1425 */
1426typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);
1427
1428/* Called right before libbpf performs bpf_prog_load() to load BPF program
1429 * into the kernel. Callback can adjust opts as necessary.
1430 */
1431typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,
1432					     struct bpf_prog_load_opts *opts, long cookie);
1433
1434/* Called during skeleton attach or through bpf_program__attach(). If
1435 * auto-attach is not supported, callback should return 0 and set link to
1436 * NULL (it's not considered an error during skeleton attach, but it will be
1437 * an error for bpf_program__attach() calls). On error, error should be
1438 * returned directly and link set to NULL. On success, return 0 and set link
1439 * to a valid struct bpf_link.
1440 */
1441typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,
1442				       struct bpf_link **link);
1443
1444struct libbpf_prog_handler_opts {
1445	/* size of this struct, for forward/backward compatiblity */
1446	size_t sz;
1447	/* User-provided value that is passed to prog_setup_fn,
1448	 * prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to
1449	 * register one set of callbacks for multiple SEC() definitions and
1450	 * still be able to distinguish them, if necessary. For example,
1451	 * libbpf itself is using this to pass necessary flags (e.g.,
1452	 * sleepable flag) to a common internal SEC() handler.
1453	 */
1454	long cookie;
1455	/* BPF program initialization callback (see libbpf_prog_setup_fn_t).
1456	 * Callback is optional, pass NULL if it's not necessary.
1457	 */
1458	libbpf_prog_setup_fn_t prog_setup_fn;
1459	/* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).
1460	 * Callback is optional, pass NULL if it's not necessary.
1461	 */
1462	libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
1463	/* BPF program attach callback (see libbpf_prog_attach_fn_t).
1464	 * Callback is optional, pass NULL if it's not necessary.
1465	 */
1466	libbpf_prog_attach_fn_t prog_attach_fn;
1467};
1468#define libbpf_prog_handler_opts__last_field prog_attach_fn
1469
1470/**
1471 * @brief **libbpf_register_prog_handler()** registers a custom BPF program
1472 * SEC() handler.
1473 * @param sec section prefix for which custom handler is registered
1474 * @param prog_type BPF program type associated with specified section
1475 * @param exp_attach_type Expected BPF attach type associated with specified section
1476 * @param opts optional cookie, callbacks, and other extra options
1477 * @return Non-negative handler ID is returned on success. This handler ID has
1478 * to be passed to *libbpf_unregister_prog_handler()* to unregister such
1479 * custom handler. Negative error code is returned on error.
1480 *
1481 * *sec* defines which SEC() definitions are handled by this custom handler
1482 * registration. *sec* can have few different forms:
1483 *   - if *sec* is just a plain string (e.g., "abc"), it will match only
1484 *   SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result
1485 *   in an error;
1486 *   - if *sec* is of the form "abc/", proper SEC() form is
1487 *   SEC("abc/something"), where acceptable "something" should be checked by
1488 *   *prog_init_fn* callback, if there are additional restrictions;
1489 *   - if *sec* is of the form "abc+", it will successfully match both
1490 *   SEC("abc") and SEC("abc/whatever") forms;
1491 *   - if *sec* is NULL, custom handler is registered for any BPF program that
1492 *   doesn't match any of the registered (custom or libbpf's own) SEC()
1493 *   handlers. There could be only one such generic custom handler registered
1494 *   at any given time.
1495 *
1496 * All custom handlers (except the one with *sec* == NULL) are processed
1497 * before libbpf's own SEC() handlers. It is allowed to "override" libbpf's
1498 * SEC() handlers by registering custom ones for the same section prefix
1499 * (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")
1500 * handler).
1501 *
1502 * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1503 * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1504 * to ensure synchronization if there is a risk of running this API from
1505 * multiple threads simultaneously.
1506 */
1507LIBBPF_API int libbpf_register_prog_handler(const char *sec,
1508					    enum bpf_prog_type prog_type,
1509					    enum bpf_attach_type exp_attach_type,
1510					    const struct libbpf_prog_handler_opts *opts);
1511/**
1512 * @brief *libbpf_unregister_prog_handler()* unregisters previously registered
1513 * custom BPF program SEC() handler.
1514 * @param handler_id handler ID returned by *libbpf_register_prog_handler()*
1515 * after successful registration
1516 * @return 0 on success, negative error code if handler isn't found
1517 *
1518 * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1519 * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1520 * to ensure synchronization if there is a risk of running this API from
1521 * multiple threads simultaneously.
1522 */
1523LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);
1524
1525#ifdef __cplusplus
1526} /* extern "C" */
1527#endif
1528
1529#endif /* __LIBBPF_LIBBPF_H */
v6.13.7
   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#include "libbpf_common.h"
  21#include "libbpf_legacy.h"
  22
  23#ifdef __cplusplus
  24extern "C" {
  25#endif
  26
  27LIBBPF_API __u32 libbpf_major_version(void);
  28LIBBPF_API __u32 libbpf_minor_version(void);
  29LIBBPF_API const char *libbpf_version_string(void);
  30
  31enum libbpf_errno {
  32	__LIBBPF_ERRNO__START = 4000,
  33
  34	/* Something wrong in libelf */
  35	LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
  36	LIBBPF_ERRNO__FORMAT,	/* BPF object format invalid */
  37	LIBBPF_ERRNO__KVERSION,	/* Incorrect or no 'version' section */
  38	LIBBPF_ERRNO__ENDIAN,	/* Endian mismatch */
  39	LIBBPF_ERRNO__INTERNAL,	/* Internal error in libbpf */
  40	LIBBPF_ERRNO__RELOC,	/* Relocation failed */
  41	LIBBPF_ERRNO__LOAD,	/* Load program failure for unknown reason */
  42	LIBBPF_ERRNO__VERIFY,	/* Kernel verifier blocks program loading */
  43	LIBBPF_ERRNO__PROG2BIG,	/* Program too big */
  44	LIBBPF_ERRNO__KVER,	/* Incorrect kernel version */
  45	LIBBPF_ERRNO__PROGTYPE,	/* Kernel doesn't support this program type */
  46	LIBBPF_ERRNO__WRNGPID,	/* Wrong pid in netlink message */
  47	LIBBPF_ERRNO__INVSEQ,	/* Invalid netlink sequence */
  48	LIBBPF_ERRNO__NLPARSE,	/* netlink parsing error */
  49	__LIBBPF_ERRNO__END,
  50};
  51
  52LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
  53
  54/**
  55 * @brief **libbpf_bpf_attach_type_str()** converts the provided attach type
  56 * value into a textual representation.
  57 * @param t The attach type.
  58 * @return Pointer to a static string identifying the attach type. NULL is
  59 * returned for unknown **bpf_attach_type** values.
  60 */
  61LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t);
  62
  63/**
  64 * @brief **libbpf_bpf_link_type_str()** converts the provided link type value
  65 * into a textual representation.
  66 * @param t The link type.
  67 * @return Pointer to a static string identifying the link type. NULL is
  68 * returned for unknown **bpf_link_type** values.
  69 */
  70LIBBPF_API const char *libbpf_bpf_link_type_str(enum bpf_link_type t);
  71
  72/**
  73 * @brief **libbpf_bpf_map_type_str()** converts the provided map type value
  74 * into a textual representation.
  75 * @param t The map type.
  76 * @return Pointer to a static string identifying the map type. NULL is
  77 * returned for unknown **bpf_map_type** values.
  78 */
  79LIBBPF_API const char *libbpf_bpf_map_type_str(enum bpf_map_type t);
  80
  81/**
  82 * @brief **libbpf_bpf_prog_type_str()** converts the provided program type
  83 * value into a textual representation.
  84 * @param t The program type.
  85 * @return Pointer to a static string identifying the program type. NULL is
  86 * returned for unknown **bpf_prog_type** values.
  87 */
  88LIBBPF_API const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t);
  89
  90enum libbpf_print_level {
  91        LIBBPF_WARN,
  92        LIBBPF_INFO,
  93        LIBBPF_DEBUG,
  94};
  95
  96typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
  97				 const char *, va_list ap);
  98
  99/**
 100 * @brief **libbpf_set_print()** sets user-provided log callback function to
 101 * be used for libbpf warnings and informational messages. If the user callback
 102 * is not set, messages are logged to stderr by default. The verbosity of these
 103 * messages can be controlled by setting the environment variable
 104 * LIBBPF_LOG_LEVEL to either warn, info, or debug.
 105 * @param fn The log print function. If NULL, libbpf won't print anything.
 106 * @return Pointer to old print function.
 107 *
 108 * This function is thread-safe.
 109 */
 110LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
 111
 112/* Hide internal to user */
 113struct bpf_object;
 114
 115struct bpf_object_open_opts {
 116	/* size of this struct, for forward/backward compatibility */
 117	size_t sz;
 118	/* object name override, if provided:
 119	 * - for object open from file, this will override setting object
 120	 *   name from file path's base name;
 121	 * - for object open from memory buffer, this will specify an object
 122	 *   name and will override default "<addr>-<buf-size>" name;
 123	 */
 124	const char *object_name;
 125	/* parse map definitions non-strictly, allowing extra attributes/data */
 126	bool relaxed_maps;
 127	/* maps that set the 'pinning' attribute in their definition will have
 128	 * their pin_path attribute set to a file in this directory, and be
 129	 * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
 130	 */
 131	const char *pin_root_path;
 132
 133	__u32 :32; /* stub out now removed attach_prog_fd */
 134
 135	/* Additional kernel config content that augments and overrides
 136	 * system Kconfig for CONFIG_xxx externs.
 137	 */
 138	const char *kconfig;
 139	/* Path to the custom BTF to be used for BPF CO-RE relocations.
 140	 * This custom BTF completely replaces the use of vmlinux BTF
 141	 * for the purpose of CO-RE relocations.
 142	 * NOTE: any other BPF feature (e.g., fentry/fexit programs,
 143	 * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.
 144	 */
 145	const char *btf_custom_path;
 146	/* Pointer to a buffer for storing kernel logs for applicable BPF
 147	 * commands. Valid kernel_log_size has to be specified as well and are
 148	 * passed-through to bpf() syscall. Keep in mind that kernel might
 149	 * fail operation with -ENOSPC error if provided buffer is too small
 150	 * to contain entire log output.
 151	 * See the comment below for kernel_log_level for interaction between
 152	 * log_buf and log_level settings.
 153	 *
 154	 * If specified, this log buffer will be passed for:
 155	 *   - each BPF progral load (BPF_PROG_LOAD) attempt, unless overridden
 156	 *     with bpf_program__set_log() on per-program level, to get
 157	 *     BPF verifier log output.
 158	 *   - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get
 159	 *     BTF sanity checking log.
 160	 *
 161	 * Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite
 162	 * previous contents, so if you need more fine-grained control, set
 163	 * per-program buffer with bpf_program__set_log_buf() to preserve each
 164	 * individual program's verification log. Keep using kernel_log_buf
 165	 * for BTF verification log, if necessary.
 166	 */
 167	char *kernel_log_buf;
 168	size_t kernel_log_size;
 169	/*
 170	 * Log level can be set independently from log buffer. Log_level=0
 171	 * means that libbpf will attempt loading BTF or program without any
 172	 * logging requested, but will retry with either its own or custom log
 173	 * buffer, if provided, and log_level=1 on any error.
 174	 * And vice versa, setting log_level>0 will request BTF or prog
 175	 * loading with verbose log from the first attempt (and as such also
 176	 * for successfully loaded BTF or program), and the actual log buffer
 177	 * could be either libbpf's own auto-allocated log buffer, if
 178	 * kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.
 179	 * If user didn't provide custom log buffer, libbpf will emit captured
 180	 * logs through its print callback.
 181	 */
 182	__u32 kernel_log_level;
 183	/* Path to BPF FS mount point to derive BPF token from.
 184	 *
 185	 * Created BPF token will be used for all bpf() syscall operations
 186	 * that accept BPF token (e.g., map creation, BTF and program loads,
 187	 * etc) automatically within instantiated BPF object.
 188	 *
 189	 * If bpf_token_path is not specified, libbpf will consult
 190	 * LIBBPF_BPF_TOKEN_PATH environment variable. If set, it will be
 191	 * taken as a value of bpf_token_path option and will force libbpf to
 192	 * either create BPF token from provided custom BPF FS path, or will
 193	 * disable implicit BPF token creation, if envvar value is an empty
 194	 * string. bpf_token_path overrides LIBBPF_BPF_TOKEN_PATH, if both are
 195	 * set at the same time.
 196	 *
 197	 * Setting bpf_token_path option to empty string disables libbpf's
 198	 * automatic attempt to create BPF token from default BPF FS mount
 199	 * point (/sys/fs/bpf), in case this default behavior is undesirable.
 200	 */
 201	const char *bpf_token_path;
 202
 203	size_t :0;
 204};
 205#define bpf_object_open_opts__last_field bpf_token_path
 206
 207/**
 208 * @brief **bpf_object__open()** creates a bpf_object by opening
 209 * the BPF ELF object file pointed to by the passed path and loading it
 210 * into memory.
 211 * @param path BPF object file path.
 212 * @return pointer to the new bpf_object; or NULL is returned on error,
 213 * error code is stored in errno
 214 */
 215LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
 216
 217/**
 218 * @brief **bpf_object__open_file()** creates a bpf_object by opening
 219 * the BPF ELF object file pointed to by the passed path and loading it
 220 * into memory.
 221 * @param path BPF object file path
 222 * @param opts options for how to load the bpf object, this parameter is
 223 * optional and can be set to NULL
 224 * @return pointer to the new bpf_object; or NULL is returned on error,
 225 * error code is stored in errno
 226 */
 227LIBBPF_API struct bpf_object *
 228bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
 229
 230/**
 231 * @brief **bpf_object__open_mem()** creates a bpf_object by reading
 232 * the BPF objects raw bytes from a memory buffer containing a valid
 233 * BPF ELF object file.
 234 * @param obj_buf pointer to the buffer containing ELF file bytes
 235 * @param obj_buf_sz number of bytes in the buffer
 236 * @param opts options for how to load the bpf object
 237 * @return pointer to the new bpf_object; or NULL is returned on error,
 238 * error code is stored in errno
 239 */
 240LIBBPF_API struct bpf_object *
 241bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
 242		     const struct bpf_object_open_opts *opts);
 243
 244/**
 245 * @brief **bpf_object__load()** loads BPF object into kernel.
 246 * @param obj Pointer to a valid BPF object instance returned by
 247 * **bpf_object__open*()** APIs
 248 * @return 0, on success; negative error code, otherwise, error code is
 249 * stored in errno
 250 */
 251LIBBPF_API int bpf_object__load(struct bpf_object *obj);
 252
 253/**
 254 * @brief **bpf_object__close()** closes a BPF object and releases all
 255 * resources.
 256 * @param obj Pointer to a valid BPF object
 257 */
 258LIBBPF_API void bpf_object__close(struct bpf_object *obj);
 259
 260/**
 261 * @brief **bpf_object__pin_maps()** pins each map contained within
 262 * the BPF object at the passed directory.
 263 * @param obj Pointer to a valid BPF object
 264 * @param path A directory where maps should be pinned.
 265 * @return 0, on success; negative error code, otherwise
 266 *
 267 * If `path` is NULL `bpf_map__pin` (which is being used on each map)
 268 * will use the pin_path attribute of each map. In this case, maps that
 269 * don't have a pin_path set will be ignored.
 270 */
 271LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
 272
 273/**
 274 * @brief **bpf_object__unpin_maps()** unpins each map contained within
 275 * the BPF object found in the passed directory.
 276 * @param obj Pointer to a valid BPF object
 277 * @param path A directory where pinned maps should be searched for.
 278 * @return 0, on success; negative error code, otherwise
 279 *
 280 * If `path` is NULL `bpf_map__unpin` (which is being used on each map)
 281 * will use the pin_path attribute of each map. In this case, maps that
 282 * don't have a pin_path set will be ignored.
 283 */
 284LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
 285				      const char *path);
 286LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
 287					const char *path);
 288LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
 289					  const char *path);
 290LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
 291LIBBPF_API int bpf_object__unpin(struct bpf_object *object, const char *path);
 292
 293LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
 294LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
 295LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);
 296
 297/**
 298 * @brief **bpf_object__token_fd** is an accessor for BPF token FD associated
 299 * with BPF object.
 300 * @param obj Pointer to a valid BPF object
 301 * @return BPF token FD or -1, if it wasn't set
 302 */
 303LIBBPF_API int bpf_object__token_fd(const struct bpf_object *obj);
 304
 305struct btf;
 306LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
 307LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
 308
 309LIBBPF_API struct bpf_program *
 310bpf_object__find_program_by_name(const struct bpf_object *obj,
 311				 const char *name);
 312
 313LIBBPF_API int
 314libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
 315			 enum bpf_attach_type *expected_attach_type);
 316LIBBPF_API int libbpf_attach_type_by_name(const char *name,
 317					  enum bpf_attach_type *attach_type);
 318LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
 319					  enum bpf_attach_type attach_type);
 320
 321/* Accessors of bpf_program */
 322struct bpf_program;
 323
 324LIBBPF_API struct bpf_program *
 325bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);
 326
 327#define bpf_object__for_each_program(pos, obj)			\
 328	for ((pos) = bpf_object__next_program((obj), NULL);	\
 329	     (pos) != NULL;					\
 330	     (pos) = bpf_object__next_program((obj), (pos)))
 331
 332LIBBPF_API struct bpf_program *
 333bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);
 334
 335LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
 336					 __u32 ifindex);
 337
 338LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
 339LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
 340LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
 341LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
 342LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog);
 343LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach);
 344
 345struct bpf_insn;
 346
 347/**
 348 * @brief **bpf_program__insns()** gives read-only access to BPF program's
 349 * underlying BPF instructions.
 350 * @param prog BPF program for which to return instructions
 351 * @return a pointer to an array of BPF instructions that belong to the
 352 * specified BPF program
 353 *
 354 * Returned pointer is always valid and not NULL. Number of `struct bpf_insn`
 355 * pointed to can be fetched using **bpf_program__insn_cnt()** API.
 356 *
 357 * Keep in mind, libbpf can modify and append/delete BPF program's
 358 * instructions as it processes BPF object file and prepares everything for
 359 * uploading into the kernel. So depending on the point in BPF object
 360 * lifetime, **bpf_program__insns()** can return different sets of
 361 * instructions. As an example, during BPF object load phase BPF program
 362 * instructions will be CO-RE-relocated, BPF subprograms instructions will be
 363 * appended, ldimm64 instructions will have FDs embedded, etc. So instructions
 364 * returned before **bpf_object__load()** and after it might be quite
 365 * different.
 366 */
 367LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
 368
 369/**
 370 * @brief **bpf_program__set_insns()** can set BPF program's underlying
 371 * BPF instructions.
 372 *
 373 * WARNING: This is a very advanced libbpf API and users need to know
 374 * what they are doing. This should be used from prog_prepare_load_fn
 375 * callback only.
 376 *
 377 * @param prog BPF program for which to return instructions
 378 * @param new_insns a pointer to an array of BPF instructions
 379 * @param new_insn_cnt number of `struct bpf_insn`'s that form
 380 * specified BPF program
 381 * @return 0, on success; negative error code, otherwise
 382 */
 383LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog,
 384				      struct bpf_insn *new_insns, size_t new_insn_cnt);
 385
 386/**
 387 * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
 388 * that form specified BPF program.
 389 * @param prog BPF program for which to return number of BPF instructions
 390 *
 391 * See **bpf_program__insns()** documentation for notes on how libbpf can
 392 * change instructions and their count during different phases of
 393 * **bpf_object** lifetime.
 394 */
 395LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);
 396
 397LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
 398
 399/**
 400 * @brief **bpf_program__pin()** pins the BPF program to a file
 401 * in the BPF FS specified by a path. This increments the programs
 402 * reference count, allowing it to stay loaded after the process
 403 * which loaded it has exited.
 404 *
 405 * @param prog BPF program to pin, must already be loaded
 406 * @param path file path in a BPF file system
 407 * @return 0, on success; negative error code, otherwise
 408 */
 409LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
 410
 411/**
 412 * @brief **bpf_program__unpin()** unpins the BPF program from a file
 413 * in the BPFFS specified by a path. This decrements the programs
 414 * reference count.
 415 *
 416 * The file pinning the BPF program can also be unlinked by a different
 417 * process in which case this function will return an error.
 418 *
 419 * @param prog BPF program to unpin
 420 * @param path file path to the pin in a BPF file system
 421 * @return 0, on success; negative error code, otherwise
 422 */
 423LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
 424LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
 425
 426struct bpf_link;
 427
 428LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
 429LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
 430LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
 431/**
 432 * @brief **bpf_link__pin()** pins the BPF link to a file
 433 * in the BPF FS specified by a path. This increments the links
 434 * reference count, allowing it to stay loaded after the process
 435 * which loaded it has exited.
 436 *
 437 * @param link BPF link to pin, must already be loaded
 438 * @param path file path in a BPF file system
 439 * @return 0, on success; negative error code, otherwise
 440 */
 441
 442LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
 443
 444/**
 445 * @brief **bpf_link__unpin()** unpins the BPF link from a file
 446 * in the BPFFS specified by a path. This decrements the links
 447 * reference count.
 448 *
 449 * The file pinning the BPF link can also be unlinked by a different
 450 * process in which case this function will return an error.
 451 *
 452 * @param prog BPF program to unpin
 453 * @param path file path to the pin in a BPF file system
 454 * @return 0, on success; negative error code, otherwise
 455 */
 456LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
 457LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
 458					struct bpf_program *prog);
 459LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
 460LIBBPF_API int bpf_link__detach(struct bpf_link *link);
 461LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
 462
 463/**
 464 * @brief **bpf_program__attach()** is a generic function for attaching
 465 * a BPF program based on auto-detection of program type, attach type,
 466 * and extra parameters, where applicable.
 467 *
 468 * @param prog BPF program to attach
 469 * @return Reference to the newly created BPF link; or NULL is returned on error,
 470 * error code is stored in errno
 471 *
 472 * This is supported for:
 473 *   - kprobe/kretprobe (depends on SEC() definition)
 474 *   - uprobe/uretprobe (depends on SEC() definition)
 475 *   - tracepoint
 476 *   - raw tracepoint
 477 *   - tracing programs (typed raw TP/fentry/fexit/fmod_ret)
 478 */
 479LIBBPF_API struct bpf_link *
 480bpf_program__attach(const struct bpf_program *prog);
 481
 482struct bpf_perf_event_opts {
 483	/* size of this struct, for forward/backward compatibility */
 484	size_t sz;
 485	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
 486	__u64 bpf_cookie;
 487	/* don't use BPF link when attach BPF program */
 488	bool force_ioctl_attach;
 489	size_t :0;
 490};
 491#define bpf_perf_event_opts__last_field force_ioctl_attach
 492
 493LIBBPF_API struct bpf_link *
 494bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
 495
 496LIBBPF_API struct bpf_link *
 497bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
 498				    const struct bpf_perf_event_opts *opts);
 499
 500/**
 501 * enum probe_attach_mode - the mode to attach kprobe/uprobe
 502 *
 503 * force libbpf to attach kprobe/uprobe in specific mode, -ENOTSUP will
 504 * be returned if it is not supported by the kernel.
 505 */
 506enum probe_attach_mode {
 507	/* attach probe in latest supported mode by kernel */
 508	PROBE_ATTACH_MODE_DEFAULT = 0,
 509	/* attach probe in legacy mode, using debugfs/tracefs */
 510	PROBE_ATTACH_MODE_LEGACY,
 511	/* create perf event with perf_event_open() syscall */
 512	PROBE_ATTACH_MODE_PERF,
 513	/* attach probe with BPF link */
 514	PROBE_ATTACH_MODE_LINK,
 515};
 516
 517struct bpf_kprobe_opts {
 518	/* size of this struct, for forward/backward compatibility */
 519	size_t sz;
 520	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
 521	__u64 bpf_cookie;
 522	/* function's offset to install kprobe to */
 523	size_t offset;
 524	/* kprobe is return probe */
 525	bool retprobe;
 526	/* kprobe attach mode */
 527	enum probe_attach_mode attach_mode;
 528	size_t :0;
 529};
 530#define bpf_kprobe_opts__last_field attach_mode
 531
 532LIBBPF_API struct bpf_link *
 533bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
 534			   const char *func_name);
 535LIBBPF_API struct bpf_link *
 536bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
 537                                const char *func_name,
 538                                const struct bpf_kprobe_opts *opts);
 539
 540struct bpf_kprobe_multi_opts {
 541	/* size of this struct, for forward/backward compatibility */
 542	size_t sz;
 543	/* array of function symbols to attach */
 544	const char **syms;
 545	/* array of function addresses to attach */
 546	const unsigned long *addrs;
 547	/* array of user-provided values fetchable through bpf_get_attach_cookie */
 548	const __u64 *cookies;
 549	/* number of elements in syms/addrs/cookies arrays */
 550	size_t cnt;
 551	/* create return kprobes */
 552	bool retprobe;
 553	/* create session kprobes */
 554	bool session;
 555	size_t :0;
 556};
 557
 558#define bpf_kprobe_multi_opts__last_field session
 559
 560LIBBPF_API struct bpf_link *
 561bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
 562				      const char *pattern,
 563				      const struct bpf_kprobe_multi_opts *opts);
 564
 565struct bpf_uprobe_multi_opts {
 566	/* size of this struct, for forward/backward compatibility */
 567	size_t sz;
 568	/* array of function symbols to attach to */
 569	const char **syms;
 570	/* array of function addresses to attach to */
 571	const unsigned long *offsets;
 572	/* optional, array of associated ref counter offsets */
 573	const unsigned long *ref_ctr_offsets;
 574	/* optional, array of associated BPF cookies */
 575	const __u64 *cookies;
 576	/* number of elements in syms/addrs/cookies arrays */
 577	size_t cnt;
 578	/* create return uprobes */
 579	bool retprobe;
 580	/* create session kprobes */
 581	bool session;
 582	size_t :0;
 583};
 584
 585#define bpf_uprobe_multi_opts__last_field session
 586
 587/**
 588 * @brief **bpf_program__attach_uprobe_multi()** attaches a BPF program
 589 * to multiple uprobes with uprobe_multi link.
 590 *
 591 * User can specify 2 mutually exclusive set of inputs:
 592 *
 593 *   1) use only path/func_pattern/pid arguments
 594 *
 595 *   2) use path/pid with allowed combinations of
 596 *      syms/offsets/ref_ctr_offsets/cookies/cnt
 597 *
 598 *      - syms and offsets are mutually exclusive
 599 *      - ref_ctr_offsets and cookies are optional
 600 *
 601 *
 602 * @param prog BPF program to attach
 603 * @param pid Process ID to attach the uprobe to, 0 for self (own process),
 604 * -1 for all processes
 605 * @param binary_path Path to binary
 606 * @param func_pattern Regular expression to specify functions to attach
 607 * BPF program to
 608 * @param opts Additional options (see **struct bpf_uprobe_multi_opts**)
 609 * @return 0, on success; negative error code, otherwise
 610 */
 611LIBBPF_API struct bpf_link *
 612bpf_program__attach_uprobe_multi(const struct bpf_program *prog,
 613				 pid_t pid,
 614				 const char *binary_path,
 615				 const char *func_pattern,
 616				 const struct bpf_uprobe_multi_opts *opts);
 617
 618struct bpf_ksyscall_opts {
 619	/* size of this struct, for forward/backward compatibility */
 620	size_t sz;
 621	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
 622	__u64 bpf_cookie;
 623	/* attach as return probe? */
 624	bool retprobe;
 625	size_t :0;
 626};
 627#define bpf_ksyscall_opts__last_field retprobe
 628
 629/**
 630 * @brief **bpf_program__attach_ksyscall()** attaches a BPF program
 631 * to kernel syscall handler of a specified syscall. Optionally it's possible
 632 * to request to install retprobe that will be triggered at syscall exit. It's
 633 * also possible to associate BPF cookie (though options).
 634 *
 635 * Libbpf automatically will determine correct full kernel function name,
 636 * which depending on system architecture and kernel version/configuration
 637 * could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will
 638 * attach specified program using kprobe/kretprobe mechanism.
 639 *
 640 * **bpf_program__attach_ksyscall()** is an API counterpart of declarative
 641 * **SEC("ksyscall/<syscall>")** annotation of BPF programs.
 642 *
 643 * At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do
 644 * not handle all the calling convention quirks for mmap(), clone() and compat
 645 * syscalls. It also only attaches to "native" syscall interfaces. If host
 646 * system supports compat syscalls or defines 32-bit syscalls in 64-bit
 647 * kernel, such syscall interfaces won't be attached to by libbpf.
 648 *
 649 * These limitations may or may not change in the future. Therefore it is
 650 * recommended to use SEC("kprobe") for these syscalls or if working with
 651 * compat and 32-bit interfaces is required.
 652 *
 653 * @param prog BPF program to attach
 654 * @param syscall_name Symbolic name of the syscall (e.g., "bpf")
 655 * @param opts Additional options (see **struct bpf_ksyscall_opts**)
 656 * @return Reference to the newly created BPF link; or NULL is returned on
 657 * error, error code is stored in errno
 658 */
 659LIBBPF_API struct bpf_link *
 660bpf_program__attach_ksyscall(const struct bpf_program *prog,
 661			     const char *syscall_name,
 662			     const struct bpf_ksyscall_opts *opts);
 663
 664struct bpf_uprobe_opts {
 665	/* size of this struct, for forward/backward compatibility */
 666	size_t sz;
 667	/* offset of kernel reference counted USDT semaphore, added in
 668	 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
 669	 */
 670	size_t ref_ctr_offset;
 671	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
 672	__u64 bpf_cookie;
 673	/* uprobe is return probe, invoked at function return time */
 674	bool retprobe;
 675	/* Function name to attach to.  Could be an unqualified ("abc") or library-qualified
 676	 * "abc@LIBXYZ" name.  To specify function entry, func_name should be set while
 677	 * func_offset argument to bpf_prog__attach_uprobe_opts() should be 0.  To trace an
 678	 * offset within a function, specify func_name and use func_offset argument to specify
 679	 * offset within the function.  Shared library functions must specify the shared library
 680	 * binary_path.
 681	 */
 682	const char *func_name;
 683	/* uprobe attach mode */
 684	enum probe_attach_mode attach_mode;
 685	size_t :0;
 686};
 687#define bpf_uprobe_opts__last_field attach_mode
 688
 689/**
 690 * @brief **bpf_program__attach_uprobe()** attaches a BPF program
 691 * to the userspace function which is found by binary path and
 692 * offset. You can optionally specify a particular process to attach
 693 * to. You can also optionally attach the program to the function
 694 * exit instead of entry.
 695 *
 696 * @param prog BPF program to attach
 697 * @param retprobe Attach to function exit
 698 * @param pid Process ID to attach the uprobe to, 0 for self (own process),
 699 * -1 for all processes
 700 * @param binary_path Path to binary that contains the function symbol
 701 * @param func_offset Offset within the binary of the function symbol
 702 * @return Reference to the newly created BPF link; or NULL is returned on error,
 703 * error code is stored in errno
 704 */
 705LIBBPF_API struct bpf_link *
 706bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
 707			   pid_t pid, const char *binary_path,
 708			   size_t func_offset);
 709
 710/**
 711 * @brief **bpf_program__attach_uprobe_opts()** is just like
 712 * bpf_program__attach_uprobe() except with a options struct
 713 * for various configurations.
 714 *
 715 * @param prog BPF program to attach
 716 * @param pid Process ID to attach the uprobe to, 0 for self (own process),
 717 * -1 for all processes
 718 * @param binary_path Path to binary that contains the function symbol
 719 * @param func_offset Offset within the binary of the function symbol
 720 * @param opts Options for altering program attachment
 721 * @return Reference to the newly created BPF link; or NULL is returned on error,
 722 * error code is stored in errno
 723 */
 724LIBBPF_API struct bpf_link *
 725bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
 726				const char *binary_path, size_t func_offset,
 727				const struct bpf_uprobe_opts *opts);
 728
 729struct bpf_usdt_opts {
 730	/* size of this struct, for forward/backward compatibility */
 731	size_t sz;
 732	/* custom user-provided value accessible through usdt_cookie() */
 733	__u64 usdt_cookie;
 734	size_t :0;
 735};
 736#define bpf_usdt_opts__last_field usdt_cookie
 737
 738/**
 739 * @brief **bpf_program__attach_usdt()** is just like
 740 * bpf_program__attach_uprobe_opts() except it covers USDT (User-space
 741 * Statically Defined Tracepoint) attachment, instead of attaching to
 742 * user-space function entry or exit.
 743 *
 744 * @param prog BPF program to attach
 745 * @param pid Process ID to attach the uprobe to, 0 for self (own process),
 746 * -1 for all processes
 747 * @param binary_path Path to binary that contains provided USDT probe
 748 * @param usdt_provider USDT provider name
 749 * @param usdt_name USDT probe name
 750 * @param opts Options for altering program attachment
 751 * @return Reference to the newly created BPF link; or NULL is returned on error,
 752 * error code is stored in errno
 753 */
 754LIBBPF_API struct bpf_link *
 755bpf_program__attach_usdt(const struct bpf_program *prog,
 756			 pid_t pid, const char *binary_path,
 757			 const char *usdt_provider, const char *usdt_name,
 758			 const struct bpf_usdt_opts *opts);
 759
 760struct bpf_tracepoint_opts {
 761	/* size of this struct, for forward/backward compatibility */
 762	size_t sz;
 763	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
 764	__u64 bpf_cookie;
 765};
 766#define bpf_tracepoint_opts__last_field bpf_cookie
 767
 768LIBBPF_API struct bpf_link *
 769bpf_program__attach_tracepoint(const struct bpf_program *prog,
 770			       const char *tp_category,
 771			       const char *tp_name);
 772LIBBPF_API struct bpf_link *
 773bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
 774				    const char *tp_category,
 775				    const char *tp_name,
 776				    const struct bpf_tracepoint_opts *opts);
 777
 778struct bpf_raw_tracepoint_opts {
 779	size_t sz; /* size of this struct for forward/backward compatibility */
 780	__u64 cookie;
 781	size_t :0;
 782};
 783#define bpf_raw_tracepoint_opts__last_field cookie
 784
 785LIBBPF_API struct bpf_link *
 786bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
 787				   const char *tp_name);
 788LIBBPF_API struct bpf_link *
 789bpf_program__attach_raw_tracepoint_opts(const struct bpf_program *prog,
 790					const char *tp_name,
 791					struct bpf_raw_tracepoint_opts *opts);
 792
 793struct bpf_trace_opts {
 794	/* size of this struct, for forward/backward compatibility */
 795	size_t sz;
 796	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
 797	__u64 cookie;
 798};
 799#define bpf_trace_opts__last_field cookie
 800
 801LIBBPF_API struct bpf_link *
 802bpf_program__attach_trace(const struct bpf_program *prog);
 803LIBBPF_API struct bpf_link *
 804bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);
 805
 806LIBBPF_API struct bpf_link *
 807bpf_program__attach_lsm(const struct bpf_program *prog);
 808LIBBPF_API struct bpf_link *
 809bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
 810LIBBPF_API struct bpf_link *
 811bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
 812LIBBPF_API struct bpf_link *
 813bpf_program__attach_sockmap(const struct bpf_program *prog, int map_fd);
 814LIBBPF_API struct bpf_link *
 815bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
 816LIBBPF_API struct bpf_link *
 817bpf_program__attach_freplace(const struct bpf_program *prog,
 818			     int target_fd, const char *attach_func_name);
 819
 820struct bpf_netfilter_opts {
 821	/* size of this struct, for forward/backward compatibility */
 822	size_t sz;
 823
 824	__u32 pf;
 825	__u32 hooknum;
 826	__s32 priority;
 827	__u32 flags;
 828};
 829#define bpf_netfilter_opts__last_field flags
 830
 831LIBBPF_API struct bpf_link *
 832bpf_program__attach_netfilter(const struct bpf_program *prog,
 833			      const struct bpf_netfilter_opts *opts);
 834
 835struct bpf_tcx_opts {
 836	/* size of this struct, for forward/backward compatibility */
 837	size_t sz;
 838	__u32 flags;
 839	__u32 relative_fd;
 840	__u32 relative_id;
 841	__u64 expected_revision;
 842	size_t :0;
 843};
 844#define bpf_tcx_opts__last_field expected_revision
 845
 846LIBBPF_API struct bpf_link *
 847bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,
 848			const struct bpf_tcx_opts *opts);
 849
 850struct bpf_netkit_opts {
 851	/* size of this struct, for forward/backward compatibility */
 852	size_t sz;
 853	__u32 flags;
 854	__u32 relative_fd;
 855	__u32 relative_id;
 856	__u64 expected_revision;
 857	size_t :0;
 858};
 859#define bpf_netkit_opts__last_field expected_revision
 860
 861LIBBPF_API struct bpf_link *
 862bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,
 863			   const struct bpf_netkit_opts *opts);
 864
 865struct bpf_map;
 866
 867LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
 868LIBBPF_API int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map);
 869
 870struct bpf_iter_attach_opts {
 871	size_t sz; /* size of this struct for forward/backward compatibility */
 872	union bpf_iter_link_info *link_info;
 873	__u32 link_info_len;
 874};
 875#define bpf_iter_attach_opts__last_field link_info_len
 876
 877LIBBPF_API struct bpf_link *
 878bpf_program__attach_iter(const struct bpf_program *prog,
 879			 const struct bpf_iter_attach_opts *opts);
 880
 881LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);
 882
 883/**
 884 * @brief **bpf_program__set_type()** sets the program
 885 * type of the passed BPF program.
 886 * @param prog BPF program to set the program type for
 887 * @param type program type to set the BPF map to have
 888 * @return error code; or 0 if no error. An error occurs
 889 * if the object is already loaded.
 890 *
 891 * This must be called before the BPF object is loaded,
 892 * otherwise it has no effect and an error is returned.
 893 */
 894LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,
 895				     enum bpf_prog_type type);
 896
 897LIBBPF_API enum bpf_attach_type
 898bpf_program__expected_attach_type(const struct bpf_program *prog);
 899
 900/**
 901 * @brief **bpf_program__set_expected_attach_type()** sets the
 902 * attach type of the passed BPF program. This is used for
 903 * auto-detection of attachment when programs are loaded.
 904 * @param prog BPF program to set the attach type for
 905 * @param type attach type to set the BPF map to have
 906 * @return error code; or 0 if no error. An error occurs
 907 * if the object is already loaded.
 908 *
 909 * This must be called before the BPF object is loaded,
 910 * otherwise it has no effect and an error is returned.
 911 */
 912LIBBPF_API int
 913bpf_program__set_expected_attach_type(struct bpf_program *prog,
 914				      enum bpf_attach_type type);
 915
 916LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
 917LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
 918
 919/* Per-program log level and log buffer getters/setters.
 920 * See bpf_object_open_opts comments regarding log_level and log_buf
 921 * interactions.
 922 */
 923LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);
 924LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);
 925LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
 926LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
 927
 928/**
 929 * @brief **bpf_program__set_attach_target()** sets BTF-based attach target
 930 * for supported BPF program types:
 931 *   - BTF-aware raw tracepoints (tp_btf);
 932 *   - fentry/fexit/fmod_ret;
 933 *   - lsm;
 934 *   - freplace.
 935 * @param prog BPF program to set the attach type for
 936 * @param type attach type to set the BPF map to have
 937 * @return error code; or 0 if no error occurred.
 938 */
 939LIBBPF_API int
 940bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
 941			       const char *attach_func_name);
 942
 943/**
 944 * @brief **bpf_object__find_map_by_name()** returns BPF map of
 945 * the given name, if it exists within the passed BPF object
 946 * @param obj BPF object
 947 * @param name name of the BPF map
 948 * @return BPF map instance, if such map exists within the BPF object;
 949 * or NULL otherwise.
 950 */
 951LIBBPF_API struct bpf_map *
 952bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
 953
 954LIBBPF_API int
 955bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
 956
 957LIBBPF_API struct bpf_map *
 958bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
 959
 960#define bpf_object__for_each_map(pos, obj)		\
 961	for ((pos) = bpf_object__next_map((obj), NULL);	\
 962	     (pos) != NULL;				\
 963	     (pos) = bpf_object__next_map((obj), (pos)))
 964#define bpf_map__for_each bpf_object__for_each_map
 965
 966LIBBPF_API struct bpf_map *
 967bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
 968
 969/**
 970 * @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create
 971 * BPF map during BPF object load phase.
 972 * @param map the BPF map instance
 973 * @param autocreate whether to create BPF map during BPF object load
 974 * @return 0 on success; -EBUSY if BPF object was already loaded
 975 *
 976 * **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating
 977 * BPF map. By default, libbpf will attempt to create every single BPF map
 978 * defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall
 979 * and fill in map FD in BPF instructions.
 980 *
 981 * This API allows to opt-out of this process for specific map instance. This
 982 * can be useful if host kernel doesn't support such BPF map type or used
 983 * combination of flags and user application wants to avoid creating such
 984 * a map in the first place. User is still responsible to make sure that their
 985 * BPF-side code that expects to use such missing BPF map is recognized by BPF
 986 * verifier as dead code, otherwise BPF verifier will reject such BPF program.
 987 */
 988LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);
 989LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);
 990
 991/**
 992 * @brief **bpf_map__set_autoattach()** sets whether libbpf has to auto-attach
 993 * map during BPF skeleton attach phase.
 994 * @param map the BPF map instance
 995 * @param autoattach whether to attach map during BPF skeleton attach phase
 996 * @return 0 on success; negative error code, otherwise
 997 */
 998LIBBPF_API int bpf_map__set_autoattach(struct bpf_map *map, bool autoattach);
 999
1000/**
1001 * @brief **bpf_map__autoattach()** returns whether BPF map is configured to
1002 * auto-attach during BPF skeleton attach phase.
1003 * @param map the BPF map instance
1004 * @return true if map is set to auto-attach during skeleton attach phase; false, otherwise
1005 */
1006LIBBPF_API bool bpf_map__autoattach(const struct bpf_map *map);
1007
1008/**
1009 * @brief **bpf_map__fd()** gets the file descriptor of the passed
1010 * BPF map
1011 * @param map the BPF map instance
1012 * @return the file descriptor; or -EINVAL in case of an error
1013 */
1014LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
1015LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
1016/* get map name */
1017LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
1018/* get/set map type */
1019LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
1020LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
1021/* get/set map size (max_entries) */
1022LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
1023LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
1024/* get/set map flags */
1025LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
1026LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
1027/* get/set map NUMA node */
1028LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
1029LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
1030/* get/set map key size */
1031LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
1032LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
1033/* get map value size */
1034LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
1035/**
1036 * @brief **bpf_map__set_value_size()** sets map value size.
1037 * @param map the BPF map instance
1038 * @return 0, on success; negative error, otherwise
1039 *
1040 * There is a special case for maps with associated memory-mapped regions, like
1041 * the global data section maps (bss, data, rodata). When this function is used
1042 * on such a map, the mapped region is resized. Afterward, an attempt is made to
1043 * adjust the corresponding BTF info. This attempt is best-effort and can only
1044 * succeed if the last variable of the data section map is an array. The array
1045 * BTF type is replaced by a new BTF array type with a different length.
1046 * Any previously existing pointers returned from bpf_map__initial_value() or
1047 * corresponding data section skeleton pointer must be reinitialized.
1048 */
1049LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
1050/* get map key/value BTF type IDs */
1051LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
1052LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
1053/* get/set map if_index */
1054LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
1055LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
1056/* get/set map map_extra flags */
1057LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
1058LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
1059
1060LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
1061					  const void *data, size_t size);
1062LIBBPF_API void *bpf_map__initial_value(const struct bpf_map *map, size_t *psize);
1063
1064/**
1065 * @brief **bpf_map__is_internal()** tells the caller whether or not the
1066 * passed map is a special map created by libbpf automatically for things like
1067 * global variables, __ksym externs, Kconfig values, etc
1068 * @param map the bpf_map
1069 * @return true, if the map is an internal map; false, otherwise
1070 */
1071LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
1072
1073/**
1074 * @brief **bpf_map__set_pin_path()** sets the path attribute that tells where the
1075 * BPF map should be pinned. This does not actually create the 'pin'.
1076 * @param map The bpf_map
1077 * @param path The path
1078 * @return 0, on success; negative error, otherwise
1079 */
1080LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
1081
1082/**
1083 * @brief **bpf_map__pin_path()** gets the path attribute that tells where the
1084 * BPF map should be pinned.
1085 * @param map The bpf_map
1086 * @return The path string; which can be NULL
1087 */
1088LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
1089
1090/**
1091 * @brief **bpf_map__is_pinned()** tells the caller whether or not the
1092 * passed map has been pinned via a 'pin' file.
1093 * @param map The bpf_map
1094 * @return true, if the map is pinned; false, otherwise
1095 */
1096LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
1097
1098/**
1099 * @brief **bpf_map__pin()** creates a file that serves as a 'pin'
1100 * for the BPF map. This increments the reference count on the
1101 * BPF map which will keep the BPF map loaded even after the
1102 * userspace process which loaded it has exited.
1103 * @param map The bpf_map to pin
1104 * @param path A file path for the 'pin'
1105 * @return 0, on success; negative error, otherwise
1106 *
1107 * If `path` is NULL the maps `pin_path` attribute will be used. If this is
1108 * also NULL, an error will be returned and the map will not be pinned.
1109 */
1110LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
1111
1112/**
1113 * @brief **bpf_map__unpin()** removes the file that serves as a
1114 * 'pin' for the BPF map.
1115 * @param map The bpf_map to unpin
1116 * @param path A file path for the 'pin'
1117 * @return 0, on success; negative error, otherwise
1118 *
1119 * The `path` parameter can be NULL, in which case the `pin_path`
1120 * map attribute is unpinned. If both the `path` parameter and
1121 * `pin_path` map attribute are set, they must be equal.
1122 */
1123LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
1124
1125LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
1126LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
1127
1128/**
1129 * @brief **bpf_map__lookup_elem()** allows to lookup BPF map value
1130 * corresponding to provided key.
1131 * @param map BPF map to lookup element in
1132 * @param key pointer to memory containing bytes of the key used for lookup
1133 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1134 * @param value pointer to memory in which looked up value will be stored
1135 * @param value_sz size in byte of value data memory; it has to match BPF map
1136 * definition's **value_size**. For per-CPU BPF maps value size has to be
1137 * a product of BPF map value size and number of possible CPUs in the system
1138 * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1139 * per-CPU values value size has to be aligned up to closest 8 bytes for
1140 * alignment reasons, so expected size is: `round_up(value_size, 8)
1141 * * libbpf_num_possible_cpus()`.
1142 * @flags extra flags passed to kernel for this operation
1143 * @return 0, on success; negative error, otherwise
1144 *
1145 * **bpf_map__lookup_elem()** is high-level equivalent of
1146 * **bpf_map_lookup_elem()** API with added check for key and value size.
1147 */
1148LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,
1149				    const void *key, size_t key_sz,
1150				    void *value, size_t value_sz, __u64 flags);
1151
1152/**
1153 * @brief **bpf_map__update_elem()** allows to insert or update value in BPF
1154 * map that corresponds to provided key.
1155 * @param map BPF map to insert to or update element in
1156 * @param key pointer to memory containing bytes of the key
1157 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1158 * @param value pointer to memory containing bytes of the value
1159 * @param value_sz size in byte of value data memory; it has to match BPF map
1160 * definition's **value_size**. For per-CPU BPF maps value size has to be
1161 * a product of BPF map value size and number of possible CPUs in the system
1162 * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1163 * per-CPU values value size has to be aligned up to closest 8 bytes for
1164 * alignment reasons, so expected size is: `round_up(value_size, 8)
1165 * * libbpf_num_possible_cpus()`.
1166 * @flags extra flags passed to kernel for this operation
1167 * @return 0, on success; negative error, otherwise
1168 *
1169 * **bpf_map__update_elem()** is high-level equivalent of
1170 * **bpf_map_update_elem()** API with added check for key and value size.
1171 */
1172LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map,
1173				    const void *key, size_t key_sz,
1174				    const void *value, size_t value_sz, __u64 flags);
1175
1176/**
1177 * @brief **bpf_map__delete_elem()** allows to delete element in BPF map that
1178 * corresponds to provided key.
1179 * @param map BPF map to delete element from
1180 * @param key pointer to memory containing bytes of the key
1181 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1182 * @flags extra flags passed to kernel for this operation
1183 * @return 0, on success; negative error, otherwise
1184 *
1185 * **bpf_map__delete_elem()** is high-level equivalent of
1186 * **bpf_map_delete_elem()** API with added check for key size.
1187 */
1188LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map,
1189				    const void *key, size_t key_sz, __u64 flags);
1190
1191/**
1192 * @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value
1193 * corresponding to provided key and atomically delete it afterwards.
1194 * @param map BPF map to lookup element in
1195 * @param key pointer to memory containing bytes of the key used for lookup
1196 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1197 * @param value pointer to memory in which looked up value will be stored
1198 * @param value_sz size in byte of value data memory; it has to match BPF map
1199 * definition's **value_size**. For per-CPU BPF maps value size has to be
1200 * a product of BPF map value size and number of possible CPUs in the system
1201 * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1202 * per-CPU values value size has to be aligned up to closest 8 bytes for
1203 * alignment reasons, so expected size is: `round_up(value_size, 8)
1204 * * libbpf_num_possible_cpus()`.
1205 * @flags extra flags passed to kernel for this operation
1206 * @return 0, on success; negative error, otherwise
1207 *
1208 * **bpf_map__lookup_and_delete_elem()** is high-level equivalent of
1209 * **bpf_map_lookup_and_delete_elem()** API with added check for key and value size.
1210 */
1211LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
1212					       const void *key, size_t key_sz,
1213					       void *value, size_t value_sz, __u64 flags);
1214
1215/**
1216 * @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by
1217 * fetching next key that follows current key.
1218 * @param map BPF map to fetch next key from
1219 * @param cur_key pointer to memory containing bytes of current key or NULL to
1220 * fetch the first key
1221 * @param next_key pointer to memory to write next key into
1222 * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1223 * @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map;
1224 * negative error, otherwise
1225 *
1226 * **bpf_map__get_next_key()** is high-level equivalent of
1227 * **bpf_map_get_next_key()** API with added check for key size.
1228 */
1229LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map,
1230				     const void *cur_key, void *next_key, size_t key_sz);
1231
1232struct bpf_xdp_set_link_opts {
1233	size_t sz;
1234	int old_fd;
1235	size_t :0;
1236};
1237#define bpf_xdp_set_link_opts__last_field old_fd
1238
1239struct bpf_xdp_attach_opts {
1240	size_t sz;
1241	int old_prog_fd;
1242	size_t :0;
1243};
1244#define bpf_xdp_attach_opts__last_field old_prog_fd
1245
1246struct bpf_xdp_query_opts {
1247	size_t sz;
1248	__u32 prog_id;		/* output */
1249	__u32 drv_prog_id;	/* output */
1250	__u32 hw_prog_id;	/* output */
1251	__u32 skb_prog_id;	/* output */
1252	__u8 attach_mode;	/* output */
1253	__u64 feature_flags;	/* output */
1254	__u32 xdp_zc_max_segs;	/* output */
1255	size_t :0;
1256};
1257#define bpf_xdp_query_opts__last_field xdp_zc_max_segs
1258
1259LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,
1260			      const struct bpf_xdp_attach_opts *opts);
1261LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,
1262			      const struct bpf_xdp_attach_opts *opts);
1263LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);
1264LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);
1265
1266/* TC related API */
1267enum bpf_tc_attach_point {
1268	BPF_TC_INGRESS = 1 << 0,
1269	BPF_TC_EGRESS  = 1 << 1,
1270	BPF_TC_CUSTOM  = 1 << 2,
1271};
1272
1273#define BPF_TC_PARENT(a, b) 	\
1274	((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
1275
1276enum bpf_tc_flags {
1277	BPF_TC_F_REPLACE = 1 << 0,
1278};
1279
1280struct bpf_tc_hook {
1281	size_t sz;
1282	int ifindex;
1283	enum bpf_tc_attach_point attach_point;
1284	__u32 parent;
1285	size_t :0;
1286};
1287#define bpf_tc_hook__last_field parent
1288
1289struct bpf_tc_opts {
1290	size_t sz;
1291	int prog_fd;
1292	__u32 flags;
1293	__u32 prog_id;
1294	__u32 handle;
1295	__u32 priority;
1296	size_t :0;
1297};
1298#define bpf_tc_opts__last_field priority
1299
1300LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
1301LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
1302LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
1303			     struct bpf_tc_opts *opts);
1304LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
1305			     const struct bpf_tc_opts *opts);
1306LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
1307			    struct bpf_tc_opts *opts);
1308
1309/* Ring buffer APIs */
1310struct ring_buffer;
1311struct ring;
1312struct user_ring_buffer;
1313
1314typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
1315
1316struct ring_buffer_opts {
1317	size_t sz; /* size of this struct, for forward/backward compatibility */
1318};
1319
1320#define ring_buffer_opts__last_field sz
1321
1322LIBBPF_API struct ring_buffer *
1323ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
1324		 const struct ring_buffer_opts *opts);
1325LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
1326LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
1327				ring_buffer_sample_fn sample_cb, void *ctx);
1328LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
1329LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
1330LIBBPF_API int ring_buffer__consume_n(struct ring_buffer *rb, size_t n);
1331LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
1332
1333/**
1334 * @brief **ring_buffer__ring()** returns the ringbuffer object inside a given
1335 * ringbuffer manager representing a single BPF_MAP_TYPE_RINGBUF map instance.
1336 *
1337 * @param rb A ringbuffer manager object.
1338 * @param idx An index into the ringbuffers contained within the ringbuffer
1339 * manager object. The index is 0-based and corresponds to the order in which
1340 * ring_buffer__add was called.
1341 * @return A ringbuffer object on success; NULL and errno set if the index is
1342 * invalid.
1343 */
1344LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb,
1345					  unsigned int idx);
1346
1347/**
1348 * @brief **ring__consumer_pos()** returns the current consumer position in the
1349 * given ringbuffer.
1350 *
1351 * @param r A ringbuffer object.
1352 * @return The current consumer position.
1353 */
1354LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r);
1355
1356/**
1357 * @brief **ring__producer_pos()** returns the current producer position in the
1358 * given ringbuffer.
1359 *
1360 * @param r A ringbuffer object.
1361 * @return The current producer position.
1362 */
1363LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);
1364
1365/**
1366 * @brief **ring__avail_data_size()** returns the number of bytes in the
1367 * ringbuffer not yet consumed. This has no locking associated with it, so it
1368 * can be inaccurate if operations are ongoing while this is called. However, it
1369 * should still show the correct trend over the long-term.
1370 *
1371 * @param r A ringbuffer object.
1372 * @return The number of bytes not yet consumed.
1373 */
1374LIBBPF_API size_t ring__avail_data_size(const struct ring *r);
1375
1376/**
1377 * @brief **ring__size()** returns the total size of the ringbuffer's map data
1378 * area (excluding special producer/consumer pages). Effectively this gives the
1379 * amount of usable bytes of data inside the ringbuffer.
1380 *
1381 * @param r A ringbuffer object.
1382 * @return The total size of the ringbuffer map data area.
1383 */
1384LIBBPF_API size_t ring__size(const struct ring *r);
1385
1386/**
1387 * @brief **ring__map_fd()** returns the file descriptor underlying the given
1388 * ringbuffer.
1389 *
1390 * @param r A ringbuffer object.
1391 * @return The underlying ringbuffer file descriptor
1392 */
1393LIBBPF_API int ring__map_fd(const struct ring *r);
1394
1395/**
1396 * @brief **ring__consume()** consumes available ringbuffer data without event
1397 * polling.
1398 *
1399 * @param r A ringbuffer object.
1400 * @return The number of records consumed (or INT_MAX, whichever is less), or
1401 * a negative number if any of the callbacks return an error.
1402 */
1403LIBBPF_API int ring__consume(struct ring *r);
1404
1405/**
1406 * @brief **ring__consume_n()** consumes up to a requested amount of items from
1407 * a ringbuffer without event polling.
1408 *
1409 * @param r A ringbuffer object.
1410 * @param n Maximum amount of items to consume.
1411 * @return The number of items consumed, or a negative number if any of the
1412 * callbacks return an error.
1413 */
1414LIBBPF_API int ring__consume_n(struct ring *r, size_t n);
1415
1416struct user_ring_buffer_opts {
1417	size_t sz; /* size of this struct, for forward/backward compatibility */
1418};
1419
1420#define user_ring_buffer_opts__last_field sz
1421
1422/**
1423 * @brief **user_ring_buffer__new()** creates a new instance of a user ring
1424 * buffer.
1425 *
1426 * @param map_fd A file descriptor to a BPF_MAP_TYPE_USER_RINGBUF map.
1427 * @param opts Options for how the ring buffer should be created.
1428 * @return A user ring buffer on success; NULL and errno being set on a
1429 * failure.
1430 */
1431LIBBPF_API struct user_ring_buffer *
1432user_ring_buffer__new(int map_fd, const struct user_ring_buffer_opts *opts);
1433
1434/**
1435 * @brief **user_ring_buffer__reserve()** reserves a pointer to a sample in the
1436 * user ring buffer.
1437 * @param rb A pointer to a user ring buffer.
1438 * @param size The size of the sample, in bytes.
1439 * @return A pointer to an 8-byte aligned reserved region of the user ring
1440 * buffer; NULL, and errno being set if a sample could not be reserved.
1441 *
1442 * This function is *not* thread safe, and callers must synchronize accessing
1443 * this function if there are multiple producers.  If a size is requested that
1444 * is larger than the size of the entire ring buffer, errno will be set to
1445 * E2BIG and NULL is returned. If the ring buffer could accommodate the size,
1446 * but currently does not have enough space, errno is set to ENOSPC and NULL is
1447 * returned.
1448 *
1449 * After initializing the sample, callers must invoke
1450 * **user_ring_buffer__submit()** to post the sample to the kernel. Otherwise,
1451 * the sample must be freed with **user_ring_buffer__discard()**.
1452 */
1453LIBBPF_API void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);
1454
1455/**
1456 * @brief **user_ring_buffer__reserve_blocking()** reserves a record in the
1457 * ring buffer, possibly blocking for up to @timeout_ms until a sample becomes
1458 * available.
1459 * @param rb The user ring buffer.
1460 * @param size The size of the sample, in bytes.
1461 * @param timeout_ms The amount of time, in milliseconds, for which the caller
1462 * should block when waiting for a sample. -1 causes the caller to block
1463 * indefinitely.
1464 * @return A pointer to an 8-byte aligned reserved region of the user ring
1465 * buffer; NULL, and errno being set if a sample could not be reserved.
1466 *
1467 * This function is *not* thread safe, and callers must synchronize
1468 * accessing this function if there are multiple producers
1469 *
1470 * If **timeout_ms** is -1, the function will block indefinitely until a sample
1471 * becomes available. Otherwise, **timeout_ms** must be non-negative, or errno
1472 * is set to EINVAL, and NULL is returned. If **timeout_ms** is 0, no blocking
1473 * will occur and the function will return immediately after attempting to
1474 * reserve a sample.
1475 *
1476 * If **size** is larger than the size of the entire ring buffer, errno is set
1477 * to E2BIG and NULL is returned. If the ring buffer could accommodate
1478 * **size**, but currently does not have enough space, the caller will block
1479 * until at most **timeout_ms** has elapsed. If insufficient space is available
1480 * at that time, errno is set to ENOSPC, and NULL is returned.
1481 *
1482 * The kernel guarantees that it will wake up this thread to check if
1483 * sufficient space is available in the ring buffer at least once per
1484 * invocation of the **bpf_ringbuf_drain()** helper function, provided that at
1485 * least one sample is consumed, and the BPF program did not invoke the
1486 * function with BPF_RB_NO_WAKEUP. A wakeup may occur sooner than that, but the
1487 * kernel does not guarantee this. If the helper function is invoked with
1488 * BPF_RB_FORCE_WAKEUP, a wakeup event will be sent even if no sample is
1489 * consumed.
1490 *
1491 * When a sample of size **size** is found within **timeout_ms**, a pointer to
1492 * the sample is returned. After initializing the sample, callers must invoke
1493 * **user_ring_buffer__submit()** to post the sample to the ring buffer.
1494 * Otherwise, the sample must be freed with **user_ring_buffer__discard()**.
1495 */
1496LIBBPF_API void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,
1497						    __u32 size,
1498						    int timeout_ms);
1499
1500/**
1501 * @brief **user_ring_buffer__submit()** submits a previously reserved sample
1502 * into the ring buffer.
1503 * @param rb The user ring buffer.
1504 * @param sample A reserved sample.
1505 *
1506 * It is not necessary to synchronize amongst multiple producers when invoking
1507 * this function.
1508 */
1509LIBBPF_API void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample);
1510
1511/**
1512 * @brief **user_ring_buffer__discard()** discards a previously reserved sample.
1513 * @param rb The user ring buffer.
1514 * @param sample A reserved sample.
1515 *
1516 * It is not necessary to synchronize amongst multiple producers when invoking
1517 * this function.
1518 */
1519LIBBPF_API void user_ring_buffer__discard(struct user_ring_buffer *rb, void *sample);
1520
1521/**
1522 * @brief **user_ring_buffer__free()** frees a ring buffer that was previously
1523 * created with **user_ring_buffer__new()**.
1524 * @param rb The user ring buffer being freed.
1525 */
1526LIBBPF_API void user_ring_buffer__free(struct user_ring_buffer *rb);
1527
1528/* Perf buffer APIs */
1529struct perf_buffer;
1530
1531typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
1532				      void *data, __u32 size);
1533typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
1534
1535/* common use perf buffer options */
1536struct perf_buffer_opts {
1537	size_t sz;
1538	__u32 sample_period;
1539	size_t :0;
1540};
1541#define perf_buffer_opts__last_field sample_period
1542
1543/**
1544 * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
1545 * BPF_PERF_EVENT_ARRAY map
1546 * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
1547 * code to send data over to user-space
1548 * @param page_cnt number of memory pages allocated for each per-CPU buffer
1549 * @param sample_cb function called on each received data record
1550 * @param lost_cb function called when record loss has occurred
1551 * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
1552 * @return a new instance of struct perf_buffer on success, NULL on error with
1553 * *errno* containing an error code
1554 */
1555LIBBPF_API struct perf_buffer *
1556perf_buffer__new(int map_fd, size_t page_cnt,
1557		 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
1558		 const struct perf_buffer_opts *opts);
1559
1560enum bpf_perf_event_ret {
1561	LIBBPF_PERF_EVENT_DONE	= 0,
1562	LIBBPF_PERF_EVENT_ERROR	= -1,
1563	LIBBPF_PERF_EVENT_CONT	= -2,
1564};
1565
1566struct perf_event_header;
1567
1568typedef enum bpf_perf_event_ret
1569(*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
1570
1571/* raw perf buffer options, giving most power and control */
1572struct perf_buffer_raw_opts {
1573	size_t sz;
1574	long :0;
1575	long :0;
1576	/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
1577	 * max_entries of given PERF_EVENT_ARRAY map)
1578	 */
1579	int cpu_cnt;
1580	/* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
1581	int *cpus;
1582	/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
1583	int *map_keys;
1584};
1585#define perf_buffer_raw_opts__last_field map_keys
1586
1587struct perf_event_attr;
1588
1589LIBBPF_API struct perf_buffer *
1590perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1591		     perf_buffer_event_fn event_cb, void *ctx,
1592		     const struct perf_buffer_raw_opts *opts);
1593
1594LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
1595LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
1596LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
1597LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
1598LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
1599LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
1600LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
1601/**
1602 * @brief **perf_buffer__buffer()** returns the per-cpu raw mmap()'ed underlying
1603 * memory region of the ring buffer.
1604 * This ring buffer can be used to implement a custom events consumer.
1605 * The ring buffer starts with the *struct perf_event_mmap_page*, which
1606 * holds the ring buffer management fields, when accessing the header
1607 * structure it's important to be SMP aware.
1608 * You can refer to *perf_event_read_simple* for a simple example.
1609 * @param pb the perf buffer structure
1610 * @param buf_idx the buffer index to retrieve
1611 * @param buf (out) gets the base pointer of the mmap()'ed memory
1612 * @param buf_size (out) gets the size of the mmap()'ed region
1613 * @return 0 on success, negative error code for failure
1614 */
1615LIBBPF_API int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf,
1616				   size_t *buf_size);
1617
1618struct bpf_prog_linfo;
1619struct bpf_prog_info;
1620
1621LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
1622LIBBPF_API struct bpf_prog_linfo *
1623bpf_prog_linfo__new(const struct bpf_prog_info *info);
1624LIBBPF_API const struct bpf_line_info *
1625bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
1626				__u64 addr, __u32 func_idx, __u32 nr_skip);
1627LIBBPF_API const struct bpf_line_info *
1628bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
1629		      __u32 insn_off, __u32 nr_skip);
1630
1631/*
1632 * Probe for supported system features
1633 *
1634 * Note that running many of these probes in a short amount of time can cause
1635 * the kernel to reach the maximal size of lockable memory allowed for the
1636 * user, causing subsequent probes to fail. In this case, the caller may want
1637 * to adjust that limit with setrlimit().
1638 */
1639
1640/**
1641 * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports
1642 * BPF programs of a given type.
1643 * @param prog_type BPF program type to detect kernel support for
1644 * @param opts reserved for future extensibility, should be NULL
1645 * @return 1, if given program type is supported; 0, if given program type is
1646 * not supported; negative error code if feature detection failed or can't be
1647 * performed
1648 *
1649 * Make sure the process has required set of CAP_* permissions (or runs as
1650 * root) when performing feature checking.
1651 */
1652LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);
1653/**
1654 * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports
1655 * BPF maps of a given type.
1656 * @param map_type BPF map type to detect kernel support for
1657 * @param opts reserved for future extensibility, should be NULL
1658 * @return 1, if given map type is supported; 0, if given map type is
1659 * not supported; negative error code if feature detection failed or can't be
1660 * performed
1661 *
1662 * Make sure the process has required set of CAP_* permissions (or runs as
1663 * root) when performing feature checking.
1664 */
1665LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);
1666/**
1667 * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the
1668 * use of a given BPF helper from specified BPF program type.
1669 * @param prog_type BPF program type used to check the support of BPF helper
1670 * @param helper_id BPF helper ID (enum bpf_func_id) to check support for
1671 * @param opts reserved for future extensibility, should be NULL
1672 * @return 1, if given combination of program type and helper is supported; 0,
1673 * if the combination is not supported; negative error code if feature
1674 * detection for provided input arguments failed or can't be performed
1675 *
1676 * Make sure the process has required set of CAP_* permissions (or runs as
1677 * root) when performing feature checking.
1678 */
1679LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
1680				       enum bpf_func_id helper_id, const void *opts);
1681
1682/**
1683 * @brief **libbpf_num_possible_cpus()** is a helper function to get the
1684 * number of possible CPUs that the host kernel supports and expects.
1685 * @return number of possible CPUs; or error code on failure
1686 *
1687 * Example usage:
1688 *
1689 *     int ncpus = libbpf_num_possible_cpus();
1690 *     if (ncpus < 0) {
1691 *          // error handling
1692 *     }
1693 *     long values[ncpus];
1694 *     bpf_map_lookup_elem(per_cpu_map_fd, key, values);
1695 */
1696LIBBPF_API int libbpf_num_possible_cpus(void);
1697
1698struct bpf_map_skeleton {
1699	const char *name;
1700	struct bpf_map **map;
1701	void **mmaped;
1702	struct bpf_link **link;
1703};
1704
1705struct bpf_prog_skeleton {
1706	const char *name;
1707	struct bpf_program **prog;
1708	struct bpf_link **link;
1709};
1710
1711struct bpf_object_skeleton {
1712	size_t sz; /* size of this struct, for forward/backward compatibility */
1713
1714	const char *name;
1715	const void *data;
1716	size_t data_sz;
1717
1718	struct bpf_object **obj;
1719
1720	int map_cnt;
1721	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1722	struct bpf_map_skeleton *maps;
1723
1724	int prog_cnt;
1725	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1726	struct bpf_prog_skeleton *progs;
1727};
1728
1729LIBBPF_API int
1730bpf_object__open_skeleton(struct bpf_object_skeleton *s,
1731			  const struct bpf_object_open_opts *opts);
1732LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
1733LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
1734LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
1735LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
1736
1737struct bpf_var_skeleton {
1738	const char *name;
1739	struct bpf_map **map;
1740	void **addr;
1741};
1742
1743struct bpf_object_subskeleton {
1744	size_t sz; /* size of this struct, for forward/backward compatibility */
1745
1746	const struct bpf_object *obj;
1747
1748	int map_cnt;
1749	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1750	struct bpf_map_skeleton *maps;
1751
1752	int prog_cnt;
1753	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1754	struct bpf_prog_skeleton *progs;
1755
1756	int var_cnt;
1757	int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */
1758	struct bpf_var_skeleton *vars;
1759};
1760
1761LIBBPF_API int
1762bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);
1763LIBBPF_API void
1764bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);
1765
1766struct gen_loader_opts {
1767	size_t sz; /* size of this struct, for forward/backward compatibility */
1768	const char *data;
1769	const char *insns;
1770	__u32 data_sz;
1771	__u32 insns_sz;
1772};
1773
1774#define gen_loader_opts__last_field insns_sz
1775LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
1776				      struct gen_loader_opts *opts);
1777
1778enum libbpf_tristate {
1779	TRI_NO = 0,
1780	TRI_YES = 1,
1781	TRI_MODULE = 2,
1782};
1783
1784struct bpf_linker_opts {
1785	/* size of this struct, for forward/backward compatibility */
1786	size_t sz;
1787};
1788#define bpf_linker_opts__last_field sz
1789
1790struct bpf_linker_file_opts {
1791	/* size of this struct, for forward/backward compatibility */
1792	size_t sz;
1793};
1794#define bpf_linker_file_opts__last_field sz
1795
1796struct bpf_linker;
1797
1798LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
1799LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
1800				    const char *filename,
1801				    const struct bpf_linker_file_opts *opts);
1802LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
1803LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
1804
1805/*
1806 * Custom handling of BPF program's SEC() definitions
1807 */
1808
1809struct bpf_prog_load_opts; /* defined in bpf.h */
1810
1811/* Called during bpf_object__open() for each recognized BPF program. Callback
1812 * can use various bpf_program__set_*() setters to adjust whatever properties
1813 * are necessary.
1814 */
1815typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);
1816
1817/* Called right before libbpf performs bpf_prog_load() to load BPF program
1818 * into the kernel. Callback can adjust opts as necessary.
1819 */
1820typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,
1821					     struct bpf_prog_load_opts *opts, long cookie);
1822
1823/* Called during skeleton attach or through bpf_program__attach(). If
1824 * auto-attach is not supported, callback should return 0 and set link to
1825 * NULL (it's not considered an error during skeleton attach, but it will be
1826 * an error for bpf_program__attach() calls). On error, error should be
1827 * returned directly and link set to NULL. On success, return 0 and set link
1828 * to a valid struct bpf_link.
1829 */
1830typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,
1831				       struct bpf_link **link);
1832
1833struct libbpf_prog_handler_opts {
1834	/* size of this struct, for forward/backward compatibility */
1835	size_t sz;
1836	/* User-provided value that is passed to prog_setup_fn,
1837	 * prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to
1838	 * register one set of callbacks for multiple SEC() definitions and
1839	 * still be able to distinguish them, if necessary. For example,
1840	 * libbpf itself is using this to pass necessary flags (e.g.,
1841	 * sleepable flag) to a common internal SEC() handler.
1842	 */
1843	long cookie;
1844	/* BPF program initialization callback (see libbpf_prog_setup_fn_t).
1845	 * Callback is optional, pass NULL if it's not necessary.
1846	 */
1847	libbpf_prog_setup_fn_t prog_setup_fn;
1848	/* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).
1849	 * Callback is optional, pass NULL if it's not necessary.
1850	 */
1851	libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
1852	/* BPF program attach callback (see libbpf_prog_attach_fn_t).
1853	 * Callback is optional, pass NULL if it's not necessary.
1854	 */
1855	libbpf_prog_attach_fn_t prog_attach_fn;
1856};
1857#define libbpf_prog_handler_opts__last_field prog_attach_fn
1858
1859/**
1860 * @brief **libbpf_register_prog_handler()** registers a custom BPF program
1861 * SEC() handler.
1862 * @param sec section prefix for which custom handler is registered
1863 * @param prog_type BPF program type associated with specified section
1864 * @param exp_attach_type Expected BPF attach type associated with specified section
1865 * @param opts optional cookie, callbacks, and other extra options
1866 * @return Non-negative handler ID is returned on success. This handler ID has
1867 * to be passed to *libbpf_unregister_prog_handler()* to unregister such
1868 * custom handler. Negative error code is returned on error.
1869 *
1870 * *sec* defines which SEC() definitions are handled by this custom handler
1871 * registration. *sec* can have few different forms:
1872 *   - if *sec* is just a plain string (e.g., "abc"), it will match only
1873 *   SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result
1874 *   in an error;
1875 *   - if *sec* is of the form "abc/", proper SEC() form is
1876 *   SEC("abc/something"), where acceptable "something" should be checked by
1877 *   *prog_init_fn* callback, if there are additional restrictions;
1878 *   - if *sec* is of the form "abc+", it will successfully match both
1879 *   SEC("abc") and SEC("abc/whatever") forms;
1880 *   - if *sec* is NULL, custom handler is registered for any BPF program that
1881 *   doesn't match any of the registered (custom or libbpf's own) SEC()
1882 *   handlers. There could be only one such generic custom handler registered
1883 *   at any given time.
1884 *
1885 * All custom handlers (except the one with *sec* == NULL) are processed
1886 * before libbpf's own SEC() handlers. It is allowed to "override" libbpf's
1887 * SEC() handlers by registering custom ones for the same section prefix
1888 * (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")
1889 * handler).
1890 *
1891 * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1892 * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1893 * to ensure synchronization if there is a risk of running this API from
1894 * multiple threads simultaneously.
1895 */
1896LIBBPF_API int libbpf_register_prog_handler(const char *sec,
1897					    enum bpf_prog_type prog_type,
1898					    enum bpf_attach_type exp_attach_type,
1899					    const struct libbpf_prog_handler_opts *opts);
1900/**
1901 * @brief *libbpf_unregister_prog_handler()* unregisters previously registered
1902 * custom BPF program SEC() handler.
1903 * @param handler_id handler ID returned by *libbpf_register_prog_handler()*
1904 * after successful registration
1905 * @return 0 on success, negative error code if handler isn't found
1906 *
1907 * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1908 * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1909 * to ensure synchronization if there is a risk of running this API from
1910 * multiple threads simultaneously.
1911 */
1912LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);
1913
1914#ifdef __cplusplus
1915} /* extern "C" */
1916#endif
1917
1918#endif /* __LIBBPF_LIBBPF_H */