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.
5 #include "net/tools/quic/quic_server_session.h"
7 #include "base/logging.h"
8 #include "net/quic/proto/cached_network_parameters.pb.h"
9 #include "net/quic/quic_connection.h"
10 #include "net/quic/quic_flags.h"
11 #include "net/quic/quic_spdy_session.h"
12 #include "net/quic/reliable_quic_stream.h"
13 #include "net/tools/quic/quic_spdy_server_stream.h"
18 QuicServerSession::QuicServerSession(
19 const QuicConfig
& config
,
20 QuicConnection
* connection
,
21 QuicServerSessionVisitor
* visitor
,
22 const QuicCryptoServerConfig
* crypto_config
)
23 : QuicSpdySession(connection
, config
),
24 crypto_config_(crypto_config
),
26 bandwidth_resumption_enabled_(false),
27 bandwidth_estimate_sent_to_client_(QuicBandwidth::Zero()),
28 last_scup_time_(QuicTime::Zero()),
29 last_scup_packet_number_(0) {}
31 QuicServerSession::~QuicServerSession() {}
33 void QuicServerSession::Initialize() {
34 crypto_stream_
.reset(CreateQuicCryptoServerStream(crypto_config_
));
35 QuicSpdySession::Initialize();
38 QuicCryptoServerStream
* QuicServerSession::CreateQuicCryptoServerStream(
39 const QuicCryptoServerConfig
* crypto_config
) {
40 return new QuicCryptoServerStream(crypto_config
, this);
43 void QuicServerSession::OnConfigNegotiated() {
44 QuicSession::OnConfigNegotiated();
46 if (!config()->HasReceivedConnectionOptions()) {
50 // If the client has provided a bandwidth estimate from the same serving
51 // region, then pass it to the sent packet manager in preparation for possible
52 // bandwidth resumption.
53 const CachedNetworkParameters
* cached_network_params
=
54 crypto_stream_
->previous_cached_network_params();
55 const bool last_bandwidth_resumption
=
56 ContainsQuicTag(config()->ReceivedConnectionOptions(), kBWRE
);
57 const bool max_bandwidth_resumption
=
58 ContainsQuicTag(config()->ReceivedConnectionOptions(), kBWMX
);
59 bandwidth_resumption_enabled_
=
60 last_bandwidth_resumption
|| max_bandwidth_resumption
;
61 if (cached_network_params
!= nullptr && bandwidth_resumption_enabled_
&&
62 cached_network_params
->serving_region() == serving_region_
) {
63 int64 seconds_since_estimate
=
64 connection()->clock()->WallNow().ToUNIXSeconds() -
65 cached_network_params
->timestamp();
66 bool estimate_within_last_hour
=
67 seconds_since_estimate
<= kNumSecondsPerHour
;
68 if (estimate_within_last_hour
) {
69 connection()->ResumeConnectionState(*cached_network_params
,
70 max_bandwidth_resumption
);
74 if (FLAGS_enable_quic_fec
&&
75 ContainsQuicTag(config()->ReceivedConnectionOptions(), kFHDR
)) {
76 // kFHDR config maps to FEC protection always for headers stream.
77 // TODO(jri): Add crypto stream in addition to headers for kHDR.
78 headers_stream()->set_fec_policy(FEC_PROTECT_ALWAYS
);
82 void QuicServerSession::OnConnectionClosed(QuicErrorCode error
,
84 QuicSession::OnConnectionClosed(error
, from_peer
);
85 // In the unlikely event we get a connection close while doing an asynchronous
86 // crypto event, make sure we cancel the callback.
87 if (crypto_stream_
.get() != nullptr) {
88 crypto_stream_
->CancelOutstandingCallbacks();
90 visitor_
->OnConnectionClosed(connection()->connection_id(), error
);
93 void QuicServerSession::OnWriteBlocked() {
94 QuicSession::OnWriteBlocked();
95 visitor_
->OnWriteBlocked(connection());
98 void QuicServerSession::OnCongestionWindowChange(QuicTime now
) {
99 if (!bandwidth_resumption_enabled_
) {
102 // Only send updates when the application has no data to write.
103 if (HasDataToWrite()) {
107 // If not enough time has passed since the last time we sent an update to the
108 // client, or not enough packets have been sent, then return early.
109 const QuicSentPacketManager
& sent_packet_manager
=
110 connection()->sent_packet_manager();
112 sent_packet_manager
.GetRttStats()->smoothed_rtt().ToMilliseconds();
113 int64 now_ms
= now
.Subtract(last_scup_time_
).ToMilliseconds();
114 int64 packets_since_last_scup
=
115 connection()->packet_number_of_last_sent_packet() -
116 last_scup_packet_number_
;
117 if (now_ms
< (kMinIntervalBetweenServerConfigUpdatesRTTs
* srtt_ms
) ||
118 now_ms
< kMinIntervalBetweenServerConfigUpdatesMs
||
119 packets_since_last_scup
< kMinPacketsBetweenServerConfigUpdates
) {
123 // If the bandwidth recorder does not have a valid estimate, return early.
124 const QuicSustainedBandwidthRecorder
& bandwidth_recorder
=
125 sent_packet_manager
.SustainedBandwidthRecorder();
126 if (!bandwidth_recorder
.HasEstimate()) {
130 // The bandwidth recorder has recorded at least one sustained bandwidth
131 // estimate. Check that it's substantially different from the last one that
132 // we sent to the client, and if so, send the new one.
133 QuicBandwidth new_bandwidth_estimate
= bandwidth_recorder
.BandwidthEstimate();
135 int64 bandwidth_delta
=
136 std::abs(new_bandwidth_estimate
.ToBitsPerSecond() -
137 bandwidth_estimate_sent_to_client_
.ToBitsPerSecond());
139 // Define "substantial" difference as a 50% increase or decrease from the
141 bool substantial_difference
=
143 0.5 * bandwidth_estimate_sent_to_client_
.ToBitsPerSecond();
144 if (!substantial_difference
) {
148 bandwidth_estimate_sent_to_client_
= new_bandwidth_estimate
;
149 DVLOG(1) << "Server: sending new bandwidth estimate (KBytes/s): "
150 << bandwidth_estimate_sent_to_client_
.ToKBytesPerSecond();
152 // Include max bandwidth in the update.
153 QuicBandwidth max_bandwidth_estimate
=
154 bandwidth_recorder
.MaxBandwidthEstimate();
155 int32 max_bandwidth_timestamp
= bandwidth_recorder
.MaxBandwidthTimestamp();
157 // Fill the proto before passing it to the crypto stream to send.
158 CachedNetworkParameters cached_network_params
;
159 cached_network_params
.set_bandwidth_estimate_bytes_per_second(
160 bandwidth_estimate_sent_to_client_
.ToBytesPerSecond());
161 cached_network_params
.set_max_bandwidth_estimate_bytes_per_second(
162 max_bandwidth_estimate
.ToBytesPerSecond());
163 cached_network_params
.set_max_bandwidth_timestamp_seconds(
164 max_bandwidth_timestamp
);
165 cached_network_params
.set_min_rtt_ms(
166 sent_packet_manager
.GetRttStats()->min_rtt().ToMilliseconds());
167 cached_network_params
.set_previous_connection_state(
168 bandwidth_recorder
.EstimateRecordedDuringSlowStart()
169 ? CachedNetworkParameters::SLOW_START
170 : CachedNetworkParameters::CONGESTION_AVOIDANCE
);
171 cached_network_params
.set_timestamp(
172 connection()->clock()->WallNow().ToUNIXSeconds());
173 if (!serving_region_
.empty()) {
174 cached_network_params
.set_serving_region(serving_region_
);
177 crypto_stream_
->SendServerConfigUpdate(&cached_network_params
);
179 connection()->OnSendConnectionState(cached_network_params
);
181 last_scup_time_
= now
;
182 last_scup_packet_number_
= connection()->packet_number_of_last_sent_packet();
185 bool QuicServerSession::ShouldCreateIncomingDynamicStream(QuicStreamId id
) {
186 if (!connection()->connected()) {
187 LOG(DFATAL
) << "ShouldCreateIncomingDynamicStream called when disconnected";
192 DVLOG(1) << "Invalid incoming even stream_id:" << id
;
193 connection()->SendConnectionClose(QUIC_INVALID_STREAM_ID
);
196 if (!FLAGS_exact_stream_id_delta
) {
197 if (GetNumOpenStreams() >= get_max_open_streams()) {
198 DVLOG(1) << "Failed to create a new incoming stream with id:" << id
199 << " Already " << GetNumOpenStreams() << " streams open (max "
200 << get_max_open_streams() << ").";
201 connection()->SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS
);
208 QuicDataStream
* QuicServerSession::CreateIncomingDynamicStream(
210 if (!ShouldCreateIncomingDynamicStream(id
)) {
214 return new QuicSpdyServerStream(id
, this);
217 QuicDataStream
* QuicServerSession::CreateOutgoingDynamicStream() {
218 DLOG(ERROR
) << "Server push not yet supported";
222 QuicCryptoServerStream
* QuicServerSession::GetCryptoStream() {
223 return crypto_stream_
.get();