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