Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1================
2bpftool-prog
3================
4-------------------------------------------------------------------------------
5tool for inspection and simple manipulation of eBPF progs
6-------------------------------------------------------------------------------
7
8:Manual section: 8
9
10SYNOPSIS
11========
12
13 **bpftool** [*OPTIONS*] **prog** *COMMAND*
14
15 *OPTIONS* := { { **-j** | **--json** } [{ **-p** | **--pretty** }] | { **-f** | **--bpffs** } }
16
17 *COMMANDS* :=
18 { **show** | **list** | **dump xlated** | **dump jited** | **pin** | **load**
19 | **loadall** | **help** }
20
21PROG COMMANDS
22=============
23
24| **bpftool** **prog { show | list }** [*PROG*]
25| **bpftool** **prog dump xlated** *PROG* [{**file** *FILE* | **opcodes** | **visual** | **linum**}]
26| **bpftool** **prog dump jited** *PROG* [{**file** *FILE* | **opcodes** | **linum**}]
27| **bpftool** **prog pin** *PROG* *FILE*
28| **bpftool** **prog { load | loadall }** *OBJ* *PATH* [**type** *TYPE*] [**map** {**idx** *IDX* | **name** *NAME*} *MAP*] [**dev** *NAME*] [**pinmaps** *MAP_DIR*]
29| **bpftool** **prog attach** *PROG* *ATTACH_TYPE* [*MAP*]
30| **bpftool** **prog detach** *PROG* *ATTACH_TYPE* [*MAP*]
31| **bpftool** **prog tracelog**
32| **bpftool** **prog run** *PROG* **data_in** *FILE* [**data_out** *FILE* [**data_size_out** *L*]] [**ctx_in** *FILE* [**ctx_out** *FILE* [**ctx_size_out** *M*]]] [**repeat** *N*]
33| **bpftool** **prog profile** *PROG* [**duration** *DURATION*] *METRICs*
34| **bpftool** **prog help**
35|
36| *MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
37| *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* | **name** *PROG_NAME* }
38| *TYPE* := {
39| **socket** | **kprobe** | **kretprobe** | **classifier** | **action** |
40| **tracepoint** | **raw_tracepoint** | **xdp** | **perf_event** | **cgroup/skb** |
41| **cgroup/sock** | **cgroup/dev** | **lwt_in** | **lwt_out** | **lwt_xmit** |
42| **lwt_seg6local** | **sockops** | **sk_skb** | **sk_msg** | **lirc_mode2** |
43| **cgroup/bind4** | **cgroup/bind6** | **cgroup/post_bind4** | **cgroup/post_bind6** |
44| **cgroup/connect4** | **cgroup/connect6** | **cgroup/sendmsg4** | **cgroup/sendmsg6** |
45| **cgroup/recvmsg4** | **cgroup/recvmsg6** | **cgroup/sysctl** |
46| **cgroup/getsockopt** | **cgroup/setsockopt** |
47| **struct_ops** | **fentry** | **fexit** | **freplace**
48| }
49| *ATTACH_TYPE* := {
50| **msg_verdict** | **stream_verdict** | **stream_parser** | **flow_dissector**
51| }
52| *METRIC* := {
53| **cycles** | **instructions** | **l1d_loads** | **llc_misses**
54| }
55
56
57DESCRIPTION
58===========
59 **bpftool prog { show | list }** [*PROG*]
60 Show information about loaded programs. If *PROG* is
61 specified show information only about given programs,
62 otherwise list all programs currently loaded on the system.
63 In case of **tag** or **name**, *PROG* may match several
64 programs which will all be shown.
65
66 Output will start with program ID followed by program type and
67 zero or more named attributes (depending on kernel version).
68
69 Since Linux 5.1 the kernel can collect statistics on BPF
70 programs (such as the total time spent running the program,
71 and the number of times it was run). If available, bpftool
72 shows such statistics. However, the kernel does not collect
73 them by defaults, as it slightly impacts performance on each
74 program run. Activation or deactivation of the feature is
75 performed via the **kernel.bpf_stats_enabled** sysctl knob.
76
77 **bpftool prog dump xlated** *PROG* [{ **file** *FILE* | **opcodes** | **visual** | **linum** }]
78 Dump eBPF instructions of the programs from the kernel. By
79 default, eBPF will be disassembled and printed to standard
80 output in human-readable format. In this case, **opcodes**
81 controls if raw opcodes should be printed as well.
82
83 In case of **tag** or **name**, *PROG* may match several
84 programs which will all be dumped. However, if **file** or
85 **visual** is specified, *PROG* must match a single program.
86
87 If **file** is specified, the binary image will instead be
88 written to *FILE*.
89
90 If **visual** is specified, control flow graph (CFG) will be
91 built instead, and eBPF instructions will be presented with
92 CFG in DOT format, on standard output.
93
94 If the programs have line_info available, the source line will
95 be displayed by default. If **linum** is specified,
96 the filename, line number and line column will also be
97 displayed on top of the source line.
98
99 **bpftool prog dump jited** *PROG* [{ **file** *FILE* | **opcodes** | **linum** }]
100 Dump jited image (host machine code) of the program.
101
102 If *FILE* is specified image will be written to a file,
103 otherwise it will be disassembled and printed to stdout.
104 *PROG* must match a single program when **file** is specified.
105
106 **opcodes** controls if raw opcodes will be printed.
107
108 If the prog has line_info available, the source line will
109 be displayed by default. If **linum** is specified,
110 the filename, line number and line column will also be
111 displayed on top of the source line.
112
113 **bpftool prog pin** *PROG* *FILE*
114 Pin program *PROG* as *FILE*.
115
116 Note: *FILE* must be located in *bpffs* mount. It must not
117 contain a dot character ('.'), which is reserved for future
118 extensions of *bpffs*.
119
120 **bpftool prog { load | loadall }** *OBJ* *PATH* [**type** *TYPE*] [**map** {**idx** *IDX* | **name** *NAME*} *MAP*] [**dev** *NAME*] [**pinmaps** *MAP_DIR*]
121 Load bpf program(s) from binary *OBJ* and pin as *PATH*.
122 **bpftool prog load** pins only the first program from the
123 *OBJ* as *PATH*. **bpftool prog loadall** pins all programs
124 from the *OBJ* under *PATH* directory.
125 **type** is optional, if not specified program type will be
126 inferred from section names.
127 By default bpftool will create new maps as declared in the ELF
128 object being loaded. **map** parameter allows for the reuse
129 of existing maps. It can be specified multiple times, each
130 time for a different map. *IDX* refers to index of the map
131 to be replaced in the ELF file counting from 0, while *NAME*
132 allows to replace a map by name. *MAP* specifies the map to
133 use, referring to it by **id** or through a **pinned** file.
134 If **dev** *NAME* is specified program will be loaded onto
135 given networking device (offload).
136 Optional **pinmaps** argument can be provided to pin all
137 maps under *MAP_DIR* directory.
138
139 Note: *PATH* must be located in *bpffs* mount. It must not
140 contain a dot character ('.'), which is reserved for future
141 extensions of *bpffs*.
142
143 **bpftool prog attach** *PROG* *ATTACH_TYPE* [*MAP*]
144 Attach bpf program *PROG* (with type specified by
145 *ATTACH_TYPE*). Most *ATTACH_TYPEs* require a *MAP*
146 parameter, with the exception of *flow_dissector* which is
147 attached to current networking name space.
148
149 **bpftool prog detach** *PROG* *ATTACH_TYPE* [*MAP*]
150 Detach bpf program *PROG* (with type specified by
151 *ATTACH_TYPE*). Most *ATTACH_TYPEs* require a *MAP*
152 parameter, with the exception of *flow_dissector* which is
153 detached from the current networking name space.
154
155 **bpftool prog tracelog**
156 Dump the trace pipe of the system to the console (stdout).
157 Hit <Ctrl+C> to stop printing. BPF programs can write to this
158 trace pipe at runtime with the **bpf_trace_printk()** helper.
159 This should be used only for debugging purposes. For
160 streaming data from BPF programs to user space, one can use
161 perf events (see also **bpftool-map**\ (8)).
162
163 **bpftool prog run** *PROG* **data_in** *FILE* [**data_out** *FILE* [**data_size_out** *L*]] [**ctx_in** *FILE* [**ctx_out** *FILE* [**ctx_size_out** *M*]]] [**repeat** *N*]
164 Run BPF program *PROG* in the kernel testing infrastructure
165 for BPF, meaning that the program works on the data and
166 context provided by the user, and not on actual packets or
167 monitored functions etc. Return value and duration for the
168 test run are printed out to the console.
169
170 Input data is read from the *FILE* passed with **data_in**.
171 If this *FILE* is "**-**", input data is read from standard
172 input. Input context, if any, is read from *FILE* passed with
173 **ctx_in**. Again, "**-**" can be used to read from standard
174 input, but only if standard input is not already in use for
175 input data. If a *FILE* is passed with **data_out**, output
176 data is written to that file. Similarly, output context is
177 written to the *FILE* passed with **ctx_out**. For both
178 output flows, "**-**" can be used to print to the standard
179 output (as plain text, or JSON if relevant option was
180 passed). If output keywords are omitted, output data and
181 context are discarded. Keywords **data_size_out** and
182 **ctx_size_out** are used to pass the size (in bytes) for the
183 output buffers to the kernel, although the default of 32 kB
184 should be more than enough for most cases.
185
186 Keyword **repeat** is used to indicate the number of
187 consecutive runs to perform. Note that output data and
188 context printed to files correspond to the last of those
189 runs. The duration printed out at the end of the runs is an
190 average over all runs performed by the command.
191
192 Not all program types support test run. Among those which do,
193 not all of them can take the **ctx_in**/**ctx_out**
194 arguments. bpftool does not perform checks on program types.
195
196 **bpftool prog profile** *PROG* [**duration** *DURATION*] *METRICs*
197 Profile *METRICs* for bpf program *PROG* for *DURATION*
198 seconds or until user hits Ctrl-C. *DURATION* is optional.
199 If *DURATION* is not specified, the profiling will run up to
200 UINT_MAX seconds.
201
202 **bpftool prog help**
203 Print short help message.
204
205OPTIONS
206=======
207 -h, --help
208 Print short generic help message (similar to **bpftool help**).
209
210 -V, --version
211 Print version number (similar to **bpftool version**).
212
213 -j, --json
214 Generate JSON output. For commands that cannot produce JSON, this
215 option has no effect.
216
217 -p, --pretty
218 Generate human-readable JSON output. Implies **-j**.
219
220 -f, --bpffs
221 When showing BPF programs, show file names of pinned
222 programs.
223
224 -m, --mapcompat
225 Allow loading maps with unknown map definitions.
226
227 -n, --nomount
228 Do not automatically attempt to mount any virtual file system
229 (such as tracefs or BPF virtual file system) when necessary.
230
231 -d, --debug
232 Print all logs available, even debug-level information. This
233 includes logs from libbpf as well as from the verifier, when
234 attempting to load programs.
235
236EXAMPLES
237========
238**# bpftool prog show**
239
240::
241
242 10: xdp name some_prog tag 005a3d2123620c8b gpl run_time_ns 81632 run_cnt 10
243 loaded_at 2017-09-29T20:11:00+0000 uid 0
244 xlated 528B jited 370B memlock 4096B map_ids 10
245
246**# bpftool --json --pretty prog show**
247
248::
249
250 [{
251 "id": 10,
252 "type": "xdp",
253 "tag": "005a3d2123620c8b",
254 "gpl_compatible": true,
255 "run_time_ns": 81632,
256 "run_cnt": 10,
257 "loaded_at": 1506715860,
258 "uid": 0,
259 "bytes_xlated": 528,
260 "jited": true,
261 "bytes_jited": 370,
262 "bytes_memlock": 4096,
263 "map_ids": [10
264 ]
265 }
266 ]
267
268|
269| **# bpftool prog dump xlated id 10 file /tmp/t**
270| **# ls -l /tmp/t**
271
272::
273
274 -rw------- 1 root root 560 Jul 22 01:42 /tmp/t
275
276**# bpftool prog dump jited tag 005a3d2123620c8b**
277
278::
279
280 0: push %rbp
281 1: mov %rsp,%rbp
282 2: sub $0x228,%rsp
283 3: sub $0x28,%rbp
284 4: mov %rbx,0x0(%rbp)
285
286|
287| **# mount -t bpf none /sys/fs/bpf/**
288| **# bpftool prog pin id 10 /sys/fs/bpf/prog**
289| **# bpftool prog load ./my_prog.o /sys/fs/bpf/prog2**
290| **# ls -l /sys/fs/bpf/**
291
292::
293
294 -rw------- 1 root root 0 Jul 22 01:43 prog
295 -rw------- 1 root root 0 Jul 22 01:44 prog2
296
297**# bpftool prog dump jited pinned /sys/fs/bpf/prog opcodes**
298
299::
300
301 0: push %rbp
302 55
303 1: mov %rsp,%rbp
304 48 89 e5
305 4: sub $0x228,%rsp
306 48 81 ec 28 02 00 00
307 b: sub $0x28,%rbp
308 48 83 ed 28
309 f: mov %rbx,0x0(%rbp)
310 48 89 5d 00
311
312|
313| **# bpftool prog load xdp1_kern.o /sys/fs/bpf/xdp1 type xdp map name rxcnt id 7**
314| **# bpftool prog show pinned /sys/fs/bpf/xdp1**
315
316::
317
318 9: xdp name xdp_prog1 tag 539ec6ce11b52f98 gpl
319 loaded_at 2018-06-25T16:17:31-0700 uid 0
320 xlated 488B jited 336B memlock 4096B map_ids 7
321
322**# rm /sys/fs/bpf/xdp1**
323
324|
325| **# bpftool prog profile id 337 duration 10 cycles instructions llc_misses**
326
327::
328 51397 run_cnt
329 40176203 cycles (83.05%)
330 42518139 instructions # 1.06 insns per cycle (83.39%)
331 123 llc_misses # 2.89 LLC misses per million insns (83.15%)
332
333SEE ALSO
334========
335 **bpf**\ (2),
336 **bpf-helpers**\ (7),
337 **bpftool**\ (8),
338 **bpftool-map**\ (8),
339 **bpftool-cgroup**\ (8),
340 **bpftool-feature**\ (8),
341 **bpftool-net**\ (8),
342 **bpftool-perf**\ (8),
343 **bpftool-btf**\ (8)