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