Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / quic / quic_server_session.cc
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.
4
5 #include "net/quic/quic_server_session.h"
6
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"
13
14 namespace net {
15
16 QuicServerSession::QuicServerSession(const QuicConfig& config,
17                                      QuicConnection* connection,
18                                      QuicServerSessionVisitor* visitor,
19                                      bool is_secure)
20     : QuicSession(connection, config, is_secure),
21       visitor_(visitor),
22       bandwidth_estimate_sent_to_client_(QuicBandwidth::Zero()),
23       last_scup_time_(QuicTime::Zero()),
24       last_scup_sequence_number_(0) {}
25
26 QuicServerSession::~QuicServerSession() {}
27
28 void QuicServerSession::InitializeSession(
29     const QuicCryptoServerConfig& crypto_config) {
30   QuicSession::InitializeSession();
31   crypto_stream_.reset(CreateQuicCryptoServerStream(crypto_config));
32 }
33
34 QuicCryptoServerStream* QuicServerSession::CreateQuicCryptoServerStream(
35     const QuicCryptoServerConfig& crypto_config) {
36   return new QuicCryptoServerStream(crypto_config, this);
37 }
38
39 void QuicServerSession::OnConfigNegotiated() {
40   QuicSession::OnConfigNegotiated();
41   if (!FLAGS_enable_quic_fec ||
42       !config()->HasReceivedConnectionOptions() ||
43       !ContainsQuicTag(config()->ReceivedConnectionOptions(), kFHDR)) {
44     return;
45   }
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);
49 }
50
51 void QuicServerSession::OnConnectionClosed(QuicErrorCode error,
52                                            bool from_peer) {
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();
58   }
59   visitor_->OnConnectionClosed(connection()->connection_id(), error);
60 }
61
62 void QuicServerSession::OnWriteBlocked() {
63   QuicSession::OnWriteBlocked();
64   visitor_->OnWriteBlocked(connection());
65 }
66
67 void QuicServerSession::OnCongestionWindowChange(QuicTime now) {
68   if (connection()->version() <= QUIC_VERSION_21) {
69     return;
70   }
71
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();
76   int64 srtt_ms =
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) {
85     return;
86   }
87
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()) {
92     return;
93   }
94
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();
99
100   int64 bandwidth_delta =
101       std::abs(new_bandwidth_estimate.ToBitsPerSecond() -
102                bandwidth_estimate_sent_to_client_.ToBitsPerSecond());
103
104   // Define "substantial" difference as a 50% increase or decrease from the
105   // last estimate.
106   bool substantial_difference =
107       bandwidth_delta >
108       0.5 * bandwidth_estimate_sent_to_client_.ToBitsPerSecond();
109   if (!substantial_difference) {
110     return;
111   }
112
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();
116
117   // Include max bandwidth in the update.
118   QuicBandwidth max_bandwidth_estimate =
119       bandwidth_recorder.MaxBandwidthEstimate();
120   int32 max_bandwidth_timestamp = bandwidth_recorder.MaxBandwidthTimestamp();
121
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_);
140   }
141
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();
146 }
147
148 bool QuicServerSession::ShouldCreateIncomingDataStream(QuicStreamId id) {
149   if (id % 2 == 0) {
150     DVLOG(1) << "Invalid incoming even stream_id:" << id;
151     connection()->SendConnectionClose(QUIC_INVALID_STREAM_ID);
152     return false;
153   }
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);
159     return false;
160   }
161   return true;
162 }
163
164 QuicDataStream* QuicServerSession::CreateIncomingDataStream(
165     QuicStreamId id) {
166   if (!ShouldCreateIncomingDataStream(id)) {
167     return nullptr;
168   }
169
170   return new QuicSpdyServerStream(id, this);
171 }
172
173 QuicDataStream* QuicServerSession::CreateOutgoingDataStream() {
174   DLOG(ERROR) << "Server push not yet supported";
175   return nullptr;
176 }
177
178 QuicCryptoServerStream* QuicServerSession::GetCryptoStream() {
179   return crypto_stream_.get();
180 }
181
182 }  // namespace net