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(const QuicConfig
& config
,
18 QuicConnection
* connection
,
19 QuicServerSessionVisitor
* visitor
)
20 : QuicSession(connection
, config
),
22 bandwidth_estimate_sent_to_client_(QuicBandwidth::Zero()),
23 last_scup_time_(QuicTime::Zero()),
24 last_scup_sequence_number_(0) {}
26 QuicServerSession::~QuicServerSession() {}
28 void QuicServerSession::InitializeSession(
29 const QuicCryptoServerConfig
* crypto_config
) {
30 QuicSession::InitializeSession();
31 crypto_stream_
.reset(CreateQuicCryptoServerStream(crypto_config
));
34 QuicCryptoServerStream
* QuicServerSession::CreateQuicCryptoServerStream(
35 const QuicCryptoServerConfig
* crypto_config
) {
36 return new QuicCryptoServerStream(crypto_config
, this);
39 void QuicServerSession::OnConfigNegotiated() {
40 QuicSession::OnConfigNegotiated();
42 if (!config()->HasReceivedConnectionOptions()) {
46 // If the client has provided a bandwidth estimate from the same serving
47 // region, then pass it to the sent packet manager in preparation for possible
48 // bandwidth resumption.
49 const CachedNetworkParameters
* cached_network_params
=
50 crypto_stream_
->previous_cached_network_params();
51 const bool max_bandwidth_resumption
=
52 ContainsQuicTag(config()->ReceivedConnectionOptions(), kBWMX
);
53 if (cached_network_params
!= nullptr &&
54 (ContainsQuicTag(config()->ReceivedConnectionOptions(), kBWRE
) ||
55 max_bandwidth_resumption
) &&
56 cached_network_params
->serving_region() == serving_region_
) {
57 connection()->ResumeConnectionState(*cached_network_params
,
58 max_bandwidth_resumption
);
61 if (FLAGS_enable_quic_fec
&&
62 ContainsQuicTag(config()->ReceivedConnectionOptions(), kFHDR
)) {
63 // kFHDR config maps to FEC protection always for headers stream.
64 // TODO(jri): Add crypto stream in addition to headers for kHDR.
65 headers_stream_
->set_fec_policy(FEC_PROTECT_ALWAYS
);
69 void QuicServerSession::OnConnectionClosed(QuicErrorCode error
,
71 QuicSession::OnConnectionClosed(error
, from_peer
);
72 // In the unlikely event we get a connection close while doing an asynchronous
73 // crypto event, make sure we cancel the callback.
74 if (crypto_stream_
.get() != nullptr) {
75 crypto_stream_
->CancelOutstandingCallbacks();
77 visitor_
->OnConnectionClosed(connection()->connection_id(), error
);
80 void QuicServerSession::OnWriteBlocked() {
81 QuicSession::OnWriteBlocked();
82 visitor_
->OnWriteBlocked(connection());
85 void QuicServerSession::OnCongestionWindowChange(QuicTime now
) {
86 // Only send updates when the application has no data to write.
87 if (HasDataToWrite()) {
91 // If not enough time has passed since the last time we sent an update to the
92 // client, or not enough packets have been sent, then return early.
93 const QuicSentPacketManager
& sent_packet_manager
=
94 connection()->sent_packet_manager();
96 sent_packet_manager
.GetRttStats()->smoothed_rtt().ToMilliseconds();
97 int64 now_ms
= now
.Subtract(last_scup_time_
).ToMilliseconds();
98 int64 packets_since_last_scup
=
99 connection()->sequence_number_of_last_sent_packet() -
100 last_scup_sequence_number_
;
101 if (now_ms
< (kMinIntervalBetweenServerConfigUpdatesRTTs
* srtt_ms
) ||
102 now_ms
< kMinIntervalBetweenServerConfigUpdatesMs
||
103 packets_since_last_scup
< kMinPacketsBetweenServerConfigUpdates
) {
107 // If the bandwidth recorder does not have a valid estimate, return early.
108 const QuicSustainedBandwidthRecorder
& bandwidth_recorder
=
109 sent_packet_manager
.SustainedBandwidthRecorder();
110 if (!bandwidth_recorder
.HasEstimate()) {
114 // The bandwidth recorder has recorded at least one sustained bandwidth
115 // estimate. Check that it's substantially different from the last one that
116 // we sent to the client, and if so, send the new one.
117 QuicBandwidth new_bandwidth_estimate
= bandwidth_recorder
.BandwidthEstimate();
119 int64 bandwidth_delta
=
120 std::abs(new_bandwidth_estimate
.ToBitsPerSecond() -
121 bandwidth_estimate_sent_to_client_
.ToBitsPerSecond());
123 // Define "substantial" difference as a 50% increase or decrease from the
125 bool substantial_difference
=
127 0.5 * bandwidth_estimate_sent_to_client_
.ToBitsPerSecond();
128 if (!substantial_difference
) {
132 bandwidth_estimate_sent_to_client_
= new_bandwidth_estimate
;
133 DVLOG(1) << "Server: sending new bandwidth estimate (KBytes/s): "
134 << bandwidth_estimate_sent_to_client_
.ToKBytesPerSecond();
136 // Include max bandwidth in the update.
137 QuicBandwidth max_bandwidth_estimate
=
138 bandwidth_recorder
.MaxBandwidthEstimate();
139 int32 max_bandwidth_timestamp
= bandwidth_recorder
.MaxBandwidthTimestamp();
141 // Fill the proto before passing it to the crypto stream to send.
142 CachedNetworkParameters cached_network_params
;
143 cached_network_params
.set_bandwidth_estimate_bytes_per_second(
144 bandwidth_estimate_sent_to_client_
.ToBytesPerSecond());
145 cached_network_params
.set_max_bandwidth_estimate_bytes_per_second(
146 max_bandwidth_estimate
.ToBytesPerSecond());
147 cached_network_params
.set_max_bandwidth_timestamp_seconds(
148 max_bandwidth_timestamp
);
149 cached_network_params
.set_min_rtt_ms(
150 sent_packet_manager
.GetRttStats()->min_rtt().ToMilliseconds());
151 cached_network_params
.set_previous_connection_state(
152 bandwidth_recorder
.EstimateRecordedDuringSlowStart()
153 ? CachedNetworkParameters::SLOW_START
154 : CachedNetworkParameters::CONGESTION_AVOIDANCE
);
155 cached_network_params
.set_timestamp(
156 connection()->clock()->WallNow().ToUNIXSeconds());
157 if (!serving_region_
.empty()) {
158 cached_network_params
.set_serving_region(serving_region_
);
161 crypto_stream_
->SendServerConfigUpdate(&cached_network_params
);
163 connection()->OnSendConnectionState(cached_network_params
);
165 last_scup_time_
= now
;
166 last_scup_sequence_number_
=
167 connection()->sequence_number_of_last_sent_packet();
170 bool QuicServerSession::ShouldCreateIncomingDataStream(QuicStreamId id
) {
172 DVLOG(1) << "Invalid incoming even stream_id:" << id
;
173 connection()->SendConnectionClose(QUIC_INVALID_STREAM_ID
);
176 if (GetNumOpenStreams() >= get_max_open_streams()) {
177 DVLOG(1) << "Failed to create a new incoming stream with id:" << id
178 << " Already " << GetNumOpenStreams() << " streams open (max "
179 << get_max_open_streams() << ").";
180 connection()->SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS
);
186 QuicDataStream
* QuicServerSession::CreateIncomingDataStream(
188 if (!ShouldCreateIncomingDataStream(id
)) {
192 return new QuicSpdyServerStream(id
, this);
195 QuicDataStream
* QuicServerSession::CreateOutgoingDataStream() {
196 DLOG(ERROR
) << "Server push not yet supported";
200 QuicCryptoServerStream
* QuicServerSession::GetCryptoStream() {
201 return crypto_stream_
.get();