MaeBPFC.Sigils (maebpf_c v0.1.0)

Copy Markdown View Source

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

sigil_BPFC(arg, modifiers)

(macro)

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 *ctx
    • tcx / cgroup: struct __sk_buff *ctx
    • trace: void *ctx
  • struct maebpf_pkt pkt - pre-parsed packet summary (available in xdp, tcx, and cgroup hooks; unused fields are optimized away by clang):
    • 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 (0x0800 IPv4, 0x86DD IPv6)
    • __u8 ip_version - 4, 6, or 0 if not IP
    • __u8 proto - L4 IP protocol (6 TCP, 17 UDP, 1 ICMP, ...)
    • __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 (when ip_version == 6)
    • __u8 ipv6_dst[16] - IPv6 destination address bytes (when ip_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 from data
    • __u16 l4_off - byte offset of L4 header from data
    • __u8 tcp_flags - TCP flags byte (when proto == 6 and l4_ok == 1)
    • __u8 eth_ok - 1 when Ethernet header parsed (always 1 on L3 cgroup)
    • __u8 ipv4_ok - 1 when valid IPv4 header parsed
    • __u8 ipv6_ok - 1 when valid IPv6 header parsed
    • __u8 ipv4_invalid - 1 when IPv4 frame is truncated or ihl < 5
    • __u8 l4_ok - 1 when TCP or UDP header parsed
  • Module-declared maps - every counter, blocklist, allowlist, stream, and bpf_map is in scope by exact declared name (for example &bad_ips, &hits, &alerts).
  • Module-declared globals - every global is in scope directly by exact declared name (for example target_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, 1 on cgroup, 0 on trace)
    • MAEBPF_DROP - halt immediately and drop (XDP_DROP, TCX_DROP, 0 on cgroup, 0 on trace)
    • MAEBPF_STOP - stop pipeline early and return the hook's default action

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