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,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC::Test
4
+ # A path model of one bottleneck link: datagrams wait in a queue of
5
+ # `queue` bytes, go out
6
+ # one after another at `bandwidth` bits per second, and arrive `delay`
7
+ # seconds after they have gone out. One that does not fit in the queue is
8
+ # dropped (tail-drop). With a `ce_threshold`, an ECT(0) datagram that finds
9
+ # more than that many bytes waiting ahead of it is marked CE, as DCTCP's
10
+ # switches do; L4S's ECT(1) is not handled.
11
+ class Bottleneck < Data.define(:bandwidth, :delay, :queue, :ce_threshold)
12
+ def initialize(bandwidth:, delay:, queue:, ce_threshold: nil)
13
+ raise ArgumentError, "bandwidth must be positive, not #{bandwidth}" unless bandwidth.positive?
14
+ raise ArgumentError, "delay must not be negative, not #{delay}" if delay.negative?
15
+ raise ArgumentError, "queue must be positive, not #{queue}" unless queue.positive?
16
+ raise ArgumentError, "ce_threshold must not be negative, not #{ce_threshold}" if ce_threshold&.negative?
17
+ super
18
+ end
19
+
20
+ def start(random:) = State.new(self)
21
+
22
+ class State
23
+ def initialize(model)
24
+ @model = model
25
+ # The datagrams not yet gone out: when each will have, and its size.
26
+ @waiting = [] #: Array[[Float, Integer]]
27
+ @busy_until = 0.0
28
+ # The datagrams on the wire, in the order they arrive.
29
+ @arrivals = [] #: Array[[Float, Datagram]]
30
+ end
31
+
32
+ def submit(datagram, now:)
33
+ @waiting.shift while (first = @waiting.first) && first.first <= now
34
+ size = datagram.payload.bytesize
35
+ ahead = @waiting.sum(&:last)
36
+ return if ahead + size > @model.queue
37
+ threshold = @model.ce_threshold
38
+ marked = threshold && ahead > threshold && datagram.ecn == :ect0
39
+ gone = [now, @busy_until].max + size * 8.0 / @model.bandwidth
40
+ @busy_until = gone
41
+ @waiting << [gone, size]
42
+ @arrivals << [gone + @model.delay, marked ? datagram.with(ecn: :ce) : datagram]
43
+ end
44
+
45
+ def each_delivery(now:)
46
+ while (first = @arrivals.first) && first.first <= now
47
+ @arrivals.shift
48
+ yield first.last
49
+ end
50
+ end
51
+
52
+ def next_timeout = @arrivals.first&.first
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC::Test
4
+ # What every congestion control algorithm keeps to, the application's own
5
+ # included, as tests to include in a Minitest class that names the
6
+ # algorithm:
7
+ #
8
+ # class MyAlgorithmTest < Minitest::Test
9
+ # include Raiha::QUIC::Test::CongestionContract
10
+ # def congestion_control = MyAlgorithm
11
+ # end
12
+ module CongestionContract
13
+ MAX_DATAGRAM_SIZE = 1200
14
+ # The states of the algorithms that come with the layer, Rapid Start's
15
+ # among them; an algorithm of its own may add to them.
16
+ KNOWN_STATES = %i[
17
+ slow_start congestion_avoidance recovery application_limited reno_friendly concave convex rapid_start
18
+ ].freeze
19
+ SEED = 259
20
+
21
+ # The states this algorithm may be in.
22
+ def known_congestion_states = KNOWN_STATES
23
+
24
+ def test_congestion_contract_window_stays_above_the_minimum
25
+ cc = new_controller
26
+ rng = Random.new(SEED)
27
+ now = 0.0
28
+ 1000.times do
29
+ now += rng.rand * 0.05
30
+ case rng.rand(4)
31
+ when 0, 1 then acknowledge(cc, rng.rand(1..10) * MAX_DATAGRAM_SIZE, sent_at: now - 0.1, now:, app_limited: rng.rand(4).zero?)
32
+ when 2 then cc.on_loss(lost_bytes: MAX_DATAGRAM_SIZE, largest_lost_sent_at: now - rng.rand * 0.2, persistent: rng.rand(20).zero?, now:)
33
+ else cc.on_ecn_ce(largest_acked_sent_at: now - rng.rand * 0.2, now:)
34
+ end
35
+ assert_operator cc.window, :>=, cc.minimum_window
36
+ end
37
+ end
38
+
39
+ def test_congestion_contract_loss_shrinks_the_window
40
+ cc = new_controller
41
+ acknowledge(cc, 10 * MAX_DATAGRAM_SIZE, sent_at: 0.0, now: 0.1)
42
+ before = cc.window
43
+ cc.on_loss(lost_bytes: MAX_DATAGRAM_SIZE, largest_lost_sent_at: 0.2, persistent: false, now: 0.3)
44
+ assert_operator cc.window, :<, before
45
+ end
46
+
47
+ def test_congestion_contract_persistent_congestion_takes_the_minimum
48
+ cc = new_controller
49
+ acknowledge(cc, 10 * MAX_DATAGRAM_SIZE, sent_at: 0.0, now: 0.1)
50
+ cc.on_loss(lost_bytes: MAX_DATAGRAM_SIZE, largest_lost_sent_at: 0.2, persistent: true, now: 0.3)
51
+ assert_equal cc.minimum_window, cc.window
52
+ end
53
+
54
+ def test_congestion_contract_minimum_follows_the_datagram_size
55
+ cc = new_controller
56
+ cc.on_mtu_updated(max_datagram_size: 1500, now: 0.0)
57
+ assert_equal 3000, cc.minimum_window
58
+ cc.on_loss(lost_bytes: 1500, largest_lost_sent_at: 0.1, persistent: true, now: 0.2)
59
+ cc.on_mtu_updated(max_datagram_size: 4000, now: 0.3)
60
+ assert_equal 8000, cc.minimum_window
61
+ assert_operator cc.window, :>=, 8000
62
+ end
63
+
64
+ def test_congestion_contract_no_growth_while_application_limited
65
+ cc = new_controller
66
+ window = cc.window
67
+ 20.times do |i|
68
+ acknowledge(cc, 4 * MAX_DATAGRAM_SIZE, sent_at: i * 0.1, now: i * 0.1 + 0.05, app_limited: true)
69
+ assert_operator cc.window, :<=, window
70
+ end
71
+ end
72
+
73
+ def test_congestion_contract_states_are_known
74
+ cc = new_controller
75
+ states = [cc.state]
76
+ acknowledge(cc, MAX_DATAGRAM_SIZE, sent_at: 0.0, now: 0.1)
77
+ states << cc.state
78
+ acknowledge(cc, MAX_DATAGRAM_SIZE, sent_at: 0.1, now: 0.2, app_limited: true)
79
+ states << cc.state
80
+ cc.on_loss(lost_bytes: MAX_DATAGRAM_SIZE, largest_lost_sent_at: 0.15, persistent: false, now: 0.3)
81
+ states << cc.state
82
+ acknowledge(cc, MAX_DATAGRAM_SIZE, sent_at: 0.4, now: 0.5)
83
+ states << cc.state
84
+ states.each do |state|
85
+ assert_kind_of Symbol, state
86
+ assert_includes known_congestion_states, state
87
+ end
88
+ end
89
+
90
+ def test_congestion_contract_metrics_carry_the_window
91
+ cc = new_controller
92
+ assert_equal cc.window, cc.metrics[:congestion_window]
93
+ cc.on_loss(lost_bytes: MAX_DATAGRAM_SIZE, largest_lost_sent_at: 0.0, persistent: false, now: 0.1)
94
+ assert_equal cc.window, cc.metrics[:congestion_window]
95
+ end
96
+
97
+ private def new_controller
98
+ congestion_control.new(max_datagram_size: MAX_DATAGRAM_SIZE, initial_window: Raiha::QUIC::Config::DEFAULT_INITIAL_WINDOW,
99
+ config: Raiha::QUIC::Config.new(congestion_control:))
100
+ end
101
+
102
+ private def acknowledge(cc, bytes, sent_at:, now:, app_limited: false)
103
+ cc.on_ack(acked_bytes: bytes, largest_acked_sent_at: sent_at, latest_rtt: 0.1, smoothed_rtt: 0.1, min_rtt: 0.1, app_limited:, now:)
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,244 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC::Test
4
+ # A datagram on its way through a network: what models pass along, and
5
+ # mark with CE.
6
+ class Datagram < Data.define(:payload, :source, :destination, :ecn); end
7
+
8
+ # Endpoints and connections wired together on a virtual clock, so that a
9
+ # handshake or a transfer runs with no socket and no real time. A node is
10
+ # anything with `receive(payload, from:, to:, ecn:, now:)`,
11
+ # `each_transmit(now:)`, `next_timeout` and `handle_timeout(now:)`, added at
12
+ # an address; what it sends goes to the node at the destination address.
13
+ # A node can have more addresses, and sends from the one a Transmit names
14
+ # as its source. An address can be put behind a NAT, which gives what is
15
+ # sent from it another source address.
16
+ #
17
+ # Between two nodes a link puts a path model, or an array of them in order:
18
+ # a value whose `start(random:)` gives the state of one direction, with
19
+ # `submit(datagram, now:)`, `each_delivery(now:)` and `next_timeout`. The
20
+ # randomness of each direction comes from `seed:`, so a failure happens
21
+ # again on the same seed. Without a link, datagrams arrive at once.
22
+ class Network
23
+ # How many rounds of sending and delivering one instant may take before
24
+ # the network is taken to be stuck there.
25
+ MAX_ROUNDS = 10_000
26
+
27
+ # A node or a path keeps asking for an instant that has passed, or
28
+ # keeps sending at one: time cannot go on.
29
+ class Stuck < StandardError; end
30
+
31
+ attr_reader :clock
32
+
33
+ def initialize(seed:, clock: VirtualClock.new)
34
+ @random = Random.new(seed)
35
+ @clock = clock
36
+ @nodes = {} #: Hash[Raiha::QUIC::address, _Node]
37
+ @addresses = {} #: Hash[_Node, Raiha::QUIC::address]
38
+ @addresses.compare_by_identity
39
+ @links = {} #: Hash[[Raiha::QUIC::address, Raiha::QUIC::address], _Path]
40
+ @outside = {} #: Hash[Raiha::QUIC::address, Raiha::QUIC::address]
41
+ @inside = {} #: Hash[Raiha::QUIC::address, Raiha::QUIC::address]
42
+ @direct = Direct.new
43
+ end
44
+
45
+ def now = @clock.now
46
+
47
+ # Puts `node` on the network at `address` ([host, port]) and gives it
48
+ # back.
49
+ def add(node, address:)
50
+ take(address, node)
51
+ @addresses[node] = address
52
+ node
53
+ end
54
+
55
+ # The address `node` was added at.
56
+ def address_of(node) = @addresses.fetch(node)
57
+
58
+ # Gives `node`, already on the network, another address, as a server's
59
+ # second address it offers as its preferred address.
60
+ def add_address(node, address:)
61
+ address_of(node)
62
+ take(address, node)
63
+ self
64
+ end
65
+
66
+ # Puts a node's address `inside` behind a NAT, or changes the NAT's
67
+ # binding: from now on, what the node sends from `inside` leaves from
68
+ # `to`, and what comes to `to` reaches the node at `inside`. What comes
69
+ # to the address it was seen at before is lost, as when a NAT has
70
+ # dropped the binding.
71
+ def rebind(inside, to:)
72
+ raise ArgumentError, "no node is at #{inside.inspect}" unless @nodes.key?(inside)
73
+ raise ArgumentError, "#{to.inspect} is taken" if @nodes.key?(to) || @inside.key?(to)
74
+ @inside.delete(@outside[inside])
75
+ @outside[inside] = to
76
+ @inside[to] = inside
77
+ self
78
+ end
79
+
80
+ # Puts a server's Endpoint on the network at `address`, run by a
81
+ # Test::Server that takes every offer with `tls_config` and `keyring`,
82
+ # and gives the Test::Server. `time` is the wall clock the Endpoint
83
+ # starts at; its random bytes come from the seed.
84
+ def add_server(config:, tls_config:, keyring:, time:, address:, **options)
85
+ endpoint = Raiha::QUIC::Endpoint.server(config:, random: Random.new(@random.rand(1 << 64)), time:, now:)
86
+ add(Server.new(endpoint, tls_config:, keyring:, **options), address:)
87
+ end
88
+
89
+ # Links two nodes with `model` both ways, or with `forward` from `a` to
90
+ # `b` and `reverse` back.
91
+ def link(a, b, model: nil, forward: model, reverse: model)
92
+ from = address_of(a)
93
+ to = address_of(b)
94
+ @links[[from, to]] = start(forward) if forward
95
+ @links[[to, from]] = start(reverse) if reverse
96
+ self
97
+ end
98
+
99
+ # Runs until the block is true, and gives true; or gives false once
100
+ # nothing is left to happen or `within` seconds have passed.
101
+ def run_until(within: 60.0)
102
+ deadline = now + within
103
+ loop do
104
+ settle
105
+ return true if yield
106
+ time = next_event
107
+ return false if time.nil? || time > deadline
108
+ @clock.advance_to(time)
109
+ end
110
+ end
111
+
112
+ # Lets `seconds` go by, with everything that happens in them.
113
+ def advance(seconds)
114
+ target = now + seconds
115
+ loop do
116
+ settle
117
+ time = next_event
118
+ break if time.nil? || time > target
119
+ @clock.advance_to(time)
120
+ end
121
+ @clock.advance_to(target)
122
+ settle
123
+ end
124
+
125
+ # Everything due at this instant: timers run out, datagrams sent and
126
+ # delivered, until nothing more happens. A timer run out can set
127
+ # another that is due at once, as a PTO after a loss timer may be.
128
+ private def settle
129
+ MAX_ROUNDS.times do
130
+ moved = false
131
+ @addresses.each_key do |node|
132
+ timeout = node.next_timeout
133
+ next unless timeout && timeout <= now
134
+ moved = true
135
+ node.handle_timeout(now:)
136
+ end
137
+ @addresses.each_key do |node|
138
+ node.each_transmit(now:) do |transmit|
139
+ moved = true
140
+ transmit_from(node, transmit)
141
+ end
142
+ end
143
+ each_path do |path|
144
+ path.each_delivery(now:) do |datagram|
145
+ moved = true
146
+ deliver(datagram)
147
+ end
148
+ end
149
+ return unless moved
150
+ end
151
+ raise Stuck, "still sending at #{now}"
152
+ end
153
+
154
+ # Sends from the address the Transmit names, or the one the node was
155
+ # added at, which the NAT the node is behind changes; over the link
156
+ # between the node and the one at the destination, whichever addresses
157
+ # they use.
158
+ private def transmit_from(node, transmit)
159
+ source = transmit.source || address_of(node)
160
+ raise ArgumentError, "#{source.inspect} is not an address of the node" unless @nodes[source].equal?(node) # steep:ignore
161
+ payload = transmit.payload
162
+ size = transmit.segment_size || payload.bytesize
163
+ receiver = (to = inside(transmit.destination)) && @nodes[to]
164
+ path = receiver ? @links.fetch([address_of(node), address_of(receiver)], @direct) : @direct
165
+ source = @outside.fetch(source, source)
166
+ 0.step(payload.bytesize - 1, size) do |offset|
167
+ path.submit(Datagram.new(payload: payload.byteslice(offset, size), source:, destination: transmit.destination, ecn: transmit.ecn), now:) # steep:ignore
168
+ end
169
+ end
170
+
171
+ private def deliver(datagram)
172
+ to = inside(datagram.destination) or return
173
+ node = @nodes[to] or return
174
+ node.receive(datagram.payload, from: datagram.source, to:, ecn: datagram.ecn, now:)
175
+ end
176
+
177
+ # The address on the network a datagram to `address` reaches: through
178
+ # the NAT, or none for an address the NAT hides.
179
+ private def inside(address) = @inside.fetch(address) { address unless @outside.key?(address) }
180
+
181
+ private def take(address, node)
182
+ raise ArgumentError, "#{address.inspect} is taken" if @nodes.key?(address) || @inside.key?(address)
183
+ @nodes[address] = node
184
+ end
185
+
186
+ private def each_path(&)
187
+ yield @direct
188
+ @links.each_value(&)
189
+ end
190
+
191
+ # When something happens next, after everything due now has happened.
192
+ private def next_event
193
+ times = @addresses.each_key.filter_map(&:next_timeout)
194
+ each_path { |path| (time = path.next_timeout) && times << time }
195
+ time = times.min
196
+ raise Stuck, "#{time} is still due at #{now}" if time && time <= now
197
+ time
198
+ end
199
+
200
+ private def start(model)
201
+ random = Random.new(@random.rand(1 << 64))
202
+ case model
203
+ when Array then Chain.new(model.map { |stage| stage.start(random: Random.new(random.rand(1 << 64))) })
204
+ else model.start(random:)
205
+ end
206
+ end
207
+
208
+ # Datagrams without a link: delivered at once.
209
+ class Direct
210
+ def initialize
211
+ @queue = [] #: Array[Datagram]
212
+ end
213
+
214
+ def submit(datagram, now:) = @queue << datagram
215
+
216
+ def each_delivery(now:)
217
+ queue = @queue
218
+ @queue = []
219
+ queue.each { |datagram| yield datagram }
220
+ end
221
+
222
+ # Nothing waits: what is submitted is delivered in the same instant.
223
+ def next_timeout = nil
224
+ end
225
+
226
+ # Models one after another: what leaves one enters the next at once.
227
+ class Chain
228
+ def initialize(stages)
229
+ @stages = stages
230
+ end
231
+
232
+ def submit(datagram, now:) = @stages.fetch(0).submit(datagram, now:)
233
+
234
+ def each_delivery(now:, &)
235
+ @stages.each_cons(2) do |stage, following|
236
+ stage.each_delivery(now:) { |datagram| following.submit(datagram, now:) } # steep:ignore
237
+ end
238
+ @stages.fetch(-1).each_delivery(now:, &)
239
+ end
240
+
241
+ def next_timeout = @stages.filter_map(&:next_timeout).min
242
+ end
243
+ end
244
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC::Test
4
+ # A path model that drops each datagram with probability `rate` and lets
5
+ # the rest through at once.
6
+ class RandomLoss < Data.define(:rate)
7
+ def initialize(rate:)
8
+ raise ArgumentError, "rate is 0.0..1.0, not #{rate}" unless (0.0..1.0).cover?(rate)
9
+ super
10
+ end
11
+
12
+ def start(random:) = State.new(rate, random)
13
+
14
+ class State
15
+ def initialize(rate, random)
16
+ @rate = rate
17
+ @random = random
18
+ @queue = [] #: Array[Datagram]
19
+ end
20
+
21
+ def submit(datagram, now:)
22
+ @queue << datagram unless @random.rand < @rate
23
+ end
24
+
25
+ def each_delivery(now:)
26
+ queue = @queue
27
+ @queue = []
28
+ queue.each { |datagram| yield datagram }
29
+ end
30
+
31
+ def next_timeout = nil
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC::Test
4
+ # A path model that holds back a datagram with probability `rate` until
5
+ # the next one has gone past it, so that the two arrive the other way
6
+ # round. One with nothing behind it goes after `hold` seconds. The rest go
7
+ # through at once.
8
+ class Reorder < Data.define(:rate, :hold)
9
+ def initialize(rate:, hold: 0.01)
10
+ raise ArgumentError, "rate is 0.0..1.0, not #{rate}" unless (0.0..1.0).cover?(rate)
11
+ raise ArgumentError, "hold must be positive, not #{hold}" unless hold.positive?
12
+ super
13
+ end
14
+
15
+ def start(random:) = State.new(rate, hold, random)
16
+
17
+ class State
18
+ def initialize(rate, hold, random)
19
+ @rate = rate
20
+ @hold = hold
21
+ @random = random
22
+ @queue = [] #: Array[Datagram]
23
+ # The datagram held back and when it goes regardless.
24
+ @held = nil #: [Datagram, Float]?
25
+ end
26
+
27
+ def submit(datagram, now:)
28
+ if (held = @held)
29
+ @queue << datagram << held.first
30
+ @held = nil
31
+ elsif @random.rand < @rate
32
+ @held = [datagram, now + @hold]
33
+ else
34
+ @queue << datagram
35
+ end
36
+ end
37
+
38
+ def each_delivery(now:)
39
+ if (held = @held) && held.last <= now
40
+ @queue << held.first
41
+ @held = nil
42
+ end
43
+ queue = @queue
44
+ @queue = []
45
+ queue.each { |datagram| yield datagram }
46
+ end
47
+
48
+ def next_timeout = @held&.last
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC::Test
4
+ # A server's Endpoint on a Network, run as an application that takes
5
+ # every offer would run it: each offer is admitted and each ClientHello
6
+ # accepted with one TLS Config and Keyring, and what the Endpoint answers
7
+ # itself is sent. With `retry_unvalidated`, an offer whose address is not
8
+ # validated gets a Retry instead. A Config that takes 0-RTT needs
9
+ # `anti_replay`, given to each connection.
10
+ class Server
11
+ attr_reader :endpoint
12
+
13
+ def initialize(endpoint, tls_config:, keyring:, retry_unvalidated: false, tracer: nil, anti_replay: nil)
14
+ @endpoint = endpoint
15
+ @tls_config = tls_config
16
+ @keyring = keyring
17
+ @retry_unvalidated = retry_unvalidated
18
+ @tracer = tracer
19
+ @anti_replay = anti_replay
20
+ @transmits = [] #: Array[Raiha::QUIC::Transmit]
21
+ end
22
+
23
+ def receive(payload, from:, to:, ecn:, now:) = dispatch(@endpoint.receive(payload, from:, to:, ecn:, now:), now)
24
+
25
+ def each_transmit(now:, &)
26
+ while (transmit = @transmits.shift)
27
+ yield transmit
28
+ end
29
+ @endpoint.each_transmit(now:, &)
30
+ end
31
+
32
+ def next_timeout = @endpoint.next_timeout
33
+
34
+ def handle_timeout(now:) = @endpoint.handle_timeout(now:)
35
+
36
+ # The connections the Endpoint has taken.
37
+ def connections
38
+ all = [] #: Array[Raiha::QUIC::Connection]
39
+ @endpoint.each_connection { |connection| all << connection }
40
+ all
41
+ end
42
+
43
+ private def dispatch(route, now)
44
+ case route
45
+ when Raiha::QUIC::Route::Incoming
46
+ return @endpoint.retry(route.handle, now:) if @retry_unvalidated && !route.address_validated
47
+ dispatch(@endpoint.admit(route.handle, now:), now)
48
+ when Raiha::QUIC::Route::ClientHello
49
+ @endpoint.accept(route.handle, tls_config: @tls_config, keyring: @keyring, now:, tracer: @tracer, anti_replay: @anti_replay)
50
+ when Raiha::QUIC::Route::Respond then @transmits << route.transmit
51
+ end
52
+ end
53
+ end
54
+ end