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