aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / net / tests / ctl_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* Deterministic loss/recovery and refusal tests for the M6-A control-transport |
| 18 | * stub (TEST identity only). A seeded simulated link drops, duplicates, |
| 19 | * corrupts and reorders frames; every scenario must deliver every message in |
| 20 | * order exactly once, or fail the link deterministically. */ |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include "../aienos_ctl.h" |
| 26 | #include "../../argus/sha256.h" |
| 27 | |
| 28 | static int failures, checks; |
| 29 | #define CHECK(c) do { checks++; if (!(c)) { failures++; fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); } } while (0) |
| 30 | #define EQ(a, b) CHECK((long long)(a) == (long long)(b)) |
| 31 | |
| 32 | typedef struct { uint64_t s; } prng; |
| 33 | static uint32_t rnd(prng *p) { p->s ^= p->s << 13; p->s ^= p->s >> 7; p->s ^= p->s << 17; return (uint32_t)(p->s >> 11); } |
| 34 | static int chance(prng *p, unsigned pct) { return rnd(p) % 100 < pct; } |
| 35 | |
| 36 | typedef struct { |
| 37 | unsigned drop, dup, corrupt, max_delay; /* percent, percent, percent, ticks */ |
| 38 | uint64_t blackout_from, blackout_to; /* everything dropped in [from, to) */ |
| 39 | } link_cfg; |
| 40 | static uint64_t drop_every; /* Lane 19: drop every Nth frame per direction (0 = off) */ |
| 41 | |
| 42 | #define QMAX 256 |
| 43 | typedef struct { uint64_t at; int to; size_t n; uint8_t f[CT_MAX_FRAME]; } wire_frame; |
| 44 | typedef struct { |
| 45 | wire_frame q[QMAX]; |
| 46 | size_t nq; |
| 47 | sha256_ctx transcript; |
| 48 | uint64_t sent, dropped, count[2]; |
| 49 | } wire; |
| 50 | |
| 51 | typedef struct { |
| 52 | int done, link_down_a, link_down_b; |
| 53 | uint64_t ticks; |
| 54 | uint32_t delivered[2]; |
| 55 | ct_counters ca, cb; |
| 56 | uint8_t digest[32]; |
| 57 | } result; |
| 58 | |
| 59 | #define MSGS 64 |
| 60 | static void msg_for(int from, uint32_t i, uint8_t *m, size_t *n) |
| 61 | { |
| 62 | *n = (i * 37u + (unsigned)from * 11u) % 200u + (i % 5 == 0 ? 0 : 1); |
| 63 | for (size_t k = 0; k < *n; k++) m[k] = (uint8_t)(i * 131u + k * 7u + (unsigned)from); |
| 64 | } |
| 65 | |
| 66 | static void put_on_wire(wire *w, prng *p, const link_cfg *L, uint64_t now, int to, const uint8_t *f, size_t n) |
| 67 | { |
| 68 | w->sent++; |
| 69 | sha256_update(&w->transcript, f, n); |
| 70 | w->count[to]++; |
| 71 | if ((now >= L->blackout_from && now < L->blackout_to) || chance(p, L->drop) || |
| 72 | (drop_every && w->count[to] % drop_every == 0)) { w->dropped++; return; } |
| 73 | int copies = chance(p, L->dup) ? 2 : 1; |
| 74 | for (int c = 0; c < copies && w->nq < QMAX; c++) { |
| 75 | wire_frame *x = &w->q[w->nq++]; |
| 76 | x->at = now + 1 + (L->max_delay ? rnd(p) % L->max_delay : 0); |
| 77 | x->to = to; |
| 78 | x->n = n; |
| 79 | memcpy(x->f, f, n); |
| 80 | if (chance(p, L->corrupt)) x->f[rnd(p) % n] ^= (uint8_t)(1u << (rnd(p) % 8)); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static result run(uint64_t seed, const link_cfg *L, uint32_t nmsg, uint32_t rto, uint32_t retries, uint64_t limit) |
| 85 | { |
| 86 | static ct_endpoint ep[2]; |
| 87 | static wire w; |
| 88 | ct_identity id; |
| 89 | result r; |
| 90 | memset(&r, 0, sizeof r); |
| 91 | memset(&w, 0, sizeof w); |
| 92 | sha256_init(&w.transcript); |
| 93 | prng p = {seed ? seed : 1}; |
| 94 | CHECK(ct_test_identity(&id, "lane5-test-link") == CT_OK); |
| 95 | CHECK(ct_init(&ep[0], &id, 0, 0xA1E5u, rto, retries) == CT_OK); |
| 96 | CHECK(ct_init(&ep[1], &id, 1, 0xA1E5u, rto, retries) == CT_OK); |
| 97 | uint32_t queued[2] = {0, 0}; |
| 98 | uint8_t buf[CT_MAX_FRAME], m[CT_MAX_PAYLOAD], want[CT_MAX_PAYLOAD]; |
| 99 | size_t n, wl; |
| 100 | for (uint64_t now = 0; now < limit; now++) { |
| 101 | for (int e = 0; e < 2; e++) { |
| 102 | while (queued[e] < nmsg) { |
| 103 | msg_for(e, queued[e], m, &n); |
| 104 | ct_status s = ct_send(&ep[e], m, n); |
| 105 | if (s == CT_ERR_WINDOW_FULL || s == CT_ERR_LINK_DOWN) break; |
| 106 | EQ(s, CT_OK); |
| 107 | queued[e]++; |
| 108 | } |
| 109 | for (;;) { |
| 110 | ct_status s = ct_poll_tx(&ep[e], now, buf, sizeof buf, &n); |
| 111 | if (s != CT_OK) break; |
| 112 | put_on_wire(&w, &p, L, now, 1 - e, buf, n); |
| 113 | } |
| 114 | } |
| 115 | /* deliver everything due in FIFO order; only the random delay reorders */ |
| 116 | size_t keep = 0; |
| 117 | for (size_t k = 0; k < w.nq; k++) { |
| 118 | if (w.q[k].at > now) { if (keep != k) w.q[keep] = w.q[k]; keep++; continue; } |
| 119 | const wire_frame *xp = &w.q[k]; |
| 120 | wire_frame x; x.to = xp->to; x.n = xp->n; memcpy(x.f, xp->f, xp->n); |
| 121 | ct_status s = ct_receive(&ep[x.to], x.f, x.n, m, sizeof m, &n); |
| 122 | CHECK(s == CT_OK || s == CT_DELIVERED || s == CT_ERR_BAD_TAG || s == CT_ERR_MALFORMED || |
| 123 | s == CT_ERR_REFUSED || s == CT_ERR_LINK_DOWN); |
| 124 | if (s == CT_DELIVERED) { |
| 125 | uint32_t i = r.delivered[x.to]++; |
| 126 | msg_for(1 - x.to, i, want, &wl); /* in order, exactly once, intact */ |
| 127 | CHECK(n == wl && memcmp(m, want, n) == 0); |
| 128 | } |
| 129 | } |
| 130 | w.nq = keep; |
| 131 | r.link_down_a = ep[0].link_down; |
| 132 | r.link_down_b = ep[1].link_down; |
| 133 | if (r.delivered[0] == nmsg && r.delivered[1] == nmsg && ct_in_flight(&ep[0]) == 0 && |
| 134 | ct_in_flight(&ep[1]) == 0) { r.done = 1; r.ticks = now; break; } |
| 135 | if (r.link_down_a || r.link_down_b) { r.ticks = now; break; } |
| 136 | } |
| 137 | r.ca = ep[0].c; |
| 138 | r.cb = ep[1].c; |
| 139 | sha256_final(&w.transcript, r.digest); |
| 140 | return r; |
| 141 | } |
| 142 | |
| 143 | static void hex(const uint8_t *d, char *o) { for (int i = 0; i < 32; i++) sprintf(o + 2 * i, "%02x", d[i]); } |
| 144 | |
| 145 | static void scenario(const char *name, uint64_t seed, link_cfg L, uint32_t rto, uint32_t retries, int expect_done) |
| 146 | { |
| 147 | result a = run(seed, &L, MSGS, rto, retries, 200000); |
| 148 | result b = run(seed, &L, MSGS, rto, retries, 200000); |
| 149 | CHECK(memcmp(&a, &b, sizeof a) == 0); /* bit-identical rerun */ |
| 150 | EQ(a.done, expect_done); |
| 151 | if (expect_done) { EQ(a.delivered[0], MSGS); EQ(a.delivered[1], MSGS); } |
| 152 | char h[65]; hex(a.digest, h); |
| 153 | printf(" %-22s done=%d ticks=%llu retx=%llu/%llu dup=%llu/%llu gap=%llu/%llu badtag=%llu/%llu malformed=%llu/%llu transcript=%.16s\n", |
| 154 | name, a.done, (unsigned long long)a.ticks, |
| 155 | (unsigned long long)a.ca.tx_retransmits, (unsigned long long)a.cb.tx_retransmits, |
| 156 | (unsigned long long)a.ca.rx_duplicate, (unsigned long long)a.cb.rx_duplicate, |
| 157 | (unsigned long long)a.ca.rx_gap, (unsigned long long)a.cb.rx_gap, |
| 158 | (unsigned long long)a.ca.rx_bad_tag, (unsigned long long)a.cb.rx_bad_tag, |
| 159 | (unsigned long long)a.ca.rx_malformed, (unsigned long long)a.cb.rx_malformed, h); |
| 160 | } |
| 161 | |
| 162 | static void test_loss_recovery(void) |
| 163 | { |
| 164 | scenario("clean", 1, (link_cfg){0, 0, 0, 0, 0, 0}, 4, 5, 1); |
| 165 | { /* a clean FIFO link needs no retransmission and sees no gap */ |
| 166 | link_cfg L = {0, 0, 0, 0, 0, 0}; |
| 167 | result c = run(1, &L, MSGS, 4, 5, 100000); |
| 168 | EQ(c.ca.tx_retransmits + c.cb.tx_retransmits, 0); |
| 169 | EQ(c.ca.rx_gap + c.cb.rx_gap + c.ca.rx_duplicate + c.cb.rx_duplicate, 0); |
| 170 | } |
| 171 | scenario("loss30", 2, (link_cfg){30, 0, 0, 0, 0, 0}, 4, 40, 1); |
| 172 | scenario("loss+dup+reorder", 3, (link_cfg){20, 15, 0, 6, 0, 0}, 8, 40, 1); |
| 173 | scenario("corrupt20", 4, (link_cfg){0, 0, 20, 2, 0, 0}, 4, 40, 1); |
| 174 | scenario("everything", 5, (link_cfg){25, 10, 10, 5, 0, 0}, 8, 60, 1); |
| 175 | scenario("blackout-recover", 6, (link_cfg){5, 0, 0, 2, 20, 120}, 10, 20, 1); |
| 176 | |
| 177 | /* total blackout: the link fails deterministically after max_retries */ |
| 178 | link_cfg dead = {100, 0, 0, 0, 0, 0}; |
| 179 | result d = run(7, &dead, 1, 5, 3, 10000); |
| 180 | EQ(d.done, 0); |
| 181 | /* Lane 19: resend waits are jittered, so the two sides no longer give up on |
| 182 | * the same tick. The first side down has sent 1 + 3 frames: at 0, then 5 |
| 183 | * (first retransmit waits exactly rto), then two waits in [5, 10], and is |
| 184 | * declared down after a third wait in [5, 10]: tick 20..35. */ |
| 185 | CHECK(d.link_down_a || d.link_down_b); |
| 186 | EQ(d.link_down_a ? d.ca.tx_retransmits : d.cb.tx_retransmits, 3); |
| 187 | CHECK(d.ticks >= 20 && d.ticks <= 35); |
| 188 | result d2 = run(7, &dead, 1, 5, 3, 10000); |
| 189 | CHECK(memcmp(&d, &d2, sizeof d) == 0); /* deterministic */ |
| 190 | printf(" %-22s link_down at tick %llu after %llu retransmissions\n", "blackout-dead", |
| 191 | (unsigned long long)d.ticks, (unsigned long long)d.ca.tx_retransmits); |
| 192 | } |
| 193 | |
| 194 | /* ---------------- refusal and malformed-frame negatives ---------------- */ |
| 195 | static void pair(ct_endpoint *a, ct_endpoint *b, uint32_t session) |
| 196 | { |
| 197 | ct_identity id; |
| 198 | ct_test_identity(&id, "lane5-test-link"); |
| 199 | ct_init(a, &id, 0, session, 4, 3); |
| 200 | ct_init(b, &id, 1, session, 4, 3); |
| 201 | } |
| 202 | |
| 203 | static void test_refusals(void) |
| 204 | { |
| 205 | static ct_endpoint a, b, c; |
| 206 | uint8_t f[CT_MAX_FRAME + 8], x[CT_MAX_FRAME + 8], m[CT_MAX_PAYLOAD]; |
| 207 | size_t n, ml; |
| 208 | ct_identity id, other; |
| 209 | EQ(ct_test_identity(&id, ""), CT_ERR_ARG); |
| 210 | EQ(ct_test_identity(&id, "lane5-test-link"), CT_OK); |
| 211 | EQ(id.kind, CT_IDENTITY_TEST); |
| 212 | id.kind = 0x50; /* anything but TEST is refused: production identity needs M5 */ |
| 213 | EQ(ct_init(&a, &id, 0, 1, 4, 3), CT_ERR_REFUSED); |
| 214 | EQ(ct_test_identity(&id, "lane5-test-link"), CT_OK); |
| 215 | EQ(ct_init(&a, &id, 2, 1, 4, 3), CT_ERR_ARG); |
| 216 | EQ(ct_init(&a, &id, 0, 1, 0, 3), CT_ERR_ARG); |
| 217 | |
| 218 | pair(&a, &b, 77); |
| 219 | EQ(ct_send(&a, (const uint8_t *)"hello", 5), CT_OK); |
| 220 | EQ(ct_poll_tx(&a, 0, f, sizeof f, &n), CT_OK); |
| 221 | EQ(n, CT_HEADER_LEN + 5 + CT_TAG_LEN); |
| 222 | /* every truncation and a trailing byte */ |
| 223 | for (size_t k = 0; k < n; k++) CHECK(ct_receive(&b, f, k, m, sizeof m, &ml) < 0); |
| 224 | memcpy(x, f, n); x[n] = 0; |
| 225 | EQ(ct_receive(&b, x, n + 1, m, sizeof m, &ml), CT_ERR_MALFORMED); |
| 226 | /* header field corruption */ |
| 227 | const size_t malformed_at[] = {0, 3, 4, 5, 22, 23}; |
| 228 | for (size_t i = 0; i < sizeof malformed_at / sizeof *malformed_at; i++) { |
| 229 | memcpy(x, f, n); x[malformed_at[i]] ^= 0x40; |
| 230 | EQ(ct_receive(&b, x, n, m, sizeof m, &ml), CT_ERR_MALFORMED); |
| 231 | } |
| 232 | memcpy(x, f, n); x[6] = 2; |
| 233 | EQ(ct_receive(&b, x, n, m, sizeof m, &ml), CT_ERR_MALFORMED); |
| 234 | memcpy(x, f, n); x[20] = 0x04; x[21] = 0x01; /* payload_len 1025 > max */ |
| 235 | EQ(ct_receive(&b, x, n, m, sizeof m, &ml), CT_ERR_MALFORMED); |
| 236 | memcpy(x, f, n); x[21] = 4; /* length lie */ |
| 237 | EQ(ct_receive(&b, x, n, m, sizeof m, &ml), CT_ERR_MALFORMED); |
| 238 | memcpy(x, f, n); x[7] = 0x50; /* non-TEST identity kind */ |
| 239 | EQ(ct_receive(&b, x, n, m, sizeof m, &ml), CT_ERR_REFUSED); |
| 240 | /* every authenticated byte is covered by the tag */ |
| 241 | const size_t tagged_at[] = {8, 11, 12, 15, 16, 19, 24, 28, CT_HEADER_LEN + 5, CT_HEADER_LEN + 5 + 15}; |
| 242 | for (size_t i = 0; i < sizeof tagged_at / sizeof *tagged_at; i++) { |
| 243 | memcpy(x, f, n); x[tagged_at[i]] ^= 1; |
| 244 | EQ(ct_receive(&b, x, n, m, sizeof m, &ml), CT_ERR_BAD_TAG); |
| 245 | } |
| 246 | /* wrong key */ |
| 247 | ct_test_identity(&other, "someone-else"); |
| 248 | ct_init(&c, &other, 1, 77, 4, 3); |
| 249 | EQ(ct_receive(&c, f, n, m, sizeof m, &ml), CT_ERR_BAD_TAG); |
| 250 | /* reflection: A's own frame back to A */ |
| 251 | EQ(ct_receive(&a, f, n, m, sizeof m, &ml), CT_ERR_REFUSED); |
| 252 | /* another session with the same key */ |
| 253 | ct_init(&c, &id, 1, 78, 4, 3); |
| 254 | EQ(ct_receive(&c, f, n, m, sizeof m, &ml), CT_ERR_REFUSED); |
| 255 | /* output buffer too small leaves state unchanged; then delivers */ |
| 256 | EQ(ct_receive(&b, f, n, m, 4, &ml), CT_ERR_CAPACITY); |
| 257 | EQ(b.expected, 0); |
| 258 | EQ(ct_receive(&b, f, n, m, sizeof m, &ml), CT_DELIVERED); |
| 259 | CHECK(ml == 5 && memcmp(m, "hello", 5) == 0); |
| 260 | /* replay is never redelivered */ |
| 261 | for (int i = 0; i < 5; i++) EQ(ct_receive(&b, f, n, m, sizeof m, &ml), CT_OK); |
| 262 | EQ(b.c.rx_delivered, 1); |
| 263 | EQ(b.c.rx_duplicate, 5); |
| 264 | /* B acks; A frees the slot; a replayed old ack is harmless */ |
| 265 | uint8_t ackf[64]; size_t an; |
| 266 | EQ(ct_poll_tx(&b, 0, ackf, sizeof ackf, &an), CT_OK); |
| 267 | EQ(an, CT_HEADER_LEN + CT_TAG_LEN); |
| 268 | EQ(ct_poll_tx(&b, 0, ackf + 40, 24, &n), CT_IDLE); |
| 269 | EQ(ct_receive(&a, ackf, an, m, sizeof m, &ml), CT_OK); |
| 270 | EQ(ct_in_flight(&a), 0); |
| 271 | EQ(ct_receive(&a, ackf, an, m, sizeof m, &ml), CT_OK); |
| 272 | /* an ACK for data never sent */ |
| 273 | b.expected = 9; b.ack_pending = 1; |
| 274 | EQ(ct_poll_tx(&b, 0, ackf, sizeof ackf, &an), CT_OK); |
| 275 | EQ(ct_receive(&a, ackf, an, m, sizeof m, &ml), CT_ERR_BAD_ACK); |
| 276 | EQ(ct_in_flight(&a), 0); |
| 277 | /* turning DATA into ACK in flight breaks the tag */ |
| 278 | b.expected = 1; |
| 279 | EQ(ct_send(&b, (const uint8_t *)"z", 1), CT_OK); |
| 280 | EQ(ct_poll_tx(&b, 0, x, sizeof x, &n), CT_OK); |
| 281 | x[5] = CT_TYPE_ACK; |
| 282 | EQ(ct_receive(&a, x, n, m, sizeof m, &ml), CT_ERR_BAD_TAG); |
| 283 | |
| 284 | /* sender limits */ |
| 285 | pair(&a, &b, 5); |
| 286 | static uint8_t big[CT_MAX_PAYLOAD + 1]; |
| 287 | EQ(ct_send(&a, big, CT_MAX_PAYLOAD + 1), CT_ERR_TOO_LARGE); |
| 288 | EQ(ct_send(&a, big, CT_MAX_PAYLOAD), CT_OK); |
| 289 | for (uint32_t i = 1; i < CT_WINDOW; i++) EQ(ct_send(&a, big, 1), CT_OK); |
| 290 | EQ(ct_send(&a, big, 1), CT_ERR_WINDOW_FULL); |
| 291 | EQ(ct_poll_tx(&a, 0, f, CT_HEADER_LEN + CT_MAX_PAYLOAD + CT_TAG_LEN - 1, &n), CT_ERR_CAPACITY); |
| 292 | EQ(ct_poll_tx(&a, 0, f, sizeof f, &n), CT_OK); |
| 293 | EQ(n, CT_MAX_FRAME); |
| 294 | EQ(ct_receive(&b, f, n, m, sizeof m, &ml), CT_DELIVERED); |
| 295 | EQ(ml, CT_MAX_PAYLOAD); |
| 296 | pair(&a, &b, 6); |
| 297 | a.next_seq = a.base = UINT32_MAX; |
| 298 | EQ(ct_send(&a, big, 1), CT_ERR_SEQ_EXHAUSTED); |
| 299 | /* gap: seq 1 before seq 0 is not delivered; seq 0 then is, then 1 on retransmit */ |
| 300 | pair(&a, &b, 8); |
| 301 | uint8_t f0[64], f1[64]; size_t n0, n1; |
| 302 | ct_send(&a, (const uint8_t *)"0", 1); |
| 303 | ct_send(&a, (const uint8_t *)"1", 1); |
| 304 | EQ(ct_poll_tx(&a, 0, f0, sizeof f0, &n0), CT_OK); |
| 305 | EQ(ct_poll_tx(&a, 0, f1, sizeof f1, &n1), CT_OK); |
| 306 | EQ(ct_receive(&b, f1, n1, m, sizeof m, &ml), CT_OK); |
| 307 | EQ(b.c.rx_gap, 1); |
| 308 | EQ(ct_receive(&b, f0, n0, m, sizeof m, &ml), CT_DELIVERED); |
| 309 | EQ(m[0], '0'); |
| 310 | EQ(ct_poll_tx(&a, 3, f1, sizeof f1, &n1), CT_IDLE); /* rto 4 not reached */ |
| 311 | EQ(ct_poll_tx(&a, 4, f0, sizeof f0, &n0), CT_OK); /* retransmit seq 0 */ |
| 312 | EQ(ct_poll_tx(&a, 4, f1, sizeof f1, &n1), CT_OK); /* retransmit seq 1 */ |
| 313 | EQ(ct_receive(&b, f1, n1, m, sizeof m, &ml), CT_DELIVERED); |
| 314 | EQ(m[0], '1'); |
| 315 | /* a clock that goes backwards never triggers a retransmit storm */ |
| 316 | EQ(ct_poll_tx(&a, 1, f1, sizeof f1, &n1), CT_IDLE); |
| 317 | /* link down is sticky */ |
| 318 | pair(&a, &b, 9); |
| 319 | ct_send(&a, (const uint8_t *)"q", 1); |
| 320 | uint64_t t = 0; |
| 321 | while (ct_poll_tx(&a, t, f, sizeof f, &n) != CT_ERR_LINK_DOWN) t++; |
| 322 | EQ(ct_send(&a, (const uint8_t *)"q", 1), CT_ERR_LINK_DOWN); |
| 323 | EQ(ct_receive(&a, ackf, an, m, sizeof m, &ml), CT_ERR_LINK_DOWN); |
| 324 | } |
| 325 | |
| 326 | /* ---------------- resend jitter (Lane 19) ---------------- */ |
| 327 | /* Strictly periodic loss, both senders in lockstep, no sender-side timing |
| 328 | * tricks: with a fixed resend interval this starved one frame until the link |
| 329 | * went down. The jittered resend schedule must recover for every N. */ |
| 330 | static void test_periodic_loss(void) |
| 331 | { |
| 332 | int recovered = 0, total = 0; |
| 333 | static const uint64_t N[] = {2, 3, 4, 5, 6, 7, 8, 11, 16}; |
| 334 | for (size_t i = 0; i < sizeof N / sizeof N[0]; i++) { |
| 335 | link_cfg L = {0, 0, 0, 0, 0, 0}; |
| 336 | drop_every = N[i]; |
| 337 | result a = run(9, &L, MSGS, 3, 40, 200000); |
| 338 | result b = run(9, &L, MSGS, 3, 40, 200000); |
| 339 | CHECK(memcmp(&a, &b, sizeof a) == 0); /* bit-identical rerun */ |
| 340 | EQ(a.done, 1); |
| 341 | total++; recovered += a.done; |
| 342 | EQ(a.delivered[0], MSGS); |
| 343 | EQ(a.delivered[1], MSGS); |
| 344 | CHECK(a.ca.tx_retransmits + a.cb.tx_retransmits > 0); /* loss really happened */ |
| 345 | if (!a.done) |
| 346 | fprintf(stderr, "periodic loss N=%llu: delivered %u/%u down %d/%d\n", |
| 347 | (unsigned long long)N[i], a.delivered[0], a.delivered[1], a.link_down_a, a.link_down_b); |
| 348 | } |
| 349 | drop_every = 0; |
| 350 | printf(" %-22s %d/%d drop-every-N patterns (N=2..16) recovered, bit-identical reruns\n", "periodic-loss", |
| 351 | recovered, total); |
| 352 | } |
| 353 | |
| 354 | /* Transmit ticks of one message on a dead link, until link down. */ |
| 355 | static int tx_schedule(uint32_t session, uint8_t role, uint32_t seq, uint32_t rto, uint32_t retries, |
| 356 | uint64_t *at, int max) |
| 357 | { |
| 358 | ct_endpoint e; |
| 359 | ct_identity id; |
| 360 | uint8_t f[CT_MAX_FRAME]; |
| 361 | size_t n; |
| 362 | ct_test_identity(&id, "lane19-jitter"); |
| 363 | ct_init(&e, &id, role, session, rto, retries); |
| 364 | e.next_seq = e.base = seq; |
| 365 | EQ(ct_send(&e, (const uint8_t *)"j", 1), CT_OK); |
| 366 | int k = 0; |
| 367 | for (uint64_t t = 0; t < 100000; t++) { |
| 368 | ct_status s = ct_poll_tx(&e, t, f, sizeof f, &n); |
| 369 | if (s == CT_ERR_LINK_DOWN) break; |
| 370 | if (s == CT_OK && k < max) at[k++] = t; |
| 371 | } |
| 372 | return k; |
| 373 | } |
| 374 | |
| 375 | static void test_resend_schedule(void) |
| 376 | { |
| 377 | uint64_t at[64], at2[64]; |
| 378 | int first_exact = 1, bounded = 1, by_seq = 0, by_session = 0, by_role = 0, by_tries = 0; |
| 379 | for (uint32_t q = 0; q < 64; q++) { |
| 380 | int k = tx_schedule(0xA1E5u, 0, q * 7919u, 5, 6, at, 64); |
| 381 | EQ(k, 1 + 6); /* first send + max_retries retransmissions */ |
| 382 | EQ(tx_schedule(0xA1E5u, 0, q * 7919u, 5, 6, at2, 64), k); |
| 383 | CHECK(memcmp(at, at2, sizeof at[0] * (size_t)k) == 0); /* deterministic */ |
| 384 | if (at[1] - at[0] != 5) first_exact = 0; |
| 385 | for (int i = 1; i < k; i++) |
| 386 | if (at[i] - at[i - 1] < 5 || at[i] - at[i - 1] > 10) bounded = 0; |
| 387 | for (int i = 3; i < k; i++) |
| 388 | if (at[i] - at[i - 1] != at[2] - at[1]) by_tries = 1; |
| 389 | uint64_t g_seq = at[2] - at[1]; |
| 390 | tx_schedule(0xA1E5u, 0, q * 7919u + 1, 5, 6, at2, 64); |
| 391 | if (at2[2] - at2[1] != g_seq) by_seq = 1; |
| 392 | tx_schedule(0xA1E5u + 1 + q, 0, q * 7919u, 5, 6, at2, 64); |
| 393 | if (at2[2] - at2[1] != g_seq) by_session = 1; |
| 394 | tx_schedule(0xA1E5u, 1, q * 7919u, 5, 6, at2, 64); |
| 395 | if (at2[2] - at2[1] != g_seq) by_role = 1; |
| 396 | } |
| 397 | CHECK(first_exact); /* the first retransmit waits exactly rto */ |
| 398 | CHECK(bounded); /* every wait is in [rto, 2*rto] */ |
| 399 | CHECK(by_tries && by_seq && by_session && by_role); |
| 400 | /* rto 1: waits are 1 or 2, and both occur */ |
| 401 | int saw1 = 0, saw2 = 0; |
| 402 | for (uint32_t q = 0; q < 16; q++) { |
| 403 | int k = tx_schedule(3, 0, q, 1, 8, at, 64); |
| 404 | for (int i = 2; i < k; i++) { |
| 405 | uint64_t g = at[i] - at[i - 1]; |
| 406 | CHECK(g == 1 || g == 2); |
| 407 | saw1 |= g == 1; saw2 |= g == 2; |
| 408 | } |
| 409 | } |
| 410 | CHECK(saw1 && saw2); |
| 411 | /* largest rto: the wait is computed without 32-bit overflow */ |
| 412 | ct_endpoint e; |
| 413 | ct_identity id; |
| 414 | uint8_t f[CT_MAX_FRAME]; |
| 415 | size_t n; |
| 416 | ct_test_identity(&id, "lane19-jitter"); |
| 417 | ct_init(&e, &id, 0, 1, UINT32_MAX, 3); |
| 418 | ct_send(&e, (const uint8_t *)"j", 1); |
| 419 | EQ(ct_poll_tx(&e, 0, f, sizeof f, &n), CT_OK); |
| 420 | EQ(ct_poll_tx(&e, UINT32_MAX - 1ull, f, sizeof f, &n), CT_IDLE); |
| 421 | EQ(ct_poll_tx(&e, UINT32_MAX, f, sizeof f, &n), CT_OK); |
| 422 | EQ(ct_poll_tx(&e, 2ull * UINT32_MAX - 1, f, sizeof f, &n), CT_IDLE); |
| 423 | EQ(ct_poll_tx(&e, 3ull * UINT32_MAX, f, sizeof f, &n), CT_OK); |
| 424 | printf(" %-22s first wait = rto, later waits in [rto, 2*rto], vary by seq/session/role/try\n", |
| 425 | "resend-schedule"); |
| 426 | } |
| 427 | |
| 428 | int main(void) |
| 429 | { |
| 430 | test_refusals(); |
| 431 | test_loss_recovery(); |
| 432 | test_periodic_loss(); |
| 433 | test_resend_schedule(); |
| 434 | printf("ctl_test checks=%d failures=%d\n", checks, failures); |
| 435 | printf("CTL_TRANSPORT_LOSS_RECOVERY: %s\n", failures ? "FAIL" : "PASS"); |
| 436 | return failures ? 1 : 0; |
| 437 | } |