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