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