1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
4 * Common BPF ELF operations.
8 * Copyright (C) 2015 Huawei Inc.
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!)
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.
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>
23 #ifndef __LIBBPF_BPF_H
24 #define __LIBBPF_BPF_H
26 #include <linux/bpf.h>
31 #include "libbpf_common.h"
32 #include "libbpf_legacy.h"
38 int libbpf_set_memlock_rlim(size_t memlock_bytes);
40 struct bpf_map_create_opts {
41 size_t sz; /* size of this struct for forward/backward compatibility */
44 __u32 btf_key_type_id;
45 __u32 btf_value_type_id;
46 __u32 btf_vmlinux_value_type_id;
55 #define bpf_map_create_opts__last_field map_ifindex
57 LIBBPF_API int bpf_map_create(enum bpf_map_type map_type,
62 const struct bpf_map_create_opts *opts);
64 struct bpf_prog_load_opts {
65 size_t sz; /* size of this struct for forward/backward compatibility */
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.
73 enum bpf_attach_type expected_attach_type;
81 __u32 attach_btf_obj_fd;
85 /* .BTF.ext func info data */
86 const void *func_info;
88 __u32 func_info_rec_size;
90 /* .BTF.ext line info data */
91 const void *line_info;
93 __u32 line_info_rec_size;
95 /* verifier log options */
99 /* output: actual total log contents size (including termintaing zero).
100 * It could be both larger than original log_size (if log was
101 * truncated), or smaller (if log buffer wasn't filled completely).
102 * If kernel doesn't support this feature, log_size is left unchanged.
107 #define bpf_prog_load_opts__last_field log_true_size
109 LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type,
110 const char *prog_name, const char *license,
111 const struct bpf_insn *insns, size_t insn_cnt,
112 struct bpf_prog_load_opts *opts);
114 /* Flags to direct loading requirements */
115 #define MAPS_RELAX_COMPAT 0x01
117 /* Recommended log buffer size */
118 #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */
120 struct bpf_btf_load_opts {
121 size_t sz; /* size of this struct for forward/backward compatibility */
123 /* kernel log options */
127 /* output: actual total log contents size (including termintaing zero).
128 * It could be both larger than original log_size (if log was
129 * truncated), or smaller (if log buffer wasn't filled completely).
130 * If kernel doesn't support this feature, log_size is left unchanged.
135 #define bpf_btf_load_opts__last_field log_true_size
137 LIBBPF_API int bpf_btf_load(const void *btf_data, size_t btf_size,
138 struct bpf_btf_load_opts *opts);
140 LIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value,
143 LIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value);
144 LIBBPF_API int bpf_map_lookup_elem_flags(int fd, const void *key, void *value,
146 LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key,
148 LIBBPF_API int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key,
149 void *value, __u64 flags);
150 LIBBPF_API int bpf_map_delete_elem(int fd, const void *key);
151 LIBBPF_API int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags);
152 LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key);
153 LIBBPF_API int bpf_map_freeze(int fd);
155 struct bpf_map_batch_opts {
156 size_t sz; /* size of this struct for forward/backward compatibility */
160 #define bpf_map_batch_opts__last_field flags
164 * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple
165 * elements in a BPF map.
167 * @param fd BPF map file descriptor
168 * @param keys pointer to an array of *count* keys
169 * @param count input and output parameter; on input **count** represents the
170 * number of elements in the map to delete in batch;
171 * on output if a non-EFAULT error is returned, **count** represents the number of deleted
172 * elements if the output **count** value is not equal to the input **count** value
173 * If EFAULT is returned, **count** should not be trusted to be correct.
174 * @param opts options for configuring the way the batch deletion works
175 * @return 0, on success; negative error code, otherwise (errno is also set to
178 LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys,
180 const struct bpf_map_batch_opts *opts);
183 * @brief **bpf_map_lookup_batch()** allows for batch lookup of BPF map elements.
185 * The parameter *in_batch* is the address of the first element in the batch to read.
186 * *out_batch* is an output parameter that should be passed as *in_batch* to subsequent
187 * calls to **bpf_map_lookup_batch()**. NULL can be passed for *in_batch* to indicate
188 * that the batched lookup starts from the beginning of the map.
190 * The *keys* and *values* are output parameters which must point to memory large enough to
191 * hold *count* items based on the key and value size of the map *map_fd*. The *keys*
192 * buffer must be of *key_size* * *count*. The *values* buffer must be of
193 * *value_size* * *count*.
195 * @param fd BPF map file descriptor
196 * @param in_batch address of the first element in batch to read, can pass NULL to
197 * indicate that the batched lookup starts from the beginning of the map.
198 * @param out_batch output parameter that should be passed to next call as *in_batch*
199 * @param keys pointer to an array large enough for *count* keys
200 * @param values pointer to an array large enough for *count* values
201 * @param count input and output parameter; on input it's the number of elements
202 * in the map to read in batch; on output it's the number of elements that were
204 * If a non-EFAULT error is returned, count will be set as the number of elements
205 * that were read before the error occurred.
206 * If EFAULT is returned, **count** should not be trusted to be correct.
207 * @param opts options for configuring the way the batch lookup works
208 * @return 0, on success; negative error code, otherwise (errno is also set to
211 LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch,
212 void *keys, void *values, __u32 *count,
213 const struct bpf_map_batch_opts *opts);
216 * @brief **bpf_map_lookup_and_delete_batch()** allows for batch lookup and deletion
217 * of BPF map elements where each element is deleted after being retrieved.
219 * @param fd BPF map file descriptor
220 * @param in_batch address of the first element in batch to read, can pass NULL to
221 * get address of the first element in *out_batch*
222 * @param out_batch output parameter that should be passed to next call as *in_batch*
223 * @param keys pointer to an array of *count* keys
224 * @param values pointer to an array large enough for *count* values
225 * @param count input and output parameter; on input it's the number of elements
226 * in the map to read and delete in batch; on output it represents the number of
227 * elements that were successfully read and deleted
228 * If a non-**EFAULT** error code is returned and if the output **count** value
229 * is not equal to the input **count** value, up to **count** elements may
231 * if **EFAULT** is returned up to *count* elements may have been deleted without
232 * being returned via the *keys* and *values* output parameters.
233 * @param opts options for configuring the way the batch lookup and delete works
234 * @return 0, on success; negative error code, otherwise (errno is also set to
237 LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch,
238 void *out_batch, void *keys,
239 void *values, __u32 *count,
240 const struct bpf_map_batch_opts *opts);
243 * @brief **bpf_map_update_batch()** updates multiple elements in a map
244 * by specifying keys and their corresponding values.
246 * The *keys* and *values* parameters must point to memory large enough
247 * to hold *count* items based on the key and value size of the map.
249 * The *opts* parameter can be used to control how *bpf_map_update_batch()*
250 * should handle keys that either do or do not already exist in the map.
251 * In particular the *flags* parameter of *bpf_map_batch_opts* can be
252 * one of the following:
254 * Note that *count* is an input and output parameter, where on output it
255 * represents how many elements were successfully updated. Also note that if
256 * **EFAULT** then *count* should not be trusted to be correct.
259 * Create new elements or update existing.
262 * Create new elements only if they do not exist.
265 * Update existing elements.
268 * Update spin_lock-ed map elements. This must be
269 * specified if the map value contains a spinlock.
271 * @param fd BPF map file descriptor
272 * @param keys pointer to an array of *count* keys
273 * @param values pointer to an array of *count* values
274 * @param count input and output parameter; on input it's the number of elements
275 * in the map to update in batch; on output if a non-EFAULT error is returned,
276 * **count** represents the number of updated elements if the output **count**
277 * value is not equal to the input **count** value.
278 * If EFAULT is returned, **count** should not be trusted to be correct.
279 * @param opts options for configuring the way the batch update works
280 * @return 0, on success; negative error code, otherwise (errno is also set to
283 LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values,
285 const struct bpf_map_batch_opts *opts);
287 struct bpf_obj_pin_opts {
288 size_t sz; /* size of this struct for forward/backward compatibility */
295 #define bpf_obj_pin_opts__last_field path_fd
297 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
298 LIBBPF_API int bpf_obj_pin_opts(int fd, const char *pathname,
299 const struct bpf_obj_pin_opts *opts);
301 struct bpf_obj_get_opts {
302 size_t sz; /* size of this struct for forward/backward compatibility */
309 #define bpf_obj_get_opts__last_field path_fd
311 LIBBPF_API int bpf_obj_get(const char *pathname);
312 LIBBPF_API int bpf_obj_get_opts(const char *pathname,
313 const struct bpf_obj_get_opts *opts);
315 struct bpf_prog_attach_opts {
316 size_t sz; /* size of this struct for forward/backward compatibility */
320 #define bpf_prog_attach_opts__last_field replace_prog_fd
322 LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
323 enum bpf_attach_type type, unsigned int flags);
324 LIBBPF_API int bpf_prog_attach_opts(int prog_fd, int attachable_fd,
325 enum bpf_attach_type type,
326 const struct bpf_prog_attach_opts *opts);
327 LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
328 LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
329 enum bpf_attach_type type);
331 union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */
332 struct bpf_link_create_opts {
333 size_t sz; /* size of this struct for forward/backward compatibility */
335 union bpf_iter_link_info *iter_info;
346 const unsigned long *addrs;
347 const __u64 *cookies;
355 #define bpf_link_create_opts__last_field kprobe_multi.cookies
357 LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
358 enum bpf_attach_type attach_type,
359 const struct bpf_link_create_opts *opts);
361 LIBBPF_API int bpf_link_detach(int link_fd);
363 struct bpf_link_update_opts {
364 size_t sz; /* size of this struct for forward/backward compatibility */
365 __u32 flags; /* extra flags */
366 __u32 old_prog_fd; /* expected old program FD */
367 __u32 old_map_fd; /* expected old map FD */
369 #define bpf_link_update_opts__last_field old_map_fd
371 LIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd,
372 const struct bpf_link_update_opts *opts);
374 LIBBPF_API int bpf_iter_create(int link_fd);
376 struct bpf_prog_test_run_attr {
381 void *data_out; /* optional */
382 __u32 data_size_out; /* in: max length of data_out
383 * out: length of data_out */
384 __u32 retval; /* out: return code of the BPF program */
385 __u32 duration; /* out: average per repetition in ns */
386 const void *ctx_in; /* optional */
388 void *ctx_out; /* optional */
389 __u32 ctx_size_out; /* in: max length of ctx_out
390 * out: length of cxt_out */
393 LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id);
394 LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
395 LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id);
396 LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id);
398 struct bpf_get_fd_by_id_opts {
399 size_t sz; /* size of this struct for forward/backward compatibility */
400 __u32 open_flags; /* permissions requested for the operation on fd */
403 #define bpf_get_fd_by_id_opts__last_field open_flags
405 LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
406 LIBBPF_API int bpf_prog_get_fd_by_id_opts(__u32 id,
407 const struct bpf_get_fd_by_id_opts *opts);
408 LIBBPF_API int bpf_map_get_fd_by_id(__u32 id);
409 LIBBPF_API int bpf_map_get_fd_by_id_opts(__u32 id,
410 const struct bpf_get_fd_by_id_opts *opts);
411 LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id);
412 LIBBPF_API int bpf_btf_get_fd_by_id_opts(__u32 id,
413 const struct bpf_get_fd_by_id_opts *opts);
414 LIBBPF_API int bpf_link_get_fd_by_id(__u32 id);
415 LIBBPF_API int bpf_link_get_fd_by_id_opts(__u32 id,
416 const struct bpf_get_fd_by_id_opts *opts);
417 LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len);
420 * @brief **bpf_prog_get_info_by_fd()** obtains information about the BPF
421 * program corresponding to *prog_fd*.
423 * Populates up to *info_len* bytes of *info* and updates *info_len* with the
424 * actual number of bytes written to *info*.
426 * @param prog_fd BPF program file descriptor
427 * @param info pointer to **struct bpf_prog_info** that will be populated with
428 * BPF program information
429 * @param info_len pointer to the size of *info*; on success updated with the
430 * number of bytes written to *info*
431 * @return 0, on success; negative error code, otherwise (errno is also set to
434 LIBBPF_API int bpf_prog_get_info_by_fd(int prog_fd, struct bpf_prog_info *info, __u32 *info_len);
437 * @brief **bpf_map_get_info_by_fd()** obtains information about the BPF
438 * map corresponding to *map_fd*.
440 * Populates up to *info_len* bytes of *info* and updates *info_len* with the
441 * actual number of bytes written to *info*.
443 * @param map_fd BPF map file descriptor
444 * @param info pointer to **struct bpf_map_info** that will be populated with
445 * BPF map information
446 * @param info_len pointer to the size of *info*; on success updated with the
447 * number of bytes written to *info*
448 * @return 0, on success; negative error code, otherwise (errno is also set to
451 LIBBPF_API int bpf_map_get_info_by_fd(int map_fd, struct bpf_map_info *info, __u32 *info_len);
454 * @brief **bpf_btf_get_info_by_fd()** obtains information about the
455 * BTF object corresponding to *btf_fd*.
457 * Populates up to *info_len* bytes of *info* and updates *info_len* with the
458 * actual number of bytes written to *info*.
460 * @param btf_fd BTF object file descriptor
461 * @param info pointer to **struct bpf_btf_info** that will be populated with
462 * BTF object information
463 * @param info_len pointer to the size of *info*; on success updated with the
464 * number of bytes written to *info*
465 * @return 0, on success; negative error code, otherwise (errno is also set to
468 LIBBPF_API int bpf_btf_get_info_by_fd(int btf_fd, struct bpf_btf_info *info, __u32 *info_len);
471 * @brief **bpf_btf_get_info_by_fd()** obtains information about the BPF
472 * link corresponding to *link_fd*.
474 * Populates up to *info_len* bytes of *info* and updates *info_len* with the
475 * actual number of bytes written to *info*.
477 * @param link_fd BPF link file descriptor
478 * @param info pointer to **struct bpf_link_info** that will be populated with
479 * BPF link information
480 * @param info_len pointer to the size of *info*; on success updated with the
481 * number of bytes written to *info*
482 * @return 0, on success; negative error code, otherwise (errno is also set to
485 LIBBPF_API int bpf_link_get_info_by_fd(int link_fd, struct bpf_link_info *info, __u32 *info_len);
487 struct bpf_prog_query_opts {
488 size_t sz; /* size of this struct for forward/backward compatibility */
490 __u32 attach_flags; /* output argument */
492 __u32 prog_cnt; /* input+output argument */
493 __u32 *prog_attach_flags;
495 #define bpf_prog_query_opts__last_field prog_attach_flags
497 LIBBPF_API int bpf_prog_query_opts(int target_fd,
498 enum bpf_attach_type type,
499 struct bpf_prog_query_opts *opts);
500 LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type,
501 __u32 query_flags, __u32 *attach_flags,
502 __u32 *prog_ids, __u32 *prog_cnt);
504 LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd);
505 LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf,
506 __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
507 __u64 *probe_offset, __u64 *probe_addr);
510 /* forward-declaring enums in C++ isn't compatible with pure C enums, so
511 * instead define bpf_enable_stats() as accepting int as an input
513 LIBBPF_API int bpf_enable_stats(int type);
515 enum bpf_stats_type; /* defined in up-to-date linux/bpf.h */
516 LIBBPF_API int bpf_enable_stats(enum bpf_stats_type type);
519 struct bpf_prog_bind_opts {
520 size_t sz; /* size of this struct for forward/backward compatibility */
523 #define bpf_prog_bind_opts__last_field flags
525 LIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd,
526 const struct bpf_prog_bind_opts *opts);
528 struct bpf_test_run_opts {
529 size_t sz; /* size of this struct for forward/backward compatibility */
530 const void *data_in; /* optional */
531 void *data_out; /* optional */
533 __u32 data_size_out; /* in: max length of data_out
534 * out: length of data_out
536 const void *ctx_in; /* optional */
537 void *ctx_out; /* optional */
539 __u32 ctx_size_out; /* in: max length of ctx_out
540 * out: length of cxt_out
542 __u32 retval; /* out: return code of the BPF program */
544 __u32 duration; /* out: average per repetition in ns */
549 #define bpf_test_run_opts__last_field batch_size
551 LIBBPF_API int bpf_prog_test_run_opts(int prog_fd,
552 struct bpf_test_run_opts *opts);
558 #endif /* __LIBBPF_BPF_H */