libbpf.h 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  1. /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
  2. /*
  3. * Common eBPF ELF object loading 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. #ifndef __LIBBPF_LIBBPF_H
  10. #define __LIBBPF_LIBBPF_H
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <stdint.h>
  14. #include <stdbool.h>
  15. #include <sys/types.h> // for size_t
  16. #include <linux/bpf.h>
  17. #include "libbpf_common.h"
  18. #include "libbpf_legacy.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /**
  23. * @brief **libbpf_major_version()** provides the major version of libbpf.
  24. * @return An integer, the major version number
  25. */
  26. LIBBPF_API __u32 libbpf_major_version(void);
  27. /**
  28. * @brief **libbpf_minor_version()** provides the minor version of libbpf.
  29. * @return An integer, the minor version number
  30. */
  31. LIBBPF_API __u32 libbpf_minor_version(void);
  32. /**
  33. * @brief **libbpf_version_string()** provides the version of libbpf in a
  34. * human-readable form, e.g., "v1.7".
  35. * @return Pointer to a static string containing the version
  36. *
  37. * The format is *not* a part of a stable API and may change in the future.
  38. */
  39. LIBBPF_API const char *libbpf_version_string(void);
  40. enum libbpf_errno {
  41. __LIBBPF_ERRNO__START = 4000,
  42. /* Something wrong in libelf */
  43. LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
  44. LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */
  45. LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
  46. LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */
  47. LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
  48. LIBBPF_ERRNO__RELOC, /* Relocation failed */
  49. LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
  50. LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */
  51. LIBBPF_ERRNO__PROG2BIG, /* Program too big */
  52. LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
  53. LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
  54. LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */
  55. LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */
  56. LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */
  57. __LIBBPF_ERRNO__END,
  58. };
  59. /**
  60. * @brief **libbpf_strerror()** converts the provided error code into a
  61. * human-readable string.
  62. * @param err The error code to convert
  63. * @param buf Pointer to a buffer where the error message will be stored
  64. * @param size The number of bytes in the buffer
  65. * @return 0, on success; negative error code, otherwise
  66. */
  67. LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
  68. /**
  69. * @brief **libbpf_bpf_attach_type_str()** converts the provided attach type
  70. * value into a textual representation.
  71. * @param t The attach type.
  72. * @return Pointer to a static string identifying the attach type. NULL is
  73. * returned for unknown **bpf_attach_type** values.
  74. */
  75. LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t);
  76. /**
  77. * @brief **libbpf_bpf_link_type_str()** converts the provided link type value
  78. * into a textual representation.
  79. * @param t The link type.
  80. * @return Pointer to a static string identifying the link type. NULL is
  81. * returned for unknown **bpf_link_type** values.
  82. */
  83. LIBBPF_API const char *libbpf_bpf_link_type_str(enum bpf_link_type t);
  84. /**
  85. * @brief **libbpf_bpf_map_type_str()** converts the provided map type value
  86. * into a textual representation.
  87. * @param t The map type.
  88. * @return Pointer to a static string identifying the map type. NULL is
  89. * returned for unknown **bpf_map_type** values.
  90. */
  91. LIBBPF_API const char *libbpf_bpf_map_type_str(enum bpf_map_type t);
  92. /**
  93. * @brief **libbpf_bpf_prog_type_str()** converts the provided program type
  94. * value into a textual representation.
  95. * @param t The program type.
  96. * @return Pointer to a static string identifying the program type. NULL is
  97. * returned for unknown **bpf_prog_type** values.
  98. */
  99. LIBBPF_API const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t);
  100. enum libbpf_print_level {
  101. LIBBPF_WARN,
  102. LIBBPF_INFO,
  103. LIBBPF_DEBUG,
  104. };
  105. typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
  106. const char *, va_list ap);
  107. /**
  108. * @brief **libbpf_set_print()** sets user-provided log callback function to
  109. * be used for libbpf warnings and informational messages. If the user callback
  110. * is not set, messages are logged to stderr by default. The verbosity of these
  111. * messages can be controlled by setting the environment variable
  112. * LIBBPF_LOG_LEVEL to either warn, info, or debug.
  113. * @param fn The log print function. If NULL, libbpf won't print anything.
  114. * @return Pointer to old print function.
  115. *
  116. * This function is thread-safe.
  117. */
  118. LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
  119. /* Hide internal to user */
  120. struct bpf_object;
  121. struct bpf_object_open_opts {
  122. /* size of this struct, for forward/backward compatibility */
  123. size_t sz;
  124. /* object name override, if provided:
  125. * - for object open from file, this will override setting object
  126. * name from file path's base name;
  127. * - for object open from memory buffer, this will specify an object
  128. * name and will override default "<addr>-<buf-size>" name;
  129. */
  130. const char *object_name;
  131. /* parse map definitions non-strictly, allowing extra attributes/data */
  132. bool relaxed_maps;
  133. /* maps that set the 'pinning' attribute in their definition will have
  134. * their pin_path attribute set to a file in this directory, and be
  135. * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
  136. */
  137. const char *pin_root_path;
  138. __u32 :32; /* stub out now removed attach_prog_fd */
  139. /* Additional kernel config content that augments and overrides
  140. * system Kconfig for CONFIG_xxx externs.
  141. */
  142. const char *kconfig;
  143. /* Path to the custom BTF to be used for BPF CO-RE relocations.
  144. * This custom BTF completely replaces the use of vmlinux BTF
  145. * for the purpose of CO-RE relocations.
  146. * NOTE: any other BPF feature (e.g., fentry/fexit programs,
  147. * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.
  148. */
  149. const char *btf_custom_path;
  150. /* Pointer to a buffer for storing kernel logs for applicable BPF
  151. * commands. Valid kernel_log_size has to be specified as well and are
  152. * passed-through to bpf() syscall. Keep in mind that kernel might
  153. * fail operation with -ENOSPC error if provided buffer is too small
  154. * to contain entire log output.
  155. * See the comment below for kernel_log_level for interaction between
  156. * log_buf and log_level settings.
  157. *
  158. * If specified, this log buffer will be passed for:
  159. * - each BPF progral load (BPF_PROG_LOAD) attempt, unless overridden
  160. * with bpf_program__set_log() on per-program level, to get
  161. * BPF verifier log output.
  162. * - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get
  163. * BTF sanity checking log.
  164. *
  165. * Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite
  166. * previous contents, so if you need more fine-grained control, set
  167. * per-program buffer with bpf_program__set_log_buf() to preserve each
  168. * individual program's verification log. Keep using kernel_log_buf
  169. * for BTF verification log, if necessary.
  170. */
  171. char *kernel_log_buf;
  172. size_t kernel_log_size;
  173. /*
  174. * Log level can be set independently from log buffer. Log_level=0
  175. * means that libbpf will attempt loading BTF or program without any
  176. * logging requested, but will retry with either its own or custom log
  177. * buffer, if provided, and log_level=1 on any error.
  178. * And vice versa, setting log_level>0 will request BTF or prog
  179. * loading with verbose log from the first attempt (and as such also
  180. * for successfully loaded BTF or program), and the actual log buffer
  181. * could be either libbpf's own auto-allocated log buffer, if
  182. * kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.
  183. * If user didn't provide custom log buffer, libbpf will emit captured
  184. * logs through its print callback.
  185. */
  186. __u32 kernel_log_level;
  187. /* Path to BPF FS mount point to derive BPF token from.
  188. *
  189. * Created BPF token will be used for all bpf() syscall operations
  190. * that accept BPF token (e.g., map creation, BTF and program loads,
  191. * etc) automatically within instantiated BPF object.
  192. *
  193. * If bpf_token_path is not specified, libbpf will consult
  194. * LIBBPF_BPF_TOKEN_PATH environment variable. If set, it will be
  195. * taken as a value of bpf_token_path option and will force libbpf to
  196. * either create BPF token from provided custom BPF FS path, or will
  197. * disable implicit BPF token creation, if envvar value is an empty
  198. * string. bpf_token_path overrides LIBBPF_BPF_TOKEN_PATH, if both are
  199. * set at the same time.
  200. *
  201. * Setting bpf_token_path option to empty string disables libbpf's
  202. * automatic attempt to create BPF token from default BPF FS mount
  203. * point (/sys/fs/bpf), in case this default behavior is undesirable.
  204. */
  205. const char *bpf_token_path;
  206. size_t :0;
  207. };
  208. #define bpf_object_open_opts__last_field bpf_token_path
  209. /**
  210. * @brief **bpf_object__open()** creates a bpf_object by opening
  211. * the BPF ELF object file pointed to by the passed path and loading it
  212. * into memory.
  213. * @param path BPF object file path.
  214. * @return pointer to the new bpf_object; or NULL is returned on error,
  215. * error code is stored in errno
  216. */
  217. LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
  218. /**
  219. * @brief **bpf_object__open_file()** creates a bpf_object by opening
  220. * the BPF ELF object file pointed to by the passed path and loading it
  221. * into memory.
  222. * @param path BPF object file path
  223. * @param opts options for how to load the bpf object, this parameter is
  224. * optional and can be set to NULL
  225. * @return pointer to the new bpf_object; or NULL is returned on error,
  226. * error code is stored in errno
  227. */
  228. LIBBPF_API struct bpf_object *
  229. bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
  230. /**
  231. * @brief **bpf_object__open_mem()** creates a bpf_object by reading
  232. * the BPF objects raw bytes from a memory buffer containing a valid
  233. * BPF ELF object file.
  234. * @param obj_buf pointer to the buffer containing ELF file bytes
  235. * @param obj_buf_sz number of bytes in the buffer
  236. * @param opts options for how to load the bpf object
  237. * @return pointer to the new bpf_object; or NULL is returned on error,
  238. * error code is stored in errno
  239. */
  240. LIBBPF_API struct bpf_object *
  241. bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
  242. const struct bpf_object_open_opts *opts);
  243. /**
  244. * @brief **bpf_object__prepare()** prepares BPF object for loading:
  245. * performs ELF processing, relocations, prepares final state of BPF program
  246. * instructions (accessible with bpf_program__insns()), creates and
  247. * (potentially) pins maps. Leaves BPF object in the state ready for program
  248. * loading.
  249. * @param obj Pointer to a valid BPF object instance returned by
  250. * **bpf_object__open*()** API
  251. * @return 0, on success; negative error code, otherwise, error code is
  252. * stored in errno
  253. */
  254. LIBBPF_API int bpf_object__prepare(struct bpf_object *obj);
  255. /**
  256. * @brief **bpf_object__load()** loads BPF object into kernel.
  257. * @param obj Pointer to a valid BPF object instance returned by
  258. * **bpf_object__open*()** APIs
  259. * @return 0, on success; negative error code, otherwise, error code is
  260. * stored in errno
  261. */
  262. LIBBPF_API int bpf_object__load(struct bpf_object *obj);
  263. /**
  264. * @brief **bpf_object__close()** closes a BPF object and releases all
  265. * resources.
  266. * @param obj Pointer to a valid BPF object
  267. */
  268. LIBBPF_API void bpf_object__close(struct bpf_object *obj);
  269. /**
  270. * @brief **bpf_object__pin_maps()** pins each map contained within
  271. * the BPF object at the passed directory.
  272. * @param obj Pointer to a valid BPF object
  273. * @param path A directory where maps should be pinned.
  274. * @return 0, on success; negative error code, otherwise
  275. *
  276. * If `path` is NULL `bpf_map__pin` (which is being used on each map)
  277. * will use the pin_path attribute of each map. In this case, maps that
  278. * don't have a pin_path set will be ignored.
  279. */
  280. LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
  281. /**
  282. * @brief **bpf_object__unpin_maps()** unpins each map contained within
  283. * the BPF object found in the passed directory.
  284. * @param obj Pointer to a valid BPF object
  285. * @param path A directory where pinned maps should be searched for.
  286. * @return 0, on success; negative error code, otherwise
  287. *
  288. * If `path` is NULL `bpf_map__unpin` (which is being used on each map)
  289. * will use the pin_path attribute of each map. In this case, maps that
  290. * don't have a pin_path set will be ignored.
  291. */
  292. LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
  293. const char *path);
  294. LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
  295. const char *path);
  296. LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
  297. const char *path);
  298. LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
  299. LIBBPF_API int bpf_object__unpin(struct bpf_object *object, const char *path);
  300. LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
  301. LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
  302. LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);
  303. /**
  304. * @brief **bpf_object__token_fd** is an accessor for BPF token FD associated
  305. * with BPF object.
  306. * @param obj Pointer to a valid BPF object
  307. * @return BPF token FD or -1, if it wasn't set
  308. */
  309. LIBBPF_API int bpf_object__token_fd(const struct bpf_object *obj);
  310. struct btf;
  311. LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
  312. LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
  313. LIBBPF_API struct bpf_program *
  314. bpf_object__find_program_by_name(const struct bpf_object *obj,
  315. const char *name);
  316. LIBBPF_API int
  317. libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
  318. enum bpf_attach_type *expected_attach_type);
  319. LIBBPF_API int libbpf_attach_type_by_name(const char *name,
  320. enum bpf_attach_type *attach_type);
  321. LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
  322. enum bpf_attach_type attach_type);
  323. /* Accessors of bpf_program */
  324. struct bpf_program;
  325. LIBBPF_API struct bpf_program *
  326. bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);
  327. #define bpf_object__for_each_program(pos, obj) \
  328. for ((pos) = bpf_object__next_program((obj), NULL); \
  329. (pos) != NULL; \
  330. (pos) = bpf_object__next_program((obj), (pos)))
  331. LIBBPF_API struct bpf_program *
  332. bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);
  333. LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
  334. __u32 ifindex);
  335. LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
  336. LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
  337. LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
  338. LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
  339. LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog);
  340. LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach);
  341. struct bpf_insn;
  342. /**
  343. * @brief **bpf_program__insns()** gives read-only access to BPF program's
  344. * underlying BPF instructions.
  345. * @param prog BPF program for which to return instructions
  346. * @return a pointer to an array of BPF instructions that belong to the
  347. * specified BPF program
  348. *
  349. * Returned pointer is always valid and not NULL. Number of `struct bpf_insn`
  350. * pointed to can be fetched using **bpf_program__insn_cnt()** API.
  351. *
  352. * Keep in mind, libbpf can modify and append/delete BPF program's
  353. * instructions as it processes BPF object file and prepares everything for
  354. * uploading into the kernel. So depending on the point in BPF object
  355. * lifetime, **bpf_program__insns()** can return different sets of
  356. * instructions. As an example, during BPF object load phase BPF program
  357. * instructions will be CO-RE-relocated, BPF subprograms instructions will be
  358. * appended, ldimm64 instructions will have FDs embedded, etc. So instructions
  359. * returned before **bpf_object__load()** and after it might be quite
  360. * different.
  361. */
  362. LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
  363. /**
  364. * @brief **bpf_program__set_insns()** can set BPF program's underlying
  365. * BPF instructions.
  366. *
  367. * WARNING: This is a very advanced libbpf API and users need to know
  368. * what they are doing. This should be used from prog_prepare_load_fn
  369. * callback only.
  370. *
  371. * @param prog BPF program for which to return instructions
  372. * @param new_insns a pointer to an array of BPF instructions
  373. * @param new_insn_cnt number of `struct bpf_insn`'s that form
  374. * specified BPF program
  375. * @return 0, on success; negative error code, otherwise
  376. */
  377. LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog,
  378. struct bpf_insn *new_insns, size_t new_insn_cnt);
  379. /**
  380. * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
  381. * that form specified BPF program.
  382. * @param prog BPF program for which to return number of BPF instructions
  383. *
  384. * See **bpf_program__insns()** documentation for notes on how libbpf can
  385. * change instructions and their count during different phases of
  386. * **bpf_object** lifetime.
  387. */
  388. LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);
  389. LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
  390. /**
  391. * @brief **bpf_program__pin()** pins the BPF program to a file
  392. * in the BPF FS specified by a path. This increments the programs
  393. * reference count, allowing it to stay loaded after the process
  394. * which loaded it has exited.
  395. *
  396. * @param prog BPF program to pin, must already be loaded
  397. * @param path file path in a BPF file system
  398. * @return 0, on success; negative error code, otherwise
  399. */
  400. LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
  401. /**
  402. * @brief **bpf_program__unpin()** unpins the BPF program from a file
  403. * in the BPFFS specified by a path. This decrements program's in-kernel
  404. * reference count.
  405. *
  406. * The file pinning the BPF program can also be unlinked by a different
  407. * process in which case this function will return an error.
  408. *
  409. * @param prog BPF program to unpin
  410. * @param path file path to the pin in a BPF file system
  411. * @return 0, on success; negative error code, otherwise
  412. */
  413. LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
  414. LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
  415. struct bpf_link;
  416. LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
  417. LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
  418. LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
  419. /**
  420. * @brief **bpf_link__pin()** pins the BPF link to a file
  421. * in the BPF FS specified by a path. This increments the links
  422. * reference count, allowing it to stay loaded after the process
  423. * which loaded it has exited.
  424. *
  425. * @param link BPF link to pin, must already be loaded
  426. * @param path file path in a BPF file system
  427. * @return 0, on success; negative error code, otherwise
  428. */
  429. LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
  430. /**
  431. * @brief **bpf_link__unpin()** unpins the BPF link from a file
  432. * in the BPFFS. This decrements link's in-kernel reference count.
  433. *
  434. * The file pinning the BPF link can also be unlinked by a different
  435. * process in which case this function will return an error.
  436. *
  437. * @param link BPF link to unpin
  438. * @return 0, on success; negative error code, otherwise
  439. */
  440. LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
  441. LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
  442. struct bpf_program *prog);
  443. LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
  444. LIBBPF_API int bpf_link__detach(struct bpf_link *link);
  445. LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
  446. /**
  447. * @brief **bpf_program__attach()** is a generic function for attaching
  448. * a BPF program based on auto-detection of program type, attach type,
  449. * and extra parameters, where applicable.
  450. *
  451. * @param prog BPF program to attach
  452. * @return Reference to the newly created BPF link; or NULL is returned on error,
  453. * error code is stored in errno
  454. *
  455. * This is supported for:
  456. * - kprobe/kretprobe (depends on SEC() definition)
  457. * - uprobe/uretprobe (depends on SEC() definition)
  458. * - tracepoint
  459. * - raw tracepoint
  460. * - tracing programs (typed raw TP/fentry/fexit/fmod_ret)
  461. */
  462. LIBBPF_API struct bpf_link *
  463. bpf_program__attach(const struct bpf_program *prog);
  464. struct bpf_perf_event_opts {
  465. /* size of this struct, for forward/backward compatibility */
  466. size_t sz;
  467. /* custom user-provided value fetchable through bpf_get_attach_cookie() */
  468. __u64 bpf_cookie;
  469. /* don't use BPF link when attach BPF program */
  470. bool force_ioctl_attach;
  471. /* don't automatically enable the event */
  472. bool dont_enable;
  473. size_t :0;
  474. };
  475. #define bpf_perf_event_opts__last_field dont_enable
  476. LIBBPF_API struct bpf_link *
  477. bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
  478. LIBBPF_API struct bpf_link *
  479. bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
  480. const struct bpf_perf_event_opts *opts);
  481. /**
  482. * enum probe_attach_mode - the mode to attach kprobe/uprobe
  483. *
  484. * force libbpf to attach kprobe/uprobe in specific mode, -ENOTSUP will
  485. * be returned if it is not supported by the kernel.
  486. */
  487. enum probe_attach_mode {
  488. /* attach probe in latest supported mode by kernel */
  489. PROBE_ATTACH_MODE_DEFAULT = 0,
  490. /* attach probe in legacy mode, using debugfs/tracefs */
  491. PROBE_ATTACH_MODE_LEGACY,
  492. /* create perf event with perf_event_open() syscall */
  493. PROBE_ATTACH_MODE_PERF,
  494. /* attach probe with BPF link */
  495. PROBE_ATTACH_MODE_LINK,
  496. };
  497. struct bpf_kprobe_opts {
  498. /* size of this struct, for forward/backward compatibility */
  499. size_t sz;
  500. /* custom user-provided value fetchable through bpf_get_attach_cookie() */
  501. __u64 bpf_cookie;
  502. /* function's offset to install kprobe to */
  503. size_t offset;
  504. /* kprobe is return probe */
  505. bool retprobe;
  506. /* kprobe attach mode */
  507. enum probe_attach_mode attach_mode;
  508. size_t :0;
  509. };
  510. #define bpf_kprobe_opts__last_field attach_mode
  511. LIBBPF_API struct bpf_link *
  512. bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
  513. const char *func_name);
  514. LIBBPF_API struct bpf_link *
  515. bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
  516. const char *func_name,
  517. const struct bpf_kprobe_opts *opts);
  518. struct bpf_kprobe_multi_opts {
  519. /* size of this struct, for forward/backward compatibility */
  520. size_t sz;
  521. /* array of function symbols to attach */
  522. const char **syms;
  523. /* array of function addresses to attach */
  524. const unsigned long *addrs;
  525. /* array of user-provided values fetchable through bpf_get_attach_cookie */
  526. const __u64 *cookies;
  527. /* number of elements in syms/addrs/cookies arrays */
  528. size_t cnt;
  529. /* create return kprobes */
  530. bool retprobe;
  531. /* create session kprobes */
  532. bool session;
  533. /* enforce unique match */
  534. bool unique_match;
  535. size_t :0;
  536. };
  537. #define bpf_kprobe_multi_opts__last_field unique_match
  538. LIBBPF_API struct bpf_link *
  539. bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
  540. const char *pattern,
  541. const struct bpf_kprobe_multi_opts *opts);
  542. struct bpf_uprobe_multi_opts {
  543. /* size of this struct, for forward/backward compatibility */
  544. size_t sz;
  545. /* array of function symbols to attach to */
  546. const char **syms;
  547. /* array of function addresses to attach to */
  548. const unsigned long *offsets;
  549. /* optional, array of associated ref counter offsets */
  550. const unsigned long *ref_ctr_offsets;
  551. /* optional, array of associated BPF cookies */
  552. const __u64 *cookies;
  553. /* number of elements in syms/addrs/cookies arrays */
  554. size_t cnt;
  555. /* create return uprobes */
  556. bool retprobe;
  557. /* create session kprobes */
  558. bool session;
  559. size_t :0;
  560. };
  561. #define bpf_uprobe_multi_opts__last_field session
  562. /**
  563. * @brief **bpf_program__attach_uprobe_multi()** attaches a BPF program
  564. * to multiple uprobes with uprobe_multi link.
  565. *
  566. * User can specify 2 mutually exclusive set of inputs:
  567. *
  568. * 1) use only path/func_pattern/pid arguments
  569. *
  570. * 2) use path/pid with allowed combinations of
  571. * syms/offsets/ref_ctr_offsets/cookies/cnt
  572. *
  573. * - syms and offsets are mutually exclusive
  574. * - ref_ctr_offsets and cookies are optional
  575. *
  576. *
  577. * @param prog BPF program to attach
  578. * @param pid Process ID to attach the uprobe to, 0 for self (own process),
  579. * -1 for all processes
  580. * @param binary_path Path to binary
  581. * @param func_pattern Regular expression to specify functions to attach
  582. * BPF program to
  583. * @param opts Additional options (see **struct bpf_uprobe_multi_opts**)
  584. * @return 0, on success; negative error code, otherwise
  585. */
  586. LIBBPF_API struct bpf_link *
  587. bpf_program__attach_uprobe_multi(const struct bpf_program *prog,
  588. pid_t pid,
  589. const char *binary_path,
  590. const char *func_pattern,
  591. const struct bpf_uprobe_multi_opts *opts);
  592. struct bpf_ksyscall_opts {
  593. /* size of this struct, for forward/backward compatibility */
  594. size_t sz;
  595. /* custom user-provided value fetchable through bpf_get_attach_cookie() */
  596. __u64 bpf_cookie;
  597. /* attach as return probe? */
  598. bool retprobe;
  599. size_t :0;
  600. };
  601. #define bpf_ksyscall_opts__last_field retprobe
  602. /**
  603. * @brief **bpf_program__attach_ksyscall()** attaches a BPF program
  604. * to kernel syscall handler of a specified syscall. Optionally it's possible
  605. * to request to install retprobe that will be triggered at syscall exit. It's
  606. * also possible to associate BPF cookie (though options).
  607. *
  608. * Libbpf automatically will determine correct full kernel function name,
  609. * which depending on system architecture and kernel version/configuration
  610. * could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will
  611. * attach specified program using kprobe/kretprobe mechanism.
  612. *
  613. * **bpf_program__attach_ksyscall()** is an API counterpart of declarative
  614. * **SEC("ksyscall/<syscall>")** annotation of BPF programs.
  615. *
  616. * At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do
  617. * not handle all the calling convention quirks for mmap(), clone() and compat
  618. * syscalls. It also only attaches to "native" syscall interfaces. If host
  619. * system supports compat syscalls or defines 32-bit syscalls in 64-bit
  620. * kernel, such syscall interfaces won't be attached to by libbpf.
  621. *
  622. * These limitations may or may not change in the future. Therefore it is
  623. * recommended to use SEC("kprobe") for these syscalls or if working with
  624. * compat and 32-bit interfaces is required.
  625. *
  626. * @param prog BPF program to attach
  627. * @param syscall_name Symbolic name of the syscall (e.g., "bpf")
  628. * @param opts Additional options (see **struct bpf_ksyscall_opts**)
  629. * @return Reference to the newly created BPF link; or NULL is returned on
  630. * error, error code is stored in errno
  631. */
  632. LIBBPF_API struct bpf_link *
  633. bpf_program__attach_ksyscall(const struct bpf_program *prog,
  634. const char *syscall_name,
  635. const struct bpf_ksyscall_opts *opts);
  636. struct bpf_uprobe_opts {
  637. /* size of this struct, for forward/backward compatibility */
  638. size_t sz;
  639. /* offset of kernel reference counted USDT semaphore, added in
  640. * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
  641. */
  642. size_t ref_ctr_offset;
  643. /* custom user-provided value fetchable through bpf_get_attach_cookie() */
  644. __u64 bpf_cookie;
  645. /* uprobe is return probe, invoked at function return time */
  646. bool retprobe;
  647. /* Function name to attach to. Could be an unqualified ("abc") or library-qualified
  648. * "abc@LIBXYZ" name. To specify function entry, func_name should be set while
  649. * func_offset argument to bpf_prog__attach_uprobe_opts() should be 0. To trace an
  650. * offset within a function, specify func_name and use func_offset argument to specify
  651. * offset within the function. Shared library functions must specify the shared library
  652. * binary_path.
  653. */
  654. const char *func_name;
  655. /* uprobe attach mode */
  656. enum probe_attach_mode attach_mode;
  657. size_t :0;
  658. };
  659. #define bpf_uprobe_opts__last_field attach_mode
  660. /**
  661. * @brief **bpf_program__attach_uprobe()** attaches a BPF program
  662. * to the userspace function which is found by binary path and
  663. * offset. You can optionally specify a particular process to attach
  664. * to. You can also optionally attach the program to the function
  665. * exit instead of entry.
  666. *
  667. * @param prog BPF program to attach
  668. * @param retprobe Attach to function exit
  669. * @param pid Process ID to attach the uprobe to, 0 for self (own process),
  670. * -1 for all processes
  671. * @param binary_path Path to binary that contains the function symbol
  672. * @param func_offset Offset within the binary of the function symbol
  673. * @return Reference to the newly created BPF link; or NULL is returned on error,
  674. * error code is stored in errno
  675. */
  676. LIBBPF_API struct bpf_link *
  677. bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
  678. pid_t pid, const char *binary_path,
  679. size_t func_offset);
  680. /**
  681. * @brief **bpf_program__attach_uprobe_opts()** is just like
  682. * bpf_program__attach_uprobe() except with a options struct
  683. * for various configurations.
  684. *
  685. * @param prog BPF program to attach
  686. * @param pid Process ID to attach the uprobe to, 0 for self (own process),
  687. * -1 for all processes
  688. * @param binary_path Path to binary that contains the function symbol
  689. * @param func_offset Offset within the binary of the function symbol
  690. * @param opts Options for altering program attachment
  691. * @return Reference to the newly created BPF link; or NULL is returned on error,
  692. * error code is stored in errno
  693. */
  694. LIBBPF_API struct bpf_link *
  695. bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
  696. const char *binary_path, size_t func_offset,
  697. const struct bpf_uprobe_opts *opts);
  698. struct bpf_usdt_opts {
  699. /* size of this struct, for forward/backward compatibility */
  700. size_t sz;
  701. /* custom user-provided value accessible through usdt_cookie() */
  702. __u64 usdt_cookie;
  703. size_t :0;
  704. };
  705. #define bpf_usdt_opts__last_field usdt_cookie
  706. /**
  707. * @brief **bpf_program__attach_usdt()** is just like
  708. * bpf_program__attach_uprobe_opts() except it covers USDT (User-space
  709. * Statically Defined Tracepoint) attachment, instead of attaching to
  710. * user-space function entry or exit.
  711. *
  712. * @param prog BPF program to attach
  713. * @param pid Process ID to attach the uprobe to, 0 for self (own process),
  714. * -1 for all processes
  715. * @param binary_path Path to binary that contains provided USDT probe
  716. * @param usdt_provider USDT provider name
  717. * @param usdt_name USDT probe name
  718. * @param opts Options for altering program attachment
  719. * @return Reference to the newly created BPF link; or NULL is returned on error,
  720. * error code is stored in errno
  721. */
  722. LIBBPF_API struct bpf_link *
  723. bpf_program__attach_usdt(const struct bpf_program *prog,
  724. pid_t pid, const char *binary_path,
  725. const char *usdt_provider, const char *usdt_name,
  726. const struct bpf_usdt_opts *opts);
  727. struct bpf_tracepoint_opts {
  728. /* size of this struct, for forward/backward compatibility */
  729. size_t sz;
  730. /* custom user-provided value fetchable through bpf_get_attach_cookie() */
  731. __u64 bpf_cookie;
  732. };
  733. #define bpf_tracepoint_opts__last_field bpf_cookie
  734. LIBBPF_API struct bpf_link *
  735. bpf_program__attach_tracepoint(const struct bpf_program *prog,
  736. const char *tp_category,
  737. const char *tp_name);
  738. LIBBPF_API struct bpf_link *
  739. bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
  740. const char *tp_category,
  741. const char *tp_name,
  742. const struct bpf_tracepoint_opts *opts);
  743. struct bpf_raw_tracepoint_opts {
  744. size_t sz; /* size of this struct for forward/backward compatibility */
  745. __u64 cookie;
  746. size_t :0;
  747. };
  748. #define bpf_raw_tracepoint_opts__last_field cookie
  749. LIBBPF_API struct bpf_link *
  750. bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
  751. const char *tp_name);
  752. LIBBPF_API struct bpf_link *
  753. bpf_program__attach_raw_tracepoint_opts(const struct bpf_program *prog,
  754. const char *tp_name,
  755. struct bpf_raw_tracepoint_opts *opts);
  756. struct bpf_trace_opts {
  757. /* size of this struct, for forward/backward compatibility */
  758. size_t sz;
  759. /* custom user-provided value fetchable through bpf_get_attach_cookie() */
  760. __u64 cookie;
  761. };
  762. #define bpf_trace_opts__last_field cookie
  763. LIBBPF_API struct bpf_link *
  764. bpf_program__attach_trace(const struct bpf_program *prog);
  765. LIBBPF_API struct bpf_link *
  766. bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);
  767. LIBBPF_API struct bpf_link *
  768. bpf_program__attach_lsm(const struct bpf_program *prog);
  769. LIBBPF_API struct bpf_link *
  770. bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
  771. LIBBPF_API struct bpf_link *
  772. bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
  773. LIBBPF_API struct bpf_link *
  774. bpf_program__attach_sockmap(const struct bpf_program *prog, int map_fd);
  775. LIBBPF_API struct bpf_link *
  776. bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
  777. LIBBPF_API struct bpf_link *
  778. bpf_program__attach_freplace(const struct bpf_program *prog,
  779. int target_fd, const char *attach_func_name);
  780. struct bpf_netfilter_opts {
  781. /* size of this struct, for forward/backward compatibility */
  782. size_t sz;
  783. __u32 pf;
  784. __u32 hooknum;
  785. __s32 priority;
  786. __u32 flags;
  787. };
  788. #define bpf_netfilter_opts__last_field flags
  789. LIBBPF_API struct bpf_link *
  790. bpf_program__attach_netfilter(const struct bpf_program *prog,
  791. const struct bpf_netfilter_opts *opts);
  792. struct bpf_tcx_opts {
  793. /* size of this struct, for forward/backward compatibility */
  794. size_t sz;
  795. __u32 flags;
  796. __u32 relative_fd;
  797. __u32 relative_id;
  798. __u64 expected_revision;
  799. size_t :0;
  800. };
  801. #define bpf_tcx_opts__last_field expected_revision
  802. LIBBPF_API struct bpf_link *
  803. bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,
  804. const struct bpf_tcx_opts *opts);
  805. struct bpf_netkit_opts {
  806. /* size of this struct, for forward/backward compatibility */
  807. size_t sz;
  808. __u32 flags;
  809. __u32 relative_fd;
  810. __u32 relative_id;
  811. __u64 expected_revision;
  812. size_t :0;
  813. };
  814. #define bpf_netkit_opts__last_field expected_revision
  815. LIBBPF_API struct bpf_link *
  816. bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,
  817. const struct bpf_netkit_opts *opts);
  818. struct bpf_cgroup_opts {
  819. /* size of this struct, for forward/backward compatibility */
  820. size_t sz;
  821. __u32 flags;
  822. __u32 relative_fd;
  823. __u32 relative_id;
  824. __u64 expected_revision;
  825. size_t :0;
  826. };
  827. #define bpf_cgroup_opts__last_field expected_revision
  828. LIBBPF_API struct bpf_link *
  829. bpf_program__attach_cgroup_opts(const struct bpf_program *prog, int cgroup_fd,
  830. const struct bpf_cgroup_opts *opts);
  831. struct bpf_map;
  832. LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
  833. LIBBPF_API int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map);
  834. struct bpf_iter_attach_opts {
  835. size_t sz; /* size of this struct for forward/backward compatibility */
  836. union bpf_iter_link_info *link_info;
  837. __u32 link_info_len;
  838. };
  839. #define bpf_iter_attach_opts__last_field link_info_len
  840. LIBBPF_API struct bpf_link *
  841. bpf_program__attach_iter(const struct bpf_program *prog,
  842. const struct bpf_iter_attach_opts *opts);
  843. LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);
  844. /**
  845. * @brief **bpf_program__set_type()** sets the program
  846. * type of the passed BPF program.
  847. * @param prog BPF program to set the program type for
  848. * @param type program type to set the BPF map to have
  849. * @return error code; or 0 if no error. An error occurs
  850. * if the object is already loaded.
  851. *
  852. * This must be called before the BPF object is loaded,
  853. * otherwise it has no effect and an error is returned.
  854. */
  855. LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,
  856. enum bpf_prog_type type);
  857. LIBBPF_API enum bpf_attach_type
  858. bpf_program__expected_attach_type(const struct bpf_program *prog);
  859. /**
  860. * @brief **bpf_program__set_expected_attach_type()** sets the
  861. * attach type of the passed BPF program. This is used for
  862. * auto-detection of attachment when programs are loaded.
  863. * @param prog BPF program to set the attach type for
  864. * @param type attach type to set the BPF map to have
  865. * @return error code; or 0 if no error. An error occurs
  866. * if the object is already loaded.
  867. *
  868. * This must be called before the BPF object is loaded,
  869. * otherwise it has no effect and an error is returned.
  870. */
  871. LIBBPF_API int
  872. bpf_program__set_expected_attach_type(struct bpf_program *prog,
  873. enum bpf_attach_type type);
  874. LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
  875. LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
  876. /* Per-program log level and log buffer getters/setters.
  877. * See bpf_object_open_opts comments regarding log_level and log_buf
  878. * interactions.
  879. */
  880. LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);
  881. LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);
  882. LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
  883. LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
  884. LIBBPF_API struct bpf_func_info *bpf_program__func_info(const struct bpf_program *prog);
  885. LIBBPF_API __u32 bpf_program__func_info_cnt(const struct bpf_program *prog);
  886. LIBBPF_API struct bpf_line_info *bpf_program__line_info(const struct bpf_program *prog);
  887. LIBBPF_API __u32 bpf_program__line_info_cnt(const struct bpf_program *prog);
  888. /**
  889. * @brief **bpf_program__set_attach_target()** sets BTF-based attach target
  890. * for supported BPF program types:
  891. * - BTF-aware raw tracepoints (tp_btf);
  892. * - fentry/fexit/fmod_ret;
  893. * - lsm;
  894. * - freplace.
  895. * @param prog BPF program to configure; must be not yet loaded.
  896. * @param attach_prog_fd FD of target BPF program (for freplace/extension).
  897. * If >0 and func name omitted, defers BTF ID resolution.
  898. * @param attach_func_name Target function name. Used either with
  899. * attach_prog_fd to find destination BTF type ID in that BPF program, or
  900. * alone (no attach_prog_fd) to resolve kernel (vmlinux/module) BTF ID.
  901. * Must be provided if attach_prog_fd is 0.
  902. * @return error code; or 0 if no error occurred.
  903. */
  904. LIBBPF_API int
  905. bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
  906. const char *attach_func_name);
  907. struct bpf_prog_assoc_struct_ops_opts; /* defined in bpf.h */
  908. /**
  909. * @brief **bpf_program__assoc_struct_ops()** associates a BPF program with a
  910. * struct_ops map.
  911. *
  912. * @param prog BPF program
  913. * @param map struct_ops map to be associated with the BPF program
  914. * @param opts optional options, can be NULL
  915. *
  916. * @return 0, on success; negative error code, otherwise
  917. */
  918. LIBBPF_API int
  919. bpf_program__assoc_struct_ops(struct bpf_program *prog, struct bpf_map *map,
  920. struct bpf_prog_assoc_struct_ops_opts *opts);
  921. /**
  922. * @brief **bpf_object__find_map_by_name()** returns BPF map of
  923. * the given name, if it exists within the passed BPF object
  924. * @param obj BPF object
  925. * @param name name of the BPF map
  926. * @return BPF map instance, if such map exists within the BPF object;
  927. * or NULL otherwise.
  928. */
  929. LIBBPF_API struct bpf_map *
  930. bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
  931. LIBBPF_API int
  932. bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
  933. LIBBPF_API struct bpf_map *
  934. bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
  935. #define bpf_object__for_each_map(pos, obj) \
  936. for ((pos) = bpf_object__next_map((obj), NULL); \
  937. (pos) != NULL; \
  938. (pos) = bpf_object__next_map((obj), (pos)))
  939. #define bpf_map__for_each bpf_object__for_each_map
  940. LIBBPF_API struct bpf_map *
  941. bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
  942. /**
  943. * @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create
  944. * BPF map during BPF object load phase.
  945. * @param map the BPF map instance
  946. * @param autocreate whether to create BPF map during BPF object load
  947. * @return 0 on success; -EBUSY if BPF object was already loaded
  948. *
  949. * **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating
  950. * BPF map. By default, libbpf will attempt to create every single BPF map
  951. * defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall
  952. * and fill in map FD in BPF instructions.
  953. *
  954. * This API allows to opt-out of this process for specific map instance. This
  955. * can be useful if host kernel doesn't support such BPF map type or used
  956. * combination of flags and user application wants to avoid creating such
  957. * a map in the first place. User is still responsible to make sure that their
  958. * BPF-side code that expects to use such missing BPF map is recognized by BPF
  959. * verifier as dead code, otherwise BPF verifier will reject such BPF program.
  960. */
  961. LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);
  962. LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);
  963. /**
  964. * @brief **bpf_map__set_autoattach()** sets whether libbpf has to auto-attach
  965. * map during BPF skeleton attach phase.
  966. * @param map the BPF map instance
  967. * @param autoattach whether to attach map during BPF skeleton attach phase
  968. * @return 0 on success; negative error code, otherwise
  969. */
  970. LIBBPF_API int bpf_map__set_autoattach(struct bpf_map *map, bool autoattach);
  971. /**
  972. * @brief **bpf_map__autoattach()** returns whether BPF map is configured to
  973. * auto-attach during BPF skeleton attach phase.
  974. * @param map the BPF map instance
  975. * @return true if map is set to auto-attach during skeleton attach phase; false, otherwise
  976. */
  977. LIBBPF_API bool bpf_map__autoattach(const struct bpf_map *map);
  978. /**
  979. * @brief **bpf_map__fd()** gets the file descriptor of the passed
  980. * BPF map
  981. * @param map the BPF map instance
  982. * @return the file descriptor; or -EINVAL in case of an error
  983. */
  984. LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
  985. LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
  986. /* get map name */
  987. LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
  988. /* get/set map type */
  989. LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
  990. LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
  991. /* get/set map size (max_entries) */
  992. LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
  993. LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
  994. /* get/set map flags */
  995. LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
  996. LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
  997. /* get/set map NUMA node */
  998. LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
  999. LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
  1000. /* get/set map key size */
  1001. LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
  1002. LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
  1003. /* get map value size */
  1004. LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
  1005. /**
  1006. * @brief **bpf_map__set_value_size()** sets map value size.
  1007. * @param map the BPF map instance
  1008. * @param size the new value size
  1009. * @return 0, on success; negative error, otherwise
  1010. *
  1011. * There is a special case for maps with associated memory-mapped regions, like
  1012. * the global data section maps (bss, data, rodata). When this function is used
  1013. * on such a map, the mapped region is resized. Afterward, an attempt is made to
  1014. * adjust the corresponding BTF info. This attempt is best-effort and can only
  1015. * succeed if the last variable of the data section map is an array. The array
  1016. * BTF type is replaced by a new BTF array type with a different length.
  1017. * Any previously existing pointers returned from bpf_map__initial_value() or
  1018. * corresponding data section skeleton pointer must be reinitialized.
  1019. */
  1020. LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
  1021. /* get map key/value BTF type IDs */
  1022. LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
  1023. LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
  1024. /* get/set map if_index */
  1025. LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
  1026. LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
  1027. /* get/set map map_extra flags */
  1028. LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
  1029. LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
  1030. LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
  1031. const void *data, size_t size);
  1032. LIBBPF_API void *bpf_map__initial_value(const struct bpf_map *map, size_t *psize);
  1033. /**
  1034. * @brief **bpf_map__is_internal()** tells the caller whether or not the
  1035. * passed map is a special map created by libbpf automatically for things like
  1036. * global variables, __ksym externs, Kconfig values, etc
  1037. * @param map the bpf_map
  1038. * @return true, if the map is an internal map; false, otherwise
  1039. */
  1040. LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
  1041. /**
  1042. * @brief **bpf_map__set_pin_path()** sets the path attribute that tells where the
  1043. * BPF map should be pinned. This does not actually create the 'pin'.
  1044. * @param map The bpf_map
  1045. * @param path The path
  1046. * @return 0, on success; negative error, otherwise
  1047. */
  1048. LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
  1049. /**
  1050. * @brief **bpf_map__pin_path()** gets the path attribute that tells where the
  1051. * BPF map should be pinned.
  1052. * @param map The bpf_map
  1053. * @return The path string; which can be NULL
  1054. */
  1055. LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
  1056. /**
  1057. * @brief **bpf_map__is_pinned()** tells the caller whether or not the
  1058. * passed map has been pinned via a 'pin' file.
  1059. * @param map The bpf_map
  1060. * @return true, if the map is pinned; false, otherwise
  1061. */
  1062. LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
  1063. /**
  1064. * @brief **bpf_map__pin()** creates a file that serves as a 'pin'
  1065. * for the BPF map. This increments the reference count on the
  1066. * BPF map which will keep the BPF map loaded even after the
  1067. * userspace process which loaded it has exited.
  1068. * @param map The bpf_map to pin
  1069. * @param path A file path for the 'pin'
  1070. * @return 0, on success; negative error, otherwise
  1071. *
  1072. * If `path` is NULL the maps `pin_path` attribute will be used. If this is
  1073. * also NULL, an error will be returned and the map will not be pinned.
  1074. */
  1075. LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
  1076. /**
  1077. * @brief **bpf_map__unpin()** removes the file that serves as a
  1078. * 'pin' for the BPF map.
  1079. * @param map The bpf_map to unpin
  1080. * @param path A file path for the 'pin'
  1081. * @return 0, on success; negative error, otherwise
  1082. *
  1083. * The `path` parameter can be NULL, in which case the `pin_path`
  1084. * map attribute is unpinned. If both the `path` parameter and
  1085. * `pin_path` map attribute are set, they must be equal.
  1086. */
  1087. LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
  1088. LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
  1089. LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
  1090. /**
  1091. * @brief **bpf_map__lookup_elem()** allows to lookup BPF map value
  1092. * corresponding to provided key.
  1093. * @param map BPF map to lookup element in
  1094. * @param key pointer to memory containing bytes of the key used for lookup
  1095. * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
  1096. * @param value pointer to memory in which looked up value will be stored
  1097. * @param value_sz size in byte of value data memory; it has to match BPF map
  1098. * definition's **value_size**. For per-CPU BPF maps, value size can be
  1099. * `value_size` if either **BPF_F_CPU** or **BPF_F_ALL_CPUS** is specified
  1100. * in **flags**, otherwise a product of BPF map value size and number of
  1101. * possible CPUs in the system (could be fetched with
  1102. * **libbpf_num_possible_cpus()**). Note also that for per-CPU values value
  1103. * size has to be aligned up to closest 8 bytes, so expected size is:
  1104. * `round_up(value_size, 8) * libbpf_num_possible_cpus()`.
  1105. * @param flags extra flags passed to kernel for this operation
  1106. * @return 0, on success; negative error, otherwise
  1107. *
  1108. * **bpf_map__lookup_elem()** is high-level equivalent of
  1109. * **bpf_map_lookup_elem()** API with added check for key and value size.
  1110. */
  1111. LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,
  1112. const void *key, size_t key_sz,
  1113. void *value, size_t value_sz, __u64 flags);
  1114. /**
  1115. * @brief **bpf_map__update_elem()** allows to insert or update value in BPF
  1116. * map that corresponds to provided key.
  1117. * @param map BPF map to insert to or update element in
  1118. * @param key pointer to memory containing bytes of the key
  1119. * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
  1120. * @param value pointer to memory containing bytes of the value
  1121. * @param value_sz refer to **bpf_map__lookup_elem**'s description.'
  1122. * @param flags extra flags passed to kernel for this operation
  1123. * @return 0, on success; negative error, otherwise
  1124. *
  1125. * **bpf_map__update_elem()** is high-level equivalent of
  1126. * **bpf_map_update_elem()** API with added check for key and value size.
  1127. */
  1128. LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map,
  1129. const void *key, size_t key_sz,
  1130. const void *value, size_t value_sz, __u64 flags);
  1131. /**
  1132. * @brief **bpf_map__delete_elem()** allows to delete element in BPF map that
  1133. * corresponds to provided key.
  1134. * @param map BPF map to delete element from
  1135. * @param key pointer to memory containing bytes of the key
  1136. * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
  1137. * @param flags extra flags passed to kernel for this operation
  1138. * @return 0, on success; negative error, otherwise
  1139. *
  1140. * **bpf_map__delete_elem()** is high-level equivalent of
  1141. * **bpf_map_delete_elem()** API with added check for key size.
  1142. */
  1143. LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map,
  1144. const void *key, size_t key_sz, __u64 flags);
  1145. /**
  1146. * @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value
  1147. * corresponding to provided key and atomically delete it afterwards.
  1148. * @param map BPF map to lookup element in
  1149. * @param key pointer to memory containing bytes of the key used for lookup
  1150. * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
  1151. * @param value pointer to memory in which looked up value will be stored
  1152. * @param value_sz size in byte of value data memory; it has to match BPF map
  1153. * definition's **value_size**. For per-CPU BPF maps value size has to be
  1154. * a product of BPF map value size and number of possible CPUs in the system
  1155. * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
  1156. * per-CPU values value size has to be aligned up to closest 8 bytes for
  1157. * alignment reasons, so expected size is: `round_up(value_size, 8)
  1158. * * libbpf_num_possible_cpus()`.
  1159. * @param flags extra flags passed to kernel for this operation
  1160. * @return 0, on success; negative error, otherwise
  1161. *
  1162. * **bpf_map__lookup_and_delete_elem()** is high-level equivalent of
  1163. * **bpf_map_lookup_and_delete_elem()** API with added check for key and value size.
  1164. */
  1165. LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
  1166. const void *key, size_t key_sz,
  1167. void *value, size_t value_sz, __u64 flags);
  1168. /**
  1169. * @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by
  1170. * fetching next key that follows current key.
  1171. * @param map BPF map to fetch next key from
  1172. * @param cur_key pointer to memory containing bytes of current key or NULL to
  1173. * fetch the first key
  1174. * @param next_key pointer to memory to write next key into
  1175. * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
  1176. * @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map;
  1177. * negative error, otherwise
  1178. *
  1179. * **bpf_map__get_next_key()** is high-level equivalent of
  1180. * **bpf_map_get_next_key()** API with added check for key size.
  1181. */
  1182. LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map,
  1183. const void *cur_key, void *next_key, size_t key_sz);
  1184. /**
  1185. * @brief **bpf_map__set_exclusive_program()** sets a map to be exclusive to the
  1186. * specified program. This must be called *before* the map is created.
  1187. *
  1188. * @param map BPF map to make exclusive.
  1189. * @param prog BPF program to be the exclusive user of the map. Must belong
  1190. * to the same bpf_object as the map.
  1191. * @return 0 on success; a negative error code otherwise.
  1192. *
  1193. * This function must be called after the BPF object is opened but before
  1194. * it is loaded. Once the object is loaded, only the specified program
  1195. * will be able to access the map's contents.
  1196. */
  1197. LIBBPF_API int bpf_map__set_exclusive_program(struct bpf_map *map, struct bpf_program *prog);
  1198. /**
  1199. * @brief **bpf_map__exclusive_program()** returns the exclusive program
  1200. * that is registered with the map (if any).
  1201. * @param map BPF map to which the exclusive program is registered.
  1202. * @return the registered exclusive program.
  1203. */
  1204. LIBBPF_API struct bpf_program *bpf_map__exclusive_program(struct bpf_map *map);
  1205. struct bpf_xdp_set_link_opts {
  1206. size_t sz;
  1207. int old_fd;
  1208. size_t :0;
  1209. };
  1210. #define bpf_xdp_set_link_opts__last_field old_fd
  1211. struct bpf_xdp_attach_opts {
  1212. size_t sz;
  1213. int old_prog_fd;
  1214. size_t :0;
  1215. };
  1216. #define bpf_xdp_attach_opts__last_field old_prog_fd
  1217. struct bpf_xdp_query_opts {
  1218. size_t sz;
  1219. __u32 prog_id; /* output */
  1220. __u32 drv_prog_id; /* output */
  1221. __u32 hw_prog_id; /* output */
  1222. __u32 skb_prog_id; /* output */
  1223. __u8 attach_mode; /* output */
  1224. __u64 feature_flags; /* output */
  1225. __u32 xdp_zc_max_segs; /* output */
  1226. size_t :0;
  1227. };
  1228. #define bpf_xdp_query_opts__last_field xdp_zc_max_segs
  1229. LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,
  1230. const struct bpf_xdp_attach_opts *opts);
  1231. LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,
  1232. const struct bpf_xdp_attach_opts *opts);
  1233. LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);
  1234. LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);
  1235. /* TC related API */
  1236. enum bpf_tc_attach_point {
  1237. BPF_TC_INGRESS = 1 << 0,
  1238. BPF_TC_EGRESS = 1 << 1,
  1239. BPF_TC_CUSTOM = 1 << 2,
  1240. BPF_TC_QDISC = 1 << 3,
  1241. };
  1242. #define BPF_TC_PARENT(a, b) \
  1243. ((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
  1244. enum bpf_tc_flags {
  1245. BPF_TC_F_REPLACE = 1 << 0,
  1246. };
  1247. struct bpf_tc_hook {
  1248. size_t sz;
  1249. int ifindex;
  1250. enum bpf_tc_attach_point attach_point;
  1251. __u32 parent;
  1252. __u32 handle;
  1253. const char *qdisc;
  1254. size_t :0;
  1255. };
  1256. #define bpf_tc_hook__last_field qdisc
  1257. struct bpf_tc_opts {
  1258. size_t sz;
  1259. int prog_fd;
  1260. __u32 flags;
  1261. __u32 prog_id;
  1262. __u32 handle;
  1263. __u32 priority;
  1264. size_t :0;
  1265. };
  1266. #define bpf_tc_opts__last_field priority
  1267. LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
  1268. LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
  1269. LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
  1270. struct bpf_tc_opts *opts);
  1271. LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
  1272. const struct bpf_tc_opts *opts);
  1273. LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
  1274. struct bpf_tc_opts *opts);
  1275. /* Ring buffer APIs */
  1276. struct ring_buffer;
  1277. struct ring;
  1278. struct user_ring_buffer;
  1279. typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
  1280. struct ring_buffer_opts {
  1281. size_t sz; /* size of this struct, for forward/backward compatibility */
  1282. };
  1283. #define ring_buffer_opts__last_field sz
  1284. LIBBPF_API struct ring_buffer *
  1285. ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
  1286. const struct ring_buffer_opts *opts);
  1287. LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
  1288. LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
  1289. ring_buffer_sample_fn sample_cb, void *ctx);
  1290. LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
  1291. LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
  1292. LIBBPF_API int ring_buffer__consume_n(struct ring_buffer *rb, size_t n);
  1293. LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
  1294. /**
  1295. * @brief **ring_buffer__ring()** returns the ringbuffer object inside a given
  1296. * ringbuffer manager representing a single BPF_MAP_TYPE_RINGBUF map instance.
  1297. *
  1298. * @param rb A ringbuffer manager object.
  1299. * @param idx An index into the ringbuffers contained within the ringbuffer
  1300. * manager object. The index is 0-based and corresponds to the order in which
  1301. * ring_buffer__add was called.
  1302. * @return A ringbuffer object on success; NULL and errno set if the index is
  1303. * invalid.
  1304. */
  1305. LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb,
  1306. unsigned int idx);
  1307. /**
  1308. * @brief **ring__consumer_pos()** returns the current consumer position in the
  1309. * given ringbuffer.
  1310. *
  1311. * @param r A ringbuffer object.
  1312. * @return The current consumer position.
  1313. */
  1314. LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r);
  1315. /**
  1316. * @brief **ring__producer_pos()** returns the current producer position in the
  1317. * given ringbuffer.
  1318. *
  1319. * @param r A ringbuffer object.
  1320. * @return The current producer position.
  1321. */
  1322. LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);
  1323. /**
  1324. * @brief **ring__avail_data_size()** returns the number of bytes in the
  1325. * ringbuffer not yet consumed. This has no locking associated with it, so it
  1326. * can be inaccurate if operations are ongoing while this is called. However, it
  1327. * should still show the correct trend over the long-term.
  1328. *
  1329. * @param r A ringbuffer object.
  1330. * @return The number of bytes not yet consumed.
  1331. */
  1332. LIBBPF_API size_t ring__avail_data_size(const struct ring *r);
  1333. /**
  1334. * @brief **ring__size()** returns the total size of the ringbuffer's map data
  1335. * area (excluding special producer/consumer pages). Effectively this gives the
  1336. * amount of usable bytes of data inside the ringbuffer.
  1337. *
  1338. * @param r A ringbuffer object.
  1339. * @return The total size of the ringbuffer map data area.
  1340. */
  1341. LIBBPF_API size_t ring__size(const struct ring *r);
  1342. /**
  1343. * @brief **ring__map_fd()** returns the file descriptor underlying the given
  1344. * ringbuffer.
  1345. *
  1346. * @param r A ringbuffer object.
  1347. * @return The underlying ringbuffer file descriptor
  1348. */
  1349. LIBBPF_API int ring__map_fd(const struct ring *r);
  1350. /**
  1351. * @brief **ring__consume()** consumes available ringbuffer data without event
  1352. * polling.
  1353. *
  1354. * @param r A ringbuffer object.
  1355. * @return The number of records consumed (or INT_MAX, whichever is less), or
  1356. * a negative number if any of the callbacks return an error.
  1357. */
  1358. LIBBPF_API int ring__consume(struct ring *r);
  1359. /**
  1360. * @brief **ring__consume_n()** consumes up to a requested amount of items from
  1361. * a ringbuffer without event polling.
  1362. *
  1363. * @param r A ringbuffer object.
  1364. * @param n Maximum amount of items to consume.
  1365. * @return The number of items consumed, or a negative number if any of the
  1366. * callbacks return an error.
  1367. */
  1368. LIBBPF_API int ring__consume_n(struct ring *r, size_t n);
  1369. struct user_ring_buffer_opts {
  1370. size_t sz; /* size of this struct, for forward/backward compatibility */
  1371. };
  1372. #define user_ring_buffer_opts__last_field sz
  1373. /**
  1374. * @brief **user_ring_buffer__new()** creates a new instance of a user ring
  1375. * buffer.
  1376. *
  1377. * @param map_fd A file descriptor to a BPF_MAP_TYPE_USER_RINGBUF map.
  1378. * @param opts Options for how the ring buffer should be created.
  1379. * @return A user ring buffer on success; NULL and errno being set on a
  1380. * failure.
  1381. */
  1382. LIBBPF_API struct user_ring_buffer *
  1383. user_ring_buffer__new(int map_fd, const struct user_ring_buffer_opts *opts);
  1384. /**
  1385. * @brief **user_ring_buffer__reserve()** reserves a pointer to a sample in the
  1386. * user ring buffer.
  1387. * @param rb A pointer to a user ring buffer.
  1388. * @param size The size of the sample, in bytes.
  1389. * @return A pointer to an 8-byte aligned reserved region of the user ring
  1390. * buffer; NULL, and errno being set if a sample could not be reserved.
  1391. *
  1392. * This function is *not* thread safe, and callers must synchronize accessing
  1393. * this function if there are multiple producers. If a size is requested that
  1394. * is larger than the size of the entire ring buffer, errno will be set to
  1395. * E2BIG and NULL is returned. If the ring buffer could accommodate the size,
  1396. * but currently does not have enough space, errno is set to ENOSPC and NULL is
  1397. * returned.
  1398. *
  1399. * After initializing the sample, callers must invoke
  1400. * **user_ring_buffer__submit()** to post the sample to the kernel. Otherwise,
  1401. * the sample must be freed with **user_ring_buffer__discard()**.
  1402. */
  1403. LIBBPF_API void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);
  1404. /**
  1405. * @brief **user_ring_buffer__reserve_blocking()** reserves a record in the
  1406. * ring buffer, possibly blocking for up to @timeout_ms until a sample becomes
  1407. * available.
  1408. * @param rb The user ring buffer.
  1409. * @param size The size of the sample, in bytes.
  1410. * @param timeout_ms The amount of time, in milliseconds, for which the caller
  1411. * should block when waiting for a sample. -1 causes the caller to block
  1412. * indefinitely.
  1413. * @return A pointer to an 8-byte aligned reserved region of the user ring
  1414. * buffer; NULL, and errno being set if a sample could not be reserved.
  1415. *
  1416. * This function is *not* thread safe, and callers must synchronize
  1417. * accessing this function if there are multiple producers
  1418. *
  1419. * If **timeout_ms** is -1, the function will block indefinitely until a sample
  1420. * becomes available. Otherwise, **timeout_ms** must be non-negative, or errno
  1421. * is set to EINVAL, and NULL is returned. If **timeout_ms** is 0, no blocking
  1422. * will occur and the function will return immediately after attempting to
  1423. * reserve a sample.
  1424. *
  1425. * If **size** is larger than the size of the entire ring buffer, errno is set
  1426. * to E2BIG and NULL is returned. If the ring buffer could accommodate
  1427. * **size**, but currently does not have enough space, the caller will block
  1428. * until at most **timeout_ms** has elapsed. If insufficient space is available
  1429. * at that time, errno is set to ENOSPC, and NULL is returned.
  1430. *
  1431. * The kernel guarantees that it will wake up this thread to check if
  1432. * sufficient space is available in the ring buffer at least once per
  1433. * invocation of the **bpf_ringbuf_drain()** helper function, provided that at
  1434. * least one sample is consumed, and the BPF program did not invoke the
  1435. * function with BPF_RB_NO_WAKEUP. A wakeup may occur sooner than that, but the
  1436. * kernel does not guarantee this. If the helper function is invoked with
  1437. * BPF_RB_FORCE_WAKEUP, a wakeup event will be sent even if no sample is
  1438. * consumed.
  1439. *
  1440. * When a sample of size **size** is found within **timeout_ms**, a pointer to
  1441. * the sample is returned. After initializing the sample, callers must invoke
  1442. * **user_ring_buffer__submit()** to post the sample to the ring buffer.
  1443. * Otherwise, the sample must be freed with **user_ring_buffer__discard()**.
  1444. */
  1445. LIBBPF_API void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,
  1446. __u32 size,
  1447. int timeout_ms);
  1448. /**
  1449. * @brief **user_ring_buffer__submit()** submits a previously reserved sample
  1450. * into the ring buffer.
  1451. * @param rb The user ring buffer.
  1452. * @param sample A reserved sample.
  1453. *
  1454. * It is not necessary to synchronize amongst multiple producers when invoking
  1455. * this function.
  1456. */
  1457. LIBBPF_API void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample);
  1458. /**
  1459. * @brief **user_ring_buffer__discard()** discards a previously reserved sample.
  1460. * @param rb The user ring buffer.
  1461. * @param sample A reserved sample.
  1462. *
  1463. * It is not necessary to synchronize amongst multiple producers when invoking
  1464. * this function.
  1465. */
  1466. LIBBPF_API void user_ring_buffer__discard(struct user_ring_buffer *rb, void *sample);
  1467. /**
  1468. * @brief **user_ring_buffer__free()** frees a ring buffer that was previously
  1469. * created with **user_ring_buffer__new()**.
  1470. * @param rb The user ring buffer being freed.
  1471. */
  1472. LIBBPF_API void user_ring_buffer__free(struct user_ring_buffer *rb);
  1473. /* Perf buffer APIs */
  1474. struct perf_buffer;
  1475. typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
  1476. void *data, __u32 size);
  1477. typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
  1478. /* common use perf buffer options */
  1479. struct perf_buffer_opts {
  1480. size_t sz;
  1481. __u32 sample_period;
  1482. size_t :0;
  1483. };
  1484. #define perf_buffer_opts__last_field sample_period
  1485. /**
  1486. * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
  1487. * BPF_PERF_EVENT_ARRAY map
  1488. * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
  1489. * code to send data over to user-space
  1490. * @param page_cnt number of memory pages allocated for each per-CPU buffer
  1491. * @param sample_cb function called on each received data record
  1492. * @param lost_cb function called when record loss has occurred
  1493. * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
  1494. * @param opts optional parameters for the perf buffer, can be null
  1495. * @return a new instance of struct perf_buffer on success, NULL on error with
  1496. * *errno* containing an error code
  1497. */
  1498. LIBBPF_API struct perf_buffer *
  1499. perf_buffer__new(int map_fd, size_t page_cnt,
  1500. perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
  1501. const struct perf_buffer_opts *opts);
  1502. enum bpf_perf_event_ret {
  1503. LIBBPF_PERF_EVENT_DONE = 0,
  1504. LIBBPF_PERF_EVENT_ERROR = -1,
  1505. LIBBPF_PERF_EVENT_CONT = -2,
  1506. };
  1507. struct perf_event_header;
  1508. typedef enum bpf_perf_event_ret
  1509. (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
  1510. /* raw perf buffer options, giving most power and control */
  1511. struct perf_buffer_raw_opts {
  1512. size_t sz;
  1513. long :0;
  1514. long :0;
  1515. /* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
  1516. * max_entries of given PERF_EVENT_ARRAY map)
  1517. */
  1518. int cpu_cnt;
  1519. /* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
  1520. int *cpus;
  1521. /* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
  1522. int *map_keys;
  1523. };
  1524. #define perf_buffer_raw_opts__last_field map_keys
  1525. struct perf_event_attr;
  1526. LIBBPF_API struct perf_buffer *
  1527. perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
  1528. perf_buffer_event_fn event_cb, void *ctx,
  1529. const struct perf_buffer_raw_opts *opts);
  1530. LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
  1531. LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
  1532. LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
  1533. LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
  1534. LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
  1535. LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
  1536. LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
  1537. /**
  1538. * @brief **perf_buffer__buffer()** returns the per-cpu raw mmap()'ed underlying
  1539. * memory region of the ring buffer.
  1540. * This ring buffer can be used to implement a custom events consumer.
  1541. * The ring buffer starts with the *struct perf_event_mmap_page*, which
  1542. * holds the ring buffer management fields, when accessing the header
  1543. * structure it's important to be SMP aware.
  1544. * You can refer to *perf_event_read_simple* for a simple example.
  1545. * @param pb the perf buffer structure
  1546. * @param buf_idx the buffer index to retrieve
  1547. * @param buf (out) gets the base pointer of the mmap()'ed memory
  1548. * @param buf_size (out) gets the size of the mmap()'ed region
  1549. * @return 0 on success, negative error code for failure
  1550. */
  1551. LIBBPF_API int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf,
  1552. size_t *buf_size);
  1553. struct bpf_prog_linfo;
  1554. struct bpf_prog_info;
  1555. LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
  1556. LIBBPF_API struct bpf_prog_linfo *
  1557. bpf_prog_linfo__new(const struct bpf_prog_info *info);
  1558. LIBBPF_API const struct bpf_line_info *
  1559. bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
  1560. __u64 addr, __u32 func_idx, __u32 nr_skip);
  1561. LIBBPF_API const struct bpf_line_info *
  1562. bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
  1563. __u32 insn_off, __u32 nr_skip);
  1564. /*
  1565. * Probe for supported system features
  1566. *
  1567. * Note that running many of these probes in a short amount of time can cause
  1568. * the kernel to reach the maximal size of lockable memory allowed for the
  1569. * user, causing subsequent probes to fail. In this case, the caller may want
  1570. * to adjust that limit with setrlimit().
  1571. */
  1572. /**
  1573. * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports
  1574. * BPF programs of a given type.
  1575. * @param prog_type BPF program type to detect kernel support for
  1576. * @param opts reserved for future extensibility, should be NULL
  1577. * @return 1, if given program type is supported; 0, if given program type is
  1578. * not supported; negative error code if feature detection failed or can't be
  1579. * performed
  1580. *
  1581. * Make sure the process has required set of CAP_* permissions (or runs as
  1582. * root) when performing feature checking.
  1583. */
  1584. LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);
  1585. /**
  1586. * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports
  1587. * BPF maps of a given type.
  1588. * @param map_type BPF map type to detect kernel support for
  1589. * @param opts reserved for future extensibility, should be NULL
  1590. * @return 1, if given map type is supported; 0, if given map type is
  1591. * not supported; negative error code if feature detection failed or can't be
  1592. * performed
  1593. *
  1594. * Make sure the process has required set of CAP_* permissions (or runs as
  1595. * root) when performing feature checking.
  1596. */
  1597. LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);
  1598. /**
  1599. * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the
  1600. * use of a given BPF helper from specified BPF program type.
  1601. * @param prog_type BPF program type used to check the support of BPF helper
  1602. * @param helper_id BPF helper ID (enum bpf_func_id) to check support for
  1603. * @param opts reserved for future extensibility, should be NULL
  1604. * @return 1, if given combination of program type and helper is supported; 0,
  1605. * if the combination is not supported; negative error code if feature
  1606. * detection for provided input arguments failed or can't be performed
  1607. *
  1608. * Make sure the process has required set of CAP_* permissions (or runs as
  1609. * root) when performing feature checking.
  1610. */
  1611. LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
  1612. enum bpf_func_id helper_id, const void *opts);
  1613. /**
  1614. * @brief **libbpf_num_possible_cpus()** is a helper function to get the
  1615. * number of possible CPUs that the host kernel supports and expects.
  1616. * @return number of possible CPUs; or error code on failure
  1617. *
  1618. * Example usage:
  1619. *
  1620. * int ncpus = libbpf_num_possible_cpus();
  1621. * if (ncpus < 0) {
  1622. * // error handling
  1623. * }
  1624. * long values[ncpus];
  1625. * bpf_map_lookup_elem(per_cpu_map_fd, key, values);
  1626. */
  1627. LIBBPF_API int libbpf_num_possible_cpus(void);
  1628. struct bpf_map_skeleton {
  1629. const char *name;
  1630. struct bpf_map **map;
  1631. void **mmaped;
  1632. struct bpf_link **link;
  1633. };
  1634. struct bpf_prog_skeleton {
  1635. const char *name;
  1636. struct bpf_program **prog;
  1637. struct bpf_link **link;
  1638. };
  1639. struct bpf_object_skeleton {
  1640. size_t sz; /* size of this struct, for forward/backward compatibility */
  1641. const char *name;
  1642. const void *data;
  1643. size_t data_sz;
  1644. struct bpf_object **obj;
  1645. int map_cnt;
  1646. int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
  1647. struct bpf_map_skeleton *maps;
  1648. int prog_cnt;
  1649. int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
  1650. struct bpf_prog_skeleton *progs;
  1651. };
  1652. LIBBPF_API int
  1653. bpf_object__open_skeleton(struct bpf_object_skeleton *s,
  1654. const struct bpf_object_open_opts *opts);
  1655. LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
  1656. LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
  1657. LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
  1658. LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
  1659. struct bpf_var_skeleton {
  1660. const char *name;
  1661. struct bpf_map **map;
  1662. void **addr;
  1663. };
  1664. struct bpf_object_subskeleton {
  1665. size_t sz; /* size of this struct, for forward/backward compatibility */
  1666. const struct bpf_object *obj;
  1667. int map_cnt;
  1668. int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
  1669. struct bpf_map_skeleton *maps;
  1670. int prog_cnt;
  1671. int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
  1672. struct bpf_prog_skeleton *progs;
  1673. int var_cnt;
  1674. int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */
  1675. struct bpf_var_skeleton *vars;
  1676. };
  1677. LIBBPF_API int
  1678. bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);
  1679. LIBBPF_API void
  1680. bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);
  1681. struct gen_loader_opts {
  1682. size_t sz; /* size of this struct, for forward/backward compatibility */
  1683. const char *data;
  1684. const char *insns;
  1685. __u32 data_sz;
  1686. __u32 insns_sz;
  1687. bool gen_hash;
  1688. };
  1689. #define gen_loader_opts__last_field gen_hash
  1690. LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
  1691. struct gen_loader_opts *opts);
  1692. enum libbpf_tristate {
  1693. TRI_NO = 0,
  1694. TRI_YES = 1,
  1695. TRI_MODULE = 2,
  1696. };
  1697. struct bpf_linker_opts {
  1698. /* size of this struct, for forward/backward compatibility */
  1699. size_t sz;
  1700. };
  1701. #define bpf_linker_opts__last_field sz
  1702. struct bpf_linker_file_opts {
  1703. /* size of this struct, for forward/backward compatibility */
  1704. size_t sz;
  1705. };
  1706. #define bpf_linker_file_opts__last_field sz
  1707. struct bpf_linker;
  1708. LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
  1709. LIBBPF_API struct bpf_linker *bpf_linker__new_fd(int fd, struct bpf_linker_opts *opts);
  1710. LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
  1711. const char *filename,
  1712. const struct bpf_linker_file_opts *opts);
  1713. LIBBPF_API int bpf_linker__add_fd(struct bpf_linker *linker, int fd,
  1714. const struct bpf_linker_file_opts *opts);
  1715. LIBBPF_API int bpf_linker__add_buf(struct bpf_linker *linker, void *buf, size_t buf_sz,
  1716. const struct bpf_linker_file_opts *opts);
  1717. LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
  1718. LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
  1719. /*
  1720. * Custom handling of BPF program's SEC() definitions
  1721. */
  1722. struct bpf_prog_load_opts; /* defined in bpf.h */
  1723. /* Called during bpf_object__open() for each recognized BPF program. Callback
  1724. * can use various bpf_program__set_*() setters to adjust whatever properties
  1725. * are necessary.
  1726. */
  1727. typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);
  1728. /* Called right before libbpf performs bpf_prog_load() to load BPF program
  1729. * into the kernel. Callback can adjust opts as necessary.
  1730. */
  1731. typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,
  1732. struct bpf_prog_load_opts *opts, long cookie);
  1733. /* Called during skeleton attach or through bpf_program__attach(). If
  1734. * auto-attach is not supported, callback should return 0 and set link to
  1735. * NULL (it's not considered an error during skeleton attach, but it will be
  1736. * an error for bpf_program__attach() calls). On error, error should be
  1737. * returned directly and link set to NULL. On success, return 0 and set link
  1738. * to a valid struct bpf_link.
  1739. */
  1740. typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,
  1741. struct bpf_link **link);
  1742. struct libbpf_prog_handler_opts {
  1743. /* size of this struct, for forward/backward compatibility */
  1744. size_t sz;
  1745. /* User-provided value that is passed to prog_setup_fn,
  1746. * prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to
  1747. * register one set of callbacks for multiple SEC() definitions and
  1748. * still be able to distinguish them, if necessary. For example,
  1749. * libbpf itself is using this to pass necessary flags (e.g.,
  1750. * sleepable flag) to a common internal SEC() handler.
  1751. */
  1752. long cookie;
  1753. /* BPF program initialization callback (see libbpf_prog_setup_fn_t).
  1754. * Callback is optional, pass NULL if it's not necessary.
  1755. */
  1756. libbpf_prog_setup_fn_t prog_setup_fn;
  1757. /* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).
  1758. * Callback is optional, pass NULL if it's not necessary.
  1759. */
  1760. libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
  1761. /* BPF program attach callback (see libbpf_prog_attach_fn_t).
  1762. * Callback is optional, pass NULL if it's not necessary.
  1763. */
  1764. libbpf_prog_attach_fn_t prog_attach_fn;
  1765. };
  1766. #define libbpf_prog_handler_opts__last_field prog_attach_fn
  1767. /**
  1768. * @brief **libbpf_register_prog_handler()** registers a custom BPF program
  1769. * SEC() handler.
  1770. * @param sec section prefix for which custom handler is registered
  1771. * @param prog_type BPF program type associated with specified section
  1772. * @param exp_attach_type Expected BPF attach type associated with specified section
  1773. * @param opts optional cookie, callbacks, and other extra options
  1774. * @return Non-negative handler ID is returned on success. This handler ID has
  1775. * to be passed to *libbpf_unregister_prog_handler()* to unregister such
  1776. * custom handler. Negative error code is returned on error.
  1777. *
  1778. * *sec* defines which SEC() definitions are handled by this custom handler
  1779. * registration. *sec* can have few different forms:
  1780. * - if *sec* is just a plain string (e.g., "abc"), it will match only
  1781. * SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result
  1782. * in an error;
  1783. * - if *sec* is of the form "abc/", proper SEC() form is
  1784. * SEC("abc/something"), where acceptable "something" should be checked by
  1785. * *prog_init_fn* callback, if there are additional restrictions;
  1786. * - if *sec* is of the form "abc+", it will successfully match both
  1787. * SEC("abc") and SEC("abc/whatever") forms;
  1788. * - if *sec* is NULL, custom handler is registered for any BPF program that
  1789. * doesn't match any of the registered (custom or libbpf's own) SEC()
  1790. * handlers. There could be only one such generic custom handler registered
  1791. * at any given time.
  1792. *
  1793. * All custom handlers (except the one with *sec* == NULL) are processed
  1794. * before libbpf's own SEC() handlers. It is allowed to "override" libbpf's
  1795. * SEC() handlers by registering custom ones for the same section prefix
  1796. * (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")
  1797. * handler).
  1798. *
  1799. * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
  1800. * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
  1801. * to ensure synchronization if there is a risk of running this API from
  1802. * multiple threads simultaneously.
  1803. */
  1804. LIBBPF_API int libbpf_register_prog_handler(const char *sec,
  1805. enum bpf_prog_type prog_type,
  1806. enum bpf_attach_type exp_attach_type,
  1807. const struct libbpf_prog_handler_opts *opts);
  1808. /**
  1809. * @brief *libbpf_unregister_prog_handler()* unregisters previously registered
  1810. * custom BPF program SEC() handler.
  1811. * @param handler_id handler ID returned by *libbpf_register_prog_handler()*
  1812. * after successful registration
  1813. * @return 0 on success, negative error code if handler isn't found
  1814. *
  1815. * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
  1816. * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
  1817. * to ensure synchronization if there is a risk of running this API from
  1818. * multiple threads simultaneously.
  1819. */
  1820. LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);
  1821. #ifdef __cplusplus
  1822. } /* extern "C" */
  1823. #endif
  1824. #endif /* __LIBBPF_LIBBPF_H */