bpftool-gen.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. .. SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2. ================
  3. bpftool-gen
  4. ================
  5. -------------------------------------------------------------------------------
  6. tool for BPF code-generation
  7. -------------------------------------------------------------------------------
  8. :Manual section: 8
  9. .. include:: substitutions.rst
  10. SYNOPSIS
  11. ========
  12. **bpftool** [*OPTIONS*] **gen** *COMMAND*
  13. *OPTIONS* := { |COMMON_OPTIONS| | { **-L** | **--use-loader** } }
  14. *COMMAND* := { **object** | **skeleton** | **help** }
  15. GEN COMMANDS
  16. =============
  17. | **bpftool** **gen object** *OUTPUT_FILE* *INPUT_FILE* [*INPUT_FILE*...]
  18. | **bpftool** **gen skeleton** *FILE* [**name** *OBJECT_NAME*]
  19. | **bpftool** **gen subskeleton** *FILE* [**name** *OBJECT_NAME*]
  20. | **bpftool** **gen min_core_btf** *INPUT* *OUTPUT* *OBJECT* [*OBJECT*...]
  21. | **bpftool** **gen help**
  22. DESCRIPTION
  23. ===========
  24. **bpftool gen object** *OUTPUT_FILE* *INPUT_FILE* [*INPUT_FILE*...]
  25. Statically link (combine) together one or more *INPUT_FILE*'s
  26. into a single resulting *OUTPUT_FILE*. All the files involved
  27. are BPF ELF object files.
  28. The rules of BPF static linking are mostly the same as for
  29. user-space object files, but in addition to combining data
  30. and instruction sections, .BTF and .BTF.ext (if present in
  31. any of the input files) data are combined together. .BTF
  32. data is deduplicated, so all the common types across
  33. *INPUT_FILE*'s will only be represented once in the resulting
  34. BTF information.
  35. BPF static linking allows to partition BPF source code into
  36. individually compiled files that are then linked into
  37. a single resulting BPF object file, which can be used to
  38. generated BPF skeleton (with **gen skeleton** command) or
  39. passed directly into **libbpf** (using **bpf_object__open()**
  40. family of APIs).
  41. **bpftool gen skeleton** *FILE*
  42. Generate BPF skeleton C header file for a given *FILE*.
  43. BPF skeleton is an alternative interface to existing libbpf
  44. APIs for working with BPF objects. Skeleton code is intended
  45. to significantly shorten and simplify code to load and work
  46. with BPF programs from userspace side. Generated code is
  47. tailored to specific input BPF object *FILE*, reflecting its
  48. structure by listing out available maps, program, variables,
  49. etc. Skeleton eliminates the need to lookup mentioned
  50. components by name. Instead, if skeleton instantiation
  51. succeeds, they are populated in skeleton structure as valid
  52. libbpf types (e.g., **struct bpf_map** pointer) and can be
  53. passed to existing generic libbpf APIs.
  54. In addition to simple and reliable access to maps and
  55. programs, skeleton provides a storage for BPF links (**struct
  56. bpf_link**) for each BPF program within BPF object. When
  57. requested, supported BPF programs will be automatically
  58. attached and resulting BPF links stored for further use by
  59. user in pre-allocated fields in skeleton struct. For BPF
  60. programs that can't be automatically attached by libbpf,
  61. user can attach them manually, but store resulting BPF link
  62. in per-program link field. All such set up links will be
  63. automatically destroyed on BPF skeleton destruction. This
  64. eliminates the need for users to manage links manually and
  65. rely on libbpf support to detach programs and free up
  66. resources.
  67. Another facility provided by BPF skeleton is an interface to
  68. global variables of all supported kinds: mutable, read-only,
  69. as well as extern ones. This interface allows to pre-setup
  70. initial values of variables before BPF object is loaded and
  71. verified by kernel. For non-read-only variables, the same
  72. interface can be used to fetch values of global variables on
  73. userspace side, even if they are modified by BPF code.
  74. During skeleton generation, contents of source BPF object
  75. *FILE* is embedded within generated code and is thus not
  76. necessary to keep around. This ensures skeleton and BPF
  77. object file are matching 1-to-1 and always stay in sync.
  78. Generated code is dual-licensed under LGPL-2.1 and
  79. BSD-2-Clause licenses.
  80. It is a design goal and guarantee that skeleton interfaces
  81. are interoperable with generic libbpf APIs. User should
  82. always be able to use skeleton API to create and load BPF
  83. object, and later use libbpf APIs to keep working with
  84. specific maps, programs, etc.
  85. As part of skeleton, few custom functions are generated.
  86. Each of them is prefixed with object name. Object name can
  87. either be derived from object file name, i.e., if BPF object
  88. file name is **example.o**, BPF object name will be
  89. **example**. Object name can be also specified explicitly
  90. through **name** *OBJECT_NAME* parameter. The following
  91. custom functions are provided (assuming **example** as
  92. the object name):
  93. - **example__open** and **example__open_opts**.
  94. These functions are used to instantiate skeleton. It
  95. corresponds to libbpf's **bpf_object__open**\ () API.
  96. **_opts** variants accepts extra **bpf_object_open_opts**
  97. options.
  98. - **example__load**.
  99. This function creates maps, loads and verifies BPF
  100. programs, initializes global data maps. It corresponds to
  101. libppf's **bpf_object__load**\ () API.
  102. - **example__open_and_load** combines **example__open** and
  103. **example__load** invocations in one commonly used
  104. operation.
  105. - **example__attach** and **example__detach**
  106. This pair of functions allow to attach and detach,
  107. correspondingly, already loaded BPF object. Only BPF
  108. programs of types supported by libbpf for auto-attachment
  109. will be auto-attached and their corresponding BPF links
  110. instantiated. For other BPF programs, user can manually
  111. create a BPF link and assign it to corresponding fields in
  112. skeleton struct. **example__detach** will detach both
  113. links created automatically, as well as those populated by
  114. user manually.
  115. - **example__destroy**
  116. Detach and unload BPF programs, free up all the resources
  117. used by skeleton and BPF object.
  118. If BPF object has global variables, corresponding structs
  119. with memory layout corresponding to global data data section
  120. layout will be created. Currently supported ones are: *.data*,
  121. *.bss*, *.rodata*, and *.kconfig* structs/data sections.
  122. These data sections/structs can be used to set up initial
  123. values of variables, if set before **example__load**.
  124. Afterwards, if target kernel supports memory-mapped BPF
  125. arrays, same structs can be used to fetch and update
  126. (non-read-only) data from userspace, with same simplicity
  127. as for BPF side.
  128. **bpftool gen subskeleton** *FILE*
  129. Generate BPF subskeleton C header file for a given *FILE*.
  130. Subskeletons are similar to skeletons, except they do not own
  131. the corresponding maps, programs, or global variables. They
  132. require that the object file used to generate them is already
  133. loaded into a *bpf_object* by some other means.
  134. This functionality is useful when a library is included into a
  135. larger BPF program. A subskeleton for the library would have
  136. access to all objects and globals defined in it, without
  137. having to know about the larger program.
  138. Consequently, there are only two functions defined
  139. for subskeletons:
  140. - **example__open(bpf_object\*)**
  141. Instantiates a subskeleton from an already opened (but not
  142. necessarily loaded) **bpf_object**.
  143. - **example__destroy()**
  144. Frees the storage for the subskeleton but *does not* unload
  145. any BPF programs or maps.
  146. **bpftool** **gen min_core_btf** *INPUT* *OUTPUT* *OBJECT* [*OBJECT*...]
  147. Generate a minimum BTF file as *OUTPUT*, derived from a given
  148. *INPUT* BTF file, containing all needed BTF types so one, or
  149. more, given eBPF objects CO-RE relocations may be satisfied.
  150. When kernels aren't compiled with CONFIG_DEBUG_INFO_BTF,
  151. libbpf, when loading an eBPF object, has to rely on external
  152. BTF files to be able to calculate CO-RE relocations.
  153. Usually, an external BTF file is built from existing kernel
  154. DWARF data using pahole. It contains all the types used by
  155. its respective kernel image and, because of that, is big.
  156. The min_core_btf feature builds smaller BTF files, customized
  157. to one or multiple eBPF objects, so they can be distributed
  158. together with an eBPF CO-RE based application, turning the
  159. application portable to different kernel versions.
  160. Check examples bellow for more information how to use it.
  161. **bpftool gen help**
  162. Print short help message.
  163. OPTIONS
  164. =======
  165. .. include:: common_options.rst
  166. -L, --use-loader
  167. For skeletons, generate a "light" skeleton (also known as "loader"
  168. skeleton). A light skeleton contains a loader eBPF program. It does
  169. not use the majority of the libbpf infrastructure, and does not need
  170. libelf.
  171. EXAMPLES
  172. ========
  173. **$ cat example1.bpf.c**
  174. ::
  175. #include <stdbool.h>
  176. #include <linux/ptrace.h>
  177. #include <linux/bpf.h>
  178. #include <bpf/bpf_helpers.h>
  179. const volatile int param1 = 42;
  180. bool global_flag = true;
  181. struct { int x; } data = {};
  182. SEC("raw_tp/sys_enter")
  183. int handle_sys_enter(struct pt_regs *ctx)
  184. {
  185. static long my_static_var;
  186. if (global_flag)
  187. my_static_var++;
  188. else
  189. data.x += param1;
  190. return 0;
  191. }
  192. **$ cat example2.bpf.c**
  193. ::
  194. #include <linux/ptrace.h>
  195. #include <linux/bpf.h>
  196. #include <bpf/bpf_helpers.h>
  197. struct {
  198. __uint(type, BPF_MAP_TYPE_HASH);
  199. __uint(max_entries, 128);
  200. __type(key, int);
  201. __type(value, long);
  202. } my_map SEC(".maps");
  203. SEC("raw_tp/sys_exit")
  204. int handle_sys_exit(struct pt_regs *ctx)
  205. {
  206. int zero = 0;
  207. bpf_map_lookup_elem(&my_map, &zero);
  208. return 0;
  209. }
  210. This is example BPF application with two BPF programs and a mix of BPF maps
  211. and global variables. Source code is split across two source code files.
  212. **$ clang -target bpf -g example1.bpf.c -o example1.bpf.o**
  213. **$ clang -target bpf -g example2.bpf.c -o example2.bpf.o**
  214. **$ bpftool gen object example.bpf.o example1.bpf.o example2.bpf.o**
  215. This set of commands compiles *example1.bpf.c* and *example2.bpf.c*
  216. individually and then statically links respective object files into the final
  217. BPF ELF object file *example.bpf.o*.
  218. **$ bpftool gen skeleton example.bpf.o name example | tee example.skel.h**
  219. ::
  220. /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
  221. /* THIS FILE IS AUTOGENERATED! */
  222. #ifndef __EXAMPLE_SKEL_H__
  223. #define __EXAMPLE_SKEL_H__
  224. #include <stdlib.h>
  225. #include <bpf/libbpf.h>
  226. struct example {
  227. struct bpf_object_skeleton *skeleton;
  228. struct bpf_object *obj;
  229. struct {
  230. struct bpf_map *rodata;
  231. struct bpf_map *data;
  232. struct bpf_map *bss;
  233. struct bpf_map *my_map;
  234. } maps;
  235. struct {
  236. struct bpf_program *handle_sys_enter;
  237. struct bpf_program *handle_sys_exit;
  238. } progs;
  239. struct {
  240. struct bpf_link *handle_sys_enter;
  241. struct bpf_link *handle_sys_exit;
  242. } links;
  243. struct example__bss {
  244. struct {
  245. int x;
  246. } data;
  247. } *bss;
  248. struct example__data {
  249. _Bool global_flag;
  250. long int handle_sys_enter_my_static_var;
  251. } *data;
  252. struct example__rodata {
  253. int param1;
  254. } *rodata;
  255. };
  256. static void example__destroy(struct example *obj);
  257. static inline struct example *example__open_opts(
  258. const struct bpf_object_open_opts *opts);
  259. static inline struct example *example__open();
  260. static inline int example__load(struct example *obj);
  261. static inline struct example *example__open_and_load();
  262. static inline int example__attach(struct example *obj);
  263. static inline void example__detach(struct example *obj);
  264. #endif /* __EXAMPLE_SKEL_H__ */
  265. **$ cat example.c**
  266. ::
  267. #include "example.skel.h"
  268. int main()
  269. {
  270. struct example *skel;
  271. int err = 0;
  272. skel = example__open();
  273. if (!skel)
  274. goto cleanup;
  275. skel->rodata->param1 = 128;
  276. err = example__load(skel);
  277. if (err)
  278. goto cleanup;
  279. err = example__attach(skel);
  280. if (err)
  281. goto cleanup;
  282. /* all libbpf APIs are usable */
  283. printf("my_map name: %s\n", bpf_map__name(skel->maps.my_map));
  284. printf("sys_enter prog FD: %d\n",
  285. bpf_program__fd(skel->progs.handle_sys_enter));
  286. /* detach and re-attach sys_exit program */
  287. bpf_link__destroy(skel->links.handle_sys_exit);
  288. skel->links.handle_sys_exit =
  289. bpf_program__attach(skel->progs.handle_sys_exit);
  290. printf("my_static_var: %ld\n",
  291. skel->bss->handle_sys_enter_my_static_var);
  292. cleanup:
  293. example__destroy(skel);
  294. return err;
  295. }
  296. **# ./example**
  297. ::
  298. my_map name: my_map
  299. sys_enter prog FD: 8
  300. my_static_var: 7
  301. This is a stripped-out version of skeleton generated for above example code.
  302. min_core_btf
  303. ------------
  304. **$ bpftool btf dump file 5.4.0-example.btf format raw**
  305. ::
  306. [1] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
  307. [2] CONST '(anon)' type_id=1
  308. [3] VOLATILE '(anon)' type_id=1
  309. [4] ARRAY '(anon)' type_id=1 index_type_id=21 nr_elems=2
  310. [5] PTR '(anon)' type_id=8
  311. [6] CONST '(anon)' type_id=5
  312. [7] INT 'char' size=1 bits_offset=0 nr_bits=8 encoding=(none)
  313. [8] CONST '(anon)' type_id=7
  314. [9] INT 'unsigned int' size=4 bits_offset=0 nr_bits=32 encoding=(none)
  315. <long output>
  316. **$ bpftool btf dump file one.bpf.o format raw**
  317. ::
  318. [1] PTR '(anon)' type_id=2
  319. [2] STRUCT 'trace_event_raw_sys_enter' size=64 vlen=4
  320. 'ent' type_id=3 bits_offset=0
  321. 'id' type_id=7 bits_offset=64
  322. 'args' type_id=9 bits_offset=128
  323. '__data' type_id=12 bits_offset=512
  324. [3] STRUCT 'trace_entry' size=8 vlen=4
  325. 'type' type_id=4 bits_offset=0
  326. 'flags' type_id=5 bits_offset=16
  327. 'preempt_count' type_id=5 bits_offset=24
  328. <long output>
  329. **$ bpftool gen min_core_btf 5.4.0-example.btf 5.4.0-smaller.btf one.bpf.o**
  330. **$ bpftool btf dump file 5.4.0-smaller.btf format raw**
  331. ::
  332. [1] TYPEDEF 'pid_t' type_id=6
  333. [2] STRUCT 'trace_event_raw_sys_enter' size=64 vlen=1
  334. 'args' type_id=4 bits_offset=128
  335. [3] STRUCT 'task_struct' size=9216 vlen=2
  336. 'pid' type_id=1 bits_offset=17920
  337. 'real_parent' type_id=7 bits_offset=18048
  338. [4] ARRAY '(anon)' type_id=5 index_type_id=8 nr_elems=6
  339. [5] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
  340. [6] TYPEDEF '__kernel_pid_t' type_id=8
  341. [7] PTR '(anon)' type_id=3
  342. [8] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
  343. <end>
  344. Now, the "5.4.0-smaller.btf" file may be used by libbpf as an external BTF file
  345. when loading the "one.bpf.o" object into the "5.4.0-example" kernel. Note that
  346. the generated BTF file won't allow other eBPF objects to be loaded, just the
  347. ones given to min_core_btf.
  348. ::
  349. LIBBPF_OPTS(bpf_object_open_opts, opts, .btf_custom_path = "5.4.0-smaller.btf");
  350. struct bpf_object *obj;
  351. obj = bpf_object__open_file("one.bpf.o", &opts);
  352. ...