MaeBPF.DSL (maebpf v0.1.0)

Copy Markdown View Source

Unified compile-time DSL backing use MaeBPF.

One Elixir module compiles to one multi-program, multi-map little-endian bpfel ELF64 object embedded directly into the .beam at mix compile time (on both macOS and Linux with zero clang dependency).

Example:

defmodule MyEdge.Gateway do
  use MaeBPF

  counter   :blocked_dns
  blocklist :bad_ips, type: :lpm_ipv4, max_entries: 10_000
  stream    :alerts,  ringbuf_kb: 256

  xdp :ingress do
    plug MaeBPF.Plugs.DropInvalid
    drop ipv4(src_in: :bad_ips), count: :blocked_dns
    drop udp(dport: 53),         count: :blocked_dns, emit: :alerts
    pass tcp(dport: 443)
    default :pass
  end

  tcx :egress, direction: :egress do
    drop tcp(dport: 25)
    default :pass
  end
end

Summary

Functions

Declares a module-level IPv4 or domain allowlist map (type: :lpm_domain | :lpm_ipv4 | :lru_hash | :hash).

Declares a module-level IPv4 or domain blocklist map (type: :lpm_ipv4 | :lpm_domain | :lru_hash | :hash).

Declares a custom BPF map using explicit %MaeBPF.IR.MapDef{} fields.

Declares module-level C code (structs, static __always_inline helpers, or extern declarations) emitted before hook functions in use MaeBPFC modules.

Declares a Linux cgroup v2 cgroup_skb hook block (direction: :ingress | :egress).

Declares a module-level counter map (:percpu_array by default, key: u32, value: u64).

Sets the default fallback action (:pass, :drop, :tx, :next, etc.) for packets that reach the end of the current hook pipeline.

Declarative drop rule sugar inside a hook block

Declares a typed eBPF global variable in .rodata (readonly: true, default) or .data/.bss (readonly: false) for use MaeBPFC modules.

Declarative pass rule sugar inside a hook block

Appends a MaeBPF.Plug module to the current hook pipeline.

Scopes a block of packet rules to matching L4 ports and/or protocol inside an xdp, tcx, or cgroup hook block.

Declares a module-level RingBuf event stream (ringbuf_kb: default 256). IR normalization automatically registers "__maebpf_rb_drops_<name>".

Declares a tcx program hook block (direction: :ingress | :egress).

Declares a kernel tracepoint hook block.

Declares an xdp program hook block sharing all module-level maps.

Functions

allowlist(id, opts \\ [])

(macro)

Declares a module-level IPv4 or domain allowlist map (type: :lpm_domain | :lpm_ipv4 | :lru_hash | :hash).

allowlist :allowed_domains, type: :lpm_domain, max_entries: 10_000

blocklist(id, opts \\ [])

(macro)

Declares a module-level IPv4 or domain blocklist map (type: :lpm_ipv4 | :lpm_domain | :lru_hash | :hash).

bpf_map(id, opts)

(macro)

Declares a custom BPF map using explicit %MaeBPF.IR.MapDef{} fields.

c_preamble(code_or_inline)

(macro)

Declares module-level C code (structs, static __always_inline helpers, or extern declarations) emitted before hook functions in use MaeBPFC modules.

c_preamble ~BPFC"""
static __always_inline int is_watched(__u16 port) {
    return port == target_port;
}
"""

cgroup(name, list)

(macro)

Declares a Linux cgroup v2 cgroup_skb hook block (direction: :ingress | :egress).

Compiles to ELF section "cgroup_skb/ingress" or "cgroup_skb/egress" with L3 IPv4 packet offsets (no 14-byte Ethernet header) and cgroup_skb verdicts (1 = :pass, 0 = :drop).

cgroup(name, opts, list)

(macro)

counter(id, opts \\ [])

(macro)

Declares a module-level counter map (:percpu_array by default, key: u32, value: u64).

default(action)

(macro)

Sets the default fallback action (:pass, :drop, :tx, :next, etc.) for packets that reach the end of the current hook pipeline.

drop(matcher, opts \\ [])

(macro)

Declarative drop rule sugar inside a hook block:

global(name, type, opts \\ [])

(macro)

Declares a typed eBPF global variable in .rodata (readonly: true, default) or .data/.bss (readonly: false) for use MaeBPFC modules.

global :target_comm, {:string, 16}, default: "curl"
global :target_port, :u32, default: 443
global :watch_ports, {:array, :u16, 4}, default: [80, 443, 8080, 8443]
global :magic_hdr, {:bytes, 4}, default: <<0xDE, 0xAD, 0xBE, 0xEF>>
global :debug_mode, :bool, default: false, readonly: false

Supported types: :bool, :u8, :u16, :u32, :u64, :i8, :i16, :i32, :i64, :be16, :be32, :be64, :ipv4, :ipv6, :mac, {:string, n}, {:bytes, n}, and {:array, elem_type, n}.

pass(matcher, opts \\ [])

(macro)

Declarative pass rule sugar inside a hook block:

plug(plug_mod, opts \\ [])

(macro)

Appends a MaeBPF.Plug module to the current hook pipeline.

scope(opts, list)

(macro)

Scopes a block of packet rules to matching L4 ports and/or protocol inside an xdp, tcx, or cgroup hook block.

Packets that do not match the scope guard skip every rule inside the block untouched and continue to subsequent rules or default.

Options:

  • :ports - integer port or list of ports in 1..65535 (also accepts :dport or :sport shorthands)
  • :proto - :any (default, matching TCP or UDP), :tcp, or :udp
  • :match - :dport (default), :sport, or :either

stream(id, opts \\ [])

(macro)

Declares a module-level RingBuf event stream (ringbuf_kb: default 256). IR normalization automatically registers "__maebpf_rb_drops_<name>".

tcx(name, list)

(macro)

Declares a tcx program hook block (direction: :ingress | :egress).

tcx(name, opts, list)

(macro)

trace(name, target_or_opts, list)

(macro)

Declares a kernel tracepoint hook block.

Target may be a "category/name" string or keyword options [tracepoint: "category/name"] / [category: "syscalls", name: "sys_enter_openat"]. Pass filter_pids: true or pids: [os_pid, ...] to compile in an entry PID filter backed by the hidden "__maebpf_pid_filter" HASH map.

trace(name, target, opts, list)

(macro)

xdp(name, list)

(macro)

Declares an xdp program hook block sharing all module-level maps.

xdp(name, opts, list)

(macro)