xref: /linux/tools/lib/bpf/libbpf.h (revision b6459415b384cb829f0b2a4268f211c789f6cf0b)
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
24 extern "C" {
25 #endif
26 
27 LIBBPF_API __u32 libbpf_major_version(void);
28 LIBBPF_API __u32 libbpf_minor_version(void);
29 LIBBPF_API const char *libbpf_version_string(void);
30 
31 enum 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 
52 LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
53 
54 enum libbpf_print_level {
55         LIBBPF_WARN,
56         LIBBPF_INFO,
57         LIBBPF_DEBUG,
58 };
59 
60 typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
61 				 const char *, va_list ap);
62 
63 LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
64 
65 /* Hide internal to user */
66 struct bpf_object;
67 
68 struct bpf_object_open_attr {
69 	const char *file;
70 	enum bpf_prog_type prog_type;
71 };
72 
73 struct bpf_object_open_opts {
74 	/* size of this struct, for forward/backward compatiblity */
75 	size_t sz;
76 	/* object name override, if provided:
77 	 * - for object open from file, this will override setting object
78 	 *   name from file path's base name;
79 	 * - for object open from memory buffer, this will specify an object
80 	 *   name and will override default "<addr>-<buf-size>" name;
81 	 */
82 	const char *object_name;
83 	/* parse map definitions non-strictly, allowing extra attributes/data */
84 	bool relaxed_maps;
85 	/* DEPRECATED: handle CO-RE relocations non-strictly, allowing failures.
86 	 * Value is ignored. Relocations always are processed non-strictly.
87 	 * Non-relocatable instructions are replaced with invalid ones to
88 	 * prevent accidental errors.
89 	 * */
90 	LIBBPF_DEPRECATED_SINCE(0, 6, "field has no effect")
91 	bool relaxed_core_relocs;
92 	/* maps that set the 'pinning' attribute in their definition will have
93 	 * their pin_path attribute set to a file in this directory, and be
94 	 * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
95 	 */
96 	const char *pin_root_path;
97 
98 	LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__set_attach_target() on each individual bpf_program")
99 	__u32 attach_prog_fd;
100 	/* Additional kernel config content that augments and overrides
101 	 * system Kconfig for CONFIG_xxx externs.
102 	 */
103 	const char *kconfig;
104 	/* Path to the custom BTF to be used for BPF CO-RE relocations.
105 	 * This custom BTF completely replaces the use of vmlinux BTF
106 	 * for the purpose of CO-RE relocations.
107 	 * NOTE: any other BPF feature (e.g., fentry/fexit programs,
108 	 * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.
109 	 */
110 	const char *btf_custom_path;
111 	/* Pointer to a buffer for storing kernel logs for applicable BPF
112 	 * commands. Valid kernel_log_size has to be specified as well and are
113 	 * passed-through to bpf() syscall. Keep in mind that kernel might
114 	 * fail operation with -ENOSPC error if provided buffer is too small
115 	 * to contain entire log output.
116 	 * See the comment below for kernel_log_level for interaction between
117 	 * log_buf and log_level settings.
118 	 *
119 	 * If specified, this log buffer will be passed for:
120 	 *   - each BPF progral load (BPF_PROG_LOAD) attempt, unless overriden
121 	 *     with bpf_program__set_log() on per-program level, to get
122 	 *     BPF verifier log output.
123 	 *   - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get
124 	 *     BTF sanity checking log.
125 	 *
126 	 * Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite
127 	 * previous contents, so if you need more fine-grained control, set
128 	 * per-program buffer with bpf_program__set_log_buf() to preserve each
129 	 * individual program's verification log. Keep using kernel_log_buf
130 	 * for BTF verification log, if necessary.
131 	 */
132 	char *kernel_log_buf;
133 	size_t kernel_log_size;
134 	/*
135 	 * Log level can be set independently from log buffer. Log_level=0
136 	 * means that libbpf will attempt loading BTF or program without any
137 	 * logging requested, but will retry with either its own or custom log
138 	 * buffer, if provided, and log_level=1 on any error.
139 	 * And vice versa, setting log_level>0 will request BTF or prog
140 	 * loading with verbose log from the first attempt (and as such also
141 	 * for successfully loaded BTF or program), and the actual log buffer
142 	 * could be either libbpf's own auto-allocated log buffer, if
143 	 * kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.
144 	 * If user didn't provide custom log buffer, libbpf will emit captured
145 	 * logs through its print callback.
146 	 */
147 	__u32 kernel_log_level;
148 
149 	size_t :0;
150 };
151 #define bpf_object_open_opts__last_field kernel_log_level
152 
153 LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
154 
155 /**
156  * @brief **bpf_object__open_file()** creates a bpf_object by opening
157  * the BPF ELF object file pointed to by the passed path and loading it
158  * into memory.
159  * @param path BPF object file path
160  * @param opts options for how to load the bpf object, this parameter is
161  * optional and can be set to NULL
162  * @return pointer to the new bpf_object; or NULL is returned on error,
163  * error code is stored in errno
164  */
165 LIBBPF_API struct bpf_object *
166 bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
167 
168 /**
169  * @brief **bpf_object__open_mem()** creates a bpf_object by reading
170  * the BPF objects raw bytes from a memory buffer containing a valid
171  * BPF ELF object file.
172  * @param obj_buf pointer to the buffer containing ELF file bytes
173  * @param obj_buf_sz number of bytes in the buffer
174  * @param opts options for how to load the bpf object
175  * @return pointer to the new bpf_object; or NULL is returned on error,
176  * error code is stored in errno
177  */
178 LIBBPF_API struct bpf_object *
179 bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
180 		     const struct bpf_object_open_opts *opts);
181 
182 /* deprecated bpf_object__open variants */
183 LIBBPF_API struct bpf_object *
184 bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz,
185 			const char *name);
186 LIBBPF_API struct bpf_object *
187 bpf_object__open_xattr(struct bpf_object_open_attr *attr);
188 
189 enum libbpf_pin_type {
190 	LIBBPF_PIN_NONE,
191 	/* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
192 	LIBBPF_PIN_BY_NAME,
193 };
194 
195 /* pin_maps and unpin_maps can both be called with a NULL path, in which case
196  * they will use the pin_path attribute of each map (and ignore all maps that
197  * don't have a pin_path set).
198  */
199 LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
200 LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
201 				      const char *path);
202 LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
203 					const char *path);
204 LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
205 					  const char *path);
206 LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
207 LIBBPF_API void bpf_object__close(struct bpf_object *object);
208 
209 struct bpf_object_load_attr {
210 	struct bpf_object *obj;
211 	int log_level;
212 	const char *target_btf_path;
213 };
214 
215 /* Load/unload object into/from kernel */
216 LIBBPF_API int bpf_object__load(struct bpf_object *obj);
217 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__load() instead")
218 LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr);
219 LIBBPF_DEPRECATED_SINCE(0, 6, "bpf_object__unload() is deprecated, use bpf_object__close() instead")
220 LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
221 
222 LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
223 LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
224 LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);
225 
226 struct btf;
227 LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
228 LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
229 
230 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__find_program_by_name() instead")
231 LIBBPF_API struct bpf_program *
232 bpf_object__find_program_by_title(const struct bpf_object *obj,
233 				  const char *title);
234 LIBBPF_API struct bpf_program *
235 bpf_object__find_program_by_name(const struct bpf_object *obj,
236 				 const char *name);
237 
238 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "track bpf_objects in application code instead")
239 struct bpf_object *bpf_object__next(struct bpf_object *prev);
240 #define bpf_object__for_each_safe(pos, tmp)			\
241 	for ((pos) = bpf_object__next(NULL),		\
242 		(tmp) = bpf_object__next(pos);		\
243 	     (pos) != NULL;				\
244 	     (pos) = (tmp), (tmp) = bpf_object__next(tmp))
245 
246 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
247 LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
248 				    bpf_object_clear_priv_t clear_priv);
249 LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog);
250 
251 LIBBPF_API int
252 libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
253 			 enum bpf_attach_type *expected_attach_type);
254 LIBBPF_API int libbpf_attach_type_by_name(const char *name,
255 					  enum bpf_attach_type *attach_type);
256 LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
257 					  enum bpf_attach_type attach_type);
258 
259 /* Accessors of bpf_program */
260 struct bpf_program;
261 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_program() instead")
262 struct bpf_program *bpf_program__next(struct bpf_program *prog,
263 				      const struct bpf_object *obj);
264 LIBBPF_API struct bpf_program *
265 bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);
266 
267 #define bpf_object__for_each_program(pos, obj)			\
268 	for ((pos) = bpf_object__next_program((obj), NULL);	\
269 	     (pos) != NULL;					\
270 	     (pos) = bpf_object__next_program((obj), (pos)))
271 
272 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_program() instead")
273 struct bpf_program *bpf_program__prev(struct bpf_program *prog,
274 				      const struct bpf_object *obj);
275 LIBBPF_API struct bpf_program *
276 bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);
277 
278 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
279 
280 LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
281 				     bpf_program_clear_priv_t clear_priv);
282 
283 LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
284 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
285 					 __u32 ifindex);
286 
287 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
288 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
289 LIBBPF_API LIBBPF_DEPRECATED("BPF program title is confusing term; please use bpf_program__section_name() instead")
290 const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy);
291 LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
292 LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
293 
294 /* returns program size in bytes */
295 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insn_cnt() instead")
296 LIBBPF_API size_t bpf_program__size(const struct bpf_program *prog);
297 
298 struct bpf_insn;
299 
300 /**
301  * @brief **bpf_program__insns()** gives read-only access to BPF program's
302  * underlying BPF instructions.
303  * @param prog BPF program for which to return instructions
304  * @return a pointer to an array of BPF instructions that belong to the
305  * specified BPF program
306  *
307  * Returned pointer is always valid and not NULL. Number of `struct bpf_insn`
308  * pointed to can be fetched using **bpf_program__insn_cnt()** API.
309  *
310  * Keep in mind, libbpf can modify and append/delete BPF program's
311  * instructions as it processes BPF object file and prepares everything for
312  * uploading into the kernel. So depending on the point in BPF object
313  * lifetime, **bpf_program__insns()** can return different sets of
314  * instructions. As an example, during BPF object load phase BPF program
315  * instructions will be CO-RE-relocated, BPF subprograms instructions will be
316  * appended, ldimm64 instructions will have FDs embedded, etc. So instructions
317  * returned before **bpf_object__load()** and after it might be quite
318  * different.
319  */
320 LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
321 /**
322  * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
323  * that form specified BPF program.
324  * @param prog BPF program for which to return number of BPF instructions
325  *
326  * See **bpf_program__insns()** documentation for notes on how libbpf can
327  * change instructions and their count during different phases of
328  * **bpf_object** lifetime.
329  */
330 LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);
331 
332 LIBBPF_DEPRECATED_SINCE(0, 6, "use bpf_object__load() instead")
333 LIBBPF_API int bpf_program__load(struct bpf_program *prog, const char *license, __u32 kern_version);
334 LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
335 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
336 LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
337 					 const char *path,
338 					 int instance);
339 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
340 LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
341 					   const char *path,
342 					   int instance);
343 
344 /**
345  * @brief **bpf_program__pin()** pins the BPF program to a file
346  * in the BPF FS specified by a path. This increments the programs
347  * reference count, allowing it to stay loaded after the process
348  * which loaded it has exited.
349  *
350  * @param prog BPF program to pin, must already be loaded
351  * @param path file path in a BPF file system
352  * @return 0, on success; negative error code, otherwise
353  */
354 LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
355 
356 /**
357  * @brief **bpf_program__unpin()** unpins the BPF program from a file
358  * in the BPFFS specified by a path. This decrements the programs
359  * reference count.
360  *
361  * The file pinning the BPF program can also be unlinked by a different
362  * process in which case this function will return an error.
363  *
364  * @param prog BPF program to unpin
365  * @param path file path to the pin in a BPF file system
366  * @return 0, on success; negative error code, otherwise
367  */
368 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
369 LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
370 
371 struct bpf_link;
372 
373 LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
374 LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
375 LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
376 LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
377 LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
378 LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
379 					struct bpf_program *prog);
380 LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
381 LIBBPF_API int bpf_link__detach(struct bpf_link *link);
382 LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
383 
384 LIBBPF_API struct bpf_link *
385 bpf_program__attach(const struct bpf_program *prog);
386 
387 struct bpf_perf_event_opts {
388 	/* size of this struct, for forward/backward compatiblity */
389 	size_t sz;
390 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
391 	__u64 bpf_cookie;
392 };
393 #define bpf_perf_event_opts__last_field bpf_cookie
394 
395 LIBBPF_API struct bpf_link *
396 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
397 
398 LIBBPF_API struct bpf_link *
399 bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
400 				    const struct bpf_perf_event_opts *opts);
401 
402 struct bpf_kprobe_opts {
403 	/* size of this struct, for forward/backward compatiblity */
404 	size_t sz;
405 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
406 	__u64 bpf_cookie;
407 	/* function's offset to install kprobe to */
408 	size_t offset;
409 	/* kprobe is return probe */
410 	bool retprobe;
411 	size_t :0;
412 };
413 #define bpf_kprobe_opts__last_field retprobe
414 
415 LIBBPF_API struct bpf_link *
416 bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
417 			   const char *func_name);
418 LIBBPF_API struct bpf_link *
419 bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
420                                 const char *func_name,
421                                 const struct bpf_kprobe_opts *opts);
422 
423 struct bpf_uprobe_opts {
424 	/* size of this struct, for forward/backward compatiblity */
425 	size_t sz;
426 	/* offset of kernel reference counted USDT semaphore, added in
427 	 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
428 	 */
429 	size_t ref_ctr_offset;
430 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
431 	__u64 bpf_cookie;
432 	/* uprobe is return probe, invoked at function return time */
433 	bool retprobe;
434 	size_t :0;
435 };
436 #define bpf_uprobe_opts__last_field retprobe
437 
438 /**
439  * @brief **bpf_program__attach_uprobe()** attaches a BPF program
440  * to the userspace function which is found by binary path and
441  * offset. You can optionally specify a particular proccess to attach
442  * to. You can also optionally attach the program to the function
443  * exit instead of entry.
444  *
445  * @param prog BPF program to attach
446  * @param retprobe Attach to function exit
447  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
448  * -1 for all processes
449  * @param binary_path Path to binary that contains the function symbol
450  * @param func_offset Offset within the binary of the function symbol
451  * @return Reference to the newly created BPF link; or NULL is returned on error,
452  * error code is stored in errno
453  */
454 LIBBPF_API struct bpf_link *
455 bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
456 			   pid_t pid, const char *binary_path,
457 			   size_t func_offset);
458 
459 /**
460  * @brief **bpf_program__attach_uprobe_opts()** is just like
461  * bpf_program__attach_uprobe() except with a options struct
462  * for various configurations.
463  *
464  * @param prog BPF program to attach
465  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
466  * -1 for all processes
467  * @param binary_path Path to binary that contains the function symbol
468  * @param func_offset Offset within the binary of the function symbol
469  * @param opts Options for altering program attachment
470  * @return Reference to the newly created BPF link; or NULL is returned on error,
471  * error code is stored in errno
472  */
473 LIBBPF_API struct bpf_link *
474 bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
475 				const char *binary_path, size_t func_offset,
476 				const struct bpf_uprobe_opts *opts);
477 
478 struct bpf_tracepoint_opts {
479 	/* size of this struct, for forward/backward compatiblity */
480 	size_t sz;
481 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
482 	__u64 bpf_cookie;
483 };
484 #define bpf_tracepoint_opts__last_field bpf_cookie
485 
486 LIBBPF_API struct bpf_link *
487 bpf_program__attach_tracepoint(const struct bpf_program *prog,
488 			       const char *tp_category,
489 			       const char *tp_name);
490 LIBBPF_API struct bpf_link *
491 bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
492 				    const char *tp_category,
493 				    const char *tp_name,
494 				    const struct bpf_tracepoint_opts *opts);
495 
496 LIBBPF_API struct bpf_link *
497 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
498 				   const char *tp_name);
499 LIBBPF_API struct bpf_link *
500 bpf_program__attach_trace(const struct bpf_program *prog);
501 LIBBPF_API struct bpf_link *
502 bpf_program__attach_lsm(const struct bpf_program *prog);
503 LIBBPF_API struct bpf_link *
504 bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
505 LIBBPF_API struct bpf_link *
506 bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
507 LIBBPF_API struct bpf_link *
508 bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
509 LIBBPF_API struct bpf_link *
510 bpf_program__attach_freplace(const struct bpf_program *prog,
511 			     int target_fd, const char *attach_func_name);
512 
513 struct bpf_map;
514 
515 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
516 
517 struct bpf_iter_attach_opts {
518 	size_t sz; /* size of this struct for forward/backward compatibility */
519 	union bpf_iter_link_info *link_info;
520 	__u32 link_info_len;
521 };
522 #define bpf_iter_attach_opts__last_field link_info_len
523 
524 LIBBPF_API struct bpf_link *
525 bpf_program__attach_iter(const struct bpf_program *prog,
526 			 const struct bpf_iter_attach_opts *opts);
527 
528 /*
529  * Libbpf allows callers to adjust BPF programs before being loaded
530  * into kernel. One program in an object file can be transformed into
531  * multiple variants to be attached to different hooks.
532  *
533  * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
534  * form an API for this purpose.
535  *
536  * - bpf_program_prep_t:
537  *   Defines a 'preprocessor', which is a caller defined function
538  *   passed to libbpf through bpf_program__set_prep(), and will be
539  *   called before program is loaded. The processor should adjust
540  *   the program one time for each instance according to the instance id
541  *   passed to it.
542  *
543  * - bpf_program__set_prep:
544  *   Attaches a preprocessor to a BPF program. The number of instances
545  *   that should be created is also passed through this function.
546  *
547  * - bpf_program__nth_fd:
548  *   After the program is loaded, get resulting FD of a given instance
549  *   of the BPF program.
550  *
551  * If bpf_program__set_prep() is not used, the program would be loaded
552  * without adjustment during bpf_object__load(). The program has only
553  * one instance. In this case bpf_program__fd(prog) is equal to
554  * bpf_program__nth_fd(prog, 0).
555  */
556 struct bpf_prog_prep_result {
557 	/*
558 	 * If not NULL, load new instruction array.
559 	 * If set to NULL, don't load this instance.
560 	 */
561 	struct bpf_insn *new_insn_ptr;
562 	int new_insn_cnt;
563 
564 	/* If not NULL, result FD is written to it. */
565 	int *pfd;
566 };
567 
568 /*
569  * Parameters of bpf_program_prep_t:
570  *  - prog:	The bpf_program being loaded.
571  *  - n:	Index of instance being generated.
572  *  - insns:	BPF instructions array.
573  *  - insns_cnt:Number of instructions in insns.
574  *  - res:	Output parameter, result of transformation.
575  *
576  * Return value:
577  *  - Zero:	pre-processing success.
578  *  - Non-zero:	pre-processing error, stop loading.
579  */
580 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
581 				  struct bpf_insn *insns, int insns_cnt,
582 				  struct bpf_prog_prep_result *res);
583 
584 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insns() for getting bpf_program instructions")
585 LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
586 				     bpf_program_prep_t prep);
587 
588 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
589 LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n);
590 
591 /*
592  * Adjust type of BPF program. Default is kprobe.
593  */
594 LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
595 LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
596 LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
597 LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
598 LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog);
599 LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
600 LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
601 LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
602 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
603 LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
604 LIBBPF_API int bpf_program__set_struct_ops(struct bpf_program *prog);
605 LIBBPF_API int bpf_program__set_extension(struct bpf_program *prog);
606 LIBBPF_API int bpf_program__set_sk_lookup(struct bpf_program *prog);
607 
608 LIBBPF_API enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog);
609 LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
610 				      enum bpf_prog_type type);
611 
612 LIBBPF_API enum bpf_attach_type
613 bpf_program__get_expected_attach_type(const struct bpf_program *prog);
614 LIBBPF_API void
615 bpf_program__set_expected_attach_type(struct bpf_program *prog,
616 				      enum bpf_attach_type type);
617 
618 LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
619 LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
620 
621 /* Per-program log level and log buffer getters/setters.
622  * See bpf_object_open_opts comments regarding log_level and log_buf
623  * interactions.
624  */
625 LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);
626 LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);
627 LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
628 LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
629 
630 LIBBPF_API int
631 bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
632 			       const char *attach_func_name);
633 
634 LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog);
635 LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog);
636 LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog);
637 LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog);
638 LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog);
639 LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog);
640 LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog);
641 LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
642 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
643 LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
644 LIBBPF_API bool bpf_program__is_struct_ops(const struct bpf_program *prog);
645 LIBBPF_API bool bpf_program__is_extension(const struct bpf_program *prog);
646 LIBBPF_API bool bpf_program__is_sk_lookup(const struct bpf_program *prog);
647 
648 /*
649  * No need for __attribute__((packed)), all members of 'bpf_map_def'
650  * are all aligned.  In addition, using __attribute__((packed))
651  * would trigger a -Wpacked warning message, and lead to an error
652  * if -Werror is set.
653  */
654 struct bpf_map_def {
655 	unsigned int type;
656 	unsigned int key_size;
657 	unsigned int value_size;
658 	unsigned int max_entries;
659 	unsigned int map_flags;
660 };
661 
662 /**
663  * @brief **bpf_object__find_map_by_name()** returns BPF map of
664  * the given name, if it exists within the passed BPF object
665  * @param obj BPF object
666  * @param name name of the BPF map
667  * @return BPF map instance, if such map exists within the BPF object;
668  * or NULL otherwise.
669  */
670 LIBBPF_API struct bpf_map *
671 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
672 
673 LIBBPF_API int
674 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
675 
676 /*
677  * Get bpf_map through the offset of corresponding struct bpf_map_def
678  * in the BPF object file.
679  */
680 LIBBPF_API struct bpf_map *
681 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
682 
683 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_map() instead")
684 struct bpf_map *bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
685 LIBBPF_API struct bpf_map *
686 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
687 
688 #define bpf_object__for_each_map(pos, obj)		\
689 	for ((pos) = bpf_object__next_map((obj), NULL);	\
690 	     (pos) != NULL;				\
691 	     (pos) = bpf_object__next_map((obj), (pos)))
692 #define bpf_map__for_each bpf_object__for_each_map
693 
694 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_map() instead")
695 struct bpf_map *bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
696 LIBBPF_API struct bpf_map *
697 bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
698 
699 /**
700  * @brief **bpf_map__fd()** gets the file descriptor of the passed
701  * BPF map
702  * @param map the BPF map instance
703  * @return the file descriptor; or -EINVAL in case of an error
704  */
705 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
706 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
707 /* get map definition */
708 LIBBPF_API const struct bpf_map_def *bpf_map__def(const struct bpf_map *map);
709 /* get map name */
710 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
711 /* get/set map type */
712 LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
713 LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
714 /* get/set map size (max_entries) */
715 LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
716 LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
717 LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
718 /* get/set map flags */
719 LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
720 LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
721 /* get/set map NUMA node */
722 LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
723 LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
724 /* get/set map key size */
725 LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
726 LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
727 /* get/set map value size */
728 LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
729 LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
730 /* get map key/value BTF type IDs */
731 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
732 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
733 /* get/set map if_index */
734 LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
735 LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
736 /* get/set map map_extra flags */
737 LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
738 LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
739 
740 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
741 LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
742 				 bpf_map_clear_priv_t clear_priv);
743 LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
744 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
745 					  const void *data, size_t size);
746 LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
747 LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);
748 
749 /**
750  * @brief **bpf_map__is_internal()** tells the caller whether or not the
751  * passed map is a special map created by libbpf automatically for things like
752  * global variables, __ksym externs, Kconfig values, etc
753  * @param map the bpf_map
754  * @return true, if the map is an internal map; false, otherwise
755  */
756 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
757 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
758 LIBBPF_API const char *bpf_map__get_pin_path(const struct bpf_map *map);
759 LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
760 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
761 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
762 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
763 
764 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
765 LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
766 
767 /**
768  * @brief **libbpf_get_error()** extracts the error code from the passed
769  * pointer
770  * @param ptr pointer returned from libbpf API function
771  * @return error code; or 0 if no error occured
772  *
773  * Many libbpf API functions which return pointers have logic to encode error
774  * codes as pointers, and do not return NULL. Meaning **libbpf_get_error()**
775  * should be used on the return value from these functions immediately after
776  * calling the API function, with no intervening calls that could clobber the
777  * `errno` variable. Consult the individual functions documentation to verify
778  * if this logic applies should be used.
779  *
780  * For these API functions, if `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)`
781  * is enabled, NULL is returned on error instead.
782  *
783  * If ptr is NULL, then errno should be already set by the failing
784  * API, because libbpf never returns NULL on success and it now always
785  * sets errno on error.
786  *
787  * Example usage:
788  *
789  *   struct perf_buffer *pb;
790  *
791  *   pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES, &opts);
792  *   err = libbpf_get_error(pb);
793  *   if (err) {
794  *	  pb = NULL;
795  *	  fprintf(stderr, "failed to open perf buffer: %d\n", err);
796  *	  goto cleanup;
797  *   }
798  */
799 LIBBPF_API long libbpf_get_error(const void *ptr);
800 
801 struct bpf_prog_load_attr {
802 	const char *file;
803 	enum bpf_prog_type prog_type;
804 	enum bpf_attach_type expected_attach_type;
805 	int ifindex;
806 	int log_level;
807 	int prog_flags;
808 };
809 
810 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__open() and bpf_object__load() instead")
811 LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
812 				   struct bpf_object **pobj, int *prog_fd);
813 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__open() and bpf_object__load() instead")
814 LIBBPF_API int bpf_prog_load_deprecated(const char *file, enum bpf_prog_type type,
815 					struct bpf_object **pobj, int *prog_fd);
816 
817 /* XDP related API */
818 struct xdp_link_info {
819 	__u32 prog_id;
820 	__u32 drv_prog_id;
821 	__u32 hw_prog_id;
822 	__u32 skb_prog_id;
823 	__u8 attach_mode;
824 };
825 
826 struct bpf_xdp_set_link_opts {
827 	size_t sz;
828 	int old_fd;
829 	size_t :0;
830 };
831 #define bpf_xdp_set_link_opts__last_field old_fd
832 
833 LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
834 LIBBPF_API int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
835 					const struct bpf_xdp_set_link_opts *opts);
836 LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
837 LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
838 				     size_t info_size, __u32 flags);
839 
840 /* TC related API */
841 enum bpf_tc_attach_point {
842 	BPF_TC_INGRESS = 1 << 0,
843 	BPF_TC_EGRESS  = 1 << 1,
844 	BPF_TC_CUSTOM  = 1 << 2,
845 };
846 
847 #define BPF_TC_PARENT(a, b) 	\
848 	((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
849 
850 enum bpf_tc_flags {
851 	BPF_TC_F_REPLACE = 1 << 0,
852 };
853 
854 struct bpf_tc_hook {
855 	size_t sz;
856 	int ifindex;
857 	enum bpf_tc_attach_point attach_point;
858 	__u32 parent;
859 	size_t :0;
860 };
861 #define bpf_tc_hook__last_field parent
862 
863 struct bpf_tc_opts {
864 	size_t sz;
865 	int prog_fd;
866 	__u32 flags;
867 	__u32 prog_id;
868 	__u32 handle;
869 	__u32 priority;
870 	size_t :0;
871 };
872 #define bpf_tc_opts__last_field priority
873 
874 LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
875 LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
876 LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
877 			     struct bpf_tc_opts *opts);
878 LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
879 			     const struct bpf_tc_opts *opts);
880 LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
881 			    struct bpf_tc_opts *opts);
882 
883 /* Ring buffer APIs */
884 struct ring_buffer;
885 
886 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
887 
888 struct ring_buffer_opts {
889 	size_t sz; /* size of this struct, for forward/backward compatiblity */
890 };
891 
892 #define ring_buffer_opts__last_field sz
893 
894 LIBBPF_API struct ring_buffer *
895 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
896 		 const struct ring_buffer_opts *opts);
897 LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
898 LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
899 				ring_buffer_sample_fn sample_cb, void *ctx);
900 LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
901 LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
902 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
903 
904 /* Perf buffer APIs */
905 struct perf_buffer;
906 
907 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
908 				      void *data, __u32 size);
909 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
910 
911 /* common use perf buffer options */
912 struct perf_buffer_opts {
913 	union {
914 		size_t sz;
915 		struct { /* DEPRECATED: will be removed in v1.0 */
916 			/* if specified, sample_cb is called for each sample */
917 			perf_buffer_sample_fn sample_cb;
918 			/* if specified, lost_cb is called for each batch of lost samples */
919 			perf_buffer_lost_fn lost_cb;
920 			/* ctx is provided to sample_cb and lost_cb */
921 			void *ctx;
922 		};
923 	};
924 };
925 #define perf_buffer_opts__last_field sz
926 
927 /**
928  * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
929  * BPF_PERF_EVENT_ARRAY map
930  * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
931  * code to send data over to user-space
932  * @param page_cnt number of memory pages allocated for each per-CPU buffer
933  * @param sample_cb function called on each received data record
934  * @param lost_cb function called when record loss has occurred
935  * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
936  * @return a new instance of struct perf_buffer on success, NULL on error with
937  * *errno* containing an error code
938  */
939 LIBBPF_API struct perf_buffer *
940 perf_buffer__new(int map_fd, size_t page_cnt,
941 		 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
942 		 const struct perf_buffer_opts *opts);
943 
944 LIBBPF_API struct perf_buffer *
945 perf_buffer__new_v0_6_0(int map_fd, size_t page_cnt,
946 			perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
947 			const struct perf_buffer_opts *opts);
948 
949 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use new variant of perf_buffer__new() instead")
950 struct perf_buffer *perf_buffer__new_deprecated(int map_fd, size_t page_cnt,
951 						const struct perf_buffer_opts *opts);
952 
953 #define perf_buffer__new(...) ___libbpf_overload(___perf_buffer_new, __VA_ARGS__)
954 #define ___perf_buffer_new6(map_fd, page_cnt, sample_cb, lost_cb, ctx, opts) \
955 	perf_buffer__new(map_fd, page_cnt, sample_cb, lost_cb, ctx, opts)
956 #define ___perf_buffer_new3(map_fd, page_cnt, opts) \
957 	perf_buffer__new_deprecated(map_fd, page_cnt, opts)
958 
959 enum bpf_perf_event_ret {
960 	LIBBPF_PERF_EVENT_DONE	= 0,
961 	LIBBPF_PERF_EVENT_ERROR	= -1,
962 	LIBBPF_PERF_EVENT_CONT	= -2,
963 };
964 
965 struct perf_event_header;
966 
967 typedef enum bpf_perf_event_ret
968 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
969 
970 /* raw perf buffer options, giving most power and control */
971 struct perf_buffer_raw_opts {
972 	union {
973 		struct {
974 			size_t sz;
975 			long :0;
976 			long :0;
977 		};
978 		struct { /* DEPRECATED: will be removed in v1.0 */
979 			/* perf event attrs passed directly into perf_event_open() */
980 			struct perf_event_attr *attr;
981 			/* raw event callback */
982 			perf_buffer_event_fn event_cb;
983 			/* ctx is provided to event_cb */
984 			void *ctx;
985 		};
986 	};
987 	/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
988 	 * max_entries of given PERF_EVENT_ARRAY map)
989 	 */
990 	int cpu_cnt;
991 	/* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
992 	int *cpus;
993 	/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
994 	int *map_keys;
995 };
996 #define perf_buffer_raw_opts__last_field map_keys
997 
998 LIBBPF_API struct perf_buffer *
999 perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1000 		     perf_buffer_event_fn event_cb, void *ctx,
1001 		     const struct perf_buffer_raw_opts *opts);
1002 
1003 LIBBPF_API struct perf_buffer *
1004 perf_buffer__new_raw_v0_6_0(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1005 			    perf_buffer_event_fn event_cb, void *ctx,
1006 			    const struct perf_buffer_raw_opts *opts);
1007 
1008 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use new variant of perf_buffer__new_raw() instead")
1009 struct perf_buffer *perf_buffer__new_raw_deprecated(int map_fd, size_t page_cnt,
1010 						    const struct perf_buffer_raw_opts *opts);
1011 
1012 #define perf_buffer__new_raw(...) ___libbpf_overload(___perf_buffer_new_raw, __VA_ARGS__)
1013 #define ___perf_buffer_new_raw6(map_fd, page_cnt, attr, event_cb, ctx, opts) \
1014 	perf_buffer__new_raw(map_fd, page_cnt, attr, event_cb, ctx, opts)
1015 #define ___perf_buffer_new_raw3(map_fd, page_cnt, opts) \
1016 	perf_buffer__new_raw_deprecated(map_fd, page_cnt, opts)
1017 
1018 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
1019 LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
1020 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
1021 LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
1022 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
1023 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
1024 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
1025 
1026 typedef enum bpf_perf_event_ret
1027 	(*bpf_perf_event_print_t)(struct perf_event_header *hdr,
1028 				  void *private_data);
1029 LIBBPF_API enum bpf_perf_event_ret
1030 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
1031 			   void **copy_mem, size_t *copy_size,
1032 			   bpf_perf_event_print_t fn, void *private_data);
1033 
1034 struct bpf_prog_linfo;
1035 struct bpf_prog_info;
1036 
1037 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
1038 LIBBPF_API struct bpf_prog_linfo *
1039 bpf_prog_linfo__new(const struct bpf_prog_info *info);
1040 LIBBPF_API const struct bpf_line_info *
1041 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
1042 				__u64 addr, __u32 func_idx, __u32 nr_skip);
1043 LIBBPF_API const struct bpf_line_info *
1044 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
1045 		      __u32 insn_off, __u32 nr_skip);
1046 
1047 /*
1048  * Probe for supported system features
1049  *
1050  * Note that running many of these probes in a short amount of time can cause
1051  * the kernel to reach the maximal size of lockable memory allowed for the
1052  * user, causing subsequent probes to fail. In this case, the caller may want
1053  * to adjust that limit with setrlimit().
1054  */
1055 LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_prog_type() instead")
1056 LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type, __u32 ifindex);
1057 LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_map_type() instead")
1058 LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
1059 LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_helper() instead")
1060 LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id, enum bpf_prog_type prog_type, __u32 ifindex);
1061 LIBBPF_DEPRECATED_SINCE(0, 8, "implement your own or use bpftool for feature detection")
1062 LIBBPF_API bool bpf_probe_large_insn_limit(__u32 ifindex);
1063 
1064 /**
1065  * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports
1066  * BPF programs of a given type.
1067  * @param prog_type BPF program type to detect kernel support for
1068  * @param opts reserved for future extensibility, should be NULL
1069  * @return 1, if given program type is supported; 0, if given program type is
1070  * not supported; negative error code if feature detection failed or can't be
1071  * performed
1072  *
1073  * Make sure the process has required set of CAP_* permissions (or runs as
1074  * root) when performing feature checking.
1075  */
1076 LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);
1077 /**
1078  * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports
1079  * BPF maps of a given type.
1080  * @param map_type BPF map type to detect kernel support for
1081  * @param opts reserved for future extensibility, should be NULL
1082  * @return 1, if given map type is supported; 0, if given map type is
1083  * not supported; negative error code if feature detection failed or can't be
1084  * performed
1085  *
1086  * Make sure the process has required set of CAP_* permissions (or runs as
1087  * root) when performing feature checking.
1088  */
1089 LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);
1090 /**
1091  * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the
1092  * use of a given BPF helper from specified BPF program type.
1093  * @param prog_type BPF program type used to check the support of BPF helper
1094  * @param helper_id BPF helper ID (enum bpf_func_id) to check support for
1095  * @param opts reserved for future extensibility, should be NULL
1096  * @return 1, if given combination of program type and helper is supported; 0,
1097  * if the combination is not supported; negative error code if feature
1098  * detection for provided input arguments failed or can't be performed
1099  *
1100  * Make sure the process has required set of CAP_* permissions (or runs as
1101  * root) when performing feature checking.
1102  */
1103 LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
1104 				       enum bpf_func_id helper_id, const void *opts);
1105 
1106 /*
1107  * Get bpf_prog_info in continuous memory
1108  *
1109  * struct bpf_prog_info has multiple arrays. The user has option to choose
1110  * arrays to fetch from kernel. The following APIs provide an uniform way to
1111  * fetch these data. All arrays in bpf_prog_info are stored in a single
1112  * continuous memory region. This makes it easy to store the info in a
1113  * file.
1114  *
1115  * Before writing bpf_prog_info_linear to files, it is necessary to
1116  * translate pointers in bpf_prog_info to offsets. Helper functions
1117  * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr()
1118  * are introduced to switch between pointers and offsets.
1119  *
1120  * Examples:
1121  *   # To fetch map_ids and prog_tags:
1122  *   __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) |
1123  *           (1UL << BPF_PROG_INFO_PROG_TAGS);
1124  *   struct bpf_prog_info_linear *info_linear =
1125  *           bpf_program__get_prog_info_linear(fd, arrays);
1126  *
1127  *   # To save data in file
1128  *   bpf_program__bpil_addr_to_offs(info_linear);
1129  *   write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
1130  *
1131  *   # To read data from file
1132  *   read(f, info_linear, <proper_size>);
1133  *   bpf_program__bpil_offs_to_addr(info_linear);
1134  */
1135 enum bpf_prog_info_array {
1136 	BPF_PROG_INFO_FIRST_ARRAY = 0,
1137 	BPF_PROG_INFO_JITED_INSNS = 0,
1138 	BPF_PROG_INFO_XLATED_INSNS,
1139 	BPF_PROG_INFO_MAP_IDS,
1140 	BPF_PROG_INFO_JITED_KSYMS,
1141 	BPF_PROG_INFO_JITED_FUNC_LENS,
1142 	BPF_PROG_INFO_FUNC_INFO,
1143 	BPF_PROG_INFO_LINE_INFO,
1144 	BPF_PROG_INFO_JITED_LINE_INFO,
1145 	BPF_PROG_INFO_PROG_TAGS,
1146 	BPF_PROG_INFO_LAST_ARRAY,
1147 };
1148 
1149 struct bpf_prog_info_linear {
1150 	/* size of struct bpf_prog_info, when the tool is compiled */
1151 	__u32			info_len;
1152 	/* total bytes allocated for data, round up to 8 bytes */
1153 	__u32			data_len;
1154 	/* which arrays are included in data */
1155 	__u64			arrays;
1156 	struct bpf_prog_info	info;
1157 	__u8			data[];
1158 };
1159 
1160 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1161 LIBBPF_API struct bpf_prog_info_linear *
1162 bpf_program__get_prog_info_linear(int fd, __u64 arrays);
1163 
1164 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1165 LIBBPF_API void
1166 bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
1167 
1168 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1169 LIBBPF_API void
1170 bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
1171 
1172 /**
1173  * @brief **libbpf_num_possible_cpus()** is a helper function to get the
1174  * number of possible CPUs that the host kernel supports and expects.
1175  * @return number of possible CPUs; or error code on failure
1176  *
1177  * Example usage:
1178  *
1179  *     int ncpus = libbpf_num_possible_cpus();
1180  *     if (ncpus < 0) {
1181  *          // error handling
1182  *     }
1183  *     long values[ncpus];
1184  *     bpf_map_lookup_elem(per_cpu_map_fd, key, values);
1185  */
1186 LIBBPF_API int libbpf_num_possible_cpus(void);
1187 
1188 struct bpf_map_skeleton {
1189 	const char *name;
1190 	struct bpf_map **map;
1191 	void **mmaped;
1192 };
1193 
1194 struct bpf_prog_skeleton {
1195 	const char *name;
1196 	struct bpf_program **prog;
1197 	struct bpf_link **link;
1198 };
1199 
1200 struct bpf_object_skeleton {
1201 	size_t sz; /* size of this struct, for forward/backward compatibility */
1202 
1203 	const char *name;
1204 	const void *data;
1205 	size_t data_sz;
1206 
1207 	struct bpf_object **obj;
1208 
1209 	int map_cnt;
1210 	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1211 	struct bpf_map_skeleton *maps;
1212 
1213 	int prog_cnt;
1214 	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1215 	struct bpf_prog_skeleton *progs;
1216 };
1217 
1218 LIBBPF_API int
1219 bpf_object__open_skeleton(struct bpf_object_skeleton *s,
1220 			  const struct bpf_object_open_opts *opts);
1221 LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
1222 LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
1223 LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
1224 LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
1225 
1226 struct gen_loader_opts {
1227 	size_t sz; /* size of this struct, for forward/backward compatiblity */
1228 	const char *data;
1229 	const char *insns;
1230 	__u32 data_sz;
1231 	__u32 insns_sz;
1232 };
1233 
1234 #define gen_loader_opts__last_field insns_sz
1235 LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
1236 				      struct gen_loader_opts *opts);
1237 
1238 enum libbpf_tristate {
1239 	TRI_NO = 0,
1240 	TRI_YES = 1,
1241 	TRI_MODULE = 2,
1242 };
1243 
1244 struct bpf_linker_opts {
1245 	/* size of this struct, for forward/backward compatiblity */
1246 	size_t sz;
1247 };
1248 #define bpf_linker_opts__last_field sz
1249 
1250 struct bpf_linker_file_opts {
1251 	/* size of this struct, for forward/backward compatiblity */
1252 	size_t sz;
1253 };
1254 #define bpf_linker_file_opts__last_field sz
1255 
1256 struct bpf_linker;
1257 
1258 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
1259 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
1260 				    const char *filename,
1261 				    const struct bpf_linker_file_opts *opts);
1262 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
1263 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
1264 
1265 #ifdef __cplusplus
1266 } /* extern "C" */
1267 #endif
1268 
1269 #endif /* __LIBBPF_LIBBPF_H */
1270