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