Send due handshake ACKs ahead of 1-RTT frames under SAOW and re-spin its flag. When `SAOW` (`send_acks_after_on_can_write_`) runs `visitor_->OnCanWrite()` before `SendAllPendingAcks()`, writing 1-RTT control or stream frames first prevents due `INITIAL` and `HANDSHAKE` ACKs from coalescing ahead of them in the same UDP packet. On clients, a due `INITIAL` ACK left pending after `WriteBufferedCryptoFrames()` writes a `HANDSHAKE` packet would also emit a 1200-byte padded `INITIAL` ACK right before `~ScopedPacketFlusher()` calls `OnHandshakePacketSent()` to discard `INITIAL` keys. Deprecate `gfe2_reloadable_flag_quic_send_acks_after_on_can_write` (already flipped in dev) and re-spin it as `gfe2_reloadable_flag_quic_send_acks_after_on_can_write2`: - Send expired `INITIAL` and `HANDSHAKE` ACKs via `QuicConnection::MaybeSendPendingHandshakeAcks()` in `QuicSession::OnCanWrite()` right after `WriteBufferedCryptoFrames()` (skipping when `ScopedAckDeferrer` is active). - On clients, process `HANDSHAKE_DATA` before `INITIAL_DATA` in `MaybeSendPendingHandshakeAcks()` and suppress client `INITIAL` ACKs only once a `HANDSHAKE` packet has been sent or queued in `packet_creator_` (preserving `INITIAL` ACK state if flushing the `HANDSHAKE` ACK fails). Protected by quic_reloadable_flag_quic_send_acks_after_on_can_write2. PiperOrigin-RevId: 1035059950
diff --git a/quiche/common/quiche_feature_flags_list.h b/quiche/common/quiche_feature_flags_list.h index 99f5f13..f1f20f3 100644 --- a/quiche/common/quiche_feature_flags_list.h +++ b/quiche/common/quiche_feature_flags_list.h
@@ -71,7 +71,7 @@ QUICHE_FLAG(bool, quiche_reloadable_flag_quic_require_handshake_confirmation, true, true, "If true, require handshake confirmation for QUIC connections, functionally disabling 0-rtt handshakes.") QUICHE_FLAG(bool, quiche_reloadable_flag_quic_reset_server_params_in_reset_ssl, false, false, "If true, reset server_params_ in TlsServerHandshaker::ResetSsl().") QUICHE_FLAG(bool, quiche_reloadable_flag_quic_sampler_remove_obsolete_packets, false, false, "When true, BandwidthSampler::OnPacketSent removes obsolete packets below QuicUnackedPacketMap::GetLeastUnacked() before emplacing new packets.") -QUICHE_FLAG(bool, quiche_reloadable_flag_quic_send_acks_after_on_can_write, false, false, "When true, QuicConnection::OnCanWrite() calls visitor_->OnCanWrite() before SendAllPendingAcks() so outgoing data frames can opportunistically bundle pending ACKs first.") +QUICHE_FLAG(bool, quiche_reloadable_flag_quic_send_acks_after_on_can_write2, false, false, "When true, QuicConnection::OnCanWrite() calls visitor_->OnCanWrite() before SendAllPendingAcks(), sends due INITIAL/HANDSHAKE ACKs ahead of control and stream frames, and suppresses due client INITIAL ACKs before INITIAL keys are discarded.") QUICHE_FLAG(bool, quiche_reloadable_flag_quic_test_peer_addr_change_after_normalize, false, false, "If true, QuicConnection::ProcessValidatedPacket will use normalized address to test peer address changes.") QUICHE_FLAG(bool, quiche_reloadable_flag_quic_testonly_default_false, false, false, "A testonly reloadable flag that will always default to false.") QUICHE_FLAG(bool, quiche_reloadable_flag_quic_testonly_default_true, true, true, "A testonly reloadable flag that will always default to true.")
diff --git a/quiche/quic/core/http/end_to_end_test.cc b/quiche/quic/core/http/end_to_end_test.cc index 6371c12..905681c 100644 --- a/quiche/quic/core/http/end_to_end_test.cc +++ b/quiche/quic/core/http/end_to_end_test.cc
@@ -10029,7 +10029,7 @@ } TEST_P(EndToEndTest, SendAcksAfterOnCanWrite) { - SetQuicReloadableFlag(quic_send_acks_after_on_can_write, true); + SetQuicReloadableFlag(quic_send_acks_after_on_can_write2, true); client_extra_copts_.push_back(kSAOW); SetPacketLossPercentage(30); ASSERT_TRUE(Initialize());
diff --git a/quiche/quic/core/quic_connection.cc b/quiche/quic/core/quic_connection.cc index 3bbb79a..7c9d7af 100644 --- a/quiche/quic/core/quic_connection.cc +++ b/quiche/quic/core/quic_connection.cc
@@ -8,6 +8,7 @@ #include <sys/types.h> #include <algorithm> +#include <array> #include <cstddef> #include <cstdint> #include <cstdlib> @@ -613,7 +614,7 @@ if (config.HasClientSentConnectionOption(kDFER, perspective_)) { defer_send_in_response_to_packets_ = false; } - if (GetQuicReloadableFlag(quic_send_acks_after_on_can_write) && + if (GetQuicReloadableFlag(quic_send_acks_after_on_can_write2) && version().IsIetfQuic() && config.HasClientSentConnectionOption(kSAOW, perspective_)) { send_acks_after_on_can_write_ = true; @@ -3059,7 +3060,7 @@ WriteQueuedPackets(); if (send_acks_after_on_can_write_) { - QUIC_RELOADABLE_FLAG_COUNT(quic_send_acks_after_on_can_write); + QUIC_RELOADABLE_FLAG_COUNT_N(quic_send_acks_after_on_can_write2, 1, 2); // Sending queued packets may have caused the socket to become write // blocked, or the congestion manager to prohibit sending. const bool can_write_retransmittable = CanWrite(HAS_RETRANSMITTABLE_DATA); @@ -6360,6 +6361,82 @@ visitor_->OnAckNeedsRetransmittableFrame(); } +bool QuicConnection::ShouldClientSuppressInitialAck() const { + // Only clients discard INITIAL keys upon sending a HANDSHAKE packet + // (RFC 9001 Section 4.9.1); servers discard INITIAL keys upon receiving one. + if (perspective_ != Perspective::IS_CLIENT) { + return false; + } + // If a HANDSHAKE packet has already been sent, or is currently buffered in + // packet_creator_ and will be flushed before ~ScopedPacketFlusher() calls + // OnHandshakePacketSent(), suppress the INITIAL ACK to avoid sending a + // 1200-byte padded INITIAL packet right before dropping INITIAL keys. + return handshake_packet_sent_ || + (packet_creator_.HasPendingFrames() && + packet_creator_.encryption_level() == ENCRYPTION_HANDSHAKE); +} + +void QuicConnection::MaybeSendPendingHandshakeAcks() { + if (!connected_ || !send_acks_after_on_can_write_ || IsHandshakeConfirmed() || + defer_ack_only_packets_) { + return; + } + QUIC_CODE_COUNT_N(quic_saow_handshake_acks, 1, 4); + const QuicTime now = clock_->ApproximateNow(); + auto should_send_ack = [&](PacketNumberSpace space) { + const QuicTime ack_timeout = + uber_received_packet_manager_.GetAckTimeout(space); + return ack_timeout.IsInitialized() && ack_timeout <= now && + framer_.HasAnEncrypterForSpace(space); + }; + bool updated_ack_state = false; + // On clients, process HANDSHAKE_DATA before INITIAL_DATA so that an INITIAL + // ACK is only suppressed after a HANDSHAKE packet has actually been sent or + // queued (and not if flushing the HANDSHAKE ACK fails, e.g., when blocked). + const std::array<PacketNumberSpace, 2> spaces = + perspective_ == Perspective::IS_CLIENT + ? std::array<PacketNumberSpace, 2>{HANDSHAKE_DATA, INITIAL_DATA} + : std::array<PacketNumberSpace, 2>{INITIAL_DATA, HANDSHAKE_DATA}; + for (PacketNumberSpace space : spaces) { + if (!connected_) { + break; + } + if (space == INITIAL_DATA && + uber_received_packet_manager_.GetAckTimeout(INITIAL_DATA) + .IsInitialized() && + ShouldClientSuppressInitialAck()) { + QUIC_CODE_COUNT_N(quic_saow_handshake_acks, 2, 4); + uber_received_packet_manager_.ResetAckStates(ENCRYPTION_INITIAL); + updated_ack_state = true; + continue; + } + if (!should_send_ack(space)) { + continue; + } + ScopedEncryptionLevelContext context( + this, QuicUtils::GetEncryptionLevelToSendAckofSpace(space)); + QuicFrames frames = { + uber_received_packet_manager_.GetUpdatedAckFrame(space, now)}; + if (!packet_creator_.FlushAckFrame(frames) || !connected_) { + break; + } + ResetAckStates(); + if (space == INITIAL_DATA) { + QUIC_CODE_COUNT_N(quic_saow_handshake_acks, 3, 4); + } else { + QUIC_CODE_COUNT_N(quic_saow_handshake_acks, 4, 4); + } + updated_ack_state = true; + } + if (updated_ack_state) { + QUIC_RELOADABLE_FLAG_COUNT_N(quic_send_acks_after_on_can_write2, 2, 2); + if (connected_) { + ack_alarm().Update(uber_received_packet_manager_.GetEarliestAckTimeout(), + kAlarmGranularity); + } + } +} + bool QuicConnection::ShouldBundleRetransmittableFrameWithAck() const { if (consecutive_num_packets_with_no_retransmittable_frames_ >= max_consecutive_num_packets_with_no_retransmittable_frames_) {
diff --git a/quiche/quic/core/quic_connection.h b/quiche/quic/core/quic_connection.h index 921c7bc..bff4cfd 100644 --- a/quiche/quic/core/quic_connection.h +++ b/quiche/quic/core/quic_connection.h
@@ -1553,6 +1553,16 @@ return packet_creator_.bundle_crypto_with_other_frames(); } + bool send_acks_after_on_can_write() const { + return send_acks_after_on_can_write_; + } + + // Sends any INITIAL or HANDSHAKE ACKs whose ACK timeout has expired so they + // can be coalesced ahead of control or application frames. No-op when + // ACK-only packets are deferred (ScopedAckDeferrer). Caller must hold an + // active ScopedPacketFlusher. + void MaybeSendPendingHandshakeAcks(); + void OnDiscardZeroRttDecryptionKeysAlarm() override; void OnIdleDetectorAlarm() override; void OnNetworkBlackholeDetectorAlarm() override; @@ -2042,6 +2052,11 @@ // Returns true if the ACK frame should be bundled with ACK-eliciting frame. bool ShouldBundleRetransmittableFrameWithAck() const; + // Returns true if a client INITIAL ACK should be suppressed because a + // HANDSHAKE packet has already been sent or is queued in packet_creator_ + // (dropping INITIAL keys in ~ScopedPacketFlusher()). + bool ShouldClientSuppressInitialAck() const; + // Called to update ACK timeout when an retransmittable frame has been parsed. void MaybeUpdateAckTimeout();
diff --git a/quiche/quic/core/quic_connection_test.cc b/quiche/quic/core/quic_connection_test.cc index 70a9714..c3b658a 100644 --- a/quiche/quic/core/quic_connection_test.cc +++ b/quiche/quic/core/quic_connection_test.cc
@@ -1599,6 +1599,32 @@ connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); } + void EnableSendAcksAfterOnCanWrite( + Perspective perspective = Perspective::IS_SERVER, + QuicTagVector extra_copts = {}) { + SetQuicReloadableFlag(quic_send_acks_after_on_can_write2, true); + QuicTagVector copts = {kSAOW}; + copts.insert(copts.end(), extra_copts.begin(), extra_copts.end()); + QuicConfig config; + if (perspective == Perspective::IS_SERVER) { + set_perspective(Perspective::IS_SERVER); + QuicConfigPeer::SetReceivedConnectionOptions(&config, copts); + } else { + config.SetConnectionOptionsToSend(copts); + } + EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); + EXPECT_CALL(*send_algorithm_, EnableECT1()).WillOnce(Return(false)); + EXPECT_CALL(*send_algorithm_, EnableECT0()).WillOnce(Return(false)); + connection_.SetFromConfig(config); + connection_.SetEncrypter( + ENCRYPTION_HANDSHAKE, + std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); + connection_.SetEncrypter( + ENCRYPTION_FORWARD_SECURE, + std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); + connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); + } + void TestClientRetryHandling(bool invalid_retry_tag, bool missing_original_id_in_config, bool wrong_original_id_in_config, @@ -20779,7 +20805,7 @@ if (!connection_.SupportsMultiplePacketNumberSpaces()) { return; } - SetQuicReloadableFlag(quic_send_acks_after_on_can_write, false); + SetQuicReloadableFlag(quic_send_acks_after_on_can_write2, false); QuicConfig config; config.SetConnectionOptionsToSend({kSAOW}); EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); @@ -20815,7 +20841,7 @@ if (!connection_.SupportsMultiplePacketNumberSpaces()) { return; } - SetQuicReloadableFlag(quic_send_acks_after_on_can_write, true); + SetQuicReloadableFlag(quic_send_acks_after_on_can_write2, true); QuicConfig config; config.SetConnectionOptionsToSend({kSAOW}); EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); @@ -21835,7 +21861,7 @@ if (!connection_.SupportsMultiplePacketNumberSpaces()) { return; } - SetQuicReloadableFlag(quic_send_acks_after_on_can_write, true); + SetQuicReloadableFlag(quic_send_acks_after_on_can_write2, true); set_perspective(Perspective::IS_SERVER); // QuicDispatcher allows deferring ACK-only packets when the client sends // SDAO. @@ -21945,6 +21971,362 @@ EXPECT_TRUE(connection_.CanWrite(HAS_RETRANSMITTABLE_DATA)); } +TEST_P(QuicConnectionTest, MaybeSendPendingHandshakeAcks) { + if (!connection_.SupportsMultiplePacketNumberSpaces()) { + return; + } + EnableSendAcksAfterOnCanWrite(); + connection_.RemoveEncrypter(ENCRYPTION_INITIAL); + + // Receive a single HANDSHAKE packet, which schedules a delayed ACK after + // kAlarmGranularity (1ms). + EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); + QuicCryptoFrame crypto_frame(ENCRYPTION_HANDSHAKE, 0, "handshake"); + ProcessFramePacketAtLevel(1, QuicFrame(&crypto_frame), ENCRYPTION_HANDSHAKE); + EXPECT_TRUE(connection_.HasPendingAcks()); + + // Unexpired delayed ACKs should remain scheduled on the ACK alarm. + { + QuicConnection::ScopedPacketFlusher flusher(&connection_); + connection_.MaybeSendPendingHandshakeAcks(); + } + EXPECT_EQ(0u, writer_->packets_write_attempts()); + EXPECT_TRUE(connection_.HasPendingAcks()); + + clock_.AdvanceTime(kAlarmGranularity); + EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); + EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); + { + QuicConnection::ScopedPacketFlusher flusher(&connection_); + connection_.MaybeSendPendingHandshakeAcks(); + // The HANDSHAKE ACK is written before any 1-RTT frames, rather than being + // left for ~ScopedPacketFlusher(). + EXPECT_FALSE(connection_.HasPendingAcks()); + connection_.SendControlFrame(QuicFrame(QuicHandshakeDoneFrame(1))); + } + // Both the HANDSHAKE ACK and 1-RTT HANDSHAKE_DONE should be coalesced into a + // single UDP packet. + EXPECT_EQ(1u, writer_->packets_write_attempts()); + EXPECT_FALSE(connection_.HasPendingAcks()); +} + +TEST_P(QuicConnectionTest, MaybeSendPendingHandshakeAcksSendsServerInitialAck) { + if (!connection_.SupportsMultiplePacketNumberSpaces()) { + return; + } + EnableSendAcksAfterOnCanWrite(); + EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); + ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); + EXPECT_TRUE(connection_.HasPendingAcks()); + + // Unlike the client, the server keeps its INITIAL keys after moving to a + // higher encryption level, so a due INITIAL ACK is still sent. + clock_.AdvanceTime(DefaultDelayedAckTime()); + EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); + { + QuicConnection::ScopedPacketFlusher flusher(&connection_); + connection_.MaybeSendPendingHandshakeAcks(); + EXPECT_FALSE(connection_.HasPendingAcks()); + } + EXPECT_EQ(1u, writer_->packets_write_attempts()); +} + +TEST_P(QuicConnectionTest, + MaybeSendPendingHandshakeAcksSuppressesClientInitialAckBeforeKeyDrop) { + if (!connection_.SupportsMultiplePacketNumberSpaces()) { + return; + } + EnableSendAcksAfterOnCanWrite(Perspective::IS_CLIENT); + EXPECT_TRUE( + connection_.framer().HasEncrypterOfEncryptionLevel(ENCRYPTION_INITIAL)); + + EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); + QuicCryptoFrame crypto_frame(ENCRYPTION_INITIAL, 0, "initial"); + ProcessFramePacketAtLevel(1, QuicFrame(&crypto_frame), ENCRYPTION_INITIAL); + EXPECT_TRUE(connection_.HasPendingAcks()); + + clock_.AdvanceTime(DefaultDelayedAckTime()); + EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); + EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); + { + QuicConnection::ScopedPacketFlusher flusher(&connection_); + connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); + connection_.SendCryptoDataWithString("handshake", 0, ENCRYPTION_HANDSHAKE); + connection_.MaybeSendPendingHandshakeAcks(); + } + EXPECT_EQ(1u, writer_->packets_write_attempts()); + EXPECT_EQ(ENCRYPTION_HANDSHAKE, connection_.sent_packet_manager() + .unacked_packets() + .GetTransmissionInfo(QuicPacketNumber(1)) + .encryption_level); + EXPECT_FALSE(connection_.HasPendingAcks()); +} + +TEST_P(QuicConnectionTest, + MaybeSendPendingHandshakeAcksDropsClientInitialAckWhenHandshakeAckDue) { + if (!connection_.SupportsMultiplePacketNumberSpaces()) { + return; + } + EnableSendAcksAfterOnCanWrite(Perspective::IS_CLIENT); + EXPECT_TRUE( + connection_.framer().HasEncrypterOfEncryptionLevel(ENCRYPTION_INITIAL)); + + EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(2); + QuicCryptoFrame initial_crypto(ENCRYPTION_INITIAL, 0, "initial"); + ProcessFramePacketAtLevel(1, QuicFrame(&initial_crypto), ENCRYPTION_INITIAL); + QuicCryptoFrame handshake_crypto(ENCRYPTION_HANDSHAKE, 0, "handshake"); + ProcessFramePacketAtLevel(2, QuicFrame(&handshake_crypto), + ENCRYPTION_HANDSHAKE); + EXPECT_TRUE(connection_.HasPendingAcks()); + + clock_.AdvanceTime(DefaultDelayedAckTime()); + EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); + EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); + { + QuicConnection::ScopedPacketFlusher flusher(&connection_); + connection_.MaybeSendPendingHandshakeAcks(); + } + EXPECT_EQ(1u, writer_->packets_write_attempts()); + EXPECT_LT(writer_->last_packet_size(), kDefaultMaxPacketSize); + EXPECT_EQ(ENCRYPTION_HANDSHAKE, connection_.sent_packet_manager() + .unacked_packets() + .GetTransmissionInfo(QuicPacketNumber(1)) + .encryption_level); + EXPECT_FALSE(connection_.HasPendingAcks()); +} + +TEST_P( + QuicConnectionTest, + MaybeSendPendingHandshakeAcksSendsClientInitialAckWithoutHandshakePacket) { + if (!connection_.SupportsMultiplePacketNumberSpaces()) { + return; + } + EnableSendAcksAfterOnCanWrite(Perspective::IS_CLIENT); + EXPECT_TRUE( + connection_.framer().HasEncrypterOfEncryptionLevel(ENCRYPTION_INITIAL)); + + EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); + QuicCryptoFrame crypto_frame(ENCRYPTION_INITIAL, 0, "initial"); + ProcessFramePacketAtLevel(1, QuicFrame(&crypto_frame), ENCRYPTION_INITIAL); + EXPECT_TRUE(connection_.HasPendingAcks()); + + // ENCRYPTION_FORWARD_SECURE alone without a sent, pending, or due HANDSHAKE + // packet does not drop INITIAL keys, so the due INITIAL ACK must be sent. + clock_.AdvanceTime(DefaultDelayedAckTime()); + EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(0); + EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); + { + QuicConnection::ScopedPacketFlusher flusher(&connection_); + connection_.MaybeSendPendingHandshakeAcks(); + } + EXPECT_EQ(1u, writer_->packets_write_attempts()); + EXPECT_EQ(ENCRYPTION_INITIAL, connection_.sent_packet_manager() + .unacked_packets() + .GetTransmissionInfo(QuicPacketNumber(1)) + .encryption_level); + EXPECT_FALSE(connection_.HasPendingAcks()); +} + +TEST_P(QuicConnectionTest, + MaybeSendPendingHandshakeAcksDefersAckOnlyUnderScopedAckDeferrer) { + if (!connection_.SupportsMultiplePacketNumberSpaces()) { + return; + } + EnableSendAcksAfterOnCanWrite(Perspective::IS_CLIENT, {kDAOP}); + connection_.RemoveEncrypter(ENCRYPTION_INITIAL); + + EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(2); + EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); + { + QuicConnection::ScopedAckDeferrer ack_deferrer(&connection_); + QuicCryptoFrame crypto1(ENCRYPTION_HANDSHAKE, 0, "hs1"); + ProcessFramePacketAtLevel(1, QuicFrame(&crypto1), ENCRYPTION_HANDSHAKE); + QuicCryptoFrame crypto2(ENCRYPTION_HANDSHAKE, 3, "hs2"); + ProcessFramePacketAtLevel(2, QuicFrame(&crypto2), ENCRYPTION_HANDSHAKE); + EXPECT_EQ(0u, writer_->packets_write_attempts()); + EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); + + // MaybeSendPendingHandshakeAcks should leave due ACKs deferred while + // ScopedAckDeferrer is active. + { + QuicConnection::ScopedPacketFlusher flusher(&connection_); + connection_.MaybeSendPendingHandshakeAcks(); + } + EXPECT_EQ(0u, writer_->packets_write_attempts()); + EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); + } + // Exiting ScopedAckDeferrer sends the deferred HANDSHAKE ACK. + EXPECT_EQ(1u, writer_->packets_write_attempts()); + ASSERT_EQ(1u, writer_->ack_frames().size()); + EXPECT_EQ(QuicPacketNumber(2), LargestAcked(writer_->ack_frames()[0])); + EXPECT_FALSE(connection_.HasPendingAcks()); +} + +TEST_P(QuicConnectionTest, MaybeSendPendingHandshakeAcksNoOpWhenDisabled) { + if (!connection_.SupportsMultiplePacketNumberSpaces()) { + return; + } + SetQuicReloadableFlag(quic_send_acks_after_on_can_write2, false); + connection_.RemoveEncrypter(ENCRYPTION_INITIAL); + connection_.SetEncrypter( + ENCRYPTION_HANDSHAKE, + std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); + connection_.SetEncrypter( + ENCRYPTION_FORWARD_SECURE, + std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); + connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); + + EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); + QuicCryptoFrame crypto_frame(ENCRYPTION_HANDSHAKE, 0, "handshake"); + ProcessFramePacketAtLevel(1, QuicFrame(&crypto_frame), ENCRYPTION_HANDSHAKE); + clock_.AdvanceTime(kAlarmGranularity); + EXPECT_TRUE(connection_.HasPendingAcks()); + + EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); + { + QuicConnection::ScopedPacketFlusher flusher(&connection_); + connection_.MaybeSendPendingHandshakeAcks(); + // MaybeSendPendingHandshakeAcks() should not flush the due HANDSHAKE ACK + // when SAOW is disabled. + EXPECT_EQ(0u, writer_->packets_write_attempts()); + EXPECT_TRUE(connection_.HasPendingAcks()); + } +} + +TEST_P( + QuicConnectionTest, + MaybeSendPendingHandshakeAcksSuppressesClientInitialAckWithPendingFrame) { + if (!connection_.SupportsMultiplePacketNumberSpaces()) { + return; + } + EnableSendAcksAfterOnCanWrite(Perspective::IS_CLIENT); + + EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); + QuicCryptoFrame crypto_frame(ENCRYPTION_INITIAL, 0, "initial"); + ProcessFramePacketAtLevel(1, QuicFrame(&crypto_frame), ENCRYPTION_INITIAL); + EXPECT_TRUE(connection_.HasPendingAcks()); + + clock_.AdvanceTime(DefaultDelayedAckTime()); + EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); + EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); + { + QuicConnection::ScopedPacketFlusher flusher(&connection_); + QuicConnection::ScopedEncryptionLevelContext context(&connection_, + ENCRYPTION_HANDSHAKE); + ASSERT_TRUE( + creator_->AddFrame(QuicFrame(QuicPingFrame()), NOT_RETRANSMISSION)); + ASSERT_TRUE(connection_.packet_creator().HasPendingFrames()); + ASSERT_EQ(ENCRYPTION_HANDSHAKE, + connection_.packet_creator().encryption_level()); + connection_.MaybeSendPendingHandshakeAcks(); + } + EXPECT_EQ(1u, writer_->packets_write_attempts()); + EXPECT_EQ(ENCRYPTION_HANDSHAKE, connection_.sent_packet_manager() + .unacked_packets() + .GetTransmissionInfo(QuicPacketNumber(1)) + .encryption_level); + EXPECT_FALSE(connection_.HasPendingAcks()); +} + +TEST_P( + QuicConnectionTest, + MaybeSendPendingHandshakeAcksSendsClientInitialAckWithPendingInitialFrame) { + if (!connection_.SupportsMultiplePacketNumberSpaces()) { + return; + } + EnableSendAcksAfterOnCanWrite(Perspective::IS_CLIENT); + EXPECT_TRUE( + connection_.framer().HasEncrypterOfEncryptionLevel(ENCRYPTION_INITIAL)); + + EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); + QuicCryptoFrame crypto_frame(ENCRYPTION_INITIAL, 0, "initial"); + ProcessFramePacketAtLevel(1, QuicFrame(&crypto_frame), ENCRYPTION_INITIAL); + EXPECT_TRUE(connection_.HasPendingAcks()); + + clock_.AdvanceTime(DefaultDelayedAckTime()); + EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(0); + EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); + { + QuicConnection::ScopedPacketFlusher flusher(&connection_); + QuicConnection::ScopedEncryptionLevelContext context(&connection_, + ENCRYPTION_INITIAL); + ASSERT_TRUE( + creator_->AddFrame(QuicFrame(QuicPingFrame()), NOT_RETRANSMISSION)); + ASSERT_TRUE(connection_.packet_creator().HasPendingFrames()); + ASSERT_EQ(ENCRYPTION_INITIAL, + connection_.packet_creator().encryption_level()); + ASSERT_TRUE(connection_.HasPendingAcks()); + connection_.MaybeSendPendingHandshakeAcks(); + EXPECT_FALSE(connection_.HasPendingAcks()); + } + EXPECT_EQ(1u, writer_->packets_write_attempts()); + EXPECT_EQ(ENCRYPTION_INITIAL, connection_.sent_packet_manager() + .unacked_packets() + .GetTransmissionInfo(QuicPacketNumber(1)) + .encryption_level); + ASSERT_EQ(1u, writer_->ack_frames().size()); + EXPECT_EQ(QuicPacketNumber(1), LargestAcked(writer_->ack_frames()[0])); + EXPECT_FALSE(connection_.HasPendingAcks()); +} + +TEST_P(QuicConnectionTest, + MaybeSendPendingHandshakeAcksKeepsInitialAckWhenBlocked) { + if (!connection_.SupportsMultiplePacketNumberSpaces()) { + return; + } + EnableSendAcksAfterOnCanWrite(Perspective::IS_CLIENT); + EXPECT_TRUE( + connection_.framer().HasEncrypterOfEncryptionLevel(ENCRYPTION_INITIAL)); + + EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(2); + QuicCryptoFrame initial_crypto(ENCRYPTION_INITIAL, 0, "initial"); + ProcessFramePacketAtLevel(1, QuicFrame(&initial_crypto), ENCRYPTION_INITIAL); + QuicCryptoFrame handshake_crypto(ENCRYPTION_HANDSHAKE, 0, "handshake"); + ProcessFramePacketAtLevel(2, QuicFrame(&handshake_crypto), + ENCRYPTION_HANDSHAKE); + const QuicTime initial_ack_timeout = + connection_.received_packet_manager().GetAckTimeout(INITIAL_DATA); + const QuicTime handshake_ack_timeout = + connection_.received_packet_manager().GetAckTimeout(HANDSHAKE_DATA); + ASSERT_TRUE(initial_ack_timeout.IsInitialized()); + ASSERT_TRUE(handshake_ack_timeout.IsInitialized()); + + clock_.AdvanceTime(DefaultDelayedAckTime()); + writer_->SetWriteBlocked(); + EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(1)); + EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(0); + { + QuicConnection::ScopedPacketFlusher flusher(&connection_); + connection_.MaybeSendPendingHandshakeAcks(); + } + // Flushing the HANDSHAKE ACK failed while write-blocked, so INITIAL ACK state + // must remain intact and INITIAL keys must not be discarded (even though + // ~ScopedPacketFlusher() cancels the ACK alarm while write-blocked). + EXPECT_EQ(0u, writer_->packets_write_attempts()); + EXPECT_TRUE(connection_.received_packet_manager().IsAckFrameUpdated()); + EXPECT_EQ(initial_ack_timeout, + connection_.received_packet_manager().GetAckTimeout(INITIAL_DATA)); + EXPECT_EQ( + handshake_ack_timeout, + connection_.received_packet_manager().GetAckTimeout(HANDSHAKE_DATA)); + + // Once writable, flushing the HANDSHAKE ACK succeeds and suppresses the + // INITIAL ACK. + writer_->SetWritable(); + EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); + EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); + { + QuicConnection::ScopedPacketFlusher flusher(&connection_); + connection_.MaybeSendPendingHandshakeAcks(); + } + EXPECT_EQ(1u, writer_->packets_write_attempts()); + EXPECT_EQ(ENCRYPTION_HANDSHAKE, connection_.sent_packet_manager() + .unacked_packets() + .GetTransmissionInfo(QuicPacketNumber(1)) + .encryption_level); + EXPECT_FALSE(connection_.HasPendingAcks()); +} + } // namespace } // namespace test } // namespace quic
diff --git a/quiche/quic/core/quic_packet_creator.h b/quiche/quic/core/quic_packet_creator.h index 0399f5a..44089fa 100644 --- a/quiche/quic/core/quic_packet_creator.h +++ b/quiche/quic/core/quic_packet_creator.h
@@ -335,7 +335,7 @@ // Sets the encryption level that will be applied to new packets. void set_encryption_level(EncryptionLevel level); - EncryptionLevel encryption_level() { return packet_.encryption_level; } + EncryptionLevel encryption_level() const { return packet_.encryption_level; } // packet number of the last created packet, or 0 if no packets have been // created.
diff --git a/quiche/quic/core/quic_session.cc b/quiche/quic/core/quic_session.cc index 9c403e7..c15529e 100644 --- a/quiche/quic/core/quic_session.cc +++ b/quiche/quic/core/quic_session.cc
@@ -877,6 +877,12 @@ // write blocked or closed. return; } + if (connection_->send_acks_after_on_can_write()) { + connection_->MaybeSendPendingHandshakeAcks(); + if (!connection_->connected()) { + return; + } + } } if (control_frame_manager_.WillingToWrite()) { control_frame_manager_.OnCanWrite();
diff --git a/quiche/quic/core/quic_session_test.cc b/quiche/quic/core/quic_session_test.cc index c502e3d..25f0331 100644 --- a/quiche/quic/core/quic_session_test.cc +++ b/quiche/quic/core/quic_session_test.cc
@@ -4027,6 +4027,186 @@ EXPECT_FALSE(session_.prioritize_handshake_frames()); } +TEST_P(QuicSessionTestServer, + OnCanWriteFlushesPendingHandshakeAcksAfterCryptoAndBeforeControl) { + if (!VersionIsIetfQuic(transport_version())) { + return; + } + SetQuicReloadableFlag(quic_send_acks_after_on_can_write2, true); + QuicConfigPeer::SetReceivedConnectionOptions(session_.config(), {kSAOW}); + connection_->SetFromConfig(*session_.config()); + ASSERT_TRUE(connection_->send_acks_after_on_can_write()); + + connection_->SetEncrypter( + ENCRYPTION_INITIAL, + std::make_unique<TaggingEncrypter>(ENCRYPTION_INITIAL)); + connection_->SetEncrypter( + ENCRYPTION_HANDSHAKE, + std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); + connection_->SetEncrypter( + ENCRYPTION_FORWARD_SECURE, + std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); + session_.GetMutableCryptoStream()->EstablishZeroRttEncryption(); + connection_->SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); + QuicConnectionPeer::SetAddressValidated(connection_); + + // Schedule due INITIAL and HANDSHAKE ACKs. + const QuicTime now = connection_->clock()->ApproximateNow(); + QuicPacketHeader initial_header; + initial_header.packet_number = QuicPacketNumber(1); + connection_->received_packet_manager().RecordPacketReceived( + ENCRYPTION_INITIAL, initial_header, now, ECN_NOT_ECT, QuicTime::Zero()); + connection_->received_packet_manager().MaybeUpdateAckTimeout( + /*should_last_packet_instigate_acks=*/true, ENCRYPTION_INITIAL, + QuicPacketNumber(1), now, now, + connection_->sent_packet_manager().GetRttStats()); + + QuicPacketHeader handshake_header; + handshake_header.packet_number = QuicPacketNumber(1); + connection_->received_packet_manager().RecordPacketReceived( + ENCRYPTION_HANDSHAKE, handshake_header, now, ECN_NOT_ECT, + QuicTime::Zero()); + connection_->received_packet_manager().MaybeUpdateAckTimeout( + /*should_last_packet_instigate_acks=*/true, ENCRYPTION_HANDSHAKE, + QuicPacketNumber(1), now, now, + connection_->sent_packet_manager().GetRttStats()); + QuicConnectionPeer::GetAckAlarm(connection_) + .Update(connection_->received_packet_manager().GetEarliestAckTimeout(), + kAlarmGranularity); + connection_->AdvanceTime( + QuicTime::Delta::FromMilliseconds(GetDefaultDelayedAckTimeMs())); + ASSERT_TRUE(connection_->HasPendingAcks()); + + // Buffer a CRYPTO frame, a control frame, and a write-blocked stream. + TestCryptoStream* crypto_stream = session_.GetMutableCryptoStream(); + std::string crypto_data(1350, 'a'); + EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_HANDSHAKE, 1350, 0)) + .WillOnce(Return(1000)); + crypto_stream->WriteCryptoData(ENCRYPTION_HANDSHAKE, crypto_data); + ASSERT_TRUE(crypto_stream->HasBufferedCryptoFrames()); + + TestStream* stream = session_.CreateOutgoingBidirectionalStream(); + EXPECT_CALL(*connection_, SendControlFrame(_)).WillOnce(Return(false)); + session_.SendWindowUpdate(stream->id(), 100); + ASSERT_TRUE(session_.control_frame_manager().WillingToWrite()); + session_.MarkConnectionLevelWriteBlocked(stream->id()); + + MockPacketWriter* writer = static_cast<MockPacketWriter*>( + QuicConnectionPeer::GetWriter(connection_)); + { + InSequence s; + EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_HANDSHAKE, 350, 1000)) + .WillOnce([this]() { + // Due INITIAL/HANDSHAKE ACKs are not flushed until after + // WriteBufferedCryptoFrames() completes. + EXPECT_TRUE(connection_->HasPendingAcks()); + return 350; + }); + EXPECT_CALL(*connection_, SendControlFrame(_)) + .WillOnce([this](const QuicFrame& frame) { + // Due INITIAL/HANDSHAKE ACKs are flushed into the coalesced packet + // before control_frame_manager_.OnCanWrite(). + EXPECT_FALSE(connection_->HasPendingAcks()); + EXPECT_TRUE(QuicConnectionPeer::GetCoalescedPacket(connection_) + .ContainsPacketOfEncryptionLevel(ENCRYPTION_INITIAL)); + EXPECT_TRUE( + QuicConnectionPeer::GetCoalescedPacket(connection_) + .ContainsPacketOfEncryptionLevel(ENCRYPTION_HANDSHAKE)); + return ClearControlFrame(frame); + }); + EXPECT_CALL(*stream, OnCanWrite()).WillOnce([this]() { + EXPECT_FALSE(connection_->HasPendingAcks()); + }); + EXPECT_CALL(*writer, WritePacket(_, _, _, _, _, _)) + .WillOnce(Return(WriteResult(WRITE_STATUS_OK, 0))); + } + + { + // Hold an outer ScopedPacketFlusher (as QuicConnection::OnCanWrite() does) + // so RetransmitLostData()'s inner flusher does not flush ACKs early. + QuicConnection::ScopedPacketFlusher flusher(connection_); + session_.OnCanWrite(); + } + EXPECT_FALSE(crypto_stream->HasBufferedCryptoFrames()); + EXPECT_FALSE(session_.control_frame_manager().WillingToWrite()); + EXPECT_FALSE(connection_->HasPendingAcks()); +} + +TEST_P(QuicSessionTestServer, + OnCanWriteStopsWhenMaybeSendPendingHandshakeAcksClosesConnection) { + if (!VersionIsIetfQuic(transport_version())) { + return; + } + SetQuicReloadableFlag(quic_send_acks_after_on_can_write2, true); + QuicConfigPeer::SetReceivedConnectionOptions(session_.config(), {kSAOW}); + connection_->SetFromConfig(*session_.config()); + ASSERT_TRUE(connection_->send_acks_after_on_can_write()); + + connection_->SetEncrypter( + ENCRYPTION_HANDSHAKE, + std::make_unique<TaggingEncrypter>(ENCRYPTION_HANDSHAKE)); + connection_->SetEncrypter( + ENCRYPTION_FORWARD_SECURE, + std::make_unique<TaggingEncrypter>(ENCRYPTION_FORWARD_SECURE)); + session_.GetMutableCryptoStream()->EstablishZeroRttEncryption(); + connection_->SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); + QuicConnectionPeer::SetAddressValidated(connection_); + + // Buffer a control frame and mark a stream write-blocked. Neither should run + // if MaybeSendPendingHandshakeAcks() closes the connection. + TestStream* stream = session_.CreateOutgoingBidirectionalStream(); + EXPECT_CALL(*connection_, SendControlFrame(_)).WillOnce(Return(false)); + session_.SendWindowUpdate(stream->id(), 100); + ASSERT_TRUE(session_.control_frame_manager().WillingToWrite()); + session_.MarkConnectionLevelWriteBlocked(stream->id()); + + MockPacketWriter* writer = static_cast<MockPacketWriter*>( + QuicConnectionPeer::GetWriter(connection_)); + { + // Hold an outer ScopedPacketFlusher (as QuicConnection::OnCanWrite() does) + // and pre-populate the coalesced packet with an ENCRYPTION_HANDSHAKE + // packet so flushing a second ENCRYPTION_HANDSHAKE packet (the ACK) + // triggers FlushCoalescedPacket() -> writer_->WritePacket(). + QuicConnection::ScopedPacketFlusher flusher(connection_); + EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_HANDSHAKE, 9, 0)) + .WillOnce(Invoke(connection_, + &MockQuicConnection::QuicConnection_SendCryptoData)); + session_.GetMutableCryptoStream()->WriteCryptoData(ENCRYPTION_HANDSHAKE, + "handshake"); + ASSERT_TRUE(QuicConnectionPeer::GetCoalescedPacket(connection_) + .ContainsPacketOfEncryptionLevel(ENCRYPTION_HANDSHAKE)); + + const QuicTime now = connection_->clock()->ApproximateNow(); + QuicPacketHeader handshake_header; + handshake_header.packet_number = QuicPacketNumber(1); + connection_->received_packet_manager().RecordPacketReceived( + ENCRYPTION_HANDSHAKE, handshake_header, now, ECN_NOT_ECT, + QuicTime::Zero()); + connection_->received_packet_manager().MaybeUpdateAckTimeout( + /*should_last_packet_instigate_acks=*/true, ENCRYPTION_HANDSHAKE, + QuicPacketNumber(1), now, now, + connection_->sent_packet_manager().GetRttStats()); + QuicConnectionPeer::GetAckAlarm(connection_) + .Update(connection_->received_packet_manager().GetEarliestAckTimeout(), + kAlarmGranularity); + connection_->AdvanceTime(kAlarmGranularity); + ASSERT_TRUE(connection_->HasPendingAcks()); + + EXPECT_CALL(*writer, WritePacket(_, _, _, _, _, _)) + .WillOnce(Return(WriteResult(WRITE_STATUS_ERROR, 1))); + EXPECT_CALL(*connection_, CloseConnection(QUIC_PACKET_WRITE_ERROR, _, _)) + .WillOnce( + Invoke(connection_, &MockQuicConnection::ReallyCloseConnection)); + EXPECT_CALL(session_visitor_, OnConnectionClosed(_, _, _, _)); + EXPECT_CALL(*connection_, SendControlFrame(_)).Times(0); + EXPECT_CALL(*stream, OnCanWrite()).Times(0); + + session_.OnCanWrite(); + } + EXPECT_FALSE(connection_->connected()); + EXPECT_TRUE(session_.control_frame_manager().WillingToWrite()); +} + } // namespace } // namespace test } // namespace quic