Prioritize buffered CRYPTO and control frames in QuicSession when the client sends the PHSR connection option. `RetransmitLostData` previously retransmitted all lost application `STREAM` data in `streams_with_pending_retransmission_` before buffered `CRYPTO` frames (`WriteBufferedCryptoFrames`) or buffered control frames (`control_frame_manager_.OnCanWrite`, such as `HANDSHAKE_DONE`, `NEW_TOKEN`, and `NEW_CONNECTION_ID`). When a server sent a large 0.5-RTT response to a 0-RTT request under packet loss, application `STREAM` loss retransmissions could exhaust the congestion window and starve post-handshake `CRYPTO` and control frames, delaying handshake confirmation on the client. Protected by quic_reloadable_flag_quic_prioritize_handshake_frames. PiperOrigin-RevId: 1027037216
diff --git a/quiche/common/quiche_feature_flags_list.h b/quiche/common/quiche_feature_flags_list.h index 056a5cf..dbc748a 100644 --- a/quiche/common/quiche_feature_flags_list.h +++ b/quiche/common/quiche_feature_flags_list.h
@@ -61,6 +61,7 @@ QUICHE_FLAG(bool, quiche_reloadable_flag_quic_optimize_qpack_blocking_manager, false, false, "If true, optimize qpack_blocking_manager for CPU efficiency.") QUICHE_FLAG(bool, quiche_reloadable_flag_quic_pacing_remove_non_initial_burst, false, false, "If true, remove the non-initial burst in QUIC PacingSender.") QUICHE_FLAG(bool, quiche_reloadable_flag_quic_parse_cert_compression_algos_from_chlo, true, true, "If true, parse offered cert compression algorithms from received CHLOs.") +QUICHE_FLAG(bool, quiche_reloadable_flag_quic_prioritize_handshake_frames, false, false, "When true and the client sends the PHSR connection option, QuicSession::RetransmitLostData writes buffered CRYPTO frames and control frames before retransmitting lost application stream data.") QUICHE_FLAG(bool, quiche_reloadable_flag_quic_priority_respect_incremental, false, false, "If true, respect the incremental parameter of each stream in QuicWriteBlockedList.") QUICHE_FLAG(bool, quiche_reloadable_flag_quic_receive_ack_frequency2, false, false, "When true, advertises support for ACK_FREQUENCY and IMMEDIATE_ACK from draft-ietf-quic-ack-frequency-14 and processes them correctly.") QUICHE_FLAG(bool, quiche_reloadable_flag_quic_reject_empty_cid_in_ncid, false, true, "If true, QUIC framer will reject NEW_CONNECTION_ID frames with empty connection IDs.")
diff --git a/quiche/quic/core/crypto/crypto_protocol.h b/quiche/quic/core/crypto/crypto_protocol.h index 6affa82..57b5f81 100644 --- a/quiche/quic/core/crypto/crypto_protocol.h +++ b/quiche/quic/core/crypto/crypto_protocol.h
@@ -525,6 +525,8 @@ DEFINE_STATIC_QUIC_TAG(BCFR); // Bundle CRYPTO frames when retransmitting. DEFINE_STATIC_QUIC_TAG(SAOW); // Send ACKs after OnCanWrite. DEFINE_STATIC_QUIC_TAG(CPLP); // Coalesce PING with last packet. +DEFINE_STATIC_QUIC_TAG(PHSR); // Prioritize handshake over stream + // retransmissions. // Stats collection tags DEFINE_STATIC_QUIC_TAG(EPID); // Endpoint identifier.
diff --git a/quiche/quic/core/quic_session.cc b/quiche/quic/core/quic_session.cc index 8bf14e0..9c403e7 100644 --- a/quiche/quic/core/quic_session.cc +++ b/quiche/quic/core/quic_session.cc
@@ -1551,6 +1551,10 @@ } if (VersionIsIetfQuic(transport_version())) { + if (GetQuicReloadableFlag(quic_prioritize_handshake_frames) && + config()->HasClientSentConnectionOption(kPHSR, perspective_)) { + prioritize_handshake_frames_ = true; + } ietf_streamid_manager_.SetMaxOpenIncomingBidirectionalStreams( config()->GetMaxBidirectionalStreamsToSend()); ietf_streamid_manager_.SetMaxOpenIncomingUnidirectionalStreams( @@ -2803,17 +2807,31 @@ QuicConnection::ScopedPacketFlusher retransmission_flusher(connection_); // Retransmit crypto data first. bool uses_crypto_frames = VersionIsIetfQuic(transport_version()); - if (QuicCryptoStream* const crypto_stream = GetMutableCryptoStream(); - uses_crypto_frames && crypto_stream->HasPendingCryptoRetransmission()) { + QuicCryptoStream* const crypto_stream = GetMutableCryptoStream(); + if (uses_crypto_frames && crypto_stream->HasPendingCryptoRetransmission()) { crypto_stream->WritePendingCryptoRetransmission(); } + if (prioritize_handshake_frames_ && uses_crypto_frames) { + // Couldn't write crypto retransmissions, so must be write blocked. + if (crypto_stream->HasPendingCryptoRetransmission()) { + return false; + } + // When PHSR is negotiated, write buffered CRYPTO frames before + // retransmitting lost stream data, so that post-handshake CRYPTO data is + // not starved by stream retransmissions under loss. + if (connection_->connected() && crypto_stream->HasBufferedCryptoFrames()) { + QUIC_RELOADABLE_FLAG_COUNT_N(quic_prioritize_handshake_frames, 1, 2); + crypto_stream->WriteBufferedCryptoFrames(); + } + if (!connection_->connected() || crypto_stream->HasBufferedCryptoFrames()) { + return false; + } + } // Retransmit crypto data in stream 1 frames (version < 47). if (!uses_crypto_frames && streams_with_pending_retransmission_.contains( QuicUtils::GetCryptoStreamId(transport_version()))) { // Retransmit crypto data first. - QuicStream* const crypto_stream = - GetStream(QuicUtils::GetCryptoStreamId(transport_version())); crypto_stream->OnCanWrite(); QUICHE_DCHECK(CheckStreamWriteBlocked(crypto_stream)); if (crypto_stream->HasPendingRetransmission()) { @@ -2830,6 +2848,23 @@ return false; } } + if (prioritize_handshake_frames_ && uses_crypto_frames && + IsEncryptionEstablished()) { + // When PHSR is negotiated, also write buffered control frames (e.g. + // HANDSHAKE_DONE, NEW_TOKEN) before retransmitting lost stream data, once + // encryption is established. + if (!connection_->connected()) { + return false; + } + if (control_frame_manager_.WillingToWrite()) { + QUIC_RELOADABLE_FLAG_COUNT_N(quic_prioritize_handshake_frames, 2, 2); + control_frame_manager_.OnCanWrite(); + if (!connection_->connected() || + control_frame_manager_.WillingToWrite()) { + return false; + } + } + } while (!streams_with_pending_retransmission_.empty()) { if (!CanWriteStreamData()) { break;
diff --git a/quiche/quic/core/quic_session.h b/quiche/quic/core/quic_session.h index ed584a7..d81fd60 100644 --- a/quiche/quic/core/quic_session.h +++ b/quiche/quic/core/quic_session.h
@@ -872,6 +872,9 @@ bool bundle_retransmitted_crypto_frames() const { return bundle_retransmitted_crypto_frames_; } + bool prioritize_handshake_frames() const { + return prioritize_handshake_frames_; + } protected: using StreamMap = @@ -1310,6 +1313,10 @@ bool bundle_retransmitted_crypto_frames_ = GetQuicReloadableFlag(quic_bundle_crypto_retransmits); + + // If true, RetransmitLostData() writes buffered CRYPTO frames and pending + // control frames before retransmitting lost stream data. + bool prioritize_handshake_frames_ = false; }; } // namespace quic
diff --git a/quiche/quic/core/quic_session_test.cc b/quiche/quic/core/quic_session_test.cc index a160743..c502e3d 100644 --- a/quiche/quic/core/quic_session_test.cc +++ b/quiche/quic/core/quic_session_test.cc
@@ -3838,6 +3838,195 @@ QuicSessionPeer::GetLocallyClosedStreamsHighestOffset(&session_).size()); } +TEST_P(QuicSessionTestServer, PrioritizeHandshakeFrames) { + if (!VersionIsIetfQuic(transport_version())) { + return; + } + SetQuicReloadableFlag(quic_prioritize_handshake_frames, true); + CompleteHandshake(); + QuicConfigPeer::SetReceivedConnectionOptions(session_.config(), {kPHSR}); + session_.OnConfigNegotiated(); + EXPECT_TRUE(session_.prioritize_handshake_frames()); + + // 1. Mark an application stream frame as lost so the stream has pending + // retransmissions. + TestStream* stream = session_.CreateOutgoingBidirectionalStream(); + QuicStreamFrame frame(stream->id(), false, 0, 9); + EXPECT_CALL(*stream, HasPendingRetransmission()) + .WillOnce(Return(true)) + .WillOnce(Return(false)); + session_.OnFrameLost(QuicFrame(frame)); + + // 2. Buffer CRYPTO data on the crypto stream. + TestCryptoStream* crypto_stream = session_.GetMutableCryptoStream(); + std::string crypto_data(1350, 'a'); + EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_FORWARD_SECURE, 1350, 0)) + .WillOnce(Return(1000)); + crypto_stream->WriteCryptoData(ENCRYPTION_FORWARD_SECURE, crypto_data); + EXPECT_TRUE(crypto_stream->HasBufferedCryptoFrames()); + + // 3. Buffer a control frame in the control frame manager. + EXPECT_CALL(*connection_, SendControlFrame(_)).WillOnce(Return(false)); + session_.SendWindowUpdate(stream->id(), 100); + EXPECT_TRUE(session_.control_frame_manager().WillingToWrite()); + + InSequence s; + EXPECT_CALL(*connection_, + SendCryptoData(ENCRYPTION_FORWARD_SECURE, 350, 1000)) + .WillOnce(Return(350)); + EXPECT_CALL(*connection_, SendControlFrame(_)).WillOnce(&ClearControlFrame); + EXPECT_CALL(*stream, OnCanWrite()); + + session_.OnCanWrite(); + EXPECT_FALSE(crypto_stream->HasBufferedCryptoFrames()); + EXPECT_FALSE(session_.control_frame_manager().WillingToWrite()); +} + +TEST_P(QuicSessionTestServer, PrioritizeHandshakeFramesWriteBlocked) { + if (!VersionIsIetfQuic(transport_version())) { + return; + } + SetQuicReloadableFlag(quic_prioritize_handshake_frames, true); + CompleteHandshake(); + QuicConfigPeer::SetReceivedConnectionOptions(session_.config(), {kPHSR}); + session_.OnConfigNegotiated(); + EXPECT_TRUE(session_.prioritize_handshake_frames()); + + // 1. Mark an application stream frame as lost so the stream has pending + // retransmissions. + TestStream* stream = session_.CreateOutgoingBidirectionalStream(); + QuicStreamFrame frame(stream->id(), false, 0, 9); + EXPECT_CALL(*stream, HasPendingRetransmission()) + .WillOnce(Return(true)) + .WillOnce(Return(false)); + session_.OnFrameLost(QuicFrame(frame)); + + // 2. Buffer CRYPTO data on the crypto stream. + TestCryptoStream* crypto_stream = session_.GetMutableCryptoStream(); + std::string crypto_data(1350, 'a'); + EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_FORWARD_SECURE, 1350, 0)) + .WillOnce(Return(1000)); + crypto_stream->WriteCryptoData(ENCRYPTION_FORWARD_SECURE, crypto_data); + EXPECT_TRUE(crypto_stream->HasBufferedCryptoFrames()); + + // 3. Buffer a control frame in the control frame manager. + EXPECT_CALL(*connection_, SendControlFrame(_)).WillOnce(Return(false)); + session_.SendWindowUpdate(stream->id(), 100); + EXPECT_TRUE(session_.control_frame_manager().WillingToWrite()); + + // First OnCanWrite: writing buffered CRYPTO frames is write-blocked, so + // neither the control frame nor the stream retransmission is attempted. + EXPECT_CALL(*connection_, + SendCryptoData(ENCRYPTION_FORWARD_SECURE, 350, 1000)) + .WillOnce(Return(0)); + EXPECT_CALL(*connection_, SendControlFrame(_)).Times(0); + EXPECT_CALL(*stream, OnCanWrite()).Times(0); + session_.OnCanWrite(); + EXPECT_TRUE(crypto_stream->HasBufferedCryptoFrames()); + EXPECT_TRUE(session_.control_frame_manager().WillingToWrite()); + + // Second OnCanWrite: buffered CRYPTO frames succeed, but writing the buffered + // control frame is write-blocked, so the stream retransmission is still not + // attempted. + { + InSequence s; + EXPECT_CALL(*connection_, + SendCryptoData(ENCRYPTION_FORWARD_SECURE, 350, 1000)) + .WillOnce(Return(350)); + EXPECT_CALL(*connection_, SendControlFrame(_)).WillOnce(Return(false)); + } + EXPECT_CALL(*stream, OnCanWrite()).Times(0); + session_.OnCanWrite(); + EXPECT_FALSE(crypto_stream->HasBufferedCryptoFrames()); + EXPECT_TRUE(session_.control_frame_manager().WillingToWrite()); + + // Third OnCanWrite: the buffered control frame and stream retransmission both + // succeed. + { + InSequence s; + EXPECT_CALL(*connection_, SendControlFrame(_)).WillOnce(&ClearControlFrame); + EXPECT_CALL(*stream, OnCanWrite()); + } + session_.OnCanWrite(); + EXPECT_FALSE(session_.control_frame_manager().WillingToWrite()); +} + +TEST_P(QuicSessionTestServer, + StreamRetransmissionsBeforeBufferedHandshakeWhenFlagFalse) { + if (!VersionIsIetfQuic(transport_version())) { + return; + } + SetQuicReloadableFlag(quic_prioritize_handshake_frames, false); + CompleteHandshake(); + QuicConfigPeer::SetReceivedConnectionOptions(session_.config(), {kPHSR}); + session_.OnConfigNegotiated(); + EXPECT_FALSE(session_.prioritize_handshake_frames()); + + // 1. Mark an application stream frame as lost so the stream has pending + // retransmissions. + TestStream* stream = session_.CreateOutgoingBidirectionalStream(); + QuicStreamFrame frame(stream->id(), false, 0, 9); + EXPECT_CALL(*stream, HasPendingRetransmission()) + .WillOnce(Return(true)) + .WillOnce(Return(false)); + session_.OnFrameLost(QuicFrame(frame)); + + // 2. Buffer CRYPTO data on the crypto stream. + TestCryptoStream* crypto_stream = session_.GetMutableCryptoStream(); + std::string crypto_data(1350, 'a'); + EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_FORWARD_SECURE, 1350, 0)) + .WillOnce(Return(1000)); + crypto_stream->WriteCryptoData(ENCRYPTION_FORWARD_SECURE, crypto_data); + EXPECT_TRUE(crypto_stream->HasBufferedCryptoFrames()); + + // 3. Buffer a control frame in the control frame manager. + EXPECT_CALL(*connection_, SendControlFrame(_)).WillOnce(Return(false)); + session_.SendWindowUpdate(stream->id(), 100); + EXPECT_TRUE(session_.control_frame_manager().WillingToWrite()); + + InSequence s; + EXPECT_CALL(*stream, OnCanWrite()); + EXPECT_CALL(*connection_, + SendCryptoData(ENCRYPTION_FORWARD_SECURE, 350, 1000)) + .WillOnce(Return(350)); + EXPECT_CALL(*connection_, SendControlFrame(_)).WillOnce(&ClearControlFrame); + + session_.OnCanWrite(); + EXPECT_FALSE(crypto_stream->HasBufferedCryptoFrames()); + EXPECT_FALSE(session_.control_frame_manager().WillingToWrite()); +} + +TEST_P(QuicSessionTestClient, PrioritizeHandshakeFrames) { + if (!VersionIsIetfQuic(transport_version())) { + return; + } + SetQuicReloadableFlag(quic_prioritize_handshake_frames, true); + EXPECT_FALSE(session_.prioritize_handshake_frames()); + session_.config()->SetConnectionOptionsToSend({kPHSR}); + connection_->SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); + session_.OnConfigNegotiated(); + EXPECT_TRUE(session_.prioritize_handshake_frames()); +} + +TEST_P(QuicSessionTestClient, NoPrioritizeHandshakeFramesWithoutPhsr) { + if (!VersionIsIetfQuic(transport_version())) { + return; + } + SetQuicReloadableFlag(quic_prioritize_handshake_frames, true); + connection_->SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); + session_.OnConfigNegotiated(); + EXPECT_FALSE(session_.prioritize_handshake_frames()); +} + +TEST_P(QuicSessionTestServer, NoPrioritizeHandshakeFramesWithoutPhsr) { + if (!VersionIsIetfQuic(transport_version())) { + return; + } + SetQuicReloadableFlag(quic_prioritize_handshake_frames, true); + CompleteHandshake(); + EXPECT_FALSE(session_.prioritize_handshake_frames()); +} + } // namespace } // namespace test } // namespace quic