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,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ module Congestion
5
+ # The rate a window is paced at (RFC 9002 "Pacing"): N times the window
6
+ # over the smoothed RTT, N small but above 1 so that variations in RTT
7
+ # do not leave the window unused. Slow start paces at twice that, as the
8
+ # window doubles in a round trip and the rate is to keep up with it
9
+ # (Linux's pacing_ss_ratio, quicly's slow start).
10
+ module Pacing
11
+ SLOW_START = 2.0
12
+ OTHERWISE = 1.25
13
+
14
+ # Bytes a second; nil for an RTT of nothing, a path with no delay,
15
+ # where there is nothing to pace.
16
+ def self.rate(window, smoothed_rtt, slow_start:)
17
+ (slow_start ? SLOW_START : OTHERWISE) * window / smoothed_rtt if smoothed_rtt.positive?
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,249 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ module Congestion
5
+ module Startup
6
+ # Rapid Start (draft-kazuho-ccwg-rapid-start-02): the start of a
7
+ # connection and its first recovery period, in place of the slow start
8
+ # of the controller it wraps, which goes on once the period ends.
9
+ #
10
+ # While the path builds no queue, rtt_floor (the least latest RTT of
11
+ # about the last min_rtt) no greater than queue_buildup_thresh, the
12
+ # window grows by twice the bytes acknowledged, three times a round
13
+ # trip; otherwise by the bytes acknowledged, as in slow start
14
+ # (Increasing the Congestion Window); with no sample in about the last
15
+ # min_rtt, as slow start. Pacing goes at three, two or, in the
16
+ # recovery period, 1.25 times the window a smoothed RTT, but for the
17
+ # first full flight after the first RTT sample, until the bytes sent
18
+ # in flight since come to the window, which goes over a full RTT and
19
+ # no faster than slow start's first, twice RFC 9002's initial window a
20
+ # smoothed RTT (Full-RTT Pacing).
21
+ #
22
+ # Before the first RTT sample, as for 0-RTT, there is nothing to pace
23
+ # over a full RTT with, and nothing is paced: the path lets no more
24
+ # than RFC 9002's initial window go in flight then, however large the
25
+ # Config's (Path#window).
26
+ #
27
+ # The first loss or ECN-CE starts the recovery period (Congestion
28
+ # Handling): the window is multiplied by the silence factor, which
29
+ # stops sending until the bytes in flight come under it, then lessened
30
+ # by the ack factor of each byte acknowledged and the loss factor of
31
+ # each byte lost, the bytes of the loss that started it among them,
32
+ # all of the factors from the wrapped controller's beta (Reduction
33
+ # Factors). It goes no lower than a third of beta times the window
34
+ # before, beta times the initial window (the MAY) or the minimum
35
+ # window. Once the period ends, with an ACK of a packet sent after it
36
+ # began, Rapid Start is done? and hands over: the controller takes the
37
+ # window, the BDP and queue it came to, the window over beta, when the
38
+ # period began and the smoothed RTT, and that ACK. The ACK frame comes
39
+ # whole, so that bytes of packets sent before the period began that
40
+ # come in it go to the controller too and lessen nothing; with the
41
+ # peer acknowledging every second packet while data flows, that is a
42
+ # packet or so. A loss or ECN-CE of a packet sent after the period
43
+ # began, or persistent congestion, hands over first and goes to the
44
+ # controller. Done, everything goes to the controller, until the path
45
+ # takes it back with handover.
46
+ class RapidStart
47
+ # The version of the draft this follows.
48
+ DRAFT = "draft-kazuho-ccwg-rapid-start-02"
49
+ # Reduction Factors.
50
+ K = 2.0 / 3
51
+ # queue_buildup_thresh's RECOMMENDED terms: seconds and a factor.
52
+ QUEUE_BUILDUP_MARGIN = 0.004
53
+ QUEUE_BUILDUP_FACTOR = 1.10
54
+ # The pacing N of growth by three times a round trip.
55
+ THREE_TIMES = 3.0
56
+
57
+ # queue_buildup_thresh, seconds (Increasing the Congestion Window).
58
+ def self.queue_buildup_threshold(min_rtt) = [min_rtt + QUEUE_BUILDUP_MARGIN, min_rtt * QUEUE_BUILDUP_FACTOR].min
59
+
60
+ def initialize(cc, max_datagram_size:, initial_window:, tracer: nil)
61
+ @cc = cc
62
+ @max_datagram_size = max_datagram_size
63
+ @initial_window = initial_window
64
+ @tracer = tracer
65
+ @cwnd = cc.window.to_f
66
+ @initial = true
67
+ @app_limited = false
68
+ # :probing, :recovery or :done.
69
+ @phase = :probing
70
+ @recovery_start = nil #: Float?
71
+ # The recovery period's beta, and how low it takes the window.
72
+ @beta = 1.0
73
+ @floor = 0.0
74
+ @smoothed_rtt = nil #: Float?
75
+ @min_rtt = nil #: Float?
76
+ # The RTT samples of about the last min_rtt that no later one is
77
+ # below, and when each came, for rtt_floor: the first is the least,
78
+ # and asking for it, as pacing does for every datagram, costs
79
+ # nothing.
80
+ @floor_times = [] #: Array[Float]
81
+ @floor_rtts = [] #: Array[Float]
82
+ # Whether the first full flight after the first RTT sample is still
83
+ # going, and the bytes it sent in flight so far.
84
+ @first_flight = true
85
+ @first_flight_sent = 0
86
+ @state = :rapid_start
87
+ end
88
+
89
+ # Whether the first recovery period is over, or what came after it
90
+ # was the controller's to take.
91
+ def done? = @phase == :done
92
+
93
+ # The controller Rapid Start wrapped, for the path to take back once
94
+ # done?.
95
+ def handover = @cc
96
+
97
+ def window = done? ? @cc.window : @cwnd.to_i
98
+
99
+ def minimum_window = @cc.minimum_window
100
+
101
+ def on_packet_sent(bytes:, now:)
102
+ if @first_flight && @smoothed_rtt
103
+ @first_flight_sent += bytes
104
+ @first_flight = false if @first_flight_sent >= @cwnd
105
+ end
106
+ @cc.on_packet_sent(bytes:, now:)
107
+ end
108
+
109
+ def on_ack(acked_bytes:, largest_acked_sent_at:, latest_rtt:, smoothed_rtt:, min_rtt:, app_limited:, now:, rate_sample: nil)
110
+ unless done?
111
+ take_rtt(latest_rtt, smoothed_rtt, min_rtt, now)
112
+ @app_limited = app_limited
113
+ start = @recovery_start
114
+ hand_over(now) if start && largest_acked_sent_at > start
115
+ end
116
+ return @cc.on_ack(acked_bytes:, largest_acked_sent_at:, latest_rtt:, smoothed_rtt:, min_rtt:, app_limited:, now:, rate_sample:) if done?
117
+ if @phase == :recovery
118
+ reduce(ack_factor * acked_bytes)
119
+ elsif !app_limited
120
+ @cwnd += (three_times? ? 2 : 1) * acked_bytes
121
+ @initial = false
122
+ end
123
+ update_state(now)
124
+ end
125
+
126
+ def on_loss(lost_bytes:, largest_lost_sent_at:, persistent:, now:)
127
+ start = @recovery_start
128
+ hand_over(now) if !done? && (persistent || (start && largest_lost_sent_at > start))
129
+ return @cc.on_loss(lost_bytes:, largest_lost_sent_at:, persistent:, now:) if done?
130
+ enter_recovery(@cc.beta(ecn: false), now) if @phase == :probing
131
+ reduce(loss_factor * lost_bytes)
132
+ update_state(now)
133
+ end
134
+
135
+ def on_ecn_ce(largest_acked_sent_at:, now:)
136
+ start = @recovery_start
137
+ hand_over(now) if !done? && start && largest_acked_sent_at > start
138
+ return @cc.on_ecn_ce(largest_acked_sent_at:, now:) if done?
139
+ enter_recovery(@cc.beta(ecn: true), now) if @phase == :probing
140
+ update_state(now)
141
+ end
142
+
143
+ # The controller hears of it too, for when it takes over. An initial
144
+ # window not yet moved is worked out again (RFC 9002 "Initial and
145
+ # Minimum Congestion Window").
146
+ def on_mtu_updated(max_datagram_size:, now:)
147
+ @cc.on_mtu_updated(max_datagram_size:, now:)
148
+ return if done?
149
+ @max_datagram_size = max_datagram_size
150
+ @cwnd = @initial_window.call(max_datagram_size).to_f if @initial
151
+ @cwnd = [@cwnd, minimum_window.to_f].max
152
+ end
153
+
154
+ # Bytes a second, nil until there is an RTT sample.
155
+ def pacing_rate
156
+ return @cc.pacing_rate if done?
157
+ rtt = @smoothed_rtt or return nil
158
+ return nil unless rtt.positive?
159
+ return Pacing::OTHERWISE * @cwnd / rtt if @phase == :recovery
160
+ return [@cwnd, 2.0 * Config::DEFAULT_INITIAL_WINDOW.call(@max_datagram_size)].max / rtt if @first_flight
161
+ (three_times? ? THREE_TIMES : Pacing::SLOW_START) * @cwnd / rtt
162
+ end
163
+
164
+ # :rapid_start, :recovery or :application_limited, the controller's
165
+ # once done.
166
+ def state = done? ? @cc.state : @state
167
+
168
+ def metrics
169
+ return @cc.metrics if done?
170
+ metrics = { congestion_window: window } #: Hash[Symbol, Integer]
171
+ rate = pacing_rate
172
+ # qlog has it in bits a second.
173
+ metrics[:pacing_rate] = (rate * 8).to_i if rate
174
+ metrics
175
+ end
176
+
177
+ # Keeps the RTT samples of the last min_rtt, an ACK without one
178
+ # letting the old ones go all the same.
179
+ private def take_rtt(latest_rtt, smoothed_rtt, min_rtt, now)
180
+ return unless min_rtt
181
+ @smoothed_rtt = smoothed_rtt
182
+ @min_rtt = min_rtt
183
+ if latest_rtt
184
+ while (last = @floor_rtts.last) && last >= latest_rtt
185
+ @floor_times.pop
186
+ @floor_rtts.pop
187
+ end
188
+ @floor_times << now
189
+ @floor_rtts << latest_rtt
190
+ end
191
+ while (time = @floor_times.first) && time < now - min_rtt
192
+ @floor_times.shift
193
+ @floor_rtts.shift
194
+ end
195
+ end
196
+
197
+ # Whether the path builds no queue: rtt_floor no greater than
198
+ # queue_buildup_thresh.
199
+ private def three_times?
200
+ min_rtt = @min_rtt or return false
201
+ floor = @floor_rtts.first or return false
202
+ floor <= self.class.queue_buildup_threshold(min_rtt)
203
+ end
204
+
205
+ private def enter_recovery(beta, now)
206
+ @phase = :recovery
207
+ @initial = false
208
+ @recovery_start = now
209
+ @beta = beta
210
+ @floor = [@cwnd * beta / 3, @initial_window.call(@max_datagram_size) * beta, minimum_window.to_f].max
211
+ reduce(@cwnd * (1 - silence_factor))
212
+ end
213
+
214
+ private def silence_factor = @beta + K * (1 - @beta)
215
+
216
+ private def ack_factor = K * (1 - @beta)
217
+
218
+ # The silence factor, so that the window the period ends with is
219
+ # beta times the bytes acknowledged, however many were lost.
220
+ private def loss_factor = silence_factor
221
+
222
+ # The minimum window as it is now, after an MTU update.
223
+ private def reduce(bytes)
224
+ @cwnd = [@cwnd - bytes, @floor, minimum_window.to_f].max
225
+ end
226
+
227
+ # The controller takes its state from where Rapid Start's left off,
228
+ # and the tracer hears of the change from Rapid Start's.
229
+ private def hand_over(now)
230
+ full_bdp = @phase == :recovery ? @cwnd / @beta : @cwnd
231
+ @phase = :done
232
+ @cc.on_startup_exit(window: @cwnd.to_i, full_bdp:, recovery_start: @recovery_start, smoothed_rtt: @smoothed_rtt, now:)
233
+ state = @cc.state
234
+ @tracer.congestion_state_updated(old: @state, new: state, now:) if @tracer && state != @state # steep:ignore
235
+ end
236
+
237
+ private def update_state(now)
238
+ state = if @phase == :recovery then :recovery
239
+ elsif @app_limited then :application_limited
240
+ else :rapid_start
241
+ end
242
+ return if state == @state
243
+ @tracer.congestion_state_updated(old: @state, new: state, now:) if @tracer # steep:ignore
244
+ @state = state
245
+ end
246
+ end
247
+ end
248
+ end
249
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ module Congestion
5
+ # The parts that wrap a path's controller while the connection starts,
6
+ # in place of its slow start, and hand it back for the rest
7
+ # (Config#startup).
8
+ module Startup
9
+ end
10
+ end
11
+ end
12
+
13
+ require_relative "startup/rapid_start"
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # Congestion control, one controller a path. Loss recovery tells it what
5
+ # was sent, acknowledged and lost, and asks it for the window; it keeps no
6
+ # count of the bytes in flight and does not detect losses itself.
7
+ module Congestion
8
+ end
9
+ end
10
+
11
+ require_relative "congestion/pacing"
12
+ require_relative "congestion/new_reno"
13
+ require_relative "congestion/cubic"
14
+ require_relative "congestion/startup"
15
+ require_relative "congestion/algorithms"