Allow sending up to two probe packets on PTO during the QUIC handshake with the PTO2 connection option.

When a multi-packet `INITIAL` or `HANDSHAKE` flight (such as a server `HANDSHAKE` flight spanning two packets or a post-quantum `ClientHello`) experienced tail loss of more than one packet, a single PTO probe required multiple consecutive PTO round trips to recover the flight.

Protected by quic_reloadable_flag_quic_handshake_pto_two_packets.

PiperOrigin-RevId: 1014668399
diff --git a/quiche/common/quiche_feature_flags_list.h b/quiche/common/quiche_feature_flags_list.h
index e9c5791..6475bae 100755
--- a/quiche/common/quiche_feature_flags_list.h
+++ b/quiche/common/quiche_feature_flags_list.h
@@ -46,6 +46,7 @@
 QUICHE_FLAG(bool, quiche_reloadable_flag_quic_fin_before_completed_http_headers, false, true, "If true, close the connection with error if FIN is received before finish receiving the whole HTTP headers.")
 QUICHE_FLAG(bool, quiche_reloadable_flag_quic_fix_mtu_discovery, false, true, "If true, fix a externally reported bug in QUIC MTU discovery.")
 QUICHE_FLAG(bool, quiche_reloadable_flag_quic_fix_timeouts, true, true, "If true, postpone setting handshake timeout to infinite to handshake complete.")
+QUICHE_FLAG(bool, quiche_reloadable_flag_quic_handshake_pto_two_packets, false, false, "When true, send up to two probe packets on PTO during the IETF QUIC handshake.")
 QUICHE_FLAG(bool, quiche_reloadable_flag_quic_include_datagrams_in_willing_to_write, false, false, "If true, checks for queued datagrams when determining if a connection is willing to write.")
 QUICHE_FLAG(bool, quiche_reloadable_flag_quic_maybe_copy_datagram_frames, false, true, "If true, maybe copy datagram frames in QuicUnackedPacketMap.")
 QUICHE_FLAG(bool, quiche_reloadable_flag_quic_move_clock_now, false, false, "If true, move the call to clock.Now() in QuicPacketReader::ReadAndDispatchPackets to after socket_api_.ReadMultiplePackets().")
diff --git a/quiche/quic/core/crypto/crypto_protocol.h b/quiche/quic/core/crypto/crypto_protocol.h
index 4a99048..f03f109 100644
--- a/quiche/quic/core/crypto/crypto_protocol.h
+++ b/quiche/quic/core/crypto/crypto_protocol.h
@@ -261,6 +261,7 @@
 
 DEFINE_STATIC_QUIC_TAG(1PTO);  // Send 1 packet upon PTO.
 DEFINE_STATIC_QUIC_TAG(2PTO);  // Send 2 packets upon PTO.
+DEFINE_STATIC_QUIC_TAG(PTO2);  // Send 2 packets upon PTO during handshake.
 
 DEFINE_STATIC_QUIC_TAG(6PTO);  // Closes connection on 6
                                // consecutive PTOs.
diff --git a/quiche/quic/core/quic_sent_packet_manager.cc b/quiche/quic/core/quic_sent_packet_manager.cc
index ba2cff1..40e853a 100644
--- a/quiche/quic/core/quic_sent_packet_manager.cc
+++ b/quiche/quic/core/quic_sent_packet_manager.cc
@@ -236,6 +236,10 @@
   if (config.HasClientSentConnectionOption(kRNIB, perspective)) {
     pacing_sender_.set_remove_non_initial_burst();
   }
+  if (GetQuicReloadableFlag(quic_handshake_pto_two_packets) &&
+      config.HasClientSentConnectionOption(kPTO2, perspective)) {
+    handshake_pto_two_packets_ = true;
+  }
   // Path degradation experiments
   if (config.HasClientRequestedIndependentOption(kPDE2, perspective)) {
     num_ptos_for_path_degrading_ = 2;
@@ -850,6 +854,11 @@
   return GetRetransmissionMode();
 }
 
+bool QuicSentPacketManager::ShouldSendTwoHandshakeProbes() const {
+  return handshake_pto_two_packets_ && handshake_mode_disabled_ &&
+         !handshake_finished_;
+}
+
 void QuicSentPacketManager::RetransmitCryptoPackets() {
   QUICHE_DCHECK_EQ(HANDSHAKE_MODE, GetRetransmissionMode());
   ++consecutive_crypto_retransmission_count_;
@@ -922,6 +931,12 @@
       return;
     }
   }
