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