| 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 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 1047 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1058 ++num_retransmittable_packets_received_since_last_ack_sent_; | 1058 ++num_retransmittable_packets_received_since_last_ack_sent_; |
| 1059 if (ack_mode_ != TCP_ACKING && | 1059 if (ack_mode_ != TCP_ACKING && |
| 1060 last_header_.packet_number > kMinReceivedBeforeAckDecimation) { | 1060 last_header_.packet_number > kMinReceivedBeforeAckDecimation) { |
| 1061 // Ack up to 10 packets at once. | 1061 // Ack up to 10 packets at once. |
| 1062 if (num_retransmittable_packets_received_since_last_ack_sent_ >= | 1062 if (num_retransmittable_packets_received_since_last_ack_sent_ >= |
| 1063 kMaxRetransmittablePacketsBeforeAck) { | 1063 kMaxRetransmittablePacketsBeforeAck) { |
| 1064 ack_queued_ = true; | 1064 ack_queued_ = true; |
| 1065 } else if (!ack_alarm_->IsSet()) { | 1065 } else if (!ack_alarm_->IsSet()) { |
| 1066 // Wait the minimum of a quarter min_rtt and the delayed ack time. | 1066 // Wait the minimum of a quarter min_rtt and the delayed ack time. |
| 1067 QuicTime::Delta ack_delay = QuicTime::Delta::Min( | 1067 QuicTime::Delta ack_delay = QuicTime::Delta::Min( |
| 1068 DelayedAckTime(), | 1068 DelayedAckTime(), sent_packet_manager_->GetRttStats()->min_rtt() * |
| 1069 sent_packet_manager_->GetRttStats()->min_rtt().Multiply( | 1069 ack_decimation_delay_); |
| 1070 ack_decimation_delay_)); | 1070 ack_alarm_->Set(clock_->ApproximateNow() + ack_delay); |
| 1071 ack_alarm_->Set(clock_->ApproximateNow().Add(ack_delay)); | |
| 1072 } | 1071 } |
| 1073 } else { | 1072 } else { |
| 1074 // Ack with a timer or every 2 packets by default. | 1073 // Ack with a timer or every 2 packets by default. |
| 1075 if (num_retransmittable_packets_received_since_last_ack_sent_ >= | 1074 if (num_retransmittable_packets_received_since_last_ack_sent_ >= |
| 1076 kDefaultRetransmittablePacketsBeforeAck) { | 1075 kDefaultRetransmittablePacketsBeforeAck) { |
| 1077 ack_queued_ = true; | 1076 ack_queued_ = true; |
| 1078 } else if (!ack_alarm_->IsSet()) { | 1077 } else if (!ack_alarm_->IsSet()) { |
| 1079 ack_alarm_->Set(clock_->ApproximateNow().Add(DelayedAckTime())); | 1078 ack_alarm_->Set(clock_->ApproximateNow() + DelayedAckTime()); |
| 1080 } | 1079 } |
| 1081 } | 1080 } |
| 1082 | 1081 |
| 1083 // If there are new missing packets to report, send an ack immediately. | 1082 // If there are new missing packets to report, send an ack immediately. |
| 1084 if (received_packet_manager_.HasNewMissingPackets()) { | 1083 if (received_packet_manager_.HasNewMissingPackets()) { |
| 1085 if (ack_mode_ == ACK_DECIMATION_WITH_REORDERING) { | 1084 if (ack_mode_ == ACK_DECIMATION_WITH_REORDERING) { |
| 1086 // Wait the minimum of an eighth min_rtt and the existing ack time. | 1085 // Wait the minimum of an eighth min_rtt and the existing ack time. |
| 1087 QuicTime ack_time = clock_->ApproximateNow().Add( | 1086 QuicTime ack_time = |
| 1088 sent_packet_manager_->GetRttStats()->min_rtt().Multiply(0.125)); | 1087 clock_->ApproximateNow() + |
| 1088 0.125 * sent_packet_manager_->GetRttStats()->min_rtt(); |
| 1089 if (!ack_alarm_->IsSet() || ack_alarm_->deadline() > ack_time) { | 1089 if (!ack_alarm_->IsSet() || ack_alarm_->deadline() > ack_time) { |
| 1090 ack_alarm_->Cancel(); | 1090 ack_alarm_->Cancel(); |
| 1091 ack_alarm_->Set(ack_time); | 1091 ack_alarm_->Set(ack_time); |
| 1092 } | 1092 } |
| 1093 } else { | 1093 } else { |
| 1094 ack_queued_ = true; | 1094 ack_queued_ = true; |
| 1095 } | 1095 } |
| 1096 } | 1096 } |
| 1097 } | 1097 } |
| 1098 | 1098 |
| (...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1571 sent_packet_manager_->TimeUntilSend(now, retransmittable, &path_id); | 1571 sent_packet_manager_->TimeUntilSend(now, retransmittable, &path_id); |
| 1572 if (delay.IsInfinite()) { | 1572 if (delay.IsInfinite()) { |
| 1573 DCHECK_EQ(kInvalidPathId, path_id); | 1573 DCHECK_EQ(kInvalidPathId, path_id); |
| 1574 send_alarm_->Cancel(); | 1574 send_alarm_->Cancel(); |
| 1575 return false; | 1575 return false; |
| 1576 } | 1576 } |
| 1577 | 1577 |
| 1578 DCHECK_NE(kInvalidPathId, path_id); | 1578 DCHECK_NE(kInvalidPathId, path_id); |
| 1579 // If the scheduler requires a delay, then we can not send this packet now. | 1579 // If the scheduler requires a delay, then we can not send this packet now. |
| 1580 if (!delay.IsZero()) { | 1580 if (!delay.IsZero()) { |
| 1581 send_alarm_->Update(now.Add(delay), QuicTime::Delta::FromMilliseconds(1)); | 1581 send_alarm_->Update(now + delay, QuicTime::Delta::FromMilliseconds(1)); |
| 1582 DVLOG(1) << ENDPOINT << "Delaying sending " << delay.ToMilliseconds() | 1582 DVLOG(1) << ENDPOINT << "Delaying sending " << delay.ToMilliseconds() |
| 1583 << "ms"; | 1583 << "ms"; |
| 1584 return false; | 1584 return false; |
| 1585 } | 1585 } |
| 1586 return true; | 1586 return true; |
| 1587 } | 1587 } |
| 1588 | 1588 |
| 1589 bool QuicConnection::WritePacket(SerializedPacket* packet) { | 1589 bool QuicConnection::WritePacket(SerializedPacket* packet) { |
| 1590 if (packet->packet_number < | 1590 if (packet->packet_number < |
| 1591 sent_packet_manager_->GetLargestSentPacket(packet->path_id)) { | 1591 sent_packet_manager_->GetLargestSentPacket(packet->path_id)) { |
| (...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2134 } | 2134 } |
| 2135 | 2135 |
| 2136 void QuicConnection::SetNetworkTimeouts(QuicTime::Delta handshake_timeout, | 2136 void QuicConnection::SetNetworkTimeouts(QuicTime::Delta handshake_timeout, |
| 2137 QuicTime::Delta idle_timeout) { | 2137 QuicTime::Delta idle_timeout) { |
| 2138 QUIC_BUG_IF(idle_timeout > handshake_timeout) | 2138 QUIC_BUG_IF(idle_timeout > handshake_timeout) |
| 2139 << "idle_timeout:" << idle_timeout.ToMilliseconds() | 2139 << "idle_timeout:" << idle_timeout.ToMilliseconds() |
| 2140 << " handshake_timeout:" << handshake_timeout.ToMilliseconds(); | 2140 << " handshake_timeout:" << handshake_timeout.ToMilliseconds(); |
| 2141 // Adjust the idle timeout on client and server to prevent clients from | 2141 // Adjust the idle timeout on client and server to prevent clients from |
| 2142 // sending requests to servers which have already closed the connection. | 2142 // sending requests to servers which have already closed the connection. |
| 2143 if (perspective_ == Perspective::IS_SERVER) { | 2143 if (perspective_ == Perspective::IS_SERVER) { |
| 2144 idle_timeout = idle_timeout.Add(QuicTime::Delta::FromSeconds(3)); | 2144 idle_timeout = idle_timeout + QuicTime::Delta::FromSeconds(3); |
| 2145 } else if (idle_timeout > QuicTime::Delta::FromSeconds(1)) { | 2145 } else if (idle_timeout > QuicTime::Delta::FromSeconds(1)) { |
| 2146 idle_timeout = idle_timeout.Subtract(QuicTime::Delta::FromSeconds(1)); | 2146 idle_timeout = idle_timeout - QuicTime::Delta::FromSeconds(1); |
| 2147 } | 2147 } |
| 2148 handshake_timeout_ = handshake_timeout; | 2148 handshake_timeout_ = handshake_timeout; |
| 2149 idle_network_timeout_ = idle_timeout; | 2149 idle_network_timeout_ = idle_timeout; |
| 2150 | 2150 |
| 2151 SetTimeoutAlarm(); | 2151 SetTimeoutAlarm(); |
| 2152 } | 2152 } |
| 2153 | 2153 |
| 2154 void QuicConnection::CheckForTimeout() { | 2154 void QuicConnection::CheckForTimeout() { |
| 2155 QuicTime now = clock_->ApproximateNow(); | 2155 QuicTime now = clock_->ApproximateNow(); |
| 2156 QuicTime time_of_last_packet = | 2156 QuicTime time_of_last_packet = |
| 2157 max(time_of_last_received_packet_, last_send_for_timeout_); | 2157 max(time_of_last_received_packet_, last_send_for_timeout_); |
| 2158 | 2158 |
| 2159 // |delta| can be < 0 as |now| is approximate time but |time_of_last_packet| | 2159 // |delta| can be < 0 as |now| is approximate time but |time_of_last_packet| |
| 2160 // is accurate time. However, this should not change the behavior of | 2160 // is accurate time. However, this should not change the behavior of |
| 2161 // timeout handling. | 2161 // timeout handling. |
| 2162 QuicTime::Delta idle_duration = now.Subtract(time_of_last_packet); | 2162 QuicTime::Delta idle_duration = now - time_of_last_packet; |
| 2163 DVLOG(1) << ENDPOINT << "last packet " | 2163 DVLOG(1) << ENDPOINT << "last packet " |
| 2164 << time_of_last_packet.ToDebuggingValue() | 2164 << time_of_last_packet.ToDebuggingValue() |
| 2165 << " now:" << now.ToDebuggingValue() | 2165 << " now:" << now.ToDebuggingValue() |
| 2166 << " idle_duration:" << idle_duration.ToMicroseconds() | 2166 << " idle_duration:" << idle_duration.ToMicroseconds() |
| 2167 << " idle_network_timeout: " | 2167 << " idle_network_timeout: " |
| 2168 << idle_network_timeout_.ToMicroseconds(); | 2168 << idle_network_timeout_.ToMicroseconds(); |
| 2169 if (idle_duration >= idle_network_timeout_) { | 2169 if (idle_duration >= idle_network_timeout_) { |
| 2170 const string error_details = "No recent network activity."; | 2170 const string error_details = "No recent network activity."; |
| 2171 DVLOG(1) << ENDPOINT << error_details; | 2171 DVLOG(1) << ENDPOINT << error_details; |
| 2172 CloseConnection(QUIC_NETWORK_IDLE_TIMEOUT, error_details, | 2172 CloseConnection(QUIC_NETWORK_IDLE_TIMEOUT, error_details, |
| 2173 idle_timeout_connection_close_behavior_); | 2173 idle_timeout_connection_close_behavior_); |
| 2174 return; | 2174 return; |
| 2175 } | 2175 } |
| 2176 | 2176 |
| 2177 if (!handshake_timeout_.IsInfinite()) { | 2177 if (!handshake_timeout_.IsInfinite()) { |
| 2178 QuicTime::Delta connected_duration = | 2178 QuicTime::Delta connected_duration = now - stats_.connection_creation_time; |
| 2179 now.Subtract(stats_.connection_creation_time); | |
| 2180 DVLOG(1) << ENDPOINT | 2179 DVLOG(1) << ENDPOINT |
| 2181 << "connection time: " << connected_duration.ToMicroseconds() | 2180 << "connection time: " << connected_duration.ToMicroseconds() |
| 2182 << " handshake timeout: " << handshake_timeout_.ToMicroseconds(); | 2181 << " handshake timeout: " << handshake_timeout_.ToMicroseconds(); |
| 2183 if (connected_duration >= handshake_timeout_) { | 2182 if (connected_duration >= handshake_timeout_) { |
| 2184 const string error_details = "Handshake timeout expired."; | 2183 const string error_details = "Handshake timeout expired."; |
| 2185 DVLOG(1) << ENDPOINT << error_details; | 2184 DVLOG(1) << ENDPOINT << error_details; |
| 2186 CloseConnection(QUIC_HANDSHAKE_TIMEOUT, error_details, | 2185 CloseConnection(QUIC_HANDSHAKE_TIMEOUT, error_details, |
| 2187 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); | 2186 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 2188 return; | 2187 return; |
| 2189 } | 2188 } |
| 2190 } | 2189 } |
| 2191 | 2190 |
| 2192 SetTimeoutAlarm(); | 2191 SetTimeoutAlarm(); |
| 2193 } | 2192 } |
| 2194 | 2193 |
| 2195 void QuicConnection::SetTimeoutAlarm() { | 2194 void QuicConnection::SetTimeoutAlarm() { |
| 2196 QuicTime time_of_last_packet = | 2195 QuicTime time_of_last_packet = |
| 2197 max(time_of_last_received_packet_, time_of_last_sent_new_packet_); | 2196 max(time_of_last_received_packet_, time_of_last_sent_new_packet_); |
| 2198 | 2197 |
| 2199 QuicTime deadline = time_of_last_packet.Add(idle_network_timeout_); | 2198 QuicTime deadline = time_of_last_packet + idle_network_timeout_; |
| 2200 if (!handshake_timeout_.IsInfinite()) { | 2199 if (!handshake_timeout_.IsInfinite()) { |
| 2201 deadline = | 2200 deadline = |
| 2202 min(deadline, stats_.connection_creation_time.Add(handshake_timeout_)); | 2201 min(deadline, stats_.connection_creation_time + handshake_timeout_); |
| 2203 } | 2202 } |
| 2204 | 2203 |
| 2205 timeout_alarm_->Cancel(); | 2204 timeout_alarm_->Cancel(); |
| 2206 timeout_alarm_->Set(deadline); | 2205 timeout_alarm_->Set(deadline); |
| 2207 } | 2206 } |
| 2208 | 2207 |
| 2209 void QuicConnection::SetPingAlarm() { | 2208 void QuicConnection::SetPingAlarm() { |
| 2210 if (perspective_ == Perspective::IS_SERVER) { | 2209 if (perspective_ == Perspective::IS_SERVER) { |
| 2211 // Only clients send pings. | 2210 // Only clients send pings. |
| 2212 return; | 2211 return; |
| 2213 } | 2212 } |
| 2214 if (!visitor_->HasOpenDynamicStreams()) { | 2213 if (!visitor_->HasOpenDynamicStreams()) { |
| 2215 ping_alarm_->Cancel(); | 2214 ping_alarm_->Cancel(); |
| 2216 // Don't send a ping unless there are open streams. | 2215 // Don't send a ping unless there are open streams. |
| 2217 return; | 2216 return; |
| 2218 } | 2217 } |
| 2219 QuicTime::Delta ping_timeout = QuicTime::Delta::FromSeconds(kPingTimeoutSecs); | 2218 QuicTime::Delta ping_timeout = QuicTime::Delta::FromSeconds(kPingTimeoutSecs); |
| 2220 ping_alarm_->Update(clock_->ApproximateNow().Add(ping_timeout), | 2219 ping_alarm_->Update(clock_->ApproximateNow() + ping_timeout, |
| 2221 QuicTime::Delta::FromSeconds(1)); | 2220 QuicTime::Delta::FromSeconds(1)); |
| 2222 } | 2221 } |
| 2223 | 2222 |
| 2224 void QuicConnection::SetRetransmissionAlarm() { | 2223 void QuicConnection::SetRetransmissionAlarm() { |
| 2225 if (delay_setting_retransmission_alarm_) { | 2224 if (delay_setting_retransmission_alarm_) { |
| 2226 pending_retransmission_alarm_ = true; | 2225 pending_retransmission_alarm_ = true; |
| 2227 return; | 2226 return; |
| 2228 } | 2227 } |
| 2229 QuicTime retransmission_time = sent_packet_manager_->GetRetransmissionTime(); | 2228 QuicTime retransmission_time = sent_packet_manager_->GetRetransmissionTime(); |
| 2230 retransmission_alarm_->Update(retransmission_time, | 2229 retransmission_alarm_->Update(retransmission_time, |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2511 // the sender and a signaling mechanism -- if the sender uses a | 2510 // the sender and a signaling mechanism -- if the sender uses a |
| 2512 // different MinRTO, we may get spurious retransmissions. May not have | 2511 // different MinRTO, we may get spurious retransmissions. May not have |
| 2513 // any benefits, but if the delayed ack becomes a significant source | 2512 // any benefits, but if the delayed ack becomes a significant source |
| 2514 // of (likely, tail) latency, then consider such a mechanism. | 2513 // of (likely, tail) latency, then consider such a mechanism. |
| 2515 const QuicTime::Delta QuicConnection::DelayedAckTime() { | 2514 const QuicTime::Delta QuicConnection::DelayedAckTime() { |
| 2516 return QuicTime::Delta::FromMilliseconds( | 2515 return QuicTime::Delta::FromMilliseconds( |
| 2517 min(kMaxDelayedAckTimeMs, kMinRetransmissionTimeMs / 2)); | 2516 min(kMaxDelayedAckTimeMs, kMinRetransmissionTimeMs / 2)); |
| 2518 } | 2517 } |
| 2519 | 2518 |
| 2520 } // namespace net | 2519 } // namespace net |
| OLD | NEW |