PATTERN Cited by 1 source
iptables packet counter for rate metric¶
Use systems/iptables's built-in per-rule packet and byte counters as a lightweight, kernel-side rate-metric primitive. Add a rule matching traffic of interest, jump it to an empty user-defined chain to isolate the counter, and poll the counter from a shell loop reporting to your metrics pipeline.
Mechanism¶
- Create a new chain for isolation:
- Add a jump rule on the
OUTPUTchain matching the target (e.g. packets destined for the AWS VPC resolver): - Add an empty catch-all rule inside the user-defined chain so the counter tallies every matched packet without side effects:
- Poll the counter once per second:
The metric pipeline calculates rates by differencing successive samples.
When this beats alternatives¶
- vs tcpdump: tcpdump captures every packet, which is expensive at high rate and requires parsing pcap files. iptables counters are free (already incremented by the kernel) and integer-valued.
- vs eBPF: eBPF is more flexible but requires more recent kernels and a deploy-an-agent discipline. iptables is in every modern Linux distribution out of the box.
- vs application metrics: application-level DNS metrics count logical queries, not outbound packets. When retries at multiple layers amplify logical queries into many outbound packets (the canonical DNS retry amplification shape), only the packet-level metric reveals the true outbound rate.
Limitations¶
- No per-flow or per-source breakdown without adding more rules. Each rule's counter is scalar.
- Counter overflow. On 32-bit counters at very high rates, overflow matters; modern kernels use 64-bit counters but some userspace scripts truncate.
- Lost on iptables reload / restart unless using
iptables-save/iptables-restorewith counter preservation, or netfilter persistent rules. - Ordering matters. A rule placed after a terminating rule
(
ACCEPT,DROP) won't see the traffic.
Seen in¶
- Stripe — The secret life of DNS packets (2024-12-12).
Canonical wiki instance. Stripe added the jump-to-empty-chain
rule on every DNS server host, ran a 1-second shell loop to
report packets-per-second to Datadog, and used the new metric
to confirm the correlation between
SERVFAILspikes and outbound packet rate to the VPC resolver exceeding the 1,024-pps cap.