1 // Copyright 2019 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef QUICHE_QUIC_CORE_CONGESTION_CONTROL_UBER_LOSS_ALGORITHM_H_ 6 #define QUICHE_QUIC_CORE_CONGESTION_CONTROL_UBER_LOSS_ALGORITHM_H_ 7 8 #include "absl/types/optional.h" 9 #include "quiche/quic/core/congestion_control/general_loss_algorithm.h" 10 #include "quiche/quic/core/quic_types.h" 11 #include "quiche/quic/platform/api/quic_flags.h" 12 13 namespace quic { 14 15 namespace test { 16 17 class QuicSentPacketManagerPeer; 18 19 } // namespace test 20 21 struct QUIC_EXPORT_PRIVATE LossDetectionParameters { 22 // See GeneralLossAlgorithm for the meaning of reordering_(shift|threshold). 23 absl::optional<int> reordering_shift; 24 absl::optional<QuicPacketCount> reordering_threshold; 25 }; 26 27 class QUIC_EXPORT_PRIVATE LossDetectionTunerInterface { 28 public: ~LossDetectionTunerInterface()29 virtual ~LossDetectionTunerInterface() {} 30 31 // Start the tuning by choosing parameters and saving them into |*params|. 32 // Called near the start of a QUIC session, see the .cc file for exactly 33 // where. 34 virtual bool Start(LossDetectionParameters* params) = 0; 35 36 // Finish tuning. The tuner is expected to use the actual loss detection 37 // performance(for its definition of performance) to improve the parameter 38 // selection for future QUIC sessions. 39 // Called when a QUIC session closes. 40 virtual void Finish(const LossDetectionParameters& params) = 0; 41 }; 42 43 // This class comprises multiple loss algorithms, each per packet number space. 44 class QUIC_EXPORT_PRIVATE UberLossAlgorithm : public LossDetectionInterface { 45 public: 46 UberLossAlgorithm(); 47 UberLossAlgorithm(const UberLossAlgorithm&) = delete; 48 UberLossAlgorithm& operator=(const UberLossAlgorithm&) = delete; ~UberLossAlgorithm()49 ~UberLossAlgorithm() override {} 50 51 void SetFromConfig(const QuicConfig& config, 52 Perspective perspective) override; 53 54 // Detects lost packets. 55 DetectionStats DetectLosses(const QuicUnackedPacketMap& unacked_packets, 56 QuicTime time, const RttStats& rtt_stats, 57 QuicPacketNumber largest_newly_acked, 58 const AckedPacketVector& packets_acked, 59 LostPacketVector* packets_lost) override; 60 61 // Returns the earliest time the early retransmit timer should be active. 62 QuicTime GetLossTimeout() const override; 63 64 // Called to increases time or packet threshold. 65 void SpuriousLossDetected(const QuicUnackedPacketMap& unacked_packets, 66 const RttStats& rtt_stats, 67 QuicTime ack_receive_time, 68 QuicPacketNumber packet_number, 69 QuicPacketNumber previous_largest_acked) override; 70 71 void SetLossDetectionTuner( 72 std::unique_ptr<LossDetectionTunerInterface> tuner); 73 void OnConfigNegotiated() override; 74 void OnMinRttAvailable() override; 75 void OnUserAgentIdKnown() override; 76 void OnConnectionClosed() override; 77 void OnReorderingDetected() override; 78 79 // Sets reordering_shift for all packet number spaces. 80 void SetReorderingShift(int reordering_shift); 81 82 // Sets reordering_threshold for all packet number spaces. 83 void SetReorderingThreshold(QuicPacketCount reordering_threshold); 84 85 // Enable adaptive reordering threshold of all packet number spaces. 86 void EnableAdaptiveReorderingThreshold(); 87 88 // Disable adaptive reordering threshold of all packet number spaces. 89 void DisableAdaptiveReorderingThreshold(); 90 91 // Enable adaptive time threshold of all packet number spaces. 92 void EnableAdaptiveTimeThreshold(); 93 94 // Get the packet reordering threshold from the APPLICATION_DATA PN space. 95 // Always 3 when adaptive reordering is not enabled. 96 QuicPacketCount GetPacketReorderingThreshold() const; 97 98 // Get the packet reordering shift from the APPLICATION_DATA PN space. 99 int GetPacketReorderingShift() const; 100 101 // Disable packet threshold loss detection for *runt* packets. 102 void DisablePacketThresholdForRuntPackets(); 103 104 // Called to reset loss detection of |space|. 105 void ResetLossDetection(PacketNumberSpace space); 106 use_adaptive_reordering_threshold()107 bool use_adaptive_reordering_threshold() const { 108 return general_loss_algorithms_[APPLICATION_DATA] 109 .use_adaptive_reordering_threshold(); 110 } 111 use_adaptive_time_threshold()112 bool use_adaptive_time_threshold() const { 113 return general_loss_algorithms_[APPLICATION_DATA] 114 .use_adaptive_time_threshold(); 115 } 116 117 private: 118 friend class test::QuicSentPacketManagerPeer; 119 120 void MaybeStartTuning(); 121 122 // One loss algorithm per packet number space. 123 GeneralLossAlgorithm general_loss_algorithms_[NUM_PACKET_NUMBER_SPACES]; 124 125 // Used to tune reordering_shift and reordering_threshold. 126 std::unique_ptr<LossDetectionTunerInterface> tuner_; 127 LossDetectionParameters tuned_parameters_; 128 bool tuner_started_ = false; 129 bool min_rtt_available_ = false; 130 // Whether user agent is known to the session. 131 bool user_agent_known_ = false; 132 // Whether tuning is configured in QuicConfig. 133 bool tuning_configured_ = false; 134 bool reorder_happened_ = false; // Whether any reordered packet is observed. 135 }; 136 137 } // namespace quic 138 139 #endif // QUICHE_QUIC_CORE_CONGESTION_CONTROL_UBER_LOSS_ALGORITHM_H_ 140