feat(fw): move per-source rate limiting into the XDP datapath#158
Merged
Conversation
The per-source rate machine (counting, rate calculation, and the blocking decision) now lives entirely in the kernel. The eBPF datapath tracks each source over a fixed 1s window in V4/V6_SRC_STATE and blocks over-rate sources in-line (blocked_until, re-extended each window still over-rate); drops engage once the destination escalates to SOURCE_BLOCK. Userspace only arms SRC_RATE_CFG (startup + PUT /limits) and batch-reads the state for display. Deleted: cumulative V4/V6_SRC_COUNTERS, the auto V4/V6_CIDR_SRC block tries, SourceTracker/advance_source/step_sources, CIDR aggregation (plan_blocks), and the spoof gate. The seeding/counter-lifecycle bug class is structurally gone; blocking latency is packet-exact instead of up-to-one-500ms-tick; control-loop CPU no longer scales with flood history. Also: raw BPF_MAP_LOOKUP_BATCH reader (batch.rs) for all remaining map scans (dest/tx counters, GC sweeps, state snapshots) with per-entry fallback on old kernels — one syscall per ~4k entries instead of two per entry; idle source states GC'd after 60s. Compat: legacy escalation config keys (agg-fanout, src-exit-pct, spoof gate, …) still parse and are ignored (regression-tested) so existing config files keep loading across self-upgrades. src_exit_pct is removed from the Limits API; the dashboard drops the cooling state and src exit field. Verified: 43 lib + 2 bin + 15 API tests and the full netns e2e suite (21 root tests) pass, including two new scenarios proving the kernel blocks and releases an over-rate source with zero userspace ticks. Dest/prefix detection deliberately stays in userspace: bounded (16k), multi-axis policy with prefix aggregation, and mitigation state persists in DEST_STATE regardless of the daemon.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Moves the per-source rate machine — counting, rate calculation, and the blocking decision — out of the userspace control loop and into the XDP datapath.
Why
The userspace design polled the entire per-source counter map (up to 256k LRU entries, 2 syscalls/entry) every 500 ms tick to compute rate deltas. That produced a chain of real production incidents:
src_rate_pps) invisible next to the dest limit,Each was patched, but the class of bug is structural: the decision was made from sampled copies of state the kernel already owns. Now the decision uses the same state the packets update.
Design
V4/V6_SRC_STATE: LruHashMap<ip, SrcState{window_start_ns, count, blocked_until_ns}>— fixed 1 s window, in-line per packet under mitigation: window roll → reset; count; crossingmax_per_window→blocked_until = now + cooldown(re-extended each window still over-rate = natural hysteresis). Blocked sources keep being counted (rate measured pre-drop).SOURCE_BLOCKescalation (ladder unchanged, driven byescalate_pass_pps); counting is not.SRC_RATE_CFGarray written by userspace at startup and onPUT /limits(max_per_windowprecomputed — no division in the datapath).V4/V6_SRC_COUNTERS, autoV4/V6_CIDR_SRCtries,SourceTracker/advance_source/step_sources, CIDR aggregation, spoof gate (~600 lines of policy code + tests). A spoofed high-cardinality flood churns the LRU without ever tripping a per-source limit; the port-filter/SYN-proxy layers remain the defense there.batch.rs: rawBPF_MAP_LOOKUP_BATCHreader (one syscall per ~4k entries) for all remaining scans (dest/tx counters, GC, display snapshots), with per-entry fallback latched off on old kernels.Compatibility
agg-fanout,src-exit-pct,max-real-sources, …) still parse and are ignored (regression test) — a config parse failure after a self-upgrade restart would take the firewall down.src_exit_pctremoved from theLimitsAPI (serde defaults keep old PUT bodies working); dashboard drops thecoolingstate andsrc exit %field.DEST_STATEeven if the daemon dies.Testing
kernel_blocks_over_rate_source_and_releases_after_cooldown— kernel blocks an over-rate source and releases it after cooldown with zero userspace control ticks; low-rate neighbour in the same /24 untouched.source_drops_gated_on_source_block_flag— over-rate source is counted/marked but not dropped untilSOURCE_BLOCKescalates.Work file:
work/fw-inkernel-src-rate.md. Suggest releasing as v0.4.0.