bpftool-map(8)

πŸ’– NAME πŸ’–

okay so you've found bpftool-map(8) β€” that's the page name, and it lives in the System Manager's Manual, section 8. what even is it? bestie, it's literally the tool for inspection and simple manipulation of eBPF maps. that's the whole vibe. πŸ—ΊοΈβœ¨


πŸŽ€ SYNOPSIS πŸŽ€

bpftool [OPTIONS] map COMMAND

OPTIONS := { { -j | --json } [{ -p | --pretty }] | { -d | --debug } | {
-f | --bpffs } | { -n | --nomount } }

COMMANDS := { show | list | create | dump | update | lookup | getnext |
delete | pin | help }

🌸 MAP COMMANDS 🌸

the actual main characters, in order, no cap:

bpftool map { show | list }   [MAP]
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]
bpftool map dump       MAP
bpftool map update     MAP [key DATA] [value VALUE] [UPDATE_FLAGS]
bpftool map lookup     MAP [key DATA]
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]
bpftool map peek       MAP
bpftool map push       MAP value VALUE
bpftool map pop        MAP
bpftool map enqueue    MAP value VALUE
bpftool map dequeue    MAP
bpftool map freeze     MAP
bpftool map help

MAP := { id MAP_ID | pinned FILE | name MAP_NAME }
DATA := { [hex] BYTES }
PROG := { id PROG_ID | pinned FILE | tag PROG_TAG | name PROG_NAME }
VALUE := { DATA | MAP | PROG }
UPDATE_FLAGS := { any | exist | noexist }
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
  | devmap | devmap_hash | sockmap | cpumap | xskmap | sockhash
  | cgroup_storage | reuseport_sockarray | percpu_cgroup_storage
  | queue | stack | sk_storage | struct_ops | ringbuf | inode_storage
  | task_storage | bloom_filter | user_ringbuf | cgrp_storage | arena
  | insn_array | rhash }

πŸ‘‘ DESCRIPTION πŸ‘‘

here's every single command, explained with love and zero skipped details 🫢

bpftool map { show | list } [MAP]

shows you info about the maps that are loaded. if you pass MAP, it only shows you info about that specific map (or those specific maps); if you don't, it lists all the maps currently loaded on the system. and heads up: if you're matching by name, MAP can match, like, several maps at once β€” and all of them get shown. no gatekeeping πŸ’…

the output starts with the map ID, then the map type, then zero or more named attributes (which ones you get depends on your kernel version).

and since Linux 5.8, bpftool can discover info about the processes that hold open file descriptors (FDs) against BPF maps. on those kernels, bpftool just automatically emits that info too. iconic behaviour honestly.

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]

makes a brand-new map with the parameters you give it, and pins it to bpffs as FILE.

  • FLAGS β€” should be an integer that's the combination of the flags you want, e.g. 1024 for BPF_F_MMAPABLE (go peek at the bpf.h UAPI header for the flags that already exist).
  • inner_map β€” to create maps of type array-of-maps or hash-of-maps, you have to use the inner_map keyword to pass an inner map. the kernel needs it to collect the metadata related to the inner maps that your new map is gonna work with. non-negotiable, the kernel said so.
  • offload_dev β€” this keyword expects a network interface name, and it's used to request hardware offload for the map.

bpftool map dump MAP

dumps all the entries in the given MAP. and if you match by name, MAP can match several maps β€” and every single one of them gets dumped. bring snacks πŸ“

bpftool map update MAP [key DATA] [value VALUE] [UPDATE_FLAGS]

updates the map entry for a given KEY.

UPDATE_FLAGS can be one of these three besties:

  • any β€” update an existing entry, or add it if it doesn't exist.
  • exist β€” update only if the entry already exists.
  • noexist β€” update only if the entry doesn't exist.

about the byte parsing, this is the important lore: if the hex keyword is provided in front of the bytes sequence, the bytes are parsed as hexadecimal values, even if no "0x" prefix is added. if that keyword is not provided, then the bytes are parsed as decimal values β€” unless you provide a "0x" prefix (for hexadecimal) or a "0" prefix (for octal). so yeah, the prefix changes everything. ✨

bpftool map lookup MAP [key DATA]

looks up a key in the map. simple. elegant. does exactly what it says.

