xref: /linux/tools/lib/bpf/libbpf.h (revision e5c15a363de6f87d5aff9a2674f77c49f70a9ca2)
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 enum libbpf_errno {
28 	__LIBBPF_ERRNO__START = 4000,
29 
30 	/* Something wrong in libelf */
31 	LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
32 	LIBBPF_ERRNO__FORMAT,	/* BPF object format invalid */
33 	LIBBPF_ERRNO__KVERSION,	/* Incorrect or no 'version' section */
34 	LIBBPF_ERRNO__ENDIAN,	/* Endian mismatch */
35 	LIBBPF_ERRNO__INTERNAL,	/* Internal error in libbpf */
36 	LIBBPF_ERRNO__RELOC,	/* Relocation failed */
37 	LIBBPF_ERRNO__LOAD,	/* Load program failure for unknown reason */
38 	LIBBPF_ERRNO__VERIFY,	/* Kernel verifier blocks program loading */
39 	LIBBPF_ERRNO__PROG2BIG,	/* Program too big */
40 	LIBBPF_ERRNO__KVER,	/* Incorrect kernel version */
41 	LIBBPF_ERRNO__PROGTYPE,	/* Kernel doesn't support this program type */
42 	LIBBPF_ERRNO__WRNGPID,	/* Wrong pid in netlink message */
43 	LIBBPF_ERRNO__INVSEQ,	/* Invalid netlink sequence */
44 	LIBBPF_ERRNO__NLPARSE,	/* netlink parsing error */
45 	__LIBBPF_ERRNO__END,
46 };
47 
48 LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
49 
50 enum libbpf_print_level {
51         LIBBPF_WARN,
52         LIBBPF_INFO,
53         LIBBPF_DEBUG,
54 };
55 
56 typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
57 				 const char *, va_list ap);
58 
59 LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
60 
61 /* Hide internal to user */
62 struct bpf_object;
63 
64 struct bpf_object_open_attr {
65 	const char *file;
66 	enum bpf_prog_type prog_type;
67 };
68 
69 struct bpf_object_open_opts {
70 	/* size of this struct, for forward/backward compatiblity */
71 	size_t sz;
72 	/* object name override, if provided:
73 	 * - for object open from file, this will override setting object
74 	 *   name from file path's base name;
75 	 * - for object open from memory buffer, this will specify an object
76 	 *   name and will override default "<addr>-<buf-size>" name;
77 	 */
78 	const char *object_name;
79 	/* parse map definitions non-strictly, allowing extra attributes/data */
80 	bool relaxed_maps;
81 	/* DEPRECATED: handle CO-RE relocations non-strictly, allowing failures.
82 	 * Value is ignored. Relocations always are processed non-strictly.
83 	 * Non-relocatable instructions are replaced with invalid ones to
84 	 * prevent accidental errors.
85 	 * */
86 	LIBBPF_DEPRECATED_SINCE(0, 6, "field has no effect")
87 	bool relaxed_core_relocs;
88 	/* maps that set the 'pinning' attribute in their definition will have
89 	 * their pin_path attribute set to a file in this directory, and be
90 	 * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
91 	 */
92 	const char *pin_root_path;
93 
94 	LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__set_attach_target() on each individual bpf_program")
95 	__u32 attach_prog_fd;
96 	/* Additional kernel config content that augments and overrides
97 	 * system Kconfig for CONFIG_xxx externs.
98 	 */
99 	const char *kconfig;
100 	/* Path to the custom BTF to be used for BPF CO-RE relocations.
101 	 * This custom BTF completely replaces the use of vmlinux BTF
102 	 * for the purpose of CO-RE relocations.
103 	 * NOTE: any other BPF feature (e.g., fentry/fexit programs,
104 	 * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.
105 	 */
106 	const char *btf_custom_path;
107 };
108 #define bpf_object_open_opts__last_field btf_custom_path
109 
110 LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
111 LIBBPF_API struct bpf_object *
112 bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
113 LIBBPF_API struct bpf_object *
114 bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
115 		     const struct bpf_object_open_opts *opts);
116 
117 /* deprecated bpf_object__open variants */
118 LIBBPF_API struct bpf_object *
119 bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz,
120 			const char *name);
121 LIBBPF_API struct bpf_object *
122 bpf_object__open_xattr(struct bpf_object_open_attr *attr);
123 
124 enum libbpf_pin_type {
125 	LIBBPF_PIN_NONE,
126 	/* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
127 	LIBBPF_PIN_BY_NAME,
128 };
129 
130 /* pin_maps and unpin_maps can both be called with a NULL path, in which case
131  * they will use the pin_path attribute of each map (and ignore all maps that
132  * don't have a pin_path set).
133  */
134 LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
135 LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
136 				      const char *path);
137 LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
138 					const char *path);
139 LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
140 					  const char *path);
141 LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
142 LIBBPF_API void bpf_object__close(struct bpf_object *object);
143 
144 struct bpf_object_load_attr {
145 	struct bpf_object *obj;
146 	int log_level;
147 	const char *target_btf_path;
148 };
149 
150 /* Load/unload object into/from kernel */
151 LIBBPF_API int bpf_object__load(struct bpf_object *obj);
152 LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr);
153 LIBBPF_DEPRECATED_SINCE(0, 6, "bpf_object__unload() is deprecated, use bpf_object__close() instead")
154 LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
155 
156 LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
157 LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
158 LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);
159 
160 struct btf;
161 LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
162 LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
163 
164 LIBBPF_API struct bpf_program *
165 bpf_object__find_program_by_title(const struct bpf_object *obj,
166 				  const char *title);
167 LIBBPF_API struct bpf_program *
168 bpf_object__find_program_by_name(const struct bpf_object *obj,
169 				 const char *name);
170 
171 LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
172 #define bpf_object__for_each_safe(pos, tmp)			\
173 	for ((pos) = bpf_object__next(NULL),		\
174 		(tmp) = bpf_object__next(pos);		\
175 	     (pos) != NULL;				\
176 	     (pos) = (tmp), (tmp) = bpf_object__next(tmp))
177 
178 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
179 LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
180 				    bpf_object_clear_priv_t clear_priv);
181 LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog);
182 
183 LIBBPF_API int
184 libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
185 			 enum bpf_attach_type *expected_attach_type);
186 LIBBPF_API int libbpf_attach_type_by_name(const char *name,
187 					  enum bpf_attach_type *attach_type);
188 LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
189 					  enum bpf_attach_type attach_type);
190 
191 /* Accessors of bpf_program */
192 struct bpf_program;
193 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_program() instead")
194 struct bpf_program *bpf_program__next(struct bpf_program *prog,
195 				      const struct bpf_object *obj);
196 LIBBPF_API struct bpf_program *
197 bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);
198 
199 #define bpf_object__for_each_program(pos, obj)			\
200 	for ((pos) = bpf_object__next_program((obj), NULL);	\
201 	     (pos) != NULL;					\
202 	     (pos) = bpf_object__next_program((obj), (pos)))
203 
204 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_program() instead")
205 struct bpf_program *bpf_program__prev(struct bpf_program *prog,
206 				      const struct bpf_object *obj);
207 LIBBPF_API struct bpf_program *
208 bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);
209 
210 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
211 
212 LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
213 				     bpf_program_clear_priv_t clear_priv);
214 
215 LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
216 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
217 					 __u32 ifindex);
218 
219 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
220 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
221 LIBBPF_API LIBBPF_DEPRECATED("BPF program title is confusing term; please use bpf_program__section_name() instead")
222 const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy);
223 LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
224 LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
225 
226 /* returns program size in bytes */
227 LIBBPF_API size_t bpf_program__size(const struct bpf_program *prog);
228 
229 LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license,
230 				 __u32 kern_version);
231 LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
232 LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
233 					 const char *path,
234 					 int instance);
235 LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
236 					   const char *path,
237 					   int instance);
238 LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
239 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
240 LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
241 
242 struct bpf_link;
243 
244 LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
245 LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
246 LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
247 LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
248 LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
249 LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
250 					struct bpf_program *prog);
251 LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
252 LIBBPF_API int bpf_link__detach(struct bpf_link *link);
253 LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
254 
255 LIBBPF_API struct bpf_link *
256 bpf_program__attach(const struct bpf_program *prog);
257 
258 struct bpf_perf_event_opts {
259 	/* size of this struct, for forward/backward compatiblity */
260 	size_t sz;
261 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
262 	__u64 bpf_cookie;
263 };
264 #define bpf_perf_event_opts__last_field bpf_cookie
265 
266 LIBBPF_API struct bpf_link *
267 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
268 
269 LIBBPF_API struct bpf_link *
270 bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
271 				    const struct bpf_perf_event_opts *opts);
272 
273 struct bpf_kprobe_opts {
274 	/* size of this struct, for forward/backward compatiblity */
275 	size_t sz;
276 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
277 	__u64 bpf_cookie;
278 	/* function's offset to install kprobe to */
279 	size_t offset;
280 	/* kprobe is return probe */
281 	bool retprobe;
282 	size_t :0;
283 };
284 #define bpf_kprobe_opts__last_field retprobe
285 
286 LIBBPF_API struct bpf_link *
287 bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
288 			   const char *func_name);
289 LIBBPF_API struct bpf_link *
290 bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
291                                 const char *func_name,
292                                 const struct bpf_kprobe_opts *opts);
293 
294 struct bpf_uprobe_opts {
295 	/* size of this struct, for forward/backward compatiblity */
296 	size_t sz;
297 	/* offset of kernel reference counted USDT semaphore, added in
298 	 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
299 	 */
300 	size_t ref_ctr_offset;
301 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
302 	__u64 bpf_cookie;
303 	/* uprobe is return probe, invoked at function return time */
304 	bool retprobe;
305 	size_t :0;
306 };
307 #define bpf_uprobe_opts__last_field retprobe
308 
309 LIBBPF_API struct bpf_link *
310 bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
311 			   pid_t pid, const char *binary_path,
312 			   size_t func_offset);
313 LIBBPF_API struct bpf_link *
314 bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
315 				const char *binary_path, size_t func_offset,
316 				const struct bpf_uprobe_opts *opts);
317 
318 struct bpf_tracepoint_opts {
319 	/* size of this struct, for forward/backward compatiblity */
320 	size_t sz;
321 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
322 	__u64 bpf_cookie;
323 };
324 #define bpf_tracepoint_opts__last_field bpf_cookie
325 
326 LIBBPF_API struct bpf_link *
327 bpf_program__attach_tracepoint(const struct bpf_program *prog,
328 			       const char *tp_category,
329 			       const char *tp_name);
330 LIBBPF_API struct bpf_link *
331 bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
332 				    const char *tp_category,
333 				    const char *tp_name,
334 				    const struct bpf_tracepoint_opts *opts);
335 
336 LIBBPF_API struct bpf_link *
337 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
338 				   const char *tp_name);
339 LIBBPF_API struct bpf_link *
340 bpf_program__attach_trace(const struct bpf_program *prog);
341 LIBBPF_API struct bpf_link *
342 bpf_program__attach_lsm(const struct bpf_program *prog);
343 LIBBPF_API struct bpf_link *
344 bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
345 LIBBPF_API struct bpf_link *
346 bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
347 LIBBPF_API struct bpf_link *
348 bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
349 LIBBPF_API struct bpf_link *
350 bpf_program__attach_freplace(const struct bpf_program *prog,
351 			     int target_fd, const char *attach_func_name);
352 
353 struct bpf_map;
354 
355 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
356 
357 struct bpf_iter_attach_opts {
358 	size_t sz; /* size of this struct for forward/backward compatibility */
359 	union bpf_iter_link_info *link_info;
360 	__u32 link_info_len;
361 };
362 #define bpf_iter_attach_opts__last_field link_info_len
363 
364 LIBBPF_API struct bpf_link *
365 bpf_program__attach_iter(const struct bpf_program *prog,
366 			 const struct bpf_iter_attach_opts *opts);
367 
368 struct bpf_insn;
369 
370 /*
371  * Libbpf allows callers to adjust BPF programs before being loaded
372  * into kernel. One program in an object file can be transformed into
373  * multiple variants to be attached to different hooks.
374  *
375  * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
376  * form an API for this purpose.
377  *
378  * - bpf_program_prep_t:
379  *   Defines a 'preprocessor', which is a caller defined function
380  *   passed to libbpf through bpf_program__set_prep(), and will be
381  *   called before program is loaded. The processor should adjust
382  *   the program one time for each instance according to the instance id
383  *   passed to it.
384  *
385  * - bpf_program__set_prep:
386  *   Attaches a preprocessor to a BPF program. The number of instances
387  *   that should be created is also passed through this function.
388  *
389  * - bpf_program__nth_fd:
390  *   After the program is loaded, get resulting FD of a given instance
391  *   of the BPF program.
392  *
393  * If bpf_program__set_prep() is not used, the program would be loaded
394  * without adjustment during bpf_object__load(). The program has only
395  * one instance. In this case bpf_program__fd(prog) is equal to
396  * bpf_program__nth_fd(prog, 0).
397  */
398 
399 struct bpf_prog_prep_result {
400 	/*
401 	 * If not NULL, load new instruction array.
402 	 * If set to NULL, don't load this instance.
403 	 */
404 	struct bpf_insn *new_insn_ptr;
405 	int new_insn_cnt;
406 
407 	/* If not NULL, result FD is written to it. */
408 	int *pfd;
409 };
410 
411 /*
412  * Parameters of bpf_program_prep_t:
413  *  - prog:	The bpf_program being loaded.
414  *  - n:	Index of instance being generated.
415  *  - insns:	BPF instructions array.
416  *  - insns_cnt:Number of instructions in insns.
417  *  - res:	Output parameter, result of transformation.
418  *
419  * Return value:
420  *  - Zero:	pre-processing success.
421  *  - Non-zero:	pre-processing error, stop loading.
422  */
423 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
424 				  struct bpf_insn *insns, int insns_cnt,
425 				  struct bpf_prog_prep_result *res);
426 
427 LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
428 				     bpf_program_prep_t prep);
429 
430 LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n);
431 
432 /*
433  * Adjust type of BPF program. Default is kprobe.
434  */
435 LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
436 LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
437 LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
438 LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
439 LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog);
440 LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
441 LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
442 LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
443 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
444 LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
445 LIBBPF_API int bpf_program__set_struct_ops(struct bpf_program *prog);
446 LIBBPF_API int bpf_program__set_extension(struct bpf_program *prog);
447 LIBBPF_API int bpf_program__set_sk_lookup(struct bpf_program *prog);
448 
449 LIBBPF_API enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog);
450 LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
451 				      enum bpf_prog_type type);
452 
453 LIBBPF_API enum bpf_attach_type
454 bpf_program__get_expected_attach_type(const struct bpf_program *prog);
455 LIBBPF_API void
456 bpf_program__set_expected_attach_type(struct bpf_program *prog,
457 				      enum bpf_attach_type type);
458 
459 LIBBPF_API int
460 bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
461 			       const char *attach_func_name);
462 
463 LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog);
464 LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog);
465 LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog);
466 LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog);
467 LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog);
468 LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog);
469 LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog);
470 LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
471 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
472 LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
473 LIBBPF_API bool bpf_program__is_struct_ops(const struct bpf_program *prog);
474 LIBBPF_API bool bpf_program__is_extension(const struct bpf_program *prog);
475 LIBBPF_API bool bpf_program__is_sk_lookup(const struct bpf_program *prog);
476 
477 /*
478  * No need for __attribute__((packed)), all members of 'bpf_map_def'
479  * are all aligned.  In addition, using __attribute__((packed))
480  * would trigger a -Wpacked warning message, and lead to an error
481  * if -Werror is set.
482  */
483 struct bpf_map_def {
484 	unsigned int type;
485 	unsigned int key_size;
486 	unsigned int value_size;
487 	unsigned int max_entries;
488 	unsigned int map_flags;
489 };
490 
491 /**
492  * @brief **bpf_object__find_map_by_name()** returns BPF map of
493  * the given name, if it exists within the passed BPF object
494  * @param obj BPF object
495  * @param name name of the BPF map
496  * @return BPF map instance, if such map exists within the BPF object;
497  * or NULL otherwise.
498  */
499 LIBBPF_API struct bpf_map *
500 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
501 
502 LIBBPF_API int
503 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
504 
505 /*
506  * Get bpf_map through the offset of corresponding struct bpf_map_def
507  * in the BPF object file.
508  */
509 LIBBPF_API struct bpf_map *
510 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
511 
512 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_map() instead")
513 struct bpf_map *bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
514 LIBBPF_API struct bpf_map *
515 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
516 
517 #define bpf_object__for_each_map(pos, obj)		\
518 	for ((pos) = bpf_object__next_map((obj), NULL);	\
519 	     (pos) != NULL;				\
520 	     (pos) = bpf_object__next_map((obj), (pos)))
521 #define bpf_map__for_each bpf_object__for_each_map
522 
523 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_map() instead")
524 struct bpf_map *bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
525 LIBBPF_API struct bpf_map *
526 bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
527 
528 /**
529  * @brief **bpf_map__fd()** gets the file descriptor of the passed
530  * BPF map
531  * @param map the BPF map instance
532  * @return the file descriptor; or -EINVAL in case of an error
533  */
534 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
535 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
536 /* get map definition */
537 LIBBPF_API const struct bpf_map_def *bpf_map__def(const struct bpf_map *map);
538 /* get map name */
539 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
540 /* get/set map type */
541 LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
542 LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
543 /* get/set map size (max_entries) */
544 LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
545 LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
546 LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
547 /* get/set map flags */
548 LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
549 LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
550 /* get/set map NUMA node */
551 LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
552 LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
553 /* get/set map key size */
554 LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
555 LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
556 /* get/set map value size */
557 LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
558 LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
559 /* get map key/value BTF type IDs */
560 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
561 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
562 /* get/set map if_index */
563 LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
564 LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
565 
566 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
567 LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
568 				 bpf_map_clear_priv_t clear_priv);
569 LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
570 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
571 					  const void *data, size_t size);
572 LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
573 LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);
574 
575 /**
576  * @brief **bpf_map__is_internal()** tells the caller whether or not the
577  * passed map is a special map created by libbpf automatically for things like
578  * global variables, __ksym externs, Kconfig values, etc
579  * @param map the bpf_map
580  * @return true, if the map is an internal map; false, otherwise
581  */
582 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
583 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
584 LIBBPF_API const char *bpf_map__get_pin_path(const struct bpf_map *map);
585 LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
586 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
587 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
588 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
589 
590 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
591 LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
592 
593 /**
594  * @brief **libbpf_get_error()** extracts the error code from the passed
595  * pointer
596  * @param ptr pointer returned from libbpf API function
597  * @return error code; or 0 if no error occured
598  *
599  * Many libbpf API functions which return pointers have logic to encode error
600  * codes as pointers, and do not return NULL. Meaning **libbpf_get_error()**
601  * should be used on the return value from these functions immediately after
602  * calling the API function, with no intervening calls that could clobber the
603  * `errno` variable. Consult the individual functions documentation to verify
604  * if this logic applies should be used.
605  *
606  * For these API functions, if `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)`
607  * is enabled, NULL is returned on error instead.
608  *
609  * If ptr is NULL, then errno should be already set by the failing
610  * API, because libbpf never returns NULL on success and it now always
611  * sets errno on error.
612  *
613  * Example usage:
614  *
615  *   struct perf_buffer *pb;
616  *
617  *   pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES, &opts);
618  *   err = libbpf_get_error(pb);
619  *   if (err) {
620  *	  pb = NULL;
621  *	  fprintf(stderr, "failed to open perf buffer: %d\n", err);
622  *	  goto cleanup;
623  *   }
624  */
625 LIBBPF_API long libbpf_get_error(const void *ptr);
626 
627 struct bpf_prog_load_attr {
628 	const char *file;
629 	enum bpf_prog_type prog_type;
630 	enum bpf_attach_type expected_attach_type;
631 	int ifindex;
632 	int log_level;
633 	int prog_flags;
634 };
635 
636 LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
637 				   struct bpf_object **pobj, int *prog_fd);
638 LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type,
639 			     struct bpf_object **pobj, int *prog_fd);
640 
641 /* XDP related API */
642 struct xdp_link_info {
643 	__u32 prog_id;
644 	__u32 drv_prog_id;
645 	__u32 hw_prog_id;
646 	__u32 skb_prog_id;
647 	__u8 attach_mode;
648 };
649 
650 struct bpf_xdp_set_link_opts {
651 	size_t sz;
652 	int old_fd;
653 	size_t :0;
654 };
655 #define bpf_xdp_set_link_opts__last_field old_fd
656 
657 LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
658 LIBBPF_API int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
659 					const struct bpf_xdp_set_link_opts *opts);
660 LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
661 LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
662 				     size_t info_size, __u32 flags);
663 
664 /* TC related API */
665 enum bpf_tc_attach_point {
666 	BPF_TC_INGRESS = 1 << 0,
667 	BPF_TC_EGRESS  = 1 << 1,
668 	BPF_TC_CUSTOM  = 1 << 2,
669 };
670 
671 #define BPF_TC_PARENT(a, b) 	\
672 	((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
673 
674 enum bpf_tc_flags {
675 	BPF_TC_F_REPLACE = 1 << 0,
676 };
677 
678 struct bpf_tc_hook {
679 	size_t sz;
680 	int ifindex;
681 	enum bpf_tc_attach_point attach_point;
682 	__u32 parent;
683 	size_t :0;
684 };
685 #define bpf_tc_hook__last_field parent
686 
687 struct bpf_tc_opts {
688 	size_t sz;
689 	int prog_fd;
690 	__u32 flags;
691 	__u32 prog_id;
692 	__u32 handle;
693 	__u32 priority;
694 	size_t :0;
695 };
696 #define bpf_tc_opts__last_field priority
697 
698 LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
699 LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
700 LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
701 			     struct bpf_tc_opts *opts);
702 LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
703 			     const struct bpf_tc_opts *opts);
704 LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
705 			    struct bpf_tc_opts *opts);
706 
707 /* Ring buffer APIs */
708 struct ring_buffer;
709 
710 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
711 
712 struct ring_buffer_opts {
713 	size_t sz; /* size of this struct, for forward/backward compatiblity */
714 };
715 
716 #define ring_buffer_opts__last_field sz
717 
718 LIBBPF_API struct ring_buffer *
719 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
720 		 const struct ring_buffer_opts *opts);
721 LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
722 LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
723 				ring_buffer_sample_fn sample_cb, void *ctx);
724 LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
725 LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
726 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
727 
728 /* Perf buffer APIs */
729 struct perf_buffer;
730 
731 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
732 				      void *data, __u32 size);
733 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
734 
735 /* common use perf buffer options */
736 struct perf_buffer_opts {
737 	/* if specified, sample_cb is called for each sample */
738 	perf_buffer_sample_fn sample_cb;
739 	/* if specified, lost_cb is called for each batch of lost samples */
740 	perf_buffer_lost_fn lost_cb;
741 	/* ctx is provided to sample_cb and lost_cb */
742 	void *ctx;
743 };
744 
745 LIBBPF_API struct perf_buffer *
746 perf_buffer__new(int map_fd, size_t page_cnt,
747 		 const struct perf_buffer_opts *opts);
748 
749 enum bpf_perf_event_ret {
750 	LIBBPF_PERF_EVENT_DONE	= 0,
751 	LIBBPF_PERF_EVENT_ERROR	= -1,
752 	LIBBPF_PERF_EVENT_CONT	= -2,
753 };
754 
755 struct perf_event_header;
756 
757 typedef enum bpf_perf_event_ret
758 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
759 
760 /* raw perf buffer options, giving most power and control */
761 struct perf_buffer_raw_opts {
762 	/* perf event attrs passed directly into perf_event_open() */
763 	struct perf_event_attr *attr;
764 	/* raw event callback */
765 	perf_buffer_event_fn event_cb;
766 	/* ctx is provided to event_cb */
767 	void *ctx;
768 	/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
769 	 * max_entries of given PERF_EVENT_ARRAY map)
770 	 */
771 	int cpu_cnt;
772 	/* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
773 	int *cpus;
774 	/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
775 	int *map_keys;
776 };
777 
778 LIBBPF_API struct perf_buffer *
779 perf_buffer__new_raw(int map_fd, size_t page_cnt,
780 		     const struct perf_buffer_raw_opts *opts);
781 
782 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
783 LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
784 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
785 LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
786 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
787 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
788 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
789 
790 typedef enum bpf_perf_event_ret
791 	(*bpf_perf_event_print_t)(struct perf_event_header *hdr,
792 				  void *private_data);
793 LIBBPF_API enum bpf_perf_event_ret
794 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
795 			   void **copy_mem, size_t *copy_size,
796 			   bpf_perf_event_print_t fn, void *private_data);
797 
798 struct bpf_prog_linfo;
799 struct bpf_prog_info;
800 
801 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
802 LIBBPF_API struct bpf_prog_linfo *
803 bpf_prog_linfo__new(const struct bpf_prog_info *info);
804 LIBBPF_API const struct bpf_line_info *
805 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
806 				__u64 addr, __u32 func_idx, __u32 nr_skip);
807 LIBBPF_API const struct bpf_line_info *
808 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
809 		      __u32 insn_off, __u32 nr_skip);
810 
811 /*
812  * Probe for supported system features
813  *
814  * Note that running many of these probes in a short amount of time can cause
815  * the kernel to reach the maximal size of lockable memory allowed for the
816  * user, causing subsequent probes to fail. In this case, the caller may want
817  * to adjust that limit with setrlimit().
818  */
819 LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type,
820 				    __u32 ifindex);
821 LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
822 LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id,
823 				 enum bpf_prog_type prog_type, __u32 ifindex);
824 LIBBPF_API bool bpf_probe_large_insn_limit(__u32 ifindex);
825 
826 /*
827  * Get bpf_prog_info in continuous memory
828  *
829  * struct bpf_prog_info has multiple arrays. The user has option to choose
830  * arrays to fetch from kernel. The following APIs provide an uniform way to
831  * fetch these data. All arrays in bpf_prog_info are stored in a single
832  * continuous memory region. This makes it easy to store the info in a
833  * file.
834  *
835  * Before writing bpf_prog_info_linear to files, it is necessary to
836  * translate pointers in bpf_prog_info to offsets. Helper functions
837  * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr()
838  * are introduced to switch between pointers and offsets.
839  *
840  * Examples:
841  *   # To fetch map_ids and prog_tags:
842  *   __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) |
843  *           (1UL << BPF_PROG_INFO_PROG_TAGS);
844  *   struct bpf_prog_info_linear *info_linear =
845  *           bpf_program__get_prog_info_linear(fd, arrays);
846  *
847  *   # To save data in file
848  *   bpf_program__bpil_addr_to_offs(info_linear);
849  *   write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
850  *
851  *   # To read data from file
852  *   read(f, info_linear, <proper_size>);
853  *   bpf_program__bpil_offs_to_addr(info_linear);
854  */
855 enum bpf_prog_info_array {
856 	BPF_PROG_INFO_FIRST_ARRAY = 0,
857 	BPF_PROG_INFO_JITED_INSNS = 0,
858 	BPF_PROG_INFO_XLATED_INSNS,
859 	BPF_PROG_INFO_MAP_IDS,
860 	BPF_PROG_INFO_JITED_KSYMS,
861 	BPF_PROG_INFO_JITED_FUNC_LENS,
862 	BPF_PROG_INFO_FUNC_INFO,
863 	BPF_PROG_INFO_LINE_INFO,
864 	BPF_PROG_INFO_JITED_LINE_INFO,
865 	BPF_PROG_INFO_PROG_TAGS,
866 	BPF_PROG_INFO_LAST_ARRAY,
867 };
868 
869 struct bpf_prog_info_linear {
870 	/* size of struct bpf_prog_info, when the tool is compiled */
871 	__u32			info_len;
872 	/* total bytes allocated for data, round up to 8 bytes */
873 	__u32			data_len;
874 	/* which arrays are included in data */
875 	__u64			arrays;
876 	struct bpf_prog_info	info;
877 	__u8			data[];
878 };
879 
880 LIBBPF_API struct bpf_prog_info_linear *
881 bpf_program__get_prog_info_linear(int fd, __u64 arrays);
882 
883 LIBBPF_API void
884 bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
885 
886 LIBBPF_API void
887 bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
888 
889 /**
890  * @brief **libbpf_num_possible_cpus()** is a helper function to get the
891  * number of possible CPUs that the host kernel supports and expects.
892  * @return number of possible CPUs; or error code on failure
893  *
894  * Example usage:
895  *
896  *     int ncpus = libbpf_num_possible_cpus();
897  *     if (ncpus < 0) {
898  *          // error handling
899  *     }
900  *     long values[ncpus];
901  *     bpf_map_lookup_elem(per_cpu_map_fd, key, values);
902  */
903 LIBBPF_API int libbpf_num_possible_cpus(void);
904 
905 struct bpf_map_skeleton {
906 	const char *name;
907 	struct bpf_map **map;
908 	void **mmaped;
909 };
910 
911 struct bpf_prog_skeleton {
912 	const char *name;
913 	struct bpf_program **prog;
914 	struct bpf_link **link;
915 };
916 
917 struct bpf_object_skeleton {
918 	size_t sz; /* size of this struct, for forward/backward compatibility */
919 
920 	const char *name;
921 	const void *data;
922 	size_t data_sz;
923 
924 	struct bpf_object **obj;
925 
926 	int map_cnt;
927 	int map_skel_sz; /* sizeof(struct bpf_skeleton_map) */
928 	struct bpf_map_skeleton *maps;
929 
930 	int prog_cnt;
931 	int prog_skel_sz; /* sizeof(struct bpf_skeleton_prog) */
932 	struct bpf_prog_skeleton *progs;
933 };
934 
935 LIBBPF_API int
936 bpf_object__open_skeleton(struct bpf_object_skeleton *s,
937 			  const struct bpf_object_open_opts *opts);
938 LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
939 LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
940 LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
941 LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
942 
943 struct gen_loader_opts {
944 	size_t sz; /* size of this struct, for forward/backward compatiblity */
945 	const char *data;
946 	const char *insns;
947 	__u32 data_sz;
948 	__u32 insns_sz;
949 };
950 
951 #define gen_loader_opts__last_field insns_sz
952 LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
953 				      struct gen_loader_opts *opts);
954 
955 enum libbpf_tristate {
956 	TRI_NO = 0,
957 	TRI_YES = 1,
958 	TRI_MODULE = 2,
959 };
960 
961 struct bpf_linker_opts {
962 	/* size of this struct, for forward/backward compatiblity */
963 	size_t sz;
964 };
965 #define bpf_linker_opts__last_field sz
966 
967 struct bpf_linker_file_opts {
968 	/* size of this struct, for forward/backward compatiblity */
969 	size_t sz;
970 };
971 #define bpf_linker_file_opts__last_field sz
972 
973 struct bpf_linker;
974 
975 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
976 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
977 				    const char *filename,
978 				    const struct bpf_linker_file_opts *opts);
979 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
980 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
981 
982 #ifdef __cplusplus
983 } /* extern "C" */
984 #endif
985 
986 #endif /* __LIBBPF_LIBBPF_H */
987