libbpf.h 73 KB

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