@amritk/nish-aarch64-linux 0.39.0 → 0.41.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/INSTALL.md +79 -133
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +112 -33
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +21 -17
  6. package/runtime/nish.h +10 -0
  7. package/runtime/page-frame.d.ts +123 -0
  8. package/runtime/page-frame.mjs +736 -0
  9. package/runtime/page-game.d.ts +132 -0
  10. package/runtime/page-game.mjs +738 -0
  11. package/runtime/runtime-aes.c +85 -8
  12. package/runtime/runtime-bytes.c +88 -4
  13. package/runtime/runtime-net.c +32 -0
  14. package/runtime/runtime-sandbox.c +55 -28
  15. package/runtime/runtime-uring.c +1194 -0
  16. package/runtime/runtime-wasm.c +422 -0
  17. package/runtime/vite.mjs +1 -1
  18. package/scripts/build.sh +22 -6
  19. package/std/README.md +74 -87
  20. package/std/array.ts +36 -3
  21. package/std/collections.ts +51 -0
  22. package/std/crypto/hmac.ts +37 -10
  23. package/std/crypto/p256.ts +1388 -2664
  24. package/std/crypto/pki.ts +116 -4
  25. package/std/crypto/sha256.ts +34 -6
  26. package/std/crypto/x509.ts +7 -0
  27. package/std/host/game.ts +273 -0
  28. package/std/input.ts +56 -0
  29. package/std/json-tokenizer.ts +325 -112
  30. package/std/json-typed.ts +43 -8
  31. package/std/json-value.ts +144 -3
  32. package/std/json.ts +63 -8
  33. package/std/net/fetch.ts +22 -4
  34. package/std/net/http-server.ts +3382 -0
  35. package/std/net/http1-client-codec.ts +6 -2
  36. package/std/net/http1-codec.ts +137 -12
  37. package/std/net/http1-server.ts +83 -29
  38. package/std/net/http2.ts +47 -14
  39. package/std/net/http3-server.ts +29 -3
  40. package/std/net/http3.ts +180 -7
  41. package/std/net/prefork.ts +49 -13
  42. package/std/net/quic-recovery.ts +240 -9
  43. package/std/net/quic-stream.ts +21 -1
  44. package/std/net/quic.ts +98 -4
  45. package/std/net/router.ts +378 -31
  46. package/std/net/serve.ts +873 -65
  47. package/std/net/tls/verify.ts +19 -1
  48. package/std/net/url.ts +83 -35
  49. package/std/net/websocket.ts +74 -1
  50. package/std/net/webtransport.ts +7 -1
  51. package/std/raster.ts +837 -0
  52. package/std/release.ts +593 -0
  53. package/std/synth.ts +143 -0
  54. package/std/temporal.ts +3091 -0
  55. package/std/threads.ts +298 -0
  56. package/std/yaml-json.ts +368 -28
  57. package/std/yaml.ts +122 -27
@@ -2,7 +2,7 @@
2
2
  * `nish/net/quic-recovery` — QUIC loss detection and congestion control
3
3
  * (RFC 9002): the packets each packet number space has in flight, the
4
4
  * round-trip estimate, loss by packet and by time threshold, the probe
5
- * timeout with its backoff, NewReno, and a pacer. Sans-IO: it never sees a
5
+ * timeout with its backoff, NewReno or CUBIC, and a pacer. Sans-IO: it never sees a
6
6
  * byte of a packet, only packet numbers, sizes and the caller's clock.
7
7
  *
8
8
  * import { QuicRecovery, QUIC_RECOVERY_TIMEOUT_PTO } from "nish/net/quic-recovery";
@@ -50,6 +50,18 @@
50
50
  * only if, when it arrived, the bytes in flight were at least half the
51
51
  * window.
52
52
  *
53
+ * **Congestion control.** `controller` picks it, once, before the first
54
+ * packet: `QUIC_RECOVERY_NEWRENO` (Appendix B, the default) or
55
+ * `QUIC_RECOVERY_CUBIC` (RFC 9438). Both share slow start, recovery
56
+ * periods, persistent congestion's collapse to the minimum window and the
57
+ * rule above for a window not in use. CUBIC keeps 0.7 of the window at a
58
+ * congestion event rather than half, then grows along W(t) = W_max + C ×
59
+ * (t − K)³ with C 0.4, concave up to the window it lost at and convex past
60
+ * it, never below what Reno would have reached (its Reno-friendly region),
61
+ * and with fast convergence. Its arithmetic is integers in bytes and
62
+ * milliseconds, and K's cube root is found by bisection, so two machines
63
+ * given the same events reach the same window to the byte.
64
+ *
53
65
  * **ECN** (§7.1, B.7; RFC 9000 §13.4.2 and its Appendix A.4). A caller that
