Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

defmap

defmap declares a BPF map that can be shared between BPF programs and userspace.

Syntax

(defmap name :type TYPE
  [:key-size N] [:value-size N]
  :max-entries N
  [:map-flags FLAGS])

Required arguments are :type and :max-entries. The :key-size and :value-size arguments are required for most map types but omitted for ring buffers. :map-flags is optional and defaults to 0.

Map Types

Whistler supports the following map types:

KeywordBPF Map TypeNotes
:hashBPF_MAP_TYPE_HASHGeneric hash table
:arrayBPF_MAP_TYPE_ARRAYFixed-size array, integer keys
:percpu-hashBPF_MAP_TYPE_PERCPU_HASHPer-CPU hash table
:percpu-arrayBPF_MAP_TYPE_PERCPU_ARRAYPer-CPU array
:ringbufBPF_MAP_TYPE_RINGBUFRing buffer (key/value sizes omitted)
:prog-arrayBPF_MAP_TYPE_PROG_ARRAYArray of program file descriptors for tail calls
:lpm-trieBPF_MAP_TYPE_LPM_TRIELongest-prefix-match trie
:lru-hashBPF_MAP_TYPE_LRU_HASHLRU-evicting hash table

Examples

Hash map

(defmap connection-table :type :hash
  :key-size 16
  :value-size 8
  :max-entries 1024)

Array map

(defmap counters :type :array
  :key-size 4
  :value-size 8
  :max-entries 256)

Per-CPU hash

(defmap per-cpu-cache :type :percpu-hash
  :key-size 4
  :value-size 64
  :max-entries 512)

Per-CPU array

(defmap per-cpu-stats :type :percpu-array
  :key-size 4
  :value-size 32
  :max-entries 16)

Ring buffer

Ring buffer maps only require :type and :max-entries. The :max-entries value must be a power of two and specifies the buffer size in bytes.

(defmap events :type :ringbuf
  :max-entries (* 256 1024))

Program array (for tail calls)

(defmap dispatch :type :prog-array
  :key-size 4
  :value-size 4
  :max-entries 8)

LPM trie

LPM trie maps require the BPF_F_NO_PREALLOC flag (value 1).

(defmap routes :type :lpm-trie
  :key-size 8
  :value-size 4
  :max-entries 1024
  :map-flags 1)

LRU hash

(defmap recent-flows :type :lru-hash
  :key-size 16
  :value-size 8
  :max-entries 4096)