pub mod bpf_fn;
pub mod codegen;
pub mod runtime;
pub mod spec;
pub use spec::{
BpfAttachPoint, BpfMapKind, BpfMapSpec, BpfPolicySpec, BpfProgramKind, BpfProgramSpec,
};
impl tatara_lisp::DocumentedDomain for BpfProgramSpec {
const DOCSTRING: &'static str =
"One BPF program — kind (XDP/TC/kprobe/...), attach point, source, license. \
Loaded via aya at runtime; built hermetically through substrate's ebpf.nix.";
const FIELD_DOCS: &'static [(&'static str, &'static str)] = &[
("name", "Program name — the symbol exported in the BPF object."),
("kind", "BPF program kind. Drives the aya `#[xdp]` etc. attribute."),
("attach", "Where the program attaches (interface, kernel symbol, cgroup, ...)"),
("source", "Path to the program body — `*.rs`, `*.bpf.o`, or `*.tlisp:fn`."),
("license", "SPDX license string. GPL required for most helpers."),
("pin_path", "Optional bpffs pin path so the program survives the loader."),
("uses_maps", "BPF maps this program reads or writes."),
];
}
impl tatara_lisp::DocumentedDomain for BpfMapSpec {
const DOCSTRING: &'static str =
"One BPF map — hash / array / per-cpu / ring-buf / etc. \
The kernel-↔-userspace data plane for BPF programs.";
const FIELD_DOCS: &'static [(&'static str, &'static str)] = &[
("name", "Map name."),
("kind", "Map kind — drives access pattern (hash/array/perf-event/...)"),
("key_size", "Key size in bytes (0 for keyless maps like RingBuf)."),
("value_size", "Value size in bytes."),
("max_entries", "Capacity. For RingBuf, total bytes (page-rounded)."),
("pin_path", "Optional bpffs pin path."),
];
}
impl tatara_lisp::DocumentedDomain for BpfPolicySpec {
const DOCSTRING: &'static str =
"Composition of programs + maps applied as one unit. The IaC-shape \
arch-synthesizer + FluxCD consume.";
const FIELD_DOCS: &'static [(&'static str, &'static str)] = &[
("name", "Policy name."),
("description", "Human-readable description."),
("programs", "Names of `defbpf-program`s composed in this policy."),
("maps", "Names of `defbpf-map`s composed in this policy."),
];
}
impl tatara_lisp::DependentDomain for BpfMapSpec {
const DEPENDS_ON: &'static [&'static str] = &[];
}
impl tatara_lisp::DependentDomain for BpfProgramSpec {
const DEPENDS_ON: &'static [&'static str] = &["defbpf-map"];
}
impl tatara_lisp::DependentDomain for BpfPolicySpec {
const DEPENDS_ON: &'static [&'static str] = &["defbpf-program", "defbpf-map"];
}
impl tatara_lisp::AttestableDomain for BpfMapSpec {
const ATTESTATION_NAMESPACE: &'static str = "pleme.io/ebpf";
}
impl tatara_lisp::AttestableDomain for BpfProgramSpec {
const ATTESTATION_NAMESPACE: &'static str = "pleme.io/ebpf";
}
impl tatara_lisp::AttestableDomain for BpfPolicySpec {
const ATTESTATION_NAMESPACE: &'static str = "pleme.io/ebpf";
}
impl tatara_lisp::ValidatedDomain for BpfProgramSpec {
fn validate_value(value: &serde_json::Value) -> std::result::Result<(), String> {
let obj = value
.as_object()
.ok_or_else(|| "expected JSON object".to_string())?;
let license = obj
.get("license")
.and_then(|v| v.as_str())
.unwrap_or("");
let uses_maps = obj
.get("uses_maps")
.and_then(|v| v.as_array())
.map(|a| !a.is_empty())
.unwrap_or(false);
if uses_maps && !is_gpl_compatible(license) {
return Err(format!(
"BPF program declares `:uses-maps` but `:license` `{license}` \
is not GPL-compatible — kernel verifier will reject \
calls to bpf_map_lookup_elem etc."
));
}
if let Some(kind) = obj.get("kind").and_then(|v| v.as_str()) {
let attach_target = obj
.get("attach")
.and_then(|a| a.get("target"))
.and_then(|t| t.as_str())
.unwrap_or("");
let needs_iface = matches!(kind, ":xdp" | ":tc");
if needs_iface && attach_target.is_empty() {
return Err(format!(
"BPF program kind `{kind}` requires `:attach (:target \"<iface>\")` — got empty target"
));
}
}
Ok(())
}
}
fn is_gpl_compatible(license: &str) -> bool {
matches!(license, "GPL" | "GPL v2" | "Dual MIT/GPL" | "Dual BSD/GPL")
}
impl tatara_lisp::ValidatedDomain for BpfMapSpec {}
impl tatara_lisp::ValidatedDomain for BpfPolicySpec {}
impl tatara_lisp::CompliantDomain for BpfMapSpec {
const FRAMEWORKS: &'static [&'static str] = &[];
const CONTROLS: &'static [&'static str] = &[];
}
impl tatara_lisp::CompliantDomain for BpfProgramSpec {
const FRAMEWORKS: &'static [&'static str] = &[];
const CONTROLS: &'static [&'static str] = &[];
}
impl tatara_lisp::CompliantDomain for BpfPolicySpec {
const FRAMEWORKS: &'static [&'static str] = &["NIST 800-53", "CIS"];
const CONTROLS: &'static [&'static str] = &[
"NIST SC-7", "NIST SI-3", "CIS 5.1", ];
}
impl tatara_lisp::ObservableDomain for BpfMapSpec {
const METRIC_PREFIX: &'static str = "";
const LOG_LABELS: &'static [&'static str] = &[];
}
impl tatara_lisp::ObservableDomain for BpfProgramSpec {
const METRIC_PREFIX: &'static str = "tatara_ebpf_program";
const LOG_LABELS: &'static [&'static str] = &["program", "kind", "interface"];
}
impl tatara_lisp::ObservableDomain for BpfPolicySpec {
const METRIC_PREFIX: &'static str = "tatara_ebpf_policy";
const LOG_LABELS: &'static [&'static str] = &["policy"];
}
impl tatara_lisp::HelpDomain for BpfMapSpec {
const MNEMONIC: &'static str = "kernel-↔-userspace data plane";
const EXAMPLES: &'static [&'static str] = &[concat!(
"(defbpf-map\n",
" :name \"syn-counter\"\n",
" :kind :per-cpu-array\n",
" :key-size 4 :value-size 8 :max-entries 1)"
)];
}
impl tatara_lisp::HelpDomain for BpfProgramSpec {
const MNEMONIC: &'static str = "one BPF program (XDP/TC/kprobe/...)";
const EXAMPLES: &'static [&'static str] = &[concat!(
"(defbpf-program\n",
" :name \"drop-syn-flood\"\n",
" :kind :xdp\n",
" :attach (:target \"eth0\")\n",
" :source \"bpf/drop_syn.rs\"\n",
" :license \"GPL\")"
)];
}
impl tatara_lisp::HelpDomain for BpfPolicySpec {
const MNEMONIC: &'static str = "composition of programs + maps as one IaC unit";
const EXAMPLES: &'static [&'static str] = &[concat!(
"(defbpf-policy\n",
" :name \"edge-protection\"\n",
" :description \"L4 SYN-flood mitigation\"\n",
" :programs (\"drop_syn_flood\")\n",
" :maps (\"syn_counter\"))"
)];
}
impl tatara_lisp::StableDomain for BpfMapSpec {
const STABILITY: &'static str = "stable";
const SINCE_VERSION: &'static str = "0.1.0";
}
impl tatara_lisp::StableDomain for BpfProgramSpec {
const STABILITY: &'static str = "stable";
const SINCE_VERSION: &'static str = "0.2.0";
}
impl tatara_lisp::StableDomain for BpfPolicySpec {
const STABILITY: &'static str = "stable";
const SINCE_VERSION: &'static str = "0.2.0";
}
impl tatara_lisp::LifecycleProtocol for BpfProgramSpec {
const STRATEGY: tatara_lisp::RolloutStrategy = tatara_lisp::RolloutStrategy::BlueGreen;
const DRAIN_SECONDS: u32 = 5;
}
impl tatara_lisp::LifecycleProtocol for BpfMapSpec {
const STRATEGY: tatara_lisp::RolloutStrategy = tatara_lisp::RolloutStrategy::Recreate;
const DRAIN_SECONDS: u32 = 1;
}
impl tatara_lisp::LifecycleProtocol for BpfPolicySpec {
const STRATEGY: tatara_lisp::RolloutStrategy = tatara_lisp::RolloutStrategy::BlueGreen;
const DRAIN_SECONDS: u32 = 5;
}
pub fn register() {
tatara_lisp::domain::register::<BpfProgramSpec>();
tatara_lisp::domain::register::<BpfMapSpec>();
tatara_lisp::domain::register::<BpfPolicySpec>();
tatara_lisp::domain::register_doc::<BpfProgramSpec>();
tatara_lisp::domain::register_doc::<BpfMapSpec>();
tatara_lisp::domain::register_doc::<BpfPolicySpec>();
tatara_lisp::domain::register_deps::<BpfProgramSpec>();
tatara_lisp::domain::register_deps::<BpfMapSpec>();
tatara_lisp::domain::register_deps::<BpfPolicySpec>();
tatara_lisp::domain::register_attest::<BpfProgramSpec>();
tatara_lisp::domain::register_attest::<BpfMapSpec>();
tatara_lisp::domain::register_attest::<BpfPolicySpec>();
tatara_lisp::domain::register_validate::<BpfProgramSpec>();
tatara_lisp::domain::register_validate::<BpfMapSpec>();
tatara_lisp::domain::register_validate::<BpfPolicySpec>();
tatara_lisp::domain::register_lifecycle::<BpfProgramSpec>();
tatara_lisp::domain::register_lifecycle::<BpfMapSpec>();
tatara_lisp::domain::register_lifecycle::<BpfPolicySpec>();
tatara_lisp::domain::register_compliance::<BpfProgramSpec>();
tatara_lisp::domain::register_compliance::<BpfMapSpec>();
tatara_lisp::domain::register_compliance::<BpfPolicySpec>();
tatara_lisp::domain::register_observability::<BpfProgramSpec>();
tatara_lisp::domain::register_observability::<BpfMapSpec>();
tatara_lisp::domain::register_observability::<BpfPolicySpec>();
tatara_lisp::domain::register_help::<BpfProgramSpec>();
tatara_lisp::domain::register_help::<BpfMapSpec>();
tatara_lisp::domain::register_help::<BpfPolicySpec>();
tatara_lisp::domain::register_stability::<BpfProgramSpec>();
tatara_lisp::domain::register_stability::<BpfMapSpec>();
tatara_lisp::domain::register_stability::<BpfPolicySpec>();
}