| OLD | NEW |
| (Empty) |
| 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 #include "net/quic/core/quic_sent_entropy_manager.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "net/base/linked_hash_map.h" | |
| 9 #include "net/quic/core/quic_flags.h" | |
| 10 | |
| 11 using std::make_pair; | |
| 12 using std::max; | |
| 13 using std::min; | |
| 14 | |
| 15 namespace net { | |
| 16 | |
| 17 QuicSentEntropyManager::QuicSentEntropyManager() : map_offset_(1) {} | |
| 18 | |
| 19 QuicSentEntropyManager::~QuicSentEntropyManager() {} | |
| 20 | |
| 21 QuicPacketEntropyHash QuicSentEntropyManager::GetPacketEntropy( | |
| 22 QuicPacketNumber packet_number) const { | |
| 23 return packets_entropy_[packet_number - map_offset_]; | |
| 24 } | |
| 25 | |
| 26 QuicPacketNumber QuicSentEntropyManager::GetLargestPacketWithEntropy() const { | |
| 27 return map_offset_ + packets_entropy_.size() - 1; | |
| 28 } | |
| 29 | |
| 30 QuicPacketNumber QuicSentEntropyManager::GetSmallestPacketWithEntropy() const { | |
| 31 return map_offset_; | |
| 32 } | |
| 33 | |
| 34 void QuicSentEntropyManager::UpdateCumulativeEntropy( | |
| 35 QuicPacketNumber packet_number, | |
| 36 CumulativeEntropy* cumulative) const { | |
| 37 while (cumulative->packet_number < packet_number) { | |
| 38 ++cumulative->packet_number; | |
| 39 cumulative->entropy ^= GetPacketEntropy(cumulative->packet_number); | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 void QuicSentEntropyManager::RecordPacketEntropyHash( | |
| 44 QuicPacketNumber packet_number, | |
| 45 QuicPacketEntropyHash entropy_hash) { | |
| 46 if (!packets_entropy_.empty()) { | |
| 47 // Ensure packets always are recorded in order. | |
| 48 // Every packet's entropy is recorded, even if it's not sent, so there | |
| 49 // are not packet number gaps. | |
| 50 DCHECK_EQ(GetLargestPacketWithEntropy() + 1, packet_number); | |
| 51 } | |
| 52 packets_entropy_.push_back(entropy_hash); | |
| 53 DVLOG(2) << "Recorded packet number " << packet_number | |
| 54 << " with entropy hash: " << static_cast<int>(entropy_hash); | |
| 55 } | |
| 56 | |
| 57 QuicPacketEntropyHash QuicSentEntropyManager::GetCumulativeEntropy( | |
| 58 QuicPacketNumber packet_number) { | |
| 59 DCHECK_LE(last_cumulative_entropy_.packet_number, packet_number); | |
| 60 DCHECK_GE(GetLargestPacketWithEntropy(), packet_number); | |
| 61 // First the entropy for largest_observed packet number should be updated. | |
| 62 UpdateCumulativeEntropy(packet_number, &last_cumulative_entropy_); | |
| 63 return last_cumulative_entropy_.entropy; | |
| 64 } | |
| 65 | |
| 66 bool QuicSentEntropyManager::IsValidEntropy( | |
| 67 QuicPacketNumber largest_observed, | |
| 68 const PacketNumberQueue& missing_packets, | |
| 69 QuicPacketEntropyHash entropy_hash) { | |
| 70 DCHECK_GE(largest_observed, last_valid_entropy_.packet_number); | |
| 71 // Ensure the largest and smallest packet numbers are in range. | |
| 72 if (largest_observed > GetLargestPacketWithEntropy()) { | |
| 73 return false; | |
| 74 } | |
| 75 if (!missing_packets.Empty() && | |
| 76 missing_packets.Min() < GetSmallestPacketWithEntropy()) { | |
| 77 return false; | |
| 78 } | |
| 79 // First the entropy for largest_observed packet number should be updated. | |
| 80 UpdateCumulativeEntropy(largest_observed, &last_valid_entropy_); | |
| 81 | |
| 82 // Now XOR out all the missing entropies. | |
| 83 QuicPacketEntropyHash expected_entropy_hash = last_valid_entropy_.entropy; | |
| 84 for (const Interval<QuicPacketNumber>& interval : missing_packets) { | |
| 85 for (QuicPacketNumber packet_number = interval.min(); | |
| 86 packet_number < interval.max(); ++packet_number) { | |
| 87 expected_entropy_hash ^= GetPacketEntropy(packet_number); | |
| 88 } | |
| 89 } | |
| 90 DLOG_IF(WARNING, entropy_hash != expected_entropy_hash) | |
| 91 << "Invalid entropy hash: " << static_cast<int>(entropy_hash) | |
| 92 << " expected entropy hash: " << static_cast<int>(expected_entropy_hash); | |
| 93 return entropy_hash == expected_entropy_hash; | |
| 94 } | |
| 95 | |
| 96 void QuicSentEntropyManager::ClearEntropyBefore( | |
| 97 QuicPacketNumber packet_number) { | |
| 98 // Don't discard entropy before updating the cumulative entropy used to | |
| 99 // calculate EntropyHash and IsValidEntropy. | |
| 100 if (last_cumulative_entropy_.packet_number < packet_number) { | |
| 101 UpdateCumulativeEntropy(packet_number, &last_cumulative_entropy_); | |
| 102 } | |
| 103 if (last_valid_entropy_.packet_number < packet_number) { | |
| 104 UpdateCumulativeEntropy(packet_number, &last_valid_entropy_); | |
| 105 } | |
| 106 while (map_offset_ < packet_number) { | |
| 107 packets_entropy_.pop_front(); | |
| 108 ++map_offset_; | |
| 109 } | |
| 110 DVLOG(2) << "Cleared entropy before: " << packet_number; | |
| 111 } | |
| 112 | |
| 113 } // namespace net | |
| OLD | NEW |