54
66
  * marks its packets ECT(0) asks `ecnCodepoint()` what to mark with, says so
55
67
  * with `onEctSent` for every packet it marked, and hands an ACK_ECN frame's
@@ -77,7 +89,7 @@
77
89
  * timeout, at a rate that halves each time, until the idle timeout ends the
78
90
  * connection.
79
91
  *
80
- * Written from RFC 9002 §5 to §7 and its Appendices A and B, in this
92
+ * Written from RFC 9002 §5 to §7 and its Appendices A and B, and RFC 9438, in this
81
93
  * module's own structure; nothing here is ported from another
82
94
  * implementation. Private names carry the `quicRecovery` prefix
83
95
  * (`docs/wp26-stdlib.md` §3e).
@@ -180,9 +192,75 @@ const QUIC_RECOVERY_MICROS: i64 = 1000
180
192
  */
181
193
  const QUIC_RECOVERY_PACER_WINDOW: i64 = 4294967296
182
194
 
195
+ /** The congestion controller of RFC 9002 §7 and Appendix B: NewReno. */
196
+ export const QUIC_RECOVERY_NEWRENO: i32 = 0
197
+ /** The congestion controller of RFC 9438: CUBIC, with its Reno-friendly region and fast convergence. */
198
+ export const QUIC_RECOVERY_CUBIC: i32 = 1
199
+ /**
200
+ * The controller a server runs unless it has a reason to pick: CUBIC. It
201
+ * beat NewReno outside the spread across loss draws on transfers of 32 and
202
+ * 128 bandwidth-delay products and finished level on one
203
+ * (`bench/quic-goodput.ts`; the figures are in docs/security/quic.md).
204
+ * It holds the same value as `QUIC_RECOVERY_CUBIC`, written out rather than
205
+ * aliased so the default reads as a choice of its own.
206
+ */
207
+ export const QUIC_RECOVERY_DEFAULT: i32 = 1
208
+
209
+ /**
210
+ * CUBIC's longest span in milliseconds: K and every `t − K` its curve is
211
+ * read at are clamped to ±40 s. A cube of it times twice the largest UDP
212
+ * payload stays inside an `i64` (2 × 65535 × 40000³ < 2^63), and K reaches
213
+ * it only after a fall of 25600 datagrams, 30 MB at 1200 bytes. What the
214
+ * clamp costs: 40 s past K, W(t) stops at W_max + 25600 datagrams, so a
215
+ * window that far past its last loss grows only as fast as W_est.
216
+ */
217
+ const QUIC_RECOVERY_CUBIC_SPAN: i64 = 40000
218
+ /**
219
+ * C (0.4 segments/s³) on milliseconds: W(t) = W_max + 2 × size × (t − K)³ / 5 × 10⁹
220
+ * bytes, `size` the datagram size and t in milliseconds (RFC 9438 §4.2).
221
+ */
222
+ const QUIC_RECOVERY_CUBIC_SCALE: i64 = 5000000000
223
+
183
224
  /** The larger of two times or durations. */
184
225
  const quicRecoveryMax = (a: i64, b: i64): i64 => (a > b ? a : b)
185
226
 
227
+ /** `x` clamped to −`QUIC_RECOVERY_CUBIC_SPAN` .. `QUIC_RECOVERY_CUBIC_SPAN`. */
228
+ const quicRecoveryCubicClamp = (x: i64): i64 => {
229
+ if (x > QUIC_RECOVERY_CUBIC_SPAN) {
230
+ return QUIC_RECOVERY_CUBIC_SPAN
231
+ }
232
+ return x < -QUIC_RECOVERY_CUBIC_SPAN ? -QUIC_RECOVERY_CUBIC_SPAN : x
233
+ }
234
+
235
+ /**
236
+ * The integer cube root of `x`, the largest r with r³ <= x, for 0 <= x <=
237
+ * `QUIC_RECOVERY_CUBIC_SPAN`³, found by bisection over 0 .. the span: 16
238
+ * steps, the same answer on every machine, no floating point.
239
+ *
240
+ * @param x The cube, 0 to 40000³.
241
+ * @returns Its root, 0 to 40000.
242
+ */
243
+ export const quicRecoveryCbrt = (x: i64): i64 => {
244
+ let lo: i64 = 0
245
+ let hi: i64 = QUIC_RECOVERY_CUBIC_SPAN
246
+ if (x <= lo) {
247
+ return lo
248
+ }
249
+ if (x >= hi * hi * hi) {
250
+ return hi
251
+ }
252
+ // lo³ <= x < hi³ holds at every step.
253
+ while (hi - lo > 1) {
254
+ const mid: i64 = (lo + hi) / 2
255
+ if (mid * mid * mid <= x) {
256
+ lo = mid
257
+ } else {
258
+ hi = mid
259
+ }
260
+ }
261
+ return lo
262
+ }
263
+
186
264
  /**
187
265
  * The ack-eliciting packets one space sent, in a ring of `capacity` slots
188
266
  * in the order they went out: slot `slot(k)` is the `k`-th from the oldest
@@ -546,6 +624,33 @@ export class QuicRecovery {
546
624
  ssthresh: i64 = 0
547
625
  /** congestion_recovery_start_time (B.2), or -1 outside recovery. */
