fix: XDP unsafe loader + Redis sync; enforce 250-line/4-file layout limits

- xdp: CString for if_nametoindex (was UB), real detach via prog fd
  (fabricated borrow_raw(-1) silently never detached), SAFETY comments,
  saturating expiry math; +5 unit tests
- redis: real pubsub reconnect with exponential backoff (was sleep+return);
  KEYS -> SCAN in heartbeat sweep
- ci: cargo test --all-features, repo-gates job — module size gate
  (scripts/check_module_size.sh, ratchet baseline) + default-secrets grep
- refactor src/ to <=250 LOC/file, <=4 .rs/dir without behavior change;
  thin bins (rampart.rs 330 -> 6 LOC), new app/, subnet/, intel/,
  profile/, prefix/, challenge/, filter/, probe/, inventory/, metrics/, node/
- docs: TODO v5.0 (status refresh, new rules, findings backlog),
  README quickstart now matches real binaries
- verify: fmt/clippy -D warnings/test --all-features (164 tests)/clang XDP green
This commit is contained in:
Loki 2026-09-15 23:55:17 +02:00
parent 7545b3d3ad
commit bf6d34c882
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
64 changed files with 2052 additions and 1408 deletions

View file

@ -31,53 +31,116 @@ impl RedisStore {
}
}
const BLACKLIST_CHANNEL: &str = "rampart:blacklist:events";
const BACKOFF_MIN: Duration = Duration::from_secs(1);
const BACKOFF_MAX: Duration = Duration::from_secs(30);
/// Capped exponential backoff step: `prev` doubled, clamped to `max`.
fn next_backoff(prev: Duration, max: Duration) -> Duration {
prev.saturating_mul(2).min(max)
}
/// What the outer reconnect loop should do after one pubsub session.
enum Flow {
/// Shutdown requested — exit the task.
Stop,
/// Connection dropped or session failed — reconnect with backoff.
Retry,
}
/// Resolves only when shutdown is truly requested (value `true`) or every
/// sender has been dropped; spurious non-shutdown notifications keep it pending
/// so it never cancels in-flight connect/subscribe work.
async fn wait_for_shutdown(shutdown: &mut watch::Receiver<bool>) {
loop {
if shutdown.changed().await.is_err() || *shutdown.borrow_and_update() {
return;
}
}
}
/// Blacklist pubsub subscriber with real reconnect: on connect/subscribe
/// failure or stream end it retries with capped exponential backoff (reset to
/// `BACKOFF_MIN` after each successfully applied message). Every await is
/// raced against the shutdown watch so the task stays cancellable.
pub async fn start_blacklist_sync(
client: &redis::Client,
blacklist: Arc<Blacklist>,
mut shutdown: watch::Receiver<bool>,
) {
let mut backoff = BACKOFF_MIN;
loop {
match subscribe_once(client, &blacklist, &mut shutdown, &mut backoff).await {
Flow::Stop => {
tracing::info!("shutting down blacklist subscriber");
return;
},
Flow::Retry => {},
}
tokio::select! {
_ = tokio::time::sleep(backoff) => {},
() = wait_for_shutdown(&mut shutdown) => {
tracing::info!("shutting down blacklist subscriber");
return;
},
}
backoff = next_backoff(backoff, BACKOFF_MAX);
}
}
/// One connect+subscribe+consume session. Sets `*backoff` to `BACKOFF_MIN`
/// whenever a message is applied, proving the link is healthy. Each blocking
/// await is raced against shutdown. (Connect+subscribe stay inline: the
/// `PubSub` type produced by `into_pubsub` is awkward to name in a split
/// helper's return type.)
async fn subscribe_once(
client: &redis::Client,
blacklist: &Blacklist,
shutdown: &mut watch::Receiver<bool>,
backoff: &mut Duration,
) -> Flow {
#[allow(deprecated)]
let conn = match client.get_async_connection().await {
Ok(c) => c,
Err(e) => {
tracing::error!("failed to connect to Redis for blacklist sync: {e}");
return;
let conn = tokio::select! {
connected = client.get_async_connection() => match connected {
Ok(c) => c,
Err(e) => {
tracing::error!("failed to connect to Redis for blacklist sync: {e}");
return Flow::Retry;
},
},
() = wait_for_shutdown(shutdown) => return Flow::Stop,
};
let mut pubsub = conn.into_pubsub();
if let Err(e) = pubsub.subscribe("rampart:blacklist:events").await {
tracing::error!("failed to subscribe to blacklist events: {e}");
return;
tokio::select! {
subscribed = pubsub.subscribe(BLACKLIST_CHANNEL) => match subscribed {
Ok(()) => tracing::info!("subscribed to {BLACKLIST_CHANNEL}"),
Err(e) => {
tracing::error!("failed to subscribe to blacklist events: {e}");
return Flow::Retry;
},
},
() = wait_for_shutdown(shutdown) => return Flow::Stop,
}
tracing::info!("subscribed to rampart:blacklist:events");
let mut stream = pubsub.on_message();
loop {
let mut stream = pubsub.on_message();
let msg_fut = stream.next();
tokio::pin!(msg_fut);
tokio::select! {
_ = shutdown.changed() => {
if *shutdown.borrow() {
tracing::info!("shutting down blacklist subscriber");
return;
}
}
result = &mut msg_fut => {
match result {
Some(msg) => {
if let Err(e) = handle_event(&msg, &blacklist) {
tracing::error!("blacklist event error: {e}");
}
maybe_msg = stream.next() => match maybe_msg {
Some(msg) => {
if let Err(e) = handle_event(&msg, blacklist) {
tracing::error!("blacklist event error: {e}");
} else {
*backoff = BACKOFF_MIN;
}
None => {
tracing::error!("pubsub stream ended");
tokio::time::sleep(Duration::from_secs(1)).await;
return;
}
}
}
},
None => {
tracing::error!("blacklist pubsub stream ended; resubscribing");
return Flow::Retry;
},
},
() = wait_for_shutdown(shutdown) => return Flow::Stop,
}
}
}
@ -126,3 +189,23 @@ impl StateStore for RedisStore {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn backoff_doubles_until_cap() {
let mut b = BACKOFF_MIN;
for expected_secs in [2, 4, 8, 16, 30, 30] {
b = next_backoff(b, BACKOFF_MAX);
assert_eq!(b, Duration::from_secs(expected_secs));
}
}
#[test]
fn backoff_never_exceeds_max_even_with_huge_prev() {
let b = next_backoff(Duration::from_secs(u64::MAX / 2), BACKOFF_MAX);
assert!(b <= BACKOFF_MAX);
}
}