Map type BPF_MAP_TYPE_RHASH
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_sizeandvalue_sizemust 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_entriesmust be between 1 and2^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_extrais an initial element count hint used to size the bucket table. Zero selects therhashtable default. The hint must not exceed eithermax_entriesor 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:
BPF_MAP_LOOKUP_ELEMBPF_MAP_LOOKUP_AND_DELETE_ELEMBPF_MAP_UPDATE_ELEMBPF_MAP_DELETE_ELEMBPF_MAP_GET_NEXT_KEYBPF_MAP_LOOKUP_BATCHBPF_MAP_LOOKUP_AND_DELETE_BATCHBPF_MAP_UPDATE_BATCHBPF_MAP_DELETE_BATCH
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_NO_PREALLOCis mandatory. Entries are allocated on demand.BPF_F_RDONLYandBPF_F_WRONLYrestrict syscall access.BPF_F_RDONLY_PROGandBPF_F_WRONLY_PROGrestrict BPF program access.BPF_F_NUMA_NODEis accepted, but theRHASHallocator does not use the requested node for its table or element allocations.
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");