xref: /linux/tools/lib/bpf/bpf.h (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 
3 /*
4  * common eBPF 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 };
55 #define bpf_map_create_opts__last_field map_ifindex
56 
57 LIBBPF_API int bpf_map_create(enum bpf_map_type map_type,
58 			      const char *map_name,
59 			      __u32 key_size,
60 			      __u32 value_size,
61 			      __u32 max_entries,
62 			      const struct bpf_map_create_opts *opts);
63 
64 struct bpf_prog_load_opts {
65 	size_t sz; /* size of this struct for forward/backward compatibility */
66 
67 	/* libbpf can retry BPF_PROG_LOAD command if bpf() syscall returns
68 	 * -EAGAIN. This field determines how many attempts libbpf has to
69 	 *  make. If not specified, libbpf will use default value of 5.
70 	 */
71 	int attempts;
72 
73 	enum bpf_attach_type expected_attach_type;
74 	__u32 prog_btf_fd;
75 	__u32 prog_flags;
76 	__u32 prog_ifindex;
77 	__u32 kern_version;
78 
79 	__u32 attach_btf_id;
80 	__u32 attach_prog_fd;
81 	__u32 attach_btf_obj_fd;
82 
83 	const int *fd_array;
84 
85 	/* .BTF.ext func info data */
86 	const void *func_info;
87 	__u32 func_info_cnt;
88 	__u32 func_info_rec_size;
89 
90 	/* .BTF.ext line info data */
91 	const void *line_info;
92 	__u32 line_info_cnt;
93 	__u32 line_info_rec_size;
94 
95 	/* verifier log options */
96 	__u32 log_level;
97 	__u32 log_size;
98 	char *log_buf;
99 };
100 #define bpf_prog_load_opts__last_field log_buf
101 
102 LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type,
103 			     const char *prog_name, const char *license,
104 			     const struct bpf_insn *insns, size_t insn_cnt,
105 			     const struct bpf_prog_load_opts *opts);
106 /* this "specialization" should go away in libbpf 1.0 */
107 LIBBPF_API int bpf_prog_load_v0_6_0(enum bpf_prog_type prog_type,
108 				    const char *prog_name, const char *license,
109 				    const struct bpf_insn *insns, size_t insn_cnt,
110 				    const struct bpf_prog_load_opts *opts);
111 
112 /* This is an elaborate way to not conflict with deprecated bpf_prog_load()
113  * API, defined in libbpf.h. Once we hit libbpf 1.0, all this will be gone.
114  * With this approach, if someone is calling bpf_prog_load() with
115  * 4 arguments, they will use the deprecated API, which keeps backwards
116  * compatibility (both source code and binary). If bpf_prog_load() is called
117  * with 6 arguments, though, it gets redirected to __bpf_prog_load.
118  * So looking forward to libbpf 1.0 when this hack will be gone and
119  * __bpf_prog_load() will be called just bpf_prog_load().
120  */
121 #ifndef bpf_prog_load
122 #define bpf_prog_load(...) ___libbpf_overload(___bpf_prog_load, __VA_ARGS__)
123 #define ___bpf_prog_load4(file, type, pobj, prog_fd) \
124 	bpf_prog_load_deprecated(file, type, pobj, prog_fd)
125 #define ___bpf_prog_load6(prog_type, prog_name, license, insns, insn_cnt, opts) \
126 	bpf_prog_load(prog_type, prog_name, license, insns, insn_cnt, opts)
127 #endif /* bpf_prog_load */
128 
129 struct bpf_load_program_attr {
130 	enum bpf_prog_type prog_type;
131 	enum bpf_attach_type expected_attach_type;
132 	const char *name;
133 	const struct bpf_insn *insns;
134 	size_t insns_cnt;
135 	const char *license;
136 	union {
137 		__u32 kern_version;
138 		__u32 attach_prog_fd;
139 	};
140 	union {
141 		__u32 prog_ifindex;
142 		__u32 attach_btf_id;
143 	};
144 	__u32 prog_btf_fd;
145 	__u32 func_info_rec_size;
146 	const void *func_info;
147 	__u32 func_info_cnt;
148 	__u32 line_info_rec_size;
149 	const void *line_info;
150 	__u32 line_info_cnt;
151 	__u32 log_level;
152 	__u32 prog_flags;
153 };
154 
155 /* Flags to direct loading requirements */
156 #define MAPS_RELAX_COMPAT	0x01
157 
158 /* Recommended log buffer size */
159 #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */
160 
161 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_load() instead")
162 LIBBPF_API int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
163 				      char *log_buf, size_t log_buf_sz);
164 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_load() instead")
165 LIBBPF_API int bpf_load_program(enum bpf_prog_type type,
166 				const struct bpf_insn *insns, size_t insns_cnt,
167 				const char *license, __u32 kern_version,
168 				char *log_buf, size_t log_buf_sz);
169 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_load() instead")
170 LIBBPF_API int bpf_verify_program(enum bpf_prog_type type,
171 				  const struct bpf_insn *insns,
172 				  size_t insns_cnt, __u32 prog_flags,
173 				  const char *license, __u32 kern_version,
174 				  char *log_buf, size_t log_buf_sz,
175 				  int log_level);
176 
177 struct bpf_btf_load_opts {
178 	size_t sz; /* size of this struct for forward/backward compatibility */
179 
180 	/* kernel log options */
181 	char *log_buf;
182 	__u32 log_level;
183 	__u32 log_size;
184 };
185 #define bpf_btf_load_opts__last_field log_size
186 
187 LIBBPF_API int bpf_btf_load(const void *btf_data, size_t btf_size,
188 			    const struct bpf_btf_load_opts *opts);
189 
190 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_btf_load() instead")
191 LIBBPF_API int bpf_load_btf(const void *btf, __u32 btf_size, char *log_buf,
192 			    __u32 log_buf_size, bool do_log);
193 
194 LIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value,
195 				   __u64 flags);
196 
197 LIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value);
198 LIBBPF_API int bpf_map_lookup_elem_flags(int fd, const void *key, void *value,
199 					 __u64 flags);
200 LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key,
201 					      void *value);
202 LIBBPF_API int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key,
203 						    void *value, __u64 flags);
204 LIBBPF_API int bpf_map_delete_elem(int fd, const void *key);
205 LIBBPF_API int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags);
206 LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key);
207 LIBBPF_API int bpf_map_freeze(int fd);
208 
209 struct bpf_map_batch_opts {
210 	size_t sz; /* size of this struct for forward/backward compatibility */
211 	__u64 elem_flags;
212 	__u64 flags;
213 };
214 #define bpf_map_batch_opts__last_field flags
215 
216 
217 /**
218  * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple
219  * elements in a BPF map.
220  *
221  * @param fd BPF map file descriptor
222  * @param keys pointer to an array of *count* keys
223  * @param count input and output parameter; on input **count** represents the
224  * number of  elements in the map to delete in batch;
225  * on output if a non-EFAULT error is returned, **count** represents the number of deleted
226  * elements if the output **count** value is not equal to the input **count** value
227  * If EFAULT is returned, **count** should not be trusted to be correct.
228  * @param opts options for configuring the way the batch deletion works
229  * @return 0, on success; negative error code, otherwise (errno is also set to
230  * the error code)
231  */
232 LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys,
233 				    __u32 *count,
234 				    const struct bpf_map_batch_opts *opts);
235 
236 /**
237  * @brief **bpf_map_lookup_batch()** allows for batch lookup of BPF map elements.
238  *
239  * The parameter *in_batch* is the address of the first element in the batch to read.
240  * *out_batch* is an output parameter that should be passed as *in_batch* to subsequent
241  * calls to **bpf_map_lookup_batch()**. NULL can be passed for *in_batch* to indicate
242  * that the batched lookup starts from the beginning of the map.
243  *
244  * The *keys* and *values* are output parameters which must point to memory large enough to
245  * hold *count* items based on the key and value size of the map *map_fd*. The *keys*
246  * buffer must be of *key_size* * *count*. The *values* buffer must be of
247  * *value_size* * *count*.
248  *
249  * @param fd BPF map file descriptor
250  * @param in_batch address of the first element in batch to read, can pass NULL to
251  * indicate that the batched lookup starts from the beginning of the map.
252  * @param out_batch output parameter that should be passed to next call as *in_batch*
253  * @param keys pointer to an array large enough for *count* keys
254  * @param values pointer to an array large enough for *count* values
255  * @param count input and output parameter; on input it's the number of elements
256  * in the map to read in batch; on output it's the number of elements that were
257  * successfully read.
258  * If a non-EFAULT error is returned, count will be set as the number of elements
259  * that were read before the error occurred.
260  * If EFAULT is returned, **count** should not be trusted to be correct.
261  * @param opts options for configuring the way the batch lookup works
262  * @return 0, on success; negative error code, otherwise (errno is also set to
263  * the error code)
264  */
265 LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch,
266 				    void *keys, void *values, __u32 *count,
267 				    const struct bpf_map_batch_opts *opts);
268 
269 /**
270  * @brief **bpf_map_lookup_and_delete_batch()** allows for batch lookup and deletion
271  * of BPF map elements where each element is deleted after being retrieved.
272  *
273  * @param fd BPF map file descriptor
274  * @param in_batch address of the first element in batch to read, can pass NULL to
275  * get address of the first element in *out_batch*
276  * @param out_batch output parameter that should be passed to next call as *in_batch*
277  * @param keys pointer to an array of *count* keys
278  * @param values pointer to an array large enough for *count* values
279  * @param count input and output parameter; on input it's the number of elements
280  * in the map to read and delete in batch; on output it represents the number of
281  * elements that were successfully read and deleted
282  * If a non-**EFAULT** error code is returned and if the output **count** value
283  * is not equal to the input **count** value, up to **count** elements may
284  * have been deleted.
285  * if **EFAULT** is returned up to *count* elements may have been deleted without
286  * being returned via the *keys* and *values* output parameters.
287  * @param opts options for configuring the way the batch lookup and delete 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_lookup_and_delete_batch(int fd, void *in_batch,
292 					void *out_batch, void *keys,
293 					void *values, __u32 *count,
294 					const struct bpf_map_batch_opts *opts);
295 
296 /**
297  * @brief **bpf_map_update_batch()** updates multiple elements in a map
298  * by specifying keys and their corresponding values.
299  *
300  * The *keys* and *values* parameters must point to memory large enough
301  * to hold *count* items based on the key and value size of the map.
302  *
303  * The *opts* parameter can be used to control how *bpf_map_update_batch()*
304  * should handle keys that either do or do not already exist in the map.
305  * In particular the *flags* parameter of *bpf_map_batch_opts* can be
306  * one of the following:
307  *
308  * Note that *count* is an input and output parameter, where on output it
309  * represents how many elements were successfully updated. Also note that if
310  * **EFAULT** then *count* should not be trusted to be correct.
311  *
312  * **BPF_ANY**
313  *    Create new elements or update existing.
314  *
315  * **BPF_NOEXIST**
316  *    Create new elements only if they do not exist.
317  *
318  * **BPF_EXIST**
319  *    Update existing elements.
320  *
321  * **BPF_F_LOCK**
322  *    Update spin_lock-ed map elements. This must be
323  *    specified if the map value contains a spinlock.
324  *
325  * @param fd BPF map file descriptor
326  * @param keys pointer to an array of *count* keys
327  * @param values pointer to an array of *count* values
328  * @param count input and output parameter; on input it's the number of elements
329  * in the map to update in batch; on output if a non-EFAULT error is returned,
330  * **count** represents the number of updated elements if the output **count**
331  * value is not equal to the input **count** value.
332  * If EFAULT is returned, **count** should not be trusted to be correct.
333  * @param opts options for configuring the way the batch update works
334  * @return 0, on success; negative error code, otherwise (errno is also set to
335  * the error code)
336  */
337 LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values,
338 				    __u32 *count,
339 				    const struct bpf_map_batch_opts *opts);
340 
341 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
342 LIBBPF_API int bpf_obj_get(const char *pathname);
343 
344 struct bpf_prog_attach_opts {
345 	size_t sz; /* size of this struct for forward/backward compatibility */
346 	unsigned int flags;
347 	int replace_prog_fd;
348 };
349 #define bpf_prog_attach_opts__last_field replace_prog_fd
350 
351 LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
352 			       enum bpf_attach_type type, unsigned int flags);
353 LIBBPF_API int bpf_prog_attach_opts(int prog_fd, int attachable_fd,
354 				     enum bpf_attach_type type,
355 				     const struct bpf_prog_attach_opts *opts);
356 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_prog_attach_opts() instead")
357 LIBBPF_API int bpf_prog_attach_xattr(int prog_fd, int attachable_fd,
358 				     enum bpf_attach_type type,
359 				     const struct bpf_prog_attach_opts *opts);
360 LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
361 LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
362 				enum bpf_attach_type type);
363 
364 union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */
365 struct bpf_link_create_opts {
366 	size_t sz; /* size of this struct for forward/backward compatibility */
367 	__u32 flags;
368 	union bpf_iter_link_info *iter_info;
369 	__u32 iter_info_len;
370 	__u32 target_btf_id;
371 	union {
372 		struct {
373 			__u64 bpf_cookie;
374 		} perf_event;
375 		struct {
376 			__u32 flags;
377 			__u32 cnt;
378 			const char **syms;
379 			const unsigned long *addrs;
380 			const __u64 *cookies;
381 		} kprobe_multi;
382 		struct {
383 			__u64 cookie;
384 		} tracing;
385 	};
386 	size_t :0;
387 };
388 #define bpf_link_create_opts__last_field kprobe_multi.cookies
389 
390 LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
391 			       enum bpf_attach_type attach_type,
392 			       const struct bpf_link_create_opts *opts);
393 
394 LIBBPF_API int bpf_link_detach(int link_fd);
395 
396 struct bpf_link_update_opts {
397 	size_t sz; /* size of this struct for forward/backward compatibility */
398 	__u32 flags;	   /* extra flags */
399 	__u32 old_prog_fd; /* expected old program FD */
400 };
401 #define bpf_link_update_opts__last_field old_prog_fd
402 
403 LIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd,
404 			       const struct bpf_link_update_opts *opts);
405 
406 LIBBPF_API int bpf_iter_create(int link_fd);
407 
408 struct bpf_prog_test_run_attr {
409 	int prog_fd;
410 	int repeat;
411 	const void *data_in;
412 	__u32 data_size_in;
413 	void *data_out;      /* optional */
414 	__u32 data_size_out; /* in: max length of data_out
415 			      * out: length of data_out */
416 	__u32 retval;        /* out: return code of the BPF program */
417 	__u32 duration;      /* out: average per repetition in ns */
418 	const void *ctx_in; /* optional */
419 	__u32 ctx_size_in;
420 	void *ctx_out;      /* optional */
421 	__u32 ctx_size_out; /* in: max length of ctx_out
422 			     * out: length of cxt_out */
423 };
424 
425 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_test_run_opts() instead")
426 LIBBPF_API int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr);
427 
428 /*
429  * bpf_prog_test_run does not check that data_out is large enough. Consider
430  * using bpf_prog_test_run_opts instead.
431  */
432 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_test_run_opts() instead")
433 LIBBPF_API int bpf_prog_test_run(int prog_fd, int repeat, void *data,
434 				 __u32 size, void *data_out, __u32 *size_out,
435 				 __u32 *retval, __u32 *duration);
436 LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id);
437 LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
438 LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id);
439 LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id);
440 LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
441 LIBBPF_API int bpf_map_get_fd_by_id(__u32 id);
442 LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id);
443 LIBBPF_API int bpf_link_get_fd_by_id(__u32 id);
444 LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len);
445 LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type,
446 			      __u32 query_flags, __u32 *attach_flags,
447 			      __u32 *prog_ids, __u32 *prog_cnt);
448 LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd);
449 LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf,
450 				 __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
451 				 __u64 *probe_offset, __u64 *probe_addr);
452 
453 enum bpf_stats_type; /* defined in up-to-date linux/bpf.h */
454 LIBBPF_API int bpf_enable_stats(enum bpf_stats_type type);
455 
456 struct bpf_prog_bind_opts {
457 	size_t sz; /* size of this struct for forward/backward compatibility */
458 	__u32 flags;
459 };
460 #define bpf_prog_bind_opts__last_field flags
461 
462 LIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd,
463 				 const struct bpf_prog_bind_opts *opts);
464 
465 struct bpf_test_run_opts {
466 	size_t sz; /* size of this struct for forward/backward compatibility */
467 	const void *data_in; /* optional */
468 	void *data_out;      /* optional */
469 	__u32 data_size_in;
470 	__u32 data_size_out; /* in: max length of data_out
471 			      * out: length of data_out
472 			      */
473 	const void *ctx_in; /* optional */
474 	void *ctx_out;      /* optional */
475 	__u32 ctx_size_in;
476 	__u32 ctx_size_out; /* in: max length of ctx_out
477 			     * out: length of cxt_out
478 			     */
479 	__u32 retval;        /* out: return code of the BPF program */
480 	int repeat;
481 	__u32 duration;      /* out: average per repetition in ns */
482 	__u32 flags;
483 	__u32 cpu;
484 	__u32 batch_size;
485 };
486 #define bpf_test_run_opts__last_field batch_size
487 
488 LIBBPF_API int bpf_prog_test_run_opts(int prog_fd,
489 				      struct bpf_test_run_opts *opts);
490 
491 #ifdef __cplusplus
492 } /* extern "C" */
493 #endif
494 
495 #endif /* __LIBBPF_BPF_H */
496