bpftool-map.rst 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. .. SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2. ================
  3. bpftool-map
  4. ================
  5. -------------------------------------------------------------------------------
  6. tool for inspection and simple manipulation of eBPF maps
  7. -------------------------------------------------------------------------------
  8. :Manual section: 8
  9. .. include:: substitutions.rst
  10. SYNOPSIS
  11. ========
  12. **bpftool** [*OPTIONS*] **map** *COMMAND*
  13. *OPTIONS* := { |COMMON_OPTIONS| | { **-f** | **--bpffs** } | { **-n** | **--nomount** } }
  14. *COMMANDS* :=
  15. { **show** | **list** | **create** | **dump** | **update** | **lookup** | **getnext** |
  16. **delete** | **pin** | **help** }
  17. MAP COMMANDS
  18. =============
  19. | **bpftool** **map** { **show** | **list** } [*MAP*]
  20. | **bpftool** **map create** *FILE* **type** *TYPE* **key** *KEY_SIZE* **value** *VALUE_SIZE* \
  21. | **entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**inner_map** *MAP*] \
  22. | [**offload_dev** *NAME*]
  23. | **bpftool** **map dump** *MAP*
  24. | **bpftool** **map update** *MAP* [**key** *DATA*] [**value** *VALUE*] [*UPDATE_FLAGS*]
  25. | **bpftool** **map lookup** *MAP* [**key** *DATA*]
  26. | **bpftool** **map getnext** *MAP* [**key** *DATA*]
  27. | **bpftool** **map delete** *MAP* **key** *DATA*
  28. | **bpftool** **map pin** *MAP* *FILE*
  29. | **bpftool** **map event_pipe** *MAP* [**cpu** *N* **index** *M*]
  30. | **bpftool** **map peek** *MAP*
  31. | **bpftool** **map push** *MAP* **value** *VALUE*
  32. | **bpftool** **map pop** *MAP*
  33. | **bpftool** **map enqueue** *MAP* **value** *VALUE*
  34. | **bpftool** **map dequeue** *MAP*
  35. | **bpftool** **map freeze** *MAP*
  36. | **bpftool** **map help**
  37. |
  38. | *MAP* := { **id** *MAP_ID* | **pinned** *FILE* | **name** *MAP_NAME* }
  39. | *DATA* := { [**hex**] *BYTES* }
  40. | *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* | **name** *PROG_NAME* }
  41. | *VALUE* := { *DATA* | *MAP* | *PROG* }
  42. | *UPDATE_FLAGS* := { **any** | **exist** | **noexist** }
  43. | *TYPE* := { **hash** | **array** | **prog_array** | **perf_event_array** | **percpu_hash**
  44. | | **percpu_array** | **stack_trace** | **cgroup_array** | **lru_hash**
  45. | | **lru_percpu_hash** | **lpm_trie** | **array_of_maps** | **hash_of_maps**
  46. | | **devmap** | **devmap_hash** | **sockmap** | **cpumap** | **xskmap** | **sockhash**
  47. | | **cgroup_storage** | **reuseport_sockarray** | **percpu_cgroup_storage**
  48. | | **queue** | **stack** | **sk_storage** | **struct_ops** | **ringbuf** | **inode_storage**
  49. | | **task_storage** | **bloom_filter** | **user_ringbuf** | **cgrp_storage** | **arena** }
  50. DESCRIPTION
  51. ===========
  52. bpftool map { show | list } [*MAP*]
  53. Show information about loaded maps. If *MAP* is specified show information
  54. only about given maps, otherwise list all maps currently loaded on the
  55. system. In case of **name**, *MAP* may match several maps which will all
  56. be shown.
  57. Output will start with map ID followed by map type and zero or more named
  58. attributes (depending on kernel version).
  59. Since Linux 5.8 bpftool is able to discover information about processes
  60. that hold open file descriptors (FDs) against BPF maps. On such kernels
  61. bpftool will automatically emit this information as well.
  62. bpftool map create *FILE* type *TYPE* key *KEY_SIZE* value *VALUE_SIZE* entries *MAX_ENTRIES* name *NAME* [flags *FLAGS*] [inner_map *MAP*] [offload_dev *NAME*]
  63. Create a new map with given parameters and pin it to *bpffs* as *FILE*.
  64. *FLAGS* should be an integer which is the combination of desired flags,
  65. e.g. 1024 for **BPF_F_MMAPABLE** (see bpf.h UAPI header for existing
  66. flags).
  67. To create maps of type array-of-maps or hash-of-maps, the **inner_map**
  68. keyword must be used to pass an inner map. The kernel needs it to collect
  69. metadata related to the inner maps that the new map will work with.
  70. Keyword **offload_dev** expects a network interface name, and is used to
  71. request hardware offload for the map.
  72. bpftool map dump *MAP*
  73. Dump all entries in a given *MAP*. In case of **name**, *MAP* may match
  74. several maps which will all be dumped.
  75. bpftool map update *MAP* [key *DATA*] [value *VALUE*] [*UPDATE_FLAGS*]
  76. Update map entry for a given *KEY*.
  77. *UPDATE_FLAGS* can be one of: **any** update existing entry or add if
  78. doesn't exit; **exist** update only if entry already exists; **noexist**
  79. update only if entry doesn't exist.
  80. If the **hex** keyword is provided in front of the bytes sequence, the
  81. bytes are parsed as hexadecimal values, even if no "0x" prefix is added. If
  82. the keyword is not provided, then the bytes are parsed as decimal values,
  83. unless a "0x" prefix (for hexadecimal) or a "0" prefix (for octal) is
  84. provided.
  85. bpftool map lookup *MAP* [key *DATA*]
  86. Lookup **key** in the map.
  87. bpftool map getnext *MAP* [key *DATA*]
  88. Get next key. If *key* is not specified, get first key.
  89. bpftool map delete *MAP* key *DATA*
  90. Remove entry from the map.
  91. bpftool map pin *MAP* *FILE*
  92. Pin map *MAP* as *FILE*.
  93. Note: *FILE* must be located in *bpffs* mount. It must not contain a dot
  94. character ('.'), which is reserved for future extensions of *bpffs*.
  95. bpftool map event_pipe *MAP* [cpu *N* index *M*]
  96. Read events from a **BPF_MAP_TYPE_PERF_EVENT_ARRAY** map.
  97. Install perf rings into a perf event array map and dump output of any
  98. **bpf_perf_event_output**\ () call in the kernel. By default read the
  99. number of CPUs on the system and install perf ring for each CPU in the
  100. corresponding index in the array.
  101. If **cpu** and **index** are specified, install perf ring for given **cpu**
  102. at **index** in the array (single ring).
  103. Note that installing a perf ring into an array will silently replace any
  104. existing ring. Any other application will stop receiving events if it
  105. installed its rings earlier.
  106. bpftool map peek *MAP*
  107. Peek next value in the queue or stack.
  108. bpftool map push *MAP* value *VALUE*
  109. Push *VALUE* onto the stack.
  110. bpftool map pop *MAP*
  111. Pop and print value from the stack.
  112. bpftool map enqueue *MAP* value *VALUE*
  113. Enqueue *VALUE* into the queue.
  114. bpftool map dequeue *MAP*
  115. Dequeue and print value from the queue.
  116. bpftool map freeze *MAP*
  117. Freeze the map as read-only from user space. Entries from a frozen map can
  118. not longer be updated or deleted with the **bpf**\ () system call. This
  119. operation is not reversible, and the map remains immutable from user space
  120. until its destruction. However, read and write permissions for BPF programs
  121. to the map remain unchanged.
  122. bpftool map help
  123. Print short help message.
  124. OPTIONS
  125. =======
  126. .. include:: common_options.rst
  127. -f, --bpffs
  128. Show file names of pinned maps.
  129. -n, --nomount
  130. Do not automatically attempt to mount any virtual file system (such as
  131. tracefs or BPF virtual file system) when necessary.
  132. EXAMPLES
  133. ========
  134. **# bpftool map show**
  135. ::
  136. 10: hash name some_map flags 0x0
  137. key 4B value 8B max_entries 2048 memlock 167936B
  138. pids systemd(1)
  139. The following three commands are equivalent:
  140. |
  141. | **# bpftool map update id 10 key hex 20 c4 b7 00 value hex 0f ff ff ab 01 02 03 4c**
  142. | **# bpftool map update id 10 key 0x20 0xc4 0xb7 0x00 value 0x0f 0xff 0xff 0xab 0x01 0x02 0x03 0x4c**
  143. | **# bpftool map update id 10 key 32 196 183 0 value 15 255 255 171 1 2 3 76**
  144. **# bpftool map lookup id 10 key 0 1 2 3**
  145. ::
  146. key: 00 01 02 03 value: 00 01 02 03 04 05 06 07
  147. **# bpftool map dump id 10**
  148. ::
  149. key: 00 01 02 03 value: 00 01 02 03 04 05 06 07
  150. key: 0d 00 07 00 value: 02 00 00 00 01 02 03 04
  151. Found 2 elements
  152. **# bpftool map getnext id 10 key 0 1 2 3**
  153. ::
  154. key:
  155. 00 01 02 03
  156. next key:
  157. 0d 00 07 00
  158. |
  159. | **# mount -t bpf none /sys/fs/bpf/**
  160. | **# bpftool map pin id 10 /sys/fs/bpf/map**
  161. | **# bpftool map del pinned /sys/fs/bpf/map key 13 00 07 00**
  162. Note that map update can also be used in order to change the program references
  163. hold by a program array map. This can be used, for example, to change the
  164. programs used for tail-call jumps at runtime, without having to reload the
  165. entry-point program. Below is an example for this use case: we load a program
  166. defining a prog array map, and with a main function that contains a tail call
  167. to other programs that can be used either to "process" packets or to "debug"
  168. processing. Note that the prog array map MUST be pinned into the BPF virtual
  169. file system for the map update to work successfully, as kernel flushes prog
  170. array maps when they have no more references from user space (and the update
  171. would be lost as soon as bpftool exits).
  172. |
  173. | **# bpftool prog loadall tail_calls.o /sys/fs/bpf/foo type xdp**
  174. | **# bpftool prog --bpffs**
  175. ::
  176. 545: xdp name main_func tag 674b4b5597193dc3 gpl
  177. loaded_at 2018-12-12T15:02:58+0000 uid 0
  178. xlated 240B jited 257B memlock 4096B map_ids 294
  179. pinned /sys/fs/bpf/foo/xdp
  180. 546: xdp name bpf_func_process tag e369a529024751fc gpl
  181. loaded_at 2018-12-12T15:02:58+0000 uid 0
  182. xlated 200B jited 164B memlock 4096B
  183. pinned /sys/fs/bpf/foo/process
  184. 547: xdp name bpf_func_debug tag 0b597868bc7f0976 gpl
  185. loaded_at 2018-12-12T15:02:58+0000 uid 0
  186. xlated 200B jited 164B memlock 4096B
  187. pinned /sys/fs/bpf/foo/debug
  188. **# bpftool map**
  189. ::
  190. 294: prog_array name jmp_table flags 0x0
  191. key 4B value 4B max_entries 1 memlock 4096B
  192. owner_prog_type xdp owner jited
  193. |
  194. | **# bpftool map pin id 294 /sys/fs/bpf/bar**
  195. | **# bpftool map dump pinned /sys/fs/bpf/bar**
  196. ::
  197. Found 0 elements
  198. |
  199. | **# bpftool map update pinned /sys/fs/bpf/bar key 0 0 0 0 value pinned /sys/fs/bpf/foo/debug**
  200. | **# bpftool map dump pinned /sys/fs/bpf/bar**
  201. ::
  202. key: 00 00 00 00 value: 22 02 00 00
  203. Found 1 element