Home | History | Annotate | Download | only in quic
      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.
      4 //
      5 // Manages the packet entropy calculation for both sent and received packets
      6 // for a connection.
      7 
      8 #ifndef NET_QUIC_QUIC_RECEIVED_PACKET_MANAGER_H_
      9 #define NET_QUIC_QUIC_RECEIVED_PACKET_MANAGER_H_
     10 
     11 #include "net/quic/congestion_control/receive_algorithm_interface.h"
     12 #include "net/quic/quic_framer.h"
     13 #include "net/quic/quic_protocol.h"
     14 
     15 namespace net {
     16 
     17 namespace test {
     18 class QuicConnectionPeer;
     19 class QuicReceivedPacketManagerPeer;
     20 }  // namespace test
     21 
     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 {
     27  public:
     28   explicit QuicReceivedPacketManager(CongestionFeedbackType congestion_type);
     29   virtual ~QuicReceivedPacketManager();
     30 
     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   // revived: true if the packet was lost and then recovered with help of a
     36   // FEC packet.
     37   void RecordPacketReceived(QuicByteCount bytes,
     38                             const QuicPacketHeader& header,
     39                             QuicTime receipt_time,
     40                             bool revived);
     41 
     42   // Checks whether |sequence_number| is missing and less than largest observed.
     43   bool IsMissing(QuicPacketSequenceNumber sequence_number);
     44 
     45   // Checks if we're still waiting for the packet with |sequence_number|.
     46   bool IsAwaitingPacket(QuicPacketSequenceNumber sequence_number);
     47 
     48   // Update the |received_info| for an outgoing ack.
     49   void UpdateReceivedPacketInfo(ReceivedPacketInfo* received_info,
     50                                 QuicTime approximate_now);
     51 
     52   // Should be called before sending an ACK packet, to decide if we need
     53   // to attach a QuicCongestionFeedbackFrame block.
     54   // Returns false if no QuicCongestionFeedbackFrame block is needed.
     55   // Otherwise fills in feedback and returns true.
     56   virtual bool GenerateCongestionFeedback(
     57       QuicCongestionFeedbackFrame* feedback);
     58 
     59   // QuicReceivedEntropyHashCalculatorInterface
     60   // Called by QuicFramer, when the outgoing ack gets truncated, to recalculate
     61   // the received entropy hash for the truncated ack frame.
     62   virtual QuicPacketEntropyHash EntropyHash(
     63       QuicPacketSequenceNumber sequence_number) const OVERRIDE;
     64 
     65   // These two are called by OnAckFrame.
     66   //
     67   // Updates internal state based on |incoming_ack.received_info|.
     68   void UpdatePacketInformationReceivedByPeer(const QuicAckFrame& incoming_ack);
     69   // Updates internal state based on |incoming_ack.sent_info|.
     70   void UpdatePacketInformationSentByPeer(const QuicAckFrame& incoming_ack);
     71 
     72   // Returns whether the peer is missing packets.
     73   bool HasMissingPackets();
     74 
     75   // Returns true when there are new missing packets to be reported within 3
     76   // packets of the largest observed.
     77   bool HasNewMissingPackets();
     78 
     79   QuicPacketSequenceNumber peer_largest_observed_packet() {
     80     return peer_largest_observed_packet_;
     81   }
     82 
     83   QuicPacketSequenceNumber least_packet_awaited_by_peer() {
     84     return least_packet_awaited_by_peer_;
     85   }
     86 
     87   QuicPacketSequenceNumber peer_least_packet_awaiting_ack() {
     88     return peer_least_packet_awaiting_ack_;
     89   }
     90 
     91  private:
     92   friend class test::QuicConnectionPeer;
     93   friend class test::QuicReceivedPacketManagerPeer;
     94 
     95   typedef std::map<QuicPacketSequenceNumber,
     96                    QuicPacketEntropyHash> ReceivedEntropyMap;
     97 
     98   // Record the received entropy hash against |sequence_number|.
     99   void RecordPacketEntropyHash(QuicPacketSequenceNumber sequence_number,
    100                                QuicPacketEntropyHash entropy_hash);
    101 
    102   // Recalculate the entropy hash and clears old packet entropies,
    103   // now that the sender sent us the |entropy_hash| for packets up to,
    104   // but not including, |peer_least_unacked|.
    105   void RecalculateEntropyHash(QuicPacketSequenceNumber peer_least_unacked,
    106                               QuicPacketEntropyHash entropy_hash);
    107 
    108   // Deletes all missing packets before least unacked. The connection won't
    109   // process any packets with sequence number before |least_unacked| that it
    110   // received after this call. Returns true if there were missing packets before
    111   // |least_unacked| unacked, false otherwise.
    112   bool DontWaitForPacketsBefore(QuicPacketSequenceNumber least_unacked);
    113 
    114   // TODO(satyamshekhar): Can be optimized using an interval set like data
    115   // structure.
    116   // Map of received sequence numbers to their corresponding entropy.
    117   // Every received packet has an entry, and packets without the entropy bit set
    118   // have an entropy value of 0.
    119   // TODO(ianswett): When the entropy flag is off, the entropy should not be 0.
    120   ReceivedEntropyMap packets_entropy_;
    121 
    122   // Cumulative hash of entropy of all received packets.
    123   QuicPacketEntropyHash packets_entropy_hash_;
    124 
    125   // The largest sequence number cleared by RecalculateEntropyHash.
    126   // Received entropy cannot be calculated for numbers less than it.
    127   QuicPacketSequenceNumber largest_sequence_number_;
    128 
    129 
    130   // Track some peer state so we can do less bookkeeping.
    131   // Largest sequence number that the peer has observed. Mostly received,
    132   // missing in case of truncated acks.
    133   QuicPacketSequenceNumber peer_largest_observed_packet_;
    134   // Least sequence number which the peer is still waiting for.
    135   QuicPacketSequenceNumber least_packet_awaited_by_peer_;
    136   // Least sequence number of the the packet sent by the peer for which it
    137   // hasn't received an ack.
    138   QuicPacketSequenceNumber peer_least_packet_awaiting_ack_;
    139 
    140   // Received packet information used to produce acks.
    141   ReceivedPacketInfo received_info_;
    142 
    143   // The time we received the largest_observed sequence number, or zero if
    144   // no sequence numbers have been received since UpdateReceivedPacketInfo.
    145   // Needed for calculating delta_time_largest_observed.
    146   QuicTime time_largest_observed_;
    147 
    148   scoped_ptr<ReceiveAlgorithmInterface> receive_algorithm_;
    149 };
    150 
    151 }  // namespace net
    152 
    153 #endif  // NET_QUIC_QUIC_RECEIVED_PACKET_MANAGER_H_
    154