@amritk/nish-aarch64-linux 0.38.0 → 0.39.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 (58) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +348 -37
  3. package/package.json +1 -1
  4. package/runtime/core/host.d.ts +11 -0
  5. package/runtime/core/host.mjs +35 -0
  6. package/runtime/nish.d.ts +301 -18
  7. package/runtime/nish.h +74 -10
  8. package/runtime/nish.mjs +69 -6
  9. package/runtime/node.mjs +117 -0
  10. package/runtime/runtime-aes.c +12 -4
  11. package/runtime/runtime-async.c +43 -10
  12. package/runtime/runtime-checksum.c +207 -15
  13. package/runtime/runtime-net.c +32 -3
  14. package/runtime/runtime-os.c +174 -27
  15. package/runtime/runtime-region.c +94 -10
  16. package/runtime/runtime-sandbox.c +377 -17
  17. package/runtime/runtime-sha256.c +11 -4
  18. package/runtime/runtime-wasm.c +15 -0
  19. package/runtime/runtime.c +5 -0
  20. package/scripts/build.sh +27 -8
  21. package/std/README.md +5 -4
  22. package/std/compress/checksum.ts +13 -0
  23. package/std/compress/deflate.ts +0 -2
  24. package/std/crypto/p256.ts +247 -74
  25. package/std/crypto/p384.ts +3 -1
  26. package/std/crypto/pki.ts +35 -9
  27. package/std/crypto/x25519.ts +153 -64
  28. package/std/crypto/x509.ts +113 -10
  29. package/std/json-tokenizer.ts +20 -0
  30. package/std/json-typed.ts +217 -8
  31. package/std/net/handler.ts +130 -16
  32. package/std/net/http1-codec.ts +499 -150
  33. package/std/net/http1-server.ts +495 -22
  34. package/std/net/http2-cleartext.ts +526 -0
  35. package/std/net/http2-tls.ts +48 -0
  36. package/std/net/http2.ts +84 -0
  37. package/std/net/postgres-async.ts +1304 -86
  38. package/std/net/postgres-carrier.ts +343 -10
  39. package/std/net/postgres.ts +122 -7
  40. package/std/net/prefork.ts +195 -11
  41. package/std/net/quic-datagram.ts +30 -0
  42. package/std/net/quic-listener.ts +38 -12
  43. package/std/net/quic-packet.ts +19 -6
  44. package/std/net/quic-recovery.ts +134 -19
  45. package/std/net/quic-stream.ts +174 -36
  46. package/std/net/quic.ts +418 -18
  47. package/std/net/router.ts +39 -17
  48. package/std/net/serve.ts +162 -58
  49. package/std/net/tls/codec.ts +110 -32
  50. package/std/net/tls/record-server.ts +37 -7
  51. package/std/net/tls-tcp.ts +56 -1
  52. package/std/net/tls.ts +196 -50
  53. package/std/net/websocket.ts +55 -18
  54. package/std/net/webtransport.ts +179 -1
  55. package/std/regex.ts +69 -47
  56. package/std/region.ts +62 -11
  57. package/std/secret.ts +26 -59
  58. package/std/text.ts +233 -0
package/std/net/quic.ts CHANGED
@@ -360,12 +360,24 @@ import { Secret, expose, exposeWith, secret, wipe } from "nish:secret"
360
360
  /** The length of every connection ID this server issues; a short header's DCID is read at this length. */
361
361
  export const QUIC_CONN_CID_LENGTH: i32 = 8
362
362
  /**
363
- * The largest datagram the server sends, and the smallest a client's first
364
- * Initial may arrive in (RFC 9000 §14.1): 1200 bytes, which every QUIC path
365
- * carries, so no path MTU discovery is needed. `takeDatagramInto` needs this
366
- * much room.
363
+ * The most processes a steered UDP group may have (`QuicServerConfig.workerCount`):
364
+ * the worker index is one byte of the connection ID, read modulo the count.
365
+ */
366
+ export const QUIC_WORKER_MAX: i32 = 255
367
+ /**
368
+ * The datagram size every QUIC path carries, and the smallest a client's
369
+ * first Initial may arrive in (RFC 9000 §14.1): 1200 bytes. The server sends
370
+ * no larger datagram until path MTU discovery has validated one (`pathMtu`),
371
+ * and falls back to it when a validated path stops carrying larger ones.
372
+ * `takeDatagramInto` needs this much room.
367
373
  */
368
374
  export const QUIC_CONN_DATAGRAM_SIZE: i32 = 1200
375
+ /**
376
+ * The largest datagram path MTU discovery searches for: 1452 bytes, what a
377
+ * 1500-byte Ethernet MTU carries under an IPv6 and a UDP header. The search
378
+ * also stops at `pathMtuLimit` and at the client's `max_udp_payload_size`.
379
+ */
380
+ export const QUIC_CONN_DATAGRAM_MAX: i32 = 1452
369
381
  /**
370
382
  * How many random bytes the constructor and `reset` take: 32 for the TLS
371
383
  * server random, 32 for the x25519 key, 8 for the first connection ID and
@@ -631,8 +643,53 @@ export interface QuicServerConfig {
631
643
  statelessResetKey: u8[]
632
644
  /** The key `nish/net/quic-listener` authenticates its Retry tokens with (§8.1.4), `QUIC_CONN_STATIC_KEY_SIZE` bytes. */
633
645
  retryTokenKey: u8[]
