| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/quic/quic_server_session_base.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "net/quic/proto/cached_network_parameters.pb.h" | |
| 9 #include "net/quic/quic_bug_tracker.h" | |
| 10 #include "net/quic/quic_connection.h" | |
| 11 #include "net/quic/quic_flags.h" | |
| 12 #include "net/quic/quic_spdy_session.h" | |
| 13 #include "net/quic/reliable_quic_stream.h" | |
| 14 | |
| 15 using std::string; | |
| 16 | |
| 17 namespace net { | |
| 18 | |
| 19 QuicServerSessionBase::QuicServerSessionBase( | |
| 20 const QuicConfig& config, | |
| 21 QuicConnection* connection, | |
| 22 Visitor* visitor, | |
| 23 Helper* helper, | |
| 24 const QuicCryptoServerConfig* crypto_config, | |
| 25 QuicCompressedCertsCache* compressed_certs_cache) | |
| 26 : QuicSpdySession(connection, config), | |
| 27 crypto_config_(crypto_config), | |
| 28 compressed_certs_cache_(compressed_certs_cache), | |
| 29 visitor_(visitor), | |
| 30 helper_(helper), | |
| 31 bandwidth_resumption_enabled_(false), | |
| 32 bandwidth_estimate_sent_to_client_(QuicBandwidth::Zero()), | |
| 33 last_scup_time_(QuicTime::Zero()), | |
| 34 last_scup_packet_number_(0), | |
| 35 server_push_enabled_(false) {} | |
| 36 | |
| 37 QuicServerSessionBase::~QuicServerSessionBase() {} | |
| 38 | |
| 39 void QuicServerSessionBase::Initialize() { | |
| 40 crypto_stream_.reset( | |
| 41 CreateQuicCryptoServerStream(crypto_config_, compressed_certs_cache_)); | |
| 42 QuicSpdySession::Initialize(); | |
| 43 } | |
| 44 | |
| 45 void QuicServerSessionBase::OnConfigNegotiated() { | |
| 46 QuicSpdySession::OnConfigNegotiated(); | |
| 47 | |
| 48 if (!config()->HasReceivedConnectionOptions()) { | |
| 49 return; | |
| 50 } | |
| 51 | |
| 52 // Enable bandwidth resumption if peer sent correct connection options. | |
| 53 const bool last_bandwidth_resumption = | |
| 54 ContainsQuicTag(config()->ReceivedConnectionOptions(), kBWRE); | |
| 55 const bool max_bandwidth_resumption = | |
| 56 ContainsQuicTag(config()->ReceivedConnectionOptions(), kBWMX); | |
| 57 bandwidth_resumption_enabled_ = | |
| 58 last_bandwidth_resumption || max_bandwidth_resumption; | |
| 59 server_push_enabled_ = | |
| 60 ContainsQuicTag(config()->ReceivedConnectionOptions(), kSPSH); | |
| 61 | |
| 62 // If the client has provided a bandwidth estimate from the same serving | |
| 63 // region as this server, then decide whether to use the data for bandwidth | |
| 64 // resumption. | |
| 65 const CachedNetworkParameters* cached_network_params = | |
| 66 crypto_stream_->PreviousCachedNetworkParams(); | |
| 67 if (cached_network_params != nullptr && | |
| 68 cached_network_params->serving_region() == serving_region_) { | |
| 69 // Log the received connection parameters, regardless of how they | |
| 70 // get used for bandwidth resumption. | |
| 71 connection()->OnReceiveConnectionState(*cached_network_params); | |
| 72 | |
| 73 if (bandwidth_resumption_enabled_) { | |
| 74 // Only do bandwidth resumption if estimate is recent enough. | |
| 75 const int64_t seconds_since_estimate = | |
| 76 connection()->clock()->WallNow().ToUNIXSeconds() - | |
| 77 cached_network_params->timestamp(); | |
| 78 if (seconds_since_estimate <= kNumSecondsPerHour) { | |
| 79 connection()->ResumeConnectionState(*cached_network_params, | |
| 80 max_bandwidth_resumption); | |
| 81 } | |
| 82 } | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 void QuicServerSessionBase::OnConnectionClosed(QuicErrorCode error, | |
| 87 const string& error_details, | |
| 88 ConnectionCloseSource source) { | |
| 89 QuicSession::OnConnectionClosed(error, error_details, source); | |
| 90 // In the unlikely event we get a connection close while doing an asynchronous | |
| 91 // crypto event, make sure we cancel the callback. | |
| 92 if (crypto_stream_.get() != nullptr) { | |
| 93 crypto_stream_->CancelOutstandingCallbacks(); | |
| 94 } | |
| 95 visitor_->OnConnectionClosed(connection()->connection_id(), error, | |
| 96 error_details); | |
| 97 } | |
| 98 | |
| 99 void QuicServerSessionBase::OnWriteBlocked() { | |
| 100 QuicSession::OnWriteBlocked(); | |
| 101 visitor_->OnWriteBlocked(connection()); | |
| 102 } | |
| 103 | |
| 104 void QuicServerSessionBase::OnCongestionWindowChange(QuicTime now) { | |
| 105 if (!bandwidth_resumption_enabled_) { | |
| 106 return; | |
| 107 } | |
| 108 // Only send updates when the application has no data to write. | |
| 109 if (HasDataToWrite()) { | |
| 110 return; | |
| 111 } | |
| 112 | |
| 113 // If not enough time has passed since the last time we sent an update to the | |
| 114 // client, or not enough packets have been sent, then return early. | |
| 115 const QuicSentPacketManagerInterface& sent_packet_manager = | |
| 116 connection()->sent_packet_manager(); | |
| 117 int64_t srtt_ms = | |
| 118 sent_packet_manager.GetRttStats()->smoothed_rtt().ToMilliseconds(); | |
| 119 int64_t now_ms = (now - last_scup_time_).ToMilliseconds(); | |
| 120 int64_t packets_since_last_scup = | |
| 121 connection()->packet_number_of_last_sent_packet() - | |
| 122 last_scup_packet_number_; | |
| 123 if (now_ms < (kMinIntervalBetweenServerConfigUpdatesRTTs * srtt_ms) || | |
| 124 now_ms < kMinIntervalBetweenServerConfigUpdatesMs || | |
| 125 packets_since_last_scup < kMinPacketsBetweenServerConfigUpdates) { | |
| 126 return; | |
| 127 } | |
| 128 | |
| 129 // If the bandwidth recorder does not have a valid estimate, return early. | |
| 130 const QuicSustainedBandwidthRecorder* bandwidth_recorder = | |
| 131 sent_packet_manager.SustainedBandwidthRecorder(); | |
| 132 if (bandwidth_recorder == nullptr || !bandwidth_recorder->HasEstimate()) { | |
| 133 return; | |
| 134 } | |
| 135 | |
| 136 // The bandwidth recorder has recorded at least one sustained bandwidth | |
| 137 // estimate. Check that it's substantially different from the last one that | |
| 138 // we sent to the client, and if so, send the new one. | |
| 139 QuicBandwidth new_bandwidth_estimate = | |
| 140 bandwidth_recorder->BandwidthEstimate(); | |
| 141 | |
| 142 int64_t bandwidth_delta = | |
| 143 std::abs(new_bandwidth_estimate.ToBitsPerSecond() - | |
| 144 bandwidth_estimate_sent_to_client_.ToBitsPerSecond()); | |
| 145 | |
| 146 // Define "substantial" difference as a 50% increase or decrease from the | |
| 147 // last estimate. | |
| 148 bool substantial_difference = | |
| 149 bandwidth_delta > | |
| 150 0.5 * bandwidth_estimate_sent_to_client_.ToBitsPerSecond(); | |
| 151 if (!substantial_difference) { | |
| 152 return; | |
| 153 } | |
| 154 | |
| 155 bandwidth_estimate_sent_to_client_ = new_bandwidth_estimate; | |
| 156 DVLOG(1) << "Server: sending new bandwidth estimate (KBytes/s): " | |
| 157 << bandwidth_estimate_sent_to_client_.ToKBytesPerSecond(); | |
| 158 | |
| 159 // Include max bandwidth in the update. | |
| 160 QuicBandwidth max_bandwidth_estimate = | |
| 161 bandwidth_recorder->MaxBandwidthEstimate(); | |
| 162 int32_t max_bandwidth_timestamp = bandwidth_recorder->MaxBandwidthTimestamp(); | |
| 163 | |
| 164 // Fill the proto before passing it to the crypto stream to send. | |
| 165 const int32_t bw_estimate_bytes_per_second = | |
| 166 BandwidthToCachedParameterBytesPerSecond( | |
| 167 bandwidth_estimate_sent_to_client_); | |
| 168 const int32_t max_bw_estimate_bytes_per_second = | |
| 169 BandwidthToCachedParameterBytesPerSecond(max_bandwidth_estimate); | |
| 170 QUIC_BUG_IF(max_bw_estimate_bytes_per_second < 0) | |
| 171 << max_bw_estimate_bytes_per_second; | |
| 172 QUIC_BUG_IF(bw_estimate_bytes_per_second < 0) << bw_estimate_bytes_per_second; | |
| 173 | |
| 174 CachedNetworkParameters cached_network_params; | |
| 175 cached_network_params.set_bandwidth_estimate_bytes_per_second( | |
| 176 bw_estimate_bytes_per_second); | |
| 177 cached_network_params.set_max_bandwidth_estimate_bytes_per_second( | |
| 178 max_bw_estimate_bytes_per_second); | |
| 179 cached_network_params.set_max_bandwidth_timestamp_seconds( | |
| 180 max_bandwidth_timestamp); | |
| 181 cached_network_params.set_min_rtt_ms( | |
| 182 sent_packet_manager.GetRttStats()->min_rtt().ToMilliseconds()); | |
| 183 cached_network_params.set_previous_connection_state( | |
| 184 bandwidth_recorder->EstimateRecordedDuringSlowStart() | |
| 185 ? CachedNetworkParameters::SLOW_START | |
| 186 : CachedNetworkParameters::CONGESTION_AVOIDANCE); | |
| 187 cached_network_params.set_timestamp( | |
| 188 connection()->clock()->WallNow().ToUNIXSeconds()); | |
| 189 if (!serving_region_.empty()) { | |
| 190 cached_network_params.set_serving_region(serving_region_); | |
| 191 } | |
| 192 | |
| 193 crypto_stream_->SendServerConfigUpdate(&cached_network_params); | |
| 194 | |
| 195 connection()->OnSendConnectionState(cached_network_params); | |
| 196 | |
| 197 last_scup_time_ = now; | |
| 198 last_scup_packet_number_ = connection()->packet_number_of_last_sent_packet(); | |
| 199 } | |
| 200 | |
| 201 QuicConnectionId QuicServerSessionBase::GenerateConnectionIdForReject( | |
| 202 QuicConnectionId connection_id) { | |
| 203 return helper_->GenerateConnectionIdForReject(connection_id); | |
| 204 } | |
| 205 | |
| 206 bool QuicServerSessionBase::CanAcceptClientHello( | |
| 207 const CryptoHandshakeMessage& message, | |
| 208 string* error_details) { | |
| 209 return helper_->CanAcceptClientHello(message, connection()->self_address(), | |
| 210 error_details); | |
| 211 } | |
| 212 | |
| 213 bool QuicServerSessionBase::ShouldCreateIncomingDynamicStream(QuicStreamId id) { | |
| 214 if (!connection()->connected()) { | |
| 215 QUIC_BUG << "ShouldCreateIncomingDynamicStream called when disconnected"; | |
| 216 return false; | |
| 217 } | |
| 218 | |
| 219 if (id % 2 == 0) { | |
| 220 DVLOG(1) << "Invalid incoming even stream_id:" << id; | |
| 221 connection()->CloseConnection( | |
| 222 QUIC_INVALID_STREAM_ID, "Client created even numbered stream", | |
| 223 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); | |
| 224 return false; | |
| 225 } | |
| 226 return true; | |
| 227 } | |
| 228 | |
| 229 bool QuicServerSessionBase::ShouldCreateOutgoingDynamicStream() { | |
| 230 if (!connection()->connected()) { | |
| 231 QUIC_BUG << "ShouldCreateOutgoingDynamicStream called when disconnected"; | |
| 232 return false; | |
| 233 } | |
| 234 if (!crypto_stream_->encryption_established()) { | |
| 235 QUIC_BUG << "Encryption not established so no outgoing stream created."; | |
| 236 return false; | |
| 237 } | |
| 238 if (GetNumOpenOutgoingStreams() >= max_open_outgoing_streams()) { | |
| 239 VLOG(1) << "No more streams should be created. " | |
| 240 << "Already " << GetNumOpenOutgoingStreams() << " open."; | |
| 241 return false; | |
| 242 } | |
| 243 return true; | |
| 244 } | |
| 245 | |
| 246 QuicCryptoServerStreamBase* QuicServerSessionBase::GetCryptoStream() { | |
| 247 return crypto_stream_.get(); | |
| 248 } | |
| 249 | |
| 250 int32_t QuicServerSessionBase::BandwidthToCachedParameterBytesPerSecond( | |
| 251 const QuicBandwidth& bandwidth) { | |
| 252 int64_t bytes_per_second = bandwidth.ToBytesPerSecond(); | |
| 253 return (bytes_per_second > static_cast<int64_t>(INT32_MAX) | |
| 254 ? INT32_MAX | |
| 255 : static_cast<int32_t>(bytes_per_second)); | |
| 256 } | |
| 257 | |
| 258 } // namespace net | |
| OLD | NEW |