bpftool-prog.rst 14 KB

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