Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: net/quic/quic_connection.cc

Issue 515303003: Landing Recent QUIC Changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Bug fix for 409191 Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/quic/quic_connection.h ('k') | net/quic/quic_connection_logger.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 connection_->SendPing(); 147 connection_->SendPing();
148 return QuicTime::Zero(); 148 return QuicTime::Zero();
149 } 149 }
150 150
151 private: 151 private:
152 QuicConnection* connection_; 152 QuicConnection* connection_;
153 153
154 DISALLOW_COPY_AND_ASSIGN(PingAlarm); 154 DISALLOW_COPY_AND_ASSIGN(PingAlarm);
155 }; 155 };
156 156
157 QuicConnection::PacketType GetPacketType(
158 const RetransmittableFrames* retransmittable_frames) {
159 if (!retransmittable_frames) {
160 return QuicConnection::NORMAL;
161 }
162 for (size_t i = 0; i < retransmittable_frames->frames().size(); ++i) {
163 if (retransmittable_frames->frames()[i].type == CONNECTION_CLOSE_FRAME) {
164 return QuicConnection::CONNECTION_CLOSE;
165 }
166 }
167 return QuicConnection::NORMAL;
168 }
169
170 } // namespace 157 } // namespace
171 158
172 QuicConnection::QueuedPacket::QueuedPacket(SerializedPacket packet, 159 QuicConnection::QueuedPacket::QueuedPacket(SerializedPacket packet,
173 EncryptionLevel level, 160 EncryptionLevel level,
174 TransmissionType transmission_type) 161 TransmissionType transmission_type)
175 : sequence_number(packet.sequence_number), 162 : serialized_packet(packet),
176 packet(packet.packet),
177 encryption_level(level), 163 encryption_level(level),
178 transmission_type(transmission_type), 164 transmission_type(transmission_type) {
179 retransmittable((transmission_type != NOT_RETRANSMISSION ||
180 packet.retransmittable_frames != NULL) ?
181 HAS_RETRANSMITTABLE_DATA : NO_RETRANSMITTABLE_DATA),
182 handshake(packet.retransmittable_frames == NULL ?
183 NOT_HANDSHAKE : packet.retransmittable_frames->HasCryptoHandshake()),
184 type(GetPacketType(packet.retransmittable_frames)),
185 length(packet.packet->length()) {
186 } 165 }
187 166
188 #define ENDPOINT (is_server_ ? "Server: " : " Client: ") 167 #define ENDPOINT (is_server_ ? "Server: " : " Client: ")
189 168
190 QuicConnection::QuicConnection(QuicConnectionId connection_id, 169 QuicConnection::QuicConnection(QuicConnectionId connection_id,
191 IPEndPoint address, 170 IPEndPoint address,
192 QuicConnectionHelperInterface* helper, 171 QuicConnectionHelperInterface* helper,
193 const PacketWriterFactory& writer_factory, 172 const PacketWriterFactory& writer_factory,
194 bool owns_writer, 173 bool owns_writer,
195 bool is_server, 174 bool is_server,
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 } 234 }
256 235
257 QuicConnection::~QuicConnection() { 236 QuicConnection::~QuicConnection() {
258 if (owns_writer_) { 237 if (owns_writer_) {
259 delete writer_; 238 delete writer_;
260 } 239 }
261 STLDeleteElements(&undecryptable_packets_); 240 STLDeleteElements(&undecryptable_packets_);
262 STLDeleteValues(&group_map_); 241 STLDeleteValues(&group_map_);
263 for (QueuedPacketList::iterator it = queued_packets_.begin(); 242 for (QueuedPacketList::iterator it = queued_packets_.begin();
264 it != queued_packets_.end(); ++it) { 243 it != queued_packets_.end(); ++it) {
265 delete it->packet; 244 delete it->serialized_packet.packet;
266 } 245 }
267 } 246 }
268 247
269 void QuicConnection::SetFromConfig(const QuicConfig& config) { 248 void QuicConnection::SetFromConfig(const QuicConfig& config) {
270 SetIdleNetworkTimeout(config.idle_connection_state_lifetime()); 249 SetIdleNetworkTimeout(config.idle_connection_state_lifetime());
271 sent_packet_manager_.SetFromConfig(config); 250 sent_packet_manager_.SetFromConfig(config);
272 } 251 }
273 252
274 bool QuicConnection::SelectMutualVersion( 253 bool QuicConnection::SelectMutualVersion(
275 const QuicVersionVector& available_versions) { 254 const QuicVersionVector& available_versions) {
(...skipping 17 matching lines...) Expand all
293 // Packets that we cannot decrypt are dropped. 272 // Packets that we cannot decrypt are dropped.
294 // TODO(rch): add stats to measure this. 273 // TODO(rch): add stats to measure this.
295 if (!connected_ || framer->error() == QUIC_DECRYPTION_FAILURE) { 274 if (!connected_ || framer->error() == QUIC_DECRYPTION_FAILURE) {
296 return; 275 return;
297 } 276 }
298 SendConnectionCloseWithDetails(framer->error(), framer->detailed_error()); 277 SendConnectionCloseWithDetails(framer->error(), framer->detailed_error());
299 } 278 }
300 279
301 void QuicConnection::OnPacket() { 280 void QuicConnection::OnPacket() {
302 DCHECK(last_stream_frames_.empty() && 281 DCHECK(last_stream_frames_.empty() &&
282 last_ack_frames_.empty() &&
283 last_congestion_frames_.empty() &&
284 last_stop_waiting_frames_.empty() &&
285 last_rst_frames_.empty() &&
303 last_goaway_frames_.empty() && 286 last_goaway_frames_.empty() &&
304 last_window_update_frames_.empty() && 287 last_window_update_frames_.empty() &&
305 last_blocked_frames_.empty() && 288 last_blocked_frames_.empty() &&
306 last_rst_frames_.empty() && 289 last_ping_frames_.empty() &&
307 last_ack_frames_.empty() && 290 last_close_frames_.empty());
308 last_congestion_frames_.empty() &&
309 last_stop_waiting_frames_.empty());
310 } 291 }
311 292
312 void QuicConnection::OnPublicResetPacket( 293 void QuicConnection::OnPublicResetPacket(
313 const QuicPublicResetPacket& packet) { 294 const QuicPublicResetPacket& packet) {
314 if (debug_visitor_.get() != NULL) { 295 if (debug_visitor_.get() != NULL) {
315 debug_visitor_->OnPublicResetPacket(packet); 296 debug_visitor_->OnPublicResetPacket(packet);
316 } 297 }
317 CloseConnection(QUIC_PUBLIC_RESET, true); 298 CloseConnection(QUIC_PUBLIC_RESET, true);
318 299
319 DVLOG(1) << ENDPOINT << "Connection " << connection_id() 300 DVLOG(1) << ENDPOINT << "Connection " << connection_id()
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 // Might be old packets that were sent by the client before the version 337 // Might be old packets that were sent by the client before the version
357 // was negotiated. Drop these. 338 // was negotiated. Drop these.
358 return false; 339 return false;
359 340
360 default: 341 default:
361 DCHECK(false); 342 DCHECK(false);
362 } 343 }
363 344
364 version_negotiation_state_ = NEGOTIATED_VERSION; 345 version_negotiation_state_ = NEGOTIATED_VERSION;
365 visitor_->OnSuccessfulVersionNegotiation(received_version); 346 visitor_->OnSuccessfulVersionNegotiation(received_version);
347 if (debug_visitor_.get() != NULL) {
348 debug_visitor_->OnSuccessfulVersionNegotiation(received_version);
349 }
366 DVLOG(1) << ENDPOINT << "version negotiated " << received_version; 350 DVLOG(1) << ENDPOINT << "version negotiated " << received_version;
367 351
368 // Store the new version. 352 // Store the new version.
369 framer_.set_version(received_version); 353 framer_.set_version(received_version);
370 354
371 // TODO(satyamshekhar): Store the sequence number of this packet and close the 355 // TODO(satyamshekhar): Store the sequence number of this packet and close the
372 // connection if we ever received a packet with incorrect version and whose 356 // connection if we ever received a packet with incorrect version and whose
373 // sequence number is greater. 357 // sequence number is greater.
374 return true; 358 return true;
375 } 359 }
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 << " without version flag before version negotiated."; 466 << " without version flag before version negotiated.";
483 // Packets should have the version flag till version negotiation is 467 // Packets should have the version flag till version negotiation is
484 // done. 468 // done.
485 CloseConnection(QUIC_INVALID_VERSION, false); 469 CloseConnection(QUIC_INVALID_VERSION, false);
486 return false; 470 return false;
487 } else { 471 } else {
488 DCHECK_EQ(1u, header.public_header.versions.size()); 472 DCHECK_EQ(1u, header.public_header.versions.size());
489 DCHECK_EQ(header.public_header.versions[0], version()); 473 DCHECK_EQ(header.public_header.versions[0], version());
490 version_negotiation_state_ = NEGOTIATED_VERSION; 474 version_negotiation_state_ = NEGOTIATED_VERSION;
491 visitor_->OnSuccessfulVersionNegotiation(version()); 475 visitor_->OnSuccessfulVersionNegotiation(version());
476 if (debug_visitor_.get() != NULL) {
477 debug_visitor_->OnSuccessfulVersionNegotiation(version());
478 }
492 } 479 }
493 } else { 480 } else {
494 DCHECK(!header.public_header.version_flag); 481 DCHECK(!header.public_header.version_flag);
495 // If the client gets a packet without the version flag from the server 482 // If the client gets a packet without the version flag from the server
496 // it should stop sending version since the version negotiation is done. 483 // it should stop sending version since the version negotiation is done.
497 packet_generator_.StopSendingVersion(); 484 packet_generator_.StopSendingVersion();
498 version_negotiation_state_ = NEGOTIATED_VERSION; 485 version_negotiation_state_ = NEGOTIATED_VERSION;
499 visitor_->OnSuccessfulVersionNegotiation(version()); 486 visitor_->OnSuccessfulVersionNegotiation(version());
487 if (debug_visitor_.get() != NULL) {
488 debug_visitor_->OnSuccessfulVersionNegotiation(version());
489 }
500 } 490 }
501 } 491 }
502 492
503 DCHECK_EQ(NEGOTIATED_VERSION, version_negotiation_state_); 493 DCHECK_EQ(NEGOTIATED_VERSION, version_negotiation_state_);
504 494
505 --stats_.packets_dropped; 495 --stats_.packets_dropped;
506 DVLOG(1) << ENDPOINT << "Received packet header: " << header; 496 DVLOG(1) << ENDPOINT << "Received packet header: " << header;
507 last_header_ = header; 497 last_header_ = header;
508 DCHECK(connected_); 498 DCHECK(connected_);
509 return true; 499 return true;
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 600
611 last_stop_waiting_frames_.push_back(frame); 601 last_stop_waiting_frames_.push_back(frame);
612 return connected_; 602 return connected_;
613 } 603 }
614 604
615 bool QuicConnection::OnPingFrame(const QuicPingFrame& frame) { 605 bool QuicConnection::OnPingFrame(const QuicPingFrame& frame) {
616 DCHECK(connected_); 606 DCHECK(connected_);
617 if (debug_visitor_.get() != NULL) { 607 if (debug_visitor_.get() != NULL) {
618 debug_visitor_->OnPingFrame(frame); 608 debug_visitor_->OnPingFrame(frame);
619 } 609 }
610 last_ping_frames_.push_back(frame);
620 return true; 611 return true;
621 } 612 }
622 613
623 bool QuicConnection::ValidateAckFrame(const QuicAckFrame& incoming_ack) { 614 bool QuicConnection::ValidateAckFrame(const QuicAckFrame& incoming_ack) {
624 if (incoming_ack.largest_observed > packet_generator_.sequence_number()) { 615 if (incoming_ack.largest_observed > packet_generator_.sequence_number()) {
625 DLOG(ERROR) << ENDPOINT << "Peer's observed unsent packet:" 616 DLOG(ERROR) << ENDPOINT << "Peer's observed unsent packet:"
626 << incoming_ack.largest_observed << " vs " 617 << incoming_ack.largest_observed << " vs "
627 << packet_generator_.sequence_number(); 618 << packet_generator_.sequence_number();
628 // We got an error for data we have not sent. Error out. 619 // We got an error for data we have not sent. Error out.
629 return false; 620 return false;
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
771 762
772 void QuicConnection::OnPacketComplete() { 763 void QuicConnection::OnPacketComplete() {
773 // Don't do anything if this packet closed the connection. 764 // Don't do anything if this packet closed the connection.
774 if (!connected_) { 765 if (!connected_) {
775 ClearLastFrames(); 766 ClearLastFrames();
776 return; 767 return;
777 } 768 }
778 769
779 DVLOG(1) << ENDPOINT << (last_packet_revived_ ? "Revived" : "Got") 770 DVLOG(1) << ENDPOINT << (last_packet_revived_ ? "Revived" : "Got")
780 << " packet " << last_header_.packet_sequence_number 771 << " packet " << last_header_.packet_sequence_number
781 << " with " << last_ack_frames_.size() << " acks, " 772 << " with " << last_stream_frames_.size()<< " stream frames "
773 << last_ack_frames_.size() << " acks, "
782 << last_congestion_frames_.size() << " congestions, " 774 << last_congestion_frames_.size() << " congestions, "
783 << last_stop_waiting_frames_.size() << " stop_waiting, " 775 << last_stop_waiting_frames_.size() << " stop_waiting, "
776 << last_rst_frames_.size() << " rsts, "
784 << last_goaway_frames_.size() << " goaways, " 777 << last_goaway_frames_.size() << " goaways, "
785 << last_window_update_frames_.size() << " window updates, " 778 << last_window_update_frames_.size() << " window updates, "
786 << last_blocked_frames_.size() << " blocked, " 779 << last_blocked_frames_.size() << " blocked, "
787 << last_rst_frames_.size() << " rsts, " 780 << last_ping_frames_.size() << " pings, "
788 << last_close_frames_.size() << " closes, " 781 << last_close_frames_.size() << " closes, "
789 << last_stream_frames_.size() 782 << "for " << last_header_.public_header.connection_id;
790 << " stream frames for "
791 << last_header_.public_header.connection_id;
792 783
793 // Call MaybeQueueAck() before recording the received packet, since we want 784 // Call MaybeQueueAck() before recording the received packet, since we want
794 // to trigger an ack if the newly received packet was previously missing. 785 // to trigger an ack if the newly received packet was previously missing.
795 MaybeQueueAck(); 786 MaybeQueueAck();
796 787
797 // Record received or revived packet to populate ack info correctly before 788 // Record received or revived packet to populate ack info correctly before
798 // processing stream frames, since the processing may result in a response 789 // processing stream frames, since the processing may result in a response
799 // packet with a bundled ack. 790 // packet with a bundled ack.
800 if (last_packet_revived_) { 791 if (last_packet_revived_) {
801 received_packet_manager_.RecordPacketRevived( 792 received_packet_manager_.RecordPacketRevived(
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
874 } 865 }
875 } 866 }
876 867
877 if (ack_queued_) { 868 if (ack_queued_) {
878 ack_alarm_->Cancel(); 869 ack_alarm_->Cancel();
879 } 870 }
880 } 871 }
881 872
882 void QuicConnection::ClearLastFrames() { 873 void QuicConnection::ClearLastFrames() {
883 last_stream_frames_.clear(); 874 last_stream_frames_.clear();
875 last_ack_frames_.clear();
876 last_congestion_frames_.clear();
877 last_stop_waiting_frames_.clear();
878 last_rst_frames_.clear();
884 last_goaway_frames_.clear(); 879 last_goaway_frames_.clear();
885 last_window_update_frames_.clear(); 880 last_window_update_frames_.clear();
886 last_blocked_frames_.clear(); 881 last_blocked_frames_.clear();
887 last_rst_frames_.clear(); 882 last_ping_frames_.clear();
888 last_ack_frames_.clear(); 883 last_close_frames_.clear();
889 last_stop_waiting_frames_.clear();
890 last_congestion_frames_.clear();
891 } 884 }
892 885
893 QuicAckFrame* QuicConnection::CreateAckFrame() { 886 QuicAckFrame* QuicConnection::CreateAckFrame() {
894 QuicAckFrame* outgoing_ack = new QuicAckFrame(); 887 QuicAckFrame* outgoing_ack = new QuicAckFrame();
895 received_packet_manager_.UpdateReceivedPacketInfo( 888 received_packet_manager_.UpdateReceivedPacketInfo(
896 outgoing_ack, clock_->ApproximateNow()); 889 outgoing_ack, clock_->ApproximateNow());
897 DVLOG(1) << ENDPOINT << "Creating ack frame: " << *outgoing_ack; 890 DVLOG(1) << ENDPOINT << "Creating ack frame: " << *outgoing_ack;
898 return outgoing_ack; 891 return outgoing_ack;
899 } 892 }
900 893
901 QuicCongestionFeedbackFrame* QuicConnection::CreateFeedbackFrame() { 894 QuicCongestionFeedbackFrame* QuicConnection::CreateFeedbackFrame() {
902 return new QuicCongestionFeedbackFrame(outgoing_congestion_feedback_); 895 return new QuicCongestionFeedbackFrame(outgoing_congestion_feedback_);
903 } 896 }
904 897
905 QuicStopWaitingFrame* QuicConnection::CreateStopWaitingFrame() { 898 QuicStopWaitingFrame* QuicConnection::CreateStopWaitingFrame() {
906 QuicStopWaitingFrame stop_waiting; 899 QuicStopWaitingFrame stop_waiting;
907 UpdateStopWaiting(&stop_waiting); 900 UpdateStopWaiting(&stop_waiting);
908 return new QuicStopWaitingFrame(stop_waiting); 901 return new QuicStopWaitingFrame(stop_waiting);
909 } 902 }
910 903
911 bool QuicConnection::ShouldLastPacketInstigateAck() const { 904 bool QuicConnection::ShouldLastPacketInstigateAck() const {
912 if (!last_stream_frames_.empty() || 905 if (!last_stream_frames_.empty() ||
913 !last_goaway_frames_.empty() || 906 !last_goaway_frames_.empty() ||
914 !last_rst_frames_.empty() || 907 !last_rst_frames_.empty() ||
915 !last_window_update_frames_.empty() || 908 !last_window_update_frames_.empty() ||
916 !last_blocked_frames_.empty()) { 909 !last_blocked_frames_.empty() ||
910 !last_ping_frames_.empty()) {
917 return true; 911 return true;
918 } 912 }
919 913
920 if (!last_ack_frames_.empty() && last_ack_frames_.back().is_truncated) { 914 if (!last_ack_frames_.empty() && last_ack_frames_.back().is_truncated) {
921 return true; 915 return true;
922 } 916 }
923 return false; 917 return false;
924 } 918 }
925 919
926 void QuicConnection::UpdateStopWaitingCount() { 920 void QuicConnection::UpdateStopWaitingCount() {
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
1205 } 1199 }
1206 1200
1207 void QuicConnection::WriteQueuedPackets() { 1201 void QuicConnection::WriteQueuedPackets() {
1208 DCHECK(!writer_->IsWriteBlocked()); 1202 DCHECK(!writer_->IsWriteBlocked());
1209 1203
1210 if (pending_version_negotiation_packet_) { 1204 if (pending_version_negotiation_packet_) {
1211 SendVersionNegotiationPacket(); 1205 SendVersionNegotiationPacket();
1212 } 1206 }
1213 1207
1214 QueuedPacketList::iterator packet_iterator = queued_packets_.begin(); 1208 QueuedPacketList::iterator packet_iterator = queued_packets_.begin();
1215 while (!writer_->IsWriteBlocked() && 1209 while (packet_iterator != queued_packets_.end() &&
1216 packet_iterator != queued_packets_.end()) { 1210 WritePacket(*packet_iterator)) {
1217 if (WritePacket(*packet_iterator)) { 1211 delete packet_iterator->serialized_packet.packet;
1218 delete packet_iterator->packet; 1212 packet_iterator = queued_packets_.erase(packet_iterator);
1219 packet_iterator = queued_packets_.erase(packet_iterator);
1220 } else {
1221 // Continue, because some queued packets may still be writable.
1222 // This can happen if a retransmit send fails.
1223 ++packet_iterator;
1224 }
1225 } 1213 }
1226 } 1214 }
1227 1215
1228 void QuicConnection::WritePendingRetransmissions() { 1216 void QuicConnection::WritePendingRetransmissions() {
1229 // Keep writing as long as there's a pending retransmission which can be 1217 // Keep writing as long as there's a pending retransmission which can be
1230 // written. 1218 // written.
1231 while (sent_packet_manager_.HasPendingRetransmissions()) { 1219 while (sent_packet_manager_.HasPendingRetransmissions()) {
1232 const QuicSentPacketManager::PendingRetransmission pending = 1220 const QuicSentPacketManager::PendingRetransmission pending =
1233 sent_packet_manager_.NextPendingRetransmission(); 1221 sent_packet_manager_.NextPendingRetransmission();
1234 if (GetPacketType(&pending.retransmittable_frames) == NORMAL && 1222 if (!CanWrite(HAS_RETRANSMITTABLE_DATA)) {
1235 !CanWrite(HAS_RETRANSMITTABLE_DATA)) {
1236 break; 1223 break;
1237 } 1224 }
1238 1225
1239 // Re-packetize the frames with a new sequence number for retransmission. 1226 // Re-packetize the frames with a new sequence number for retransmission.
1240 // Retransmitted data packets do not use FEC, even when it's enabled. 1227 // Retransmitted data packets do not use FEC, even when it's enabled.
1241 // Retransmitted packets use the same sequence number length as the 1228 // Retransmitted packets use the same sequence number length as the
1242 // original. 1229 // original.
1243 // Flush the packet generator before making a new packet. 1230 // Flush the packet generator before making a new packet.
1244 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that 1231 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that
1245 // does not require the creator to be flushed. 1232 // does not require the creator to be flushed.
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1309 // If the scheduler requires a delay, then we can not send this packet now. 1296 // If the scheduler requires a delay, then we can not send this packet now.
1310 if (!delay.IsZero()) { 1297 if (!delay.IsZero()) {
1311 send_alarm_->Update(now.Add(delay), QuicTime::Delta::FromMilliseconds(1)); 1298 send_alarm_->Update(now.Add(delay), QuicTime::Delta::FromMilliseconds(1));
1312 DVLOG(1) << "Delaying sending."; 1299 DVLOG(1) << "Delaying sending.";
1313 return false; 1300 return false;
1314 } 1301 }
1315 send_alarm_->Cancel(); 1302 send_alarm_->Cancel();
1316 return true; 1303 return true;
1317 } 1304 }
1318 1305
1319 bool QuicConnection::WritePacket(QueuedPacket packet) { 1306 bool QuicConnection::WritePacket(const QueuedPacket& packet) {
1320 QuicPacketSequenceNumber sequence_number = packet.sequence_number; 1307 QuicPacketSequenceNumber sequence_number =
1308 packet.serialized_packet.sequence_number;
1321 if (ShouldDiscardPacket(packet.encryption_level, 1309 if (ShouldDiscardPacket(packet.encryption_level,
1322 sequence_number, 1310 sequence_number,
1323 packet.retransmittable)) { 1311 IsRetransmittable(packet))) {
1324 ++stats_.packets_discarded; 1312 ++stats_.packets_discarded;
1325 return true; 1313 return true;
1326 } 1314 }
1327 1315 // Connection close packets are encypted and saved, so don't exit early.
1328 // If the packet is CONNECTION_CLOSE, we need to try to send it immediately 1316 if (writer_->IsWriteBlocked() && !IsConnectionClose(packet)) {
1329 // and encrypt it to hand it off to TimeWaitListManager.
1330 // If the packet is QUEUED, we don't re-consult the congestion control.
1331 // This ensures packets are sent in sequence number order.
1332 // TODO(ianswett): The congestion control should have been consulted before
1333 // serializing the packet, so this could be turned into a LOG_IF(DFATAL).
1334 if (packet.type == NORMAL && !CanWrite(packet.retransmittable)) {
1335 return false; 1317 return false;
1336 } 1318 }
1337 1319
1338 // Some encryption algorithms require the packet sequence numbers not be 1320 // Some encryption algorithms require the packet sequence numbers not be
1339 // repeated. 1321 // repeated.
1340 DCHECK_LE(sequence_number_of_last_sent_packet_, sequence_number); 1322 DCHECK_LE(sequence_number_of_last_sent_packet_, sequence_number);
1341 sequence_number_of_last_sent_packet_ = sequence_number; 1323 sequence_number_of_last_sent_packet_ = sequence_number;
1342 1324
1343 QuicEncryptedPacket* encrypted = framer_.EncryptPacket( 1325 QuicEncryptedPacket* encrypted = framer_.EncryptPacket(
1344 packet.encryption_level, sequence_number, *packet.packet); 1326 packet.encryption_level,
1327 sequence_number,
1328 *packet.serialized_packet.packet);
1345 if (encrypted == NULL) { 1329 if (encrypted == NULL) {
1346 LOG(DFATAL) << ENDPOINT << "Failed to encrypt packet number " 1330 LOG(DFATAL) << ENDPOINT << "Failed to encrypt packet number "
1347 << sequence_number; 1331 << sequence_number;
1348 // CloseConnection does not send close packet, so no infinite loop here. 1332 // CloseConnection does not send close packet, so no infinite loop here.
1349 CloseConnection(QUIC_ENCRYPTION_FAILURE, false); 1333 CloseConnection(QUIC_ENCRYPTION_FAILURE, false);
1350 return false; 1334 return false;
1351 } 1335 }
1352 1336
1353 // Connection close packets are eventually owned by TimeWaitListManager. 1337 // Connection close packets are eventually owned by TimeWaitListManager.
1354 // Others are deleted at the end of this call. 1338 // Others are deleted at the end of this call.
1355 scoped_ptr<QuicEncryptedPacket> encrypted_deleter; 1339 scoped_ptr<QuicEncryptedPacket> encrypted_deleter;
1356 if (packet.type == CONNECTION_CLOSE) { 1340 if (IsConnectionClose(packet)) {
1357 DCHECK(connection_close_packet_.get() == NULL); 1341 DCHECK(connection_close_packet_.get() == NULL);
1358 connection_close_packet_.reset(encrypted); 1342 connection_close_packet_.reset(encrypted);
1359 // This assures we won't try to write *forced* packets when blocked. 1343 // This assures we won't try to write *forced* packets when blocked.
1360 // Return true to stop processing. 1344 // Return true to stop processing.
1361 if (writer_->IsWriteBlocked()) { 1345 if (writer_->IsWriteBlocked()) {
1362 visitor_->OnWriteBlocked(); 1346 visitor_->OnWriteBlocked();
1363 return true; 1347 return true;
1364 } 1348 }
1365 } else { 1349 } else {
1366 encrypted_deleter.reset(encrypted); 1350 encrypted_deleter.reset(encrypted);
1367 } 1351 }
1368 1352
1369 if (!FLAGS_quic_allow_oversized_packets_for_test) { 1353 if (!FLAGS_quic_allow_oversized_packets_for_test) {
1370 DCHECK_LE(encrypted->length(), kMaxPacketSize); 1354 DCHECK_LE(encrypted->length(), kMaxPacketSize);
1371 } 1355 }
1372 DCHECK_LE(encrypted->length(), packet_generator_.max_packet_length()); 1356 DCHECK_LE(encrypted->length(), packet_generator_.max_packet_length());
1373 DVLOG(1) << ENDPOINT << "Sending packet " << sequence_number 1357 DVLOG(1) << ENDPOINT << "Sending packet " << sequence_number << " : "
1374 << " : " << (packet.packet->is_fec_packet() ? "FEC " : 1358 << (packet.serialized_packet.packet->is_fec_packet() ? "FEC " :
1375 (packet.retransmittable == HAS_RETRANSMITTABLE_DATA 1359 (IsRetransmittable(packet) == HAS_RETRANSMITTABLE_DATA
1376 ? "data bearing " : " ack only ")) 1360 ? "data bearing " : " ack only "))
1377 << ", encryption level: " 1361 << ", encryption level: "
1378 << QuicUtils::EncryptionLevelToString(packet.encryption_level) 1362 << QuicUtils::EncryptionLevelToString(packet.encryption_level)
1379 << ", length:" << packet.packet->length() << ", encrypted length:" 1363 << ", length:"
1364 << packet.serialized_packet.packet->length()
1365 << ", encrypted length:"
1380 << encrypted->length(); 1366 << encrypted->length();
1381 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl 1367 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl
1382 << QuicUtils::StringToHexASCIIDump(packet.packet->AsStringPiece()); 1368 << QuicUtils::StringToHexASCIIDump(
1369 packet.serialized_packet.packet->AsStringPiece());
1383 1370
1384 WriteResult result = writer_->WritePacket(encrypted->data(), 1371 WriteResult result = writer_->WritePacket(encrypted->data(),
1385 encrypted->length(), 1372 encrypted->length(),
1386 self_address().address(), 1373 self_address().address(),
1387 peer_address()); 1374 peer_address());
1388 if (result.error_code == ERR_IO_PENDING) { 1375 if (result.error_code == ERR_IO_PENDING) {
1389 DCHECK_EQ(WRITE_STATUS_BLOCKED, result.status); 1376 DCHECK_EQ(WRITE_STATUS_BLOCKED, result.status);
1390 } 1377 }
1391 if (debug_visitor_.get() != NULL) { 1378 if (debug_visitor_.get() != NULL) {
1392 // Pass the write result to the visitor. 1379 // Pass the write result to the visitor.
(...skipping 22 matching lines...) Expand all
1415 DVLOG(1) << ENDPOINT << "time of last sent packet: " 1402 DVLOG(1) << ENDPOINT << "time of last sent packet: "
1416 << now.ToDebuggingValue(); 1403 << now.ToDebuggingValue();
1417 1404
1418 // TODO(ianswett): Change the sequence number length and other packet creator 1405 // TODO(ianswett): Change the sequence number length and other packet creator
1419 // options by a more explicit API than setting a struct value directly, 1406 // options by a more explicit API than setting a struct value directly,
1420 // perhaps via the NetworkChangeVisitor. 1407 // perhaps via the NetworkChangeVisitor.
1421 packet_generator_.UpdateSequenceNumberLength( 1408 packet_generator_.UpdateSequenceNumberLength(
1422 sent_packet_manager_.least_packet_awaited_by_peer(), 1409 sent_packet_manager_.least_packet_awaited_by_peer(),
1423 sent_packet_manager_.GetCongestionWindow()); 1410 sent_packet_manager_.GetCongestionWindow());
1424 1411
1425 bool reset_retransmission_alarm = 1412 bool reset_retransmission_alarm = sent_packet_manager_.OnPacketSent(
1426 sent_packet_manager_.OnPacketSent(sequence_number, 1413 sequence_number,
1427 now, 1414 now,
1428 encrypted->length(), 1415 encrypted->length(),
1429 packet.transmission_type, 1416 packet.transmission_type,
1430 packet.retransmittable); 1417 IsRetransmittable(packet));
1431 1418
1432 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) { 1419 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) {
1433 retransmission_alarm_->Update(sent_packet_manager_.GetRetransmissionTime(), 1420 retransmission_alarm_->Update(sent_packet_manager_.GetRetransmissionTime(),
1434 QuicTime::Delta::FromMilliseconds(1)); 1421 QuicTime::Delta::FromMilliseconds(1));
1435 } 1422 }
1436 1423
1437 stats_.bytes_sent += result.bytes_written; 1424 stats_.bytes_sent += result.bytes_written;
1438 ++stats_.packets_sent; 1425 ++stats_.packets_sent;
1439 if (packet.transmission_type != NOT_RETRANSMISSION) { 1426 if (packet.transmission_type != NOT_RETRANSMISSION) {
1440 stats_.bytes_retransmitted += result.bytes_written; 1427 stats_.bytes_retransmitted += result.bytes_written;
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1516 visitor_->OnCongestionWindowChange(clock_->ApproximateNow()); 1503 visitor_->OnCongestionWindowChange(clock_->ApproximateNow());
1517 } 1504 }
1518 1505
1519 void QuicConnection::OnHandshakeComplete() { 1506 void QuicConnection::OnHandshakeComplete() {
1520 sent_packet_manager_.SetHandshakeConfirmed(); 1507 sent_packet_manager_.SetHandshakeConfirmed();
1521 } 1508 }
1522 1509
1523 bool QuicConnection::SendOrQueuePacket(EncryptionLevel level, 1510 bool QuicConnection::SendOrQueuePacket(EncryptionLevel level,
1524 const SerializedPacket& packet, 1511 const SerializedPacket& packet,
1525 TransmissionType transmission_type) { 1512 TransmissionType transmission_type) {
1513 // The caller of this function is responsible for checking CanWrite().
1526 if (packet.packet == NULL) { 1514 if (packet.packet == NULL) {
1527 LOG(DFATAL) << "NULL packet passed in to SendOrQueuePacket"; 1515 LOG(DFATAL) << "NULL packet passed in to SendOrQueuePacket";
1528 return true; 1516 return true;
1529 } 1517 }
1530 1518
1531 sent_entropy_manager_.RecordPacketEntropyHash(packet.sequence_number, 1519 sent_entropy_manager_.RecordPacketEntropyHash(packet.sequence_number,
1532 packet.entropy_hash); 1520 packet.entropy_hash);
1533 QueuedPacket queued_packet(packet, level, transmission_type); 1521 QueuedPacket queued_packet(packet, level, transmission_type);
1534 // If there are already queued packets, put this at the end, 1522 LOG_IF(DFATAL, !queued_packets_.empty() && !writer_->IsWriteBlocked())
1535 // unless it's ConnectionClose, in which case it is written immediately. 1523 << "Packets should only be left queued if we're write blocked.";
1536 if ((queued_packet.type == CONNECTION_CLOSE || queued_packets_.empty()) && 1524 if (WritePacket(queued_packet)) {
1537 WritePacket(queued_packet)) {
1538 delete packet.packet; 1525 delete packet.packet;
1539 return true; 1526 return true;
1540 } 1527 }
1541 queued_packet.type = QUEUED;
1542 queued_packets_.push_back(queued_packet); 1528 queued_packets_.push_back(queued_packet);
1543 return false; 1529 return false;
1544 } 1530 }
1545 1531
1546 void QuicConnection::UpdateStopWaiting(QuicStopWaitingFrame* stop_waiting) { 1532 void QuicConnection::UpdateStopWaiting(QuicStopWaitingFrame* stop_waiting) {
1547 stop_waiting->least_unacked = GetLeastUnacked(); 1533 stop_waiting->least_unacked = GetLeastUnacked();
1548 stop_waiting->entropy_hash = sent_entropy_manager_.GetCumulativeEntropy( 1534 stop_waiting->entropy_hash = sent_entropy_manager_.GetCumulativeEntropy(
1549 stop_waiting->least_unacked - 1); 1535 stop_waiting->least_unacked - 1);
1550 } 1536 }
1551 1537
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after
1984 } 1970 }
1985 // If we changed the generator's batch state, restore original batch state. 1971 // If we changed the generator's batch state, restore original batch state.
1986 if (!already_in_batch_mode_) { 1972 if (!already_in_batch_mode_) {
1987 DVLOG(1) << "Leaving Batch Mode."; 1973 DVLOG(1) << "Leaving Batch Mode.";
1988 connection_->packet_generator_.FinishBatchOperations(); 1974 connection_->packet_generator_.FinishBatchOperations();
1989 } 1975 }
1990 DCHECK_EQ(already_in_batch_mode_, 1976 DCHECK_EQ(already_in_batch_mode_,
1991 connection_->packet_generator_.InBatchMode()); 1977 connection_->packet_generator_.InBatchMode());
1992 } 1978 }
1993 1979
1980 HasRetransmittableData QuicConnection::IsRetransmittable(
1981 QueuedPacket packet) {
1982 // TODO(cyr): Understand why the first check here is necessary. Without it,
1983 // DiscardRetransmit test fails.
1984 if (packet.transmission_type != NOT_RETRANSMISSION ||
1985 packet.serialized_packet.retransmittable_frames != NULL) {
1986 return HAS_RETRANSMITTABLE_DATA;
1987 } else {
1988 return NO_RETRANSMITTABLE_DATA;
1989 }
1990 }
1991
1992 bool QuicConnection::IsConnectionClose(
1993 QueuedPacket packet) {
1994 RetransmittableFrames* retransmittable_frames =
1995 packet.serialized_packet.retransmittable_frames;
1996 if (!retransmittable_frames) {
1997 return false;
1998 }
1999 for (size_t i = 0; i < retransmittable_frames->frames().size(); ++i) {
2000 if (retransmittable_frames->frames()[i].type == CONNECTION_CLOSE_FRAME) {
2001 return true;
2002 }
2003 }
2004 return false;
2005 }
2006
1994 } // namespace net 2007 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/quic_connection.h ('k') | net/quic/quic_connection_logger.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698