Operations on eBPF maps belonging to a loaded object or opened from bpffs
via open_pinned/1, including native per-CPU map lookups and updates.
Summary
Functions
Explicitly closes a pinned map handle's file descriptor without unpinning it from bpffs.
Computes a fill-ratio summary map from an entry count and max_entries.
Counts the currently populated entries in a pinned map handle.
Counts the currently populated entries in a loaded map via dump/2.
Decodes a 260-byte reversed-FQDN LPM_TRIE key back to its canonical
lowercase domain ("evil.example.com") or wildcard ("*.blocked.org").
Decodes an 8-byte Linux BPF_MAP_TYPE_LPM_TRIE IPv4 key binary into
{:ok, {{a, b, c, d}, prefix_len}}.
Deletes an entry by binary key on a pinned map handle.
Deletes a map entry by its binary key on a loaded object.
Deletes a CIDR prefix from an IPv4 BPF_MAP_TYPE_LPM_TRIE map.
Deletes an exact or wildcard domain entry from a reversed-FQDN
BPF_MAP_TYPE_LPM_TRIE map.
Removes a program from a pinned BPF_MAP_TYPE_PROG_ARRAY slot.
Removes a program from a BPF_MAP_TYPE_PROG_ARRAY slot on object.
Dumps entries from a pinned map handle via bpf_map_lookup_batch.
Dumps entries from a loaded map (dump(object, map_name)) or from a pinned
map handle with options (dump(pinned_map, opts)).
Dumps all {key, value} entries from a loaded map by paginating through
lookup_batch, respecting max_entries from info/2 unless overridden via
opts.
Validates, lowercases, and encodes an exact or wildcard ("*.suffix") FQDN
into a 260-byte reversed-FQDN LPM_TRIE key binary.
Returns the kernel file descriptor for a pinned map handle.
Returns the kernel file descriptor for a loaded map.
Computes {:ok, %{entries: count, max_entries: max, fill_ratio: float}} for
a pinned map handle.
Formats an 8-byte Linux BPF_MAP_TYPE_LPM_TRIE IPv4 key binary into a CIDR
string such as "10.0.0.0/8".
Returns kernel metadata for a pinned map handle.
Returns kernel metadata (id, type, type_id, key_size, value_size,
max_entries, name) for a loaded map.
Reads initial bytes of an internal map (.rodata, .data, .bss) before or
after load via bpf_map__initial_value.
Looks up an entry by binary key on a pinned map handle.
Looks up a map entry by its binary key on a loaded object.
Reads up to max_entries entries using bpf_map_lookup_batch in chunks of
batch_size.
Looks up a CIDR or bare IPv4 address (interpreted as /32) in an IPv4
BPF_MAP_TYPE_LPM_TRIE map.
Looks up a per-CPU entry on a pinned map handle, returning
{:ok, [cpu0_bin, cpu1_bin, ...]} (trimmed to value_size per CPU) or
:not_found.
Looks up a per-CPU entry (PERCPU_ARRAY, PERCPU_HASH, LRU_PERCPU_HASH,
or PERCPU_CGROUP_STORAGE) on a loaded object, returning
{:ok, [cpu0_bin, cpu1_bin, ...]} (trimmed to value_size per CPU) or
:not_found.
Sums a per-CPU u64 value directly in C across all possible CPUs on a pinned
map handle, returning {:ok, total_u64} or :not_found.
Sums a per-CPU u64 value directly in C across all possible CPUs without
allocating per-CPU Erlang binaries, returning {:ok, total_u64} or
:not_found.
Reads the kernel program ID stored in a pinned BPF_MAP_TYPE_PROG_ARRAY slot.
Reads the kernel program ID stored in a BPF_MAP_TYPE_PROG_ARRAY slot on
object.
Encodes an exact domain ("evil.example.com") or wildcard domain
("*.blocked.org") into a 260-byte reversed-FQDN BPF_MAP_TYPE_LPM_TRIE
key (<<prefixlen::32-little, data::binary-size(256)>>).
Encodes an IPv4 CIDR string (e.g. "10.0.0.0/8") or {ip, prefix_len} tuple
into a Linux BPF_MAP_TYPE_LPM_TRIE 8-byte key binary
(<<prefix_len::32-little, a, b, c, d>>), masking host bits cleanly.
Encodes an IPv4 tuple (e.g. {10, 0, 0, 0}) or dotted-quad IPv4 string and a
prefix_len (0..32) into a Linux BPF_MAP_TYPE_LPM_TRIE key binary,
masking host bits cleanly.
Returns {:ok, count} with the number of possible CPUs reported by
libbpf_num_possible_cpus().
Opens a pinned BPF map from bpffs via bpf_obj_get and returns an RAII
pinned map handle usable with lookup/2, update/3..4, delete/2,
lookup_percpu/2, lookup_percpu_sum_u64/2, update_percpu/3..4, fd/1,
info/1, reuse_fd/3, and MaeBPF.RingBuf.open/1.
Strictly parses an IPv4 CIDR string into {:ok, {{a, b, c, d}, prefix_len}}
with host bits masked to zero, or returns {:error, :invalid_cidr}.
Pins a loaded map to a bpffs path under /sys/fs/bpf/ via bpf_map__pin.
Inserts or updates a CIDR entry in an IPv4 BPF_MAP_TYPE_LPM_TRIE map.
Inserts or updates an exact or wildcard domain entry in a reversed-FQDN
BPF_MAP_TYPE_LPM_TRIE map (type: :lpm_domain).
Inserts a program into a pinned BPF_MAP_TYPE_PROG_ARRAY tail-call slot.
Inserts a program into a BPF_MAP_TYPE_PROG_ARRAY tail-call slot on object.
Reuses an existing map file descriptor on an opened/unloaded object prior to
bpf_object__load, preserving live kernel map state across hot-swaps or
restarts.
Overwrites initial bytes of an internal map (.rodata, .data, .bss)
prior to bpf_object__load via bpf_map__set_initial_value.
Unpins a loaded map from a bpffs path via bpf_map__unpin.
Updates an entry on a pinned map handle with default flags (:any).
Updates an entry on a pinned map handle with explicit flags, or updates a
map on a loaded object with default flags (:any).
Updates a map entry on a loaded object with key_bin, val_bin, and flags.
Updates a per-CPU entry on a pinned map handle with default flags (:any).
Updates a per-CPU entry on a pinned map handle with explicit flags, or on a
loaded object with default flags (:any).
Updates a per-CPU entry on a loaded object with explicit flags.
Types
@type cidr_input() :: binary() | {ipv4_tuple(), 0..32} | {binary(), 0..32}
@type ipv4_tuple() :: {0..255, 0..255, 0..255, 0..255}
@type update_flag() :: non_neg_integer() | :any | :noexist | :exist
Functions
Explicitly closes a pinned map handle's file descriptor without unpinning it from bpffs.
@spec compute_fill_ratio(non_neg_integer(), non_neg_integer()) :: %{ entries: non_neg_integer(), max_entries: non_neg_integer(), fill_ratio: float() }
Computes a fill-ratio summary map from an entry count and max_entries.
Counts the currently populated entries in a pinned map handle.
Counts the currently populated entries in a loaded map via dump/2.
Decodes a 260-byte reversed-FQDN LPM_TRIE key back to its canonical
lowercase domain ("evil.example.com") or wildcard ("*.blocked.org").
{:ok, "*.blocked.org"} = MaeBPF.Map.decode_lpm_domain_key(wild_key)
@spec decode_lpm_ipv4_key(term()) :: {:ok, {ipv4_tuple(), 0..32}} | {:error, :invalid_lpm_ipv4_key}
Decodes an 8-byte Linux BPF_MAP_TYPE_LPM_TRIE IPv4 key binary into
{:ok, {{a, b, c, d}, prefix_len}}.
Deletes an entry by binary key on a pinned map handle.
Deletes a map entry by its binary key on a loaded object.
Deletes a CIDR prefix from an IPv4 BPF_MAP_TYPE_LPM_TRIE map.
Deletes an exact or wildcard domain entry from a reversed-FQDN
BPF_MAP_TYPE_LPM_TRIE map.
:ok = MaeBPF.Map.delete_domain(obj, "blocked_domains", "*.blocked.org")
Removes a program from a pinned BPF_MAP_TYPE_PROG_ARRAY slot.
Removes a program from a BPF_MAP_TYPE_PROG_ARRAY slot on object.
Dumps entries from a pinned map handle via bpf_map_lookup_batch.
Dumps entries from a loaded map (dump(object, map_name)) or from a pinned
map handle with options (dump(pinned_map, opts)).
Dumps all {key, value} entries from a loaded map by paginating through
lookup_batch, respecting max_entries from info/2 unless overridden via
opts.
Options:
:batch_size- entries per kernel batch syscall (default256,1..65_536):max_entries- explicit entry cap (defaults toinfo/2 max_entries)
Validates, lowercases, and encodes an exact or wildcard ("*.suffix") FQDN
into a 260-byte reversed-FQDN LPM_TRIE key binary.
{:ok, key_bin} = MaeBPF.Map.encode_lpm_domain_key("Evil.Example.COM")
{:ok, wild_bin} = MaeBPF.Map.encode_lpm_domain_key("*.blocked.org")
Returns the kernel file descriptor for a pinned map handle.
Returns the kernel file descriptor for a loaded map.
Computes {:ok, %{entries: count, max_entries: max, fill_ratio: float}} for
a pinned map handle.
Computes {:ok, %{entries: count, max_entries: max, fill_ratio: float}} for
a loaded map by combining info/2 (max_entries) with dump/3.
Formats an 8-byte Linux BPF_MAP_TYPE_LPM_TRIE IPv4 key binary into a CIDR
string such as "10.0.0.0/8".
Returns kernel metadata for a pinned map handle.
Returns kernel metadata (id, type, type_id, key_size, value_size,
max_entries, name) for a loaded map.
Reads initial bytes of an internal map (.rodata, .data, .bss) before or
after load via bpf_map__initial_value.
{:ok, bin} = MaeBPF.Map.initial_value(obj, ".rodata")
Looks up an entry by binary key on a pinned map handle.
Looks up a map entry by its binary key on a loaded object.
Reads up to max_entries entries using bpf_map_lookup_batch in chunks of
batch_size.
Looks up a CIDR or bare IPv4 address (interpreted as /32) in an IPv4
BPF_MAP_TYPE_LPM_TRIE map.
Looks up a per-CPU entry on a pinned map handle, returning
{:ok, [cpu0_bin, cpu1_bin, ...]} (trimmed to value_size per CPU) or
:not_found.
Looks up a per-CPU entry (PERCPU_ARRAY, PERCPU_HASH, LRU_PERCPU_HASH,
or PERCPU_CGROUP_STORAGE) on a loaded object, returning
{:ok, [cpu0_bin, cpu1_bin, ...]} (trimmed to value_size per CPU) or
:not_found.
Sums a per-CPU u64 value directly in C across all possible CPUs on a pinned
map handle, returning {:ok, total_u64} or :not_found.
Sums a per-CPU u64 value directly in C across all possible CPUs without
allocating per-CPU Erlang binaries, returning {:ok, total_u64} or
:not_found.
Reads the kernel program ID stored in a pinned BPF_MAP_TYPE_PROG_ARRAY slot.
Note: kernel BPF_MAP_LOOKUP_ELEM on a PROG_ARRAY returns prog->aux->id
(not a file descriptor).
Reads the kernel program ID stored in a BPF_MAP_TYPE_PROG_ARRAY slot on
object.
{:ok, prog_id} = MaeBPF.Map.lookup_prog_id(obj, "jump_table", 0)
@spec lpm_domain_key(String.t()) :: <<_::2080>>
Encodes an exact domain ("evil.example.com") or wildcard domain
("*.blocked.org") into a 260-byte reversed-FQDN BPF_MAP_TYPE_LPM_TRIE
key (<<prefixlen::32-little, data::binary-size(256)>>).
# Exact domain: reversed + NUL, prefixlen = (len + 1) * 8
key = MaeBPF.Map.lpm_domain_key("evil.example.com")
# Wildcard domain: reversed suffix + ".", prefixlen = len * 8
wild = MaeBPF.Map.lpm_domain_key("*.blocked.org")Raises ArgumentError on invalid RFC 1035 domain syntax or length > 253 bytes.
@spec lpm_ipv4_key(cidr_input()) :: <<_::64>>
Encodes an IPv4 CIDR string (e.g. "10.0.0.0/8") or {ip, prefix_len} tuple
into a Linux BPF_MAP_TYPE_LPM_TRIE 8-byte key binary
(<<prefix_len::32-little, a, b, c, d>>), masking host bits cleanly.
Raises ArgumentError on malformed CIDRs or out-of-range prefix lengths.
@spec lpm_ipv4_key(ipv4_tuple() | binary(), 0..32) :: <<_::64>>
Encodes an IPv4 tuple (e.g. {10, 0, 0, 0}) or dotted-quad IPv4 string and a
prefix_len (0..32) into a Linux BPF_MAP_TYPE_LPM_TRIE key binary,
masking host bits cleanly.
Raises ArgumentError on invalid addresses or prefix lengths outside 0..32.
Returns {:ok, count} with the number of possible CPUs reported by
libbpf_num_possible_cpus().
Opens a pinned BPF map from bpffs via bpf_obj_get and returns an RAII
pinned map handle usable with lookup/2, update/3..4, delete/2,
lookup_percpu/2, lookup_percpu_sum_u64/2, update_percpu/3..4, fd/1,
info/1, reuse_fd/3, and MaeBPF.RingBuf.open/1.
@spec parse_cidr(term()) :: {:ok, {ipv4_tuple(), 0..32}} | {:error, :invalid_cidr}
Strictly parses an IPv4 CIDR string into {:ok, {{a, b, c, d}, prefix_len}}
with host bits masked to zero, or returns {:error, :invalid_cidr}.
Pins a loaded map to a bpffs path under /sys/fs/bpf/ via bpf_map__pin.
Inserts or updates a CIDR entry in an IPv4 BPF_MAP_TYPE_LPM_TRIE map.
Inserts or updates an exact or wildcard domain entry in a reversed-FQDN
BPF_MAP_TYPE_LPM_TRIE map (type: :lpm_domain).
:ok = MaeBPF.Map.put_domain(obj, "blocked_domains", "evil.example.com")
:ok = MaeBPF.Map.put_domain(obj, "blocked_domains", "*.blocked.org")
Inserts a program into a pinned BPF_MAP_TYPE_PROG_ARRAY tail-call slot.
prog_ref may be {object, prog_name} or a positive program FD integer.
:ok = MaeBPF.Map.put_prog(pinned_progs, 0, {obj, "tail_handler"})
Inserts a program into a BPF_MAP_TYPE_PROG_ARRAY tail-call slot on object.
prog_ref may be a program name binary in object, {other_obj, prog_name},
or a positive program FD integer.
:ok = MaeBPF.Map.put_prog(obj, "jump_table", 0, "handle_v2")
:ok = MaeBPF.Map.put_prog(obj, "jump_table", 1, {other_obj, "other_prog"})
Reuses an existing map file descriptor on an opened/unloaded object prior to
bpf_object__load, preserving live kernel map state across hot-swaps or
restarts.
source may be a raw FD integer, a {source_object, source_map_name} tuple,
an active MaeBPF.RingBuf handle, or an opened pinned map handle.
Overwrites initial bytes of an internal map (.rodata, .data, .bss)
prior to bpf_object__load via bpf_map__set_initial_value.
:ok = MaeBPF.Map.set_initial_value(obj, ".rodata", patched_bin)
Unpins a loaded map from a bpffs path via bpf_map__unpin.
Updates an entry on a pinned map handle with default flags (:any).
Updates an entry on a pinned map handle with explicit flags, or updates a
map on a loaded object with default flags (:any).
Updates a map entry on a loaded object with key_bin, val_bin, and flags.
flags accepts :any (0), :noexist (1), :exist (2), or a raw
non-negative integer bitmask passed to bpf_map_update_elem.
Updates a per-CPU entry on a pinned map handle with default flags (:any).
values_or_bin may be a list of num_possible_cpus binaries (each of size
value_size) or a single binary broadcast uniformly across all CPUs.
Updates a per-CPU entry on a pinned map handle with explicit flags, or on a
loaded object with default flags (:any).
Updates a per-CPU entry on a loaded object with explicit flags.
values_or_bin may be a list of num_possible_cpus binaries (each of size
value_size) or a single binary broadcast uniformly across all CPUs.