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