MaeBPF.IR (maebpf v0.1.0)

Copy Markdown View Source

Typed intermediate representation (IR) and static safety checker for MaeBPF.

A %MaeBPF.IR.Module{} describes a single eBPF ELF object containing one or more programs (:xdp, {:tcx, :ingress | :egress}, {:tracepoint, "category/name"}) sharing module-level BPF maps.

Before lowering to eBPF ISA v3, normalize/1 and validate/1:

  • Auto-register a hidden PERCPU_ARRAY drop counter ("__maebpf_rb_drops_<stream>", key_size: 4, value_size: 8, max_entries: 1) for every declared :ringbuf stream.
  • Enforce the 512-byte kernel eBPF stack frame limit per program.
  • Verify map references, key/value sizes, LPM_TRIE BPF_F_NO_PREALLOC, port ranges, CIDR prefixes, and instruction/branch budgets.
  • Compute minimal shared packet header parsing requirements (:eth, :ipv4, :udp, :tcp) for shared bounds-check fusion.

Summary

Functions

Constructs a %MaeBPF.IR.MapDef{} for an IPv4 or reversed-FQDN domain allowlist (symmetric to blocklist_map/2, tagged with meta.role = :allowlist).

Analyzes a %MaeBPF.IR.Program{} to determine which packet header parsing checks (:eth, :ipv4, :udp, :tcp) are required so the compiler can emit shared bounds checks once at the prologue.

Constructs a %MaeBPF.IR.MapDef{} for an IPv4 (:lru_hash, :hash, :lpm_ipv4) or reversed-FQDN domain (:lpm_domain) blocklist.

Returns the BPF_F_NO_PREALLOC flag constant (1).

Constructs a %MaeBPF.IR.MapDef{} for a counter map (:percpu_array by default).

Returns the canonical hidden debug RingBuf drop counter map name.

Returns the canonical hidden debug RingBuf map name ("__maebpf_debug_rb").

Derives the default ELF function symbol name for a program hook and name.

Returns the hidden drop counter map ID atom for a :ringbuf stream.

Returns the canonical hidden PERCPU_ARRAY drop counter map name for a :ringbuf stream name.

Estimates instruction and branch counts for static budget validation.

Flattens a list of IR ops including nested :scope inner ops.

Formats a single IR validation error term into a human-readable message.

Returns the kernel bpf_map_type integer for a supported IR map kind.

Returns the maximum eBPF stack frame size in bytes (512).

Normalizes a %MaeBPF.IR.Module{}

Returns true when program has tracepoint PID filtering enabled.

Constructs the hidden HASH map definition (4B u32 PID -> 4B u32) used to filter tracepoint invocations to selected OS PIDs.

Returns the canonical hidden tracepoint OS PID filter HASH map name.

Returns the canonical hidden per-CPU sampling sequence counter map name.

Derives the canonical libbpf ELF section name for a program hook.

Constructs the hidden 5-tuple LRU_HASH flow verdict cache (key_size: 12, value_size: 4) used by MaeBPF.Plugs.TLSSNI.

Returns the canonical hidden LRU_HASH 5-tuple flow cache map name used by :tls_sni.

Constructs the hidden PERCPU_ARRAY scratch buffer map (value_size: 2304, max_entries: 1) used by MaeBPF.Plugs.TLSSNI.

Returns the canonical hidden PERCPU_ARRAY scratch map name used by :tls_sni.

Computes exact kernel stack bytes required by program within ir_module.

Constructs a %MaeBPF.IR.MapDef{} for a RingBuf event stream.

Validates a %MaeBPF.IR.Module{} statically.

Like validate/2, returning the normalized %MaeBPF.IR.Module{} or raising ArgumentError with a detailed validation report.

Returns a copy of ir configured for live zero-downtime debug tracing.

Returns a copy of ir with tracepoint PID filtering enabled on all tracepoint programs, injecting the hidden "__maebpf_pid_filter" HASH map.

Functions

allowlist_map(id, opts \\ [])

Constructs a %MaeBPF.IR.MapDef{} for an IPv4 or reversed-FQDN domain allowlist (symmetric to blocklist_map/2, tagged with meta.role = :allowlist).

analyze_headers(prog, opts \\ [])

Analyzes a %MaeBPF.IR.Program{} to determine which packet header parsing checks (:eth, :ipv4, :udp, :tcp) are required so the compiler can emit shared bounds checks once at the prologue.

