bpftool-prog.rst 13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
================
bpftool-prog
================
-------------------------------------------------------------------------------
tool for inspection and simple manipulation of eBPF progs
-------------------------------------------------------------------------------

:Manual section: 8

SYNOPSIS
========

13 14
	**bpftool** [*OPTIONS*] **prog** *COMMAND*

15
	*OPTIONS* := { { **-j** | **--json** } [{ **-p** | **--pretty** }] | { **-f** | **--bpffs** } }
16 17

	*COMMANDS* :=
18 19
	{ **show** | **list** | **dump xlated** | **dump jited** | **pin** | **load**
	| **loadall** | **help** }
20

21
PROG COMMANDS
22 23
=============

24
|	**bpftool** **prog** { **show** | **list** } [*PROG*]
25 26
|	**bpftool** **prog dump xlated** *PROG* [{**file** *FILE* | **opcodes** | **visual** | **linum**}]
|	**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 30 31
|	**bpftool** **prog attach** *PROG* *ATTACH_TYPE* [*MAP*]
|	**bpftool** **prog detach** *PROG* *ATTACH_TYPE* [*MAP*]
|	**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 39 40 41 42 43
|	*TYPE* := {
|		**socket** | **kprobe** | **kretprobe** | **classifier** | **action** |
|		**tracepoint** | **raw_tracepoint** | **xdp** | **perf_event** | **cgroup/skb** |
|		**cgroup/sock** | **cgroup/dev** | **lwt_in** | **lwt_out** | **lwt_xmit** |
|		**lwt_seg6local** | **sockops** | **sk_skb** | **sk_msg** | **lirc_mode2** |
|		**cgroup/bind4** | **cgroup/bind6** | **cgroup/post_bind4** | **cgroup/post_bind6** |
44 45
|		**cgroup/connect4** | **cgroup/connect6** | **cgroup/getpeername4** | **cgroup/getpeername6** |
|               **cgroup/getsockname4** | **cgroup/getsockname6** | **cgroup/sendmsg4** | **cgroup/sendmsg6** |
46
|		**cgroup/recvmsg4** | **cgroup/recvmsg6** | **cgroup/sysctl** |
47 48
|		**cgroup/getsockopt** | **cgroup/setsockopt** |
|		**struct_ops** | **fentry** | **fexit** | **freplace**
49
|	}
50
|       *ATTACH_TYPE* := {
51
|		**msg_verdict** | **stream_verdict** | **stream_parser** | **flow_dissector**
52
|	}
53
|	*METRICs* := {
54 55
|		**cycles** | **instructions** | **l1d_loads** | **llc_misses**
|	}
56

57 58 59

DESCRIPTION
===========
60
	**bpftool prog { show | list }** [*PROG*]
61
		  Show information about loaded programs.  If *PROG* is
62 63
		  specified show information only about given programs,
		  otherwise list all programs currently loaded on the system.
64 65
		  In case of **tag** or **name**, *PROG* may match several
		  programs which will all be shown.
66 67 68 69

		  Output will start with program ID followed by program type and
		  zero or more named attributes (depending on kernel version).

70 71 72 73 74 75 76 77
		  Since Linux 5.1 the kernel can collect statistics on BPF
		  programs (such as the total time spent running the program,
		  and the number of times it was run). If available, bpftool
		  shows such statistics. However, the kernel does not collect
		  them by defaults, as it slightly impacts performance on each
		  program run. Activation or deactivation of the feature is
		  performed via the **kernel.bpf_stats_enabled** sysctl knob.

78
	**bpftool prog dump xlated** *PROG* [{ **file** *FILE* | **opcodes** | **visual** | **linum** }]
79
		  Dump eBPF instructions of the programs from the kernel. By
80 81 82
		  default, eBPF will be disassembled and printed to standard
		  output in human-readable format. In this case, **opcodes**
		  controls if raw opcodes should be printed as well.
83

84 85 86
		  In case of **tag** or **name**, *PROG* may match several
		  programs which will all be dumped.  However, if **file** or
		  **visual** is specified, *PROG* must match a single program.
87

88 89 90 91 92 93
		  If **file** is specified, the binary image will instead be
		  written to *FILE*.

		  If **visual** is specified, control flow graph (CFG) will be
		  built instead, and eBPF instructions will be presented with
		  CFG in DOT format, on standard output.
94

95
		  If the programs have line_info available, the source line will
96 97 98
		  be displayed by default.  If **linum** is specified,
		  the filename, line number and line column will also be
		  displayed on top of the source line.
99

100
	**bpftool prog dump jited**  *PROG* [{ **file** *FILE* | **opcodes** | **linum** }]
101
		  Dump jited image (host machine code) of the program.
102

103 104
		  If *FILE* is specified image will be written to a file,
		  otherwise it will be disassembled and printed to stdout.
105
		  *PROG* must match a single program when **file** is specified.
106 107 108

		  **opcodes** controls if raw opcodes will be printed.

