xref: /linux/tools/lib/bpf/bpf.h (revision 55a9ed0e16baf4d025c160d46bc1e3fac0d4cdc4)
11bc38b8fSAlexei Starovoitov /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
26061a3d6SEric Leblond 
3e3ed2fefSWang Nan /*
4e3ed2fefSWang Nan  * common eBPF ELF operations.
5e3ed2fefSWang Nan  *
6e3ed2fefSWang Nan  * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
7e3ed2fefSWang Nan  * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
8e3ed2fefSWang Nan  * Copyright (C) 2015 Huawei Inc.
9203d1cacSWang Nan  *
10203d1cacSWang Nan  * This program is free software; you can redistribute it and/or
11203d1cacSWang Nan  * modify it under the terms of the GNU Lesser General Public
12203d1cacSWang Nan  * License as published by the Free Software Foundation;
13203d1cacSWang Nan  * version 2.1 of the License (not later!)
14203d1cacSWang Nan  *
15203d1cacSWang Nan  * This program is distributed in the hope that it will be useful,
16203d1cacSWang Nan  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17203d1cacSWang Nan  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18203d1cacSWang Nan  * GNU Lesser General Public License for more details.
19203d1cacSWang Nan  *
20203d1cacSWang Nan  * You should have received a copy of the GNU Lesser General Public
21203d1cacSWang Nan  * License along with this program; if not,  see <http://www.gnu.org/licenses>
22e3ed2fefSWang Nan  */
23eff81908SAndrey Ignatov #ifndef __LIBBPF_BPF_H
24eff81908SAndrey Ignatov #define __LIBBPF_BPF_H
25e3ed2fefSWang Nan 
26e3ed2fefSWang Nan #include <linux/bpf.h>
27878a4d32SBjörn Töpel #include <stdbool.h>
287a5980f9SMickaël Salaün #include <stddef.h>
2932e621e5SDaniel T. Lee #include <stdint.h>
30e3ed2fefSWang Nan 
31544402d4SAndrii Nakryiko #include "libbpf_common.h"
32be80e9cdSAndrii Nakryiko #include "libbpf_legacy.h"
33544402d4SAndrii Nakryiko 
348c4905b9SStanislav Fomichev #ifdef __cplusplus
358c4905b9SStanislav Fomichev extern "C" {
368c4905b9SStanislav Fomichev #endif
378c4905b9SStanislav Fomichev 
38e542f2c4SAndrii Nakryiko int libbpf_set_memlock_rlim(size_t memlock_bytes);
39e542f2c4SAndrii Nakryiko 
40992c4225SAndrii Nakryiko struct bpf_map_create_opts {
41992c4225SAndrii Nakryiko 	size_t sz; /* size of this struct for forward/backward compatibility */
42992c4225SAndrii Nakryiko 
43992c4225SAndrii Nakryiko 	__u32 btf_fd;
44992c4225SAndrii Nakryiko 	__u32 btf_key_type_id;
45992c4225SAndrii Nakryiko 	__u32 btf_value_type_id;
46992c4225SAndrii Nakryiko 	__u32 btf_vmlinux_value_type_id;
47992c4225SAndrii Nakryiko 
4874d98070SAndrii Nakryiko 	__u32 inner_map_fd;
4974d98070SAndrii Nakryiko 	__u32 map_flags;
50992c4225SAndrii Nakryiko 	__u64 map_extra;
51992c4225SAndrii Nakryiko 
5274d98070SAndrii Nakryiko 	__u32 numa_node;
5374d98070SAndrii Nakryiko 	__u32 map_ifindex;
54992c4225SAndrii Nakryiko };
55992c4225SAndrii Nakryiko #define bpf_map_create_opts__last_field map_ifindex
56992c4225SAndrii Nakryiko 
57992c4225SAndrii Nakryiko LIBBPF_API int bpf_map_create(enum bpf_map_type map_type,
58992c4225SAndrii Nakryiko 			      const char *map_name,
59992c4225SAndrii Nakryiko 			      __u32 key_size,
60992c4225SAndrii Nakryiko 			      __u32 value_size,
61992c4225SAndrii Nakryiko 			      __u32 max_entries,
62992c4225SAndrii Nakryiko 			      const struct bpf_map_create_opts *opts);
63992c4225SAndrii Nakryiko 
64d10ef2b8SAndrii Nakryiko struct bpf_prog_load_opts {
65d10ef2b8SAndrii Nakryiko 	size_t sz; /* size of this struct for forward/backward compatibility */
66d10ef2b8SAndrii Nakryiko 
67d10ef2b8SAndrii Nakryiko 	/* libbpf can retry BPF_PROG_LOAD command if bpf() syscall returns
68d10ef2b8SAndrii Nakryiko 	 * -EAGAIN. This field determines how many attempts libbpf has to
69d10ef2b8SAndrii Nakryiko 	 *  make. If not specified, libbpf will use default value of 5.
70d10ef2b8SAndrii Nakryiko 	 */
71d10ef2b8SAndrii Nakryiko 	int attempts;
72d10ef2b8SAndrii Nakryiko 
73d10ef2b8SAndrii Nakryiko 	enum bpf_attach_type expected_attach_type;
74d10ef2b8SAndrii Nakryiko 	__u32 prog_btf_fd;
75d10ef2b8SAndrii Nakryiko 	__u32 prog_flags;
76d10ef2b8SAndrii Nakryiko 	__u32 prog_ifindex;
77d10ef2b8SAndrii Nakryiko 	__u32 kern_version;
78d10ef2b8SAndrii Nakryiko 
79d10ef2b8SAndrii Nakryiko 	__u32 attach_btf_id;
80d10ef2b8SAndrii Nakryiko 	__u32 attach_prog_fd;
81d10ef2b8SAndrii Nakryiko 	__u32 attach_btf_obj_fd;
82d10ef2b8SAndrii Nakryiko 
83d10ef2b8SAndrii Nakryiko 	const int *fd_array;
84d10ef2b8SAndrii Nakryiko 
85d10ef2b8SAndrii Nakryiko 	/* .BTF.ext func info data */
86d10ef2b8SAndrii Nakryiko 	const void *func_info;
87d10ef2b8SAndrii Nakryiko 	__u32 func_info_cnt;
88d10ef2b8SAndrii Nakryiko 	__u32 func_info_rec_size;
89d10ef2b8SAndrii Nakryiko 
90d10ef2b8SAndrii Nakryiko 	/* .BTF.ext line info data */
91d10ef2b8SAndrii Nakryiko 	const void *line_info;
92d10ef2b8SAndrii Nakryiko 	__u32 line_info_cnt;
93d10ef2b8SAndrii Nakryiko 	__u32 line_info_rec_size;
94d10ef2b8SAndrii Nakryiko 
95d10ef2b8SAndrii Nakryiko 	/* verifier log options */
96d10ef2b8SAndrii Nakryiko 	__u32 log_level;
97d10ef2b8SAndrii Nakryiko 	__u32 log_size;
98d10ef2b8SAndrii Nakryiko 	char *log_buf;
99d10ef2b8SAndrii Nakryiko };
100d10ef2b8SAndrii Nakryiko #define bpf_prog_load_opts__last_field log_buf
101d10ef2b8SAndrii Nakryiko 
102d10ef2b8SAndrii Nakryiko LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type,
103d10ef2b8SAndrii Nakryiko 			     const char *prog_name, const char *license,
104d10ef2b8SAndrii Nakryiko 			     const struct bpf_insn *insns, size_t insn_cnt,
105d10ef2b8SAndrii Nakryiko 			     const struct bpf_prog_load_opts *opts);
106d7be143bSAndrey Ignatov 
107c034a177SJohn Fastabend /* Flags to direct loading requirements */
108c034a177SJohn Fastabend #define MAPS_RELAX_COMPAT	0x01
109c034a177SJohn Fastabend 
110e0e3ea88SAndrii Nakryiko /* Recommended log buffer size */
1114519efa6SMcCabe, Robert J #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */
112e0e3ea88SAndrii Nakryiko 
1130ed08d67SAndrii Nakryiko struct bpf_btf_load_opts {
1140ed08d67SAndrii Nakryiko 	size_t sz; /* size of this struct for forward/backward compatibility */
1150ed08d67SAndrii Nakryiko 
1160ed08d67SAndrii Nakryiko 	/* kernel log options */
1170ed08d67SAndrii Nakryiko 	char *log_buf;
1180ed08d67SAndrii Nakryiko 	__u32 log_level;
1190ed08d67SAndrii Nakryiko 	__u32 log_size;
1200ed08d67SAndrii Nakryiko };
1210ed08d67SAndrii Nakryiko #define bpf_btf_load_opts__last_field log_size
1220ed08d67SAndrii Nakryiko 
1230ed08d67SAndrii Nakryiko LIBBPF_API int bpf_btf_load(const void *btf_data, size_t btf_size,
1240ed08d67SAndrii Nakryiko 			    const struct bpf_btf_load_opts *opts);
1250ed08d67SAndrii Nakryiko 
126ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value,
12783d994d0SJoe Stringer 				   __u64 flags);
1289742da01SWang Nan 
129ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value);
130df5d22faSAlexei Starovoitov LIBBPF_API int bpf_map_lookup_elem_flags(int fd, const void *key, void *value,
131df5d22faSAlexei Starovoitov 					 __u64 flags);
13243b987d2SMauricio Vasquez B LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key,
13343b987d2SMauricio Vasquez B 					      void *value);
134d59b9f2dSDenis Salopek LIBBPF_API int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key,
135d59b9f2dSDenis Salopek 						    void *value, __u64 flags);
136ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_delete_elem(int fd, const void *key);
137737d0646SAndrii Nakryiko LIBBPF_API int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags);
138ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key);
139d859900cSDaniel Borkmann LIBBPF_API int bpf_map_freeze(int fd);
1402ab3d86eSYonghong Song 
1412ab3d86eSYonghong Song struct bpf_map_batch_opts {
1422ab3d86eSYonghong Song 	size_t sz; /* size of this struct for forward/backward compatibility */
1432ab3d86eSYonghong Song 	__u64 elem_flags;
1442ab3d86eSYonghong Song 	__u64 flags;
1452ab3d86eSYonghong Song };
1462ab3d86eSYonghong Song #define bpf_map_batch_opts__last_field flags
1472ab3d86eSYonghong Song 
148e59618f0SGrant Seltzer 
149e59618f0SGrant Seltzer /**
150e59618f0SGrant Seltzer  * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple
151e59618f0SGrant Seltzer  * elements in a BPF map.
152e59618f0SGrant Seltzer  *
153e59618f0SGrant Seltzer  * @param fd BPF map file descriptor
154e59618f0SGrant Seltzer  * @param keys pointer to an array of *count* keys
155e59618f0SGrant Seltzer  * @param count input and output parameter; on input **count** represents the
156e59618f0SGrant Seltzer  * number of  elements in the map to delete in batch;
157e59618f0SGrant Seltzer  * on output if a non-EFAULT error is returned, **count** represents the number of deleted
158e59618f0SGrant Seltzer  * elements if the output **count** value is not equal to the input **count** value
159e59618f0SGrant Seltzer  * If EFAULT is returned, **count** should not be trusted to be correct.
160e59618f0SGrant Seltzer  * @param opts options for configuring the way the batch deletion works
161e59618f0SGrant Seltzer  * @return 0, on success; negative error code, otherwise (errno is also set to
162e59618f0SGrant Seltzer  * the error code)
163e59618f0SGrant Seltzer  */
164e59618f0SGrant Seltzer LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys,
1652ab3d86eSYonghong Song 				    __u32 *count,
1662ab3d86eSYonghong Song 				    const struct bpf_map_batch_opts *opts);
167e59618f0SGrant Seltzer 
168e59618f0SGrant Seltzer /**
169e59618f0SGrant Seltzer  * @brief **bpf_map_lookup_batch()** allows for batch lookup of BPF map elements.
170e59618f0SGrant Seltzer  *
171e59618f0SGrant Seltzer  * The parameter *in_batch* is the address of the first element in the batch to read.
172e59618f0SGrant Seltzer  * *out_batch* is an output parameter that should be passed as *in_batch* to subsequent
173e59618f0SGrant Seltzer  * calls to **bpf_map_lookup_batch()**. NULL can be passed for *in_batch* to indicate
174e59618f0SGrant Seltzer  * that the batched lookup starts from the beginning of the map.
175e59618f0SGrant Seltzer  *
176e59618f0SGrant Seltzer  * The *keys* and *values* are output parameters which must point to memory large enough to
177e59618f0SGrant Seltzer  * hold *count* items based on the key and value size of the map *map_fd*. The *keys*
178e59618f0SGrant Seltzer  * buffer must be of *key_size* * *count*. The *values* buffer must be of
179e59618f0SGrant Seltzer  * *value_size* * *count*.
180e59618f0SGrant Seltzer  *
181e59618f0SGrant Seltzer  * @param fd BPF map file descriptor
182e59618f0SGrant Seltzer  * @param in_batch address of the first element in batch to read, can pass NULL to
183e59618f0SGrant Seltzer  * indicate that the batched lookup starts from the beginning of the map.
184e59618f0SGrant Seltzer  * @param out_batch output parameter that should be passed to next call as *in_batch*
185e59618f0SGrant Seltzer  * @param keys pointer to an array large enough for *count* keys
186e59618f0SGrant Seltzer  * @param values pointer to an array large enough for *count* values
187e59618f0SGrant Seltzer  * @param count input and output parameter; on input it's the number of elements
188e59618f0SGrant Seltzer  * in the map to read in batch; on output it's the number of elements that were
189e59618f0SGrant Seltzer  * successfully read.
190e59618f0SGrant Seltzer  * If a non-EFAULT error is returned, count will be set as the number of elements
191e59618f0SGrant Seltzer  * that were read before the error occurred.
192e59618f0SGrant Seltzer  * If EFAULT is returned, **count** should not be trusted to be correct.
193e59618f0SGrant Seltzer  * @param opts options for configuring the way the batch lookup works
194e59618f0SGrant Seltzer  * @return 0, on success; negative error code, otherwise (errno is also set to
195e59618f0SGrant Seltzer  * the error code)
196e59618f0SGrant Seltzer  */
1972ab3d86eSYonghong Song LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch,
1982ab3d86eSYonghong Song 				    void *keys, void *values, __u32 *count,
1992ab3d86eSYonghong Song 				    const struct bpf_map_batch_opts *opts);
200e59618f0SGrant Seltzer 
201e59618f0SGrant Seltzer /**
202e59618f0SGrant Seltzer  * @brief **bpf_map_lookup_and_delete_batch()** allows for batch lookup and deletion
203e59618f0SGrant Seltzer  * of BPF map elements where each element is deleted after being retrieved.
204e59618f0SGrant Seltzer  *
205e59618f0SGrant Seltzer  * @param fd BPF map file descriptor
206e59618f0SGrant Seltzer  * @param in_batch address of the first element in batch to read, can pass NULL to
207e59618f0SGrant Seltzer  * get address of the first element in *out_batch*
208e59618f0SGrant Seltzer  * @param out_batch output parameter that should be passed to next call as *in_batch*
209e59618f0SGrant Seltzer  * @param keys pointer to an array of *count* keys
210e59618f0SGrant Seltzer  * @param values pointer to an array large enough for *count* values
211e59618f0SGrant Seltzer  * @param count input and output parameter; on input it's the number of elements
212e59618f0SGrant Seltzer  * in the map to read and delete in batch; on output it represents the number of
213e59618f0SGrant Seltzer  * elements that were successfully read and deleted
214e59618f0SGrant Seltzer  * If a non-**EFAULT** error code is returned and if the output **count** value
215e59618f0SGrant Seltzer  * is not equal to the input **count** value, up to **count** elements may
216e59618f0SGrant Seltzer  * have been deleted.
217e59618f0SGrant Seltzer  * if **EFAULT** is returned up to *count* elements may have been deleted without
218e59618f0SGrant Seltzer  * being returned via the *keys* and *values* output parameters.
219e59618f0SGrant Seltzer  * @param opts options for configuring the way the batch lookup and delete works
220e59618f0SGrant Seltzer  * @return 0, on success; negative error code, otherwise (errno is also set to
221e59618f0SGrant Seltzer  * the error code)
222e59618f0SGrant Seltzer  */
2232ab3d86eSYonghong Song LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch,
2242ab3d86eSYonghong Song 					void *out_batch, void *keys,
2252ab3d86eSYonghong Song 					void *values, __u32 *count,
2262ab3d86eSYonghong Song 					const struct bpf_map_batch_opts *opts);
227e59618f0SGrant Seltzer 
228e59618f0SGrant Seltzer /**
229e59618f0SGrant Seltzer  * @brief **bpf_map_update_batch()** updates multiple elements in a map
230e59618f0SGrant Seltzer  * by specifying keys and their corresponding values.
231e59618f0SGrant Seltzer  *
232e59618f0SGrant Seltzer  * The *keys* and *values* parameters must point to memory large enough
233e59618f0SGrant Seltzer  * to hold *count* items based on the key and value size of the map.
234e59618f0SGrant Seltzer  *
235e59618f0SGrant Seltzer  * The *opts* parameter can be used to control how *bpf_map_update_batch()*
236e59618f0SGrant Seltzer  * should handle keys that either do or do not already exist in the map.
237e59618f0SGrant Seltzer  * In particular the *flags* parameter of *bpf_map_batch_opts* can be
238e59618f0SGrant Seltzer  * one of the following:
239e59618f0SGrant Seltzer  *
240e59618f0SGrant Seltzer  * Note that *count* is an input and output parameter, where on output it
241e59618f0SGrant Seltzer  * represents how many elements were successfully updated. Also note that if
242e59618f0SGrant Seltzer  * **EFAULT** then *count* should not be trusted to be correct.
243e59618f0SGrant Seltzer  *
244e59618f0SGrant Seltzer  * **BPF_ANY**
245e59618f0SGrant Seltzer  *    Create new elements or update existing.
246e59618f0SGrant Seltzer  *
247e59618f0SGrant Seltzer  * **BPF_NOEXIST**
248e59618f0SGrant Seltzer  *    Create new elements only if they do not exist.
249e59618f0SGrant Seltzer  *
250e59618f0SGrant Seltzer  * **BPF_EXIST**
251e59618f0SGrant Seltzer  *    Update existing elements.
252e59618f0SGrant Seltzer  *
253e59618f0SGrant Seltzer  * **BPF_F_LOCK**
254e59618f0SGrant Seltzer  *    Update spin_lock-ed map elements. This must be
255e59618f0SGrant Seltzer  *    specified if the map value contains a spinlock.
256e59618f0SGrant Seltzer  *
257e59618f0SGrant Seltzer  * @param fd BPF map file descriptor
258e59618f0SGrant Seltzer  * @param keys pointer to an array of *count* keys
259e59618f0SGrant Seltzer  * @param values pointer to an array of *count* values
260e59618f0SGrant Seltzer  * @param count input and output parameter; on input it's the number of elements
261e59618f0SGrant Seltzer  * in the map to update in batch; on output if a non-EFAULT error is returned,
262e59618f0SGrant Seltzer  * **count** represents the number of updated elements if the output **count**
263e59618f0SGrant Seltzer  * value is not equal to the input **count** value.
264e59618f0SGrant Seltzer  * If EFAULT is returned, **count** should not be trusted to be correct.
265e59618f0SGrant Seltzer  * @param opts options for configuring the way the batch update works
266e59618f0SGrant Seltzer  * @return 0, on success; negative error code, otherwise (errno is also set to
267e59618f0SGrant Seltzer  * the error code)
268e59618f0SGrant Seltzer  */
269e59618f0SGrant Seltzer LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values,
2702ab3d86eSYonghong Song 				    __u32 *count,
2712ab3d86eSYonghong Song 				    const struct bpf_map_batch_opts *opts);
2722ab3d86eSYonghong Song 
273395fc4faSJoe Burton struct bpf_obj_get_opts {
274395fc4faSJoe Burton 	size_t sz; /* size of this struct for forward/backward compatibility */
275395fc4faSJoe Burton 
276395fc4faSJoe Burton 	__u32 file_flags;
277395fc4faSJoe Burton 
278395fc4faSJoe Burton 	size_t :0;
279395fc4faSJoe Burton };
280395fc4faSJoe Burton #define bpf_obj_get_opts__last_field file_flags
281395fc4faSJoe Burton 
282ab9e0848SAndrey Ignatov LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
283ab9e0848SAndrey Ignatov LIBBPF_API int bpf_obj_get(const char *pathname);
284395fc4faSJoe Burton LIBBPF_API int bpf_obj_get_opts(const char *pathname,
285395fc4faSJoe Burton 				const struct bpf_obj_get_opts *opts);
286cdbee383SAndrey Ignatov 
287cdbee383SAndrey Ignatov struct bpf_prog_attach_opts {
288cdbee383SAndrey Ignatov 	size_t sz; /* size of this struct for forward/backward compatibility */
289cdbee383SAndrey Ignatov 	unsigned int flags;
290cdbee383SAndrey Ignatov 	int replace_prog_fd;
291cdbee383SAndrey Ignatov };
292cdbee383SAndrey Ignatov #define bpf_prog_attach_opts__last_field replace_prog_fd
293cdbee383SAndrey Ignatov 
294ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
295ab9e0848SAndrey Ignatov 			       enum bpf_attach_type type, unsigned int flags);
296d6c9c24eSChristy Lee LIBBPF_API int bpf_prog_attach_opts(int prog_fd, int attachable_fd,
297d6c9c24eSChristy Lee 				     enum bpf_attach_type type,
298d6c9c24eSChristy Lee 				     const struct bpf_prog_attach_opts *opts);
299ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
300ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
301ab9e0848SAndrey Ignatov 				enum bpf_attach_type type);
30264a97591SLorenz Bauer 
30374fc097dSYonghong Song union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */
304cc4f864bSAndrii Nakryiko struct bpf_link_create_opts {
305cc4f864bSAndrii Nakryiko 	size_t sz; /* size of this struct for forward/backward compatibility */
306cd31039aSYonghong Song 	__u32 flags;
30774fc097dSYonghong Song 	union bpf_iter_link_info *iter_info;
30874fc097dSYonghong Song 	__u32 iter_info_len;
309a5359091SToke Høiland-Jørgensen 	__u32 target_btf_id;
3103ec84f4bSAndrii Nakryiko 	union {
3113ec84f4bSAndrii Nakryiko 		struct {
3123ec84f4bSAndrii Nakryiko 			__u64 bpf_cookie;
3133ec84f4bSAndrii Nakryiko 		} perf_event;
3145117c26eSJiri Olsa 		struct {
3155117c26eSJiri Olsa 			__u32 flags;
3165117c26eSJiri Olsa 			__u32 cnt;
3175117c26eSJiri Olsa 			const char **syms;
3185117c26eSJiri Olsa 			const unsigned long *addrs;
3195117c26eSJiri Olsa 			const __u64 *cookies;
3205117c26eSJiri Olsa 		} kprobe_multi;
321129b9c5eSKui-Feng Lee 		struct {
322129b9c5eSKui-Feng Lee 			__u64 cookie;
323129b9c5eSKui-Feng Lee 		} tracing;
324cc4f864bSAndrii Nakryiko 	};
3253ec84f4bSAndrii Nakryiko 	size_t :0;
3263ec84f4bSAndrii Nakryiko };
3275117c26eSJiri Olsa #define bpf_link_create_opts__last_field kprobe_multi.cookies
328cc4f864bSAndrii Nakryiko 
329cc4f864bSAndrii Nakryiko LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
330cc4f864bSAndrii Nakryiko 			       enum bpf_attach_type attach_type,
331cc4f864bSAndrii Nakryiko 			       const struct bpf_link_create_opts *opts);
332cc4f864bSAndrii Nakryiko 
3332e49527eSAndrii Nakryiko LIBBPF_API int bpf_link_detach(int link_fd);
3342e49527eSAndrii Nakryiko 
335cc4f864bSAndrii Nakryiko struct bpf_link_update_opts {
336cc4f864bSAndrii Nakryiko 	size_t sz; /* size of this struct for forward/backward compatibility */
337cc4f864bSAndrii Nakryiko 	__u32 flags;	   /* extra flags */
338cc4f864bSAndrii Nakryiko 	__u32 old_prog_fd; /* expected old program FD */
339cc4f864bSAndrii Nakryiko };
340cc4f864bSAndrii Nakryiko #define bpf_link_update_opts__last_field old_prog_fd
341cc4f864bSAndrii Nakryiko 
342cc4f864bSAndrii Nakryiko LIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd,
343cc4f864bSAndrii Nakryiko 			       const struct bpf_link_update_opts *opts);
344cc4f864bSAndrii Nakryiko 
345c09add2fSYonghong Song LIBBPF_API int bpf_iter_create(int link_fd);
346c09add2fSYonghong Song 
34764a97591SLorenz Bauer struct bpf_prog_test_run_attr {
34864a97591SLorenz Bauer 	int prog_fd;
34964a97591SLorenz Bauer 	int repeat;
35064a97591SLorenz Bauer 	const void *data_in;
35164a97591SLorenz Bauer 	__u32 data_size_in;
35264a97591SLorenz Bauer 	void *data_out;      /* optional */
35364a97591SLorenz Bauer 	__u32 data_size_out; /* in: max length of data_out
35464a97591SLorenz Bauer 			      * out: length of data_out */
35564a97591SLorenz Bauer 	__u32 retval;        /* out: return code of the BPF program */
35664a97591SLorenz Bauer 	__u32 duration;      /* out: average per repetition in ns */
3575e903c65SStanislav Fomichev 	const void *ctx_in; /* optional */
3585e903c65SStanislav Fomichev 	__u32 ctx_size_in;
3595e903c65SStanislav Fomichev 	void *ctx_out;      /* optional */
3605e903c65SStanislav Fomichev 	__u32 ctx_size_out; /* in: max length of ctx_out
3615e903c65SStanislav Fomichev 			     * out: length of cxt_out */
36264a97591SLorenz Bauer };
36364a97591SLorenz Bauer 
364ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id);
365ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
36609d7c2e3SQuentin Monnet LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id);
3670dbc8668SAndrii Nakryiko LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id);
368243e3005SRoberto Sassu 
369243e3005SRoberto Sassu struct bpf_get_fd_by_id_opts {
370243e3005SRoberto Sassu 	size_t sz; /* size of this struct for forward/backward compatibility */
371243e3005SRoberto Sassu 	__u32 open_flags; /* permissions requested for the operation on fd */
372243e3005SRoberto Sassu 	size_t :0;
373243e3005SRoberto Sassu };
374243e3005SRoberto Sassu #define bpf_get_fd_by_id_opts__last_field open_flags
375243e3005SRoberto Sassu 
376ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
3778f13f168SRoberto Sassu LIBBPF_API int bpf_prog_get_fd_by_id_opts(__u32 id,
3788f13f168SRoberto Sassu 				const struct bpf_get_fd_by_id_opts *opts);
379ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_get_fd_by_id(__u32 id);
380243e3005SRoberto Sassu LIBBPF_API int bpf_map_get_fd_by_id_opts(__u32 id,
381243e3005SRoberto Sassu 				const struct bpf_get_fd_by_id_opts *opts);
382ab9e0848SAndrey Ignatov LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id);
3832ce7cbf2SRoberto Sassu LIBBPF_API int bpf_btf_get_fd_by_id_opts(__u32 id,
3842ce7cbf2SRoberto Sassu 				const struct bpf_get_fd_by_id_opts *opts);
3850dbc8668SAndrii Nakryiko LIBBPF_API int bpf_link_get_fd_by_id(__u32 id);
38697c8f9ddSRoberto Sassu LIBBPF_API int bpf_link_get_fd_by_id_opts(__u32 id,
38797c8f9ddSRoberto Sassu 				const struct bpf_get_fd_by_id_opts *opts);
3880dbc8668SAndrii Nakryiko LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len);
389*55a9ed0eSIlya Leoshkevich /* Type-safe variants of bpf_obj_get_info_by_fd(). The callers still needs to
390*55a9ed0eSIlya Leoshkevich  * pass info_len, which should normally be
391*55a9ed0eSIlya Leoshkevich  * sizeof(struct bpf_{prog,map,btf,link}_info), in order to be compatible with
392*55a9ed0eSIlya Leoshkevich  * different libbpf and kernel versions.
393*55a9ed0eSIlya Leoshkevich  */
394*55a9ed0eSIlya Leoshkevich LIBBPF_API int bpf_prog_get_info_by_fd(int prog_fd, struct bpf_prog_info *info, __u32 *info_len);
395*55a9ed0eSIlya Leoshkevich LIBBPF_API int bpf_map_get_info_by_fd(int map_fd, struct bpf_map_info *info, __u32 *info_len);
396*55a9ed0eSIlya Leoshkevich LIBBPF_API int bpf_btf_get_info_by_fd(int btf_fd, struct bpf_btf_info *info, __u32 *info_len);
397*55a9ed0eSIlya Leoshkevich LIBBPF_API int bpf_link_get_info_by_fd(int link_fd, struct bpf_link_info *info, __u32 *info_len);
398a4b2f3cfSStanislav Fomichev 
399a4b2f3cfSStanislav Fomichev struct bpf_prog_query_opts {
400a4b2f3cfSStanislav Fomichev 	size_t sz; /* size of this struct for forward/backward compatibility */
401a4b2f3cfSStanislav Fomichev 	__u32 query_flags;
402a4b2f3cfSStanislav Fomichev 	__u32 attach_flags; /* output argument */
403a4b2f3cfSStanislav Fomichev 	__u32 *prog_ids;
404a4b2f3cfSStanislav Fomichev 	__u32 prog_cnt; /* input+output argument */
405a4b2f3cfSStanislav Fomichev 	__u32 *prog_attach_flags;
406a4b2f3cfSStanislav Fomichev };
407a4b2f3cfSStanislav Fomichev #define bpf_prog_query_opts__last_field prog_attach_flags
408a4b2f3cfSStanislav Fomichev 
409a4b2f3cfSStanislav Fomichev LIBBPF_API int bpf_prog_query_opts(int target_fd,
410a4b2f3cfSStanislav Fomichev 				   enum bpf_attach_type type,
411a4b2f3cfSStanislav Fomichev 				   struct bpf_prog_query_opts *opts);
412ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type,
413ab9e0848SAndrey Ignatov 			      __u32 query_flags, __u32 *attach_flags,
414ab9e0848SAndrey Ignatov 			      __u32 *prog_ids, __u32 *prog_cnt);
415a4b2f3cfSStanislav Fomichev 
416ab9e0848SAndrey Ignatov LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd);
417ab9e0848SAndrey Ignatov LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf,
418ab9e0848SAndrey Ignatov 				 __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
419ab9e0848SAndrey Ignatov 				 __u64 *probe_offset, __u64 *probe_addr);
4204e15507fSAndrii Nakryiko 
421b4269341SAndrii Nakryiko #ifdef __cplusplus
422b4269341SAndrii Nakryiko /* forward-declaring enums in C++ isn't compatible with pure C enums, so
423b4269341SAndrii Nakryiko  * instead define bpf_enable_stats() as accepting int as an input
424b4269341SAndrii Nakryiko  */
425b4269341SAndrii Nakryiko LIBBPF_API int bpf_enable_stats(int type);
426b4269341SAndrii Nakryiko #else
4274e15507fSAndrii Nakryiko enum bpf_stats_type; /* defined in up-to-date linux/bpf.h */
4280bee1067SSong Liu LIBBPF_API int bpf_enable_stats(enum bpf_stats_type type);
429b4269341SAndrii Nakryiko #endif
4308c4905b9SStanislav Fomichev 
4315d23328dSYiFei Zhu struct bpf_prog_bind_opts {
4325d23328dSYiFei Zhu 	size_t sz; /* size of this struct for forward/backward compatibility */
4335d23328dSYiFei Zhu 	__u32 flags;
4345d23328dSYiFei Zhu };
4355d23328dSYiFei Zhu #define bpf_prog_bind_opts__last_field flags
4365d23328dSYiFei Zhu 
4375d23328dSYiFei Zhu LIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd,
4385d23328dSYiFei Zhu 				 const struct bpf_prog_bind_opts *opts);
43988f7fe72SSong Liu 
44088f7fe72SSong Liu struct bpf_test_run_opts {
44188f7fe72SSong Liu 	size_t sz; /* size of this struct for forward/backward compatibility */
44288f7fe72SSong Liu 	const void *data_in; /* optional */
44388f7fe72SSong Liu 	void *data_out;      /* optional */
44488f7fe72SSong Liu 	__u32 data_size_in;
44588f7fe72SSong Liu 	__u32 data_size_out; /* in: max length of data_out
44688f7fe72SSong Liu 			      * out: length of data_out
44788f7fe72SSong Liu 			      */
44888f7fe72SSong Liu 	const void *ctx_in; /* optional */
44988f7fe72SSong Liu 	void *ctx_out;      /* optional */
45088f7fe72SSong Liu 	__u32 ctx_size_in;
45188f7fe72SSong Liu 	__u32 ctx_size_out; /* in: max length of ctx_out
45288f7fe72SSong Liu 			     * out: length of cxt_out
45388f7fe72SSong Liu 			     */
45488f7fe72SSong Liu 	__u32 retval;        /* out: return code of the BPF program */
45588f7fe72SSong Liu 	int repeat;
45688f7fe72SSong Liu 	__u32 duration;      /* out: average per repetition in ns */
45788f7fe72SSong Liu 	__u32 flags;
45888f7fe72SSong Liu 	__u32 cpu;
45924592ad1SToke Høiland-Jørgensen 	__u32 batch_size;
46088f7fe72SSong Liu };
46124592ad1SToke Høiland-Jørgensen #define bpf_test_run_opts__last_field batch_size
46288f7fe72SSong Liu 
46388f7fe72SSong Liu LIBBPF_API int bpf_prog_test_run_opts(int prog_fd,
46488f7fe72SSong Liu 				      struct bpf_test_run_opts *opts);
46588f7fe72SSong Liu 
4668c4905b9SStanislav Fomichev #ifdef __cplusplus
4678c4905b9SStanislav Fomichev } /* extern "C" */
4688c4905b9SStanislav Fomichev #endif
4698c4905b9SStanislav Fomichev 
470eff81908SAndrey Ignatov #endif /* __LIBBPF_BPF_H */
471