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