bpftool-map.rst 10.1 KB
Newer Older
J
Jakub Kicinski 已提交
1 2 3 4 5 6 7 8 9 10 11 12
================
bpftool-map
================
-------------------------------------------------------------------------------
tool for inspection and simple manipulation of eBPF maps
-------------------------------------------------------------------------------

:Manual section: 8

SYNOPSIS
========

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

15
	*OPTIONS* := { { **-j** | **--json** } [{ **-p** | **--pretty** }] | { **-f** | **--bpffs** } }
J
Jakub Kicinski 已提交
16 17

	*COMMANDS* :=
18 19
	{ **show** | **list** | **create** | **dump** | **update** | **lookup** | **getnext**
	| **delete** | **pin** | **help** }
J
Jakub Kicinski 已提交
20 21 22 23

MAP COMMANDS
=============

24
|	**bpftool** **map** { **show** | **list** }   [*MAP*]
25 26
|	**bpftool** **map create**     *FILE* **type** *TYPE* **key** *KEY_SIZE* **value** *VALUE_SIZE* \
|		**entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**dev** *NAME*]
27
|	**bpftool** **map dump**       *MAP*
28
|	**bpftool** **map update**     *MAP* [**key** *DATA*] [**value** *VALUE*] [*UPDATE_FLAGS*]
29
|	**bpftool** **map lookup**     *MAP* [**key** *DATA*]
30 31 32 33
|	**bpftool** **map getnext**    *MAP* [**key** *DATA*]
|	**bpftool** **map delete**     *MAP*  **key** *DATA*
|	**bpftool** **map pin**        *MAP*  *FILE*
|	**bpftool** **map event_pipe** *MAP* [**cpu** *N* **index** *M*]
S
Stanislav Fomichev 已提交
34
|	**bpftool** **map peek**       *MAP*
35
|	**bpftool** **map push**       *MAP* **value** *VALUE*
36
|	**bpftool** **map pop**        *MAP*
37
|	**bpftool** **map enqueue**    *MAP* **value** *VALUE*
38
|	**bpftool** **map dequeue**    *MAP*
39
|	**bpftool** **map freeze**     *MAP*
40
|	**bpftool** **map help**
J
Jakub Kicinski 已提交
41
|
P
Paul Chaignon 已提交
42
|	*MAP* := { **id** *MAP_ID* | **pinned** *FILE* | **name** *MAP_NAME* }
43
|	*DATA* := { [**hex**] *BYTES* }
P
Paul Chaignon 已提交
44
|	*PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* | **name** *PROG_NAME* }
45
|	*VALUE* := { *DATA* | *MAP* | *PROG* }
46
|	*UPDATE_FLAGS* := { **any** | **exist** | **noexist** }
47 48 49
|	*TYPE* := { **hash** | **array** | **prog_array** | **perf_event_array** | **percpu_hash**
|		| **percpu_array** | **stack_trace** | **cgroup_array** | **lru_hash**
|		| **lru_percpu_hash** | **lpm_trie** | **array_of_maps** | **hash_of_maps**
50
|		| **devmap** | **devmap_hash** | **sockmap** | **cpumap** | **xskmap** | **sockhash**
51
|		| **cgroup_storage** | **reuseport_sockarray** | **percpu_cgroup_storage**
52
|		| **queue** | **stack** | **sk_storage** | **struct_ops** | **ringbuf** }
J
Jakub Kicinski 已提交
53 54 55

DESCRIPTION
===========
56
	**bpftool map { show | list }**   [*MAP*]
J
Jakub Kicinski 已提交
57
		  Show information about loaded maps.  If *MAP* is specified
P
Paul Chaignon 已提交
58 59 60
		  show information only about given maps, otherwise list all
		  maps currently loaded on the system.  In case of **name**,
		  *MAP* may match several maps which will all be shown.
J
Jakub Kicinski 已提交
61 62 63 64

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

65 66 67 68 69
		  Since Linux 5.8 bpftool is able to discover information about
		  processes that hold open file descriptors (FDs) against BPF
		  maps. On such kernels bpftool will automatically emit this
		  information as well.

70 71 72 73
	**bpftool map create** *FILE* **type** *TYPE* **key** *KEY_SIZE* **value** *VALUE_SIZE*  **entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**dev** *NAME*]
		  Create a new map with given parameters and pin it to *bpffs*
		  as *FILE*.