548
626
  recoveryStart: i64 = -1
627
+ /**
628
+ * The congestion controller, `QUIC_RECOVERY_NEWRENO` or `QUIC_RECOVERY_CUBIC`, which the caller
629
+ * sets once, before the first packet; `reset` keeps it.
630
+ */
631
+ controller: i32 = 0
632
+ /** CUBIC's W_max (RFC 9438 §4.2) in bytes: the window its curve plateaus at, 0 before the first congestion event. */
633
+ cubicMax: i64 = 0
634
+ /** CUBIC's K in milliseconds: how long after `cubicEpoch` the curve reaches `cubicMax`. */
635
+ cubicK: i64 = 0
636
+ /** When CUBIC's congestion avoidance stage began (t = 0 of its curve), in milliseconds, or -1 outside one. */
637
+ cubicEpoch: i64 = -1
638
+ /** CUBIC's W_est (§4.3) in bytes: the window Reno's AIMD with β 0.7 would have reached since `cubicEpoch`. */
639
+ cubicEst: i64 = 0
640
+ /** What `cubicEst`'s last step truncated: the numerator of a fraction of 17 × the window then. */
641
+ cubicEstCarry: i64 = 0
642
+ /**
643
+ * What the window's last step toward the curve truncated: the numerator of a fraction of the
644
+ * window then, so steps smaller than a byte add up instead of being lost (RFC 9438's cwnd_cnt).
645
+ */
646
+ cubicCarry: i64 = 0
647
+ /** CUBIC's cwnd_prior (§4.6): the window just before the last reduction, in bytes. */
648
+ cubicPrior: i64 = 0
649
+ /**
650
+ * When the window last stopped being used (§5.8) during a congestion avoidance stage, or -1: the
651
+ * curve's epoch moves on by the time it was idle, so it resumes where it stood.
652
+ */
653
+ cubicIdle: i64 = -1
549
654
  /** The pacer's credit in bytes (§7.7), and the time it was last topped up in microseconds, or -1. */
550
655
  pacerBudget: i64 = 0
551
656
  /**
@@ -648,6 +753,14 @@ export class QuicRecovery {
648
753
  this.congestionWindow = QUIC_RECOVERY_INITIAL_WINDOW
649
754
  this.ssthresh = QUIC_MAX_VARINT
650
755
  this.recoveryStart = -1
756
+ this.cubicMax = 0
757
+ this.cubicK = 0
758
+ this.cubicEpoch = -1
759
+ this.cubicEst = 0
760
+ this.cubicEstCarry = 0
761
+ this.cubicCarry = 0
762
+ this.cubicPrior = 0
763
+ this.cubicIdle = -1
651
764
  this.pacerBudget = QUIC_RECOVERY_INITIAL_WINDOW
652
765
  this.pacerTime = -1
653
766
  this.pacerRtt = QUIC_RECOVERY_INITIAL_RTT * QUIC_RECOVERY_MICROS
@@ -970,8 +1083,11 @@ export class QuicRecovery {
970
1083
  }
971
1084
  this.detectLoss(sp, now)
972
1085
  this.packetsLost(sp, now)
1086
+ if (this.controller === QUIC_RECOVERY_CUBIC) {
1087
+ this.cubicInUse(inUse, now)
1088
+ }
973
1089
  for (let k: i32 = 0; k < sp.ackedCount; k += 1) {
974
- this.packetAcked(sp, sp.ackedSlot(k), inUse)
1090
+ this.packetAcked(sp, sp.ackedSlot(k), inUse, now)
975
1091
  }
976
1092
  this.ptoCount = 0
977
1093
  return sp.ackedCount
@@ -1192,6 +1308,9 @@ export class QuicRecovery {
1192
1308
  if (newest >= 0 && this.persistentCongestion(sp)) {
1193
1309
  this.congestionWindow = this.minimumWindow()
1194
1310
  this.recoveryStart = -1
1311
+ // CUBIC starts over from slow start (RFC 9438 §4.8): its next stage is a new epoch.
1312
+ this.cubicEpoch = -1
1313
+ this.cubicIdle = -1
1195
1314
  this.persistentCongestions = this.persistentCongestions + 1
1196
1315
  }
1197
1316
  for (let k: i32 = 0; k < sp.lostCount; k += 1) {
@@ -1250,35 +1369,147 @@ export class QuicRecovery {
1250
1369
  return this.recoveryStart >= 0 && sentTime <= this.recoveryStart
1251
1370
  }
1252
1371
 
1253
- /** OnCongestionEvent (B.6): halve the window, once per recovery period. */
1372
+ /**
1373
+ * OnCongestionEvent (B.6), once per recovery period: NewReno halves the
1374
+ * window. CUBIC (RFC 9438 §4.6, §4.7) keeps 0.7 of it, and remembers
1375
+ * where it was as W_max, or 0.85 of that when it is below the W_max
1376
+ * before (fast convergence), so a flow losing ground leaves room sooner.
1377
+ */
1254
1378
  private congestionEvent(sentTime: i64, now: i64): void {
1255
1379
  if (this.inRecovery(sentTime)) {
1256
1380
  return
1257
1381
  }
1258
1382
  this.recoveryStart = now
1259
- this.ssthresh = this.congestionWindow / 2
1383
+ const cwnd: i64 = this.congestionWindow
1384
+ if (this.controller === QUIC_RECOVERY_CUBIC) {
1385
+ this.cubicMax = cwnd < this.cubicMax ? (cwnd * 17) / 20 : cwnd
1386
+ this.cubicPrior = cwnd
1387
+ this.cubicEpoch = -1
1388
+ this.cubicIdle = -1
1389
+ this.ssthresh = (cwnd * 7) / 10
1390
+ } else {
1391
+ this.ssthresh = cwnd / 2
1392
+ }
1260
1393
  this.congestionWindow = quicRecoveryMax(this.ssthresh, this.minimumWindow())
1261
1394
  this.congestionEvents = this.congestionEvents + 1
1262
1395
  }
