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 |sent_info|.
68 void UpdatePacketInformationSentByPeer(const SentPacketInfo& sent_info);
70 // Returns whether the peer is missing packets.
71 bool HasMissingPackets();
73 // Returns true when there are new missing packets to be reported within 3
74 // packets of the largest observed.
75 bool HasNewMissingPackets();
77 QuicPacketSequenceNumber peer_largest_observed_packet() {
78 return peer_largest_observed_packet_;
81 QuicPacketSequenceNumber least_packet_awaited_by_peer() {
82 return least_packet_awaited_by_peer_;
85 QuicPacketSequenceNumber peer_least_packet_awaiting_ack() {
86 return peer_least_packet_awaiting_ack_;
90 friend class test::QuicConnectionPeer;
91 friend class test::QuicReceivedPacketManagerPeer;
93 typedef std::map<QuicPacketSequenceNumber,
94 QuicPacketEntropyHash> ReceivedEntropyMap;
96 // Record the received entropy hash against |sequence_number|.
97 void RecordPacketEntropyHash(QuicPacketSequenceNumber sequence_number,
98 QuicPacketEntropyHash entropy_hash);
100 // Recalculate the entropy hash and clears old packet entropies,
101 // now that the sender sent us the |entropy_hash| for packets up to,
102 // but not including, |peer_least_unacked|.
103 void RecalculateEntropyHash(QuicPacketSequenceNumber peer_least_unacked,
104 QuicPacketEntropyHash entropy_hash);
106 // Deletes all missing packets before least unacked. The connection won't
107 // process any packets with sequence number before |least_unacked| that it
108 // received after this call. Returns true if there were missing packets before
109 // |least_unacked| unacked, false otherwise.
110 bool DontWaitForPacketsBefore(QuicPacketSequenceNumber least_unacked);
112 // TODO(satyamshekhar): Can be optimized using an interval set like data
114 // Map of received sequence numbers to their corresponding entropy.
115 // Every received packet has an entry, and packets without the entropy bit set
116 // have an entropy value of 0.
117 // TODO(ianswett): When the entropy flag is off, the entropy should not be 0.
118 ReceivedEntropyMap packets_entropy_;
120 // Cumulative hash of entropy of all received packets.
121 QuicPacketEntropyHash packets_entropy_hash_;
123 // The largest sequence number cleared by RecalculateEntropyHash.
124 // Received entropy cannot be calculated for numbers less than it.
125 QuicPacketSequenceNumber largest_sequence_number_;
128 // Track some peer state so we can do less bookkeeping.
129 // Largest sequence number that the peer has observed. Mostly received,
130 // missing in case of truncated acks.
131 QuicPacketSequenceNumber peer_largest_observed_packet_;
132 // Least sequence number which the peer is still waiting for.
133 QuicPacketSequenceNumber least_packet_awaited_by_peer_;
134 // Least sequence number of the the packet sent by the peer for which it
135 // hasn't received an ack.
136 QuicPacketSequenceNumber peer_least_packet_awaiting_ack_;
138 // Received packet information used to produce acks.
139 ReceivedPacketInfo received_info_;
141 // The time we received the largest_observed sequence number, or zero if
142 // no sequence numbers have been received since UpdateReceivedPacketInfo.
143 // Needed for calculating delta_time_largest_observed.
144 QuicTime time_largest_observed_;
146 scoped_ptr<ReceiveAlgorithmInterface> receive_algorithm_;
151 #endif // NET_QUIC_QUIC_RECEIVED_PACKET_MANAGER_H_