libbpf.h 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  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 <[email protected]>
  6. * Copyright (C) 2015 Wang Nan <[email protected]>
  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. LIBBPF_API __u32 libbpf_major_version(void);
  23. LIBBPF_API __u32 libbpf_minor_version(void);
  24. LIBBPF_API const char *libbpf_version_string(void);
  25. enum libbpf_errno {
  26. __LIBBPF_ERRNO__START = 4000,
  27. /* Something wrong in libelf */
  28. LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
  29. LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */
  30. LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
  31. LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */
  32. LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
  33. LIBBPF_ERRNO__RELOC, /* Relocation failed */
  34. LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
  35. LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */
  36. LIBBPF_ERRNO__PROG2BIG, /* Program too big */
  37. LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
  38. LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
  39. LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */
  40. LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */
  41. LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */
  42. __LIBBPF_ERRNO__END,
  43. };
  44. LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
  45. /**
  46. * @brief **libbpf_bpf_attach_type_str()** converts the provided attach type
  47. * value into a textual representation.
  48. * @param t The attach type.
  49. * @return Pointer to a static string identifying the attach type. NULL is
  50. * returned for unknown **bpf_attach_type** values.
  51. */
  52. LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t);
  53. /**
  54. * @brief **libbpf_bpf_link_type_str()** converts the provided link type value
  55. * into a textual representation.
  56. * @param t The link type.
  57. * @return Pointer to a static string identifying the link type. NULL is
  58. * returned for unknown **bpf_link_type** values.
  59. */
  60. LIBBPF_API const char *libbpf_bpf_link_type_str(enum bpf_link_type t);
  61. /**
  62. * @brief **libbpf_bpf_map_type_str()** converts the provided map type value
  63. * into a textual representation.
  64. * @param t The map type.
  65. * @return Pointer to a static string identifying the map type. NULL is
  66. * returned for unknown **bpf_map_type** values.
  67. */
  68. LIBBPF_API const char *libbpf_bpf_map_type_str(enum bpf_map_type t);
  69. /**
  70. * @brief **libbpf_bpf_prog_type_str()** converts the provided program type
  71. * value into a textual representation.
  72. * @param t The program type.
  73. * @return Pointer to a static string identifying the program type. NULL is
  74. * returned for unknown **bpf_prog_type** values.
  75. */
  76. LIBBPF_API const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t);
  77. enum libbpf_print_level {
  78. LIBBPF_WARN,
  79. LIBBPF_INFO,
  80. LIBBPF_DEBUG,
  81. };
  82. typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
  83. const char *, va_list ap);
  84. LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
  85. /* Hide internal to user */
  86. struct bpf_object;
  87. struct bpf_object_open_opts {
  88. /* size of this struct, for forward/backward compatibility */
  89. size_t sz;
  90. /* object name override, if provided:
  91. * - for object open from file, this will override setting object
  92. * name from file path's base name;
  93. * - for object open from memory buffer, this will specify an object
  94. * name and will override default "<addr>-<buf-size>" name;
  95. */
  96. const char *object_name;
  97. /* parse map definitions non-strictly, allowing extra attributes/data */
  98. bool relaxed_maps;
  99. /* maps that set the 'pinning' attribute in their definition will have
  100. * their pin_path attribute set to a file in this directory, and be
  101. * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
  102. */
  103. const char *pin_root_path;
  104. __u32 :32; /* stub out now removed attach_prog_fd */
  105. /* Additional kernel config content that augments and overrides
  106. * system Kconfig for CONFIG_xxx externs.
  107. */
  108. const char *kconfig;
  109. /* Path to the custom BTF to be used for BPF CO-RE relocations.
  110. * This custom BTF completely replaces the use of vmlinux BTF
  111. * for the purpose of CO-RE relocations.
  112. * NOTE: any other BPF feature (e.g., fentry/fexit programs,
  113. * struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.
  114. */
  115. const char *btf_custom_path;
  116. /* Pointer to a buffer for storing kernel logs for applicable BPF
  117. * commands. Valid kernel_log_size has to be specified as well and are
  118. * passed-through to bpf() syscall. Keep in mind that kernel might
  119. * fail operation with -ENOSPC error if provided buffer is too small
  120. * to contain entire log output.
  121. * See the comment below for kernel_log_level for interaction between
  122. * log_buf and log_level settings.
  123. *
  124. * If specified, this log buffer will be passed for:
  125. * - each BPF progral load (BPF_PROG_LOAD) attempt, unless overriden
  126. * with bpf_program__set_log() on per-program level, to get
  127. * BPF verifier log output.
  128. * - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get
  129. * BTF sanity checking log.
  130. *
  131. * Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite
  132. * previous contents, so if you need more fine-grained control, set
  133. * per-program buffer with bpf_program__set_log_buf() to preserve each
  134. * individual program's verification log. Keep using kernel_log_buf
  135. * for BTF verification log, if necessary.
  136. */
  137. char *kernel_log_buf;
  138. size_t kernel_log_size;
  139. /*
  140. * Log level can be set independently from log buffer. Log_level=0
  141. * means that libbpf will attempt loading BTF or program without any
  142. * logging requested, but will retry with either its own or custom log
  143. * buffer, if provided, and log_level=1 on any error.
  144. * And vice versa, setting log_level>0 will request BTF or prog
  145. * loading with verbose log from the first attempt (and as such also
  146. * for successfully loaded BTF or program), and the actual log buffer
  147. * could be either libbpf's own auto-allocated log buffer, if
  148. * kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.
  149. * If user didn't provide custom log buffer, libbpf will emit captured
  150. * logs through its print callback.
  151. */
  152. __u32 kernel_log_level;
  153. size_t :0;
  154. };
  155. #define bpf_object_open_opts__last_field kernel_log_level
  156. LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
  157. /**
  158. * @brief **bpf_object__open_file()** creates a bpf_object by opening
  159. * the BPF ELF object file pointed to by the passed path and loading it
  160. * into memory.
  161. * @param path BPF object file path
  162. * @param opts options for how to load the bpf object, this parameter is
  163. * optional and can be set to NULL
  164. * @return pointer to the new bpf_object; or NULL is returned on error,
  165. * error code is stored in errno
  166. */
  167. LIBBPF_API struct bpf_object *
  168. bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
  169. /**
  170. * @brief **bpf_object__open_mem()** creates a bpf_object by reading
  171. * the BPF objects raw bytes from a memory buffer containing a valid
  172. * BPF ELF object file.
  173. * @param obj_buf pointer to the buffer containing ELF file bytes
  174. * @param obj_buf_sz number of bytes in the buffer
  175. * @param opts options for how to load the bpf object
  176. * @return pointer to the new bpf_object; or NULL is returned on error,
  177. * error code is stored in errno
  178. */
  179. LIBBPF_API struct bpf_object *
  180. bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
  181. const struct bpf_object_open_opts *opts);
  182. /* Load/unload object into/from kernel */
  183. LIBBPF_API int bpf_object__load(struct bpf_object *obj);
  184. LIBBPF_API void bpf_object__close(struct bpf_object *object);
  185. /* pin_maps and unpin_maps can both be called with a NULL path, in which case
  186. * they will use the pin_path attribute of each map (and ignore all maps that
  187. * don't have a pin_path set).
  188. */
  189. LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
  190. LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
  191. const char *path);
  192. LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
  193. const char *path);
  194. LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
  195. const char *path);
  196. LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
  197. LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
  198. LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
  199. LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);
  200. struct btf;
  201. LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
  202. LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
  203. LIBBPF_API struct bpf_program *
  204. bpf_object__find_program_by_name(const struct bpf_object *obj,
  205. const char *name);
  206. LIBBPF_API int
  207. libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
  208. enum bpf_attach_type *expected_attach_type);
  209. LIBBPF_API int libbpf_attach_type_by_name(const char *name,
  210. enum bpf_attach_type *attach_type);
  211. LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
  212. enum bpf_attach_type attach_type);
  213. /* Accessors of bpf_program */
  214. struct bpf_program;
  215. LIBBPF_API struct bpf_program *
  216. bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);
  217. #define bpf_object__for_each_program(pos, obj) \
  218. for ((pos) = bpf_object__next_program((obj), NULL); \
  219. (pos) != NULL; \
  220. (pos) = bpf_object__next_program((obj), (pos)))
  221. LIBBPF_API struct bpf_program *
  222. bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);
  223. LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
  224. __u32 ifindex);
  225. LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
  226. LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
  227. LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
  228. LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
  229. LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog);
  230. LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach);
  231. struct bpf_insn;
  232. /**
  233. * @brief **bpf_program__insns()** gives read-only access to BPF program's
  234. * underlying BPF instructions.
  235. * @param prog BPF program for which to return instructions
  236. * @return a pointer to an array of BPF instructions that belong to the
  237. * specified BPF program
  238. *
  239. * Returned pointer is always valid and not NULL. Number of `struct bpf_insn`
  240. * pointed to can be fetched using **bpf_program__insn_cnt()** API.
  241. *
  242. * Keep in mind, libbpf can modify and append/delete BPF program's
  243. * instructions as it processes BPF object file and prepares everything for
  244. * uploading into the kernel. So depending on the point in BPF object
  245. * lifetime, **bpf_program__insns()** can return different sets of
  246. * instructions. As an example, during BPF object load phase BPF program
  247. * instructions will be CO-RE-relocated, BPF subprograms instructions will be
  248. * appended, ldimm64 instructions will have FDs embedded, etc. So instructions
  249. * returned before **bpf_object__load()** and after it might be quite
  250. * different.
  251. */
  252. LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
  253. /**
  254. * @brief **bpf_program__set_insns()** can set BPF program's underlying
  255. * BPF instructions.
  256. *
  257. * WARNING: This is a very advanced libbpf API and users need to know
  258. * what they are doing. This should be used from prog_prepare_load_fn
  259. * callback only.
  260. *
  261. * @param prog BPF program for which to return instructions
  262. * @param new_insns a pointer to an array of BPF instructions
  263. * @param new_insn_cnt number of `struct bpf_insn`'s that form
  264. * specified BPF program
  265. * @return 0, on success; negative error code, otherwise
  266. */
  267. LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog,
  268. struct bpf_insn *new_insns, size_t new_insn_cnt);
  269. /**
  270. * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
  271. * that form specified BPF program.
  272. * @param prog BPF program for which to return number of BPF instructions
  273. *
  274. * See **bpf_program__insns()** documentation for notes on how libbpf can
  275. * change instructions and their count during different phases of
  276. * **bpf_object** lifetime.
  277. */
  278. LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);
  279. LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
  280. /**
  281. * @brief **bpf_program__pin()** pins the BPF program to a file
  282. * in the BPF FS specified by a path. This increments the programs
  283. * reference count, allowing it to stay loaded after the process
  284. * which loaded it has exited.
  285. *
  286. * @param prog BPF program to pin, must already be loaded
  287. * @param path file path in a BPF file system
  288. * @return 0, on success; negative error code, otherwise
  289. */
  290. LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
  291. /**
  292. * @brief **bpf_program__unpin()** unpins the BPF program from a file
  293. * in the BPFFS specified by a path. This decrements the programs
  294. * reference count.
  295. *
  296. * The file pinning the BPF program can also be unlinked by a different
  297. * process in which case this function will return an error.
  298. *
  299. * @param prog BPF program to unpin
  300. * @param path file path to the pin in a BPF file system
  301. * @return 0, on success; negative error code, otherwise
  302. */
  303. LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
  304. LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
  305. struct bpf_link;
  306. LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
  307. LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
  308. LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
  309. /**
  310. * @brief **bpf_link__pin()** pins the BPF link to a file
  311. * in the BPF FS specified by a path. This increments the links
  312. * reference count, allowing it to stay loaded after the process
  313. * which loaded it has exited.
  314. *
  315. * @param link BPF link to pin, must already be loaded
  316. * @param path file path in a BPF file system
  317. * @return 0, on success; negative error code, otherwise
  318. */
  319. LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
  320. /**
  321. * @brief **bpf_link__unpin()** unpins the BPF link from a file
  322. * in the BPFFS specified by a path. This decrements the links
  323. * reference count.
  324. *
  325. * The file pinning the BPF link can also be unlinked by a different
  326. * process in which case this function will return an error.
  327. *
  328. * @param prog BPF program to unpin
  329. * @param path file path to the pin in a BPF file system
  330. * @return 0, on success; negative error code, otherwise
  331. */
  332. LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
  333. LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
  334. struct bpf_program *prog);
  335. LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
  336. LIBBPF_API int bpf_link__detach(struct bpf_link *link);
  337. LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
  338. /**
  339. * @brief **bpf_program__attach()** is a generic function for attaching
  340. * a BPF program based on auto-detection of program type, attach type,
  341. * and extra paremeters, where applicable.
  342. *
  343. * @param prog BPF program to attach
  344. * @return Reference to the newly created BPF link; or NULL is returned on error,
  345. * error code is stored in errno
  346. *
  347. * This is supported for:
  348. * - kprobe/kretprobe (depends on SEC() definition)
  349. * - uprobe/uretprobe (depends on SEC() definition)
  350. * - tracepoint
  351. * - raw tracepoint
  352. * - tracing programs (typed raw TP/fentry/fexit/fmod_ret)
  353. */
  354. LIBBPF_API struct bpf_link *
  355. bpf_program__attach(const struct bpf_program *prog);
  356. struct bpf_perf_event_opts {
  357. /* size of this struct, for forward/backward compatiblity */
  358. size_t sz;
  359. /* custom user-provided value fetchable through bpf_get_attach_cookie() */
  360. __u64 bpf_cookie;
  361. };
  362. #define bpf_perf_event_opts__last_field bpf_cookie
  363. LIBBPF_API struct bpf_link *
  364. bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
  365. LIBBPF_API struct bpf_link *
  366. bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
  367. const struct bpf_perf_event_opts *opts);
  368. struct bpf_kprobe_opts {
  369. /* size of this struct, for forward/backward compatiblity */
  370. size_t sz;
  371. /* custom user-provided value fetchable through bpf_get_attach_cookie() */
  372. __u64 bpf_cookie;
  373. /* function's offset to install kprobe to */
  374. size_t offset;
  375. /* kprobe is return probe */
  376. bool retprobe;
  377. size_t :0;
  378. };
  379. #define bpf_kprobe_opts__last_field retprobe
  380. LIBBPF_API struct bpf_link *
  381. bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
  382. const char *func_name);
  383. LIBBPF_API struct bpf_link *
  384. bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
  385. const char *func_name,
  386. const struct bpf_kprobe_opts *opts);
  387. struct bpf_kprobe_multi_opts {
  388. /* size of this struct, for forward/backward compatibility */
  389. size_t sz;
  390. /* array of function symbols to attach */
  391. const char **syms;
  392. /* array of function addresses to attach */
  393. const unsigned long *addrs;
  394. /* array of user-provided values fetchable through bpf_get_attach_cookie */
  395. const __u64 *cookies;
  396. /* number of elements in syms/addrs/cookies arrays */
  397. size_t cnt;
  398. /* create return kprobes */
  399. bool retprobe;
  400. size_t :0;
  401. };
  402. #define bpf_kprobe_multi_opts__last_field retprobe
  403. LIBBPF_API struct bpf_link *
  404. bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
  405. const char *pattern,
  406. const struct bpf_kprobe_multi_opts *opts);
  407. struct bpf_ksyscall_opts {
  408. /* size of this struct, for forward/backward compatiblity */
  409. size_t sz;
  410. /* custom user-provided value fetchable through bpf_get_attach_cookie() */
  411. __u64 bpf_cookie;
  412. /* attach as return probe? */
  413. bool retprobe;
  414. size_t :0;
  415. };
  416. #define bpf_ksyscall_opts__last_field retprobe
  417. /**
  418. * @brief **bpf_program__attach_ksyscall()** attaches a BPF program
  419. * to kernel syscall handler of a specified syscall. Optionally it's possible
  420. * to request to install retprobe that will be triggered at syscall exit. It's
  421. * also possible to associate BPF cookie (though options).
  422. *
  423. * Libbpf automatically will determine correct full kernel function name,
  424. * which depending on system architecture and kernel version/configuration
  425. * could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will
  426. * attach specified program using kprobe/kretprobe mechanism.
  427. *
  428. * **bpf_program__attach_ksyscall()** is an API counterpart of declarative
  429. * **SEC("ksyscall/<syscall>")** annotation of BPF programs.
  430. *
  431. * At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do
  432. * not handle all the calling convention quirks for mmap(), clone() and compat
  433. * syscalls. It also only attaches to "native" syscall interfaces. If host
  434. * system supports compat syscalls or defines 32-bit syscalls in 64-bit
  435. * kernel, such syscall interfaces won't be attached to by libbpf.
  436. *
  437. * These limitations may or may not change in the future. Therefore it is
  438. * recommended to use SEC("kprobe") for these syscalls or if working with
  439. * compat and 32-bit interfaces is required.
  440. *
  441. * @param prog BPF program to attach
  442. * @param syscall_name Symbolic name of the syscall (e.g., "bpf")
  443. * @param opts Additional options (see **struct bpf_ksyscall_opts**)
  444. * @return Reference to the newly created BPF link; or NULL is returned on
  445. * error, error code is stored in errno
  446. */
  447. LIBBPF_API struct bpf_link *
  448. bpf_program__attach_ksyscall(const struct bpf_program *prog,
  449. const char *syscall_name,
  450. const struct bpf_ksyscall_opts *opts);
  451. struct bpf_uprobe_opts {
  452. /* size of this struct, for forward/backward compatiblity */
  453. size_t sz;
  454. /* offset of kernel reference counted USDT semaphore, added in
  455. * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
  456. */
  457. size_t ref_ctr_offset;
  458. /* custom user-provided value fetchable through bpf_get_attach_cookie() */
  459. __u64 bpf_cookie;
  460. /* uprobe is return probe, invoked at function return time */
  461. bool retprobe;
  462. /* Function name to attach to. Could be an unqualified ("abc") or library-qualified
  463. * "abc@LIBXYZ" name. To specify function entry, func_name should be set while
  464. * func_offset argument to bpf_prog__attach_uprobe_opts() should be 0. To trace an
  465. * offset within a function, specify func_name and use func_offset argument to specify
  466. * offset within the function. Shared library functions must specify the shared library
  467. * binary_path.
  468. */
  469. const char *func_name;
  470. size_t :0;
  471. };
  472. #define bpf_uprobe_opts__last_field func_name
  473. /**
  474. * @brief **bpf_program__attach_uprobe()** attaches a BPF program
  475. * to the userspace function which is found by binary path and
  476. * offset. You can optionally specify a particular proccess to attach
  477. * to. You can also optionally attach the program to the function
  478. * exit instead of entry.
  479. *
  480. * @param prog BPF program to attach
  481. * @param retprobe Attach to function exit
  482. * @param pid Process ID to attach the uprobe to, 0 for self (own process),
  483. * -1 for all processes
  484. * @param binary_path Path to binary that contains the function symbol
  485. * @param func_offset Offset within the binary of the function symbol
  486. * @return Reference to the newly created BPF link; or NULL is returned on error,
  487. * error code is stored in errno
  488. */
  489. LIBBPF_API struct bpf_link *
  490. bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
  491. pid_t pid, const char *binary_path,
  492. size_t func_offset);
  493. /**
  494. * @brief **bpf_program__attach_uprobe_opts()** is just like
  495. * bpf_program__attach_uprobe() except with a options struct
  496. * for various configurations.
  497. *
  498. * @param prog BPF program to attach
  499. * @param pid Process ID to attach the uprobe to, 0 for self (own process),
  500. * -1 for all processes
  501. * @param binary_path Path to binary that contains the function symbol
  502. * @param func_offset Offset within the binary of the function symbol
  503. * @param opts Options for altering program attachment
  504. * @return Reference to the newly created BPF link; or NULL is returned on error,
  505. * error code is stored in errno
  506. */
  507. LIBBPF_API struct bpf_link *
  508. bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
  509. const char *binary_path, size_t func_offset,
  510. const struct bpf_uprobe_opts *opts);
  511. struct bpf_usdt_opts {
  512. /* size of this struct, for forward/backward compatibility */
  513. size_t sz;
  514. /* custom user-provided value accessible through usdt_cookie() */
  515. __u64 usdt_cookie;
  516. size_t :0;
  517. };
  518. #define bpf_usdt_opts__last_field usdt_cookie
  519. /**
  520. * @brief **bpf_program__attach_usdt()** is just like
  521. * bpf_program__attach_uprobe_opts() except it covers USDT (User-space
  522. * Statically Defined Tracepoint) attachment, instead of attaching to
  523. * user-space function entry or exit.
  524. *
  525. * @param prog BPF program to attach
  526. * @param pid Process ID to attach the uprobe to, 0 for self (own process),
  527. * -1 for all processes
  528. * @param binary_path Path to binary that contains provided USDT probe
  529. * @param usdt_provider USDT provider name
  530. * @param usdt_name USDT probe name
  531. * @param opts Options for altering program attachment
  532. * @return Reference to the newly created BPF link; or NULL is returned on error,
  533. * error code is stored in errno
  534. */
  535. LIBBPF_API struct bpf_link *
  536. bpf_program__attach_usdt(const struct bpf_program *prog,
  537. pid_t pid, const char *binary_path,
  538. const char *usdt_provider, const char *usdt_name,
  539. const struct bpf_usdt_opts *opts);
  540. struct bpf_tracepoint_opts {
  541. /* size of this struct, for forward/backward compatiblity */
  542. size_t sz;
  543. /* custom user-provided value fetchable through bpf_get_attach_cookie() */
  544. __u64 bpf_cookie;
  545. };
  546. #define bpf_tracepoint_opts__last_field bpf_cookie
  547. LIBBPF_API struct bpf_link *
  548. bpf_program__attach_tracepoint(const struct bpf_program *prog,
  549. const char *tp_category,
  550. const char *tp_name);
  551. LIBBPF_API struct bpf_link *
  552. bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
  553. const char *tp_category,
  554. const char *tp_name,
  555. const struct bpf_tracepoint_opts *opts);
  556. LIBBPF_API struct bpf_link *
  557. bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
  558. const char *tp_name);
  559. struct bpf_trace_opts {
  560. /* size of this struct, for forward/backward compatibility */
  561. size_t sz;
  562. /* custom user-provided value fetchable through bpf_get_attach_cookie() */
  563. __u64 cookie;
  564. };
  565. #define bpf_trace_opts__last_field cookie
  566. LIBBPF_API struct bpf_link *
  567. bpf_program__attach_trace(const struct bpf_program *prog);
  568. LIBBPF_API struct bpf_link *
  569. bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);
  570. LIBBPF_API struct bpf_link *
  571. bpf_program__attach_lsm(const struct bpf_program *prog);
  572. LIBBPF_API struct bpf_link *
  573. bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
  574. LIBBPF_API struct bpf_link *
  575. bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
  576. LIBBPF_API struct bpf_link *
  577. bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
  578. LIBBPF_API struct bpf_link *
  579. bpf_program__attach_freplace(const struct bpf_program *prog,
  580. int target_fd, const char *attach_func_name);
  581. struct bpf_map;
  582. LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
  583. struct bpf_iter_attach_opts {
  584. size_t sz; /* size of this struct for forward/backward compatibility */
  585. union bpf_iter_link_info *link_info;
  586. __u32 link_info_len;
  587. };
  588. #define bpf_iter_attach_opts__last_field link_info_len
  589. LIBBPF_API struct bpf_link *
  590. bpf_program__attach_iter(const struct bpf_program *prog,
  591. const struct bpf_iter_attach_opts *opts);
  592. LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);
  593. /**
  594. * @brief **bpf_program__set_type()** sets the program
  595. * type of the passed BPF program.
  596. * @param prog BPF program to set the program type for
  597. * @param type program type to set the BPF map to have
  598. * @return error code; or 0 if no error. An error occurs
  599. * if the object is already loaded.
  600. *
  601. * This must be called before the BPF object is loaded,
  602. * otherwise it has no effect and an error is returned.
  603. */
  604. LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,
  605. enum bpf_prog_type type);
  606. LIBBPF_API enum bpf_attach_type
  607. bpf_program__expected_attach_type(const struct bpf_program *prog);
  608. /**
  609. * @brief **bpf_program__set_expected_attach_type()** sets the
  610. * attach type of the passed BPF program. This is used for
  611. * auto-detection of attachment when programs are loaded.
  612. * @param prog BPF program to set the attach type for
  613. * @param type attach type to set the BPF map to have
  614. * @return error code; or 0 if no error. An error occurs
  615. * if the object is already loaded.
  616. *
  617. * This must be called before the BPF object is loaded,
  618. * otherwise it has no effect and an error is returned.
  619. */
  620. LIBBPF_API int
  621. bpf_program__set_expected_attach_type(struct bpf_program *prog,
  622. enum bpf_attach_type type);
  623. LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
  624. LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
  625. /* Per-program log level and log buffer getters/setters.
  626. * See bpf_object_open_opts comments regarding log_level and log_buf
  627. * interactions.
  628. */
  629. LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);
  630. LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);
  631. LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
  632. LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
  633. /**
  634. * @brief **bpf_program__set_attach_target()** sets BTF-based attach target
  635. * for supported BPF program types:
  636. * - BTF-aware raw tracepoints (tp_btf);
  637. * - fentry/fexit/fmod_ret;
  638. * - lsm;
  639. * - freplace.
  640. * @param prog BPF program to set the attach type for
  641. * @param type attach type to set the BPF map to have
  642. * @return error code; or 0 if no error occurred.
  643. */
  644. LIBBPF_API int
  645. bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
  646. const char *attach_func_name);
  647. /**
  648. * @brief **bpf_object__find_map_by_name()** returns BPF map of
  649. * the given name, if it exists within the passed BPF object
  650. * @param obj BPF object
  651. * @param name name of the BPF map
  652. * @return BPF map instance, if such map exists within the BPF object;
  653. * or NULL otherwise.
  654. */
  655. LIBBPF_API struct bpf_map *
  656. bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
  657. LIBBPF_API int
  658. bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
  659. LIBBPF_API struct bpf_map *
  660. bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
  661. #define bpf_object__for_each_map(pos, obj) \
  662. for ((pos) = bpf_object__next_map((obj), NULL); \
  663. (pos) != NULL; \
  664. (pos) = bpf_object__next_map((obj), (pos)))
  665. #define bpf_map__for_each bpf_object__for_each_map
  666. LIBBPF_API struct bpf_map *
  667. bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
  668. /**
  669. * @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create
  670. * BPF map during BPF object load phase.
  671. * @param map the BPF map instance
  672. * @param autocreate whether to create BPF map during BPF object load
  673. * @return 0 on success; -EBUSY if BPF object was already loaded
  674. *
  675. * **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating
  676. * BPF map. By default, libbpf will attempt to create every single BPF map
  677. * defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall
  678. * and fill in map FD in BPF instructions.
  679. *
  680. * This API allows to opt-out of this process for specific map instance. This
  681. * can be useful if host kernel doesn't support such BPF map type or used
  682. * combination of flags and user application wants to avoid creating such
  683. * a map in the first place. User is still responsible to make sure that their
  684. * BPF-side code that expects to use such missing BPF map is recognized by BPF
  685. * verifier as dead code, otherwise BPF verifier will reject such BPF program.
  686. */
  687. LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);
  688. LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);
  689. /**
  690. * @brief **bpf_map__fd()** gets the file descriptor of the passed
  691. * BPF map
  692. * @param map the BPF map instance
  693. * @return the file descriptor; or -EINVAL in case of an error
  694. */
  695. LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
  696. LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
  697. /* get map name */
  698. LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
  699. /* get/set map type */
  700. LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
  701. LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
  702. /* get/set map size (max_entries) */
  703. LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
  704. LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
  705. /* get/set map flags */
  706. LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
  707. LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
  708. /* get/set map NUMA node */
  709. LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
  710. LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
  711. /* get/set map key size */
  712. LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
  713. LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
  714. /* get/set map value size */
  715. LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
  716. LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
  717. /* get map key/value BTF type IDs */
  718. LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
  719. LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
  720. /* get/set map if_index */
  721. LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
  722. LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
  723. /* get/set map map_extra flags */
  724. LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
  725. LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
  726. LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
  727. const void *data, size_t size);
  728. LIBBPF_API const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize);
  729. /**
  730. * @brief **bpf_map__is_internal()** tells the caller whether or not the
  731. * passed map is a special map created by libbpf automatically for things like
  732. * global variables, __ksym externs, Kconfig values, etc
  733. * @param map the bpf_map
  734. * @return true, if the map is an internal map; false, otherwise
  735. */
  736. LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
  737. LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
  738. LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
  739. LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
  740. LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
  741. LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
  742. LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
  743. LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
  744. /**
  745. * @brief **bpf_map__lookup_elem()** allows to lookup BPF map value
  746. * corresponding to provided key.
  747. * @param map BPF map to lookup element in
  748. * @param key pointer to memory containing bytes of the key used for lookup
  749. * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
  750. * @param value pointer to memory in which looked up value will be stored
  751. * @param value_sz size in byte of value data memory; it has to match BPF map
  752. * definition's **value_size**. For per-CPU BPF maps value size has to be
  753. * a product of BPF map value size and number of possible CPUs in the system
  754. * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
  755. * per-CPU values value size has to be aligned up to closest 8 bytes for
  756. * alignment reasons, so expected size is: `round_up(value_size, 8)
  757. * * libbpf_num_possible_cpus()`.
  758. * @flags extra flags passed to kernel for this operation
  759. * @return 0, on success; negative error, otherwise
  760. *
  761. * **bpf_map__lookup_elem()** is high-level equivalent of
  762. * **bpf_map_lookup_elem()** API with added check for key and value size.
  763. */
  764. LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,
  765. const void *key, size_t key_sz,
  766. void *value, size_t value_sz, __u64 flags);
  767. /**
  768. * @brief **bpf_map__update_elem()** allows to insert or update value in BPF
  769. * map that corresponds to provided key.
  770. * @param map BPF map to insert to or update element in
  771. * @param key pointer to memory containing bytes of the key
  772. * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
  773. * @param value pointer to memory containing bytes of the value
  774. * @param value_sz size in byte of value data memory; it has to match BPF map
  775. * definition's **value_size**. For per-CPU BPF maps value size has to be
  776. * a product of BPF map value size and number of possible CPUs in the system
  777. * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
  778. * per-CPU values value size has to be aligned up to closest 8 bytes for
  779. * alignment reasons, so expected size is: `round_up(value_size, 8)
  780. * * libbpf_num_possible_cpus()`.
  781. * @flags extra flags passed to kernel for this operation
  782. * @return 0, on success; negative error, otherwise
  783. *
  784. * **bpf_map__update_elem()** is high-level equivalent of
  785. * **bpf_map_update_elem()** API with added check for key and value size.
  786. */
  787. LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map,
  788. const void *key, size_t key_sz,
  789. const void *value, size_t value_sz, __u64 flags);
  790. /**
  791. * @brief **bpf_map__delete_elem()** allows to delete element in BPF map that
  792. * corresponds to provided key.
  793. * @param map BPF map to delete element from
  794. * @param key pointer to memory containing bytes of the key
  795. * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
  796. * @flags extra flags passed to kernel for this operation
  797. * @return 0, on success; negative error, otherwise
  798. *
  799. * **bpf_map__delete_elem()** is high-level equivalent of
  800. * **bpf_map_delete_elem()** API with added check for key size.
  801. */
  802. LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map,
  803. const void *key, size_t key_sz, __u64 flags);
  804. /**
  805. * @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value
  806. * corresponding to provided key and atomically delete it afterwards.
  807. * @param map BPF map to lookup element in
  808. * @param key pointer to memory containing bytes of the key used for lookup
  809. * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
  810. * @param value pointer to memory in which looked up value will be stored
  811. * @param value_sz size in byte of value data memory; it has to match BPF map
  812. * definition's **value_size**. For per-CPU BPF maps value size has to be
  813. * a product of BPF map value size and number of possible CPUs in the system
  814. * (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
  815. * per-CPU values value size has to be aligned up to closest 8 bytes for
  816. * alignment reasons, so expected size is: `round_up(value_size, 8)
  817. * * libbpf_num_possible_cpus()`.
  818. * @flags extra flags passed to kernel for this operation
  819. * @return 0, on success; negative error, otherwise
  820. *
  821. * **bpf_map__lookup_and_delete_elem()** is high-level equivalent of
  822. * **bpf_map_lookup_and_delete_elem()** API with added check for key and value size.
  823. */
  824. LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
  825. const void *key, size_t key_sz,
  826. void *value, size_t value_sz, __u64 flags);
  827. /**
  828. * @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by
  829. * fetching next key that follows current key.
  830. * @param map BPF map to fetch next key from
  831. * @param cur_key pointer to memory containing bytes of current key or NULL to
  832. * fetch the first key
  833. * @param next_key pointer to memory to write next key into
  834. * @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
  835. * @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map;
  836. * negative error, otherwise
  837. *
  838. * **bpf_map__get_next_key()** is high-level equivalent of
  839. * **bpf_map_get_next_key()** API with added check for key size.
  840. */
  841. LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map,
  842. const void *cur_key, void *next_key, size_t key_sz);
  843. struct bpf_xdp_set_link_opts {
  844. size_t sz;
  845. int old_fd;
  846. size_t :0;
  847. };
  848. #define bpf_xdp_set_link_opts__last_field old_fd
  849. struct bpf_xdp_attach_opts {
  850. size_t sz;
  851. int old_prog_fd;
  852. size_t :0;
  853. };
  854. #define bpf_xdp_attach_opts__last_field old_prog_fd
  855. struct bpf_xdp_query_opts {
  856. size_t sz;
  857. __u32 prog_id; /* output */
  858. __u32 drv_prog_id; /* output */
  859. __u32 hw_prog_id; /* output */
  860. __u32 skb_prog_id; /* output */
  861. __u8 attach_mode; /* output */
  862. size_t :0;
  863. };
  864. #define bpf_xdp_query_opts__last_field attach_mode
  865. LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,
  866. const struct bpf_xdp_attach_opts *opts);
  867. LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,
  868. const struct bpf_xdp_attach_opts *opts);
  869. LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);
  870. LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);
  871. /* TC related API */
  872. enum bpf_tc_attach_point {
  873. BPF_TC_INGRESS = 1 << 0,
  874. BPF_TC_EGRESS = 1 << 1,
  875. BPF_TC_CUSTOM = 1 << 2,
  876. };
  877. #define BPF_TC_PARENT(a, b) \
  878. ((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
  879. enum bpf_tc_flags {
  880. BPF_TC_F_REPLACE = 1 << 0,
  881. };
  882. struct bpf_tc_hook {
  883. size_t sz;
  884. int ifindex;
  885. enum bpf_tc_attach_point attach_point;
  886. __u32 parent;
  887. size_t :0;
  888. };
  889. #define bpf_tc_hook__last_field parent
  890. struct bpf_tc_opts {
  891. size_t sz;
  892. int prog_fd;
  893. __u32 flags;
  894. __u32 prog_id;
  895. __u32 handle;
  896. __u32 priority;
  897. size_t :0;
  898. };
  899. #define bpf_tc_opts__last_field priority
  900. LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
  901. LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
  902. LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
  903. struct bpf_tc_opts *opts);
  904. LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
  905. const struct bpf_tc_opts *opts);
  906. LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
  907. struct bpf_tc_opts *opts);
  908. /* Ring buffer APIs */
  909. struct ring_buffer;
  910. struct user_ring_buffer;
  911. typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
  912. struct ring_buffer_opts {
  913. size_t sz; /* size of this struct, for forward/backward compatiblity */
  914. };
  915. #define ring_buffer_opts__last_field sz
  916. LIBBPF_API struct ring_buffer *
  917. ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
  918. const struct ring_buffer_opts *opts);
  919. LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
  920. LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
  921. ring_buffer_sample_fn sample_cb, void *ctx);
  922. LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
  923. LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
  924. LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
  925. struct user_ring_buffer_opts {
  926. size_t sz; /* size of this struct, for forward/backward compatibility */
  927. };
  928. #define user_ring_buffer_opts__last_field sz
  929. /* @brief **user_ring_buffer__new()** creates a new instance of a user ring
  930. * buffer.
  931. *
  932. * @param map_fd A file descriptor to a BPF_MAP_TYPE_USER_RINGBUF map.
  933. * @param opts Options for how the ring buffer should be created.
  934. * @return A user ring buffer on success; NULL and errno being set on a
  935. * failure.
  936. */
  937. LIBBPF_API struct user_ring_buffer *
  938. user_ring_buffer__new(int map_fd, const struct user_ring_buffer_opts *opts);
  939. /* @brief **user_ring_buffer__reserve()** reserves a pointer to a sample in the
  940. * user ring buffer.
  941. * @param rb A pointer to a user ring buffer.
  942. * @param size The size of the sample, in bytes.
  943. * @return A pointer to an 8-byte aligned reserved region of the user ring
  944. * buffer; NULL, and errno being set if a sample could not be reserved.
  945. *
  946. * This function is *not* thread safe, and callers must synchronize accessing
  947. * this function if there are multiple producers. If a size is requested that
  948. * is larger than the size of the entire ring buffer, errno will be set to
  949. * E2BIG and NULL is returned. If the ring buffer could accommodate the size,
  950. * but currently does not have enough space, errno is set to ENOSPC and NULL is
  951. * returned.
  952. *
  953. * After initializing the sample, callers must invoke
  954. * **user_ring_buffer__submit()** to post the sample to the kernel. Otherwise,
  955. * the sample must be freed with **user_ring_buffer__discard()**.
  956. */
  957. LIBBPF_API void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);
  958. /* @brief **user_ring_buffer__reserve_blocking()** reserves a record in the
  959. * ring buffer, possibly blocking for up to @timeout_ms until a sample becomes
  960. * available.
  961. * @param rb The user ring buffer.
  962. * @param size The size of the sample, in bytes.
  963. * @param timeout_ms The amount of time, in milliseconds, for which the caller
  964. * should block when waiting for a sample. -1 causes the caller to block
  965. * indefinitely.
  966. * @return A pointer to an 8-byte aligned reserved region of the user ring
  967. * buffer; NULL, and errno being set if a sample could not be reserved.
  968. *
  969. * This function is *not* thread safe, and callers must synchronize
  970. * accessing this function if there are multiple producers
  971. *
  972. * If **timeout_ms** is -1, the function will block indefinitely until a sample
  973. * becomes available. Otherwise, **timeout_ms** must be non-negative, or errno
  974. * is set to EINVAL, and NULL is returned. If **timeout_ms** is 0, no blocking
  975. * will occur and the function will return immediately after attempting to
  976. * reserve a sample.
  977. *
  978. * If **size** is larger than the size of the entire ring buffer, errno is set
  979. * to E2BIG and NULL is returned. If the ring buffer could accommodate
  980. * **size**, but currently does not have enough space, the caller will block
  981. * until at most **timeout_ms** has elapsed. If insufficient space is available
  982. * at that time, errno is set to ENOSPC, and NULL is returned.
  983. *
  984. * The kernel guarantees that it will wake up this thread to check if
  985. * sufficient space is available in the ring buffer at least once per
  986. * invocation of the **bpf_ringbuf_drain()** helper function, provided that at
  987. * least one sample is consumed, and the BPF program did not invoke the
  988. * function with BPF_RB_NO_WAKEUP. A wakeup may occur sooner than that, but the
  989. * kernel does not guarantee this. If the helper function is invoked with
  990. * BPF_RB_FORCE_WAKEUP, a wakeup event will be sent even if no sample is
  991. * consumed.
  992. *
  993. * When a sample of size **size** is found within **timeout_ms**, a pointer to
  994. * the sample is returned. After initializing the sample, callers must invoke
  995. * **user_ring_buffer__submit()** to post the sample to the ring buffer.
  996. * Otherwise, the sample must be freed with **user_ring_buffer__discard()**.
  997. */
  998. LIBBPF_API void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,
  999. __u32 size,
  1000. int timeout_ms);
  1001. /* @brief **user_ring_buffer__submit()** submits a previously reserved sample
  1002. * into the ring buffer.
  1003. * @param rb The user ring buffer.
  1004. * @param sample A reserved sample.
  1005. *
  1006. * It is not necessary to synchronize amongst multiple producers when invoking
  1007. * this function.
  1008. */
  1009. LIBBPF_API void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample);
  1010. /* @brief **user_ring_buffer__discard()** discards a previously reserved sample.
  1011. * @param rb The user ring buffer.
  1012. * @param sample A reserved sample.
  1013. *
  1014. * It is not necessary to synchronize amongst multiple producers when invoking
  1015. * this function.
  1016. */
  1017. LIBBPF_API void user_ring_buffer__discard(struct user_ring_buffer *rb, void *sample);
  1018. /* @brief **user_ring_buffer__free()** frees a ring buffer that was previously
  1019. * created with **user_ring_buffer__new()**.
  1020. * @param rb The user ring buffer being freed.
  1021. */
  1022. LIBBPF_API void user_ring_buffer__free(struct user_ring_buffer *rb);
  1023. /* Perf buffer APIs */
  1024. struct perf_buffer;
  1025. typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
  1026. void *data, __u32 size);
  1027. typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
  1028. /* common use perf buffer options */
  1029. struct perf_buffer_opts {
  1030. size_t sz;
  1031. };
  1032. #define perf_buffer_opts__last_field sz
  1033. /**
  1034. * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
  1035. * BPF_PERF_EVENT_ARRAY map
  1036. * @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
  1037. * code to send data over to user-space
  1038. * @param page_cnt number of memory pages allocated for each per-CPU buffer
  1039. * @param sample_cb function called on each received data record
  1040. * @param lost_cb function called when record loss has occurred
  1041. * @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
  1042. * @return a new instance of struct perf_buffer on success, NULL on error with
  1043. * *errno* containing an error code
  1044. */
  1045. LIBBPF_API struct perf_buffer *
  1046. perf_buffer__new(int map_fd, size_t page_cnt,
  1047. perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
  1048. const struct perf_buffer_opts *opts);
  1049. enum bpf_perf_event_ret {
  1050. LIBBPF_PERF_EVENT_DONE = 0,
  1051. LIBBPF_PERF_EVENT_ERROR = -1,
  1052. LIBBPF_PERF_EVENT_CONT = -2,
  1053. };
  1054. struct perf_event_header;
  1055. typedef enum bpf_perf_event_ret
  1056. (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
  1057. /* raw perf buffer options, giving most power and control */
  1058. struct perf_buffer_raw_opts {
  1059. size_t sz;
  1060. long :0;
  1061. long :0;
  1062. /* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
  1063. * max_entries of given PERF_EVENT_ARRAY map)
  1064. */
  1065. int cpu_cnt;
  1066. /* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
  1067. int *cpus;
  1068. /* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
  1069. int *map_keys;
  1070. };
  1071. #define perf_buffer_raw_opts__last_field map_keys
  1072. struct perf_event_attr;
  1073. LIBBPF_API struct perf_buffer *
  1074. perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
  1075. perf_buffer_event_fn event_cb, void *ctx,
  1076. const struct perf_buffer_raw_opts *opts);
  1077. LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
  1078. LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
  1079. LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
  1080. LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
  1081. LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
  1082. LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
  1083. LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
  1084. /**
  1085. * @brief **perf_buffer__buffer()** returns the per-cpu raw mmap()'ed underlying
  1086. * memory region of the ring buffer.
  1087. * This ring buffer can be used to implement a custom events consumer.
  1088. * The ring buffer starts with the *struct perf_event_mmap_page*, which
  1089. * holds the ring buffer managment fields, when accessing the header
  1090. * structure it's important to be SMP aware.
  1091. * You can refer to *perf_event_read_simple* for a simple example.
  1092. * @param pb the perf buffer structure
  1093. * @param buf_idx the buffer index to retreive
  1094. * @param buf (out) gets the base pointer of the mmap()'ed memory
  1095. * @param buf_size (out) gets the size of the mmap()'ed region
  1096. * @return 0 on success, negative error code for failure
  1097. */
  1098. LIBBPF_API int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf,
  1099. size_t *buf_size);
  1100. struct bpf_prog_linfo;
  1101. struct bpf_prog_info;
  1102. LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
  1103. LIBBPF_API struct bpf_prog_linfo *
  1104. bpf_prog_linfo__new(const struct bpf_prog_info *info);
  1105. LIBBPF_API const struct bpf_line_info *
  1106. bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
  1107. __u64 addr, __u32 func_idx, __u32 nr_skip);
  1108. LIBBPF_API const struct bpf_line_info *
  1109. bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
  1110. __u32 insn_off, __u32 nr_skip);
  1111. /*
  1112. * Probe for supported system features
  1113. *
  1114. * Note that running many of these probes in a short amount of time can cause
  1115. * the kernel to reach the maximal size of lockable memory allowed for the
  1116. * user, causing subsequent probes to fail. In this case, the caller may want
  1117. * to adjust that limit with setrlimit().
  1118. */
  1119. /**
  1120. * @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports
  1121. * BPF programs of a given type.
  1122. * @param prog_type BPF program type to detect kernel support for
  1123. * @param opts reserved for future extensibility, should be NULL
  1124. * @return 1, if given program type is supported; 0, if given program type is
  1125. * not supported; negative error code if feature detection failed or can't be
  1126. * performed
  1127. *
  1128. * Make sure the process has required set of CAP_* permissions (or runs as
  1129. * root) when performing feature checking.
  1130. */
  1131. LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);
  1132. /**
  1133. * @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports
  1134. * BPF maps of a given type.
  1135. * @param map_type BPF map type to detect kernel support for
  1136. * @param opts reserved for future extensibility, should be NULL
  1137. * @return 1, if given map type is supported; 0, if given map type is
  1138. * not supported; negative error code if feature detection failed or can't be
  1139. * performed
  1140. *
  1141. * Make sure the process has required set of CAP_* permissions (or runs as
  1142. * root) when performing feature checking.
  1143. */
  1144. LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);
  1145. /**
  1146. * @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the
  1147. * use of a given BPF helper from specified BPF program type.
  1148. * @param prog_type BPF program type used to check the support of BPF helper
  1149. * @param helper_id BPF helper ID (enum bpf_func_id) to check support for
  1150. * @param opts reserved for future extensibility, should be NULL
  1151. * @return 1, if given combination of program type and helper is supported; 0,
  1152. * if the combination is not supported; negative error code if feature
  1153. * detection for provided input arguments failed or can't be performed
  1154. *
  1155. * Make sure the process has required set of CAP_* permissions (or runs as
  1156. * root) when performing feature checking.
  1157. */
  1158. LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
  1159. enum bpf_func_id helper_id, const void *opts);
  1160. /**
  1161. * @brief **libbpf_num_possible_cpus()** is a helper function to get the
  1162. * number of possible CPUs that the host kernel supports and expects.
  1163. * @return number of possible CPUs; or error code on failure
  1164. *
  1165. * Example usage:
  1166. *
  1167. * int ncpus = libbpf_num_possible_cpus();
  1168. * if (ncpus < 0) {
  1169. * // error handling
  1170. * }
  1171. * long values[ncpus];
  1172. * bpf_map_lookup_elem(per_cpu_map_fd, key, values);
  1173. */
  1174. LIBBPF_API int libbpf_num_possible_cpus(void);
  1175. struct bpf_map_skeleton {
  1176. const char *name;
  1177. struct bpf_map **map;
  1178. void **mmaped;
  1179. };
  1180. struct bpf_prog_skeleton {
  1181. const char *name;
  1182. struct bpf_program **prog;
  1183. struct bpf_link **link;
  1184. };
  1185. struct bpf_object_skeleton {
  1186. size_t sz; /* size of this struct, for forward/backward compatibility */
  1187. const char *name;
  1188. const void *data;
  1189. size_t data_sz;
  1190. struct bpf_object **obj;
  1191. int map_cnt;
  1192. int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
  1193. struct bpf_map_skeleton *maps;
  1194. int prog_cnt;
  1195. int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
  1196. struct bpf_prog_skeleton *progs;
  1197. };
  1198. LIBBPF_API int
  1199. bpf_object__open_skeleton(struct bpf_object_skeleton *s,
  1200. const struct bpf_object_open_opts *opts);
  1201. LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
  1202. LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
  1203. LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
  1204. LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
  1205. struct bpf_var_skeleton {
  1206. const char *name;
  1207. struct bpf_map **map;
  1208. void **addr;
  1209. };
  1210. struct bpf_object_subskeleton {
  1211. size_t sz; /* size of this struct, for forward/backward compatibility */
  1212. const struct bpf_object *obj;
  1213. int map_cnt;
  1214. int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
  1215. struct bpf_map_skeleton *maps;
  1216. int prog_cnt;
  1217. int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
  1218. struct bpf_prog_skeleton *progs;
  1219. int var_cnt;
  1220. int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */
  1221. struct bpf_var_skeleton *vars;
  1222. };
  1223. LIBBPF_API int
  1224. bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);
  1225. LIBBPF_API void
  1226. bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);
  1227. struct gen_loader_opts {
  1228. size_t sz; /* size of this struct, for forward/backward compatiblity */
  1229. const char *data;
  1230. const char *insns;
  1231. __u32 data_sz;
  1232. __u32 insns_sz;
  1233. };
  1234. #define gen_loader_opts__last_field insns_sz
  1235. LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
  1236. struct gen_loader_opts *opts);
  1237. enum libbpf_tristate {
  1238. TRI_NO = 0,
  1239. TRI_YES = 1,
  1240. TRI_MODULE = 2,
  1241. };
  1242. struct bpf_linker_opts {
  1243. /* size of this struct, for forward/backward compatiblity */
  1244. size_t sz;
  1245. };
  1246. #define bpf_linker_opts__last_field sz
  1247. struct bpf_linker_file_opts {
  1248. /* size of this struct, for forward/backward compatiblity */
  1249. size_t sz;
  1250. };
  1251. #define bpf_linker_file_opts__last_field sz
  1252. struct bpf_linker;
  1253. LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
  1254. LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
  1255. const char *filename,
  1256. const struct bpf_linker_file_opts *opts);
  1257. LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
  1258. LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
  1259. /*
  1260. * Custom handling of BPF program's SEC() definitions
  1261. */
  1262. struct bpf_prog_load_opts; /* defined in bpf.h */
  1263. /* Called during bpf_object__open() for each recognized BPF program. Callback
  1264. * can use various bpf_program__set_*() setters to adjust whatever properties
  1265. * are necessary.
  1266. */
  1267. typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);
  1268. /* Called right before libbpf performs bpf_prog_load() to load BPF program
  1269. * into the kernel. Callback can adjust opts as necessary.
  1270. */
  1271. typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,
  1272. struct bpf_prog_load_opts *opts, long cookie);
  1273. /* Called during skeleton attach or through bpf_program__attach(). If
  1274. * auto-attach is not supported, callback should return 0 and set link to
  1275. * NULL (it's not considered an error during skeleton attach, but it will be
  1276. * an error for bpf_program__attach() calls). On error, error should be
  1277. * returned directly and link set to NULL. On success, return 0 and set link
  1278. * to a valid struct bpf_link.
  1279. */
  1280. typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,
  1281. struct bpf_link **link);
  1282. struct libbpf_prog_handler_opts {
  1283. /* size of this struct, for forward/backward compatiblity */
  1284. size_t sz;
  1285. /* User-provided value that is passed to prog_setup_fn,
  1286. * prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to
  1287. * register one set of callbacks for multiple SEC() definitions and
  1288. * still be able to distinguish them, if necessary. For example,
  1289. * libbpf itself is using this to pass necessary flags (e.g.,
  1290. * sleepable flag) to a common internal SEC() handler.
  1291. */
  1292. long cookie;
  1293. /* BPF program initialization callback (see libbpf_prog_setup_fn_t).
  1294. * Callback is optional, pass NULL if it's not necessary.
  1295. */
  1296. libbpf_prog_setup_fn_t prog_setup_fn;
  1297. /* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).
  1298. * Callback is optional, pass NULL if it's not necessary.
  1299. */
  1300. libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
  1301. /* BPF program attach callback (see libbpf_prog_attach_fn_t).
  1302. * Callback is optional, pass NULL if it's not necessary.
  1303. */
  1304. libbpf_prog_attach_fn_t prog_attach_fn;
  1305. };
  1306. #define libbpf_prog_handler_opts__last_field prog_attach_fn
  1307. /**
  1308. * @brief **libbpf_register_prog_handler()** registers a custom BPF program
  1309. * SEC() handler.
  1310. * @param sec section prefix for which custom handler is registered
  1311. * @param prog_type BPF program type associated with specified section
  1312. * @param exp_attach_type Expected BPF attach type associated with specified section
  1313. * @param opts optional cookie, callbacks, and other extra options
  1314. * @return Non-negative handler ID is returned on success. This handler ID has
  1315. * to be passed to *libbpf_unregister_prog_handler()* to unregister such
  1316. * custom handler. Negative error code is returned on error.
  1317. *
  1318. * *sec* defines which SEC() definitions are handled by this custom handler
  1319. * registration. *sec* can have few different forms:
  1320. * - if *sec* is just a plain string (e.g., "abc"), it will match only
  1321. * SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result
  1322. * in an error;
  1323. * - if *sec* is of the form "abc/", proper SEC() form is
  1324. * SEC("abc/something"), where acceptable "something" should be checked by
  1325. * *prog_init_fn* callback, if there are additional restrictions;
  1326. * - if *sec* is of the form "abc+", it will successfully match both
  1327. * SEC("abc") and SEC("abc/whatever") forms;
  1328. * - if *sec* is NULL, custom handler is registered for any BPF program that
  1329. * doesn't match any of the registered (custom or libbpf's own) SEC()
  1330. * handlers. There could be only one such generic custom handler registered
  1331. * at any given time.
  1332. *
  1333. * All custom handlers (except the one with *sec* == NULL) are processed
  1334. * before libbpf's own SEC() handlers. It is allowed to "override" libbpf's
  1335. * SEC() handlers by registering custom ones for the same section prefix
  1336. * (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")
  1337. * handler).
  1338. *
  1339. * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
  1340. * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
  1341. * to ensure synchronization if there is a risk of running this API from
  1342. * multiple threads simultaneously.
  1343. */
  1344. LIBBPF_API int libbpf_register_prog_handler(const char *sec,
  1345. enum bpf_prog_type prog_type,
  1346. enum bpf_attach_type exp_attach_type,
  1347. const struct libbpf_prog_handler_opts *opts);
  1348. /**
  1349. * @brief *libbpf_unregister_prog_handler()* unregisters previously registered
  1350. * custom BPF program SEC() handler.
  1351. * @param handler_id handler ID returned by *libbpf_register_prog_handler()*
  1352. * after successful registration
  1353. * @return 0 on success, negative error code if handler isn't found
  1354. *
  1355. * Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
  1356. * libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
  1357. * to ensure synchronization if there is a risk of running this API from
  1358. * multiple threads simultaneously.
  1359. */
  1360. LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);
  1361. #ifdef __cplusplus
  1362. } /* extern "C" */
  1363. #endif
  1364. #endif /* __LIBBPF_LIBBPF_H */