bpftool-gen.rst 16 KB

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