646
+ /**
647
+ * This process's index k in a prefork group whose UDP sockets are steered
648
+ * by connection ID (`preforkIndex`), or -1 outside one. With a valid pair,
649
+ * every connection ID the server issues carries k in byte 0, so the
650
+ * kernel hands the connection's packets to this process whatever address
651
+ * they come from. Copy it from the supervisor's flags, never compute it.
652
+ */
653
+ workerIndex: i32
654
+ /**
655
+ * How many processes N share the steered UDP port (`preforkUdpCount`), or 0
656
+ * outside a prefork group. Valid with `workerIndex` when 0 <= k < N <= 255,
657
+ * or both unset (-1 and 0); `QuicListener` and `Http3Server` panic on any
658
+ * other pair.
659
+ */
660
+ workerCount: i32
634
661
  }
635
662
 
663
+ /**
664
+ * Panics, with the pair in the message, unless `config`'s worker fields are
665
+ * both unset (`workerIndex` -1 and `workerCount` 0) or name a worker of a
666
+ * steered group (0 <= k < N <= 255). `QuicListener` calls it when it is built,
667
+ * before it reads a datagram, so an `Http3Server` panics through its listener.
668
+ *
669
+ * @param config The configuration whose `workerIndex` and `workerCount` are checked.
670
+ */
671
+ export const quicWorkerCheck = (config: QuicServerConfig): void => {
672
+ const k: i32 = config.workerIndex
673
+ const n: i32 = config.workerCount
674
+ if ((k === -1 && n === 0) || (k >= 0 && k < n && n <= QUIC_WORKER_MAX)) {
675
+ return
676
+ }
677
+ panic(`nish/net/quic: workerIndex ${k} and workerCount ${n}, not 0 <= k < n <= ${QUIC_WORKER_MAX}`)
678
+ }
679
+
680
+ /**
681
+ * The byte a server writes at the start of every connection ID it issues:
682
+ * `config.workerIndex` when the pair names a worker of a steered group, -1
683
+ * (write nothing) otherwise.
684
+ *
685
+ * @param config The server's configuration.
686
+ * @returns The worker index k, from 0 to 254, or -1 when the IDs carry no worker.
687
+ */
688
+ export const quicWorkerByte = (config: QuicServerConfig): i32 =>
689
+ config.workerIndex >= 0 && config.workerIndex < config.workerCount && config.workerCount <= QUIC_WORKER_MAX
690
+ ? config.workerIndex
691
+ : -1
692
+
636
693
  /** One run of stream data the client sent, in order, as `readStream` answers it: `fin` when it ends the stream. */
