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,229 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ipaddr"
4
+
5
+ module Raiha::QUIC
6
+ # What connections are set up with, one value for every connection of an
7
+ # Endpoint and shareable between Ractors: the transport parameters this
8
+ # end sends, named as RFC 9000 names them, and settings of this
9
+ # implementation that never go on the wire. Certificates and keys, the
10
+ # random source and the tracer are not here, since they cannot be shared
11
+ # between Ractors; the TLS settings are in the TLS layer's Config.
12
+ #
13
+ # Times are Float seconds. initial_max_streams_uni is 100, as quic-go's
14
+ # and quiche's are: HTTP/3 needs three of the peer's streams going one
15
+ # way, and the peer's streams of reserved or unknown types are not to
16
+ # leave it short of them. max_datagram_frame_size nil (or 0) is no
17
+ # DATAGRAM frames; RFC 9221 suggests 65535 to have them. max_udp_payload_size
18
+ # is what this end can receive, and initial_max_datagram_size what it
19
+ # sends before a path is probed: two things, however alike the names.
20
+ # enable_pmtud probes each path for larger datagrams (RFC 8899
21
+ # "Datagram Packetization Layer Path MTU Discovery"); a probe that IP
22
+ # fragments on the way would pass for one that fits, so the I/O layer
23
+ # must keep IP from fragmenting what it sends, as Runtime::UDP does
24
+ # where it can, or the application sets it false.
25
+ #
26
+ # congestion_control is the class of the algorithm each path makes its
27
+ # controller from, or its name in Congestion::ALGORITHMS; initial_window
28
+ # turns a maximum datagram size into the initial window and must be
29
+ # shareable, as a lambda made by Ractor.shareable_lambda. startup is the
30
+ # class that wraps the controller while the connection starts, nil for
31
+ # the controller's own slow start, and pacing_granularity the seconds
32
+ # between the wake-ups of pacing. max_transmit_size is the most bytes a
33
+ # Transmit carries, datagrams of one size but the last for GSO, nil for
34
+ # a datagram each, as an I/O layer that sends Transmit#payload as it is
35
+ # needs. fast_convergence is CUBIC's (RFC 9438
36
+ # "Fast Convergence"), and beta_ecn the factor the window is multiplied by
37
+ # when an ECN-CE mark alone starts a recovery period, nil for the
38
+ # algorithm's own factor of a loss.
39
+ #
40
+ # retry_token_key and stateless_reset_key are the secrets an Endpoint
41
+ # makes its Retry tokens and stateless reset tokens with; every worker
42
+ # serving the same connections, in Ractors or processes, must have the
43
+ # same ones.
44
+ # Left nil, an Endpoint draws its own, which only one process can use.
45
+ # inspect does not show them.
46
+ #
47
+ # preferred_address is the address a server offers its clients to move
48
+ # to once the handshake is confirmed (RFC 9000 "Preferred Address"),
49
+ # `{ipv4: [host, port], ipv6: [host, port]}` with either or both, nil
50
+ # for none. address_families are those of the addresses this end can
51
+ # send to, :ipv4 and :ipv6, which a client chooses the server's
52
+ # preferred address from. ecn_address_families are those whose paths
53
+ # use ECN; a path to an address of another starts with ECN failed, its
54
+ # packets Not-ECT, as where the socket can neither mark nor read the
55
+ # codepoint (Runtime::UDP.ecn, OpenBSD's IPv4).
56
+ class Config < Data.define(
57
+ :max_idle_timeout, :max_udp_payload_size,
58
+ :initial_max_data, :initial_max_stream_data_bidi_local, :initial_max_stream_data_bidi_remote, :initial_max_stream_data_uni,
59
+ :initial_max_streams_bidi, :initial_max_streams_uni,
60
+ :max_ack_delay, :ack_delay_exponent, :active_connection_id_limit, :disable_active_migration, :max_datagram_frame_size,
61
+ :versions, :offered_versions, :deployed_versions, :connection_id_length, :initial_max_datagram_size, :enable_pmtud,
62
+ :max_stream_receive_window, :max_receive_window, :max_send_buffer,
63
+ :congestion_control, :initial_window, :startup, :pacing_granularity, :max_transmit_size, :fast_convergence, :beta_ecn,
64
+ :retry_token_key, :stateless_reset_key, :preferred_address, :address_families, :ecn_address_families
65
+ )
66
+ include Raiha::TLS::MaskedInspect
67
+
68
+ DEFAULT_CONGESTION_CONTROL = Congestion::Cubic
69
+ # RFC 9002 "Initial and Minimum Congestion Window". Made shareable here,
70
+ # where self is the class.
71
+ DEFAULT_INITIAL_WINDOW = Ractor.make_shareable(->(max_datagram_size) { [10 * max_datagram_size, [14_720, 2 * max_datagram_size].max].min })
72
+
73
+ # The fewest bytes a key of an Endpoint's tokens has: HMAC-SHA256's.
74
+ MIN_KEY_LENGTH = 32
75
+
76
+ ADDRESS_FAMILIES = %i[ipv4 ipv6].freeze
77
+
78
+ TRANSPORT_PARAMETERS = %i[
79
+ max_idle_timeout max_udp_payload_size
80
+ initial_max_data initial_max_stream_data_bidi_local initial_max_stream_data_bidi_remote initial_max_stream_data_uni
81
+ initial_max_streams_bidi initial_max_streams_uni
82
+ max_ack_delay ack_delay_exponent active_connection_id_limit disable_active_migration max_datagram_frame_size
83
+ ].freeze
84
+
85
+ # Beside the rules of the transport parameters themselves: the versions
86
+ # are supported ones, each once, those offered some of them, and those
87
+ # fully deployed some of those offered (RFC 9368 "Server Deployments of
88
+ # QUIC"); the connection IDs this end issues are 0 to 20 bytes, a
89
+ # datagram is 1200 bytes at least (RFC 9000 "Datagram Size"),
90
+ # and the receive windows can grow from the initial ones: the stream
91
+ # window to each initial stream limit, and the connection window to the
92
+ # initial limit and to 1.5 times the stream window. The initial window
93
+ # is the minimum of two datagrams at least, pacing wakes up after some
94
+ # time, a Transmit carries something, and beta_ecn shrinks the window. A
95
+ # key has MIN_KEY_LENGTH bytes at least. A preferred address has an
96
+ # address of its family and a port, and comes with connection IDs that
97
+ # are not zero-length, as a server that sends with none offers none (RFC
98
+ # 9000 "Preferred Address"); the address families are some of
99
+ # ADDRESS_FAMILIES.
100
+ def initialize(
101
+ max_idle_timeout: 30.0, max_udp_payload_size: 65_527,
102
+ initial_max_data: 768 * 1024, initial_max_stream_data_bidi_local: 512 * 1024,
103
+ initial_max_stream_data_bidi_remote: 512 * 1024, initial_max_stream_data_uni: 512 * 1024,
104
+ initial_max_streams_bidi: 100, initial_max_streams_uni: 100,
105
+ max_ack_delay: 0.025, ack_delay_exponent: 3, active_connection_id_limit: 8, disable_active_migration: false,
106
+ max_datagram_frame_size: nil,
107
+ versions: [Version::V2, Version::V1], offered_versions: nil, deployed_versions: nil, connection_id_length: 8, initial_max_datagram_size: 1200, enable_pmtud: true,
108
+ max_stream_receive_window: 6 * 1024 * 1024, max_receive_window: 15 * 1024 * 1024, max_send_buffer: 15 * 1024 * 1024,
109
+ congestion_control: DEFAULT_CONGESTION_CONTROL, initial_window: DEFAULT_INITIAL_WINDOW, startup: nil, pacing_granularity: 0.001,
110
+ max_transmit_size: nil, fast_convergence: true, beta_ecn: nil, retry_token_key: nil, stateless_reset_key: nil,
111
+ preferred_address: nil, address_families: ADDRESS_FAMILIES, ecn_address_families: ADDRESS_FAMILIES
112
+ )
113
+ TransportParameters.new(
114
+ max_idle_timeout:, max_udp_payload_size:, initial_max_data:, initial_max_stream_data_bidi_local:,
115
+ initial_max_stream_data_bidi_remote:, initial_max_stream_data_uni:, initial_max_streams_bidi:, initial_max_streams_uni:,
116
+ max_ack_delay:, ack_delay_exponent:, active_connection_id_limit:, disable_active_migration:,
117
+ max_datagram_frame_size: max_datagram_frame_size || 0
118
+ )
119
+ raise ArgumentError, "no versions" if versions.empty?
120
+ versions.each { |version| raise ArgumentError, "#{version.inspect} is no supported version" unless Version::SUPPORTED.include?(version) }
121
+ raise ArgumentError, "a version twice in #{versions.map(&:number).inspect}" unless versions.uniq.size == versions.size
122
+ if offered_versions
123
+ raise ArgumentError, "no offered_versions" if offered_versions.empty?
124
+ check_subset(:offered_versions, offered_versions, :versions, versions)
125
+ end
126
+ check_subset(:deployed_versions, deployed_versions, :offered_versions, offered_versions || versions) if deployed_versions
127
+ unless connection_id_length.between?(0, Wire::CONNECTION_ID_MAX_LENGTH)
128
+ raise ArgumentError, "connection_id_length #{connection_id_length} is not 0 to 20"
129
+ end
130
+ raise ArgumentError, "initial_max_datagram_size #{initial_max_datagram_size} is under 1200" if initial_max_datagram_size < 1200
131
+ [initial_max_stream_data_bidi_local, initial_max_stream_data_bidi_remote, initial_max_stream_data_uni].each do |initial|
132
+ raise ArgumentError, "max_stream_receive_window #{max_stream_receive_window} is under #{initial}" if max_stream_receive_window < initial
133
+ end
134
+ raise ArgumentError, "max_receive_window #{max_receive_window} is under initial_max_data" if max_receive_window < initial_max_data
135
+ if max_receive_window < max_stream_receive_window * 1.5
136
+ raise ArgumentError, "max_receive_window #{max_receive_window} is under 1.5 times max_stream_receive_window"
137
+ end
138
+ raise ArgumentError, "max_send_buffer #{max_send_buffer} is not positive" unless max_send_buffer.positive?
139
+ congestion_control = Congestion.resolve(congestion_control)
140
+ window = initial_window.call(initial_max_datagram_size)
141
+ unless window.is_a?(Integer) && window >= 2 * initial_max_datagram_size
142
+ raise ArgumentError, "an initial window of #{window.inspect} for #{initial_max_datagram_size}-byte datagrams is under two of them"
143
+ end
144
+ case startup
145
+ when nil, Class then nil
146
+ else raise ArgumentError, "startup is a class or nil"
147
+ end
148
+ raise ArgumentError, "pacing_granularity #{pacing_granularity} is not positive" unless pacing_granularity.positive?
149
+ raise ArgumentError, "max_transmit_size #{max_transmit_size} is not positive" if max_transmit_size && !max_transmit_size.positive?
150
+ raise ArgumentError, "beta_ecn #{beta_ecn} is not between 0 and 1" if beta_ecn && !(beta_ecn > 0 && beta_ecn < 1)
151
+ { retry_token_key:, stateless_reset_key: }.each do |name, key|
152
+ raise ArgumentError, "#{name} is under #{MIN_KEY_LENGTH} bytes" if key && key.bytesize < MIN_KEY_LENGTH
153
+ end
154
+ check_preferred_address(preferred_address, connection_id_length) if preferred_address
155
+ if address_families.empty? || !(address_families - ADDRESS_FAMILIES).empty?
156
+ raise ArgumentError, "address_families #{address_families.inspect} are not some of #{ADDRESS_FAMILIES.inspect}"
157
+ end
158
+ # None is ECN on no path at all.
159
+ unless (ecn_address_families - ADDRESS_FAMILIES).empty?
160
+ raise ArgumentError, "ecn_address_families #{ecn_address_families.inspect} are not of #{ADDRESS_FAMILIES.inspect}"
161
+ end
162
+ retry_token_key = retry_token_key&.b&.freeze
163
+ stateless_reset_key = stateless_reset_key&.b&.freeze
164
+ # A copy, so that making it shareable does not freeze the caller's.
165
+ versions = Ractor.make_shareable(versions, copy: true)
166
+ offered_versions = Ractor.make_shareable(offered_versions, copy: true)
167
+ deployed_versions = Ractor.make_shareable(deployed_versions, copy: true)
168
+ preferred_address = Ractor.make_shareable(preferred_address, copy: true)
169
+ address_families = Ractor.make_shareable(address_families, copy: true)
170
+ ecn_address_families = Ractor.make_shareable(ecn_address_families, copy: true)
171
+ begin
172
+ initial_window = Ractor.make_shareable(initial_window, copy: true)
173
+ rescue TypeError, Ractor::Error => error
174
+ raise ArgumentError, "initial_window cannot be shared between Ractors (#{error.message}); make it with Ractor.shareable_lambda"
175
+ end
176
+ super
177
+ end
178
+
179
+ def masked_members = %i[retry_token_key stateless_reset_key]
180
+
181
+ # The versions a server's Version Negotiation packet lists, the Offered
182
+ # Versions of RFC 9368 "Server Deployments of QUIC": `offered_versions`,
183
+ # or `versions` when nil. A client reads neither this nor `deployed`.
184
+ def offered = offered_versions || versions
185
+
186
+ # The versions every server of the deployment supports, the Fully
187
+ # Deployed Versions of RFC 9368, which a server's Available Versions
188
+ # list and it changes to by compatible version negotiation:
189
+ # `deployed_versions`, or `offered` when nil. While a version is added
190
+ # to the deployment or taken out, they are fewer, so that no client
191
+ # takes the difference for a downgrade.
192
+ def deployed = deployed_versions || offered
193
+
194
+ # `subset`, the setting `name`, is some of `superset`, the setting
195
+ # `of`, each once.
196
+ private def check_subset(name, subset, of, superset)
197
+ raise ArgumentError, "a version twice in #{name}" unless subset.uniq.size == subset.size
198
+ return if (subset - superset).empty?
199
+ raise ArgumentError, "#{name} #{subset.map(&:number).inspect} are not some of #{of} #{superset.map(&:number).inspect}"
200
+ end
201
+
202
+ private def check_preferred_address(preferred_address, connection_id_length)
203
+ raise ArgumentError, "a preferred address with zero-length connection IDs" if connection_id_length.zero?
204
+ if preferred_address.empty? || !(preferred_address.keys - ADDRESS_FAMILIES).empty?
205
+ raise ArgumentError, "a preferred address of #{preferred_address.keys.inspect}, not some of #{ADDRESS_FAMILIES.inspect}"
206
+ end
207
+ preferred_address.each do |family, (host, port)|
208
+ address = IPAddr.new(host)
209
+ # An IPv4-mapped address is IPv4's on the wire, and comes in as IPv4's.
210
+ raise ArgumentError, "#{host} is no #{family} address" unless family == :ipv4 ? address.ipv4? : address.ipv6? && !address.ipv4_mapped?
211
+ raise ArgumentError, "a preferred address with port #{port}" unless port.between?(1, 65_535)
212
+ end
213
+ end
214
+
215
+ # The transport parameters a connection with these settings sends, with
216
+ # its connection IDs, its version information (RFC 9368) and, for a
217
+ # server, its token and preferred address.
218
+ def transport_parameters(
219
+ initial_source_connection_id:, original_destination_connection_id: nil, retry_source_connection_id: nil,
220
+ stateless_reset_token: nil, preferred_address: nil, version_information: nil
221
+ )
222
+ parameters = to_h.slice(*TRANSPORT_PARAMETERS).merge(max_datagram_frame_size: max_datagram_frame_size || 0)
223
+ TransportParameters.new(
224
+ **parameters, initial_source_connection_id:, original_destination_connection_id:, retry_source_connection_id:,
225
+ stateless_reset_token:, preferred_address:, version_information:
226
+ )
227
+ end
228
+ end
229
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ module Congestion
5
+ # The algorithms that come with the layer, by name.
6
+ ALGORITHMS = { cubic: Cubic, new_reno: NewReno }.freeze
7
+
8
+ # The algorithm `name` stands for: a Symbol of ALGORITHMS, or a class,
9
+ # one of these or of the application's own, passed through as it is.
10
+ def self.resolve(name)
11
+ case name
12
+ when Symbol then ALGORITHMS.fetch(name) { raise ArgumentError, "unknown congestion control: #{name.inspect}" }
13
+ else name
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,258 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ module Congestion
5
+ # CUBIC as RFC 9438 describes it. Slow start is Reno's. In congestion
6
+ # avoidance the window follows the cubic function of the time since the
7
+ # avoidance stage began,
8
+ #
9
+ # W_cubic(t) = C * (t - K)^3 + W_max
10
+ #
11
+ # which is flat around W_max, the window before the last reduction: it
12
+ # grows towards W_cubic(t + smoothed RTT), at most to 1.5 times the
13
+ # window, each ACK frame (the concave region below W_max and the convex
14
+ # region above it), and never below W_est, the window an AIMD sender
15
+ # with the same reduction would have (the Reno-friendly region). A loss
16
+ # or an ECN-CE multiplies the window by 0.7 once a round trip, by the
17
+ # Config's beta_ecn for an ECN-CE alone when there is one, and with fast
18
+ # convergence lowers W_max further when the window is below it.
19
+ # Persistent congestion takes the window to the minimum, back into slow
20
+ # start, and the stage after it starts from where the window is.
21
+ #
22
+ # The window is cut from itself, not from the bytes in flight; the
23
+ # time `t` stands still while the connection is application-limited, so
24
+ # that the window does not jump after an idle period (RFC 9438
25
+ # "Window Increase Function").
26
+ #
27
+ # Windows are bytes here, the segments of RFC 9438 times the maximum
28
+ # datagram size; the window is kept as a Float, so that the small growth
29
+ # of an ACK is not lost, and given as an Integer.
30
+ class Cubic
31
+ # β_cubic, the factor a congestion event multiplies the window by.
32
+ BETA = 0.7
33
+ # C, in segments a second cubed.
34
+ C = 0.4
35
+ # α_cubic, the growth of W_est a round trip in segments, before W_est
36
+ # reaches the window before the last reduction.
37
+ ALPHA = 3 * (1 - BETA) / (1 + BETA)
38
+ # The most target goes above the window, as a factor of it.
39
+ MAX_TARGET_FACTOR = 1.5
40
+
41
+ def self.requires_rate_sample? = false
42
+
43
+ # :slow_start, :recovery, :application_limited, :reno_friendly,
44
+ # :concave or :convex.
45
+ attr_reader :state
46
+
47
+ # `config` gives fast_convergence and beta_ecn.
48
+ def initialize(max_datagram_size:, initial_window:, config:, tracer: nil)
49
+ @max_datagram_size = max_datagram_size
50
+ @initial_window = initial_window
51
+ @fast_convergence = config.fast_convergence
52
+ @beta_ecn = config.beta_ecn
53
+ @cwnd = initial_window.call(max_datagram_size).to_f
54
+ @ssthresh = nil #: Float?
55
+ @recovery_start = nil #: Float?
56
+ @in_recovery = false
57
+ @app_limited = false
58
+ @initial = true
59
+ # The congestion avoidance stage: when it began, the window then,
60
+ # W_max and K, W_est and α_cubic, and the window before the last
61
+ # reduction.
62
+ @epoch_start = nil #: Float?
63
+ @w_max = 0.0
64
+ @k = 0.0
65
+ @w_est = 0.0
66
+ @alpha = ALPHA
67
+ @cwnd_prior = nil #: Float?
68
+ # Since when the connection has been application-limited, which
69
+ # the time of the stage does not count.
70
+ @app_limited_since = nil #: Float?
71
+ # A stage after persistent congestion starts with K = 0 and W_max the
72
+ # window then (RFC 9438 "Timeout").
73
+ @after_timeout = false
74
+ @state = :slow_start
75
+ @tracer = tracer
76
+ # The smoothed RTT once there is an RTT sample, which pacing waits
77
+ # for.
78
+ @smoothed_rtt = nil #: Float?
79
+ end
80
+
81
+ def window = @cwnd.to_i
82
+
83
+ def minimum_window = 2 * @max_datagram_size
84
+
85
+ def on_packet_sent(bytes:, now:) = nil
86
+
87
+ def on_ack(acked_bytes:, largest_acked_sent_at:, latest_rtt:, smoothed_rtt:, min_rtt:, app_limited:, now:, rate_sample: nil)
88
+ @smoothed_rtt = smoothed_rtt if min_rtt
89
+ start = @recovery_start
90
+ @in_recovery = false if start && largest_acked_sent_at > start
91
+ @app_limited = app_limited
92
+ pause_epoch(app_limited, now)
93
+ unless @in_recovery || app_limited
94
+ if slow_start?
95
+ @cwnd += acked_bytes
96
+ else
97
+ avoid_congestion(acked_bytes, smoothed_rtt, now)
98
+ end
99
+ @initial = false
100
+ end
101
+ update_state(now)
102
+ end
103
+
104
+ def on_loss(lost_bytes:, largest_lost_sent_at:, persistent:, now:)
105
+ congestion_event(largest_lost_sent_at, now, BETA)
106
+ if persistent
107
+ @cwnd = minimum_window.to_f
108
+ @recovery_start = nil
109
+ @in_recovery = false
110
+ @epoch_start = nil
111
+ @after_timeout = true
112
+ end
113
+ update_state(now)
114
+ end
115
+
116
+ def on_ecn_ce(largest_acked_sent_at:, now:)
117
+ congestion_event(largest_acked_sent_at, now, @beta_ecn || BETA)
118
+ update_state(now)
119
+ end
120
+
121
+ # The minimum window follows the size, and an initial window not yet
122
+ # moved is worked out again (RFC 9002 "Initial and Minimum Congestion
123
+ # Window").
124
+ def on_mtu_updated(max_datagram_size:, now:)
125
+ @max_datagram_size = max_datagram_size
126
+ @cwnd = @initial_window.call(max_datagram_size).to_f if @initial
127
+ @cwnd = [@cwnd, minimum_window.to_f].max
128
+ end
129
+
130
+ # Bytes a second (Pacing), nil until there is an RTT sample: the
131
+ # initial window may go at once before then (RFC 9002 "Pacing").
132
+ def pacing_rate = (rtt = @smoothed_rtt) && Pacing.rate(@cwnd, rtt, slow_start: slow_start?)
133
+
134
+ # β_cubic, or the Config's beta_ecn for a congestion event an ECN-CE
135
+ # alone started when `ecn`; a startup works its recovery out from it.
136
+ def beta(ecn:) = (@beta_ecn if ecn) || BETA
137
+
138
+ # A startup that wrapped this controller is done with the first
139
+ # recovery period, which began at `recovery_start`: its window goes on
140
+ # in congestion avoidance, and a loss of a packet sent before the
141
+ # period began is no new congestion event. The smoothed RTT, when the
142
+ # startup had one, paces at once, though no ACK has come yet; the
143
+ # startup tells the tracer of the change of state. W_max, and the window before
144
+ # the reduction, are twice `full_bdp`, the BDP and queue the startup
145
+ # came to: the W_max after a classic slow start, which overshoots the
146
+ # BDP twice, as quicly has it. The stage begins with the first ACK in
147
+ # congestion avoidance, K from W_max and the window then, without fast
148
+ # convergence, as the startup reduced the window itself.
149
+ def on_startup_exit(window:, full_bdp:, recovery_start:, smoothed_rtt:, now:)
150
+ @cwnd = [window.to_f, minimum_window.to_f].max
151
+ @ssthresh = @cwnd
152
+ @w_max = 2 * full_bdp
153
+ @cwnd_prior = @w_max
154
+ @recovery_start = recovery_start
155
+ @in_recovery = false
156
+ @initial = false
157
+ @epoch_start = nil
158
+ @alpha = ALPHA
159
+ @after_timeout = false
160
+ @smoothed_rtt = smoothed_rtt if smoothed_rtt
161
+ @state = current_state(now)
162
+ end
163
+
164
+ def metrics
165
+ metrics = { congestion_window: window } #: Hash[Symbol, Integer]
166
+ ssthresh = @ssthresh
167
+ metrics[:ssthresh] = ssthresh.to_i if ssthresh
168
+ rate = pacing_rate
169
+ # qlog has it in bits a second.
170
+ metrics[:pacing_rate] = (rate * 8).to_i if rate
171
+ metrics
172
+ end
173
+
174
+ private def slow_start?
175
+ ssthresh = @ssthresh
176
+ ssthresh.nil? || @cwnd < ssthresh
177
+ end
178
+
179
+ # The time of the stage stands still while application-limited: the
180
+ # stage begins later by how long that lasted.
181
+ private def pause_epoch(app_limited, now)
182
+ if app_limited
183
+ @app_limited_since ||= now
184
+ elsif (since = @app_limited_since)
185
+ @app_limited_since = nil
186
+ epoch = @epoch_start
187
+ @epoch_start = epoch + (now - since) if epoch
188
+ end
189
+ end
190
+
191
+ # One ACK frame of `acked_bytes` in congestion avoidance (RFC 9438
192
+ # "Reno-Friendly Region", "Concave Region", "Convex Region").
193
+ private def avoid_congestion(acked_bytes, smoothed_rtt, now)
194
+ start_epoch(now) unless @epoch_start
195
+ epoch = @epoch_start || now
196
+ t = now - epoch
197
+ @w_est += @alpha * @max_datagram_size * acked_bytes / @cwnd
198
+ prior = @cwnd_prior
199
+ @alpha = 1.0 if prior && @w_est >= prior
200
+ if w_cubic(t) < @w_est
201
+ @cwnd = [@cwnd, @w_est].max
202
+ else
203
+ target = w_cubic(t + smoothed_rtt).clamp(@cwnd, MAX_TARGET_FACTOR * @cwnd)
204
+ @cwnd += (target - @cwnd) * acked_bytes / @cwnd
205
+ end
206
+ end
207
+
208
+ # A congestion avoidance stage begins: K from W_max and the window
209
+ # now, and W_est from the window.
210
+ private def start_epoch(now)
211
+ @epoch_start = now
212
+ if @after_timeout || @w_max <= @cwnd
213
+ @after_timeout = false
214
+ @w_max = @cwnd
215
+ @k = 0.0
216
+ else
217
+ @k = Math.cbrt((@w_max - @cwnd) / (C * @max_datagram_size))
218
+ end
219
+ @w_est = @cwnd
220
+ end
221
+
222
+ private def w_cubic(t) = C * @max_datagram_size * (t - @k)**3 + @w_max
223
+
224
+ # RFC 9002 appendix B "On New Congestion Event", with RFC 9438
225
+ # "Multiplicative Decrease" and "Fast Convergence": nothing for a packet
226
+ # sent before the current recovery period began.
227
+ private def congestion_event(sent_at, now, beta)
228
+ start = @recovery_start
229
+ return if start && sent_at <= start
230
+ @recovery_start = now
231
+ @in_recovery = true
232
+ @initial = false
233
+ @w_max = @fast_convergence && @cwnd < @w_max ? @cwnd * (1 + beta) / 2 : @cwnd
234
+ @cwnd_prior = @cwnd
235
+ @ssthresh = @cwnd = [@cwnd * beta, minimum_window.to_f].max
236
+ @epoch_start = nil
237
+ @alpha = ALPHA
238
+ end
239
+
240
+ private def current_state(now)
241
+ if @in_recovery then :recovery
242
+ elsif @app_limited then :application_limited
243
+ elsif slow_start? then :slow_start
244
+ elsif @epoch_start && w_cubic(now - (@epoch_start || now)) < @w_est then :reno_friendly
245
+ elsif @cwnd < @w_max then :concave
246
+ else :convex
247
+ end
248
+ end
249
+
250
+ private def update_state(now)
251
+ state = current_state(now)
252
+ return if state == @state
253
+ @tracer.congestion_state_updated(old: @state, new: state, now:) if @tracer # steep:ignore
254
+ @state = state
255
+ end
256
+ end
257
+ end
258
+ end
@@ -0,0 +1,161 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ module Congestion
5
+ # NewReno as RFC 9002 "Congestion Control" and its appendix B describe
6
+ # it. The window grows by the bytes acknowledged in slow start and by
7
+ # one datagram a window in congestion avoidance; a loss or an ECN-CE
8
+ # halves it once a round trip, or an ECN-CE alone multiplies it by the
9
+ # Config's beta_ecn when there is one, and persistent congestion takes
10
+ # it to the minimum, back into slow start.
11
+ #
12
+ # Loss recovery calls it once an ACK frame, not once a packet: the
13
+ # recovery period ends with the first ACK whose largest acknowledged
14
+ # packet was sent after the period began.
15
+ class NewReno
16
+ # kLossReductionFactor, as a divisor of the window.
17
+ LOSS_REDUCTION_DIVISOR = 2
18
+
19
+ # No rate samples: the window is all it needs.
20
+ def self.requires_rate_sample? = false
21
+
22
+ # Bytes.
23
+ attr_reader :window
24
+
25
+ # `initial_window` turns a maximum datagram size into the initial
26
+ # window, so that it can be worked out again when the size changes.
27
+ # `config` is the connection's Config, which an algorithm reads its own
28
+ # settings from.
29
+ def initialize(max_datagram_size:, initial_window:, config:, tracer: nil)
30
+ @max_datagram_size = max_datagram_size
31
+ @initial_window = initial_window
32
+ @beta_ecn = config.beta_ecn
33
+ @window = initial_window.call(max_datagram_size)
34
+ @ssthresh = nil #: Integer?
35
+ @recovery_start = nil #: Float?
36
+ @in_recovery = false
37
+ @app_limited = false
38
+ # Whether the window is still the initial one.
39
+ @initial = true
40
+ @state = :slow_start
41
+ @tracer = tracer
42
+ # The smoothed RTT once there is an RTT sample, which pacing waits
43
+ # for.
44
+ @smoothed_rtt = nil #: Float?
45
+ end
46
+
47
+ def minimum_window = 2 * @max_datagram_size
48
+
49
+ def on_packet_sent(bytes:, now:) = nil
50
+
51
+ def on_ack(acked_bytes:, largest_acked_sent_at:, latest_rtt:, smoothed_rtt:, min_rtt:, app_limited:, now:, rate_sample: nil)
52
+ @smoothed_rtt = smoothed_rtt if min_rtt
53
+ start = @recovery_start
54
+ @in_recovery = false if start && largest_acked_sent_at > start
55
+ @app_limited = app_limited
56
+ unless @in_recovery || app_limited
57
+ @window += slow_start? ? acked_bytes : @max_datagram_size * acked_bytes / @window
58
+ @initial = false
59
+ end
60
+ update_state(now)
61
+ end
62
+
63
+ # `persistent` takes the window to the minimum even within a recovery
64
+ # period, and ends the period (RFC 9002 "Persistent Congestion").
65
+ def on_loss(lost_bytes:, largest_lost_sent_at:, persistent:, now:)
66
+ congestion_event(largest_lost_sent_at, now)
67
+ if persistent
68
+ @window = minimum_window
69
+ @recovery_start = nil
70
+ @in_recovery = false
71
+ end
72
+ update_state(now)
73
+ end
74
+
75
+ def on_ecn_ce(largest_acked_sent_at:, now:)
76
+ congestion_event(largest_acked_sent_at, now, beta: @beta_ecn)
77
+ update_state(now)
78
+ end
79
+
80
+ # The minimum window follows the size, and an initial window not yet
81
+ # moved is worked out again (RFC 9002 "Initial and Minimum Congestion
82
+ # Window").
83
+ def on_mtu_updated(max_datagram_size:, now:)
84
+ @max_datagram_size = max_datagram_size
85
+ @window = @initial_window.call(max_datagram_size) if @initial
86
+ @window = [@window, minimum_window].max
87
+ end
88
+
89
+ # Bytes a second (Pacing), nil until there is an RTT sample: the
90
+ # initial window may go at once before then (RFC 9002 "Pacing").
91
+ def pacing_rate = (rtt = @smoothed_rtt) && Pacing.rate(@window, rtt, slow_start: slow_start?)
92
+
93
+ # The factor a congestion event multiplies the window by, for one an
94
+ # ECN-CE alone started when `ecn`; a startup works its recovery out
95
+ # from it.
96
+ def beta(ecn:) = (@beta_ecn if ecn) || 1.0 / LOSS_REDUCTION_DIVISOR
97
+
98
+ # A startup that wrapped this controller is done with the first
99
+ # recovery period, which began at `recovery_start`: its window goes on
100
+ # in congestion avoidance, and a loss of a packet sent before the
101
+ # period began is no new congestion event. The smoothed RTT, when the
102
+ # startup had one, paces at once, though no ACK has come yet; the
103
+ # startup tells the tracer of the change of state. `full_bdp`, the BDP
104
+ # and queue it came to, says nothing to NewReno.
105
+ def on_startup_exit(window:, full_bdp:, recovery_start:, smoothed_rtt:, now:)
106
+ @window = [window, minimum_window].max
107
+ @ssthresh = @window
108
+ @recovery_start = recovery_start
109
+ @in_recovery = false
110
+ @initial = false
111
+ @smoothed_rtt = smoothed_rtt if smoothed_rtt
112
+ @state = current_state(now)
113
+ end
114
+
115
+ # :slow_start, :congestion_avoidance, :recovery or :application_limited.
116
+ attr_reader :state
117
+
118
+ def metrics
119
+ metrics = { congestion_window: @window } #: Hash[Symbol, Integer]
120
+ metrics[:ssthresh] = @ssthresh if @ssthresh # steep:ignore
121
+ rate = pacing_rate
122
+ # qlog has it in bits a second.
123
+ metrics[:pacing_rate] = (rate * 8).to_i if rate
124
+ metrics
125
+ end
126
+
127
+ private def slow_start?
128
+ ssthresh = @ssthresh
129
+ ssthresh.nil? || @window < ssthresh
130
+ end
131
+
132
+ # RFC 9002 appendix B "On New Congestion Event": nothing for a packet
133
+ # sent before the current recovery period began. The window is halved,
134
+ # or multiplied by `beta` when there is one.
135
+ private def congestion_event(sent_at, now, beta: nil)
136
+ start = @recovery_start
137
+ return if start && sent_at <= start
138
+ @recovery_start = now
139
+ @in_recovery = true
140
+ @initial = false
141
+ ssthresh = @ssthresh = beta ? (@window * beta).to_i : @window / LOSS_REDUCTION_DIVISOR
142
+ @window = [ssthresh, minimum_window].max
143
+ end
144
+
145
+ private def current_state(now)
146
+ if @in_recovery then :recovery
147
+ elsif @app_limited then :application_limited
148
+ elsif slow_start? then :slow_start
149
+ else :congestion_avoidance
150
+ end
151
+ end
152
+
153
+ private def update_state(now)
154
+ state = current_state(now)
155
+ return if state == @state
156
+ @tracer.congestion_state_updated(old: @state, new: state, now:) if @tracer # steep:ignore
157
+ @state = state
158
+ end
159
+ end
160
+ end
161
+ end