Skip to content

Map type BPF_MAP_TYPE_RHASH

v7.2

BPF_MAP_TYPE_RHASH is a general-purpose hash map whose bucket table grows and shrinks as entries are added and removed. Like BPF_MAP_TYPE_HASH, it stores key/value pairs shared between CPUs, but it uses the kernel's resizable hash table and allocates entries on demand. This can be useful when the number of entries varies substantially over the map's lifetime.

Attributes

  • key_size and value_size must both be nonzero. The key size cannot exceed 65535 bytes; the combined key and value size is also subject to the kernel's allocation limits.
  • max_entries must be between 1 and 2^31, inclusive. It limits the number of entries, not the initial table size. Inserting a new key into a full map fails with -E2BIG; existing keys can still be updated.
  • map_extra is an initial element count hint used to size the bucket table. Zero selects the rhashtable default. The hint must not exceed either max_entries or 65535, and the upper 32 bits must be zero. It is not a reservation or a capacity limit.

If BTF types are supplied, a key type must be specified. Values can contain supported special fields such as BPF spin locks, timers, and kernel pointers; BPF list heads and red-black tree roots are not supported in RHASH values.

Syscall commands

The following syscall commands work with this map type:

Batch lookup and lookup-and-delete use a cursor containing a key; allocate at least key_size bytes for each cursor buffer. If the next cursor key is no longer present, the next batch call returns -EAGAIN; restart with a null in_batch cursor. A final batch can return -ENOENT while still returning entries in count.

Iteration is not a snapshot. Concurrent insertion, deletion, or resizing may cause bpf_for_each_map_elem to miss or revisit entries. Under sustained resizing, iteration may not terminate; a callback can stop it after a bounded number of visits. The map also supports BPF map element iterators.

Helper functions

The following helper functions work with this map type, subject to program type restrictions:

Flags

BPF_F_ZERO_SEED and BPF_F_NO_COMMON_LRU are not supported.

Example

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

struct {
    __uint(type, BPF_MAP_TYPE_RHASH);
    __uint(map_flags, BPF_F_NO_PREALLOC);
    __uint(max_entries, 1024);
    __type(key, __u32);
    __type(value, __u64);
} counts SEC(".maps");

Kernel sources