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/core/quic_connection.h" | 5 #include "net/quic/core/quic_connection.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 #include <sys/types.h> | 8 #include <sys/types.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 ping_alarm_( | 249 ping_alarm_( |
250 alarm_factory_->CreateAlarm(arena_.New<PingAlarmDelegate>(this), | 250 alarm_factory_->CreateAlarm(arena_.New<PingAlarmDelegate>(this), |
251 &arena_)), | 251 &arena_)), |
252 mtu_discovery_alarm_(alarm_factory_->CreateAlarm( | 252 mtu_discovery_alarm_(alarm_factory_->CreateAlarm( |
253 arena_.New<MtuDiscoveryAlarmDelegate>(this), | 253 arena_.New<MtuDiscoveryAlarmDelegate>(this), |
254 &arena_)), | 254 &arena_)), |
255 visitor_(nullptr), | 255 visitor_(nullptr), |
256 debug_visitor_(nullptr), | 256 debug_visitor_(nullptr), |
257 packet_generator_(connection_id_, | 257 packet_generator_(connection_id_, |
258 &framer_, | 258 &framer_, |
259 random_generator_, | |
260 helper->GetBufferAllocator(), | 259 helper->GetBufferAllocator(), |
261 this), | 260 this), |
262 idle_network_timeout_(QuicTime::Delta::Infinite()), | 261 idle_network_timeout_(QuicTime::Delta::Infinite()), |
263 handshake_timeout_(QuicTime::Delta::Infinite()), | 262 handshake_timeout_(QuicTime::Delta::Infinite()), |
264 time_of_last_received_packet_(clock_->ApproximateNow()), | 263 time_of_last_received_packet_(clock_->ApproximateNow()), |
265 time_of_last_sent_new_packet_(clock_->ApproximateNow()), | 264 time_of_last_sent_new_packet_(clock_->ApproximateNow()), |
266 last_send_for_timeout_(clock_->ApproximateNow()), | 265 last_send_for_timeout_(clock_->ApproximateNow()), |
267 packet_number_of_last_sent_packet_(0), | 266 packet_number_of_last_sent_packet_(0), |
268 sent_packet_manager_(new QuicSentPacketManager( | 267 sent_packet_manager_(new QuicSentPacketManager( |
269 perspective, | 268 perspective, |
(...skipping 12 matching lines...) Expand all Loading... |
282 packets_between_mtu_probes_(kPacketsBetweenMtuProbesBase), | 281 packets_between_mtu_probes_(kPacketsBetweenMtuProbesBase), |
283 next_mtu_probe_at_(kPacketsBetweenMtuProbesBase), | 282 next_mtu_probe_at_(kPacketsBetweenMtuProbesBase), |
284 largest_received_packet_size_(0), | 283 largest_received_packet_size_(0), |
285 goaway_sent_(false), | 284 goaway_sent_(false), |
286 goaway_received_(false), | 285 goaway_received_(false), |
287 multipath_enabled_(false), | 286 multipath_enabled_(false), |
288 write_error_occured_(false) { | 287 write_error_occured_(false) { |
289 DVLOG(1) << ENDPOINT | 288 DVLOG(1) << ENDPOINT |
290 << "Created connection with connection_id: " << connection_id; | 289 << "Created connection with connection_id: " << connection_id; |
291 framer_.set_visitor(this); | 290 framer_.set_visitor(this); |
292 framer_.set_received_entropy_calculator(&received_packet_manager_); | |
293 if (!FLAGS_quic_receive_packet_once_decrypted) { | 291 if (!FLAGS_quic_receive_packet_once_decrypted) { |
294 last_stop_waiting_frame_.least_unacked = 0; | 292 last_stop_waiting_frame_.least_unacked = 0; |
295 } | 293 } |
296 stats_.connection_creation_time = clock_->ApproximateNow(); | 294 stats_.connection_creation_time = clock_->ApproximateNow(); |
297 if (FLAGS_quic_enable_multipath) { | 295 if (FLAGS_quic_enable_multipath) { |
298 sent_packet_manager_.reset(new QuicMultipathSentPacketManager( | 296 sent_packet_manager_.reset(new QuicMultipathSentPacketManager( |
299 sent_packet_manager_.release(), this)); | 297 sent_packet_manager_.release(), this)); |
300 } | 298 } |
301 // TODO(ianswett): Supply the NetworkChangeVisitor as a constructor argument | 299 // TODO(ianswett): Supply the NetworkChangeVisitor as a constructor argument |
302 // and make it required non-null, because it's always used. | 300 // and make it required non-null, because it's always used. |
303 sent_packet_manager_->SetNetworkChangeVisitor(this); | 301 sent_packet_manager_->SetNetworkChangeVisitor(this); |
304 // Allow the packet writer to potentially reduce the packet size to a value | 302 // Allow the packet writer to potentially reduce the packet size to a value |
305 // even smaller than kDefaultMaxPacketSize. | 303 // even smaller than kDefaultMaxPacketSize. |
306 SetMaxPacketLength(perspective_ == Perspective::IS_SERVER | 304 SetMaxPacketLength(perspective_ == Perspective::IS_SERVER |
307 ? kDefaultServerMaxPacketSize | 305 ? kDefaultServerMaxPacketSize |
308 : kDefaultMaxPacketSize); | 306 : kDefaultMaxPacketSize); |
309 received_packet_manager_.SetVersion(version()); | |
310 } | 307 } |
311 | 308 |
312 QuicConnection::~QuicConnection() { | 309 QuicConnection::~QuicConnection() { |
313 if (owns_writer_) { | 310 if (owns_writer_) { |
314 delete writer_; | 311 delete writer_; |
315 } | 312 } |
316 ClearQueuedPackets(); | 313 ClearQueuedPackets(); |
317 } | 314 } |
318 | 315 |
319 void QuicConnection::ClearQueuedPackets() { | 316 void QuicConnection::ClearQueuedPackets() { |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 case NEGOTIATED_VERSION: | 490 case NEGOTIATED_VERSION: |
494 // Might be old packets that were sent by the client before the version | 491 // Might be old packets that were sent by the client before the version |
495 // was negotiated. Drop these. | 492 // was negotiated. Drop these. |
496 return false; | 493 return false; |
497 | 494 |
498 default: | 495 default: |
499 DCHECK(false); | 496 DCHECK(false); |
500 } | 497 } |
501 | 498 |
502 version_negotiation_state_ = NEGOTIATED_VERSION; | 499 version_negotiation_state_ = NEGOTIATED_VERSION; |
503 received_packet_manager_.SetVersion(received_version); | |
504 visitor_->OnSuccessfulVersionNegotiation(received_version); | 500 visitor_->OnSuccessfulVersionNegotiation(received_version); |
505 if (debug_visitor_ != nullptr) { | 501 if (debug_visitor_ != nullptr) { |
506 debug_visitor_->OnSuccessfulVersionNegotiation(received_version); | 502 debug_visitor_->OnSuccessfulVersionNegotiation(received_version); |
507 } | 503 } |
508 DVLOG(1) << ENDPOINT << "version negotiated " << received_version; | 504 DVLOG(1) << ENDPOINT << "version negotiated " << received_version; |
509 | 505 |
510 // Store the new version. | 506 // Store the new version. |
511 framer_.set_version(received_version); | 507 framer_.set_version(received_version); |
512 | 508 |
513 // TODO(satyamshekhar): Store the packet number of this packet and close the | 509 // TODO(satyamshekhar): Store the packet number of this packet and close the |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 "No common version found. Supported versions: {" + | 552 "No common version found. Supported versions: {" + |
557 QuicVersionVectorToString(framer_.supported_versions()) + | 553 QuicVersionVectorToString(framer_.supported_versions()) + |
558 "}, peer supported versions: {" + | 554 "}, peer supported versions: {" + |
559 QuicVersionVectorToString(packet.versions) + "}", | 555 QuicVersionVectorToString(packet.versions) + "}", |
560 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); | 556 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
561 return; | 557 return; |
562 } | 558 } |
563 | 559 |
564 DVLOG(1) << ENDPOINT | 560 DVLOG(1) << ENDPOINT |
565 << "Negotiated version: " << QuicVersionToString(version()); | 561 << "Negotiated version: " << QuicVersionToString(version()); |
566 received_packet_manager_.SetVersion(version()); | |
567 server_supported_versions_ = packet.versions; | 562 server_supported_versions_ = packet.versions; |
568 version_negotiation_state_ = NEGOTIATION_IN_PROGRESS; | 563 version_negotiation_state_ = NEGOTIATION_IN_PROGRESS; |
569 RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION); | 564 RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION); |
570 } | 565 } |
571 | 566 |
572 bool QuicConnection::OnUnauthenticatedPublicHeader( | 567 bool QuicConnection::OnUnauthenticatedPublicHeader( |
573 const QuicPacketPublicHeader& header) { | 568 const QuicPacketPublicHeader& header) { |
574 if (header.connection_id == connection_id_) { | 569 if (header.connection_id == connection_id_) { |
575 return true; | 570 return true; |
576 } | 571 } |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
725 if (error != nullptr) { | 720 if (error != nullptr) { |
726 CloseConnection(QUIC_INVALID_ACK_DATA, error, | 721 CloseConnection(QUIC_INVALID_ACK_DATA, error, |
727 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); | 722 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
728 return false; | 723 return false; |
729 } | 724 } |
730 | 725 |
731 if (send_alarm_->IsSet()) { | 726 if (send_alarm_->IsSet()) { |
732 send_alarm_->Cancel(); | 727 send_alarm_->Cancel(); |
733 } | 728 } |
734 ProcessAckFrame(incoming_ack); | 729 ProcessAckFrame(incoming_ack); |
735 if (incoming_ack.is_truncated) { | |
736 should_last_packet_instigate_acks_ = true; | |
737 } | |
738 // If the incoming ack's packets set expresses missing packets: peer is still | 730 // If the incoming ack's packets set expresses missing packets: peer is still |
739 // waiting for a packet lower than a packet that we are no longer planning to | 731 // waiting for a packet lower than a packet that we are no longer planning to |
740 // send. | 732 // send. |
741 // If the incoming ack's packets set expresses received packets: peer is still | 733 // If the incoming ack's packets set expresses received packets: peer is still |
742 // acking packets which we never care about. | 734 // acking packets which we never care about. |
743 // Send an ack to raise the high water mark. | 735 // Send an ack to raise the high water mark. |
744 if (!incoming_ack.packets.Empty() && | 736 if (!incoming_ack.packets.Empty() && |
745 GetLeastUnacked(incoming_ack.path_id) > incoming_ack.packets.Min()) { | 737 GetLeastUnacked(incoming_ack.path_id) > incoming_ack.packets.Min()) { |
746 ++stop_waiting_count_; | 738 ++stop_waiting_count_; |
747 } else { | 739 } else { |
748 stop_waiting_count_ = 0; | 740 stop_waiting_count_ = 0; |
749 } | 741 } |
750 | 742 |
751 return connected_; | 743 return connected_; |
752 } | 744 } |
753 | 745 |
754 void QuicConnection::ProcessAckFrame(const QuicAckFrame& incoming_ack) { | 746 void QuicConnection::ProcessAckFrame(const QuicAckFrame& incoming_ack) { |
755 largest_seen_packet_with_ack_ = last_header_.packet_number; | 747 largest_seen_packet_with_ack_ = last_header_.packet_number; |
756 sent_packet_manager_->OnIncomingAck(incoming_ack, | 748 sent_packet_manager_->OnIncomingAck(incoming_ack, |
757 time_of_last_received_packet_); | 749 time_of_last_received_packet_); |
758 if (version() <= QUIC_VERSION_33) { | |
759 sent_entropy_manager_.ClearEntropyBefore( | |
760 sent_packet_manager_->GetLeastPacketAwaitedByPeer( | |
761 incoming_ack.path_id) - | |
762 1); | |
763 } | |
764 // Always reset the retransmission alarm when an ack comes in, since we now | 750 // Always reset the retransmission alarm when an ack comes in, since we now |
765 // have a better estimate of the current rtt than when it was set. | 751 // have a better estimate of the current rtt than when it was set. |
766 SetRetransmissionAlarm(); | 752 SetRetransmissionAlarm(); |
767 } | 753 } |
768 | 754 |
769 void QuicConnection::ProcessStopWaitingFrame( | 755 void QuicConnection::ProcessStopWaitingFrame( |
770 const QuicStopWaitingFrame& stop_waiting) { | 756 const QuicStopWaitingFrame& stop_waiting) { |
771 largest_seen_packet_with_stop_waiting_ = last_header_.packet_number; | 757 largest_seen_packet_with_stop_waiting_ = last_header_.packet_number; |
772 received_packet_manager_.UpdatePacketInformationSentByPeer(stop_waiting); | 758 received_packet_manager_.UpdatePacketInformationSentByPeer(stop_waiting); |
773 } | 759 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
831 << incoming_ack.largest_observed << " vs " | 817 << incoming_ack.largest_observed << " vs " |
832 << sent_packet_manager_->GetLargestObserved(incoming_ack.path_id) | 818 << sent_packet_manager_->GetLargestObserved(incoming_ack.path_id) |
833 << " packet_number:" << last_header_.packet_number | 819 << " packet_number:" << last_header_.packet_number |
834 << " largest seen with ack:" << largest_seen_packet_with_ack_ | 820 << " largest seen with ack:" << largest_seen_packet_with_ack_ |
835 << " connection_id: " << connection_id_; | 821 << " connection_id: " << connection_id_; |
836 // A new ack has a diminished largest_observed value. Error out. | 822 // A new ack has a diminished largest_observed value. Error out. |
837 // If this was an old packet, we wouldn't even have checked. | 823 // If this was an old packet, we wouldn't even have checked. |
838 return "Largest observed too low."; | 824 return "Largest observed too low."; |
839 } | 825 } |
840 | 826 |
841 if (version() <= QUIC_VERSION_33) { | 827 if (!incoming_ack.packets.Empty() && |
842 if (!incoming_ack.packets.Empty() && | 828 incoming_ack.packets.Max() != incoming_ack.largest_observed) { |
843 incoming_ack.packets.Max() > incoming_ack.largest_observed) { | 829 QUIC_BUG << ENDPOINT |
844 LOG(WARNING) << ENDPOINT | 830 << "Peer last received packet: " << incoming_ack.packets.Max() |
845 << "Peer sent missing packet: " << incoming_ack.packets.Max() | 831 << " which is not equal to largest observed: " |
846 << " which is greater than largest observed: " | 832 << incoming_ack.largest_observed; |
847 << incoming_ack.largest_observed; | 833 return "Last received packet not equal to largest observed."; |
848 return "Missing packet higher than largest observed."; | |
849 } | |
850 | |
851 if (!incoming_ack.packets.Empty() && | |
852 incoming_ack.packets.Min() < | |
853 sent_packet_manager_->GetLeastPacketAwaitedByPeer( | |
854 incoming_ack.path_id)) { | |
855 LOG(WARNING) << ENDPOINT | |
856 << "Peer sent missing packet: " << incoming_ack.packets.Min() | |
857 << " which is smaller than least_packet_awaited_by_peer_: " | |
858 << sent_packet_manager_->GetLeastPacketAwaitedByPeer( | |
859 incoming_ack.path_id); | |
860 return "Missing packet smaller than least awaited."; | |
861 } | |
862 if (!sent_entropy_manager_.IsValidEntropy(incoming_ack.largest_observed, | |
863 incoming_ack.packets, | |
864 incoming_ack.entropy_hash)) { | |
865 DLOG(WARNING) << ENDPOINT << "Peer sent invalid entropy." | |
866 << " largest_observed:" << incoming_ack.largest_observed | |
867 << " last_received:" << last_header_.packet_number; | |
868 return "Invalid entropy."; | |
869 } | |
870 } else { | |
871 if (!incoming_ack.packets.Empty() && | |
872 incoming_ack.packets.Max() != incoming_ack.largest_observed) { | |
873 QUIC_BUG << ENDPOINT | |
874 << "Peer last received packet: " << incoming_ack.packets.Max() | |
875 << " which is not equal to largest observed: " | |
876 << incoming_ack.largest_observed; | |
877 return "Last received packet not equal to largest observed."; | |
878 } | |
879 } | 834 } |
880 | 835 |
881 return nullptr; | 836 return nullptr; |
882 } | 837 } |
883 | 838 |
884 const char* QuicConnection::ValidateStopWaitingFrame( | 839 const char* QuicConnection::ValidateStopWaitingFrame( |
885 const QuicStopWaitingFrame& stop_waiting) { | 840 const QuicStopWaitingFrame& stop_waiting) { |
886 if (stop_waiting.least_unacked < | 841 if (stop_waiting.least_unacked < |
887 received_packet_manager_.peer_least_packet_awaiting_ack()) { | 842 received_packet_manager_.peer_least_packet_awaiting_ack()) { |
888 DLOG(ERROR) << ENDPOINT << "Peer's sent low least_unacked: " | 843 DLOG(ERROR) << ENDPOINT << "Peer's sent low least_unacked: " |
(...skipping 15 matching lines...) Expand all Loading... |
904 } | 859 } |
905 | 860 |
906 bool QuicConnection::OnRstStreamFrame(const QuicRstStreamFrame& frame) { | 861 bool QuicConnection::OnRstStreamFrame(const QuicRstStreamFrame& frame) { |
907 DCHECK(connected_); | 862 DCHECK(connected_); |
908 if (debug_visitor_ != nullptr) { | 863 if (debug_visitor_ != nullptr) { |
909 debug_visitor_->OnRstStreamFrame(frame); | 864 debug_visitor_->OnRstStreamFrame(frame); |
910 } | 865 } |
911 DVLOG(1) << ENDPOINT | 866 DVLOG(1) << ENDPOINT |
912 << "RST_STREAM_FRAME received for stream: " << frame.stream_id | 867 << "RST_STREAM_FRAME received for stream: " << frame.stream_id |
913 << " with error: " | 868 << " with error: " |
914 << QuicUtils::StreamErrorToString(frame.error_code); | 869 << QuicRstStreamErrorCodeToString(frame.error_code); |
915 visitor_->OnRstStream(frame); | 870 visitor_->OnRstStream(frame); |
916 visitor_->PostProcessAfterData(); | 871 visitor_->PostProcessAfterData(); |
917 should_last_packet_instigate_acks_ = true; | 872 should_last_packet_instigate_acks_ = true; |
918 return connected_; | 873 return connected_; |
919 } | 874 } |
920 | 875 |
921 bool QuicConnection::OnConnectionCloseFrame( | 876 bool QuicConnection::OnConnectionCloseFrame( |
922 const QuicConnectionCloseFrame& frame) { | 877 const QuicConnectionCloseFrame& frame) { |
923 DCHECK(connected_); | 878 DCHECK(connected_); |
924 if (debug_visitor_ != nullptr) { | 879 if (debug_visitor_ != nullptr) { |
925 debug_visitor_->OnConnectionCloseFrame(frame); | 880 debug_visitor_->OnConnectionCloseFrame(frame); |
926 } | 881 } |
927 DVLOG(1) << ENDPOINT | 882 DVLOG(1) << ENDPOINT |
928 << "Received ConnectionClose for connection: " << connection_id() | 883 << "Received ConnectionClose for connection: " << connection_id() |
929 << ", with error: " << QuicUtils::ErrorToString(frame.error_code) | 884 << ", with error: " << QuicErrorCodeToString(frame.error_code) |
930 << " (" << frame.error_details << ")"; | 885 << " (" << frame.error_details << ")"; |
931 if (frame.error_code == QUIC_BAD_MULTIPATH_FLAG) { | 886 if (frame.error_code == QUIC_BAD_MULTIPATH_FLAG) { |
932 LOG(ERROR) << " quic_version: " << version() | 887 LOG(ERROR) << "Unexpected QUIC_BAD_MULTIPATH_FLAG error." |
933 << " last_received_header: " << last_header_ | 888 << " last_received_header: " << last_header_ |
934 << " encryption_level: " << encryption_level_; | 889 << " encryption_level: " << encryption_level_; |
935 } | 890 } |
936 TearDownLocalConnectionState(frame.error_code, frame.error_details, | 891 TearDownLocalConnectionState(frame.error_code, frame.error_details, |
937 ConnectionCloseSource::FROM_PEER); | 892 ConnectionCloseSource::FROM_PEER); |
938 return connected_; | 893 return connected_; |
939 } | 894 } |
940 | 895 |
941 bool QuicConnection::OnGoAwayFrame(const QuicGoAwayFrame& frame) { | 896 bool QuicConnection::OnGoAwayFrame(const QuicGoAwayFrame& frame) { |
942 DCHECK(connected_); | 897 DCHECK(connected_); |
943 if (debug_visitor_ != nullptr) { | 898 if (debug_visitor_ != nullptr) { |
944 debug_visitor_->OnGoAwayFrame(frame); | 899 debug_visitor_->OnGoAwayFrame(frame); |
945 } | 900 } |
946 DVLOG(1) << ENDPOINT << "GOAWAY_FRAME received with last good stream: " | 901 DVLOG(1) << ENDPOINT << "GOAWAY_FRAME received with last good stream: " |
947 << frame.last_good_stream_id | 902 << frame.last_good_stream_id |
948 << " and error: " << QuicUtils::ErrorToString(frame.error_code) | 903 << " and error: " << QuicErrorCodeToString(frame.error_code) |
949 << " and reason: " << frame.reason_phrase; | 904 << " and reason: " << frame.reason_phrase; |
950 | 905 |
951 goaway_received_ = true; | 906 goaway_received_ = true; |
952 visitor_->OnGoAway(frame); | 907 visitor_->OnGoAway(frame); |
953 visitor_->PostProcessAfterData(); | 908 visitor_->PostProcessAfterData(); |
954 should_last_packet_instigate_acks_ = true; | 909 should_last_packet_instigate_acks_ = true; |
955 return connected_; | 910 return connected_; |
956 } | 911 } |
957 | 912 |
958 bool QuicConnection::OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) { | 913 bool QuicConnection::OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1032 ProcessStopWaitingFrame(last_stop_waiting_frame_); | 987 ProcessStopWaitingFrame(last_stop_waiting_frame_); |
1033 if (!connected_) { | 988 if (!connected_) { |
1034 return; | 989 return; |
1035 } | 990 } |
1036 } | 991 } |
1037 | 992 |
1038 MaybeQueueAck(was_missing); | 993 MaybeQueueAck(was_missing); |
1039 } | 994 } |
1040 | 995 |
1041 ClearLastFrames(); | 996 ClearLastFrames(); |
1042 MaybeCloseIfTooManyOutstandingPackets(); | |
1043 } | 997 } |
1044 | 998 |
1045 void QuicConnection::MaybeQueueAck(bool was_missing) { | 999 void QuicConnection::MaybeQueueAck(bool was_missing) { |
1046 ++num_packets_received_since_last_ack_sent_; | 1000 ++num_packets_received_since_last_ack_sent_; |
1047 // Always send an ack every 20 packets in order to allow the peer to discard | 1001 // Always send an ack every 20 packets in order to allow the peer to discard |
1048 // information from the SentPacketManager and provide an RTT measurement. | 1002 // information from the SentPacketManager and provide an RTT measurement. |
1049 if (num_packets_received_since_last_ack_sent_ >= | 1003 if (num_packets_received_since_last_ack_sent_ >= |
1050 kMaxPacketsReceivedBeforeAckSend) { | 1004 kMaxPacketsReceivedBeforeAckSend) { |
1051 ack_queued_ = true; | 1005 ack_queued_ = true; |
1052 } | 1006 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1106 } | 1060 } |
1107 } | 1061 } |
1108 | 1062 |
1109 void QuicConnection::ClearLastFrames() { | 1063 void QuicConnection::ClearLastFrames() { |
1110 should_last_packet_instigate_acks_ = false; | 1064 should_last_packet_instigate_acks_ = false; |
1111 if (!FLAGS_quic_receive_packet_once_decrypted) { | 1065 if (!FLAGS_quic_receive_packet_once_decrypted) { |
1112 last_stop_waiting_frame_.least_unacked = 0; | 1066 last_stop_waiting_frame_.least_unacked = 0; |
1113 } | 1067 } |
1114 } | 1068 } |
1115 | 1069 |
1116 void QuicConnection::MaybeCloseIfTooManyOutstandingPackets() { | |
1117 if (version() > QUIC_VERSION_33) { | |
1118 return; | |
1119 } | |
1120 // This occurs if we don't discard old packets we've sent fast enough. | |
1121 // It's possible largest observed is less than least unacked. | |
1122 if (sent_packet_manager_->GetLargestObserved(last_header_.path_id) > | |
1123 (sent_packet_manager_->GetLeastUnacked(last_header_.path_id) + | |
1124 kMaxTrackedPackets)) { | |
1125 CloseConnection( | |
1126 QUIC_TOO_MANY_OUTSTANDING_SENT_PACKETS, | |
1127 StringPrintf("More than %" PRIu64 " outstanding.", kMaxTrackedPackets), | |
1128 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); | |
1129 } | |
1130 // This occurs if there are received packet gaps and the peer does not raise | |
1131 // the least unacked fast enough. | |
1132 if (received_packet_manager_.NumTrackedPackets() > kMaxTrackedPackets) { | |
1133 CloseConnection( | |
1134 QUIC_TOO_MANY_OUTSTANDING_RECEIVED_PACKETS, | |
1135 StringPrintf("More than %" PRIu64 " outstanding.", kMaxTrackedPackets), | |
1136 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); | |
1137 } | |
1138 } | |
1139 | |
1140 const QuicFrame QuicConnection::GetUpdatedAckFrame() { | 1070 const QuicFrame QuicConnection::GetUpdatedAckFrame() { |
1141 return received_packet_manager_.GetUpdatedAckFrame(clock_->ApproximateNow()); | 1071 return received_packet_manager_.GetUpdatedAckFrame(clock_->ApproximateNow()); |
1142 } | 1072 } |
1143 | 1073 |
1144 void QuicConnection::PopulateStopWaitingFrame( | 1074 void QuicConnection::PopulateStopWaitingFrame( |
1145 QuicStopWaitingFrame* stop_waiting) { | 1075 QuicStopWaitingFrame* stop_waiting) { |
1146 stop_waiting->least_unacked = GetLeastUnacked(stop_waiting->path_id); | 1076 stop_waiting->least_unacked = GetLeastUnacked(stop_waiting->path_id); |
1147 if (version() <= QUIC_VERSION_33) { | |
1148 stop_waiting->entropy_hash = sent_entropy_manager_.GetCumulativeEntropy( | |
1149 stop_waiting->least_unacked - 1); | |
1150 } | |
1151 } | 1077 } |
1152 | 1078 |
1153 QuicPacketNumber QuicConnection::GetLeastUnacked(QuicPathId path_id) const { | 1079 QuicPacketNumber QuicConnection::GetLeastUnacked(QuicPathId path_id) const { |
1154 return sent_packet_manager_->GetLeastUnacked(path_id); | 1080 return sent_packet_manager_->GetLeastUnacked(path_id); |
1155 } | 1081 } |
1156 | 1082 |
1157 void QuicConnection::MaybeSendInResponseToPacket() { | 1083 void QuicConnection::MaybeSendInResponseToPacket() { |
1158 if (!connected_) { | 1084 if (!connected_) { |
1159 return; | 1085 return; |
1160 } | 1086 } |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1473 " without version flag before version negotiated.", | 1399 " without version flag before version negotiated.", |
1474 ENDPOINT, header.packet_number); | 1400 ENDPOINT, header.packet_number); |
1475 DLOG(WARNING) << error_details; | 1401 DLOG(WARNING) << error_details; |
1476 CloseConnection(QUIC_INVALID_VERSION, error_details, | 1402 CloseConnection(QUIC_INVALID_VERSION, error_details, |
1477 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); | 1403 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
1478 return false; | 1404 return false; |
1479 } else { | 1405 } else { |
1480 DCHECK_EQ(1u, header.public_header.versions.size()); | 1406 DCHECK_EQ(1u, header.public_header.versions.size()); |
1481 DCHECK_EQ(header.public_header.versions[0], version()); | 1407 DCHECK_EQ(header.public_header.versions[0], version()); |
1482 version_negotiation_state_ = NEGOTIATED_VERSION; | 1408 version_negotiation_state_ = NEGOTIATED_VERSION; |
1483 received_packet_manager_.SetVersion(version()); | |
1484 visitor_->OnSuccessfulVersionNegotiation(version()); | 1409 visitor_->OnSuccessfulVersionNegotiation(version()); |
1485 if (debug_visitor_ != nullptr) { | 1410 if (debug_visitor_ != nullptr) { |
1486 debug_visitor_->OnSuccessfulVersionNegotiation(version()); | 1411 debug_visitor_->OnSuccessfulVersionNegotiation(version()); |
1487 } | 1412 } |
1488 } | 1413 } |
1489 } else { | 1414 } else { |
1490 DCHECK(!header.public_header.version_flag); | 1415 DCHECK(!header.public_header.version_flag); |
1491 // If the client gets a packet without the version flag from the server | 1416 // If the client gets a packet without the version flag from the server |
1492 // it should stop sending version since the version negotiation is done. | 1417 // it should stop sending version since the version negotiation is done. |
1493 packet_generator_.StopSendingVersion(); | 1418 packet_generator_.StopSendingVersion(); |
1494 version_negotiation_state_ = NEGOTIATED_VERSION; | 1419 version_negotiation_state_ = NEGOTIATED_VERSION; |
1495 received_packet_manager_.SetVersion(version()); | |
1496 visitor_->OnSuccessfulVersionNegotiation(version()); | 1420 visitor_->OnSuccessfulVersionNegotiation(version()); |
1497 if (debug_visitor_ != nullptr) { | 1421 if (debug_visitor_ != nullptr) { |
1498 debug_visitor_->OnSuccessfulVersionNegotiation(version()); | 1422 debug_visitor_->OnSuccessfulVersionNegotiation(version()); |
1499 } | 1423 } |
1500 } | 1424 } |
1501 } | 1425 } |
1502 | 1426 |
1503 DCHECK_EQ(NEGOTIATED_VERSION, version_negotiation_state_); | 1427 DCHECK_EQ(NEGOTIATED_VERSION, version_negotiation_state_); |
1504 | 1428 |
1505 if (last_size_ > largest_received_packet_size_) { | 1429 if (last_size_ > largest_received_packet_size_) { |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1692 visitor_->OnWriteBlocked(); | 1616 visitor_->OnWriteBlocked(); |
1693 // If the socket buffers the the data, then the packet should not | 1617 // If the socket buffers the the data, then the packet should not |
1694 // be queued and sent again, which would result in an unnecessary | 1618 // be queued and sent again, which would result in an unnecessary |
1695 // duplicate packet being sent. The helper must call OnCanWrite | 1619 // duplicate packet being sent. The helper must call OnCanWrite |
1696 // when the write completes, and OnWriteError if an error occurs. | 1620 // when the write completes, and OnWriteError if an error occurs. |
1697 if (!writer_->IsWriteBlockedDataBuffered()) { | 1621 if (!writer_->IsWriteBlockedDataBuffered()) { |
1698 return false; | 1622 return false; |
1699 } | 1623 } |
1700 } | 1624 } |
1701 | 1625 |
1702 if (FLAGS_quic_only_track_sent_packets) { | 1626 // In some cases, an MTU probe can cause EMSGSIZE. This indicates that the |
1703 // In some cases, an MTU probe can cause EMSGSIZE. This indicates that the | 1627 // MTU discovery is permanently unsuccessful. |
1704 // MTU discovery is permanently unsuccessful. | 1628 if (result.status == WRITE_STATUS_ERROR && |
1705 if (result.status == WRITE_STATUS_ERROR && | 1629 result.error_code == kMessageTooBigErrorCode && |
1706 result.error_code == kMessageTooBigErrorCode && | 1630 packet->retransmittable_frames.empty() && |
1707 packet->retransmittable_frames.empty() && | 1631 packet->encrypted_length > long_term_mtu_) { |
1708 packet->encrypted_length > long_term_mtu_) { | 1632 mtu_discovery_target_ = 0; |
1709 mtu_discovery_target_ = 0; | 1633 mtu_discovery_alarm_->Cancel(); |
1710 mtu_discovery_alarm_->Cancel(); | 1634 // The write failed, but the writer is not blocked, so return true. |
1711 // The write failed, but the writer is not blocked, so return true. | 1635 return true; |
1712 return true; | 1636 } |
1713 } | |
1714 | 1637 |
1715 if (result.status == WRITE_STATUS_ERROR) { | 1638 if (result.status == WRITE_STATUS_ERROR) { |
1716 OnWriteError(result.error_code); | 1639 OnWriteError(result.error_code); |
1717 DLOG(ERROR) << ENDPOINT << "failed writing " << encrypted_length | 1640 DLOG(ERROR) << ENDPOINT << "failed writing " << encrypted_length |
1718 << " from host " | 1641 << " from host " << (self_address().address().empty() |
1719 << (self_address().address().empty() | 1642 ? " empty address " |
1720 ? " empty address " | 1643 : self_address().ToStringWithoutPort()) |
1721 : self_address().ToStringWithoutPort()) | 1644 << " to address " << peer_address().ToString() |
1722 << " to address " << peer_address().ToString() | 1645 << " with error code " << result.error_code; |
1723 << " with error code " << result.error_code; | 1646 return false; |
1724 return false; | |
1725 } | |
1726 } | 1647 } |
1727 | 1648 |
1728 if (result.status != WRITE_STATUS_ERROR && debug_visitor_ != nullptr) { | 1649 if (result.status != WRITE_STATUS_ERROR && debug_visitor_ != nullptr) { |
1729 // Pass the write result to the visitor. | 1650 // Pass the write result to the visitor. |
1730 debug_visitor_->OnPacketSent(*packet, packet->original_path_id, | 1651 debug_visitor_->OnPacketSent(*packet, packet->original_path_id, |
1731 packet->original_packet_number, | 1652 packet->original_packet_number, |
1732 packet->transmission_type, packet_send_time); | 1653 packet->transmission_type, packet_send_time); |
1733 } | 1654 } |
1734 if (packet->transmission_type == NOT_RETRANSMISSION) { | 1655 if (packet->transmission_type == NOT_RETRANSMISSION) { |
1735 time_of_last_sent_new_packet_ = packet_send_time; | 1656 time_of_last_sent_new_packet_ = packet_send_time; |
(...skipping 25 matching lines...) Expand all Loading... |
1761 sent_packet_manager_->GetLeastUnacked(packet->path_id), | 1682 sent_packet_manager_->GetLeastUnacked(packet->path_id), |
1762 sent_packet_manager_->EstimateMaxPacketsInFlight(max_packet_length())); | 1683 sent_packet_manager_->EstimateMaxPacketsInFlight(max_packet_length())); |
1763 | 1684 |
1764 stats_.bytes_sent += result.bytes_written; | 1685 stats_.bytes_sent += result.bytes_written; |
1765 ++stats_.packets_sent; | 1686 ++stats_.packets_sent; |
1766 if (packet->transmission_type != NOT_RETRANSMISSION) { | 1687 if (packet->transmission_type != NOT_RETRANSMISSION) { |
1767 stats_.bytes_retransmitted += result.bytes_written; | 1688 stats_.bytes_retransmitted += result.bytes_written; |
1768 ++stats_.packets_retransmitted; | 1689 ++stats_.packets_retransmitted; |
1769 } | 1690 } |
1770 | 1691 |
1771 if (!FLAGS_quic_only_track_sent_packets) { | |
1772 // In some cases, an MTU probe can cause EMSGSIZE. This indicates that the | |
1773 // MTU discovery is permanently unsuccessful. | |
1774 if (result.status == WRITE_STATUS_ERROR && | |
1775 result.error_code == kMessageTooBigErrorCode && | |
1776 packet->retransmittable_frames.empty() && | |
1777 packet->encrypted_length > long_term_mtu_) { | |
1778 mtu_discovery_target_ = 0; | |
1779 mtu_discovery_alarm_->Cancel(); | |
1780 return true; | |
1781 } | |
1782 | |
1783 if (result.status == WRITE_STATUS_ERROR) { | |
1784 OnWriteError(result.error_code); | |
1785 DLOG(ERROR) << ENDPOINT << "failed writing " << encrypted_length | |
1786 << " from host " | |
1787 << (self_address().address().empty() | |
1788 ? " empty address " | |
1789 : self_address().ToStringWithoutPort()) | |
1790 << " to address " << peer_address().ToString() | |
1791 << " with error code " << result.error_code; | |
1792 return false; | |
1793 } | |
1794 } | |
1795 | |
1796 return true; | 1692 return true; |
1797 } | 1693 } |
1798 | 1694 |
1799 bool QuicConnection::ShouldDiscardPacket(const SerializedPacket& packet) { | 1695 bool QuicConnection::ShouldDiscardPacket(const SerializedPacket& packet) { |
1800 if (!connected_) { | 1696 if (!connected_) { |
1801 DVLOG(1) << ENDPOINT << "Not sending packet as connection is disconnected."; | 1697 DVLOG(1) << ENDPOINT << "Not sending packet as connection is disconnected."; |
1802 return true; | 1698 return true; |
1803 } | 1699 } |
1804 | 1700 |
1805 QuicPacketNumber packet_number = packet.packet_number; | 1701 QuicPacketNumber packet_number = packet.packet_number; |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1898 ack_alarm_->Update(clock_->ApproximateNow(), QuicTime::Delta::Zero()); | 1794 ack_alarm_->Update(clock_->ApproximateNow(), QuicTime::Delta::Zero()); |
1899 } | 1795 } |
1900 } | 1796 } |
1901 | 1797 |
1902 void QuicConnection::SendOrQueuePacket(SerializedPacket* packet) { | 1798 void QuicConnection::SendOrQueuePacket(SerializedPacket* packet) { |
1903 // The caller of this function is responsible for checking CanWrite(). | 1799 // The caller of this function is responsible for checking CanWrite(). |
1904 if (packet->encrypted_buffer == nullptr) { | 1800 if (packet->encrypted_buffer == nullptr) { |
1905 QUIC_BUG << "packet.encrypted_buffer == nullptr in to SendOrQueuePacket"; | 1801 QUIC_BUG << "packet.encrypted_buffer == nullptr in to SendOrQueuePacket"; |
1906 return; | 1802 return; |
1907 } | 1803 } |
1908 if (version() <= QUIC_VERSION_33) { | |
1909 sent_entropy_manager_.RecordPacketEntropyHash(packet->packet_number, | |
1910 packet->entropy_hash); | |
1911 } | |
1912 // If there are already queued packets, queue this one immediately to ensure | 1804 // If there are already queued packets, queue this one immediately to ensure |
1913 // it's written in sequence number order. | 1805 // it's written in sequence number order. |
1914 if (!queued_packets_.empty() || !WritePacket(packet)) { | 1806 if (!queued_packets_.empty() || !WritePacket(packet)) { |
1915 // Take ownership of the underlying encrypted packet. | 1807 // Take ownership of the underlying encrypted packet. |
1916 packet->encrypted_buffer = QuicUtils::CopyBuffer(*packet); | 1808 packet->encrypted_buffer = QuicUtils::CopyBuffer(*packet); |
1917 queued_packets_.push_back(*packet); | 1809 queued_packets_.push_back(*packet); |
1918 packet->retransmittable_frames.clear(); | 1810 packet->retransmittable_frames.clear(); |
1919 } | 1811 } |
1920 | 1812 |
1921 QuicUtils::ClearSerializedPacket(packet); | 1813 QuicUtils::ClearSerializedPacket(packet); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2063 QuicErrorCode error, | 1955 QuicErrorCode error, |
2064 const string& error_details, | 1956 const string& error_details, |
2065 ConnectionCloseBehavior connection_close_behavior) { | 1957 ConnectionCloseBehavior connection_close_behavior) { |
2066 DCHECK(!error_details.empty()); | 1958 DCHECK(!error_details.empty()); |
2067 if (!connected_) { | 1959 if (!connected_) { |
2068 DVLOG(1) << "Connection is already closed."; | 1960 DVLOG(1) << "Connection is already closed."; |
2069 return; | 1961 return; |
2070 } | 1962 } |
2071 | 1963 |
2072 DVLOG(1) << ENDPOINT << "Closing connection: " << connection_id() | 1964 DVLOG(1) << ENDPOINT << "Closing connection: " << connection_id() |
2073 << ", with error: " << QuicUtils::ErrorToString(error) << " (" | 1965 << ", with error: " << QuicErrorCodeToString(error) << " (" << error |
2074 << error << "), and details: " << error_details; | 1966 << "), and details: " << error_details; |
2075 | 1967 |
2076 if (connection_close_behavior == | 1968 if (connection_close_behavior == |
2077 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET) { | 1969 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET) { |
2078 SendConnectionClosePacket(error, error_details, SEND_ACK); | 1970 SendConnectionClosePacket(error, error_details, SEND_ACK); |
2079 } else if (connection_close_behavior == | 1971 } else if (connection_close_behavior == |
2080 ConnectionCloseBehavior:: | 1972 ConnectionCloseBehavior:: |
2081 SEND_CONNECTION_CLOSE_PACKET_WITH_NO_ACK) { | 1973 SEND_CONNECTION_CLOSE_PACKET_WITH_NO_ACK) { |
2082 SendConnectionClosePacket(error, error_details, NO_ACK); | 1974 SendConnectionClosePacket(error, error_details, NO_ACK); |
2083 } | 1975 } |
2084 | 1976 |
2085 TearDownLocalConnectionState(error, error_details, | 1977 ConnectionCloseSource source = ConnectionCloseSource::FROM_SELF; |
2086 ConnectionCloseSource::FROM_SELF); | 1978 if (perspective_ == Perspective::IS_CLIENT && |
| 1979 error == QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT) { |
| 1980 // Regard stateless rejected connection as closed by server. |
| 1981 source = ConnectionCloseSource::FROM_PEER; |
| 1982 } |
| 1983 TearDownLocalConnectionState(error, error_details, source); |
2087 } | 1984 } |
2088 | 1985 |
2089 void QuicConnection::SendConnectionClosePacket(QuicErrorCode error, | 1986 void QuicConnection::SendConnectionClosePacket(QuicErrorCode error, |
2090 const string& details, | 1987 const string& details, |
2091 AckBundling ack_mode) { | 1988 AckBundling ack_mode) { |
2092 DVLOG(1) << ENDPOINT << "Sending connection close packet."; | 1989 DVLOG(1) << ENDPOINT << "Sending connection close packet."; |
2093 ClearQueuedPackets(); | 1990 ClearQueuedPackets(); |
2094 ScopedPacketBundler ack_bundler(this, ack_mode); | 1991 ScopedPacketBundler ack_bundler(this, ack_mode); |
2095 QuicConnectionCloseFrame* frame = new QuicConnectionCloseFrame(); | 1992 QuicConnectionCloseFrame* frame = new QuicConnectionCloseFrame(); |
2096 frame->error_code = error; | 1993 frame->error_code = error; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2137 | 2034 |
2138 void QuicConnection::SendGoAway(QuicErrorCode error, | 2035 void QuicConnection::SendGoAway(QuicErrorCode error, |
2139 QuicStreamId last_good_stream_id, | 2036 QuicStreamId last_good_stream_id, |
2140 const string& reason) { | 2037 const string& reason) { |
2141 if (goaway_sent_) { | 2038 if (goaway_sent_) { |
2142 return; | 2039 return; |
2143 } | 2040 } |
2144 goaway_sent_ = true; | 2041 goaway_sent_ = true; |
2145 | 2042 |
2146 DVLOG(1) << ENDPOINT << "Going away with error " | 2043 DVLOG(1) << ENDPOINT << "Going away with error " |
2147 << QuicUtils::ErrorToString(error) << " (" << error << ")"; | 2044 << QuicErrorCodeToString(error) << " (" << error << ")"; |
2148 | 2045 |
2149 // Opportunistically bundle an ack with this outgoing packet. | 2046 // Opportunistically bundle an ack with this outgoing packet. |
2150 ScopedPacketBundler ack_bundler(this, SEND_ACK_IF_PENDING); | 2047 ScopedPacketBundler ack_bundler(this, SEND_ACK_IF_PENDING); |
2151 packet_generator_.AddControlFrame( | 2048 packet_generator_.AddControlFrame( |
2152 QuicFrame(new QuicGoAwayFrame(error, last_good_stream_id, reason))); | 2049 QuicFrame(new QuicGoAwayFrame(error, last_good_stream_id, reason))); |
2153 } | 2050 } |
2154 | 2051 |
2155 QuicByteCount QuicConnection::max_packet_length() const { | 2052 QuicByteCount QuicConnection::max_packet_length() const { |
2156 return packet_generator_.GetCurrentMaxPacketLength(); | 2053 return packet_generator_.GetCurrentMaxPacketLength(); |
2157 } | 2054 } |
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2583 | 2480 |
2584 void QuicConnection::CheckIfApplicationLimited() { | 2481 void QuicConnection::CheckIfApplicationLimited() { |
2585 if (queued_packets_.empty() && | 2482 if (queued_packets_.empty() && |
2586 !sent_packet_manager_->HasPendingRetransmissions() && | 2483 !sent_packet_manager_->HasPendingRetransmissions() && |
2587 !visitor_->WillingAndAbleToWrite()) { | 2484 !visitor_->WillingAndAbleToWrite()) { |
2588 sent_packet_manager_->OnApplicationLimited(); | 2485 sent_packet_manager_->OnApplicationLimited(); |
2589 } | 2486 } |
2590 } | 2487 } |
2591 | 2488 |
2592 } // namespace net | 2489 } // namespace net |
OLD | NEW |