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/source_address_token.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_server_config_update_time_(QuicTime::Zero()) {}
24 QuicServerSession::~QuicServerSession() {}
26 void QuicServerSession::InitializeSession(
27 const QuicCryptoServerConfig
& crypto_config
) {
28 QuicSession::InitializeSession();
29 crypto_stream_
.reset(CreateQuicCryptoServerStream(crypto_config
));
32 QuicCryptoServerStream
* QuicServerSession::CreateQuicCryptoServerStream(
33 const QuicCryptoServerConfig
& crypto_config
) {
34 return new QuicCryptoServerStream(crypto_config
, this);
37 void QuicServerSession::OnConfigNegotiated() {
38 QuicSession::OnConfigNegotiated();
39 if (!FLAGS_enable_quic_fec
||
40 !config()->HasReceivedConnectionOptions() ||
41 !ContainsQuicTag(config()->ReceivedConnectionOptions(), kFHDR
)) {
44 // kFHDR config maps to FEC protection always for headers stream.
45 // TODO(jri): Add crypto stream in addition to headers for kHDR.
46 headers_stream_
->set_fec_policy(FEC_PROTECT_ALWAYS
);
49 void QuicServerSession::OnConnectionClosed(QuicErrorCode error
,
51 QuicSession::OnConnectionClosed(error
, from_peer
);
52 // In the unlikely event we get a connection close while doing an asynchronous
53 // crypto event, make sure we cancel the callback.
54 if (crypto_stream_
.get() != nullptr) {
55 crypto_stream_
->CancelOutstandingCallbacks();
57 visitor_
->OnConnectionClosed(connection()->connection_id(), error
);
60 void QuicServerSession::OnWriteBlocked() {
61 QuicSession::OnWriteBlocked();
62 visitor_
->OnWriteBlocked(connection());
65 void QuicServerSession::OnCongestionWindowChange(QuicTime now
) {
66 if (connection()->version() <= QUIC_VERSION_21
) {
70 // If not enough time has passed since the last time we sent an update to the
71 // client, then return early.
72 const QuicSentPacketManager
& sent_packet_manager
=
73 connection()->sent_packet_manager();
75 sent_packet_manager
.GetRttStats()->SmoothedRtt().ToMilliseconds();
76 int64 now_ms
= now
.Subtract(last_server_config_update_time_
).ToMilliseconds();
77 if (now_ms
< (kMinIntervalBetweenServerConfigUpdatesRTTs
* srtt_ms
) ||
78 now_ms
< kMinIntervalBetweenServerConfigUpdatesMs
) {
82 // If the bandwidth recorder does not have a valid estimate, return early.
83 const QuicSustainedBandwidthRecorder
& bandwidth_recorder
=
84 sent_packet_manager
.SustainedBandwidthRecorder();
85 if (!bandwidth_recorder
.HasEstimate()) {
89 // The bandwidth recorder has recorded at least one sustained bandwidth
90 // estimate. Check that it's substantially different from the last one that
91 // we sent to the client, and if so, send the new one.
92 QuicBandwidth new_bandwidth_estimate
= bandwidth_recorder
.BandwidthEstimate();
94 int64 bandwidth_delta
=
95 std::abs(new_bandwidth_estimate
.ToBitsPerSecond() -
96 bandwidth_estimate_sent_to_client_
.ToBitsPerSecond());
98 // Define "substantial" difference as a 50% increase or decrease from the
100 bool substantial_difference
=
102 0.5 * bandwidth_estimate_sent_to_client_
.ToBitsPerSecond();
103 if (!substantial_difference
) {
107 bandwidth_estimate_sent_to_client_
= new_bandwidth_estimate
;
108 DVLOG(1) << "Server: sending new bandwidth estimate (KBytes/s): "
109 << bandwidth_estimate_sent_to_client_
.ToKBytesPerSecond();
111 // Include max bandwidth in the update.
112 QuicBandwidth max_bandwidth_estimate
=
113 bandwidth_recorder
.MaxBandwidthEstimate();
114 int32 max_bandwidth_timestamp
= bandwidth_recorder
.MaxBandwidthTimestamp();
116 // Fill the proto before passing it to the crypto stream to send.
117 CachedNetworkParameters cached_network_params
;
118 cached_network_params
.set_bandwidth_estimate_bytes_per_second(
119 bandwidth_estimate_sent_to_client_
.ToBytesPerSecond());
120 cached_network_params
.set_max_bandwidth_estimate_bytes_per_second(
121 max_bandwidth_estimate
.ToBytesPerSecond());
122 cached_network_params
.set_max_bandwidth_timestamp_seconds(
123 max_bandwidth_timestamp
);
124 cached_network_params
.set_min_rtt_ms(
125 sent_packet_manager
.GetRttStats()->MinRtt().ToMilliseconds());
126 cached_network_params
.set_previous_connection_state(
127 bandwidth_recorder
.EstimateRecordedDuringSlowStart()
128 ? CachedNetworkParameters::SLOW_START
129 : CachedNetworkParameters::CONGESTION_AVOIDANCE
);
130 cached_network_params
.set_timestamp(
131 connection()->clock()->WallNow().ToUNIXSeconds());
132 if (!serving_region_
.empty()) {
133 cached_network_params
.set_serving_region(serving_region_
);
136 crypto_stream_
->SendServerConfigUpdate(&cached_network_params
, false);
137 last_server_config_update_time_
= now
;
140 bool QuicServerSession::ShouldCreateIncomingDataStream(QuicStreamId id
) {
142 DVLOG(1) << "Invalid incoming even stream_id:" << id
;
143 connection()->SendConnectionClose(QUIC_INVALID_STREAM_ID
);
146 if (GetNumOpenStreams() >= get_max_open_streams()) {
147 DVLOG(1) << "Failed to create a new incoming stream with id:" << id
148 << " Already " << GetNumOpenStreams() << " streams open (max "
149 << get_max_open_streams() << ").";
150 connection()->SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS
);
156 QuicDataStream
* QuicServerSession::CreateIncomingDataStream(
158 if (!ShouldCreateIncomingDataStream(id
)) {
162 return new QuicSpdyServerStream(id
, this);
165 QuicDataStream
* QuicServerSession::CreateOutgoingDataStream() {
166 DLOG(ERROR
) << "Server push not yet supported";
170 QuicCryptoServerStream
* QuicServerSession::GetCryptoStream() {
171 return crypto_stream_
.get();