bpf.h 25 KB

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