bpftool-map.rst 9.8 KB

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