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

Managed Atomic eBPF (`maeBPF`): zero-clang pure-Elixir eBPF compilation,
compile-time fused `MaeBPF.Plug` pipelines, state-preserving atomic
`BPF_LINK_UPDATE` hot-swapping, and zero-polling `enif_select` ring buffer
streaming for Elixir/BEAM.

Use `use MaeBPF` inside an Elixir module to declare shared maps and hook
blocks (`xdp`, `tcx`, `trace`) that AOT-compile into a single little-endian
`bpfel` ELF64 object embedded directly into `.beam` on both macOS and Linux.

Top-level public facade delegating to:
  * `MaeBPF.DSL` - unified `use MaeBPF` macro DSL and AOT ELF embedding
  * `MaeBPF.IR` - typed multi-program IR, static safety checker, and header analysis
  * `MaeBPF.Plug` - compile-time fused eBPF plug behaviour and built-in `MaeBPF.Plugs.*`
  * `MaeBPF.Compiler.Pure` - zero-`clang` pure-Elixir eBPF ISA v3 + ELF64 + BTF compiler
  * `MaeBPF.Sim` & `MaeBPF.Packet` - cross-platform macOS/Linux IR simulator and packet builders
  * `MaeBPF.Test` & `MaeBPF.TestRun` - dual-mode simulator + Linux `BPF_PROG_TEST_RUN` harness
  * `MaeBPF.Schema` & `MaeBPF.Migration` - packed binary codecs and live map schema migration
  * `MaeBPF.Probe` - Linux kernel version, BTF/JIT, privilege, and feature probing
  * `MaeBPF.Disasm` & `MaeBPF.Diagnostics` - eBPF ISA disassembler and source-mapped verifier errors
  * `MaeBPF.Server` - OTP GenServer managing object, links, ringbuf, debug tracing, and hot-swap
  * `MaeBPF.Map` - kernel BPF map operations, per-CPU maps, LPM tries, pinning, and FD reuse
  * `MaeBPF.Link` - XDP, TCX, and tracepoint `bpf_link` attachment, pinning, and updates
  * `MaeBPF.RingBuf` - zero-polling bounded and packed `enif_select` ring buffer consumer
  * `MaeBPF.Telemetry` - `bpftool`-parity runtime stats, bytecode dump, and kernel ID enumeration

When the optional `:maebpf_c` companion package is installed, `compile_c/2`,
`compile_c_timed/2`, `ensure_vmlinux_h/1`, and `{:c, source}` /
`{:c_source, source}` inputs automatically dispatch to `MaeBPF.Compiler.C`.

# `attach`

Starts a linked `MaeBPF.Supervisor` supervising a list of policy child specs.

Accepts standard OTP child specs (`{MyPolicy, target: ...}`) as well as
concise target shorthands (`{MyPolicy, {:xdp, "eth0"}}`,
`{MyPolicy, {:cgroup, "/sys/fs/cgroup/app"}}`, `{MyPolicy, {:systemd, "app.service"}}`).

Call `MaeBPF.detach/1` on the returned supervisor PID to stop all children
and automatically detach every unpinned kernel link.

# `attach`

Attaches either:
  * A list of supervised policy children with supervisor options
    (`MaeBPF.attach(children, strategy: :one_for_one, name: MySup)`)
  * A single policy/source to one target, a list of targets (starts a
    `MaeBPF.Supervisor` running one worker per target), or a keyword/map of
    named hooks for a multi-hook module (starts one `MaeBPF.Server`)

# `attach`

Compiles (if needed), loads, and attaches an eBPF policy or program.

`source` may be a `use MaeBPF` module, a `%MaeBPF.IR.Module{}`, a compiled
ELF binary, `{:elf, bin}`, `{:pure_spec, spec}`, `{:c, src}` /
`{:c_source, src}` (requires optional `:maebpf_c`), or a keyword option list.

`attach_spec` may be:
  * `:none`
  * `{:xdp, ifname_or_index}` or `{:xdp, ifname_or_index, xdp_opts}`
  * `{:tcx, ifname_or_index, :ingress | :egress}` or with `tcx_opts`
  * `{:cgroup, cgroup_path}`
  * `{:systemd, unit_name}` (resolves unified cgroup v2 path)
  * `{:pid, os_pid}` (resolves unified cgroup v2 path)
  * `{:tracepoint, category, name}` or `{:tracepoint, category, name, opts}`
  * A list of multiple targets (`[{:systemd, "a.service"}, {:systemd, "b.service"}]`),
    starting a linked `MaeBPF.Supervisor` with one isolated worker per target
  * A keyword list or map of named hooks for one multi-hook module
    (`[ingress: {:xdp, "veth0"}, egress: {:cgroup, "/sys/fs/cgroup/app"}]`),
    starting one `MaeBPF.Server` sharing a single kernel object and maps

# `close`

Explicitly closes an eBPF object resource.

# `compile_c`

Compiles C eBPF source via the optional `:maebpf_c` companion package.

Returns `{:error, {:missing_optional_package, :maebpf_c, ...}}` when
`:maebpf_c` is not installed.

# `compile_c_timed`

Compiles C eBPF source and returns elapsed microseconds via the optional
`:maebpf_c` companion package.

Returns `{:error, {:missing_optional_package, :maebpf_c, ...}}` when
`:maebpf_c` is not installed.

