bpf.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
  2. /*
  3. * Common BPF ELF operations.
  4. *
  5. * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
  6. * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
  7. * Copyright (C) 2015 Huawei Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License (not later!)
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this program; if not, see <http://www.gnu.org/licenses>
  21. */
  22. #ifndef __LIBBPF_BPF_H
  23. #define __LIBBPF_BPF_H
  24. #include <linux/bpf.h>
  25. #include <stdbool.h>
  26. #include <stddef.h>
  27. #include <stdint.h>
  28. #include "libbpf_common.h"
  29. #include "libbpf_legacy.h"
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. LIBBPF_API int libbpf_set_memlock_rlim(size_t memlock_bytes);
  34. struct bpf_map_create_opts {
  35. size_t sz; /* size of this struct for forward/backward compatibility */
  36. __u32 btf_fd;
  37. __u32 btf_key_type_id;
  38. __u32 btf_value_type_id;
  39. __u32 btf_vmlinux_value_type_id;
  40. __u32 inner_map_fd;
  41. __u32 map_flags;
  42. __u64 map_extra;
  43. __u32 numa_node;
  44. __u32 map_ifindex;
  45. __s32 value_type_btf_obj_fd;
  46. __u32 token_fd;
  47. const void *excl_prog_hash;
  48. __u32 excl_prog_hash_size;
  49. size_t :0;
  50. };
  51. #define bpf_map_create_opts__last_field excl_prog_hash_size
  52. LIBBPF_API int bpf_map_create(enum bpf_map_type map_type,
  53. const char *map_name,
  54. __u32 key_size,
  55. __u32 value_size,
  56. __u32 max_entries,
  57. const struct bpf_map_create_opts *opts);
  58. struct bpf_prog_load_opts {
  59. size_t sz; /* size of this struct for forward/backward compatibility */
  60. /* libbpf can retry BPF_PROG_LOAD command if bpf() syscall returns
  61. * -EAGAIN. This field determines how many attempts libbpf has to
  62. * make. If not specified, libbpf will use default value of 5.
  63. */
  64. int attempts;
  65. enum bpf_attach_type expected_attach_type;
  66. __u32 prog_btf_fd;
  67. __u32 prog_flags;
  68. __u32 prog_ifindex;
  69. __u32 kern_version;
  70. __u32 attach_btf_id;
  71. __u32 attach_prog_fd;
  72. __u32 attach_btf_obj_fd;
  73. const int *fd_array;
  74. /* .BTF.ext func info data */
  75. const void *func_info;
  76. __u32 func_info_cnt;
  77. __u32 func_info_rec_size;
  78. /* .BTF.ext line info data */
  79. const void *line_info;
  80. __u32 line_info_cnt;
  81. __u32 line_info_rec_size;
  82. /* verifier log options */
  83. __u32 log_level;
  84. __u32 log_size;
  85. char *log_buf;
  86. /* output: actual total log contents size (including terminating zero).
  87. * It could be both larger than original log_size (if log was
  88. * truncated), or smaller (if log buffer wasn't filled completely).
  89. * If kernel doesn't support this feature, log_size is left unchanged.
  90. */
  91. __u32 log_true_size;
  92. __u32 token_fd;
  93. /* if set, provides the length of fd_array */
  94. __u32 fd_array_cnt;
  95. size_t :0;
  96. };
  97. #define bpf_prog_load_opts__last_field fd_array_cnt
  98. LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type,
  99. const char *prog_name, const char *license,
  100. const struct bpf_insn *insns, size_t insn_cnt,
  101. struct bpf_prog_load_opts *opts);
  102. /* Flags to direct loading requirements */
  103. #define MAPS_RELAX_COMPAT 0x01
  104. /* Recommended log buffer size */
  105. #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */
  106. struct bpf_btf_load_opts {
  107. size_t sz; /* size of this struct for forward/backward compatibility */
  108. /* kernel log options */
  109. char *log_buf;
  110. __u32 log_level;
  111. __u32 log_size;
  112. /* output: actual total log contents size (including terminating zero).
  113. * It could be both larger than original log_size (if log was
  114. * truncated), or smaller (if log buffer wasn't filled completely).
  115. * If kernel doesn't support this feature, log_size is left unchanged.
  116. */
  117. __u32 log_true_size;
  118. __u32 btf_flags;
  119. __u32 token_fd;
  120. size_t :0;
  121. };
  122. #define bpf_btf_load_opts__last_field token_fd
  123. LIBBPF_API int bpf_btf_load(const void *btf_data, size_t btf_size,
  124. struct bpf_btf_load_opts *opts);
  125. LIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value,
  126. __u64 flags);
  127. LIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value);
  128. LIBBPF_API int bpf_map_lookup_elem_flags(int fd, const void *key, void *value,
  129. __u64 flags);
  130. LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key,
  131. void *value);
  132. LIBBPF_API int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key,
  133. void *value, __u64 flags);
  134. LIBBPF_API int bpf_map_delete_elem(int fd, const void *key);
  135. LIBBPF_API int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags);
  136. LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key);
  137. LIBBPF_API int bpf_map_freeze(int fd);
  138. struct bpf_map_batch_opts {
  139. size_t sz; /* size of this struct for forward/backward compatibility */
  140. __u64 elem_flags;
  141. __u64 flags;
  142. };
  143. #define bpf_map_batch_opts__last_field flags
  144. /**
  145. * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple
  146. * elements in a BPF map.
  147. *
  148. * @param fd BPF map file descriptor
  149. * @param keys pointer to an array of *count* keys
  150. * @param count input and output parameter; on input **count** represents the
  151. * number of elements in the map to delete in batch;
  152. * on output if a non-EFAULT error is returned, **count** represents the number of deleted
  153. * elements if the output **count** value is not equal to the input **count** value
  154. * If EFAULT is returned, **count** should not be trusted to be correct.
  155. * @param opts options for configuring the way the batch deletion works
  156. * @return 0, on success; negative error code, otherwise (errno is also set to
  157. * the error code)
  158. */
  159. LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys,
  160. __u32 *count,
  161. const struct bpf_map_batch_opts *opts);
  162. /**
  163. * @brief **bpf_map_lookup_batch()** allows for batch lookup of BPF map elements.
  164. *
  165. * The parameter *in_batch* is the address of the first element in the batch to
  166. * read. *out_batch* is an output parameter that should be passed as *in_batch*
  167. * to subsequent calls to **bpf_map_lookup_batch()**. NULL can be passed for
  168. * *in_batch* to indicate that the batched lookup starts from the beginning of
  169. * the map. Both *in_batch* and *out_batch* must point to memory large enough to
  170. * hold a single key, except for maps of type **BPF_MAP_TYPE_{HASH, PERCPU_HASH,
  171. * LRU_HASH, LRU_PERCPU_HASH}**, for which the memory size must be at
  172. * least 4 bytes wide regardless of key size.
  173. *
  174. * The *keys* and *values* are output parameters which must point to memory large enough to
  175. * hold *count* items based on the key and value size of the map *map_fd*. The *keys*
  176. * buffer must be of *key_size* * *count*. The *values* buffer must be of
  177. * *value_size* * *count*.
  178. *
  179. * @param fd BPF map file descriptor
  180. * @param in_batch address of the first element in batch to read, can pass NULL to
  181. * indicate that the batched lookup starts from the beginning of the map.
  182. * @param out_batch output parameter that should be passed to next call as *in_batch*
  183. * @param keys pointer to an array large enough for *count* keys
  184. * @param values pointer to an array large enough for *count* values
  185. * @param count input and output parameter; on input it's the number of elements
  186. * in the map to read in batch; on output it's the number of elements that were
  187. * successfully read.
  188. * If a non-EFAULT error is returned, count will be set as the number of elements
  189. * that were read before the error occurred.
  190. * If EFAULT is returned, **count** should not be trusted to be correct.
  191. * @param opts options for configuring the way the batch lookup works
  192. * @return 0, on success; negative error code, otherwise (errno is also set to
  193. * the error code)
  194. */
  195. LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch,
  196. void *keys, void *values, __u32 *count,
  197. const struct bpf_map_batch_opts *opts);
  198. /**
  199. * @brief **bpf_map_lookup_and_delete_batch()** allows for batch lookup and deletion
  200. * of BPF map elements where each element is deleted after being retrieved.
  201. *
  202. * @param fd BPF map file descriptor
  203. * @param in_batch address of the first element in batch to read, can pass NULL to
  204. * get address of the first element in *out_batch*. If not NULL, must be large
  205. * enough to hold a key. For **BPF_MAP_TYPE_{HASH, PERCPU_HASH, LRU_HASH,
  206. * LRU_PERCPU_HASH}**, the memory size must be at least 4 bytes wide regardless
  207. * of key size.
  208. * @param out_batch output parameter that should be passed to next call as *in_batch*
  209. * @param keys pointer to an array of *count* keys
  210. * @param values pointer to an array large enough for *count* values
  211. * @param count input and output parameter; on input it's the number of elements
  212. * in the map to read and delete in batch; on output it represents the number of
  213. * elements that were successfully read and deleted
  214. * If a non-**EFAULT** error code is returned and if the output **count** value
  215. * is not equal to the input **count** value, up to **count** elements may
  216. * have been deleted.
  217. * if **EFAULT** is returned up to *count* elements may have been deleted without
  218. * being returned via the *keys* and *values* output parameters.
  219. * @param opts options for configuring the way the batch lookup and delete works
  220. * @return 0, on success; negative error code, otherwise (errno is also set to
  221. * the error code)
  222. */
  223. LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch,
  224. void *out_batch, void *keys,
  225. void *values, __u32 *count,
  226. const struct bpf_map_batch_opts *opts);
  227. /**
  228. * @brief **bpf_map_update_batch()** updates multiple elements in a map
  229. * by specifying keys and their corresponding values.
  230. *
  231. * The *keys* and *values* parameters must point to memory large enough
  232. * to hold *count* items based on the key and value size of the map.
  233. *
  234. * The *opts* parameter can be used to control how *bpf_map_update_batch()*
  235. * should handle keys that either do or do not already exist in the map.
  236. * In particular the *flags* parameter of *bpf_map_batch_opts* can be
  237. * one of the following:
  238. *
  239. * Note that *count* is an input and output parameter, where on output it
  240. * represents how many elements were successfully updated. Also note that if
  241. * **EFAULT** then *count* should not be trusted to be correct.
  242. *
  243. * **BPF_ANY**
  244. * Create new elements or update existing.
  245. *
  246. * **BPF_NOEXIST**
  247. * Create new elements only if they do not exist.
  248. *
  249. * **BPF_EXIST**
  250. * Update existing elements.
  251. *
  252. * **BPF_F_LOCK**
  253. * Update spin_lock-ed map elements. This must be
  254. * specified if the map value contains a spinlock.
  255. *
  256. * **BPF_F_CPU**
  257. * As for percpu maps, update value on the specified CPU. And the cpu
  258. * info is embedded into the high 32 bits of **opts->elem_flags**.
  259. *
  260. * **BPF_F_ALL_CPUS**
  261. * As for percpu maps, update value across all CPUs. This flag cannot
  262. * be used with BPF_F_CPU at the same time.
  263. *
  264. * @param fd BPF map file descriptor
  265. * @param keys pointer to an array of *count* keys
  266. * @param values pointer to an array of *count* values
  267. * @param count input and output parameter; on input it's the number of elements
  268. * in the map to update in batch; on output if a non-EFAULT error is returned,
  269. * **count** represents the number of updated elements if the output **count**
  270. * value is not equal to the input **count** value.
  271. * If EFAULT is returned, **count** should not be trusted to be correct.
  272. * @param opts options for configuring the way the batch update works
  273. * @return 0, on success; negative error code, otherwise (errno is also set to
  274. * the error code)
  275. */
  276. LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values,
  277. __u32 *count,
  278. const struct bpf_map_batch_opts *opts);
  279. struct bpf_obj_pin_opts {
  280. size_t sz; /* size of this struct for forward/backward compatibility */
  281. __u32 file_flags;
  282. int path_fd;
  283. size_t :0;
  284. };
  285. #define bpf_obj_pin_opts__last_field path_fd
  286. LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
  287. LIBBPF_API int bpf_obj_pin_opts(int fd, const char *pathname,
  288. const struct bpf_obj_pin_opts *opts);
  289. struct bpf_obj_get_opts {
  290. size_t sz; /* size of this struct for forward/backward compatibility */
  291. __u32 file_flags;
  292. int path_fd;
  293. size_t :0;
  294. };
  295. #define bpf_obj_get_opts__last_field path_fd
  296. LIBBPF_API int bpf_obj_get(const char *pathname);
  297. LIBBPF_API int bpf_obj_get_opts(const char *pathname,
  298. const struct bpf_obj_get_opts *opts);
  299. LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
  300. enum bpf_attach_type type, unsigned int flags);
  301. LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
  302. LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
  303. enum bpf_attach_type type);
  304. struct bpf_prog_attach_opts {
  305. size_t sz; /* size of this struct for forward/backward compatibility */
  306. __u32 flags;
  307. union {
  308. int replace_prog_fd;
  309. int replace_fd;
  310. };
  311. int relative_fd;
  312. __u32 relative_id;
  313. __u64 expected_revision;
  314. size_t :0;
  315. };
  316. #define bpf_prog_attach_opts__last_field expected_revision
  317. struct bpf_prog_detach_opts {
  318. size_t sz; /* size of this struct for forward/backward compatibility */
  319. __u32 flags;
  320. int relative_fd;
  321. __u32 relative_id;
  322. __u64 expected_revision;
  323. size_t :0;
  324. };
  325. #define bpf_prog_detach_opts__last_field expected_revision
  326. /**
  327. * @brief **bpf_prog_attach_opts()** attaches the BPF program corresponding to
  328. * *prog_fd* to a *target* which can represent a file descriptor or netdevice
  329. * ifindex.
  330. *
  331. * @param prog_fd BPF program file descriptor
  332. * @param target attach location file descriptor or ifindex
  333. * @param type attach type for the BPF program
  334. * @param opts options for configuring the attachment
  335. * @return 0, on success; negative error code, otherwise (errno is also set to
  336. * the error code)
  337. */
  338. LIBBPF_API int bpf_prog_attach_opts(int prog_fd, int target,
  339. enum bpf_attach_type type,
  340. const struct bpf_prog_attach_opts *opts);
  341. /**
  342. * @brief **bpf_prog_detach_opts()** detaches the BPF program corresponding to
  343. * *prog_fd* from a *target* which can represent a file descriptor or netdevice
  344. * ifindex.
  345. *
  346. * @param prog_fd BPF program file descriptor
  347. * @param target detach location file descriptor or ifindex
  348. * @param type detach type for the BPF program
  349. * @param opts options for configuring the detachment
  350. * @return 0, on success; negative error code, otherwise (errno is also set to
  351. * the error code)
  352. */
  353. LIBBPF_API int bpf_prog_detach_opts(int prog_fd, int target,
  354. enum bpf_attach_type type,
  355. const struct bpf_prog_detach_opts *opts);
  356. union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */
  357. struct bpf_link_create_opts {
  358. size_t sz; /* size of this struct for forward/backward compatibility */
  359. __u32 flags;
  360. union bpf_iter_link_info *iter_info;
  361. __u32 iter_info_len;
  362. __u32 target_btf_id;
  363. union {
  364. struct {
  365. __u64 bpf_cookie;
  366. } perf_event;
  367. struct {
  368. __u32 flags;
  369. __u32 cnt;
  370. const char **syms;
  371. const unsigned long *addrs;
  372. const __u64 *cookies;
  373. } kprobe_multi;
  374. struct {
  375. __u32 flags;
  376. __u32 cnt;
  377. const char *path;
  378. const unsigned long *offsets;
  379. const unsigned long *ref_ctr_offsets;
  380. const __u64 *cookies;
  381. __u32 pid;
  382. } uprobe_multi;
  383. struct {
  384. __u64 cookie;
  385. } tracing;
  386. struct {
  387. __u32 pf;
  388. __u32 hooknum;
  389. __s32 priority;
  390. __u32 flags;
  391. } netfilter;
  392. struct {
  393. __u32 relative_fd;
  394. __u32 relative_id;
  395. __u64 expected_revision;
  396. } tcx;
  397. struct {
  398. __u32 relative_fd;
  399. __u32 relative_id;
  400. __u64 expected_revision;
  401. } netkit;
  402. struct {
  403. __u32 relative_fd;
  404. __u32 relative_id;
  405. __u64 expected_revision;
  406. } cgroup;
  407. };
  408. size_t :0;
  409. };
  410. #define bpf_link_create_opts__last_field uprobe_multi.pid
  411. LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
  412. enum bpf_attach_type attach_type,
  413. const struct bpf_link_create_opts *opts);
  414. LIBBPF_API int bpf_link_detach(int link_fd);
  415. struct bpf_link_update_opts {
  416. size_t sz; /* size of this struct for forward/backward compatibility */
  417. __u32 flags; /* extra flags */
  418. __u32 old_prog_fd; /* expected old program FD */
  419. __u32 old_map_fd; /* expected old map FD */
  420. };
  421. #define bpf_link_update_opts__last_field old_map_fd
  422. LIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd,
  423. const struct bpf_link_update_opts *opts);
  424. LIBBPF_API int bpf_iter_create(int link_fd);
  425. struct bpf_prog_test_run_attr {
  426. int prog_fd;
  427. int repeat;
  428. const void *data_in;
  429. __u32 data_size_in;
  430. void *data_out; /* optional */
  431. __u32 data_size_out; /* in: max length of data_out
  432. * out: length of data_out */
  433. __u32 retval; /* out: return code of the BPF program */
  434. __u32 duration; /* out: average per repetition in ns */
  435. const void *ctx_in; /* optional */
  436. __u32 ctx_size_in;
  437. void *ctx_out; /* optional */
  438. __u32 ctx_size_out; /* in: max length of ctx_out
  439. * out: length of cxt_out */
  440. };
  441. LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id);
  442. LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
  443. LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id);
  444. LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id);
  445. struct bpf_get_fd_by_id_opts {
  446. size_t sz; /* size of this struct for forward/backward compatibility */
  447. __u32 open_flags; /* permissions requested for the operation on fd */
  448. __u32 token_fd;
  449. size_t :0;
  450. };
  451. #define bpf_get_fd_by_id_opts__last_field token_fd
  452. LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
  453. LIBBPF_API int bpf_prog_get_fd_by_id_opts(__u32 id,
  454. const struct bpf_get_fd_by_id_opts *opts);
  455. LIBBPF_API int bpf_map_get_fd_by_id(__u32 id);
  456. LIBBPF_API int bpf_map_get_fd_by_id_opts(__u32 id,
  457. const struct bpf_get_fd_by_id_opts *opts);
  458. LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id);
  459. LIBBPF_API int bpf_btf_get_fd_by_id_opts(__u32 id,
  460. const struct bpf_get_fd_by_id_opts *opts);
  461. LIBBPF_API int bpf_link_get_fd_by_id(__u32 id);
  462. LIBBPF_API int bpf_link_get_fd_by_id_opts(__u32 id,
  463. const struct bpf_get_fd_by_id_opts *opts);
  464. LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len);
  465. /**
  466. * @brief **bpf_prog_get_info_by_fd()** obtains information about the BPF
  467. * program corresponding to *prog_fd*.
  468. *
  469. * Populates up to *info_len* bytes of *info* and updates *info_len* with the
  470. * actual number of bytes written to *info*. Note that *info* should be
  471. * zero-initialized or initialized as expected by the requested *info*
  472. * type. Failing to (zero-)initialize *info* under certain circumstances can
  473. * result in this helper returning an error.
  474. *
  475. * @param prog_fd BPF program file descriptor
  476. * @param info pointer to **struct bpf_prog_info** that will be populated with
  477. * BPF program information
  478. * @param info_len pointer to the size of *info*; on success updated with the
  479. * number of bytes written to *info*
  480. * @return 0, on success; negative error code, otherwise (errno is also set to
  481. * the error code)
  482. */
  483. LIBBPF_API int bpf_prog_get_info_by_fd(int prog_fd, struct bpf_prog_info *info, __u32 *info_len);
  484. /**
  485. * @brief **bpf_map_get_info_by_fd()** obtains information about the BPF
  486. * map corresponding to *map_fd*.
  487. *
  488. * Populates up to *info_len* bytes of *info* and updates *info_len* with the
  489. * actual number of bytes written to *info*. Note that *info* should be
  490. * zero-initialized or initialized as expected by the requested *info*
  491. * type. Failing to (zero-)initialize *info* under certain circumstances can
  492. * result in this helper returning an error.
  493. *
  494. * @param map_fd BPF map file descriptor
  495. * @param info pointer to **struct bpf_map_info** that will be populated with
  496. * BPF map information
  497. * @param info_len pointer to the size of *info*; on success updated with the
  498. * number of bytes written to *info*
  499. * @return 0, on success; negative error code, otherwise (errno is also set to
  500. * the error code)
  501. */
  502. LIBBPF_API int bpf_map_get_info_by_fd(int map_fd, struct bpf_map_info *info, __u32 *info_len);
  503. /**
  504. * @brief **bpf_btf_get_info_by_fd()** obtains information about the
  505. * BTF object corresponding to *btf_fd*.
  506. *
  507. * Populates up to *info_len* bytes of *info* and updates *info_len* with the
  508. * actual number of bytes written to *info*. Note that *info* should be
  509. * zero-initialized or initialized as expected by the requested *info*
  510. * type. Failing to (zero-)initialize *info* under certain circumstances can
  511. * result in this helper returning an error.
  512. *
  513. * @param btf_fd BTF object file descriptor
  514. * @param info pointer to **struct bpf_btf_info** that will be populated with
  515. * BTF object information
  516. * @param info_len pointer to the size of *info*; on success updated with the
  517. * number of bytes written to *info*
  518. * @return 0, on success; negative error code, otherwise (errno is also set to
  519. * the error code)
  520. */
  521. LIBBPF_API int bpf_btf_get_info_by_fd(int btf_fd, struct bpf_btf_info *info, __u32 *info_len);
  522. /**
  523. * @brief **bpf_btf_get_info_by_fd()** obtains information about the BPF
  524. * link corresponding to *link_fd*.
  525. *
  526. * Populates up to *info_len* bytes of *info* and updates *info_len* with the
  527. * actual number of bytes written to *info*. Note that *info* should be
  528. * zero-initialized or initialized as expected by the requested *info*
  529. * type. Failing to (zero-)initialize *info* under certain circumstances can
  530. * result in this helper returning an error.
  531. *
  532. * @param link_fd BPF link file descriptor
  533. * @param info pointer to **struct bpf_link_info** that will be populated with
  534. * BPF link information
  535. * @param info_len pointer to the size of *info*; on success updated with the
  536. * number of bytes written to *info*
  537. * @return 0, on success; negative error code, otherwise (errno is also set to
  538. * the error code)
  539. */
  540. LIBBPF_API int bpf_link_get_info_by_fd(int link_fd, struct bpf_link_info *info, __u32 *info_len);
  541. struct bpf_prog_query_opts {
  542. size_t sz; /* size of this struct for forward/backward compatibility */
  543. __u32 query_flags;
  544. __u32 attach_flags; /* output argument */
  545. __u32 *prog_ids;
  546. union {
  547. /* input+output argument */
  548. __u32 prog_cnt;
  549. __u32 count;
  550. };
  551. __u32 *prog_attach_flags;
  552. __u32 *link_ids;
  553. __u32 *link_attach_flags;
  554. __u64 revision;
  555. size_t :0;
  556. };
  557. #define bpf_prog_query_opts__last_field revision
  558. /**
  559. * @brief **bpf_prog_query_opts()** queries the BPF programs and BPF links
  560. * which are attached to *target* which can represent a file descriptor or
  561. * netdevice ifindex.
  562. *
  563. * @param target query location file descriptor or ifindex
  564. * @param type attach type for the BPF program
  565. * @param opts options for configuring the query
  566. * @return 0, on success; negative error code, otherwise (errno is also set to
  567. * the error code)
  568. */
  569. LIBBPF_API int bpf_prog_query_opts(int target, enum bpf_attach_type type,
  570. struct bpf_prog_query_opts *opts);
  571. LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type,
  572. __u32 query_flags, __u32 *attach_flags,
  573. __u32 *prog_ids, __u32 *prog_cnt);
  574. struct bpf_raw_tp_opts {
  575. size_t sz; /* size of this struct for forward/backward compatibility */
  576. const char *tp_name;
  577. __u64 cookie;
  578. size_t :0;
  579. };
  580. #define bpf_raw_tp_opts__last_field cookie
  581. LIBBPF_API int bpf_raw_tracepoint_open_opts(int prog_fd, struct bpf_raw_tp_opts *opts);
  582. LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd);
  583. LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf,
  584. __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
  585. __u64 *probe_offset, __u64 *probe_addr);
  586. #ifdef __cplusplus
  587. /* forward-declaring enums in C++ isn't compatible with pure C enums, so
  588. * instead define bpf_enable_stats() as accepting int as an input
  589. */
  590. LIBBPF_API int bpf_enable_stats(int type);
  591. #else
  592. enum bpf_stats_type; /* defined in up-to-date linux/bpf.h */
  593. LIBBPF_API int bpf_enable_stats(enum bpf_stats_type type);
  594. #endif
  595. struct bpf_prog_bind_opts {
  596. size_t sz; /* size of this struct for forward/backward compatibility */
  597. __u32 flags;
  598. };
  599. #define bpf_prog_bind_opts__last_field flags
  600. LIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd,
  601. const struct bpf_prog_bind_opts *opts);
  602. struct bpf_test_run_opts {
  603. size_t sz; /* size of this struct for forward/backward compatibility */
  604. const void *data_in; /* optional */
  605. void *data_out; /* optional */
  606. __u32 data_size_in;
  607. __u32 data_size_out; /* in: max length of data_out
  608. * out: length of data_out
  609. */
  610. const void *ctx_in; /* optional */
  611. void *ctx_out; /* optional */
  612. __u32 ctx_size_in;
  613. __u32 ctx_size_out; /* in: max length of ctx_out
  614. * out: length of cxt_out
  615. */
  616. __u32 retval; /* out: return code of the BPF program */
  617. int repeat;
  618. __u32 duration; /* out: average per repetition in ns */
  619. __u32 flags;
  620. __u32 cpu;
  621. __u32 batch_size;
  622. };
  623. #define bpf_test_run_opts__last_field batch_size
  624. LIBBPF_API int bpf_prog_test_run_opts(int prog_fd,
  625. struct bpf_test_run_opts *opts);
  626. struct bpf_token_create_opts {
  627. size_t sz; /* size of this struct for forward/backward compatibility */
  628. __u32 flags;
  629. size_t :0;
  630. };
  631. #define bpf_token_create_opts__last_field flags
  632. /**
  633. * @brief **bpf_token_create()** creates a new instance of BPF token derived
  634. * from specified BPF FS mount point.
  635. *
  636. * BPF token created with this API can be passed to bpf() syscall for
  637. * commands like BPF_PROG_LOAD, BPF_MAP_CREATE, etc.
  638. *
  639. * @param bpffs_fd FD for BPF FS instance from which to derive a BPF token
  640. * instance.
  641. * @param opts optional BPF token creation options, can be NULL
  642. *
  643. * @return BPF token FD > 0, on success; negative error code, otherwise (errno
  644. * is also set to the error code)
  645. */
  646. LIBBPF_API int bpf_token_create(int bpffs_fd,
  647. struct bpf_token_create_opts *opts);
  648. struct bpf_prog_stream_read_opts {
  649. size_t sz;
  650. size_t :0;
  651. };
  652. #define bpf_prog_stream_read_opts__last_field sz
  653. /**
  654. * @brief **bpf_prog_stream_read** reads data from the BPF stream of a given BPF
  655. * program.
  656. *
  657. * @param prog_fd FD for the BPF program whose BPF stream is to be read.
  658. * @param stream_id ID of the BPF stream to be read.
  659. * @param buf Buffer to read data into from the BPF stream.
  660. * @param buf_len Maximum number of bytes to read from the BPF stream.
  661. * @param opts optional options, can be NULL
  662. *
  663. * @return The number of bytes read, on success; negative error code, otherwise
  664. * (errno is also set to the error code)
  665. */
  666. LIBBPF_API int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len,
  667. struct bpf_prog_stream_read_opts *opts);
  668. struct bpf_prog_assoc_struct_ops_opts {
  669. size_t sz;
  670. __u32 flags;
  671. size_t :0;
  672. };
  673. #define bpf_prog_assoc_struct_ops_opts__last_field flags
  674. /**
  675. * @brief **bpf_prog_assoc_struct_ops** associates a BPF program with a
  676. * struct_ops map.
  677. *
  678. * @param prog_fd FD for the BPF program
  679. * @param map_fd FD for the struct_ops map to be associated with the BPF program
  680. * @param opts optional options, can be NULL
  681. *
  682. * @return 0 on success; negative error code, otherwise (errno is also set to
  683. * the error code)
  684. */
  685. LIBBPF_API int bpf_prog_assoc_struct_ops(int prog_fd, int map_fd,
  686. struct bpf_prog_assoc_struct_ops_opts *opts);
  687. #ifdef __cplusplus
  688. } /* extern "C" */
  689. #endif
  690. #endif /* __LIBBPF_BPF_H */