Sigil macros for embedding inline eBPF C blocks inside use MaeBPFC modules.
defmodule MyEdge.CustomFilter do
use MaeBPFC
global :target_port, :u32, default: 443
counter :dropped
xdp :ingress do
plug MaeBPF.Plugs.DropInvalid
plug ~BPFC"""
if (pkt.l4_ok && pkt.dport == target_port) {
__u32 k = 0;
__u64 *cnt = bpf_map_lookup_elem(&dropped, &k);
if (cnt) (*cnt)++;
return MAEBPF_DROP;
}
"""
default :pass
end
end
Summary
Functions
Embeds a raw eBPF C snippet with exact Elixir #line source mapping inside
c_preamble or plug (for xdp, tcx, cgroup, and trace hooks).
Functions
Embeds a raw eBPF C snippet with exact Elixir #line source mapping inside
c_preamble or plug (for xdp, tcx, cgroup, and trace hooks).
Uses ~BPFC to avoid colliding with Elixir's built-in ~C charlist sigil.
Produces a %MaeBPFC.InlineC{} struct carrying __CALLER__.file and the
exact starting source line so clang diagnostics pinpoint the exact .ex
line.
1. maeBPF-Injected Variables and Return Macros
Available automatically inside plug ~BPFC"""...""":
ctx- raw kernel hook context pointer:xdp:struct xdp_md *ctxtcx/cgroup:struct __sk_buff *ctxtrace:void *ctx
struct maebpf_pkt pkt- pre-parsed packet summary (available inxdp,tcx, andcgrouphooks; unused fields are optimized away byclang):void *data- packet start pointer (ctx->data)void *data_end- packet end pointer (ctx->data_end)__u32 len- packet length in bytes__u16 eth_proto- host-order EtherType (0x0800IPv4,0x86DDIPv6)__u8 ip_version-4,6, or0if not IP__u8 proto- L4 IP protocol (6TCP,17UDP,1ICMP, ...)__be32 ipv4_src- IPv4 source address (network byte order)__be32 ipv4_dst- IPv4 destination address (network byte order)__u8 ipv6_src[16]- IPv6 source address bytes (whenip_version == 6)__u8 ipv6_dst[16]- IPv6 destination address bytes (whenip_version == 6)__u16 sport- L4 source port (host byte order)__u16 dport- L4 destination port (host byte order)__be16 sport_be- L4 source port (network byte order)__be16 dport_be- L4 destination port (network byte order)__u16 l3_off- byte offset of L3 header fromdata__u16 l4_off- byte offset of L4 header fromdata__u8 tcp_flags- TCP flags byte (whenproto == 6andl4_ok == 1)__u8 eth_ok-1when Ethernet header parsed (always1on L3cgroup)__u8 ipv4_ok-1when valid IPv4 header parsed__u8 ipv6_ok-1when valid IPv6 header parsed__u8 ipv4_invalid-1when IPv4 frame is truncated orihl < 5__u8 l4_ok-1when TCP or UDP header parsed
- Module-declared maps - every
counter,blocklist,allowlist,stream, andbpf_mapis in scope by exact declared name (for example&bad_ips,&hits,&alerts). - Module-declared globals - every
globalis in scope directly by exact declared name (for exampletarget_port,target_comm,watch_ports,debug_mode). - Return macros:
MAEBPF_CONT- continue to the next plug in the pipeline (default if the block falls through without returning)MAEBPF_PASS- halt immediately and pass (XDP_PASS,TCX_PASS,1oncgroup,0ontrace)MAEBPF_DROP- halt immediately and drop (XDP_DROP,TCX_DROP,0oncgroup,0ontrace)MAEBPF_STOP- stop pipeline early and return the hook'sdefaultaction
2. Standard Linux / libbpf Headers and Helpers Already Included
No manual #include is needed for common eBPF types, maps, or helpers
(see man 7 bpf-helpers for full helper contracts):
- Kernel / UAPI types (
"vmlinux.h"on Linux CO-RE, or portable BPF UAPI definitions in standalone mode):__u8..__u64,__s8..__s64,__be16..__be64,bool,struct xdp_md,struct __sk_buff,struct ethhdr,struct iphdr,struct ipv6hdr,struct tcphdr,struct udphdr,ETH_P_IP,ETH_P_IPV6,ETH_P_8021Q,IPPROTO_TCP,IPPROTO_UDP. <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,SEC,__uint,__type.<bpf/bpf_endian.h>:bpf_htons,bpf_ntohs,bpf_htonl,bpf_ntohl.
Examples
# Module-level C helper + inline packet inspection
c_preamble ~BPFC"""
static __always_inline int is_watched(__u16 port) {
for (int i = 0; i < 4; i++) {
if (watch_ports[i] == port) return 1;
}
return 0;
}
"""
xdp :ingress do
plug ~BPFC"""
if (pkt.l4_ok && is_watched(pkt.dport)) {
return MAEBPF_DROP;
}
return MAEBPF_CONT;
"""
default :pass
end