Gate BBR receive-timestamp bandwidth clamping behind the BTSC connection option

PiperOrigin-RevId: 1034397655
diff --git a/quiche/quic/core/congestion_control/bandwidth_sampler.cc b/quiche/quic/core/congestion_control/bandwidth_sampler.cc
index 1444ed2..21ead00 100644
--- a/quiche/quic/core/congestion_control/bandwidth_sampler.cc
+++ b/quiche/quic/core/congestion_control/bandwidth_sampler.cc
@@ -490,18 +490,21 @@
   BandwidthSample sample;
   sample.bandwidth = std::min(send_rate, ack_rate);
 
-  // If QUIC receive timestamps are available, use them to clamp the returned
-  // bandwidth sample.  Those generally should not be trusted for increasing
-  // the bandwidth estimate to avoid "speculative ACK"-style attacks, but they
-  // do work as a reasonable signal to compute an upper bound.
-  const std::optional<QuicReceiveTime> recv_time1 =
-      acked_packet.receive_timestamp;
-  const std::optional<QuicReceiveTime> recv_time0 = a0.receive_time;
-  if (recv_time0.has_value() && recv_time1.has_value() &&
-      *recv_time0 < *recv_time1) {
-    const QuicBandwidth recv_rate = QuicBandwidth::FromBytesAndTimeDelta(
-        total_bytes_acked_ - a0.total_bytes_acked, *recv_time1 - *recv_time0);
-    sample.bandwidth = std::min(sample.bandwidth, recv_rate);
+  // If QUIC receive timestamps are available and clamping is enabled, use them
+  // to clamp the returned bandwidth sample.  Those generally should not be
+  // trusted for increasing the bandwidth estimate to avoid "speculative
+  // ACK"-style attacks, but they do work as a reasonable signal to compute an
+  // upper bound.
+  if (clamp_bandwidth_with_receive_timestamps_) {
+    const std::optional<QuicReceiveTime> recv_time1 =
+        acked_packet.receive_timestamp;
+    const std::optional<QuicReceiveTime> recv_time0 = a0.receive_time;
+    if (recv_time0.has_value() && recv_time1.has_value() &&
+        *recv_time0 < *recv_time1) {
+      const QuicBandwidth recv_rate = QuicBandwidth::FromBytesAndTimeDelta(
+          total_bytes_acked_ - a0.total_bytes_acked, *recv_time1 - *recv_time0);
+      sample.bandwidth = std::min(sample.bandwidth, recv_rate);
+    }
   }
 
   if (GetQuicReloadableFlag(quic_bandwidth_sampler_guard_rtt_subtraction)) {
diff --git a/quiche/quic/core/congestion_control/bandwidth_sampler.h b/quiche/quic/core/congestion_control/bandwidth_sampler.h
index 3232cda..5503b40 100644
--- a/quiche/quic/core/congestion_control/bandwidth_sampler.h
+++ b/quiche/quic/core/congestion_control/bandwidth_sampler.h
@@ -389,6 +389,10 @@
     limit_max_ack_height_tracker_by_send_rate_ = value;
   }
 
+  void SetClampBandwidthWithReceiveTimestamps(bool value) {
+    clamp_bandwidth_with_receive_timestamps_ = value;
+  }
+
   void SetReduceExtraAckedOnBandwidthIncrease(bool value) {
     max_ack_height_tracker_.SetReduceExtraAckedOnBandwidthIncrease(value);
   }
@@ -635,6 +639,9 @@
   // True if connection option 'BBRB' is set.
   bool limit_max_ack_height_tracker_by_send_rate_ = false;
 
+  // True if connection option 'BTSC' is set.
+  bool clamp_bandwidth_with_receive_timestamps_ = false;
+
   const bool remove_obsolete_packets_on_send_ =
       GetQuicReloadableFlag(quic_sampler_remove_obsolete_packets);
 };
diff --git a/quiche/quic/core/congestion_control/bandwidth_sampler_test.cc b/quiche/quic/core/congestion_control/bandwidth_sampler_test.cc
index 7326cc6..091400d 100644
--- a/quiche/quic/core/congestion_control/bandwidth_sampler_test.cc
+++ b/quiche/quic/core/congestion_control/bandwidth_sampler_test.cc
@@ -926,6 +926,7 @@
 }
 
 TEST_P(BandwidthSamplerTest, ReceiveTimestamps) {
+  sampler_.SetClampBandwidthWithReceiveTimestamps(true);
   constexpr QuicTimeDelta kTimeBetweenPackets =
       QuicTimeDelta::FromMilliseconds(10);
   constexpr QuicBandwidth kExpectedBandwidth =
@@ -963,5 +964,26 @@
   }
 }
 
