@amritk/nish-x86_64-linux 0.38.0 → 0.40.1
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.
- package/INSTALL.md +74 -129
- package/bin/nish +0 -0
- package/docs/AI.md +455 -65
- package/package.json +1 -1
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +320 -33
- package/runtime/nish.h +84 -10
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/page-frame.d.ts +123 -0
- package/runtime/page-frame.mjs +736 -0
- package/runtime/page-game.d.ts +132 -0
- package/runtime/page-game.mjs +738 -0
- package/runtime/runtime-aes.c +97 -12
- package/runtime/runtime-async.c +43 -10
- package/runtime/runtime-bytes.c +88 -4
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-net.c +64 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +94 -10
- package/runtime/runtime-sandbox.c +404 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-wasm.c +437 -0
- package/runtime/runtime.c +5 -0
- package/runtime/vite.mjs +1 -1
- package/scripts/build.sh +42 -8
- package/std/README.md +78 -91
- package/std/array.ts +36 -3
- package/std/collections.ts +51 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/hmac.ts +37 -10
- package/std/crypto/p256.ts +1629 -2732
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +151 -13
- package/std/crypto/sha256.ts +34 -6
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +120 -10
- package/std/host/game.ts +273 -0
- package/std/input.ts +56 -0
- package/std/json-tokenizer.ts +344 -111
- package/std/json-typed.ts +256 -12
- package/std/json-value.ts +144 -3
- package/std/json.ts +63 -8
- package/std/net/fetch.ts +22 -4
- package/std/net/handler.ts +130 -16
- package/std/net/http-server.ts +3382 -0
- package/std/net/http1-client-codec.ts +6 -2
- package/std/net/http1-codec.ts +636 -162
- package/std/net/http1-server.ts +569 -42
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +131 -14
- package/std/net/http3-server.ts +29 -3
- package/std/net/http3.ts +180 -7
- package/std/net/postgres-async.ts +1304 -86
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +122 -7
- package/std/net/prefork.ts +195 -11
- package/std/net/quic-datagram.ts +30 -0
- package/std/net/quic-listener.ts +38 -12
- package/std/net/quic-packet.ts +19 -6
- package/std/net/quic-recovery.ts +373 -27
- package/std/net/quic-stream.ts +195 -37
- package/std/net/quic.ts +516 -22
- package/std/net/router.ts +407 -38
- package/std/net/serve.ts +1022 -110
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls/verify.ts +19 -1
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/url.ts +83 -35
- package/std/net/websocket.ts +129 -19
- package/std/net/webtransport.ts +185 -1
- package/std/raster.ts +837 -0
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/release.ts +593 -0
- package/std/secret.ts +26 -59
- package/std/synth.ts +143 -0
- package/std/text.ts +233 -0
- package/std/threads.ts +298 -0
- package/std/yaml-json.ts +368 -28
- package/std/yaml.ts +122 -27
package/std/net/quic-recovery.ts
CHANGED
|
@@ -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).
|
|
@@ -111,11 +123,15 @@ export const QUIC_RECOVERY_PACKET_THRESHOLD: i64 = 3
|
|
|
111
123
|
export const QUIC_RECOVERY_GRANULARITY: i64 = 1
|
|
112
124
|
/** kInitialRtt (§6.2.2), in milliseconds: the round trip assumed before the first sample. */
|
|
113
125
|
export const QUIC_RECOVERY_INITIAL_RTT: i64 = 333
|
|
114
|
-
/**
|
|
126
|
+
/**
|
|
127
|
+
* max_datagram_size (B.1) on a path nothing has been learnt of: the 1200
|
|
128
|
+
* bytes every datagram is at most until path MTU discovery validates more
|
|
129
|
+
* (`QuicRecovery.datagramSize`).
|
|
130
|
+
*/
|
|
115
131
|
export const QUIC_RECOVERY_DATAGRAM_SIZE: i64 = 1200
|
|
116
|
-
/** kInitialWindow (§7.2): min(10 × 1200, max(14720, 2 × 1200)), so 12000 bytes. */
|
|
132
|
+
/** kInitialWindow (§7.2) at 1200 bytes: min(10 × 1200, max(14720, 2 × 1200)), so 12000 bytes. */
|
|
117
133
|
export const QUIC_RECOVERY_INITIAL_WINDOW: i64 = 12000
|
|
118
|
-
/** kMinimumWindow (§7.2): two datagrams, 2400 bytes. */
|
|
134
|
+
/** kMinimumWindow (§7.2) at 1200 bytes: two datagrams, 2400 bytes. */
|
|
119
135
|
export const QUIC_RECOVERY_MINIMUM_WINDOW: i64 = 2400
|
|
120
136
|
/** kPersistentCongestionThreshold (§7.6.1): lost packets spanning three probe timeouts collapse the window. */
|
|
121
137
|
export const QUIC_RECOVERY_PERSISTENT_THRESHOLD: i64 = 3
|
|
@@ -143,6 +159,10 @@ const QUIC_RECOVERY_LOST: i32 = 3
|
|
|
143
159
|
const QUIC_RECOVERY_LOST_NOW: i32 = 4
|
|
144
160
|
/** The bit of a slot's state byte that says its packet was sent marked ECT(0), kept beside the state so a slot costs no second byte. */
|
|
145
161
|
const QUIC_RECOVERY_MARKED: i32 = 128
|
|
162
|
+
/** The bit of a slot's state byte that says its packet was a path MTU probe, whose loss is not congestion (RFC 9000 §14.4). */
|
|
163
|
+
const QUIC_RECOVERY_PROBE: i32 = 64
|
|
164
|
+
/** The state bits of a slot's state byte, below the two flags. */
|
|
165
|
+
const QUIC_RECOVERY_STATE_BITS: i32 = 63
|
|
146
166
|
|
|
147
167
|
/** The ECT(0) codepoint (RFC 3168), the only one this module asks a caller to mark with. */
|
|
148
168
|
export const QUIC_RECOVERY_ECT0: i32 = 2
|
|
@@ -172,9 +192,75 @@ const QUIC_RECOVERY_MICROS: i64 = 1000
|
|
|
172
192
|
*/
|
|
173
193
|
const QUIC_RECOVERY_PACER_WINDOW: i64 = 4294967296
|
|
174
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
|
+
|
|
175
224
|
/** The larger of two times or durations. */
|
|
176
225
|
const quicRecoveryMax = (a: i64, b: i64): i64 => (a > b ? a : b)
|
|
177
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
|
+
|
|
178
264
|
/**
|
|
179
265
|
* The ack-eliciting packets one space sent, in a ring of `capacity` slots
|
|
180
266
|
* in the order they went out: slot `slot(k)` is the `k`-th from the oldest
|
|
@@ -270,7 +356,7 @@ export class QuicSentPackets {
|
|
|
270
356
|
if (s < 0 || s >= toI32(this.state.length)) {
|
|
271
357
|
return QUIC_RECOVERY_FREE
|
|
272
358
|
}
|
|
273
|
-
return toI32(this.state[s]) &
|
|
359
|
+
return toI32(this.state[s]) & QUIC_RECOVERY_STATE_BITS
|
|
274
360
|
}
|
|
275
361
|
|
|
276
362
|
/**
|
|
@@ -279,7 +365,7 @@ export class QuicSentPackets {
|
|
|
279
365
|
*/
|
|
280
366
|
setState(s: i32, value: i32): void {
|
|
281
367
|
if (s >= 0 && s < toI32(this.state.length)) {
|
|
282
|
-
this.state[s] = toU8((toI32(this.state[s]) & QUIC_RECOVERY_MARKED) | value)
|
|
368
|
+
this.state[s] = toU8((toI32(this.state[s]) & (QUIC_RECOVERY_MARKED | QUIC_RECOVERY_PROBE)) | value)
|
|
283
369
|
}
|
|
284
370
|
}
|
|
285
371
|
|
|
@@ -340,7 +426,30 @@ export class QuicSentPackets {
|
|
|
340
426
|
*/
|
|
341
427
|
setMarked(s: i32, value: boolean): void {
|
|
342
428
|
if (s >= 0 && s < toI32(this.state.length)) {
|
|
343
|
-
|
|
429
|
+
const kept: i32 = toI32(this.state[s]) & (QUIC_RECOVERY_STATE_BITS | QUIC_RECOVERY_PROBE)
|
|
430
|
+
this.state[s] = toU8(kept | (value ? QUIC_RECOVERY_MARKED : 0))
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Whether the packet in slot `s` was a path MTU probe (RFC 8899), whose
|
|
436
|
+
* loss says nothing of congestion.
|
|
437
|
+
*
|
|
438
|
+
* @param s A ring slot, as `onPacketSent`, `ackedSlot` or `lostSlot` answers it; any other index is safe.
|
|
439
|
+
* @returns True when `onProbeSent` marked it; false for an index outside the ring.
|
|
440
|
+
*/
|
|
441
|
+
probeAt(s: i32): boolean {
|
|
442
|
+
return s >= 0 && s < toI32(this.state.length) && (toI32(this.state[s]) & QUIC_RECOVERY_PROBE) !== 0
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Sets whether the packet in slot `s` was a path MTU probe.
|
|
447
|
+
* @internal Read in quic-recovery.ts outside the class.
|
|
448
|
+
*/
|
|
449
|
+
setProbe(s: i32, value: boolean): void {
|
|
450
|
+
if (s >= 0 && s < toI32(this.state.length)) {
|
|
451
|
+
const kept: i32 = toI32(this.state[s]) & (QUIC_RECOVERY_STATE_BITS | QUIC_RECOVERY_MARKED)
|
|
452
|
+
this.state[s] = toU8(kept | (value ? QUIC_RECOVERY_PROBE : 0))
|
|
344
453
|
}
|
|
345
454
|
}
|
|
346
455
|
|
|
@@ -515,6 +624,33 @@ export class QuicRecovery {
|
|
|
515
624
|
ssthresh: i64 = 0
|
|
516
625
|
/** congestion_recovery_start_time (B.2), or -1 outside recovery. */
|
|
517
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
|
|
518
654
|
/** The pacer's credit in bytes (§7.7), and the time it was last topped up in microseconds, or -1. */
|
|
519
655
|
pacerBudget: i64 = 0
|
|
520
656
|
/**
|
|
@@ -530,6 +666,15 @@ export class QuicRecovery {
|
|
|
530
666
|
ptoCount: i32 = 0
|
|
531
667
|
/** The space the last `onTimeout` acted on, or -1. */
|
|
532
668
|
timeoutSpace: i32 = -1
|
|
669
|
+
/**
|
|
670
|
+
* max_datagram_size (B.1): the largest datagram the connection sends on
|
|
671
|
+
* this path, 1200 until path MTU discovery validates more. The initial and
|
|
672
|
+
* minimum windows and congestion avoidance's step are counted in it.
|
|
673
|
+
* `resetPath`, which the constructor runs, sets it to
|
|
674
|
+
* `QUIC_RECOVERY_DATAGRAM_SIZE`; a field's initializer
|
|
675
|
+
* must be a literal.
|
|
676
|
+
*/
|
|
677
|
+
datagramSize: i64 = 1200
|
|
533
678
|
/** How many congestion events reduced the window, and how many of them were persistent congestion. */
|
|
534
679
|
congestionEvents: i32 = 0
|
|
535
680
|
/**
|
|
@@ -599,6 +744,7 @@ export class QuicRecovery {
|
|
|
599
744
|
* @internal Read by quic.ts.
|
|
600
745
|
*/
|
|
601
746
|
resetPath(): void {
|
|
747
|
+
this.datagramSize = QUIC_RECOVERY_DATAGRAM_SIZE
|
|
602
748
|
this.latestRtt = 0
|
|
603
749
|
this.smoothedRtt = QUIC_RECOVERY_INITIAL_RTT
|
|
604
750
|
this.rttVar = QUIC_RECOVERY_INITIAL_RTT / 2
|
|
@@ -607,11 +753,63 @@ export class QuicRecovery {
|
|
|
607
753
|
this.congestionWindow = QUIC_RECOVERY_INITIAL_WINDOW
|
|
608
754
|
this.ssthresh = QUIC_MAX_VARINT
|
|
609
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
|
|
610
764
|
this.pacerBudget = QUIC_RECOVERY_INITIAL_WINDOW
|
|
611
765
|
this.pacerTime = -1
|
|
612
766
|
this.pacerRtt = QUIC_RECOVERY_INITIAL_RTT * QUIC_RECOVERY_MICROS
|
|
613
767
|
}
|
|
614
768
|
|
|
769
|
+
/**
|
|
770
|
+
* kInitialWindow (§7.2) for the current datagram size: min(10 × size,
|
|
771
|
+
* max(14720, 2 × size)).
|
|
772
|
+
*
|
|
773
|
+
* @returns The initial window in bytes: 12000 at 1200 bytes.
|
|
774
|
+
*/
|
|
775
|
+
initialWindow(): i64 {
|
|
776
|
+
const ten: i64 = 10 * this.datagramSize
|
|
777
|
+
const floor: i64 = quicRecoveryMax(14720, 2 * this.datagramSize)
|
|
778
|
+
return ten < floor ? ten : floor
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* kMinimumWindow (§7.2) for the current datagram size: two datagrams.
|
|
783
|
+
*
|
|
784
|
+
* @returns The minimum window in bytes: 2400 at 1200 bytes.
|
|
785
|
+
*/
|
|
786
|
+
minimumWindow(): i64 {
|
|
787
|
+
return 2 * this.datagramSize
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/**
|
|
791
|
+
* Sets max_datagram_size to `size` bytes, as path MTU discovery validates
|
|
792
|
+
* a larger path or falls back from one (RFC 9002 §7.2). A window still at
|
|
793
|
+
* the old initial window becomes the new one, so a connection that has
|
|
794
|
+
* learnt nothing of the path yet starts from what the new size allows; a
|
|
795
|
+
* window below the new minimum is raised to it. A smaller size keeps the
|
|
796
|
+
* window as it is, and the pacer's held credit is cut to the new burst.
|
|
797
|
+
*
|
|
798
|
+
* @param size The new datagram size in bytes; anything under 1200 is taken as 1200.
|
|
799
|
+
*/
|
|
800
|
+
setDatagramSize(size: i64): void {
|
|
801
|
+
const next: i64 = quicRecoveryMax(size, QUIC_RECOVERY_DATAGRAM_SIZE)
|
|
802
|
+
const atInitial: boolean = this.congestionWindow === this.initialWindow()
|
|
803
|
+
this.datagramSize = next
|
|
804
|
+
if (atInitial) {
|
|
805
|
+
this.congestionWindow = this.initialWindow()
|
|
806
|
+
}
|
|
807
|
+
this.congestionWindow = quicRecoveryMax(this.congestionWindow, this.minimumWindow())
|
|
808
|
+
if (this.pacerBudget > this.initialWindow()) {
|
|
809
|
+
this.pacerBudget = this.initialWindow()
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
|
|
615
813
|
/**
|
|
616
814
|
* Tests ECN again from the start, as on a new path (RFC 9000 §13.4.2).
|
|
617
815
|
* Every packet still in flight loses its mark: those sent on the old
|
|
@@ -664,10 +862,10 @@ export class QuicRecovery {
|
|
|
664
862
|
* Whether the window has room for another full-sized datagram (B.4). The
|
|
665
863
|
* caller sends nothing ack-eliciting while it has not, except a probe.
|
|
666
864
|
*
|
|
667
|
-
* @returns True when the bytes in flight plus
|
|
865
|
+
* @returns True when the bytes in flight plus `datagramSize` bytes fit the congestion window.
|
|
668
866
|
*/
|
|
669
867
|
canSend(): boolean {
|
|
670
|
-
return this.bytesInFlight() +
|
|
868
|
+
return this.bytesInFlight() + this.datagramSize <= this.congestionWindow
|
|
671
869
|
}
|
|
672
870
|
|
|
673
871
|
/**
|
|
@@ -703,6 +901,7 @@ export class QuicRecovery {
|
|
|
703
901
|
sp.fineSent[s] = this.fineNow(now)
|
|
704
902
|
sp.size[s] = size
|
|
705
903
|
sp.setMarked(s, false)
|
|
904
|
+
sp.setProbe(s, false)
|
|
706
905
|
sp.setState(s, QUIC_RECOVERY_SENT)
|
|
707
906
|
sp.count = sp.count + 1
|
|
708
907
|
sp.inFlight = sp.inFlight + 1
|
|
@@ -712,6 +911,22 @@ export class QuicRecovery {
|
|
|
712
911
|
return s
|
|
713
912
|
}
|
|
714
913
|
|
|
914
|
+
/**
|
|
915
|
+
* Marks the packet in `space`'s slot `slot`, just recorded by
|
|
916
|
+
* `onPacketSent`, as a path MTU probe (RFC 8899): it counts in flight and
|
|
917
|
+
* its acknowledgement grows the window, but its loss is no congestion
|
|
918
|
+
* event and no part of persistent congestion (RFC 9000 §14.4).
|
|
919
|
+
*
|
|
920
|
+
* @param space The space's index, as given to `onPacketSent`.
|
|
921
|
+
* @param slot The slot `onPacketSent` answered; -1 or any other index outside the ring does nothing.
|
|
922
|
+
*/
|
|
923
|
+
onProbeSent(space: i32, slot: i32): void {
|
|
924
|
+
const sp: QuicSentPackets | null = this.space(space)
|
|
925
|
+
if (sp !== null) {
|
|
926
|
+
sp.setProbe(slot, true)
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
|
|
715
930
|
/**
|
|
716
931
|
* The ECN codepoint to mark the next packet with (RFC 9000 A.4): ECT(0)
|
|
717
932
|
* for the first `QUIC_RECOVERY_ECN_TESTING_PACKETS` and, once the path is
|
|
@@ -868,8 +1083,11 @@ export class QuicRecovery {
|
|
|
868
1083
|
}
|
|
869
1084
|
this.detectLoss(sp, now)
|
|
870
1085
|
this.packetsLost(sp, now)
|
|
1086
|
+
if (this.controller === QUIC_RECOVERY_CUBIC) {
|
|
1087
|
+
this.cubicInUse(inUse, now)
|
|
1088
|
+
}
|
|
871
1089
|
for (let k: i32 = 0; k < sp.ackedCount; k += 1) {
|
|
872
|
-
this.packetAcked(sp, sp.ackedSlot(k), inUse)
|
|
1090
|
+
this.packetAcked(sp, sp.ackedSlot(k), inUse, now)
|
|
873
1091
|
}
|
|
874
1092
|
this.ptoCount = 0
|
|
875
1093
|
return sp.ackedCount
|
|
@@ -1068,7 +1286,9 @@ export class QuicRecovery {
|
|
|
1068
1286
|
}
|
|
1069
1287
|
let newest: i64 = -1
|
|
1070
1288
|
for (let k: i32 = 0; k < sp.lostCount; k += 1) {
|
|
1071
|
-
|
|
1289
|
+
if (!sp.probeAt(sp.lostSlot(k))) {
|
|
1290
|
+
newest = quicRecoveryMax(newest, sp.timeAt(sp.lostSlot(k)))
|
|
1291
|
+
}
|
|
1072
1292
|
if (sp.markedAt(sp.lostSlot(k)) && this.ecnState !== QUIC_RECOVERY_ECN_CAPABLE) {
|
|
1073
1293
|
this.ecnTestLost = this.ecnTestLost + 1
|
|
1074
1294
|
}
|
|
@@ -1081,10 +1301,16 @@ export class QuicRecovery {
|
|
|
1081
1301
|
) {
|
|
1082
1302
|
this.ecnState = QUIC_RECOVERY_ECN_FAILED
|
|
1083
1303
|
}
|
|
1084
|
-
|
|
1085
|
-
if (
|
|
1086
|
-
this.
|
|
1304
|
+
// A lost path MTU probe is no sign of congestion (RFC 9000 §14.4): only the others count.
|
|
1305
|
+
if (newest >= 0) {
|
|
1306
|
+
this.congestionEvent(newest, now)
|
|
1307
|
+
}
|
|
1308
|
+
if (newest >= 0 && this.persistentCongestion(sp)) {
|
|
1309
|
+
this.congestionWindow = this.minimumWindow()
|
|
1087
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
|
|
1088
1314
|
this.persistentCongestions = this.persistentCongestions + 1
|
|
1089
1315
|
}
|
|
1090
1316
|
for (let k: i32 = 0; k < sp.lostCount; k += 1) {
|
|
@@ -1109,8 +1335,17 @@ export class QuicRecovery {
|
|
|
1109
1335
|
for (let k: i32 = 0; k < sp.count; k += 1) {
|
|
1110
1336
|
const s: i32 = sp.slot(k)
|
|
1111
1337
|
const state: i32 = sp.stateAt(s)
|
|
1338
|
+
// An acknowledged probe still breaks the run; only a lost one says nothing (RFC 9000 §14.4).
|
|
1339
|
+
if (state === QUIC_RECOVERY_ACKED) {
|
|
1340
|
+
first = -1
|
|
1341
|
+
fresh = false
|
|
1342
|
+
continue
|
|
1343
|
+
}
|
|
1344
|
+
if (sp.probeAt(s)) {
|
|
1345
|
+
continue
|
|
1346
|
+
}
|
|
1112
1347
|
const lost: boolean = state === QUIC_RECOVERY_LOST || state === QUIC_RECOVERY_LOST_NOW
|
|
1113
|
-
if (
|
|
1348
|
+
if (lost && sp.timeAt(s) < this.firstSampleTime) {
|
|
1114
1349
|
first = -1
|
|
1115
1350
|
fresh = false
|
|
1116
1351
|
continue
|
|
@@ -1134,33 +1369,144 @@ export class QuicRecovery {
|
|
|
1134
1369
|
return this.recoveryStart >= 0 && sentTime <= this.recoveryStart
|
|
1135
1370
|
}
|
|
1136
1371
|
|
|
1137
|
-
/**
|
|
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
|
+
*/
|
|
1138
1378
|
private congestionEvent(sentTime: i64, now: i64): void {
|
|
1139
1379
|
if (this.inRecovery(sentTime)) {
|
|
1140
1380
|
return
|
|
1141
1381
|
}
|
|
1142
1382
|
this.recoveryStart = now
|
|
1143
|
-
|
|
1144
|
-
|
|
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
|
+
}
|
|
1393
|
+
this.congestionWindow = quicRecoveryMax(this.ssthresh, this.minimumWindow())
|
|
1145
1394
|
this.congestionEvents = this.congestionEvents + 1
|
|
1146
1395
|
}
|
|
1147
1396
|
|
|
1148
1397
|
/**
|
|
1149
1398
|
* OnPacketAcked (B.5) for slot `s`: in slow start the window grows by the
|
|
1150
|
-
* packet's size
|
|
1151
|
-
* all for a packet sent during
|
|
1152
|
-
* 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).
|
|
1153
1402
|
*/
|
|
1154
|
-
private packetAcked(sp: QuicSentPackets, s: i32, inUse: boolean): void {
|
|
1403
|
+
private packetAcked(sp: QuicSentPackets, s: i32, inUse: boolean, now: i64): void {
|
|
1155
1404
|
if (!inUse || this.inRecovery(sp.timeAt(s))) {
|
|
1156
1405
|
return
|
|
1157
1406
|
}
|
|
1158
1407
|
const size: i64 = toI64(sp.sizeAt(s))
|
|
1159
1408
|
if (this.congestionWindow < this.ssthresh) {
|
|
1160
1409
|
this.congestionWindow = this.congestionWindow + size
|
|
1410
|
+
} else if (this.controller === QUIC_RECOVERY_CUBIC) {
|
|
1411
|
+
this.cubicAcked(size, now)
|
|
1161
1412
|
} else {
|
|
1162
|
-
this.congestionWindow =
|
|
1163
|
-
|
|
1413
|
+
this.congestionWindow = this.congestionWindow + (this.datagramSize * size) / this.congestionWindow
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
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
|
|
1164
1510
|
}
|
|
1165
1511
|
}
|
|
1166
1512
|
|
|
@@ -1369,8 +1715,8 @@ export class QuicRecovery {
|
|
|
1369
1715
|
const rtt: i64 = quicRecoveryMax(this.pacerRtt, 1)
|
|
1370
1716
|
const earned: i64 = (elapsed * this.pacerWindow() * 5) / (rtt * 4)
|
|
1371
1717
|
this.pacerBudget = this.pacerBudget + earned
|
|
1372
|
-
if (this.pacerBudget >
|
|
1373
|
-
this.pacerBudget =
|
|
1718
|
+
if (this.pacerBudget > this.initialWindow()) {
|
|
1719
|
+
this.pacerBudget = this.initialWindow()
|
|
1374
1720
|
}
|
|
1375
1721
|
this.pacerTime = at
|
|
1376
1722
|
}
|