MaeBPF.Compiler.Pure (maebpf v0.1.0)

Copy Markdown View Source

Zero-clang pure-Elixir eBPF ISA v3 + ELF64 + .BTF + .BTF.ext compiler.

Emits standards-compliant little-endian ELF64 (EM_BPF, ELFDATA2LSB) relocatable objects containing:

  • One or more executable program sections (xdp, tcx/ingress, tcx/egress, tracepoint/<category>/<name>)
  • license section (without a leading dot, matching libbpf requirements)
  • Optional .maps section and .rel<sec> R_BPF_64_64 relocations
  • Valid .BTF type graph (BTF-defined maps including LPM_TRIE with BPF_F_NO_PREALLOC and RINGBUF without key/value, DATASEC(".maps"), per-program FUNC + FUNC_PROTO) and .BTF.ext func_info records so libbpf 1.5.0 and the Linux kernel load all programs and shared maps cleanly.

Supports both low-level instruction specifications (compile/1, compile_timed/1, xdp_packet_counter/1, xdp_percpu_packet_counter/1, xdp_return/1) and high-level fused %MaeBPF.IR.Module{} compilation (compile_ir/1, compile_ir/2, compile_ir!/1, compile_ir!/2).

Summary

Functions

Compiles a low-level program specification map into a valid ELF64 eBPF binary.

Compiles a %MaeBPF.IR.Module{} (multi-program, multi-map, fused packet parser, static safety checked) into a little-endian bpfel ELF64 object.

Like compile_ir/2, raising ArgumentError on validation or compilation failure.

Compiles a low-level program specification map and returns {:ok, elf_bin, elapsed_us}.

Builds a ready-to-load XDP program in pure Elixir that counts packets in a BTF-defined BPF_MAP_TYPE_ARRAY or BPF_MAP_TYPE_PERCPU_ARRAY map (key: u32, value: u64) and returns XDP_PASS (2) by default.

Convenience wrapper for xdp_packet_counter/1 using a lock-free BPF_MAP_TYPE_PERCPU_ARRAY counter map.

Builds a minimal pure-Elixir XDP program returning action (default XDP_PASS = 2).

Functions

compile(spec)

Compiles a low-level program specification map into a valid ELF64 eBPF binary.

Specification keys:

  • :section - ELF section name (default "xdp")
  • :name - program function name (default "xdp_prog")
  • :license - license string (default "GPL")
  • :maps - list of map specs
  • :instructions - list of eBPF instructions and {:label, atom} markers

compile_ir(ir_module, opts \\ [])

Compiles a %MaeBPF.IR.Module{} (multi-program, multi-map, fused packet parser, static safety checked) into a little-endian bpfel ELF64 object.

Options:

  • :debug_trace - when true, injects "__maebpf_debug_rb" and "__maebpf_rb_drops___maebpf_debug_rb" and emits per-plug trace events (default false)
  • :sample - debug trace sampling interval (>= 1, default 1)
  • :ringbuf_kb - debug RingBuf size in KiB (default 256)
  • :max_stack_bytes - stack limit override (default 512)
  • :max_instructions - instruction budget override (default 4096)
  • :max_branches - branch budget override (default 512)

Returns {:ok, %{elf: bin, ir: ir, source_map: map, insn_counts: map, stack_bytes: map, elapsed_us: int}}.

compile_ir!(ir_module, opts \\ [])

Like compile_ir/2, raising ArgumentError on validation or compilation failure.

compile_timed(spec)

Compiles a low-level program specification map and returns {:ok, elf_bin, elapsed_us}.

xdp_packet_counter(opts \\ [])

Builds a ready-to-load XDP program in pure Elixir that counts packets in a BTF-defined BPF_MAP_TYPE_ARRAY or BPF_MAP_TYPE_PERCPU_ARRAY map (key: u32, value: u64) and returns XDP_PASS (2) by default.

xdp_percpu_packet_counter(opts \\ [])

Convenience wrapper for xdp_packet_counter/1 using a lock-free BPF_MAP_TYPE_PERCPU_ARRAY counter map.

xdp_return(opts \\ [])

Builds a minimal pure-Elixir XDP program returning action (default XDP_PASS = 2).