bpftool map getnext MAP [key DATA]

gets the next key. and if you don't specify a key, it gets the first key. cute.

bpftool map delete MAP key DATA

removes an entry from the map. bye bestie πŸ‘‹

bpftool map pin MAP FILE

pins map MAP as FILE.

note bestie: FILE must be located in a bpffs mount. and it must not contain a dot character ('.') β€” that's reserved for future extensions of bpffs. so no sneaky dots, okay?

bpftool map event_pipe MAP [cpu N index M]

reads events from a BPF_MAP_TYPE_PERF_EVENT_ARRAY map.

it installs perf rings into a perf event array map and dumps the output of any bpf_perf_event_output() call in the kernel. by default, it reads the number of CPUs on the system and installs a perf ring for each CPU at the corresponding index in the array.

if cpu and index are specified, it installs the perf ring for the given cpu at index in the array (a single ring).

⚠️ note that installing a perf ring into an array will silently replace any existing ring. and any other application will stop receiving events if it installed its rings earlier. silently, bestie. SILENTLY. 😭

bpftool map peek MAP

peeks the next value in the queue or stack. just a peek, no take-backs.

bpftool map push MAP value VALUE

pushes VALUE onto the stack.

bpftool map pop MAP

pops and prints the value from the stack.

bpftool map enqueue MAP value VALUE

enqueues VALUE into the queue.

bpftool map dequeue MAP

dequeues and prints the value from the queue.

bpftool map freeze MAP

freezes the map as read-only from user space. entries from a frozen map can no longer be updated or deleted with the bpf() system call. and this is the dramatic part: this operation is not reversible, and the map remains immutable from user space until its destruction. however β€” plot twist β€” read and write permissions for BPF programs to the map remain unchanged. so it's frozen for you, not for the programs. πŸ’€

bpftool map help

prints a short help message. the little guy of the command family.


πŸ’… OPTIONS πŸ’…

  • -h, --help β€” print a short help message (similar to bpftool help).
  • -V, --version β€” print bpftool's version number (similar to bpftool version), the number of the libbpf version in use, and the optional features that were included when bpftool was compiled. those optional features include linking against LLVM or libbfd to provide the disassembler for JIT-ted programs (bpftool prog dump jited), and usage of BPF skeletons (some features like bpftool prog profile, or showing pids associated to BPF objects, may rely on it).
  • -j, --json β€” generate JSON output. for commands that can't produce JSON, this option has no effect. shrug.
  • -p, --pretty β€” generate human-readable JSON output. implies -j.
  • -d, --debug β€” print all logs available, even debug-level info. this includes logs from libbpf as well as from the verifier, when attempting to load programs. everything. all of it. the whole lore dump.
  • -f, --bpffs β€” show file names of pinned maps.
  • -n, --nomount β€” do not automatically attempt to mount any virtual file system (such as tracefs or BPF virtual file system) when necessary.

πŸ”₯ EXAMPLES πŸ”₯

starting strong with a classic:

# bpftool map show

   10: hash  name some_map  flags 0x0
         key 4B  value 8B  max_entries 2048  memlock 167936B
         pids systemd(1)

the following three commands are equivalent, and yes that's the whole point of this bit β€” same bytes, three different outfits πŸ’–

# 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
# 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

okay so here's a whole saga 🧚 β€” note that map update can also be used in order to change the program references held 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 the 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). so pin it. pin it pin it pin it.

# 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

and that's the glow-up arc of a map going from empty to having exactly one element in it. we love to see it 🌈


πŸ¦‹ SEE ALSO πŸ¦‹

the whole friend group, go read them too:

bpf(2), bpf-helpers(7), bpftool(8), bpftool-btf(8), bpftool-cgroup(8), bpftool-feature(8), bpftool-gen(8), bpftool-iter(8), bpftool-link(8), bpftool-net(8), bpftool-perf(8), bpftool-prog(8), bpftool-struct_ops(8), bpftool-token(8)


that's the whole page bestie, no notes πŸ’œ this was bpftool-map(8) from the System Manager's Manual, section 8, and the footer says the same thing β€” bpftool-map(8) β€” so now you know exactly which page you're on and who to blame. go forth and inspect some maps, i'm obsessed with you 🫢✨

πŸ“¦ where this page comes from