PageRenderTime 82ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/bpf/bpftool/Documentation/bpftool-prog.rst

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