| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | |
| 5 // Manages the packet entropy calculation for both sent and received packets | |
| 6 // for a connection. | |
| 7 | 4 |
| 8 #ifndef NET_QUIC_QUIC_RECEIVED_PACKET_MANAGER_H_ | 5 #ifndef NET_QUIC_QUIC_RECEIVED_PACKET_MANAGER_H_ |
| 9 #define NET_QUIC_QUIC_RECEIVED_PACKET_MANAGER_H_ | 6 #define NET_QUIC_QUIC_RECEIVED_PACKET_MANAGER_H_ |
| 10 | 7 |
| 11 #include <stddef.h> | 8 #include <stddef.h> |
| 12 | 9 |
| 13 #include <deque> | 10 #include <deque> |
| 14 | 11 |
| 15 #include "base/macros.h" | 12 #include "base/macros.h" |
| 16 #include "net/base/net_export.h" | 13 #include "net/base/net_export.h" |
| 17 #include "net/quic/core/quic_config.h" | 14 #include "net/quic/core/quic_config.h" |
| 18 #include "net/quic/core/quic_framer.h" | 15 #include "net/quic/core/quic_framer.h" |
| 19 #include "net/quic/core/quic_protocol.h" | 16 #include "net/quic/core/quic_protocol.h" |
| 20 | 17 |
| 21 namespace net { | 18 namespace net { |
| 22 | 19 |
| 23 namespace test { | 20 namespace test { |
| 24 class EntropyTrackerPeer; | |
| 25 class QuicConnectionPeer; | 21 class QuicConnectionPeer; |
| 26 class QuicReceivedPacketManagerPeer; | 22 class QuicReceivedPacketManagerPeer; |
| 27 } // namespace test | 23 } // namespace test |
| 28 | 24 |
| 29 struct QuicConnectionStats; | 25 struct QuicConnectionStats; |
| 30 | 26 |
| 31 // Records all received packets by a connection and tracks their entropy. | 27 // Records all received packets by a connection. |
| 32 // Also calculates the correct entropy for the framer when it truncates an ack | 28 class NET_EXPORT_PRIVATE QuicReceivedPacketManager { |
| 33 // frame being serialized. | |
| 34 class NET_EXPORT_PRIVATE QuicReceivedPacketManager | |
| 35 : public QuicReceivedEntropyHashCalculatorInterface { | |
| 36 public: | 29 public: |
| 37 class NET_EXPORT_PRIVATE EntropyTracker { | |
| 38 public: | |
| 39 EntropyTracker(); | |
| 40 ~EntropyTracker(); | |
| 41 | |
| 42 // Compute the XOR of the entropy of all received packets up to | |
| 43 // and including packet_number. | |
| 44 // Requires that either: | |
| 45 // packet_number == largest_observed_ | |
| 46 // or: | |
| 47 // packet_number > first_gap_ && | |
| 48 // packet_number < largest_observed_ && | |
| 49 // packet_number in packets_entropy_ | |
| 50 QuicPacketEntropyHash EntropyHash(QuicPacketNumber packet_number) const; | |
| 51 | |
| 52 // Record the received entropy hash against |packet_number|. | |
| 53 // Performs garbage collection to advance first_gap_ if | |
| 54 // packet_number == first_gap_. | |
| 55 void RecordPacketEntropyHash(QuicPacketNumber packet_number, | |
| 56 QuicPacketEntropyHash entropy_hash); | |
| 57 | |
| 58 // Sets the entropy hash up to but not including a packet number based | |
| 59 // on the hash provided by a StopWaiting frame. Clears older packet | |
| 60 // entropy entries and performs garbage collection up to the first gap. | |
| 61 void SetCumulativeEntropyUpTo(QuicPacketNumber packet_number, | |
| 62 QuicPacketEntropyHash entropy_hash); | |
| 63 | |
| 64 size_t size() const { return packets_entropy_.size(); } | |
| 65 | |
| 66 private: | |
| 67 friend class test::EntropyTrackerPeer; | |
| 68 | |
| 69 // A deque indexed by packet number storing the packet's hash and whether | |
| 70 // a hash was recorded for that packet number. | |
| 71 typedef std::deque<std::pair<QuicPacketEntropyHash, bool>> | |
| 72 ReceivedEntropyHashes; | |
| 73 | |
| 74 // Recomputes first_gap_ and removes packets_entropy_ entries that are no | |
| 75 // longer needed to compute EntropyHash. | |
| 76 void AdvanceFirstGapAndGarbageCollectEntropyMap(); | |
| 77 | |
| 78 // Map of received packet numbers to their corresponding entropy. | |
| 79 // Stores an entry for every received packet whose packet_number is larger | |
| 80 // than first_gap_. Packets without the entropy bit set have an entropy | |
| 81 // value of 0. | |
| 82 ReceivedEntropyHashes packets_entropy_; | |
| 83 | |
| 84 // Cumulative hash of entropy of all received packets. | |
| 85 QuicPacketEntropyHash packets_entropy_hash_; | |
| 86 | |
| 87 // packet number of the first packet that we do not know the entropy of. | |
| 88 // If there are no gaps in the received packet sequence, | |
| 89 // packets_entropy_ will be empty and first_gap_ will be equal to | |
| 90 // 'largest_observed_ + 1' since that's the first packet for which | |
| 91 // entropy is unknown. If there are gaps, packets_entropy_ will | |
| 92 // contain entries for all received packets with packet_number > | |
| 93 // first_gap_. | |
| 94 QuicPacketNumber first_gap_; | |
| 95 | |
| 96 // packet number of the largest observed packet. | |
| 97 QuicPacketNumber largest_observed_; | |
| 98 | |
| 99 DISALLOW_COPY_AND_ASSIGN(EntropyTracker); | |
| 100 }; | |
| 101 | |
| 102 explicit QuicReceivedPacketManager(QuicConnectionStats* stats); | 30 explicit QuicReceivedPacketManager(QuicConnectionStats* stats); |
| 103 ~QuicReceivedPacketManager() override; | 31 virtual ~QuicReceivedPacketManager(); |
| 104 | 32 |
| 105 // Updates the internal state concerning which packets have been received. | 33 // Updates the internal state concerning which packets have been received. |
| 106 // header: the packet header. | 34 // header: the packet header. |
| 107 // timestamp: the arrival time of the packet. | 35 // timestamp: the arrival time of the packet. |
| 108 virtual void RecordPacketReceived(const QuicPacketHeader& header, | 36 virtual void RecordPacketReceived(const QuicPacketHeader& header, |
| 109 QuicTime receipt_time); | 37 QuicTime receipt_time); |
| 110 | 38 |
| 111 // Checks whether |packet_number| is missing and less than largest observed. | 39 // Checks whether |packet_number| is missing and less than largest observed. |
| 112 virtual bool IsMissing(QuicPacketNumber packet_number); | 40 virtual bool IsMissing(QuicPacketNumber packet_number); |
| 113 | 41 |
| 114 // Checks if we're still waiting for the packet with |packet_number|. | 42 // Checks if we're still waiting for the packet with |packet_number|. |
| 115 virtual bool IsAwaitingPacket(QuicPacketNumber packet_number); | 43 virtual bool IsAwaitingPacket(QuicPacketNumber packet_number); |
| 116 | 44 |
| 117 // Retrieves a frame containing a QuicAckFrame. The ack frame may not be | 45 // Retrieves a frame containing a QuicAckFrame. The ack frame may not be |
| 118 // changed outside QuicReceivedPacketManager and must be serialized before | 46 // changed outside QuicReceivedPacketManager and must be serialized before |
| 119 // another packet is received, or it will change. | 47 // another packet is received, or it will change. |
| 120 const QuicFrame GetUpdatedAckFrame(QuicTime approximate_now); | 48 const QuicFrame GetUpdatedAckFrame(QuicTime approximate_now); |
| 121 | 49 |
| 122 // QuicReceivedEntropyHashCalculatorInterface | |
| 123 // Called by QuicFramer, when the outgoing ack gets truncated, to recalculate | |
| 124 // the received entropy hash for the truncated ack frame. | |
| 125 QuicPacketEntropyHash EntropyHash( | |
| 126 QuicPacketNumber packet_number) const override; | |
| 127 | |
| 128 // Updates internal state based on |stop_waiting|. | 50 // Updates internal state based on |stop_waiting|. |
| 129 virtual void UpdatePacketInformationSentByPeer( | 51 virtual void UpdatePacketInformationSentByPeer( |
| 130 const QuicStopWaitingFrame& stop_waiting); | 52 const QuicStopWaitingFrame& stop_waiting); |
| 131 | 53 |
| 132 // Returns true if there are any missing packets. | 54 // Returns true if there are any missing packets. |
| 133 bool HasMissingPackets() const; | 55 bool HasMissingPackets() const; |
| 134 | 56 |
| 135 // Returns true when there are new missing packets to be reported within 3 | 57 // Returns true when there are new missing packets to be reported within 3 |
| 136 // packets of the largest observed. | 58 // packets of the largest observed. |
| 137 virtual bool HasNewMissingPackets() const; | 59 virtual bool HasNewMissingPackets() const; |
| 138 | 60 |
| 139 // Returns the number of packets being tracked in the EntropyTracker. | |
| 140 size_t NumTrackedPackets() const; | |
| 141 | |
| 142 // Sets the mode of packets set of ack_frame_ based on |version|. | |
| 143 void SetVersion(QuicVersion version); | |
| 144 | |
| 145 QuicPacketNumber peer_least_packet_awaiting_ack() { | 61 QuicPacketNumber peer_least_packet_awaiting_ack() { |
| 146 return peer_least_packet_awaiting_ack_; | 62 return peer_least_packet_awaiting_ack_; |
| 147 } | 63 } |
| 148 | 64 |
| 149 virtual bool ack_frame_updated() const; | 65 virtual bool ack_frame_updated() const; |
| 150 | 66 |
| 151 QuicPacketNumber GetLargestObserved() const; | 67 QuicPacketNumber GetLargestObserved() const; |
| 152 | 68 |
| 153 // For logging purposes. | 69 // For logging purposes. |
| 154 const QuicAckFrame& ack_frame() const { return ack_frame_; } | 70 const QuicAckFrame& ack_frame() const { return ack_frame_; } |
| 155 | 71 |
| 156 private: | 72 private: |
| 157 friend class test::QuicConnectionPeer; | 73 friend class test::QuicConnectionPeer; |
| 158 friend class test::QuicReceivedPacketManagerPeer; | 74 friend class test::QuicReceivedPacketManagerPeer; |
| 159 | 75 |
| 160 // Deletes all missing packets before least unacked. The connection won't | 76 // Deletes all missing packets before least unacked. The connection won't |
| 161 // process any packets with packet number before |least_unacked| that it | 77 // process any packets with packet number before |least_unacked| that it |
| 162 // received after this call. Returns true if there were missing packets before | 78 // received after this call. Returns true if there were missing packets before |
| 163 // |least_unacked| unacked, false otherwise. | 79 // |least_unacked| unacked, false otherwise. |
| 164 bool DontWaitForPacketsBefore(QuicPacketNumber least_unacked); | 80 bool DontWaitForPacketsBefore(QuicPacketNumber least_unacked); |
| 165 | 81 |
| 166 // Tracks entropy hashes of received packets. | |
| 167 EntropyTracker entropy_tracker_; | |
| 168 | |
| 169 // Least packet number of the the packet sent by the peer for which it | 82 // Least packet number of the the packet sent by the peer for which it |
| 170 // hasn't received an ack. | 83 // hasn't received an ack. |
| 171 QuicPacketNumber peer_least_packet_awaiting_ack_; | 84 QuicPacketNumber peer_least_packet_awaiting_ack_; |
| 172 | 85 |
| 173 // Received packet information used to produce acks. | 86 // Received packet information used to produce acks. |
| 174 QuicAckFrame ack_frame_; | 87 QuicAckFrame ack_frame_; |
| 175 | 88 |
| 176 // True if |ack_frame_| has been updated since UpdateReceivedPacketInfo was | 89 // True if |ack_frame_| has been updated since UpdateReceivedPacketInfo was |
| 177 // last called. | 90 // last called. |
| 178 bool ack_frame_updated_; | 91 bool ack_frame_updated_; |
| 179 | 92 |
| 180 // The time we received the largest_observed packet number, or zero if | 93 // The time we received the largest_observed packet number, or zero if |
| 181 // no packet numbers have been received since UpdateReceivedPacketInfo. | 94 // no packet numbers have been received since UpdateReceivedPacketInfo. |
| 182 // Needed for calculating ack_delay_time. | 95 // Needed for calculating ack_delay_time. |
| 183 QuicTime time_largest_observed_; | 96 QuicTime time_largest_observed_; |
| 184 | 97 |
| 185 QuicConnectionStats* stats_; | 98 QuicConnectionStats* stats_; |
| 186 | 99 |
| 187 DISALLOW_COPY_AND_ASSIGN(QuicReceivedPacketManager); | 100 DISALLOW_COPY_AND_ASSIGN(QuicReceivedPacketManager); |
| 188 }; | 101 }; |
| 189 | 102 |
| 190 } // namespace net | 103 } // namespace net |
| 191 | 104 |
| 192 #endif // NET_QUIC_QUIC_RECEIVED_PACKET_MANAGER_H_ | 105 #endif // NET_QUIC_QUIC_RECEIVED_PACKET_MANAGER_H_ |
| OLD | NEW |