# `MaeBPF.Map`
[🔗](https://github.com/cocoa-xu/MaeBPF/blob/v0.1.0/maebpf/lib/maebpf/map.ex#L1)

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.

# `cidr_input`

```elixir
@type cidr_input() :: binary() | {ipv4_tuple(), 0..32} | {binary(), 0..32}
```

# `ipv4_tuple`

```elixir
@type ipv4_tuple() :: {0..255, 0..255, 0..255, 0..255}
```

# `update_flag`

```elixir
@type update_flag() :: non_neg_integer() | :any | :noexist | :exist
```

# `close`

Explicitly closes a pinned map handle's file descriptor without unpinning it
from bpffs.

# `compute_fill_ratio`

```elixir
@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`.

# `count_entries`

Counts the currently populated entries in a pinned map handle.

# `count_entries`

Counts the currently populated entries in a loaded map via `dump/2`.

# `decode_lpm_domain_key`

```elixir
@spec decode_lpm_domain_key(term()) ::
  {:ok, String.t()} | {:error, :invalid_lpm_domain_key}
```

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)

# `decode_lpm_ipv4_key`

```elixir
@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}}`.

# `delete`

Deletes an entry by binary key on a pinned map handle.

# `delete`

Deletes a map entry by its binary key on a loaded object.

# `delete_cidr`

Deletes a CIDR prefix from an IPv4 `BPF_MAP_TYPE_LPM_TRIE` map.

# `delete_domain`

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")

# `delete_prog`

Removes a program from a pinned `BPF_MAP_TYPE_PROG_ARRAY` slot.

# `delete_prog`

Removes a program from a `BPF_MAP_TYPE_PROG_ARRAY` slot on `object`.

# `dump`

Dumps entries from a pinned map handle via `bpf_map_lookup_batch`.

# `dump`

Dumps entries from a loaded map (`dump(object, map_name)`) or from a pinned
map handle with options (`dump(pinned_map, opts)`).

# `dump`

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 (default `256`, `1..65_536`)
  * `:max_entries` - explicit entry cap (defaults to `info/2` `max_entries`)

# `encode_lpm_domain_key`

```elixir
@spec encode_lpm_domain_key(term()) :: {:ok, &lt;&lt;_::2080&gt;&gt;} | {:error, term()}
```

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")

# `fd`

Returns the kernel file descriptor for a pinned map handle.

# `fd`

Returns the kernel file descriptor for a loaded map.

# `fill_ratio`

Computes `{:ok, %{entries: count, max_entries: max, fill_ratio: float}}` for
a pinned map handle.

# `fill_ratio`

Computes `{:ok, %{entries: count, max_entries: max, fill_ratio: float}}` for
a loaded map by combining `info/2` (`max_entries`) with `dump/3`.

# `format_lpm_ipv4_key`

```elixir
@spec format_lpm_ipv4_key(term()) :: {:ok, binary()} | {:error, :invalid_lpm_ipv4_key}
```

Formats an 8-byte Linux `BPF_MAP_TYPE_LPM_TRIE` IPv4 key binary into a CIDR
string such as `"10.0.0.0/8"`.

# `info`

Returns kernel metadata for a pinned map handle.

# `info`

Returns kernel metadata (`id`, `type`, `type_id`, `key_size`, `value_size`,
`max_entries`, `name`) for a loaded map.

# `initial_value`

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")

# `lookup`

Looks up an entry by binary key on a pinned map handle.

# `lookup`

Looks up a map entry by its binary key on a loaded object.

# `lookup_batch`

Reads up to `max_entries` entries using `bpf_map_lookup_batch` in chunks of
`batch_size`.

# `lookup_cidr`

Looks up a CIDR or bare IPv4 address (interpreted as `/32`) in an IPv4
`BPF_MAP_TYPE_LPM_TRIE` map.

# `lookup_percpu`

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`.

# `lookup_percpu`

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`.

# `lookup_percpu_sum_u64`

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`.

# `lookup_percpu_sum_u64`

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`.

# `lookup_prog_id`

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).

# `lookup_prog_id`

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)

# `lpm_domain_key`

```elixir
@spec lpm_domain_key(String.t()) :: &lt;&lt;_::2080&gt;&gt;
```

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.

# `lpm_ipv4_key`

```elixir
@spec lpm_ipv4_key(cidr_input()) :: &lt;&lt;_::64&gt;&gt;
```

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.

# `lpm_ipv4_key`

```elixir
@spec lpm_ipv4_key(ipv4_tuple() | binary(), 0..32) :: &lt;&lt;_::64&gt;&gt;
```

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`.

# `num_possible_cpus`

Returns `{:ok, count}` with the number of possible CPUs reported by
`libbpf_num_possible_cpus()`.

# `open_pinned`

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`.

# `parse_cidr`

```elixir
@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}`.

# `pin`

Pins a loaded map to a bpffs path under `/sys/fs/bpf/` via `bpf_map__pin`.

# `put_cidr`

Inserts or updates a CIDR entry in an IPv4 `BPF_MAP_TYPE_LPM_TRIE` map.

# `put_domain`

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")

# `put_prog`

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"})

# `put_prog`

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"})

# `reuse_fd`

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.

# `set_initial_value`

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)

# `unpin`

Unpins a loaded map from a bpffs path via `bpf_map__unpin`.

# `update`

Updates an entry on a pinned map handle with default flags (`:any`).

# `update`

Updates an entry on a pinned map handle with explicit `flags`, or updates a
map on a loaded object with default flags (`:any`).

# `update`

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`.

# `update_percpu`

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.

# `update_percpu`

Updates a per-CPU entry on a pinned map handle with explicit `flags`, or on a
loaded object with default flags (`:any`).

# `update_percpu`

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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
