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
3 // found in the LICENSE file.
5 // Manages the packet entropy calculation for both sent and received packets
8 #ifndef NET_QUIC_QUIC_RECEIVED_PACKET_MANAGER_H_
9 #define NET_QUIC_QUIC_RECEIVED_PACKET_MANAGER_H_
11 #include "net/quic/congestion_control/receive_algorithm_interface.h"
12 #include "net/quic/quic_framer.h"
13 #include "net/quic/quic_protocol.h"
18 class QuicConnectionPeer
;
19 class QuicReceivedPacketManagerPeer
;
22 // Records all received packets by a connection and tracks their entropy.
23 // Also calculates the correct entropy for the framer when it truncates an ack
24 // frame being serialized.
25 class NET_EXPORT_PRIVATE QuicReceivedPacketManager
:
26 public QuicReceivedEntropyHashCalculatorInterface
{
28 explicit QuicReceivedPacketManager(CongestionFeedbackType congestion_type
);
29 virtual ~QuicReceivedPacketManager();
31 // Updates the internal state concerning which packets have been received.
32 // bytes: the packet size in bytes including Quic Headers.
33 // header: the packet header.
34 // timestamp: the arrival time of the packet.
35 void RecordPacketReceived(QuicByteCount bytes
,
36 const QuicPacketHeader
& header
,
37 QuicTime receipt_time
);
39 void RecordPacketRevived(QuicPacketSequenceNumber sequence_number
);
41 // Checks whether |sequence_number| is missing and less than largest observed.
42 bool IsMissing(QuicPacketSequenceNumber sequence_number
);
44 // Checks if we're still waiting for the packet with |sequence_number|.
45 bool IsAwaitingPacket(QuicPacketSequenceNumber sequence_number
);
47 // Update the |received_info| for an outgoing ack.
48 void UpdateReceivedPacketInfo(ReceivedPacketInfo
* received_info
,
49 QuicTime approximate_now
);
51 // Should be called before sending an ACK packet, to decide if we need
52 // to attach a QuicCongestionFeedbackFrame block.
53 // Returns false if no QuicCongestionFeedbackFrame block is needed.
54 // Otherwise fills in feedback and returns true.
55 virtual bool GenerateCongestionFeedback(
56 QuicCongestionFeedbackFrame
* feedback
);
58 // QuicReceivedEntropyHashCalculatorInterface
59 // Called by QuicFramer, when the outgoing ack gets truncated, to recalculate
60 // the received entropy hash for the truncated ack frame.
61 virtual QuicPacketEntropyHash
EntropyHash(
62 QuicPacketSequenceNumber sequence_number
) const OVERRIDE
;
64 // Updates internal state based on |received_info|.
65 void UpdatePacketInformationReceivedByPeer(
66 const ReceivedPacketInfo
& received_nfo
);
67 // Updates internal state based on |stop_waiting|.
68 void UpdatePacketInformationSentByPeer(
69 const QuicStopWaitingFrame
& stop_waiting
);
71 // Returns whether the peer is missing packets.
72 bool HasMissingPackets();
74 // Returns true when there are new missing packets to be reported within 3
75 // packets of the largest observed.
76 bool HasNewMissingPackets();
78 QuicPacketSequenceNumber
peer_largest_observed_packet() {
79 return peer_largest_observed_packet_
;
82 QuicPacketSequenceNumber
least_packet_awaited_by_peer() {
83 return least_packet_awaited_by_peer_
;
86 QuicPacketSequenceNumber
peer_least_packet_awaiting_ack() {
87 return peer_least_packet_awaiting_ack_
;
91 friend class test::QuicConnectionPeer
;
92 friend class test::QuicReceivedPacketManagerPeer
;
94 typedef std::map
<QuicPacketSequenceNumber
,
95 QuicPacketEntropyHash
> ReceivedEntropyMap
;
97 // Record the received entropy hash against |sequence_number|.
98 void RecordPacketEntropyHash(QuicPacketSequenceNumber sequence_number
,
99 QuicPacketEntropyHash entropy_hash
);
101 // Recalculate the entropy hash and clears old packet entropies,
102 // now that the sender sent us the |entropy_hash| for packets up to,
103 // but not including, |peer_least_unacked|.
104 void RecalculateEntropyHash(QuicPacketSequenceNumber peer_least_unacked
,
105 QuicPacketEntropyHash entropy_hash
);
107 // Deletes all missing packets before least unacked. The connection won't
108 // process any packets with sequence number before |least_unacked| that it
109 // received after this call. Returns true if there were missing packets before
110 // |least_unacked| unacked, false otherwise.
111 bool DontWaitForPacketsBefore(QuicPacketSequenceNumber least_unacked
);
113 // TODO(satyamshekhar): Can be optimized using an interval set like data
115 // Map of received sequence numbers to their corresponding entropy.
116 // Every received packet has an entry, and packets without the entropy bit set
117 // have an entropy value of 0.
118 // TODO(ianswett): When the entropy flag is off, the entropy should not be 0.
119 ReceivedEntropyMap packets_entropy_
;
121 // Cumulative hash of entropy of all received packets.
122 QuicPacketEntropyHash packets_entropy_hash_
;
124 // The largest sequence number cleared by RecalculateEntropyHash.
125 // Received entropy cannot be calculated for numbers less than it.
126 QuicPacketSequenceNumber largest_sequence_number_
;
129 // Track some peer state so we can do less bookkeeping.
130 // Largest sequence number that the peer has observed. Mostly received,
131 // missing in case of truncated acks.
132 QuicPacketSequenceNumber peer_largest_observed_packet_
;
133 // Least sequence number which the peer is still waiting for.
134 QuicPacketSequenceNumber least_packet_awaited_by_peer_
;
135 // Least sequence number of the the packet sent by the peer for which it
136 // hasn't received an ack.
137 QuicPacketSequenceNumber peer_least_packet_awaiting_ack_
;
139 // Received packet information used to produce acks.
140 ReceivedPacketInfo received_info_
;
142 // The time we received the largest_observed sequence number, or zero if
143 // no sequence numbers have been received since UpdateReceivedPacketInfo.
144 // Needed for calculating delta_time_largest_observed.
145 QuicTime time_largest_observed_
;
147 scoped_ptr
<ReceiveAlgorithmInterface
> receive_algorithm_
;
152 #endif // NET_QUIC_QUIC_RECEIVED_PACKET_MANAGER_H_