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_sequence_number_(0) {
32 QuicServerSession::~QuicServerSession() {}
34 void QuicServerSession::Initialize() {
35 crypto_stream_
.reset(CreateQuicCryptoServerStream(crypto_config_
));
36 QuicSpdySession::Initialize();
39 QuicCryptoServerStream
* QuicServerSession::CreateQuicCryptoServerStream(
40 const QuicCryptoServerConfig
* crypto_config
) {
41 return new QuicCryptoServerStream(crypto_config
, this);
44 void QuicServerSession::OnConfigNegotiated() {
45 QuicSession::OnConfigNegotiated();
47 if (!config()->HasReceivedConnectionOptions()) {
51 // If the client has provided a bandwidth estimate from the same serving
52 // region, then pass it to the sent packet manager in preparation for possible
53 // bandwidth resumption.
54 const CachedNetworkParameters
* cached_network_params
=
55 crypto_stream_
->previous_cached_network_params();
56 const bool last_bandwidth_resumption
=
57 ContainsQuicTag(config()->ReceivedConnectionOptions(), kBWRE
);
58 const bool max_bandwidth_resumption
=
59 ContainsQuicTag(config()->ReceivedConnectionOptions(), kBWMX
);
60 bandwidth_resumption_enabled_
=
61 last_bandwidth_resumption
|| max_bandwidth_resumption
;
62 if (cached_network_params
!= nullptr && bandwidth_resumption_enabled_
&&
63 cached_network_params
->serving_region() == serving_region_
) {
64 int64 seconds_since_estimate
=
65 connection()->clock()->WallNow().ToUNIXSeconds() -
66 cached_network_params
->timestamp();
67 bool estimate_within_last_hour
=
68 seconds_since_estimate
<= kNumSecondsPerHour
;
69 if (estimate_within_last_hour
) {
70 connection()->ResumeConnectionState(*cached_network_params
,
71 max_bandwidth_resumption
);
75 if (FLAGS_enable_quic_fec
&&
76 ContainsQuicTag(config()->ReceivedConnectionOptions(), kFHDR
)) {
77 // kFHDR config maps to FEC protection always for headers stream.
78 // TODO(jri): Add crypto stream in addition to headers for kHDR.
79 headers_stream()->set_fec_policy(FEC_PROTECT_ALWAYS
);
83 void QuicServerSession::OnConnectionClosed(QuicErrorCode error
,
85 QuicSession::OnConnectionClosed(error
, from_peer
);
86 // In the unlikely event we get a connection close while doing an asynchronous
87 // crypto event, make sure we cancel the callback.
88 if (crypto_stream_
.get() != nullptr) {
89 crypto_stream_
->CancelOutstandingCallbacks();
91 visitor_
->OnConnectionClosed(connection()->connection_id(), error
);
94 void QuicServerSession::OnWriteBlocked() {
95 QuicSession::OnWriteBlocked();
96 visitor_
->OnWriteBlocked(connection());
99 void QuicServerSession::OnCongestionWindowChange(QuicTime now
) {
100 if (!bandwidth_resumption_enabled_
) {
103 // Only send updates when the application has no data to write.
104 if (HasDataToWrite()) {
108 // If not enough time has passed since the last time we sent an update to the
109 // client, or not enough packets have been sent, then return early.
110 const QuicSentPacketManager
& sent_packet_manager
=
111 connection()->sent_packet_manager();
113 sent_packet_manager
.GetRttStats()->smoothed_rtt().ToMilliseconds();
114 int64 now_ms
= now
.Subtract(last_scup_time_
).ToMilliseconds();
115 int64 packets_since_last_scup
=
116 connection()->sequence_number_of_last_sent_packet() -
117 last_scup_sequence_number_
;
118 if (now_ms
< (kMinIntervalBetweenServerConfigUpdatesRTTs
* srtt_ms
) ||
119 now_ms
< kMinIntervalBetweenServerConfigUpdatesMs
||
120 packets_since_last_scup
< kMinPacketsBetweenServerConfigUpdates
) {
124 // If the bandwidth recorder does not have a valid estimate, return early.
125 const QuicSustainedBandwidthRecorder
& bandwidth_recorder
=
126 sent_packet_manager
.SustainedBandwidthRecorder();
127 if (!bandwidth_recorder
.HasEstimate()) {
131 // The bandwidth recorder has recorded at least one sustained bandwidth
132 // estimate. Check that it's substantially different from the last one that
133 // we sent to the client, and if so, send the new one.
134 QuicBandwidth new_bandwidth_estimate
= bandwidth_recorder
.BandwidthEstimate();
136 int64 bandwidth_delta
=
137 std::abs(new_bandwidth_estimate
.ToBitsPerSecond() -
138 bandwidth_estimate_sent_to_client_
.ToBitsPerSecond());
140 // Define "substantial" difference as a 50% increase or decrease from the
142 bool substantial_difference
=
144 0.5 * bandwidth_estimate_sent_to_client_
.ToBitsPerSecond();
145 if (!substantial_difference
) {
149 bandwidth_estimate_sent_to_client_
= new_bandwidth_estimate
;
150 DVLOG(1) << "Server: sending new bandwidth estimate (KBytes/s): "
151 << bandwidth_estimate_sent_to_client_
.ToKBytesPerSecond();
153 // Include max bandwidth in the update.
154 QuicBandwidth max_bandwidth_estimate
=
155 bandwidth_recorder
.MaxBandwidthEstimate();
156 int32 max_bandwidth_timestamp
= bandwidth_recorder
.MaxBandwidthTimestamp();
158 // Fill the proto before passing it to the crypto stream to send.
159 CachedNetworkParameters cached_network_params
;
160 cached_network_params
.set_bandwidth_estimate_bytes_per_second(
161 bandwidth_estimate_sent_to_client_
.ToBytesPerSecond());
162 cached_network_params
.set_max_bandwidth_estimate_bytes_per_second(
163 max_bandwidth_estimate
.ToBytesPerSecond());
164 cached_network_params
.set_max_bandwidth_timestamp_seconds(
165 max_bandwidth_timestamp
);
166 cached_network_params
.set_min_rtt_ms(
167 sent_packet_manager
.GetRttStats()->min_rtt().ToMilliseconds());
168 cached_network_params
.set_previous_connection_state(
169 bandwidth_recorder
.EstimateRecordedDuringSlowStart()
170 ? CachedNetworkParameters::SLOW_START
171 : CachedNetworkParameters::CONGESTION_AVOIDANCE
);
172 cached_network_params
.set_timestamp(
173 connection()->clock()->WallNow().ToUNIXSeconds());
174 if (!serving_region_
.empty()) {
175 cached_network_params
.set_serving_region(serving_region_
);
178 crypto_stream_
->SendServerConfigUpdate(&cached_network_params
);
180 connection()->OnSendConnectionState(cached_network_params
);
182 last_scup_time_
= now
;
183 last_scup_sequence_number_
=
184 connection()->sequence_number_of_last_sent_packet();
187 bool QuicServerSession::ShouldCreateIncomingDynamicStream(QuicStreamId id
) {
188 if (!connection()->connected()) {
189 LOG(DFATAL
) << "ShouldCreateIncomingDynamicStream called when disconnected";
194 DVLOG(1) << "Invalid incoming even stream_id:" << id
;
195 connection()->SendConnectionClose(QUIC_INVALID_STREAM_ID
);
198 if (GetNumOpenStreams() >= get_max_open_streams()) {
199 DVLOG(1) << "Failed to create a new incoming stream with id:" << id
200 << " Already " << GetNumOpenStreams() << " streams open (max "
201 << get_max_open_streams() << ").";
202 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();