xref: /linux/tools/lib/bpf/libbpf.h (revision 1ba5ad36e00f46e3f7676f5de6b87f5a2f57f1f1)
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 /**
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  */
61 LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t);
62 
63 /**
64  * @brief **libbpf_bpf_map_type_str()** converts the provided map type value
65  * into a textual representation.
66  * @param t The map type.
67  * @return Pointer to a static string identifying the map type. NULL is
68  * returned for unknown **bpf_map_type** values.
69  */
70 LIBBPF_API const char *libbpf_bpf_map_type_str(enum bpf_map_type t);
71 
72 /**
73  * @brief **libbpf_bpf_prog_type_str()** converts the provided program type
74  * value into a textual representation.
75  * @param t The program type.
76  * @return Pointer to a static string identifying the program type. NULL is
77  * returned for unknown **bpf_prog_type** values.
78  */
79 LIBBPF_API const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t);
80 
81 enum libbpf_print_level {
82         LIBBPF_WARN,
83         LIBBPF_INFO,
84         LIBBPF_DEBUG,
85 };
86 
87 typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
88 				 const char *, va_list ap);
89 
90 LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
91 
92 /* Hide internal to user */
93 struct bpf_object;
94 
95 struct bpf_object_open_attr {
96 	const char *file;
97 	enum bpf_prog_type prog_type;
98 };
99 
100 struct bpf_object_open_opts {
101 	/* size of this struct, for forward/backward compatiblity */
102 	size_t sz;
103 	/* object name override, if provided:
104 	 * - for object open from file, this will override setting object
105 	 *   name from file path's base name;
106 	 * - for object open from memory buffer, this will specify an object
107 	 *   name and will override default "<addr>-<buf-size>" name;
108 	 */
109 	const char *object_name;
110 	/* parse map definitions non-strictly, allowing extra attributes/data */
111 	bool relaxed_maps;
112 	/* DEPRECATED: handle CO-RE relocations non-strictly, allowing failures.
113 	 * Value is ignored. Relocations always are processed non-strictly.
114 	 * Non-relocatable instructions are replaced with invalid ones to
115 	 * prevent accidental errors.
116 	 * */
117 	LIBBPF_DEPRECATED_SINCE(0, 6, "field has no effect")
118 	bool relaxed_core_relocs;
119 	/* maps that set the 'pinning' attribute in their definition will have
120 	 * their pin_path attribute set to a file in this directory, and be
121 	 * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
122 	 */
123 	const char *pin_root_path;
124 
125 	LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__set_attach_target() on each individual bpf_program")
126 	__u32 attach_prog_fd;
127 	/* Additional kernel config content that augments and overrides
128 	 * system Kconfig for CONFIG_xxx externs.
129 	 */
130 	const char *kconfig;
131 	/* Path to the custom BTF to be used for BPF CO-RE relocations.
132 	 * This custom BTF completely replaces the use of vmlinux BTF
133 	 * for the purpose of CO-RE relocations.
134 	 * NOTE: any other BPF feature (e.g., fentry/fexit programs,
135 	 * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.
136 	 */
137 	const char *btf_custom_path;
138 	/* Pointer to a buffer for storing kernel logs for applicable BPF
139 	 * commands. Valid kernel_log_size has to be specified as well and are
140 	 * passed-through to bpf() syscall. Keep in mind that kernel might
141 	 * fail operation with -ENOSPC error if provided buffer is too small
142 	 * to contain entire log output.
143 	 * See the comment below for kernel_log_level for interaction between
144 	 * log_buf and log_level settings.
145 	 *
146 	 * If specified, this log buffer will be passed for:
147 	 *   - each BPF progral load (BPF_PROG_LOAD) attempt, unless overriden
148 	 *     with bpf_program__set_log() on per-program level, to get
149 	 *     BPF verifier log output.
150 	 *   - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get
151 	 *     BTF sanity checking log.
152 	 *
153 	 * Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite
154 	 * previous contents, so if you need more fine-grained control, set
155 	 * per-program buffer with bpf_program__set_log_buf() to preserve each
156 	 * individual program's verification log. Keep using kernel_log_buf
157 	 * for BTF verification log, if necessary.
158 	 */
159 	char *kernel_log_buf;
160 	size_t kernel_log_size;
161 	/*
162 	 * Log level can be set independently from log buffer. Log_level=0
163 	 * means that libbpf will attempt loading BTF or program without any
164 	 * logging requested, but will retry with either its own or custom log
165 	 * buffer, if provided, and log_level=1 on any error.
166 	 * And vice versa, setting log_level>0 will request BTF or prog
167 	 * loading with verbose log from the first attempt (and as such also
168 	 * for successfully loaded BTF or program), and the actual log buffer
169 	 * could be either libbpf's own auto-allocated log buffer, if
170 	 * kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.
171 	 * If user didn't provide custom log buffer, libbpf will emit captured
172 	 * logs through its print callback.
173 	 */
174 	__u32 kernel_log_level;
175 
176 	size_t :0;
177 };
178 #define bpf_object_open_opts__last_field kernel_log_level
179 
180 LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
181 
182 /**
183  * @brief **bpf_object__open_file()** creates a bpf_object by opening
184  * the BPF ELF object file pointed to by the passed path and loading it
185  * into memory.
186  * @param path BPF object file path
187  * @param opts options for how to load the bpf object, this parameter is
188  * optional and can be set to NULL
189  * @return pointer to the new bpf_object; or NULL is returned on error,
190  * error code is stored in errno
191  */
192 LIBBPF_API struct bpf_object *
193 bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
194 
195 /**
196  * @brief **bpf_object__open_mem()** creates a bpf_object by reading
197  * the BPF objects raw bytes from a memory buffer containing a valid
198  * BPF ELF object file.
199  * @param obj_buf pointer to the buffer containing ELF file bytes
200  * @param obj_buf_sz number of bytes in the buffer
201  * @param opts options for how to load the bpf object
202  * @return pointer to the new bpf_object; or NULL is returned on error,
203  * error code is stored in errno
204  */
205 LIBBPF_API struct bpf_object *
206 bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
207 		     const struct bpf_object_open_opts *opts);
208 
209 /* deprecated bpf_object__open variants */
210 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__open_mem() instead")
211 LIBBPF_API struct bpf_object *
212 bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz,
213 			const char *name);
214 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__open_file() instead")
215 LIBBPF_API struct bpf_object *
216 bpf_object__open_xattr(struct bpf_object_open_attr *attr);
217 
218 enum libbpf_pin_type {
219 	LIBBPF_PIN_NONE,
220 	/* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
221 	LIBBPF_PIN_BY_NAME,
222 };
223 
224 /* pin_maps and unpin_maps can both be called with a NULL path, in which case
225  * they will use the pin_path attribute of each map (and ignore all maps that
226  * don't have a pin_path set).
227  */
228 LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
229 LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
230 				      const char *path);
231 LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
232 					const char *path);
233 LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
234 					  const char *path);
235 LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
236 LIBBPF_API void bpf_object__close(struct bpf_object *object);
237 
238 struct bpf_object_load_attr {
239 	struct bpf_object *obj;
240 	int log_level;
241 	const char *target_btf_path;
242 };
243 
244 /* Load/unload object into/from kernel */
245 LIBBPF_API int bpf_object__load(struct bpf_object *obj);
246 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__load() instead")
247 LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr);
248 LIBBPF_DEPRECATED_SINCE(0, 6, "bpf_object__unload() is deprecated, use bpf_object__close() instead")
249 LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
250 
251 LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
252 LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
253 LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);
254 
255 struct btf;
256 LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
257 LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
258 
259 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__find_program_by_name() instead")
260 LIBBPF_API struct bpf_program *
261 bpf_object__find_program_by_title(const struct bpf_object *obj,
262 				  const char *title);
263 LIBBPF_API struct bpf_program *
264 bpf_object__find_program_by_name(const struct bpf_object *obj,
265 				 const char *name);
266 
267 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "track bpf_objects in application code instead")
268 struct bpf_object *bpf_object__next(struct bpf_object *prev);
269 #define bpf_object__for_each_safe(pos, tmp)			\
270 	for ((pos) = bpf_object__next(NULL),		\
271 		(tmp) = bpf_object__next(pos);		\
272 	     (pos) != NULL;				\
273 	     (pos) = (tmp), (tmp) = bpf_object__next(tmp))
274 
275 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
276 LIBBPF_DEPRECATED_SINCE(0, 7, "storage via set_priv/priv is deprecated")
277 LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
278 				    bpf_object_clear_priv_t clear_priv);
279 LIBBPF_DEPRECATED_SINCE(0, 7, "storage via set_priv/priv is deprecated")
280 LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog);
281 
282 LIBBPF_API int
283 libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
284 			 enum bpf_attach_type *expected_attach_type);
285 LIBBPF_API int libbpf_attach_type_by_name(const char *name,
286 					  enum bpf_attach_type *attach_type);
287 LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
288 					  enum bpf_attach_type attach_type);
289 
290 /* Accessors of bpf_program */
291 struct bpf_program;
292 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_program() instead")
293 struct bpf_program *bpf_program__next(struct bpf_program *prog,
294 				      const struct bpf_object *obj);
295 LIBBPF_API struct bpf_program *
296 bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);
297 
298 #define bpf_object__for_each_program(pos, obj)			\
299 	for ((pos) = bpf_object__next_program((obj), NULL);	\
300 	     (pos) != NULL;					\
301 	     (pos) = bpf_object__next_program((obj), (pos)))
302 
303 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_program() instead")
304 struct bpf_program *bpf_program__prev(struct bpf_program *prog,
305 				      const struct bpf_object *obj);
306 LIBBPF_API struct bpf_program *
307 bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);
308 
309 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
310 
311 LIBBPF_DEPRECATED_SINCE(0, 7, "storage via set_priv/priv is deprecated")
312 LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
313 				     bpf_program_clear_priv_t clear_priv);
314 LIBBPF_DEPRECATED_SINCE(0, 7, "storage via set_priv/priv is deprecated")
315 LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
316 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
317 					 __u32 ifindex);
318 
319 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
320 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
321 LIBBPF_API LIBBPF_DEPRECATED("BPF program title is confusing term; please use bpf_program__section_name() instead")
322 const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy);
323 LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
324 LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
325 
326 /* returns program size in bytes */
327 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insn_cnt() instead")
328 LIBBPF_API size_t bpf_program__size(const struct bpf_program *prog);
329 
330 struct bpf_insn;
331 
332 /**
333  * @brief **bpf_program__insns()** gives read-only access to BPF program's
334  * underlying BPF instructions.
335  * @param prog BPF program for which to return instructions
336  * @return a pointer to an array of BPF instructions that belong to the
337  * specified BPF program
338  *
339  * Returned pointer is always valid and not NULL. Number of `struct bpf_insn`
340  * pointed to can be fetched using **bpf_program__insn_cnt()** API.
341  *
342  * Keep in mind, libbpf can modify and append/delete BPF program's
343  * instructions as it processes BPF object file and prepares everything for
344  * uploading into the kernel. So depending on the point in BPF object
345  * lifetime, **bpf_program__insns()** can return different sets of
346  * instructions. As an example, during BPF object load phase BPF program
347  * instructions will be CO-RE-relocated, BPF subprograms instructions will be
348  * appended, ldimm64 instructions will have FDs embedded, etc. So instructions
349  * returned before **bpf_object__load()** and after it might be quite
350  * different.
351  */
352 LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
353 
354 /**
355  * @brief **bpf_program__set_insns()** can set BPF program's underlying
356  * BPF instructions.
357  *
358  * WARNING: This is a very advanced libbpf API and users need to know
359  * what they are doing. This should be used from prog_prepare_load_fn
360  * callback only.
361  *
362  * @param prog BPF program for which to return instructions
363  * @param new_insns a pointer to an array of BPF instructions
364  * @param new_insn_cnt number of `struct bpf_insn`'s that form
365  * specified BPF program
366  * @return 0, on success; negative error code, otherwise
367  */
368 LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog,
369 				      struct bpf_insn *new_insns, size_t new_insn_cnt);
370 
371 /**
372  * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
373  * that form specified BPF program.
374  * @param prog BPF program for which to return number of BPF instructions
375  *
376  * See **bpf_program__insns()** documentation for notes on how libbpf can
377  * change instructions and their count during different phases of
378  * **bpf_object** lifetime.
379  */
380 LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);
381 
382 LIBBPF_DEPRECATED_SINCE(0, 6, "use bpf_object__load() instead")
383 LIBBPF_API int bpf_program__load(struct bpf_program *prog, const char *license, __u32 kern_version);
384 LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
385 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
386 LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
387 					 const char *path,
388 					 int instance);
389 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
390 LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
391 					   const char *path,
392 					   int instance);
393 
394 /**
395  * @brief **bpf_program__pin()** pins the BPF program to a file
396  * in the BPF FS specified by a path. This increments the programs
397  * reference count, allowing it to stay loaded after the process
398  * which loaded it has exited.
399  *
400  * @param prog BPF program to pin, must already be loaded
401  * @param path file path in a BPF file system
402  * @return 0, on success; negative error code, otherwise
403  */
404 LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
405 
406 /**
407  * @brief **bpf_program__unpin()** unpins the BPF program from a file
408  * in the BPFFS specified by a path. This decrements the programs
409  * reference count.
410  *
411  * The file pinning the BPF program can also be unlinked by a different
412  * process in which case this function will return an error.
413  *
414  * @param prog BPF program to unpin
415  * @param path file path to the pin in a BPF file system
416  * @return 0, on success; negative error code, otherwise
417  */
418 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
419 LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
420 
421 struct bpf_link;
422 
423 LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
424 LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
425 LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
426 /**
427  * @brief **bpf_link__pin()** pins the BPF link to a file
428  * in the BPF FS specified by a path. This increments the links
429  * reference count, allowing it to stay loaded after the process
430  * which loaded it has exited.
431  *
432  * @param link BPF link to pin, must already be loaded
433  * @param path file path in a BPF file system
434  * @return 0, on success; negative error code, otherwise
435  */
436 
437 LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
438 
439 /**
440  * @brief **bpf_link__unpin()** unpins the BPF link from a file
441  * in the BPFFS specified by a path. This decrements the links
442  * reference count.
443  *
444  * The file pinning the BPF link can also be unlinked by a different
445  * process in which case this function will return an error.
446  *
447  * @param prog BPF program to unpin
448  * @param path file path to the pin in a BPF file system
449  * @return 0, on success; negative error code, otherwise
450  */
451 LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
452 LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
453 					struct bpf_program *prog);
454 LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
455 LIBBPF_API int bpf_link__detach(struct bpf_link *link);
456 LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
457 
458 /**
459  * @brief **bpf_program__attach()** is a generic function for attaching
460  * a BPF program based on auto-detection of program type, attach type,
461  * and extra paremeters, where applicable.
462  *
463  * @param prog BPF program to attach
464  * @return Reference to the newly created BPF link; or NULL is returned on error,
465  * error code is stored in errno
466  *
467  * This is supported for:
468  *   - kprobe/kretprobe (depends on SEC() definition)
469  *   - uprobe/uretprobe (depends on SEC() definition)
470  *   - tracepoint
471  *   - raw tracepoint
472  *   - tracing programs (typed raw TP/fentry/fexit/fmod_ret)
473  */
474 LIBBPF_API struct bpf_link *
475 bpf_program__attach(const struct bpf_program *prog);
476 
477 struct bpf_perf_event_opts {
478 	/* size of this struct, for forward/backward compatiblity */
479 	size_t sz;
480 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
481 	__u64 bpf_cookie;
482 };
483 #define bpf_perf_event_opts__last_field bpf_cookie
484 
485 LIBBPF_API struct bpf_link *
486 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
487 
488 LIBBPF_API struct bpf_link *
489 bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
490 				    const struct bpf_perf_event_opts *opts);
491 
492 struct bpf_kprobe_opts {
493 	/* size of this struct, for forward/backward compatiblity */
494 	size_t sz;
495 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
496 	__u64 bpf_cookie;
497 	/* function's offset to install kprobe to */
498 	size_t offset;
499 	/* kprobe is return probe */
500 	bool retprobe;
501 	size_t :0;
502 };
503 #define bpf_kprobe_opts__last_field retprobe
504 
505 LIBBPF_API struct bpf_link *
506 bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
507 			   const char *func_name);
508 LIBBPF_API struct bpf_link *
509 bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
510                                 const char *func_name,
511                                 const struct bpf_kprobe_opts *opts);
512 
513 struct bpf_kprobe_multi_opts {
514 	/* size of this struct, for forward/backward compatibility */
515 	size_t sz;
516 	/* array of function symbols to attach */
517 	const char **syms;
518 	/* array of function addresses to attach */
519 	const unsigned long *addrs;
520 	/* array of user-provided values fetchable through bpf_get_attach_cookie */
521 	const __u64 *cookies;
522 	/* number of elements in syms/addrs/cookies arrays */
523 	size_t cnt;
524 	/* create return kprobes */
525 	bool retprobe;
526 	size_t :0;
527 };
528 
529 #define bpf_kprobe_multi_opts__last_field retprobe
530 
531 LIBBPF_API struct bpf_link *
532 bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
533 				      const char *pattern,
534 				      const struct bpf_kprobe_multi_opts *opts);
535 
536 struct bpf_uprobe_opts {
537 	/* size of this struct, for forward/backward compatiblity */
538 	size_t sz;
539 	/* offset of kernel reference counted USDT semaphore, added in
540 	 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
541 	 */
542 	size_t ref_ctr_offset;
543 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
544 	__u64 bpf_cookie;
545 	/* uprobe is return probe, invoked at function return time */
546 	bool retprobe;
547 	/* Function name to attach to.  Could be an unqualified ("abc") or library-qualified
548 	 * "abc@LIBXYZ" name.  To specify function entry, func_name should be set while
549 	 * func_offset argument to bpf_prog__attach_uprobe_opts() should be 0.  To trace an
550 	 * offset within a function, specify func_name and use func_offset argument to specify
551 	 * offset within the function.  Shared library functions must specify the shared library
552 	 * binary_path.
553 	 */
554 	const char *func_name;
555 	size_t :0;
556 };
557 #define bpf_uprobe_opts__last_field func_name
558 
559 /**
560  * @brief **bpf_program__attach_uprobe()** attaches a BPF program
561  * to the userspace function which is found by binary path and
562  * offset. You can optionally specify a particular proccess to attach
563  * to. You can also optionally attach the program to the function
564  * exit instead of entry.
565  *
566  * @param prog BPF program to attach
567  * @param retprobe Attach to function exit
568  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
569  * -1 for all processes
570  * @param binary_path Path to binary that contains the function symbol
571  * @param func_offset Offset within the binary of the function symbol
572  * @return Reference to the newly created BPF link; or NULL is returned on error,
573  * error code is stored in errno
574  */
575 LIBBPF_API struct bpf_link *
576 bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
577 			   pid_t pid, const char *binary_path,
578 			   size_t func_offset);
579 
580 /**
581  * @brief **bpf_program__attach_uprobe_opts()** is just like
582  * bpf_program__attach_uprobe() except with a options struct
583  * for various configurations.
584  *
585  * @param prog BPF program to attach
586  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
587  * -1 for all processes
588  * @param binary_path Path to binary that contains the function symbol
589  * @param func_offset Offset within the binary of the function symbol
590  * @param opts Options for altering program attachment
591  * @return Reference to the newly created BPF link; or NULL is returned on error,
592  * error code is stored in errno
593  */
594 LIBBPF_API struct bpf_link *
595 bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
596 				const char *binary_path, size_t func_offset,
597 				const struct bpf_uprobe_opts *opts);
598 
599 struct bpf_usdt_opts {
600 	/* size of this struct, for forward/backward compatibility */
601 	size_t sz;
602 	/* custom user-provided value accessible through usdt_cookie() */
603 	__u64 usdt_cookie;
604 	size_t :0;
605 };
606 #define bpf_usdt_opts__last_field usdt_cookie
607 
608 /**
609  * @brief **bpf_program__attach_usdt()** is just like
610  * bpf_program__attach_uprobe_opts() except it covers USDT (User-space
611  * Statically Defined Tracepoint) attachment, instead of attaching to
612  * user-space function entry or exit.
613  *
614  * @param prog BPF program to attach
615  * @param pid Process ID to attach the uprobe to, 0 for self (own process),
616  * -1 for all processes
617  * @param binary_path Path to binary that contains provided USDT probe
618  * @param usdt_provider USDT provider name
619  * @param usdt_name USDT probe name
620  * @param opts Options for altering program attachment
621  * @return Reference to the newly created BPF link; or NULL is returned on error,
622  * error code is stored in errno
623  */
624 LIBBPF_API struct bpf_link *
625 bpf_program__attach_usdt(const struct bpf_program *prog,
626 			 pid_t pid, const char *binary_path,
627 			 const char *usdt_provider, const char *usdt_name,
628 			 const struct bpf_usdt_opts *opts);
629 
630 struct bpf_tracepoint_opts {
631 	/* size of this struct, for forward/backward compatiblity */
632 	size_t sz;
633 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
634 	__u64 bpf_cookie;
635 };
636 #define bpf_tracepoint_opts__last_field bpf_cookie
637 
638 LIBBPF_API struct bpf_link *
639 bpf_program__attach_tracepoint(const struct bpf_program *prog,
640 			       const char *tp_category,
641 			       const char *tp_name);
642 LIBBPF_API struct bpf_link *
643 bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
644 				    const char *tp_category,
645 				    const char *tp_name,
646 				    const struct bpf_tracepoint_opts *opts);
647 
648 LIBBPF_API struct bpf_link *
649 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
650 				   const char *tp_name);
651 
652 struct bpf_trace_opts {
653 	/* size of this struct, for forward/backward compatibility */
654 	size_t sz;
655 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
656 	__u64 cookie;
657 };
658 #define bpf_trace_opts__last_field cookie
659 
660 LIBBPF_API struct bpf_link *
661 bpf_program__attach_trace(const struct bpf_program *prog);
662 LIBBPF_API struct bpf_link *
663 bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);
664 
665 LIBBPF_API struct bpf_link *
666 bpf_program__attach_lsm(const struct bpf_program *prog);
667 LIBBPF_API struct bpf_link *
668 bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
669 LIBBPF_API struct bpf_link *
670 bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
671 LIBBPF_API struct bpf_link *
672 bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
673 LIBBPF_API struct bpf_link *
674 bpf_program__attach_freplace(const struct bpf_program *prog,
675 			     int target_fd, const char *attach_func_name);
676 
677 struct bpf_map;
678 
679 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
680 
681 struct bpf_iter_attach_opts {
682 	size_t sz; /* size of this struct for forward/backward compatibility */
683 	union bpf_iter_link_info *link_info;
684 	__u32 link_info_len;
685 };
686 #define bpf_iter_attach_opts__last_field link_info_len
687 
688 LIBBPF_API struct bpf_link *
689 bpf_program__attach_iter(const struct bpf_program *prog,
690 			 const struct bpf_iter_attach_opts *opts);
691 
692 /*
693  * Libbpf allows callers to adjust BPF programs before being loaded
694  * into kernel. One program in an object file can be transformed into
695  * multiple variants to be attached to different hooks.
696  *
697  * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
698  * form an API for this purpose.
699  *
700  * - bpf_program_prep_t:
701  *   Defines a 'preprocessor', which is a caller defined function
702  *   passed to libbpf through bpf_program__set_prep(), and will be
703  *   called before program is loaded. The processor should adjust
704  *   the program one time for each instance according to the instance id
705  *   passed to it.
706  *
707  * - bpf_program__set_prep:
708  *   Attaches a preprocessor to a BPF program. The number of instances
709  *   that should be created is also passed through this function.
710  *
711  * - bpf_program__nth_fd:
712  *   After the program is loaded, get resulting FD of a given instance
713  *   of the BPF program.
714  *
715  * If bpf_program__set_prep() is not used, the program would be loaded
716  * without adjustment during bpf_object__load(). The program has only
717  * one instance. In this case bpf_program__fd(prog) is equal to
718  * bpf_program__nth_fd(prog, 0).
719  */
720 struct bpf_prog_prep_result {
721 	/*
722 	 * If not NULL, load new instruction array.
723 	 * If set to NULL, don't load this instance.
724 	 */
725 	struct bpf_insn *new_insn_ptr;
726 	int new_insn_cnt;
727 
728 	/* If not NULL, result FD is written to it. */
729 	int *pfd;
730 };
731 
732 /*
733  * Parameters of bpf_program_prep_t:
734  *  - prog:	The bpf_program being loaded.
735  *  - n:	Index of instance being generated.
736  *  - insns:	BPF instructions array.
737  *  - insns_cnt:Number of instructions in insns.
738  *  - res:	Output parameter, result of transformation.
739  *
740  * Return value:
741  *  - Zero:	pre-processing success.
742  *  - Non-zero:	pre-processing error, stop loading.
743  */
744 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
745 				  struct bpf_insn *insns, int insns_cnt,
746 				  struct bpf_prog_prep_result *res);
747 
748 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insns() for getting bpf_program instructions")
749 LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
750 				     bpf_program_prep_t prep);
751 
752 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
753 LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n);
754 
755 /*
756  * Adjust type of BPF program. Default is kprobe.
757  */
758 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
759 LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
760 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
761 LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
762 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
763 LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
764 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
765 LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
766 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
767 LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog);
768 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
769 LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
770 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
771 LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
772 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
773 LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
774 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
775 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
776 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
777 LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
778 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
779 LIBBPF_API int bpf_program__set_struct_ops(struct bpf_program *prog);
780 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
781 LIBBPF_API int bpf_program__set_extension(struct bpf_program *prog);
782 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__set_type() instead")
783 LIBBPF_API int bpf_program__set_sk_lookup(struct bpf_program *prog);
784 
785 LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);
786 
787 /**
788  * @brief **bpf_program__set_type()** sets the program
789  * type of the passed BPF program.
790  * @param prog BPF program to set the program type for
791  * @param type program type to set the BPF map to have
792  * @return error code; or 0 if no error. An error occurs
793  * if the object is already loaded.
794  *
795  * This must be called before the BPF object is loaded,
796  * otherwise it has no effect and an error is returned.
797  */
798 LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,
799 				     enum bpf_prog_type type);
800 
801 LIBBPF_API enum bpf_attach_type
802 bpf_program__expected_attach_type(const struct bpf_program *prog);
803 
804 /**
805  * @brief **bpf_program__set_expected_attach_type()** sets the
806  * attach type of the passed BPF program. This is used for
807  * auto-detection of attachment when programs are loaded.
808  * @param prog BPF program to set the attach type for
809  * @param type attach type to set the BPF map to have
810  * @return error code; or 0 if no error. An error occurs
811  * if the object is already loaded.
812  *
813  * This must be called before the BPF object is loaded,
814  * otherwise it has no effect and an error is returned.
815  */
816 LIBBPF_API int
817 bpf_program__set_expected_attach_type(struct bpf_program *prog,
818 				      enum bpf_attach_type type);
819 
820 LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
821 LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
822 
823 /* Per-program log level and log buffer getters/setters.
824  * See bpf_object_open_opts comments regarding log_level and log_buf
825  * interactions.
826  */
827 LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);
828 LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);
829 LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
830 LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
831 
832 /**
833  * @brief **bpf_program__set_attach_target()** sets BTF-based attach target
834  * for supported BPF program types:
835  *   - BTF-aware raw tracepoints (tp_btf);
836  *   - fentry/fexit/fmod_ret;
837  *   - lsm;
838  *   - freplace.
839  * @param prog BPF program to set the attach type for
840  * @param type attach type to set the BPF map to have
841  * @return error code; or 0 if no error occurred.
842  */
843 LIBBPF_API int
844 bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
845 			       const char *attach_func_name);
846 
847 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
848 LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog);
849 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
850 LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog);
851 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
852 LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog);
853 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
854 LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog);
855 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
856 LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog);
857 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
858 LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog);
859 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
860 LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog);
861 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
862 LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
863 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
864 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
865 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
866 LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
867 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
868 LIBBPF_API bool bpf_program__is_struct_ops(const struct bpf_program *prog);
869 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
870 LIBBPF_API bool bpf_program__is_extension(const struct bpf_program *prog);
871 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_program__type() instead")
872 LIBBPF_API bool bpf_program__is_sk_lookup(const struct bpf_program *prog);
873 
874 /*
875  * No need for __attribute__((packed)), all members of 'bpf_map_def'
876  * are all aligned.  In addition, using __attribute__((packed))
877  * would trigger a -Wpacked warning message, and lead to an error
878  * if -Werror is set.
879  */
880 struct bpf_map_def {
881 	unsigned int type;
882 	unsigned int key_size;
883 	unsigned int value_size;
884 	unsigned int max_entries;
885 	unsigned int map_flags;
886 };
887 
888 /**
889  * @brief **bpf_object__find_map_by_name()** returns BPF map of
890  * the given name, if it exists within the passed BPF object
891  * @param obj BPF object
892  * @param name name of the BPF map
893  * @return BPF map instance, if such map exists within the BPF object;
894  * or NULL otherwise.
895  */
896 LIBBPF_API struct bpf_map *
897 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
898 
899 LIBBPF_API int
900 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
901 
902 /*
903  * Get bpf_map through the offset of corresponding struct bpf_map_def
904  * in the BPF object file.
905  */
906 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__find_map_by_name() instead")
907 struct bpf_map *
908 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
909 
910 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_map() instead")
911 struct bpf_map *bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
912 LIBBPF_API struct bpf_map *
913 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
914 
915 #define bpf_object__for_each_map(pos, obj)		\
916 	for ((pos) = bpf_object__next_map((obj), NULL);	\
917 	     (pos) != NULL;				\
918 	     (pos) = bpf_object__next_map((obj), (pos)))
919 #define bpf_map__for_each bpf_object__for_each_map
920 
921 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_map() instead")
922 struct bpf_map *bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
923 LIBBPF_API struct bpf_map *
924 bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
925 
926 /**
927  * @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create
928  * BPF map during BPF object load phase.
929  * @param map the BPF map instance
930  * @param autocreate whether to create BPF map during BPF object load
931  * @return 0 on success; -EBUSY if BPF object was already loaded
932  *
933  * **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating
934  * BPF map. By default, libbpf will attempt to create every single BPF map
935  * defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall
936  * and fill in map FD in BPF instructions.
937  *
938  * This API allows to opt-out of this process for specific map instance. This
939  * can be useful if host kernel doesn't support such BPF map type or used
940  * combination of flags and user application wants to avoid creating such
941  * a map in the first place. User is still responsible to make sure that their
942  * BPF-side code that expects to use such missing BPF map is recognized by BPF
943  * verifier as dead code, otherwise BPF verifier will reject such BPF program.
944  */
945 LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);
946 LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);
947 
948 /**
949  * @brief **bpf_map__fd()** gets the file descriptor of the passed
950  * BPF map
951  * @param map the BPF map instance
952  * @return the file descriptor; or -EINVAL in case of an error
953  */
954 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
955 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
956 /* get map definition */
957 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 8, "use appropriate getters or setters instead")
958 const struct bpf_map_def *bpf_map__def(const struct bpf_map *map);
959 /* get map name */
960 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
961 /* get/set map type */
962 LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
963 LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
964 /* get/set map size (max_entries) */
965 LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
966 LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
967 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_map__set_max_entries() instead")
968 LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
969 /* get/set map flags */
970 LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
971 LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
972 /* get/set map NUMA node */
973 LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
974 LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
975 /* get/set map key size */
976 LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
977 LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
978 /* get/set map value size */
979 LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
980 LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
981 /* get map key/value BTF type IDs */
982 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
983 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
984 /* get/set map if_index */
985 LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
986 LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
987 /* get/set map map_extra flags */
988 LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
989 LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
990 
991 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
992 LIBBPF_DEPRECATED_SINCE(0, 7, "storage via set_priv/priv is deprecated")
993 LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
994 				 bpf_map_clear_priv_t clear_priv);
995 LIBBPF_DEPRECATED_SINCE(0, 7, "storage via set_priv/priv is deprecated")
996 LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
997 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
998 					  const void *data, size_t size);
999 LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
1000 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_map__type() instead")
1001 LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);
1002 
1003 /**
1004  * @brief **bpf_map__is_internal()** tells the caller whether or not the
1005  * passed map is a special map created by libbpf automatically for things like
1006  * global variables, __ksym externs, Kconfig values, etc
1007  * @param map the bpf_map
1008  * @return true, if the map is an internal map; false, otherwise
1009  */
1010 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
1011 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
1012 LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
1013 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
1014 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
1015 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
1016 
1017 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
1018 LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
1019 
1020 /**
1021  * @brief **bpf_map__lookup_elem()** allows to lookup BPF map value
1022  * corresponding to provided key.
1023  * @param map BPF map to lookup element in
1024  * @param key pointer to memory containing bytes of the key used for lookup
1025  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1026  * @param value pointer to memory in which looked up value will be stored
1027  * @param value_sz size in byte of value data memory; it has to match BPF map
1028  * definition's **value_size**. For per-CPU BPF maps value size has to be
1029  * a product of BPF map value size and number of possible CPUs in the system
1030  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1031  * per-CPU values value size has to be aligned up to closest 8 bytes for
1032  * alignment reasons, so expected size is: `round_up(value_size, 8)
1033  * * libbpf_num_possible_cpus()`.
1034  * @flags extra flags passed to kernel for this operation
1035  * @return 0, on success; negative error, otherwise
1036  *
1037  * **bpf_map__lookup_elem()** is high-level equivalent of
1038  * **bpf_map_lookup_elem()** API with added check for key and value size.
1039  */
1040 LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,
1041 				    const void *key, size_t key_sz,
1042 				    void *value, size_t value_sz, __u64 flags);
1043 
1044 /**
1045  * @brief **bpf_map__update_elem()** allows to insert or update value in BPF
1046  * map that corresponds to provided key.
1047  * @param map BPF map to insert to or update element in
1048  * @param key pointer to memory containing bytes of the key
1049  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1050  * @param value pointer to memory containing bytes of the value
1051  * @param value_sz size in byte of value data memory; it has to match BPF map
1052  * definition's **value_size**. For per-CPU BPF maps value size has to be
1053  * a product of BPF map value size and number of possible CPUs in the system
1054  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1055  * per-CPU values value size has to be aligned up to closest 8 bytes for
1056  * alignment reasons, so expected size is: `round_up(value_size, 8)
1057  * * libbpf_num_possible_cpus()`.
1058  * @flags extra flags passed to kernel for this operation
1059  * @return 0, on success; negative error, otherwise
1060  *
1061  * **bpf_map__update_elem()** is high-level equivalent of
1062  * **bpf_map_update_elem()** API with added check for key and value size.
1063  */
1064 LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map,
1065 				    const void *key, size_t key_sz,
1066 				    const void *value, size_t value_sz, __u64 flags);
1067 
1068 /**
1069  * @brief **bpf_map__delete_elem()** allows to delete element in BPF map that
1070  * corresponds to provided key.
1071  * @param map BPF map to delete element from
1072  * @param key pointer to memory containing bytes of the key
1073  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1074  * @flags extra flags passed to kernel for this operation
1075  * @return 0, on success; negative error, otherwise
1076  *
1077  * **bpf_map__delete_elem()** is high-level equivalent of
1078  * **bpf_map_delete_elem()** API with added check for key size.
1079  */
1080 LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map,
1081 				    const void *key, size_t key_sz, __u64 flags);
1082 
1083 /**
1084  * @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value
1085  * corresponding to provided key and atomically delete it afterwards.
1086  * @param map BPF map to lookup element in
1087  * @param key pointer to memory containing bytes of the key used for lookup
1088  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1089  * @param value pointer to memory in which looked up value will be stored
1090  * @param value_sz size in byte of value data memory; it has to match BPF map
1091  * definition's **value_size**. For per-CPU BPF maps value size has to be
1092  * a product of BPF map value size and number of possible CPUs in the system
1093  * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1094  * per-CPU values value size has to be aligned up to closest 8 bytes for
1095  * alignment reasons, so expected size is: `round_up(value_size, 8)
1096  * * libbpf_num_possible_cpus()`.
1097  * @flags extra flags passed to kernel for this operation
1098  * @return 0, on success; negative error, otherwise
1099  *
1100  * **bpf_map__lookup_and_delete_elem()** is high-level equivalent of
1101  * **bpf_map_lookup_and_delete_elem()** API with added check for key and value size.
1102  */
1103 LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
1104 					       const void *key, size_t key_sz,
1105 					       void *value, size_t value_sz, __u64 flags);
1106 
1107 /**
1108  * @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by
1109  * fetching next key that follows current key.
1110  * @param map BPF map to fetch next key from
1111  * @param cur_key pointer to memory containing bytes of current key or NULL to
1112  * fetch the first key
1113  * @param next_key pointer to memory to write next key into
1114  * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1115  * @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map;
1116  * negative error, otherwise
1117  *
1118  * **bpf_map__get_next_key()** is high-level equivalent of
1119  * **bpf_map_get_next_key()** API with added check for key size.
1120  */
1121 LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map,
1122 				     const void *cur_key, void *next_key, size_t key_sz);
1123 
1124 /**
1125  * @brief **libbpf_get_error()** extracts the error code from the passed
1126  * pointer
1127  * @param ptr pointer returned from libbpf API function
1128  * @return error code; or 0 if no error occured
1129  *
1130  * Many libbpf API functions which return pointers have logic to encode error
1131  * codes as pointers, and do not return NULL. Meaning **libbpf_get_error()**
1132  * should be used on the return value from these functions immediately after
1133  * calling the API function, with no intervening calls that could clobber the
1134  * `errno` variable. Consult the individual functions documentation to verify
1135  * if this logic applies should be used.
1136  *
1137  * For these API functions, if `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)`
1138  * is enabled, NULL is returned on error instead.
1139  *
1140  * If ptr is NULL, then errno should be already set by the failing
1141  * API, because libbpf never returns NULL on success and it now always
1142  * sets errno on error.
1143  *
1144  * Example usage:
1145  *
1146  *   struct perf_buffer *pb;
1147  *
1148  *   pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES, &opts);
1149  *   err = libbpf_get_error(pb);
1150  *   if (err) {
1151  *	  pb = NULL;
1152  *	  fprintf(stderr, "failed to open perf buffer: %d\n", err);
1153  *	  goto cleanup;
1154  *   }
1155  */
1156 LIBBPF_API long libbpf_get_error(const void *ptr);
1157 
1158 struct bpf_prog_load_attr {
1159 	const char *file;
1160 	enum bpf_prog_type prog_type;
1161 	enum bpf_attach_type expected_attach_type;
1162 	int ifindex;
1163 	int log_level;
1164 	int prog_flags;
1165 };
1166 
1167 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_object__open() and bpf_object__load() instead")
1168 LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
1169 				   struct bpf_object **pobj, int *prog_fd);
1170 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__open() and bpf_object__load() instead")
1171 LIBBPF_API int bpf_prog_load_deprecated(const char *file, enum bpf_prog_type type,
1172 					struct bpf_object **pobj, int *prog_fd);
1173 
1174 /* XDP related API */
1175 struct xdp_link_info {
1176 	__u32 prog_id;
1177 	__u32 drv_prog_id;
1178 	__u32 hw_prog_id;
1179 	__u32 skb_prog_id;
1180 	__u8 attach_mode;
1181 };
1182 
1183 struct bpf_xdp_set_link_opts {
1184 	size_t sz;
1185 	int old_fd;
1186 	size_t :0;
1187 };
1188 #define bpf_xdp_set_link_opts__last_field old_fd
1189 
1190 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_attach() instead")
1191 LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
1192 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_attach() instead")
1193 LIBBPF_API int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
1194 					const struct bpf_xdp_set_link_opts *opts);
1195 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_query_id() instead")
1196 LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
1197 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_query() instead")
1198 LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
1199 				     size_t info_size, __u32 flags);
1200 
1201 struct bpf_xdp_attach_opts {
1202 	size_t sz;
1203 	int old_prog_fd;
1204 	size_t :0;
1205 };
1206 #define bpf_xdp_attach_opts__last_field old_prog_fd
1207 
1208 struct bpf_xdp_query_opts {
1209 	size_t sz;
1210 	__u32 prog_id;		/* output */
1211 	__u32 drv_prog_id;	/* output */
1212 	__u32 hw_prog_id;	/* output */
1213 	__u32 skb_prog_id;	/* output */
1214 	__u8 attach_mode;	/* output */
1215 	size_t :0;
1216 };
1217 #define bpf_xdp_query_opts__last_field attach_mode
1218 
1219 LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,
1220 			      const struct bpf_xdp_attach_opts *opts);
1221 LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,
1222 			      const struct bpf_xdp_attach_opts *opts);
1223 LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);
1224 LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);
1225 
1226 /* TC related API */
1227 enum bpf_tc_attach_point {
1228 	BPF_TC_INGRESS = 1 << 0,
1229 	BPF_TC_EGRESS  = 1 << 1,
1230 	BPF_TC_CUSTOM  = 1 << 2,
1231 };
1232 
1233 #define BPF_TC_PARENT(a, b) 	\
1234 	((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
1235 
1236 enum bpf_tc_flags {
1237 	BPF_TC_F_REPLACE = 1 << 0,
1238 };
1239 
1240 struct bpf_tc_hook {
1241 	size_t sz;
1242 	int ifindex;
1243 	enum bpf_tc_attach_point attach_point;
1244 	__u32 parent;
1245 	size_t :0;
1246 };
1247 #define bpf_tc_hook__last_field parent
1248 
1249 struct bpf_tc_opts {
1250 	size_t sz;
1251 	int prog_fd;
1252 	__u32 flags;
1253 	__u32 prog_id;
1254 	__u32 handle;
1255 	__u32 priority;
1256 	size_t :0;
1257 };
1258 #define bpf_tc_opts__last_field priority
1259 
1260 LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
1261 LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
1262 LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
1263 			     struct bpf_tc_opts *opts);
1264 LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
1265 			     const struct bpf_tc_opts *opts);
1266 LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
1267 			    struct bpf_tc_opts *opts);
1268 
1269 /* Ring buffer APIs */
1270 struct ring_buffer;
1271 
1272 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
1273 
1274 struct ring_buffer_opts {
1275 	size_t sz; /* size of this struct, for forward/backward compatiblity */
1276 };
1277 
1278 #define ring_buffer_opts__last_field sz
1279 
1280 LIBBPF_API struct ring_buffer *
1281 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
1282 		 const struct ring_buffer_opts *opts);
1283 LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
1284 LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
1285 				ring_buffer_sample_fn sample_cb, void *ctx);
1286 LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
1287 LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
1288 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
1289 
1290 /* Perf buffer APIs */
1291 struct perf_buffer;
1292 
1293 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
1294 				      void *data, __u32 size);
1295 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
1296 
1297 /* common use perf buffer options */
1298 struct perf_buffer_opts {
1299 	union {
1300 		size_t sz;
1301 		struct { /* DEPRECATED: will be removed in v1.0 */
1302 			/* if specified, sample_cb is called for each sample */
1303 			perf_buffer_sample_fn sample_cb;
1304 			/* if specified, lost_cb is called for each batch of lost samples */
1305 			perf_buffer_lost_fn lost_cb;
1306 			/* ctx is provided to sample_cb and lost_cb */
1307 			void *ctx;
1308 		};
1309 	};
1310 };
1311 #define perf_buffer_opts__last_field sz
1312 
1313 /**
1314  * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
1315  * BPF_PERF_EVENT_ARRAY map
1316  * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
1317  * code to send data over to user-space
1318  * @param page_cnt number of memory pages allocated for each per-CPU buffer
1319  * @param sample_cb function called on each received data record
1320  * @param lost_cb function called when record loss has occurred
1321  * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
1322  * @return a new instance of struct perf_buffer on success, NULL on error with
1323  * *errno* containing an error code
1324  */
1325 LIBBPF_API struct perf_buffer *
1326 perf_buffer__new(int map_fd, size_t page_cnt,
1327 		 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
1328 		 const struct perf_buffer_opts *opts);
1329 
1330 LIBBPF_API struct perf_buffer *
1331 perf_buffer__new_v0_6_0(int map_fd, size_t page_cnt,
1332 			perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
1333 			const struct perf_buffer_opts *opts);
1334 
1335 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use new variant of perf_buffer__new() instead")
1336 struct perf_buffer *perf_buffer__new_deprecated(int map_fd, size_t page_cnt,
1337 						const struct perf_buffer_opts *opts);
1338 
1339 #define perf_buffer__new(...) ___libbpf_overload(___perf_buffer_new, __VA_ARGS__)
1340 #define ___perf_buffer_new6(map_fd, page_cnt, sample_cb, lost_cb, ctx, opts) \
1341 	perf_buffer__new(map_fd, page_cnt, sample_cb, lost_cb, ctx, opts)
1342 #define ___perf_buffer_new3(map_fd, page_cnt, opts) \
1343 	perf_buffer__new_deprecated(map_fd, page_cnt, opts)
1344 
1345 enum bpf_perf_event_ret {
1346 	LIBBPF_PERF_EVENT_DONE	= 0,
1347 	LIBBPF_PERF_EVENT_ERROR	= -1,
1348 	LIBBPF_PERF_EVENT_CONT	= -2,
1349 };
1350 
1351 struct perf_event_header;
1352 
1353 typedef enum bpf_perf_event_ret
1354 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
1355 
1356 /* raw perf buffer options, giving most power and control */
1357 struct perf_buffer_raw_opts {
1358 	union {
1359 		struct {
1360 			size_t sz;
1361 			long :0;
1362 			long :0;
1363 		};
1364 		struct { /* DEPRECATED: will be removed in v1.0 */
1365 			/* perf event attrs passed directly into perf_event_open() */
1366 			struct perf_event_attr *attr;
1367 			/* raw event callback */
1368 			perf_buffer_event_fn event_cb;
1369 			/* ctx is provided to event_cb */
1370 			void *ctx;
1371 		};
1372 	};
1373 	/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
1374 	 * max_entries of given PERF_EVENT_ARRAY map)
1375 	 */
1376 	int cpu_cnt;
1377 	/* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
1378 	int *cpus;
1379 	/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
1380 	int *map_keys;
1381 };
1382 #define perf_buffer_raw_opts__last_field map_keys
1383 
1384 LIBBPF_API struct perf_buffer *
1385 perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1386 		     perf_buffer_event_fn event_cb, void *ctx,
1387 		     const struct perf_buffer_raw_opts *opts);
1388 
1389 LIBBPF_API struct perf_buffer *
1390 perf_buffer__new_raw_v0_6_0(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1391 			    perf_buffer_event_fn event_cb, void *ctx,
1392 			    const struct perf_buffer_raw_opts *opts);
1393 
1394 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use new variant of perf_buffer__new_raw() instead")
1395 struct perf_buffer *perf_buffer__new_raw_deprecated(int map_fd, size_t page_cnt,
1396 						    const struct perf_buffer_raw_opts *opts);
1397 
1398 #define perf_buffer__new_raw(...) ___libbpf_overload(___perf_buffer_new_raw, __VA_ARGS__)
1399 #define ___perf_buffer_new_raw6(map_fd, page_cnt, attr, event_cb, ctx, opts) \
1400 	perf_buffer__new_raw(map_fd, page_cnt, attr, event_cb, ctx, opts)
1401 #define ___perf_buffer_new_raw3(map_fd, page_cnt, opts) \
1402 	perf_buffer__new_raw_deprecated(map_fd, page_cnt, opts)
1403 
1404 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
1405 LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
1406 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
1407 LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
1408 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
1409 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
1410 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
1411 
1412 typedef enum bpf_perf_event_ret
1413 	(*bpf_perf_event_print_t)(struct perf_event_header *hdr,
1414 				  void *private_data);
1415 LIBBPF_DEPRECATED_SINCE(0, 8, "use perf_buffer__poll() or  perf_buffer__consume() instead")
1416 LIBBPF_API enum bpf_perf_event_ret
1417 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
1418 			   void **copy_mem, size_t *copy_size,
1419 			   bpf_perf_event_print_t fn, void *private_data);
1420 
1421 struct bpf_prog_linfo;
1422 struct bpf_prog_info;
1423 
1424 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
1425 LIBBPF_API struct bpf_prog_linfo *
1426 bpf_prog_linfo__new(const struct bpf_prog_info *info);
1427 LIBBPF_API const struct bpf_line_info *
1428 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
1429 				__u64 addr, __u32 func_idx, __u32 nr_skip);
1430 LIBBPF_API const struct bpf_line_info *
1431 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
1432 		      __u32 insn_off, __u32 nr_skip);
1433 
1434 /*
1435  * Probe for supported system features
1436  *
1437  * Note that running many of these probes in a short amount of time can cause
1438  * the kernel to reach the maximal size of lockable memory allowed for the
1439  * user, causing subsequent probes to fail. In this case, the caller may want
1440  * to adjust that limit with setrlimit().
1441  */
1442 LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_prog_type() instead")
1443 LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type, __u32 ifindex);
1444 LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_map_type() instead")
1445 LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
1446 LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_helper() instead")
1447 LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id, enum bpf_prog_type prog_type, __u32 ifindex);
1448 LIBBPF_DEPRECATED_SINCE(0, 8, "implement your own or use bpftool for feature detection")
1449 LIBBPF_API bool bpf_probe_large_insn_limit(__u32 ifindex);
1450 
1451 /**
1452  * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports
1453  * BPF programs of a given type.
1454  * @param prog_type BPF program type to detect kernel support for
1455  * @param opts reserved for future extensibility, should be NULL
1456  * @return 1, if given program type is supported; 0, if given program type is
1457  * not supported; negative error code if feature detection failed or can't be
1458  * performed
1459  *
1460  * Make sure the process has required set of CAP_* permissions (or runs as
1461  * root) when performing feature checking.
1462  */
1463 LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);
1464 /**
1465  * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports
1466  * BPF maps of a given type.
1467  * @param map_type BPF map type to detect kernel support for
1468  * @param opts reserved for future extensibility, should be NULL
1469  * @return 1, if given map type is supported; 0, if given map type is
1470  * not supported; negative error code if feature detection failed or can't be
1471  * performed
1472  *
1473  * Make sure the process has required set of CAP_* permissions (or runs as
1474  * root) when performing feature checking.
1475  */
1476 LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);
1477 /**
1478  * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the
1479  * use of a given BPF helper from specified BPF program type.
1480  * @param prog_type BPF program type used to check the support of BPF helper
1481  * @param helper_id BPF helper ID (enum bpf_func_id) to check support for
1482  * @param opts reserved for future extensibility, should be NULL
1483  * @return 1, if given combination of program type and helper is supported; 0,
1484  * if the combination is not supported; negative error code if feature
1485  * detection for provided input arguments failed or can't be performed
1486  *
1487  * Make sure the process has required set of CAP_* permissions (or runs as
1488  * root) when performing feature checking.
1489  */
1490 LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
1491 				       enum bpf_func_id helper_id, const void *opts);
1492 
1493 /*
1494  * Get bpf_prog_info in continuous memory
1495  *
1496  * struct bpf_prog_info has multiple arrays. The user has option to choose
1497  * arrays to fetch from kernel. The following APIs provide an uniform way to
1498  * fetch these data. All arrays in bpf_prog_info are stored in a single
1499  * continuous memory region. This makes it easy to store the info in a
1500  * file.
1501  *
1502  * Before writing bpf_prog_info_linear to files, it is necessary to
1503  * translate pointers in bpf_prog_info to offsets. Helper functions
1504  * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr()
1505  * are introduced to switch between pointers and offsets.
1506  *
1507  * Examples:
1508  *   # To fetch map_ids and prog_tags:
1509  *   __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) |
1510  *           (1UL << BPF_PROG_INFO_PROG_TAGS);
1511  *   struct bpf_prog_info_linear *info_linear =
1512  *           bpf_program__get_prog_info_linear(fd, arrays);
1513  *
1514  *   # To save data in file
1515  *   bpf_program__bpil_addr_to_offs(info_linear);
1516  *   write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
1517  *
1518  *   # To read data from file
1519  *   read(f, info_linear, <proper_size>);
1520  *   bpf_program__bpil_offs_to_addr(info_linear);
1521  */
1522 enum bpf_prog_info_array {
1523 	BPF_PROG_INFO_FIRST_ARRAY = 0,
1524 	BPF_PROG_INFO_JITED_INSNS = 0,
1525 	BPF_PROG_INFO_XLATED_INSNS,
1526 	BPF_PROG_INFO_MAP_IDS,
1527 	BPF_PROG_INFO_JITED_KSYMS,
1528 	BPF_PROG_INFO_JITED_FUNC_LENS,
1529 	BPF_PROG_INFO_FUNC_INFO,
1530 	BPF_PROG_INFO_LINE_INFO,
1531 	BPF_PROG_INFO_JITED_LINE_INFO,
1532 	BPF_PROG_INFO_PROG_TAGS,
1533 	BPF_PROG_INFO_LAST_ARRAY,
1534 };
1535 
1536 struct bpf_prog_info_linear {
1537 	/* size of struct bpf_prog_info, when the tool is compiled */
1538 	__u32			info_len;
1539 	/* total bytes allocated for data, round up to 8 bytes */
1540 	__u32			data_len;
1541 	/* which arrays are included in data */
1542 	__u64			arrays;
1543 	struct bpf_prog_info	info;
1544 	__u8			data[];
1545 };
1546 
1547 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1548 LIBBPF_API struct bpf_prog_info_linear *
1549 bpf_program__get_prog_info_linear(int fd, __u64 arrays);
1550 
1551 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1552 LIBBPF_API void
1553 bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
1554 
1555 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
1556 LIBBPF_API void
1557 bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
1558 
1559 /**
1560  * @brief **libbpf_num_possible_cpus()** is a helper function to get the
1561  * number of possible CPUs that the host kernel supports and expects.
1562  * @return number of possible CPUs; or error code on failure
1563  *
1564  * Example usage:
1565  *
1566  *     int ncpus = libbpf_num_possible_cpus();
1567  *     if (ncpus < 0) {
1568  *          // error handling
1569  *     }
1570  *     long values[ncpus];
1571  *     bpf_map_lookup_elem(per_cpu_map_fd, key, values);
1572  */
1573 LIBBPF_API int libbpf_num_possible_cpus(void);
1574 
1575 struct bpf_map_skeleton {
1576 	const char *name;
1577 	struct bpf_map **map;
1578 	void **mmaped;
1579 };
1580 
1581 struct bpf_prog_skeleton {
1582 	const char *name;
1583 	struct bpf_program **prog;
1584 	struct bpf_link **link;
1585 };
1586 
1587 struct bpf_object_skeleton {
1588 	size_t sz; /* size of this struct, for forward/backward compatibility */
1589 
1590 	const char *name;
1591 	const void *data;
1592 	size_t data_sz;
1593 
1594 	struct bpf_object **obj;
1595 
1596 	int map_cnt;
1597 	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1598 	struct bpf_map_skeleton *maps;
1599 
1600 	int prog_cnt;
1601 	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1602 	struct bpf_prog_skeleton *progs;
1603 };
1604 
1605 LIBBPF_API int
1606 bpf_object__open_skeleton(struct bpf_object_skeleton *s,
1607 			  const struct bpf_object_open_opts *opts);
1608 LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
1609 LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
1610 LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
1611 LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
1612 
1613 struct bpf_var_skeleton {
1614 	const char *name;
1615 	struct bpf_map **map;
1616 	void **addr;
1617 };
1618 
1619 struct bpf_object_subskeleton {
1620 	size_t sz; /* size of this struct, for forward/backward compatibility */
1621 
1622 	const struct bpf_object *obj;
1623 
1624 	int map_cnt;
1625 	int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1626 	struct bpf_map_skeleton *maps;
1627 
1628 	int prog_cnt;
1629 	int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1630 	struct bpf_prog_skeleton *progs;
1631 
1632 	int var_cnt;
1633 	int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */
1634 	struct bpf_var_skeleton *vars;
1635 };
1636 
1637 LIBBPF_API int
1638 bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);
1639 LIBBPF_API void
1640 bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);
1641 
1642 struct gen_loader_opts {
1643 	size_t sz; /* size of this struct, for forward/backward compatiblity */
1644 	const char *data;
1645 	const char *insns;
1646 	__u32 data_sz;
1647 	__u32 insns_sz;
1648 };
1649 
1650 #define gen_loader_opts__last_field insns_sz
1651 LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
1652 				      struct gen_loader_opts *opts);
1653 
1654 enum libbpf_tristate {
1655 	TRI_NO = 0,
1656 	TRI_YES = 1,
1657 	TRI_MODULE = 2,
1658 };
1659 
1660 struct bpf_linker_opts {
1661 	/* size of this struct, for forward/backward compatiblity */
1662 	size_t sz;
1663 };
1664 #define bpf_linker_opts__last_field sz
1665 
1666 struct bpf_linker_file_opts {
1667 	/* size of this struct, for forward/backward compatiblity */
1668 	size_t sz;
1669 };
1670 #define bpf_linker_file_opts__last_field sz
1671 
1672 struct bpf_linker;
1673 
1674 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
1675 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
1676 				    const char *filename,
1677 				    const struct bpf_linker_file_opts *opts);
1678 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
1679 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
1680 
1681 /*
1682  * Custom handling of BPF program's SEC() definitions
1683  */
1684 
1685 struct bpf_prog_load_opts; /* defined in bpf.h */
1686 
1687 /* Called during bpf_object__open() for each recognized BPF program. Callback
1688  * can use various bpf_program__set_*() setters to adjust whatever properties
1689  * are necessary.
1690  */
1691 typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);
1692 
1693 /* Called right before libbpf performs bpf_prog_load() to load BPF program
1694  * into the kernel. Callback can adjust opts as necessary.
1695  */
1696 typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,
1697 					     struct bpf_prog_load_opts *opts, long cookie);
1698 
1699 /* Called during skeleton attach or through bpf_program__attach(). If
1700  * auto-attach is not supported, callback should return 0 and set link to
1701  * NULL (it's not considered an error during skeleton attach, but it will be
1702  * an error for bpf_program__attach() calls). On error, error should be
1703  * returned directly and link set to NULL. On success, return 0 and set link
1704  * to a valid struct bpf_link.
1705  */
1706 typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,
1707 				       struct bpf_link **link);
1708 
1709 struct libbpf_prog_handler_opts {
1710 	/* size of this struct, for forward/backward compatiblity */
1711 	size_t sz;
1712 	/* User-provided value that is passed to prog_setup_fn,
1713 	 * prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to
1714 	 * register one set of callbacks for multiple SEC() definitions and
1715 	 * still be able to distinguish them, if necessary. For example,
1716 	 * libbpf itself is using this to pass necessary flags (e.g.,
1717 	 * sleepable flag) to a common internal SEC() handler.
1718 	 */
1719 	long cookie;
1720 	/* BPF program initialization callback (see libbpf_prog_setup_fn_t).
1721 	 * Callback is optional, pass NULL if it's not necessary.
1722 	 */
1723 	libbpf_prog_setup_fn_t prog_setup_fn;
1724 	/* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).
1725 	 * Callback is optional, pass NULL if it's not necessary.
1726 	 */
1727 	libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
1728 	/* BPF program attach callback (see libbpf_prog_attach_fn_t).
1729 	 * Callback is optional, pass NULL if it's not necessary.
1730 	 */
1731 	libbpf_prog_attach_fn_t prog_attach_fn;
1732 };
1733 #define libbpf_prog_handler_opts__last_field prog_attach_fn
1734 
1735 /**
1736  * @brief **libbpf_register_prog_handler()** registers a custom BPF program
1737  * SEC() handler.
1738  * @param sec section prefix for which custom handler is registered
1739  * @param prog_type BPF program type associated with specified section
1740  * @param exp_attach_type Expected BPF attach type associated with specified section
1741  * @param opts optional cookie, callbacks, and other extra options
1742  * @return Non-negative handler ID is returned on success. This handler ID has
1743  * to be passed to *libbpf_unregister_prog_handler()* to unregister such
1744  * custom handler. Negative error code is returned on error.
1745  *
1746  * *sec* defines which SEC() definitions are handled by this custom handler
1747  * registration. *sec* can have few different forms:
1748  *   - if *sec* is just a plain string (e.g., "abc"), it will match only
1749  *   SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result
1750  *   in an error;
1751  *   - if *sec* is of the form "abc/", proper SEC() form is
1752  *   SEC("abc/something"), where acceptable "something" should be checked by
1753  *   *prog_init_fn* callback, if there are additional restrictions;
1754  *   - if *sec* is of the form "abc+", it will successfully match both
1755  *   SEC("abc") and SEC("abc/whatever") forms;
1756  *   - if *sec* is NULL, custom handler is registered for any BPF program that
1757  *   doesn't match any of the registered (custom or libbpf's own) SEC()
1758  *   handlers. There could be only one such generic custom handler registered
1759  *   at any given time.
1760  *
1761  * All custom handlers (except the one with *sec* == NULL) are processed
1762  * before libbpf's own SEC() handlers. It is allowed to "override" libbpf's
1763  * SEC() handlers by registering custom ones for the same section prefix
1764  * (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")
1765  * handler).
1766  *
1767  * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1768  * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1769  * to ensure synchronization if there is a risk of running this API from
1770  * multiple threads simultaneously.
1771  */
1772 LIBBPF_API int libbpf_register_prog_handler(const char *sec,
1773 					    enum bpf_prog_type prog_type,
1774 					    enum bpf_attach_type exp_attach_type,
1775 					    const struct libbpf_prog_handler_opts *opts);
1776 /**
1777  * @brief *libbpf_unregister_prog_handler()* unregisters previously registered
1778  * custom BPF program SEC() handler.
1779  * @param handler_id handler ID returned by *libbpf_register_prog_handler()*
1780  * after successful registration
1781  * @return 0 on success, negative error code if handler isn't found
1782  *
1783  * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1784  * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1785  * to ensure synchronization if there is a risk of running this API from
1786  * multiple threads simultaneously.
1787  */
1788 LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);
1789 
1790 #ifdef __cplusplus
1791 } /* extern "C" */
1792 #endif
1793 
1794 #endif /* __LIBBPF_LIBBPF_H */
1795