C CO-RE eBPF compiler, use MaeBPFC DSL, ~BPFC inline C sigil, static BPF
object linker, and MaeBPFC.Plugs.TLSSNI companion package for :maebpf.
defmodule MyEdge.HybridFilter do
use MaeBPFC,
include_dirs: ["c_src"],
includes: ["my_defs.h"],
defines: [ENABLE_FAST_PATH: true],
link_objects: ["priv/helper.bpf.o"]
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
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 MaeBPF.Plugs.DropInvalid
plug ~BPFC"""
if (pkt.l4_ok && (pkt.dport == target_port || is_watched(pkt.dport))) {
return MAEBPF_DROP;
}
"""
default :pass
end
enduse MaeBPFC Options
:license- ELF license string (default"GPL"):include_dirs- list of header search directories passed toclangas-I <dir>:includes- list of headers emitted at the top of generated C ("foo.h"->#include "foo.h","<foo.h>"->#include <foo.h>):defines- keyword list or map of preprocessor macros passed as-D KEY=VAL(or-D KEYwhen value istrue):link_objects- list of precompiled BPF-target.bpf.oobject paths (EM_BPF,ET_REL) statically linked into the compiled module viabpftool gen object. Only kernel BPF.bpf.oobjects are accepted; userspace nativelibc/.sobinaries cannot be linked into Linux kernel eBPF.:vmlinux_h(or:vmlinux) -:auto(default: uses cached Linuxvmlinux.h+<bpf/bpf_helpers.h>when available, otherwise emits self-contained portable BPF headers so macOS AOT compilation works),true, orfalse:vmlinux_dir- custom directory for cachedvmlinux.h:cflags(or:extra_flags) - extraclangcompiler flags:mcpu- BPF CPU version (default"v3"):opt_level- optimization flag (default"-O2")
Inspect the exact generated C CO-RE source at any time in IEx:
IO.puts(MyEdge.HybridFilter.__maebpf__(:c_source))