Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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_connection.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | |
| 9 | #include "base/logging.h" |
| 10 | #include "base/stl_util.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 11 | #include "net/quic/crypto/quic_decrypter.h" |
| 12 | #include "net/quic/crypto/quic_encrypter.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 13 | #include "net/quic/quic_utils.h" |
| 14 | |
| 15 | using base::hash_map; |
| 16 | using base::hash_set; |
| 17 | using base::StringPiece; |
| 18 | using std::list; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 19 | using std::make_pair; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 20 | using std::min; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 21 | using std::max; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 22 | using std::vector; |
| 23 | using std::set; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 24 | using std::string; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 25 | |
| 26 | namespace net { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | // The largest gap in packets we'll accept without closing the connection. |
| 30 | // This will likely have to be tuned. |
| 31 | const QuicPacketSequenceNumber kMaxPacketGap = 5000; |
| 32 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 33 | // We want to make sure if we get a large nack packet, we don't queue up too |
| 34 | // many packets at once. 10 is arbitrary. |
| 35 | const int kMaxRetransmissionsPerAck = 10; |
| 36 | |
| 37 | // TCP retransmits after 2 nacks. We allow for a third in case of out-of-order |
| 38 | // delivery. |
| 39 | // TODO(ianswett): Change to match TCP's rule of retransmitting once an ack |
| 40 | // at least 3 sequence numbers larger arrives. |
| 41 | const size_t kNumberOfNacksBeforeRetransmission = 3; |
| 42 | |
| 43 | // The maxiumum number of packets we'd like to queue. We may end up queueing |
| 44 | // more in the case of many control frames. |
| 45 | // 6 is arbitrary. |
| 46 | const int kMaxPacketsToSerializeAtOnce = 6; |
| 47 | |
| 48 | // Limit the number of packets we send per retransmission-alarm so we |
| 49 | // eventually cede. 10 is arbitrary. |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 50 | const size_t kMaxPacketsPerRetransmissionAlarm = 10; |
| 51 | |
| 52 | // Limit the number of FEC groups to two. If we get enough out of order packets |
| 53 | // that this becomes limiting, we can revisit. |
| 54 | const size_t kMaxFecGroups = 2; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 55 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 56 | // Limit the number of undecryptable packets we buffer in |
| 57 | // expectation of the CHLO/SHLO arriving. |
| 58 | const size_t kMaxUndecryptablePackets = 10; |
| 59 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 60 | bool Near(QuicPacketSequenceNumber a, QuicPacketSequenceNumber b) { |
| 61 | QuicPacketSequenceNumber delta = (a > b) ? a - b : b - a; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 62 | return delta <= kMaxPacketGap; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 65 | } // namespace |
| 66 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 67 | #define ENDPOINT (is_server_ ? "Server: " : " Client: ") |
| 68 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 69 | QuicConnection::QuicConnection(QuicGuid guid, |
| 70 | IPEndPoint address, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 71 | QuicConnectionHelperInterface* helper, |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 72 | bool is_server, |
| 73 | QuicVersion version) |
| 74 | : framer_(version, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 75 | helper->GetClock()->ApproximateNow(), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 76 | is_server), |
Torne (Richard Coles) | b2df76e | 2013-05-13 16:52:09 +0100 | [diff] [blame] | 77 | helper_(helper), |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 78 | encryption_level_(ENCRYPTION_NONE), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 79 | clock_(helper->GetClock()), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 80 | random_generator_(helper->GetRandomGenerator()), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 81 | guid_(guid), |
| 82 | peer_address_(address), |
| 83 | largest_seen_packet_with_ack_(0), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 84 | handling_retransmission_timeout_(false), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 85 | write_blocked_(false), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 86 | debug_visitor_(NULL), |
| 87 | packet_creator_(guid_, &framer_, random_generator_, is_server), |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 88 | packet_generator_(this, NULL, &packet_creator_), |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 89 | idle_network_timeout_( |
| 90 | QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs)), |
| 91 | overall_connection_timeout_(QuicTime::Delta::Infinite()), |
| 92 | creation_time_(clock_->ApproximateNow()), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 93 | time_of_last_received_packet_(clock_->ApproximateNow()), |
| 94 | time_of_last_sent_packet_(clock_->ApproximateNow()), |
| 95 | congestion_manager_(clock_, kTCP), |
| 96 | version_negotiation_state_(START_NEGOTIATION), |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 97 | max_packets_per_retransmission_alarm_(kMaxPacketsPerRetransmissionAlarm), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 98 | is_server_(is_server), |
| 99 | connected_(true), |
| 100 | received_truncated_ack_(false), |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 101 | send_ack_in_response_to_packet_(false), |
| 102 | address_migrating_(false) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 103 | helper_->SetConnection(this); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 104 | helper_->SetTimeoutAlarm(idle_network_timeout_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 105 | framer_.set_visitor(this); |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 106 | framer_.set_received_entropy_calculator(&received_packet_manager_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 107 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 108 | /* |
| 109 | if (FLAGS_fake_packet_loss_percentage > 0) { |
| 110 | int32 seed = RandomBase::WeakSeed32(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 111 | LOG(INFO) << ENDPOINT << "Seeding packet loss with " << seed; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 112 | random_.reset(new MTRandom(seed)); |
| 113 | } |
| 114 | */ |
| 115 | } |
| 116 | |
| 117 | QuicConnection::~QuicConnection() { |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 118 | STLDeleteElements(&undecryptable_packets_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 119 | STLDeleteValues(&unacked_packets_); |
| 120 | STLDeleteValues(&group_map_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 121 | for (QueuedPacketList::iterator it = queued_packets_.begin(); |
| 122 | it != queued_packets_.end(); ++it) { |
| 123 | delete it->packet; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 127 | bool QuicConnection::SelectMutualVersion( |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 128 | const QuicVersionVector& available_versions) { |
| 129 | // Try to find the highest mutual version by iterating over supported |
| 130 | // versions, starting with the highest, and breaking out of the loop once we |
| 131 | // find a matching version in the provided available_versions vector. |
| 132 | for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { |
| 133 | const QuicVersion& version = kSupportedQuicVersions[i]; |
| 134 | if (std::find(available_versions.begin(), available_versions.end(), |
| 135 | version) != available_versions.end()) { |
| 136 | framer_.set_version(version); |
| 137 | return true; |
| 138 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 141 | return false; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 144 | void QuicConnection::OnError(QuicFramer* framer) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 145 | // Packets that we cannot decrypt are dropped. |
| 146 | // TODO(rch): add stats to measure this. |
| 147 | if (!connected_ || framer->error() == QUIC_DECRYPTION_FAILURE) { |
| 148 | return; |
| 149 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 150 | SendConnectionClose(framer->error()); |
| 151 | } |
| 152 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 153 | void QuicConnection::OnPacket() { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void QuicConnection::OnPublicResetPacket( |
| 157 | const QuicPublicResetPacket& packet) { |
| 158 | if (debug_visitor_) { |
| 159 | debug_visitor_->OnPublicResetPacket(packet); |
| 160 | } |
| 161 | CloseConnection(QUIC_PUBLIC_RESET, true); |
| 162 | } |
| 163 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 164 | bool QuicConnection::OnProtocolVersionMismatch(QuicVersion received_version) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 165 | // TODO(satyamshekhar): Implement no server state in this mode. |
| 166 | if (!is_server_) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 167 | LOG(DFATAL) << ENDPOINT << "Framer called OnProtocolVersionMismatch. " |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 168 | << "Closing connection."; |
| 169 | CloseConnection(QUIC_INTERNAL_ERROR, false); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 170 | return false; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 171 | } |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 172 | DCHECK_NE(version(), received_version); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 173 | |
| 174 | if (debug_visitor_) { |
| 175 | debug_visitor_->OnProtocolVersionMismatch(received_version); |
| 176 | } |
| 177 | |
| 178 | switch (version_negotiation_state_) { |
| 179 | case START_NEGOTIATION: |
| 180 | if (!framer_.IsSupportedVersion(received_version)) { |
| 181 | SendVersionNegotiationPacket(); |
| 182 | version_negotiation_state_ = SENT_NEGOTIATION_PACKET; |
| 183 | return false; |
| 184 | } |
| 185 | break; |
| 186 | |
| 187 | case SENT_NEGOTIATION_PACKET: |
| 188 | if (!framer_.IsSupportedVersion(received_version)) { |
| 189 | // Drop packets which can't be parsed due to version mismatch. |
| 190 | return false; |
| 191 | } |
| 192 | break; |
| 193 | |
| 194 | case NEGOTIATED_VERSION: |
| 195 | // Might be old packets that were sent by the client before the version |
| 196 | // was negotiated. Drop these. |
| 197 | return false; |
| 198 | |
| 199 | default: |
| 200 | DCHECK(false); |
| 201 | } |
| 202 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 203 | version_negotiation_state_ = NEGOTIATED_VERSION; |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 204 | |
| 205 | // Store the new version. |
| 206 | framer_.set_version(received_version); |
| 207 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 208 | // TODO(satyamshekhar): Store the sequence number of this packet and close the |
| 209 | // connection if we ever received a packet with incorrect version and whose |
| 210 | // sequence number is greater. |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | // Handles version negotiation for client connection. |
| 215 | void QuicConnection::OnVersionNegotiationPacket( |
| 216 | const QuicVersionNegotiationPacket& packet) { |
| 217 | if (is_server_) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 218 | LOG(DFATAL) << ENDPOINT << "Framer parsed VersionNegotiationPacket." |
| 219 | << " Closing connection."; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 220 | CloseConnection(QUIC_INTERNAL_ERROR, false); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 221 | return; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 222 | } |
| 223 | if (debug_visitor_) { |
| 224 | debug_visitor_->OnVersionNegotiationPacket(packet); |
| 225 | } |
| 226 | |
| 227 | if (version_negotiation_state_ == NEGOTIATED_VERSION) { |
| 228 | // Possibly a duplicate version negotiation packet. |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | if (std::find(packet.versions.begin(), |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 233 | packet.versions.end(), version()) != |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 234 | packet.versions.end()) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 235 | DLOG(WARNING) << ENDPOINT << "The server already supports our version. " |
| 236 | << "It should have accepted our connection."; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 237 | // Just drop the connection. |
| 238 | CloseConnection(QUIC_INVALID_VERSION_NEGOTIATION_PACKET, false); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | if (!SelectMutualVersion(packet.versions)) { |
| 243 | SendConnectionCloseWithDetails(QUIC_INVALID_VERSION, |
| 244 | "no common version found"); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 245 | return; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | version_negotiation_state_ = NEGOTIATED_VERSION; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 249 | RetransmitUnackedPackets(ALL_PACKETS); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void QuicConnection::OnRevivedPacket() { |
| 253 | } |
| 254 | |
| 255 | bool QuicConnection::OnPacketHeader(const QuicPacketHeader& header) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 256 | if (debug_visitor_) { |
| 257 | debug_visitor_->OnPacketHeader(header); |
| 258 | } |
| 259 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 260 | if (!ProcessValidatedPacket()) { |
| 261 | return false; |
| 262 | } |
| 263 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 264 | // Will be decrement below if we fall through to return true; |
| 265 | ++stats_.packets_dropped; |
| 266 | |
| 267 | if (header.public_header.guid != guid_) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 268 | DLOG(INFO) << ENDPOINT << "Ignoring packet from unexpected GUID: " |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 269 | << header.public_header.guid << " instead of " << guid_; |
| 270 | return false; |
| 271 | } |
| 272 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 273 | if (!Near(header.packet_sequence_number, |
| 274 | last_header_.packet_sequence_number)) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 275 | DLOG(INFO) << ENDPOINT << "Packet " << header.packet_sequence_number |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 276 | << " out of bounds. Discarding"; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 277 | SendConnectionCloseWithDetails(QUIC_INVALID_PACKET_HEADER, |
| 278 | "Packet sequence number out of bounds"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 279 | return false; |
| 280 | } |
| 281 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 282 | // If this packet has already been seen, or that the sender |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 283 | // has told us will not be retransmitted, then stop processing the packet. |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 284 | if (!received_packet_manager_.IsAwaitingPacket( |
| 285 | header.packet_sequence_number)) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 286 | return false; |
| 287 | } |
| 288 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 289 | if (version_negotiation_state_ != NEGOTIATED_VERSION) { |
| 290 | if (is_server_) { |
| 291 | if (!header.public_header.version_flag) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 292 | DLOG(WARNING) << ENDPOINT << "Got packet without version flag before " |
| 293 | << "version negotiated."; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 294 | // Packets should have the version flag till version negotiation is |
| 295 | // done. |
| 296 | CloseConnection(QUIC_INVALID_VERSION, false); |
| 297 | return false; |
| 298 | } else { |
| 299 | DCHECK_EQ(1u, header.public_header.versions.size()); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 300 | DCHECK_EQ(header.public_header.versions[0], version()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 301 | version_negotiation_state_ = NEGOTIATED_VERSION; |
| 302 | } |
| 303 | } else { |
| 304 | DCHECK(!header.public_header.version_flag); |
| 305 | // If the client gets a packet without the version flag from the server |
| 306 | // it should stop sending version since the version negotiation is done. |
| 307 | packet_creator_.StopSendingVersion(); |
| 308 | version_negotiation_state_ = NEGOTIATED_VERSION; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | DCHECK_EQ(NEGOTIATED_VERSION, version_negotiation_state_); |
| 313 | |
| 314 | --stats_.packets_dropped; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 315 | DVLOG(1) << ENDPOINT << "Received packet header: " << header; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 316 | last_header_ = header; |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | void QuicConnection::OnFecProtectedPayload(StringPiece payload) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 321 | DCHECK_EQ(IN_FEC_GROUP, last_header_.is_in_fec_group); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 322 | DCHECK_NE(0u, last_header_.fec_group); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 323 | QuicFecGroup* group = GetFecGroup(); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 324 | if (group != NULL) { |
| 325 | group->Update(last_header_, payload); |
| 326 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 329 | bool QuicConnection::OnStreamFrame(const QuicStreamFrame& frame) { |
| 330 | DCHECK(connected_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 331 | if (debug_visitor_) { |
| 332 | debug_visitor_->OnStreamFrame(frame); |
| 333 | } |
| 334 | last_stream_frames_.push_back(frame); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 335 | return true; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 338 | bool QuicConnection::OnAckFrame(const QuicAckFrame& incoming_ack) { |
| 339 | DCHECK(connected_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 340 | if (debug_visitor_) { |
| 341 | debug_visitor_->OnAckFrame(incoming_ack); |
| 342 | } |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 343 | DVLOG(1) << ENDPOINT << "OnAckFrame: " << incoming_ack; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 344 | |
| 345 | if (last_header_.packet_sequence_number <= largest_seen_packet_with_ack_) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 346 | DLOG(INFO) << ENDPOINT << "Received an old ack frame: ignoring"; |
| 347 | return true; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 348 | } |
| 349 | largest_seen_packet_with_ack_ = last_header_.packet_sequence_number; |
| 350 | |
| 351 | if (!ValidateAckFrame(incoming_ack)) { |
| 352 | SendConnectionClose(QUIC_INVALID_ACK_DATA); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 353 | return false; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 356 | received_truncated_ack_ = |
| 357 | incoming_ack.received_info.missing_packets.size() >= |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 358 | QuicFramer::GetMaxUnackedPackets(last_header_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 359 | |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 360 | received_packet_manager_.UpdatePacketInformationReceivedByPeer(incoming_ack); |
| 361 | received_packet_manager_.UpdatePacketInformationSentByPeer(incoming_ack); |
| 362 | // Possibly close any FecGroups which are now irrelevant. |
| 363 | CloseFecGroupsBefore(incoming_ack.sent_info.least_unacked + 1); |
| 364 | |
| 365 | sent_entropy_manager_.ClearEntropyBefore( |
| 366 | received_packet_manager_.least_packet_awaited_by_peer() - 1); |
| 367 | |
| 368 | SequenceNumberSet acked_packets; |
| 369 | HandleAckForSentPackets(incoming_ack, &acked_packets); |
| 370 | HandleAckForSentFecPackets(incoming_ack, &acked_packets); |
| 371 | if (acked_packets.size() > 0) { |
| 372 | visitor_->OnAck(acked_packets); |
| 373 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 374 | congestion_manager_.OnIncomingAckFrame(incoming_ack, |
| 375 | time_of_last_received_packet_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 376 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 377 | // Now the we have received an ack, we might be able to send packets which are |
| 378 | // queued locally, or drain streams which are blocked. |
| 379 | QuicTime::Delta delay = congestion_manager_.TimeUntilSend( |
| 380 | time_of_last_received_packet_, NOT_RETRANSMISSION, |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 381 | HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 382 | if (delay.IsZero()) { |
| 383 | helper_->UnregisterSendAlarmIfRegistered(); |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 384 | WriteIfNotBlocked(); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 385 | } else if (!delay.IsInfinite()) { |
| 386 | helper_->SetSendAlarm(time_of_last_received_packet_.Add(delay)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 387 | } |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 388 | return connected_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 391 | bool QuicConnection::OnCongestionFeedbackFrame( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 392 | const QuicCongestionFeedbackFrame& feedback) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 393 | DCHECK(connected_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 394 | if (debug_visitor_) { |
| 395 | debug_visitor_->OnCongestionFeedbackFrame(feedback); |
| 396 | } |
| 397 | congestion_manager_.OnIncomingQuicCongestionFeedbackFrame( |
| 398 | feedback, time_of_last_received_packet_); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 399 | return connected_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 402 | bool QuicConnection::ValidateAckFrame(const QuicAckFrame& incoming_ack) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 403 | if (incoming_ack.received_info.largest_observed > |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 404 | packet_creator_.sequence_number()) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 405 | DLOG(ERROR) << ENDPOINT << "Peer's observed unsent packet:" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 406 | << incoming_ack.received_info.largest_observed << " vs " |
| 407 | << packet_creator_.sequence_number(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 408 | // We got an error for data we have not sent. Error out. |
| 409 | return false; |
| 410 | } |
| 411 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 412 | if (incoming_ack.received_info.largest_observed < |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 413 | received_packet_manager_.peer_largest_observed_packet()) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 414 | DLOG(ERROR) << ENDPOINT << "Peer's largest_observed packet decreased:" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 415 | << incoming_ack.received_info.largest_observed << " vs " |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 416 | << received_packet_manager_.peer_largest_observed_packet(); |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 417 | // A new ack has a diminished largest_observed value. Error out. |
| 418 | // If this was an old packet, we wouldn't even have checked. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 419 | return false; |
| 420 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 421 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 422 | // We can't have too many unacked packets, or our ack frames go over |
| 423 | // kMaxPacketSize. |
| 424 | DCHECK_LE(incoming_ack.received_info.missing_packets.size(), |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 425 | QuicFramer::GetMaxUnackedPackets(last_header_)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 426 | |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 427 | if (incoming_ack.sent_info.least_unacked < |
| 428 | received_packet_manager_.peer_least_packet_awaiting_ack()) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 429 | DLOG(ERROR) << ENDPOINT << "Peer's sent low least_unacked: " |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 430 | << incoming_ack.sent_info.least_unacked << " vs " |
| 431 | << received_packet_manager_.peer_least_packet_awaiting_ack(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 432 | // We never process old ack frames, so this number should only increase. |
| 433 | return false; |
| 434 | } |
| 435 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 436 | if (incoming_ack.sent_info.least_unacked > |
| 437 | last_header_.packet_sequence_number) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 438 | DLOG(ERROR) << ENDPOINT << "Peer sent least_unacked:" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 439 | << incoming_ack.sent_info.least_unacked |
| 440 | << " greater than the enclosing packet sequence number:" |
| 441 | << last_header_.packet_sequence_number; |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | if (!incoming_ack.received_info.missing_packets.empty() && |
| 446 | *incoming_ack.received_info.missing_packets.rbegin() > |
| 447 | incoming_ack.received_info.largest_observed) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 448 | DLOG(ERROR) << ENDPOINT << "Peer sent missing packet: " |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 449 | << *incoming_ack.received_info.missing_packets.rbegin() |
| 450 | << " greater than largest observed: " |
| 451 | << incoming_ack.received_info.largest_observed; |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | if (!incoming_ack.received_info.missing_packets.empty() && |
| 456 | *incoming_ack.received_info.missing_packets.begin() < |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 457 | received_packet_manager_.least_packet_awaited_by_peer()) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 458 | DLOG(ERROR) << ENDPOINT << "Peer sent missing packet: " |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 459 | << *incoming_ack.received_info.missing_packets.begin() |
| 460 | << "smaller than least_packet_awaited_by_peer_: " |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 461 | << received_packet_manager_.least_packet_awaited_by_peer(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 462 | return false; |
| 463 | } |
| 464 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 465 | if (!sent_entropy_manager_.IsValidEntropy( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 466 | incoming_ack.received_info.largest_observed, |
| 467 | incoming_ack.received_info.missing_packets, |
| 468 | incoming_ack.received_info.entropy_hash)) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 469 | DLOG(ERROR) << ENDPOINT << "Peer sent invalid entropy."; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 470 | return false; |
| 471 | } |
| 472 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 473 | return true; |
| 474 | } |
| 475 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 476 | void QuicConnection::HandleAckForSentPackets(const QuicAckFrame& incoming_ack, |
| 477 | SequenceNumberSet* acked_packets) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 478 | int retransmitted_packets = 0; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 479 | // Go through the packets we have not received an ack for and see if this |
| 480 | // incoming_ack shows they've been seen by the peer. |
| 481 | UnackedPacketMap::iterator it = unacked_packets_.begin(); |
| 482 | while (it != unacked_packets_.end()) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 483 | QuicPacketSequenceNumber sequence_number = it->first; |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 484 | if (sequence_number > |
| 485 | received_packet_manager_.peer_largest_observed_packet()) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 486 | // These are very new sequence_numbers. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 487 | break; |
| 488 | } |
| 489 | RetransmittableFrames* unacked = it->second; |
| 490 | if (!IsAwaitingPacket(incoming_ack.received_info, sequence_number)) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 491 | // Packet was acked, so remove it from our unacked packet list. |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 492 | DVLOG(1) << ENDPOINT <<"Got an ack for packet " << sequence_number; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 493 | acked_packets->insert(sequence_number); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 494 | delete unacked; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 495 | unacked_packets_.erase(it++); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 496 | retransmission_map_.erase(sequence_number); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 497 | } else { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 498 | // This is a packet which we planned on retransmitting and has not been |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 499 | // seen at the time of this ack being sent out. See if it's our new |
| 500 | // lowest unacked packet. |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 501 | DVLOG(1) << ENDPOINT << "still missing packet " << sequence_number; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 502 | ++it; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 503 | // The peer got packets after this sequence number. This is an explicit |
| 504 | // nack. |
| 505 | RetransmissionMap::iterator retransmission_it = |
| 506 | retransmission_map_.find(sequence_number); |
| 507 | ++(retransmission_it->second.number_nacks); |
| 508 | if (retransmission_it->second.number_nacks >= |
| 509 | kNumberOfNacksBeforeRetransmission && |
| 510 | retransmitted_packets < kMaxRetransmissionsPerAck) { |
| 511 | ++retransmitted_packets; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 512 | DVLOG(1) << ENDPOINT << "Trying to retransmit packet " |
| 513 | << sequence_number |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 514 | << " as it has been nacked 3 or more times."; |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 515 | // RetransmitPacket will retransmit with a new sequence_number. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 516 | RetransmitPacket(sequence_number); |
| 517 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | void QuicConnection::HandleAckForSentFecPackets( |
| 523 | const QuicAckFrame& incoming_ack, SequenceNumberSet* acked_packets) { |
| 524 | UnackedPacketMap::iterator it = unacked_fec_packets_.begin(); |
| 525 | while (it != unacked_fec_packets_.end()) { |
| 526 | QuicPacketSequenceNumber sequence_number = it->first; |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 527 | if (sequence_number > |
| 528 | received_packet_manager_.peer_largest_observed_packet()) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 529 | break; |
| 530 | } |
| 531 | if (!IsAwaitingPacket(incoming_ack.received_info, sequence_number)) { |
| 532 | DVLOG(1) << ENDPOINT << "Got an ack for fec packet: " << sequence_number; |
| 533 | acked_packets->insert(sequence_number); |
| 534 | unacked_fec_packets_.erase(it++); |
| 535 | } else { |
| 536 | DVLOG(1) << ENDPOINT << "Still missing ack for fec packet: " |
| 537 | << sequence_number; |
| 538 | ++it; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 543 | void QuicConnection::OnFecData(const QuicFecData& fec) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 544 | DCHECK_EQ(IN_FEC_GROUP, last_header_.is_in_fec_group); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 545 | DCHECK_NE(0u, last_header_.fec_group); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 546 | QuicFecGroup* group = GetFecGroup(); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 547 | if (group != NULL) { |
| 548 | group->UpdateFec(last_header_.packet_sequence_number, |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 549 | last_header_.entropy_flag, fec); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 550 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 553 | bool QuicConnection::OnRstStreamFrame(const QuicRstStreamFrame& frame) { |
| 554 | DCHECK(connected_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 555 | if (debug_visitor_) { |
| 556 | debug_visitor_->OnRstStreamFrame(frame); |
| 557 | } |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 558 | DLOG(INFO) << ENDPOINT << "Stream reset with error " |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 559 | << QuicUtils::StreamErrorToString(frame.error_code); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 560 | visitor_->OnRstStream(frame); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 561 | return connected_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 564 | bool QuicConnection::OnConnectionCloseFrame( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 565 | const QuicConnectionCloseFrame& frame) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 566 | DCHECK(connected_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 567 | if (debug_visitor_) { |
| 568 | debug_visitor_->OnConnectionCloseFrame(frame); |
| 569 | } |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 570 | DLOG(INFO) << ENDPOINT << "Connection closed with error " |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 571 | << QuicUtils::ErrorToString(frame.error_code) |
| 572 | << " " << frame.error_details; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 573 | CloseConnection(frame.error_code, true); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 574 | return false; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 577 | bool QuicConnection::OnGoAwayFrame(const QuicGoAwayFrame& frame) { |
| 578 | DCHECK(connected_); |
| 579 | DLOG(INFO) << ENDPOINT << "Go away received with error " |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 580 | << QuicUtils::ErrorToString(frame.error_code) |
| 581 | << " and reason:" << frame.reason_phrase; |
| 582 | visitor_->OnGoAway(frame); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 583 | return connected_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void QuicConnection::OnPacketComplete() { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 587 | // Don't do anything if this packet closed the connection. |
| 588 | if (!connected_) { |
| 589 | last_stream_frames_.clear(); |
| 590 | return; |
| 591 | } |
| 592 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 593 | if (!last_packet_revived_) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 594 | DLOG(INFO) << ENDPOINT << "Got packet " |
| 595 | << last_header_.packet_sequence_number |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 596 | << " with " << last_stream_frames_.size() |
| 597 | << " stream frames for " << last_header_.public_header.guid; |
| 598 | congestion_manager_.RecordIncomingPacket( |
| 599 | last_size_, last_header_.packet_sequence_number, |
| 600 | time_of_last_received_packet_, last_packet_revived_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 601 | } else { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 602 | DLOG(INFO) << ENDPOINT << "Got revived packet with " |
| 603 | << last_stream_frames_.size() << " frames."; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 606 | if ((last_stream_frames_.empty() || |
| 607 | visitor_->OnPacket(self_address_, peer_address_, |
| 608 | last_header_, last_stream_frames_))) { |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 609 | received_packet_manager_.RecordPacketReceived( |
| 610 | last_header_, time_of_last_received_packet_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 611 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 612 | |
| 613 | MaybeSendAckInResponseToPacket(); |
| 614 | last_stream_frames_.clear(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 617 | QuicAckFrame* QuicConnection::CreateAckFrame() { |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 618 | QuicAckFrame* outgoing_ack = new QuicAckFrame(); |
| 619 | received_packet_manager_.UpdateReceivedPacketInfo( |
| 620 | &(outgoing_ack->received_info), clock_->ApproximateNow()); |
| 621 | UpdateSentPacketInfo(&(outgoing_ack->sent_info)); |
| 622 | DVLOG(1) << ENDPOINT << "Creating ack frame: " << *outgoing_ack; |
| 623 | return outgoing_ack; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 624 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 625 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 626 | QuicCongestionFeedbackFrame* QuicConnection::CreateFeedbackFrame() { |
| 627 | return new QuicCongestionFeedbackFrame(outgoing_congestion_feedback_); |
| 628 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 629 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 630 | void QuicConnection::MaybeSendAckInResponseToPacket() { |
| 631 | if (send_ack_in_response_to_packet_) { |
| 632 | SendAck(); |
| 633 | } else if (!last_stream_frames_.empty()) { |
| 634 | // TODO(alyssar) this case should really be "if the packet contained any |
| 635 | // non-ack frame", rather than "if the packet contained a stream frame" |
| 636 | helper_->SetAckAlarm(congestion_manager_.DefaultRetransmissionTime()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 637 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 638 | send_ack_in_response_to_packet_ = !send_ack_in_response_to_packet_; |
| 639 | } |
| 640 | |
| 641 | void QuicConnection::SendVersionNegotiationPacket() { |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 642 | QuicVersionVector supported_versions; |
| 643 | for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { |
| 644 | supported_versions.push_back(kSupportedQuicVersions[i]); |
| 645 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 646 | QuicEncryptedPacket* encrypted = |
| 647 | packet_creator_.SerializeVersionNegotiationPacket(supported_versions); |
| 648 | // TODO(satyamshekhar): implement zero server state negotiation. |
| 649 | int error; |
| 650 | helper_->WritePacketToWire(*encrypted, &error); |
| 651 | delete encrypted; |
| 652 | } |
| 653 | |
| 654 | QuicConsumedData QuicConnection::SendStreamData(QuicStreamId id, |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 655 | StringPiece data, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 656 | QuicStreamOffset offset, |
| 657 | bool fin) { |
| 658 | return packet_generator_.ConsumeData(id, data, offset, fin); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | void QuicConnection::SendRstStream(QuicStreamId id, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 662 | QuicRstStreamErrorCode error) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 663 | packet_generator_.AddControlFrame( |
| 664 | QuicFrame(new QuicRstStreamFrame(id, error))); |
| 665 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 666 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 667 | const QuicConnectionStats& QuicConnection::GetStats() { |
| 668 | // Update rtt and estimated bandwidth. |
| 669 | stats_.rtt = congestion_manager_.SmoothedRtt().ToMicroseconds(); |
| 670 | stats_.estimated_bandwidth = |
| 671 | congestion_manager_.BandwidthEstimate().ToBytesPerSecond(); |
| 672 | return stats_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | void QuicConnection::ProcessUdpPacket(const IPEndPoint& self_address, |
| 676 | const IPEndPoint& peer_address, |
| 677 | const QuicEncryptedPacket& packet) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 678 | if (!connected_) { |
| 679 | return; |
| 680 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 681 | if (debug_visitor_) { |
| 682 | debug_visitor_->OnPacketReceived(self_address, peer_address, packet); |
| 683 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 684 | last_packet_revived_ = false; |
| 685 | last_size_ = packet.length(); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 686 | |
| 687 | address_migrating_ = false; |
| 688 | |
| 689 | if (peer_address_.address().empty()) { |
| 690 | peer_address_ = peer_address; |
| 691 | } |
| 692 | if (self_address_.address().empty()) { |
| 693 | self_address_ = self_address; |
| 694 | } |
| 695 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 696 | if (!(peer_address == peer_address_ && self_address == self_address_)) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 697 | address_migrating_ = true; |
| 698 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 699 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 700 | stats_.bytes_received += packet.length(); |
| 701 | ++stats_.packets_received; |
| 702 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 703 | if (!framer_.ProcessPacket(packet)) { |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 704 | // If we are unable to decrypt this packet, it might be |
| 705 | // because the CHLO or SHLO packet was lost. |
| 706 | if (encryption_level_ != ENCRYPTION_FORWARD_SECURE && |
| 707 | framer_.error() == QUIC_DECRYPTION_FAILURE && |
| 708 | undecryptable_packets_.size() < kMaxUndecryptablePackets) { |
| 709 | QueueUndecryptablePacket(packet); |
| 710 | } |
| 711 | DVLOG(1) << ENDPOINT << "Unable to process packet. Last packet processed: " |
| 712 | << last_header_.packet_sequence_number; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 713 | return; |
| 714 | } |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 715 | MaybeProcessUndecryptablePackets(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 716 | MaybeProcessRevivedPacket(); |
| 717 | } |
| 718 | |
| 719 | bool QuicConnection::OnCanWrite() { |
| 720 | write_blocked_ = false; |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 721 | return DoWrite(); |
| 722 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 723 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 724 | bool QuicConnection::WriteIfNotBlocked() { |
| 725 | if (write_blocked_) { |
| 726 | return false; |
| 727 | } |
| 728 | return DoWrite(); |
| 729 | } |
| 730 | |
| 731 | bool QuicConnection::DoWrite() { |
| 732 | DCHECK(!write_blocked_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 733 | WriteQueuedPackets(); |
| 734 | |
| 735 | // Sending queued packets may have caused the socket to become write blocked, |
| 736 | // or the congestion manager to prohibit sending. If we've sent everything |
| 737 | // we had queued and we're still not blocked, let the visitor know it can |
| 738 | // write more. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 739 | if (CanWrite(NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, |
| 740 | NOT_HANDSHAKE)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 741 | packet_generator_.StartBatchOperations(); |
| 742 | bool all_bytes_written = visitor_->OnCanWrite(); |
| 743 | packet_generator_.FinishBatchOperations(); |
| 744 | |
| 745 | // After the visitor writes, it may have caused the socket to become write |
| 746 | // blocked or the congestion manager to prohibit sending, so check again. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 747 | if (!write_blocked_ && !all_bytes_written && |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 748 | CanWrite(NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, |
| 749 | NOT_HANDSHAKE)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 750 | // We're not write blocked, but some stream didn't write out all of its |
| 751 | // bytes. Register for 'immediate' resumption so we'll keep writing after |
| 752 | // other quic connections have had a chance to use the socket. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 753 | helper_->SetSendAlarm(clock_->ApproximateNow()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
| 757 | return !write_blocked_; |
| 758 | } |
| 759 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 760 | bool QuicConnection::ProcessValidatedPacket() { |
| 761 | if (address_migrating_) { |
| 762 | SendConnectionCloseWithDetails( |
| 763 | QUIC_ERROR_MIGRATING_ADDRESS, |
| 764 | "Address migration is not yet a supported feature"); |
| 765 | return false; |
| 766 | } |
| 767 | time_of_last_received_packet_ = clock_->Now(); |
| 768 | DVLOG(1) << ENDPOINT << "time of last received packet: " |
| 769 | << time_of_last_received_packet_.ToDebuggingValue(); |
| 770 | return true; |
| 771 | } |
| 772 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 773 | bool QuicConnection::WriteQueuedPackets() { |
| 774 | DCHECK(!write_blocked_); |
| 775 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 776 | size_t num_queued_packets = queued_packets_.size() + 1; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 777 | QueuedPacketList::iterator packet_iterator = queued_packets_.begin(); |
| 778 | while (!write_blocked_ && packet_iterator != queued_packets_.end()) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 779 | // Ensure that from one iteration of this loop to the next we |
| 780 | // succeeded in sending a packet so we don't infinitely loop. |
| 781 | // TODO(rch): clean up and close the connection if we really hit this. |
| 782 | DCHECK_LT(queued_packets_.size(), num_queued_packets); |
| 783 | num_queued_packets = queued_packets_.size(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 784 | if (WritePacket(packet_iterator->encryption_level, |
| 785 | packet_iterator->sequence_number, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 786 | packet_iterator->packet, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 787 | packet_iterator->retransmittable, |
| 788 | NO_FORCE)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 789 | packet_iterator = queued_packets_.erase(packet_iterator); |
| 790 | } else { |
| 791 | // Continue, because some queued packets may still be writable. |
| 792 | // This can happen if a retransmit send fail. |
| 793 | ++packet_iterator; |
| 794 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 795 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 796 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 797 | return !write_blocked_; |
| 798 | } |
| 799 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 800 | bool QuicConnection::MaybeRetransmitPacketForRTO( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 801 | QuicPacketSequenceNumber sequence_number) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 802 | DCHECK_EQ(ContainsKey(unacked_packets_, sequence_number), |
| 803 | ContainsKey(retransmission_map_, sequence_number)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 804 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 805 | if (!ContainsKey(unacked_packets_, sequence_number)) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 806 | DVLOG(2) << ENDPOINT << "alarm fired for " << sequence_number |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 807 | << " but it has been acked or already retransmitted with" |
| 808 | << " different sequence number."; |
| 809 | // So no extra delay is added for this packet. |
| 810 | return true; |
| 811 | } |
| 812 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 813 | RetransmissionMap::iterator retransmission_it = |
| 814 | retransmission_map_.find(sequence_number); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 815 | // If the packet hasn't been acked and we're getting truncated acks, ignore |
| 816 | // any RTO for packets larger than the peer's largest observed packet; it may |
| 817 | // have been received by the peer and just wasn't acked due to the ack frame |
| 818 | // running out of space. |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 819 | if (received_truncated_ack_ && sequence_number > |
| 820 | received_packet_manager_.peer_largest_observed_packet() && |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 821 | // We allow retransmission of already retransmitted packets so that we |
| 822 | // retransmit packets that were retransmissions of the packet with |
| 823 | // sequence number < the largest observed field of the truncated ack. |
| 824 | retransmission_it->second.number_retransmissions == 0) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 825 | return false; |
| 826 | } else { |
| 827 | ++stats_.rto_count; |
| 828 | RetransmitPacket(sequence_number); |
| 829 | return true; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 830 | } |
| 831 | } |
| 832 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 833 | void QuicConnection::RetransmitUnackedPackets( |
| 834 | RetransmissionType retransmission_type) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 835 | if (unacked_packets_.empty()) { |
| 836 | return; |
| 837 | } |
| 838 | UnackedPacketMap::iterator next_it = unacked_packets_.begin(); |
| 839 | QuicPacketSequenceNumber end_sequence_number = |
| 840 | unacked_packets_.rbegin()->first; |
| 841 | do { |
| 842 | UnackedPacketMap::iterator current_it = next_it; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 843 | ++next_it; |
| 844 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 845 | if (retransmission_type == ALL_PACKETS || |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 846 | current_it->second->encryption_level() == ENCRYPTION_INITIAL) { |
| 847 | // TODO(satyamshekhar): Think about congestion control here. |
| 848 | // Specifically, about the retransmission count of packets being sent |
| 849 | // proactively to achieve 0 (minimal) RTT. |
| 850 | RetransmitPacket(current_it->first); |
| 851 | } |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 852 | } while (next_it != unacked_packets_.end() && |
| 853 | next_it->first <= end_sequence_number); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | void QuicConnection::RetransmitPacket( |
| 857 | QuicPacketSequenceNumber sequence_number) { |
| 858 | UnackedPacketMap::iterator unacked_it = |
| 859 | unacked_packets_.find(sequence_number); |
| 860 | RetransmissionMap::iterator retransmission_it = |
| 861 | retransmission_map_.find(sequence_number); |
| 862 | // There should always be an entry corresponding to |sequence_number| in |
| 863 | // both |retransmission_map_| and |unacked_packets_|. Retransmissions due to |
| 864 | // RTO for sequence numbers that are already acked or retransmitted are |
| 865 | // ignored by MaybeRetransmitPacketForRTO. |
| 866 | DCHECK(unacked_it != unacked_packets_.end()); |
| 867 | DCHECK(retransmission_it != retransmission_map_.end()); |
| 868 | RetransmittableFrames* unacked = unacked_it->second; |
| 869 | // TODO(pwestin): Need to fix potential issue with FEC and a 1 packet |
| 870 | // congestion window see b/8331807 for details. |
| 871 | congestion_manager_.AbandoningPacket(sequence_number); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 872 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 873 | // Re-packetize the frames with a new sequence number for retransmission. |
| 874 | // Retransmitted data packets do not use FEC, even when it's enabled. |
| 875 | SerializedPacket serialized_packet = |
| 876 | packet_creator_.SerializeAllFrames(unacked->frames()); |
| 877 | RetransmissionInfo retransmission_info(serialized_packet.sequence_number); |
| 878 | retransmission_info.number_retransmissions = |
| 879 | retransmission_it->second.number_retransmissions + 1; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 880 | // Remove info with old sequence number. |
| 881 | unacked_packets_.erase(unacked_it); |
| 882 | retransmission_map_.erase(retransmission_it); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 883 | DVLOG(1) << ENDPOINT << "Retransmitting unacked packet " << sequence_number |
| 884 | << " as " << serialized_packet.sequence_number; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 885 | DCHECK(unacked_packets_.empty() || |
| 886 | unacked_packets_.rbegin()->first < serialized_packet.sequence_number); |
| 887 | unacked_packets_.insert(make_pair(serialized_packet.sequence_number, |
| 888 | unacked)); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 889 | retransmission_map_.insert(make_pair(serialized_packet.sequence_number, |
| 890 | retransmission_info)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 891 | SendOrQueuePacket(unacked->encryption_level(), |
| 892 | serialized_packet.sequence_number, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 893 | serialized_packet.packet, |
| 894 | serialized_packet.entropy_hash, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 895 | HAS_RETRANSMITTABLE_DATA); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 898 | bool QuicConnection::CanWrite(Retransmission retransmission, |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 899 | HasRetransmittableData retransmittable, |
| 900 | IsHandshake handshake) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 901 | // TODO(ianswett): If the packet is a retransmit, the current send alarm may |
| 902 | // be too long. |
| 903 | if (write_blocked_ || helper_->IsSendAlarmSet()) { |
| 904 | return false; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 907 | QuicTime now = clock_->Now(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 908 | QuicTime::Delta delay = congestion_manager_.TimeUntilSend( |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 909 | now, retransmission, retransmittable, handshake); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 910 | if (delay.IsInfinite()) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 911 | return false; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | // If the scheduler requires a delay, then we can not send this packet now. |
| 915 | if (!delay.IsZero()) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 916 | helper_->SetSendAlarm(now.Add(delay)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 917 | return false; |
| 918 | } |
| 919 | return true; |
| 920 | } |
| 921 | |
| 922 | bool QuicConnection::IsRetransmission( |
| 923 | QuicPacketSequenceNumber sequence_number) { |
| 924 | RetransmissionMap::iterator it = retransmission_map_.find(sequence_number); |
| 925 | return it != retransmission_map_.end() && |
| 926 | it->second.number_retransmissions > 0; |
| 927 | } |
| 928 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 929 | void QuicConnection::SetupRetransmission( |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 930 | QuicPacketSequenceNumber sequence_number, |
| 931 | EncryptionLevel level) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 932 | RetransmissionMap::iterator it = retransmission_map_.find(sequence_number); |
| 933 | if (it == retransmission_map_.end()) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 934 | DVLOG(1) << ENDPOINT << "Will not retransmit packet " << sequence_number; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 935 | return; |
| 936 | } |
| 937 | |
| 938 | RetransmissionInfo retransmission_info = it->second; |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 939 | // TODO(rch): consider using a much smaller retransmisison_delay |
| 940 | // for the ENCRYPTION_NONE packets. |
| 941 | size_t effective_retransmission_count = |
| 942 | level == ENCRYPTION_NONE ? 0 : retransmission_info.number_retransmissions; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 943 | QuicTime::Delta retransmission_delay = |
| 944 | congestion_manager_.GetRetransmissionDelay( |
| 945 | unacked_packets_.size(), |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 946 | effective_retransmission_count); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 947 | |
| 948 | retransmission_timeouts_.push(RetransmissionTime( |
| 949 | sequence_number, |
| 950 | clock_->ApproximateNow().Add(retransmission_delay), |
| 951 | false)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 952 | |
| 953 | // Do not set the retransmisson alarm if we're already handling the |
| 954 | // retransmission alarm because the retransmission alarm will be reset when |
| 955 | // OnRetransmissionTimeout completes. |
| 956 | if (!handling_retransmission_timeout_) { |
| 957 | helper_->SetRetransmissionAlarm(retransmission_delay); |
| 958 | } |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 959 | // TODO(satyamshekhar): restore packet reordering with Ian's TODO in |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 960 | // SendStreamData(). |
| 961 | } |
| 962 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 963 | void QuicConnection::SetupAbandonFecTimer( |
| 964 | QuicPacketSequenceNumber sequence_number) { |
| 965 | DCHECK(ContainsKey(unacked_fec_packets_, sequence_number)); |
| 966 | QuicTime::Delta retransmission_delay = |
| 967 | QuicTime::Delta::FromMilliseconds( |
| 968 | congestion_manager_.DefaultRetransmissionTime().ToMilliseconds() * 3); |
| 969 | retransmission_timeouts_.push(RetransmissionTime( |
| 970 | sequence_number, |
| 971 | clock_->ApproximateNow().Add(retransmission_delay), |
| 972 | true)); |
| 973 | } |
| 974 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 975 | void QuicConnection::DropPacket(QuicPacketSequenceNumber sequence_number) { |
| 976 | UnackedPacketMap::iterator unacked_it = |
| 977 | unacked_packets_.find(sequence_number); |
| 978 | // Packet was not meant to be retransmitted. |
| 979 | if (unacked_it == unacked_packets_.end()) { |
| 980 | DCHECK(!ContainsKey(retransmission_map_, sequence_number)); |
| 981 | return; |
| 982 | } |
| 983 | // Delete the unacked packet. |
| 984 | delete unacked_it->second; |
| 985 | unacked_packets_.erase(unacked_it); |
| 986 | retransmission_map_.erase(sequence_number); |
| 987 | return; |
| 988 | } |
| 989 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 990 | bool QuicConnection::WritePacket(EncryptionLevel level, |
| 991 | QuicPacketSequenceNumber sequence_number, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 992 | QuicPacket* packet, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 993 | HasRetransmittableData retransmittable, |
| 994 | Force forced) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 995 | if (!connected_) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 996 | DLOG(INFO) << ENDPOINT |
| 997 | << "Not sending packet as connection is disconnected."; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 998 | delete packet; |
| 999 | // Returning true because we deleted the packet and the caller shouldn't |
| 1000 | // delete it again. |
| 1001 | return true; |
| 1002 | } |
| 1003 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 1004 | if (encryption_level_ == ENCRYPTION_FORWARD_SECURE && |
| 1005 | level == ENCRYPTION_NONE) { |
| 1006 | // Drop packets that are NULL encrypted since the peer won't accept them |
| 1007 | // anymore. |
| 1008 | DLOG(INFO) << ENDPOINT << "Dropped packet: " << sequence_number |
| 1009 | << " since the packet is NULL encrypted."; |
| 1010 | DropPacket(sequence_number); |
| 1011 | delete packet; |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1015 | Retransmission retransmission = IsRetransmission(sequence_number) ? |
| 1016 | IS_RETRANSMISSION : NOT_RETRANSMISSION; |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 1017 | IsHandshake handshake = level == ENCRYPTION_NONE ? IS_HANDSHAKE |
| 1018 | : NOT_HANDSHAKE; |
| 1019 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1020 | // If we are not forced and we can't write, then simply return false; |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 1021 | if (forced == NO_FORCE && |
| 1022 | !CanWrite(retransmission, retransmittable, handshake)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1023 | return false; |
| 1024 | } |
| 1025 | |
| 1026 | scoped_ptr<QuicEncryptedPacket> encrypted( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1027 | framer_.EncryptPacket(level, sequence_number, *packet)); |
| 1028 | DLOG(INFO) << ENDPOINT << "Sending packet number " << sequence_number |
| 1029 | << " : " << (packet->is_fec_packet() ? "FEC " : |
| 1030 | (retransmittable == HAS_RETRANSMITTABLE_DATA |
| 1031 | ? "data bearing " : " ack only ")) |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 1032 | << ", encryption level: " |
| 1033 | << QuicUtils::EncryptionLevelToString(level) |
| 1034 | << ", length:" << packet->length(); |
| 1035 | DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl |
| 1036 | << QuicUtils::StringToHexASCIIDump(packet->AsStringPiece()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1037 | |
| 1038 | DCHECK(encrypted->length() <= kMaxPacketSize) |
| 1039 | << "Packet " << sequence_number << " will not be read; too large: " |
| 1040 | << packet->length() << " " << encrypted->length() << " " |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 1041 | << " forced: " << (forced == FORCE ? "yes" : "no"); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1042 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1043 | int error; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1044 | QuicTime now = clock_->Now(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 1045 | if (!retransmission) { |
| 1046 | time_of_last_sent_packet_ = now; |
| 1047 | } |
| 1048 | DVLOG(1) << ENDPOINT << "time of last sent packet: " |
| 1049 | << now.ToDebuggingValue(); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1050 | if (WritePacketToWire(sequence_number, level, *encrypted, &error) == -1) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 1051 | if (helper_->IsWriteBlocked(error)) { |
| 1052 | // TODO(satyashekhar): It might be more efficient (fewer system calls), if |
| 1053 | // all connections share this variable i.e this becomes a part of |
| 1054 | // PacketWriterInterface. |
| 1055 | write_blocked_ = true; |
| 1056 | // If the socket buffers the the data, then the packet should not |
| 1057 | // be queued and sent again, which would result in an unnecessary |
| 1058 | // duplicate packet being sent. |
| 1059 | return helper_->IsWriteBlockedDataBuffered(); |
| 1060 | } |
| 1061 | // We can't send an error as the socket is presumably borked. |
| 1062 | CloseConnection(QUIC_PACKET_WRITE_ERROR, false); |
| 1063 | return false; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1064 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1065 | |
| 1066 | // Set the retransmit alarm only when we have sent the packet to the client |
| 1067 | // and not when it goes to the pending queue, otherwise we will end up adding |
| 1068 | // an entry to retransmission_timeout_ every time we attempt a write. |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1069 | if (retransmittable == HAS_RETRANSMITTABLE_DATA) { |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 1070 | SetupRetransmission(sequence_number, level); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1071 | } else if (packet->is_fec_packet()) { |
| 1072 | SetupAbandonFecTimer(sequence_number); |
| 1073 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1074 | |
| 1075 | congestion_manager_.SentPacket(sequence_number, now, packet->length(), |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1076 | retransmission); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1077 | |
| 1078 | stats_.bytes_sent += encrypted->length(); |
| 1079 | ++stats_.packets_sent; |
| 1080 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1081 | if (retransmission == IS_RETRANSMISSION) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1082 | stats_.bytes_retransmitted += encrypted->length(); |
| 1083 | ++stats_.packets_retransmitted; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1086 | delete packet; |
| 1087 | return true; |
| 1088 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1089 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1090 | int QuicConnection::WritePacketToWire(QuicPacketSequenceNumber sequence_number, |
| 1091 | EncryptionLevel level, |
| 1092 | const QuicEncryptedPacket& packet, |
| 1093 | int* error) { |
| 1094 | int bytes_written = helper_->WritePacketToWire(packet, error); |
| 1095 | if (debug_visitor_) { |
| 1096 | // WritePacketToWire returned -1, then |error| will be populated with |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 1097 | // an error code, which we want to pass along to the visitor. |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1098 | debug_visitor_->OnPacketSent(sequence_number, level, packet, |
| 1099 | bytes_written == -1 ? *error : bytes_written); |
| 1100 | } |
| 1101 | return bytes_written; |
| 1102 | } |
| 1103 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1104 | bool QuicConnection::OnSerializedPacket( |
| 1105 | const SerializedPacket& serialized_packet) { |
| 1106 | if (serialized_packet.retransmittable_frames != NULL) { |
| 1107 | DCHECK(unacked_packets_.empty() || |
| 1108 | unacked_packets_.rbegin()->first < |
| 1109 | serialized_packet.sequence_number); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1110 | // Retransmitted frames will be sent with the same encryption level as the |
| 1111 | // original. |
| 1112 | serialized_packet.retransmittable_frames->set_encryption_level( |
| 1113 | encryption_level_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1114 | unacked_packets_.insert( |
| 1115 | make_pair(serialized_packet.sequence_number, |
| 1116 | serialized_packet.retransmittable_frames)); |
| 1117 | // All unacked packets might be retransmitted. |
| 1118 | retransmission_map_.insert( |
| 1119 | make_pair(serialized_packet.sequence_number, |
| 1120 | RetransmissionInfo(serialized_packet.sequence_number))); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1121 | } else if (serialized_packet.packet->is_fec_packet()) { |
| 1122 | unacked_fec_packets_.insert(make_pair( |
| 1123 | serialized_packet.sequence_number, |
| 1124 | serialized_packet.retransmittable_frames)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1125 | } |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1126 | return SendOrQueuePacket(encryption_level_, |
| 1127 | serialized_packet.sequence_number, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1128 | serialized_packet.packet, |
| 1129 | serialized_packet.entropy_hash, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1130 | serialized_packet.retransmittable_frames != NULL ? |
| 1131 | HAS_RETRANSMITTABLE_DATA : |
| 1132 | NO_RETRANSMITTABLE_DATA); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1135 | bool QuicConnection::SendOrQueuePacket(EncryptionLevel level, |
| 1136 | QuicPacketSequenceNumber sequence_number, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1137 | QuicPacket* packet, |
| 1138 | QuicPacketEntropyHash entropy_hash, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1139 | HasRetransmittableData retransmittable) { |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 1140 | sent_entropy_manager_.RecordPacketEntropyHash(sequence_number, entropy_hash); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1141 | if (!WritePacket(level, sequence_number, packet, retransmittable, NO_FORCE)) { |
| 1142 | queued_packets_.push_back(QueuedPacket(sequence_number, packet, level, |
| 1143 | retransmittable)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1144 | return false; |
| 1145 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1146 | return true; |
| 1147 | } |
| 1148 | |
| 1149 | bool QuicConnection::ShouldSimulateLostPacket() { |
| 1150 | // TODO(rch): enable this |
| 1151 | return false; |
| 1152 | /* |
| 1153 | return FLAGS_fake_packet_loss_percentage > 0 && |
| 1154 | random_->Rand32() % 100 < FLAGS_fake_packet_loss_percentage; |
| 1155 | */ |
| 1156 | } |
| 1157 | |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 1158 | void QuicConnection::UpdateSentPacketInfo(SentPacketInfo* sent_info) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1159 | if (!unacked_packets_.empty()) { |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 1160 | sent_info->least_unacked = unacked_packets_.begin()->first; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1161 | } else { |
| 1162 | // If there are no unacked packets, set the least unacked packet to |
| 1163 | // sequence_number() + 1 since that will be the sequence number of this |
| 1164 | // ack packet whenever it is sent. |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 1165 | sent_info->least_unacked = packet_creator_.sequence_number() + 1; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1166 | } |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 1167 | sent_info->entropy_hash = sent_entropy_manager_.EntropyHash( |
| 1168 | sent_info->least_unacked - 1); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1169 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1170 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1171 | void QuicConnection::SendAck() { |
| 1172 | helper_->ClearAckAlarm(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1173 | |
| 1174 | // TODO(rch): delay this until the CreateFeedbackFrame |
| 1175 | // method is invoked. This requires changes SetShouldSendAck |
| 1176 | // to be a no-arg method, and re-jiggering its implementation. |
| 1177 | bool send_feedback = false; |
| 1178 | if (congestion_manager_.GenerateCongestionFeedback( |
| 1179 | &outgoing_congestion_feedback_)) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 1180 | DVLOG(1) << ENDPOINT << "Sending feedback " |
| 1181 | << outgoing_congestion_feedback_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1182 | send_feedback = true; |
| 1183 | } |
| 1184 | |
| 1185 | packet_generator_.SetShouldSendAck(send_feedback); |
| 1186 | } |
| 1187 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1188 | void QuicConnection::MaybeAbandonFecPacket( |
| 1189 | QuicPacketSequenceNumber sequence_number) { |
| 1190 | if (!ContainsKey(unacked_fec_packets_, sequence_number)) { |
| 1191 | DVLOG(2) << ENDPOINT << "no need to abandon fec packet: " |
| 1192 | << sequence_number << "; it's already acked'"; |
| 1193 | return; |
| 1194 | } |
| 1195 | congestion_manager_.AbandoningPacket(sequence_number); |
| 1196 | // TODO(satyashekhar): Should this decrease the congestion window? |
| 1197 | } |
| 1198 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1199 | QuicTime QuicConnection::OnRetransmissionTimeout() { |
| 1200 | // This guards against registering the alarm later than we should. |
| 1201 | // |
| 1202 | // If we have packet A and B in the list and we call |
| 1203 | // MaybeRetransmitPacketForRTO on A, that may trigger a call to |
| 1204 | // SetRetransmissionAlarm if A is retransmitted as C. In that case we |
| 1205 | // don't want to register the alarm under SetRetransmissionAlarm; we |
| 1206 | // want to set it to the RTO of B when we return from this function. |
| 1207 | handling_retransmission_timeout_ = true; |
| 1208 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 1209 | for (size_t i = 0; i < max_packets_per_retransmission_alarm_ && |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1210 | !retransmission_timeouts_.empty(); ++i) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1211 | RetransmissionTime retransmission_time = retransmission_timeouts_.top(); |
| 1212 | DCHECK(retransmission_time.scheduled_time.IsInitialized()); |
| 1213 | if (retransmission_time.scheduled_time > clock_->ApproximateNow()) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1214 | break; |
| 1215 | } |
| 1216 | retransmission_timeouts_.pop(); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1217 | |
| 1218 | if (retransmission_time.for_fec) { |
| 1219 | MaybeAbandonFecPacket(retransmission_time.sequence_number); |
| 1220 | continue; |
| 1221 | } else if ( |
| 1222 | !MaybeRetransmitPacketForRTO(retransmission_time.sequence_number)) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1223 | DLOG(INFO) << ENDPOINT << "MaybeRetransmitPacketForRTO failed: " |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1224 | << "adding an extra delay for " |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1225 | << retransmission_time.sequence_number; |
| 1226 | retransmission_time.scheduled_time = clock_->ApproximateNow().Add( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1227 | congestion_manager_.DefaultRetransmissionTime()); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1228 | retransmission_timeouts_.push(retransmission_time); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | handling_retransmission_timeout_ = false; |
| 1233 | |
| 1234 | if (retransmission_timeouts_.empty()) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1235 | return QuicTime::Zero(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | // We have packets remaining. Return the absolute RTO of the oldest packet |
| 1239 | // on the list. |
| 1240 | return retransmission_timeouts_.top().scheduled_time; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1243 | void QuicConnection::SetEncrypter(EncryptionLevel level, |
| 1244 | QuicEncrypter* encrypter) { |
| 1245 | framer_.SetEncrypter(level, encrypter); |
| 1246 | } |
| 1247 | |
| 1248 | const QuicEncrypter* QuicConnection::encrypter(EncryptionLevel level) const { |
| 1249 | return framer_.encrypter(level); |
| 1250 | } |
| 1251 | |
| 1252 | void QuicConnection::SetDefaultEncryptionLevel( |
| 1253 | EncryptionLevel level) { |
| 1254 | encryption_level_ = level; |
| 1255 | } |
| 1256 | |
| 1257 | void QuicConnection::SetDecrypter(QuicDecrypter* decrypter) { |
| 1258 | framer_.SetDecrypter(decrypter); |
| 1259 | } |
| 1260 | |
| 1261 | void QuicConnection::SetAlternativeDecrypter(QuicDecrypter* decrypter, |
| 1262 | bool latch_once_used) { |
| 1263 | framer_.SetAlternativeDecrypter(decrypter, latch_once_used); |
| 1264 | } |
| 1265 | |
| 1266 | const QuicDecrypter* QuicConnection::decrypter() const { |
| 1267 | return framer_.decrypter(); |
| 1268 | } |
| 1269 | |
| 1270 | const QuicDecrypter* QuicConnection::alternative_decrypter() const { |
| 1271 | return framer_.alternative_decrypter(); |
| 1272 | } |
| 1273 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 1274 | void QuicConnection::QueueUndecryptablePacket( |
| 1275 | const QuicEncryptedPacket& packet) { |
| 1276 | DVLOG(1) << ENDPOINT << "Queueing undecryptable packet."; |
| 1277 | char* data = new char[packet.length()]; |
| 1278 | memcpy(data, packet.data(), packet.length()); |
| 1279 | undecryptable_packets_.push_back( |
| 1280 | new QuicEncryptedPacket(data, packet.length(), true)); |
| 1281 | } |
| 1282 | |
| 1283 | void QuicConnection::MaybeProcessUndecryptablePackets() { |
| 1284 | if (undecryptable_packets_.empty() || |
| 1285 | encryption_level_ == ENCRYPTION_NONE) { |
| 1286 | return; |
| 1287 | } |
| 1288 | |
| 1289 | while (!undecryptable_packets_.empty()) { |
| 1290 | DVLOG(1) << ENDPOINT << "Attempting to process undecryptable packet"; |
| 1291 | QuicEncryptedPacket* packet = undecryptable_packets_.front(); |
| 1292 | if (!framer_.ProcessPacket(*packet) && |
| 1293 | framer_.error() == QUIC_DECRYPTION_FAILURE) { |
| 1294 | DVLOG(1) << ENDPOINT << "Unable to process undecryptable packet..."; |
| 1295 | break; |
| 1296 | } |
| 1297 | DVLOG(1) << ENDPOINT << "Processed undecryptable packet!"; |
| 1298 | delete packet; |
| 1299 | undecryptable_packets_.pop_front(); |
| 1300 | } |
| 1301 | |
| 1302 | // Once forward secure encryption is in use, there will be no |
| 1303 | // new keys installed and hence any undecryptable packets will |
| 1304 | // never be able to be decrypted. |
| 1305 | if (encryption_level_ == ENCRYPTION_FORWARD_SECURE) { |
| 1306 | STLDeleteElements(&undecryptable_packets_); |
| 1307 | } |
| 1308 | } |
| 1309 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1310 | void QuicConnection::MaybeProcessRevivedPacket() { |
| 1311 | QuicFecGroup* group = GetFecGroup(); |
| 1312 | if (group == NULL || !group->CanRevive()) { |
| 1313 | return; |
| 1314 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1315 | QuicPacketHeader revived_header; |
| 1316 | char revived_payload[kMaxPacketSize]; |
| 1317 | size_t len = group->Revive(&revived_header, revived_payload, kMaxPacketSize); |
| 1318 | revived_header.public_header.guid = guid_; |
| 1319 | revived_header.public_header.version_flag = false; |
| 1320 | revived_header.public_header.reset_flag = false; |
| 1321 | revived_header.fec_flag = false; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1322 | revived_header.is_in_fec_group = NOT_IN_FEC_GROUP; |
| 1323 | revived_header.fec_group = 0; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1324 | group_map_.erase(last_header_.fec_group); |
| 1325 | delete group; |
| 1326 | |
| 1327 | last_packet_revived_ = true; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1328 | if (debug_visitor_) { |
| 1329 | debug_visitor_->OnRevivedPacket(revived_header, |
| 1330 | StringPiece(revived_payload, len)); |
| 1331 | } |
| 1332 | |
| 1333 | ++stats_.packets_revived; |
| 1334 | framer_.ProcessRevivedPacket(&revived_header, |
| 1335 | StringPiece(revived_payload, len)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | QuicFecGroup* QuicConnection::GetFecGroup() { |
| 1339 | QuicFecGroupNumber fec_group_num = last_header_.fec_group; |
| 1340 | if (fec_group_num == 0) { |
| 1341 | return NULL; |
| 1342 | } |
| 1343 | if (group_map_.count(fec_group_num) == 0) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 1344 | if (group_map_.size() >= kMaxFecGroups) { // Too many groups |
| 1345 | if (fec_group_num < group_map_.begin()->first) { |
| 1346 | // The group being requested is a group we've seen before and deleted. |
| 1347 | // Don't recreate it. |
| 1348 | return NULL; |
| 1349 | } |
| 1350 | // Clear the lowest group number. |
| 1351 | delete group_map_.begin()->second; |
| 1352 | group_map_.erase(group_map_.begin()); |
| 1353 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1354 | group_map_[fec_group_num] = new QuicFecGroup(); |
| 1355 | } |
| 1356 | return group_map_[fec_group_num]; |
| 1357 | } |
| 1358 | |
| 1359 | void QuicConnection::SendConnectionClose(QuicErrorCode error) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1360 | SendConnectionCloseWithDetails(error, string()); |
| 1361 | } |
| 1362 | |
| 1363 | void QuicConnection::SendConnectionClosePacket(QuicErrorCode error, |
| 1364 | const string& details) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1365 | DLOG(INFO) << ENDPOINT << "Force closing with error " |
| 1366 | << QuicUtils::ErrorToString(error) << " (" << error << ") " |
| 1367 | << details; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1368 | QuicConnectionCloseFrame frame; |
| 1369 | frame.error_code = error; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1370 | frame.error_details = details; |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 1371 | UpdateSentPacketInfo(&frame.ack_frame.sent_info); |
| 1372 | received_packet_manager_.UpdateReceivedPacketInfo( |
| 1373 | &frame.ack_frame.received_info, clock_->ApproximateNow()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1374 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1375 | SerializedPacket serialized_packet = |
| 1376 | packet_creator_.SerializeConnectionClose(&frame); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1377 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 1378 | // We need to update the sent entropy hash for all sent packets. |
| 1379 | sent_entropy_manager_.RecordPacketEntropyHash( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1380 | serialized_packet.sequence_number, |
| 1381 | serialized_packet.entropy_hash); |
| 1382 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 1383 | if (!WritePacket(encryption_level_, |
| 1384 | serialized_packet.sequence_number, |
| 1385 | serialized_packet.packet, |
| 1386 | serialized_packet.retransmittable_frames != NULL ? |
| 1387 | HAS_RETRANSMITTABLE_DATA : NO_RETRANSMITTABLE_DATA, |
| 1388 | FORCE)) { |
| 1389 | delete serialized_packet.packet; |
| 1390 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | void QuicConnection::SendConnectionCloseWithDetails(QuicErrorCode error, |
| 1394 | const string& details) { |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 1395 | if (!write_blocked_) { |
| 1396 | SendConnectionClosePacket(error, details); |
| 1397 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1398 | CloseConnection(error, false); |
| 1399 | } |
| 1400 | |
| 1401 | void QuicConnection::CloseConnection(QuicErrorCode error, bool from_peer) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1402 | connected_ = false; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1403 | visitor_->ConnectionClose(error, from_peer); |
| 1404 | } |
| 1405 | |
| 1406 | void QuicConnection::SendGoAway(QuicErrorCode error, |
| 1407 | QuicStreamId last_good_stream_id, |
| 1408 | const string& reason) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1409 | DLOG(INFO) << ENDPOINT << "Going away with error " |
| 1410 | << QuicUtils::ErrorToString(error) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1411 | << " (" << error << ")"; |
| 1412 | packet_generator_.AddControlFrame( |
| 1413 | QuicFrame(new QuicGoAwayFrame(error, last_good_stream_id, reason))); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | void QuicConnection::CloseFecGroupsBefore( |
| 1417 | QuicPacketSequenceNumber sequence_number) { |
| 1418 | FecGroupMap::iterator it = group_map_.begin(); |
| 1419 | while (it != group_map_.end()) { |
| 1420 | // If this is the current group or the group doesn't protect this packet |
| 1421 | // we can ignore it. |
| 1422 | if (last_header_.fec_group == it->first || |
| 1423 | !it->second->ProtectsPacketsBefore(sequence_number)) { |
| 1424 | ++it; |
| 1425 | continue; |
| 1426 | } |
| 1427 | QuicFecGroup* fec_group = it->second; |
| 1428 | DCHECK(!fec_group->CanRevive()); |
| 1429 | FecGroupMap::iterator next = it; |
| 1430 | ++next; |
| 1431 | group_map_.erase(it); |
| 1432 | delete fec_group; |
| 1433 | it = next; |
| 1434 | } |
| 1435 | } |
| 1436 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1437 | bool QuicConnection::HasQueuedData() const { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1438 | return !queued_packets_.empty() || packet_generator_.HasQueuedFrames(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1441 | void QuicConnection::SetIdleNetworkTimeout(QuicTime::Delta timeout) { |
| 1442 | if (timeout < idle_network_timeout_) { |
| 1443 | idle_network_timeout_ = timeout; |
| 1444 | CheckForTimeout(); |
| 1445 | } else { |
| 1446 | idle_network_timeout_ = timeout; |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | void QuicConnection::SetOverallConnectionTimeout(QuicTime::Delta timeout) { |
| 1451 | if (timeout < overall_connection_timeout_) { |
| 1452 | overall_connection_timeout_ = timeout; |
| 1453 | CheckForTimeout(); |
| 1454 | } else { |
| 1455 | overall_connection_timeout_ = timeout; |
| 1456 | } |
Torne (Richard Coles) | b2df76e | 2013-05-13 16:52:09 +0100 | [diff] [blame] | 1457 | } |
| 1458 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1459 | bool QuicConnection::CheckForTimeout() { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1460 | QuicTime now = clock_->ApproximateNow(); |
| 1461 | QuicTime time_of_last_packet = std::max(time_of_last_received_packet_, |
| 1462 | time_of_last_sent_packet_); |
| 1463 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1464 | // |delta| can be < 0 as |now| is approximate time but |time_of_last_packet| |
| 1465 | // is accurate time. However, this should not change the behavior of |
| 1466 | // timeout handling. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1467 | QuicTime::Delta delta = now.Subtract(time_of_last_packet); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 1468 | DVLOG(1) << ENDPOINT << "last packet " |
| 1469 | << time_of_last_packet.ToDebuggingValue() |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1470 | << " now:" << now.ToDebuggingValue() |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1471 | << " delta:" << delta.ToMicroseconds() |
| 1472 | << " network_timeout: " << idle_network_timeout_.ToMicroseconds(); |
| 1473 | if (delta >= idle_network_timeout_) { |
| 1474 | DVLOG(1) << ENDPOINT << "Connection timedout due to no network activity."; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1475 | SendConnectionClose(QUIC_CONNECTION_TIMED_OUT); |
| 1476 | return true; |
| 1477 | } |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1478 | |
| 1479 | // Next timeout delta. |
| 1480 | QuicTime::Delta timeout = idle_network_timeout_.Subtract(delta); |
| 1481 | |
| 1482 | if (!overall_connection_timeout_.IsInfinite()) { |
| 1483 | QuicTime::Delta connected_time = now.Subtract(creation_time_); |
| 1484 | DVLOG(1) << ENDPOINT << "connection time: " |
| 1485 | << connected_time.ToMilliseconds() << " overall timeout: " |
| 1486 | << overall_connection_timeout_.ToMilliseconds(); |
| 1487 | if (connected_time >= overall_connection_timeout_) { |
| 1488 | DVLOG(1) << ENDPOINT << |
| 1489 | "Connection timedout due to overall connection timeout."; |
| 1490 | SendConnectionClose(QUIC_CONNECTION_TIMED_OUT); |
| 1491 | return true; |
| 1492 | } |
| 1493 | |
| 1494 | // Take the min timeout. |
| 1495 | QuicTime::Delta connection_timeout = |
| 1496 | overall_connection_timeout_.Subtract(connected_time); |
| 1497 | if (connection_timeout < timeout) { |
| 1498 | timeout = connection_timeout; |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | helper_->SetTimeoutAlarm(timeout); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1503 | return false; |
| 1504 | } |
| 1505 | |
| 1506 | } // namespace net |