raiha 0.1.0 → 0.2.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 (347) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +33 -0
  3. data/LICENSE +202 -0
  4. data/README.md +94 -22
  5. data/docs/configuration.md +227 -0
  6. data/docs/development.md +99 -0
  7. data/docs/fiber-scheduler.md +591 -0
  8. data/docs/getting-started.md +190 -0
  9. data/docs/http3-client.md +303 -0
  10. data/docs/http3-server.md +301 -0
  11. data/docs/platforms.md +98 -0
  12. data/docs/qlog.md +86 -0
  13. data/docs/quic.md +268 -0
  14. data/docs/threads.md +127 -0
  15. data/examples/_certificate.rb +55 -0
  16. data/examples/high_level_loopback.rb +59 -0
  17. data/examples/http3_get_public.rb +68 -0
  18. data/examples/in_memory_handshake.rb +38 -0
  19. data/examples/qlog_output.rb +58 -0
  20. data/examples/qlog_roundtrips.rb +66 -0
  21. data/examples/quic_gem_loopback.rb +62 -0
  22. data/examples/stream_echo.rb +68 -0
  23. data/examples/udp_handshake.rb +76 -0
  24. data/lib/raiha/http3/action.rb +21 -0
  25. data/lib/raiha/http3/capsule.rb +86 -0
  26. data/lib/raiha/http3/connection.rb +983 -0
  27. data/lib/raiha/http3/error.rb +24 -0
  28. data/lib/raiha/http3/error_code.rb +27 -0
  29. data/lib/raiha/http3/event.rb +63 -0
  30. data/lib/raiha/http3/frame.rb +148 -0
  31. data/lib/raiha/http3/frame_reader.rb +114 -0
  32. data/lib/raiha/http3/message.rb +137 -0
  33. data/lib/raiha/http3/output.rb +61 -0
  34. data/lib/raiha/http3/priority.rb +52 -0
  35. data/lib/raiha/http3/qlog.rb +145 -0
  36. data/lib/raiha/http3/qpack/decoder.rb +302 -0
  37. data/lib/raiha/http3/qpack/dynamic_table.rb +83 -0
  38. data/lib/raiha/http3/qpack/encoder.rb +94 -0
  39. data/lib/raiha/http3/qpack/error.rb +39 -0
  40. data/lib/raiha/http3/qpack/error_code.rb +13 -0
  41. data/lib/raiha/http3/qpack/field.rb +15 -0
  42. data/lib/raiha/http3/qpack/huffman.rb +397 -0
  43. data/lib/raiha/http3/qpack/reader.rb +82 -0
  44. data/lib/raiha/http3/qpack/result.rb +26 -0
  45. data/lib/raiha/http3/qpack/static_table.rb +139 -0
  46. data/lib/raiha/http3/qpack/writer.rb +44 -0
  47. data/lib/raiha/http3/qpack.rb +26 -0
  48. data/lib/raiha/http3/receiving.rb +46 -0
  49. data/lib/raiha/http3/sending.rb +23 -0
  50. data/lib/raiha/http3/session.rb +31 -0
  51. data/lib/raiha/http3/settings.rb +81 -0
  52. data/lib/raiha/http3/structured_field.rb +120 -0
  53. data/lib/raiha/http3/tracer.rb +26 -0
  54. data/lib/raiha/http3/varint.rb +45 -0
  55. data/lib/raiha/http3.rb +19 -0
  56. data/lib/raiha/http3_binding.rb +139 -0
  57. data/lib/raiha/namespace.rb +11 -0
  58. data/lib/raiha/quic/config.rb +229 -0
  59. data/lib/raiha/quic/congestion/algorithms.rb +17 -0
  60. data/lib/raiha/quic/congestion/cubic.rb +258 -0
  61. data/lib/raiha/quic/congestion/new_reno.rb +161 -0
  62. data/lib/raiha/quic/congestion/pacing.rb +21 -0
  63. data/lib/raiha/quic/congestion/startup/rapid_start.rb +249 -0
  64. data/lib/raiha/quic/congestion/startup.rb +13 -0
  65. data/lib/raiha/quic/congestion.rb +15 -0
  66. data/lib/raiha/quic/connection.rb +2022 -0
  67. data/lib/raiha/quic/connection_ids.rb +62 -0
  68. data/lib/raiha/quic/crypto/aead.rb +68 -0
  69. data/lib/raiha/quic/crypto/header_protection.rb +42 -0
  70. data/lib/raiha/quic/crypto/initial.rb +23 -0
  71. data/lib/raiha/quic/crypto/keys.rb +32 -0
  72. data/lib/raiha/quic/crypto/mac.rb +15 -0
  73. data/lib/raiha/quic/crypto/random.rb +13 -0
  74. data/lib/raiha/quic/crypto.rb +18 -0
  75. data/lib/raiha/quic/crypto_stream.rb +161 -0
  76. data/lib/raiha/quic/datagrams.rb +91 -0
  77. data/lib/raiha/quic/endpoint.rb +785 -0
  78. data/lib/raiha/quic/error.rb +61 -0
  79. data/lib/raiha/quic/error_code.rb +34 -0
  80. data/lib/raiha/quic/event.rb +59 -0
  81. data/lib/raiha/quic/issued_connection_ids.rb +137 -0
  82. data/lib/raiha/quic/kept_packets.rb +41 -0
  83. data/lib/raiha/quic/mtu_search.rb +75 -0
  84. data/lib/raiha/quic/negotiation.rb +143 -0
  85. data/lib/raiha/quic/new_token.rb +55 -0
  86. data/lib/raiha/quic/pacer.rb +52 -0
  87. data/lib/raiha/quic/packet_protection.rb +310 -0
  88. data/lib/raiha/quic/path.rb +473 -0
  89. data/lib/raiha/quic/peer_connection_ids.rb +160 -0
  90. data/lib/raiha/quic/qlog.rb +260 -0
  91. data/lib/raiha/quic/ranges.rb +32 -0
  92. data/lib/raiha/quic/recovery/duty.rb +102 -0
  93. data/lib/raiha/quic/recovery/packet_number_space.rb +420 -0
  94. data/lib/raiha/quic/recovery/rate_meter.rb +95 -0
  95. data/lib/raiha/quic/recovery/rtt_estimator.rb +57 -0
  96. data/lib/raiha/quic/recovery/sent_packet.rb +16 -0
  97. data/lib/raiha/quic/recovery.rb +641 -0
  98. data/lib/raiha/quic/remembered_transport_parameters.rb +29 -0
  99. data/lib/raiha/quic/retry_token.rb +54 -0
  100. data/lib/raiha/quic/route.rb +37 -0
  101. data/lib/raiha/quic/scheduler.rb +112 -0
  102. data/lib/raiha/quic/session.rb +47 -0
  103. data/lib/raiha/quic/stateless_reset.rb +38 -0
  104. data/lib/raiha/quic/stats.rb +24 -0
  105. data/lib/raiha/quic/streams/receive_window.rb +119 -0
  106. data/lib/raiha/quic/streams/receiver.rb +217 -0
  107. data/lib/raiha/quic/streams/sender.rb +200 -0
  108. data/lib/raiha/quic/streams.rb +687 -0
  109. data/lib/raiha/quic/test/bottleneck.rb +55 -0
  110. data/lib/raiha/quic/test/congestion_contract.rb +106 -0
  111. data/lib/raiha/quic/test/network.rb +244 -0
  112. data/lib/raiha/quic/test/random_loss.rb +34 -0
  113. data/lib/raiha/quic/test/reorder.rb +51 -0
  114. data/lib/raiha/quic/test/server.rb +54 -0
  115. data/lib/raiha/quic/test/vectors.rb +337 -0
  116. data/lib/raiha/quic/test/virtual_clock.rb +21 -0
  117. data/lib/raiha/quic/test.rb +17 -0
  118. data/lib/raiha/quic/tls_binding.rb +179 -0
  119. data/lib/raiha/quic/token.rb +28 -0
  120. data/lib/raiha/quic/tracer.rb +127 -0
  121. data/lib/raiha/quic/transmit.rb +14 -0
  122. data/lib/raiha/quic/transport_parameters.rb +244 -0
  123. data/lib/raiha/quic/version.rb +63 -0
  124. data/lib/raiha/quic/wire/codec.rb +136 -0
  125. data/lib/raiha/quic/wire/frame.rb +521 -0
  126. data/lib/raiha/quic/wire/header.rb +90 -0
  127. data/lib/raiha/quic/wire/packet.rb +130 -0
  128. data/lib/raiha/quic/wire/packet_number.rb +47 -0
  129. data/lib/raiha/quic/wire/retry.rb +62 -0
  130. data/lib/raiha/quic/wire/version_negotiation.rb +53 -0
  131. data/lib/raiha/quic/wire.rb +9 -0
  132. data/lib/raiha/quic.rb +46 -0
  133. data/lib/raiha/runtime/addresses.rb +35 -0
  134. data/lib/raiha/runtime/admission.rb +37 -0
  135. data/lib/raiha/runtime/client.rb +190 -0
  136. data/lib/raiha/runtime/driver.rb +165 -0
  137. data/lib/raiha/runtime/error.rb +16 -0
  138. data/lib/raiha/runtime/http3.rb +363 -0
  139. data/lib/raiha/runtime/http3_fields.rb +38 -0
  140. data/lib/raiha/runtime/http3_server.rb +416 -0
  141. data/lib/raiha/runtime/kept.rb +36 -0
  142. data/lib/raiha/runtime/qlog.rb +204 -0
  143. data/lib/raiha/runtime/rack.rb +180 -0
  144. data/lib/raiha/runtime/server.rb +334 -0
  145. data/lib/raiha/runtime/session.rb +447 -0
  146. data/lib/raiha/runtime/threaded/client.rb +161 -0
  147. data/lib/raiha/runtime/threaded/driver.rb +230 -0
  148. data/lib/raiha/runtime/threaded/http3.rb +287 -0
  149. data/lib/raiha/runtime/threaded/http3_server.rb +432 -0
  150. data/lib/raiha/runtime/threaded/server.rb +290 -0
  151. data/lib/raiha/runtime/threaded/session.rb +604 -0
  152. data/lib/raiha/runtime/threaded.rb +45 -0
  153. data/lib/raiha/runtime/udp.rb +410 -0
  154. data/lib/raiha/runtime/worker.rb +138 -0
  155. data/lib/raiha/runtime/workers.rb +248 -0
  156. data/lib/raiha/runtime.rb +37 -0
  157. data/lib/raiha/tls/anti_replay.rb +139 -0
  158. data/lib/raiha/tls/cipher_suite.rb +62 -0
  159. data/lib/raiha/tls/codec/error.rb +43 -0
  160. data/lib/raiha/tls/codec/reader.rb +95 -0
  161. data/lib/raiha/tls/codec/writer.rb +81 -0
  162. data/lib/raiha/tls/codec.rb +5 -0
  163. data/lib/raiha/tls/config.rb +318 -0
  164. data/lib/raiha/tls/crypto/aead.rb +64 -0
  165. data/lib/raiha/tls/crypto/certificate.rb +28 -0
  166. data/lib/raiha/tls/crypto/digest.rb +30 -0
  167. data/lib/raiha/tls/crypto/ecdh.rb +74 -0
  168. data/lib/raiha/tls/crypto/hash.rb +22 -0
  169. data/lib/raiha/tls/crypto/hkdf.rb +28 -0
  170. data/lib/raiha/tls/crypto/signature.rb +87 -0
  171. data/lib/raiha/tls/crypto.rb +19 -0
  172. data/lib/raiha/tls/error.rb +36 -0
  173. data/lib/raiha/tls/event.rb +45 -0
  174. data/lib/raiha/tls/extension/alpn.rb +35 -0
  175. data/lib/raiha/tls/extension/certificate_authorities.rb +35 -0
  176. data/lib/raiha/tls/extension/compress_certificate.rb +33 -0
  177. data/lib/raiha/tls/extension/cookie.rb +27 -0
  178. data/lib/raiha/tls/extension/early_data.rb +54 -0
  179. data/lib/raiha/tls/extension/key_share.rb +90 -0
  180. data/lib/raiha/tls/extension/list.rb +49 -0
  181. data/lib/raiha/tls/extension/opaque.rb +21 -0
  182. data/lib/raiha/tls/extension/placement.rb +55 -0
  183. data/lib/raiha/tls/extension/post_handshake_auth.rb +22 -0
  184. data/lib/raiha/tls/extension/pre_shared_key.rb +81 -0
  185. data/lib/raiha/tls/extension/psk_key_exchange_modes.rb +32 -0
  186. data/lib/raiha/tls/extension/record_size_limit.rb +30 -0
  187. data/lib/raiha/tls/extension/registry.rb +50 -0
  188. data/lib/raiha/tls/extension/server_name.rb +63 -0
  189. data/lib/raiha/tls/extension/signature_algorithms.rb +39 -0
  190. data/lib/raiha/tls/extension/signature_algorithms_cert.rb +21 -0
  191. data/lib/raiha/tls/extension/supported_groups.rb +32 -0
  192. data/lib/raiha/tls/extension/supported_versions.rb +53 -0
  193. data/lib/raiha/tls/extension.rb +32 -0
  194. data/lib/raiha/tls/handshake/acceptor.rb +164 -0
  195. data/lib/raiha/tls/handshake/authentication_messages.rb +127 -0
  196. data/lib/raiha/tls/handshake/certificate.rb +56 -0
  197. data/lib/raiha/tls/handshake/certificate_request.rb +39 -0
  198. data/lib/raiha/tls/handshake/certificate_verify.rb +40 -0
  199. data/lib/raiha/tls/handshake/client_hello.rb +208 -0
  200. data/lib/raiha/tls/handshake/client_hello_builder.rb +144 -0
  201. data/lib/raiha/tls/handshake/client_side.rb +578 -0
  202. data/lib/raiha/tls/handshake/conductor.rb +390 -0
  203. data/lib/raiha/tls/handshake/encrypted_extensions.rb +33 -0
  204. data/lib/raiha/tls/handshake/end_of_early_data.rb +28 -0
  205. data/lib/raiha/tls/handshake/finished.rb +36 -0
  206. data/lib/raiha/tls/handshake/key_update.rb +38 -0
  207. data/lib/raiha/tls/handshake/key_update_handling.rb +88 -0
  208. data/lib/raiha/tls/handshake/machine.rb +238 -0
  209. data/lib/raiha/tls/handshake/message.rb +25 -0
  210. data/lib/raiha/tls/handshake/new_session_ticket.rb +48 -0
  211. data/lib/raiha/tls/handshake/reassembler.rb +88 -0
  212. data/lib/raiha/tls/handshake/server_hello.rb +98 -0
  213. data/lib/raiha/tls/handshake/server_side.rb +672 -0
  214. data/lib/raiha/tls/handshake/violation.rb +12 -0
  215. data/lib/raiha/tls/handshake.rb +62 -0
  216. data/lib/raiha/tls/iana.rb +113 -0
  217. data/lib/raiha/tls/key_exchange.rb +58 -0
  218. data/lib/raiha/tls/key_schedule.rb +269 -0
  219. data/lib/raiha/tls/keylog.rb +40 -0
  220. data/lib/raiha/tls/keyring.rb +180 -0
  221. data/lib/raiha/tls/masked_inspect.rb +33 -0
  222. data/lib/raiha/tls/output.rb +32 -0
  223. data/lib/raiha/tls/profile.rb +82 -0
  224. data/lib/raiha/tls/record/layer.rb +228 -0
  225. data/lib/raiha/tls/record/protection.rb +67 -0
  226. data/lib/raiha/tls/record.rb +11 -0
  227. data/lib/raiha/tls/server_name.rb +42 -0
  228. data/lib/raiha/tls/session.rb +106 -0
  229. data/lib/raiha/tls/signature_scheme.rb +35 -0
  230. data/lib/raiha/tls/tcp/acceptor.rb +121 -0
  231. data/lib/raiha/tls/tcp/connection.rb +371 -0
  232. data/lib/raiha/tls/tcp/event.rb +15 -0
  233. data/lib/raiha/tls/tcp/socket.rb +161 -0
  234. data/lib/raiha/tls/tcp.rb +14 -0
  235. data/lib/raiha/tls/test/fixed_kem.rb +30 -0
  236. data/lib/raiha/tls/test/fixed_random.rb +18 -0
  237. data/lib/raiha/tls/test/rfc8448.rb +923 -0
  238. data/lib/raiha/tls/test/rfc8448_handshakes.rb +118 -0
  239. data/lib/raiha/tls/test.rb +13 -0
  240. data/lib/raiha/tls/ticket.rb +184 -0
  241. data/lib/raiha/tls/ticket_keys.rb +206 -0
  242. data/lib/raiha/tls/trace.rb +58 -0
  243. data/lib/raiha/tls/transcript.rb +94 -0
  244. data/lib/raiha/tls/verifier/default.rb +72 -0
  245. data/lib/raiha/tls/verifier/host_name.rb +75 -0
  246. data/lib/raiha/tls/verifier/none.rb +14 -0
  247. data/lib/raiha/tls/verifier.rb +24 -0
  248. data/lib/raiha/tls.rb +34 -0
  249. data/lib/raiha/version.rb +3 -1
  250. data/lib/raiha.rb +8 -5
  251. data/sig/patches/array.rbs +9 -0
  252. data/sig/patches/io_buffer.rbs +13 -0
  253. data/sig/patches/openssl.rbs +25 -0
  254. data/sig/patches/socket.rbs +29 -0
  255. data/sig/patches/string.rbs +26 -0
  256. data/sig/patches/strscan.rbs +12 -0
  257. data/sig/raiha/http3/capsule.rbs +32 -0
  258. data/sig/raiha/http3/connection.rbs +324 -0
  259. data/sig/raiha/http3/frame.rbs +138 -0
  260. data/sig/raiha/http3/frame_reader.rbs +39 -0
  261. data/sig/raiha/http3/message.rbs +23 -0
  262. data/sig/raiha/http3/priority.rbs +40 -0
  263. data/sig/raiha/http3/qlog.rbs +54 -0
  264. data/sig/raiha/http3/qpack.rbs +246 -0
  265. data/sig/raiha/http3/session.rbs +11 -0
  266. data/sig/raiha/http3/settings.rbs +26 -0
  267. data/sig/raiha/http3.rbs +2 -0
  268. data/sig/raiha/http3_binding.rbs +26 -0
  269. data/sig/raiha/namespace.rbs +2 -0
  270. data/sig/raiha/quic/config.rbs +89 -0
  271. data/sig/raiha/quic/congestion.rbs +213 -0
  272. data/sig/raiha/quic/connection.rbs +249 -0
  273. data/sig/raiha/quic/connection_ids.rbs +34 -0
  274. data/sig/raiha/quic/crypto.rbs +78 -0
  275. data/sig/raiha/quic/crypto_stream.rbs +34 -0
  276. data/sig/raiha/quic/datagrams.rbs +32 -0
  277. data/sig/raiha/quic/endpoint.rbs +179 -0
  278. data/sig/raiha/quic/frame.rbs +387 -0
  279. data/sig/raiha/quic/issued_connection_ids.rbs +47 -0
  280. data/sig/raiha/quic/kept_packets.rbs +25 -0
  281. data/sig/raiha/quic/mtu_search.rbs +21 -0
  282. data/sig/raiha/quic/negotiation.rbs +33 -0
  283. data/sig/raiha/quic/new_token.rbs +14 -0
  284. data/sig/raiha/quic/pacer.rbs +16 -0
  285. data/sig/raiha/quic/packet_protection.rbs +63 -0
  286. data/sig/raiha/quic/path.rbs +106 -0
  287. data/sig/raiha/quic/peer_connection_ids.rbs +42 -0
  288. data/sig/raiha/quic/qlog.rbs +74 -0
  289. data/sig/raiha/quic/ranges.rbs +7 -0
  290. data/sig/raiha/quic/recovery.rbs +376 -0
  291. data/sig/raiha/quic/retry_token.rbs +21 -0
  292. data/sig/raiha/quic/route.rbs +51 -0
  293. data/sig/raiha/quic/scheduler.rbs +36 -0
  294. data/sig/raiha/quic/session.rbs +17 -0
  295. data/sig/raiha/quic/stateless_reset.rbs +10 -0
  296. data/sig/raiha/quic/streams.rbs +244 -0
  297. data/sig/raiha/quic/test.rbs +354 -0
  298. data/sig/raiha/quic/tls_binding.rbs +58 -0
  299. data/sig/raiha/quic/token.rbs +11 -0
  300. data/sig/raiha/quic/tracer.rbs +71 -0
  301. data/sig/raiha/quic/transport_parameters.rbs +113 -0
  302. data/sig/raiha/quic/values.rbs +200 -0
  303. data/sig/raiha/quic/version.rbs +30 -0
  304. data/sig/raiha/quic/wire.rbs +199 -0
  305. data/sig/raiha/quic.rbs +2 -0
  306. data/sig/raiha/runtime/threaded.rbs +616 -0
  307. data/sig/raiha/runtime.rbs +982 -0
  308. data/sig/raiha/tls/anti_replay.rbs +41 -0
  309. data/sig/raiha/tls/cipher_suite.rbs +41 -0
  310. data/sig/raiha/tls/codec.rbs +66 -0
  311. data/sig/raiha/tls/config.rbs +122 -0
  312. data/sig/raiha/tls/crypto.rbs +86 -0
  313. data/sig/raiha/tls/error.rbs +21 -0
  314. data/sig/raiha/tls/event.rbs +61 -0
  315. data/sig/raiha/tls/extension.rbs +331 -0
  316. data/sig/raiha/tls/handshake.rbs +311 -0
  317. data/sig/raiha/tls/iana.rbs +13 -0
  318. data/sig/raiha/tls/key_exchange.rbs +33 -0
  319. data/sig/raiha/tls/key_schedule.rbs +58 -0
  320. data/sig/raiha/tls/keylog.rbs +20 -0
  321. data/sig/raiha/tls/keyring.rbs +64 -0
  322. data/sig/raiha/tls/machine.rbs +429 -0
  323. data/sig/raiha/tls/masked_inspect.rbs +12 -0
  324. data/sig/raiha/tls/output.rbs +37 -0
  325. data/sig/raiha/tls/profile.rbs +26 -0
  326. data/sig/raiha/tls/record.rbs +79 -0
  327. data/sig/raiha/tls/server_name.rbs +10 -0
  328. data/sig/raiha/tls/session.rbs +48 -0
  329. data/sig/raiha/tls/signature_scheme.rbs +20 -0
  330. data/sig/raiha/tls/tcp.rbs +134 -0
  331. data/sig/raiha/tls/test.rbs +143 -0
  332. data/sig/raiha/tls/ticket.rbs +68 -0
  333. data/sig/raiha/tls/ticket_keys.rbs +67 -0
  334. data/sig/raiha/tls/trace.rbs +99 -0
  335. data/sig/raiha/tls/transcript.rbs +22 -0
  336. data/sig/raiha/tls/verifier.rbs +54 -0
  337. data/sig/raiha/tls.rbs +5 -0
  338. data/sig/raiha.rbs +3 -0
  339. metadata +355 -57
  340. data/.gitignore +0 -10
  341. data/CODE_OF_CONDUCT.md +0 -74
  342. data/Gemfile +0 -4
  343. data/Gemfile.lock +0 -22
  344. data/Rakefile +0 -10
  345. data/bin/console +0 -14
  346. data/bin/setup +0 -8
  347. data/raiha.gemspec +0 -40
