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_SENT_ENTROPY_MANAGER_H_
9 #define NET_QUIC_QUIC_SENT_ENTROPY_MANAGER_H_
13 #include "net/base/linked_hash_map.h"
14 #include "net/quic/quic_framer.h"
15 #include "net/quic/quic_protocol.h"
20 class QuicConnectionPeer
;
23 // Records all sent packets by a connection to track the cumulative entropy of
24 // sent packets. It is used by the connection to validate an ack
25 // frame sent by the peer as a preventive measure against the optimistic ack
27 class NET_EXPORT_PRIVATE QuicSentEntropyManager
{
29 QuicSentEntropyManager();
30 virtual ~QuicSentEntropyManager();
32 // Record |entropy_hash| for sent packet corresponding to |packet_number|.
33 void RecordPacketEntropyHash(QuicPacketNumber packet_number
,
34 QuicPacketEntropyHash entropy_hash
);
36 // Retrieves the cumulative entropy up to |packet_number|.
37 // Must always be called with a monotonically increasing |packet_number|.
38 QuicPacketEntropyHash
GetCumulativeEntropy(QuicPacketNumber packet_number
);
40 // Returns true if |entropy_hash| matches the expected sent entropy hash
41 // up to |largest_observed| removing packet numbers from |missing_packets|.
42 // Must always be called with a monotonically increasing |largest_observed|.
43 bool IsValidEntropy(QuicPacketNumber largest_observed
,
44 const PacketNumberSet
& missing_packets
,
45 QuicPacketEntropyHash entropy_hash
);
47 // Removes unnecessary entries before |packet_number|.
48 void ClearEntropyBefore(QuicPacketNumber packet_number
);
51 friend class test::QuicConnectionPeer
;
53 typedef std::deque
<QuicPacketEntropyHash
> SentEntropyMap
;
55 struct CumulativeEntropy
{
56 CumulativeEntropy() : packet_number(0), entropy(0) {}
58 QuicPacketNumber packet_number
;
59 QuicPacketEntropyHash entropy
;
62 // Convenience methods to get the largest and smallest packets with entropies.
63 QuicPacketNumber
GetLargestPacketWithEntropy() const;
64 QuicPacketNumber
GetSmallestPacketWithEntropy() const;
65 // Convenience method to get the entropy hash for |packet_number|.
66 QuicPacketEntropyHash
GetPacketEntropy(QuicPacketNumber packet_number
) const;
68 // Update the cumulative entropy to |packet_number|.
69 void UpdateCumulativeEntropy(QuicPacketNumber packet_number
,
70 CumulativeEntropy
* cumulative
) const;
72 // Maps packet numbers to the sent entropy hash for the packet number.
73 SentEntropyMap packets_entropy_
;
74 QuicPacketNumber map_offset_
;
76 // Cache the cumulative entropy for IsValidEntropy.
77 CumulativeEntropy last_valid_entropy_
;
79 // Cache the cumulative entropy for the packet number used by EntropyHash.
80 CumulativeEntropy last_cumulative_entropy_
;
82 DISALLOW_COPY_AND_ASSIGN(QuicSentEntropyManager
);
87 #endif // NET_QUIC_QUIC_SENT_ENTROPY_MANAGER_H_