guard/scripts/xdp_netns_smoke.sh
loki5512344 a8a3057719
feat(xdp): IPv6 UDP rate limit, working loader; English comments and docs
XDP behaviour:
- udp_rate_limit6 (LRU, full 128-bit source key) plus throttle_hit6, so
  udp_policy = "rate-limit" 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).

Loader (XDP could never load before this):
- .rodata is one struct rampart_config with frozen offsets enforced by
  _Static_assert; XdpGlobals is #[repr(C)] and the image is built with
  offset_of!. Before, 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 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 checks map names and .rodata size without root.

Docs and style:
- Code comments are English, JavaDoc/Rustdoc style, without emoji or
  banners (new project rule, TODO §0.8).
- README rewritten bilingual: Russian first, English below, nav buttons,
  compact 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 covers the real-kernel smoke; stale links removed.
2026-11-15 23:50:52 +01:00

194 lines
6.7 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# netns + veth smoke test of the XDP path on a real kernel (finding #7 of the
# concurrency analysis).
#
# Covers what cannot be checked in a CI container without CAP_BPF:
# 1. the verifier accepts universal_filter.o (loaded through libbpf);
# 2. attach on a live interface inside a netns, native mode;
# 3. legitimate HTTP is proxied;
# 4. a SYN flood produces ringbuf events, so opt-in auto-ban works;
# 5. traffic recovers once the ban expires;
# 6. after SIGTERM the XDP program is really detached (regression on the
# fd=-1 bug).
#
# Run: on a host with root/CAP_BPF (VDS or a VM CI runner), not in a container:
# BIN=./target/release/rampart bash scripts/xdp_netns_smoke.sh
#
# Requires: iproute2, bpftool, hping3, python3 (http.server), curl.
set -u
NS=rampart_smoke
SUBNET=10.244.251
HOST_IP=$SUBNET.1
EDGE_IP=$SUBNET.2
EDGE_PORT=9000
ORIGIN_PORT=9100
METRICS_PORT=9595
BIN=${BIN:-./target/release/rampart}
WORK=$(mktemp -d)
RAMPID=""
SRVPID=""
PASS=1
fail() { echo "FAIL: $1"; PASS=0; }
ok() { echo "ok: $1"; }
# Kill both processes and drop the namespace, whatever the exit path was.
cleanup() {
[ -n "$RAMPID" ] && kill -9 "$RAMPID" 2>/dev/null
[ -n "$SRVPID" ] && kill -9 "$SRVPID" 2>/dev/null
ip netns del "$NS" 2>/dev/null
rm -rf "$WORK"
}
trap cleanup EXIT
[ "$(id -u)" = 0 ] || { echo "нужен root (CAP_BPF+CAP_NET_ADMIN)"; exit 2; }
[ -x "$BIN" ] || { echo "бинарь не найден: $BIN"; exit 2; }
# Phase 1: netns plus veth, so the whole test is isolated and production
# traffic is never touched.
ip netns del "$NS" 2>/dev/null
ip netns add "$NS" || { echo "не удалось создать netns"; exit 2; }
ip link add rth0 type veth peer name rtap
ip link set rtap netns "$NS"
ip addr add "$HOST_IP/24" dev rth0; ip link set rth0 up
ip -n "$NS" addr add "$EDGE_IP/24" dev rtap
ip -n "$NS" link set rtap up
ip -n "$NS" link set lo up
ok "netns $NS + veth подняты"
# Phase 2: edge config. The protected range is a single port and auto-ban is
# on, otherwise there is nothing to observe.
cat > "$WORK/edge.toml" <<EOF
[bind]
address = "$EDGE_IP"
port = $EDGE_PORT
[backend]
upstreams = ["$HOST_IP:$ORIGIN_PORT"]
[limits]
handshake_timeout_secs = 5
[xdp]
enabled = true
interface = "rtap"
protected_port_start = $EDGE_PORT
protected_port_end = $EDGE_PORT
events_enabled = true
throttle_enabled = true
auto_ban_enabled = true
auto_ban_duration_secs = 5
auto_ban_conn_drop_fails = 3
[store]
redis_url = ""
blacklist_cache_ttl_secs = 300
clickhouse_url = ""
[metrics]
enabled = true
port = $METRICS_PORT
[logging]
level = "info"
format = "text"
[pow]
enabled = false
EOF
# Phase 3: origin server on the host half of the veth pair.
echo "<html><body>rampart-smoke-origin</body></html>" > "$WORK/index.html"
python3 -m http.server "$ORIGIN_PORT" --bind "$HOST_IP" -d "$WORK" >/dev/null 2>&1 &
SRVPID=$!
sleep 0.5
# Phase 4: start edge inside the namespace and wait for the attach log line.
RUST_LOG=rampart=debug RAMPART_CONFIG="$WORK/edge.toml" \
ip netns exec "$NS" "$BIN" > "$WORK/rampart.log" 2>&1 &
RAMPID=$!
ATTACHED=no
for _ in $(seq 60); do
grep -q "XDP filter attached" "$WORK/rampart.log" && { ATTACHED=yes; break; }
kill -0 "$RAMPID" 2>/dev/null || break
sleep 0.5
done
if [ "$ATTACHED" = yes ]; then
ok "XDP attached (verifier принят ядром $(uname -r))"
else
fail "XDP не аттачнулся; лог:"; sed -n '1,30p' "$WORK/rampart.log"; exit 1
fi
# Attach mode: native is expected on veth, but generic is a normal fallback in
# the load() ladder, so compare against the mode rampart actually reports.
if ip -n "$NS" link show rtap | grep -qE "xdp( |$)"; then
ok "attach native (ip link: xdp)"
elif ip -n "$NS" link show rtap | grep -q "xdpgeneric"; then
echo "warn: attach в generic (skb) — veth обычно тянет native, проверь лог"
else
fail "на rtap нет XDP-программы после 'attached'"
fi
grep -oE "mode: [a-z ()]+" "$WORK/rampart.log" | tail -1
# Phase 5: a legitimate HTTP request must reach the origin through the proxy.
BODY=$(curl -s --max-time 5 "http://$EDGE_IP:$EDGE_PORT/" || true)
echo "$BODY" | grep -q "rampart-smoke-origin" \
&& ok "легитимный HTTP проксируется" \
|| fail "легитимный HTTP не дошёл до origin"
# Phase 6: SYN flood, then throttle, ringbuf events and auto-ban.
hping3 -S -p "$EDGE_PORT" -c 4000 -i u200 "$EDGE_IP" >/dev/null 2>&1 || true
sleep 3
grep -q "xdp event" "$WORK/rampart.log" \
&& ok "ringbuf-события долетают до userspace" \
|| fail "ни одного 'xdp event' в логе"
if grep -q "xdp auto-ban issued" "$WORK/rampart.log"; then
ok "auto-ban выдан по событию"
else
fail "auto-ban не сработал"
fi
BANS=$(ip netns exec "$NS" curl -s --max-time 2 "http://127.0.0.1:$METRICS_PORT/metrics" \
| awk '/^rampart_xdp_autobans_total /{print $2}')
[ "${BANS:-0}" != "0" ] && ok "метрика rampart_xdp_autobans_total=$BANS" \
|| fail "метрика autobans пуста"
# A banned source is refused while the ban is active.
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --max-time 2 "http://$EDGE_IP:$EDGE_PORT/" || echo "err")
[ "$HTTP_CODE" = "err" ] || [ "$HTTP_CODE" = "000" ] \
&& ok "забаненный клиент отклонён (http=$HTTP_CODE)" \
|| fail "забаненный клиент получил ответ ($HTTP_CODE)"
# Phase 7: expiry. After 7s (ban is 5s) traffic must come back on its own.
sleep 7
BODY=$(curl -s --max-time 5 "http://$EDGE_IP:$EDGE_PORT/" || true)
echo "$BODY" | grep -q "rampart-smoke-origin" \
&& ok "бан истёк, легитимный трафик восстановлен" \
|| fail "после expiry трафик не восстановился"
# Phase 8: graceful stop must leave no program behind (regression on
# borrow_raw(-1) during detach).
kill -TERM "$RAMPID"
wait "$RAMPID"; RC=$?
RAMPID=""
[ "$RC" = 0 ] && ok "rampart завершился с кодом 0" || fail "код выхода rampart: $RC"
grep -q "XDP filter detached" "$WORK/rampart.log" \
&& ok "лог: detach выполнен" || fail "в логе нет строки detach"
sleep 1
if ip -n "$NS" link show rtap | grep -q "xdp"; then
fail "XDP ПРОГРАММА ОСТАЛАСЬ НА ИНТЕРФЕЙСЕ ПОСЛЕ ВЫХОДА ПРОЦЕССА (detach сломан)"
else
ok "XDP-программа реально отвязана после SIGTERM"
fi
# Summary: exit code drives CI, the log excerpt makes a failure diagnosable.
echo
if [ "$PASS" = 1 ]; then
echo "PASS — xdp netns smoke ($(uname -r))"
exit 0
else
echo "FAIL — см. вывод выше; полный лог edge:"
tail -40 "$WORK/rampart.log"
exit 1
fi