74 75 76 77 78 79 80
		  *FLAGS* should be an integer which is the combination of
		  desired flags, e.g. 1024 for **BPF_F_MMAPABLE** (see bpf.h
		  UAPI header for existing flags).

		  Keyword **dev** expects a network interface name, and is used
		  to request hardware offload for the map.

J
Jakub Kicinski 已提交
81
	**bpftool map dump**    *MAP*
P
Paul Chaignon 已提交
82 83
		  Dump all entries in a given *MAP*.  In case of **name**,
		  *MAP* may match several maps which will all be dumped.
J
Jakub Kicinski 已提交
84

85
	**bpftool map update**  *MAP* [**key** *DATA*] [**value** *VALUE*] [*UPDATE_FLAGS*]
J
Jakub Kicinski 已提交
86 87 88 89 90 91
		  Update map entry for a given *KEY*.

		  *UPDATE_FLAGS* can be one of: **any** update existing entry
		  or add if doesn't exit; **exist** update only if entry already
		  exists; **noexist** update only if entry doesn't exist.

92
		  If the **hex** keyword is provided in front of the bytes
93
		  sequence, the bytes are parsed as hexadecimal values, even if
94 95 96 97
		  no "0x" prefix is added. If the keyword is not provided, then
		  the bytes are parsed as decimal values, unless a "0x" prefix
		  (for hexadecimal) or a "0" prefix (for octal) is provided.

98
	**bpftool map lookup**  *MAP* [**key** *DATA*]
J
Jakub Kicinski 已提交
99 100
		  Lookup **key** in the map.

101
	**bpftool map getnext** *MAP* [**key** *DATA*]
J
Jakub Kicinski 已提交
102 103
		  Get next key.  If *key* is not specified, get first key.

104
	**bpftool map delete**  *MAP*  **key** *DATA*
J
Jakub Kicinski 已提交
105 106 107 108 109
		  Remove entry from the map.

	**bpftool map pin**     *MAP*  *FILE*
		  Pin map *MAP* as *FILE*.

110 111 112
		  Note: *FILE* must be located in *bpffs* mount. It must not
		  contain a dot character ('.'), which is reserved for future
		  extensions of *bpffs*.
J
Jakub Kicinski 已提交
113

114
	**bpftool** **map event_pipe** *MAP* [**cpu** *N* **index** *M*]
115
		  Read events from a **BPF_MAP_TYPE_PERF_EVENT_ARRAY** map.
116 117

		  Install perf rings into a perf event array map and dump
118
		  output of any **bpf_perf_event_output**\ () call in the kernel.
119 120 121 122 123 124 125 126 127 128 129
		  By default read the number of CPUs on the system and
		  install perf ring for each CPU in the corresponding index
		  in the array.

		  If **cpu** and **index** are specified, install perf ring
		  for given **cpu** at **index** in the array (single ring).

		  Note that installing a perf ring into an array will silently
		  replace any existing ring.  Any other application will stop
		  receiving events if it installed its rings earlier.

S
Stanislav Fomichev 已提交
130
	**bpftool map peek**  *MAP*
131
		  Peek next value in the queue or stack.
S
Stanislav Fomichev 已提交
132

133
	**bpftool map push**  *MAP* **value** *VALUE*
134
		  Push *VALUE* onto the stack.
135

136
	**bpftool map pop**  *MAP*
137
		  Pop and print value from the stack.
138

139
	**bpftool map enqueue**  *MAP* **value** *VALUE*
140
		  Enqueue *VALUE* into the queue.
141

142
	**bpftool map dequeue**  *MAP*
143
		  Dequeue and print value from the queue.
144

145 146 147
	**bpftool map freeze**  *MAP*
		  Freeze the map as read-only from user space. Entries from a
		  frozen map can not longer be updated or deleted with the
148
		  **bpf**\ () system call. This operation is not reversible,
149 150 151 152
		  and the map remains immutable from user space until its
		  destruction. However, read and write permissions for BPF
		  programs to the map remain unchanged.

J
Jakub Kicinski 已提交
153 154 155
	**bpftool map help**
		  Print short help message.

156 157 158 159 160
OPTIONS
=======
	-h, --help
		  Print short generic help message (similar to **bpftool help**).

161
	-V, --version
162 163
		  Print version number (similar to **bpftool version**).

164 165 166 167 168 169 170
	-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**.

171 172 173
	-f, --bpffs
		  Show file names of pinned maps.

174 175 176 177
	-n, --nomount
		  Do not automatically attempt to mount any virtual file system
		  (such as tracefs or BPF virtual file system) when necessary.

