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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -0
- data/LICENSE +202 -0
- data/README.md +94 -22
- data/docs/configuration.md +227 -0
- data/docs/development.md +99 -0
- data/docs/fiber-scheduler.md +591 -0
- data/docs/getting-started.md +190 -0
- data/docs/http3-client.md +303 -0
- data/docs/http3-server.md +301 -0
- data/docs/platforms.md +98 -0
- data/docs/qlog.md +86 -0
- data/docs/quic.md +268 -0
- data/docs/threads.md +127 -0
- data/examples/_certificate.rb +55 -0
- data/examples/high_level_loopback.rb +59 -0
- data/examples/http3_get_public.rb +68 -0
- data/examples/in_memory_handshake.rb +38 -0
- data/examples/qlog_output.rb +58 -0
- data/examples/qlog_roundtrips.rb +66 -0
- data/examples/quic_gem_loopback.rb +62 -0
- data/examples/stream_echo.rb +68 -0
- data/examples/udp_handshake.rb +76 -0
- data/lib/raiha/http3/action.rb +21 -0
- data/lib/raiha/http3/capsule.rb +86 -0
- data/lib/raiha/http3/connection.rb +983 -0
- data/lib/raiha/http3/error.rb +24 -0
- data/lib/raiha/http3/error_code.rb +27 -0
- data/lib/raiha/http3/event.rb +63 -0
- data/lib/raiha/http3/frame.rb +148 -0
- data/lib/raiha/http3/frame_reader.rb +114 -0
- data/lib/raiha/http3/message.rb +137 -0
- data/lib/raiha/http3/output.rb +61 -0
- data/lib/raiha/http3/priority.rb +52 -0
- data/lib/raiha/http3/qlog.rb +145 -0
- data/lib/raiha/http3/qpack/decoder.rb +302 -0
- data/lib/raiha/http3/qpack/dynamic_table.rb +83 -0
- data/lib/raiha/http3/qpack/encoder.rb +94 -0
- data/lib/raiha/http3/qpack/error.rb +39 -0
- data/lib/raiha/http3/qpack/error_code.rb +13 -0
- data/lib/raiha/http3/qpack/field.rb +15 -0
- data/lib/raiha/http3/qpack/huffman.rb +397 -0
- data/lib/raiha/http3/qpack/reader.rb +82 -0
- data/lib/raiha/http3/qpack/result.rb +26 -0
- data/lib/raiha/http3/qpack/static_table.rb +139 -0
- data/lib/raiha/http3/qpack/writer.rb +44 -0
- data/lib/raiha/http3/qpack.rb +26 -0
- data/lib/raiha/http3/receiving.rb +46 -0
- data/lib/raiha/http3/sending.rb +23 -0
- data/lib/raiha/http3/session.rb +31 -0
- data/lib/raiha/http3/settings.rb +81 -0
- data/lib/raiha/http3/structured_field.rb +120 -0
- data/lib/raiha/http3/tracer.rb +26 -0
- data/lib/raiha/http3/varint.rb +45 -0
- data/lib/raiha/http3.rb +19 -0
- data/lib/raiha/http3_binding.rb +139 -0
- data/lib/raiha/namespace.rb +11 -0
- data/lib/raiha/quic/config.rb +229 -0
- data/lib/raiha/quic/congestion/algorithms.rb +17 -0
- data/lib/raiha/quic/congestion/cubic.rb +258 -0
- data/lib/raiha/quic/congestion/new_reno.rb +161 -0
- data/lib/raiha/quic/congestion/pacing.rb +21 -0
- data/lib/raiha/quic/congestion/startup/rapid_start.rb +249 -0
- data/lib/raiha/quic/congestion/startup.rb +13 -0
- data/lib/raiha/quic/congestion.rb +15 -0
- data/lib/raiha/quic/connection.rb +2022 -0
- data/lib/raiha/quic/connection_ids.rb +62 -0
- data/lib/raiha/quic/crypto/aead.rb +68 -0
- data/lib/raiha/quic/crypto/header_protection.rb +42 -0
- data/lib/raiha/quic/crypto/initial.rb +23 -0
- data/lib/raiha/quic/crypto/keys.rb +32 -0
- data/lib/raiha/quic/crypto/mac.rb +15 -0
- data/lib/raiha/quic/crypto/random.rb +13 -0
- data/lib/raiha/quic/crypto.rb +18 -0
- data/lib/raiha/quic/crypto_stream.rb +161 -0
- data/lib/raiha/quic/datagrams.rb +91 -0
- data/lib/raiha/quic/endpoint.rb +785 -0
- data/lib/raiha/quic/error.rb +61 -0
- data/lib/raiha/quic/error_code.rb +34 -0
- data/lib/raiha/quic/event.rb +59 -0
- data/lib/raiha/quic/issued_connection_ids.rb +137 -0
- data/lib/raiha/quic/kept_packets.rb +41 -0
- data/lib/raiha/quic/mtu_search.rb +75 -0
- data/lib/raiha/quic/negotiation.rb +143 -0
- data/lib/raiha/quic/new_token.rb +55 -0
- data/lib/raiha/quic/pacer.rb +52 -0
- data/lib/raiha/quic/packet_protection.rb +310 -0
- data/lib/raiha/quic/path.rb +473 -0
- data/lib/raiha/quic/peer_connection_ids.rb +160 -0
- data/lib/raiha/quic/qlog.rb +260 -0
- data/lib/raiha/quic/ranges.rb +32 -0
- data/lib/raiha/quic/recovery/duty.rb +102 -0
- data/lib/raiha/quic/recovery/packet_number_space.rb +420 -0
- data/lib/raiha/quic/recovery/rate_meter.rb +95 -0
- data/lib/raiha/quic/recovery/rtt_estimator.rb +57 -0
- data/lib/raiha/quic/recovery/sent_packet.rb +16 -0
- data/lib/raiha/quic/recovery.rb +641 -0
- data/lib/raiha/quic/remembered_transport_parameters.rb +29 -0
- data/lib/raiha/quic/retry_token.rb +54 -0
- data/lib/raiha/quic/route.rb +37 -0
- data/lib/raiha/quic/scheduler.rb +112 -0
- data/lib/raiha/quic/session.rb +47 -0
- data/lib/raiha/quic/stateless_reset.rb +38 -0
- data/lib/raiha/quic/stats.rb +24 -0
- data/lib/raiha/quic/streams/receive_window.rb +119 -0
- data/lib/raiha/quic/streams/receiver.rb +217 -0
- data/lib/raiha/quic/streams/sender.rb +200 -0
- data/lib/raiha/quic/streams.rb +687 -0
- data/lib/raiha/quic/test/bottleneck.rb +55 -0
- data/lib/raiha/quic/test/congestion_contract.rb +106 -0
- data/lib/raiha/quic/test/network.rb +244 -0
- data/lib/raiha/quic/test/random_loss.rb +34 -0
- data/lib/raiha/quic/test/reorder.rb +51 -0
- data/lib/raiha/quic/test/server.rb +54 -0
- data/lib/raiha/quic/test/vectors.rb +337 -0
- data/lib/raiha/quic/test/virtual_clock.rb +21 -0
- data/lib/raiha/quic/test.rb +17 -0
- data/lib/raiha/quic/tls_binding.rb +179 -0
- data/lib/raiha/quic/token.rb +28 -0
- data/lib/raiha/quic/tracer.rb +127 -0
- data/lib/raiha/quic/transmit.rb +14 -0
- data/lib/raiha/quic/transport_parameters.rb +244 -0
- data/lib/raiha/quic/version.rb +63 -0
- data/lib/raiha/quic/wire/codec.rb +136 -0
- data/lib/raiha/quic/wire/frame.rb +521 -0
- data/lib/raiha/quic/wire/header.rb +90 -0
- data/lib/raiha/quic/wire/packet.rb +130 -0
- data/lib/raiha/quic/wire/packet_number.rb +47 -0
- data/lib/raiha/quic/wire/retry.rb +62 -0
- data/lib/raiha/quic/wire/version_negotiation.rb +53 -0
- data/lib/raiha/quic/wire.rb +9 -0
- data/lib/raiha/quic.rb +46 -0
- data/lib/raiha/runtime/addresses.rb +35 -0
- data/lib/raiha/runtime/admission.rb +37 -0
- data/lib/raiha/runtime/client.rb +190 -0
- data/lib/raiha/runtime/driver.rb +165 -0
- data/lib/raiha/runtime/error.rb +16 -0
- data/lib/raiha/runtime/http3.rb +363 -0
- data/lib/raiha/runtime/http3_fields.rb +38 -0
- data/lib/raiha/runtime/http3_server.rb +416 -0
- data/lib/raiha/runtime/kept.rb +36 -0
- data/lib/raiha/runtime/qlog.rb +204 -0
- data/lib/raiha/runtime/rack.rb +180 -0
- data/lib/raiha/runtime/server.rb +334 -0
- data/lib/raiha/runtime/session.rb +447 -0
- data/lib/raiha/runtime/threaded/client.rb +161 -0
- data/lib/raiha/runtime/threaded/driver.rb +230 -0
- data/lib/raiha/runtime/threaded/http3.rb +287 -0
- data/lib/raiha/runtime/threaded/http3_server.rb +432 -0
- data/lib/raiha/runtime/threaded/server.rb +290 -0
- data/lib/raiha/runtime/threaded/session.rb +604 -0
- data/lib/raiha/runtime/threaded.rb +45 -0
- data/lib/raiha/runtime/udp.rb +410 -0
- data/lib/raiha/runtime/worker.rb +138 -0
- data/lib/raiha/runtime/workers.rb +248 -0
- data/lib/raiha/runtime.rb +37 -0
- data/lib/raiha/tls/anti_replay.rb +139 -0
- data/lib/raiha/tls/cipher_suite.rb +62 -0
- data/lib/raiha/tls/codec/error.rb +43 -0
- data/lib/raiha/tls/codec/reader.rb +95 -0
- data/lib/raiha/tls/codec/writer.rb +81 -0
- data/lib/raiha/tls/codec.rb +5 -0
- data/lib/raiha/tls/config.rb +318 -0
- data/lib/raiha/tls/crypto/aead.rb +64 -0
- data/lib/raiha/tls/crypto/certificate.rb +28 -0
- data/lib/raiha/tls/crypto/digest.rb +30 -0
- data/lib/raiha/tls/crypto/ecdh.rb +74 -0
- data/lib/raiha/tls/crypto/hash.rb +22 -0
- data/lib/raiha/tls/crypto/hkdf.rb +28 -0
- data/lib/raiha/tls/crypto/signature.rb +87 -0
- data/lib/raiha/tls/crypto.rb +19 -0
- data/lib/raiha/tls/error.rb +36 -0
- data/lib/raiha/tls/event.rb +45 -0
- data/lib/raiha/tls/extension/alpn.rb +35 -0
- data/lib/raiha/tls/extension/certificate_authorities.rb +35 -0
- data/lib/raiha/tls/extension/compress_certificate.rb +33 -0
- data/lib/raiha/tls/extension/cookie.rb +27 -0
- data/lib/raiha/tls/extension/early_data.rb +54 -0
- data/lib/raiha/tls/extension/key_share.rb +90 -0
- data/lib/raiha/tls/extension/list.rb +49 -0
- data/lib/raiha/tls/extension/opaque.rb +21 -0
- data/lib/raiha/tls/extension/placement.rb +55 -0
- data/lib/raiha/tls/extension/post_handshake_auth.rb +22 -0
- data/lib/raiha/tls/extension/pre_shared_key.rb +81 -0
- data/lib/raiha/tls/extension/psk_key_exchange_modes.rb +32 -0
- data/lib/raiha/tls/extension/record_size_limit.rb +30 -0
- data/lib/raiha/tls/extension/registry.rb +50 -0
- data/lib/raiha/tls/extension/server_name.rb +63 -0
- data/lib/raiha/tls/extension/signature_algorithms.rb +39 -0
- data/lib/raiha/tls/extension/signature_algorithms_cert.rb +21 -0
- data/lib/raiha/tls/extension/supported_groups.rb +32 -0
- data/lib/raiha/tls/extension/supported_versions.rb +53 -0
- data/lib/raiha/tls/extension.rb +32 -0
- data/lib/raiha/tls/handshake/acceptor.rb +164 -0
- data/lib/raiha/tls/handshake/authentication_messages.rb +127 -0
- data/lib/raiha/tls/handshake/certificate.rb +56 -0
- data/lib/raiha/tls/handshake/certificate_request.rb +39 -0
- data/lib/raiha/tls/handshake/certificate_verify.rb +40 -0
- data/lib/raiha/tls/handshake/client_hello.rb +208 -0
- data/lib/raiha/tls/handshake/client_hello_builder.rb +144 -0
- data/lib/raiha/tls/handshake/client_side.rb +578 -0
- data/lib/raiha/tls/handshake/conductor.rb +390 -0
- data/lib/raiha/tls/handshake/encrypted_extensions.rb +33 -0
- data/lib/raiha/tls/handshake/end_of_early_data.rb +28 -0
- data/lib/raiha/tls/handshake/finished.rb +36 -0
- data/lib/raiha/tls/handshake/key_update.rb +38 -0
- data/lib/raiha/tls/handshake/key_update_handling.rb +88 -0
- data/lib/raiha/tls/handshake/machine.rb +238 -0
- data/lib/raiha/tls/handshake/message.rb +25 -0
- data/lib/raiha/tls/handshake/new_session_ticket.rb +48 -0
- data/lib/raiha/tls/handshake/reassembler.rb +88 -0
- data/lib/raiha/tls/handshake/server_hello.rb +98 -0
- data/lib/raiha/tls/handshake/server_side.rb +672 -0
- data/lib/raiha/tls/handshake/violation.rb +12 -0
- data/lib/raiha/tls/handshake.rb +62 -0
- data/lib/raiha/tls/iana.rb +113 -0
- data/lib/raiha/tls/key_exchange.rb +58 -0
- data/lib/raiha/tls/key_schedule.rb +269 -0
- data/lib/raiha/tls/keylog.rb +40 -0
- data/lib/raiha/tls/keyring.rb +180 -0
- data/lib/raiha/tls/masked_inspect.rb +33 -0
- data/lib/raiha/tls/output.rb +32 -0
- data/lib/raiha/tls/profile.rb +82 -0
- data/lib/raiha/tls/record/layer.rb +228 -0
- data/lib/raiha/tls/record/protection.rb +67 -0
- data/lib/raiha/tls/record.rb +11 -0
- data/lib/raiha/tls/server_name.rb +42 -0
- data/lib/raiha/tls/session.rb +106 -0
- data/lib/raiha/tls/signature_scheme.rb +35 -0
- data/lib/raiha/tls/tcp/acceptor.rb +121 -0
- data/lib/raiha/tls/tcp/connection.rb +371 -0
- data/lib/raiha/tls/tcp/event.rb +15 -0
- data/lib/raiha/tls/tcp/socket.rb +161 -0
- data/lib/raiha/tls/tcp.rb +14 -0
- data/lib/raiha/tls/test/fixed_kem.rb +30 -0
- data/lib/raiha/tls/test/fixed_random.rb +18 -0
- data/lib/raiha/tls/test/rfc8448.rb +923 -0
- data/lib/raiha/tls/test/rfc8448_handshakes.rb +118 -0
- data/lib/raiha/tls/test.rb +13 -0
- data/lib/raiha/tls/ticket.rb +184 -0
- data/lib/raiha/tls/ticket_keys.rb +206 -0
- data/lib/raiha/tls/trace.rb +58 -0
- data/lib/raiha/tls/transcript.rb +94 -0
- data/lib/raiha/tls/verifier/default.rb +72 -0
- data/lib/raiha/tls/verifier/host_name.rb +75 -0
- data/lib/raiha/tls/verifier/none.rb +14 -0
- data/lib/raiha/tls/verifier.rb +24 -0
- data/lib/raiha/tls.rb +34 -0
- data/lib/raiha/version.rb +3 -1
- data/lib/raiha.rb +8 -5
- data/sig/patches/array.rbs +9 -0
- data/sig/patches/io_buffer.rbs +13 -0
- data/sig/patches/openssl.rbs +25 -0
- data/sig/patches/socket.rbs +29 -0
- data/sig/patches/string.rbs +26 -0
- data/sig/patches/strscan.rbs +12 -0
- data/sig/raiha/http3/capsule.rbs +32 -0
- data/sig/raiha/http3/connection.rbs +324 -0
- data/sig/raiha/http3/frame.rbs +138 -0
- data/sig/raiha/http3/frame_reader.rbs +39 -0
- data/sig/raiha/http3/message.rbs +23 -0
- data/sig/raiha/http3/priority.rbs +40 -0
- data/sig/raiha/http3/qlog.rbs +54 -0
- data/sig/raiha/http3/qpack.rbs +246 -0
- data/sig/raiha/http3/session.rbs +11 -0
- data/sig/raiha/http3/settings.rbs +26 -0
- data/sig/raiha/http3.rbs +2 -0
- data/sig/raiha/http3_binding.rbs +26 -0
- data/sig/raiha/namespace.rbs +2 -0
- data/sig/raiha/quic/config.rbs +89 -0
- data/sig/raiha/quic/congestion.rbs +213 -0
- data/sig/raiha/quic/connection.rbs +249 -0
- data/sig/raiha/quic/connection_ids.rbs +34 -0
- data/sig/raiha/quic/crypto.rbs +78 -0
- data/sig/raiha/quic/crypto_stream.rbs +34 -0
- data/sig/raiha/quic/datagrams.rbs +32 -0
- data/sig/raiha/quic/endpoint.rbs +179 -0
- data/sig/raiha/quic/frame.rbs +387 -0
- data/sig/raiha/quic/issued_connection_ids.rbs +47 -0
- data/sig/raiha/quic/kept_packets.rbs +25 -0
- data/sig/raiha/quic/mtu_search.rbs +21 -0
- data/sig/raiha/quic/negotiation.rbs +33 -0
- data/sig/raiha/quic/new_token.rbs +14 -0
- data/sig/raiha/quic/pacer.rbs +16 -0
- data/sig/raiha/quic/packet_protection.rbs +63 -0
- data/sig/raiha/quic/path.rbs +106 -0
- data/sig/raiha/quic/peer_connection_ids.rbs +42 -0
- data/sig/raiha/quic/qlog.rbs +74 -0
- data/sig/raiha/quic/ranges.rbs +7 -0
- data/sig/raiha/quic/recovery.rbs +376 -0
- data/sig/raiha/quic/retry_token.rbs +21 -0
- data/sig/raiha/quic/route.rbs +51 -0
- data/sig/raiha/quic/scheduler.rbs +36 -0
- data/sig/raiha/quic/session.rbs +17 -0
- data/sig/raiha/quic/stateless_reset.rbs +10 -0
- data/sig/raiha/quic/streams.rbs +244 -0
- data/sig/raiha/quic/test.rbs +354 -0
- data/sig/raiha/quic/tls_binding.rbs +58 -0
- data/sig/raiha/quic/token.rbs +11 -0
- data/sig/raiha/quic/tracer.rbs +71 -0
- data/sig/raiha/quic/transport_parameters.rbs +113 -0
- data/sig/raiha/quic/values.rbs +200 -0
- data/sig/raiha/quic/version.rbs +30 -0
- data/sig/raiha/quic/wire.rbs +199 -0
- data/sig/raiha/quic.rbs +2 -0
- data/sig/raiha/runtime/threaded.rbs +616 -0
- data/sig/raiha/runtime.rbs +982 -0
- data/sig/raiha/tls/anti_replay.rbs +41 -0
- data/sig/raiha/tls/cipher_suite.rbs +41 -0
- data/sig/raiha/tls/codec.rbs +66 -0
- data/sig/raiha/tls/config.rbs +122 -0
- data/sig/raiha/tls/crypto.rbs +86 -0
- data/sig/raiha/tls/error.rbs +21 -0
- data/sig/raiha/tls/event.rbs +61 -0
- data/sig/raiha/tls/extension.rbs +331 -0
- data/sig/raiha/tls/handshake.rbs +311 -0
- data/sig/raiha/tls/iana.rbs +13 -0
- data/sig/raiha/tls/key_exchange.rbs +33 -0
- data/sig/raiha/tls/key_schedule.rbs +58 -0
- data/sig/raiha/tls/keylog.rbs +20 -0
- data/sig/raiha/tls/keyring.rbs +64 -0
- data/sig/raiha/tls/machine.rbs +429 -0
- data/sig/raiha/tls/masked_inspect.rbs +12 -0
- data/sig/raiha/tls/output.rbs +37 -0
- data/sig/raiha/tls/profile.rbs +26 -0
- data/sig/raiha/tls/record.rbs +79 -0
- data/sig/raiha/tls/server_name.rbs +10 -0
- data/sig/raiha/tls/session.rbs +48 -0
- data/sig/raiha/tls/signature_scheme.rbs +20 -0
- data/sig/raiha/tls/tcp.rbs +134 -0
- data/sig/raiha/tls/test.rbs +143 -0
- data/sig/raiha/tls/ticket.rbs +68 -0
- data/sig/raiha/tls/ticket_keys.rbs +67 -0
- data/sig/raiha/tls/trace.rbs +99 -0
- data/sig/raiha/tls/transcript.rbs +22 -0
- data/sig/raiha/tls/verifier.rbs +54 -0
- data/sig/raiha/tls.rbs +5 -0
- data/sig/raiha.rbs +3 -0
- metadata +355 -57
- data/.gitignore +0 -10
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile +0 -4
- data/Gemfile.lock +0 -22
- data/Rakefile +0 -10
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/raiha.gemspec +0 -40
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Raiha::QUIC
|
|
6
|
+
# qlog (draft-ietf-quic-qlog-main-schema-13 and quic-events-12).
|
|
7
|
+
module Qlog
|
|
8
|
+
# The bytes the traces given it may write together. What they write is
|
|
9
|
+
# spent and never comes back: once it is gone, they all stop, and a
|
|
10
|
+
# trace begun after writes nothing. A Ractor does not share it, so
|
|
11
|
+
# each worker has its own.
|
|
12
|
+
class Budget
|
|
13
|
+
attr_reader :max_bytes, :spent
|
|
14
|
+
|
|
15
|
+
def initialize(max_bytes:)
|
|
16
|
+
raise ArgumentError, "max_bytes is a positive Integer, not #{max_bytes.inspect}" unless max_bytes.is_a?(Integer) && max_bytes.positive?
|
|
17
|
+
@max_bytes = max_bytes
|
|
18
|
+
@spent = 0
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def left = @max_bytes - @spent
|
|
22
|
+
|
|
23
|
+
# A trace wrote `bytes`, which it has made sure are left.
|
|
24
|
+
def spend(bytes)
|
|
25
|
+
@spent += bytes
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# A Tracer that writes the qlog of one connection as a JSON Text
|
|
30
|
+
# Sequence (RFC 7464) to `io`, a record as each event happens, so that
|
|
31
|
+
# the trace of a connection that dies keeps what came before it. The
|
|
32
|
+
# header also has the qlog_version and qlog_format of the earlier
|
|
33
|
+
# drafts, which qvis looks for.
|
|
34
|
+
#
|
|
35
|
+
# Times are milliseconds of the monotonic clock the connection is given;
|
|
36
|
+
# a trace of the TLS layer, which comes with no time, has the time of
|
|
37
|
+
# the event before it, which the same input caused.
|
|
38
|
+
# Connection IDs and other bytes are hex; the bytes of data, only their
|
|
39
|
+
# length, but the data of a path challenge and response. Events qlog has no name for go under "raiha:": a spurious loss,
|
|
40
|
+
# and the TLS layer's traces as "raiha:tls_" and the name of the trace.
|
|
41
|
+
# A layer above QUIC writes its own events with `record`, at the time of
|
|
42
|
+
# the event before them, and names its schema in `event_schemas`.
|
|
43
|
+
#
|
|
44
|
+
# The trace of one connection writes no more than `max_bytes`, however
|
|
45
|
+
# long the peer keeps it busy, and the traces given one `budget` no
|
|
46
|
+
# more than it together, however many connections a peer opens: the
|
|
47
|
+
# record that would go past either, less the room for one more, goes
|
|
48
|
+
# instead as "raiha:qlog_stopped" with the limit, and nothing comes
|
|
49
|
+
# after it; a trace whose header does not fit writes nothing. Keeping
|
|
50
|
+
# the files apart, and taking the old ones away, is up to whoever
|
|
51
|
+
# gives each trace its `io`.
|
|
52
|
+
class Tracer
|
|
53
|
+
include Raiha::QUIC::Tracer
|
|
54
|
+
|
|
55
|
+
RECORD_SEPARATOR = "\x1e"
|
|
56
|
+
PACKET_TYPES = { initial: "initial", zero_rtt: "0RTT", handshake: "handshake", one_rtt: "1RTT" }.freeze
|
|
57
|
+
SPACE_PACKET_TYPES = { initial: "initial", handshake: "handshake", application_data: "1RTT" }.freeze
|
|
58
|
+
# The metrics and parameters that are times, in seconds here and
|
|
59
|
+
# milliseconds in qlog.
|
|
60
|
+
TIMES = %i[latest_rtt smoothed_rtt min_rtt rtt_variance max_idle_timeout max_ack_delay timer_granularity initial_rtt].freeze
|
|
61
|
+
DEFAULT_MAX_BYTES = 64 * 1024 * 1024
|
|
62
|
+
# The room kept for the record that says the trace stopped.
|
|
63
|
+
STOP_ROOM = 256
|
|
64
|
+
|
|
65
|
+
# `vantage_point` is :client or :server.
|
|
66
|
+
def initialize(io, vantage_point:, title: nil, max_bytes: DEFAULT_MAX_BYTES, budget: nil, event_schemas: [])
|
|
67
|
+
raise ArgumentError, "max_bytes is a positive Integer, not #{max_bytes.inspect}" unless max_bytes.is_a?(Integer) && max_bytes.positive?
|
|
68
|
+
@io = io
|
|
69
|
+
@max_bytes = max_bytes
|
|
70
|
+
@budget = budget
|
|
71
|
+
@written = 0
|
|
72
|
+
@stopped = false
|
|
73
|
+
@now = 0.0
|
|
74
|
+
header = {
|
|
75
|
+
file_schema: "urn:ietf:params:qlog:file:sequential", serialization_format: "application/qlog+json-seq",
|
|
76
|
+
qlog_version: "0.3", qlog_format: "JSON-SEQ", title:,
|
|
77
|
+
trace: {
|
|
78
|
+
vantage_point: { type: vantage_point.to_s },
|
|
79
|
+
common_fields: { time_format: "relative_to_epoch", reference_time: { clock_type: "monotonic", epoch: "unknown" } },
|
|
80
|
+
event_schemas: ["urn:ietf:params:qlog:events:quic", *event_schemas]
|
|
81
|
+
}
|
|
82
|
+
}.compact #: Hash[Symbol, json]
|
|
83
|
+
write(header)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Whether the connection lists the frames of each packet: not once the
|
|
87
|
+
# trace has stopped.
|
|
88
|
+
def trace_frames? = !@stopped
|
|
89
|
+
|
|
90
|
+
def packet_sent(header:, frames:, raw_length:, now:) = packet("quic:packet_sent", header, frames, raw_length, now)
|
|
91
|
+
|
|
92
|
+
def packet_received(header:, frames:, raw_length:, now:) = packet("quic:packet_received", header, frames, raw_length, now)
|
|
93
|
+
|
|
94
|
+
def packet_dropped(reason:, raw_length:, now:) = event(now, "quic:packet_dropped", { raw: { length: raw_length }, trigger: reason.to_s })
|
|
95
|
+
|
|
96
|
+
def packet_lost(packet_number:, space:, trigger:, mtu_probe:, now:)
|
|
97
|
+
header = { packet_type: SPACE_PACKET_TYPES.fetch(space), packet_number: } #: Hash[Symbol, json]
|
|
98
|
+
event(now, "quic:packet_lost", { header:, trigger: trigger.to_s, is_mtu_probe_packet: mtu_probe })
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def metrics_updated(now:, **metrics) = event(now, "quic:recovery_metrics_updated", values(metrics))
|
|
102
|
+
|
|
103
|
+
def timer_updated(timer_type:, event_type:, space:, delta:, now:)
|
|
104
|
+
data = { timer_type: timer_type.to_s, packet_number_space: space&.to_s, event_type: event_type.to_s, delta: delta && delta * 1000 }
|
|
105
|
+
event(now, "quic:timer_updated", data.compact)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def ecn_state_updated(old:, new:, now:) = event(now, "quic:ecn_state_updated", { old: old.to_s, new: new.to_s })
|
|
109
|
+
|
|
110
|
+
def recovery_parameters_set(now:, **parameters) = event(now, "quic:recovery_parameters_set", values(parameters))
|
|
111
|
+
|
|
112
|
+
def spurious_loss(packet_number:, space:, now:)
|
|
113
|
+
event(now, "raiha:spurious_loss", { packet_number_space: space.to_s, packet_number: })
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def congestion_state_updated(old:, new:, now:) = event(now, "quic:congestion_state_updated", { old: old.to_s, new: new.to_s })
|
|
117
|
+
|
|
118
|
+
def connection_data_blocked_updated(old:, new:, reason:, now:)
|
|
119
|
+
event(now, "quic:connection_data_blocked_updated", { old: old.to_s, new: new.to_s, reason: reason.to_s })
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def stream_data_blocked_updated(stream_id:, old:, new:, reason:, now:)
|
|
123
|
+
event(now, "quic:stream_data_blocked_updated", { old: old.to_s, new: new.to_s, stream_id:, reason: reason.to_s })
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def mtu_updated(old:, new:, done:, now:) = event(now, "quic:mtu_updated", { old:, new:, done: })
|
|
127
|
+
|
|
128
|
+
def connection_id_updated(owner:, old:, new:, now:)
|
|
129
|
+
event(now, "quic:connection_id_updated", { owner: owner.to_s, old: hex(old), new: hex(new) })
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def migration_state_updated(state:, local:, remote:, now:)
|
|
133
|
+
data = { new: state.to_s, path_local: endpoint(local), path_remote: endpoint(remote) } #: Hash[Symbol, json]
|
|
134
|
+
event(now, "quic:migration_state_updated", data.compact)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Versions are hex, as in a packet header; version_information too.
|
|
138
|
+
def parameters_set(owner:, parameters:, now:)
|
|
139
|
+
fields = parameters.to_h.compact
|
|
140
|
+
information = fields.delete(:version_information)
|
|
141
|
+
data = { owner: owner.to_s, **values(fields) } #: Hash[Symbol, json]
|
|
142
|
+
if information.is_a?(TransportParameters::VersionInformation)
|
|
143
|
+
data[:version_information] = { chosen_version: version(information.chosen), available_versions: information.available.map { version(_1) } }
|
|
144
|
+
end
|
|
145
|
+
event(now, "quic:parameters_set", data)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def version_information(now:, client_versions: nil, server_versions: nil, chosen_version: nil)
|
|
149
|
+
data = {
|
|
150
|
+
client_versions: client_versions&.map { version(_1) }, server_versions: server_versions&.map { version(_1) },
|
|
151
|
+
chosen_version: chosen_version&.then { version(_1) }
|
|
152
|
+
} #: Hash[Symbol, json]
|
|
153
|
+
event(now, "quic:version_information", data.compact)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# A key update changes the 1-RTT keys of both directions; no key goes
|
|
157
|
+
# in the trace.
|
|
158
|
+
def key_updated(level:, now:)
|
|
159
|
+
%w[client_1rtt_secret server_1rtt_secret].each { |key_type| event(now, "quic:key_updated", { key_type: }) }
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# A trace of the TLS layer: a Data, whose name and fields it takes.
|
|
163
|
+
# An event of a layer above QUIC, `name` with `data`, already as qlog
|
|
164
|
+
# has it.
|
|
165
|
+
def record(name, data) = write({ time: @now * 1000, name:, data: })
|
|
166
|
+
|
|
167
|
+
def tls_trace(value)
|
|
168
|
+
name = value.class.name.to_s.split("::").last.to_s.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
|
|
169
|
+
write({ time: @now * 1000, name: "raiha:tls_#{name}", data: values(value.to_h) })
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
private def packet(name, header, frames, raw_length, now)
|
|
173
|
+
fields = {
|
|
174
|
+
packet_type: PACKET_TYPES.fetch(header.packet_type), packet_number: header.packet_number,
|
|
175
|
+
version: header.version&.then { version(_1) }, dcid: hex(header.dcid), scid: header.scid&.then { hex(_1) },
|
|
176
|
+
key_phase: header.key_phase
|
|
177
|
+
} #: Hash[Symbol, json]
|
|
178
|
+
data = { header: fields.compact, frames: frames&.map { |frame| frame(frame) }, raw: { length: raw_length } } #: Hash[Symbol, json]
|
|
179
|
+
event(now, name, data.compact)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# A frame as qlog has it: its type, and its fields, with data as
|
|
183
|
+
# its length but a path challenge's and response's, in hex.
|
|
184
|
+
private def frame(frame)
|
|
185
|
+
case frame
|
|
186
|
+
when Wire::Frame::PathChallenge then { frame_type: "path_challenge", data: hex(frame.data) }
|
|
187
|
+
when Wire::Frame::PathResponse then { frame_type: "path_response", data: hex(frame.data) }
|
|
188
|
+
when Wire::Frame::Ack
|
|
189
|
+
ranges = frame.ranges.reverse.map { |range| [range.begin, range.end] } #: Array[json]
|
|
190
|
+
{ frame_type: "ack", ack_delay: frame.delay * 1000, acked_ranges: ranges }
|
|
191
|
+
when Wire::Frame::ConnectionClose
|
|
192
|
+
{ frame_type: "connection_close", error_space: frame.application ? "application" : "transport", error_code: frame.code,
|
|
193
|
+
trigger_frame_type: frame.frame_type, reason: frame.reason.dup.force_encoding(Encoding::UTF_8).scrub }.compact
|
|
194
|
+
else
|
|
195
|
+
name = frame.class.name.to_s.split("::").last.to_s.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
|
|
196
|
+
{ frame_type: name, **values(frame.to_h) }
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# `fields` as qlog values: times in milliseconds, bytes as hex, data
|
|
201
|
+
# as its length, Symbols as Strings.
|
|
202
|
+
private def version(number) = format("%08x", number)
|
|
203
|
+
|
|
204
|
+
private def values(fields)
|
|
205
|
+
fields.to_h do |name, value|
|
|
206
|
+
case value
|
|
207
|
+
when String then name == :data || name == :token ? [:raw, { length: value.bytesize }] : [name, hex(value)]
|
|
208
|
+
when Float, Integer then [name, TIMES.include?(name) ? value * 1000 : value]
|
|
209
|
+
when Symbol then [name, value.to_s]
|
|
210
|
+
when true, false, nil then [name, value]
|
|
211
|
+
when Data then [name, values(value.to_h)]
|
|
212
|
+
when Array then [name, value.map { |each| each.is_a?(String) ? hex(each) : each.to_s }]
|
|
213
|
+
else [name, value.to_s]
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
private def event(now, name, data)
|
|
219
|
+
@now = now
|
|
220
|
+
write({ time: now * 1000, name:, data: })
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
private def write(record)
|
|
224
|
+
return if @stopped
|
|
225
|
+
json = JSON.generate(record)
|
|
226
|
+
unless room_for?(record_size(json) + STOP_ROOM)
|
|
227
|
+
@stopped = true
|
|
228
|
+
return if @written.zero?
|
|
229
|
+
json = JSON.generate({ time: @now * 1000, name: "raiha:qlog_stopped", data: stop_reason(record_size(json) + STOP_ROOM) })
|
|
230
|
+
return unless room_for?(record_size(json))
|
|
231
|
+
end
|
|
232
|
+
@written += record_size(json)
|
|
233
|
+
@budget&.spend(record_size(json))
|
|
234
|
+
@io.write(RECORD_SEPARATOR, json, "\n")
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
# Which limit `bytes` more would go past, and how large it is.
|
|
238
|
+
private def stop_reason(bytes)
|
|
239
|
+
return { reason: "max_bytes", max_bytes: @max_bytes } if @budget.nil? || @written + bytes > @max_bytes
|
|
240
|
+
{ reason: "budget", max_bytes: @budget.max_bytes } # steep:ignore
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
# Whether this trace and its budget both have `bytes` left.
|
|
244
|
+
private def room_for?(bytes) = @written + bytes <= @max_bytes && (@budget.nil? || bytes <= @budget.left) # steep:ignore
|
|
245
|
+
|
|
246
|
+
# The bytes `json` takes in the sequence, with the separator before it
|
|
247
|
+
# and the line feed after.
|
|
248
|
+
private def record_size(json) = RECORD_SEPARATOR.bytesize + json.bytesize + 1
|
|
249
|
+
|
|
250
|
+
private def hex(bytes) = bytes.unpack1("H*")
|
|
251
|
+
|
|
252
|
+
# An address as qlog's PathEndpointInfo has it, by its family; nil
|
|
253
|
+
# for none.
|
|
254
|
+
private def endpoint(address)
|
|
255
|
+
host, port = address || (return nil)
|
|
256
|
+
host.include?(":") ? { ip_v6: host, port_v6: port } : { ip_v4: host, port_v4: port }
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
end
|
|
260
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::QUIC
|
|
4
|
+
# Sets of byte offsets as ranges that neither overlap nor touch, in
|
|
5
|
+
# order: what of a stream has been acknowledged or must go again.
|
|
6
|
+
module Ranges
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
def empty?(range) = range.begin >= range.end
|
|
10
|
+
|
|
11
|
+
# `ranges` with `range` added, merged, in order.
|
|
12
|
+
def add(ranges, range)
|
|
13
|
+
merged = ranges + [range]
|
|
14
|
+
merged.sort_by!(&:begin)
|
|
15
|
+
merged.each_with_object([]) do |part, result|
|
|
16
|
+
last = result.last
|
|
17
|
+
if last && part.begin <= last.end
|
|
18
|
+
result[-1] = last.begin...[last.end, part.end].max
|
|
19
|
+
else
|
|
20
|
+
result << part
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# `ranges` without `range`.
|
|
26
|
+
def subtract(ranges, range)
|
|
27
|
+
ranges.flat_map do |part|
|
|
28
|
+
[part.begin...[part.end, range.begin].min, [part.begin, range.end].max...part.end].reject { |piece| empty?(piece) }
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::QUIC
|
|
4
|
+
class Recovery
|
|
5
|
+
# What a packet carried that its owner must hear about when the packet
|
|
6
|
+
# is acknowledged or lost: one kind for each frame whose information is
|
|
7
|
+
# sent again when lost, or whose acknowledgment means something. Loss
|
|
8
|
+
# recovery keeps them with the packet and hands them back without
|
|
9
|
+
# looking inside; the information lost is sent again built anew from the
|
|
10
|
+
# latest state, not as a copy of the frame (RFC 9000 "Retransmission of
|
|
11
|
+
# Information"). PING, PADDING, CONNECTION_CLOSE, PATH_CHALLENGE,
|
|
12
|
+
# PATH_RESPONSE and DATAGRAM leave no duty.
|
|
13
|
+
module Duty
|
|
14
|
+
# A range of a stream's data; the data stays in the send buffer.
|
|
15
|
+
class Stream < Data.define(:stream_id, :offset, :length, :fin); end
|
|
16
|
+
|
|
17
|
+
# A range of the crypto stream of a number space (:initial, :handshake
|
|
18
|
+
# or :application_data), the one the packet was sent in.
|
|
19
|
+
class Crypto < Data.define(:space, :offset, :length); end
|
|
20
|
+
|
|
21
|
+
# An ACK frame of a number space and the largest packet number it
|
|
22
|
+
# acknowledged.
|
|
23
|
+
class Ack < Data.define(:space, :largest); end
|
|
24
|
+
|
|
25
|
+
class ResetStream < Data.define(:stream_id); end
|
|
26
|
+
|
|
27
|
+
class StopSending < Data.define(:stream_id); end
|
|
28
|
+
|
|
29
|
+
# The value sent, since only the most recent value is sent again.
|
|
30
|
+
class MaxData < Data.define(:maximum); end
|
|
31
|
+
|
|
32
|
+
class MaxStreamData < Data.define(:stream_id, :maximum); end
|
|
33
|
+
|
|
34
|
+
class MaxStreams < Data.define(:bidirectional, :maximum); end
|
|
35
|
+
|
|
36
|
+
class DataBlocked < Data.define(:maximum); end
|
|
37
|
+
|
|
38
|
+
class StreamDataBlocked < Data.define(:stream_id, :maximum); end
|
|
39
|
+
|
|
40
|
+
class StreamsBlocked < Data.define(:bidirectional, :maximum); end
|
|
41
|
+
|
|
42
|
+
class NewConnectionID < Data.define(:sequence_number); end
|
|
43
|
+
|
|
44
|
+
class RetireConnectionID < Data.define(:sequence_number); end
|
|
45
|
+
|
|
46
|
+
# A NEW_TOKEN frame; the connection keeps its one token.
|
|
47
|
+
class NewToken < Data.define; end
|
|
48
|
+
|
|
49
|
+
class HandshakeDone < Data.define; end
|
|
50
|
+
|
|
51
|
+
# Every kind; the set is closed.
|
|
52
|
+
ALL = [
|
|
53
|
+
Stream, Crypto, Ack, ResetStream, StopSending, MaxData, MaxStreamData, MaxStreams,
|
|
54
|
+
DataBlocked, StreamDataBlocked, StreamsBlocked, NewConnectionID, RetireConnectionID, NewToken, HandshakeDone
|
|
55
|
+
].freeze
|
|
56
|
+
|
|
57
|
+
# Tells `to` of the duties of the packets acknowledged and lost in
|
|
58
|
+
# `outcome`, the acknowledged first so that a range acknowledged is not
|
|
59
|
+
# put back to be sent again. An acknowledgment or loss that means
|
|
60
|
+
# nothing to the owner tells nothing.
|
|
61
|
+
def self.dispatch(outcome, to:)
|
|
62
|
+
outcome.acked.each { |packet| packet.duties.each { |duty| acked(duty, to) } }
|
|
63
|
+
outcome.lost.each { |packet| packet.duties.each { |duty| lost(duty, to) } }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Each kind has its branch and there is no else: a kind added without
|
|
67
|
+
# one raises.
|
|
68
|
+
private_class_method def self.acked(duty, owner)
|
|
69
|
+
case duty
|
|
70
|
+
in Stream then owner.stream_data_acked(duty)
|
|
71
|
+
in Crypto then owner.crypto_data_acked(duty)
|
|
72
|
+
in Ack then owner.ack_frame_acked(duty)
|
|
73
|
+
in ResetStream then owner.reset_stream_acked(duty)
|
|
74
|
+
in RetireConnectionID then owner.retire_connection_id_acked(duty)
|
|
75
|
+
in NewConnectionID then owner.new_connection_id_acked(duty)
|
|
76
|
+
in StopSending | MaxData | MaxStreamData | MaxStreams | DataBlocked | StreamDataBlocked | StreamsBlocked | NewToken | HandshakeDone
|
|
77
|
+
nil
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
private_class_method def self.lost(duty, owner)
|
|
82
|
+
case duty
|
|
83
|
+
in Stream then owner.stream_data_lost(duty)
|
|
84
|
+
in Crypto then owner.crypto_data_lost(duty)
|
|
85
|
+
in Ack then nil
|
|
86
|
+
in ResetStream then owner.reset_stream_lost(duty)
|
|
87
|
+
in StopSending then owner.stop_sending_lost(duty)
|
|
88
|
+
in MaxData then owner.max_data_lost(duty)
|
|
89
|
+
in MaxStreamData then owner.max_stream_data_lost(duty)
|
|
90
|
+
in MaxStreams then owner.max_streams_lost(duty)
|
|
91
|
+
in DataBlocked then owner.data_blocked_lost(duty)
|
|
92
|
+
in StreamDataBlocked then owner.stream_data_blocked_lost(duty)
|
|
93
|
+
in StreamsBlocked then owner.streams_blocked_lost(duty)
|
|
94
|
+
in NewConnectionID then owner.new_connection_id_lost(duty)
|
|
95
|
+
in RetireConnectionID then owner.retire_connection_id_lost(duty)
|
|
96
|
+
in NewToken then owner.new_token_lost(duty)
|
|
97
|
+
in HandshakeDone then owner.handshake_done_lost
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|