Optional C CO-RE compiler companion for maebpf.
- Inline C blocks (
use MaeBPFC,~BPFC) with exact#lineElixir error mapping - Typed
.rodata,.data, and.bssglobals viaglobal/3 - Static linking for BPF-target
.bpf.oobjects vialink_objects: - Full C CO-RE TLS SNI plug (
MaeBPFC.Plugs.TLSSNI)
1. Hybrid Elixir and Inline C
Combine standard plugs with inline C blocks, shared C preambles, header includes, macros, and typed globals.
defmodule MyEdge.HybridFilter do
use MaeBPFC,
include_dirs: ["c_src"],
includes: ["my_defs.h"],
defines: [MAX_PORTS: 4, ENABLE_FAST_PATH: true]
# Typed globals compiled into .rodata (readonly: true, default) or .data/.bss (readonly: false)
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
counter :dropped
blocklist :bad_ips, type: :lpm_ipv4, max_entries: 1024
c_preamble ~BPFC"""
static __always_inline int is_watched(__u16 port) {
#ifdef ENABLE_FAST_PATH
#pragma unroll
for (int i = 0; i < MAX_PORTS; i++) {
if (watch_ports[i] == port) return 1;
}
#endif
return 0;
}
"""
xdp :ingress do
plug MaeBPF.Plugs.DropInvalid
drop ipv4(src_in: :bad_ips), count: :dropped
plug ~BPFC"""
if (pkt.l4_ok && (pkt.dport == target_port || is_watched(pkt.dport))) {
__u32 k = 0;
__u64 *cnt = bpf_map_lookup_elem(&dropped, &k);
if (cnt) (*cnt)++;
return debug_mode ? MAEBPF_PASS : MAEBPF_DROP;
}
"""
default :pass
end
end
# Inspect generated C CO-RE source in IEx
IO.puts(MyEdge.HybridFilter.__maebpf__(:c_source))
# Override .rodata/.data globals before load, and update mutable globals live
{:ok, srv} =
MaeBPF.attach(
MyEdge.HybridFilter,
{:xdp, "eth0"},
globals: %{target_port: 8443, target_comm: "nginx", debug_mode: true}
)
{:ok, true} = MaeBPF.get_global(srv, :debug_mode)
:ok = MaeBPF.put_global(srv, :debug_mode, false)Supported global/3 types:
- Scalars:
:bool,:u8..:u64,:i8..:i64,:be16,:be32,:be64 - Network addresses:
:ipv4,:ipv6,:mac - Fixed-length buffers and arrays:
{:string, n},{:bytes, n},{:array, elem_type, n}
2. Full C CO-RE TLS SNI
Extended TLS SNI filter implemented in C CO-RE via MaeBPFC.Plugs.TLSSNI.
- 64-extension
bpf_iter_numloop covering post-quantum ClientHello payloads - IPv4, IPv6, and 802.1Q / 802.1ad VLAN support
- ECH (
0xFE0D) policy and optionaltproxy_portfwmark punt for split ClientHello reassembly - Reversed-FQDN LPM trie, 5-tuple LRU flow cache with TCP SYN reset, and
.rodatapolicy globals
defmodule MyEdge.FullSNI do
use MaeBPFC
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 MaeBPFC.Plugs.TLSSNI,
domains: :blocked_domains,
mode: :blocklist,
max_extensions: 64,
ipv6: true,
vlan: true,
ech_action: :drop,
split_action: {:tproxy, 15001},
count: :sni_blocked,
emit: :sni_alerts
default :pass
end
end3. Static BPF Object Linking
Statically link precompiled BPF-target .bpf.o objects (EM_BPF, ET_REL) via bpftool gen object. Userspace native .so or libc binaries are rejected because Linux kernel eBPF only links BPF objects.
defmodule MyEdge.LinkedFilter do
use MaeBPFC, link_objects: ["priv/helper.bpf.o"]
c_preamble ~BPFC"""
extern int helper_is_blocked_port(__u16 port);
"""
xdp :ingress do
plug ~BPFC"""
if (pkt.l4_ok && helper_is_blocked_port(pkt.dport)) return MAEBPF_DROP;
"""
default :pass
end
end4. Scope Inside Inline C Blocks
Variables and return macros injected by maeBPF:
ctx:struct xdp_md *(xdp),struct __sk_buff *(tcx/cgroup), orvoid *(trace)struct maebpf_pkt pkt(xdp/tcx/cgroup):data,data_end,len,eth_proto,ip_version,proto,ipv4_src,ipv4_dst,ipv6_src[16],ipv6_dst[16],sport,dport,sport_be,dport_be,l3_off,l4_off,tcp_flags,eth_ok,ipv4_ok,ipv6_ok,ipv4_invalid,l4_ok- Declared maps (
&dropped,&bad_ips,&alerts) andglobalvariables (target_port,target_comm, etc.) by exact name - Return macros:
MAEBPF_CONT(next plug, implicit on fallthrough),MAEBPF_PASS,MAEBPF_DROP,MAEBPF_STOP(return hook default)
Standard Linux and libbpf headers already included:
"vmlinux.h"on Linux CO-RE, or portable BPF UAPI types for standalone and macOS AOT builds<bpf/bpf_helpers.h>:bpf_map_lookup_elem,bpf_map_update_elem,bpf_map_delete_elem,bpf_ringbuf_reserve,bpf_ringbuf_submit,bpf_ringbuf_discard,bpf_skb_load_bytes,bpf_ktime_get_ns,bpf_get_current_pid_tgid,bpf_get_current_comm,bpf_tail_call,bpf_printk,bpf_for(seeman 7 bpf-helpers)<bpf/bpf_endian.h>:bpf_htons,bpf_ntohs,bpf_htonl,bpf_ntohl
5. Raw C CO-RE Compilation
Compile and attach standalone C eBPF source strings directly with {:c, source}.
c_src = """
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
SEC("xdp")
int xdp_pass(struct xdp_md *ctx) {
return XDP_PASS;
}
char LICENSE[] SEC("license") = "GPL";
"""
{:ok, elf} = MaeBPFC.compile(c_src)
{:ok, srv} = MaeBPF.attach({:c, c_src}, {:xdp, "veth0"}, program: "xdp_pass")
{:ok, _} = MaeBPF.hot_swap(srv, {:c, c_src_v2})Installation
def deps do
[
{:maebpf_c, "~> 0.1.0"}
]
endRequirements
- Elixir >= 1.17 and Erlang/OTP >= 27
- clang with
-target bpfsupport (works on both Linux and macOS) bpftooland/sys/kernel/btf/vmlinuxon Linux for CO-REvmlinux.hgeneration andlink_objects:static linking
Building and Testing
MAEBPF_PATH=../maebpf mix compile --warnings-as-errors
MAEBPF_PATH=../maebpf mix test