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,
20 : QuicSession(connection, config, is_secure),
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();
41 if (!FLAGS_enable_quic_fec ||
42 !config()->HasReceivedConnectionOptions() ||
43 !ContainsQuicTag(config()->ReceivedConnectionOptions(), kFHDR)) {
46 // kFHDR config maps to FEC protection always for headers stream.
47 // TODO(jri): Add crypto stream in addition to headers for kHDR.
48 headers_stream_->set_fec_policy(FEC_PROTECT_ALWAYS);
51 void QuicServerSession::OnConnectionClosed(QuicErrorCode error,
53 QuicSession::OnConnectionClosed(error, from_peer);
54 // In the unlikely event we get a connection close while doing an asynchronous
55 // crypto event, make sure we cancel the callback.
56 if (crypto_stream_.get() != nullptr) {
57 crypto_stream_->CancelOutstandingCallbacks();
59 visitor_->OnConnectionClosed(connection()->connection_id(), error);
62 void QuicServerSession::OnWriteBlocked() {
63 QuicSession::OnWriteBlocked();
64 visitor_->OnWriteBlocked(connection());
67 void QuicServerSession::OnCongestionWindowChange(QuicTime now) {
68 if (connection()->version() <= QUIC_VERSION_21) {
72 // If not enough time has passed since the last time we sent an update to the
73 // client, or not enough packets have been sent, then return early.
74 const QuicSentPacketManager& sent_packet_manager =
75 connection()->sent_packet_manager();
77 sent_packet_manager.GetRttStats()->smoothed_rtt().ToMilliseconds();
78 int64 now_ms = now.Subtract(last_scup_time_).ToMilliseconds();
79 int64 packets_since_last_scup =
80 connection()->sequence_number_of_last_sent_packet() -
81 last_scup_sequence_number_;
82 if (now_ms < (kMinIntervalBetweenServerConfigUpdatesRTTs * srtt_ms) ||
83 now_ms < kMinIntervalBetweenServerConfigUpdatesMs ||
84 packets_since_last_scup < kMinPacketsBetweenServerConfigUpdates) {
88 // If the bandwidth recorder does not have a valid estimate, return early.
89 const QuicSustainedBandwidthRecorder& bandwidth_recorder =
90 sent_packet_manager.SustainedBandwidthRecorder();
91 if (!bandwidth_recorder.HasEstimate()) {
95 // The bandwidth recorder has recorded at least one sustained bandwidth
96 // estimate. Check that it's substantially different from the last one that
97 // we sent to the client, and if so, send the new one.
98 QuicBandwidth new_bandwidth_estimate = bandwidth_recorder.BandwidthEstimate();
100 int64 bandwidth_delta =
101 std::abs(new_bandwidth_estimate.ToBitsPerSecond() -
102 bandwidth_estimate_sent_to_client_.ToBitsPerSecond());
104 // Define "substantial" difference as a 50% increase or decrease from the
106 bool substantial_difference =
108 0.5 * bandwidth_estimate_sent_to_client_.ToBitsPerSecond();
109 if (!substantial_difference) {
113 bandwidth_estimate_sent_to_client_ = new_bandwidth_estimate;
114 DVLOG(1) << "Server: sending new bandwidth estimate (KBytes/s): "
115 << bandwidth_estimate_sent_to_client_.ToKBytesPerSecond();
117 // Include max bandwidth in the update.
118 QuicBandwidth max_bandwidth_estimate =
119 bandwidth_recorder.MaxBandwidthEstimate();
120 int32 max_bandwidth_timestamp = bandwidth_recorder.MaxBandwidthTimestamp();
122 // Fill the proto before passing it to the crypto stream to send.
123 CachedNetworkParameters cached_network_params;
124 cached_network_params.set_bandwidth_estimate_bytes_per_second(
125 bandwidth_estimate_sent_to_client_.ToBytesPerSecond());
126 cached_network_params.set_max_bandwidth_estimate_bytes_per_second(
127 max_bandwidth_estimate.ToBytesPerSecond());
128 cached_network_params.set_max_bandwidth_timestamp_seconds(
129 max_bandwidth_timestamp);
130 cached_network_params.set_min_rtt_ms(
131 sent_packet_manager.GetRttStats()->min_rtt().ToMilliseconds());
132 cached_network_params.set_previous_connection_state(
133 bandwidth_recorder.EstimateRecordedDuringSlowStart()
134 ? CachedNetworkParameters::SLOW_START
135 : CachedNetworkParameters::CONGESTION_AVOIDANCE);
136 cached_network_params.set_timestamp(
137 connection()->clock()->WallNow().ToUNIXSeconds());
138 if (!serving_region_.empty()) {
139 cached_network_params.set_serving_region(serving_region_);
142 crypto_stream_->SendServerConfigUpdate(&cached_network_params);
143 last_scup_time_ = now;
144 last_scup_sequence_number_ =
145 connection()->sequence_number_of_last_sent_packet();
148 bool QuicServerSession::ShouldCreateIncomingDataStream(QuicStreamId id) {
150 DVLOG(1) << "Invalid incoming even stream_id:" << id;
151 connection()->SendConnectionClose(QUIC_INVALID_STREAM_ID);
154 if (GetNumOpenStreams() >= get_max_open_streams()) {
155 DVLOG(1) << "Failed to create a new incoming stream with id:" << id
156 << " Already " << GetNumOpenStreams() << " streams open (max "
157 << get_max_open_streams() << ").";
158 connection()->SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS);
164 QuicDataStream* QuicServerSession::CreateIncomingDataStream(
166 if (!ShouldCreateIncomingDataStream(id)) {
170 return new QuicSpdyServerStream(id, this);
173 QuicDataStream* QuicServerSession::CreateOutgoingDataStream() {
174 DLOG(ERROR) << "Server push not yet supported";
178 QuicCryptoServerStream* QuicServerSession::GetCryptoStream() {
179 return crypto_stream_.get();