# `compile_ir`

# `compile_pipeline`

Dynamically fuses and compiles a runtime list of `MaeBPF.Plug` specifications
(`PlugMod` or `{PlugMod, opts}`) into an ELF64 binary, validated IR, and
instruction source map in sub-millisecond time with zero `clang`.

Options:
  * `:hook` - `:xdp` (default), `{:tcx, :ingress | :egress}`, or
    `{:tracepoint, "category/name"}`
  * `:name` - program atom name (default `:pipeline`)
  * `:prog_name` - ELF function symbol name
  * `:default` - fallback action (default `:pass`)
  * `:maps` - explicit list of `%MaeBPF.IR.MapDef{}`
  * `:license` - ELF license string (default `"GPL"`)
  * `:debug_trace` - enable per-plug debug RingBuf tracing (default `false`)
  * `:sample` - debug trace sampling interval (default `1`)

# `compile_pure`

# `compile_pure_timed`

# `debug`

# `detach`

Detaches a running policy server or `MaeBPF.Supervisor` cleanly.

Stops the target process (or supervisor tree in reverse child order),
immediately detaching every unpinned kernel `bpf_link`.

# `disable_stats`

Disables kernel `BPF_STATS_RUN_TIME` either for a raw RAII stats guard handle
or dynamically on a running `MaeBPF.Server`.

# `disasm`

# `enable_stats`

Enables kernel `BPF_STATS_RUN_TIME` globally and returns an RAII guard handle.

# `enable_stats`

Dynamically enables kernel `BPF_STATS_RUN_TIME` on a running `MaeBPF.Server`
without reloading the eBPF program.

# `ensure_vmlinux_h`

Ensures a cached kernel `vmlinux.h` header via the optional `:maebpf_c`
companion package.

Returns `{:error, {:missing_optional_package, :maebpf_c, ...}}` when
`:maebpf_c` is not installed.

# `format_verifier_error`

# `get_global`

Reads and decodes a BTF-described global variable from a loaded object or
running `MaeBPF.Server`.

    {:ok, true} = MaeBPF.get_global(srv, :enabled)

# `get_globals`

Reads and decodes all BTF-described global variables from a loaded object or
running `MaeBPF.Server`.

    {:ok, %{"enabled" => true}} = MaeBPF.get_globals(srv)

# `hot_swap`

# `inspect_ir`

Returns a structured inspection summary of a `use MaeBPF` module,
`%MaeBPF.IR.Module{}`, or compiled result map for `IEx` introspection.

# `list_links`

# `list_maps`

# `list_progs`

# `load`

Loads an opened eBPF object into the Linux kernel via `bpf_object__load`.

# `map_delete_domain`

Deletes an exact or wildcard FQDN rule from a reversed-FQDN `LPM_TRIE` map.

    :ok = MaeBPF.map_delete_domain(srv, "blocked_domains", "*.blocked.org")

# `map_delete_prog`

Deletes a program entry from a `BPF_MAP_TYPE_PROG_ARRAY` slot.

    :ok = MaeBPF.map_delete_prog(srv, "jump_table", 0)

# `map_lookup_prog_id`

Reads the kernel program ID stored in a `BPF_MAP_TYPE_PROG_ARRAY` slot.

    {:ok, prog_id} = MaeBPF.map_lookup_prog_id(srv, "jump_table", 0)

# `map_put_domain`

Inserts an exact (`"evil.example.com"`) or wildcard (`"*.blocked.org"`) FQDN
rule into a reversed-FQDN `LPM_TRIE` map.

    :ok = MaeBPF.map_put_domain(srv, "blocked_domains", "*.blocked.org")

# `map_put_prog`

Inserts a program into a `BPF_MAP_TYPE_PROG_ARRAY` slot on an object or
running `MaeBPF.Server`.

    :ok = MaeBPF.map_put_prog(srv, "jump_table", 0, "tail_handler")

# `open_mem`

Opens an eBPF ELF64 binary in memory via `bpf_object__open_mem`.

# `probe`

# `prog_dump`

Dumps kernel `xlated` and `jited` bytecode for a loaded eBPF object and
program name (`MaeBPF.Telemetry.prog_dump/2`), or from a running
`MaeBPF.Server` (`MaeBPF.Server.prog_dump/2`).

# `prog_fd`

Returns the loaded kernel program file descriptor for an object or running
`MaeBPF.Server`.

    {:ok, fd} = MaeBPF.prog_fd(srv, "xdp_main")

# `put_global`

Updates a mutable (`.data` or `.bss`) global variable on a loaded object or
running `MaeBPF.Server`. Rejects `.rodata` writes with
`{:error, {:readonly_global, name, hint}}`.

    :ok = MaeBPF.put_global(srv, :enabled, false)

# `read_iter`

Reads output from an attached BPF iterator link (`iter/`) via
`bpf_iter_create`.

    {:ok, data} = MaeBPF.read_iter(srv, max_bytes: 1_048_576)

# `simulate`

# `start`

# `start_link`

# `status`

# `stop`

# `test_run`

Executes a test packet against either a loaded kernel object (`MaeBPF.TestRun`)
or a `use MaeBPF` module / `%MaeBPF.IR.Module{}` (`MaeBPF.Test`).

# `undebug`

# `unpin_and_stop`

---

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