178 179 180 181
	-d, --debug
		  Print all logs available from libbpf, including debug-level
		  information.

J
Jakub Kicinski 已提交
182 183 184 185 186 187
EXAMPLES
========
**# bpftool map show**
::

  10: hash  name some_map  flags 0x0
188 189
        key 4B  value 8B  max_entries 2048  memlock 167936B
        pids systemd(1)
J
Jakub Kicinski 已提交
190

191 192 193 194 195 196
The following three commands are equivalent:

|
| **# bpftool map update id 10 key hex   20   c4   b7   00 value hex   0f   ff   ff   ab   01   02   03   4c**
| **# bpftool map update id 10 key     0x20 0xc4 0xb7 0x00 value     0x0f 0xff 0xff 0xab 0x01 0x02 0x03 0x4c**
| **# bpftool map update id 10 key       32  196  183    0 value       15  255  255  171    1    2    3   76**
J
Jakub Kicinski 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

**# bpftool map lookup id 10 key 0 1 2 3**

::

  key: 00 01 02 03 value: 00 01 02 03 04 05 06 07


**# bpftool map dump id 10**
::

  key: 00 01 02 03  value: 00 01 02 03 04 05 06 07
  key: 0d 00 07 00  value: 02 00 00 00 01 02 03 04
  Found 2 elements

**# bpftool map getnext id 10 key 0 1 2 3**
::

  key:
  00 01 02 03
  next key:
  0d 00 07 00

|
| **# mount -t bpf none /sys/fs/bpf/**
| **# bpftool map pin id 10 /sys/fs/bpf/map**
| **# bpftool map del pinned /sys/fs/bpf/map key 13 00 07 00**

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
Note that map update can also be used in order to change the program references
hold by a program array map. This can be used, for example, to change the
programs used for tail-call jumps at runtime, without having to reload the
entry-point program. Below is an example for this use case: we load a program
defining a prog array map, and with a main function that contains a tail call
to other programs that can be used either to "process" packets or to "debug"
processing. Note that the prog array map MUST be pinned into the BPF virtual
file system for the map update to work successfully, as kernel flushes prog
array maps when they have no more references from user space (and the update
would be lost as soon as bpftool exits).

|
| **# bpftool prog loadall tail_calls.o /sys/fs/bpf/foo type xdp**
| **# bpftool prog --bpffs**

::

  545: xdp  name main_func  tag 674b4b5597193dc3  gpl
          loaded_at 2018-12-12T15:02:58+0000  uid 0
          xlated 240B  jited 257B  memlock 4096B  map_ids 294
          pinned /sys/fs/bpf/foo/xdp
  546: xdp  name bpf_func_process  tag e369a529024751fc  gpl
          loaded_at 2018-12-12T15:02:58+0000  uid 0
          xlated 200B  jited 164B  memlock 4096B
          pinned /sys/fs/bpf/foo/process
  547: xdp  name bpf_func_debug  tag 0b597868bc7f0976  gpl
          loaded_at 2018-12-12T15:02:58+0000  uid 0
          xlated 200B  jited 164B  memlock 4096B
          pinned /sys/fs/bpf/foo/debug

**# bpftool map**

::

  294: prog_array  name jmp_table  flags 0x0
          key 4B  value 4B  max_entries 1  memlock 4096B
          owner_prog_type xdp  owner jited

|
| **# bpftool map pin id 294 /sys/fs/bpf/bar**
| **# bpftool map dump pinned /sys/fs/bpf/bar**

::

  Found 0 elements

|
| **# bpftool map update pinned /sys/fs/bpf/bar key 0 0 0 0 value pinned /sys/fs/bpf/foo/debug**
| **# bpftool map dump pinned /sys/fs/bpf/bar**

::

  key: 00 00 00 00  value: 22 02 00 00
  Found 1 element

J
Jakub Kicinski 已提交
280 281
SEE ALSO
========
282 283 284
	**bpf**\ (2),
	**bpf-helpers**\ (7),
	**bpftool**\ (8),
285
	**bpftool-btf**\ (8),
286
	**bpftool-cgroup**\ (8),
287
	**bpftool-feature**\ (8),
288 289 290
	**bpftool-gen**\ (8),
	**bpftool-iter**\ (8),
	**bpftool-link**\ (8),
291
	**bpftool-net**\ (8),
292
	**bpftool-perf**\ (8),
293 294
	**bpftool-prog**\ (8),
	**bpftool-struct_ops**\ (8)