blocklist_map(id, opts \\ [])

Constructs a %MaeBPF.IR.MapDef{} for an IPv4 (:lru_hash, :hash, :lpm_ipv4) or reversed-FQDN domain (:lpm_domain) blocklist.

bpf_f_no_prealloc()

Returns the BPF_F_NO_PREALLOC flag constant (1).

counter_map(id, opts \\ [])

Constructs a %MaeBPF.IR.MapDef{} for a counter map (:percpu_array by default).

debug_drop_counter_name()

Returns the canonical hidden debug RingBuf drop counter map name.

debug_ringbuf_name()

Returns the canonical hidden debug RingBuf map name ("__maebpf_debug_rb").

default_c_name(arg1, name)

Derives the default ELF function symbol name for a program hook and name.

drop_counter_id(stream_name)

Returns the hidden drop counter map ID atom for a :ringbuf stream.

drop_counter_name(stream_name)

Returns the canonical hidden PERCPU_ARRAY drop counter map name for a :ringbuf stream name.

estimate_budget(prog, ir, opts \\ [])

Estimates instruction and branch counts for static budget validation.

flatten_ops(ops)

Flattens a list of IR ops including nested :scope inner ops.

format_error(other)

Formats a single IR validation error term into a human-readable message.

map_type_id(kind)

Returns the kernel bpf_map_type integer for a supported IR map kind.

max_stack_bytes()

Returns the maximum eBPF stack frame size in bytes (512).

normalize(ir)

Normalizes a %MaeBPF.IR.Module{}:

  • Fills missing name, section, c_name, and LPM_TRIE BPF_F_NO_PREALLOC.
  • Assigns stable 1-based plug_id indices per program when unset.
  • Auto-injects hidden "__maebpf_rb_drops_<stream>" PERCPU_ARRAY maps for every declared :ringbuf stream.
  • Auto-injects hidden "__maebpf_sample_cnt" PERCPU_ARRAY when any op uses sampling (every > 1) or debug_trace uses sample > 1.

pid_filter_enabled?(arg1)

Returns true when program has tracepoint PID filtering enabled.

pid_filter_map(opts \\ [])

Constructs the hidden HASH map definition (4B u32 PID -> 4B u32) used to filter tracepoint invocations to selected OS PIDs.

pid_filter_map_name()

Returns the canonical hidden tracepoint OS PID filter HASH map name.

sample_counter_name()

Returns the canonical hidden per-CPU sampling sequence counter map name.

section_for_hook(arg1)

Derives the canonical libbpf ELF section name for a program hook.

sni_flows_map(opts \\ [])

Constructs the hidden 5-tuple LRU_HASH flow verdict cache (key_size: 12, value_size: 4) used by MaeBPF.Plugs.TLSSNI.

sni_flows_map_name()

Returns the canonical hidden LRU_HASH 5-tuple flow cache map name used by :tls_sni.

sni_scratch_map()

Constructs the hidden PERCPU_ARRAY scratch buffer map (value_size: 2304, max_entries: 1) used by MaeBPF.Plugs.TLSSNI.

sni_scratch_map_name()

Returns the canonical hidden PERCPU_ARRAY scratch map name used by :tls_sni.

stack_bytes(prog, ir, opts \\ [])

Computes exact kernel stack bytes required by program within ir_module.

stream_map(id, opts \\ [])

Constructs a %MaeBPF.IR.MapDef{} for a RingBuf event stream.

validate(ir, opts \\ [])

Validates a %MaeBPF.IR.Module{} statically.

Options:

  • :max_stack_bytes - default 512
  • :max_instructions - default 4096
  • :max_branches - default 512
  • :debug_trace - validate including debug-trace overhead

Returns {:ok, normalized_ir} or {:error, [reason, ...]}.

validate!(ir, opts \\ [])

Like validate/2, returning the normalized %MaeBPF.IR.Module{} or raising ArgumentError with a detailed validation report.

with_debug_trace(ir, opts \\ [])

Returns a copy of ir configured for live zero-downtime debug tracing.

Injects the hidden "__maebpf_debug_rb" :ringbuf stream, its hidden "__maebpf_rb_drops___maebpf_debug_rb" drop counter, and sets meta[:debug_trace] and meta[:debug_sample].

with_pid_filter(ir, opts \\ [])

Returns a copy of ir with tracepoint PID filtering enabled on all tracepoint programs, injecting the hidden "__maebpf_pid_filter" HASH map.