feat(xdp): IPv6 UDP rate limit, working loader; English JavaDoc comments

XDP behaviour:
- udp_rate_limit6 (LRU, 128-bit source key) + throttle_hit6, so
  udp_policy = "rate-limit" now applies to native IPv6 as well.
- scripts/xdp_ipv6_smoke.sh: phase 4 patches .rodata before load and
  verifies policy=2 on a real kernel (3 pass / 3 rate-limited).

XDP loader (was completely broken, never reached the verifier):
- .rodata is now one struct rampart_config with frozen offsets enforced by
  _Static_assert; XdpGlobals is #[repr(C)] and the image is built with
  offset_of!. Previously a 96-byte image was written into a 76-byte map at
  offsets clang had reordered, so load() failed with EINVAL.
- patch_rodata fails loudly when the map size and the image size diverge.
- build.rs tracks xdp/core and xdp/hooks, not just universal_filter.c.
- build_ringbuf looked for "events_map"; the map is "events_ringbuf".
- tests/xdp_object_maps.rs: verifies map names and .rodata size without root;
  the real binary was confirmed to attach in a netns.

Docs and style:
- Code comments are English, JavaDoc/Rustdoc style, without emoji or
  decorative banners (new project rule, TODO §0.8).
- README rewritten bilingual: Russian first, English below, TOC and
  copy-paste quick start.
- docs/README.md: documentation index and maintenance rules;
  configuration.md documents the whole [xdp] section and IPv4/IPv6 coverage;
  testing.md describes the real-kernel smoke; stale research links removed.
This commit is contained in:
Loki 2026-11-15 23:44:07 +01:00
parent 295ab81fb4
commit 918e561a61
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
104 changed files with 2129 additions and 1021 deletions

View file

