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"
16 QuicServerSession::QuicServerSession(const QuicConfig
& config
,
17 QuicConnection
* connection
,
18 QuicServerSessionVisitor
* visitor
)
19 : QuicSession(connection
, config
),
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()) {
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 if (FLAGS_quic_enable_bandwidth_resumption_experiment
&&
51 cached_network_params
!= nullptr &&
52 ContainsQuicTag(config()->ReceivedConnectionOptions(), kBWRE
) &&
53 cached_network_params
->serving_region() == serving_region_
) {
54 connection()->ResumeConnectionState(*cached_network_params
);
57 if (FLAGS_enable_quic_fec
&&
58 ContainsQuicTag(config()->ReceivedConnectionOptions(), kFHDR
)) {
59 // kFHDR config maps to FEC protection always for headers stream.
60 // TODO(jri): Add crypto stream in addition to headers for kHDR.
61 headers_stream_
->set_fec_policy(FEC_PROTECT_ALWAYS
);
65 void QuicServerSession::OnConnectionClosed(QuicErrorCode error
,
67 QuicSession::OnConnectionClosed(error
, from_peer
);
68 // In the unlikely event we get a connection close while doing an asynchronous
69 // crypto event, make sure we cancel the callback.
70 if (crypto_stream_
.get() != nullptr) {
71 crypto_stream_
->CancelOutstandingCallbacks();
73 visitor_
->OnConnectionClosed(connection()->connection_id(), error
);
76 void QuicServerSession::OnWriteBlocked() {
77 QuicSession::OnWriteBlocked();
78 visitor_
->OnWriteBlocked(connection());
81 void QuicServerSession::OnCongestionWindowChange(QuicTime now
) {
82 // Only send updates when the application has no data to write.
83 if (HasDataToWrite()) {
87 // If not enough time has passed since the last time we sent an update to the
88 // client, or not enough packets have been sent, then return early.
89 const QuicSentPacketManager
& sent_packet_manager
=
90 connection()->sent_packet_manager();
92 sent_packet_manager
.GetRttStats()->smoothed_rtt().ToMilliseconds();
93 int64 now_ms
= now
.Subtract(last_scup_time_
).ToMilliseconds();
94 int64 packets_since_last_scup
=
95 connection()->sequence_number_of_last_sent_packet() -
96 last_scup_sequence_number_
;
97 if (now_ms
< (kMinIntervalBetweenServerConfigUpdatesRTTs
* srtt_ms
) ||
98 now_ms
< kMinIntervalBetweenServerConfigUpdatesMs
||
99 packets_since_last_scup
< kMinPacketsBetweenServerConfigUpdates
) {
103 // If the bandwidth recorder does not have a valid estimate, return early.
104 const QuicSustainedBandwidthRecorder
& bandwidth_recorder
=
105 sent_packet_manager
.SustainedBandwidthRecorder();
106 if (!bandwidth_recorder
.HasEstimate()) {
110 // The bandwidth recorder has recorded at least one sustained bandwidth
111 // estimate. Check that it's substantially different from the last one that
112 // we sent to the client, and if so, send the new one.
113 QuicBandwidth new_bandwidth_estimate
= bandwidth_recorder
.BandwidthEstimate();
115 int64 bandwidth_delta
=
116 std::abs(new_bandwidth_estimate
.ToBitsPerSecond() -
117 bandwidth_estimate_sent_to_client_
.ToBitsPerSecond());
119 // Define "substantial" difference as a 50% increase or decrease from the
121 bool substantial_difference
=
123 0.5 * bandwidth_estimate_sent_to_client_
.ToBitsPerSecond();
124 if (!substantial_difference
) {
128 bandwidth_estimate_sent_to_client_
= new_bandwidth_estimate
;
129 DVLOG(1) << "Server: sending new bandwidth estimate (KBytes/s): "
130 << bandwidth_estimate_sent_to_client_
.ToKBytesPerSecond();
132 // Include max bandwidth in the update.
133 QuicBandwidth max_bandwidth_estimate
=
134 bandwidth_recorder
.MaxBandwidthEstimate();
135 int32 max_bandwidth_timestamp
= bandwidth_recorder
.MaxBandwidthTimestamp();
137 // Fill the proto before passing it to the crypto stream to send.
138 CachedNetworkParameters cached_network_params
;
139 cached_network_params
.set_bandwidth_estimate_bytes_per_second(
140 bandwidth_estimate_sent_to_client_
.ToBytesPerSecond());
141 cached_network_params
.set_max_bandwidth_estimate_bytes_per_second(
142 max_bandwidth_estimate
.ToBytesPerSecond());
143 cached_network_params
.set_max_bandwidth_timestamp_seconds(
144 max_bandwidth_timestamp
);
145 cached_network_params
.set_min_rtt_ms(
146 sent_packet_manager
.GetRttStats()->min_rtt().ToMilliseconds());
147 cached_network_params
.set_previous_connection_state(
148 bandwidth_recorder
.EstimateRecordedDuringSlowStart()
149 ? CachedNetworkParameters::SLOW_START
150 : CachedNetworkParameters::CONGESTION_AVOIDANCE
);
151 cached_network_params
.set_timestamp(
152 connection()->clock()->WallNow().ToUNIXSeconds());
153 if (!serving_region_
.empty()) {
154 cached_network_params
.set_serving_region(serving_region_
);
157 crypto_stream_
->SendServerConfigUpdate(&cached_network_params
);
158 last_scup_time_
= now
;
159 last_scup_sequence_number_
=
160 connection()->sequence_number_of_last_sent_packet();
163 bool QuicServerSession::ShouldCreateIncomingDataStream(QuicStreamId id
) {
165 DVLOG(1) << "Invalid incoming even stream_id:" << id
;
166 connection()->SendConnectionClose(QUIC_INVALID_STREAM_ID
);
169 if (GetNumOpenStreams() >= get_max_open_streams()) {
170 DVLOG(1) << "Failed to create a new incoming stream with id:" << id
171 << " Already " << GetNumOpenStreams() << " streams open (max "
172 << get_max_open_streams() << ").";
173 connection()->SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS
);
179 QuicDataStream
* QuicServerSession::CreateIncomingDataStream(
181 if (!ShouldCreateIncomingDataStream(id
)) {
185 return new QuicSpdyServerStream(id
, this);
188 QuicDataStream
* QuicServerSession::CreateOutgoingDataStream() {
189 DLOG(ERROR
) << "Server push not yet supported";
193 QuicCryptoServerStream
* QuicServerSession::GetCryptoStream() {
194 return crypto_stream_
.get();