xref: /linux/tools/bpf/bpftool/Documentation/bpftool-gen.rst (revision 229c3b47b794e7257744224b21a95d3d9938d00a)
1cb21ac58SAndrii Nakryiko================
2cb21ac58SAndrii Nakryikobpftool-gen
3cb21ac58SAndrii Nakryiko================
4cb21ac58SAndrii Nakryiko-------------------------------------------------------------------------------
5cb21ac58SAndrii Nakryikotool for BPF code-generation
6cb21ac58SAndrii Nakryiko-------------------------------------------------------------------------------
7cb21ac58SAndrii Nakryiko
8cb21ac58SAndrii Nakryiko:Manual section: 8
9cb21ac58SAndrii Nakryiko
10cb21ac58SAndrii NakryikoSYNOPSIS
11cb21ac58SAndrii Nakryiko========
12cb21ac58SAndrii Nakryiko
13cb21ac58SAndrii Nakryiko	**bpftool** [*OPTIONS*] **gen** *COMMAND*
14cb21ac58SAndrii Nakryiko
15cb21ac58SAndrii Nakryiko	*OPTIONS* := { { **-j** | **--json** } [{ **-p** | **--pretty** }] }
16cb21ac58SAndrii Nakryiko
17cb21ac58SAndrii Nakryiko	*COMMAND* := { **skeleton | **help** }
18cb21ac58SAndrii Nakryiko
19cb21ac58SAndrii NakryikoGEN COMMANDS
20cb21ac58SAndrii Nakryiko=============
21cb21ac58SAndrii Nakryiko
22cb21ac58SAndrii Nakryiko|	**bpftool** **gen skeleton** *FILE*
23cb21ac58SAndrii Nakryiko|	**bpftool** **gen help**
24cb21ac58SAndrii Nakryiko
25cb21ac58SAndrii NakryikoDESCRIPTION
26cb21ac58SAndrii Nakryiko===========
27cb21ac58SAndrii Nakryiko	**bpftool gen skeleton** *FILE*
28cb21ac58SAndrii Nakryiko		  Generate BPF skeleton C header file for a given *FILE*.
29cb21ac58SAndrii Nakryiko
30cb21ac58SAndrii Nakryiko		  BPF skeleton is an alternative interface to existing libbpf
31cb21ac58SAndrii Nakryiko		  APIs for working with BPF objects. Skeleton code is intended
32cb21ac58SAndrii Nakryiko		  to significantly shorten and simplify code to load and work
33cb21ac58SAndrii Nakryiko		  with BPF programs from userspace side. Generated code is
34cb21ac58SAndrii Nakryiko		  tailored to specific input BPF object *FILE*, reflecting its
35cb21ac58SAndrii Nakryiko		  structure by listing out available maps, program, variables,
36cb21ac58SAndrii Nakryiko		  etc. Skeleton eliminates the need to lookup mentioned
37cb21ac58SAndrii Nakryiko		  components by name. Instead, if skeleton instantiation
38cb21ac58SAndrii Nakryiko		  succeeds, they are populated in skeleton structure as valid
39cb21ac58SAndrii Nakryiko		  libbpf types (e.g., struct bpf_map pointer) and can be
40cb21ac58SAndrii Nakryiko		  passed to existing generic libbpf APIs.
41cb21ac58SAndrii Nakryiko
42cb21ac58SAndrii Nakryiko		  In addition to simple and reliable access to maps and
43cb21ac58SAndrii Nakryiko		  programs, skeleton provides a storage for BPF links (struct
44cb21ac58SAndrii Nakryiko		  bpf_link) for each BPF program within BPF object. When
45cb21ac58SAndrii Nakryiko		  requested, supported BPF programs will be automatically
46cb21ac58SAndrii Nakryiko		  attached and resulting BPF links stored for further use by
47cb21ac58SAndrii Nakryiko		  user in pre-allocated fields in skeleton struct. For BPF
48cb21ac58SAndrii Nakryiko		  programs that can't be automatically attached by libbpf,
49cb21ac58SAndrii Nakryiko		  user can attach them manually, but store resulting BPF link
50cb21ac58SAndrii Nakryiko		  in per-program link field. All such set up links will be
51cb21ac58SAndrii Nakryiko		  automatically destroyed on BPF skeleton destruction. This
52cb21ac58SAndrii Nakryiko		  eliminates the need for users to manage links manually and
53cb21ac58SAndrii Nakryiko		  rely on libbpf support to detach programs and free up
54cb21ac58SAndrii Nakryiko		  resources.
55cb21ac58SAndrii Nakryiko
56cb21ac58SAndrii Nakryiko		  Another facility provided by BPF skeleton is an interface to
57cb21ac58SAndrii Nakryiko		  global variables of all supported kinds: mutable, read-only,
58cb21ac58SAndrii Nakryiko		  as well as extern ones. This interface allows to pre-setup
59cb21ac58SAndrii Nakryiko		  initial values of variables before BPF object is loaded and
60cb21ac58SAndrii Nakryiko		  verified by kernel. For non-read-only variables, the same
61cb21ac58SAndrii Nakryiko		  interface can be used to fetch values of global variables on
62cb21ac58SAndrii Nakryiko		  userspace side, even if they are modified by BPF code.
63cb21ac58SAndrii Nakryiko
64cb21ac58SAndrii Nakryiko		  During skeleton generation, contents of source BPF object
65cb21ac58SAndrii Nakryiko		  *FILE* is embedded within generated code and is thus not
66cb21ac58SAndrii Nakryiko		  necessary to keep around. This ensures skeleton and BPF
67cb21ac58SAndrii Nakryiko		  object file are matching 1-to-1 and always stay in sync.
68cb21ac58SAndrii Nakryiko		  Generated code is dual-licensed under LGPL-2.1 and
69cb21ac58SAndrii Nakryiko		  BSD-2-Clause licenses.
70cb21ac58SAndrii Nakryiko
71cb21ac58SAndrii Nakryiko		  It is a design goal and guarantee that skeleton interfaces
72cb21ac58SAndrii Nakryiko		  are interoperable with generic libbpf APIs. User should
73cb21ac58SAndrii Nakryiko		  always be able to use skeleton API to create and load BPF
74cb21ac58SAndrii Nakryiko		  object, and later use libbpf APIs to keep working with
75cb21ac58SAndrii Nakryiko		  specific maps, programs, etc.
76cb21ac58SAndrii Nakryiko
77cb21ac58SAndrii Nakryiko		  As part of skeleton, few custom functions are generated.
78cb21ac58SAndrii Nakryiko		  Each of them is prefixed with object name, derived from
79cb21ac58SAndrii Nakryiko		  object file name. I.e., if BPF object file name is
80cb21ac58SAndrii Nakryiko		  **example.o**, BPF object name will be **example**. The
81cb21ac58SAndrii Nakryiko		  following custom functions are provided in such case:
82cb21ac58SAndrii Nakryiko
83cb21ac58SAndrii Nakryiko		  - **example__open** and **example__open_opts**.
84cb21ac58SAndrii Nakryiko		    These functions are used to instantiate skeleton. It
85cb21ac58SAndrii Nakryiko		    corresponds to libbpf's **bpf_object__open()** API.
86cb21ac58SAndrii Nakryiko		    **_opts** variants accepts extra **bpf_object_open_opts**
87cb21ac58SAndrii Nakryiko		    options.
88cb21ac58SAndrii Nakryiko
89cb21ac58SAndrii Nakryiko		  - **example__load**.
90cb21ac58SAndrii Nakryiko		    This function creates maps, loads and verifies BPF
91cb21ac58SAndrii Nakryiko		    programs, initializes global data maps. It corresponds to
92cb21ac58SAndrii Nakryiko		    libppf's **bpf_object__load** API.
93cb21ac58SAndrii Nakryiko
94cb21ac58SAndrii Nakryiko		  - **example__open_and_load** combines **example__open** and
95cb21ac58SAndrii Nakryiko		    **example__load** invocations in one commonly used
96cb21ac58SAndrii Nakryiko		    operation.
97cb21ac58SAndrii Nakryiko
98cb21ac58SAndrii Nakryiko		  - **example__attach** and **example__detach**
99cb21ac58SAndrii Nakryiko		    This pair of functions allow to attach and detach,
100cb21ac58SAndrii Nakryiko		    correspondingly, already loaded BPF object. Only BPF
101cb21ac58SAndrii Nakryiko		    programs of types supported by libbpf for auto-attachment
102cb21ac58SAndrii Nakryiko		    will be auto-attached and their corresponding BPF links
103cb21ac58SAndrii Nakryiko		    instantiated. For other BPF programs, user can manually
104cb21ac58SAndrii Nakryiko		    create a BPF link and assign it to corresponding fields in
105cb21ac58SAndrii Nakryiko		    skeleton struct. **example__detach** will detach both
106cb21ac58SAndrii Nakryiko		    links created automatically, as well as those populated by
107cb21ac58SAndrii Nakryiko		    user manually.
108cb21ac58SAndrii Nakryiko
109cb21ac58SAndrii Nakryiko		  - **example__destroy**
110cb21ac58SAndrii Nakryiko		    Detach and unload BPF programs, free up all the resources
111cb21ac58SAndrii Nakryiko		    used by skeleton and BPF object.
112cb21ac58SAndrii Nakryiko
113cb21ac58SAndrii Nakryiko		  If BPF object has global variables, corresponding structs
114cb21ac58SAndrii Nakryiko		  with memory layout corresponding to global data data section
115dacce641SAndrii Nakryiko		  layout will be created. Currently supported ones are: *.data*,
116dacce641SAndrii Nakryiko		  *.bss*, *.rodata*, and *.kconfig* structs/data sections.
117dacce641SAndrii Nakryiko		  These data sections/structs can be used to set up initial
118dacce641SAndrii Nakryiko		  values of variables, if set before **example__load**.
119dacce641SAndrii Nakryiko		  Afterwards, if target kernel supports memory-mapped BPF
120dacce641SAndrii Nakryiko		  arrays, same structs can be used to fetch and update
121dacce641SAndrii Nakryiko		  (non-read-only) data from userspace, with same simplicity
122dacce641SAndrii Nakryiko		  as for BPF side.
123cb21ac58SAndrii Nakryiko
124cb21ac58SAndrii Nakryiko	**bpftool gen help**
125cb21ac58SAndrii Nakryiko		  Print short help message.
126cb21ac58SAndrii Nakryiko
127cb21ac58SAndrii NakryikoOPTIONS
128cb21ac58SAndrii Nakryiko=======
129cb21ac58SAndrii Nakryiko	-h, --help
130cb21ac58SAndrii Nakryiko		  Print short generic help message (similar to **bpftool help**).
131cb21ac58SAndrii Nakryiko
132cb21ac58SAndrii Nakryiko	-V, --version
133cb21ac58SAndrii Nakryiko		  Print version number (similar to **bpftool version**).
134cb21ac58SAndrii Nakryiko
135cb21ac58SAndrii Nakryiko	-j, --json
136cb21ac58SAndrii Nakryiko		  Generate JSON output. For commands that cannot produce JSON,
137cb21ac58SAndrii Nakryiko		  this option has no effect.
138cb21ac58SAndrii Nakryiko
139cb21ac58SAndrii Nakryiko	-p, --pretty
140cb21ac58SAndrii Nakryiko		  Generate human-readable JSON output. Implies **-j**.
141cb21ac58SAndrii Nakryiko
142cb21ac58SAndrii Nakryiko	-d, --debug
143cb21ac58SAndrii Nakryiko		  Print all logs available from libbpf, including debug-level
144cb21ac58SAndrii Nakryiko		  information.
145cb21ac58SAndrii Nakryiko
146cb21ac58SAndrii NakryikoEXAMPLES
147cb21ac58SAndrii Nakryiko========
148cb21ac58SAndrii Nakryiko**$ cat example.c**
149cb21ac58SAndrii Nakryiko::
150cb21ac58SAndrii Nakryiko
151cb21ac58SAndrii Nakryiko  #include <stdbool.h>
152cb21ac58SAndrii Nakryiko  #include <linux/ptrace.h>
153cb21ac58SAndrii Nakryiko  #include <linux/bpf.h>
154cb21ac58SAndrii Nakryiko  #include "bpf_helpers.h"
155cb21ac58SAndrii Nakryiko
156cb21ac58SAndrii Nakryiko  const volatile int param1 = 42;
157cb21ac58SAndrii Nakryiko  bool global_flag = true;
158cb21ac58SAndrii Nakryiko  struct { int x; } data = {};
159cb21ac58SAndrii Nakryiko
160cb21ac58SAndrii Nakryiko  struct {
161cb21ac58SAndrii Nakryiko  	__uint(type, BPF_MAP_TYPE_HASH);
162cb21ac58SAndrii Nakryiko  	__uint(max_entries, 128);
163cb21ac58SAndrii Nakryiko  	__type(key, int);
164cb21ac58SAndrii Nakryiko  	__type(value, long);
165cb21ac58SAndrii Nakryiko  } my_map SEC(".maps");
166cb21ac58SAndrii Nakryiko
167cb21ac58SAndrii Nakryiko  SEC("raw_tp/sys_enter")
168cb21ac58SAndrii Nakryiko  int handle_sys_enter(struct pt_regs *ctx)
169cb21ac58SAndrii Nakryiko  {
170cb21ac58SAndrii Nakryiko  	static long my_static_var;
171cb21ac58SAndrii Nakryiko  	if (global_flag)
172cb21ac58SAndrii Nakryiko  		my_static_var++;
173cb21ac58SAndrii Nakryiko  	else
174cb21ac58SAndrii Nakryiko  		data.x += param1;
175cb21ac58SAndrii Nakryiko  	return 0;
176cb21ac58SAndrii Nakryiko  }
177cb21ac58SAndrii Nakryiko
178cb21ac58SAndrii Nakryiko  SEC("raw_tp/sys_exit")
179cb21ac58SAndrii Nakryiko  int handle_sys_exit(struct pt_regs *ctx)
180cb21ac58SAndrii Nakryiko  {
181cb21ac58SAndrii Nakryiko  	int zero = 0;
182cb21ac58SAndrii Nakryiko  	bpf_map_lookup_elem(&my_map, &zero);
183cb21ac58SAndrii Nakryiko  	return 0;
184cb21ac58SAndrii Nakryiko  }
185cb21ac58SAndrii Nakryiko
186cb21ac58SAndrii NakryikoThis is example BPF application with two BPF programs and a mix of BPF maps
187cb21ac58SAndrii Nakryikoand global variables.
188cb21ac58SAndrii Nakryiko
189cb21ac58SAndrii Nakryiko**$ bpftool gen skeleton example.o**
190cb21ac58SAndrii Nakryiko::
191cb21ac58SAndrii Nakryiko
192cb21ac58SAndrii Nakryiko  /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
193cb21ac58SAndrii Nakryiko
194cb21ac58SAndrii Nakryiko  /* THIS FILE IS AUTOGENERATED! */
195cb21ac58SAndrii Nakryiko  #ifndef __EXAMPLE_SKEL_H__
196cb21ac58SAndrii Nakryiko  #define __EXAMPLE_SKEL_H__
197cb21ac58SAndrii Nakryiko
198cb21ac58SAndrii Nakryiko  #include <stdlib.h>
199*229c3b47SToke Høiland-Jørgensen  #include <bpf/libbpf.h>
200cb21ac58SAndrii Nakryiko
201cb21ac58SAndrii Nakryiko  struct example {
202cb21ac58SAndrii Nakryiko  	struct bpf_object_skeleton *skeleton;
203cb21ac58SAndrii Nakryiko  	struct bpf_object *obj;
204cb21ac58SAndrii Nakryiko  	struct {
205cb21ac58SAndrii Nakryiko  		struct bpf_map *rodata;
206cb21ac58SAndrii Nakryiko  		struct bpf_map *data;
207cb21ac58SAndrii Nakryiko  		struct bpf_map *bss;
208cb21ac58SAndrii Nakryiko  		struct bpf_map *my_map;
209cb21ac58SAndrii Nakryiko  	} maps;
210cb21ac58SAndrii Nakryiko  	struct {
211cb21ac58SAndrii Nakryiko  		struct bpf_program *handle_sys_enter;
212cb21ac58SAndrii Nakryiko  		struct bpf_program *handle_sys_exit;
213cb21ac58SAndrii Nakryiko  	} progs;
214cb21ac58SAndrii Nakryiko  	struct {
215cb21ac58SAndrii Nakryiko  		struct bpf_link *handle_sys_enter;
216cb21ac58SAndrii Nakryiko  		struct bpf_link *handle_sys_exit;
217cb21ac58SAndrii Nakryiko  	} links;
218cb21ac58SAndrii Nakryiko  	struct example__bss {
219cb21ac58SAndrii Nakryiko  		struct {
220cb21ac58SAndrii Nakryiko  			int x;
221cb21ac58SAndrii Nakryiko  		} data;
222cb21ac58SAndrii Nakryiko  	} *bss;
223cb21ac58SAndrii Nakryiko  	struct example__data {
224cb21ac58SAndrii Nakryiko  		_Bool global_flag;
225cb21ac58SAndrii Nakryiko  		long int handle_sys_enter_my_static_var;
226cb21ac58SAndrii Nakryiko  	} *data;
227cb21ac58SAndrii Nakryiko  	struct example__rodata {
228cb21ac58SAndrii Nakryiko  		int param1;
229cb21ac58SAndrii Nakryiko  	} *rodata;
230cb21ac58SAndrii Nakryiko  };
231cb21ac58SAndrii Nakryiko
232cb21ac58SAndrii Nakryiko  static void example__destroy(struct example *obj);
233cb21ac58SAndrii Nakryiko  static inline struct example *example__open_opts(
234cb21ac58SAndrii Nakryiko                const struct bpf_object_open_opts *opts);
235cb21ac58SAndrii Nakryiko  static inline struct example *example__open();
236cb21ac58SAndrii Nakryiko  static inline int example__load(struct example *obj);
237cb21ac58SAndrii Nakryiko  static inline struct example *example__open_and_load();
238cb21ac58SAndrii Nakryiko  static inline int example__attach(struct example *obj);
239cb21ac58SAndrii Nakryiko  static inline void example__detach(struct example *obj);
240cb21ac58SAndrii Nakryiko
241cb21ac58SAndrii Nakryiko  #endif /* __EXAMPLE_SKEL_H__ */
242cb21ac58SAndrii Nakryiko
243cb21ac58SAndrii Nakryiko**$ cat example_user.c**
244cb21ac58SAndrii Nakryiko::
245cb21ac58SAndrii Nakryiko
246cb21ac58SAndrii Nakryiko  #include "example.skel.h"
247cb21ac58SAndrii Nakryiko
248cb21ac58SAndrii Nakryiko  int main()
249cb21ac58SAndrii Nakryiko  {
250cb21ac58SAndrii Nakryiko  	struct example *skel;
251cb21ac58SAndrii Nakryiko  	int err = 0;
252cb21ac58SAndrii Nakryiko
253cb21ac58SAndrii Nakryiko  	skel = example__open();
254cb21ac58SAndrii Nakryiko  	if (!skel)
255cb21ac58SAndrii Nakryiko  		goto cleanup;
256cb21ac58SAndrii Nakryiko
257cb21ac58SAndrii Nakryiko  	skel->rodata->param1 = 128;
258cb21ac58SAndrii Nakryiko
259cb21ac58SAndrii Nakryiko  	err = example__load(skel);
260cb21ac58SAndrii Nakryiko  	if (err)
261cb21ac58SAndrii Nakryiko  		goto cleanup;
262cb21ac58SAndrii Nakryiko
263cb21ac58SAndrii Nakryiko  	err = example__attach(skel);
264cb21ac58SAndrii Nakryiko  	if (err)
265cb21ac58SAndrii Nakryiko  		goto cleanup;
266cb21ac58SAndrii Nakryiko
267cb21ac58SAndrii Nakryiko  	/* all libbpf APIs are usable */
268cb21ac58SAndrii Nakryiko  	printf("my_map name: %s\n", bpf_map__name(skel->maps.my_map));
269cb21ac58SAndrii Nakryiko  	printf("sys_enter prog FD: %d\n",
270cb21ac58SAndrii Nakryiko  	       bpf_program__fd(skel->progs.handle_sys_enter));
271cb21ac58SAndrii Nakryiko
272cb21ac58SAndrii Nakryiko  	/* detach and re-attach sys_exit program */
273cb21ac58SAndrii Nakryiko  	bpf_link__destroy(skel->links.handle_sys_exit);
274cb21ac58SAndrii Nakryiko  	skel->links.handle_sys_exit =
275cb21ac58SAndrii Nakryiko  		bpf_program__attach(skel->progs.handle_sys_exit);
276cb21ac58SAndrii Nakryiko
277cb21ac58SAndrii Nakryiko  	printf("my_static_var: %ld\n",
278cb21ac58SAndrii Nakryiko  	       skel->bss->handle_sys_enter_my_static_var);
279cb21ac58SAndrii Nakryiko
280cb21ac58SAndrii Nakryiko  cleanup:
281cb21ac58SAndrii Nakryiko  	example__destroy(skel);
282cb21ac58SAndrii Nakryiko  	return err;
283cb21ac58SAndrii Nakryiko  }
284cb21ac58SAndrii Nakryiko
285cb21ac58SAndrii Nakryiko**# ./example_user**
286cb21ac58SAndrii Nakryiko::
287cb21ac58SAndrii Nakryiko
288cb21ac58SAndrii Nakryiko  my_map name: my_map
289cb21ac58SAndrii Nakryiko  sys_enter prog FD: 8
290cb21ac58SAndrii Nakryiko  my_static_var: 7
291cb21ac58SAndrii Nakryiko
292cb21ac58SAndrii NakryikoThis is a stripped-out version of skeleton generated for above example code.
293cb21ac58SAndrii Nakryiko
294cb21ac58SAndrii NakryikoSEE ALSO
295cb21ac58SAndrii Nakryiko========
296cb21ac58SAndrii Nakryiko	**bpf**\ (2),
297cb21ac58SAndrii Nakryiko	**bpf-helpers**\ (7),
298cb21ac58SAndrii Nakryiko	**bpftool**\ (8),
299cb21ac58SAndrii Nakryiko	**bpftool-map**\ (8),
300cb21ac58SAndrii Nakryiko	**bpftool-prog**\ (8),
301cb21ac58SAndrii Nakryiko	**bpftool-cgroup**\ (8),
302cb21ac58SAndrii Nakryiko	**bpftool-feature**\ (8),
303cb21ac58SAndrii Nakryiko	**bpftool-net**\ (8),
304cb21ac58SAndrii Nakryiko	**bpftool-perf**\ (8),
305cb21ac58SAndrii Nakryiko	**bpftool-btf**\ (8)
306