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