637
694
  export class QuicStreamData {
638
695
  /** The RFC 9000 stream ID the bytes arrived on. */
@@ -1349,6 +1406,14 @@ const quicConnEphemeralIn = (held: u8[], entropy: u8[]): i32 => {
1349
1406
  return 0
1350
1407
  }
1351
1408
 
1409
+ /** Writes the worker byte (`quicWorkerByte`) into `cid[0]`, when the configuration names a worker. */
1410
+ const quicWorkerMark = (config: QuicServerConfig, cid: u8[]): void => {
1411
+ const k: i32 = quicWorkerByte(config)
1412
+ if (k >= 0 && toI32(cid.length) > 0) {
1413
+ cid[0] = toU8(k)
1414
+ }
1415
+ }
1416
+
1352
1417
  /** Whether the configuration's limits are ones this module can honour. */
1353
1418
  const quicConnConfigFits = (config: QuicServerConfig): boolean =>
1354
1419
  config.maxStreamData >= 1 &&
@@ -1664,6 +1729,49 @@ export class QuicConnection {
1664
1729
  /** Whether the packet being read carries a non-probing frame (§9.1), and whether a packet of the datagram moves the connection (§9.3). */
1665
1730
  private nonProbing: boolean = false
1666
1731
  private migrateOwed: boolean = false
1732
+ /** The packet number of the probe in flight. */
1733
+ private pmtuProbePn: i64 = -1
1734
+ /**
1735
+ * Path MTU discovery (RFC 9000 §14.3, RFC 8899): once the handshake is
1736
+ * confirmed and the path validated, the server probes for a datagram
1737
+ * larger than 1200 bytes with a PING padded to the size being tried, sent
1738
+ * only when nothing else is due, so a probe never holds data back. An
1739
+ * acknowledged probe validates its size as `pathMtu`, which every later
1740
+ * datagram may then fill; three lost probes of a size end the search below
1741
+ * it. A lost probe is no congestion signal (§14.4). Three loss events that
1742
+ * each lose a packet larger than 1200 bytes, with none of that size
1743
+ * acknowledged between, are taken for a black hole: `pathMtu` falls back
1744
+ * to 1200 and the search resumes below the size that stopped getting
1745
+ * through. A new address starts again from 1200.
1746
+ *
1747
+ * `pathMtuLimit` is the carrier's to lower, for an interface whose MTU
1748
+ * carries less than `QUIC_CONN_DATAGRAM_MAX`: before the first datagram
1749
+ * by setting it, and at any time through `setPathMtuLimit`, which also
1750
+ * takes a `pathMtu` above it down to it. The next probe keeps under it,
1751
+ * and an acknowledged probe raises `pathMtu` no higher than it. 1200 or
1752
+ * less turns discovery off. `reset` keeps it.
1753
+ */
1754
+ pathMtuLimit: i32 = 1452
1755
+ /** The largest datagram validated on the current path: 1200 until a probe is acknowledged. */
1756
+ pathMtu: i32 = 1200
1757
+ /** Path MTU probes sent. */
1758
+ pmtuProbes: i32 = 0
1759
+ /** Path MTU probes declared lost. */
1760
+ pmtuProbesLost: i32 = 0
1761
+ /** Times a black hole sent `pathMtu` back to 1200. */
1762
+ pmtuBlackHoles: i32 = 0
1763
+ /** The search's validated floor and the largest size not yet ruled out. */
1764
+ private pmtuLow: i32 = 0
1765
+ private pmtuHigh: i32 = 0
1766
+ /** The size the next probe tries, 0 when the search is over, and the size of the probe in flight, 0 for none. */
1767
+ private pmtuNext: i32 = 0
1768
+ private pmtuProbeSize: i32 = 0
1769
+ /** Probes of `pmtuNext` lost so far, and loss events in a row that lost a packet larger than 1200 bytes. */
1770
+ private pmtuTries: i32 = 0
1771
+ private pmtuSuspect: i32 = 0
1772
+ /** Whether the search has its bounds for this path, and whether the packet being sealed is a probe. */
1773
+ private pmtuStarted: boolean = false
1774
+ private pmtuSealing: boolean = false
1667
1775
 
1668
1776
  /**
1669
1777
  * A connection under `config`, with `entropy` its `QUIC_CONN_ENTROPY_SIZE`
@@ -1708,7 +1816,7 @@ export class QuicConnection {
1708
1816
  this.streams = new QuicStreams(streams, uni, local, buffer, fits ? config.maxData : QUIC_CONN_NONE)
1709
1817
  // The rings are made only when DATAGRAM frames are offered at all.
1710
1818
  const datagrams: i32 = fits && config.maxDatagramFrameSize > 0 ? QUIC_CONN_DATAGRAM_QUEUE : 0
1711
- this.datagramsOut = new QuicDatagramQueue(datagrams, QUIC_CONN_DATAGRAM_SIZE)
1819
+ this.datagramsOut = new QuicDatagramQueue(datagrams, QUIC_CONN_DATAGRAM_MAX)
1712
1820
  this.datagramsIn = new QuicDatagramQueue(
1713
1821
  datagrams,
1714
1822
  fits ? toI32(config.maxDatagramFrameSize) : QUIC_CONN_FROM
@@ -1833,6 +1941,10 @@ export class QuicConnection {
1833
1941
  this.writePhase = false
1834
1942
  this.cryptoArrived = false
1835
1943
  this.topUpOwed = false
1944
+ this.pmtuProbes = 0
1945
+ this.pmtuProbesLost = 0
1946
+ this.pmtuBlackHoles = 0
1947
+ this.pmtuReset()
1836
1948
  this.begin(entropy)
1837
1949
  }
1838
1950
 
@@ -1841,6 +1953,7 @@ export class QuicConnection {
1841
1953
  quicCidCopy(this.serverRandom, entropy, 0, 32)
1842
1954
  exposeWith(this.ephemeral, entropy, quicConnEphemeralIn)
1843
1955
  quicCidCopy(this.localScid, entropy, 64, QUIC_CONN_CID_LENGTH)
1956
+ quicWorkerMark(this.config, this.localScid)
1844
1957
  quicCidCopy(this.cidSeed, entropy, 72, 32)
1845
1958
  const fits: boolean = toI32(entropy.length) === QUIC_CONN_ENTROPY_SIZE && quicConnConfigFits(this.config)
1846
1959
  secureZero(entropy)
@@ -2824,7 +2937,11 @@ export class QuicConnection {
2824
2937
  return
2825
2938
  }
2826
2939
  for (let k: i32 = 0; k < sent.ackedCount; k += 1) {
2827
- this.untrack(space, sent.ackedSlot(k))
2940
+ const slot: i32 = sent.ackedSlot(k)
2941
+ this.untrack(space, slot)
2942
+ if (space.level === TLS_LEVEL_APPLICATION) {
2943
+ this.pmtuAcked(sent, slot)
2944
+ }
2828
2945
  }
2829
2946
  this.packetsLost(space, sent)
2830
2947
  }
@@ -2867,11 +2984,224 @@ export class QuicConnection {
2867
2984
 
2868
2985
  /** Every packet of `space` that loss recovery's last call declared lost: what each carried is queued again. */
2869
2986
  private packetsLost(space: QuicConnSpace, sent: QuicSentPackets): void {
2987
+ let large: boolean = false
2870
2988
  for (let k: i32 = 0; k < sent.lostCount; k += 1) {
2871
- this.requeue(space, sent.lostSlot(k))
2989
+ const slot: i32 = sent.lostSlot(k)
2990
+ this.requeue(space, slot)
2991
+ if (space.level !== TLS_LEVEL_APPLICATION) {
2992
+ continue
2993
+ }
2994
+ if (sent.probeAt(slot)) {
2995
+ this.pmtuProbeLost(sent.pnAt(slot))
2996
+ } else if (sent.sizeAt(slot) > QUIC_CONN_DATAGRAM_SIZE) {
2997
+ large = true
2998
+ }
2999
+ }
3000
+ if (large) {
3001
+ this.pmtuLargeLost()
3002
+ }
3003
+ }
3004
+
3005
+ // ---- Path MTU discovery -------------------------------------------------------
3006
+
3007
+ /**
3008
+ * The largest datagram the server sends now: `pathMtu`, or 1200 while a
3009
+ * new path is being validated.
3010
+ *
3011
+ * @returns The size in bytes, from 1200 to `QUIC_CONN_DATAGRAM_MAX`.
3012
+ */
3013
+ datagramSize(): i32 {
3014
+ return this.pathValidating ? QUIC_CONN_DATAGRAM_SIZE : this.pathMtu
3015
+ }
3016
+
3017
+ /**
3018
+ * Sets `pathMtuLimit`, for a carrier whose interface MTU changed. A
3019
+ * `pathMtu` above the new limit falls to it, no lower than 1200, and
3020
+ * recovery's datagram size with it; the search goes on below it.
3021
+ *
3022
+ * @param limit The largest datagram the interface carries, in bytes; 1200 or less turns discovery off.
3023
+ */
3024
+ setPathMtuLimit(limit: i32): void {
3025
+ this.pathMtuLimit = limit
3026
+ if (this.pathMtu > limit) {
3027
+ this.pathMtu = limit > QUIC_CONN_DATAGRAM_SIZE ? limit : QUIC_CONN_DATAGRAM_SIZE
3028
+ this.pmtuLow = this.pathMtu
3029
+ this.recovery.setDatagramSize(toI64(this.pathMtu))
2872
3030
  }
2873
3031
  }
2874
3032
 
3033
+ /**
3034
+ * Forgets what discovery learnt of the path: `pathMtu` is 1200 again, and
3035
+ * the search starts afresh. Each caller has just reset loss recovery's
3036
+ * path, whose datagram size that puts back to 1200 too.
3037
+ */
3038
+ private pmtuReset(): void {
3039
+ this.pathMtu = QUIC_CONN_DATAGRAM_SIZE
3040
+ this.pmtuStarted = false
3041
+ this.pmtuLow = QUIC_CONN_DATAGRAM_SIZE
3042
+ this.pmtuHigh = QUIC_CONN_DATAGRAM_SIZE
3043
+ this.pmtuNext = 0
3044
+ this.pmtuProbeSize = 0
3045
+ this.pmtuProbePn = -1
3046
+ this.pmtuTries = 0
3047
+ this.pmtuSuspect = 0
3048
+ }
3049
+
3050
+ /**
3051
+ * Whether a probe of `pmtuNext` bytes is due in `room` bytes: the
3052
+ * handshake confirmed, the path validated, the search not over, no probe
3053
+ * in flight, no loss-recovery probe owed, and room in the record and the
3054
+ * congestion window. The search's bounds are taken the first time:
3055
+ * `pathMtuLimit`, `QUIC_CONN_DATAGRAM_MAX` and the client's
3056
+ * `max_udp_payload_size`, whichever is least.
3057
+ */
3058
+ private pmtuDue(room: i32): boolean {
3059
+ if (!this.handshakeComplete || this.state !== QUIC_STATE_CONNECTED || this.pathValidating) {
3060
+ return false
3061
+ }
3062
+ if (!this.pmtuStarted) {
3063
+ this.pmtuStarted = true
3064
+ let high: i64 = toI64(this.pathMtuLimit)
3065
+ if (high > toI64(QUIC_CONN_DATAGRAM_MAX)) {
3066
+ high = toI64(QUIC_CONN_DATAGRAM_MAX)
3067
+ }
3068
+ if (high > this.peerParameters.maxUdpPayloadSize) {
3069
+ high = this.peerParameters.maxUdpPayloadSize
3070
+ }
3071
+ this.pmtuLow = this.pathMtu
3072
+ this.pmtuHigh = high > toI64(this.pathMtu) ? toI32(high) : this.pathMtu
3073
+ this.pmtuNext = this.pmtuHigh > this.pmtuLow ? this.pmtuHigh : 0
3074
+ }
3075
+ // A limit the carrier lowered after the search began holds from the next probe.
3076
+ if (this.pmtuHigh > this.pathMtuLimit && this.pmtuHigh > this.pmtuLow) {
3077
+ this.pmtuHigh = this.pathMtuLimit > this.pmtuLow ? this.pathMtuLimit : this.pmtuLow
3078
+ this.pmtuNext = this.pmtuHigh > this.pmtuLow ? this.pmtuHigh : 0
3079
+ this.pmtuTries = 0
3080
+ }
3081
+ const size: i32 = this.pmtuNext
3082
+ const sent: QuicSentPackets | null = this.recovery.space(TLS_LEVEL_APPLICATION)
3083
+ return (
3084
+ size > 0 &&
3085
+ size <= room &&
3086
+ this.pmtuProbeSize === 0 &&
3087
+ this.application.probes === 0 &&
3088
+ sent !== null &&
3089
+ !sent.full() &&
3090
+ this.recovery.bytesInFlight() + toI64(size) <= this.recovery.congestionWindow
3091
+ )
3092
+ }
3093
+
3094
+ /**
3095
+ * A path MTU probe of `size` bytes into `out` at `at`: a 1-RTT packet of
3096
+ * a PING and PADDING to `size`, sealed and recorded as a probe. Answers
3097
+ * its length, or 0 when it could not be sealed.
3098
+ */
3099
+ private buildPmtuProbe(out: u8[], at: i32, size: i32): i32 {
3100
+ const space: QuicConnSpace = this.application
3101
+ const overhead: i32 = this.overhead(space)
3102
+ if (overhead === 0 || overhead + 1 > size) {
3103
+ return 0
3104
+ }
3105
+ space.clearStaged()
3106
+ const payloadStart: i32 = at + overhead - QUIC_AEAD_TAG_SIZE
3107
+ const payloadEnd: i32 = at + size - QUIC_AEAD_TAG_SIZE
3108
+ const ping: i32 = quicPutTypeOnly(out, payloadStart, payloadEnd, QUIC_FRAME_PING)
3109
+ if (ping < 0) {
3110
+ return 0
3111
+ }
3112
+ space.stagedEliciting = true
3113
+ const pn: i64 = space.nextPn
3114
+ this.pmtuSealing = true
3115
+ const n: i32 = this.sealAt(space, out, at, quicPutPadding(out, ping, payloadEnd - ping))
3116
+ this.pmtuSealing = false
3117
+ if (n <= 0) {
3118
+ return 0
3119
+ }
3120
+ this.pmtuProbeSize = n
3121
+ this.pmtuProbePn = pn
3122
+ if (this.pmtuProbes < 2147483647) {
3123
+ this.pmtuProbes = this.pmtuProbes + 1
3124
+ }
3125
+ if (!this.elicitingSent) {
3126
+ this.idleSince = this.now
3127
+ this.elicitingSent = true
3128
+ }
3129
+ this.bytesSent = this.bytesSent + toI64(n)
3130
+ this.notePathSent(n)
3131
+ return n
3132
+ }
3133
+
3134
+ /**
3135
+ * An acknowledged 1-RTT packet in `sent`'s slot `slot`: the probe in
3136
+ * flight validates its size, no more than `pathMtuLimit` (which may have
3137
+ * fallen while it flew), and a packet larger than 1200 bytes shows the
3138
+ * path still carries one, which clears the black-hole count.
3139
+ */
3140
+ private pmtuAcked(sent: QuicSentPackets, slot: i32): void {
3141
+ if (sent.probeAt(slot)) {
3142
+ if (this.pmtuProbeSize > 0 && sent.pnAt(slot) === this.pmtuProbePn) {
3143
+ const probed: i32 = this.pmtuProbeSize
3144
+ const size: i32 = probed < this.pathMtuLimit ? probed : this.pathMtuLimit
3145
+ this.pmtuProbeSize = 0
3146
+ this.pmtuSuspect = 0
3147
+ if (size > this.pathMtu) {
3148
+ this.pathMtu = size
3149
+ this.recovery.setDatagramSize(toI64(size))
3150
+ }
3151
+ this.pmtuLow = size > this.pmtuLow ? size : this.pmtuLow
3152
+ this.pmtuStep()
3153
+ }
3154
+ } else if (sent.sizeAt(slot) > QUIC_CONN_DATAGRAM_SIZE) {
3155
+ this.pmtuSuspect = 0
3156
+ }
3157
+ }
3158
+
3159
+ /** The probe numbered `pn` was lost: after three of one size, the search goes on below it. */
3160
+ private pmtuProbeLost(pn: i64): void {
3161
+ if (this.pmtuProbesLost < 2147483647) {
3162
+ this.pmtuProbesLost = this.pmtuProbesLost + 1
3163
+ }
3164
+ if (this.pmtuProbeSize === 0 || pn !== this.pmtuProbePn) {
3165
+ return
3166
+ }
3167
+ const size: i32 = this.pmtuProbeSize
3168
+ this.pmtuProbeSize = 0
3169
+ this.pmtuTries = this.pmtuTries + 1
3170
+ if (this.pmtuTries >= 3) {
3171
+ this.pmtuHigh = size - 1
3172
+ this.pmtuStep()
3173
+ }
3174
+ }
3175
+
3176
+ /**
3177
+ * A loss event lost a packet larger than 1200 bytes: the third in a row
3178
+ * with none of that size acknowledged takes the path for a black hole
3179
+ * (RFC 8899 §4.3), and `pathMtu` falls back to 1200.
3180
+ */
3181
+ private pmtuLargeLost(): void {
3182
+ this.pmtuSuspect = this.pmtuSuspect + 1
3183
+ if (this.pmtuSuspect < 3 || this.pathMtu <= QUIC_CONN_DATAGRAM_SIZE) {
3184
+ return
3185
+ }
3186
+ const failed: i32 = this.pathMtu
3187
+ this.pathMtu = QUIC_CONN_DATAGRAM_SIZE
3188
+ this.recovery.setDatagramSize(toI64(QUIC_CONN_DATAGRAM_SIZE))
3189
+ this.pmtuSuspect = 0
3190
+ this.pmtuProbeSize = 0
3191
+ this.pmtuLow = QUIC_CONN_DATAGRAM_SIZE
3192
+ this.pmtuHigh = failed - 1
3193
+ if (this.pmtuBlackHoles < 2147483647) {
3194
+ this.pmtuBlackHoles = this.pmtuBlackHoles + 1
3195
+ }
3196
+ this.pmtuStep()
3197
+ }
3198
+
3199
+ /** The next size to try, halfway between the bounds, or 0 when they are within 16 bytes. */
3200
+ private pmtuStep(): void {
3201
+ this.pmtuTries = 0
3202
+ this.pmtuNext = this.pmtuHigh - this.pmtuLow < 16 ? 0 : (this.pmtuLow + this.pmtuHigh + 1) / 2
3203
+ }
3204
+
2875
3205
  /**
2876
3206
  * Queues again what the packet in row `row` of `space` carried (RFC 9000
2877
3207
  * §13.3): its CRYPTO range, its STREAM chunks with their FIN, HANDSHAKE_DONE,
@@ -3361,6 +3691,7 @@ export class QuicConnection {
3361
3691
  }
3362
3692
  while (this.cids.nextLocal < QUIC_CONN_MAX_ISSUED_CIDS && toI64(this.cids.activeLocal()) < want) {
3363
3693
  this.deriveConnectionId(this.cids.nextLocal)
3694
+ quicWorkerMark(this.config, this.cidScratch)
3364
3695
  quicConnResetTokenInto(
3365
3696
  this.kdf.sha256,
3366
3697
  this.config.statelessResetKey,
@@ -3758,6 +4089,7 @@ export class QuicConnection {
3758
4089
  this.endValidation()
3759
4090
  if (!portOnly) {
3760
4091
  this.recovery.resetPath()
4092
+ this.pmtuReset()
3761
4093
  }
3762
4094
  this.recovery.restartEcn()
3763
4095
  }
@@ -4113,15 +4445,17 @@ export class QuicConnection {
4113
4445
  /**
4114
4446
  * The largest DATAGRAM payload `sendDatagram` takes now: what fits the
4115
4447
  * client's `max_datagram_frame_size` with the frame's type and Length, and
4116
- * a 1-RTT packet of `QUIC_CONN_DATAGRAM_SIZE` with the longest packet
4117
- * number; 0 when the client takes none, or the server sends none.
4448
+ * a 1-RTT packet of `datagramSize()` with the longest packet number; 0
4449
+ * when the client takes none, or the server sends none. It grows when
4450
+ * path MTU discovery validates a larger datagram, and shrinks again on a
4451
+ * fall back to 1200, which drops a queued payload that no longer fits.
4118
4452
  *
4119
- * @returns The payload size in bytes, 0 up to a little under `QUIC_CONN_DATAGRAM_SIZE`.
4453
+ * @returns The payload size in bytes, 0 up to a little under `datagramSize()`.
4120
4454
  */
4121
4455
  maxDatagramPayload(): i32 {
4122
4456
  const entry: QuicCidEntry | null = this.cids.currentPeerEntry()
4123
4457
  const cid: i32 = entry !== null ? entry.length : QUIC_CONN_FROM
4124
- let limit: i64 = toI64(QUIC_CONN_DATAGRAM_SIZE - 1 - cid - 4 - QUIC_AEAD_TAG_SIZE)
4458
+ let limit: i64 = toI64(this.datagramSize() - 1 - cid - 4 - QUIC_AEAD_TAG_SIZE)
4125
4459
  if (this.peerMaxDatagramFrame < limit) {
4126
4460
  limit = this.peerMaxDatagramFrame
4127
4461
  }
@@ -4195,6 +4529,8 @@ export class QuicConnection {
4195
4529
  /**
4196
4530
  * The next datagram to send, or `null` when nothing is due: the
4197
4531
  * allocating form of `takeDatagramInto`, which a test or a recording uses.
4532
+ * It is held to `QUIC_CONN_DATAGRAM_SIZE` bytes, so it never sends a path
4533
+ * MTU probe nor fills a larger validated path.
4198
4534
  *
4199
4535
  * @param now The caller's monotonic time in milliseconds.
4200
4536
  * @returns A new array the caller owns, at most `QUIC_CONN_DATAGRAM_SIZE` bytes, or `null`.
@@ -4207,7 +4543,8 @@ export class QuicConnection {
4207
4543
 
4208
4544
  /**
4209
4545
  * Writes the next datagram to send into `out` at `at`, which needs
4210
- * `QUIC_CONN_DATAGRAM_SIZE` bytes of room, and answers its length, or 0 when
4546
+ * `QUIC_CONN_DATAGRAM_SIZE` bytes of room and may take up to `datagramSize()`,
4547
+ * or a path MTU probe's size (see `takeDatagramWithin`), and answers its length, or 0 when
4211
4548
  * nothing is due (or the room is not there). Call it until it answers 0 after
4212
4549
  * every `receive`, `sign`, write and `close`. It coalesces an Initial, a
4213
4550
  * Handshake and a 1-RTT packet as each has something to carry, pads a
@@ -4229,19 +4566,49 @@ export class QuicConnection {
4229
4566
  * carrier that paces calls `nish/net/quic-listener`'s
4230
4567
  * `quicListenerTakeFlight` instead.
4231
4568
  *
4569
+ * Its contract changed with path MTU discovery: it writes up to the
4570
+ * room in `out` from `at`, capped at `datagramSize()` (never past
4571
+ * `pathMtuLimit` or `QUIC_CONN_DATAGRAM_MAX`), where it once wrote at
4572
+ * most 1200 bytes. A carrier that sizes its buffer from
4573
+ * `QUIC_CONN_DATAGRAM_SIZE` keeps getting datagrams of at most that size
4574
+ * and is never probed past it; one that hands over a larger buffer must
4575
+ * send all of what comes back.
4576
+ *
4232
4577
  * @param out Where the datagram is written.
4233
4578
  * @param at Where in `out` it starts; `out` needs `QUIC_CONN_DATAGRAM_SIZE` bytes from here.
4234
4579
  * @param now The caller's monotonic time in milliseconds; timers due by then run first.
4235
- * @returns The datagram's length in bytes, at most `QUIC_CONN_DATAGRAM_SIZE`, or 0.
4580
+ * @returns The datagram's length in bytes, at most what `out` holds from `at` and `datagramSize()`, or 0.
4236
4581
  */
4237
4582
  takeDatagramInto(out: u8[], at: i32, now: i64): i32 {
4583
+ return this.takeDatagramWithin(out, at, toI32(out.length) - at, now)
4584
+ }
4585
+
4586
+ /**
4587
+ * `takeDatagramInto` with the datagram held to `room` bytes: it is at
4588
+ * most `room` and `datagramSize()`, whichever is less. When nothing else
4589
+ * is due and `room` has space for it, the datagram is a path MTU probe
4590
+ * (see `pathMtu`), so a carrier that wants probing gives at least
4591
+ * `QUIC_CONN_DATAGRAM_MAX` bytes of room, and one that gives 1200 never
4592
+ * probes. A probe never displaces data: a sender that always has
4593
+ * something due does not probe until it runs out, and stays at 1200
4594
+ * bytes until then.
4595
+ *
4596
+ * @param out Where the datagram is written.
4597
+ * @param at Where in `out` it starts.
4598
+ * @param room The most bytes the datagram may take from `at`; under `QUIC_CONN_DATAGRAM_SIZE`, or past the
4599
+ * end of `out`, nothing is written.
4600
+ * @param now The caller's monotonic time in milliseconds; timers due by then run first.
4601
+ * @returns The datagram's length in bytes, at most `room` and `QUIC_CONN_DATAGRAM_MAX`, or 0.
4602
+ */
4603
+ takeDatagramWithin(out: u8[], at: i32, room: i32, now: i64): i32 {
4238
4604
  this.handleTimer(now)
4239
4605
  if (
4240
4606
  this.state === QUIC_STATE_WAIT_INITIAL ||
4241
4607
  this.state === QUIC_STATE_DRAINING ||
4242
4608
  this.state === QUIC_STATE_TIMED_OUT ||
4243
4609
  at < 0 ||
4244
- at > toI32(out.length) - QUIC_CONN_DATAGRAM_SIZE
4610
+ room < QUIC_CONN_DATAGRAM_SIZE ||
4611
+ at > toI32(out.length) - room
4245
4612
  ) {
4246
4613
  return 0
4247
4614
  }
@@ -4262,9 +4629,13 @@ export class QuicConnection {
4262
4629
  }
4263
4630
  this.beforeDatagram()
4264
4631
  this.datagramEcn = this.ecnMarking ? this.recovery.ecnCodepoint() : 0
4632
+ const size: i32 = this.datagramSize()
4265
4633
  {
4266
4634
  using _scope = arena()
4267
- n = this.buildDatagram(out, at)
4635
+ n = this.buildDatagram(out, at, room < size ? room : size)
4636
+ if (n === 0 && this.pmtuDue(room)) {
4637
+ n = this.buildPmtuProbe(out, at, this.pmtuNext)
4638
+ }
4268
4639
  }
4269
4640
  return n
4270
4641
  }
@@ -4301,8 +4672,8 @@ export class QuicConnection {
4301
4672
  * last padded when an Initial elicits, then each sealed where it lies.
4302
4673
  * Answers the datagram's length, or 0 when no space had anything.
4303
4674
  */
4304
- private buildDatagram(out: u8[], at: i32): i32 {
4305
- let remaining: i32 = QUIC_CONN_DATAGRAM_SIZE
4675
+ private buildDatagram(out: u8[], at: i32, size: i32): i32 {
4676
+ let remaining: i32 = size
4306
4677
  // A path being validated has its own 3x limit (§9.3.1), which the
4307
4678
  // datagram may not pass; `amplificationBlocked` kept it above the floor.
4308
4679
  const budget: i64 = this.pathBudget()
@@ -4625,11 +4996,19 @@ export class QuicConnection {
4625
4996
  space.stageControl(streams.lastKind, streams.lastValue)
4626
4997
  }
4627
4998
  // DATAGRAMs ride in any packet the window lets elicit, and are recorded
4628
- // only by size: a lost one is not sent again (RFC 9221 §5.2).
4999
+ // only by size: a lost one is not sent again (RFC 9221 §5.2). One queued
5000
+ // for a larger path than the datagram size allows now never fits: it is
5001
+ // dropped rather than left to hold the rest back. While a new path is
5002
+ // validated it is not dropped but moved behind the others, which go out
5003
+ // ahead of it, since a path that keeps `pathMtu` carries it once
5004
+ // validated (RFC 9221 §5 promises no order).
5005
+ const most: i32 = this.maxDatagramPayload()
5006
+ this.datagramsPassOversized(most)
4629
5007
  let datagram: i32 = this.datagramsOut.putFrame(out, at, end)
4630
5008
  while (datagram > at) {
4631
5009
  at = datagram
4632
5010
  space.stagedEliciting = true
5011
+ this.datagramsPassOversized(most)
4633
5012
  datagram = this.datagramsOut.putFrame(out, at, end)
4634
5013
  }
4635
5014
  streams.beginPacket()
@@ -4644,6 +5023,24 @@ export class QuicConnection {
4644
5023
  return at
4645
5024
  }
4646
5025
 
5026
+ /**
5027
+ * Clears the head of the DATAGRAM queue of every payload over `most`
5028
+ * bytes: dropped, or while a new path is validated, moved to the back, at
5029
+ * most once each so a queue of nothing but large ones ends the pass.
5030
+ */
5031
+ private datagramsPassOversized(most: i32): void {
5032
+ const queue: QuicDatagramQueue = this.datagramsOut
5033
+ let passes: i32 = queue.count
5034
+ while (passes > 0 && queue.peekLength() > most) {
5035
+ if (this.pathValidating) {
5036
+ queue.deferOldest()
5037
+ } else {
5038
+ queue.dropOldest()
5039
+ }
5040
+ passes = passes - 1
5041
+ }
5042
+ }
5043
+
4647
5044
  /**
4648
5045
  * Seals the packet of `space` whose payload `buildDatagram` wrote from
4649
5046
  * `start` plus its header to `payloadEnd`: the header written in front of
@@ -4702,6 +5099,9 @@ export class QuicConnection {
4702
5099
  this.sealedUnderKey = this.sealedUnderKey + 1
4703
5100
  }
4704
5101
  const slot: i32 = this.recordSent(space, pn, end - start)
5102
+ if (this.pmtuSealing) {
5103
+ this.recovery.onProbeSent(space.level, slot)
5104
+ }
4705
5105
  if (this.datagramEcn !== 0) {
4706
5106
  this.recovery.onEctSent(space.level, slot)
4707
5107
  }
package/std/net/router.ts CHANGED
@@ -156,9 +156,13 @@ export class Router {
156
156
  private maxMethods: i32 = 0
157
157
  /** The most segments any route has: a path is split no further than one past it. */
158
158
  private deepest: i32 = 0
159
- /** During a match: how many segments the path has, where it ends, the method's index, and the pairs in `found`. */
159
+ /** During a match: how many segments the path has, the method's index, and the pairs in `found`. */
160
160
  private segments: i32 = 0
161
- private pathEnd: i32 = 0
161
+ /**
162
+ * Where the last `match`'s path ends in its target: the first `?`, or the target's end.
163
+ * @internal Read by std/net/handler.ts, so a request's path is not scanned twice.
164
+ */
165
+ pathEnd: i32 = 0
162
166
  private wanted: i32 = 0
163
167
  private foundCount: i32 = 0
164
168
  /** During a match: whether a route of another method ended at the path's end. */
@@ -449,28 +453,39 @@ export class Router {
449
453
  }
450
454
  this.visited = 0
451
455
  if (tStart === tEnd || toI32(target[tStart]) !== 47) {
456
+ let end: i32 = tStart
457
+ while (end < tEnd && toI32(target[end]) !== 63) {
458
+ end += 1
459
+ }
460
+ this.pathEnd = end
452
461
  return ROUTER_NOT_FOUND
453
462
  }
454
463
  // One pass over the path: where it ends, and each segment's span up to one
455
464
  // past the deepest route, which is as far as any edge can reach.
456
- let end: i32 = tStart
457
- while (end < tEnd && toI32(target[end]) !== 63) {
458
- end += 1
459
- }
460
465
  const keep: i32 = this.deepest + 1
461
466
  let segments: i32 = 0
462
467
  let start: i32 = tStart + 1
463
- for (let i: i32 = tStart + 1; i <= end; i++) {
464
- if (i >= 0 && i < end && i < toI32(target.length) && toI32(target[i]) !== 47) {
465
- continue
468
+ let end: i32 = start
469
+ while (end < tEnd) {
470
+ const c: i32 = toI32(target[end])
471
+ if (c === 63) {
472
+ break
466
473
  }
467
- if (segments < keep) {
468
- this.segStart[segments] = start
469
- this.segEnd[segments] = i
474
+ if (c === 47) {
475
+ if (segments < keep) {
476
+ this.segStart[segments] = start
477
+ this.segEnd[segments] = end
478
+ }
479
+ segments += 1
480
+ start = end + 1
470
481
  }
471
- segments += 1
472
- start = i + 1
482
+ end += 1
483
+ }
484
+ if (segments < keep) {
485
+ this.segStart[segments] = start
486
+ this.segEnd[segments] = end
473
487
  }
488
+ segments += 1
474
489
  this.segments = segments
475
490
  this.pathEnd = end
476
491
  this.wanted = -1
@@ -698,12 +713,19 @@ const routerLabelIsText = (bytes: u8[], at: i32, text: string, from: i32, length
698
713
  return true
699
714
  }
700
715
 
701
- /** Whether `a[at .. at + length)` and `b[from .. from + length)` hold the same bytes. */
716
+ /** Whether `a[at .. at + length)` and `b[from .. from + length)` hold the same bytes; a range outside its array does not. */
702
717
  const routerSameBytes = (a: u8[], at: i32, b: u8[], from: i32, length: i32): boolean => {
703
- for (let k: i32 = 0; k < length; k++) {
704
- if (a[at + k] !== b[from + k]) {
718
+ // Two cursors, so no index is a sum that must be checked for overflow. `stop`
719
+ // is one sum, but every caller passes a label's or a method's offset and
720
+ // length inside an array whose length is an i32, so it cannot overflow; a
721
+ // range past either end fails the per-byte checks and answers false.
722
+ const stop: i32 = at + length
723
+ let j: i32 = from
724
+ for (let i: i32 = at; i < stop; i++) {
725
+ if (i < 0 || i >= toI32(a.length) || j < 0 || j >= toI32(b.length) || a[i] !== b[j]) {
705
726
  return false
706
727
  }
728
+ j++
707
729
  }
708
730
  return true
709
731
  }