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