| 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 using std::min; | |
| 13 | |
| 14 namespace net { | |
| 15 | |
| 16 QuicPacketEntropyManager::QuicPacketEntropyManager() | |
| 17 : sent_packets_entropy_hash_(0), | |
| 18 received_packets_entropy_hash_(0), | |
| 19 largest_received_sequence_number_(0) {} | |
| 20 | |
| 21 QuicPacketEntropyManager::~QuicPacketEntropyManager() {} | |
| 22 | |
| 23 QuicPacketSequenceNumber | |
| 24 QuicPacketEntropyManager::LargestReceivedSequenceNumber() const { | |
| 25 if (received_packets_entropy_.empty()) { | |
| 26 return 0; | |
| 27 } | |
| 28 return received_packets_entropy_.rbegin()->first; | |
| 29 } | |
| 30 | |
| 31 void QuicPacketEntropyManager::RecordReceivedPacketEntropyHash( | |
| 32 QuicPacketSequenceNumber sequence_number, | |
| 33 QuicPacketEntropyHash entropy_hash) { | |
| 34 if (sequence_number < largest_received_sequence_number_) { | |
| 35 DLOG(INFO) << "Ignoring received packet entropy for sequence_number:" | |
| 36 << sequence_number << " less than largest_peer_sequence_number:" | |
| 37 << largest_received_sequence_number_; | |
| 38 return; | |
| 39 } | |
| 40 received_packets_entropy_.insert(make_pair(sequence_number, entropy_hash)); | |
| 41 received_packets_entropy_hash_ ^= entropy_hash; | |
| 42 DVLOG(2) << "setting cumulative received entropy hash to: " | |
| 43 << static_cast<int>(received_packets_entropy_hash_) | |
| 44 << " updated with sequence number " << sequence_number | |
| 45 << " entropy hash: " << static_cast<int>(entropy_hash); | |
| 46 } | |
| 47 | |
| 48 void QuicPacketEntropyManager::RecordSentPacketEntropyHash( | |
| 49 QuicPacketSequenceNumber sequence_number, | |
| 50 QuicPacketEntropyHash entropy_hash) { | |
| 51 // TODO(satyamshekhar): Check this logic again when/if we enable packet | |
| 52 // reordering. | |
| 53 sent_packets_entropy_hash_ ^= entropy_hash; | |
| 54 sent_packets_entropy_.insert( | |
| 55 make_pair(sequence_number, | |
| 56 make_pair(entropy_hash, sent_packets_entropy_hash_))); | |
| 57 DVLOG(2) << "setting cumulative sent entropy hash to: " | |
| 58 << static_cast<int>(sent_packets_entropy_hash_) | |
| 59 << " updated with sequence number " << sequence_number | |
| 60 << " entropy hash: " << static_cast<int>(entropy_hash); | |
| 61 } | |
| 62 | |
| 63 QuicPacketEntropyHash QuicPacketEntropyManager::ReceivedEntropyHash( | |
| 64 QuicPacketSequenceNumber sequence_number) const { | |
| 65 DCHECK_LE(sequence_number, LargestReceivedSequenceNumber()); | |
| 66 DCHECK_GE(sequence_number, largest_received_sequence_number_); | |
| 67 if (sequence_number == LargestReceivedSequenceNumber()) { | |
| 68 return received_packets_entropy_hash_; | |
| 69 } | |
| 70 | |
| 71 ReceivedEntropyMap::const_iterator it = | |
| 72 received_packets_entropy_.upper_bound(sequence_number); | |
| 73 // When this map is empty we should only query entropy for | |
| 74 // |largest_received_sequence_number_|. | |
| 75 LOG_IF(WARNING, it != received_packets_entropy_.end()) | |
| 76 << "largest_received: " << LargestReceivedSequenceNumber() | |
| 77 << " sequence_number: " << sequence_number; | |
| 78 | |
| 79 // TODO(satyamshekhar): Make this O(1). | |
| 80 QuicPacketEntropyHash hash = received_packets_entropy_hash_; | |
| 81 for (; it != received_packets_entropy_.end(); ++it) { | |
| 82 hash ^= it->second; | |
| 83 } | |
| 84 return hash; | |
| 85 } | |
| 86 | |
| 87 QuicPacketEntropyHash QuicPacketEntropyManager::SentEntropyHash( | |
| 88 QuicPacketSequenceNumber sequence_number) const { | |
| 89 SentEntropyMap::const_iterator it = | |
| 90 sent_packets_entropy_.find(sequence_number); | |
| 91 if (it == sent_packets_entropy_.end()) { | |
| 92 // Should only happen when we have not received ack for any packet. | |
| 93 DCHECK_EQ(0u, sequence_number); | |
| 94 return 0; | |
| 95 } | |
| 96 return it->second.second; | |
| 97 } | |
| 98 | |
| 99 void QuicPacketEntropyManager::RecalculateReceivedEntropyHash( | |
| 100 QuicPacketSequenceNumber peer_least_unacked, | |
| 101 QuicPacketEntropyHash entropy_hash) { | |
| 102 DLOG_IF(WARNING, peer_least_unacked > LargestReceivedSequenceNumber()) | |
| 103 << "Prematurely updating the entropy manager before registering the " | |
| 104 << "entropy of the containing packet creates a temporary inconsistency."; | |
| 105 if (peer_least_unacked < largest_received_sequence_number_) { | |
| 106 DLOG(INFO) << "Ignoring received peer_least_unacked:" << peer_least_unacked | |
| 107 << " less than largest_peer_sequence_number:" | |
| 108 << largest_received_sequence_number_; | |
| 109 return; | |
| 110 } | |
| 111 largest_received_sequence_number_ = peer_least_unacked; | |
| 112 received_packets_entropy_hash_ = entropy_hash; | |
| 113 ReceivedEntropyMap::iterator it = | |
| 114 received_packets_entropy_.lower_bound(peer_least_unacked); | |
| 115 // TODO(satyamshekhar): Make this O(1). | |
| 116 for (; it != received_packets_entropy_.end(); ++it) { | |
| 117 received_packets_entropy_hash_ ^= it->second; | |
| 118 } | |
| 119 // Discard entropies before least unacked. | |
| 120 received_packets_entropy_.erase( | |
| 121 received_packets_entropy_.begin(), | |
| 122 received_packets_entropy_.lower_bound( | |
| 123 min(peer_least_unacked, LargestReceivedSequenceNumber()))); | |
| 124 } | |
| 125 | |
| 126 bool QuicPacketEntropyManager::IsValidEntropy( | |
| 127 QuicPacketSequenceNumber sequence_number, | |
| 128 const SequenceNumberSet& missing_packets, | |
| 129 QuicPacketEntropyHash entropy_hash) const { | |
| 130 SentEntropyMap::const_iterator entropy_it = | |
| 131 sent_packets_entropy_.find(sequence_number); | |
| 132 if (entropy_it == sent_packets_entropy_.end()) { | |
| 133 DCHECK_EQ(0u, sequence_number); | |
| 134 // Close connection if something goes wrong. | |
| 135 return 0 == sequence_number; | |
| 136 } | |
| 137 QuicPacketEntropyHash expected_entropy_hash = entropy_it->second.second; | |
| 138 for (SequenceNumberSet::const_iterator it = missing_packets.begin(); | |
| 139 it != missing_packets.end(); ++it) { | |
| 140 entropy_it = sent_packets_entropy_.find(*it); | |
| 141 DCHECK(entropy_it != sent_packets_entropy_.end()); | |
| 142 expected_entropy_hash ^= entropy_it->second.first; | |
| 143 } | |
| 144 DLOG_IF(WARNING, entropy_hash != expected_entropy_hash) | |
| 145 << "Invalid entropy hash: " << static_cast<int>(entropy_hash) | |
| 146 << " expected entropy hash: " << static_cast<int>(expected_entropy_hash); | |
| 147 return entropy_hash == expected_entropy_hash; | |
| 148 } | |
| 149 | |
| 150 void QuicPacketEntropyManager::ClearSentEntropyBefore( | |
| 151 QuicPacketSequenceNumber sequence_number) { | |
| 152 if (sent_packets_entropy_.empty()) { | |
| 153 return; | |
| 154 } | |
| 155 SentEntropyMap::iterator it = sent_packets_entropy_.begin(); | |
| 156 while (it->first < sequence_number) { | |
| 157 sent_packets_entropy_.erase(it); | |
| 158 it = sent_packets_entropy_.begin(); | |
| 159 DCHECK(it != sent_packets_entropy_.end()); | |
| 160 } | |
| 161 DVLOG(2) << "Cleared entropy before: " | |
| 162 << sent_packets_entropy_.begin()->first; | |
| 163 } | |
| 164 | |
| 165 } // namespace net | |
| OLD | NEW |