xref: /linux/tools/lib/bpf/bpf.h (revision 2b5440b31cafad2fb4b4a83efb780a6d7430a385)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 
3 /*
4  * Common BPF ELF 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  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation;
13  * version 2.1 of the License (not later!)
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this program; if not,  see <http://www.gnu.org/licenses>
22  */
23 #ifndef __LIBBPF_BPF_H
24 #define __LIBBPF_BPF_H
25 
26 #include <linux/bpf.h>
27 #include <stdbool.h>
28 #include <stddef.h>
29 #include <stdint.h>
30 
31 #include "libbpf_common.h"
32 #include "libbpf_legacy.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 LIBBPF_API int libbpf_set_memlock_rlim(size_t memlock_bytes);
39 
40 struct bpf_log_opts {
41 	size_t sz; /* size of this struct for forward/backward compatibility */
42 
43 	char *buf;
44 	__u32 size;
45 	__u32 level;
46 	__u32 true_size; /* out parameter set by kernel */
47 
48 	size_t :0;
49 };
50 #define bpf_log_opts__last_field true_size
51 
52 struct bpf_map_create_opts {
53 	size_t sz; /* size of this struct for forward/backward compatibility */
54 
55 	__u32 btf_fd;
56 	__u32 btf_key_type_id;
57 	__u32 btf_value_type_id;
58 	__u32 btf_vmlinux_value_type_id;
59 
60 	__u32 inner_map_fd;
61 	__u32 map_flags;
62 	__u64 map_extra;
63 
64 	__u32 numa_node;
65 	__u32 map_ifindex;
66 	__s32 value_type_btf_obj_fd;
67 
68 	__u32 token_fd;
69 
70 	const void *excl_prog_hash;
71 	__u32 excl_prog_hash_size;
72 
73 	struct bpf_log_opts *log_opts;
74 
75 	size_t :0;
76 };
77 #define bpf_map_create_opts__last_field log_opts
78 
79 LIBBPF_API int bpf_map_create(enum bpf_map_type map_type,
80 			      const char *map_name,
81 			      __u32 key_size,
82 			      __u32 value_size,
83 			      __u32 max_entries,
84 			      const struct bpf_map_create_opts *opts);
85 
86 struct bpf_prog_load_opts {
87 	size_t sz; /* size of this struct for forward/backward compatibility */
88 
89 	/* libbpf can retry BPF_PROG_LOAD command if bpf() syscall returns
90 	 * -EAGAIN. This field determines how many attempts libbpf has to
91 	 *  make. If not specified, libbpf will use default value of 5.
92 	 */
93 	int attempts;
94 
95 	enum bpf_attach_type expected_attach_type;
96 	__u32 prog_btf_fd;
97 	__u32 prog_flags;
98 	__u32 prog_ifindex;
99 	__u32 kern_version;
100 
101 	__u32 attach_btf_id;
102 	__u32 attach_prog_fd;
103 	__u32 attach_btf_obj_fd;
104 
105 	const int *fd_array;
106 
107 	/* .BTF.ext func info data */
108 	const void *func_info;
109 	__u32 func_info_cnt;
110 	__u32 func_info_rec_size;
111 
112 	/* .BTF.ext line info data */
113 	const void *line_info;
114 	__u32 line_info_cnt;
115 	__u32 line_info_rec_size;
116 
117 	/* verifier log options */
118 	__u32 log_level;
119 	__u32 log_size;
120 	char *log_buf;
121 	/* output: actual total log contents size (including terminating zero).
122 	 * It could be both larger than original log_size (if log was
123 	 * truncated), or smaller (if log buffer wasn't filled completely).
124 	 * If kernel doesn't support this feature, log_size is left unchanged.
125 	 */
126 	__u32 log_true_size;
127 	__u32 token_fd;
128 
129 	/* if set, provides the length of fd_array */
130 	__u32 fd_array_cnt;
131 	size_t :0;
132 };
133 #define bpf_prog_load_opts__last_field fd_array_cnt
134 
135 LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type,
136 			     const char *prog_name, const char *license,
137 			     const struct bpf_insn *insns, size_t insn_cnt,
138 			     struct bpf_prog_load_opts *opts);
139 
140 /* Flags to direct loading requirements */
141 #define MAPS_RELAX_COMPAT	0x01
142 
143 /* Recommended log buffer size */
144 #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */
145 
146 struct bpf_btf_load_opts {
147 	size_t sz; /* size of this struct for forward/backward compatibility */
148 
149 	/* kernel log options */
150 	char *log_buf;
151 	__u32 log_level;
152 	__u32 log_size;
153 	/* output: actual total log contents size (including terminating zero).
154 	 * It could be both larger than original log_size (if log was
155 	 * truncated), or smaller (if log buffer wasn't filled completely).
156 	 * If kernel doesn't support this feature, log_size is left unchanged.
157 	 */
158 	__u32 log_true_size;
159 
160 	__u32 btf_flags;
161 	__u32 token_fd;
162 	size_t :0;
163 };
164 #define bpf_btf_load_opts__last_field token_fd
165 
166 LIBBPF_API int bpf_btf_load(const void *btf_data, size_t btf_size,
167 			    struct bpf_btf_load_opts *opts);
168 
169 LIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value,
170 				   __u64 flags);
171 
172 LIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value);
173 LIBBPF_API int bpf_map_lookup_elem_flags(int fd, const void *key, void *value,
174 					 __u64 flags);
175 LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key,
176 					      void *value);
177 LIBBPF_API int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key,
178 						    void *value, __u64 flags);
179 LIBBPF_API int bpf_map_delete_elem(int fd, const void *key);
180 LIBBPF_API int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags);
181 LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key);
182 LIBBPF_API int bpf_map_freeze(int fd);
183 
184 struct bpf_map_batch_opts {
185 	size_t sz; /* size of this struct for forward/backward compatibility */
186 	__u64 elem_flags;
187 	__u64 flags;
188 };
189 #define bpf_map_batch_opts__last_field flags
190 
191 
192 /**
193  * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple
194  * elements in a BPF map.
195  *
196  * @param fd BPF map file descriptor
197  * @param keys pointer to an array of *count* keys
198  * @param count input and output parameter; on input **count** represents the
199  * number of  elements in the map to delete in batch;
200  * on output if a non-EFAULT error is returned, **count** represents the number of deleted
201  * elements if the output **count** value is not equal to the input **count** value
202  * If EFAULT is returned, **count** should not be trusted to be correct.
203  * @param opts options for configuring the way the batch deletion works
204  * @return 0, on success; negative error code, otherwise (errno is also set to
205  * the error code)
206  */
207 LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys,
208 				    __u32 *count,
209 				    const struct bpf_map_batch_opts *opts);
210 
211 /**
212  * @brief **bpf_map_lookup_batch()** allows for batch lookup of BPF map elements.
213  *
214  * The parameter *in_batch* is the address of the first element in the batch to
215  * read. *out_batch* is an output parameter that should be passed as *in_batch*
216  * to subsequent calls to **bpf_map_lookup_batch()**. NULL can be passed for
217  * *in_batch* to indicate that the batched lookup starts from the beginning of
218  * the map. Both *in_batch* and *out_batch* must point to memory large enough to
219  * hold a single key, except for maps of type **BPF_MAP_TYPE_{HASH, PERCPU_HASH,
220  * LRU_HASH, LRU_PERCPU_HASH}**, for which the memory size must be at
221  * least 4 bytes wide regardless of key size.
222  *
223  * The *keys* and *values* are output parameters which must point to memory large enough to
224  * hold *count* items based on the key and value size of the map *map_fd*. The *keys*
225  * buffer must be of *key_size* * *count*. The *values* buffer must be of
226  * *value_size* * *count*.
227  *
228  * @param fd BPF map file descriptor
229  * @param in_batch address of the first element in batch to read, can pass NULL to
230  * indicate that the batched lookup starts from the beginning of the map.
231  * @param out_batch output parameter that should be passed to next call as *in_batch*
232  * @param keys pointer to an array large enough for *count* keys
233  * @param values pointer to an array large enough for *count* values
234  * @param count input and output parameter; on input it's the number of elements
235  * in the map to read in batch; on output it's the number of elements that were
236  * successfully read.
237  * If a non-EFAULT error is returned, count will be set as the number of elements
238  * that were read before the error occurred.
239  * If EFAULT is returned, **count** should not be trusted to be correct.
240  * @param opts options for configuring the way the batch lookup works
241  * @return 0, on success; negative error code, otherwise (errno is also set to
242  * the error code)
243  */
244 LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch,
245 				    void *keys, void *values, __u32 *count,
246 				    const struct bpf_map_batch_opts *opts);
247 
248 /**
249  * @brief **bpf_map_lookup_and_delete_batch()** allows for batch lookup and deletion
250  * of BPF map elements where each element is deleted after being retrieved.
251  *
252  * @param fd BPF map file descriptor
253  * @param in_batch address of the first element in batch to read, can pass NULL to
254  * get address of the first element in *out_batch*. If not NULL, must be large
255  * enough to hold a key. For **BPF_MAP_TYPE_{HASH, PERCPU_HASH, LRU_HASH,
256  * LRU_PERCPU_HASH}**, the memory size must be at least 4 bytes wide regardless
257  * of key size.
258  * @param out_batch output parameter that should be passed to next call as *in_batch*
259  * @param keys pointer to an array of *count* keys
260  * @param values pointer to an array large enough for *count* values
261  * @param count input and output parameter; on input it's the number of elements
262  * in the map to read and delete in batch; on output it represents the number of
263  * elements that were successfully read and deleted
264  * If a non-**EFAULT** error code is returned and if the output **count** value
265  * is not equal to the input **count** value, up to **count** elements may
266  * have been deleted.
267  * if **EFAULT** is returned up to *count* elements may have been deleted without
268  * being returned via the *keys* and *values* output parameters.
269  * @param opts options for configuring the way the batch lookup and delete works
270  * @return 0, on success; negative error code, otherwise (errno is also set to
271  * the error code)
272  */
273 LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch,
274 					void *out_batch, void *keys,
275 					void *values, __u32 *count,
276 					const struct bpf_map_batch_opts *opts);
277 
278 /**
279  * @brief **bpf_map_update_batch()** updates multiple elements in a map
280  * by specifying keys and their corresponding values.
281  *
282  * The *keys* and *values* parameters must point to memory large enough
283  * to hold *count* items based on the key and value size of the map.
284  *
285  * The *opts* parameter can be used to control how *bpf_map_update_batch()*
286  * should handle keys that either do or do not already exist in the map.
287  * In particular the *flags* parameter of *bpf_map_batch_opts* can be
288  * one of the following:
289  *
290  * Note that *count* is an input and output parameter, where on output it
291  * represents how many elements were successfully updated. Also note that if
292  * **EFAULT** then *count* should not be trusted to be correct.
293  *
294  * **BPF_ANY**
295  *    Create new elements or update existing.
296  *
297  * **BPF_NOEXIST**
298  *    Create new elements only if they do not exist.
299  *
300  * **BPF_EXIST**
301  *    Update existing elements.
302  *
303  * **BPF_F_LOCK**
304  *    Update spin_lock-ed map elements. This must be
305  *    specified if the map value contains a spinlock.
306  *
307  * **BPF_F_CPU**
308  *    As for percpu maps, update value on the specified CPU. And the cpu
309  *    info is embedded into the high 32 bits of **opts->elem_flags**.
310  *
311  * **BPF_F_ALL_CPUS**
312  *    As for percpu maps, update value across all CPUs. This flag cannot
313  *    be used with BPF_F_CPU at the same time.
314  *
315  * @param fd BPF map file descriptor
316  * @param keys pointer to an array of *count* keys
317  * @param values pointer to an array of *count* values
318  * @param count input and output parameter; on input it's the number of elements
319  * in the map to update in batch; on output if a non-EFAULT error is returned,
320  * **count** represents the number of updated elements if the output **count**
321  * value is not equal to the input **count** value.
322  * If EFAULT is returned, **count** should not be trusted to be correct.
323  * @param opts options for configuring the way the batch update works
324  * @return 0, on success; negative error code, otherwise (errno is also set to
325  * the error code)
326  */
327 LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values,
328 				    __u32 *count,
329 				    const struct bpf_map_batch_opts *opts);
330 
331 struct bpf_obj_pin_opts {
332 	size_t sz; /* size of this struct for forward/backward compatibility */
333 
334 	__u32 file_flags;
335 	int path_fd;
336 
337 	size_t :0;
338 };
339 #define bpf_obj_pin_opts__last_field path_fd
340 
341 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
342 LIBBPF_API int bpf_obj_pin_opts(int fd, const char *pathname,
343 				const struct bpf_obj_pin_opts *opts);
344 
345 struct bpf_obj_get_opts {
346 	size_t sz; /* size of this struct for forward/backward compatibility */
347 
348 	__u32 file_flags;
349 	int path_fd;
350 
351 	size_t :0;
352 };
353 #define bpf_obj_get_opts__last_field path_fd
354 
355 LIBBPF_API int bpf_obj_get(const char *pathname);
356 LIBBPF_API int bpf_obj_get_opts(const char *pathname,
357 				const struct bpf_obj_get_opts *opts);
358 
359 LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
360 			       enum bpf_attach_type type, unsigned int flags);
361 LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
362 LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
363 				enum bpf_attach_type type);
364 
365 struct bpf_prog_attach_opts {
366 	size_t sz; /* size of this struct for forward/backward compatibility */
367 	__u32 flags;
368 	union {
369 		int replace_prog_fd;
370 		int replace_fd;
371 	};
372 	int relative_fd;
373 	__u32 relative_id;
374 	__u64 expected_revision;
375 	size_t :0;
376 };
377 #define bpf_prog_attach_opts__last_field expected_revision
378 
379 struct bpf_prog_detach_opts {
380 	size_t sz; /* size of this struct for forward/backward compatibility */
381 	__u32 flags;
382 	int relative_fd;
383 	__u32 relative_id;
384 	__u64 expected_revision;
385 	size_t :0;
386 };
387 #define bpf_prog_detach_opts__last_field expected_revision
388 
389 /**
390  * @brief **bpf_prog_attach_opts()** attaches the BPF program corresponding to
391  * *prog_fd* to a *target* which can represent a file descriptor or netdevice
392  * ifindex.
393  *
394  * @param prog_fd BPF program file descriptor
395  * @param target attach location file descriptor or ifindex
396  * @param type attach type for the BPF program
397  * @param opts options for configuring the attachment
398  * @return 0, on success; negative error code, otherwise (errno is also set to
399  * the error code)
400  */
401 LIBBPF_API int bpf_prog_attach_opts(int prog_fd, int target,
402 				    enum bpf_attach_type type,
403 				    const struct bpf_prog_attach_opts *opts);
404 
405 /**
406  * @brief **bpf_prog_detach_opts()** detaches the BPF program corresponding to
407  * *prog_fd* from a *target* which can represent a file descriptor or netdevice
408  * ifindex.
409  *
410  * @param prog_fd BPF program file descriptor
411  * @param target detach location file descriptor or ifindex
412  * @param type detach type for the BPF program
413  * @param opts options for configuring the detachment
414  * @return 0, on success; negative error code, otherwise (errno is also set to
415  * the error code)
416  */
417 LIBBPF_API int bpf_prog_detach_opts(int prog_fd, int target,
418 				    enum bpf_attach_type type,
419 				    const struct bpf_prog_detach_opts *opts);
420 
421 union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */
422 struct bpf_link_create_opts {
423 	size_t sz; /* size of this struct for forward/backward compatibility */
424 	__u32 flags;
425 	union bpf_iter_link_info *iter_info;
426 	__u32 iter_info_len;
427 	__u32 target_btf_id;
428 	union {
429 		struct {
430 			__u64 bpf_cookie;
431 		} perf_event;
432 		struct {
433 			__u32 flags;
434 			__u32 cnt;
435 			const char **syms;
436 			const unsigned long *addrs;
437 			const __u64 *cookies;
438 		} kprobe_multi;
439 		struct {
440 			__u32 flags;
441 			__u32 cnt;
442 			const char *path;
443 			const unsigned long *offsets;
444 			const unsigned long *ref_ctr_offsets;
445 			const __u64 *cookies;
446 			__u32 pid;
447 			__u32 path_fd;
448 		} uprobe_multi;
449 		struct {
450 			__u64 cookie;
451 		} tracing;
452 		struct {
453 			__u32 pf;
454 			__u32 hooknum;
455 			__s32 priority;
456 			__u32 flags;
457 		} netfilter;
458 		struct {
459 			__u32 relative_fd;
460 			__u32 relative_id;
461 			__u64 expected_revision;
462 		} tcx;
463 		struct {
464 			__u32 relative_fd;
465 			__u32 relative_id;
466 			__u64 expected_revision;
467 		} netkit;
468 		struct {
469 			__u32 relative_fd;
470 			__u32 relative_id;
471 			__u64 expected_revision;
472 		} cgroup;
473 		struct {
474 			const __u32 *ids;
475 			const __u64 *cookies;
476 			__u32 cnt;
477 		} tracing_multi;
478 	};
479 	size_t :0;
480 };
481 #define bpf_link_create_opts__last_field uprobe_multi.path_fd
482 
483 LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
484 			       enum bpf_attach_type attach_type,
485 			       const struct bpf_link_create_opts *opts);
486 
487 LIBBPF_API int bpf_link_detach(int link_fd);
488 
489 struct bpf_link_update_opts {
490 	size_t sz; /* size of this struct for forward/backward compatibility */
491 	__u32 flags;	   /* extra flags */
492 	__u32 old_prog_fd; /* expected old program FD */
493 	__u32 old_map_fd;  /* expected old map FD */
494 };
495 #define bpf_link_update_opts__last_field old_map_fd
496 
497 LIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd,
498 			       const struct bpf_link_update_opts *opts);
499 
500 LIBBPF_API int bpf_iter_create(int link_fd);
501 
502 struct bpf_prog_test_run_attr {
503 	int prog_fd;
504 	int repeat;
505 	const void *data_in;
506 	__u32 data_size_in;
507 	void *data_out;      /* optional */
508 	__u32 data_size_out; /* in: max length of data_out
509 			      * out: length of data_out */
510 	__u32 retval;        /* out: return code of the BPF program */
511 	__u32 duration;      /* out: average per repetition in ns */
512 	const void *ctx_in; /* optional */
513 	__u32 ctx_size_in;
514 	void *ctx_out;      /* optional */
515 	__u32 ctx_size_out; /* in: max length of ctx_out
516 			     * out: length of cxt_out */
517 };
518 
519 LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id);
520 LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
521 LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id);
522 LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id);
523 
524 struct bpf_get_fd_by_id_opts {
525 	size_t sz; /* size of this struct for forward/backward compatibility */
526 	__u32 open_flags; /* permissions requested for the operation on fd */
527 	__u32 token_fd;
528 	size_t :0;
529 };
530 #define bpf_get_fd_by_id_opts__last_field token_fd
531 
532 LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
533 LIBBPF_API int bpf_prog_get_fd_by_id_opts(__u32 id,
534 				const struct bpf_get_fd_by_id_opts *opts);
535 LIBBPF_API int bpf_map_get_fd_by_id(__u32 id);
536 LIBBPF_API int bpf_map_get_fd_by_id_opts(__u32 id,
537 				const struct bpf_get_fd_by_id_opts *opts);
538 LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id);
539 LIBBPF_API int bpf_btf_get_fd_by_id_opts(__u32 id,
540 				const struct bpf_get_fd_by_id_opts *opts);
541 LIBBPF_API int bpf_link_get_fd_by_id(__u32 id);
542 LIBBPF_API int bpf_link_get_fd_by_id_opts(__u32 id,
543 				const struct bpf_get_fd_by_id_opts *opts);
544 LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len);
545 
546 /**
547  * @brief **bpf_prog_get_info_by_fd()** obtains information about the BPF
548  * program corresponding to *prog_fd*.
549  *
550  * Populates up to *info_len* bytes of *info* and updates *info_len* with the
551  * actual number of bytes written to *info*. Note that *info* should be
552  * zero-initialized or initialized as expected by the requested *info*
553  * type. Failing to (zero-)initialize *info* under certain circumstances can
554  * result in this helper returning an error.
555  *
556  * @param prog_fd BPF program file descriptor
557  * @param info pointer to **struct bpf_prog_info** that will be populated with
558  * BPF program information
559  * @param info_len pointer to the size of *info*; on success updated with the
560  * number of bytes written to *info*
561  * @return 0, on success; negative error code, otherwise (errno is also set to
562  * the error code)
563  */
564 LIBBPF_API int bpf_prog_get_info_by_fd(int prog_fd, struct bpf_prog_info *info, __u32 *info_len);
565 
566 /**
567  * @brief **bpf_map_get_info_by_fd()** obtains information about the BPF
568  * map corresponding to *map_fd*.
569  *
570  * Populates up to *info_len* bytes of *info* and updates *info_len* with the
571  * actual number of bytes written to *info*. Note that *info* should be
572  * zero-initialized or initialized as expected by the requested *info*
573  * type. Failing to (zero-)initialize *info* under certain circumstances can
574  * result in this helper returning an error.
575  *
576  * @param map_fd BPF map file descriptor
577  * @param info pointer to **struct bpf_map_info** that will be populated with
578  * BPF map information
579  * @param info_len pointer to the size of *info*; on success updated with the
580  * number of bytes written to *info*
581  * @return 0, on success; negative error code, otherwise (errno is also set to
582  * the error code)
583  */
584 LIBBPF_API int bpf_map_get_info_by_fd(int map_fd, struct bpf_map_info *info, __u32 *info_len);
585 
586 /**
587  * @brief **bpf_btf_get_info_by_fd()** obtains information about the
588  * BTF object corresponding to *btf_fd*.
589  *
590  * Populates up to *info_len* bytes of *info* and updates *info_len* with the
591  * actual number of bytes written to *info*. Note that *info* should be
592  * zero-initialized or initialized as expected by the requested *info*
593  * type. Failing to (zero-)initialize *info* under certain circumstances can
594  * result in this helper returning an error.
595  *
596  * @param btf_fd BTF object file descriptor
597  * @param info pointer to **struct bpf_btf_info** that will be populated with
598  * BTF object information
599  * @param info_len pointer to the size of *info*; on success updated with the
600  * number of bytes written to *info*
601  * @return 0, on success; negative error code, otherwise (errno is also set to
602  * the error code)
603  */
604 LIBBPF_API int bpf_btf_get_info_by_fd(int btf_fd, struct bpf_btf_info *info, __u32 *info_len);
605 
606 /**
607  * @brief **bpf_btf_get_info_by_fd()** obtains information about the BPF
608  * link corresponding to *link_fd*.
609  *
610  * Populates up to *info_len* bytes of *info* and updates *info_len* with the
611  * actual number of bytes written to *info*. Note that *info* should be
612  * zero-initialized or initialized as expected by the requested *info*
613  * type. Failing to (zero-)initialize *info* under certain circumstances can
614  * result in this helper returning an error.
615  *
616  * @param link_fd BPF link file descriptor
617  * @param info pointer to **struct bpf_link_info** that will be populated with
618  * BPF link information
619  * @param info_len pointer to the size of *info*; on success updated with the
620  * number of bytes written to *info*
621  * @return 0, on success; negative error code, otherwise (errno is also set to
622  * the error code)
623  */
624 LIBBPF_API int bpf_link_get_info_by_fd(int link_fd, struct bpf_link_info *info, __u32 *info_len);
625 
626 struct bpf_prog_query_opts {
627 	size_t sz; /* size of this struct for forward/backward compatibility */
628 	__u32 query_flags;
629 	__u32 attach_flags; /* output argument */
630 	__u32 *prog_ids;
631 	union {
632 		/* input+output argument */
633 		__u32 prog_cnt;
634 		__u32 count;
635 	};
636 	__u32 *prog_attach_flags;
637 	__u32 *link_ids;
638 	__u32 *link_attach_flags;
639 	__u64 revision;
640 	__u32 type_id;
641 	size_t :0;
642 };
643 #define bpf_prog_query_opts__last_field type_id
644 
645 /**
646  * @brief **bpf_prog_query_opts()** queries the BPF programs and BPF links
647  * which are attached to *target* which can represent a file descriptor or
648  * netdevice ifindex.
649  *
650  * @param target query location file descriptor or ifindex
651  * @param type attach type for the BPF program
652  * @param opts options for configuring the query
653  * @return 0, on success; negative error code, otherwise (errno is also set to
654  * the error code)
655  */
656 LIBBPF_API int bpf_prog_query_opts(int target, enum bpf_attach_type type,
657 				   struct bpf_prog_query_opts *opts);
658 LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type,
659 			      __u32 query_flags, __u32 *attach_flags,
660 			      __u32 *prog_ids, __u32 *prog_cnt);
661 
662 struct bpf_raw_tp_opts {
663 	size_t sz; /* size of this struct for forward/backward compatibility */
664 	const char *tp_name;
665 	__u64 cookie;
666 	size_t :0;
667 };
668 #define bpf_raw_tp_opts__last_field cookie
669 
670 LIBBPF_API int bpf_raw_tracepoint_open_opts(int prog_fd, struct bpf_raw_tp_opts *opts);
671 LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd);
672 LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf,
673 				 __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
674 				 __u64 *probe_offset, __u64 *probe_addr);
675 
676 #ifdef __cplusplus
677 /* forward-declaring enums in C++ isn't compatible with pure C enums, so
678  * instead define bpf_enable_stats() as accepting int as an input
679  */
680 LIBBPF_API int bpf_enable_stats(int type);
681 #else
682 enum bpf_stats_type; /* defined in up-to-date linux/bpf.h */
683 LIBBPF_API int bpf_enable_stats(enum bpf_stats_type type);
684 #endif
685 
686 struct bpf_prog_bind_opts {
687 	size_t sz; /* size of this struct for forward/backward compatibility */
688 	__u32 flags;
689 };
690 #define bpf_prog_bind_opts__last_field flags
691 
692 LIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd,
693 				 const struct bpf_prog_bind_opts *opts);
694 
695 struct bpf_test_run_opts {
696 	size_t sz; /* size of this struct for forward/backward compatibility */
697 	const void *data_in; /* optional */
698 	void *data_out;      /* optional */
699 	__u32 data_size_in;
700 	__u32 data_size_out; /* in: max length of data_out
701 			      * out: length of data_out
702 			      */
703 	const void *ctx_in; /* optional */
704 	void *ctx_out;      /* optional */
705 	__u32 ctx_size_in;
706 	__u32 ctx_size_out; /* in: max length of ctx_out
707 			     * out: length of cxt_out
708 			     */
709 	__u32 retval;        /* out: return code of the BPF program */
710 	int repeat;
711 	__u32 duration;      /* out: average per repetition in ns */
712 	__u32 flags;
713 	__u32 cpu;
714 	__u32 batch_size;
715 };
716 #define bpf_test_run_opts__last_field batch_size
717 
718 LIBBPF_API int bpf_prog_test_run_opts(int prog_fd,
719 				      struct bpf_test_run_opts *opts);
720 
721 struct bpf_token_create_opts {
722 	size_t sz; /* size of this struct for forward/backward compatibility */
723 	__u32 flags;
724 	size_t :0;
725 };
726 #define bpf_token_create_opts__last_field flags
727 
728 /**
729  * @brief **bpf_token_create()** creates a new instance of BPF token derived
730  * from specified BPF FS mount point.
731  *
732  * BPF token created with this API can be passed to bpf() syscall for
733  * commands like BPF_PROG_LOAD, BPF_MAP_CREATE, etc.
734  *
735  * @param bpffs_fd FD for BPF FS instance from which to derive a BPF token
736  * instance.
737  * @param opts optional BPF token creation options, can be NULL
738  *
739  * @return BPF token FD > 0, on success; negative error code, otherwise (errno
740  * is also set to the error code)
741  */
742 LIBBPF_API int bpf_token_create(int bpffs_fd,
743 				struct bpf_token_create_opts *opts);
744 
745 struct bpf_prog_stream_read_opts {
746 	size_t sz;
747 	size_t :0;
748 };
749 #define bpf_prog_stream_read_opts__last_field sz
750 /**
751  * @brief **bpf_prog_stream_read** reads data from the BPF stream of a given BPF
752  * program.
753  *
754  * @param prog_fd FD for the BPF program whose BPF stream is to be read.
755  * @param stream_id ID of the BPF stream to be read.
756  * @param buf Buffer to read data into from the BPF stream.
757  * @param buf_len Maximum number of bytes to read from the BPF stream.
758  * @param opts optional options, can be NULL
759  *
760  * @return The number of bytes read, on success; negative error code, otherwise
761  * (errno is also set to the error code)
762  *
763  * For blocking reads and polling, prefer **bpf_prog_stream_open**.
764  */
765 LIBBPF_API int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len,
766 				    struct bpf_prog_stream_read_opts *opts);
767 
768 struct bpf_prog_stream_open_opts {
769 	size_t sz;
770 	__u32 flags;
771 	size_t :0;
772 };
773 #define bpf_prog_stream_open_opts__last_field flags
774 
775 /**
776  * @brief **bpf_prog_stream_open** opens a file descriptor for a BPF stream of
777  * a given BPF program.
778  *
779  * @param prog_fd FD for the BPF program whose BPF stream is to be opened.
780  * @param stream_id ID of the BPF stream to be opened.
781  * @param opts optional options, can be NULL. BPF_F_STREAM_NONBLOCK requests a
782  * non-blocking descriptor.
783  *
784  * @return A new stream FD, on success; negative error code, otherwise (errno
785  * is also set to the error code)
786  */
787 LIBBPF_API int bpf_prog_stream_open(int prog_fd, __u32 stream_id,
788 				    const struct bpf_prog_stream_open_opts *opts);
789 
790 struct bpf_prog_assoc_struct_ops_opts {
791 	size_t sz;
792 	__u32 flags;
793 	size_t :0;
794 };
795 #define bpf_prog_assoc_struct_ops_opts__last_field flags
796 
797 /**
798  * @brief **bpf_prog_assoc_struct_ops** associates a BPF program with a
799  * struct_ops map.
800  *
801  * @param prog_fd FD for the BPF program
802  * @param map_fd FD for the struct_ops map to be associated with the BPF program
803  * @param opts optional options, can be NULL
804  *
805  * @return 0 on success; negative error code, otherwise (errno is also set to
806  * the error code)
807  */
808 LIBBPF_API int bpf_prog_assoc_struct_ops(int prog_fd, int map_fd,
809 					 struct bpf_prog_assoc_struct_ops_opts *opts);
810 
811 #ifdef __cplusplus
812 } /* extern "C" */
813 #endif
814 
815 #endif /* __LIBBPF_BPF_H */
816