162306a36Sopenharmony_ci.. SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci================
462306a36Sopenharmony_cibpftool-map
562306a36Sopenharmony_ci================
662306a36Sopenharmony_ci-------------------------------------------------------------------------------
762306a36Sopenharmony_citool for inspection and simple manipulation of eBPF maps
862306a36Sopenharmony_ci-------------------------------------------------------------------------------
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci:Manual section: 8
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci.. include:: substitutions.rst
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ciSYNOPSIS
1562306a36Sopenharmony_ci========
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci	**bpftool** [*OPTIONS*] **map** *COMMAND*
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci	*OPTIONS* := { |COMMON_OPTIONS| | { **-f** | **--bpffs** } | { **-n** | **--nomount** } }
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci	*COMMANDS* :=
2262306a36Sopenharmony_ci	{ **show** | **list** | **create** | **dump** | **update** | **lookup** | **getnext** |
2362306a36Sopenharmony_ci	**delete** | **pin** | **help** }
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciMAP COMMANDS
2662306a36Sopenharmony_ci=============
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci|	**bpftool** **map** { **show** | **list** }   [*MAP*]
2962306a36Sopenharmony_ci|	**bpftool** **map create**     *FILE* **type** *TYPE* **key** *KEY_SIZE* **value** *VALUE_SIZE* \
3062306a36Sopenharmony_ci|		**entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**inner_map** *MAP*] \
3162306a36Sopenharmony_ci|		[**offload_dev** *NAME*]
3262306a36Sopenharmony_ci|	**bpftool** **map dump**       *MAP*
3362306a36Sopenharmony_ci|	**bpftool** **map update**     *MAP* [**key** *DATA*] [**value** *VALUE*] [*UPDATE_FLAGS*]
3462306a36Sopenharmony_ci|	**bpftool** **map lookup**     *MAP* [**key** *DATA*]
3562306a36Sopenharmony_ci|	**bpftool** **map getnext**    *MAP* [**key** *DATA*]
3662306a36Sopenharmony_ci|	**bpftool** **map delete**     *MAP*  **key** *DATA*
3762306a36Sopenharmony_ci|	**bpftool** **map pin**        *MAP*  *FILE*
3862306a36Sopenharmony_ci|	**bpftool** **map event_pipe** *MAP* [**cpu** *N* **index** *M*]
3962306a36Sopenharmony_ci|	**bpftool** **map peek**       *MAP*
4062306a36Sopenharmony_ci|	**bpftool** **map push**       *MAP* **value** *VALUE*
4162306a36Sopenharmony_ci|	**bpftool** **map pop**        *MAP*
4262306a36Sopenharmony_ci|	**bpftool** **map enqueue**    *MAP* **value** *VALUE*
4362306a36Sopenharmony_ci|	**bpftool** **map dequeue**    *MAP*
4462306a36Sopenharmony_ci|	**bpftool** **map freeze**     *MAP*
4562306a36Sopenharmony_ci|	**bpftool** **map help**
4662306a36Sopenharmony_ci|
4762306a36Sopenharmony_ci|	*MAP* := { **id** *MAP_ID* | **pinned** *FILE* | **name** *MAP_NAME* }
4862306a36Sopenharmony_ci|	*DATA* := { [**hex**] *BYTES* }
4962306a36Sopenharmony_ci|	*PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* | **name** *PROG_NAME* }
5062306a36Sopenharmony_ci|	*VALUE* := { *DATA* | *MAP* | *PROG* }
5162306a36Sopenharmony_ci|	*UPDATE_FLAGS* := { **any** | **exist** | **noexist** }
5262306a36Sopenharmony_ci|	*TYPE* := { **hash** | **array** | **prog_array** | **perf_event_array** | **percpu_hash**
5362306a36Sopenharmony_ci|		| **percpu_array** | **stack_trace** | **cgroup_array** | **lru_hash**
5462306a36Sopenharmony_ci|		| **lru_percpu_hash** | **lpm_trie** | **array_of_maps** | **hash_of_maps**
5562306a36Sopenharmony_ci|		| **devmap** | **devmap_hash** | **sockmap** | **cpumap** | **xskmap** | **sockhash**
5662306a36Sopenharmony_ci|		| **cgroup_storage** | **reuseport_sockarray** | **percpu_cgroup_storage**
5762306a36Sopenharmony_ci|		| **queue** | **stack** | **sk_storage** | **struct_ops** | **ringbuf** | **inode_storage**
5862306a36Sopenharmony_ci|		| **task_storage** | **bloom_filter** | **user_ringbuf** | **cgrp_storage** }
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ciDESCRIPTION
6162306a36Sopenharmony_ci===========
6262306a36Sopenharmony_ci	**bpftool map { show | list }**   [*MAP*]
6362306a36Sopenharmony_ci		  Show information about loaded maps.  If *MAP* is specified
6462306a36Sopenharmony_ci		  show information only about given maps, otherwise list all
6562306a36Sopenharmony_ci		  maps currently loaded on the system.  In case of **name**,
6662306a36Sopenharmony_ci		  *MAP* may match several maps which will all be shown.
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci		  Output will start with map ID followed by map type and
6962306a36Sopenharmony_ci		  zero or more named attributes (depending on kernel version).
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci		  Since Linux 5.8 bpftool is able to discover information about
7262306a36Sopenharmony_ci		  processes that hold open file descriptors (FDs) against BPF
7362306a36Sopenharmony_ci		  maps. On such kernels bpftool will automatically emit this
7462306a36Sopenharmony_ci		  information as well.
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	**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*]
7762306a36Sopenharmony_ci		  Create a new map with given parameters and pin it to *bpffs*
7862306a36Sopenharmony_ci		  as *FILE*.
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci		  *FLAGS* should be an integer which is the combination of
8162306a36Sopenharmony_ci		  desired flags, e.g. 1024 for **BPF_F_MMAPABLE** (see bpf.h
8262306a36Sopenharmony_ci		  UAPI header for existing flags).
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci		  To create maps of type array-of-maps or hash-of-maps, the
8562306a36Sopenharmony_ci		  **inner_map** keyword must be used to pass an inner map. The
8662306a36Sopenharmony_ci		  kernel needs it to collect metadata related to the inner maps
8762306a36Sopenharmony_ci		  that the new map will work with.
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci		  Keyword **offload_dev** expects a network interface name,
9062306a36Sopenharmony_ci		  and is used to request hardware offload for the map.
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	**bpftool map dump**    *MAP*
9362306a36Sopenharmony_ci		  Dump all entries in a given *MAP*.  In case of **name**,
9462306a36Sopenharmony_ci		  *MAP* may match several maps which will all be dumped.
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	**bpftool map update**  *MAP* [**key** *DATA*] [**value** *VALUE*] [*UPDATE_FLAGS*]
9762306a36Sopenharmony_ci		  Update map entry for a given *KEY*.
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci		  *UPDATE_FLAGS* can be one of: **any** update existing entry
10062306a36Sopenharmony_ci		  or add if doesn't exit; **exist** update only if entry already
10162306a36Sopenharmony_ci		  exists; **noexist** update only if entry doesn't exist.
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci		  If the **hex** keyword is provided in front of the bytes
10462306a36Sopenharmony_ci		  sequence, the bytes are parsed as hexadecimal values, even if
10562306a36Sopenharmony_ci		  no "0x" prefix is added. If the keyword is not provided, then
10662306a36Sopenharmony_ci		  the bytes are parsed as decimal values, unless a "0x" prefix
10762306a36Sopenharmony_ci		  (for hexadecimal) or a "0" prefix (for octal) is provided.
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	**bpftool map lookup**  *MAP* [**key** *DATA*]
11062306a36Sopenharmony_ci		  Lookup **key** in the map.
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	**bpftool map getnext** *MAP* [**key** *DATA*]
11362306a36Sopenharmony_ci		  Get next key.  If *key* is not specified, get first key.
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	**bpftool map delete**  *MAP*  **key** *DATA*
11662306a36Sopenharmony_ci		  Remove entry from the map.
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci	**bpftool map pin**     *MAP*  *FILE*
11962306a36Sopenharmony_ci		  Pin map *MAP* as *FILE*.
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci		  Note: *FILE* must be located in *bpffs* mount. It must not
12262306a36Sopenharmony_ci		  contain a dot character ('.'), which is reserved for future
12362306a36Sopenharmony_ci		  extensions of *bpffs*.
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	**bpftool** **map event_pipe** *MAP* [**cpu** *N* **index** *M*]
12662306a36Sopenharmony_ci		  Read events from a **BPF_MAP_TYPE_PERF_EVENT_ARRAY** map.
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci		  Install perf rings into a perf event array map and dump
12962306a36Sopenharmony_ci		  output of any **bpf_perf_event_output**\ () call in the kernel.
13062306a36Sopenharmony_ci		  By default read the number of CPUs on the system and
13162306a36Sopenharmony_ci		  install perf ring for each CPU in the corresponding index
13262306a36Sopenharmony_ci		  in the array.
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci		  If **cpu** and **index** are specified, install perf ring
13562306a36Sopenharmony_ci		  for given **cpu** at **index** in the array (single ring).
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci		  Note that installing a perf ring into an array will silently
13862306a36Sopenharmony_ci		  replace any existing ring.  Any other application will stop
13962306a36Sopenharmony_ci		  receiving events if it installed its rings earlier.
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	**bpftool map peek**  *MAP*
14262306a36Sopenharmony_ci		  Peek next value in the queue or stack.
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	**bpftool map push**  *MAP* **value** *VALUE*
14562306a36Sopenharmony_ci		  Push *VALUE* onto the stack.
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	**bpftool map pop**  *MAP*
14862306a36Sopenharmony_ci		  Pop and print value from the stack.
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	**bpftool map enqueue**  *MAP* **value** *VALUE*
15162306a36Sopenharmony_ci		  Enqueue *VALUE* into the queue.
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	**bpftool map dequeue**  *MAP*
15462306a36Sopenharmony_ci		  Dequeue and print value from the queue.
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	**bpftool map freeze**  *MAP*
15762306a36Sopenharmony_ci		  Freeze the map as read-only from user space. Entries from a
15862306a36Sopenharmony_ci		  frozen map can not longer be updated or deleted with the
15962306a36Sopenharmony_ci		  **bpf**\ () system call. This operation is not reversible,
16062306a36Sopenharmony_ci		  and the map remains immutable from user space until its
16162306a36Sopenharmony_ci		  destruction. However, read and write permissions for BPF
16262306a36Sopenharmony_ci		  programs to the map remain unchanged.
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	**bpftool map help**
16562306a36Sopenharmony_ci		  Print short help message.
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ciOPTIONS
16862306a36Sopenharmony_ci=======
16962306a36Sopenharmony_ci	.. include:: common_options.rst
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	-f, --bpffs
17262306a36Sopenharmony_ci		  Show file names of pinned maps.
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	-n, --nomount
17562306a36Sopenharmony_ci		  Do not automatically attempt to mount any virtual file system
17662306a36Sopenharmony_ci		  (such as tracefs or BPF virtual file system) when necessary.
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ciEXAMPLES
17962306a36Sopenharmony_ci========
18062306a36Sopenharmony_ci**# bpftool map show**
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci::
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci  10: hash  name some_map  flags 0x0
18562306a36Sopenharmony_ci        key 4B  value 8B  max_entries 2048  memlock 167936B
18662306a36Sopenharmony_ci        pids systemd(1)
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ciThe following three commands are equivalent:
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci|
19162306a36Sopenharmony_ci| **# bpftool map update id 10 key hex   20   c4   b7   00 value hex   0f   ff   ff   ab   01   02   03   4c**
19262306a36Sopenharmony_ci| **# bpftool map update id 10 key     0x20 0xc4 0xb7 0x00 value     0x0f 0xff 0xff 0xab 0x01 0x02 0x03 0x4c**
19362306a36Sopenharmony_ci| **# bpftool map update id 10 key       32  196  183    0 value       15  255  255  171    1    2    3   76**
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci**# bpftool map lookup id 10 key 0 1 2 3**
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci::
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci  key: 00 01 02 03 value: 00 01 02 03 04 05 06 07
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci**# bpftool map dump id 10**
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci::
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci  key: 00 01 02 03  value: 00 01 02 03 04 05 06 07
20762306a36Sopenharmony_ci  key: 0d 00 07 00  value: 02 00 00 00 01 02 03 04
20862306a36Sopenharmony_ci  Found 2 elements
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci**# bpftool map getnext id 10 key 0 1 2 3**
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci::
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci  key:
21562306a36Sopenharmony_ci  00 01 02 03
21662306a36Sopenharmony_ci  next key:
21762306a36Sopenharmony_ci  0d 00 07 00
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci|
22062306a36Sopenharmony_ci| **# mount -t bpf none /sys/fs/bpf/**
22162306a36Sopenharmony_ci| **# bpftool map pin id 10 /sys/fs/bpf/map**
22262306a36Sopenharmony_ci| **# bpftool map del pinned /sys/fs/bpf/map key 13 00 07 00**
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ciNote that map update can also be used in order to change the program references
22562306a36Sopenharmony_cihold by a program array map. This can be used, for example, to change the
22662306a36Sopenharmony_ciprograms used for tail-call jumps at runtime, without having to reload the
22762306a36Sopenharmony_cientry-point program. Below is an example for this use case: we load a program
22862306a36Sopenharmony_cidefining a prog array map, and with a main function that contains a tail call
22962306a36Sopenharmony_cito other programs that can be used either to "process" packets or to "debug"
23062306a36Sopenharmony_ciprocessing. Note that the prog array map MUST be pinned into the BPF virtual
23162306a36Sopenharmony_cifile system for the map update to work successfully, as kernel flushes prog
23262306a36Sopenharmony_ciarray maps when they have no more references from user space (and the update
23362306a36Sopenharmony_ciwould be lost as soon as bpftool exits).
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci|
23662306a36Sopenharmony_ci| **# bpftool prog loadall tail_calls.o /sys/fs/bpf/foo type xdp**
23762306a36Sopenharmony_ci| **# bpftool prog --bpffs**
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci::
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci  545: xdp  name main_func  tag 674b4b5597193dc3  gpl
24262306a36Sopenharmony_ci          loaded_at 2018-12-12T15:02:58+0000  uid 0
24362306a36Sopenharmony_ci          xlated 240B  jited 257B  memlock 4096B  map_ids 294
24462306a36Sopenharmony_ci          pinned /sys/fs/bpf/foo/xdp
24562306a36Sopenharmony_ci  546: xdp  name bpf_func_process  tag e369a529024751fc  gpl
24662306a36Sopenharmony_ci          loaded_at 2018-12-12T15:02:58+0000  uid 0
24762306a36Sopenharmony_ci          xlated 200B  jited 164B  memlock 4096B
24862306a36Sopenharmony_ci          pinned /sys/fs/bpf/foo/process
24962306a36Sopenharmony_ci  547: xdp  name bpf_func_debug  tag 0b597868bc7f0976  gpl
25062306a36Sopenharmony_ci          loaded_at 2018-12-12T15:02:58+0000  uid 0
25162306a36Sopenharmony_ci          xlated 200B  jited 164B  memlock 4096B
25262306a36Sopenharmony_ci          pinned /sys/fs/bpf/foo/debug
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci**# bpftool map**
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci::
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci  294: prog_array  name jmp_table  flags 0x0
25962306a36Sopenharmony_ci          key 4B  value 4B  max_entries 1  memlock 4096B
26062306a36Sopenharmony_ci          owner_prog_type xdp  owner jited
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci|
26362306a36Sopenharmony_ci| **# bpftool map pin id 294 /sys/fs/bpf/bar**
26462306a36Sopenharmony_ci| **# bpftool map dump pinned /sys/fs/bpf/bar**
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci::
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci  Found 0 elements
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci|
27162306a36Sopenharmony_ci| **# bpftool map update pinned /sys/fs/bpf/bar key 0 0 0 0 value pinned /sys/fs/bpf/foo/debug**
27262306a36Sopenharmony_ci| **# bpftool map dump pinned /sys/fs/bpf/bar**
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci::
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci  key: 00 00 00 00  value: 22 02 00 00
27762306a36Sopenharmony_ci  Found 1 element
278