+  const bool send_two_handshake_probes = ShouldSendTwoHandshakeProbes();
+  if (send_two_handshake_probes) {
+    QUIC_RELOADABLE_FLAG_COUNT_N(quic_handshake_pto_two_packets, 1, 2);
+  }
+  const size_t max_probe_packets =
+      send_two_handshake_probes ? 2 : pending_timer_transmission_count_;
   std::vector<QuicPacketNumber> probing_packets;
   if (!unacked_packets_.empty()) {
     QuicPacketNumber packet_number = unacked_packets_.GetLeastUnacked();
@@ -937,7 +952,7 @@
                transmission_info->encryption_level) == packet_number_space)) {
         QUICHE_DCHECK(transmission_info->in_flight);
         probing_packets.push_back(packet_number);
-        if (probing_packets.size() == pending_timer_transmission_count_) {
+        if (probing_packets.size() == max_probe_packets) {
           break;
         }
       }
@@ -947,7 +962,17 @@
   for (QuicPacketNumber retransmission : probing_packets) {
     QUIC_DVLOG(1) << ENDPOINT << "Marking " << retransmission
                   << " for probing retransmission";
-    MarkForRetransmission(retransmission, PTO_RETRANSMISSION);
+    if (send_two_handshake_probes && pending_timer_transmission_count_ == 0) {
+      QUIC_RELOADABLE_FLAG_COUNT_N(quic_handshake_pto_two_packets, 2, 2);
+      pending_timer_transmission_count_ = 1;
+      MarkForRetransmission(retransmission, PTO_RETRANSMISSION);
+      // Defensive: ensure no credit lingers if MarkForRetransmission failed to
+      // serialize a packet, since pending_timer_transmission_count_ > 0 blocks
+      // GetRetransmissionTime() from re-arming the PTO timer.
+      pending_timer_transmission_count_ = 0;
+    } else {
+      MarkForRetransmission(retransmission, PTO_RETRANSMISSION);
+    }
   }
   // It is possible that there is not enough outstanding data for probing.
 }
diff --git a/quiche/quic/core/quic_sent_packet_manager.h b/quiche/quic/core/quic_sent_packet_manager.h
index 9086619..c2a5d46 100644
--- a/quiche/quic/core/quic_sent_packet_manager.h
+++ b/quiche/quic/core/quic_sent_packet_manager.h
@@ -474,7 +474,7 @@
   // Setting the send algorithm once the connection is underway is dangerous.
   void SetSendAlgorithm(SendAlgorithmInterface* send_algorithm);
 
-  // Sends one probe packet.
+  // Sends one or more probe packets.
   void MaybeSendProbePacket();
 
   // Called to disable HANDSHAKE_MODE, and only PTO and LOSS modes are used.
@@ -529,6 +529,11 @@
   friend class test::QuicConnectionPeer;
   friend class test::QuicSentPacketManagerPeer;
 
+  // Returns true if up to two probe packets should be sent when the PTO fires.
+  // This applies when PTO2 was negotiated, IETF PTO is in use, and the
+  // handshake has not been confirmed.
+  bool ShouldSendTwoHandshakeProbes() const;
+
   // Returns the current retransmission mode.
   RetransmissionTimeoutMode GetRetransmissionMode() const;
 
@@ -775,6 +780,8 @@
   // used to prevent the execution of ECN-specific code unless flag-protected
   // code has explicitly enabled it.
   bool ecn_queried_ = false;
+
+  bool handshake_pto_two_packets_ = false;
 };
 
 }  // namespace quic
diff --git a/quiche/quic/core/quic_sent_packet_manager_test.cc b/quiche/quic/core/quic_sent_packet_manager_test.cc
index a4a423d..b4bbfb6 100644
--- a/quiche/quic/core/quic_sent_packet_manager_test.cc
+++ b/quiche/quic/core/quic_sent_packet_manager_test.cc
@@ -3789,6 +3789,122 @@
   EXPECT_EQ(1u, stats_.application_data_packets_with_receive_timestamps_acked);
 }
 
