Debian Codegraph @

test/rist/test_bonded_carrier_never_starved_netns.sh

raw symbols SCIP
1 #!/bin/bash
2 # librist. Copyright © 2026 SipRadius LLC.
3 # SPDX-License-Identifier: BSD-2-Clause
4 #
5 # Regression for the RTT-mute / stall "no carrier" starvation. The sender pulls
6 # a leg from the payload rotation for two independent reasons: RTT-mute (leg RTT
7 # over the ceiling) and stall (leg gone silent on its return path). Each check
8 # keeps at least one leg out of its OWN mechanism, but they run independently:
9 # one leg muted for high RTT PLUS its only sibling stalled for silence leaves
10 # the balancer with zero carriers, and it then drops the payload. The receiver
11 # starves on both legs at once even though one leg is still perfectly able to
12 # deliver (just late).
13 #
14 # This drives exactly that split on a two-leg bonded caller, in sequence so the
15 # mute genuinely fires before the stall:
16 # 1. leg A (ca): +300 ms each way -> smoothed RTT ~600 ms > rtt-drop=400, and
17 # with leg B still healthy it is actually MUTED (its share moves to B).
18 # 2. leg B (cb): 100% loss both ways -> silent on its return path -> STALLED.
19 # Now A is muted and B is stalled: the balancer has no carrier.
20 # The buffer is large (6 s) so the liveness timeout (~12 s) does not tear leg B
21 # down during the window: it stays authenticated-but-stalled, holding the split.
22 #
23 # Before the fix both legs were skipped and the receiver's per-interval
24 # "received" collapsed to ~0 (only the muted leg's 1-in-100 trickle). After it,
25 # the muted leg is kept as the sole carrier and data keeps flowing, late but
26 # continuous.
27 #
28 # Needs real interfaces + tc, so it runs only on Linux as root with netns/tc and
29 # SKIPs (77) otherwise.
30 #
31 # Usage: test_bonded_carrier_never_starved_netns.sh <ristsender> <ristreceiver>
32 # Exit: 0 = a carrier survived the muted+stalled split (fixed)
33 # 1 = receiver starved on both legs at once (regression)
34 # 77 = SKIP (needs Linux root + netns + tc)
35 # 99 = setup error / INDETERMINATE (the split did not form)
36 set -u
37
38 TX="${1:-}"
39 RX="${2:-}"
40 USER=carrieruser; PASS=carrierpass; CN=testcn
41 RXIP=10.0.2.61
42 NS=rist_carrier_snd
43 RXLOG="$(mktemp)"; TXLOG="$(mktemp)"
44
45 skip() { echo "SKIP: $*"; cleanup 2>/dev/null; exit 77; }
46 setup_err() { echo "SETUP ERROR: $*"; cleanup 2>/dev/null; exit 99; }
47
48 cleanup() {
49 ip netns del "$NS" 2>/dev/null
50 ip link del veth-ca 2>/dev/null
51 ip link del veth-cb 2>/dev/null
52 ip link del cbr0 2>/dev/null
53 [ -n "${TXPID:-}" ] && kill "$TXPID" 2>/dev/null
54 [ -n "${RXPID:-}" ] && kill "$RXPID" 2>/dev/null
55 [ -n "${FEEDPID:-}" ] && kill "$FEEDPID" 2>/dev/null
56 rm -f "$RXLOG" "$TXLOG" 2>/dev/null
57 }
58 trap cleanup EXIT
59
60 # ---- environment gate: skip cleanly where we cannot reproduce ----------
61 [ "$(uname -s)" = "Linux" ] || skip "not Linux"
62 [ "$(id -u)" = "0" ] || skip "needs root (CAP_NET_ADMIN) for netns/veth/tc"
63 [ -n "$TX" ] && [ -x "$TX" ] || setup_err "ristsender not found ($TX)"
64 [ -n "$RX" ] && [ -x "$RX" ] || setup_err "ristreceiver not found ($RX)"
65 command -v ip >/dev/null 2>&1 || skip "iproute2 'ip' missing"
66 command -v tc >/dev/null 2>&1 || skip "iproute2 'tc' missing"
67 ip netns add "$NS" 2>/dev/null || skip "cannot create netns (no CAP_NET_ADMIN?)"
68
69 # ---- topology: bridge in root ns, two veth legs into a sender netns -----
70 ip link add cbr0 type bridge || setup_err "bridge add"
71 ip addr add ${RXIP}/24 dev cbr0 || setup_err "bridge addr"
72 ip link set cbr0 up || setup_err "bridge up"
73
74 ip link add veth-ca type veth peer name ca || setup_err "veth-ca"
75 ip link add veth-cb type veth peer name cb || setup_err "veth-cb"
76 ip link set veth-ca master cbr0; ip link set veth-ca up
77 ip link set veth-cb master cbr0; ip link set veth-cb up
78 ip link set ca netns "$NS"; ip link set cb netns "$NS"
79 ip netns exec "$NS" ip addr add 10.0.2.30/24 dev ca
80 ip netns exec "$NS" ip addr add 10.0.2.131/24 dev cb
81 ip netns exec "$NS" ip link set ca up
82 ip netns exec "$NS" ip link set cb up
83 ip netns exec "$NS" ip link set lo up
84 # same-subnet multi-homing: disable rp_filter so miface egress is kept
85 ip netns exec "$NS" sysctl -qw net.ipv4.conf.all.rp_filter=0
86 ip netns exec "$NS" sysctl -qw net.ipv4.conf.ca.rp_filter=0
87 ip netns exec "$NS" sysctl -qw net.ipv4.conf.cb.rp_filter=0
88 sysctl -qw net.ipv4.conf.all.rp_filter=0 >/dev/null 2>&1
89
90 ip netns exec "$NS" ping -c1 -W2 -I ca ${RXIP} >/dev/null 2>&1 || skip "leg A no connectivity"
91 ip netns exec "$NS" ping -c1 -W2 -I cb ${RXIP} >/dev/null 2>&1 || skip "leg B no connectivity"
92
93 # ---- receiver: listener, advanced profile, SRP authenticator -----------
94 # 6 s buffer keeps the liveness timeout (~2x buffer) well above the ~8 s
95 # impairment, so leg B stays authenticated-but-stalled. -S 500 gives fine
96 # stats sampling for the received-per-interval measurement below.
97 $RX -p 2 -v 6 -S 500 \
98 -i "rist://@${RXIP}:2041?username=${USER}&password=${PASS}&buffer=6000" \
99 -o "udp://127.0.0.1:12356" >"$RXLOG" 2>&1 &
100 RXPID=$!
101 sleep 1
102
103 # udp feeder inside the sender netns -> ristsender input (~50 pkt/s)
104 ip netns exec "$NS" bash -c 'while :; do printf "RISTTESTPACKET%08d" $RANDOM > /dev/udp/127.0.0.1/5567; sleep 0.02; done' &
105 FEEDPID=$!
106 sleep 0.5
107
108 # ---- sender: bonded caller, two SRP legs with RTT auto-mute configured --
109 RTTQS="rtt-drop=400&rtt-restore=200&rtt-drop-settle=1000&rtt-drop-trickle=100"
110 ip netns exec "$NS" $TX -p 2 -v 6 \
111 -i "udp://@127.0.0.1:5567" \
112 -o "rist://${RXIP}:2041?miface=ca&weight=5&username=${USER}&password=${PASS}&cname=${CN}&buffer=6000&${RTTQS},rist://${RXIP}:2041?miface=cb&weight=5&username=${USER}&password=${PASS}&cname=${CN}&buffer=6000&${RTTQS}" \
113 >"$TXLOG" 2>&1 &
114 TXPID=$!
115
116 echo "== warmup 12s (both legs authenticate + stream) =="
117 sleep 12
118 auths_setup=$(grep -c "Successfully authenticated" "$RXLOG")
119 echo "after warmup: successful auths=${auths_setup}"
120 if [ "$auths_setup" -lt 2 ]; then
121 echo "INDETERMINATE: both legs did not authenticate before the impairment."
122 exit 99
123 fi
124
125 echo "== step 1: leg A (ca) +300ms each way -> RTT over ceiling, mutes (B healthy) =="
126 ip netns exec "$NS" tc qdisc add dev ca root netem delay 300ms || setup_err "tc add ca"
127 tc qdisc add dev veth-ca root netem delay 300ms || setup_err "tc add veth-ca"
128 sleep 4 # RTT ramp + drop-settle: leg A actually mutes while B still carries
129
130 echo "== step 2: leg B (cb) 100% loss both ways -> stalls; A must be kept carrier =="
131 ip netns exec "$NS" tc qdisc add dev cb root netem loss 100% || setup_err "tc add cb"
132 tc qdisc add dev veth-cb root netem loss 100% || setup_err "tc add veth-cb"
133
134 echo "== let the split form (3s) =="
135 sleep 3
136 rx_off=$(wc -l < "$RXLOG")
137
138 echo "== measure received-per-interval for 6s while muted+stalled =="
139 sleep 6
140
141 # ---- teardown impairment; observe recovery briefly ----------------------
142 ip netns exec "$NS" tc qdisc del dev ca root 2>/dev/null
143 tc qdisc del dev veth-ca root 2>/dev/null
144 ip netns exec "$NS" tc qdisc del dev cb root 2>/dev/null
145 tc qdisc del dev veth-cb root 2>/dev/null
146
147 # ---- INDETERMINATE guard: the muted+stalled split must actually form -----
148 muted=$(grep -c "over .*ceiling" "$TXLOG")
149 stalled=$(grep -c "stalled: silent" "$TXLOG")
150 echo "== split check: mute events=${muted}, stall events=${stalled} =="
151 if [ "$muted" -eq 0 ] || [ "$stalled" -eq 0 ]; then
152 echo "INDETERMINATE: impairment did not produce a muted+stalled split"\
153 "(mute=${muted}, stall=${stalled}); cannot judge the carrier guard."
154 exit 99
155 fi
156
157 # ---- verdict: with both legs pulled, did the receiver keep getting data? -
158 # Per-interval flow "received" over the measurement window. Streaming ~50 pkt/s
159 # at -S 500 is ~25/interval when a carrier survives; starvation leaves only the
160 # muted leg's 1-in-100 trickle, i.e. ~0.
161 window=$(tail -n +"$((rx_off + 1))" "$RXLOG" | grep '"receiver-stats"')
162 intervals=$(printf '%s\n' "$window" | grep -c '"received"')
163 ok=$(printf '%s\n' "$window" | grep -o '"received":[0-9]*' \
164 | grep -o '[0-9]*' | awk '$1 >= 10 {c++} END {print c+0}')
165 maxrx=$(printf '%s\n' "$window" | grep -o '"received":[0-9]*' \
166 | grep -o '[0-9]*' | sort -n | tail -1)
167 echo "== during split: ${intervals} stats intervals, ${ok} with received>=10,"\
168 "peak received/interval=${maxrx:-0} =="
169
170 if [ "${intervals:-0}" -lt 3 ]; then
171 echo "INDETERMINATE: too few receiver-stats samples in the window"\
172 "(${intervals}); cannot judge starvation."
173 exit 99
174 fi
175 if [ "${ok:-0}" -lt 3 ]; then
176 echo "FAIL: receiver starved during the muted+stalled split"\
177 "(${ok}/${intervals} intervals carried data, peak ${maxrx:-0}/interval);"\
178 "both legs were pulled and the payload was dropped."
179 exit 1
180 fi
181
182 echo "PASS: a carrier survived the muted+stalled split"\
183 "(${ok}/${intervals} intervals carried data, peak ${maxrx}/interval);"\
184 "data kept flowing on the reinstated leg instead of starving."
185 exit 0