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,420 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::QUIC
|
|
4
|
+
class Recovery
|
|
5
|
+
# One packet number space (RFC 9000 "Packet Numbers"): :initial,
|
|
6
|
+
# :handshake or :application_data. On the sending side it hands out the
|
|
7
|
+
# packet numbers, now and then skipping one to catch a peer that
|
|
8
|
+
# acknowledges what it never received, and keeps the record of each
|
|
9
|
+
# packet sent until it is acknowledged, lost or discarded, in a Hash in
|
|
10
|
+
# the order the packets were sent. On the receiving side it keeps the
|
|
11
|
+
# packet numbers received, as the starts and ends of their ranges in
|
|
12
|
+
# two Integer arrays, and the ECN marks they came with.
|
|
13
|
+
class PacketNumberSpace
|
|
14
|
+
attr_reader :name
|
|
15
|
+
# The largest packet number sent, nil before any.
|
|
16
|
+
attr_reader :largest_sent
|
|
17
|
+
# The largest packet number the peer acknowledged, nil before any.
|
|
18
|
+
attr_reader :largest_acked_packet
|
|
19
|
+
# When the next packet can be declared lost by time, nil when none
|
|
20
|
+
# can.
|
|
21
|
+
attr_accessor :loss_time
|
|
22
|
+
attr_reader :time_of_last_ack_eliciting_packet
|
|
23
|
+
# The ack-eliciting packets in flight in this space.
|
|
24
|
+
attr_reader :ack_eliciting_in_flight
|
|
25
|
+
# The packet number skipped and not yet shown to be one the peer
|
|
26
|
+
# leaves out of its ACKs, nil when none.
|
|
27
|
+
attr_reader :skipped
|
|
28
|
+
# How many packet numbers or records the last `acknowledge` went
|
|
29
|
+
# through.
|
|
30
|
+
attr_reader :ack_steps
|
|
31
|
+
# The packets sent marked ECT(0), and the ECN counts of the last ACK
|
|
32
|
+
# that passed validation.
|
|
33
|
+
attr_reader :ect0_sent
|
|
34
|
+
attr_accessor :ecn_counts
|
|
35
|
+
|
|
36
|
+
def initialize(name)
|
|
37
|
+
@name = name
|
|
38
|
+
@next_packet_number = 0
|
|
39
|
+
@largest_sent = nil #: Integer?
|
|
40
|
+
@largest_acked_packet = nil #: Integer?
|
|
41
|
+
@loss_time = nil #: Float?
|
|
42
|
+
@time_of_last_ack_eliciting_packet = nil #: Float?
|
|
43
|
+
@ack_eliciting_in_flight = 0
|
|
44
|
+
@sent = {} #: Hash[Integer, SentPacket]
|
|
45
|
+
@lost = {} #: Hash[Integer, SentPacket]
|
|
46
|
+
# The packets of only ACKs sent since the last ack-eliciting one.
|
|
47
|
+
@ack_only_tail = [] #: Array[Integer]
|
|
48
|
+
@skip_after = nil #: Integer?
|
|
49
|
+
@skipped = nil #: Integer?
|
|
50
|
+
@ack_steps = 0
|
|
51
|
+
@rejected_below = nil #: Integer?
|
|
52
|
+
@ect0_sent = 0
|
|
53
|
+
@ecn_counts = NO_ECN_COUNTS
|
|
54
|
+
@received_starts = [] #: Array[Integer]
|
|
55
|
+
@received_ends = [] #: Array[Integer]
|
|
56
|
+
@ignore_below = 0
|
|
57
|
+
@received_ecn = { ect0: 0, ect1: 0, ce: 0 } #: Hash[Symbol, Integer]
|
|
58
|
+
@largest_received_at = nil #: Float?
|
|
59
|
+
@largest_ack_eliciting_received = nil #: Integer?
|
|
60
|
+
@unacked_ack_eliciting = 0
|
|
61
|
+
@first_unacked_at = nil #: Float?
|
|
62
|
+
@ack_deadline = nil #: Float?
|
|
63
|
+
@ack_eliciting_threshold = 2
|
|
64
|
+
@max_ack_delay = 0.0
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The ack-eliciting packets after which an ACK goes at once, and the
|
|
68
|
+
# longest an ACK of an ack-eliciting packet waits (RFC 9000 "Sending ACK
|
|
69
|
+
# Frames"). Values of the space, not constants, for ACK_FREQUENCY to
|
|
70
|
+
# change one day; Initial and Handshake packets are always acknowledged
|
|
71
|
+
# at once.
|
|
72
|
+
attr_accessor :ack_eliciting_threshold
|
|
73
|
+
attr_accessor :max_ack_delay
|
|
74
|
+
# When an ACK must go, nil when none is owed.
|
|
75
|
+
attr_reader :ack_deadline
|
|
76
|
+
|
|
77
|
+
# Numbers below it are no longer kept and count as duplicates.
|
|
78
|
+
attr_reader :ignore_below
|
|
79
|
+
|
|
80
|
+
# Records the packet number received, with the ECN mark the datagram
|
|
81
|
+
# came with; false for a duplicate, which is not counted (RFC 9000
|
|
82
|
+
# "Packet Number Spaces"). The ranges are searched from the newest,
|
|
83
|
+
# where most packets land, and a packet in order only moves the end of
|
|
84
|
+
# the last range. Past MAX_ACK_RANGES ranges the oldest is let go.
|
|
85
|
+
#
|
|
86
|
+
# An ack-eliciting packet owes an ACK: at once in Initial and Handshake,
|
|
87
|
+
# as the threshold's count of them, out of order, after a gap, or with
|
|
88
|
+
# a CE mark; within max_ack_delay of the first one otherwise. `now` is
|
|
89
|
+
# when the packet came, for the ACK Delay.
|
|
90
|
+
def receive(packet_number, ecn:, ack_eliciting:, now:)
|
|
91
|
+
return false if packet_number < @ignore_below
|
|
92
|
+
largest = @received_ends.last
|
|
93
|
+
return false unless record_received(packet_number)
|
|
94
|
+
@received_ecn[ecn] += 1 if ecn != :not_ect
|
|
95
|
+
@largest_received_at = now if largest.nil? || packet_number > largest
|
|
96
|
+
owe_ack(packet_number, ce: ecn == :ce, now:) if ack_eliciting
|
|
97
|
+
@ack_deadline = now if ecn == :ce && @unacked_ack_eliciting.positive?
|
|
98
|
+
true
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# An ACK of what was received went.
|
|
102
|
+
def ack_sent
|
|
103
|
+
@unacked_ack_eliciting = 0
|
|
104
|
+
@first_unacked_at = nil
|
|
105
|
+
@ack_deadline = nil
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Whether an ack-eliciting packet waits for an ACK, which can then ride
|
|
109
|
+
# along with anything sent.
|
|
110
|
+
def ack_owed? = @unacked_ack_eliciting.positive?
|
|
111
|
+
|
|
112
|
+
# The ACK frame of what was received, nil before anything: at most
|
|
113
|
+
# MAX_ACK_RANGES ranges, the delay since the largest came, and the ECN
|
|
114
|
+
# counts when a packet was marked.
|
|
115
|
+
def ack_frame(now:)
|
|
116
|
+
largest_at = @largest_received_at or return nil
|
|
117
|
+
Wire::Frame::Ack.new(ranges: received_ranges, delay: [now - largest_at, 0.0].max, ecn: received_ecn_counts)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
private def owe_ack(packet_number, ce:, now:)
|
|
121
|
+
@unacked_ack_eliciting += 1
|
|
122
|
+
largest = @largest_ack_eliciting_received
|
|
123
|
+
reordered = largest && packet_number < largest
|
|
124
|
+
gap = largest && packet_number > largest && range_start(packet_number) > largest + 1
|
|
125
|
+
@largest_ack_eliciting_received = packet_number if largest.nil? || packet_number > largest
|
|
126
|
+
at_once = @name != :application_data || @unacked_ack_eliciting >= @ack_eliciting_threshold || reordered || gap || ce
|
|
127
|
+
deadline = at_once ? now : (@first_unacked_at ||= now) + @max_ack_delay
|
|
128
|
+
@ack_deadline = [@ack_deadline || deadline, deadline].min
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# The start of the range received that holds `packet_number`.
|
|
132
|
+
private def range_start(packet_number)
|
|
133
|
+
index = @received_starts.size - 1
|
|
134
|
+
index -= 1 while index.positive? && @received_starts.fetch(index) > packet_number
|
|
135
|
+
@received_starts.fetch(index)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Adds `packet_number` to the ranges; false when it is in one already.
|
|
139
|
+
private def record_received(packet_number)
|
|
140
|
+
starts = @received_starts
|
|
141
|
+
ends = @received_ends
|
|
142
|
+
index = starts.size - 1
|
|
143
|
+
index -= 1 while index >= 0 && starts.fetch(index) > packet_number
|
|
144
|
+
return false if index >= 0 && packet_number <= ends.fetch(index)
|
|
145
|
+
after_left = index >= 0 && ends.fetch(index) + 1 == packet_number
|
|
146
|
+
before_right = index + 1 < starts.size && starts.fetch(index + 1) - 1 == packet_number
|
|
147
|
+
if after_left && before_right
|
|
148
|
+
ends[index] = ends.fetch(index + 1)
|
|
149
|
+
starts.delete_at(index + 1)
|
|
150
|
+
ends.delete_at(index + 1)
|
|
151
|
+
elsif after_left
|
|
152
|
+
ends[index] = packet_number
|
|
153
|
+
elsif before_right
|
|
154
|
+
starts[index + 1] = packet_number
|
|
155
|
+
else
|
|
156
|
+
starts.insert(index + 1, packet_number)
|
|
157
|
+
ends.insert(index + 1, packet_number)
|
|
158
|
+
end
|
|
159
|
+
if starts.size > MAX_ACK_RANGES
|
|
160
|
+
starts.shift
|
|
161
|
+
@ignore_below = ends.shift + 1
|
|
162
|
+
end
|
|
163
|
+
true
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# The ranges received, the largest first, for an ACK frame.
|
|
167
|
+
def received_ranges = @received_starts.each_with_index.map { |first, index| first..@received_ends.fetch(index) }.reverse!
|
|
168
|
+
|
|
169
|
+
def largest_received = @received_ends.last
|
|
170
|
+
|
|
171
|
+
# The ECN counts of the packets received, nil when none was marked.
|
|
172
|
+
def received_ecn_counts
|
|
173
|
+
return nil if @received_ecn.each_value.all?(&:zero?)
|
|
174
|
+
Wire::Frame::Ack::ECNCounts.new(ect0: @received_ecn.fetch(:ect0), ect1: @received_ecn.fetch(:ect1), ce: @received_ecn.fetch(:ce))
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# An ACK this end sent, whose largest was `largest`, was acknowledged:
|
|
178
|
+
# the numbers up to it need not be acknowledged again (RFC 9000
|
|
179
|
+
# "Limiting Ranges by Tracking ACK Frames").
|
|
180
|
+
def acknowledged_up_to(largest)
|
|
181
|
+
return if largest < @ignore_below
|
|
182
|
+
@ignore_below = largest + 1
|
|
183
|
+
while (first = @received_starts.first)
|
|
184
|
+
last = @received_ends.fetch(0)
|
|
185
|
+
if last <= largest
|
|
186
|
+
@received_starts.shift
|
|
187
|
+
@received_ends.shift
|
|
188
|
+
else
|
|
189
|
+
@received_starts[0] = largest + 1 if first <= largest
|
|
190
|
+
break
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
NO_ECN_COUNTS = Wire::Frame::Ack::ECNCounts.new(ect0: 0, ect1: 0, ce: 0)
|
|
196
|
+
|
|
197
|
+
# The least number the next packet will have, one more when a number
|
|
198
|
+
# is left out first; not taken.
|
|
199
|
+
def upcoming_packet_number = @next_packet_number
|
|
200
|
+
|
|
201
|
+
# The packet number for the next packet. After `skip_after`'s count
|
|
202
|
+
# of packets, one number is left out first.
|
|
203
|
+
def next_packet_number
|
|
204
|
+
number = @next_packet_number
|
|
205
|
+
case @skip_after
|
|
206
|
+
when 0
|
|
207
|
+
@skipped = number
|
|
208
|
+
number += 1
|
|
209
|
+
@skip_after = nil
|
|
210
|
+
when Integer then @skip_after -= 1 # steep:ignore
|
|
211
|
+
end
|
|
212
|
+
@next_packet_number = number + 1
|
|
213
|
+
number
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# Whether a skip is to come or has yet to be shown.
|
|
217
|
+
def skipping? = !@skip_after.nil? || !@skipped.nil?
|
|
218
|
+
|
|
219
|
+
# Whether a packet of only an ACK has gone since the last ack-eliciting
|
|
220
|
+
# one.
|
|
221
|
+
def acks_alone? = !@ack_only_tail.empty?
|
|
222
|
+
|
|
223
|
+
# Leaves a packet number out after `packets` more packets.
|
|
224
|
+
def skip_after(packets)
|
|
225
|
+
raise ArgumentError, "a skip is pending already" if skipping?
|
|
226
|
+
@skip_after = packets
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# Keeps the record of a packet sent. Of the packets of only ACKs that
|
|
230
|
+
# follow the last ack-eliciting one, which the peer may never
|
|
231
|
+
# acknowledge, the latest MAX_NON_ACK_ELICITING_TAIL are kept (loss
|
|
232
|
+
# recovery's design, "Limits").
|
|
233
|
+
def add(packet)
|
|
234
|
+
number = packet.packet_number
|
|
235
|
+
@sent[number] = packet
|
|
236
|
+
@largest_sent = number
|
|
237
|
+
@ect0_sent += 1 if packet.ecn == :ect0
|
|
238
|
+
if packet.ack_eliciting
|
|
239
|
+
@ack_only_tail.clear
|
|
240
|
+
elsif !packet.in_flight
|
|
241
|
+
@ack_only_tail << number
|
|
242
|
+
@sent.delete(@ack_only_tail.shift) if @ack_only_tail.size > MAX_NON_ACK_ELICITING_TAIL
|
|
243
|
+
end
|
|
244
|
+
return unless packet.in_flight && packet.ack_eliciting
|
|
245
|
+
@time_of_last_ack_eliciting_packet = packet.time_sent
|
|
246
|
+
@ack_eliciting_in_flight += 1
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def sent?(packet_number) = @sent.key?(packet_number)
|
|
250
|
+
|
|
251
|
+
# The oldest record in flight that elicits an ACK, nil when none.
|
|
252
|
+
def oldest_ack_eliciting = @sent.each_value.find { |packet| packet.ack_eliciting && packet.in_flight }
|
|
253
|
+
|
|
254
|
+
# The oldest record in flight that elicits an ACK and carries
|
|
255
|
+
# something to send again, nil when none: a PING, a PMTU probe or a
|
|
256
|
+
# probe of nothing but a PING carries nothing.
|
|
257
|
+
def oldest_to_send_again = @sent.each_value.find { |packet| packet.ack_eliciting && packet.in_flight && !packet.duties.empty? }
|
|
258
|
+
|
|
259
|
+
# Takes the ranges of an ACK frame of `frame_type`, from the largest
|
|
260
|
+
# down, and gives the records they acknowledge for the first time,
|
|
261
|
+
# taken out, in packet number order. An ACK of a packet never sent, the
|
|
262
|
+
# skipped number among them, is PROTOCOL_VIOLATION (loss recovery's
|
|
263
|
+
# design, "Validation"); one of packets acknowledged before is not.
|
|
264
|
+
#
|
|
265
|
+
# The ranges are first cut down to the records there are; then either
|
|
266
|
+
# each number in them is looked up or the records are walked once
|
|
267
|
+
# against them, whichever is less, so that wide ranges cost no more
|
|
268
|
+
# than the records.
|
|
269
|
+
def acknowledge(ranges, frame_type:)
|
|
270
|
+
largest = ranges.first.end
|
|
271
|
+
sent = @largest_sent
|
|
272
|
+
if sent.nil? || largest > sent
|
|
273
|
+
raise Violation.new("an ACK of packet #{largest}, never sent", frame_type:)
|
|
274
|
+
end
|
|
275
|
+
skipped = @skipped
|
|
276
|
+
if skipped && ranges.any? { |range| range.cover?(skipped) }
|
|
277
|
+
raise Violation.new("an ACK of packet #{skipped}, skipped", frame_type:)
|
|
278
|
+
end
|
|
279
|
+
rejected = @rejected_below
|
|
280
|
+
if rejected && ranges.fetch(-1).begin < rejected
|
|
281
|
+
raise Violation.new("an ACK of a 0-RTT packet after 0-RTT was rejected", frame_type:)
|
|
282
|
+
end
|
|
283
|
+
@largest_acked_packet = [@largest_acked_packet || largest, largest].max
|
|
284
|
+
@skipped = nil if skipped && largest > skipped
|
|
285
|
+
acked = take_acknowledged(ranges)
|
|
286
|
+
acked.each { |packet| @ack_eliciting_in_flight -= 1 if packet.in_flight && packet.ack_eliciting }
|
|
287
|
+
acked
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
# Takes out the records lost (RFC 9002 "Detecting Lost Packets"): those
|
|
291
|
+
# at least PACKET_THRESHOLD below the largest acknowledged, or sent
|
|
292
|
+
# `loss_delay` or more before `now`. Each is yielded with its trigger,
|
|
293
|
+
# :reordering_threshold or :time_threshold, and `loss_time` set to
|
|
294
|
+
# when the next one would be lost by time.
|
|
295
|
+
#
|
|
296
|
+
# The time is compared by the same sum as the loss time, so that a
|
|
297
|
+
# packet is lost at its loss time itself: `now - loss_delay` may round
|
|
298
|
+
# below the time it was sent, which would set the timer again to the
|
|
299
|
+
# time it ran out at.
|
|
300
|
+
def detect_lost(loss_delay:, now:)
|
|
301
|
+
largest = @largest_acked_packet
|
|
302
|
+
@loss_time = nil
|
|
303
|
+
lost = [] #: Array[SentPacket]
|
|
304
|
+
return lost unless largest
|
|
305
|
+
@sent.each_value do |packet|
|
|
306
|
+
break if packet.packet_number > largest
|
|
307
|
+
loss_time = packet.time_sent + loss_delay
|
|
308
|
+
if largest >= packet.packet_number + PACKET_THRESHOLD
|
|
309
|
+
lost << packet
|
|
310
|
+
yield packet, :reordering_threshold
|
|
311
|
+
elsif loss_time <= now
|
|
312
|
+
lost << packet
|
|
313
|
+
yield packet, :time_threshold
|
|
314
|
+
else
|
|
315
|
+
@loss_time = [@loss_time || loss_time, loss_time].min
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
lost.each do |packet|
|
|
319
|
+
@sent.delete(packet.packet_number)
|
|
320
|
+
@lost[packet.packet_number] = packet
|
|
321
|
+
@ack_eliciting_in_flight -= 1 if packet.in_flight && packet.ack_eliciting
|
|
322
|
+
end
|
|
323
|
+
@lost.shift while @lost.size > MAX_LOST_PACKETS
|
|
324
|
+
lost
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
# Takes out the records declared lost that the ranges acknowledge
|
|
328
|
+
# after all (loss recovery's design, "Lost packets kept").
|
|
329
|
+
def take_spurious(ranges)
|
|
330
|
+
@lost.each_value.select do |packet|
|
|
331
|
+
number = packet.packet_number
|
|
332
|
+
range = ranges.bsearch { |candidate| candidate.begin <= number }
|
|
333
|
+
range&.cover?(number)
|
|
334
|
+
end.each { |packet| @lost.delete(packet.packet_number) }
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
# Throws everything sent away, and gives the records that were still
|
|
338
|
+
# waiting (RFC 9002 "Discarding Keys and Packet State"). The packet
|
|
339
|
+
# numbers go on. The ECN counts start again, as what comes after a
|
|
340
|
+
# Retry or a Version Negotiation packet goes to a peer that counts
|
|
341
|
+
# from nothing.
|
|
342
|
+
def discard
|
|
343
|
+
records = @sent.values
|
|
344
|
+
@sent.clear
|
|
345
|
+
@lost.clear
|
|
346
|
+
@ect0_sent = 0
|
|
347
|
+
@ecn_counts = NO_ECN_COUNTS
|
|
348
|
+
@received_starts.clear
|
|
349
|
+
@received_ends.clear
|
|
350
|
+
ack_sent
|
|
351
|
+
@loss_time = nil
|
|
352
|
+
@time_of_last_ack_eliciting_packet = nil
|
|
353
|
+
@ack_eliciting_in_flight = 0
|
|
354
|
+
records
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
# Throws away the records below `boundary`, the 0-RTT packets of a
|
|
358
|
+
# rejected 0-RTT, and gives those that were still waiting; from now on
|
|
359
|
+
# an ACK of any of them is PROTOCOL_VIOLATION (RFC 9001 "Rejecting
|
|
360
|
+
# 0-RTT").
|
|
361
|
+
def reject_below(boundary)
|
|
362
|
+
@rejected_below = boundary
|
|
363
|
+
@lost.delete_if { |number, _| number < boundary }
|
|
364
|
+
rejected = @sent.values.select { |packet| packet.packet_number < boundary }
|
|
365
|
+
rejected.each do |packet|
|
|
366
|
+
@sent.delete(packet.packet_number)
|
|
367
|
+
@ack_eliciting_in_flight -= 1 if packet.in_flight && packet.ack_eliciting
|
|
368
|
+
end
|
|
369
|
+
@loss_time = nil if @sent.empty?
|
|
370
|
+
rejected
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
# Forgets the records declared lost that were sent before `time`.
|
|
374
|
+
def forget_lost(before:)
|
|
375
|
+
@lost.shift while (oldest = @lost.each_value.first) && oldest.time_sent < before
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
private def take_acknowledged(ranges)
|
|
379
|
+
@ack_steps = 0
|
|
380
|
+
oldest = @sent.each_key.first
|
|
381
|
+
return [] unless oldest
|
|
382
|
+
clipped = ranges.filter_map do |range|
|
|
383
|
+
low = [range.begin, oldest].max
|
|
384
|
+
low..range.end if low <= range.end
|
|
385
|
+
end
|
|
386
|
+
width = clipped.sum { |range| range.end - range.begin + 1 }
|
|
387
|
+
width <= @sent.size ? look_up(clipped) : walk(clipped)
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
# Each number of the ranges, from the smallest.
|
|
391
|
+
private def look_up(ranges)
|
|
392
|
+
acked = [] #: Array[SentPacket]
|
|
393
|
+
ranges.reverse_each do |range|
|
|
394
|
+
range.each do |number|
|
|
395
|
+
@ack_steps += 1
|
|
396
|
+
packet = @sent.delete(number)
|
|
397
|
+
acked << packet if packet
|
|
398
|
+
end
|
|
399
|
+
end
|
|
400
|
+
acked
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
# The records in order, against the ranges from the smallest, up to
|
|
404
|
+
# the largest range.
|
|
405
|
+
private def walk(ranges)
|
|
406
|
+
acked = [] #: Array[SentPacket]
|
|
407
|
+
ascending = ranges.reverse
|
|
408
|
+
index = 0
|
|
409
|
+
@sent.each_key do |number|
|
|
410
|
+
@ack_steps += 1
|
|
411
|
+
index += 1 while index < ascending.size && ascending.fetch(index).end < number
|
|
412
|
+
break if index == ascending.size
|
|
413
|
+
acked << @sent.fetch(number) if ascending.fetch(index).cover?(number)
|
|
414
|
+
end
|
|
415
|
+
acked.each { |packet| @sent.delete(packet.packet_number) }
|
|
416
|
+
acked
|
|
417
|
+
end
|
|
418
|
+
end
|
|
419
|
+
end
|
|
420
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::QUIC
|
|
4
|
+
class Recovery
|
|
5
|
+
# The bandwidth estimator of a path, of the form of quicly's ratemeter:
|
|
6
|
+
# the bytes acknowledged while the sender was held back by the
|
|
7
|
+
# congestion window or pacing with data waiting, in samples of 50 ms at
|
|
8
|
+
# least, the latest ten kept. Held back, the packets sent from then on
|
|
9
|
+
# count: an ACK whose largest newly acknowledged packet is one of them
|
|
10
|
+
# adds to the sample being taken, and one past them, once sending is no
|
|
11
|
+
# longer held back, ends it. What goes while the application has nothing
|
|
12
|
+
# to send says nothing of the path and counts for nothing. The rate goes
|
|
13
|
+
# out in qlog only; no congestion controller uses it.
|
|
14
|
+
class RateMeter
|
|
15
|
+
# Seconds.
|
|
16
|
+
PERIOD = 0.05
|
|
17
|
+
SAMPLES = 10
|
|
18
|
+
|
|
19
|
+
def initialize
|
|
20
|
+
# The samples taken, oldest first: their seconds and bytes.
|
|
21
|
+
@samples = [] #: Array[[Float, Integer]]
|
|
22
|
+
# The packet numbers sent while held back: from, and until once no
|
|
23
|
+
# longer.
|
|
24
|
+
@limited_from = nil #: Integer?
|
|
25
|
+
@limited_until = nil #: Integer?
|
|
26
|
+
@acked = 0
|
|
27
|
+
# The sample being taken: when it started and the bytes acknowledged
|
|
28
|
+
# then, nil while none is, and what it holds so far.
|
|
29
|
+
@started_at = nil #: Float?
|
|
30
|
+
@started_acked = 0
|
|
31
|
+
@elapsed = 0.0
|
|
32
|
+
@bytes = 0
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Sending stopped, held back by the window or pacing with data waiting
|
|
36
|
+
# (`limited`) or not; the next packet will be numbered
|
|
37
|
+
# `next_packet_number` at least.
|
|
38
|
+
def sending_stopped(limited:, next_packet_number:)
|
|
39
|
+
return if limited == limited?
|
|
40
|
+
return @limited_until = next_packet_number unless limited
|
|
41
|
+
# A sample of the last time held back, still waiting for an ACK past
|
|
42
|
+
# it, ends here, and one with nothing in it yet is dropped.
|
|
43
|
+
@elapsed.positive? ? commit : @started_at = nil
|
|
44
|
+
@limited_from = next_packet_number
|
|
45
|
+
@limited_until = nil
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# `bytes` in flight were acknowledged at `now`, the largest packet newly
|
|
49
|
+
# acknowledged numbered `largest`.
|
|
50
|
+
def on_ack(bytes, largest:, now:)
|
|
51
|
+
@acked += bytes
|
|
52
|
+
from = @limited_from or return
|
|
53
|
+
till = @limited_until
|
|
54
|
+
if largest >= from && (till.nil? || largest < till)
|
|
55
|
+
take(now)
|
|
56
|
+
elsif till && largest >= till && @started_at
|
|
57
|
+
@elapsed.positive? ? commit : @started_at = nil
|
|
58
|
+
@limited_from = @limited_until = nil
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Bytes a second over the samples and the one being taken, nil before
|
|
63
|
+
# any.
|
|
64
|
+
def rate
|
|
65
|
+
elapsed = @samples.sum(@elapsed) { |seconds, _bytes| seconds }
|
|
66
|
+
bytes = @samples.sum(@bytes) { |_seconds, bytes| bytes }
|
|
67
|
+
bytes / elapsed if elapsed.positive?
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private def limited? = !@limited_from.nil? && @limited_until.nil?
|
|
71
|
+
|
|
72
|
+
private def take(now)
|
|
73
|
+
started_at = @started_at or return start(now)
|
|
74
|
+
@elapsed = now - started_at
|
|
75
|
+
@bytes = @acked - @started_acked
|
|
76
|
+
return if @elapsed < PERIOD
|
|
77
|
+
commit
|
|
78
|
+
start(now)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
private def start(now)
|
|
82
|
+
@started_at = now
|
|
83
|
+
@started_acked = @acked
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
private def commit
|
|
87
|
+
@samples << [@elapsed, @bytes]
|
|
88
|
+
@samples.shift if @samples.size > SAMPLES
|
|
89
|
+
@started_at = nil
|
|
90
|
+
@elapsed = 0.0
|
|
91
|
+
@bytes = 0
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::QUIC
|
|
4
|
+
class Recovery
|
|
5
|
+
# The RTT of a path (RFC 9002 "Estimating the Round-Trip Time"): the
|
|
6
|
+
# latest sample, the smoothed RTT and its variation, and the minimum,
|
|
7
|
+
# with the time of the first sample.
|
|
8
|
+
class RttEstimator
|
|
9
|
+
# Seconds; 0 before the first sample.
|
|
10
|
+
attr_reader :latest_rtt
|
|
11
|
+
attr_reader :smoothed_rtt
|
|
12
|
+
attr_reader :rttvar
|
|
13
|
+
# nil before the first sample.
|
|
14
|
+
attr_reader :min_rtt
|
|
15
|
+
attr_reader :first_sample_at
|
|
16
|
+
|
|
17
|
+
def initialize
|
|
18
|
+
@latest_rtt = 0.0
|
|
19
|
+
@smoothed_rtt = INITIAL_RTT
|
|
20
|
+
@rttvar = INITIAL_RTT / 2
|
|
21
|
+
@min_rtt = nil #: Float?
|
|
22
|
+
@first_sample_at = nil #: Float?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Takes the sample `latest_rtt`, of an ACK that reported `ack_delay`.
|
|
26
|
+
# Once the handshake is confirmed the delay counts up to
|
|
27
|
+
# `max_ack_delay` (MUST), before it in full (SHOULD), and never so far
|
|
28
|
+
# as to take the sample under the minimum. The minimum ignores the
|
|
29
|
+
# delay.
|
|
30
|
+
def update(latest_rtt:, ack_delay:, max_ack_delay:, handshake_confirmed:, now:)
|
|
31
|
+
@latest_rtt = latest_rtt
|
|
32
|
+
min_rtt = @min_rtt
|
|
33
|
+
unless min_rtt && @first_sample_at
|
|
34
|
+
@min_rtt = @smoothed_rtt = latest_rtt
|
|
35
|
+
@rttvar = latest_rtt / 2
|
|
36
|
+
@first_sample_at = now
|
|
37
|
+
return
|
|
38
|
+
end
|
|
39
|
+
min_rtt = @min_rtt = [min_rtt, latest_rtt].min
|
|
40
|
+
ack_delay = [ack_delay, max_ack_delay].min if handshake_confirmed
|
|
41
|
+
adjusted = latest_rtt >= min_rtt + ack_delay ? latest_rtt - ack_delay : latest_rtt
|
|
42
|
+
@rttvar = 0.75 * @rttvar + 0.25 * (@smoothed_rtt - adjusted).abs
|
|
43
|
+
@smoothed_rtt = 0.875 * @smoothed_rtt + 0.125 * adjusted
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# The minimum taken again from the latest sample, after persistent
|
|
47
|
+
# congestion (RFC 9002 "Estimating min_rtt").
|
|
48
|
+
def reset_min_rtt
|
|
49
|
+
@min_rtt = @latest_rtt if @first_sample_at
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# The PTO period before backoff and max_ack_delay (RFC 9002 "Computing
|
|
53
|
+
# PTO").
|
|
54
|
+
def pto_period = @smoothed_rtt + [4 * @rttvar, GRANULARITY].max
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::QUIC
|
|
4
|
+
class Recovery
|
|
5
|
+
# A packet sent and not yet acknowledged, named as RFC 9002 "Sent Packet
|
|
6
|
+
# Fields" names them. `sent_bytes` counts the QUIC header and frames, not
|
|
7
|
+
# UDP or IP; `in_flight` is false for a packet of only ACK and
|
|
8
|
+
# CONNECTION_CLOSE frames; `path` is the Path it went on, `duties` what
|
|
9
|
+
# its owners hear of when it is acknowledged or lost, `pmtu_probe`
|
|
10
|
+
# whether it probed the path MTU, whose loss does not tell the congestion
|
|
11
|
+
# controller anything, and `ecn` the codepoint it carried (:not_ect or
|
|
12
|
+
# :ect0).
|
|
13
|
+
class SentPacket < Data.define(:packet_number, :time_sent, :sent_bytes, :ack_eliciting, :in_flight, :path, :duties, :pmtu_probe, :ecn)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|