+TEST_F(QuicSentPacketManagerTest, HandshakePtoTwoPacketsDisabledWhenFlagFalse) {
+  manager_.EnableMultiplePacketNumberSpacesSupport();
+  SetQuicReloadableFlag(quic_handshake_pto_two_packets, false);
+  QuicConfig config;
+  QuicConfigPeer::SetReceivedConnectionOptions(&config, {kPTO2});
+  EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
+  EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
+  manager_.SetFromConfig(config);
+  EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true));
+  EXPECT_CALL(*send_algorithm_, PacingRate(_))
+      .WillRepeatedly(Return(QuicBandwidth::Zero()));
+  EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
+      .WillRepeatedly(Return(10 * kDefaultTCPMSS));
+
+  SendDataPacket(1, ENCRYPTION_HANDSHAKE);
+  SendDataPacket(2, ENCRYPTION_HANDSHAKE);
+
+  clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
+  EXPECT_EQ(QuicSentPacketManager::PTO_MODE,
+            manager_.OnRetransmissionTimeout());
+  EXPECT_EQ(1u, manager_.pending_timer_transmission_count());
+
+  EXPECT_CALL(notifier_, RetransmitFrames(_, _))
+      .WillOnce(WithArgs<1>([this](TransmissionType type) {
+        return RetransmitDataPacket(3, type, ENCRYPTION_HANDSHAKE);
+      }));
+  manager_.MaybeSendProbePacket();
+  EXPECT_EQ(0u, manager_.pending_timer_transmission_count());
+  EXPECT_TRUE(manager_.GetRetransmissionTime().IsInitialized());
+}
+
+TEST_F(QuicSentPacketManagerTest, HandshakePtoTwoPackets) {
+  manager_.EnableMultiplePacketNumberSpacesSupport();
+  SetQuicReloadableFlag(quic_handshake_pto_two_packets, true);
+  QuicConfig config;
+  QuicConfigPeer::SetReceivedConnectionOptions(&config, {kPTO2});
+  EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
+  EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
+  manager_.SetFromConfig(config);
+  EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true));
+  EXPECT_CALL(*send_algorithm_, PacingRate(_))
+      .WillRepeatedly(Return(QuicBandwidth::Zero()));
+  EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
+      .WillRepeatedly(Return(10 * kDefaultTCPMSS));
+
+  SendDataPacket(1, ENCRYPTION_HANDSHAKE);
+  SendDataPacket(2, ENCRYPTION_HANDSHAKE);
+  SendDataPacket(3, ENCRYPTION_HANDSHAKE);
+
+  clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
+  EXPECT_EQ(QuicSentPacketManager::PTO_MODE,
+            manager_.OnRetransmissionTimeout());
+  EXPECT_EQ(1u, manager_.pending_timer_transmission_count());
+
+  EXPECT_CALL(notifier_, RetransmitFrames(_, _))
+      .WillOnce(WithArgs<1>([this](TransmissionType type) {
+        return RetransmitDataPacket(4, type, ENCRYPTION_HANDSHAKE);
+      }))
+      .WillOnce(WithArgs<1>([this](TransmissionType type) {
+        return RetransmitDataPacket(5, type, ENCRYPTION_HANDSHAKE);
+      }));
+  manager_.MaybeSendProbePacket();
+  EXPECT_EQ(0u, manager_.pending_timer_transmission_count());
+  EXPECT_TRUE(manager_.GetRetransmissionTime().IsInitialized());
+
+  // Once the handshake is confirmed, PTO only sends one probe packet, even if
+  // crypto data (e.g., a NewSessionTicket) is still outstanding.
+  manager_.SetHandshakeConfirmed();
+  EXPECT_CALL(notifier_, HasUnackedCryptoData()).WillRepeatedly(Return(true));
+  SendDataPacket(6, ENCRYPTION_FORWARD_SECURE);
+  SendDataPacket(7, ENCRYPTION_FORWARD_SECURE);
+
+  clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(2000));
+  EXPECT_EQ(QuicSentPacketManager::PTO_MODE,
+            manager_.OnRetransmissionTimeout());
+  EXPECT_EQ(1u, manager_.pending_timer_transmission_count());
+
+  EXPECT_CALL(notifier_, RetransmitFrames(_, _))
+      .WillOnce(WithArgs<1>([this](TransmissionType type) {
+        return RetransmitDataPacket(8, type, ENCRYPTION_FORWARD_SECURE);
+      }));
+  manager_.MaybeSendProbePacket();
+  EXPECT_EQ(0u, manager_.pending_timer_transmission_count());
+  EXPECT_TRUE(manager_.GetRetransmissionTime().IsInitialized());
+}
+
+TEST_F(QuicSentPacketManagerTest, HandshakePtoTwoPacketsOnlyOneInFlight) {
+  manager_.EnableMultiplePacketNumberSpacesSupport();
+  SetQuicReloadableFlag(quic_handshake_pto_two_packets, true);
+  QuicConfig config;
+  QuicConfigPeer::SetReceivedConnectionOptions(&config, {kPTO2});
+  EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
+  EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
+  manager_.SetFromConfig(config);
+  EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true));
+  EXPECT_CALL(*send_algorithm_, PacingRate(_))
+      .WillRepeatedly(Return(QuicBandwidth::Zero()));
+  EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
+      .WillRepeatedly(Return(10 * kDefaultTCPMSS));
+
+  SendDataPacket(1, ENCRYPTION_HANDSHAKE);
+
+  clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
+  EXPECT_EQ(QuicSentPacketManager::PTO_MODE,
+            manager_.OnRetransmissionTimeout());
+  EXPECT_EQ(1u, manager_.pending_timer_transmission_count());
+
+  EXPECT_CALL(notifier_, RetransmitFrames(_, _))
+      .WillOnce(WithArgs<1>([this](TransmissionType type) {
+        return RetransmitDataPacket(2, type, ENCRYPTION_HANDSHAKE);
+      }));
+  manager_.MaybeSendProbePacket();
+  EXPECT_EQ(0u, manager_.pending_timer_transmission_count());
+  EXPECT_TRUE(manager_.GetRetransmissionTime().IsInitialized());
+}
+
 }  // namespace
 }  // namespace test
 }  // namespace quic