MaeBPF (maebpf v0.1.0)

Copy Markdown View Source

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:

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.

Summary

Functions

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

Attaches either

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

Explicitly closes an eBPF object resource.

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

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

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.

Detaches a running policy server or MaeBPF.Supervisor cleanly.

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

Enables kernel BPF_STATS_RUN_TIME globally and returns an RAII guard handle.

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

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

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

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

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

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

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

Deletes a program entry from a BPF_MAP_TYPE_PROG_ARRAY slot.

Reads the kernel program ID stored in a BPF_MAP_TYPE_PROG_ARRAY slot.

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

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

Opens an eBPF ELF64 binary in memory via bpf_object__open_mem.

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

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

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

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

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

Functions

attach(children)

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(children, sup_opts)

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(source, attach_spec, opts)

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(object)

Explicitly closes an eBPF object resource.

compile_c(c_source, opts \\ [])

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(c_source, opts \\ [])

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(ir_module, opts \\ [])

See MaeBPF.Compiler.Pure.compile_ir/2.

compile_pipeline(plugs, opts \\ [])

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(spec)

See MaeBPF.Compiler.Pure.compile/1.

compile_pure_timed(spec)

See MaeBPF.Compiler.Pure.compile_timed/1.

debug(server, opts \\ [])

See MaeBPF.Server.debug/2.

detach(pid_or_name, reason \\ :normal)

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(guard)

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

disasm(input, opts \\ [])

See MaeBPF.Disasm.disasm/2.

enable_stats()

Enables kernel BPF_STATS_RUN_TIME globally and returns an RAII guard handle.

enable_stats(server)

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

ensure_vmlinux_h(opts \\ [])

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(verifier_log, source_map, opts \\ [])

See MaeBPF.Diagnostics.format_verifier_error/3.

get_global(object, name)

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(object)

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(server, source, opts \\ [])

See MaeBPF.Server.hot_swap/3.

inspect_ir(mod)

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

list_links()

See MaeBPF.Telemetry.list_link_ids/0.

list_maps()

See MaeBPF.Telemetry.list_map_ids/0.

list_progs()

See MaeBPF.Telemetry.list_prog_ids/0.

load(object)

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

map_delete_domain(object, map_name, 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(object, map_name, slot)

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(object, map_name, slot)

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(target, map_name, domain, val \\ <<1::little-32>>, flags \\ :any)

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(object, map_name, slot, prog_ref)

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(elf_bin, opts \\ %{})

Opens an eBPF ELF64 binary in memory via bpf_object__open_mem.

probe()

See MaeBPF.Probe.capabilities/0.

prog_dump(object_or_server, prog_or_hook \\ nil)

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(object_or_server, prog_or_hook \\ nil)

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

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

put_global(object, name, value)

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(link_or_server, opts \\ [])

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(module_or_ir, hook_name, packet_bin, state \\ %{})

See MaeBPF.Sim.run/4.

start(opts)

See MaeBPF.Server.start/1.

start_link(opts)

See MaeBPF.Server.start_link/1.

status(server, timeout \\ 5000)

See MaeBPF.Server.status/2.

stop(server, reason \\ :normal)

See MaeBPF.Server.stop/2.

test_run(target, hook_or_prog, data_in, opts \\ [])

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

undebug(server)

See MaeBPF.Server.undebug/1.

unpin_and_stop(server, reason \\ :normal)

See MaeBPF.Server.unpin_and_stop/2.