xref: /linux/tools/lib/bpf/libbpf.h (revision 41fb0cf1bced59c1fe178cf6cc9f716b5da9e40e)
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 LIBBPF_DEPRECATED_SINCE(0, 7, "track bpf_objects in application code instead")
172 struct bpf_object *bpf_object__next(struct bpf_object *prev);
173 #define bpf_object__for_each_safe(pos, tmp)			\
174 	for ((pos) = bpf_object__next(NULL),		\
175 		(tmp) = bpf_object__next(pos);		\
176 	     (pos) != NULL;				\
177 	     (pos) = (tmp), (tmp) = bpf_object__next(tmp))
178 
179 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
180 LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
181 				    bpf_object_clear_priv_t clear_priv);
182 LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog);
183 
184 LIBBPF_API int
185 libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
186 			 enum bpf_attach_type *expected_attach_type);
187 LIBBPF_API int libbpf_attach_type_by_name(const char *name,
188 					  enum bpf_attach_type *attach_type);
189 LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
190 					  enum bpf_attach_type attach_type);
191 
192 /* Accessors of bpf_program */
193 struct bpf_program;
194 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_program() instead")
195 struct bpf_program *bpf_program__next(struct bpf_program *prog,
196 				      const struct bpf_object *obj);
197 LIBBPF_API struct bpf_program *
198 bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);
199 
200 #define bpf_object__for_each_program(pos, obj)			\
201 	for ((pos) = bpf_object__next_program((obj), NULL);	\
202 	     (pos) != NULL;					\
203 	     (pos) = bpf_object__next_program((obj), (pos)))
204 
205 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_program() instead")
206 struct bpf_program *bpf_program__prev(struct bpf_program *prog,
207 				      const struct bpf_object *obj);
208 LIBBPF_API struct bpf_program *
209 bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);
210 
211 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
212 
213 LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
214 				     bpf_program_clear_priv_t clear_priv);
215 
216 LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
217 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
218 					 __u32 ifindex);
219 
220 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
221 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
222 LIBBPF_API LIBBPF_DEPRECATED("BPF program title is confusing term; please use bpf_program__section_name() instead")
223 const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy);
224 LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
225 LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
226 
227 /* returns program size in bytes */
228 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insn_cnt() instead")
229 LIBBPF_API size_t bpf_program__size(const struct bpf_program *prog);
230 
231 struct bpf_insn;
232 
233 /**
234  * @brief **bpf_program__insns()** gives read-only access to BPF program's
235  * underlying BPF instructions.
236  * @param prog BPF program for which to return instructions
237  * @return a pointer to an array of BPF instructions that belong to the
238  * specified BPF program
239  *
240  * Returned pointer is always valid and not NULL. Number of `struct bpf_insn`
241  * pointed to can be fetched using **bpf_program__insn_cnt()** API.
242  *
243  * Keep in mind, libbpf can modify and append/delete BPF program's
244  * instructions as it processes BPF object file and prepares everything for
245  * uploading into the kernel. So depending on the point in BPF object
246  * lifetime, **bpf_program__insns()** can return different sets of
247  * instructions. As an example, during BPF object load phase BPF program
248  * instructions will be CO-RE-relocated, BPF subprograms instructions will be
249  * appended, ldimm64 instructions will have FDs embedded, etc. So instructions
250  * returned before **bpf_object__load()** and after it might be quite
251  * different.
252  */
253 LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
254 /**
255  * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
256  * that form specified BPF program.
257  * @param prog BPF program for which to return number of BPF instructions
258  *
259  * See **bpf_program__insns()** documentation for notes on how libbpf can
260  * change instructions and their count during different phases of
261  * **bpf_object** lifetime.
262  */
263 LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);
264 
265 LIBBPF_DEPRECATED_SINCE(0, 6, "use bpf_object__load() instead")
266 LIBBPF_API int bpf_program__load(struct bpf_program *prog, const char *license, __u32 kern_version);
267 LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
268 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
269 LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
270 					 const char *path,
271 					 int instance);
272 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
273 LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
274 					   const char *path,
275 					   int instance);
276 LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
277 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
278 LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
279 
280 struct bpf_link;
281 
282 LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
283 LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
284 LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
285 LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
286 LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
287 LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
288 					struct bpf_program *prog);
289 LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
290 LIBBPF_API int bpf_link__detach(struct bpf_link *link);
291 LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
292 
293 LIBBPF_API struct bpf_link *
294 bpf_program__attach(const struct bpf_program *prog);
295 
296 struct bpf_perf_event_opts {
297 	/* size of this struct, for forward/backward compatiblity */
298 	size_t sz;
299 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
300 	__u64 bpf_cookie;
301 };
302 #define bpf_perf_event_opts__last_field bpf_cookie
303 
304 LIBBPF_API struct bpf_link *
305 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
306 
307 LIBBPF_API struct bpf_link *
308 bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
309 				    const struct bpf_perf_event_opts *opts);
310 
311 struct bpf_kprobe_opts {
312 	/* size of this struct, for forward/backward compatiblity */
313 	size_t sz;
314 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
315 	__u64 bpf_cookie;
316 	/* function's offset to install kprobe to */
317 	size_t offset;
318 	/* kprobe is return probe */
319 	bool retprobe;
320 	size_t :0;
321 };
322 #define bpf_kprobe_opts__last_field retprobe
323 
324 LIBBPF_API struct bpf_link *
325 bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
326 			   const char *func_name);
327 LIBBPF_API struct bpf_link *
328 bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
329                                 const char *func_name,
330                                 const struct bpf_kprobe_opts *opts);
331 
332 struct bpf_uprobe_opts {
333 	/* size of this struct, for forward/backward compatiblity */
334 	size_t sz;
335 	/* offset of kernel reference counted USDT semaphore, added in
336 	 * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
337 	 */
338 	size_t ref_ctr_offset;
339 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
340 	__u64 bpf_cookie;
341 	/* uprobe is return probe, invoked at function return time */
342 	bool retprobe;
343 	size_t :0;
344 };
345 #define bpf_uprobe_opts__last_field retprobe
346 
347 LIBBPF_API struct bpf_link *
348 bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
349 			   pid_t pid, const char *binary_path,
350 			   size_t func_offset);
351 LIBBPF_API struct bpf_link *
352 bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
353 				const char *binary_path, size_t func_offset,
354 				const struct bpf_uprobe_opts *opts);
355 
356 struct bpf_tracepoint_opts {
357 	/* size of this struct, for forward/backward compatiblity */
358 	size_t sz;
359 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
360 	__u64 bpf_cookie;
361 };
362 #define bpf_tracepoint_opts__last_field bpf_cookie
363 
364 LIBBPF_API struct bpf_link *
365 bpf_program__attach_tracepoint(const struct bpf_program *prog,
366 			       const char *tp_category,
367 			       const char *tp_name);
368 LIBBPF_API struct bpf_link *
369 bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
370 				    const char *tp_category,
371 				    const char *tp_name,
372 				    const struct bpf_tracepoint_opts *opts);
373 
374 LIBBPF_API struct bpf_link *
375 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
376 				   const char *tp_name);
377 LIBBPF_API struct bpf_link *
378 bpf_program__attach_trace(const struct bpf_program *prog);
379 LIBBPF_API struct bpf_link *
380 bpf_program__attach_lsm(const struct bpf_program *prog);
381 LIBBPF_API struct bpf_link *
382 bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
383 LIBBPF_API struct bpf_link *
384 bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
385 LIBBPF_API struct bpf_link *
386 bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
387 LIBBPF_API struct bpf_link *
388 bpf_program__attach_freplace(const struct bpf_program *prog,
389 			     int target_fd, const char *attach_func_name);
390 
391 struct bpf_map;
392 
393 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
394 
395 struct bpf_iter_attach_opts {
396 	size_t sz; /* size of this struct for forward/backward compatibility */
397 	union bpf_iter_link_info *link_info;
398 	__u32 link_info_len;
399 };
400 #define bpf_iter_attach_opts__last_field link_info_len
401 
402 LIBBPF_API struct bpf_link *
403 bpf_program__attach_iter(const struct bpf_program *prog,
404 			 const struct bpf_iter_attach_opts *opts);
405 
406 /*
407  * Libbpf allows callers to adjust BPF programs before being loaded
408  * into kernel. One program in an object file can be transformed into
409  * multiple variants to be attached to different hooks.
410  *
411  * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
412  * form an API for this purpose.
413  *
414  * - bpf_program_prep_t:
415  *   Defines a 'preprocessor', which is a caller defined function
416  *   passed to libbpf through bpf_program__set_prep(), and will be
417  *   called before program is loaded. The processor should adjust
418  *   the program one time for each instance according to the instance id
419  *   passed to it.
420  *
421  * - bpf_program__set_prep:
422  *   Attaches a preprocessor to a BPF program. The number of instances
423  *   that should be created is also passed through this function.
424  *
425  * - bpf_program__nth_fd:
426  *   After the program is loaded, get resulting FD of a given instance
427  *   of the BPF program.
428  *
429  * If bpf_program__set_prep() is not used, the program would be loaded
430  * without adjustment during bpf_object__load(). The program has only
431  * one instance. In this case bpf_program__fd(prog) is equal to
432  * bpf_program__nth_fd(prog, 0).
433  */
434 struct bpf_prog_prep_result {
435 	/*
436 	 * If not NULL, load new instruction array.
437 	 * If set to NULL, don't load this instance.
438 	 */
439 	struct bpf_insn *new_insn_ptr;
440 	int new_insn_cnt;
441 
442 	/* If not NULL, result FD is written to it. */
443 	int *pfd;
444 };
445 
446 /*
447  * Parameters of bpf_program_prep_t:
448  *  - prog:	The bpf_program being loaded.
449  *  - n:	Index of instance being generated.
450  *  - insns:	BPF instructions array.
451  *  - insns_cnt:Number of instructions in insns.
452  *  - res:	Output parameter, result of transformation.
453  *
454  * Return value:
455  *  - Zero:	pre-processing success.
456  *  - Non-zero:	pre-processing error, stop loading.
457  */
458 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
459 				  struct bpf_insn *insns, int insns_cnt,
460 				  struct bpf_prog_prep_result *res);
461 
462 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insns() for getting bpf_program instructions")
463 LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
464 				     bpf_program_prep_t prep);
465 
466 LIBBPF_DEPRECATED_SINCE(0, 7, "multi-instance bpf_program support is deprecated")
467 LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n);
468 
469 /*
470  * Adjust type of BPF program. Default is kprobe.
471  */
472 LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
473 LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
474 LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
475 LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
476 LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog);
477 LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
478 LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
479 LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
480 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
481 LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
482 LIBBPF_API int bpf_program__set_struct_ops(struct bpf_program *prog);
483 LIBBPF_API int bpf_program__set_extension(struct bpf_program *prog);
484 LIBBPF_API int bpf_program__set_sk_lookup(struct bpf_program *prog);
485 
486 LIBBPF_API enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog);
487 LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
488 				      enum bpf_prog_type type);
489 
490 LIBBPF_API enum bpf_attach_type
491 bpf_program__get_expected_attach_type(const struct bpf_program *prog);
492 LIBBPF_API void
493 bpf_program__set_expected_attach_type(struct bpf_program *prog,
494 				      enum bpf_attach_type type);
495 
496 LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
497 LIBBPF_API int bpf_program__set_extra_flags(struct bpf_program *prog, __u32 extra_flags);
498 
499 LIBBPF_API int
500 bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
501 			       const char *attach_func_name);
502 
503 LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog);
504 LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog);
505 LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog);
506 LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog);
507 LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog);
508 LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog);
509 LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog);
510 LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
511 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
512 LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
513 LIBBPF_API bool bpf_program__is_struct_ops(const struct bpf_program *prog);
514 LIBBPF_API bool bpf_program__is_extension(const struct bpf_program *prog);
515 LIBBPF_API bool bpf_program__is_sk_lookup(const struct bpf_program *prog);
516 
517 /*
518  * No need for __attribute__((packed)), all members of 'bpf_map_def'
519  * are all aligned.  In addition, using __attribute__((packed))
520  * would trigger a -Wpacked warning message, and lead to an error
521  * if -Werror is set.
522  */
523 struct bpf_map_def {
524 	unsigned int type;
525 	unsigned int key_size;
526 	unsigned int value_size;
527 	unsigned int max_entries;
528 	unsigned int map_flags;
529 };
530 
531 /**
532  * @brief **bpf_object__find_map_by_name()** returns BPF map of
533  * the given name, if it exists within the passed BPF object
534  * @param obj BPF object
535  * @param name name of the BPF map
536  * @return BPF map instance, if such map exists within the BPF object;
537  * or NULL otherwise.
538  */
539 LIBBPF_API struct bpf_map *
540 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
541 
542 LIBBPF_API int
543 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
544 
545 /*
546  * Get bpf_map through the offset of corresponding struct bpf_map_def
547  * in the BPF object file.
548  */
549 LIBBPF_API struct bpf_map *
550 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
551 
552 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__next_map() instead")
553 struct bpf_map *bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
554 LIBBPF_API struct bpf_map *
555 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
556 
557 #define bpf_object__for_each_map(pos, obj)		\
558 	for ((pos) = bpf_object__next_map((obj), NULL);	\
559 	     (pos) != NULL;				\
560 	     (pos) = bpf_object__next_map((obj), (pos)))
561 #define bpf_map__for_each bpf_object__for_each_map
562 
563 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__prev_map() instead")
564 struct bpf_map *bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
565 LIBBPF_API struct bpf_map *
566 bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
567 
568 /**
569  * @brief **bpf_map__fd()** gets the file descriptor of the passed
570  * BPF map
571  * @param map the BPF map instance
572  * @return the file descriptor; or -EINVAL in case of an error
573  */
574 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
575 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
576 /* get map definition */
577 LIBBPF_API const struct bpf_map_def *bpf_map__def(const struct bpf_map *map);
578 /* get map name */
579 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
580 /* get/set map type */
581 LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
582 LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
583 /* get/set map size (max_entries) */
584 LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
585 LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
586 LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
587 /* get/set map flags */
588 LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
589 LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
590 /* get/set map NUMA node */
591 LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
592 LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
593 /* get/set map key size */
594 LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
595 LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
596 /* get/set map value size */
597 LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
598 LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
599 /* get map key/value BTF type IDs */
600 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
601 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
602 /* get/set map if_index */
603 LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
604 LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
605 /* get/set map map_extra flags */
606 LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
607 LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
608 
609 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
610 LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
611 				 bpf_map_clear_priv_t clear_priv);
612 LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
613 LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
614 					  const void *data, size_t size);
615 LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
616 LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);
617 
618 /**
619  * @brief **bpf_map__is_internal()** tells the caller whether or not the
620  * passed map is a special map created by libbpf automatically for things like
621  * global variables, __ksym externs, Kconfig values, etc
622  * @param map the bpf_map
623  * @return true, if the map is an internal map; false, otherwise
624  */
625 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
626 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
627 LIBBPF_API const char *bpf_map__get_pin_path(const struct bpf_map *map);
628 LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
629 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
630 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
631 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
632 
633 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
634 LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
635 
636 /**
637  * @brief **libbpf_get_error()** extracts the error code from the passed
638  * pointer
639  * @param ptr pointer returned from libbpf API function
640  * @return error code; or 0 if no error occured
641  *
642  * Many libbpf API functions which return pointers have logic to encode error
643  * codes as pointers, and do not return NULL. Meaning **libbpf_get_error()**
644  * should be used on the return value from these functions immediately after
645  * calling the API function, with no intervening calls that could clobber the
646  * `errno` variable. Consult the individual functions documentation to verify
647  * if this logic applies should be used.
648  *
649  * For these API functions, if `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)`
650  * is enabled, NULL is returned on error instead.
651  *
652  * If ptr is NULL, then errno should be already set by the failing
653  * API, because libbpf never returns NULL on success and it now always
654  * sets errno on error.
655  *
656  * Example usage:
657  *
658  *   struct perf_buffer *pb;
659  *
660  *   pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES, &opts);
661  *   err = libbpf_get_error(pb);
662  *   if (err) {
663  *	  pb = NULL;
664  *	  fprintf(stderr, "failed to open perf buffer: %d\n", err);
665  *	  goto cleanup;
666  *   }
667  */
668 LIBBPF_API long libbpf_get_error(const void *ptr);
669 
670 struct bpf_prog_load_attr {
671 	const char *file;
672 	enum bpf_prog_type prog_type;
673 	enum bpf_attach_type expected_attach_type;
674 	int ifindex;
675 	int log_level;
676 	int prog_flags;
677 };
678 
679 LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
680 				   struct bpf_object **pobj, int *prog_fd);
681 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__open() and bpf_object__load() instead")
682 LIBBPF_API int bpf_prog_load_deprecated(const char *file, enum bpf_prog_type type,
683 					struct bpf_object **pobj, int *prog_fd);
684 
685 /* XDP related API */
686 struct xdp_link_info {
687 	__u32 prog_id;
688 	__u32 drv_prog_id;
689 	__u32 hw_prog_id;
690 	__u32 skb_prog_id;
691 	__u8 attach_mode;
692 };
693 
694 struct bpf_xdp_set_link_opts {
695 	size_t sz;
696 	int old_fd;
697 	size_t :0;
698 };
699 #define bpf_xdp_set_link_opts__last_field old_fd
700 
701 LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
702 LIBBPF_API int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
703 					const struct bpf_xdp_set_link_opts *opts);
704 LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
705 LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
706 				     size_t info_size, __u32 flags);
707 
708 /* TC related API */
709 enum bpf_tc_attach_point {
710 	BPF_TC_INGRESS = 1 << 0,
711 	BPF_TC_EGRESS  = 1 << 1,
712 	BPF_TC_CUSTOM  = 1 << 2,
713 };
714 
715 #define BPF_TC_PARENT(a, b) 	\
716 	((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
717 
718 enum bpf_tc_flags {
719 	BPF_TC_F_REPLACE = 1 << 0,
720 };
721 
722 struct bpf_tc_hook {
723 	size_t sz;
724 	int ifindex;
725 	enum bpf_tc_attach_point attach_point;
726 	__u32 parent;
727 	size_t :0;
728 };
729 #define bpf_tc_hook__last_field parent
730 
731 struct bpf_tc_opts {
732 	size_t sz;
733 	int prog_fd;
734 	__u32 flags;
735 	__u32 prog_id;
736 	__u32 handle;
737 	__u32 priority;
738 	size_t :0;
739 };
740 #define bpf_tc_opts__last_field priority
741 
742 LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
743 LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
744 LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
745 			     struct bpf_tc_opts *opts);
746 LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
747 			     const struct bpf_tc_opts *opts);
748 LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
749 			    struct bpf_tc_opts *opts);
750 
751 /* Ring buffer APIs */
752 struct ring_buffer;
753 
754 typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
755 
756 struct ring_buffer_opts {
757 	size_t sz; /* size of this struct, for forward/backward compatiblity */
758 };
759 
760 #define ring_buffer_opts__last_field sz
761 
762 LIBBPF_API struct ring_buffer *
763 ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
764 		 const struct ring_buffer_opts *opts);
765 LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
766 LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
767 				ring_buffer_sample_fn sample_cb, void *ctx);
768 LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
769 LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
770 LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
771 
772 /* Perf buffer APIs */
773 struct perf_buffer;
774 
775 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
776 				      void *data, __u32 size);
777 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
778 
779 /* common use perf buffer options */
780 struct perf_buffer_opts {
781 	union {
782 		size_t sz;
783 		struct { /* DEPRECATED: will be removed in v1.0 */
784 			/* if specified, sample_cb is called for each sample */
785 			perf_buffer_sample_fn sample_cb;
786 			/* if specified, lost_cb is called for each batch of lost samples */
787 			perf_buffer_lost_fn lost_cb;
788 			/* ctx is provided to sample_cb and lost_cb */
789 			void *ctx;
790 		};
791 	};
792 };
793 #define perf_buffer_opts__last_field sz
794 
795 /**
796  * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
797  * BPF_PERF_EVENT_ARRAY map
798  * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
799  * code to send data over to user-space
800  * @param page_cnt number of memory pages allocated for each per-CPU buffer
801  * @param sample_cb function called on each received data record
802  * @param lost_cb function called when record loss has occurred
803  * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
804  * @return a new instance of struct perf_buffer on success, NULL on error with
805  * *errno* containing an error code
806  */
807 LIBBPF_API struct perf_buffer *
808 perf_buffer__new(int map_fd, size_t page_cnt,
809 		 perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
810 		 const struct perf_buffer_opts *opts);
811 
812 LIBBPF_API struct perf_buffer *
813 perf_buffer__new_v0_6_0(int map_fd, size_t page_cnt,
814 			perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
815 			const struct perf_buffer_opts *opts);
816 
817 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use new variant of perf_buffer__new() instead")
818 struct perf_buffer *perf_buffer__new_deprecated(int map_fd, size_t page_cnt,
819 						const struct perf_buffer_opts *opts);
820 
821 #define perf_buffer__new(...) ___libbpf_overload(___perf_buffer_new, __VA_ARGS__)
822 #define ___perf_buffer_new6(map_fd, page_cnt, sample_cb, lost_cb, ctx, opts) \
823 	perf_buffer__new(map_fd, page_cnt, sample_cb, lost_cb, ctx, opts)
824 #define ___perf_buffer_new3(map_fd, page_cnt, opts) \
825 	perf_buffer__new_deprecated(map_fd, page_cnt, opts)
826 
827 enum bpf_perf_event_ret {
828 	LIBBPF_PERF_EVENT_DONE	= 0,
829 	LIBBPF_PERF_EVENT_ERROR	= -1,
830 	LIBBPF_PERF_EVENT_CONT	= -2,
831 };
832 
833 struct perf_event_header;
834 
835 typedef enum bpf_perf_event_ret
836 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
837 
838 /* raw perf buffer options, giving most power and control */
839 struct perf_buffer_raw_opts {
840 	union {
841 		struct {
842 			size_t sz;
843 			long :0;
844 			long :0;
845 		};
846 		struct { /* DEPRECATED: will be removed in v1.0 */
847 			/* perf event attrs passed directly into perf_event_open() */
848 			struct perf_event_attr *attr;
849 			/* raw event callback */
850 			perf_buffer_event_fn event_cb;
851 			/* ctx is provided to event_cb */
852 			void *ctx;
853 		};
854 	};
855 	/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
856 	 * max_entries of given PERF_EVENT_ARRAY map)
857 	 */
858 	int cpu_cnt;
859 	/* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
860 	int *cpus;
861 	/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
862 	int *map_keys;
863 };
864 #define perf_buffer_raw_opts__last_field map_keys
865 
866 LIBBPF_API struct perf_buffer *
867 perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
868 		     perf_buffer_event_fn event_cb, void *ctx,
869 		     const struct perf_buffer_raw_opts *opts);
870 
871 LIBBPF_API struct perf_buffer *
872 perf_buffer__new_raw_v0_6_0(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
873 			    perf_buffer_event_fn event_cb, void *ctx,
874 			    const struct perf_buffer_raw_opts *opts);
875 
876 LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "use new variant of perf_buffer__new_raw() instead")
877 struct perf_buffer *perf_buffer__new_raw_deprecated(int map_fd, size_t page_cnt,
878 						    const struct perf_buffer_raw_opts *opts);
879 
880 #define perf_buffer__new_raw(...) ___libbpf_overload(___perf_buffer_new_raw, __VA_ARGS__)
881 #define ___perf_buffer_new_raw6(map_fd, page_cnt, attr, event_cb, ctx, opts) \
882 	perf_buffer__new_raw(map_fd, page_cnt, attr, event_cb, ctx, opts)
883 #define ___perf_buffer_new_raw3(map_fd, page_cnt, opts) \
884 	perf_buffer__new_raw_deprecated(map_fd, page_cnt, opts)
885 
886 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
887 LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
888 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
889 LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
890 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
891 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
892 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
893 
894 typedef enum bpf_perf_event_ret
895 	(*bpf_perf_event_print_t)(struct perf_event_header *hdr,
896 				  void *private_data);
897 LIBBPF_API enum bpf_perf_event_ret
898 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
899 			   void **copy_mem, size_t *copy_size,
900 			   bpf_perf_event_print_t fn, void *private_data);
901 
902 struct bpf_prog_linfo;
903 struct bpf_prog_info;
904 
905 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
906 LIBBPF_API struct bpf_prog_linfo *
907 bpf_prog_linfo__new(const struct bpf_prog_info *info);
908 LIBBPF_API const struct bpf_line_info *
909 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
910 				__u64 addr, __u32 func_idx, __u32 nr_skip);
911 LIBBPF_API const struct bpf_line_info *
912 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
913 		      __u32 insn_off, __u32 nr_skip);
914 
915 /*
916  * Probe for supported system features
917  *
918  * Note that running many of these probes in a short amount of time can cause
919  * the kernel to reach the maximal size of lockable memory allowed for the
920  * user, causing subsequent probes to fail. In this case, the caller may want
921  * to adjust that limit with setrlimit().
922  */
923 LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type,
924 				    __u32 ifindex);
925 LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
926 LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id,
927 				 enum bpf_prog_type prog_type, __u32 ifindex);
928 LIBBPF_API bool bpf_probe_large_insn_limit(__u32 ifindex);
929 
930 /*
931  * Get bpf_prog_info in continuous memory
932  *
933  * struct bpf_prog_info has multiple arrays. The user has option to choose
934  * arrays to fetch from kernel. The following APIs provide an uniform way to
935  * fetch these data. All arrays in bpf_prog_info are stored in a single
936  * continuous memory region. This makes it easy to store the info in a
937  * file.
938  *
939  * Before writing bpf_prog_info_linear to files, it is necessary to
940  * translate pointers in bpf_prog_info to offsets. Helper functions
941  * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr()
942  * are introduced to switch between pointers and offsets.
943  *
944  * Examples:
945  *   # To fetch map_ids and prog_tags:
946  *   __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) |
947  *           (1UL << BPF_PROG_INFO_PROG_TAGS);
948  *   struct bpf_prog_info_linear *info_linear =
949  *           bpf_program__get_prog_info_linear(fd, arrays);
950  *
951  *   # To save data in file
952  *   bpf_program__bpil_addr_to_offs(info_linear);
953  *   write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
954  *
955  *   # To read data from file
956  *   read(f, info_linear, <proper_size>);
957  *   bpf_program__bpil_offs_to_addr(info_linear);
958  */
959 enum bpf_prog_info_array {
960 	BPF_PROG_INFO_FIRST_ARRAY = 0,
961 	BPF_PROG_INFO_JITED_INSNS = 0,
962 	BPF_PROG_INFO_XLATED_INSNS,
963 	BPF_PROG_INFO_MAP_IDS,
964 	BPF_PROG_INFO_JITED_KSYMS,
965 	BPF_PROG_INFO_JITED_FUNC_LENS,
966 	BPF_PROG_INFO_FUNC_INFO,
967 	BPF_PROG_INFO_LINE_INFO,
968 	BPF_PROG_INFO_JITED_LINE_INFO,
969 	BPF_PROG_INFO_PROG_TAGS,
970 	BPF_PROG_INFO_LAST_ARRAY,
971 };
972 
973 struct bpf_prog_info_linear {
974 	/* size of struct bpf_prog_info, when the tool is compiled */
975 	__u32			info_len;
976 	/* total bytes allocated for data, round up to 8 bytes */
977 	__u32			data_len;
978 	/* which arrays are included in data */
979 	__u64			arrays;
980 	struct bpf_prog_info	info;
981 	__u8			data[];
982 };
983 
984 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
985 LIBBPF_API struct bpf_prog_info_linear *
986 bpf_program__get_prog_info_linear(int fd, __u64 arrays);
987 
988 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
989 LIBBPF_API void
990 bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
991 
992 LIBBPF_DEPRECATED_SINCE(0, 6, "use a custom linear prog_info wrapper")
993 LIBBPF_API void
994 bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
995 
996 /**
997  * @brief **libbpf_num_possible_cpus()** is a helper function to get the
998  * number of possible CPUs that the host kernel supports and expects.
999  * @return number of possible CPUs; or error code on failure
1000  *
1001  * Example usage:
1002  *
1003  *     int ncpus = libbpf_num_possible_cpus();
1004  *     if (ncpus < 0) {
1005  *          // error handling
1006  *     }
1007  *     long values[ncpus];
1008  *     bpf_map_lookup_elem(per_cpu_map_fd, key, values);
1009  */
1010 LIBBPF_API int libbpf_num_possible_cpus(void);
1011 
1012 struct bpf_map_skeleton {
1013 	const char *name;
1014 	struct bpf_map **map;
1015 	void **mmaped;
1016 };
1017 
1018 struct bpf_prog_skeleton {
1019 	const char *name;
1020 	struct bpf_program **prog;
1021 	struct bpf_link **link;
1022 };
1023 
1024 struct bpf_object_skeleton {
1025 	size_t sz; /* size of this struct, for forward/backward compatibility */
1026 
1027 	const char *name;
1028 	const void *data;
1029 	size_t data_sz;
1030 
1031 	struct bpf_object **obj;
1032 
1033 	int map_cnt;
1034 	int map_skel_sz; /* sizeof(struct bpf_skeleton_map) */
1035 	struct bpf_map_skeleton *maps;
1036 
1037 	int prog_cnt;
1038 	int prog_skel_sz; /* sizeof(struct bpf_skeleton_prog) */
1039 	struct bpf_prog_skeleton *progs;
1040 };
1041 
1042 LIBBPF_API int
1043 bpf_object__open_skeleton(struct bpf_object_skeleton *s,
1044 			  const struct bpf_object_open_opts *opts);
1045 LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
1046 LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
1047 LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
1048 LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
1049 
1050 struct gen_loader_opts {
1051 	size_t sz; /* size of this struct, for forward/backward compatiblity */
1052 	const char *data;
1053 	const char *insns;
1054 	__u32 data_sz;
1055 	__u32 insns_sz;
1056 };
1057 
1058 #define gen_loader_opts__last_field insns_sz
1059 LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
1060 				      struct gen_loader_opts *opts);
1061 
1062 enum libbpf_tristate {
1063 	TRI_NO = 0,
1064 	TRI_YES = 1,
1065 	TRI_MODULE = 2,
1066 };
1067 
1068 struct bpf_linker_opts {
1069 	/* size of this struct, for forward/backward compatiblity */
1070 	size_t sz;
1071 };
1072 #define bpf_linker_opts__last_field sz
1073 
1074 struct bpf_linker_file_opts {
1075 	/* size of this struct, for forward/backward compatiblity */
1076 	size_t sz;
1077 };
1078 #define bpf_linker_file_opts__last_field sz
1079 
1080 struct bpf_linker;
1081 
1082 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
1083 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
1084 				    const char *filename,
1085 				    const struct bpf_linker_file_opts *opts);
1086 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
1087 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
1088 
1089 #ifdef __cplusplus
1090 } /* extern "C" */
1091 #endif
1092 
1093 #endif /* __LIBBPF_LIBBPF_H */
1094