109 110 111 112
		  If the prog has line_info available, the source line will
		  be displayed by default.  If **linum** is specified,
		  the filename, line number and line column will also be
		  displayed on top of the source line.
113

114 115 116
	**bpftool prog pin** *PROG* *FILE*
		  Pin program *PROG* as *FILE*.

117 118 119
		  Note: *FILE* must be located in *bpffs* mount. It must not
		  contain a dot character ('.'), which is reserved for future
		  extensions of *bpffs*.
120

121
	**bpftool prog { load | loadall }** *OBJ* *PATH* [**type** *TYPE*] [**map** {**idx** *IDX* | **name** *NAME*} *MAP*] [**dev** *NAME*] [**pinmaps** *MAP_DIR*]
122 123 124 125
		  Load bpf program(s) from binary *OBJ* and pin as *PATH*.
		  **bpftool prog load** pins only the first program from the
		  *OBJ* as *PATH*. **bpftool prog loadall** pins all programs
		  from the *OBJ* under *PATH* directory.
126 127
		  **type** is optional, if not specified program type will be
		  inferred from section names.
128 129 130 131 132 133 134
		  By default bpftool will create new maps as declared in the ELF
		  object being loaded.  **map** parameter allows for the reuse
		  of existing maps.  It can be specified multiple times, each
		  time for a different map.  *IDX* refers to index of the map
		  to be replaced in the ELF file counting from 0, while *NAME*
		  allows to replace a map by name.  *MAP* specifies the map to
		  use, referring to it by **id** or through a **pinned** file.
135 136
		  If **dev** *NAME* is specified program will be loaded onto
		  given networking device (offload).
137 138
		  Optional **pinmaps** argument can be provided to pin all
		  maps under *MAP_DIR* directory.
139

140
		  Note: *PATH* must be located in *bpffs* mount. It must not
141 142
		  contain a dot character ('.'), which is reserved for future
		  extensions of *bpffs*.
143

144 145 146 147 148 149 150 151 152 153 154
	**bpftool prog attach** *PROG* *ATTACH_TYPE* [*MAP*]
		  Attach bpf program *PROG* (with type specified by
		  *ATTACH_TYPE*). Most *ATTACH_TYPEs* require a *MAP*
		  parameter, with the exception of *flow_dissector* which is
		  attached to current networking name space.

	**bpftool prog detach** *PROG* *ATTACH_TYPE* [*MAP*]
		  Detach bpf program *PROG* (with type specified by
		  *ATTACH_TYPE*). Most *ATTACH_TYPEs* require a *MAP*
		  parameter, with the exception of *flow_dissector* which is
		  detached from the current networking name space.
155

156 157 158
	**bpftool prog tracelog**
		  Dump the trace pipe of the system to the console (stdout).
		  Hit <Ctrl+C> to stop printing. BPF programs can write to this
159
		  trace pipe at runtime with the **bpf_trace_printk**\ () helper.
