| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 #include "net/quic/quic_packet_entropy_manager.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "net/base/linked_hash_map.h" |
| 9 |
| 10 using std::make_pair; |
| 11 using std::max; |
| 12 |
| 13 namespace net { |
| 14 |
| 15 QuicPacketEntropyManager::QuicPacketEntropyManager() |
| 16 : sent_packets_entropy_hash_(0), |
| 17 received_packets_entropy_hash_(0), |
| 18 largest_received_sequence_number_(0) {} |
| 19 |
| 20 QuicPacketEntropyManager::~QuicPacketEntropyManager() {} |
| 21 |
| 22 void QuicPacketEntropyManager::RecordReceivedPacketEntropyHash( |
| 23 QuicPacketSequenceNumber sequence_number, |
| 24 QuicPacketEntropyHash entropy_hash) { |
| 25 largest_received_sequence_number_ = |
| 26 max<QuicPacketSequenceNumber>(largest_received_sequence_number_, |
| 27 sequence_number); |
| 28 received_packets_entropy_.insert(make_pair(sequence_number, entropy_hash)); |
| 29 received_packets_entropy_hash_ ^= entropy_hash; |
| 30 DVLOG(2) << "setting cumulative received entropy hash to: " |
| 31 << static_cast<int>(received_packets_entropy_hash_) |
| 32 << " updated with sequence number " << sequence_number |
| 33 << " entropy hash: " << static_cast<int>(entropy_hash); |
| 34 } |
| 35 |
| 36 void QuicPacketEntropyManager::RecordSentPacketEntropyHash( |
| 37 QuicPacketSequenceNumber sequence_number, |
| 38 QuicPacketEntropyHash entropy_hash) { |
| 39 // TODO(satyamshekhar): Check this logic again when/if we enable packet |
| 40 // reordering. |
| 41 sent_packets_entropy_hash_ ^= entropy_hash; |
| 42 sent_packets_entropy_.insert( |
| 43 make_pair(sequence_number, |
| 44 make_pair(entropy_hash, sent_packets_entropy_hash_))); |
| 45 DVLOG(2) << "setting cumulative sent entropy hash to: " |
| 46 << static_cast<int>(sent_packets_entropy_hash_) |
| 47 << " updated with sequence number " << sequence_number |
| 48 << " entropy hash: " << static_cast<int>(entropy_hash); |
| 49 } |
| 50 |
| 51 QuicPacketEntropyHash QuicPacketEntropyManager::ReceivedEntropyHash( |
| 52 QuicPacketSequenceNumber sequence_number) const { |
| 53 if (sequence_number == largest_received_sequence_number_) { |
| 54 return received_packets_entropy_hash_; |
| 55 } |
| 56 |
| 57 ReceivedEntropyMap::const_iterator it = |
| 58 received_packets_entropy_.upper_bound(sequence_number); |
| 59 // When this map is empty we should only query entropy for |
| 60 // |largest_received_sequence_number_|. |
| 61 LOG_IF(WARNING, it != received_packets_entropy_.end()) |
| 62 << "largest_received: " << largest_received_sequence_number_ |
| 63 << " sequence_number: " << sequence_number; |
| 64 |
| 65 // TODO(satyamshekhar): Make this O(1). |
| 66 QuicPacketEntropyHash hash = received_packets_entropy_hash_; |
| 67 for (; it != received_packets_entropy_.end(); ++it) { |
| 68 hash ^= it->second; |
| 69 } |
| 70 return hash; |
| 71 } |
| 72 |
| 73 QuicPacketEntropyHash QuicPacketEntropyManager::SentEntropyHash( |
| 74 QuicPacketSequenceNumber sequence_number) const { |
| 75 SentEntropyMap::const_iterator it = |
| 76 sent_packets_entropy_.find(sequence_number); |
| 77 if (it == sent_packets_entropy_.end()) { |
| 78 // Should only happen when we have not received ack for any packet. |
| 79 DCHECK_EQ(0u, sequence_number); |
| 80 return 0; |
| 81 } |
| 82 return it->second.second; |
| 83 } |
| 84 |
| 85 void QuicPacketEntropyManager::RecalculateReceivedEntropyHash( |
| 86 QuicPacketSequenceNumber sequence_number, |
| 87 QuicPacketEntropyHash entropy_hash) { |
| 88 received_packets_entropy_hash_ = entropy_hash; |
| 89 ReceivedEntropyMap::iterator it = |
| 90 received_packets_entropy_.lower_bound(sequence_number); |
| 91 // TODO(satyamshekhar): Make this O(1). |
| 92 for (; it != received_packets_entropy_.end(); ++it) { |
| 93 received_packets_entropy_hash_ ^= it->second; |
| 94 } |
| 95 } |
| 96 |
| 97 bool QuicPacketEntropyManager::IsValidEntropy( |
| 98 QuicPacketSequenceNumber sequence_number, |
| 99 const SequenceNumberSet& missing_packets, |
| 100 QuicPacketEntropyHash entropy_hash) const { |
| 101 SentEntropyMap::const_iterator entropy_it = |
| 102 sent_packets_entropy_.find(sequence_number); |
| 103 if (entropy_it == sent_packets_entropy_.end()) { |
| 104 DCHECK_EQ(0u, sequence_number); |
| 105 // Close connection if something goes wrong. |
| 106 return 0 == sequence_number; |
| 107 } |
| 108 QuicPacketEntropyHash expected_entropy_hash = entropy_it->second.second; |
| 109 for (SequenceNumberSet::const_iterator it = missing_packets.begin(); |
| 110 it != missing_packets.end(); ++it) { |
| 111 entropy_it = sent_packets_entropy_.find(*it); |
| 112 DCHECK(entropy_it != sent_packets_entropy_.end()); |
| 113 expected_entropy_hash ^= entropy_it->second.first; |
| 114 } |
| 115 DLOG_IF(WARNING, entropy_hash != expected_entropy_hash) |
| 116 << "Invalid entropy hash: " << static_cast<int>(entropy_hash) |
| 117 << " expected entropy hash: " << static_cast<int>(expected_entropy_hash); |
| 118 return entropy_hash == expected_entropy_hash; |
| 119 } |
| 120 |
| 121 void QuicPacketEntropyManager::ClearSentEntropyBefore( |
| 122 QuicPacketSequenceNumber sequence_number) { |
| 123 if (sent_packets_entropy_.empty()) { |
| 124 return; |
| 125 } |
| 126 SentEntropyMap::iterator it = sent_packets_entropy_.begin(); |
| 127 while (it->first < sequence_number) { |
| 128 sent_packets_entropy_.erase(it); |
| 129 it = sent_packets_entropy_.begin(); |
| 130 DCHECK(it != sent_packets_entropy_.end()); |
| 131 } |
| 132 DVLOG(2) << "Cleared entropy before: " |
| 133 << sent_packets_entropy_.begin()->first; |
| 134 } |
| 135 |
| 136 void QuicPacketEntropyManager::ClearReceivedEntropyBefore( |
| 137 QuicPacketSequenceNumber sequence_number) { |
| 138 // This might clear the received_packets_entropy_ map if the current packet |
| 139 // is peer_least_packet_awaiting_ack_ and it doesn't have entropy flag set. |
| 140 received_packets_entropy_.erase( |
| 141 received_packets_entropy_.begin(), |
| 142 received_packets_entropy_.lower_bound(sequence_number)); |
| 143 } |
| 144 |
| 145 } // namespace net |
| OLD | NEW |