Debian Codegraph @

test/rist/test_rtt_mute_bonding.c

raw symbols SCIP
1 /* librist. Copyright © 2026 SipRadius LLC.
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * End-to-end test for dynamic RTT-based bonded-leg muting (?rtt-drop=).
5 *
6 * A real two-leg bonded sender streams to a receiver over 127.0.0.1. A
7 * latency spike on one leg is simulated by driving that leg's measured
8 * smoothed RTT (peer->eight_times_rtt) above the ?rtt-drop ceiling, which
9 * is exactly the signal the production evaluator (rist_sender_rtt_mute_check)
10 * consumes. We then observe, through the real sender balancer and its
11 * per-leg TX counters (stats_sender_instant.sent), that:
12 *
13 * 1. both legs share the unique payload before any spike;
14 * 2. a sustained spike on leg B mutes it: leg A carries 100% of the
15 * unique payload (leg B's data TX freezes, trickle=0) and the stream
16 * keeps flowing;
17 * 3. once leg B's RTT recovers it rejoins the bond and carries data again;
18 * 4. when BOTH legs are over the ceiling the last-healthy-leg guard keeps
19 * one of them in the bond (never a full black-out).
20 *
21 * Runs anywhere the public library + loopback do; no root, no netem.
22 *
23 * Exit codes:
24 * 0 - mute, reroute, recovery and last-leg guard all behaved
25 * 1 - a behavioural assertion failed
26 * 2 - INDETERMINATE: bonding never established / no data flowed
27 * 99 - setup error
28 */
29
30 #include "librist/librist.h"
31 #include "rist-private.h"
32
33 #include <inttypes.h>
34 #include <stdbool.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #ifdef _WIN32
40 #include <windows.h>
41 #define msleep(ms) Sleep(ms)
42 #else
43 #include <unistd.h>
44 #define msleep(ms) usleep((ms) * 1000)
45 #endif
46
47 #ifndef RIST_CLOCK
48 #define RIST_CLOCK (4294967LL)
49 #endif
50
51 #define LEG_A_PORT 20141
52 #define LEG_B_PORT 20142
53 #define RTT_DROP_MS 200
54 #define RTT_RESTORE_MS 100
55 #define RTT_SETTLE_MS 1000
56
57 /* smoothed RTT (ms) -> peer->eight_times_rtt (8x, in RIST_CLOCK ticks) */
58 #define MS_TO_RTT8(ms) ((uint64_t)(ms) * (uint64_t)RIST_CLOCK * 8ULL)
59
60 static struct rist_logging_settings *log_settings = NULL;
61 static volatile int rx_pkts = 0;
62 static volatile bool sender_run = true;
63
64 static int log_cb(void *arg, enum rist_log_level level, const char *msg) {
65 (void)arg;
66 const char *tag = level <= RIST_LOG_ERROR ? "E" :
67 level <= RIST_LOG_WARN ? "W" :
68 level <= RIST_LOG_INFO ? "I" : "D";
69 fprintf(stderr, "%s| %s", tag, msg);
70 return 0;
71 }
72
73 static int rx_data_cb(void *arg, struct rist_data_block *b) {
74 (void)arg;
75 rx_pkts++;
76 rist_receiver_data_block_free2(&b);
77 return 0;
78 }
79
80 static PTHREAD_START_FUNC(sender_feed, arg) {
81 struct rist_ctx *tx = arg;
82 uint32_t counter = 0;
83 uint8_t buf[256];
84 memset(buf, 0, sizeof(buf));
85 while (sender_run) {
86 struct rist_data_block blk;
87 memset(&blk, 0, sizeof(blk));
88 memcpy(buf, &counter, sizeof(counter));
89 blk.payload = buf;
90 blk.payload_len = sizeof(buf);
91 blk.virt_src_port = 1968;
92 rist_sender_data_write(tx, &blk);
93 counter++;
94 msleep(10); /* ~100 pkt/s */
95 }
96 return 0;
97 }
98
99 /* Pin one or both legs' smoothed RTT to rtt_ms for hold_ms, re-writing under
100 * peerlist_lock frequently so the evaluator's 1 Hz sample always sees it (real
101 * loopback echo responses would otherwise decay it back toward zero). */
102 static void pin_rtt(struct rist_peer *a, struct rist_peer *b, int rtt_ms, int hold_ms) {
103 struct rist_common_ctx *cctx = &a->sender_ctx->common;
104 for (int elapsed = 0; elapsed < hold_ms; elapsed += 50) {
105 pthread_mutex_lock(&cctx->peerlist_lock);
106 if (a) a->eight_times_rtt = MS_TO_RTT8(rtt_ms);
107 if (b) b->eight_times_rtt = MS_TO_RTT8(rtt_ms);
108 pthread_mutex_unlock(&cctx->peerlist_lock);
109 msleep(50);
110 }
111 }
112
113 static struct rist_peer *add_leg(struct rist_ctx *tx, int dst_port) {
114 char url[320];
115 snprintf(url, sizeof(url),
116 "rist://127.0.0.1:%d?weight=5&rtt-drop=%d&rtt-restore=%d"
117 "&rtt-drop-settle=%d&rtt-drop-trickle=0"
118 "&session-timeout=30000&keepalive-interval=1000",
119 dst_port, RTT_DROP_MS, RTT_RESTORE_MS, RTT_SETTLE_MS);
120 struct rist_peer_config *pcfg = NULL;
121 if (rist_parse_address2(url, (void *)&pcfg) != 0)
122 return NULL;
123 struct rist_peer *peer = NULL;
124 if (rist_peer_create(tx, &peer, pcfg) != 0) { free(pcfg); return NULL; }
125 free(pcfg);
126 return peer;
127 }
128
129 static struct rist_peer *add_listen(struct rist_ctx *rx, int port) {
130 char url[320];
131 snprintf(url, sizeof(url),
132 "rist://@127.0.0.1:%d?session-timeout=30000&keepalive-interval=1000",
133 port);
134 struct rist_peer_config *pcfg = NULL;
135 if (rist_parse_address2(url, (void *)&pcfg) != 0)
136 return NULL;
137 struct rist_peer *peer = NULL;
138 if (rist_peer_create(rx, &peer, pcfg) != 0) { free(pcfg); return NULL; }
139 free(pcfg);
140 return peer;
141 }
142
143 int main(void) {
144 if (rist_logging_set(&log_settings, RIST_LOG_WARN, log_cb, NULL, NULL, stderr) != 0)
145 return 99;
146
147 /* receiver: one ctx, two bonded listening legs (same flow). */
148 struct rist_ctx *rx = NULL;
149 if (rist_receiver_create(&rx, RIST_PROFILE_MAIN, log_settings) != 0) return 99;
150 if (!add_listen(rx, LEG_A_PORT) || !add_listen(rx, LEG_B_PORT)) {
151 rist_destroy(rx); return 99;
152 }
153 if (rist_receiver_data_callback_set2(rx, rx_data_cb, NULL) != 0 ||
154 rist_start(rx) != 0) { rist_destroy(rx); return 99; }
155
156 /* sender: caller, two bonded legs to the two receiver ports. */
157 struct rist_ctx *tx = NULL;
158 if (rist_sender_create(&tx, RIST_PROFILE_MAIN, 0, log_settings) != 0) {
159 rist_destroy(rx); return 99;
160 }
161 struct rist_peer *leg_a = add_leg(tx, LEG_A_PORT);
162 struct rist_peer *leg_b = add_leg(tx, LEG_B_PORT);
163 if (!leg_a || !leg_b) { rist_destroy(tx); rist_destroy(rx); return 99; }
164 if (rist_start(tx) != 0) { rist_destroy(tx); rist_destroy(rx); return 99; }
165
166 pthread_t feeder;
167 pthread_create(&feeder, NULL, sender_feed, tx);
168
169 /* warm up: both legs authenticate and start carrying data. */
170 msleep(4000);
171 uint64_t base_a = leg_a->stats_sender_instant.sent;
172 uint64_t base_b = leg_b->stats_sender_instant.sent;
173 int rx_warm = rx_pkts;
174 fprintf(stderr, "warmup: leg_a.sent=%"PRIu64" leg_b.sent=%"PRIu64" rx=%d\n",
175 base_a, base_b, rx_warm);
176 fprintf(stderr, "diag: leg_b cfg rtt_drop=%u restore=%u settle=%u trickle=%u | "
177 "is_data=%d listening=%d parent=%p auth=%d e8rtt=%"PRIu64" (~%"PRIu64"ms)\n",
178 leg_b->config.rtt_drop, leg_b->config.rtt_restore,
179 leg_b->config.rtt_drop_settle, leg_b->config.rtt_drop_trickle,
180 leg_b->is_data, leg_b->listening, (void *)leg_b->parent,
181 leg_b->authenticated, leg_b->eight_times_rtt,
182 (uint64_t)(leg_b->eight_times_rtt / 8 / RIST_CLOCK));
183
184 /* == Phase 1: spike leg B over the ceiling; it must mute == */
185 fprintf(stderr, "\n== phase 1: leg B RTT spike (%dms > %dms ceiling) ==\n",
186 400, RTT_DROP_MS);
187 int waited = 0;
188 while (!leg_b->rtt_muted && waited < 9000) {
189 pin_rtt(leg_b, NULL, 400, 500);
190 waited += 500;
191 }
192 bool muted_b = leg_b->rtt_muted;
193 uint32_t mute_events_b = leg_b->rtt_mute_count;
194 fprintf(stderr, "leg B muted=%d mute_events=%u after %dms of spike\n",
195 muted_b, mute_events_b, waited);
196 /* Measure the freeze AFTER the mute engages. With trickle disabled on this
197 * leg, a muted leg must carry neither new unique payload (diverted by the
198 * balancer) nor retransmissions (rerouted to a healthy sibling), so its
199 * total send rate collapses while healthy leg A keeps the stream flowing. */
200 uint64_t a0 = leg_a->stats_sender_instant.sent;
201 uint64_t b0 = leg_b->stats_sender_instant.sent;
202 uint32_t b0_retx = leg_b->stats_sender_instant.retrans;
203 int rx0 = rx_pkts;
204 pin_rtt(leg_b, NULL, 400, 3000); /* keep it muted */
205 uint64_t a1 = leg_a->stats_sender_instant.sent;
206 uint64_t b1 = leg_b->stats_sender_instant.sent;
207 uint32_t b1_retx = leg_b->stats_sender_instant.retrans;
208 int rx1 = rx_pkts;
209 uint64_t b_sent = b1 - b0;
210 uint32_t b_retx = b1_retx - b0_retx;
211 fprintf(stderr, "while muted: dA=%"PRIu64" dB=%"PRIu64" (retx=%u) drx=%d\n",
212 a1 - a0, b_sent, b_retx, rx1 - rx0);
213
214 /* == Phase 2: leg B recovers; it must rejoin the bond == */
215 fprintf(stderr, "\n== phase 2: leg B RTT recovers ==\n");
216 waited = 0;
217 while (leg_b->rtt_muted && waited < 9000) {
218 pin_rtt(leg_b, NULL, 5, 500);
219 waited += 500;
220 }
221 bool restored_b = !leg_b->rtt_muted;
222 fprintf(stderr, "leg B muted=%d after %dms of recovery\n",
223 leg_b->rtt_muted, waited);
224 /* Measure that leg B carries data again after rejoining. */
225 uint64_t b1r = leg_b->stats_sender_instant.sent;
226 int rx1r = rx_pkts;
227 pin_rtt(leg_b, NULL, 5, 2500);
228 uint64_t b2 = leg_b->stats_sender_instant.sent;
229 int rx2 = rx_pkts;
230 fprintf(stderr, "after rejoin: dB=%"PRIu64" drx=%d\n", b2 - b1r, rx2 - rx1r);
231
232 /* == Phase 3: both legs over the ceiling; guard keeps one active == */
233 fprintf(stderr, "\n== phase 3: both legs over ceiling (last-leg guard) ==\n");
234 pin_rtt(leg_a, leg_b, 400, 7000);
235 bool a_muted = leg_a->rtt_muted;
236 bool b_muted = leg_b->rtt_muted;
237 fprintf(stderr, "both-high: leg_a muted=%d leg_b muted=%d\n", a_muted, b_muted);
238
239 /* == Phase 4: trickle keeps a muted leg measurable (default behaviour) ==
240 * Re-mute leg B (leg A recovers naturally and stays active) with a 1-in-10
241 * trickle enabled: the muted leg must carry a low rate of redundant
242 * duplicate packets so RTT stays sampled for a warm restore. */
243 fprintf(stderr, "\n== phase 4: trickle on a muted leg (1-in-%d) ==\n", 10);
244 struct rist_common_ctx *cctx = &leg_b->sender_ctx->common;
245 pthread_mutex_lock(&cctx->peerlist_lock);
246 leg_b->config.rtt_drop_trickle = 10;
247 leg_b->rtt_trickle_counter = 0;
248 pthread_mutex_unlock(&cctx->peerlist_lock);
249 waited = 0;
250 while (!leg_b->rtt_muted && waited < 9000) {
251 pin_rtt(leg_b, NULL, 400, 500);
252 waited += 500;
253 }
254 bool trickle_muted = leg_b->rtt_muted;
255 uint64_t tb0 = leg_b->stats_sender_instant.sent;
256 uint32_t tb0_retx = leg_b->stats_sender_instant.retrans;
257 uint64_t ta0 = leg_a->stats_sender_instant.sent;
258 pin_rtt(leg_b, NULL, 400, 3000);
259 uint64_t tb1 = leg_b->stats_sender_instant.sent;
260 uint32_t tb1_retx = leg_b->stats_sender_instant.retrans;
261 uint64_t ta1 = leg_a->stats_sender_instant.sent;
262 uint64_t trickle_new = (tb1 - tb0) - (uint64_t)(tb1_retx - tb0_retx);
263 uint64_t healthy_new = ta1 - ta0;
264 fprintf(stderr, "trickle: leg_b muted=%d trickle_dups=%"PRIu64" vs leg_a=%"PRIu64"\n",
265 trickle_muted, trickle_new, healthy_new);
266
267 sender_run = false;
268 pthread_join(feeder, NULL);
269 rist_destroy(tx);
270 rist_destroy(rx);
271 free(log_settings);
272
273 /* ---- verdicts ---- */
274 if (base_a == 0 || base_b == 0 || rx_warm == 0) {
275 fprintf(stderr, "INDETERMINATE: bonding never established or no data "
276 "(leg_a.sent=%"PRIu64" leg_b.sent=%"PRIu64" rx=%d).\n",
277 base_a, base_b, rx_warm);
278 return 2;
279 }
280 if (!muted_b) {
281 fprintf(stderr, "FAIL: leg B stayed in the bond despite a sustained "
282 "RTT spike over the ceiling.\n");
283 return 1;
284 }
285 if (mute_events_b == 0) {
286 fprintf(stderr, "FAIL: rtt_mute_events metric did not count the mute "
287 "transition (got 0).\n");
288 return 1;
289 }
290 if ((a1 - a0) == 0 || (rx1 - rx0) == 0) {
291 fprintf(stderr, "FAIL: stream did not keep flowing on healthy leg A "
292 "while B was muted (dA=%"PRIu64", drx=%d).\n",
293 a1 - a0, rx1 - rx0);
294 return 1;
295 }
296 /* With trickle disabled, a muted leg must go essentially silent: the
297 * balancer diverts new payload and retransmissions are rerouted to the
298 * healthy leg. A couple of packets can straddle the mute-transition
299 * boundary, so require B's total send rate to collapse to under 10% of
300 * what leg A carries. */
301 if (b_sent * 10 >= (a1 - a0)) {
302 fprintf(stderr, "FAIL: muted leg B still carried significant traffic "
303 "(B=%"PRIu64" retx=%u vs A=%"PRIu64"; mute/reroute did "
304 "not take effect).\n", b_sent, b_retx, a1 - a0);
305 return 1;
306 }
307 if (!restored_b) {
308 fprintf(stderr, "FAIL: leg B did not rejoin the bond after RTT "
309 "recovered.\n");
310 return 1;
311 }
312 if ((b2 - b1r) == 0) {
313 fprintf(stderr, "FAIL: restored leg B carried no payload after "
314 "rejoining (dB=%"PRIu64").\n", b2 - b1r);
315 return 1;
316 }
317 if (a_muted && b_muted) {
318 fprintf(stderr, "FAIL: last-healthy-leg guard failed; both legs muted "
319 "with every leg over the ceiling (stream black-out).\n");
320 return 1;
321 }
322 if (!trickle_muted) {
323 fprintf(stderr, "FAIL: leg B did not re-mute for the trickle phase.\n");
324 return 1;
325 }
326 if (trickle_new == 0) {
327 fprintf(stderr, "FAIL: trickle sent no redundant packets on the muted "
328 "leg; warm-restore probing is not working.\n");
329 return 1;
330 }
331 if (trickle_new * 2 >= healthy_new) {
332 fprintf(stderr, "FAIL: trickle rate not sparse (dups=%"PRIu64" vs "
333 "healthy=%"PRIu64"); a muted leg must only carry a low "
334 "redundant rate.\n", trickle_new, healthy_new);
335 return 1;
336 }
337
338 fprintf(stderr, "\nPASS: bonded leg muted on RTT spike (payload moved to the "
339 "healthy leg, stream never stalled), rejoined on recovery, and "
340 "the last-healthy-leg guard kept one leg active when both were "
341 "over the ceiling.\n");
342 return 0;
343 }