160 161 162 163
		  This should be used only for debugging purposes. For
		  streaming data from BPF programs to user space, one can use
		  perf events (see also **bpftool-map**\ (8)).

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	**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*]
		  Run BPF program *PROG* in the kernel testing infrastructure
		  for BPF, meaning that the program works on the data and
		  context provided by the user, and not on actual packets or
		  monitored functions etc. Return value and duration for the
		  test run are printed out to the console.

		  Input data is read from the *FILE* passed with **data_in**.
		  If this *FILE* is "**-**", input data is read from standard
		  input. Input context, if any, is read from *FILE* passed with
		  **ctx_in**. Again, "**-**" can be used to read from standard
		  input, but only if standard input is not already in use for
		  input data. If a *FILE* is passed with **data_out**, output
		  data is written to that file. Similarly, output context is
		  written to the *FILE* passed with **ctx_out**. For both
		  output flows, "**-**" can be used to print to the standard
		  output (as plain text, or JSON if relevant option was
		  passed). If output keywords are omitted, output data and
		  context are discarded. Keywords **data_size_out** and
		  **ctx_size_out** are used to pass the size (in bytes) for the
		  output buffers to the kernel, although the default of 32 kB
		  should be more than enough for most cases.

		  Keyword **repeat** is used to indicate the number of
		  consecutive runs to perform. Note that output data and
		  context printed to files correspond to the last of those
		  runs. The duration printed out at the end of the runs is an
		  average over all runs performed by the command.

		  Not all program types support test run. Among those which do,
		  not all of them can take the **ctx_in**/**ctx_out**
		  arguments. bpftool does not perform checks on program types.

197 198
	**bpftool prog profile** *PROG* [**duration** *DURATION*] *METRICs*
		  Profile *METRICs* for bpf program *PROG* for *DURATION*
199
		  seconds or until user hits <Ctrl+C>. *DURATION* is optional.
200
		  If *DURATION* is not specified, the profiling will run up to
201
		  **UINT_MAX** seconds.
202

203 204 205
	**bpftool prog help**
		  Print short help message.

206 207 208 209 210
OPTIONS
=======
	-h, --help
		  Print short generic help message (similar to **bpftool help**).

211
	-V, --version
212 213
		  Print version number (similar to **bpftool version**).

214 215 216 217 218 219 220
	-j, --json
		  Generate JSON output. For commands that cannot produce JSON, this
		  option has no effect.

	-p, --pretty
		  Generate human-readable JSON output. Implies **-j**.

221
	-f, --bpffs
222 223
		  When showing BPF programs, show file names of pinned
		  programs.
224

225 226 227
	-m, --mapcompat
		  Allow loading maps with unknown map definitions.

228 229 230 231
	-n, --nomount
		  Do not automatically attempt to mount any virtual file system
		  (such as tracefs or BPF virtual file system) when necessary.

232
	-d, --debug
233 234 235
		  Print all logs available, even debug-level information. This
		  includes logs from libbpf as well as from the verifier, when
		  attempting to load programs.
236

237 238 239
EXAMPLES
========
**# bpftool prog show**
240

241 242
::

243
    10: xdp  name some_prog  tag 005a3d2123620c8b  gpl run_time_ns 81632 run_cnt 10
244 245
            loaded_at 2017-09-29T20:11:00+0000  uid 0
            xlated 528B  jited 370B  memlock 4096B  map_ids 10
246

247 248 249 250
**# bpftool --json --pretty prog show**

::

251 252 253 254 255
    [{
            "id": 10,
            "type": "xdp",
            "tag": "005a3d2123620c8b",
            "gpl_compatible": true,
256 257
            "run_time_ns": 81632,
            "run_cnt": 10,
258 259 260 261 262 263 264 265 266 267
            "loaded_at": 1506715860,
            "uid": 0,
            "bytes_xlated": 528,
            "jited": true,
            "bytes_jited": 370,
            "bytes_memlock": 4096,
            "map_ids": [10
            ]
        }
    ]
268

269 270
|
| **# bpftool prog dump xlated id 10 file /tmp/t**
271
| **$ ls -l /tmp/t**
272

273 274 275 276 277
::

    -rw------- 1 root root 560 Jul 22 01:42 /tmp/t

**# bpftool prog dump jited tag 005a3d2123620c8b**
278 279 280

::

281 282 283 284 285
    0:   push   %rbp
    1:   mov    %rsp,%rbp
    2:   sub    $0x228,%rsp
    3:   sub    $0x28,%rbp
    4:   mov    %rbx,0x0(%rbp)
286

287 288 289
|
| **# mount -t bpf none /sys/fs/bpf/**
| **# bpftool prog pin id 10 /sys/fs/bpf/prog**
290
| **# bpftool prog load ./my_prog.o /sys/fs/bpf/prog2**
291 292
| **# ls -l /sys/fs/bpf/**

293 294 295 296 297 298
::

    -rw------- 1 root root 0 Jul 22 01:43 prog
    -rw------- 1 root root 0 Jul 22 01:44 prog2

**# bpftool prog dump jited pinned /sys/fs/bpf/prog opcodes**
299 300 301

::

302 303 304 305 306 307 308 309 310 311
   0:   push   %rbp
        55
   1:   mov    %rsp,%rbp
        48 89 e5
   4:   sub    $0x228,%rsp
        48 81 ec 28 02 00 00
   b:   sub    $0x28,%rbp
        48 83 ed 28
   f:   mov    %rbx,0x0(%rbp)
        48 89 5d 00
312

313 314 315
|
| **# bpftool prog load xdp1_kern.o /sys/fs/bpf/xdp1 type xdp map name rxcnt id 7**
| **# bpftool prog show pinned /sys/fs/bpf/xdp1**
316 317 318 319 320 321 322 323

::

    9: xdp  name xdp_prog1  tag 539ec6ce11b52f98  gpl
            loaded_at 2018-06-25T16:17:31-0700  uid 0
            xlated 488B  jited 336B  memlock 4096B  map_ids 7

**# rm /sys/fs/bpf/xdp1**
324

325 326 327 328
|
| **# bpftool prog profile id 337 duration 10 cycles instructions llc_misses**

::
329

330 331 332 333 334
         51397 run_cnt
      40176203 cycles                                                 (83.05%)
      42518139 instructions    #   1.06 insns per cycle               (83.39%)
           123 llc_misses      #   2.89 LLC misses per million insns  (83.15%)

335 336
SEE ALSO
========
337 338 339
	**bpf**\ (2),
	**bpf-helpers**\ (7),
	**bpftool**\ (8),
340
	**bpftool-btf**\ (8),
341
	**bpftool-cgroup**\ (8),
342
	**bpftool-feature**\ (8),
343 344 345 346
	**bpftool-gen**\ (8),
	**bpftool-iter**\ (8),
	**bpftool-link**\ (8),
	**bpftool-map**\ (8),
347
	**bpftool-net**\ (8),
348
	**bpftool-perf**\ (8),
349
	**bpftool-struct_ops**\ (8)