OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/quic_connection.h" | 5 #include "net/quic/quic_connection.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 #include <sys/types.h> | 8 #include <sys/types.h> |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <iterator> | 10 #include <iterator> |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 const size_t kMaxUndecryptablePackets = 10; | 57 const size_t kMaxUndecryptablePackets = 10; |
58 | 58 |
59 bool Near(QuicPacketSequenceNumber a, QuicPacketSequenceNumber b) { | 59 bool Near(QuicPacketSequenceNumber a, QuicPacketSequenceNumber b) { |
60 QuicPacketSequenceNumber delta = (a > b) ? a - b : b - a; | 60 QuicPacketSequenceNumber delta = (a > b) ? a - b : b - a; |
61 return delta <= kMaxPacketGap; | 61 return delta <= kMaxPacketGap; |
62 } | 62 } |
63 | 63 |
64 // An alarm that is scheduled to send an ack if a timeout occurs. | 64 // An alarm that is scheduled to send an ack if a timeout occurs. |
65 class AckAlarm : public QuicAlarm::Delegate { | 65 class AckAlarm : public QuicAlarm::Delegate { |
66 public: | 66 public: |
67 explicit AckAlarm(QuicConnection* connection) | 67 explicit AckAlarm(QuicConnection* connection) : connection_(connection) {} |
68 : connection_(connection) { | |
69 } | |
70 | 68 |
71 virtual QuicTime OnAlarm() OVERRIDE { | 69 virtual QuicTime OnAlarm() OVERRIDE { |
72 connection_->SendAck(); | 70 connection_->SendAck(); |
73 return QuicTime::Zero(); | 71 return QuicTime::Zero(); |
74 } | 72 } |
75 | 73 |
76 private: | 74 private: |
77 QuicConnection* connection_; | 75 QuicConnection* connection_; |
78 | 76 |
79 DISALLOW_COPY_AND_ASSIGN(AckAlarm); | 77 DISALLOW_COPY_AND_ASSIGN(AckAlarm); |
80 }; | 78 }; |
81 | 79 |
82 // This alarm will be scheduled any time a data-bearing packet is sent out. | 80 // This alarm will be scheduled any time a data-bearing packet is sent out. |
83 // When the alarm goes off, the connection checks to see if the oldest packets | 81 // When the alarm goes off, the connection checks to see if the oldest packets |
84 // have been acked, and retransmit them if they have not. | 82 // have been acked, and retransmit them if they have not. |
85 class RetransmissionAlarm : public QuicAlarm::Delegate { | 83 class RetransmissionAlarm : public QuicAlarm::Delegate { |
86 public: | 84 public: |
87 explicit RetransmissionAlarm(QuicConnection* connection) | 85 explicit RetransmissionAlarm(QuicConnection* connection) |
88 : connection_(connection) { | 86 : connection_(connection) {} |
89 } | |
90 | 87 |
91 virtual QuicTime OnAlarm() OVERRIDE { | 88 virtual QuicTime OnAlarm() OVERRIDE { |
92 connection_->OnRetransmissionTimeout(); | 89 connection_->OnRetransmissionTimeout(); |
93 return QuicTime::Zero(); | 90 return QuicTime::Zero(); |
94 } | 91 } |
95 | 92 |
96 private: | 93 private: |
97 QuicConnection* connection_; | 94 QuicConnection* connection_; |
98 | 95 |
99 DISALLOW_COPY_AND_ASSIGN(RetransmissionAlarm); | 96 DISALLOW_COPY_AND_ASSIGN(RetransmissionAlarm); |
100 }; | 97 }; |
101 | 98 |
102 // An alarm that is scheduled when the sent scheduler requires a | 99 // An alarm that is scheduled when the sent scheduler requires a |
103 // a delay before sending packets and fires when the packet may be sent. | 100 // a delay before sending packets and fires when the packet may be sent. |
104 class SendAlarm : public QuicAlarm::Delegate { | 101 class SendAlarm : public QuicAlarm::Delegate { |
105 public: | 102 public: |
106 explicit SendAlarm(QuicConnection* connection) | 103 explicit SendAlarm(QuicConnection* connection) : connection_(connection) {} |
107 : connection_(connection) { | |
108 } | |
109 | 104 |
110 virtual QuicTime OnAlarm() OVERRIDE { | 105 virtual QuicTime OnAlarm() OVERRIDE { |
111 connection_->WriteIfNotBlocked(); | 106 connection_->WriteIfNotBlocked(); |
112 // Never reschedule the alarm, since CanWrite does that. | 107 // Never reschedule the alarm, since CanWrite does that. |
113 return QuicTime::Zero(); | 108 return QuicTime::Zero(); |
114 } | 109 } |
115 | 110 |
116 private: | 111 private: |
117 QuicConnection* connection_; | 112 QuicConnection* connection_; |
118 | 113 |
119 DISALLOW_COPY_AND_ASSIGN(SendAlarm); | 114 DISALLOW_COPY_AND_ASSIGN(SendAlarm); |
120 }; | 115 }; |
121 | 116 |
122 class TimeoutAlarm : public QuicAlarm::Delegate { | 117 class TimeoutAlarm : public QuicAlarm::Delegate { |
123 public: | 118 public: |
124 explicit TimeoutAlarm(QuicConnection* connection) | 119 explicit TimeoutAlarm(QuicConnection* connection) : connection_(connection) {} |
125 : connection_(connection) { | |
126 } | |
127 | 120 |
128 virtual QuicTime OnAlarm() OVERRIDE { | 121 virtual QuicTime OnAlarm() OVERRIDE { |
129 connection_->CheckForTimeout(); | 122 connection_->CheckForTimeout(); |
130 // Never reschedule the alarm, since CheckForTimeout does that. | 123 // Never reschedule the alarm, since CheckForTimeout does that. |
131 return QuicTime::Zero(); | 124 return QuicTime::Zero(); |
132 } | 125 } |
133 | 126 |
134 private: | 127 private: |
135 QuicConnection* connection_; | 128 QuicConnection* connection_; |
136 | 129 |
137 DISALLOW_COPY_AND_ASSIGN(TimeoutAlarm); | 130 DISALLOW_COPY_AND_ASSIGN(TimeoutAlarm); |
138 }; | 131 }; |
139 | 132 |
140 class PingAlarm : public QuicAlarm::Delegate { | 133 class PingAlarm : public QuicAlarm::Delegate { |
141 public: | 134 public: |
142 explicit PingAlarm(QuicConnection* connection) | 135 explicit PingAlarm(QuicConnection* connection) : connection_(connection) {} |
143 : connection_(connection) { | |
144 } | |
145 | 136 |
146 virtual QuicTime OnAlarm() OVERRIDE { | 137 virtual QuicTime OnAlarm() OVERRIDE { |
147 connection_->SendPing(); | 138 connection_->SendPing(); |
148 return QuicTime::Zero(); | 139 return QuicTime::Zero(); |
149 } | 140 } |
150 | 141 |
151 private: | 142 private: |
152 QuicConnection* connection_; | 143 QuicConnection* connection_; |
153 | 144 |
154 DISALLOW_COPY_AND_ASSIGN(PingAlarm); | 145 DISALLOW_COPY_AND_ASSIGN(PingAlarm); |
(...skipping 10 matching lines...) Expand all Loading... |
165 } | 156 } |
166 } | 157 } |
167 return QuicConnection::NORMAL; | 158 return QuicConnection::NORMAL; |
168 } | 159 } |
169 | 160 |
170 } // namespace | 161 } // namespace |
171 | 162 |
172 QuicConnection::QueuedPacket::QueuedPacket(SerializedPacket packet, | 163 QuicConnection::QueuedPacket::QueuedPacket(SerializedPacket packet, |
173 EncryptionLevel level, | 164 EncryptionLevel level, |
174 TransmissionType transmission_type) | 165 TransmissionType transmission_type) |
175 : sequence_number(packet.sequence_number), | 166 : sequence_number(packet.sequence_number), |
176 packet(packet.packet), | 167 packet(packet.packet), |
177 encryption_level(level), | 168 encryption_level(level), |
178 transmission_type(transmission_type), | 169 transmission_type(transmission_type), |
179 retransmittable((transmission_type != NOT_RETRANSMISSION || | 170 retransmittable((transmission_type != NOT_RETRANSMISSION || |
180 packet.retransmittable_frames != NULL) ? | 171 packet.retransmittable_frames != NULL) |
181 HAS_RETRANSMITTABLE_DATA : NO_RETRANSMITTABLE_DATA), | 172 ? HAS_RETRANSMITTABLE_DATA |
182 handshake(packet.retransmittable_frames == NULL ? | 173 : NO_RETRANSMITTABLE_DATA), |
183 NOT_HANDSHAKE : packet.retransmittable_frames->HasCryptoHandshake()), | 174 handshake(packet.retransmittable_frames == NULL |
184 type(GetPacketType(packet.retransmittable_frames)), | 175 ? NOT_HANDSHAKE |
185 length(packet.packet->length()) { | 176 : packet.retransmittable_frames->HasCryptoHandshake()), |
| 177 type(GetPacketType(packet.retransmittable_frames)), |
| 178 length(packet.packet->length()) { |
186 } | 179 } |
187 | 180 |
188 #define ENDPOINT (is_server_ ? "Server: " : " Client: ") | 181 #define ENDPOINT (is_server_ ? "Server: " : " Client: ") |
189 | 182 |
190 QuicConnection::QuicConnection(QuicConnectionId connection_id, | 183 QuicConnection::QuicConnection(QuicConnectionId connection_id, |
191 IPEndPoint address, | 184 IPEndPoint address, |
192 QuicConnectionHelperInterface* helper, | 185 QuicConnectionHelperInterface* helper, |
193 QuicPacketWriter* writer, | 186 QuicPacketWriter* writer, |
194 bool is_server, | 187 bool is_server, |
195 const QuicVersionVector& supported_versions, | 188 const QuicVersionVector& supported_versions, |
196 uint32 max_flow_control_receive_window_bytes) | 189 uint32 max_flow_control_receive_window_bytes) |
197 : framer_(supported_versions, helper->GetClock()->ApproximateNow(), | 190 : framer_(supported_versions, |
| 191 helper->GetClock()->ApproximateNow(), |
198 is_server), | 192 is_server), |
199 helper_(helper), | 193 helper_(helper), |
200 writer_(writer), | 194 writer_(writer), |
201 encryption_level_(ENCRYPTION_NONE), | 195 encryption_level_(ENCRYPTION_NONE), |
202 clock_(helper->GetClock()), | 196 clock_(helper->GetClock()), |
203 random_generator_(helper->GetRandomGenerator()), | 197 random_generator_(helper->GetRandomGenerator()), |
204 connection_id_(connection_id), | 198 connection_id_(connection_id), |
205 peer_address_(address), | 199 peer_address_(address), |
206 last_packet_revived_(false), | 200 last_packet_revived_(false), |
207 last_size_(0), | 201 last_size_(0), |
(...skipping 12 matching lines...) Expand all Loading... |
220 ping_alarm_(helper->CreateAlarm(new PingAlarm(this))), | 214 ping_alarm_(helper->CreateAlarm(new PingAlarm(this))), |
221 debug_visitor_(NULL), | 215 debug_visitor_(NULL), |
222 packet_creator_(connection_id_, &framer_, random_generator_, is_server), | 216 packet_creator_(connection_id_, &framer_, random_generator_, is_server), |
223 packet_generator_(this, NULL, &packet_creator_), | 217 packet_generator_(this, NULL, &packet_creator_), |
224 idle_network_timeout_( | 218 idle_network_timeout_( |
225 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs)), | 219 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs)), |
226 overall_connection_timeout_(QuicTime::Delta::Infinite()), | 220 overall_connection_timeout_(QuicTime::Delta::Infinite()), |
227 time_of_last_received_packet_(clock_->ApproximateNow()), | 221 time_of_last_received_packet_(clock_->ApproximateNow()), |
228 time_of_last_sent_new_packet_(clock_->ApproximateNow()), | 222 time_of_last_sent_new_packet_(clock_->ApproximateNow()), |
229 sequence_number_of_last_sent_packet_(0), | 223 sequence_number_of_last_sent_packet_(0), |
230 sent_packet_manager_( | 224 sent_packet_manager_(is_server, |
231 is_server, clock_, &stats_, kTCP, | 225 clock_, |
232 FLAGS_quic_use_time_loss_detection ? kTime : kNack), | 226 &stats_, |
| 227 kTCP, |
| 228 FLAGS_quic_use_time_loss_detection ? kTime : kNack), |
233 version_negotiation_state_(START_NEGOTIATION), | 229 version_negotiation_state_(START_NEGOTIATION), |
234 is_server_(is_server), | 230 is_server_(is_server), |
235 connected_(true), | 231 connected_(true), |
236 address_migrating_(false), | 232 address_migrating_(false), |
237 max_flow_control_receive_window_bytes_( | 233 max_flow_control_receive_window_bytes_( |
238 max_flow_control_receive_window_bytes) { | 234 max_flow_control_receive_window_bytes) { |
239 if (max_flow_control_receive_window_bytes_ < kDefaultFlowControlSendWindow) { | 235 if (max_flow_control_receive_window_bytes_ < kDefaultFlowControlSendWindow) { |
240 DLOG(ERROR) << "Initial receive window (" | 236 DLOG(ERROR) << "Initial receive window (" |
241 << max_flow_control_receive_window_bytes_ | 237 << max_flow_control_receive_window_bytes_ |
242 << ") cannot be set lower than default (" | 238 << ") cannot be set lower than default (" |
243 << kDefaultFlowControlSendWindow << ")."; | 239 << kDefaultFlowControlSendWindow << ")."; |
244 max_flow_control_receive_window_bytes_ = kDefaultFlowControlSendWindow; | 240 max_flow_control_receive_window_bytes_ = kDefaultFlowControlSendWindow; |
245 } | 241 } |
246 if (!is_server_) { | 242 if (!is_server_) { |
247 // Pacing will be enabled if the client negotiates it. | 243 // Pacing will be enabled if the client negotiates it. |
248 sent_packet_manager_.MaybeEnablePacing(); | 244 sent_packet_manager_.MaybeEnablePacing(); |
249 } | 245 } |
250 DVLOG(1) << ENDPOINT << "Created connection with connection_id: " | 246 DVLOG(1) << ENDPOINT |
251 << connection_id; | 247 << "Created connection with connection_id: " << connection_id; |
252 timeout_alarm_->Set(clock_->ApproximateNow().Add(idle_network_timeout_)); | 248 timeout_alarm_->Set(clock_->ApproximateNow().Add(idle_network_timeout_)); |
253 framer_.set_visitor(this); | 249 framer_.set_visitor(this); |
254 framer_.set_received_entropy_calculator(&received_packet_manager_); | 250 framer_.set_received_entropy_calculator(&received_packet_manager_); |
255 stats_.connection_creation_time = clock_->ApproximateNow(); | 251 stats_.connection_creation_time = clock_->ApproximateNow(); |
256 } | 252 } |
257 | 253 |
258 QuicConnection::~QuicConnection() { | 254 QuicConnection::~QuicConnection() { |
259 STLDeleteElements(&undecryptable_packets_); | 255 STLDeleteElements(&undecryptable_packets_); |
260 STLDeleteValues(&group_map_); | 256 STLDeleteValues(&group_map_); |
261 for (QueuedPacketList::iterator it = queued_packets_.begin(); | 257 for (QueuedPacketList::iterator it = queued_packets_.begin(); |
262 it != queued_packets_.end(); ++it) { | 258 it != queued_packets_.end(); |
| 259 ++it) { |
263 delete it->packet; | 260 delete it->packet; |
264 } | 261 } |
265 } | 262 } |
266 | 263 |
267 void QuicConnection::SetFromConfig(const QuicConfig& config) { | 264 void QuicConnection::SetFromConfig(const QuicConfig& config) { |
268 SetIdleNetworkTimeout(config.idle_connection_state_lifetime()); | 265 SetIdleNetworkTimeout(config.idle_connection_state_lifetime()); |
269 sent_packet_manager_.SetFromConfig(config); | 266 sent_packet_manager_.SetFromConfig(config); |
270 // TODO(satyamshekhar): Set congestion control and ICSL also. | 267 // TODO(satyamshekhar): Set congestion control and ICSL also. |
271 } | 268 } |
272 | 269 |
273 bool QuicConnection::SelectMutualVersion( | 270 bool QuicConnection::SelectMutualVersion( |
274 const QuicVersionVector& available_versions) { | 271 const QuicVersionVector& available_versions) { |
275 // Try to find the highest mutual version by iterating over supported | 272 // Try to find the highest mutual version by iterating over supported |
276 // versions, starting with the highest, and breaking out of the loop once we | 273 // versions, starting with the highest, and breaking out of the loop once we |
277 // find a matching version in the provided available_versions vector. | 274 // find a matching version in the provided available_versions vector. |
278 const QuicVersionVector& supported_versions = framer_.supported_versions(); | 275 const QuicVersionVector& supported_versions = framer_.supported_versions(); |
279 for (size_t i = 0; i < supported_versions.size(); ++i) { | 276 for (size_t i = 0; i < supported_versions.size(); ++i) { |
280 const QuicVersion& version = supported_versions[i]; | 277 const QuicVersion& version = supported_versions[i]; |
281 if (std::find(available_versions.begin(), available_versions.end(), | 278 if (std::find(available_versions.begin(), |
| 279 available_versions.end(), |
282 version) != available_versions.end()) { | 280 version) != available_versions.end()) { |
283 framer_.set_version(version); | 281 framer_.set_version(version); |
284 return true; | 282 return true; |
285 } | 283 } |
286 } | 284 } |
287 | 285 |
288 return false; | 286 return false; |
289 } | 287 } |
290 | 288 |
291 void QuicConnection::OnError(QuicFramer* framer) { | 289 void QuicConnection::OnError(QuicFramer* framer) { |
292 // Packets that we cannot decrypt are dropped. | 290 // Packets that we cannot decrypt are dropped. |
293 // TODO(rch): add stats to measure this. | 291 // TODO(rch): add stats to measure this. |
294 if (!connected_ || framer->error() == QUIC_DECRYPTION_FAILURE) { | 292 if (!connected_ || framer->error() == QUIC_DECRYPTION_FAILURE) { |
295 return; | 293 return; |
296 } | 294 } |
297 SendConnectionCloseWithDetails(framer->error(), framer->detailed_error()); | 295 SendConnectionCloseWithDetails(framer->error(), framer->detailed_error()); |
298 } | 296 } |
299 | 297 |
300 void QuicConnection::OnPacket() { | 298 void QuicConnection::OnPacket() { |
301 DCHECK(last_stream_frames_.empty() && | 299 DCHECK(last_stream_frames_.empty() && last_goaway_frames_.empty() && |
302 last_goaway_frames_.empty() && | 300 last_window_update_frames_.empty() && last_blocked_frames_.empty() && |
303 last_window_update_frames_.empty() && | 301 last_rst_frames_.empty() && last_ack_frames_.empty() && |
304 last_blocked_frames_.empty() && | 302 last_congestion_frames_.empty() && last_stop_waiting_frames_.empty()); |
305 last_rst_frames_.empty() && | |
306 last_ack_frames_.empty() && | |
307 last_congestion_frames_.empty() && | |
308 last_stop_waiting_frames_.empty()); | |
309 } | 303 } |
310 | 304 |
311 void QuicConnection::OnPublicResetPacket( | 305 void QuicConnection::OnPublicResetPacket(const QuicPublicResetPacket& packet) { |
312 const QuicPublicResetPacket& packet) { | |
313 if (debug_visitor_) { | 306 if (debug_visitor_) { |
314 debug_visitor_->OnPublicResetPacket(packet); | 307 debug_visitor_->OnPublicResetPacket(packet); |
315 } | 308 } |
316 CloseConnection(QUIC_PUBLIC_RESET, true); | 309 CloseConnection(QUIC_PUBLIC_RESET, true); |
317 } | 310 } |
318 | 311 |
319 bool QuicConnection::OnProtocolVersionMismatch(QuicVersion received_version) { | 312 bool QuicConnection::OnProtocolVersionMismatch(QuicVersion received_version) { |
320 DVLOG(1) << ENDPOINT << "Received packet with mismatched version " | 313 DVLOG(1) << ENDPOINT << "Received packet with mismatched version " |
321 << received_version; | 314 << received_version; |
322 // TODO(satyamshekhar): Implement no server state in this mode. | 315 // TODO(satyamshekhar): Implement no server state in this mode. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 } | 374 } |
382 if (debug_visitor_) { | 375 if (debug_visitor_) { |
383 debug_visitor_->OnVersionNegotiationPacket(packet); | 376 debug_visitor_->OnVersionNegotiationPacket(packet); |
384 } | 377 } |
385 | 378 |
386 if (version_negotiation_state_ != START_NEGOTIATION) { | 379 if (version_negotiation_state_ != START_NEGOTIATION) { |
387 // Possibly a duplicate version negotiation packet. | 380 // Possibly a duplicate version negotiation packet. |
388 return; | 381 return; |
389 } | 382 } |
390 | 383 |
391 if (std::find(packet.versions.begin(), | 384 if (std::find(packet.versions.begin(), packet.versions.end(), version()) != |
392 packet.versions.end(), version()) != | |
393 packet.versions.end()) { | 385 packet.versions.end()) { |
394 DLOG(WARNING) << ENDPOINT << "The server already supports our version. " | 386 DLOG(WARNING) << ENDPOINT << "The server already supports our version. " |
395 << "It should have accepted our connection."; | 387 << "It should have accepted our connection."; |
396 // Just drop the connection. | 388 // Just drop the connection. |
397 CloseConnection(QUIC_INVALID_VERSION_NEGOTIATION_PACKET, false); | 389 CloseConnection(QUIC_INVALID_VERSION_NEGOTIATION_PACKET, false); |
398 return; | 390 return; |
399 } | 391 } |
400 | 392 |
401 if (!SelectMutualVersion(packet.versions)) { | 393 if (!SelectMutualVersion(packet.versions)) { |
402 SendConnectionCloseWithDetails(QUIC_INVALID_VERSION, | 394 SendConnectionCloseWithDetails(QUIC_INVALID_VERSION, |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 | 550 |
559 sent_packet_manager_.OnIncomingAck(incoming_ack.received_info, | 551 sent_packet_manager_.OnIncomingAck(incoming_ack.received_info, |
560 time_of_last_received_packet_); | 552 time_of_last_received_packet_); |
561 if (sent_packet_manager_.HasPendingRetransmissions()) { | 553 if (sent_packet_manager_.HasPendingRetransmissions()) { |
562 WriteIfNotBlocked(); | 554 WriteIfNotBlocked(); |
563 } | 555 } |
564 | 556 |
565 // Always reset the retransmission alarm when an ack comes in, since we now | 557 // Always reset the retransmission alarm when an ack comes in, since we now |
566 // have a better estimate of the current rtt than when it was set. | 558 // have a better estimate of the current rtt than when it was set. |
567 retransmission_alarm_->Cancel(); | 559 retransmission_alarm_->Cancel(); |
568 QuicTime retransmission_time = | 560 QuicTime retransmission_time = sent_packet_manager_.GetRetransmissionTime(); |
569 sent_packet_manager_.GetRetransmissionTime(); | |
570 if (retransmission_time != QuicTime::Zero()) { | 561 if (retransmission_time != QuicTime::Zero()) { |
571 retransmission_alarm_->Set(retransmission_time); | 562 retransmission_alarm_->Set(retransmission_time); |
572 } | 563 } |
573 } | 564 } |
574 | 565 |
575 void QuicConnection::ProcessStopWaitingFrame( | 566 void QuicConnection::ProcessStopWaitingFrame( |
576 const QuicStopWaitingFrame& stop_waiting) { | 567 const QuicStopWaitingFrame& stop_waiting) { |
577 largest_seen_packet_with_stop_waiting_ = last_header_.packet_sequence_number; | 568 largest_seen_packet_with_stop_waiting_ = last_header_.packet_sequence_number; |
578 received_packet_manager_.UpdatePacketInformationSentByPeer(stop_waiting); | 569 received_packet_manager_.UpdatePacketInformationSentByPeer(stop_waiting); |
579 // Possibly close any FecGroups which are now irrelevant. | 570 // Possibly close any FecGroups which are now irrelevant. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
624 if (incoming_ack.received_info.largest_observed > | 615 if (incoming_ack.received_info.largest_observed > |
625 packet_creator_.sequence_number()) { | 616 packet_creator_.sequence_number()) { |
626 DLOG(ERROR) << ENDPOINT << "Peer's observed unsent packet:" | 617 DLOG(ERROR) << ENDPOINT << "Peer's observed unsent packet:" |
627 << incoming_ack.received_info.largest_observed << " vs " | 618 << incoming_ack.received_info.largest_observed << " vs " |
628 << packet_creator_.sequence_number(); | 619 << packet_creator_.sequence_number(); |
629 // We got an error for data we have not sent. Error out. | 620 // We got an error for data we have not sent. Error out. |
630 return false; | 621 return false; |
631 } | 622 } |
632 | 623 |
633 if (incoming_ack.received_info.largest_observed < | 624 if (incoming_ack.received_info.largest_observed < |
634 received_packet_manager_.peer_largest_observed_packet()) { | 625 received_packet_manager_.peer_largest_observed_packet()) { |
635 DLOG(ERROR) << ENDPOINT << "Peer's largest_observed packet decreased:" | 626 DLOG(ERROR) << ENDPOINT << "Peer's largest_observed packet decreased:" |
636 << incoming_ack.received_info.largest_observed << " vs " | 627 << incoming_ack.received_info.largest_observed << " vs " |
637 << received_packet_manager_.peer_largest_observed_packet(); | 628 << received_packet_manager_.peer_largest_observed_packet(); |
638 // A new ack has a diminished largest_observed value. Error out. | 629 // A new ack has a diminished largest_observed value. Error out. |
639 // If this was an old packet, we wouldn't even have checked. | 630 // If this was an old packet, we wouldn't even have checked. |
640 return false; | 631 return false; |
641 } | 632 } |
642 | 633 |
643 if (version() <= QUIC_VERSION_15) { | 634 if (version() <= QUIC_VERSION_15) { |
644 if (!ValidateStopWaitingFrame(incoming_ack.sent_info)) { | 635 if (!ValidateStopWaitingFrame(incoming_ack.sent_info)) { |
645 return false; | 636 return false; |
646 } | 637 } |
647 } | 638 } |
648 | 639 |
649 if (!incoming_ack.received_info.missing_packets.empty() && | 640 if (!incoming_ack.received_info.missing_packets.empty() && |
650 *incoming_ack.received_info.missing_packets.rbegin() > | 641 *incoming_ack.received_info.missing_packets.rbegin() > |
651 incoming_ack.received_info.largest_observed) { | 642 incoming_ack.received_info.largest_observed) { |
652 DLOG(ERROR) << ENDPOINT << "Peer sent missing packet: " | 643 DLOG(ERROR) << ENDPOINT << "Peer sent missing packet: " |
653 << *incoming_ack.received_info.missing_packets.rbegin() | 644 << *incoming_ack.received_info.missing_packets.rbegin() |
654 << " which is greater than largest observed: " | 645 << " which is greater than largest observed: " |
655 << incoming_ack.received_info.largest_observed; | 646 << incoming_ack.received_info.largest_observed; |
656 return false; | 647 return false; |
657 } | 648 } |
658 | 649 |
659 if (!incoming_ack.received_info.missing_packets.empty() && | 650 if (!incoming_ack.received_info.missing_packets.empty() && |
660 *incoming_ack.received_info.missing_packets.begin() < | 651 *incoming_ack.received_info.missing_packets.begin() < |
661 received_packet_manager_.least_packet_awaited_by_peer()) { | 652 received_packet_manager_.least_packet_awaited_by_peer()) { |
662 DLOG(ERROR) << ENDPOINT << "Peer sent missing packet: " | 653 DLOG(ERROR) << ENDPOINT << "Peer sent missing packet: " |
663 << *incoming_ack.received_info.missing_packets.begin() | 654 << *incoming_ack.received_info.missing_packets.begin() |
664 << " which is smaller than least_packet_awaited_by_peer_: " | 655 << " which is smaller than least_packet_awaited_by_peer_: " |
665 << received_packet_manager_.least_packet_awaited_by_peer(); | 656 << received_packet_manager_.least_packet_awaited_by_peer(); |
666 return false; | 657 return false; |
667 } | 658 } |
668 | 659 |
669 if (!sent_entropy_manager_.IsValidEntropy( | 660 if (!sent_entropy_manager_.IsValidEntropy( |
670 incoming_ack.received_info.largest_observed, | 661 incoming_ack.received_info.largest_observed, |
671 incoming_ack.received_info.missing_packets, | 662 incoming_ack.received_info.missing_packets, |
672 incoming_ack.received_info.entropy_hash)) { | 663 incoming_ack.received_info.entropy_hash)) { |
673 DLOG(ERROR) << ENDPOINT << "Peer sent invalid entropy."; | 664 DLOG(ERROR) << ENDPOINT << "Peer sent invalid entropy."; |
674 return false; | 665 return false; |
675 } | 666 } |
676 | 667 |
677 for (SequenceNumberSet::const_iterator iter = | 668 for (SequenceNumberSet::const_iterator iter = |
678 incoming_ack.received_info.revived_packets.begin(); | 669 incoming_ack.received_info.revived_packets.begin(); |
679 iter != incoming_ack.received_info.revived_packets.end(); ++iter) { | 670 iter != incoming_ack.received_info.revived_packets.end(); |
| 671 ++iter) { |
680 if (!ContainsKey(incoming_ack.received_info.missing_packets, *iter)) { | 672 if (!ContainsKey(incoming_ack.received_info.missing_packets, *iter)) { |
681 DLOG(ERROR) << ENDPOINT | 673 DLOG(ERROR) << ENDPOINT |
682 << "Peer specified revived packet which was not missing."; | 674 << "Peer specified revived packet which was not missing."; |
683 return false; | 675 return false; |
684 } | 676 } |
685 } | 677 } |
686 return true; | 678 return true; |
687 } | 679 } |
688 | 680 |
689 bool QuicConnection::ValidateStopWaitingFrame( | 681 bool QuicConnection::ValidateStopWaitingFrame( |
690 const QuicStopWaitingFrame& stop_waiting) { | 682 const QuicStopWaitingFrame& stop_waiting) { |
691 if (stop_waiting.least_unacked < | 683 if (stop_waiting.least_unacked < |
692 received_packet_manager_.peer_least_packet_awaiting_ack()) { | 684 received_packet_manager_.peer_least_packet_awaiting_ack()) { |
693 DLOG(ERROR) << ENDPOINT << "Peer's sent low least_unacked: " | 685 DLOG(ERROR) << ENDPOINT << "Peer's sent low least_unacked: " |
694 << stop_waiting.least_unacked << " vs " | 686 << stop_waiting.least_unacked << " vs " |
695 << received_packet_manager_.peer_least_packet_awaiting_ack(); | 687 << received_packet_manager_.peer_least_packet_awaiting_ack(); |
696 // We never process old ack frames, so this number should only increase. | 688 // We never process old ack frames, so this number should only increase. |
697 return false; | 689 return false; |
698 } | 690 } |
699 | 691 |
700 if (stop_waiting.least_unacked > | 692 if (stop_waiting.least_unacked > last_header_.packet_sequence_number) { |
701 last_header_.packet_sequence_number) { | 693 DLOG(ERROR) << ENDPOINT |
702 DLOG(ERROR) << ENDPOINT << "Peer sent least_unacked:" | 694 << "Peer sent least_unacked:" << stop_waiting.least_unacked |
703 << stop_waiting.least_unacked | |
704 << " greater than the enclosing packet sequence number:" | 695 << " greater than the enclosing packet sequence number:" |
705 << last_header_.packet_sequence_number; | 696 << last_header_.packet_sequence_number; |
706 return false; | 697 return false; |
707 } | 698 } |
708 | 699 |
709 return true; | 700 return true; |
710 } | 701 } |
711 | 702 |
712 void QuicConnection::OnFecData(const QuicFecData& fec) { | 703 void QuicConnection::OnFecData(const QuicFecData& fec) { |
713 DCHECK_EQ(IN_FEC_GROUP, last_header_.is_in_fec_group); | 704 DCHECK_EQ(IN_FEC_GROUP, last_header_.is_in_fec_group); |
(...skipping 16 matching lines...) Expand all Loading... |
730 } | 721 } |
731 | 722 |
732 bool QuicConnection::OnConnectionCloseFrame( | 723 bool QuicConnection::OnConnectionCloseFrame( |
733 const QuicConnectionCloseFrame& frame) { | 724 const QuicConnectionCloseFrame& frame) { |
734 DCHECK(connected_); | 725 DCHECK(connected_); |
735 if (debug_visitor_) { | 726 if (debug_visitor_) { |
736 debug_visitor_->OnConnectionCloseFrame(frame); | 727 debug_visitor_->OnConnectionCloseFrame(frame); |
737 } | 728 } |
738 DVLOG(1) << ENDPOINT << "Connection " << connection_id() | 729 DVLOG(1) << ENDPOINT << "Connection " << connection_id() |
739 << " closed with error " | 730 << " closed with error " |
740 << QuicUtils::ErrorToString(frame.error_code) | 731 << QuicUtils::ErrorToString(frame.error_code) << " " |
741 << " " << frame.error_details; | 732 << frame.error_details; |
742 last_close_frames_.push_back(frame); | 733 last_close_frames_.push_back(frame); |
743 return connected_; | 734 return connected_; |
744 } | 735 } |
745 | 736 |
746 bool QuicConnection::OnGoAwayFrame(const QuicGoAwayFrame& frame) { | 737 bool QuicConnection::OnGoAwayFrame(const QuicGoAwayFrame& frame) { |
747 DCHECK(connected_); | 738 DCHECK(connected_); |
748 DVLOG(1) << ENDPOINT << "Go away received with error " | 739 DVLOG(1) << ENDPOINT << "Go away received with error " |
749 << QuicUtils::ErrorToString(frame.error_code) | 740 << QuicUtils::ErrorToString(frame.error_code) |
750 << " and reason:" << frame.reason_phrase; | 741 << " and reason:" << frame.reason_phrase; |
751 last_goaway_frames_.push_back(frame); | 742 last_goaway_frames_.push_back(frame); |
752 return connected_; | 743 return connected_; |
753 } | 744 } |
754 | 745 |
755 bool QuicConnection::OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) { | 746 bool QuicConnection::OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) { |
756 DCHECK(connected_); | 747 DCHECK(connected_); |
757 DVLOG(1) << ENDPOINT << "WindowUpdate received for stream: " | 748 DVLOG(1) << ENDPOINT |
758 << frame.stream_id << " with byte offset: " << frame.byte_offset; | 749 << "WindowUpdate received for stream: " << frame.stream_id |
| 750 << " with byte offset: " << frame.byte_offset; |
759 last_window_update_frames_.push_back(frame); | 751 last_window_update_frames_.push_back(frame); |
760 return connected_; | 752 return connected_; |
761 } | 753 } |
762 | 754 |
763 bool QuicConnection::OnBlockedFrame(const QuicBlockedFrame& frame) { | 755 bool QuicConnection::OnBlockedFrame(const QuicBlockedFrame& frame) { |
764 DCHECK(connected_); | 756 DCHECK(connected_); |
765 DVLOG(1) << ENDPOINT << "Blocked frame received for stream: " | 757 DVLOG(1) << ENDPOINT |
766 << frame.stream_id; | 758 << "Blocked frame received for stream: " << frame.stream_id; |
767 last_blocked_frames_.push_back(frame); | 759 last_blocked_frames_.push_back(frame); |
768 return connected_; | 760 return connected_; |
769 } | 761 } |
770 | 762 |
771 void QuicConnection::OnPacketComplete() { | 763 void QuicConnection::OnPacketComplete() { |
772 // Don't do anything if this packet closed the connection. | 764 // Don't do anything if this packet closed the connection. |
773 if (!connected_) { | 765 if (!connected_) { |
774 ClearLastFrames(); | 766 ClearLastFrames(); |
775 return; | 767 return; |
776 } | 768 } |
777 | 769 |
778 DVLOG(1) << ENDPOINT << (last_packet_revived_ ? "Revived" : "Got") | 770 DVLOG(1) << ENDPOINT << (last_packet_revived_ ? "Revived" : "Got") |
779 << " packet " << last_header_.packet_sequence_number | 771 << " packet " << last_header_.packet_sequence_number << " with " |
780 << " with " << last_ack_frames_.size() << " acks, " | 772 << last_ack_frames_.size() << " acks, " |
781 << last_congestion_frames_.size() << " congestions, " | 773 << last_congestion_frames_.size() << " congestions, " |
782 << last_stop_waiting_frames_.size() << " stop_waiting, " | 774 << last_stop_waiting_frames_.size() << " stop_waiting, " |
783 << last_goaway_frames_.size() << " goaways, " | 775 << last_goaway_frames_.size() << " goaways, " |
784 << last_window_update_frames_.size() << " window updates, " | 776 << last_window_update_frames_.size() << " window updates, " |
785 << last_blocked_frames_.size() << " blocked, " | 777 << last_blocked_frames_.size() << " blocked, " |
786 << last_rst_frames_.size() << " rsts, " | 778 << last_rst_frames_.size() << " rsts, " << last_close_frames_.size() |
787 << last_close_frames_.size() << " closes, " | 779 << " closes, " << last_stream_frames_.size() << " stream frames for " |
788 << last_stream_frames_.size() | |
789 << " stream frames for " | |
790 << last_header_.public_header.connection_id; | 780 << last_header_.public_header.connection_id; |
791 | 781 |
792 // Call MaybeQueueAck() before recording the received packet, since we want | 782 // Call MaybeQueueAck() before recording the received packet, since we want |
793 // to trigger an ack if the newly received packet was previously missing. | 783 // to trigger an ack if the newly received packet was previously missing. |
794 MaybeQueueAck(); | 784 MaybeQueueAck(); |
795 | 785 |
796 // Record received or revived packet to populate ack info correctly before | 786 // Record received or revived packet to populate ack info correctly before |
797 // processing stream frames, since the processing may result in a response | 787 // processing stream frames, since the processing may result in a response |
798 // packet with a bundled ack. | 788 // packet with a bundled ack. |
799 if (last_packet_revived_) { | 789 if (last_packet_revived_) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
848 ack_alarm_->Cancel(); | 838 ack_alarm_->Cancel(); |
849 } | 839 } |
850 | 840 |
851 UpdateStopWaitingCount(); | 841 UpdateStopWaitingCount(); |
852 | 842 |
853 ClearLastFrames(); | 843 ClearLastFrames(); |
854 } | 844 } |
855 | 845 |
856 void QuicConnection::MaybeQueueAck() { | 846 void QuicConnection::MaybeQueueAck() { |
857 // If the incoming packet was missing, send an ack immediately. | 847 // If the incoming packet was missing, send an ack immediately. |
858 ack_queued_ = received_packet_manager_.IsMissing( | 848 ack_queued_ = |
859 last_header_.packet_sequence_number); | 849 received_packet_manager_.IsMissing(last_header_.packet_sequence_number); |
860 | 850 |
861 if (!ack_queued_ && ShouldLastPacketInstigateAck()) { | 851 if (!ack_queued_ && ShouldLastPacketInstigateAck()) { |
862 if (ack_alarm_->IsSet()) { | 852 if (ack_alarm_->IsSet()) { |
863 ack_queued_ = true; | 853 ack_queued_ = true; |
864 } else { | 854 } else { |
865 // Send an ack much more quickly for crypto handshake packets. | 855 // Send an ack much more quickly for crypto handshake packets. |
866 QuicTime::Delta delayed_ack_time = sent_packet_manager_.DelayedAckTime(); | 856 QuicTime::Delta delayed_ack_time = sent_packet_manager_.DelayedAckTime(); |
867 if (last_stream_frames_.size() == 1 && | 857 if (last_stream_frames_.size() == 1 && |
868 last_stream_frames_[0].stream_id == kCryptoStreamId) { | 858 last_stream_frames_[0].stream_id == kCryptoStreamId) { |
869 delayed_ack_time = QuicTime::Delta::Zero(); | 859 delayed_ack_time = QuicTime::Delta::Zero(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
902 return new QuicCongestionFeedbackFrame(outgoing_congestion_feedback_); | 892 return new QuicCongestionFeedbackFrame(outgoing_congestion_feedback_); |
903 } | 893 } |
904 | 894 |
905 QuicStopWaitingFrame* QuicConnection::CreateStopWaitingFrame() { | 895 QuicStopWaitingFrame* QuicConnection::CreateStopWaitingFrame() { |
906 QuicStopWaitingFrame stop_waiting; | 896 QuicStopWaitingFrame stop_waiting; |
907 UpdateStopWaiting(&stop_waiting); | 897 UpdateStopWaiting(&stop_waiting); |
908 return new QuicStopWaitingFrame(stop_waiting); | 898 return new QuicStopWaitingFrame(stop_waiting); |
909 } | 899 } |
910 | 900 |
911 bool QuicConnection::ShouldLastPacketInstigateAck() const { | 901 bool QuicConnection::ShouldLastPacketInstigateAck() const { |
912 if (!last_stream_frames_.empty() || | 902 if (!last_stream_frames_.empty() || !last_goaway_frames_.empty() || |
913 !last_goaway_frames_.empty() || | 903 !last_rst_frames_.empty() || !last_window_update_frames_.empty() || |
914 !last_rst_frames_.empty() || | |
915 !last_window_update_frames_.empty() || | |
916 !last_blocked_frames_.empty()) { | 904 !last_blocked_frames_.empty()) { |
917 return true; | 905 return true; |
918 } | 906 } |
919 | 907 |
920 if (!last_ack_frames_.empty() && | 908 if (!last_ack_frames_.empty() && |
921 last_ack_frames_.back().received_info.is_truncated) { | 909 last_ack_frames_.back().received_info.is_truncated) { |
922 return true; | 910 return true; |
923 } | 911 } |
924 return false; | 912 return false; |
925 } | 913 } |
926 | 914 |
927 void QuicConnection::UpdateStopWaitingCount() { | 915 void QuicConnection::UpdateStopWaitingCount() { |
928 if (last_ack_frames_.empty()) { | 916 if (last_ack_frames_.empty()) { |
929 return; | 917 return; |
930 } | 918 } |
931 | 919 |
932 // If the peer is still waiting for a packet that we are no longer planning to | 920 // If the peer is still waiting for a packet that we are no longer planning to |
933 // send, send an ack to raise the high water mark. | 921 // send, send an ack to raise the high water mark. |
934 if (!last_ack_frames_.back().received_info.missing_packets.empty() && | 922 if (!last_ack_frames_.back().received_info.missing_packets.empty() && |
935 GetLeastUnacked() > | 923 GetLeastUnacked() > |
936 *last_ack_frames_.back().received_info.missing_packets.begin()) { | 924 *last_ack_frames_.back().received_info.missing_packets.begin()) { |
937 ++stop_waiting_count_; | 925 ++stop_waiting_count_; |
938 } else { | 926 } else { |
939 stop_waiting_count_ = 0; | 927 stop_waiting_count_ = 0; |
940 } | 928 } |
941 } | 929 } |
942 | 930 |
943 QuicPacketSequenceNumber QuicConnection::GetLeastUnacked() const { | 931 QuicPacketSequenceNumber QuicConnection::GetLeastUnacked() const { |
944 return sent_packet_manager_.HasUnackedPackets() ? | 932 return sent_packet_manager_.HasUnackedPackets() |
945 sent_packet_manager_.GetLeastUnackedSentPacket() : | 933 ? sent_packet_manager_.GetLeastUnackedSentPacket() |
946 packet_creator_.sequence_number() + 1; | 934 : packet_creator_.sequence_number() + 1; |
947 } | 935 } |
948 | 936 |
949 void QuicConnection::MaybeSendInResponseToPacket() { | 937 void QuicConnection::MaybeSendInResponseToPacket() { |
950 if (!connected_) { | 938 if (!connected_) { |
951 return; | 939 return; |
952 } | 940 } |
953 ScopedPacketBundler bundler(this, ack_queued_ ? SEND_ACK : NO_ACK); | 941 ScopedPacketBundler bundler(this, ack_queued_ ? SEND_ACK : NO_ACK); |
954 | 942 |
955 // Now that we have received an ack, we might be able to send packets which | 943 // Now that we have received an ack, we might be able to send packets which |
956 // are queued locally, or drain streams which are blocked. | 944 // are queued locally, or drain streams which are blocked. |
957 QuicTime::Delta delay = sent_packet_manager_.TimeUntilSend( | 945 QuicTime::Delta delay = |
958 time_of_last_received_packet_, NOT_RETRANSMISSION, | 946 sent_packet_manager_.TimeUntilSend(time_of_last_received_packet_, |
959 HAS_RETRANSMITTABLE_DATA); | 947 NOT_RETRANSMISSION, |
| 948 HAS_RETRANSMITTABLE_DATA); |
960 if (delay.IsZero()) { | 949 if (delay.IsZero()) { |
961 send_alarm_->Cancel(); | 950 send_alarm_->Cancel(); |
962 WriteIfNotBlocked(); | 951 WriteIfNotBlocked(); |
963 } else if (!delay.IsInfinite()) { | 952 } else if (!delay.IsInfinite()) { |
964 send_alarm_->Cancel(); | 953 send_alarm_->Cancel(); |
965 send_alarm_->Set(time_of_last_received_packet_.Add(delay)); | 954 send_alarm_->Set(time_of_last_received_packet_.Add(delay)); |
966 } | 955 } |
967 } | 956 } |
968 | 957 |
969 void QuicConnection::SendVersionNegotiationPacket() { | 958 void QuicConnection::SendVersionNegotiationPacket() { |
970 // TODO(alyssar): implement zero server state negotiation. | 959 // TODO(alyssar): implement zero server state negotiation. |
971 pending_version_negotiation_packet_ = true; | 960 pending_version_negotiation_packet_ = true; |
972 if (writer_->IsWriteBlocked()) { | 961 if (writer_->IsWriteBlocked()) { |
973 visitor_->OnWriteBlocked(); | 962 visitor_->OnWriteBlocked(); |
974 return; | 963 return; |
975 } | 964 } |
976 scoped_ptr<QuicEncryptedPacket> version_packet( | 965 scoped_ptr<QuicEncryptedPacket> version_packet( |
977 packet_creator_.SerializeVersionNegotiationPacket( | 966 packet_creator_.SerializeVersionNegotiationPacket( |
978 framer_.supported_versions())); | 967 framer_.supported_versions())); |
979 WriteResult result = writer_->WritePacket( | 968 WriteResult result = writer_->WritePacket(version_packet->data(), |
980 version_packet->data(), version_packet->length(), | 969 version_packet->length(), |
981 self_address().address(), peer_address()); | 970 self_address().address(), |
| 971 peer_address()); |
982 | 972 |
983 if (result.status == WRITE_STATUS_ERROR) { | 973 if (result.status == WRITE_STATUS_ERROR) { |
984 // We can't send an error as the socket is presumably borked. | 974 // We can't send an error as the socket is presumably borked. |
985 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); | 975 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); |
986 return; | 976 return; |
987 } | 977 } |
988 if (result.status == WRITE_STATUS_BLOCKED) { | 978 if (result.status == WRITE_STATUS_BLOCKED) { |
989 visitor_->OnWriteBlocked(); | 979 visitor_->OnWriteBlocked(); |
990 if (writer_->IsWriteBlockedDataBuffered()) { | 980 if (writer_->IsWriteBlockedDataBuffered()) { |
991 pending_version_negotiation_packet_ = false; | 981 pending_version_negotiation_packet_ = false; |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1122 MaybeSendInResponseToPacket(); | 1112 MaybeSendInResponseToPacket(); |
1123 SetPingAlarm(); | 1113 SetPingAlarm(); |
1124 } | 1114 } |
1125 | 1115 |
1126 void QuicConnection::OnCanWrite() { | 1116 void QuicConnection::OnCanWrite() { |
1127 DCHECK(!writer_->IsWriteBlocked()); | 1117 DCHECK(!writer_->IsWriteBlocked()); |
1128 | 1118 |
1129 WriteQueuedPackets(); | 1119 WriteQueuedPackets(); |
1130 WritePendingRetransmissions(); | 1120 WritePendingRetransmissions(); |
1131 | 1121 |
1132 IsHandshake pending_handshake = visitor_->HasPendingHandshake() ? | 1122 IsHandshake pending_handshake = |
1133 IS_HANDSHAKE : NOT_HANDSHAKE; | 1123 visitor_->HasPendingHandshake() ? IS_HANDSHAKE : NOT_HANDSHAKE; |
1134 // Sending queued packets may have caused the socket to become write blocked, | 1124 // Sending queued packets may have caused the socket to become write blocked, |
1135 // or the congestion manager to prohibit sending. If we've sent everything | 1125 // or the congestion manager to prohibit sending. If we've sent everything |
1136 // we had queued and we're still not blocked, let the visitor know it can | 1126 // we had queued and we're still not blocked, let the visitor know it can |
1137 // write more. | 1127 // write more. |
1138 if (!CanWrite(NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, | 1128 if (!CanWrite( |
1139 pending_handshake)) { | 1129 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, pending_handshake)) { |
1140 return; | 1130 return; |
1141 } | 1131 } |
1142 | 1132 |
1143 { // Limit the scope of the bundler. | 1133 { // Limit the scope of the bundler. |
1144 // Set |include_ack| to false in bundler; ack inclusion happens elsewhere. | 1134 // Set |include_ack| to false in bundler; ack inclusion happens elsewhere. |
1145 ScopedPacketBundler bundler(this, NO_ACK); | 1135 ScopedPacketBundler bundler(this, NO_ACK); |
1146 visitor_->OnCanWrite(); | 1136 visitor_->OnCanWrite(); |
1147 } | 1137 } |
1148 | 1138 |
1149 // After the visitor writes, it may have caused the socket to become write | 1139 // After the visitor writes, it may have caused the socket to become write |
1150 // blocked or the congestion manager to prohibit sending, so check again. | 1140 // blocked or the congestion manager to prohibit sending, so check again. |
1151 pending_handshake = visitor_->HasPendingHandshake() ? | 1141 pending_handshake = |
1152 IS_HANDSHAKE : NOT_HANDSHAKE; | 1142 visitor_->HasPendingHandshake() ? IS_HANDSHAKE : NOT_HANDSHAKE; |
1153 if (visitor_->HasPendingWrites() && !resume_writes_alarm_->IsSet() && | 1143 if (visitor_->HasPendingWrites() && !resume_writes_alarm_->IsSet() && |
1154 CanWrite(NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, | 1144 CanWrite( |
1155 pending_handshake)) { | 1145 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, pending_handshake)) { |
1156 // We're not write blocked, but some stream didn't write out all of its | 1146 // We're not write blocked, but some stream didn't write out all of its |
1157 // bytes. Register for 'immediate' resumption so we'll keep writing after | 1147 // bytes. Register for 'immediate' resumption so we'll keep writing after |
1158 // other connections and events have had a chance to use the thread. | 1148 // other connections and events have had a chance to use the thread. |
1159 resume_writes_alarm_->Set(clock_->ApproximateNow()); | 1149 resume_writes_alarm_->Set(clock_->ApproximateNow()); |
1160 } | 1150 } |
1161 } | 1151 } |
1162 | 1152 |
1163 void QuicConnection::WriteIfNotBlocked() { | 1153 void QuicConnection::WriteIfNotBlocked() { |
1164 if (!writer_->IsWriteBlocked()) { | 1154 if (!writer_->IsWriteBlocked()) { |
1165 OnCanWrite(); | 1155 OnCanWrite(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1205 } | 1195 } |
1206 } | 1196 } |
1207 | 1197 |
1208 void QuicConnection::WritePendingRetransmissions() { | 1198 void QuicConnection::WritePendingRetransmissions() { |
1209 // Keep writing as long as there's a pending retransmission which can be | 1199 // Keep writing as long as there's a pending retransmission which can be |
1210 // written. | 1200 // written. |
1211 while (sent_packet_manager_.HasPendingRetransmissions()) { | 1201 while (sent_packet_manager_.HasPendingRetransmissions()) { |
1212 const QuicSentPacketManager::PendingRetransmission pending = | 1202 const QuicSentPacketManager::PendingRetransmission pending = |
1213 sent_packet_manager_.NextPendingRetransmission(); | 1203 sent_packet_manager_.NextPendingRetransmission(); |
1214 if (GetPacketType(&pending.retransmittable_frames) == NORMAL && | 1204 if (GetPacketType(&pending.retransmittable_frames) == NORMAL && |
1215 !CanWrite(pending.transmission_type, HAS_RETRANSMITTABLE_DATA, | 1205 !CanWrite(pending.transmission_type, |
| 1206 HAS_RETRANSMITTABLE_DATA, |
1216 pending.retransmittable_frames.HasCryptoHandshake())) { | 1207 pending.retransmittable_frames.HasCryptoHandshake())) { |
1217 break; | 1208 break; |
1218 } | 1209 } |
1219 | 1210 |
1220 // Re-packetize the frames with a new sequence number for retransmission. | 1211 // Re-packetize the frames with a new sequence number for retransmission. |
1221 // Retransmitted data packets do not use FEC, even when it's enabled. | 1212 // Retransmitted data packets do not use FEC, even when it's enabled. |
1222 // Retransmitted packets use the same sequence number length as the | 1213 // Retransmitted packets use the same sequence number length as the |
1223 // original. | 1214 // original. |
1224 // Flush the packet creator before making a new packet. | 1215 // Flush the packet creator before making a new packet. |
1225 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that | 1216 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that |
1226 // does not require the creator to be flushed. | 1217 // does not require the creator to be flushed. |
1227 Flush(); | 1218 Flush(); |
1228 SerializedPacket serialized_packet = packet_creator_.ReserializeAllFrames( | 1219 SerializedPacket serialized_packet = packet_creator_.ReserializeAllFrames( |
1229 pending.retransmittable_frames.frames(), | 1220 pending.retransmittable_frames.frames(), |
1230 pending.sequence_number_length); | 1221 pending.sequence_number_length); |
1231 | 1222 |
1232 DVLOG(1) << ENDPOINT << "Retransmitting " << pending.sequence_number | 1223 DVLOG(1) << ENDPOINT << "Retransmitting " << pending.sequence_number |
1233 << " as " << serialized_packet.sequence_number; | 1224 << " as " << serialized_packet.sequence_number; |
1234 if (debug_visitor_) { | 1225 if (debug_visitor_) { |
1235 debug_visitor_->OnPacketRetransmitted( | 1226 debug_visitor_->OnPacketRetransmitted(pending.sequence_number, |
1236 pending.sequence_number, serialized_packet.sequence_number); | 1227 serialized_packet.sequence_number); |
1237 } | 1228 } |
1238 sent_packet_manager_.OnRetransmittedPacket( | 1229 sent_packet_manager_.OnRetransmittedPacket( |
1239 pending.sequence_number, serialized_packet.sequence_number); | 1230 pending.sequence_number, serialized_packet.sequence_number); |
1240 | 1231 |
1241 SendOrQueuePacket(pending.retransmittable_frames.encryption_level(), | 1232 SendOrQueuePacket(pending.retransmittable_frames.encryption_level(), |
1242 serialized_packet, | 1233 serialized_packet, |
1243 pending.transmission_type); | 1234 pending.transmission_type); |
1244 } | 1235 } |
1245 } | 1236 } |
1246 | 1237 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1303 send_alarm_->Cancel(); | 1294 send_alarm_->Cancel(); |
1304 send_alarm_->Set(now.Add(delay)); | 1295 send_alarm_->Set(now.Add(delay)); |
1305 DVLOG(1) << "Delaying sending."; | 1296 DVLOG(1) << "Delaying sending."; |
1306 return false; | 1297 return false; |
1307 } | 1298 } |
1308 return true; | 1299 return true; |
1309 } | 1300 } |
1310 | 1301 |
1311 bool QuicConnection::WritePacket(QueuedPacket packet) { | 1302 bool QuicConnection::WritePacket(QueuedPacket packet) { |
1312 QuicPacketSequenceNumber sequence_number = packet.sequence_number; | 1303 QuicPacketSequenceNumber sequence_number = packet.sequence_number; |
1313 if (ShouldDiscardPacket(packet.encryption_level, | 1304 if (ShouldDiscardPacket( |
1314 sequence_number, | 1305 packet.encryption_level, sequence_number, packet.retransmittable)) { |
1315 packet.retransmittable)) { | |
1316 ++stats_.packets_discarded; | 1306 ++stats_.packets_discarded; |
1317 return true; | 1307 return true; |
1318 } | 1308 } |
1319 | 1309 |
1320 // If the packet is CONNECTION_CLOSE, we need to try to send it immediately | 1310 // If the packet is CONNECTION_CLOSE, we need to try to send it immediately |
1321 // and encrypt it to hand it off to TimeWaitListManager. | 1311 // and encrypt it to hand it off to TimeWaitListManager. |
1322 // If the packet is QUEUED, we don't re-consult the congestion control. | 1312 // If the packet is QUEUED, we don't re-consult the congestion control. |
1323 // This ensures packets are sent in sequence number order. | 1313 // This ensures packets are sent in sequence number order. |
1324 // TODO(ianswett): The congestion control should have been consulted before | 1314 // TODO(ianswett): The congestion control should have been consulted before |
1325 // serializing the packet, so this could be turned into a LOG_IF(DFATAL). | 1315 // serializing the packet, so this could be turned into a LOG_IF(DFATAL). |
1326 if (packet.type == NORMAL && !CanWrite(packet.transmission_type, | 1316 if (packet.type == NORMAL && |
1327 packet.retransmittable, | 1317 !CanWrite( |
1328 packet.handshake)) { | 1318 packet.transmission_type, packet.retransmittable, packet.handshake)) { |
1329 return false; | 1319 return false; |
1330 } | 1320 } |
1331 | 1321 |
1332 // Some encryption algorithms require the packet sequence numbers not be | 1322 // Some encryption algorithms require the packet sequence numbers not be |
1333 // repeated. | 1323 // repeated. |
1334 DCHECK_LE(sequence_number_of_last_sent_packet_, sequence_number); | 1324 DCHECK_LE(sequence_number_of_last_sent_packet_, sequence_number); |
1335 sequence_number_of_last_sent_packet_ = sequence_number; | 1325 sequence_number_of_last_sent_packet_ = sequence_number; |
1336 | 1326 |
1337 QuicEncryptedPacket* encrypted = framer_.EncryptPacket( | 1327 QuicEncryptedPacket* encrypted = framer_.EncryptPacket( |
1338 packet.encryption_level, sequence_number, *packet.packet); | 1328 packet.encryption_level, sequence_number, *packet.packet); |
(...skipping 16 matching lines...) Expand all Loading... |
1355 if (writer_->IsWriteBlocked()) { | 1345 if (writer_->IsWriteBlocked()) { |
1356 visitor_->OnWriteBlocked(); | 1346 visitor_->OnWriteBlocked(); |
1357 return true; | 1347 return true; |
1358 } | 1348 } |
1359 } else { | 1349 } else { |
1360 encrypted_deleter.reset(encrypted); | 1350 encrypted_deleter.reset(encrypted); |
1361 } | 1351 } |
1362 | 1352 |
1363 LOG_IF(DFATAL, encrypted->length() > options()->max_packet_length) | 1353 LOG_IF(DFATAL, encrypted->length() > options()->max_packet_length) |
1364 << "Writing an encrypted packet larger than max_packet_length:" | 1354 << "Writing an encrypted packet larger than max_packet_length:" |
1365 << options()->max_packet_length << " encrypted length: " | 1355 << options()->max_packet_length |
1366 << encrypted->length(); | 1356 << " encrypted length: " << encrypted->length(); |
1367 DVLOG(1) << ENDPOINT << "Sending packet " << sequence_number | 1357 DVLOG(1) << ENDPOINT << "Sending packet " << sequence_number << " : " |
1368 << " : " << (packet.packet->is_fec_packet() ? "FEC " : | 1358 << (packet.packet->is_fec_packet() |
1369 (packet.retransmittable == HAS_RETRANSMITTABLE_DATA | 1359 ? "FEC " |
1370 ? "data bearing " : " ack only ")) | 1360 : (packet.retransmittable == HAS_RETRANSMITTABLE_DATA |
1371 << ", encryption level: " | 1361 ? "data bearing " |
| 1362 : " ack only ")) << ", encryption level: " |
1372 << QuicUtils::EncryptionLevelToString(packet.encryption_level) | 1363 << QuicUtils::EncryptionLevelToString(packet.encryption_level) |
1373 << ", length:" << packet.packet->length() << ", encrypted length:" | 1364 << ", length:" << packet.packet->length() |
1374 << encrypted->length(); | 1365 << ", encrypted length:" << encrypted->length(); |
1375 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl | 1366 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl |
1376 << QuicUtils::StringToHexASCIIDump(packet.packet->AsStringPiece()); | 1367 << QuicUtils::StringToHexASCIIDump(packet.packet->AsStringPiece()); |
1377 | 1368 |
1378 DCHECK(encrypted->length() <= kMaxPacketSize || | 1369 DCHECK(encrypted->length() <= kMaxPacketSize || |
1379 FLAGS_quic_allow_oversized_packets_for_test) | 1370 FLAGS_quic_allow_oversized_packets_for_test) |
1380 << "Packet " << sequence_number << " will not be read; too large: " | 1371 << "Packet " << sequence_number |
1381 << packet.packet->length() << " " << encrypted->length() << " " | 1372 << " will not be read; too large: " << packet.packet->length() << " " |
| 1373 << encrypted->length() << " " |
1382 << " close: " << (packet.type == CONNECTION_CLOSE ? "yes" : "no"); | 1374 << " close: " << (packet.type == CONNECTION_CLOSE ? "yes" : "no"); |
1383 | 1375 |
1384 DCHECK(pending_write_.get() == NULL); | 1376 DCHECK(pending_write_.get() == NULL); |
1385 pending_write_.reset(new QueuedPacket(packet)); | 1377 pending_write_.reset(new QueuedPacket(packet)); |
1386 | 1378 |
1387 WriteResult result = writer_->WritePacket(encrypted->data(), | 1379 WriteResult result = writer_->WritePacket(encrypted->data(), |
1388 encrypted->length(), | 1380 encrypted->length(), |
1389 self_address().address(), | 1381 self_address().address(), |
1390 peer_address()); | 1382 peer_address()); |
1391 if (result.error_code == ERR_IO_PENDING) { | 1383 if (result.error_code == ERR_IO_PENDING) { |
(...skipping 24 matching lines...) Expand all Loading... |
1416 return true; | 1408 return true; |
1417 } | 1409 } |
1418 return false; | 1410 return false; |
1419 } | 1411 } |
1420 | 1412 |
1421 bool QuicConnection::ShouldDiscardPacket( | 1413 bool QuicConnection::ShouldDiscardPacket( |
1422 EncryptionLevel level, | 1414 EncryptionLevel level, |
1423 QuicPacketSequenceNumber sequence_number, | 1415 QuicPacketSequenceNumber sequence_number, |
1424 HasRetransmittableData retransmittable) { | 1416 HasRetransmittableData retransmittable) { |
1425 if (!connected_) { | 1417 if (!connected_) { |
1426 DVLOG(1) << ENDPOINT | 1418 DVLOG(1) << ENDPOINT << "Not sending packet as connection is disconnected."; |
1427 << "Not sending packet as connection is disconnected."; | |
1428 return true; | 1419 return true; |
1429 } | 1420 } |
1430 | 1421 |
1431 if (encryption_level_ == ENCRYPTION_FORWARD_SECURE && | 1422 if (encryption_level_ == ENCRYPTION_FORWARD_SECURE && |
1432 level == ENCRYPTION_NONE) { | 1423 level == ENCRYPTION_NONE) { |
1433 // Drop packets that are NULL encrypted since the peer won't accept them | 1424 // Drop packets that are NULL encrypted since the peer won't accept them |
1434 // anymore. | 1425 // anymore. |
1435 DVLOG(1) << ENDPOINT << "Dropping packet: " << sequence_number | 1426 DVLOG(1) << ENDPOINT << "Dropping packet: " << sequence_number |
1436 << " since the packet is NULL encrypted."; | 1427 << " since the packet is NULL encrypted."; |
1437 sent_packet_manager_.DiscardUnackedPacket(sequence_number); | 1428 sent_packet_manager_.DiscardUnackedPacket(sequence_number); |
1438 return true; | 1429 return true; |
1439 } | 1430 } |
1440 | 1431 |
1441 // If the packet has been discarded before sending, don't send it. | 1432 // If the packet has been discarded before sending, don't send it. |
1442 // This occurs if a packet gets serialized, queued, then discarded. | 1433 // This occurs if a packet gets serialized, queued, then discarded. |
1443 if (!sent_packet_manager_.IsUnacked(sequence_number)) { | 1434 if (!sent_packet_manager_.IsUnacked(sequence_number)) { |
1444 DVLOG(1) << ENDPOINT << "Dropping packet before sending: " | 1435 DVLOG(1) << ENDPOINT |
1445 << sequence_number << " since it has already been discarded."; | 1436 << "Dropping packet before sending: " << sequence_number |
| 1437 << " since it has already been discarded."; |
1446 return true; | 1438 return true; |
1447 } | 1439 } |
1448 | 1440 |
1449 if (retransmittable == HAS_RETRANSMITTABLE_DATA && | 1441 if (retransmittable == HAS_RETRANSMITTABLE_DATA && |
1450 !sent_packet_manager_.HasRetransmittableFrames(sequence_number)) { | 1442 !sent_packet_manager_.HasRetransmittableFrames(sequence_number)) { |
1451 DVLOG(1) << ENDPOINT << "Dropping packet: " << sequence_number | 1443 DVLOG(1) << ENDPOINT << "Dropping packet: " << sequence_number |
1452 << " since a previous transmission has been acked."; | 1444 << " since a previous transmission has been acked."; |
1453 sent_packet_manager_.DiscardUnackedPacket(sequence_number); | 1445 sent_packet_manager_.DiscardUnackedPacket(sequence_number); |
1454 return true; | 1446 return true; |
1455 } | 1447 } |
1456 | 1448 |
1457 return false; | 1449 return false; |
1458 } | 1450 } |
1459 | 1451 |
1460 bool QuicConnection::OnPacketSent(WriteResult result) { | 1452 bool QuicConnection::OnPacketSent(WriteResult result) { |
1461 DCHECK_NE(WRITE_STATUS_BLOCKED, result.status); | 1453 DCHECK_NE(WRITE_STATUS_BLOCKED, result.status); |
1462 if (pending_write_.get() == NULL) { | 1454 if (pending_write_.get() == NULL) { |
1463 LOG(DFATAL) << "OnPacketSent called without a pending write."; | 1455 LOG(DFATAL) << "OnPacketSent called without a pending write."; |
1464 return false; | 1456 return false; |
1465 } | 1457 } |
1466 | 1458 |
1467 QuicPacketSequenceNumber sequence_number = pending_write_->sequence_number; | 1459 QuicPacketSequenceNumber sequence_number = pending_write_->sequence_number; |
1468 TransmissionType transmission_type = pending_write_->transmission_type; | 1460 TransmissionType transmission_type = pending_write_->transmission_type; |
1469 HasRetransmittableData retransmittable = pending_write_->retransmittable; | 1461 HasRetransmittableData retransmittable = pending_write_->retransmittable; |
1470 size_t length = pending_write_->length; | 1462 size_t length = pending_write_->length; |
1471 pending_write_.reset(); | 1463 pending_write_.reset(); |
1472 | 1464 |
1473 if (result.status == WRITE_STATUS_ERROR) { | 1465 if (result.status == WRITE_STATUS_ERROR) { |
1474 DVLOG(1) << "Write failed with error: " << result.error_code << " (" | 1466 DVLOG(1) << "Write failed with error: " << result.error_code << " (" |
1475 << ErrorToString(result.error_code) << ")"; | 1467 << ErrorToString(result.error_code) << ")"; |
1476 // We can't send an error as the socket is presumably borked. | 1468 // We can't send an error as the socket is presumably borked. |
1477 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); | 1469 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); |
1478 return false; | 1470 return false; |
1479 } | 1471 } |
1480 | 1472 |
1481 QuicTime now = clock_->Now(); | 1473 QuicTime now = clock_->Now(); |
1482 if (transmission_type == NOT_RETRANSMISSION) { | 1474 if (transmission_type == NOT_RETRANSMISSION) { |
1483 time_of_last_sent_new_packet_ = now; | 1475 time_of_last_sent_new_packet_ = now; |
1484 } | 1476 } |
1485 SetPingAlarm(); | 1477 SetPingAlarm(); |
1486 DVLOG(1) << ENDPOINT << "time of last sent packet: " | 1478 DVLOG(1) << ENDPOINT |
1487 << now.ToDebuggingValue(); | 1479 << "time of last sent packet: " << now.ToDebuggingValue(); |
1488 | 1480 |
1489 // TODO(ianswett): Change the sequence number length and other packet creator | 1481 // TODO(ianswett): Change the sequence number length and other packet creator |
1490 // options by a more explicit API than setting a struct value directly. | 1482 // options by a more explicit API than setting a struct value directly. |
1491 packet_creator_.UpdateSequenceNumberLength( | 1483 packet_creator_.UpdateSequenceNumberLength( |
1492 received_packet_manager_.least_packet_awaited_by_peer(), | 1484 received_packet_manager_.least_packet_awaited_by_peer(), |
1493 sent_packet_manager_.GetCongestionWindow()); | 1485 sent_packet_manager_.GetCongestionWindow()); |
1494 | 1486 |
1495 bool reset_retransmission_alarm = | 1487 bool reset_retransmission_alarm = sent_packet_manager_.OnPacketSent( |
1496 sent_packet_manager_.OnPacketSent(sequence_number, now, length, | 1488 sequence_number, now, length, transmission_type, retransmittable); |
1497 transmission_type, retransmittable); | |
1498 | 1489 |
1499 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) { | 1490 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) { |
1500 retransmission_alarm_->Cancel(); | 1491 retransmission_alarm_->Cancel(); |
1501 QuicTime retransmission_time = sent_packet_manager_.GetRetransmissionTime(); | 1492 QuicTime retransmission_time = sent_packet_manager_.GetRetransmissionTime(); |
1502 if (retransmission_time != QuicTime::Zero()) { | 1493 if (retransmission_time != QuicTime::Zero()) { |
1503 retransmission_alarm_->Set(retransmission_time); | 1494 retransmission_alarm_->Set(retransmission_time); |
1504 } | 1495 } |
1505 } | 1496 } |
1506 | 1497 |
1507 stats_.bytes_sent += result.bytes_written; | 1498 stats_.bytes_sent += result.bytes_written; |
1508 ++stats_.packets_sent; | 1499 ++stats_.packets_sent; |
1509 | 1500 |
1510 if (transmission_type != NOT_RETRANSMISSION) { | 1501 if (transmission_type != NOT_RETRANSMISSION) { |
1511 stats_.bytes_retransmitted += result.bytes_written; | 1502 stats_.bytes_retransmitted += result.bytes_written; |
1512 ++stats_.packets_retransmitted; | 1503 ++stats_.packets_retransmitted; |
1513 } | 1504 } |
1514 | 1505 |
1515 return true; | 1506 return true; |
1516 } | 1507 } |
1517 | 1508 |
1518 bool QuicConnection::OnSerializedPacket( | 1509 bool QuicConnection::OnSerializedPacket( |
1519 const SerializedPacket& serialized_packet) { | 1510 const SerializedPacket& serialized_packet) { |
1520 if (serialized_packet.retransmittable_frames) { | 1511 if (serialized_packet.retransmittable_frames) { |
1521 serialized_packet.retransmittable_frames-> | 1512 serialized_packet.retransmittable_frames->set_encryption_level( |
1522 set_encryption_level(encryption_level_); | 1513 encryption_level_); |
1523 } | 1514 } |
1524 sent_packet_manager_.OnSerializedPacket(serialized_packet); | 1515 sent_packet_manager_.OnSerializedPacket(serialized_packet); |
1525 // The TransmissionType is NOT_RETRANSMISSION because all retransmissions | 1516 // The TransmissionType is NOT_RETRANSMISSION because all retransmissions |
1526 // serialize packets and invoke SendOrQueuePacket directly. | 1517 // serialize packets and invoke SendOrQueuePacket directly. |
1527 return SendOrQueuePacket(encryption_level_, | 1518 return SendOrQueuePacket( |
1528 serialized_packet, | 1519 encryption_level_, serialized_packet, NOT_RETRANSMISSION); |
1529 NOT_RETRANSMISSION); | |
1530 } | 1520 } |
1531 | 1521 |
1532 bool QuicConnection::SendOrQueuePacket(EncryptionLevel level, | 1522 bool QuicConnection::SendOrQueuePacket(EncryptionLevel level, |
1533 const SerializedPacket& packet, | 1523 const SerializedPacket& packet, |
1534 TransmissionType transmission_type) { | 1524 TransmissionType transmission_type) { |
1535 if (packet.packet == NULL) { | 1525 if (packet.packet == NULL) { |
1536 LOG(DFATAL) << "NULL packet passed in to SendOrQueuePacket"; | 1526 LOG(DFATAL) << "NULL packet passed in to SendOrQueuePacket"; |
1537 return true; | 1527 return true; |
1538 } | 1528 } |
1539 | 1529 |
1540 sent_entropy_manager_.RecordPacketEntropyHash(packet.sequence_number, | 1530 sent_entropy_manager_.RecordPacketEntropyHash(packet.sequence_number, |
1541 packet.entropy_hash); | 1531 packet.entropy_hash); |
1542 QueuedPacket queued_packet(packet, level, transmission_type); | 1532 QueuedPacket queued_packet(packet, level, transmission_type); |
1543 // If there are already queued packets, put this at the end, | 1533 // If there are already queued packets, put this at the end, |
1544 // unless it's ConnectionClose, in which case it is written immediately. | 1534 // unless it's ConnectionClose, in which case it is written immediately. |
1545 if ((queued_packet.type == CONNECTION_CLOSE || queued_packets_.empty()) && | 1535 if ((queued_packet.type == CONNECTION_CLOSE || queued_packets_.empty()) && |
1546 WritePacket(queued_packet)) { | 1536 WritePacket(queued_packet)) { |
1547 delete packet.packet; | 1537 delete packet.packet; |
1548 return true; | 1538 return true; |
1549 } | 1539 } |
1550 queued_packet.type = QUEUED; | 1540 queued_packet.type = QUEUED; |
1551 queued_packets_.push_back(queued_packet); | 1541 queued_packets_.push_back(queued_packet); |
1552 return false; | 1542 return false; |
1553 } | 1543 } |
1554 | 1544 |
1555 void QuicConnection::UpdateStopWaiting(QuicStopWaitingFrame* stop_waiting) { | 1545 void QuicConnection::UpdateStopWaiting(QuicStopWaitingFrame* stop_waiting) { |
1556 stop_waiting->least_unacked = GetLeastUnacked(); | 1546 stop_waiting->least_unacked = GetLeastUnacked(); |
1557 stop_waiting->entropy_hash = sent_entropy_manager_.EntropyHash( | 1547 stop_waiting->entropy_hash = |
1558 stop_waiting->least_unacked - 1); | 1548 sent_entropy_manager_.EntropyHash(stop_waiting->least_unacked - 1); |
1559 } | 1549 } |
1560 | 1550 |
1561 void QuicConnection::SendPing() { | 1551 void QuicConnection::SendPing() { |
1562 if (retransmission_alarm_->IsSet()) { | 1552 if (retransmission_alarm_->IsSet()) { |
1563 return; | 1553 return; |
1564 } | 1554 } |
1565 if (version() <= QUIC_VERSION_17) { | 1555 if (version() <= QUIC_VERSION_17) { |
1566 // TODO(rch): remove this when we remove version 17. | 1556 // TODO(rch): remove this when we remove version 17. |
1567 // This is a horrible hideous hack which we should not support. | 1557 // This is a horrible hideous hack which we should not support. |
1568 IOVector data; | 1558 IOVector data; |
(...skipping 11 matching lines...) Expand all Loading... |
1580 | 1570 |
1581 void QuicConnection::SendAck() { | 1571 void QuicConnection::SendAck() { |
1582 ack_alarm_->Cancel(); | 1572 ack_alarm_->Cancel(); |
1583 stop_waiting_count_ = 0; | 1573 stop_waiting_count_ = 0; |
1584 // TODO(rch): delay this until the CreateFeedbackFrame | 1574 // TODO(rch): delay this until the CreateFeedbackFrame |
1585 // method is invoked. This requires changes SetShouldSendAck | 1575 // method is invoked. This requires changes SetShouldSendAck |
1586 // to be a no-arg method, and re-jiggering its implementation. | 1576 // to be a no-arg method, and re-jiggering its implementation. |
1587 bool send_feedback = false; | 1577 bool send_feedback = false; |
1588 if (received_packet_manager_.GenerateCongestionFeedback( | 1578 if (received_packet_manager_.GenerateCongestionFeedback( |
1589 &outgoing_congestion_feedback_)) { | 1579 &outgoing_congestion_feedback_)) { |
1590 DVLOG(1) << ENDPOINT << "Sending feedback: " | 1580 DVLOG(1) << ENDPOINT |
1591 << outgoing_congestion_feedback_; | 1581 << "Sending feedback: " << outgoing_congestion_feedback_; |
1592 send_feedback = true; | 1582 send_feedback = true; |
1593 } | 1583 } |
1594 | 1584 |
1595 packet_generator_.SetShouldSendAck(send_feedback, | 1585 packet_generator_.SetShouldSendAck(send_feedback, |
1596 version() > QUIC_VERSION_15); | 1586 version() > QUIC_VERSION_15); |
1597 } | 1587 } |
1598 | 1588 |
1599 void QuicConnection::OnRetransmissionTimeout() { | 1589 void QuicConnection::OnRetransmissionTimeout() { |
1600 if (!sent_packet_manager_.HasUnackedPackets()) { | 1590 if (!sent_packet_manager_.HasUnackedPackets()) { |
1601 return; | 1591 return; |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1744 if (connected_) { | 1734 if (connected_) { |
1745 // It's possible that while sending the connection close packet, we get a | 1735 // It's possible that while sending the connection close packet, we get a |
1746 // socket error and disconnect right then and there. Avoid a double | 1736 // socket error and disconnect right then and there. Avoid a double |
1747 // disconnect in that case. | 1737 // disconnect in that case. |
1748 CloseConnection(error, false); | 1738 CloseConnection(error, false); |
1749 } | 1739 } |
1750 } | 1740 } |
1751 | 1741 |
1752 void QuicConnection::SendConnectionClosePacket(QuicErrorCode error, | 1742 void QuicConnection::SendConnectionClosePacket(QuicErrorCode error, |
1753 const string& details) { | 1743 const string& details) { |
1754 DVLOG(1) << ENDPOINT << "Force closing " << connection_id() | 1744 DVLOG(1) << ENDPOINT << "Force closing " << connection_id() << " with error " |
1755 << " with error " << QuicUtils::ErrorToString(error) | 1745 << QuicUtils::ErrorToString(error) << " (" << error << ") " |
1756 << " (" << error << ") " << details; | 1746 << details; |
1757 ScopedPacketBundler ack_bundler(this, SEND_ACK); | 1747 ScopedPacketBundler ack_bundler(this, SEND_ACK); |
1758 QuicConnectionCloseFrame* frame = new QuicConnectionCloseFrame(); | 1748 QuicConnectionCloseFrame* frame = new QuicConnectionCloseFrame(); |
1759 frame->error_code = error; | 1749 frame->error_code = error; |
1760 frame->error_details = details; | 1750 frame->error_details = details; |
1761 packet_generator_.AddControlFrame(QuicFrame(frame)); | 1751 packet_generator_.AddControlFrame(QuicFrame(frame)); |
1762 Flush(); | 1752 Flush(); |
1763 } | 1753 } |
1764 | 1754 |
1765 void QuicConnection::CloseConnection(QuicErrorCode error, bool from_peer) { | 1755 void QuicConnection::CloseConnection(QuicErrorCode error, bool from_peer) { |
1766 if (!connected_) { | 1756 if (!connected_) { |
1767 DLOG(DFATAL) << "Error: attempt to close an already closed connection" | 1757 DLOG(DFATAL) << "Error: attempt to close an already closed connection" |
1768 << base::debug::StackTrace().ToString(); | 1758 << base::debug::StackTrace().ToString(); |
1769 return; | 1759 return; |
1770 } | 1760 } |
1771 connected_ = false; | 1761 connected_ = false; |
1772 visitor_->OnConnectionClosed(error, from_peer); | 1762 visitor_->OnConnectionClosed(error, from_peer); |
1773 // Cancel the alarms so they don't trigger any action now that the | 1763 // Cancel the alarms so they don't trigger any action now that the |
1774 // connection is closed. | 1764 // connection is closed. |
1775 ack_alarm_->Cancel(); | 1765 ack_alarm_->Cancel(); |
1776 resume_writes_alarm_->Cancel(); | 1766 resume_writes_alarm_->Cancel(); |
1777 retransmission_alarm_->Cancel(); | 1767 retransmission_alarm_->Cancel(); |
1778 send_alarm_->Cancel(); | 1768 send_alarm_->Cancel(); |
1779 timeout_alarm_->Cancel(); | 1769 timeout_alarm_->Cancel(); |
1780 } | 1770 } |
1781 | 1771 |
1782 void QuicConnection::SendGoAway(QuicErrorCode error, | 1772 void QuicConnection::SendGoAway(QuicErrorCode error, |
1783 QuicStreamId last_good_stream_id, | 1773 QuicStreamId last_good_stream_id, |
1784 const string& reason) { | 1774 const string& reason) { |
1785 DVLOG(1) << ENDPOINT << "Going away with error " | 1775 DVLOG(1) << ENDPOINT << "Going away with error " |
1786 << QuicUtils::ErrorToString(error) | 1776 << QuicUtils::ErrorToString(error) << " (" << error << ")"; |
1787 << " (" << error << ")"; | |
1788 | 1777 |
1789 // Opportunistically bundle an ack with this outgoing packet. | 1778 // Opportunistically bundle an ack with this outgoing packet. |
1790 ScopedPacketBundler ack_bundler(this, BUNDLE_PENDING_ACK); | 1779 ScopedPacketBundler ack_bundler(this, BUNDLE_PENDING_ACK); |
1791 packet_generator_.AddControlFrame( | 1780 packet_generator_.AddControlFrame( |
1792 QuicFrame(new QuicGoAwayFrame(error, last_good_stream_id, reason))); | 1781 QuicFrame(new QuicGoAwayFrame(error, last_good_stream_id, reason))); |
1793 } | 1782 } |
1794 | 1783 |
1795 void QuicConnection::CloseFecGroupsBefore( | 1784 void QuicConnection::CloseFecGroupsBefore( |
1796 QuicPacketSequenceNumber sequence_number) { | 1785 QuicPacketSequenceNumber sequence_number) { |
1797 FecGroupMap::iterator it = group_map_.begin(); | 1786 FecGroupMap::iterator it = group_map_.begin(); |
(...skipping 13 matching lines...) Expand all Loading... |
1811 delete fec_group; | 1800 delete fec_group; |
1812 it = next; | 1801 it = next; |
1813 } | 1802 } |
1814 } | 1803 } |
1815 | 1804 |
1816 void QuicConnection::Flush() { | 1805 void QuicConnection::Flush() { |
1817 packet_generator_.FlushAllQueuedFrames(); | 1806 packet_generator_.FlushAllQueuedFrames(); |
1818 } | 1807 } |
1819 | 1808 |
1820 bool QuicConnection::HasQueuedData() const { | 1809 bool QuicConnection::HasQueuedData() const { |
1821 return pending_version_negotiation_packet_ || | 1810 return pending_version_negotiation_packet_ || !queued_packets_.empty() || |
1822 !queued_packets_.empty() || packet_generator_.HasQueuedFrames(); | 1811 packet_generator_.HasQueuedFrames(); |
1823 } | 1812 } |
1824 | 1813 |
1825 bool QuicConnection::CanWriteStreamData() { | 1814 bool QuicConnection::CanWriteStreamData() { |
1826 // Don't write stream data if there are negotiation or queued data packets | 1815 // Don't write stream data if there are negotiation or queued data packets |
1827 // to send. Otherwise, continue and bundle as many frames as possible. | 1816 // to send. Otherwise, continue and bundle as many frames as possible. |
1828 if (pending_version_negotiation_packet_ || !queued_packets_.empty()) { | 1817 if (pending_version_negotiation_packet_ || !queued_packets_.empty()) { |
1829 return false; | 1818 return false; |
1830 } | 1819 } |
1831 | 1820 |
1832 IsHandshake pending_handshake = visitor_->HasPendingHandshake() ? | 1821 IsHandshake pending_handshake = |
1833 IS_HANDSHAKE : NOT_HANDSHAKE; | 1822 visitor_->HasPendingHandshake() ? IS_HANDSHAKE : NOT_HANDSHAKE; |
1834 // Sending queued packets may have caused the socket to become write blocked, | 1823 // Sending queued packets may have caused the socket to become write blocked, |
1835 // or the congestion manager to prohibit sending. If we've sent everything | 1824 // or the congestion manager to prohibit sending. If we've sent everything |
1836 // we had queued and we're still not blocked, let the visitor know it can | 1825 // we had queued and we're still not blocked, let the visitor know it can |
1837 // write more. | 1826 // write more. |
1838 return ShouldGeneratePacket(NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, | 1827 return ShouldGeneratePacket( |
1839 pending_handshake); | 1828 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, pending_handshake); |
1840 } | 1829 } |
1841 | 1830 |
1842 void QuicConnection::SetIdleNetworkTimeout(QuicTime::Delta timeout) { | 1831 void QuicConnection::SetIdleNetworkTimeout(QuicTime::Delta timeout) { |
1843 if (timeout < idle_network_timeout_) { | 1832 if (timeout < idle_network_timeout_) { |
1844 idle_network_timeout_ = timeout; | 1833 idle_network_timeout_ = timeout; |
1845 CheckForTimeout(); | 1834 CheckForTimeout(); |
1846 } else { | 1835 } else { |
1847 idle_network_timeout_ = timeout; | 1836 idle_network_timeout_ = timeout; |
1848 } | 1837 } |
1849 } | 1838 } |
1850 | 1839 |
1851 void QuicConnection::SetOverallConnectionTimeout(QuicTime::Delta timeout) { | 1840 void QuicConnection::SetOverallConnectionTimeout(QuicTime::Delta timeout) { |
1852 if (timeout < overall_connection_timeout_) { | 1841 if (timeout < overall_connection_timeout_) { |
1853 overall_connection_timeout_ = timeout; | 1842 overall_connection_timeout_ = timeout; |
1854 CheckForTimeout(); | 1843 CheckForTimeout(); |
1855 } else { | 1844 } else { |
1856 overall_connection_timeout_ = timeout; | 1845 overall_connection_timeout_ = timeout; |
1857 } | 1846 } |
1858 } | 1847 } |
1859 | 1848 |
1860 bool QuicConnection::CheckForTimeout() { | 1849 bool QuicConnection::CheckForTimeout() { |
1861 QuicTime now = clock_->ApproximateNow(); | 1850 QuicTime now = clock_->ApproximateNow(); |
1862 QuicTime time_of_last_packet = max(time_of_last_received_packet_, | 1851 QuicTime time_of_last_packet = |
1863 time_of_last_sent_new_packet_); | 1852 max(time_of_last_received_packet_, time_of_last_sent_new_packet_); |
1864 | 1853 |
1865 // |delta| can be < 0 as |now| is approximate time but |time_of_last_packet| | 1854 // |delta| can be < 0 as |now| is approximate time but |time_of_last_packet| |
1866 // is accurate time. However, this should not change the behavior of | 1855 // is accurate time. However, this should not change the behavior of |
1867 // timeout handling. | 1856 // timeout handling. |
1868 QuicTime::Delta delta = now.Subtract(time_of_last_packet); | 1857 QuicTime::Delta delta = now.Subtract(time_of_last_packet); |
1869 DVLOG(1) << ENDPOINT << "last packet " | 1858 DVLOG(1) << ENDPOINT << "last packet " |
1870 << time_of_last_packet.ToDebuggingValue() | 1859 << time_of_last_packet.ToDebuggingValue() |
1871 << " now:" << now.ToDebuggingValue() | 1860 << " now:" << now.ToDebuggingValue() |
1872 << " delta:" << delta.ToMicroseconds() | 1861 << " delta:" << delta.ToMicroseconds() |
1873 << " network_timeout: " << idle_network_timeout_.ToMicroseconds(); | 1862 << " network_timeout: " << idle_network_timeout_.ToMicroseconds(); |
1874 if (delta >= idle_network_timeout_) { | 1863 if (delta >= idle_network_timeout_) { |
1875 DVLOG(1) << ENDPOINT << "Connection timedout due to no network activity."; | 1864 DVLOG(1) << ENDPOINT << "Connection timedout due to no network activity."; |
1876 SendConnectionClose(QUIC_CONNECTION_TIMED_OUT); | 1865 SendConnectionClose(QUIC_CONNECTION_TIMED_OUT); |
1877 return true; | 1866 return true; |
1878 } | 1867 } |
1879 | 1868 |
1880 // Next timeout delta. | 1869 // Next timeout delta. |
1881 QuicTime::Delta timeout = idle_network_timeout_.Subtract(delta); | 1870 QuicTime::Delta timeout = idle_network_timeout_.Subtract(delta); |
1882 | 1871 |
1883 if (!overall_connection_timeout_.IsInfinite()) { | 1872 if (!overall_connection_timeout_.IsInfinite()) { |
1884 QuicTime::Delta connected_time = | 1873 QuicTime::Delta connected_time = |
1885 now.Subtract(stats_.connection_creation_time); | 1874 now.Subtract(stats_.connection_creation_time); |
1886 DVLOG(1) << ENDPOINT << "connection time: " | 1875 DVLOG(1) << ENDPOINT |
1887 << connected_time.ToMilliseconds() << " overall timeout: " | 1876 << "connection time: " << connected_time.ToMilliseconds() |
| 1877 << " overall timeout: " |
1888 << overall_connection_timeout_.ToMilliseconds(); | 1878 << overall_connection_timeout_.ToMilliseconds(); |
1889 if (connected_time >= overall_connection_timeout_) { | 1879 if (connected_time >= overall_connection_timeout_) { |
1890 DVLOG(1) << ENDPOINT << | 1880 DVLOG(1) << ENDPOINT |
1891 "Connection timedout due to overall connection timeout."; | 1881 << "Connection timedout due to overall connection timeout."; |
1892 SendConnectionClose(QUIC_CONNECTION_TIMED_OUT); | 1882 SendConnectionClose(QUIC_CONNECTION_TIMED_OUT); |
1893 return true; | 1883 return true; |
1894 } | 1884 } |
1895 | 1885 |
1896 // Take the min timeout. | 1886 // Take the min timeout. |
1897 QuicTime::Delta connection_timeout = | 1887 QuicTime::Delta connection_timeout = |
1898 overall_connection_timeout_.Subtract(connected_time); | 1888 overall_connection_timeout_.Subtract(connected_time); |
1899 if (connection_timeout < timeout) { | 1889 if (connection_timeout < timeout) { |
1900 timeout = connection_timeout; | 1890 timeout = connection_timeout; |
1901 } | 1891 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1945 // If we changed the generator's batch state, restore original batch state. | 1935 // If we changed the generator's batch state, restore original batch state. |
1946 if (!already_in_batch_mode_) { | 1936 if (!already_in_batch_mode_) { |
1947 DVLOG(1) << "Leaving Batch Mode."; | 1937 DVLOG(1) << "Leaving Batch Mode."; |
1948 connection_->packet_generator_.FinishBatchOperations(); | 1938 connection_->packet_generator_.FinishBatchOperations(); |
1949 } | 1939 } |
1950 DCHECK_EQ(already_in_batch_mode_, | 1940 DCHECK_EQ(already_in_batch_mode_, |
1951 connection_->packet_generator_.InBatchMode()); | 1941 connection_->packet_generator_.InBatchMode()); |
1952 } | 1942 } |
1953 | 1943 |
1954 } // namespace net | 1944 } // namespace net |
OLD | NEW |