OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "net/tools/quic/quic_server_session.h" | |
6 | |
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/reliable_quic_stream.h" | |
12 #include "net/tools/quic/quic_spdy_server_stream.h" | |
13 | |
14 namespace net { | |
15 namespace tools { | |
16 | |
17 QuicServerSession::QuicServerSession(const QuicConfig& config, | |
18 QuicConnection* connection, | |
19 QuicServerSessionVisitor* visitor) | |
20 : QuicSession(connection, config), | |
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 | |
42 if (!config()->HasReceivedConnectionOptions()) { | |
43 return; | |
44 } | |
45 | |
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 if (FLAGS_quic_enable_bandwidth_resumption_experiment && | |
52 cached_network_params != nullptr && | |
53 ContainsQuicTag(config()->ReceivedConnectionOptions(), kBWRE) && | |
54 cached_network_params->serving_region() == serving_region_) { | |
55 connection()->ResumeConnectionState(*cached_network_params); | |
56 } | |
57 | |
58 if (FLAGS_enable_quic_fec && | |
59 ContainsQuicTag(config()->ReceivedConnectionOptions(), kFHDR)) { | |
60 // kFHDR config maps to FEC protection always for headers stream. | |
61 // TODO(jri): Add crypto stream in addition to headers for kHDR. | |
62 headers_stream_->set_fec_policy(FEC_PROTECT_ALWAYS); | |
63 } | |
64 } | |
65 | |
66 void QuicServerSession::OnConnectionClosed(QuicErrorCode error, | |
67 bool from_peer) { | |
68 QuicSession::OnConnectionClosed(error, from_peer); | |
69 // In the unlikely event we get a connection close while doing an asynchronous | |
70 // crypto event, make sure we cancel the callback. | |
71 if (crypto_stream_.get() != nullptr) { | |
72 crypto_stream_->CancelOutstandingCallbacks(); | |
73 } | |
74 visitor_->OnConnectionClosed(connection()->connection_id(), error); | |
75 } | |
76 | |
77 void QuicServerSession::OnWriteBlocked() { | |
78 QuicSession::OnWriteBlocked(); | |
79 visitor_->OnWriteBlocked(connection()); | |
80 } | |
81 | |
82 void QuicServerSession::OnCongestionWindowChange(QuicTime now) { | |
83 // Only send updates when the application has no data to write. | |
84 if (HasDataToWrite()) { | |
85 return; | |
86 } | |
87 | |
88 // If not enough time has passed since the last time we sent an update to the | |
89 // client, or not enough packets have been sent, then return early. | |
90 const QuicSentPacketManager& sent_packet_manager = | |
91 connection()->sent_packet_manager(); | |
92 int64 srtt_ms = | |
93 sent_packet_manager.GetRttStats()->smoothed_rtt().ToMilliseconds(); | |
94 int64 now_ms = now.Subtract(last_scup_time_).ToMilliseconds(); | |
95 int64 packets_since_last_scup = | |
96 connection()->sequence_number_of_last_sent_packet() - | |
97 last_scup_sequence_number_; | |
98 if (now_ms < (kMinIntervalBetweenServerConfigUpdatesRTTs * srtt_ms) || | |
99 now_ms < kMinIntervalBetweenServerConfigUpdatesMs || | |
100 packets_since_last_scup < kMinPacketsBetweenServerConfigUpdates) { | |
101 return; | |
102 } | |
103 | |
104 // If the bandwidth recorder does not have a valid estimate, return early. | |
105 const QuicSustainedBandwidthRecorder& bandwidth_recorder = | |
106 sent_packet_manager.SustainedBandwidthRecorder(); | |
107 if (!bandwidth_recorder.HasEstimate()) { | |
108 return; | |
109 } | |
110 | |
111 // The bandwidth recorder has recorded at least one sustained bandwidth | |
112 // estimate. Check that it's substantially different from the last one that | |
113 // we sent to the client, and if so, send the new one. | |
114 QuicBandwidth new_bandwidth_estimate = bandwidth_recorder.BandwidthEstimate(); | |
115 | |
116 int64 bandwidth_delta = | |
117 std::abs(new_bandwidth_estimate.ToBitsPerSecond() - | |
118 bandwidth_estimate_sent_to_client_.ToBitsPerSecond()); | |
119 | |
120 // Define "substantial" difference as a 50% increase or decrease from the | |
121 // last estimate. | |
122 bool substantial_difference = | |
123 bandwidth_delta > | |
124 0.5 * bandwidth_estimate_sent_to_client_.ToBitsPerSecond(); | |
125 if (!substantial_difference) { | |
126 return; | |
127 } | |
128 | |
129 bandwidth_estimate_sent_to_client_ = new_bandwidth_estimate; | |
130 DVLOG(1) << "Server: sending new bandwidth estimate (KBytes/s): " | |
131 << bandwidth_estimate_sent_to_client_.ToKBytesPerSecond(); | |
132 | |
133 // Include max bandwidth in the update. | |
134 QuicBandwidth max_bandwidth_estimate = | |
135 bandwidth_recorder.MaxBandwidthEstimate(); | |
136 int32 max_bandwidth_timestamp = bandwidth_recorder.MaxBandwidthTimestamp(); | |
137 | |
138 // Fill the proto before passing it to the crypto stream to send. | |
139 CachedNetworkParameters cached_network_params; | |
140 cached_network_params.set_bandwidth_estimate_bytes_per_second( | |
141 bandwidth_estimate_sent_to_client_.ToBytesPerSecond()); | |
142 cached_network_params.set_max_bandwidth_estimate_bytes_per_second( | |
143 max_bandwidth_estimate.ToBytesPerSecond()); | |
144 cached_network_params.set_max_bandwidth_timestamp_seconds( | |
145 max_bandwidth_timestamp); | |
146 cached_network_params.set_min_rtt_ms( | |
147 sent_packet_manager.GetRttStats()->min_rtt().ToMilliseconds()); | |
148 cached_network_params.set_previous_connection_state( | |
149 bandwidth_recorder.EstimateRecordedDuringSlowStart() | |
150 ? CachedNetworkParameters::SLOW_START | |
151 : CachedNetworkParameters::CONGESTION_AVOIDANCE); | |
152 cached_network_params.set_timestamp( | |
153 connection()->clock()->WallNow().ToUNIXSeconds()); | |
154 if (!serving_region_.empty()) { | |
155 cached_network_params.set_serving_region(serving_region_); | |
156 } | |
157 | |
158 crypto_stream_->SendServerConfigUpdate(&cached_network_params); | |
159 last_scup_time_ = now; | |
160 last_scup_sequence_number_ = | |
161 connection()->sequence_number_of_last_sent_packet(); | |
162 } | |
163 | |
164 bool QuicServerSession::ShouldCreateIncomingDataStream(QuicStreamId id) { | |
165 if (id % 2 == 0) { | |
166 DVLOG(1) << "Invalid incoming even stream_id:" << id; | |
167 connection()->SendConnectionClose(QUIC_INVALID_STREAM_ID); | |
168 return false; | |
169 } | |
170 if (GetNumOpenStreams() >= get_max_open_streams()) { | |
171 DVLOG(1) << "Failed to create a new incoming stream with id:" << id | |
172 << " Already " << GetNumOpenStreams() << " streams open (max " | |
173 << get_max_open_streams() << ")."; | |
174 connection()->SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS); | |
175 return false; | |
176 } | |
177 return true; | |
178 } | |
179 | |
180 QuicDataStream* QuicServerSession::CreateIncomingDataStream( | |
181 QuicStreamId id) { | |
182 if (!ShouldCreateIncomingDataStream(id)) { | |
183 return nullptr; | |
184 } | |
185 | |
186 return new QuicSpdyServerStream(id, this); | |
187 } | |
188 | |
189 QuicDataStream* QuicServerSession::CreateOutgoingDataStream() { | |
190 DLOG(ERROR) << "Server push not yet supported"; | |
191 return nullptr; | |
192 } | |
193 | |
194 QuicCryptoServerStream* QuicServerSession::GetCryptoStream() { | |
195 return crypto_stream_.get(); | |
196 } | |
197 | |
198 } // namespace tools | |
199 } // namespace net | |
OLD | NEW |