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