1263
1396
 
1264
1397
  /**
1265
1398
  * OnPacketAcked (B.5) for slot `s`: in slow start the window grows by the
1266
- * packet's size, in congestion avoidance by datagramSize × size / window; not at
1267
- * all for a packet sent during recovery, or when `inUse` says the window
1268
- * was not being used (§7.8).
1399
+ * packet's size; in congestion avoidance NewReno's by datagramSize × size / window,
1400
+ * CUBIC's as `cubicAcked` says. Not at all for a packet sent during
1401
+ * recovery, or when `inUse` says the window was not being used (§7.8).
1269
1402
  */
1270
- private packetAcked(sp: QuicSentPackets, s: i32, inUse: boolean): void {
1403
+ private packetAcked(sp: QuicSentPackets, s: i32, inUse: boolean, now: i64): void {
1271
1404
  if (!inUse || this.inRecovery(sp.timeAt(s))) {
1272
1405
  return
1273
1406
  }
1274
1407
  const size: i64 = toI64(sp.sizeAt(s))
1275
1408
  if (this.congestionWindow < this.ssthresh) {
1276
1409
  this.congestionWindow = this.congestionWindow + size
1410
+ } else if (this.controller === QUIC_RECOVERY_CUBIC) {
1411
+ this.cubicAcked(size, now)
1277
1412
  } else {
1278
1413
  this.congestionWindow = this.congestionWindow + (this.datagramSize * size) / this.congestionWindow
1279
1414
  }
1280
1415
  }
1281
1416
 
1417
+ /**
1418
+ * CUBIC's congestion avoidance (RFC 9438 §4.2 to §4.5) for `size` bytes
1419
+ * newly acknowledged at `now`. The first acknowledgement of a stage starts
1420
+ * its epoch: K = ∛((W_max − cwnd) / C), or 0 with W_max taken as cwnd
1421
+ * when the window is already past it (after slow start). W_est then grows
1422
+ * as Reno's would with β 0.7, by α = 3(1 − β)/(1 + β) = 9/17 datagram a
1423
+ * window of acknowledgements, and by 1 once it has passed cwnd_prior. If
1424
+ * the curve W(t) is below W_est the window is W_est (the Reno-friendly
1425
+ * region); otherwise it moves toward W(t + RTT), held within cwnd ..
1426
+ * 1.5 × cwnd, by (target − cwnd) × size / cwnd: concave below W_max, convex
1427
+ * past it. Integers throughout; W_est and the window each carry what their
1428
+ * division truncated, so a step under a byte is deferred, never dropped.
1429
+ */
1430
+ private cubicAcked(size: i64, now: i64): void {
1431
+ const cwnd: i64 = this.congestionWindow
1432
+ if (this.cubicEpoch < 0) {
1433
+ this.cubicEpoch = now
1434
+ this.cubicIdle = -1
1435
+ this.cubicEst = cwnd
1436
+ this.cubicEstCarry = 0
1437
+ this.cubicCarry = 0
1438
+ if (this.cubicMax > cwnd) {
1439
+ this.cubicK = this.cubicSpan(this.cubicMax - cwnd)
1440
+ } else {
1441
+ this.cubicK = 0
1442
+ this.cubicMax = cwnd
1443
+ }
1444
+ }
1445
+ const alpha: i64 = this.cubicEst >= this.cubicPrior ? 17 : 9
1446
+ const num: i64 = alpha * size * this.datagramSize + this.cubicEstCarry
1447
+ const den: i64 = 17 * cwnd
1448
+ this.cubicEst = this.cubicEst + num / den
1449
+ this.cubicEstCarry = num % den
1450
+ const t: i64 = now - this.cubicEpoch
1451
+ if (this.cubicWindow(t) < this.cubicEst) {
1452
+ this.congestionWindow = quicRecoveryMax(cwnd, this.cubicEst)
1453
+ return
1454
+ }
1455
+ const ceiling: i64 = cwnd + cwnd / 2
1456
+ const curve: i64 = this.cubicWindow(t + this.smoothedRtt)
1457
+ const target: i64 = quicRecoveryMax(cwnd, curve > ceiling ? ceiling : curve)
1458
+ // The window only grows within a stage, so the carry, below the window
1459
+ // it was a fraction of, stays below the one it is added against.
1460
+ const step: i64 = (target - cwnd) * size + this.cubicCarry
1461
+ this.congestionWindow = cwnd + step / cwnd
1462
+ this.cubicCarry = step % cwnd
1463
+ }
1464
+
1465
+ /**
1466
+ * W_cubic(t) (RFC 9438 §4.2) in bytes, `t` milliseconds into the epoch:
1467
+ * W_max + C × (t − K)³ in datagrams, which is W_max + 2 × size × (t − K)³ /
1468
+ * 5 × 10⁹ in bytes and milliseconds, with t − K clamped to ±40 s. The
1469
+ * division truncates toward zero.
1470
+ */
1471
+ private cubicWindow(t: i64): i64 {
1472
+ const d: i64 = quicRecoveryCubicClamp(t - this.cubicK)
1473
+ return this.cubicMax + (2 * this.datagramSize * (d * d * d)) / QUIC_RECOVERY_CUBIC_SCALE
1474
+ }
1475
+
1476
+ /**
1477
+ * K (RFC 9438 §4.2) for a fall of `drop` bytes below W_max: ∛(drop / C) in
1478
+ * datagrams and seconds, which is ∛(drop × 5 × 10⁹ / (2 × size)) in
1479
+ * milliseconds, rounded down; a fall too deep for the cube to fit is 40 s.
1480
+ */
1481
+ private cubicSpan(drop: i64): i64 {
1482
+ const two: i64 = 2 * this.datagramSize
1483
+ const span: i64 = QUIC_RECOVERY_CUBIC_SPAN
1484
+ // drop >= span³ × 2 × size / (5 × 10⁹), which is 25600 × size, has K at the span or past it.
1485
+ if (drop >= 25600 * this.datagramSize) {
1486
+ return span
1487
+ }
1488
+ return quicRecoveryCbrt((drop * QUIC_RECOVERY_CUBIC_SCALE) / two)
1489
+ }
1490
+
1491
+ /**
1492
+ * The application-limited rule (RFC 9438 §5.8) at an acknowledgement:
1493
+ * while the window is not in use it does not grow (`packetAcked`), and the
1494
+ * time it spends so is taken out of the curve, so a stage resumes where it
1495
+ * stood rather than at the point the clock has reached.
1496
+ */
1497
+ private cubicInUse(inUse: boolean, now: i64): void {
1498
+ if (this.cubicEpoch < 0) {
1499
+ return
1500
+ }
1501
+ if (!inUse) {
1502
+ if (this.cubicIdle < 0) {
1503
+ this.cubicIdle = now
1504
+ }
1505
+ return
1506
+ }
1507
+ if (this.cubicIdle >= 0) {
1508
+ this.cubicEpoch = this.cubicEpoch + (now - this.cubicIdle)
1509
+ this.cubicIdle = -1
1510
+ }
1511
+ }
1512
+
1282
1513
  /**
1283
1514
  * The probe timeout's period without backoff (§6.2.1): smoothed_rtt +
1284
1515
  * max(4 × rttvar, kGranularity), plus the peer's max_ack_delay once the
@@ -726,7 +726,11 @@ export class QuicStream {
726
726
  }
727
727
 
728
728
  /**
729
- * How many more bytes the send buffer takes.
729
+ * How many more bytes the send buffer takes. The buffer is the stream's
730
+ * `maxStreamData` and is the memory bound of its send side: bytes stay in
731
+ * it until the peer acknowledges them, so one lost packet holds back
732
+ * everything written after it until its resend is acknowledged (quic.ts
733
+ * probes early meanwhile, so a peer whose address changes is still seen).
730
734
  *
731
735
  * @returns The buffer size less the bytes written and not yet acknowledged.
732
736
  */
@@ -1181,6 +1185,22 @@ export class QuicStreams {
1181
1185
  this.streamsBlockedUniOwed = false
1182
1186
  }
1183
1187
 
1188
+ /**
1189
+ * Whether a stream's send buffer is full behind bytes the peer has not
1190
+ * acknowledged, with nothing of it left to send: the application offered
1191
+ * more than `room` took, and every byte the buffer holds is in flight. The
1192
+ * stream then sends nothing new until an ACK frees room.
1193
+ * @internal Read by quic.ts, for its early probe.
1194
+ */
1195
+ bufferBlocked(): boolean {
1196
+ for (const stream of this.slots) {
1197
+ if (stream.id >= 0 && stream.wantsWrite && stream.room() <= 0 && !stream.wantsToSend()) {
1198
+ return true
1199
+ }
1200
+ }
1201
+ return false
1202
+ }
1203
+
1184
1204
  /**
1185
1205
  * Takes the peer's transport parameters (RFC 9000 §18.2): its limits on
1186
1206
  * the streams this side opens, the credit each kind of stream starts with,
package/std/net/quic.ts CHANGED
@@ -107,12 +107,16 @@
107
107
  *
108
108
  * **Time.** The connection has no clock: `receive`, `takeDatagram` and
109
109
  * `handleTimer` take the caller's monotonic time in milliseconds, and
110
- * `deadline()` says when `handleTimer` is next due. Five things run on it. The
110
+ * `deadline()` says when `handleTimer` is next due. Six things run on it. The
111
111
  * idle timeout (RFC 9000 §10.1) is the smaller of the two sides'
112
112
  * `max_idle_timeout`, never under `QUIC_CONN_IDLE_FLOOR` or three probe
113
113
  * timeouts; once it passes with nothing received, the connection closes
114
114
  * silently (`QUIC_STATE_TIMED_OUT`) and wipes its keys. Loss recovery's timer
115
115
  * declares packets lost by the time threshold or fires the probe timeout.
116
+ * While a stream's send buffer is full behind bytes in flight, an early
117
+ * ACK-eliciting probe goes half a round trip after the last ACK-eliciting
118
+ * packet, so a client whose NAT rebinds while nothing else is sent still
119
+ * answers from its new port (QUIC-12).
116
120
  *
117
121
  * After a key update the previous read keys are kept for a probe timeout
118
122
  * (`recovery.probeTimeout()`), for packets the network reordered, before the
@@ -313,7 +317,10 @@ import {
313
317
  import { QuicAckRanges } from "nish/net/quic-conn-ack"
314
318
  import {
315
319
  QUIC_RECOVERY_APPLICATION_CAPACITY,
320
+ QUIC_RECOVERY_CUBIC,
321
+ QUIC_RECOVERY_GRANULARITY,
316
322
  QUIC_RECOVERY_HANDSHAKE_CAPACITY,
323
+ QUIC_RECOVERY_NEWRENO,
317
324
  QUIC_RECOVERY_TIMEOUT_LOSS,
318
325
  QUIC_RECOVERY_TIMEOUT_PTO,
319
326
  QuicRecovery,
@@ -609,7 +616,11 @@ export interface QuicServerConfig {
609
616
  /**
610
617
  * `initial_max_stream_data_bidi_remote` (and `_uni`, and `_bidi_local`
611
618
  * when the server opens streams): each stream's receive window and the
612
- * size of its buffer each way, 1 to 1 MiB.
619
+ * size of its buffer each way, 1 to 1 MiB. The send buffer holds what the
620
+ * client has not acknowledged, so it bounds a stream's send-side memory
621
+ * and its bytes in flight: behind one lost packet a stream sends nothing
622
+ * new until the resend is acknowledged, and the connection sends an early
623
+ * ACK-eliciting probe meanwhile so a client's NAT rebinding is still seen.
613
624
  */
614
625
  maxStreamData: i64
615
626
  /** `initial_max_streams_bidi`: how many bidirectional streams the client may have open at once, 0 to 1024. */
@@ -658,6 +669,14 @@ export interface QuicServerConfig {
658
669
  * other pair.
659
670
  */
660
671
  workerCount: i32
672
+ /**
673
+ * The congestion controller every connection runs: `QUIC_RECOVERY_NEWRENO`
674
+ * (RFC 9002) or `QUIC_RECOVERY_CUBIC` (RFC 9438), from
675
+ * `nish/net/quic-recovery`; `QUIC_RECOVERY_DEFAULT` names the one to pass
676
+ * without a reason to pick. Any other value does not fit, and a connection
677
+ * made with it closes at once.
678
+ */
679
+ congestion: i32
661
680
  }
662
681
 
663
682
  /**
@@ -1434,6 +1453,7 @@ const quicConnConfigFits = (config: QuicServerConfig): boolean =>
1434
1453
  config.maxIdleTimeout <= QUIC_MAX_VARINT &&
1435
1454
  config.activeConnectionIdLimit >= 2 &&
1436
1455
  config.activeConnectionIdLimit <= 8 &&
1456
+ (config.congestion === QUIC_RECOVERY_NEWRENO || config.congestion === QUIC_RECOVERY_CUBIC) &&
1437
1457
  toI32(config.statelessResetKey.length) === QUIC_CONN_STATIC_KEY_SIZE
1438
1458
 
1439
1459
  /**
@@ -1585,6 +1605,12 @@ export class QuicConnection {
1585
1605
  challengeUntil: i64 = -1
1586
1606
  /** How many PATH_CHALLENGE data have been drawn over the connection; see `challengeData`. */
1587
1607
  challengesDrawn: i64 = 0
1608
+ /**
1609
+ * The Application Data space's largest acknowledged packet number when
1610
+ * the last early probe was asked for (`earlyProbeAt`), so one goes per
1611
+ * acknowledgment that moves it; -2 before any.
1612
+ */
1613
+ earlyProbeAcked: i64 = -2
1588
1614
  private frame: QuicFrame
1589
1615
  /** The header and the packet numbers of the packet being read, reused for every one. */
1590
1616
  private header: QuicHeader
@@ -1804,6 +1830,8 @@ export class QuicConnection {
1804
1830
  QUIC_CONN_LOCAL_CIDS
1805
1831
  )
1806
1832
  this.recovery = new QuicRecovery()
1833
+ this.recovery.controller =
1834
+ config.congestion === QUIC_RECOVERY_CUBIC ? QUIC_RECOVERY_CUBIC : QUIC_RECOVERY_NEWRENO
1807
1835
  this.defaultParameters = new QuicTransportParameters()
1808
1836
  this.peerParameters = this.defaultParameters
1809
1837
  // A configuration that does not fit closes the connection at once (see
@@ -1901,6 +1929,7 @@ export class QuicConnection {
1901
1929
  this.appReadSlot = 0
1902
1930
  this.appWriteSlot = 0
1903
1931
  this.earlyResends = 0
1932
+ this.earlyProbeAcked = -2
1904
1933
  this.resendLevel = -1
1905
1934
  this.now = 0
1906
1935
  this.idleSince = -1
@@ -2206,6 +2235,7 @@ export class QuicConnection {
2206
2235
  // Loss recovery's timer runs after the datagram, so an acknowledgement
2207
2236
  // it carries settles what it acknowledges before a probe is owed.
2208
2237
  this.runRecoveryTimer()
2238
+ this.runEarlyProbe()
2209
2239
  return this.closed() ? this.error : QUIC_ERROR_NO_ERROR
2210
2240
  }
2211
2241
 
@@ -3787,7 +3817,8 @@ export class QuicConnection {
3787
3817
  /**
3788
3818
  * When `handleTimer` is next due, in the caller's milliseconds: the idle
3789
3819
  * timeout, loss recovery's timer (a time-threshold loss or the probe
3790
- * timeout), the end of a key update's probe timeout, or, while a path is
3820
+ * timeout), an early probe while a stream's send buffer is full behind
3821
+ * bytes in flight, the end of a key update's probe timeout, or, while a path is
3791
3822
  * validated, its PATH_CHALLENGE's resend or the end of its validation,
3792
3823
  * whichever comes first; once the connection has closed, the end of its closing or
3793
3824
  * draining period, when its keys are wiped. -1 when nothing is timed:
@@ -3813,6 +3844,10 @@ export class QuicConnection {
3813
3844
  if (recovery >= 0 && (due < 0 || recovery < due)) {
3814
3845
  due = recovery
3815
3846
  }
3847
+ const early: i64 = this.earlyProbeAt()
3848
+ if (early >= 0 && (due < 0 || early < due)) {
3849
+ due = early
3850
+ }
3816
3851
  if (this.pathValidating) {
3817
3852
  const path: i64 =
3818
3853
  this.challengeResendAt < this.challengeUntil ? this.challengeResendAt : this.challengeUntil
@@ -3858,7 +3893,10 @@ export class QuicConnection {
3858
3893
  * Past loss recovery's timer, packets lost by the time threshold have their
3859
3894
  * frames queued again, or a probe timeout queues every space's packets in
3860
3895
  * flight and asks for a probe (RFC 9002 §6.1.2, §6.2.4): `takeDatagram` then
3861
- * sends it. `receive` and `takeDatagram` run it themselves. Time never runs
3896
+ * sends it. Past the early probe's time, while a stream's send buffer is
3897
+ * full behind bytes in flight, a probe is asked for the same way, so the
3898
+ * client answers from wherever its NAT now sends it.
3899
+ * `receive` and `takeDatagram` run it themselves. Time never runs
3862
3900
  * backwards here: an earlier `now` than one already given counts as that one.
3863
3901
  *
3864
3902
  * @param now The caller's monotonic time in milliseconds.
@@ -3866,6 +3904,7 @@ export class QuicConnection {
3866
3904
  handleTimer(now: i64): void {
3867
3905
  this.runClocks(now)
3868
3906
  this.runRecoveryTimer()
3907
+ this.runEarlyProbe()
3869
3908
  }
3870
3909
 
3871
3910
  /**
@@ -4210,6 +4249,61 @@ export class QuicConnection {
4210
4249
  return this.sealAt(space, out, at, quicPutPadding(out, next, payloadEnd - next))
4211
4250
  }
4212
4251
 
4252
+ /**
4253
+ * When a sender blocked by its own stream buffer sends an ack-eliciting
4254
+ * probe, or -1 when it need not. A stream whose send buffer is full
4255
+ * behind a hole (`QuicStreams.bufferBlocked`) sends nothing new until
4256
+ * the hole is acknowledged; once the client has acknowledged everything
4257
+ * in flight it has nothing to acknowledge and sends nothing either, so a
4258
+ * NAT rebind in that gap is never seen and every later packet, burst and
4259
+ * probe timeout alike, goes to the old binding. Half a smoothed RTT after
4260
+ * the last ack-eliciting packet, with the Application Data space's
4261
+ * largest acknowledged packet moved since the last such probe, one
4262
+ * datagram goes regardless of the window (a PING when nothing else is
4263
+ * queued), so the client always has one to answer, from wherever it now
4264
+ * is (RFC 9000 §9.3). One per acknowledgment that advances the space, so
4265
+ * up to two full datagrams a round trip, a buffer-limited transfer that
4266
+ * lost nothing included, since the probe carries any other stream's queued
4267
+ * data; none while the sent ring is full, where a probe would resend the
4268
+ * oldest packet in flight instead. After a validated migration
4269
+ * `resetPath` puts the smoothed RTT back to the 333 ms initial RTT, so the
4270
+ * probe waits about 166 ms until the new path's first sample. The probe
4271
+ * timeout still runs as before. With the NAT beside the client, a rebind after the probe
4272
+ * arrives and before the next burst does is still missed: only the client
4273
+ * sending can close that half round trip.
4274
+ */
4275
+ private earlyProbeAt(): i64 {
4276
+ if (this.state !== QUIC_STATE_CONNECTED || this.application.probes > 0) {
4277
+ return -1
4278
+ }
4279
+ const sent: QuicSentPackets | null = this.recovery.space(TLS_LEVEL_APPLICATION)
4280
+ if (
4281
+ sent === null ||
4282
+ sent.inFlight === 0 ||
4283
+ sent.lastAckEliciting < 0 ||
4284
+ sent.largestAcked <= this.earlyProbeAcked ||
4285
+ sent.full() ||
4286
+ !this.streams.bufferBlocked()
4287
+ ) {
4288
+ return -1
4289
+ }
4290
+ const half: i64 = this.recovery.smoothedRtt / 2
4291
+ return sent.lastAckEliciting + (half > QUIC_RECOVERY_GRANULARITY ? half : QUIC_RECOVERY_GRANULARITY)
4292
+ }
4293
+
4294
+ /** The early probe of `earlyProbeAt`, if it is due. */
4295
+ private runEarlyProbe(): void {
4296
+ const at: i64 = this.earlyProbeAt()
4297
+ if (at < 0 || this.now < at) {
4298
+ return
4299
+ }
4300
+ const sent: QuicSentPackets | null = this.recovery.space(TLS_LEVEL_APPLICATION)
4301
+ if (sent !== null) {
4302
+ this.earlyProbeAcked = sent.largestAcked
4303
+ this.application.probes = 1
4304
+ }
4305
+ }
4306
+
4213
4307
  /** Loss recovery's timer, if it is due (RFC 9002 §6.2, A.9). */
4214
4308
  private runRecoveryTimer(): void {
4215
4309
  if (this.closed() || this.state === QUIC_STATE_WAIT_INITIAL) {