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 struct bpf_map_create_opts { 39 size_t sz; /* size of this struct for forward/backward compatibility */ 40 41 __u32 btf_fd; 42 __u32 btf_key_type_id; 43 __u32 btf_value_type_id; 44 __u32 btf_vmlinux_value_type_id; 45 46 __u32 inner_map_fd; 47 __u32 map_flags; 48 __u64 map_extra; 49 50 __u32 numa_node; 51 __u32 map_ifindex; 52 }; 53 #define bpf_map_create_opts__last_field map_ifindex 54 55 LIBBPF_API int bpf_map_create(enum bpf_map_type map_type, 56 const char *map_name, 57 __u32 key_size, 58 __u32 value_size, 59 __u32 max_entries, 60 const struct bpf_map_create_opts *opts); 61 62 struct bpf_create_map_attr { 63 const char *name; 64 enum bpf_map_type map_type; 65 __u32 map_flags; 66 __u32 key_size; 67 __u32 value_size; 68 __u32 max_entries; 69 __u32 numa_node; 70 __u32 btf_fd; 71 __u32 btf_key_type_id; 72 __u32 btf_value_type_id; 73 __u32 map_ifindex; 74 union { 75 __u32 inner_map_fd; 76 __u32 btf_vmlinux_value_type_id; 77 }; 78 }; 79 80 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead") 81 LIBBPF_API int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr); 82 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead") 83 LIBBPF_API int bpf_create_map_node(enum bpf_map_type map_type, const char *name, 84 int key_size, int value_size, 85 int max_entries, __u32 map_flags, int node); 86 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead") 87 LIBBPF_API int bpf_create_map_name(enum bpf_map_type map_type, const char *name, 88 int key_size, int value_size, 89 int max_entries, __u32 map_flags); 90 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead") 91 LIBBPF_API int bpf_create_map(enum bpf_map_type map_type, int key_size, 92 int value_size, int max_entries, __u32 map_flags); 93 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead") 94 LIBBPF_API int bpf_create_map_in_map_node(enum bpf_map_type map_type, 95 const char *name, int key_size, 96 int inner_map_fd, int max_entries, 97 __u32 map_flags, int node); 98 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead") 99 LIBBPF_API int bpf_create_map_in_map(enum bpf_map_type map_type, 100 const char *name, int key_size, 101 int inner_map_fd, int max_entries, 102 __u32 map_flags); 103 104 struct bpf_prog_load_opts { 105 size_t sz; /* size of this struct for forward/backward compatibility */ 106 107 /* libbpf can retry BPF_PROG_LOAD command if bpf() syscall returns 108 * -EAGAIN. This field determines how many attempts libbpf has to 109 * make. If not specified, libbpf will use default value of 5. 110 */ 111 int attempts; 112 113 enum bpf_attach_type expected_attach_type; 114 __u32 prog_btf_fd; 115 __u32 prog_flags; 116 __u32 prog_ifindex; 117 __u32 kern_version; 118 119 __u32 attach_btf_id; 120 __u32 attach_prog_fd; 121 __u32 attach_btf_obj_fd; 122 123 const int *fd_array; 124 125 /* .BTF.ext func info data */ 126 const void *func_info; 127 __u32 func_info_cnt; 128 __u32 func_info_rec_size; 129 130 /* .BTF.ext line info data */ 131 const void *line_info; 132 __u32 line_info_cnt; 133 __u32 line_info_rec_size; 134 135 /* verifier log options */ 136 __u32 log_level; 137 __u32 log_size; 138 char *log_buf; 139 }; 140 #define bpf_prog_load_opts__last_field log_buf 141 142 LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type, 143 const char *prog_name, const char *license, 144 const struct bpf_insn *insns, size_t insn_cnt, 145 const struct bpf_prog_load_opts *opts); 146 /* this "specialization" should go away in libbpf 1.0 */ 147 LIBBPF_API int bpf_prog_load_v0_6_0(enum bpf_prog_type prog_type, 148 const char *prog_name, const char *license, 149 const struct bpf_insn *insns, size_t insn_cnt, 150 const struct bpf_prog_load_opts *opts); 151 152 /* This is an elaborate way to not conflict with deprecated bpf_prog_load() 153 * API, defined in libbpf.h. Once we hit libbpf 1.0, all this will be gone. 154 * With this approach, if someone is calling bpf_prog_load() with 155 * 4 arguments, they will use the deprecated API, which keeps backwards 156 * compatibility (both source code and binary). If bpf_prog_load() is called 157 * with 6 arguments, though, it gets redirected to __bpf_prog_load. 158 * So looking forward to libbpf 1.0 when this hack will be gone and 159 * __bpf_prog_load() will be called just bpf_prog_load(). 160 */ 161 #ifndef bpf_prog_load 162 #define bpf_prog_load(...) ___libbpf_overload(___bpf_prog_load, __VA_ARGS__) 163 #define ___bpf_prog_load4(file, type, pobj, prog_fd) \ 164 bpf_prog_load_deprecated(file, type, pobj, prog_fd) 165 #define ___bpf_prog_load6(prog_type, prog_name, license, insns, insn_cnt, opts) \ 166 bpf_prog_load(prog_type, prog_name, license, insns, insn_cnt, opts) 167 #endif /* bpf_prog_load */ 168 169 struct bpf_load_program_attr { 170 enum bpf_prog_type prog_type; 171 enum bpf_attach_type expected_attach_type; 172 const char *name; 173 const struct bpf_insn *insns; 174 size_t insns_cnt; 175 const char *license; 176 union { 177 __u32 kern_version; 178 __u32 attach_prog_fd; 179 }; 180 union { 181 __u32 prog_ifindex; 182 __u32 attach_btf_id; 183 }; 184 __u32 prog_btf_fd; 185 __u32 func_info_rec_size; 186 const void *func_info; 187 __u32 func_info_cnt; 188 __u32 line_info_rec_size; 189 const void *line_info; 190 __u32 line_info_cnt; 191 __u32 log_level; 192 __u32 prog_flags; 193 }; 194 195 /* Flags to direct loading requirements */ 196 #define MAPS_RELAX_COMPAT 0x01 197 198 /* Recommended log buffer size */ 199 #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */ 200 201 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_load() instead") 202 LIBBPF_API int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, 203 char *log_buf, size_t log_buf_sz); 204 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_load() instead") 205 LIBBPF_API int bpf_load_program(enum bpf_prog_type type, 206 const struct bpf_insn *insns, size_t insns_cnt, 207 const char *license, __u32 kern_version, 208 char *log_buf, size_t log_buf_sz); 209 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_load() instead") 210 LIBBPF_API int bpf_verify_program(enum bpf_prog_type type, 211 const struct bpf_insn *insns, 212 size_t insns_cnt, __u32 prog_flags, 213 const char *license, __u32 kern_version, 214 char *log_buf, size_t log_buf_sz, 215 int log_level); 216 217 struct bpf_btf_load_opts { 218 size_t sz; /* size of this struct for forward/backward compatibility */ 219 220 /* kernel log options */ 221 char *log_buf; 222 __u32 log_level; 223 __u32 log_size; 224 }; 225 #define bpf_btf_load_opts__last_field log_size 226 227 LIBBPF_API int bpf_btf_load(const void *btf_data, size_t btf_size, 228 const struct bpf_btf_load_opts *opts); 229 230 LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_btf_load() instead") 231 LIBBPF_API int bpf_load_btf(const void *btf, __u32 btf_size, char *log_buf, 232 __u32 log_buf_size, bool do_log); 233 234 LIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value, 235 __u64 flags); 236 237 LIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value); 238 LIBBPF_API int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, 239 __u64 flags); 240 LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key, 241 void *value); 242 LIBBPF_API int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key, 243 void *value, __u64 flags); 244 LIBBPF_API int bpf_map_delete_elem(int fd, const void *key); 245 LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key); 246 LIBBPF_API int bpf_map_freeze(int fd); 247 248 struct bpf_map_batch_opts { 249 size_t sz; /* size of this struct for forward/backward compatibility */ 250 __u64 elem_flags; 251 __u64 flags; 252 }; 253 #define bpf_map_batch_opts__last_field flags 254 255 LIBBPF_API int bpf_map_delete_batch(int fd, void *keys, 256 __u32 *count, 257 const struct bpf_map_batch_opts *opts); 258 LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch, 259 void *keys, void *values, __u32 *count, 260 const struct bpf_map_batch_opts *opts); 261 LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, 262 void *out_batch, void *keys, 263 void *values, __u32 *count, 264 const struct bpf_map_batch_opts *opts); 265 LIBBPF_API int bpf_map_update_batch(int fd, void *keys, void *values, 266 __u32 *count, 267 const struct bpf_map_batch_opts *opts); 268 269 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname); 270 LIBBPF_API int bpf_obj_get(const char *pathname); 271 272 struct bpf_prog_attach_opts { 273 size_t sz; /* size of this struct for forward/backward compatibility */ 274 unsigned int flags; 275 int replace_prog_fd; 276 }; 277 #define bpf_prog_attach_opts__last_field replace_prog_fd 278 279 LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd, 280 enum bpf_attach_type type, unsigned int flags); 281 LIBBPF_API int bpf_prog_attach_xattr(int prog_fd, int attachable_fd, 282 enum bpf_attach_type type, 283 const struct bpf_prog_attach_opts *opts); 284 LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type); 285 LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd, 286 enum bpf_attach_type type); 287 288 union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */ 289 struct bpf_link_create_opts { 290 size_t sz; /* size of this struct for forward/backward compatibility */ 291 __u32 flags; 292 union bpf_iter_link_info *iter_info; 293 __u32 iter_info_len; 294 __u32 target_btf_id; 295 union { 296 struct { 297 __u64 bpf_cookie; 298 } perf_event; 299 }; 300 size_t :0; 301 }; 302 #define bpf_link_create_opts__last_field perf_event 303 304 LIBBPF_API int bpf_link_create(int prog_fd, int target_fd, 305 enum bpf_attach_type attach_type, 306 const struct bpf_link_create_opts *opts); 307 308 LIBBPF_API int bpf_link_detach(int link_fd); 309 310 struct bpf_link_update_opts { 311 size_t sz; /* size of this struct for forward/backward compatibility */ 312 __u32 flags; /* extra flags */ 313 __u32 old_prog_fd; /* expected old program FD */ 314 }; 315 #define bpf_link_update_opts__last_field old_prog_fd 316 317 LIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd, 318 const struct bpf_link_update_opts *opts); 319 320 LIBBPF_API int bpf_iter_create(int link_fd); 321 322 struct bpf_prog_test_run_attr { 323 int prog_fd; 324 int repeat; 325 const void *data_in; 326 __u32 data_size_in; 327 void *data_out; /* optional */ 328 __u32 data_size_out; /* in: max length of data_out 329 * out: length of data_out */ 330 __u32 retval; /* out: return code of the BPF program */ 331 __u32 duration; /* out: average per repetition in ns */ 332 const void *ctx_in; /* optional */ 333 __u32 ctx_size_in; 334 void *ctx_out; /* optional */ 335 __u32 ctx_size_out; /* in: max length of ctx_out 336 * out: length of cxt_out */ 337 }; 338 339 LIBBPF_API int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr); 340 341 /* 342 * bpf_prog_test_run does not check that data_out is large enough. Consider 343 * using bpf_prog_test_run_xattr instead. 344 */ 345 LIBBPF_API int bpf_prog_test_run(int prog_fd, int repeat, void *data, 346 __u32 size, void *data_out, __u32 *size_out, 347 __u32 *retval, __u32 *duration); 348 LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id); 349 LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id); 350 LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id); 351 LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id); 352 LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id); 353 LIBBPF_API int bpf_map_get_fd_by_id(__u32 id); 354 LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id); 355 LIBBPF_API int bpf_link_get_fd_by_id(__u32 id); 356 LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len); 357 LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type, 358 __u32 query_flags, __u32 *attach_flags, 359 __u32 *prog_ids, __u32 *prog_cnt); 360 LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd); 361 LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, 362 __u32 *buf_len, __u32 *prog_id, __u32 *fd_type, 363 __u64 *probe_offset, __u64 *probe_addr); 364 365 enum bpf_stats_type; /* defined in up-to-date linux/bpf.h */ 366 LIBBPF_API int bpf_enable_stats(enum bpf_stats_type type); 367 368 struct bpf_prog_bind_opts { 369 size_t sz; /* size of this struct for forward/backward compatibility */ 370 __u32 flags; 371 }; 372 #define bpf_prog_bind_opts__last_field flags 373 374 LIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd, 375 const struct bpf_prog_bind_opts *opts); 376 377 struct bpf_test_run_opts { 378 size_t sz; /* size of this struct for forward/backward compatibility */ 379 const void *data_in; /* optional */ 380 void *data_out; /* optional */ 381 __u32 data_size_in; 382 __u32 data_size_out; /* in: max length of data_out 383 * out: length of data_out 384 */ 385 const void *ctx_in; /* optional */ 386 void *ctx_out; /* optional */ 387 __u32 ctx_size_in; 388 __u32 ctx_size_out; /* in: max length of ctx_out 389 * out: length of cxt_out 390 */ 391 __u32 retval; /* out: return code of the BPF program */ 392 int repeat; 393 __u32 duration; /* out: average per repetition in ns */ 394 __u32 flags; 395 __u32 cpu; 396 }; 397 #define bpf_test_run_opts__last_field cpu 398 399 LIBBPF_API int bpf_prog_test_run_opts(int prog_fd, 400 struct bpf_test_run_opts *opts); 401 402 #ifdef __cplusplus 403 } /* extern "C" */ 404 #endif 405 406 #endif /* __LIBBPF_BPF_H */ 407