+TEST_P(BandwidthSamplerTest, ReceiveTimestampsDisabledByDefault) {
+  constexpr QuicTimeDelta kTimeBetweenPackets =
+      QuicTimeDelta::FromMilliseconds(10);
+  constexpr QuicBandwidth kExpectedBandwidth =
+      QuicBandwidth::FromBytesPerSecond(kRegularPacketSize * 100);
+  QuicTimeDelta current_peer_clock = 12345 * kTimeBetweenPackets;
+
+  SendPacket(1);
+
+  // Even when the peer clock runs 2x slower than the local clock, bandwidth
+  // samples are not clamped unless receive-timestamp clamping is enabled.
+  for (int i = 2; i <= 20; ++i) {
+    SendPacket(i);
+    clock_.AdvanceTime(kTimeBetweenPackets);
+    current_peer_clock = current_peer_clock + 2 * kTimeBetweenPackets;
+    QuicBandwidth current_sample =
+        AckPacket(i - 1, QuicReceiveTime::FromPeerTime(current_peer_clock));
+    EXPECT_EQ(kExpectedBandwidth, current_sample);
+  }
+}
+
 }  // namespace test
 }  // namespace quic
diff --git a/quiche/quic/core/congestion_control/bbr2_misc.h b/quiche/quic/core/congestion_control/bbr2_misc.h
index 7382981..33e9acc 100644
--- a/quiche/quic/core/congestion_control/bbr2_misc.h
+++ b/quiche/quic/core/congestion_control/bbr2_misc.h
@@ -460,6 +460,10 @@
     bandwidth_sampler_.SetLimitMaxAckHeightTrackerBySendRate(value);
   }
 
+  void SetClampBandwidthWithReceiveTimestamps(bool value) {
+    bandwidth_sampler_.SetClampBandwidthWithReceiveTimestamps(value);
+  }
+
   void SetMaxAckHeightTrackerWindowLength(QuicRoundTripCount value) {
     bandwidth_sampler_.SetMaxAckHeightTrackerWindowLength(value);
   }
diff --git a/quiche/quic/core/congestion_control/bbr2_sender.cc b/quiche/quic/core/congestion_control/bbr2_sender.cc
index e1a87ad..f198f7b 100644
--- a/quiche/quic/core/congestion_control/bbr2_sender.cc
+++ b/quiche/quic/core/congestion_control/bbr2_sender.cc
@@ -186,6 +186,9 @@
   if (ContainsQuicTag(connection_options, kBBRB)) {
     model_.SetLimitMaxAckHeightTrackerBySendRate(true);
   }
+  if (ContainsQuicTag(connection_options, kBTSC)) {
+    model_.SetClampBandwidthWithReceiveTimestamps(true);
+  }
   if (ContainsQuicTag(connection_options, kADP0)) {
     model_.SetEnableAppDrivenPacing(true);
   }
diff --git a/quiche/quic/core/congestion_control/bbr3_sender.cc b/quiche/quic/core/congestion_control/bbr3_sender.cc
index 6a406bc..a861267 100644
--- a/quiche/quic/core/congestion_control/bbr3_sender.cc
+++ b/quiche/quic/core/congestion_control/bbr3_sender.cc
@@ -164,6 +164,9 @@
   if (ContainsQuicTag(connection_options, kBBRB)) {
     model_.SetLimitMaxAckHeightTrackerBySendRate(true);
   }
+  if (ContainsQuicTag(connection_options, kBTSC)) {
+    model_.SetClampBandwidthWithReceiveTimestamps(true);
+  }
   if (ContainsQuicTag(connection_options, kADP0)) {
     model_.SetEnableAppDrivenPacing(true);
   }
diff --git a/quiche/quic/core/congestion_control/bbr_sender.cc b/quiche/quic/core/congestion_control/bbr_sender.cc
index 0178c5b..fb04fc4 100644
--- a/quiche/quic/core/congestion_control/bbr_sender.cc
+++ b/quiche/quic/core/congestion_control/bbr_sender.cc
@@ -287,6 +287,9 @@
   if (ContainsQuicTag(connection_options, kBBRB)) {
     sampler_.SetLimitMaxAckHeightTrackerBySendRate(true);
   }
