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