1 // Copyright 2014 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.
5 #ifndef NET_QUIC_QUIC_UNACKED_PACKET_MAP_H_
6 #define NET_QUIC_QUIC_UNACKED_PACKET_MAP_H_
10 #include "net/quic/quic_protocol.h"
14 class AckNotifierManager
;
16 // Class which tracks unacked packets for three purposes:
17 // 1) Track retransmittable data, including multiple transmissions of frames.
18 // 2) Track packets and bytes in flight for congestion control.
19 // 3) Track sent time of packets to provide RTT measurements from acks.
20 class NET_EXPORT_PRIVATE QuicUnackedPacketMap
{
22 // Initialize an instance of UnackedPacketMap. The AckNotifierManager
23 // provided to the constructor will be notified whenever a packet is removed
25 explicit QuicUnackedPacketMap(AckNotifierManager
* ack_notifier_manager
);
26 ~QuicUnackedPacketMap();
28 // Adds |serialized_packet| to the map and marks it as sent at |sent_time|.
29 // Marks the packet as in flight if |set_in_flight| is true.
30 // Packets marked as in flight are expected to be marked as missing when they
31 // don't arrive, indicating the need for retransmission.
32 // |old_sequence_number| is the sequence number of the previous transmission,
33 // or 0 if there was none.
34 void AddSentPacket(const SerializedPacket
& serialized_packet
,
35 QuicPacketSequenceNumber old_sequence_number
,
36 TransmissionType transmission_type
,
38 QuicByteCount bytes_sent
,
41 // Returns true if the packet |sequence_number| is unacked.
42 bool IsUnacked(QuicPacketSequenceNumber sequence_number
) const;
44 // Sets the nack count to the max of the current nack count and |min_nacks|.
45 void NackPacket(QuicPacketSequenceNumber sequence_number
,
46 QuicPacketCount min_nacks
);
48 // Marks |sequence_number| as no longer in flight.
49 void RemoveFromInFlight(QuicPacketSequenceNumber sequence_number
);
51 // No longer retransmit data for |stream_id|.
52 void CancelRetransmissionsForStream(QuicStreamId stream_id
);
54 // Returns true if the unacked packet |sequence_number| has retransmittable
55 // frames. This will return false if the packet has been acked, if a
56 // previous transmission of this packet was ACK'd, or if this packet has been
57 // retransmitted as with different sequence number, or if the packet never
58 // had any retransmittable packets in the first place.
59 bool HasRetransmittableFrames(QuicPacketSequenceNumber sequence_number
) const;
61 // Returns true if there are any unacked packets.
62 bool HasUnackedPackets() const;
64 // Returns true if there are any unacked packets which have retransmittable
66 bool HasUnackedRetransmittableFrames() const;
68 // Returns the largest sequence number that has been sent.
69 QuicPacketSequenceNumber
largest_sent_packet() const {
70 return largest_sent_packet_
;
73 // Returns the largest sequence number that has been acked.
74 QuicPacketSequenceNumber
largest_observed() const {
75 return largest_observed_
;
78 // Returns the sum of bytes from all packets in flight.
79 QuicByteCount
bytes_in_flight() const {
80 return bytes_in_flight_
;
83 // Returns the smallest sequence number of a serialized packet which has not
84 // been acked by the peer. If there are no unacked packets, returns 0.
85 QuicPacketSequenceNumber
GetLeastUnacked() const;
87 // Clears all previous transmissions in order to make room in the ack frame
88 // for newly acked packets.
89 void ClearAllPreviousRetransmissions();
91 typedef std::deque
<TransmissionInfo
> UnackedPacketMap
;
93 typedef UnackedPacketMap::const_iterator const_iterator
;
95 const_iterator
begin() const { return unacked_packets_
.begin(); }
96 const_iterator
end() const { return unacked_packets_
.end(); }
98 // Returns true if there are unacked packets that are in flight.
99 bool HasInFlightPackets() const;
101 // Returns the TransmissionInfo associated with |sequence_number|, which
103 const TransmissionInfo
& GetTransmissionInfo(
104 QuicPacketSequenceNumber sequence_number
) const;
106 // Returns the time that the last unacked packet was sent.
107 QuicTime
GetLastPacketSentTime() const;
109 // Returns the number of unacked packets.
110 size_t GetNumUnackedPacketsDebugOnly() const;
112 // Returns true if there are multiple packets in flight.
113 bool HasMultipleInFlightPackets() const;
115 // Returns true if there are any pending crypto packets.
116 bool HasPendingCryptoPackets() const;
118 // Removes any retransmittable frames from this transmission or an associated
119 // transmission. It removes now useless transmissions, and disconnects any
120 // other packets from other transmissions.
121 void RemoveRetransmittability(QuicPacketSequenceNumber sequence_number
);
123 // Removes any other retransmissions and marks all transmissions unackable.
124 void RemoveAckability(TransmissionInfo
* info
);
126 // Increases the largest observed. Any packets less or equal to
127 // |largest_acked_packet| are discarded if they are only for the RTT purposes.
128 void IncreaseLargestObserved(QuicPacketSequenceNumber largest_observed
);
130 // Remove any packets no longer needed for retransmission, congestion, or
131 // RTT measurement purposes.
132 void RemoveObsoletePackets();
135 // Called when a packet is retransmitted with a new sequence number.
136 // |old_sequence_number| will remain unacked, but will have no
137 // retransmittable data associated with it. Retransmittable frames will be
138 // transferred to |info| and all_transmissions will be populated.
139 void TransferRetransmissionInfo(QuicPacketSequenceNumber old_sequence_number
,
140 QuicPacketSequenceNumber new_sequence_number
,
141 TransmissionType transmission_type
,
142 TransmissionInfo
* info
);
144 void MaybeRemoveRetransmittableFrames(TransmissionInfo
* transmission_info
);
146 // Returns true if packet may be useful for an RTT measurement.
147 bool IsPacketUsefulForMeasuringRtt(QuicPacketSequenceNumber sequence_number
,
148 const TransmissionInfo
& info
) const;
150 // Returns true if packet may be useful for congestion control purposes.
151 bool IsPacketUsefulForCongestionControl(const TransmissionInfo
& info
) const;
153 // Returns true if packet may be associated with retransmittable data
154 // directly or through retransmissions.
155 bool IsPacketUsefulForRetransmittableData(const TransmissionInfo
& info
) const;
157 // Returns true if the packet no longer has a purpose in the map.
158 bool IsPacketUseless(QuicPacketSequenceNumber sequence_number
,
159 const TransmissionInfo
& info
) const;
161 // Returns true if the packet is useless or it's only purpose is RTT
162 // measurement, and it's old enough that is unlikely to ever happen.
163 bool IsPacketRemovable(QuicPacketSequenceNumber sequence_number
,
164 const TransmissionInfo
& info
) const;
166 // Removes the packet with lowest sequence number from the map.
167 void PopLeastUnacked();
169 QuicPacketSequenceNumber largest_sent_packet_
;
170 QuicPacketSequenceNumber largest_observed_
;
172 // Newly serialized retransmittable and fec packets are added to this map,
173 // which contains owning pointers to any contained frames. If a packet is
174 // retransmitted, this map will contain entries for both the old and the new
175 // packet. The old packet's retransmittable frames entry will be nullptr,
176 // while the new packet's entry will contain the frames to retransmit.
177 // If the old packet is acked before the new packet, then the old entry will
178 // be removed from the map and the new entry's retransmittable frames will be
180 UnackedPacketMap unacked_packets_
;
181 // The packet at the 0th index of unacked_packets_.
182 QuicPacketSequenceNumber least_unacked_
;
184 QuicByteCount bytes_in_flight_
;
185 // Number of retransmittable crypto handshake packets.
186 size_t pending_crypto_packet_count_
;
188 // Notifier manager for ACK packets. We notify it every time we abandon a
190 AckNotifierManager
* ack_notifier_manager_
;
192 DISALLOW_COPY_AND_ASSIGN(QuicUnackedPacketMap
);
197 #endif // NET_QUIC_QUIC_UNACKED_PACKET_MAP_H_