Files
fips/testing/nat/node/entrypoint.sh
Johnathan Corgan c6609f06d9 Stop the NAT lab start from outrunning its own interface waits
The cone and symmetric starts failed now and then with
`Invalid "netns" value`, a node log ending "Timed out waiting for IPv4
on eth0" and, often, a router that had exited with an empty log. Both
entrypoints gave the harness 30 s from container start to deliver their
interface, and setup-topology.sh delivered it with ten serial helper
containers, five per pair. Starting a container is normally 0.16 s, but
while another lab starts on the same Docker daemon it stalls for 3 to
15 s at a time (measured in create and start; exec, inspect and logs
stay fast, and the kernel and disk were not the bottleneck). Ten such
starts overran the window, so the move landed on an exited container.

Reproduced before the change, with nothing injected: two NAT labs
started together on one daemon failed in 4 of 4 starts:

    Error: argument "2219471" is wrong: Invalid "netns" value
    LAB START FAILED: start_cone exited non-zero on attempt 1; ...
    fips-nat-cone-b: Timed out waiting for IPv4 on eth0
    fips-nat-router-b: (empty log)

A docker shim that delays each `docker run ... --pid=host` by 8 s also
failed both of two runs the same way. A single idle lab passes either
way.

The change:

- setup-topology.sh runs one helper container per pair (delete, add and
  both moves in one `sh -c`, each step echoed) instead of five, checks
  that both containers are still running before it, and after a failed
  move or configure step names any container that exited, with its exit
  code and last 20 log lines. wait_for_pid stops early once a container
  has exited.
- Both entrypoints wait IFACE_WAIT_SECS (default 120 s) for their
  interface, separate from WAIT_TIMEOUT_SECS (30 s), which still bounds
  the relay and STUN waits. The default is set in the scripts, not the
  compose file, so the change applies unchanged to the other branches.
- The router entrypoint now says which interface it timed out on and
  lists its links, instead of exiting silently under set -e.
- testing/nat/README.md describes the start order and the bound.

The longer wait bounds harness setup, not product behaviour; a stuck
setup still fails, naming the container.

After the change: the two-labs-together start passed 4 of 4, with
router B configured at 31 to 34 s after compose up in the symmetric
lab, past the old 30 s window, so both halves of the change are
needed. The 8 s shim passed twice, and the symmetric scenario alone
passed. Breaking the guard with a 130 s shim delay fails with
"fips-nat-cone-a exited (code 1) before its interface was moved",
followed by its log, "Timed out after 120s waiting for IPv4 on eth0".

There is no committed regression test: reproducing this needs Docker
and a second lab or a delaying shim, so it stays a captured run.
2026-10-15 21:52:38 +00:00

86 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
DATA_IF="${DATA_IF:-eth0}"
ROUTE_SUBNET="${ROUTE_SUBNET:-}"
ROUTE_VIA="${ROUTE_VIA:-}"
WAIT_TIMEOUT_SECS="${WAIT_TIMEOUT_SECS:-30}"
# The data interface is not there at start: setup-topology.sh moves it in after
# `docker compose up` returns, and that waits on container starts that have
# taken seconds each on a busy daemon. So it gets its own, longer bound; the
# relay and STUN waits below keep WAIT_TIMEOUT_SECS. Set here rather than in
# docker-compose.yml so the compose file stays as it is on every branch.
IFACE_WAIT_SECS="${IFACE_WAIT_SECS:-120}"
RELAY_HOST="${RELAY_HOST:-}"
RELAY_PORT="${RELAY_PORT:-7777}"
STUN_HOST="${STUN_HOST:-}"
STUN_PORT="${STUN_PORT:-3478}"
deadline=$((SECONDS + IFACE_WAIT_SECS))
while [ "$SECONDS" -lt "$deadline" ]; do
if ip -4 addr show dev "$DATA_IF" 2>/dev/null | grep -q 'inet '; then
break
fi
sleep 0.5
done
if ! ip -4 addr show dev "$DATA_IF" 2>/dev/null | grep -q 'inet '; then
echo "Timed out after ${IFACE_WAIT_SECS}s waiting for IPv4 on ${DATA_IF}" >&2
ip addr >&2 || true
exit 1
fi
ip link set lo up
ip link set "$DATA_IF" up
if [ -n "$ROUTE_SUBNET" ] && [ -n "$ROUTE_VIA" ]; then
ip route replace "$ROUTE_SUBNET" via "$ROUTE_VIA" dev "$DATA_IF"
fi
wait_for_tcp() {
local host="$1"
local port="$2"
local deadline=$((SECONDS + WAIT_TIMEOUT_SECS))
while [ "$SECONDS" -lt "$deadline" ]; do
if nc -z -w1 "$host" "$port" >/dev/null 2>&1; then
return 0
fi
sleep 0.5
done
return 1
}
wait_for_udp() {
local host="$1"
local port="$2"
local deadline=$((SECONDS + WAIT_TIMEOUT_SECS))
while [ "$SECONDS" -lt "$deadline" ]; do
if printf 'probe' | nc -u -w1 "$host" "$port" >/dev/null 2>&1; then
return 0
fi
sleep 0.5
done
return 1
}
if [ -n "$RELAY_HOST" ]; then
wait_for_tcp "$RELAY_HOST" "$RELAY_PORT" || {
echo "Timed out waiting for relay ${RELAY_HOST}:${RELAY_PORT}" >&2
exit 1
}
fi
if [ -n "$STUN_HOST" ]; then
wait_for_udp "$STUN_HOST" "$STUN_PORT" || {
echo "Timed out waiting for STUN ${STUN_HOST}:${STUN_PORT}" >&2
exit 1
}
fi
exec /usr/local/bin/entrypoint.sh