MaeBPF.Test (maebpf v0.1.0)

Copy Markdown View Source

Dual-mode testing harness comparing pure-Elixir IR simulation (MaeBPF.Sim) against live Linux kernel BPF_PROG_TEST_RUN (MaeBPF.TestRun).

Modes:

  • :sim - executes the IR in pure Elixir on both macOS and Linux
  • :kernel - loads the compiled ELF into the Linux kernel, populates any initial :maps, executes BPF_PROG_TEST_RUN, reads back updated maps, and closes the object cleanly
  • :both - executes both :sim and :kernel on the same packet and reports action parity (parity: sim.action == kernel.action)
  • :auto (default) - runs :sim on macOS, and on Linux runs :both when verify_kernel: true or :sim otherwise

Summary

Functions

Decodes a raw kernel BPF_PROG_TEST_RUN return value according to hook.

Resolves the effective execution mode (:sim, :kernel, or :both) given options and the current host OS.

Executes hook_name of module_or_ir against packet_bin in :auto, :sim, :kernel, or :both mode.

Like run/4, unwrapping {:ok, result} and raising on execution error or simulator/kernel parity mismatch in :both mode.

Types

mode()

@type mode() :: :auto | :sim | :kernel | :both

Functions

decode_hook_action(arg1, retval)

@spec decode_hook_action(MaeBPF.IR.Program.hook(), non_neg_integer()) ::
  atom() | {:raw, non_neg_integer()}

Decodes a raw kernel BPF_PROG_TEST_RUN return value according to hook.

resolve_mode(opts, os_type \\ :os.type())

@spec resolve_mode(keyword(), tuple()) ::
  {:ok, :sim | :kernel | :both} | {:error, term()}

Resolves the effective execution mode (:sim, :kernel, or :both) given options and the current host OS.

run(module_or_ir, hook_name, packet_bin, opts \\ [])

@spec run(
  module() | MaeBPF.IR.Module.t() | map(),
  atom() | String.t(),
  binary(),
  keyword() | map()
) ::
  {:ok, map()} | {:error, term()}

Executes hook_name of module_or_ir against packet_bin in :auto, :sim, :kernel, or :both mode.

Options:

  • :mode - :auto (default), :sim, :kernel, or :both
  • :verify_kernel - when true and :mode is :auto on Linux, runs :both (default false)
  • :maps - initial map entries (%{map_id_or_name => ...}) populated for both simulator and kernel runs
  • :repeat - BPF_PROG_TEST_RUN repetition count (default 1)
  • :data_out_size - requested output packet buffer size for kernel runs
  • :cpu - optional target CPU index for kernel runs
  • :assert_parity - when true in :both mode, returns {:error, {:parity_mismatch, ...}} if simulator and kernel actions differ (default false)
  • Simulator state options (:debug_trace, :debug_sample, :sample, :ringbuf, :ringbuf_capacity, :drops, :sample_seq) are forwarded to MaeBPF.Sim.run/4.

run!(module_or_ir, hook_name, packet_bin, opts \\ [])

@spec run!(
  module() | MaeBPF.IR.Module.t() | map(),
  atom() | String.t(),
  binary(),
  keyword() | map()
) ::
  map()

Like run/4, unwrapping {:ok, result} and raising on execution error or simulator/kernel parity mismatch in :both mode.