@ -1,17 +1,20 @@
#!/usr/bin/env bash
# Функциональный smoke XDP-банов на РЕАЛЬНОМ ядре (netns + veth + verifier).
# Functional smoke test of XDP bans on a real kernel (netns + veth + verifier).
#
# Что проверяет:
# 1. verifier принимает universal_filter.o;
# 2. native-IPv6 без бана проходит, с записью /128 в blacklist6_map — дропается;
# 3. короткая UDP-датаграмма (<20 байт L4) тоже дропается — раньше она уходила
# в XDP_PASS мимо blacklist (проверка длины была рассчитана на TCP);
# 4. то же для IPv4 (общий L4-гейт);
# 5. expiry: бан с монотонным TTL реально истекает в ядре (раньше userspace
# писал epoch-ns, а C сравнивал с bpf_ktime_get_ns() — бан жил вечно).
# What it verifies:
# 1. the verifier accepts universal_filter.o;
# 2. native IPv6 traffic passes without a ban and is dropped once a /128
# entry exists in blacklist6_map;
# 3. a short UDP datagram (under 20 bytes of L4) is dropped as well; it used
# to fall through to XDP_PASS past the blacklist because the length check
# was sized for TCP;
# 4. the same holds for IPv4, through the shared L4 gate;
# 5. expiry: a ban with a monotonic TTL really expires in the kernel; it
# used to live forever because userspace wrote epoch-ns while the C side
# compared against bpf_ktime_get_ns().
#
# Требует root/CAP_BPF+CAP_NET_ADMIN, bpftool, clang, python3.
# Запуск: sudo bash scripts/xdp_ipv6_smoke.sh
# Requires root/CAP_BPF+CAP_NET_ADMIN, bpftool, clang, python3.
# Run: sudo bash scripts/xdp_ipv6_smoke.sh
set -u
@ -30,6 +33,7 @@ PASS=1
fail() { echo "FAIL: $1"; PASS=0; }
ok() { echo "ok: $1"; }
# Detach and remove every artefact, so a rerun starts from a clean state.
cleanup() {
bpftool net detach xdp dev "$DEV0" 2>/dev/null
rm -f "$PIN" "$OBJ"
@ -41,11 +45,13 @@ trap cleanup EXIT
[ "$(id -u)" = 0 ] || { echo "нужен root (CAP_BPF+CAP_NET_ADMIN)"; exit 2; }
cd "$(dirname "$0")/.." || exit 2
# Build the object once; the same binary is reused for every phase below.
clang -O2 -g -target bpf -mcpu=v3 -c xdp/core/universal_filter.c -o "$OBJ" \
-Ixdp -Ixdp/core -Ixdp/hooks -I/usr/include/x86_64-linux-gnu \
|| { echo "clang: не собралось"; exit 1; }
ok "clang: universal_filter.c собран"
# Isolated namespace with one veth pair: host side is DEV0, peer is DEV1.
ip netns add "$NS" || { echo "не удалось создать netns"; exit 2; }
ip link add "$DEV0" type veth peer name "$DEV1" || exit 2
ip link set "$DEV1" netns "$NS"
@ -58,36 +64,43 @@ ip -n "$NS" link set "$DEV1" up
ip -n "$NS" link set lo up
ok "netns $NS + veth ($HOST6/$HOST4 <-> $NS6/$NS4)"
# Loading through the verifier is itself a check: a rejected program exits here.
bpftool prog load "$OBJ" "$PIN" type xdp || { echo "verifier отверг программу"; exit 1; }
bpftool net attach xdp pinned "$PIN" dev "$DEV0" || exit 1
ok "verifier принял программу, attach на $DEV0"
IDS=$(bpftool prog show pinned "$PIN" | grep -o 'map_ids [0-9,]*' | cut -d' ' -f2 | tr ',' ' ')
B4=""
B6=""
ST=""
for id in $IDS; do
desc=$(bpftool map show id "$id" 2>/dev/null)
case "$desc" in
*"name blacklist6_map"*) B6=$id ;;
*"name blacklist_map"*) B4=$id ;;
*"name stats_map"*) ST=$id ;;
esac
done
[ -n "$B4" ] && [ -n "$B6" ] && [ -n "$ST" ] || { echo "карты не найдены"; exit 1; }
# Resolve map ids from the pinned program; ids change on every load.
discover_maps() {
IDS=$(bpftool prog show pinned "$PIN" | grep -o 'map_ids [0-9,]*' | cut -d' ' -f2 | tr ',' ' ')
B4=""
B6=""
ST=""
for id in $IDS; do
desc=$(bpftool map show id "$id" 2>/dev/null)
case "$desc" in
*"name blacklist6_map"*) B6=$id ;;
*"name blacklist_map"*) B4=$id ;;
*"name stats_map"*) ST=$id ;;
esac
done
[ -n "$B4" ] && [ -n "$B6" ] && [ -n "$ST" ]
}
discover_maps || { echo "карты не найдены"; exit 1; }
ok "blacklist_map id=$B4, blacklist6_map id=$B6, stats_map id=$ST"
# Ключи LPM: prefixlen (u32 LE) + адрес в network order.
# LPM keys: prefixlen (u32 LE) followed by the address in network order.
KEY6="80 00 00 00 fd 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02"
KEY4="20 00 00 00 0a 63 00 02"
send6() { # $1 = размер payload
# Send one IPv6 UDP datagram of $1 payload bytes from inside the namespace.
send6() {
ip netns exec "$NS" python3 -c "
import socket
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
s.sendto(b'x' * $1, ('$HOST6', $PORT))
"
}
# Same for IPv4.
send4() {
ip netns exec "$NS" python3 -c "
import socket
@ -96,7 +109,7 @@ s.sendto(b'x' * $1, ('$HOST4', $PORT))
"
}
# Суммы per-CPU счётчиков (только ненулевые).
# Sum the per-CPU counters and print only the non-zero ones.
stats() {
bpftool -j map dump id "$ST" | python3 -c "
import json, sys
@ -110,9 +123,11 @@ for e in json.load(sys.stdin):
print(' '.join(f'{k}={v}' for k, v in out.items() if v) or 'все нули')
"
}
# Read a single counter value; empty output means the counter is zero.
metric() { stats | tr ' ' '\n' | sed -n "s/^$1=//p" | head -1; }
mono_ns_hex() { # монотонные ns + $1 секунд, 8 байт little-endian
# Monotonic now + $1 seconds as 8 little-endian bytes, the kernel's ban TTL format.
mono_ns_hex() {
python3 -c "
import time
ns = time.clock_gettime_ns(time.CLOCK_MONOTONIC) + $1 * 1_000_000_000
@ -120,7 +135,7 @@ print(' '.join(f'{b:02x}' for b in ns.to_bytes(8, 'little')))
"
}
# ── Фаза 1: IPv6 ──
# Phase 1: IPv6 — traffic passes, a ban drops it, removing the ban restores it.
pass0=$(metric pass)
for _ in 1 2 3; do send6 100; done
sleep 0.5
@ -132,7 +147,7 @@ pass1=$(metric pass)
drop0=$(metric drop)
bpftool map update id "$B6" key hex $KEY6 value hex ff ff ff ff ff ff ff ff || fail "v6 map update"
for _ in 1 2 3; do send6 100; done
for _ in 1 2 3; do send6 1; done # короткая датаграмма
for _ in 1 2 3; do send6 1; done # short datagram
sleep 0.5
drop1=$(metric drop)
[ "${drop1:-0}" -ge $((${drop0:-0} + 6)) ] \
@ -148,10 +163,11 @@ pass3=$(metric pass)
&& ok "v6 после снятия бана проходит (pass $pass2 -> $pass3)" \
|| fail "v6 после снятия бана не проходит ($(stats))"
# ── Фаза 2: IPv4 (общий L4-гейт) ──
# Phase 2: IPv4 through the shared L4 gate — only short datagrams, which is
# the min_l4 regression.
drop0=$(metric drop)
bpftool map update id "$B4" key hex $KEY4 value hex ff ff ff ff ff ff ff ff || fail "v4 map update"
for _ in 1 2 3; do send4 1; done # только короткие — регрессия на min_l4
for _ in 1 2 3; do send4 1; done # short datagrams only — the min_l4 regression
sleep 0.5
drop1=$(metric drop)
[ "${drop1:-0}" -ge $((${drop0:-0} + 3)) ] \
@ -159,7 +175,8 @@ drop1=$(metric drop)
|| fail "v4 короткая датаграмма прошла мимо бана ($(stats))"
bpftool map delete id "$B4" key hex $KEY4 || fail "v4 map delete"
# ── Фаза 3: expiry бана (монотонные часы) ──
# Phase 3: ban expiry on the monotonic clock. A TTL written in epoch-ns would
# never expire against bpf_ktime_get_ns(), so this phase pins the clock choice.
TTL=2
bpftool map update id "$B6" key hex $KEY6 value hex $(mono_ns_hex $TTL) || fail "expiry update"
drop0=$(metric drop)
@ -179,5 +196,47 @@ drop3=$(metric drop); pass3=$(metric pass)
&& ok "бан истёк по монотонным часам (pass $pass2 -> $pass3, drop $drop2 -> $drop3)" \
|| fail "бан не истёк (pass $pass2 -> $pass3, drop $drop2 -> $drop3)"
[ "$PASS" = 1 ] && echo "PASS: XDP-баны (v4/v6, короткий L4, expiry) работают на живом ядре" || echo "FAIL"
[ "$PASS" = 1 ]
# Phase 4: UDP rate-limit (policy=2) for native IPv6.
# Policy is a .rodata global and libbpf freezes .rodata after load, so the
# object section is patched before loading. The offsets are pinned by the
# _Static_asserts in xdp/core/config.h: udp_policy @49, udp_hit_count @52.
POBJ="$OBJ.rl"
cp "$OBJ" "$POBJ"
if llvm-objcopy --dump-section .rodata="$POBJ.rodata" "$POBJ" \
&& python3 - "$POBJ.rodata" <<'PY' \
&& llvm-objcopy --update-section .rodata="$POBJ.rodata" "$POBJ"
import sys
path = sys.argv[1]
img = bytearray(open(path, "rb").read())
assert len(img) >= 56, f"unexpected .rodata size {len(img)}"
img[49] = 2 # G_UDP_POLICY = rate-limit
img[52:56] = (3).to_bytes(4, "little") # G_UDP_HIT_COUNT = 3 (fast trigger)
open(path, "wb").write(img)
PY
then
# Reload with the patched policy; map ids are invalidated by the reload.
ip link set "$DEV0" xdp off 2>/dev/null
rm -f "$PIN"
if bpftool prog load "$POBJ" "$PIN" type xdp \
&& bpftool net attach xdp pinned "$PIN" dev "$DEV0"; then
if discover_maps; then
rl0=$(metric rate); pass0=$(metric pass)
for _ in 1 2 3 4 5 6; do send6 100; done
sleep 0.3
rl1=$(metric rate); pass1=$(metric pass)
[ "${rl1:-0}" -gt "${rl0:-0}" ] && [ "${pass1:-0}" -gt "${pass0:-0}" ] \
&& ok "v6 UDP rate-limit policy=2 (rate $rl0 -> $rl1, pass $pass0 -> $pass1)" \
|| fail "v6 UDP rate-limit не срабатывает ($(stats))"
else
fail "карты не найдены после перезагрузки с policy=2"
fi
else
fail "объект с policy=2 не загрузился"
fi
rm -f "$POBJ" "$POBJ.rodata"
else
fail "не удалось пропатчить .rodata (policy=2)"
fi
[ "$PASS" = 1 ] && echo "PASS: XDP-баны и UDP rate-limit (v4/v6) работают на живом ядре" || echo "FAIL"
[ "$PASS" = 1 ]