+  if (ContainsQuicTag(connection_options, kBTSC)) {
+    sampler_.SetClampBandwidthWithReceiveTimestamps(true);
+  }
 }
 
 void BbrSender::AdjustNetworkParameters(const NetworkParams& params) {
diff --git a/quiche/quic/core/congestion_control/bbr_sender_test.cc b/quiche/quic/core/congestion_control/bbr_sender_test.cc
index a419f9c..4d8977e 100644
--- a/quiche/quic/core/congestion_control/bbr_sender_test.cc
+++ b/quiche/quic/core/congestion_control/bbr_sender_test.cc
@@ -18,6 +18,7 @@
 #include "quiche/quic/core/quic_connection_stats.h"
 #include "quiche/quic/core/quic_constants.h"
 #include "quiche/quic/core/quic_packet_number.h"
+#include "quiche/quic/core/quic_receive_time.h"
 #include "quiche/quic/core/quic_tag.h"
 #include "quiche/quic/core/quic_time.h"
 #include "quiche/quic/core/quic_types.h"
@@ -1371,5 +1372,59 @@
   EXPECT_TRUE(stats.overshooting_detected_with_network_parameters_adjusted);
 }
 
+namespace {
+
+// Drives `sender` through a warm-up at 20ms/packet (to establish an initial
+// receive timestamp) followed by 10ms/packet sends and acks while peer receive
+// timestamps continue advancing at 20ms/packet.
+void SendPacketsWithSlowerReceiveTimestamps(SendAlgorithmInterface* sender,
+                                            QuicTime start_time) {
+  constexpr QuicByteCount kPacketSize = 1280;
+  constexpr QuicTimeDelta kSlowInterval = QuicTimeDelta::FromMilliseconds(20);
+  constexpr QuicTimeDelta kFastInterval = QuicTimeDelta::FromMilliseconds(10);
+
+  QuicTime now = start_time;
+  QuicTimeDelta peer_time = QuicTimeDelta::FromSeconds(10);
+  QuicByteCount bytes_in_flight = 0;
+
+  sender->OnPacketSent(now, bytes_in_flight, QuicPacketNumber(1), kPacketSize,
+                       HAS_RETRANSMITTABLE_DATA);
+  bytes_in_flight += kPacketSize;
+
+  for (uint64_t i = 2; i <= 10; ++i) {
+    const QuicTimeDelta local_step = i <= 3 ? kSlowInterval : kFastInterval;
+    now = now + local_step;
+    peer_time = peer_time + kSlowInterval;
+    sender->OnPacketSent(now, bytes_in_flight, QuicPacketNumber(i), kPacketSize,
+                         HAS_RETRANSMITTABLE_DATA);
+    bytes_in_flight += kPacketSize;
+
+    AckedPacketVector acked_packets;
+    acked_packets.emplace_back(QuicPacketNumber(i - 1), kPacketSize,
+                               QuicReceiveTime::FromPeerTime(peer_time));
+    sender->OnCongestionEvent(/*rtt_updated=*/true, bytes_in_flight, now,
+                              acked_packets, /*lost_packets=*/{},
+                              /*num_ect=*/0, /*num_ce=*/0);
+    bytes_in_flight -= kPacketSize;
+  }
+}
+
+}  // namespace
+
+TEST_F(BbrSenderTest, ReceiveTimestampsClampingDisabledByDefault) {
+  SendPacketsWithSlowerReceiveTimestamps(
+      sender_, clock_->Now() + QuicTime::Delta::FromSeconds(1));
+  EXPECT_EQ(sender_->BandwidthEstimate(),
+            QuicBandwidth::FromBytesPerSecond(1280 * 100));
+}
+
+TEST_F(BbrSenderTest, ReceiveTimestampsClampingEnabledWithBTSC) {
+  SetConnectionOption(kBTSC);
+  SendPacketsWithSlowerReceiveTimestamps(
+      sender_, clock_->Now() + QuicTime::Delta::FromSeconds(1));
+  EXPECT_EQ(sender_->BandwidthEstimate(),
+            QuicBandwidth::FromBytesPerSecond(1280 * 50));
+}
+
 }  // namespace test
 }  // namespace quic
diff --git a/quiche/quic/core/congestion_control/bbrx_simulator_test.cc b/quiche/quic/core/congestion_control/bbrx_simulator_test.cc
index e90fdbf..94fe29a 100644
--- a/quiche/quic/core/congestion_control/bbrx_simulator_test.cc
+++ b/quiche/quic/core/congestion_control/bbrx_simulator_test.cc
@@ -20,6 +20,7 @@
 #include "quiche/quic/core/congestion_control/tcp_cubic_sender_bytes.h"
 #include "quiche/quic/core/quic_bandwidth.h"
 #include "quiche/quic/core/quic_packet_number.h"
