Pure-Elixir eBPF compiler, fused Plug pipeline DSL, and OTP-managed libbpf 1.5 runtime with zero clang or LLVM dependencies.
1. App-Scoped Cgroup Policy
Scope rules to specific ports inside a cgroup hook and verify them locally on macOS or Linux.
defmodule MyApp.EgressPolicy do
use MaeBPF
counter :blocked
blocklist :bad_cidrs, type: :lpm_ipv4, max_entries: 1024
stream :alerts, ringbuf_kb: 256
cgroup :egress, direction: :egress do
plug MaeBPF.Plugs.DropInvalid
scope ports: [80, 443] do
drop ipv4(dst_in: :bad_cidrs), count: :blocked, emit: :alerts
end
default :pass
end
end
# Runs natively on macOS (:sim) or Linux (:both with BPF_PROG_TEST_RUN)
res =
MaeBPF.Test.run!(
MyApp.EgressPolicy,
:egress,
MaeBPF.Packet.tcp(dst: "198.51.100.2", dport: 443),
maps: %{bad_cidrs: ["198.51.100.0/24"]}
)
res.action
#=> :drop2. OTP Supervision and Multi-Target Attach
Attach to NICs, systemd services, cgroups, or PIDs. Programs detach automatically when the process or supervisor exits.
# Single service / cgroup / PID
{:ok, srv} = MaeBPF.attach(MyApp.EgressPolicy, {:systemd, "payments.service"})
:ok = MaeBPF.detach(srv)
# Multiple targets under MaeBPF.Supervisor
{:ok, sup} =
MaeBPF.attach([
{MyEdge.Gateway, targets: [ingress: {:xdp, "eth0"}, egress: {:tcx, "eth0", :egress}]},
{MyApp.EgressPolicy, {:cgroup, "/sys/fs/cgroup/my_app"}},
{MyAudit.Syscalls, {:tracepoint, "raw_syscalls", "sys_enter"}, pids: [app_os_pid]}
])
:ok = MaeBPF.detach(sup)
# Directly inside your Application Supervisor
children = [
{MyApp.EgressPolicy, target: {:systemd, "payments.service"}},
{MyApp.EgressPolicy, target: {:systemd, "checkout.service"}}
]
Supervisor.start_link(children, strategy: :one_for_one)3. Pure-Elixir TLS SNI Filtering
Zero-clang SNI filtering on TCX and cgroup hooks backed by a reversed-FQDN LPM trie and a 5-tuple LRU flow cache with TCP SYN reset.
defmodule MyEdge.EgressSNI do
use MaeBPF
blocklist :blocked_domains, type: :lpm_domain, max_entries: 10_000
counter :sni_blocked
stream :sni_alerts, ringbuf_kb: 256
tcx :egress, direction: :egress do
plug MaeBPF.Plugs.TLSSNI,
domains: :blocked_domains,
mode: :blocklist,
ech_action: :pass,
split_action: :pass,
count: :sni_blocked,
emit: :sni_alerts
default :pass
end
end
# Simulate locally on macOS or verify :both parity on Linux
res =
MaeBPF.Test.run!(
MyEdge.EgressSNI,
:egress,
MaeBPF.Packet.tls_client_hello(sni: "a.b.c.blocked.org"),
maps: %{blocked_domains: ["evil.example.com", "*.blocked.org"]}
)
res.action
#=> :drop
# Live updates at runtime
{:ok, srv} = MaeBPF.attach(MyEdge.EgressSNI, {:tcx, "eth0", :egress})
:ok = MaeBPF.map_put_domain(srv, "blocked_domains", "*.blocked.org")
:ok = MaeBPF.map_delete_domain(srv, "blocked_domains", "*.blocked.org")- For strict allowlists, declare
allowlist :allowed_domains, type: :lpm_domainand passmode: :allowlist, ech_action: :drop, split_action: :drop. - For 64-extension
bpf_iter_num, IPv6, VLAN, and optional TPROXY punt, useMaeBPFC.Plugs.TLSSNIin maebpf_c.
4. Multi-Hook NIC Policy, Hot-Swap, and Live Debug
Attach multiple hooks from one module, hot-swap atomically with maps preserved, and sample live ring buffer traces in IEx.
defmodule MyEdge.Gateway do
use MaeBPF
counter :blocked_dns
blocklist :bad_cidrs, type: :lpm_ipv4, max_entries: 1024
stream :alerts, ringbuf_kb: 256
xdp :ingress do
plug MaeBPF.Plugs.DropInvalid
drop ipv4(src_in: :bad_cidrs), 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
{:ok, srv} =
MaeBPF.attach(
MyEdge.Gateway,
[ingress: {:xdp, "eth0"}, egress: {:tcx, "eth0", :egress}],
ringbuf_map: "alerts",
on_event: fn bin -> IO.inspect(MaeBPF.Packet.decode_event(bin)) end
)
# Atomic BPF_LINK_UPDATE swap (same link_id, live maps reused)
{:ok, _meta} = MaeBPF.hot_swap(srv, MyEdge.GatewayV2)
# Live zero-downtime per-plug RingBuf tracing in IEx, then revert
{:ok, _} = MaeBPF.debug(srv, sample: 100)
{:ok, _} = MaeBPF.undebug(srv)
:ok = MaeBPF.detach(srv)5. Advanced Hooks, BTF Globals, and Custom Compilers
Auto-attach tracing hooks, conditional autoload, runtime BTF globals, tail calls, uprobes, packed schemas, dynamic pipelines, and custom compiler backends.
# Auto-attach fentry/kprobe/uprobe/LSM/iter with conditional autoload, BTF globals, and tail calls
{:ok, srv} =
MaeBPF.attach(
elf_or_mod,
:auto,
autoload: %{
"fentry_open" => {:when_supported, :fentry},
"kprobe_open" => {:when_unsupported, :fentry}
},
globals: %{
target_comm: "curl",
target_port: 443,
watch_ports: [80, 443, 8080, 8443],
magic_hdr: <<0xDE, 0xAD, 0xBE, 0xEF>>,
enabled: true
},
prog_arrays: %{"jump_table" => %{0 => "tail_v1"}},
swap_strategy: :overlap
)
{:ok, true} = MaeBPF.get_global(srv, :enabled)
:ok = MaeBPF.put_global(srv, :enabled, false)
:ok = MaeBPF.map_put_prog(srv, "jump_table", 0, "tail_v2")
{:ok, iter_bytes} = MaeBPF.read_iter(srv, max_bytes: 1_048_576)
# Resolve a shared library in a running process and attach a uprobe
{:ok, libssl} = MaeBPF.Target.find_process_library(os_pid, "libssl.so")
{:ok, up} = MaeBPF.attach(uprobe_elf, {:uprobe, libssl, "SSL_write", pid: os_pid})
# Packed schemas (:bool, {:string, n}, {:bytes, n}, {:array, elem, n}, integers, :ipv4/:ipv6/:mac)
schema = MaeBPF.Schema.compile!(enabled: :bool, comm: {:string, 16}, ports: {:array, :u16, 4})
bin = MaeBPF.Schema.encode!(schema, %{enabled: true, comm: "nginx", ports: [80, 443]})
# Dynamic runtime pipelines (microsecond compile, zero clang)
{:ok, compiled} =
MaeBPF.compile_pipeline(
[
MaeBPF.Plugs.DropInvalid,
{MaeBPF.Plugs.DropUDP, ports: [53, 1900], count: :blocked_udp},
{MaeBPF.Plugs.CountPackets, counter: :passed_pkts}
],
hook: :xdp,
default: :pass
)
IO.puts(MaeBPF.disasm(compiled.elf, source_map: compiled.source_map))
# Pluggable compiler backend (@behaviour MaeBPF.Compiler)
defmodule MyPolicy do
use MaeBPF, compiler: MyCustomCompiler
...
endInstallation
def deps do
[
{:maebpf, "~> 0.1.0"}
# Optional: add {:maebpf_c, "~> 0.1.0"} for use MaeBPFC, ~BPFC, and MaeBPFC.Plugs.TLSSNI
]
endRequirements
- Elixir >= 1.17 and Erlang/OTP >= 27
- Linux runtime (NIF): kernel >= 5.10 (6.6+ for TCX); precompiled NIF binaries (with
libbpf 1.5.0statically linked) are downloaded automatically viaelixir_makeforx86_64-linux-gnu,aarch64-linux-gnu,x86_64-linux-musl, andaarch64-linux-musl(onlylibelfandzlibshared libraries are needed at runtime) - Building the NIF from source (
MAEBPF_BUILD=1or unlisted Linux targets):libbpf-dev(>= 1.4),libelf-dev,zlib1g-dev, and a C11 compiler (cc) - macOS: skips the Linux NIF automatically and runs the compiler, simulator, and unit tests natively
Building and Testing
mix compile --warnings-as-errors
mix test