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