+#include "quiche/quic/core/quic_receive_time.h"
 #include "quiche/quic/core/quic_types.h"
 #include "quiche/quic/platform/api/quic_expect_bug.h"
 #include "quiche/quic/platform/api/quic_flags.h"
@@ -3134,6 +3135,56 @@
   EXPECT_EQ(filter.GetTimestamp(), time_in_seconds(100));
 }
 
+// Drives `sender` through a warm-up at 20ms/packet (to establish an initial
+// receive timestamp) followed by 10ms/packet sends and acks while peer receive
+// timestamps continue advancing at 20ms/packet.
+void SendPacketsWithSlowerReceiveTimestamps(SendAlgorithmInterface* sender,
+                                            QuicTime start_time) {
+  constexpr QuicByteCount kPacketSize = 1280;
+  constexpr QuicTimeDelta kSlowInterval = QuicTimeDelta::FromMilliseconds(20);
+  constexpr QuicTimeDelta kFastInterval = QuicTimeDelta::FromMilliseconds(10);
+
+  QuicTime now = start_time;
+  QuicTimeDelta peer_time = QuicTimeDelta::FromSeconds(10);
+  QuicByteCount bytes_in_flight = 0;
+
+  sender->OnPacketSent(now, bytes_in_flight, QuicPacketNumber(1), kPacketSize,
+                       HAS_RETRANSMITTABLE_DATA);
+  bytes_in_flight += kPacketSize;
+
+  for (uint64_t i = 2; i <= 10; ++i) {
+    const QuicTimeDelta local_step = i <= 3 ? kSlowInterval : kFastInterval;
+    now = now + local_step;
+    peer_time = peer_time + kSlowInterval;
+    sender->OnPacketSent(now, bytes_in_flight, QuicPacketNumber(i), kPacketSize,
+                         HAS_RETRANSMITTABLE_DATA);
+    bytes_in_flight += kPacketSize;
+
+    AckedPacketVector acked_packets;
+    acked_packets.emplace_back(QuicPacketNumber(i - 1), kPacketSize,
+                               QuicReceiveTime::FromPeerTime(peer_time));
+    sender->OnCongestionEvent(/*rtt_updated=*/true, bytes_in_flight, now,
+                              acked_packets, /*lost_packets=*/{},
+                              /*num_ect=*/0, /*num_ce=*/0);
+    bytes_in_flight -= kPacketSize;
+  }
+}
+
+TEST_P(BbrDefaultTopologyTest, ReceiveTimestampsClampingDisabledByDefault) {
+  SendPacketsWithSlowerReceiveTimestamps(
+      sender_, SimulatedNow() + QuicTime::Delta::FromSeconds(1));
+  EXPECT_EQ(sender_->BandwidthEstimate(),
+            QuicBandwidth::FromBytesPerSecond(1280 * 100));
+}
+
+TEST_P(BbrDefaultTopologyTest, ReceiveTimestampsClampingEnabledWithBTSC) {
+  SetConnectionOption(kBTSC);
+  SendPacketsWithSlowerReceiveTimestamps(
+      sender_, SimulatedNow() + QuicTime::Delta::FromSeconds(1));
+  EXPECT_EQ(sender_->BandwidthEstimate(),
+            QuicBandwidth::FromBytesPerSecond(1280 * 50));
+}
+
 }  // namespace
 }  // namespace test
 }  // namespace quic
diff --git a/quiche/quic/core/crypto/crypto_protocol.h b/quiche/quic/core/crypto/crypto_protocol.h
index a7ccfc1..f8bc09b 100644
--- a/quiche/quic/core/crypto/crypto_protocol.h
+++ b/quiche/quic/core/crypto/crypto_protocol.h
@@ -200,6 +200,8 @@
                                // rounds in a row.
 DEFINE_STATIC_QUIC_TAG(B3NP);  // For BBRv3, disable precautionary
                                // bandwidth probing.
+DEFINE_STATIC_QUIC_TAG(BTSC);  // Clamp BBR bandwidth samples using QUIC
+                               // receive timestamps.
 DEFINE_STATIC_QUIC_TAG(NTLP);  // No tail loss probe
 DEFINE_STATIC_QUIC_TAG(1TLP);  // 1 tail loss probe
 DEFINE_STATIC_QUIC_TAG(1RTO);  // Send 1 packet upon RTO