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/reliable_quic_stream.h"
12 #include "net/tools/quic/quic_spdy_server_stream.h"
17 QuicServerSession::QuicServerSession(
18 const QuicConfig
& config
,
19 QuicConnection
* connection
,
20 QuicServerSessionVisitor
* visitor
,
21 const QuicCryptoServerConfig
* crypto_config
)
22 : QuicSession(connection
, config
),
23 crypto_config_(crypto_config
),
25 bandwidth_resumption_enabled_(false),
26 bandwidth_estimate_sent_to_client_(QuicBandwidth::Zero()),
27 last_scup_time_(QuicTime::Zero()),
28 last_scup_sequence_number_(0) {
31 QuicServerSession::~QuicServerSession() {}
33 void QuicServerSession::Initialize() {
34 crypto_stream_
.reset(CreateQuicCryptoServerStream(crypto_config_
));
35 QuicSession::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 connection()->ResumeConnectionState(*cached_network_params
,
64 max_bandwidth_resumption
);
67 if (FLAGS_enable_quic_fec
&&
68 ContainsQuicTag(config()->ReceivedConnectionOptions(), kFHDR
)) {
69 // kFHDR config maps to FEC protection always for headers stream.
70 // TODO(jri): Add crypto stream in addition to headers for kHDR.
71 headers_stream_
->set_fec_policy(FEC_PROTECT_ALWAYS
);
75 void QuicServerSession::OnConnectionClosed(QuicErrorCode error
,
77 QuicSession::OnConnectionClosed(error
, from_peer
);
78 // In the unlikely event we get a connection close while doing an asynchronous
79 // crypto event, make sure we cancel the callback.
80 if (crypto_stream_
.get() != nullptr) {
81 crypto_stream_
->CancelOutstandingCallbacks();
83 visitor_
->OnConnectionClosed(connection()->connection_id(), error
);
86 void QuicServerSession::OnWriteBlocked() {
87 QuicSession::OnWriteBlocked();
88 visitor_
->OnWriteBlocked(connection());
91 void QuicServerSession::OnCongestionWindowChange(QuicTime now
) {
92 if (!bandwidth_resumption_enabled_
) {
95 // Only send updates when the application has no data to write.
96 if (HasDataToWrite()) {
100 // If not enough time has passed since the last time we sent an update to the
101 // client, or not enough packets have been sent, then return early.
102 const QuicSentPacketManager
& sent_packet_manager
=
103 connection()->sent_packet_manager();
105 sent_packet_manager
.GetRttStats()->smoothed_rtt().ToMilliseconds();
106 int64 now_ms
= now
.Subtract(last_scup_time_
).ToMilliseconds();
107 int64 packets_since_last_scup
=
108 connection()->sequence_number_of_last_sent_packet() -
109 last_scup_sequence_number_
;
110 if (now_ms
< (kMinIntervalBetweenServerConfigUpdatesRTTs
* srtt_ms
) ||
111 now_ms
< kMinIntervalBetweenServerConfigUpdatesMs
||
112 packets_since_last_scup
< kMinPacketsBetweenServerConfigUpdates
) {
116 // If the bandwidth recorder does not have a valid estimate, return early.
117 const QuicSustainedBandwidthRecorder
& bandwidth_recorder
=
118 sent_packet_manager
.SustainedBandwidthRecorder();
119 if (!bandwidth_recorder
.HasEstimate()) {
123 // The bandwidth recorder has recorded at least one sustained bandwidth
124 // estimate. Check that it's substantially different from the last one that
125 // we sent to the client, and if so, send the new one.
126 QuicBandwidth new_bandwidth_estimate
= bandwidth_recorder
.BandwidthEstimate();
128 int64 bandwidth_delta
=
129 std::abs(new_bandwidth_estimate
.ToBitsPerSecond() -
130 bandwidth_estimate_sent_to_client_
.ToBitsPerSecond());
132 // Define "substantial" difference as a 50% increase or decrease from the
134 bool substantial_difference
=
136 0.5 * bandwidth_estimate_sent_to_client_
.ToBitsPerSecond();
137 if (!substantial_difference
) {
141 bandwidth_estimate_sent_to_client_
= new_bandwidth_estimate
;
142 DVLOG(1) << "Server: sending new bandwidth estimate (KBytes/s): "
143 << bandwidth_estimate_sent_to_client_
.ToKBytesPerSecond();
145 // Include max bandwidth in the update.
146 QuicBandwidth max_bandwidth_estimate
=
147 bandwidth_recorder
.MaxBandwidthEstimate();
148 int32 max_bandwidth_timestamp
= bandwidth_recorder
.MaxBandwidthTimestamp();
150 // Fill the proto before passing it to the crypto stream to send.
151 CachedNetworkParameters cached_network_params
;
152 cached_network_params
.set_bandwidth_estimate_bytes_per_second(
153 bandwidth_estimate_sent_to_client_
.ToBytesPerSecond());
154 cached_network_params
.set_max_bandwidth_estimate_bytes_per_second(
155 max_bandwidth_estimate
.ToBytesPerSecond());
156 cached_network_params
.set_max_bandwidth_timestamp_seconds(
157 max_bandwidth_timestamp
);
158 cached_network_params
.set_min_rtt_ms(
159 sent_packet_manager
.GetRttStats()->min_rtt().ToMilliseconds());
160 cached_network_params
.set_previous_connection_state(
161 bandwidth_recorder
.EstimateRecordedDuringSlowStart()
162 ? CachedNetworkParameters::SLOW_START
163 : CachedNetworkParameters::CONGESTION_AVOIDANCE
);
164 cached_network_params
.set_timestamp(
165 connection()->clock()->WallNow().ToUNIXSeconds());
166 if (!serving_region_
.empty()) {
167 cached_network_params
.set_serving_region(serving_region_
);
170 crypto_stream_
->SendServerConfigUpdate(&cached_network_params
);
172 connection()->OnSendConnectionState(cached_network_params
);
174 last_scup_time_
= now
;
175 last_scup_sequence_number_
=
176 connection()->sequence_number_of_last_sent_packet();
179 bool QuicServerSession::ShouldCreateIncomingDataStream(QuicStreamId id
) {
180 if (!connection()->connected()) {
181 LOG(DFATAL
) << "ShouldCreateIncomingDataStream called when disconnected";
186 DVLOG(1) << "Invalid incoming even stream_id:" << id
;
187 connection()->SendConnectionClose(QUIC_INVALID_STREAM_ID
);
190 if (GetNumOpenStreams() >= get_max_open_streams()) {
191 DVLOG(1) << "Failed to create a new incoming stream with id:" << id
192 << " Already " << GetNumOpenStreams() << " streams open (max "
193 << get_max_open_streams() << ").";
194 connection()->SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS
);
200 QuicDataStream
* QuicServerSession::CreateIncomingDataStream(
202 if (!ShouldCreateIncomingDataStream(id
)) {
206 return new QuicSpdyServerStream(id
, this);
209 QuicDataStream
* QuicServerSession::CreateOutgoingDataStream() {
210 DLOG(ERROR
) << "Server push not yet supported";
214 QuicCryptoServerStream
* QuicServerSession::GetCryptoStream() {
215 return crypto_stream_
.get();