@@ -0,0 +1,641 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # Loss recovery (RFC 9002): what was sent in each packet number space,
5
+ # what the peer acknowledged, what is lost and when to probe, and what to
6
+ # acknowledge in turn. The RTT, the congestion controller and the bytes in
7
+ # flight are each path's (Path); a packet counts on the path it was sent
8
+ # on.
9
+ #
10
+ # It takes packets by their type and keeps them in three packet number
11
+ # spaces: Initial, Handshake, and Application Data for 0-RTT and 1-RTT.
12
+ class Recovery
13
+ # kInitialRtt, the RTT before a sample (RFC 9002 "Initial RTT"). A
14
+ # constant, and the RTT of an earlier connection is not used.
15
+ INITIAL_RTT = 0.333
16
+ # kGranularity, the timer granularity (RFC 9002 "Time Threshold").
17
+ GRANULARITY = 0.001
18
+ # kPacketThreshold and kTimeThreshold (RFC 9002 "Acknowledgment-Based
19
+ # Detection"), fixed.
20
+ PACKET_THRESHOLD = 3
21
+ TIME_THRESHOLD = 9.0 / 8
22
+ # kPersistentCongestionThreshold (RFC 9002 "Duration").
23
+ PERSISTENT_CONGESTION_THRESHOLD = 3
24
+ # The ranges of packet numbers received kept a space, and so sent in an
25
+ # ACK; past it the oldest is let go (loss recovery's design, "Limits").
26
+ MAX_ACK_RANGES = 64
27
+ # The packets of only ACKs kept after the last ack-eliciting one.
28
+ MAX_NON_ACK_ELICITING_TAIL = 1000
29
+ # The least RTT sample a Retry gives.
30
+ MIN_RETRY_RTT = 0.005
31
+ # The records declared lost kept a space, beside those older than three
32
+ # PTOs, to tell spurious losses (loss recovery's design, "Limits").
33
+ MAX_LOST_PACKETS = 64
34
+
35
+ SPACES = { initial: :initial, handshake: :handshake, zero_rtt: :application_data, one_rtt: :application_data }.freeze #: Hash[Symbol, space]
36
+
37
+ # The path packets are sent on now.
38
+ attr_reader :path
39
+ attr_reader :pto_count
40
+ # The packets declared lost, and those of them acknowledged later.
41
+ attr_reader :packets_lost
42
+ attr_reader :packets_spuriously_lost
43
+ # The peer's max_ack_delay, 25 ms until its transport parameters say.
44
+ attr_writer :peer_max_ack_delay
45
+
46
+ # Ack-eliciting packets to send now in `space`, as the PTO ran out:
47
+ # new data if there is any, else what the oldest unacknowledged packet
48
+ # carried, else a PING (RFC 9002 "Sending Probe Packets"). They go out
49
+ # past the congestion window and pacing, but not past the server's
50
+ # amplification limit.
51
+ class Probe < Data.define(:space, :packets); end
52
+
53
+ # What an ACK or a timer leaves for the owners of the packets: the
54
+ # records acknowledged first, then those lost, and a probe to send.
55
+ class Outcome < Data.define(:acked, :lost, :probe)
56
+ NO_PACKETS = [] #: Array[SentPacket]
57
+ NO_PACKETS.freeze
58
+ NONE = new(acked: NO_PACKETS, lost: NO_PACKETS, probe: nil)
59
+ end
60
+
61
+ # `role` is :client or :server, and `random` gives the bytes the skips
62
+ # of packet numbers are drawn from.
63
+ def initialize(role:, config:, path:, random:, now:, tracer: nil)
64
+ @role = role
65
+ @config = config
66
+ @path = path
67
+ @random = random
68
+ @tracer = tracer
69
+ @spaces = { initial: PacketNumberSpace.new(:initial), handshake: PacketNumberSpace.new(:handshake),
70
+ application_data: PacketNumberSpace.new(:application_data) } #: Hash[space, PacketNumberSpace]
71
+ @spaces.fetch(:application_data).max_ack_delay = config.max_ack_delay
72
+ @pto_count = 0
73
+ @packets_lost = 0
74
+ @packets_spuriously_lost = 0
75
+ @peer_max_ack_delay = 0.025
76
+ @handshake_confirmed = false
77
+ @handshake_acknowledged = false
78
+ @handshake_keys = false
79
+ @first_initial_sent_at = nil #: Float?
80
+ @first_one_rtt_packet_number = nil #: Integer?
81
+ @amplification_limited = false
82
+ @loss_detection_timer = nil #: Float?
83
+ @metrics = {} #: Hash[Symbol, metric]
84
+ if tracer
85
+ tracer.recovery_parameters_set(
86
+ now:, reordering_threshold: PACKET_THRESHOLD, time_threshold: TIME_THRESHOLD, timer_granularity: GRANULARITY,
87
+ initial_rtt: INITIAL_RTT, max_datagram_size: path.max_datagram_size, initial_congestion_window: path.congestion.window,
88
+ minimum_congestion_window: path.congestion.minimum_window, persistent_congestion_threshold: PERSISTENT_CONGESTION_THRESHOLD
89
+ )
90
+ end
91
+ end
92
+
93
+ # When the loss detection timer runs out (RFC 9002 "Setting the Loss
94
+ # Detection Timer"), nil when it is not set.
95
+ attr_reader :loss_detection_timer
96
+
97
+ # The handshake is confirmed (RFC 9001 "Handshake Confirmed"), which
98
+ # lets the PTO count Application Data.
99
+ def handshake_confirmed!(now:)
100
+ @handshake_confirmed = true
101
+ set_loss_detection_timer(now)
102
+ end
103
+
104
+ def handshake_confirmed? = @handshake_confirmed
105
+
106
+ # This end has Handshake keys, so that a probe to avoid a deadlock goes
107
+ # in a Handshake packet.
108
+ def handshake_keys_installed!
109
+ @handshake_keys = true
110
+ end
111
+
112
+ # A server that may send no more until the client's address is
113
+ # validated sets no timer, since it could send nothing when it ran out;
114
+ # once a datagram lifts the limit the timer is set again (RFC 9002
115
+ # "Handling Anti-Amplification Limits").
116
+ def amplification_limited(limited, now:)
117
+ @amplification_limited = limited
118
+ set_loss_detection_timer(now)
119
+ end
120
+
121
+ # The Initial or Handshake keys are gone (RFC 9001 "Discarding Unused
122
+ # Keys"): what was sent in that space is thrown away and taken out of
123
+ # flight, told to neither the congestion controller nor the owners, and
124
+ # the PTO starts over (RFC 9002 "Discarding Keys and Packet State").
125
+ def discard_space(packet_type, now:)
126
+ space = space_of(packet_type)
127
+ raise ArgumentError, "Application Data is never discarded" if space.name == :application_data
128
+ space.discard.each do |packet|
129
+ next unless packet.in_flight && !packet.path.abandoned?
130
+ packet.path.remove_in_flight(sent_bytes: packet.sent_bytes, ack_eliciting: packet.ack_eliciting)
131
+ end
132
+ @pto_count = 0
133
+ set_loss_detection_timer(now)
134
+ end
135
+
136
+ # A client's Retry came at `now` (RFC 9002 "Handling Retry Packets",
137
+ # loss recovery's design, "On a Retry"). What was sent in Initial
138
+ # packets that elicited an ACK and every 0-RTT packet go back to their
139
+ # owners as lost, to be sent again, without a word to the congestion
140
+ # controller; the packet numbers go on. The path starts over, and unless
141
+ # a PTO ran out the time from the first Initial to the Retry, 5 ms at
142
+ # least, is its first RTT sample.
143
+ def on_retry(now:)
144
+ initial = @spaces.fetch(:initial).discard
145
+ zero_rtt = @spaces.fetch(:application_data).discard
146
+ first_sent_at = @first_initial_sent_at
147
+ sample = first_sent_at if @pto_count.zero?
148
+ @path.start_over
149
+ if sample
150
+ @path.rtt.update(latest_rtt: [now - sample, MIN_RETRY_RTT].max, ack_delay: 0.0, max_ack_delay: @peer_max_ack_delay,
151
+ handshake_confirmed: false, now:)
152
+ end
153
+ @pto_count = 0
154
+ set_loss_detection_timer(now)
155
+ trace_metrics(now)
156
+ Outcome.new(acked: Outcome::NO_PACKETS, lost: initial.select(&:ack_eliciting) + zero_rtt, probe: nil)
157
+ end
158
+
159
+ # A client's Version Negotiation came at `now`, and it starts a new
160
+ # attempt in another version (RFC 9368 "Incompatible Version
161
+ # Negotiation"). What it sent in Initial and 0-RTT packets is thrown
162
+ # away and taken out of flight with nothing to send again, as the new
163
+ # attempt sends its own; the packet numbers go on, as after a Retry. The
164
+ # path starts over, and unless a PTO ran out the time from the first
165
+ # Initial, 5 ms at least, is its first RTT sample, as from a Retry. A
166
+ # Retry after it takes none across the two attempts.
167
+ def start_over(now:)
168
+ @spaces.fetch(:initial).discard
169
+ @spaces.fetch(:application_data).discard
170
+ sample = @first_initial_sent_at if @pto_count.zero?
171
+ @first_initial_sent_at = nil
172
+ @path.start_over
173
+ if sample
174
+ @path.rtt.update(latest_rtt: [now - sample, MIN_RETRY_RTT].max, ack_delay: 0.0, max_ack_delay: @peer_max_ack_delay,
175
+ handshake_confirmed: false, now:)
176
+ end
177
+ @pto_count = 0
178
+ set_loss_detection_timer(now)
179
+ trace_metrics(now)
180
+ end
181
+
182
+ # The server rejected 0-RTT (RFC 9001 "Rejecting 0-RTT", loss recovery's
183
+ # design, "On a rejected 0-RTT"). The 0-RTT packets are those of
184
+ # Application Data below the first 1-RTT one, or all of them when no
185
+ # 1-RTT packet went yet: thrown away and taken out of flight without a
186
+ # word to the congestion controller or the owners, whose streams start
187
+ # over by other means. An ACK of one of them from now on is
188
+ # PROTOCOL_VIOLATION.
189
+ def on_zero_rtt_rejected(now:)
190
+ space = @spaces.fetch(:application_data)
191
+ boundary = @first_one_rtt_packet_number || (space.largest_sent || -1) + 1
192
+ space.reject_below(boundary).each do |packet|
193
+ next unless packet.in_flight && !packet.path.abandoned?
194
+ packet.path.remove_in_flight(sent_bytes: packet.sent_bytes, ack_eliciting: packet.ack_eliciting)
195
+ end
196
+ set_loss_detection_timer(now)
197
+ end
198
+
199
+ # When something is to be done next: the loss detection timer or an
200
+ # ACK that must go.
201
+ def next_timeout = [@loss_detection_timer, *@spaces.each_value.filter_map(&:ack_deadline)].compact.min
202
+
203
+ # Runs out the loss detection timer at `now` if it is due (RFC 9002 "On
204
+ # Timeout"). A loss time declares the packets lost; otherwise the PTO
205
+ # asks for two probes in the space it ran out in, or one in a Handshake
206
+ # or Initial packet when nothing is in flight to keep the server from
207
+ # being stuck, and backs off; the path in use hears of it.
208
+ def on_timeout(now:)
209
+ timer = @loss_detection_timer
210
+ return Outcome::NONE unless timer && now >= timer
211
+ loss = loss_space
212
+ pto = pto_space
213
+ trace_timer(:expired, (loss || pto)&.name || (lone_probe_space unless ack_eliciting_in_flight?), nil, now)
214
+ if loss
215
+ lost = detect_lost(loss, now)
216
+ on_lost(lost, now)
217
+ set_loss_detection_timer(now)
218
+ trace_metrics(now)
219
+ return Outcome.new(acked: Outcome::NO_PACKETS, lost:, probe: nil)
220
+ end
221
+ probe =
222
+ if ack_eliciting_in_flight?
223
+ Probe.new(space: pto&.name || :initial, packets: 2)
224
+ else
225
+ Probe.new(space: lone_probe_space, packets: 1)
226
+ end
227
+ @pto_count += 1
228
+ @path.probe_timed_out(now:)
229
+ set_loss_detection_timer(now)
230
+ trace_metrics(now)
231
+ Outcome.new(acked: Outcome::NO_PACKETS, lost: Outcome::NO_PACKETS, probe:)
232
+ end
233
+
234
+ # The number for the next packet of `packet_type` (:initial,
235
+ # :handshake, :zero_rtt or :one_rtt). Once the handshake is confirmed,
236
+ # an Application Data number is left out now and then (loss recovery's
237
+ # design, "Packet number skipping").
238
+ def next_packet_number(packet_type)
239
+ space = space_of(packet_type)
240
+ space.skip_after(skip_interval) if space.name == :application_data && @handshake_confirmed && !space.skipping?
241
+ space.next_packet_number
242
+ end
243
+
244
+ # The least number the next packet of `packet_type` will have, without
245
+ # taking it.
246
+ def upcoming_packet_number(packet_type) = space_of(packet_type).upcoming_packet_number
247
+
248
+ # How many bytes the packet number of the packet of `packet_type`
249
+ # numbered `packet_number` goes in, by what the peer has acknowledged
250
+ # (RFC 9000 "Packet Number Encoding and Decoding").
251
+ def packet_number_length(packet_type, packet_number)
252
+ Wire::PacketNumber.length(packet_number, largest_acked: space_of(packet_type).largest_acked_packet)
253
+ end
254
+
255
+ # The oldest packet in flight in `space` that elicits an ACK, nil when
256
+ # none: what a probe carries again when nothing new waits (loss
257
+ # recovery's design, "Probes"). It stays recorded as it is.
258
+ def oldest_ack_eliciting(space) = @spaces.fetch(space).oldest_ack_eliciting
259
+
260
+ # The oldest packet in flight in `space` that elicits an ACK and carries
261
+ # something to send again, nil when none: what a probe carries again,
262
+ # so that a PMTU probe or an earlier probe of only a PING does not make
263
+ # every probe a PING while what was lost waits for an ACK.
264
+ def oldest_to_send_again(space) = @spaces.fetch(space).oldest_to_send_again
265
+
266
+ # The largest packet number received in the space of `packet_type`, nil
267
+ # before any, which the packet number of the next is decoded against.
268
+ def largest_received(packet_type) = space_of(packet_type).largest_received
269
+
270
+ # The connection moved to `path`: PTOs and loss detection go by its
271
+ # RTT from now on, and the timer is set again by it.
272
+ def switch_path(path, now:)
273
+ @path = path
274
+ set_loss_detection_timer(now)
275
+ end
276
+
277
+ # Records a packet sent at `now` on `path`, the current one unless
278
+ # told, and counts it on the path when it is in flight.
279
+ def on_packet_sent(packet_type:, packet_number:, sent_bytes:, ack_eliciting:, in_flight:, duties:, now:, pmtu_probe: false, path: @path)
280
+ ecn = path.ecn
281
+ packet = SentPacket.new(packet_number:, time_sent: now, sent_bytes:, ack_eliciting:, in_flight:, path:,
282
+ duties: duties.frozen? ? duties : duties.dup.freeze, pmtu_probe:, ecn:)
283
+ space_of(packet_type).add(packet)
284
+ @first_initial_sent_at ||= now if packet_type == :initial
285
+ @first_one_rtt_packet_number ||= packet_number if packet_type == :one_rtt
286
+ path.ecn_sent if ecn == :ect0
287
+ if in_flight
288
+ path.add_in_flight(sent_bytes:, ack_eliciting:, now:)
289
+ set_loss_detection_timer(now)
290
+ end
291
+ packet
292
+ end
293
+
294
+ # Takes an ACK frame that came in a packet of `packet_type` at `now`
295
+ # (RFC 9002 "On Receiving an Acknowledgment"). `received_at` is when the
296
+ # packet arrived, earlier than `now` for one kept until its keys came;
297
+ # the RTT sample counts from it. An ACK of a packet never sent raises a
298
+ # Violation.
299
+ def on_ack_received(packet_type:, ack:, now:, received_at: now)
300
+ space = space_of(packet_type)
301
+ largest_before = space.largest_acked_packet
302
+ acked = space.acknowledge(ack.ranges, frame_type: ack.ecn ? 0x03 : 0x02)
303
+ spurious = take_spurious(space, ack.ranges, now)
304
+ mtu_probes_acked(spurious, now)
305
+ if acked.empty?
306
+ return spurious.empty? ? Outcome::NONE : Outcome.new(acked: spurious, lost: Outcome::NO_PACKETS, probe: nil)
307
+ end
308
+ @handshake_acknowledged = true if space.name == :handshake
309
+ sampled = take_rtt_sample(space, ack, acked, received_at)
310
+ process_ecn(space, ack, acked, now) if largest_before.nil? || ack.largest > largest_before
311
+ lost = detect_lost(space, now)
312
+ on_lost(lost, now)
313
+ on_acked(space, acked, sampled, now)
314
+ @pto_count = 0 if peer_completed_address_validation?
315
+ space.forget_lost(before: now - 3 * @path.rtt.pto_period)
316
+ set_loss_detection_timer(now)
317
+ trace_metrics(now)
318
+ Outcome.new(acked: spurious.empty? ? acked : acked + spurious, lost:, probe: nil)
319
+ end
320
+
321
+ # Records a packet of `packet_type` received with the ECN mark of its
322
+ # datagram, once its protection is off; false for a duplicate, which
323
+ # the caller drops (RFC 9000 "Packet Number Spaces"). An ack-eliciting
324
+ # one owes an ACK, at once or within max_ack_delay (loss recovery's
325
+ # design, "When to send an ACK"). `received_at` is when the packet
326
+ # arrived, earlier than `now` for one kept until its keys came.
327
+ def on_packet_received(packet_type:, packet_number:, ecn:, ack_eliciting:, now:, received_at: now)
328
+ space = space_of(packet_type)
329
+ before = space.ack_deadline
330
+ received = space.receive(packet_number, ecn:, ack_eliciting:, now: received_at)
331
+ deadline = space.ack_deadline
332
+ if deadline && deadline != before && @tracer
333
+ @tracer.timer_updated(timer_type: :ack, event_type: :set, space: space.name, delta: [deadline - now, 0.0].max, now:) # steep:ignore
334
+ end
335
+ received
336
+ end
337
+
338
+ # Whether an ACK must go now in `space` (:initial, :handshake or
339
+ # :application_data).
340
+ def ack_due?(space, now:)
341
+ deadline = @spaces.fetch(space).ack_deadline
342
+ !deadline.nil? && now >= deadline
343
+ end
344
+
345
+ # Whether an ack-eliciting packet waits for an ACK in `space`, which can
346
+ # then ride along with anything sent there.
347
+ def ack_owed?(space) = @spaces.fetch(space).ack_owed?
348
+
349
+ # The ACK frame to send in `space` now, nil before anything came.
350
+ def ack_frame(space, now:) = @spaces.fetch(space).ack_frame(now:)
351
+
352
+ # An ACK frame of `space` went out.
353
+ def on_ack_sent(space, now:)
354
+ space = @spaces.fetch(space)
355
+ if space.ack_deadline && @tracer
356
+ @tracer.timer_updated(timer_type: :ack, event_type: :cancelled, space: space.name, delta: nil, now:) # steep:ignore
357
+ end
358
+ space.ack_sent
359
+ end
360
+
361
+ # Whether a packet of only an ACK of `space` should carry a PING too:
362
+ # with packets of only ACKs going one after another and nothing
363
+ # ack-eliciting sent for a round trip, the peer would never acknowledge
364
+ # the ACKs and what they acknowledge would never be let go (RFC 9000
365
+ # "Limiting Ranges by Tracking ACK Frames"). The first after an
366
+ # ack-eliciting packet goes alone, or two ends with nothing else to send
367
+ # would answer each other's PING with one of their own.
368
+ def ping_with_ack?(space, now:)
369
+ space = @spaces.fetch(space)
370
+ return false unless space.acks_alone?
371
+ sent_at = space.time_of_last_ack_eliciting_packet
372
+ sent_at.nil? || now - sent_at >= @path.rtt.smoothed_rtt
373
+ end
374
+
375
+ # A packet that carried an ACK frame of this end with the largest
376
+ # `largest` was acknowledged (the duty Duty::Ack): what the frame
377
+ # acknowledged need not be acknowledged again.
378
+ def ack_frame_acknowledged(space, largest:) = @spaces.fetch(space).acknowledged_up_to(largest)
379
+
380
+ # Whether the peer has validated this end's address, so that nothing is
381
+ # owed to keep it from being stuck at its amplification limit (RFC 9002
382
+ # "Handshakes and New Paths"): always for a server, and for a client once
383
+ # a Handshake packet is acknowledged or the handshake confirmed.
384
+ def peer_completed_address_validation? = @role == :server || @handshake_acknowledged || @handshake_confirmed
385
+
386
+ private def space_of(packet_type) = @spaces.fetch(SPACES.fetch(packet_type))
387
+
388
+ # A Hash#any? block of two parameters takes the key and value without
389
+ # an Array for the pair.
390
+ private def ack_eliciting_in_flight? = @spaces.any? { |_name, space| space.ack_eliciting_in_flight.positive? }
391
+
392
+ # The space the block gives the earliest time for, nil when it gives
393
+ # none; the first of those that tie. The loss detection timer is set
394
+ # again on every packet sent and every ACK, so the spaces are walked
395
+ # with a block and the earliest kept in locals: no Enumerator, Array or
396
+ # record of a time.
397
+ private def earliest_space
398
+ earliest = nil #: PacketNumberSpace?
399
+ earliest_time = Float::INFINITY
400
+ @spaces.each_value do |space|
401
+ time = yield(space)
402
+ next unless time && time < earliest_time
403
+ earliest = space
404
+ earliest_time = time
405
+ end
406
+ earliest
407
+ end
408
+
409
+ # The space whose packets can be lost by time the earliest.
410
+ private def loss_space = earliest_space(&:loss_time)
411
+
412
+ # RFC 9002 "Computing PTO": the space with ack-eliciting packets in
413
+ # flight whose PTO runs out the earliest, Application Data only once the
414
+ # handshake is confirmed.
415
+ private def pto_space = earliest_space { pto_time(_1) }
416
+
417
+ # The PTO period of the current path, backed off by the PTO count.
418
+ private def pto_duration = @path.rtt.pto_period * (1 << @pto_count)
419
+
420
+ # When the PTO of `space` runs out: the PTO duration from its last
421
+ # ack-eliciting packet, with the peer's max_ack_delay backed off for
422
+ # Application Data. nil with none of them in flight, or for Application
423
+ # Data before the handshake is confirmed.
424
+ private def pto_time(space)
425
+ sent_at = space.time_of_last_ack_eliciting_packet
426
+ return nil unless space.ack_eliciting_in_flight.positive? && sent_at
427
+ return sent_at + pto_duration unless space.name == :application_data
428
+ sent_at + pto_duration + @peer_max_ack_delay * (1 << @pto_count) if @handshake_confirmed
429
+ end
430
+
431
+ # Where a probe goes with nothing in flight, to keep the server from
432
+ # being stuck at its amplification limit.
433
+ private def lone_probe_space = @handshake_keys ? :handshake : :initial
434
+
435
+ # RFC 9002 "Setting the Loss Detection Timer": the earliest loss time;
436
+ # else, unless the amplification limit leaves nothing to send, the
437
+ # earliest PTO; else, with nothing in flight and the address not yet
438
+ # validated by the peer, a PTO from now.
439
+ private def set_loss_detection_timer(now)
440
+ loss = loss_space
441
+ pto = pto_space unless loss || @amplification_limited
442
+ before = @loss_detection_timer
443
+ timer = @loss_detection_timer =
444
+ if loss then loss.loss_time
445
+ elsif @amplification_limited then nil
446
+ elsif pto then pto_time(pto)
447
+ elsif ack_eliciting_in_flight? || peer_completed_address_validation? then nil
448
+ else now + pto_duration
449
+ end
450
+ if timer
451
+ trace_timer(:set, (loss || pto)&.name || lone_probe_space, timer - now, now) unless timer == before
452
+ elsif before
453
+ trace_timer(:cancelled, nil, nil, now)
454
+ end
455
+ end
456
+
457
+ private def trace_timer(event_type, space, delta, now)
458
+ @tracer.timer_updated(timer_type: :loss_timeout, event_type:, space:, delta:, now:) if @tracer # steep:ignore
459
+ end
460
+
461
+ # RFC 9002 "Generating RTT Samples": when the largest acknowledged is
462
+ # acknowledged for the first time and with an ack-eliciting packet. The
463
+ # ACK Delay of an Initial ACK counts for nothing. The sample goes to the
464
+ # path the largest was sent on, and is lost with an abandoned one; the
465
+ # path is given back.
466
+ private def take_rtt_sample(space, ack, acked, received_at)
467
+ largest = acked.fetch(-1)
468
+ return nil unless largest.packet_number == ack.largest && acked.any?(&:ack_eliciting)
469
+ path = largest.path
470
+ return nil if path.abandoned?
471
+ path.rtt.update(latest_rtt: received_at - largest.time_sent, ack_delay: space.name == :initial ? 0.0 : ack.delay,
472
+ max_ack_delay: @peer_max_ack_delay, handshake_confirmed: @handshake_confirmed, now: received_at)
473
+ path
474
+ end
475
+
476
+ # The records of `space` lost by now, by the RTT of the current path
477
+ # (loss recovery's design, "Which path's RTT"): 9/8 of the larger of the
478
+ # latest and smoothed RTT, and no less than the granularity.
479
+ private def detect_lost(space, now)
480
+ rtt = @path.rtt
481
+ loss_delay = [TIME_THRESHOLD * [rtt.latest_rtt, rtt.smoothed_rtt].max, GRANULARITY].max
482
+ space.detect_lost(loss_delay:, now:) do |packet, trigger|
483
+ if @tracer
484
+ @tracer.packet_lost(packet_number: packet.packet_number, space: space.name, trigger:, mtu_probe: packet.pmtu_probe, now:) # steep:ignore
485
+ end
486
+ end
487
+ end
488
+
489
+ # The records declared lost that an ACK acknowledges after all: counted
490
+ # as spurious losses and handed back as acknowledged, and nothing to
491
+ # the congestion controller, the RTT or the ECN counts (loss recovery's
492
+ # design, "Acknowledged after all").
493
+ private def take_spurious(space, ranges, now)
494
+ spurious = space.take_spurious(ranges)
495
+ @packets_spuriously_lost += spurious.size
496
+ if @tracer
497
+ spurious.each { |packet| @tracer.spurious_loss(packet_number: packet.packet_number, space: space.name, now:) } # steep:ignore
498
+ end
499
+ spurious
500
+ end
501
+
502
+ # Checks the ECN counts of an ACK that raised the largest acknowledged
503
+ # (RFC 9000 "ECN Validation", loss recovery's design, "Validating the
504
+ # counts"), for the path that sent the largest newly acknowledged
505
+ # packet: the counts must be there when ECT(0) packets are newly
506
+ # acknowledged, never go down, show no ECT(1), not exceed the ECT(0)
507
+ # packets sent, and grow by as many ECT(0) and CE marks as ECT(0)
508
+ # packets are newly acknowledged. A path that fails stops marking and
509
+ # heeding counts. CE marks that grew tell its congestion controller
510
+ # once.
511
+ private def process_ecn(space, ack, acked, now)
512
+ largest = acked.fetch(-1)
513
+ path = largest.path
514
+ return if path.abandoned? || path.ecn_state == :failed
515
+ newly = acked.count { |packet| packet.ecn == :ect0 }
516
+ counts = ack.ecn
517
+ unless counts
518
+ path.ecn_failed(now:) if newly.positive?
519
+ return
520
+ end
521
+ last = space.ecn_counts
522
+ if counts.ect1.positive? || counts.ect0 < last.ect0 || counts.ce < last.ce || counts.ect0 > space.ect0_sent ||
523
+ (counts.ect0 - last.ect0) + (counts.ce - last.ce) < newly
524
+ path.ecn_failed(now:)
525
+ return
526
+ end
527
+ space.ecn_counts = counts
528
+ path.ecn_validated(now:) if newly.positive?
529
+ path.congestion.on_ecn_ce(largest_acked_sent_at: largest.time_sent, now:) if counts.ce > last.ce
530
+ end
531
+
532
+ # Each path that sent packets lost takes them out of flight and tells
533
+ # its congestion controller once, with the latest time one of them was
534
+ # sent (RFC 9002 "On Packets Lost"); persistent congestion takes its
535
+ # minimum RTT anew, and tells the path, which may have lost its larger
536
+ # datagrams in a black hole. The loss of a probe of the path MTU tells
537
+ # the controller nothing, and goes to the path's search; a path
538
+ # abandoned does neither.
539
+ private def on_lost(lost, now)
540
+ @packets_lost += lost.size
541
+ lost.each { |packet| packet.path.mtu_probe_lost(packet.sent_bytes, now:) if packet.pmtu_probe && !packet.path.abandoned? }
542
+ lost.select { |packet| packet.ecn == :ect0 }.group_by(&:path).each { |path, packets| path.ecn_lost(packets.size, now:) }
543
+ lost.select(&:in_flight).group_by(&:path).each do |path, packets|
544
+ next if path.abandoned?
545
+ packets.each { |packet| path.remove_in_flight(sent_bytes: packet.sent_bytes, ack_eliciting: packet.ack_eliciting) }
546
+ congested = packets.reject(&:pmtu_probe)
547
+ largest_lost_sent_at = congested.map(&:time_sent).max or next
548
+ persistent = persistent_congestion?(path, lost)
549
+ path.congestion.on_loss(lost_bytes: congested.sum(&:sent_bytes), largest_lost_sent_at:, persistent:, now:)
550
+ next unless persistent
551
+ path.rtt.reset_min_rtt
552
+ path.persistent_congestion(now:)
553
+ end
554
+ end
555
+
556
+ # RFC 9002 "Establishing Persistent Congestion", within the losses of
557
+ # one detection in one space (loss recovery's design, "Persistent
558
+ # congestion"). The losses run on while the packet numbers follow each
559
+ # other on `path`; a gap, a packet acknowledged, skipped or lost before,
560
+ # or one of another path ends the run. Two ack-eliciting packets of a
561
+ # run sent after the path's first RTT sample and further apart than
562
+ # the period establish it; the period is three times the smoothed RTT,
563
+ # four times its variation and the peer's max_ack_delay.
564
+ private def persistent_congestion?(path, lost)
565
+ rtt = path.rtt
566
+ first_sample_at = rtt.first_sample_at or return false
567
+ period = (rtt.smoothed_rtt + [4 * rtt.rttvar, GRANULARITY].max + @peer_max_ack_delay) * PERSISTENT_CONGESTION_THRESHOLD
568
+ run_start = nil #: Float?
569
+ previous = nil #: SentPacket?
570
+ lost.each do |packet|
571
+ follows = previous && packet.packet_number == previous.packet_number + 1 && previous.path.equal?(path) && packet.path.equal?(path)
572
+ run_start = nil unless follows
573
+ previous = packet
574
+ next unless packet.path.equal?(path) && packet.ack_eliciting && packet.time_sent > first_sample_at
575
+ start = run_start ||= packet.time_sent
576
+ return true if packet.time_sent - start > period
577
+ end
578
+ false
579
+ end
580
+
581
+ # Each path that sent packets acknowledged takes them out of flight and
582
+ # tells its congestion controller once (RFC 9002 "On Packet
583
+ # Acknowledgment"), and its rate meter those of Application Data; a
584
+ # path abandoned does none of it.
585
+ private def on_acked(space, acked, sampled, now)
586
+ acked.select(&:in_flight).group_by(&:path).each do |path, packets|
587
+ next if path.abandoned?
588
+ packets.each { |packet| path.remove_in_flight(sent_bytes: packet.sent_bytes, ack_eliciting: packet.ack_eliciting) }
589
+ acked_bytes = packets.sum(&:sent_bytes)
590
+ path.delivered(acked_bytes, largest: packets.fetch(-1).packet_number, now:) if space.name == :application_data
591
+ path.acked(acked_bytes:, largest_acked_sent_at: packets.map(&:time_sent).max || now, sampled: path.equal?(sampled), now:)
592
+ end
593
+ mtu_probes_acked(acked, now)
594
+ end
595
+
596
+ # The probes of the path MTU among `packets`, acknowledged, go to the
597
+ # search of the path that sent them, those declared lost before too;
598
+ # a probe is the one packet of its datagram, and its size the
599
+ # datagram's.
600
+ private def mtu_probes_acked(packets, now)
601
+ packets.each { |packet| packet.path.mtu_probe_acked(packet.sent_bytes, now:) if packet.pmtu_probe && !packet.path.abandoned? }
602
+ end
603
+
604
+ # The values of the current path that changed since last told, at once
605
+ # (loss recovery's design, "metrics_updated"); the delivery rate once
606
+ # there is one, in bits a second as pacing_rate is.
607
+ private def trace_metrics(now)
608
+ return unless @tracer
609
+ rtt = @path.rtt
610
+ metrics = {
611
+ latest_rtt: rtt.latest_rtt, smoothed_rtt: rtt.smoothed_rtt, min_rtt: rtt.min_rtt, rtt_variance: rtt.rttvar,
612
+ pto_count: @pto_count, bytes_in_flight: @path.bytes_in_flight, packets_in_flight: @path.packets_in_flight,
613
+ **@path.congestion.metrics
614
+ } #: Hash[Symbol, metric]
615
+ rate = @path.delivery_rate
616
+ metrics[:delivery_rate] = (rate * 8).to_i if rate
617
+ changed = metrics.reject { |name, value| @metrics[name] == value }
618
+ return if changed.empty?
619
+ @metrics = metrics
620
+ @tracer.metrics_updated(now:, **changed) # steep:ignore
621
+ end
622
+
623
+ # Packets to send before the next skip: twice the initial window, half
624
+ # the congestion window and up to one and a half more, in datagrams.
625
+ # Never less than twice the initial window, so skips never come close.
626
+ private def skip_interval
627
+ max_datagram_size = @path.max_datagram_size
628
+ initial = @config.initial_window.call(max_datagram_size) / max_datagram_size
629
+ window = @path.window_packets
630
+ 2 * initial + window / 2 + random_below(window * 3 / 2 + 1)
631
+ end
632
+
633
+ private def random_below(bound) = @random.bytes(4).unpack1("N") % bound
634
+ end
635
+ end
636
+
637
+ require_relative "recovery/rtt_estimator"
638
+ require_relative "recovery/rate_meter"
639
+ require_relative "recovery/duty"
640
+ require_relative "recovery/sent_packet"
641
+ require_relative "recovery/packet_number_space"
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # The server's transport parameters that a client remembers for 0-RTT
5
+ # (RFC 9000 "Values of Transport Parameters for 0-RTT"): every one but the
6
+ # seven a client must not use remembered values of, which this type does
7
+ # not have. Not holding them is what keeps them from being used.
8
+ # max_datagram_frame_size is here too: a client that sends 0-RTT data
9
+ # remembers every parameter it can process. version_information is not:
10
+ # it tells the versions of one connection, and a reserved version in it
11
+ # changes every time.
12
+ class RememberedTransportParameters < Data.define(
13
+ :max_idle_timeout, :max_udp_payload_size,
14
+ :initial_max_data, :initial_max_stream_data_bidi_local, :initial_max_stream_data_bidi_remote, :initial_max_stream_data_uni,
15
+ :initial_max_streams_bidi, :initial_max_streams_uni,
16
+ :disable_active_migration, :active_connection_id_limit, :max_datagram_frame_size
17
+ )
18
+ # The canonical form: every parameter in the order of its ID, in the
19
+ # transport parameters' wire form, defaults included. The server puts it
20
+ # in the context of its tickets, so that a ticket's 0-RTT is refused
21
+ # once any of these change. disable_active_migration, a flag, is there
22
+ # only when true.
23
+ def encode
24
+ writer = Wire::Writer.new
25
+ to_h.each { |name, value| TransportParameters.write(writer, name, value) }
26
+ writer.to_s
27
+ end
28
+ end
29
+ end