maeBPF C Compiler

Hex.pm

Optional C CO-RE compiler companion for maebpf.

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:

2. Full C CO-RE TLS SNI

Extended TLS SNI filter implemented in C CO-RE via MaeBPFC.Plugs.TLSSNI.

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
end

3. 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
end

4. Scope Inside Inline C Blocks

Variables and return macros injected by maeBPF:

Standard Linux and libbpf headers already included:

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"}
  ]
end

Requirements

Building and Testing

MAEBPF_PATH=../maebpf mix compile --warnings-as-errors
MAEBPF_PATH=../maebpf mix test