[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / net / quic / quic_server_session.cc
blob4545b08768b2a2bc330bdf0421fddcdcaccc5d2b
1 // Copyright 2014 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.
5 #include "net/quic/quic_server_session.h"
7 #include "base/logging.h"
8 #include "net/quic/crypto/cached_network_parameters.h"
9 #include "net/quic/quic_connection.h"
10 #include "net/quic/quic_flags.h"
11 #include "net/quic/quic_spdy_server_stream.h"
12 #include "net/quic/reliable_quic_stream.h"
14 namespace net {
16 QuicServerSession::QuicServerSession(const QuicConfig& config,
17 QuicConnection* connection,
18 QuicServerSessionVisitor* visitor)
19 : QuicSession(connection, config),
20 visitor_(visitor),
21 bandwidth_estimate_sent_to_client_(QuicBandwidth::Zero()),
22 last_scup_time_(QuicTime::Zero()),
23 last_scup_sequence_number_(0) {}
25 QuicServerSession::~QuicServerSession() {}
27 void QuicServerSession::InitializeSession(
28 const QuicCryptoServerConfig* crypto_config) {
29 QuicSession::InitializeSession();
30 crypto_stream_.reset(CreateQuicCryptoServerStream(crypto_config));
33 QuicCryptoServerStream* QuicServerSession::CreateQuicCryptoServerStream(
34 const QuicCryptoServerConfig* crypto_config) {
35 return new QuicCryptoServerStream(crypto_config, this);
38 void QuicServerSession::OnConfigNegotiated() {
39 QuicSession::OnConfigNegotiated();
41 if (!config()->HasReceivedConnectionOptions()) {
42 return;
45 // If the client has provided a bandwidth estimate from the same serving
46 // region, then pass it to the sent packet manager in preparation for possible
47 // bandwidth resumption.
48 const CachedNetworkParameters* cached_network_params =
49 crypto_stream_->previous_cached_network_params();
50 const bool max_bandwidth_resumption =
51 ContainsQuicTag(config()->ReceivedConnectionOptions(), kBWMX);
52 if (cached_network_params != nullptr &&
53 (ContainsQuicTag(config()->ReceivedConnectionOptions(), kBWRE) ||
54 max_bandwidth_resumption) &&
55 cached_network_params->serving_region() == serving_region_) {
56 connection()->ResumeConnectionState(*cached_network_params,
57 max_bandwidth_resumption);
60 if (FLAGS_enable_quic_fec &&
61 ContainsQuicTag(config()->ReceivedConnectionOptions(), kFHDR)) {
62 // kFHDR config maps to FEC protection always for headers stream.
63 // TODO(jri): Add crypto stream in addition to headers for kHDR.
64 headers_stream_->set_fec_policy(FEC_PROTECT_ALWAYS);
68 void QuicServerSession::OnConnectionClosed(QuicErrorCode error,
69 bool from_peer) {
70 QuicSession::OnConnectionClosed(error, from_peer);
71 // In the unlikely event we get a connection close while doing an asynchronous
72 // crypto event, make sure we cancel the callback.
73 if (crypto_stream_.get() != nullptr) {
74 crypto_stream_->CancelOutstandingCallbacks();
76 visitor_->OnConnectionClosed(connection()->connection_id(), error);
79 void QuicServerSession::OnWriteBlocked() {
80 QuicSession::OnWriteBlocked();
81 visitor_->OnWriteBlocked(connection());
84 void QuicServerSession::OnCongestionWindowChange(QuicTime now) {
85 // Only send updates when the application has no data to write.
86 if (HasDataToWrite()) {
87 return;
90 // If not enough time has passed since the last time we sent an update to the
91 // client, or not enough packets have been sent, then return early.
92 const QuicSentPacketManager& sent_packet_manager =
93 connection()->sent_packet_manager();
94 int64 srtt_ms =
95 sent_packet_manager.GetRttStats()->smoothed_rtt().ToMilliseconds();
96 int64 now_ms = now.Subtract(last_scup_time_).ToMilliseconds();
97 int64 packets_since_last_scup =
98 connection()->sequence_number_of_last_sent_packet() -
99 last_scup_sequence_number_;
100 if (now_ms < (kMinIntervalBetweenServerConfigUpdatesRTTs * srtt_ms) ||
101 now_ms < kMinIntervalBetweenServerConfigUpdatesMs ||
102 packets_since_last_scup < kMinPacketsBetweenServerConfigUpdates) {
103 return;
106 // If the bandwidth recorder does not have a valid estimate, return early.
107 const QuicSustainedBandwidthRecorder& bandwidth_recorder =
108 sent_packet_manager.SustainedBandwidthRecorder();
109 if (!bandwidth_recorder.HasEstimate()) {
110 return;
113 // The bandwidth recorder has recorded at least one sustained bandwidth
114 // estimate. Check that it's substantially different from the last one that
115 // we sent to the client, and if so, send the new one.
116 QuicBandwidth new_bandwidth_estimate = bandwidth_recorder.BandwidthEstimate();
118 int64 bandwidth_delta =
119 std::abs(new_bandwidth_estimate.ToBitsPerSecond() -
120 bandwidth_estimate_sent_to_client_.ToBitsPerSecond());
122 // Define "substantial" difference as a 50% increase or decrease from the
123 // last estimate.
124 bool substantial_difference =
125 bandwidth_delta >
126 0.5 * bandwidth_estimate_sent_to_client_.ToBitsPerSecond();
127 if (!substantial_difference) {
128 return;
131 bandwidth_estimate_sent_to_client_ = new_bandwidth_estimate;
132 DVLOG(1) << "Server: sending new bandwidth estimate (KBytes/s): "
133 << bandwidth_estimate_sent_to_client_.ToKBytesPerSecond();
135 // Include max bandwidth in the update.
136 QuicBandwidth max_bandwidth_estimate =
137 bandwidth_recorder.MaxBandwidthEstimate();
138 int32 max_bandwidth_timestamp = bandwidth_recorder.MaxBandwidthTimestamp();
140 // Fill the proto before passing it to the crypto stream to send.
141 CachedNetworkParameters cached_network_params;
142 cached_network_params.set_bandwidth_estimate_bytes_per_second(
143 bandwidth_estimate_sent_to_client_.ToBytesPerSecond());
144 cached_network_params.set_max_bandwidth_estimate_bytes_per_second(
145 max_bandwidth_estimate.ToBytesPerSecond());
146 cached_network_params.set_max_bandwidth_timestamp_seconds(
147 max_bandwidth_timestamp);
148 cached_network_params.set_min_rtt_ms(
149 sent_packet_manager.GetRttStats()->min_rtt().ToMilliseconds());
150 cached_network_params.set_previous_connection_state(
151 bandwidth_recorder.EstimateRecordedDuringSlowStart()
152 ? CachedNetworkParameters::SLOW_START
153 : CachedNetworkParameters::CONGESTION_AVOIDANCE);
154 cached_network_params.set_timestamp(
155 connection()->clock()->WallNow().ToUNIXSeconds());
156 if (!serving_region_.empty()) {
157 cached_network_params.set_serving_region(serving_region_);
160 crypto_stream_->SendServerConfigUpdate(&cached_network_params);
162 connection()->OnSendConnectionState(cached_network_params);
164 last_scup_time_ = now;
165 last_scup_sequence_number_ =
166 connection()->sequence_number_of_last_sent_packet();
169 bool QuicServerSession::ShouldCreateIncomingDataStream(QuicStreamId id) {
170 if (id % 2 == 0) {
171 DVLOG(1) << "Invalid incoming even stream_id:" << id;
172 connection()->SendConnectionClose(QUIC_INVALID_STREAM_ID);
173 return false;
175 if (GetNumOpenStreams() >= get_max_open_streams()) {
176 DVLOG(1) << "Failed to create a new incoming stream with id:" << id
177 << " Already " << GetNumOpenStreams() << " streams open (max "
178 << get_max_open_streams() << ").";
179 connection()->SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS);
180 return false;
182 return true;
185 QuicDataStream* QuicServerSession::CreateIncomingDataStream(
186 QuicStreamId id) {
187 if (!ShouldCreateIncomingDataStream(id)) {
188 return nullptr;
191 return new QuicSpdyServerStream(id, this);
194 QuicDataStream* QuicServerSession::CreateOutgoingDataStream() {
195 DLOG(ERROR) << "Server push not yet supported";
196 return nullptr;
199 QuicCryptoServerStream* QuicServerSession::GetCryptoStream() {
200 return crypto_stream_.get();
203 } // namespace net