11bc38b8fSAlexei Starovoitov /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 26061a3d6SEric Leblond 3e3ed2fefSWang Nan /* 40a504fa1SIlya Leoshkevich * Common BPF 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; 9994e55c0fSAndrii Nakryiko /* output: actual total log contents size (including termintaing zero). 10094e55c0fSAndrii Nakryiko * It could be both larger than original log_size (if log was 10194e55c0fSAndrii Nakryiko * truncated), or smaller (if log buffer wasn't filled completely). 10294e55c0fSAndrii Nakryiko * If kernel doesn't support this feature, log_size is left unchanged. 10394e55c0fSAndrii Nakryiko */ 10494e55c0fSAndrii Nakryiko __u32 log_true_size; 10594e55c0fSAndrii Nakryiko size_t :0; 106d10ef2b8SAndrii Nakryiko }; 10794e55c0fSAndrii Nakryiko #define bpf_prog_load_opts__last_field log_true_size 108d10ef2b8SAndrii Nakryiko 109d10ef2b8SAndrii Nakryiko LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type, 110d10ef2b8SAndrii Nakryiko const char *prog_name, const char *license, 111d10ef2b8SAndrii Nakryiko const struct bpf_insn *insns, size_t insn_cnt, 11294e55c0fSAndrii Nakryiko struct bpf_prog_load_opts *opts); 113d7be143bSAndrey Ignatov 114c034a177SJohn Fastabend /* Flags to direct loading requirements */ 115c034a177SJohn Fastabend #define MAPS_RELAX_COMPAT 0x01 116c034a177SJohn Fastabend 117e0e3ea88SAndrii Nakryiko /* Recommended log buffer size */ 1184519efa6SMcCabe, Robert J #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */ 119e0e3ea88SAndrii Nakryiko 1200ed08d67SAndrii Nakryiko struct bpf_btf_load_opts { 1210ed08d67SAndrii Nakryiko size_t sz; /* size of this struct for forward/backward compatibility */ 1220ed08d67SAndrii Nakryiko 1230ed08d67SAndrii Nakryiko /* kernel log options */ 1240ed08d67SAndrii Nakryiko char *log_buf; 1250ed08d67SAndrii Nakryiko __u32 log_level; 1260ed08d67SAndrii Nakryiko __u32 log_size; 127097d8002SAndrii Nakryiko /* output: actual total log contents size (including termintaing zero). 128097d8002SAndrii Nakryiko * It could be both larger than original log_size (if log was 129097d8002SAndrii Nakryiko * truncated), or smaller (if log buffer wasn't filled completely). 130097d8002SAndrii Nakryiko * If kernel doesn't support this feature, log_size is left unchanged. 131097d8002SAndrii Nakryiko */ 132097d8002SAndrii Nakryiko __u32 log_true_size; 133097d8002SAndrii Nakryiko size_t :0; 1340ed08d67SAndrii Nakryiko }; 135097d8002SAndrii Nakryiko #define bpf_btf_load_opts__last_field log_true_size 1360ed08d67SAndrii Nakryiko 1370ed08d67SAndrii Nakryiko LIBBPF_API int bpf_btf_load(const void *btf_data, size_t btf_size, 138097d8002SAndrii Nakryiko struct bpf_btf_load_opts *opts); 1390ed08d67SAndrii Nakryiko 140ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value, 14183d994d0SJoe Stringer __u64 flags); 1429742da01SWang Nan 143ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value); 144df5d22faSAlexei Starovoitov LIBBPF_API int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, 145df5d22faSAlexei Starovoitov __u64 flags); 14643b987d2SMauricio Vasquez B LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key, 14743b987d2SMauricio Vasquez B void *value); 148d59b9f2dSDenis Salopek LIBBPF_API int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key, 149d59b9f2dSDenis Salopek void *value, __u64 flags); 150ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_delete_elem(int fd, const void *key); 151737d0646SAndrii Nakryiko LIBBPF_API int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags); 152ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key); 153d859900cSDaniel Borkmann LIBBPF_API int bpf_map_freeze(int fd); 1542ab3d86eSYonghong Song 1552ab3d86eSYonghong Song struct bpf_map_batch_opts { 1562ab3d86eSYonghong Song size_t sz; /* size of this struct for forward/backward compatibility */ 1572ab3d86eSYonghong Song __u64 elem_flags; 1582ab3d86eSYonghong Song __u64 flags; 1592ab3d86eSYonghong Song }; 1602ab3d86eSYonghong Song #define bpf_map_batch_opts__last_field flags 1612ab3d86eSYonghong Song 162e59618f0SGrant Seltzer 163e59618f0SGrant Seltzer /** 164e59618f0SGrant Seltzer * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple 165e59618f0SGrant Seltzer * elements in a BPF map. 166e59618f0SGrant Seltzer * 167e59618f0SGrant Seltzer * @param fd BPF map file descriptor 168e59618f0SGrant Seltzer * @param keys pointer to an array of *count* keys 169e59618f0SGrant Seltzer * @param count input and output parameter; on input **count** represents the 170e59618f0SGrant Seltzer * number of elements in the map to delete in batch; 171e59618f0SGrant Seltzer * on output if a non-EFAULT error is returned, **count** represents the number of deleted 172e59618f0SGrant Seltzer * elements if the output **count** value is not equal to the input **count** value 173e59618f0SGrant Seltzer * If EFAULT is returned, **count** should not be trusted to be correct. 174e59618f0SGrant Seltzer * @param opts options for configuring the way the batch deletion works 175e59618f0SGrant Seltzer * @return 0, on success; negative error code, otherwise (errno is also set to 176e59618f0SGrant Seltzer * the error code) 177e59618f0SGrant Seltzer */ 178e59618f0SGrant Seltzer LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys, 1792ab3d86eSYonghong Song __u32 *count, 1802ab3d86eSYonghong Song const struct bpf_map_batch_opts *opts); 181e59618f0SGrant Seltzer 182e59618f0SGrant Seltzer /** 183e59618f0SGrant Seltzer * @brief **bpf_map_lookup_batch()** allows for batch lookup of BPF map elements. 184e59618f0SGrant Seltzer * 185e59618f0SGrant Seltzer * The parameter *in_batch* is the address of the first element in the batch to read. 186e59618f0SGrant Seltzer * *out_batch* is an output parameter that should be passed as *in_batch* to subsequent 187e59618f0SGrant Seltzer * calls to **bpf_map_lookup_batch()**. NULL can be passed for *in_batch* to indicate 188e59618f0SGrant Seltzer * that the batched lookup starts from the beginning of the map. 189e59618f0SGrant Seltzer * 190e59618f0SGrant Seltzer * The *keys* and *values* are output parameters which must point to memory large enough to 191e59618f0SGrant Seltzer * hold *count* items based on the key and value size of the map *map_fd*. The *keys* 192e59618f0SGrant Seltzer * buffer must be of *key_size* * *count*. The *values* buffer must be of 193e59618f0SGrant Seltzer * *value_size* * *count*. 194e59618f0SGrant Seltzer * 195e59618f0SGrant Seltzer * @param fd BPF map file descriptor 196e59618f0SGrant Seltzer * @param in_batch address of the first element in batch to read, can pass NULL to 197e59618f0SGrant Seltzer * indicate that the batched lookup starts from the beginning of the map. 198e59618f0SGrant Seltzer * @param out_batch output parameter that should be passed to next call as *in_batch* 199e59618f0SGrant Seltzer * @param keys pointer to an array large enough for *count* keys 200e59618f0SGrant Seltzer * @param values pointer to an array large enough for *count* values 201e59618f0SGrant Seltzer * @param count input and output parameter; on input it's the number of elements 202e59618f0SGrant Seltzer * in the map to read in batch; on output it's the number of elements that were 203e59618f0SGrant Seltzer * successfully read. 204e59618f0SGrant Seltzer * If a non-EFAULT error is returned, count will be set as the number of elements 205e59618f0SGrant Seltzer * that were read before the error occurred. 206e59618f0SGrant Seltzer * If EFAULT is returned, **count** should not be trusted to be correct. 207e59618f0SGrant Seltzer * @param opts options for configuring the way the batch lookup works 208e59618f0SGrant Seltzer * @return 0, on success; negative error code, otherwise (errno is also set to 209e59618f0SGrant Seltzer * the error code) 210e59618f0SGrant Seltzer */ 2112ab3d86eSYonghong Song LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch, 2122ab3d86eSYonghong Song void *keys, void *values, __u32 *count, 2132ab3d86eSYonghong Song const struct bpf_map_batch_opts *opts); 214e59618f0SGrant Seltzer 215e59618f0SGrant Seltzer /** 216e59618f0SGrant Seltzer * @brief **bpf_map_lookup_and_delete_batch()** allows for batch lookup and deletion 217e59618f0SGrant Seltzer * of BPF map elements where each element is deleted after being retrieved. 218e59618f0SGrant Seltzer * 219e59618f0SGrant Seltzer * @param fd BPF map file descriptor 220e59618f0SGrant Seltzer * @param in_batch address of the first element in batch to read, can pass NULL to 221e59618f0SGrant Seltzer * get address of the first element in *out_batch* 222e59618f0SGrant Seltzer * @param out_batch output parameter that should be passed to next call as *in_batch* 223e59618f0SGrant Seltzer * @param keys pointer to an array of *count* keys 224e59618f0SGrant Seltzer * @param values pointer to an array large enough for *count* values 225e59618f0SGrant Seltzer * @param count input and output parameter; on input it's the number of elements 226e59618f0SGrant Seltzer * in the map to read and delete in batch; on output it represents the number of 227e59618f0SGrant Seltzer * elements that were successfully read and deleted 228e59618f0SGrant Seltzer * If a non-**EFAULT** error code is returned and if the output **count** value 229e59618f0SGrant Seltzer * is not equal to the input **count** value, up to **count** elements may 230e59618f0SGrant Seltzer * have been deleted. 231e59618f0SGrant Seltzer * if **EFAULT** is returned up to *count* elements may have been deleted without 232e59618f0SGrant Seltzer * being returned via the *keys* and *values* output parameters. 233e59618f0SGrant Seltzer * @param opts options for configuring the way the batch lookup and delete works 234e59618f0SGrant Seltzer * @return 0, on success; negative error code, otherwise (errno is also set to 235e59618f0SGrant Seltzer * the error code) 236e59618f0SGrant Seltzer */ 2372ab3d86eSYonghong Song LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, 2382ab3d86eSYonghong Song void *out_batch, void *keys, 2392ab3d86eSYonghong Song void *values, __u32 *count, 2402ab3d86eSYonghong Song const struct bpf_map_batch_opts *opts); 241e59618f0SGrant Seltzer 242e59618f0SGrant Seltzer /** 243e59618f0SGrant Seltzer * @brief **bpf_map_update_batch()** updates multiple elements in a map 244e59618f0SGrant Seltzer * by specifying keys and their corresponding values. 245e59618f0SGrant Seltzer * 246e59618f0SGrant Seltzer * The *keys* and *values* parameters must point to memory large enough 247e59618f0SGrant Seltzer * to hold *count* items based on the key and value size of the map. 248e59618f0SGrant Seltzer * 249e59618f0SGrant Seltzer * The *opts* parameter can be used to control how *bpf_map_update_batch()* 250e59618f0SGrant Seltzer * should handle keys that either do or do not already exist in the map. 251e59618f0SGrant Seltzer * In particular the *flags* parameter of *bpf_map_batch_opts* can be 252e59618f0SGrant Seltzer * one of the following: 253e59618f0SGrant Seltzer * 254e59618f0SGrant Seltzer * Note that *count* is an input and output parameter, where on output it 255e59618f0SGrant Seltzer * represents how many elements were successfully updated. Also note that if 256e59618f0SGrant Seltzer * **EFAULT** then *count* should not be trusted to be correct. 257e59618f0SGrant Seltzer * 258e59618f0SGrant Seltzer * **BPF_ANY** 259e59618f0SGrant Seltzer * Create new elements or update existing. 260e59618f0SGrant Seltzer * 261e59618f0SGrant Seltzer * **BPF_NOEXIST** 262e59618f0SGrant Seltzer * Create new elements only if they do not exist. 263e59618f0SGrant Seltzer * 264e59618f0SGrant Seltzer * **BPF_EXIST** 265e59618f0SGrant Seltzer * Update existing elements. 266e59618f0SGrant Seltzer * 267e59618f0SGrant Seltzer * **BPF_F_LOCK** 268e59618f0SGrant Seltzer * Update spin_lock-ed map elements. This must be 269e59618f0SGrant Seltzer * specified if the map value contains a spinlock. 270e59618f0SGrant Seltzer * 271e59618f0SGrant Seltzer * @param fd BPF map file descriptor 272e59618f0SGrant Seltzer * @param keys pointer to an array of *count* keys 273e59618f0SGrant Seltzer * @param values pointer to an array of *count* values 274e59618f0SGrant Seltzer * @param count input and output parameter; on input it's the number of elements 275e59618f0SGrant Seltzer * in the map to update in batch; on output if a non-EFAULT error is returned, 276e59618f0SGrant Seltzer * **count** represents the number of updated elements if the output **count** 277e59618f0SGrant Seltzer * value is not equal to the input **count** value. 278e59618f0SGrant Seltzer * If EFAULT is returned, **count** should not be trusted to be correct. 279e59618f0SGrant Seltzer * @param opts options for configuring the way the batch update works 280e59618f0SGrant Seltzer * @return 0, on success; negative error code, otherwise (errno is also set to 281e59618f0SGrant Seltzer * the error code) 282e59618f0SGrant Seltzer */ 283e59618f0SGrant Seltzer LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values, 2842ab3d86eSYonghong Song __u32 *count, 2852ab3d86eSYonghong Song const struct bpf_map_batch_opts *opts); 2862ab3d86eSYonghong Song 287f1674dc7SAndrii Nakryiko struct bpf_obj_pin_opts { 288f1674dc7SAndrii Nakryiko size_t sz; /* size of this struct for forward/backward compatibility */ 289f1674dc7SAndrii Nakryiko 290f1674dc7SAndrii Nakryiko __u32 file_flags; 291f1674dc7SAndrii Nakryiko int path_fd; 292f1674dc7SAndrii Nakryiko 293f1674dc7SAndrii Nakryiko size_t :0; 294f1674dc7SAndrii Nakryiko }; 295f1674dc7SAndrii Nakryiko #define bpf_obj_pin_opts__last_field path_fd 296f1674dc7SAndrii Nakryiko 297f1674dc7SAndrii Nakryiko LIBBPF_API int bpf_obj_pin(int fd, const char *pathname); 298f1674dc7SAndrii Nakryiko LIBBPF_API int bpf_obj_pin_opts(int fd, const char *pathname, 299f1674dc7SAndrii Nakryiko const struct bpf_obj_pin_opts *opts); 300f1674dc7SAndrii Nakryiko 301395fc4faSJoe Burton struct bpf_obj_get_opts { 302395fc4faSJoe Burton size_t sz; /* size of this struct for forward/backward compatibility */ 303395fc4faSJoe Burton 304395fc4faSJoe Burton __u32 file_flags; 305f1674dc7SAndrii Nakryiko int path_fd; 306395fc4faSJoe Burton 307395fc4faSJoe Burton size_t :0; 308395fc4faSJoe Burton }; 309f1674dc7SAndrii Nakryiko #define bpf_obj_get_opts__last_field path_fd 310395fc4faSJoe Burton 311ab9e0848SAndrey Ignatov LIBBPF_API int bpf_obj_get(const char *pathname); 312395fc4faSJoe Burton LIBBPF_API int bpf_obj_get_opts(const char *pathname, 313395fc4faSJoe Burton const struct bpf_obj_get_opts *opts); 314cdbee383SAndrey Ignatov 315ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd, 316ab9e0848SAndrey Ignatov enum bpf_attach_type type, unsigned int flags); 317ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type); 318ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd, 319ab9e0848SAndrey Ignatov enum bpf_attach_type type); 32064a97591SLorenz Bauer 321*fe20ce3aSDaniel Borkmann struct bpf_prog_attach_opts { 322*fe20ce3aSDaniel Borkmann size_t sz; /* size of this struct for forward/backward compatibility */ 323*fe20ce3aSDaniel Borkmann __u32 flags; 324*fe20ce3aSDaniel Borkmann union { 325*fe20ce3aSDaniel Borkmann int replace_prog_fd; 326*fe20ce3aSDaniel Borkmann int replace_fd; 327*fe20ce3aSDaniel Borkmann }; 328*fe20ce3aSDaniel Borkmann int relative_fd; 329*fe20ce3aSDaniel Borkmann __u32 relative_id; 330*fe20ce3aSDaniel Borkmann __u64 expected_revision; 331*fe20ce3aSDaniel Borkmann size_t :0; 332*fe20ce3aSDaniel Borkmann }; 333*fe20ce3aSDaniel Borkmann #define bpf_prog_attach_opts__last_field expected_revision 334*fe20ce3aSDaniel Borkmann 335*fe20ce3aSDaniel Borkmann struct bpf_prog_detach_opts { 336*fe20ce3aSDaniel Borkmann size_t sz; /* size of this struct for forward/backward compatibility */ 337*fe20ce3aSDaniel Borkmann __u32 flags; 338*fe20ce3aSDaniel Borkmann int relative_fd; 339*fe20ce3aSDaniel Borkmann __u32 relative_id; 340*fe20ce3aSDaniel Borkmann __u64 expected_revision; 341*fe20ce3aSDaniel Borkmann size_t :0; 342*fe20ce3aSDaniel Borkmann }; 343*fe20ce3aSDaniel Borkmann #define bpf_prog_detach_opts__last_field expected_revision 344*fe20ce3aSDaniel Borkmann 345*fe20ce3aSDaniel Borkmann /** 346*fe20ce3aSDaniel Borkmann * @brief **bpf_prog_attach_opts()** attaches the BPF program corresponding to 347*fe20ce3aSDaniel Borkmann * *prog_fd* to a *target* which can represent a file descriptor or netdevice 348*fe20ce3aSDaniel Borkmann * ifindex. 349*fe20ce3aSDaniel Borkmann * 350*fe20ce3aSDaniel Borkmann * @param prog_fd BPF program file descriptor 351*fe20ce3aSDaniel Borkmann * @param target attach location file descriptor or ifindex 352*fe20ce3aSDaniel Borkmann * @param type attach type for the BPF program 353*fe20ce3aSDaniel Borkmann * @param opts options for configuring the attachment 354*fe20ce3aSDaniel Borkmann * @return 0, on success; negative error code, otherwise (errno is also set to 355*fe20ce3aSDaniel Borkmann * the error code) 356*fe20ce3aSDaniel Borkmann */ 357*fe20ce3aSDaniel Borkmann LIBBPF_API int bpf_prog_attach_opts(int prog_fd, int target, 358*fe20ce3aSDaniel Borkmann enum bpf_attach_type type, 359*fe20ce3aSDaniel Borkmann const struct bpf_prog_attach_opts *opts); 360*fe20ce3aSDaniel Borkmann 361*fe20ce3aSDaniel Borkmann /** 362*fe20ce3aSDaniel Borkmann * @brief **bpf_prog_detach_opts()** detaches the BPF program corresponding to 363*fe20ce3aSDaniel Borkmann * *prog_fd* from a *target* which can represent a file descriptor or netdevice 364*fe20ce3aSDaniel Borkmann * ifindex. 365*fe20ce3aSDaniel Borkmann * 366*fe20ce3aSDaniel Borkmann * @param prog_fd BPF program file descriptor 367*fe20ce3aSDaniel Borkmann * @param target detach location file descriptor or ifindex 368*fe20ce3aSDaniel Borkmann * @param type detach type for the BPF program 369*fe20ce3aSDaniel Borkmann * @param opts options for configuring the detachment 370*fe20ce3aSDaniel Borkmann * @return 0, on success; negative error code, otherwise (errno is also set to 371*fe20ce3aSDaniel Borkmann * the error code) 372*fe20ce3aSDaniel Borkmann */ 373*fe20ce3aSDaniel Borkmann LIBBPF_API int bpf_prog_detach_opts(int prog_fd, int target, 374*fe20ce3aSDaniel Borkmann enum bpf_attach_type type, 375*fe20ce3aSDaniel Borkmann const struct bpf_prog_detach_opts *opts); 376*fe20ce3aSDaniel Borkmann 37774fc097dSYonghong Song union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */ 378cc4f864bSAndrii Nakryiko struct bpf_link_create_opts { 379cc4f864bSAndrii Nakryiko size_t sz; /* size of this struct for forward/backward compatibility */ 380cd31039aSYonghong Song __u32 flags; 38174fc097dSYonghong Song union bpf_iter_link_info *iter_info; 38274fc097dSYonghong Song __u32 iter_info_len; 383a5359091SToke Høiland-Jørgensen __u32 target_btf_id; 3843ec84f4bSAndrii Nakryiko union { 3853ec84f4bSAndrii Nakryiko struct { 3863ec84f4bSAndrii Nakryiko __u64 bpf_cookie; 3873ec84f4bSAndrii Nakryiko } perf_event; 3885117c26eSJiri Olsa struct { 3895117c26eSJiri Olsa __u32 flags; 3905117c26eSJiri Olsa __u32 cnt; 3915117c26eSJiri Olsa const char **syms; 3925117c26eSJiri Olsa const unsigned long *addrs; 3935117c26eSJiri Olsa const __u64 *cookies; 3945117c26eSJiri Olsa } kprobe_multi; 395129b9c5eSKui-Feng Lee struct { 396129b9c5eSKui-Feng Lee __u64 cookie; 397129b9c5eSKui-Feng Lee } tracing; 39852364abbSFlorian Westphal struct { 39952364abbSFlorian Westphal __u32 pf; 40052364abbSFlorian Westphal __u32 hooknum; 40152364abbSFlorian Westphal __s32 priority; 40252364abbSFlorian Westphal __u32 flags; 40352364abbSFlorian Westphal } netfilter; 404cc4f864bSAndrii Nakryiko }; 4053ec84f4bSAndrii Nakryiko size_t :0; 4063ec84f4bSAndrii Nakryiko }; 4075117c26eSJiri Olsa #define bpf_link_create_opts__last_field kprobe_multi.cookies 408cc4f864bSAndrii Nakryiko 409cc4f864bSAndrii Nakryiko LIBBPF_API int bpf_link_create(int prog_fd, int target_fd, 410cc4f864bSAndrii Nakryiko enum bpf_attach_type attach_type, 411cc4f864bSAndrii Nakryiko const struct bpf_link_create_opts *opts); 412cc4f864bSAndrii Nakryiko 4132e49527eSAndrii Nakryiko LIBBPF_API int bpf_link_detach(int link_fd); 4142e49527eSAndrii Nakryiko 415cc4f864bSAndrii Nakryiko struct bpf_link_update_opts { 416cc4f864bSAndrii Nakryiko size_t sz; /* size of this struct for forward/backward compatibility */ 417cc4f864bSAndrii Nakryiko __u32 flags; /* extra flags */ 418cc4f864bSAndrii Nakryiko __u32 old_prog_fd; /* expected old program FD */ 419912dd4b0SKui-Feng Lee __u32 old_map_fd; /* expected old map FD */ 420cc4f864bSAndrii Nakryiko }; 421912dd4b0SKui-Feng Lee #define bpf_link_update_opts__last_field old_map_fd 422cc4f864bSAndrii Nakryiko 423cc4f864bSAndrii Nakryiko LIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd, 424cc4f864bSAndrii Nakryiko const struct bpf_link_update_opts *opts); 425cc4f864bSAndrii Nakryiko 426c09add2fSYonghong Song LIBBPF_API int bpf_iter_create(int link_fd); 427c09add2fSYonghong Song 42864a97591SLorenz Bauer struct bpf_prog_test_run_attr { 42964a97591SLorenz Bauer int prog_fd; 43064a97591SLorenz Bauer int repeat; 43164a97591SLorenz Bauer const void *data_in; 43264a97591SLorenz Bauer __u32 data_size_in; 43364a97591SLorenz Bauer void *data_out; /* optional */ 43464a97591SLorenz Bauer __u32 data_size_out; /* in: max length of data_out 43564a97591SLorenz Bauer * out: length of data_out */ 43664a97591SLorenz Bauer __u32 retval; /* out: return code of the BPF program */ 43764a97591SLorenz Bauer __u32 duration; /* out: average per repetition in ns */ 4385e903c65SStanislav Fomichev const void *ctx_in; /* optional */ 4395e903c65SStanislav Fomichev __u32 ctx_size_in; 4405e903c65SStanislav Fomichev void *ctx_out; /* optional */ 4415e903c65SStanislav Fomichev __u32 ctx_size_out; /* in: max length of ctx_out 4425e903c65SStanislav Fomichev * out: length of cxt_out */ 44364a97591SLorenz Bauer }; 44464a97591SLorenz Bauer 445ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id); 446ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id); 44709d7c2e3SQuentin Monnet LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id); 4480dbc8668SAndrii Nakryiko LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id); 449243e3005SRoberto Sassu 450243e3005SRoberto Sassu struct bpf_get_fd_by_id_opts { 451243e3005SRoberto Sassu size_t sz; /* size of this struct for forward/backward compatibility */ 452243e3005SRoberto Sassu __u32 open_flags; /* permissions requested for the operation on fd */ 453243e3005SRoberto Sassu size_t :0; 454243e3005SRoberto Sassu }; 455243e3005SRoberto Sassu #define bpf_get_fd_by_id_opts__last_field open_flags 456243e3005SRoberto Sassu 457ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id); 4588f13f168SRoberto Sassu LIBBPF_API int bpf_prog_get_fd_by_id_opts(__u32 id, 4598f13f168SRoberto Sassu const struct bpf_get_fd_by_id_opts *opts); 460ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_get_fd_by_id(__u32 id); 461243e3005SRoberto Sassu LIBBPF_API int bpf_map_get_fd_by_id_opts(__u32 id, 462243e3005SRoberto Sassu const struct bpf_get_fd_by_id_opts *opts); 463ab9e0848SAndrey Ignatov LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id); 4642ce7cbf2SRoberto Sassu LIBBPF_API int bpf_btf_get_fd_by_id_opts(__u32 id, 4652ce7cbf2SRoberto Sassu const struct bpf_get_fd_by_id_opts *opts); 4660dbc8668SAndrii Nakryiko LIBBPF_API int bpf_link_get_fd_by_id(__u32 id); 46797c8f9ddSRoberto Sassu LIBBPF_API int bpf_link_get_fd_by_id_opts(__u32 id, 46897c8f9ddSRoberto Sassu const struct bpf_get_fd_by_id_opts *opts); 4690dbc8668SAndrii Nakryiko LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len); 4700a504fa1SIlya Leoshkevich 4710a504fa1SIlya Leoshkevich /** 4720a504fa1SIlya Leoshkevich * @brief **bpf_prog_get_info_by_fd()** obtains information about the BPF 4730a504fa1SIlya Leoshkevich * program corresponding to *prog_fd*. 4740a504fa1SIlya Leoshkevich * 4750a504fa1SIlya Leoshkevich * Populates up to *info_len* bytes of *info* and updates *info_len* with the 4760a504fa1SIlya Leoshkevich * actual number of bytes written to *info*. 4770a504fa1SIlya Leoshkevich * 4780a504fa1SIlya Leoshkevich * @param prog_fd BPF program file descriptor 4790a504fa1SIlya Leoshkevich * @param info pointer to **struct bpf_prog_info** that will be populated with 4800a504fa1SIlya Leoshkevich * BPF program information 4810a504fa1SIlya Leoshkevich * @param info_len pointer to the size of *info*; on success updated with the 4820a504fa1SIlya Leoshkevich * number of bytes written to *info* 4830a504fa1SIlya Leoshkevich * @return 0, on success; negative error code, otherwise (errno is also set to 4840a504fa1SIlya Leoshkevich * the error code) 48555a9ed0eSIlya Leoshkevich */ 48655a9ed0eSIlya Leoshkevich LIBBPF_API int bpf_prog_get_info_by_fd(int prog_fd, struct bpf_prog_info *info, __u32 *info_len); 4870a504fa1SIlya Leoshkevich 4880a504fa1SIlya Leoshkevich /** 4890a504fa1SIlya Leoshkevich * @brief **bpf_map_get_info_by_fd()** obtains information about the BPF 4900a504fa1SIlya Leoshkevich * map corresponding to *map_fd*. 4910a504fa1SIlya Leoshkevich * 4920a504fa1SIlya Leoshkevich * Populates up to *info_len* bytes of *info* and updates *info_len* with the 4930a504fa1SIlya Leoshkevich * actual number of bytes written to *info*. 4940a504fa1SIlya Leoshkevich * 4950a504fa1SIlya Leoshkevich * @param map_fd BPF map file descriptor 4960a504fa1SIlya Leoshkevich * @param info pointer to **struct bpf_map_info** that will be populated with 4970a504fa1SIlya Leoshkevich * BPF map information 4980a504fa1SIlya Leoshkevich * @param info_len pointer to the size of *info*; on success updated with the 4990a504fa1SIlya Leoshkevich * number of bytes written to *info* 5000a504fa1SIlya Leoshkevich * @return 0, on success; negative error code, otherwise (errno is also set to 5010a504fa1SIlya Leoshkevich * the error code) 5020a504fa1SIlya Leoshkevich */ 50355a9ed0eSIlya Leoshkevich LIBBPF_API int bpf_map_get_info_by_fd(int map_fd, struct bpf_map_info *info, __u32 *info_len); 5040a504fa1SIlya Leoshkevich 5050a504fa1SIlya Leoshkevich /** 5060a504fa1SIlya Leoshkevich * @brief **bpf_btf_get_info_by_fd()** obtains information about the 5070a504fa1SIlya Leoshkevich * BTF object corresponding to *btf_fd*. 5080a504fa1SIlya Leoshkevich * 5090a504fa1SIlya Leoshkevich * Populates up to *info_len* bytes of *info* and updates *info_len* with the 5100a504fa1SIlya Leoshkevich * actual number of bytes written to *info*. 5110a504fa1SIlya Leoshkevich * 5120a504fa1SIlya Leoshkevich * @param btf_fd BTF object file descriptor 5130a504fa1SIlya Leoshkevich * @param info pointer to **struct bpf_btf_info** that will be populated with 5140a504fa1SIlya Leoshkevich * BTF object information 5150a504fa1SIlya Leoshkevich * @param info_len pointer to the size of *info*; on success updated with the 5160a504fa1SIlya Leoshkevich * number of bytes written to *info* 5170a504fa1SIlya Leoshkevich * @return 0, on success; negative error code, otherwise (errno is also set to 5180a504fa1SIlya Leoshkevich * the error code) 5190a504fa1SIlya Leoshkevich */ 52055a9ed0eSIlya Leoshkevich LIBBPF_API int bpf_btf_get_info_by_fd(int btf_fd, struct bpf_btf_info *info, __u32 *info_len); 5210a504fa1SIlya Leoshkevich 5220a504fa1SIlya Leoshkevich /** 5230a504fa1SIlya Leoshkevich * @brief **bpf_btf_get_info_by_fd()** obtains information about the BPF 5240a504fa1SIlya Leoshkevich * link corresponding to *link_fd*. 5250a504fa1SIlya Leoshkevich * 5260a504fa1SIlya Leoshkevich * Populates up to *info_len* bytes of *info* and updates *info_len* with the 5270a504fa1SIlya Leoshkevich * actual number of bytes written to *info*. 5280a504fa1SIlya Leoshkevich * 5290a504fa1SIlya Leoshkevich * @param link_fd BPF link file descriptor 5300a504fa1SIlya Leoshkevich * @param info pointer to **struct bpf_link_info** that will be populated with 5310a504fa1SIlya Leoshkevich * BPF link information 5320a504fa1SIlya Leoshkevich * @param info_len pointer to the size of *info*; on success updated with the 5330a504fa1SIlya Leoshkevich * number of bytes written to *info* 5340a504fa1SIlya Leoshkevich * @return 0, on success; negative error code, otherwise (errno is also set to 5350a504fa1SIlya Leoshkevich * the error code) 5360a504fa1SIlya Leoshkevich */ 53755a9ed0eSIlya Leoshkevich LIBBPF_API int bpf_link_get_info_by_fd(int link_fd, struct bpf_link_info *info, __u32 *info_len); 538a4b2f3cfSStanislav Fomichev 539a4b2f3cfSStanislav Fomichev struct bpf_prog_query_opts { 540a4b2f3cfSStanislav Fomichev size_t sz; /* size of this struct for forward/backward compatibility */ 541a4b2f3cfSStanislav Fomichev __u32 query_flags; 542a4b2f3cfSStanislav Fomichev __u32 attach_flags; /* output argument */ 543a4b2f3cfSStanislav Fomichev __u32 *prog_ids; 544*fe20ce3aSDaniel Borkmann union { 545*fe20ce3aSDaniel Borkmann /* input+output argument */ 546*fe20ce3aSDaniel Borkmann __u32 prog_cnt; 547*fe20ce3aSDaniel Borkmann __u32 count; 548a4b2f3cfSStanislav Fomichev }; 549*fe20ce3aSDaniel Borkmann __u32 *prog_attach_flags; 550*fe20ce3aSDaniel Borkmann __u32 *link_ids; 551*fe20ce3aSDaniel Borkmann __u32 *link_attach_flags; 552*fe20ce3aSDaniel Borkmann __u64 revision; 553*fe20ce3aSDaniel Borkmann size_t :0; 554*fe20ce3aSDaniel Borkmann }; 555*fe20ce3aSDaniel Borkmann #define bpf_prog_query_opts__last_field revision 556a4b2f3cfSStanislav Fomichev 557*fe20ce3aSDaniel Borkmann /** 558*fe20ce3aSDaniel Borkmann * @brief **bpf_prog_query_opts()** queries the BPF programs and BPF links 559*fe20ce3aSDaniel Borkmann * which are attached to *target* which can represent a file descriptor or 560*fe20ce3aSDaniel Borkmann * netdevice ifindex. 561*fe20ce3aSDaniel Borkmann * 562*fe20ce3aSDaniel Borkmann * @param target query location file descriptor or ifindex 563*fe20ce3aSDaniel Borkmann * @param type attach type for the BPF program 564*fe20ce3aSDaniel Borkmann * @param opts options for configuring the query 565*fe20ce3aSDaniel Borkmann * @return 0, on success; negative error code, otherwise (errno is also set to 566*fe20ce3aSDaniel Borkmann * the error code) 567*fe20ce3aSDaniel Borkmann */ 568*fe20ce3aSDaniel Borkmann LIBBPF_API int bpf_prog_query_opts(int target, enum bpf_attach_type type, 569a4b2f3cfSStanislav Fomichev struct bpf_prog_query_opts *opts); 570ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type, 571ab9e0848SAndrey Ignatov __u32 query_flags, __u32 *attach_flags, 572ab9e0848SAndrey Ignatov __u32 *prog_ids, __u32 *prog_cnt); 573a4b2f3cfSStanislav Fomichev 574ab9e0848SAndrey Ignatov LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd); 575ab9e0848SAndrey Ignatov LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, 576ab9e0848SAndrey Ignatov __u32 *buf_len, __u32 *prog_id, __u32 *fd_type, 577ab9e0848SAndrey Ignatov __u64 *probe_offset, __u64 *probe_addr); 5784e15507fSAndrii Nakryiko 579b4269341SAndrii Nakryiko #ifdef __cplusplus 580b4269341SAndrii Nakryiko /* forward-declaring enums in C++ isn't compatible with pure C enums, so 581b4269341SAndrii Nakryiko * instead define bpf_enable_stats() as accepting int as an input 582b4269341SAndrii Nakryiko */ 583b4269341SAndrii Nakryiko LIBBPF_API int bpf_enable_stats(int type); 584b4269341SAndrii Nakryiko #else 5854e15507fSAndrii Nakryiko enum bpf_stats_type; /* defined in up-to-date linux/bpf.h */ 5860bee1067SSong Liu LIBBPF_API int bpf_enable_stats(enum bpf_stats_type type); 587b4269341SAndrii Nakryiko #endif 5888c4905b9SStanislav Fomichev 5895d23328dSYiFei Zhu struct bpf_prog_bind_opts { 5905d23328dSYiFei Zhu size_t sz; /* size of this struct for forward/backward compatibility */ 5915d23328dSYiFei Zhu __u32 flags; 5925d23328dSYiFei Zhu }; 5935d23328dSYiFei Zhu #define bpf_prog_bind_opts__last_field flags 5945d23328dSYiFei Zhu 5955d23328dSYiFei Zhu LIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd, 5965d23328dSYiFei Zhu const struct bpf_prog_bind_opts *opts); 59788f7fe72SSong Liu 59888f7fe72SSong Liu struct bpf_test_run_opts { 59988f7fe72SSong Liu size_t sz; /* size of this struct for forward/backward compatibility */ 60088f7fe72SSong Liu const void *data_in; /* optional */ 60188f7fe72SSong Liu void *data_out; /* optional */ 60288f7fe72SSong Liu __u32 data_size_in; 60388f7fe72SSong Liu __u32 data_size_out; /* in: max length of data_out 60488f7fe72SSong Liu * out: length of data_out 60588f7fe72SSong Liu */ 60688f7fe72SSong Liu const void *ctx_in; /* optional */ 60788f7fe72SSong Liu void *ctx_out; /* optional */ 60888f7fe72SSong Liu __u32 ctx_size_in; 60988f7fe72SSong Liu __u32 ctx_size_out; /* in: max length of ctx_out 61088f7fe72SSong Liu * out: length of cxt_out 61188f7fe72SSong Liu */ 61288f7fe72SSong Liu __u32 retval; /* out: return code of the BPF program */ 61388f7fe72SSong Liu int repeat; 61488f7fe72SSong Liu __u32 duration; /* out: average per repetition in ns */ 61588f7fe72SSong Liu __u32 flags; 61688f7fe72SSong Liu __u32 cpu; 61724592ad1SToke Høiland-Jørgensen __u32 batch_size; 61888f7fe72SSong Liu }; 61924592ad1SToke Høiland-Jørgensen #define bpf_test_run_opts__last_field batch_size 62088f7fe72SSong Liu 62188f7fe72SSong Liu LIBBPF_API int bpf_prog_test_run_opts(int prog_fd, 62288f7fe72SSong Liu struct bpf_test_run_opts *opts); 62388f7fe72SSong Liu 6248c4905b9SStanislav Fomichev #ifdef __cplusplus 6258c4905b9SStanislav Fomichev } /* extern "C" */ 6268c4905b9SStanislav Fomichev #endif 6278c4905b9SStanislav Fomichev 628eff81908SAndrey Ignatov #endif /* __LIBBPF_BPF_H */ 629