[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / net / quic / quic_client_session.cc
blob0625f08077d30c5c7c62a77e5668a72b8ee1c2d6
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/quic/quic_client_session.h"
7 #include "base/callback_helpers.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/metrics/histogram.h"
10 #include "base/metrics/sparse_histogram.h"
11 #include "base/stl_util.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/values.h"
14 #include "net/base/io_buffer.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/network_activity_monitor.h"
17 #include "net/http/transport_security_state.h"
18 #include "net/quic/crypto/proof_verifier_chromium.h"
19 #include "net/quic/crypto/quic_server_info.h"
20 #include "net/quic/quic_connection_helper.h"
21 #include "net/quic/quic_crypto_client_stream_factory.h"
22 #include "net/quic/quic_server_id.h"
23 #include "net/quic/quic_stream_factory.h"
24 #include "net/spdy/spdy_session.h"
25 #include "net/ssl/channel_id_service.h"
26 #include "net/ssl/ssl_connection_status_flags.h"
27 #include "net/ssl/ssl_info.h"
28 #include "net/udp/datagram_client_socket.h"
30 namespace net {
32 namespace {
34 // The length of time to wait for a 0-RTT handshake to complete
35 // before allowing the requests to possibly proceed over TCP.
36 const int k0RttHandshakeTimeoutMs = 300;
38 // IPv6 packets have an additional 20 bytes of overhead than IPv4 packets.
39 const size_t kAdditionalOverheadForIPv6 = 20;
41 // Histograms for tracking down the crashes from http://crbug.com/354669
42 // Note: these values must be kept in sync with the corresponding values in:
43 // tools/metrics/histograms/histograms.xml
44 enum Location {
45 DESTRUCTOR = 0,
46 ADD_OBSERVER = 1,
47 TRY_CREATE_STREAM = 2,
48 CREATE_OUTGOING_RELIABLE_STREAM = 3,
49 NOTIFY_FACTORY_OF_SESSION_CLOSED_LATER = 4,
50 NOTIFY_FACTORY_OF_SESSION_CLOSED = 5,
51 NUM_LOCATIONS = 6,
54 void RecordUnexpectedOpenStreams(Location location) {
55 UMA_HISTOGRAM_ENUMERATION("Net.QuicSession.UnexpectedOpenStreams", location,
56 NUM_LOCATIONS);
59 void RecordUnexpectedObservers(Location location) {
60 UMA_HISTOGRAM_ENUMERATION("Net.QuicSession.UnexpectedObservers", location,
61 NUM_LOCATIONS);
64 void RecordUnexpectedNotGoingAway(Location location) {
65 UMA_HISTOGRAM_ENUMERATION("Net.QuicSession.UnexpectedNotGoingAway", location,
66 NUM_LOCATIONS);
69 // Histogram for recording the different reasons that a QUIC session is unable
70 // to complete the handshake.
71 enum HandshakeFailureReason {
72 HANDSHAKE_FAILURE_UNKNOWN = 0,
73 HANDSHAKE_FAILURE_BLACK_HOLE = 1,
74 HANDSHAKE_FAILURE_PUBLIC_RESET = 2,
75 NUM_HANDSHAKE_FAILURE_REASONS = 3,
78 void RecordHandshakeFailureReason(HandshakeFailureReason reason) {
79 UMA_HISTOGRAM_ENUMERATION(
80 "Net.QuicSession.ConnectionClose.HandshakeNotConfirmed.Reason",
81 reason, NUM_HANDSHAKE_FAILURE_REASONS);
84 // Note: these values must be kept in sync with the corresponding values in:
85 // tools/metrics/histograms/histograms.xml
86 enum HandshakeState {
87 STATE_STARTED = 0,
88 STATE_ENCRYPTION_ESTABLISHED = 1,
89 STATE_HANDSHAKE_CONFIRMED = 2,
90 STATE_FAILED = 3,
91 NUM_HANDSHAKE_STATES = 4
94 void RecordHandshakeState(HandshakeState state) {
95 UMA_HISTOGRAM_ENUMERATION("Net.QuicHandshakeState", state,
96 NUM_HANDSHAKE_STATES);
99 base::Value* NetLogQuicClientSessionCallback(
100 const QuicServerId* server_id,
101 bool require_confirmation,
102 NetLog::LogLevel /* log_level */) {
103 base::DictionaryValue* dict = new base::DictionaryValue();
104 dict->SetString("host", server_id->host());
105 dict->SetInteger("port", server_id->port());
106 dict->SetBoolean("is_https", server_id->is_https());
107 dict->SetBoolean("privacy_mode",
108 server_id->privacy_mode() == PRIVACY_MODE_ENABLED);
109 dict->SetBoolean("require_confirmation", require_confirmation);
110 return dict;
113 } // namespace
115 QuicClientSession::StreamRequest::StreamRequest() : stream_(nullptr) {}
117 QuicClientSession::StreamRequest::~StreamRequest() {
118 CancelRequest();
121 int QuicClientSession::StreamRequest::StartRequest(
122 const base::WeakPtr<QuicClientSession>& session,
123 QuicReliableClientStream** stream,
124 const CompletionCallback& callback) {
125 session_ = session;
126 stream_ = stream;
127 int rv = session_->TryCreateStream(this, stream_);
128 if (rv == ERR_IO_PENDING) {
129 callback_ = callback;
132 return rv;
135 void QuicClientSession::StreamRequest::CancelRequest() {
136 if (session_)
137 session_->CancelRequest(this);
138 session_.reset();
139 callback_.Reset();
142 void QuicClientSession::StreamRequest::OnRequestCompleteSuccess(
143 QuicReliableClientStream* stream) {
144 session_.reset();
145 *stream_ = stream;
146 ResetAndReturn(&callback_).Run(OK);
149 void QuicClientSession::StreamRequest::OnRequestCompleteFailure(int rv) {
150 session_.reset();
151 ResetAndReturn(&callback_).Run(rv);
154 QuicClientSession::QuicClientSession(
155 QuicConnection* connection,
156 scoped_ptr<DatagramClientSocket> socket,
157 QuicStreamFactory* stream_factory,
158 TransportSecurityState* transport_security_state,
159 scoped_ptr<QuicServerInfo> server_info,
160 const QuicConfig& config,
161 const char* const connection_description,
162 base::TimeTicks dns_resolution_end_time,
163 base::TaskRunner* task_runner,
164 NetLog* net_log)
165 : QuicClientSessionBase(connection, config),
166 require_confirmation_(false),
167 stream_factory_(stream_factory),
168 socket_(socket.Pass()),
169 transport_security_state_(transport_security_state),
170 server_info_(server_info.Pass()),
171 num_total_streams_(0),
172 task_runner_(task_runner),
173 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_QUIC_SESSION)),
174 packet_reader_(socket_.get(), this, net_log_),
175 dns_resolution_end_time_(dns_resolution_end_time),
176 logger_(new QuicConnectionLogger(this, connection_description, net_log_)),
177 going_away_(false),
178 weak_factory_(this) {
179 connection->set_debug_visitor(logger_.get());
180 IPEndPoint address;
181 if (socket && socket->GetLocalAddress(&address) == OK &&
182 address.GetFamily() == ADDRESS_FAMILY_IPV6) {
183 connection->set_max_packet_length(
184 connection->max_packet_length() - kAdditionalOverheadForIPv6);
188 void QuicClientSession::InitializeSession(
189 const QuicServerId& server_id,
190 QuicCryptoClientConfig* crypto_config,
191 QuicCryptoClientStreamFactory* crypto_client_stream_factory) {
192 server_id_ = server_id;
193 crypto_stream_.reset(
194 crypto_client_stream_factory ?
195 crypto_client_stream_factory->CreateQuicCryptoClientStream(
196 server_id, this, crypto_config) :
197 new QuicCryptoClientStream(server_id, this,
198 new ProofVerifyContextChromium(net_log_),
199 crypto_config));
200 QuicClientSessionBase::InitializeSession();
201 // TODO(rch): pass in full host port proxy pair
202 net_log_.BeginEvent(NetLog::TYPE_QUIC_SESSION,
203 base::Bind(NetLogQuicClientSessionCallback,
204 &server_id,
205 require_confirmation_));
208 QuicClientSession::~QuicClientSession() {
209 if (!streams()->empty())
210 RecordUnexpectedOpenStreams(DESTRUCTOR);
211 if (!observers_.empty())
212 RecordUnexpectedObservers(DESTRUCTOR);
213 if (!going_away_)
214 RecordUnexpectedNotGoingAway(DESTRUCTOR);
216 while (!streams()->empty() ||
217 !observers_.empty() ||
218 !stream_requests_.empty()) {
219 // The session must be closed before it is destroyed.
220 DCHECK(streams()->empty());
221 CloseAllStreams(ERR_UNEXPECTED);
222 DCHECK(observers_.empty());
223 CloseAllObservers(ERR_UNEXPECTED);
225 connection()->set_debug_visitor(nullptr);
226 net_log_.EndEvent(NetLog::TYPE_QUIC_SESSION);
228 while (!stream_requests_.empty()) {
229 StreamRequest* request = stream_requests_.front();
230 stream_requests_.pop_front();
231 request->OnRequestCompleteFailure(ERR_ABORTED);
235 if (connection()->connected()) {
236 // Ensure that the connection is closed by the time the session is
237 // destroyed.
238 connection()->CloseConnection(QUIC_INTERNAL_ERROR, false);
241 if (IsEncryptionEstablished())
242 RecordHandshakeState(STATE_ENCRYPTION_ESTABLISHED);
243 if (IsCryptoHandshakeConfirmed())
244 RecordHandshakeState(STATE_HANDSHAKE_CONFIRMED);
245 else
246 RecordHandshakeState(STATE_FAILED);
248 UMA_HISTOGRAM_COUNTS("Net.QuicSession.NumTotalStreams", num_total_streams_);
249 UMA_HISTOGRAM_COUNTS("Net.QuicNumSentClientHellos",
250 crypto_stream_->num_sent_client_hellos());
251 if (!IsCryptoHandshakeConfirmed())
252 return;
254 // Sending one client_hello means we had zero handshake-round-trips.
255 int round_trip_handshakes = crypto_stream_->num_sent_client_hellos() - 1;
257 // Don't bother with these histogram during tests, which mock out
258 // num_sent_client_hellos().
259 if (round_trip_handshakes < 0 || !stream_factory_)
260 return;
262 bool port_selected = stream_factory_->enable_port_selection();
263 SSLInfo ssl_info;
264 if (!GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) {
265 if (port_selected) {
266 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.QuicSession.ConnectSelectPortForHTTP",
267 round_trip_handshakes, 0, 3, 4);
268 } else {
269 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.QuicSession.ConnectRandomPortForHTTP",
270 round_trip_handshakes, 0, 3, 4);
271 if (require_confirmation_) {
272 UMA_HISTOGRAM_CUSTOM_COUNTS(
273 "Net.QuicSession.ConnectRandomPortRequiringConfirmationForHTTP",
274 round_trip_handshakes, 0, 3, 4);
277 } else {
278 if (port_selected) {
279 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.QuicSession.ConnectSelectPortForHTTPS",
280 round_trip_handshakes, 0, 3, 4);
281 } else {
282 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.QuicSession.ConnectRandomPortForHTTPS",
283 round_trip_handshakes, 0, 3, 4);
284 if (require_confirmation_) {
285 UMA_HISTOGRAM_CUSTOM_COUNTS(
286 "Net.QuicSession.ConnectRandomPortRequiringConfirmationForHTTPS",
287 round_trip_handshakes, 0, 3, 4);
291 const QuicConnectionStats stats = connection()->GetStats();
292 if (server_info_ && stats.min_rtt_us > 0) {
293 base::TimeTicks wait_for_data_start_time =
294 server_info_->wait_for_data_start_time();
295 base::TimeTicks wait_for_data_end_time =
296 server_info_->wait_for_data_end_time();
297 if (!wait_for_data_start_time.is_null() &&
298 !wait_for_data_end_time.is_null()) {
299 base::TimeDelta wait_time =
300 wait_for_data_end_time - wait_for_data_start_time;
301 const base::HistogramBase::Sample kMaxWaitToRtt = 1000;
302 base::HistogramBase::Sample wait_to_rtt =
303 static_cast<base::HistogramBase::Sample>(
304 100 * wait_time.InMicroseconds() / stats.min_rtt_us);
305 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.QuicServerInfo.WaitForDataReadyToRtt",
306 wait_to_rtt, 0, kMaxWaitToRtt, 50);
310 if (stats.max_sequence_reordering == 0)
311 return;
312 const base::HistogramBase::Sample kMaxReordering = 100;
313 base::HistogramBase::Sample reordering = kMaxReordering;
314 if (stats.min_rtt_us > 0) {
315 reordering = static_cast<base::HistogramBase::Sample>(
316 100 * stats.max_time_reordering_us / stats.min_rtt_us);
318 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.QuicSession.MaxReorderingTime",
319 reordering, 0, kMaxReordering, 50);
320 if (stats.min_rtt_us > 100 * 1000) {
321 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.QuicSession.MaxReorderingTimeLongRtt",
322 reordering, 0, kMaxReordering, 50);
324 UMA_HISTOGRAM_COUNTS(
325 "Net.QuicSession.MaxReordering",
326 static_cast<base::HistogramBase::Sample>(stats.max_sequence_reordering));
329 void QuicClientSession::OnStreamFrames(
330 const std::vector<QuicStreamFrame>& frames) {
331 // Record total number of stream frames.
332 UMA_HISTOGRAM_COUNTS("Net.QuicNumStreamFramesInPacket", frames.size());
334 // Record number of frames per stream in packet.
335 typedef std::map<QuicStreamId, size_t> FrameCounter;
336 FrameCounter frames_per_stream;
337 for (size_t i = 0; i < frames.size(); ++i) {
338 frames_per_stream[frames[i].stream_id]++;
340 for (FrameCounter::const_iterator it = frames_per_stream.begin();
341 it != frames_per_stream.end(); ++it) {
342 UMA_HISTOGRAM_COUNTS("Net.QuicNumStreamFramesPerStreamInPacket",
343 it->second);
346 return QuicSession::OnStreamFrames(frames);
349 void QuicClientSession::AddObserver(Observer* observer) {
350 if (going_away_) {
351 RecordUnexpectedObservers(ADD_OBSERVER);
352 observer->OnSessionClosed(ERR_UNEXPECTED);
353 return;
356 DCHECK(!ContainsKey(observers_, observer));
357 observers_.insert(observer);
360 void QuicClientSession::RemoveObserver(Observer* observer) {
361 DCHECK(ContainsKey(observers_, observer));
362 observers_.erase(observer);
365 int QuicClientSession::TryCreateStream(StreamRequest* request,
366 QuicReliableClientStream** stream) {
367 if (!crypto_stream_->encryption_established()) {
368 DLOG(DFATAL) << "Encryption not established.";
369 return ERR_CONNECTION_CLOSED;
372 if (goaway_received()) {
373 DVLOG(1) << "Going away.";
374 return ERR_CONNECTION_CLOSED;
377 if (!connection()->connected()) {
378 DVLOG(1) << "Already closed.";
379 return ERR_CONNECTION_CLOSED;
382 if (going_away_) {
383 RecordUnexpectedOpenStreams(TRY_CREATE_STREAM);
384 return ERR_CONNECTION_CLOSED;
387 if (GetNumOpenStreams() < get_max_open_streams()) {
388 *stream = CreateOutgoingReliableStreamImpl();
389 return OK;
392 stream_requests_.push_back(request);
393 return ERR_IO_PENDING;
396 void QuicClientSession::CancelRequest(StreamRequest* request) {
397 // Remove |request| from the queue while preserving the order of the
398 // other elements.
399 StreamRequestQueue::iterator it =
400 std::find(stream_requests_.begin(), stream_requests_.end(), request);
401 if (it != stream_requests_.end()) {
402 it = stream_requests_.erase(it);
406 QuicReliableClientStream* QuicClientSession::CreateOutgoingDataStream() {
407 if (!crypto_stream_->encryption_established()) {
408 DVLOG(1) << "Encryption not active so no outgoing stream created.";
409 return nullptr;
411 if (GetNumOpenStreams() >= get_max_open_streams()) {
412 DVLOG(1) << "Failed to create a new outgoing stream. "
413 << "Already " << GetNumOpenStreams() << " open.";
414 return nullptr;
416 if (goaway_received()) {
417 DVLOG(1) << "Failed to create a new outgoing stream. "
418 << "Already received goaway.";
419 return nullptr;
421 if (going_away_) {
422 RecordUnexpectedOpenStreams(CREATE_OUTGOING_RELIABLE_STREAM);
423 return nullptr;
425 return CreateOutgoingReliableStreamImpl();
428 QuicReliableClientStream*
429 QuicClientSession::CreateOutgoingReliableStreamImpl() {
430 DCHECK(connection()->connected());
431 QuicReliableClientStream* stream =
432 new QuicReliableClientStream(GetNextStreamId(), this, net_log_);
433 ActivateStream(stream);
434 ++num_total_streams_;
435 UMA_HISTOGRAM_COUNTS("Net.QuicSession.NumOpenStreams", GetNumOpenStreams());
436 return stream;
439 QuicCryptoClientStream* QuicClientSession::GetCryptoStream() {
440 return crypto_stream_.get();
443 // TODO(rtenneti): Add unittests for GetSSLInfo which exercise the various ways
444 // we learn about SSL info (sync vs async vs cached).
445 bool QuicClientSession::GetSSLInfo(SSLInfo* ssl_info) const {
446 ssl_info->Reset();
447 if (!cert_verify_result_) {
448 return false;
451 ssl_info->cert_status = cert_verify_result_->cert_status;
452 ssl_info->cert = cert_verify_result_->verified_cert;
454 // TODO(wtc): Define QUIC "cipher suites".
455 // Report the TLS cipher suite that most closely resembles the crypto
456 // parameters of the QUIC connection.
457 QuicTag aead = crypto_stream_->crypto_negotiated_params().aead;
458 uint16 cipher_suite;
459 int security_bits;
460 switch (aead) {
461 case kAESG:
462 cipher_suite = 0xc02f; // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
463 security_bits = 128;
464 break;
465 case kCC12:
466 cipher_suite = 0xcc13; // TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
467 security_bits = 256;
468 break;
469 default:
470 NOTREACHED();
471 return false;
473 int ssl_connection_status = 0;
474 ssl_connection_status |= cipher_suite;
475 ssl_connection_status |=
476 (SSL_CONNECTION_VERSION_QUIC & SSL_CONNECTION_VERSION_MASK) <<
477 SSL_CONNECTION_VERSION_SHIFT;
479 ssl_info->public_key_hashes = cert_verify_result_->public_key_hashes;
480 ssl_info->is_issued_by_known_root =
481 cert_verify_result_->is_issued_by_known_root;
483 ssl_info->connection_status = ssl_connection_status;
484 ssl_info->client_cert_sent = false;
485 ssl_info->channel_id_sent = crypto_stream_->WasChannelIDSent();
486 ssl_info->security_bits = security_bits;
487 ssl_info->handshake_type = SSLInfo::HANDSHAKE_FULL;
488 ssl_info->pinning_failure_log = pinning_failure_log_;
489 return true;
492 int QuicClientSession::CryptoConnect(bool require_confirmation,
493 const CompletionCallback& callback) {
494 require_confirmation_ = require_confirmation;
495 handshake_start_ = base::TimeTicks::Now();
496 RecordHandshakeState(STATE_STARTED);
497 DCHECK(flow_controller());
498 crypto_stream_->CryptoConnect();
500 if (IsCryptoHandshakeConfirmed())
501 return OK;
503 // Unless we require handshake confirmation, activate the session if
504 // we have established initial encryption.
505 if (!require_confirmation_ && IsEncryptionEstablished()) {
506 // To mitigate the effects of hanging 0-RTT connections, set up a timer to
507 // cancel any requests, if the handshake takes too long.
508 task_runner_->PostDelayedTask(
509 FROM_HERE,
510 base::Bind(&QuicClientSession::OnConnectTimeout,
511 weak_factory_.GetWeakPtr()),
512 base::TimeDelta::FromMilliseconds(k0RttHandshakeTimeoutMs));
513 return OK;
517 callback_ = callback;
518 return ERR_IO_PENDING;
521 int QuicClientSession::ResumeCryptoConnect(const CompletionCallback& callback) {
523 if (IsCryptoHandshakeConfirmed())
524 return OK;
526 if (!connection()->connected())
527 return ERR_QUIC_HANDSHAKE_FAILED;
529 callback_ = callback;
530 return ERR_IO_PENDING;
533 int QuicClientSession::GetNumSentClientHellos() const {
534 return crypto_stream_->num_sent_client_hellos();
537 bool QuicClientSession::CanPool(const std::string& hostname,
538 PrivacyMode privacy_mode) const {
539 DCHECK(connection()->connected());
540 if (privacy_mode != server_id_.privacy_mode()) {
541 // Privacy mode must always match.
542 return false;
544 SSLInfo ssl_info;
545 if (!GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) {
546 // We can always pool with insecure QUIC sessions.
547 return true;
550 return SpdySession::CanPool(transport_security_state_, ssl_info,
551 server_id_.host(), hostname);
554 QuicDataStream* QuicClientSession::CreateIncomingDataStream(
555 QuicStreamId id) {
556 DLOG(ERROR) << "Server push not supported";
557 return nullptr;
560 void QuicClientSession::CloseStream(QuicStreamId stream_id) {
561 ReliableQuicStream* stream = GetStream(stream_id);
562 if (stream) {
563 logger_->UpdateReceivedFrameCounts(
564 stream_id, stream->num_frames_received(),
565 stream->num_duplicate_frames_received());
567 QuicSession::CloseStream(stream_id);
568 OnClosedStream();
571 void QuicClientSession::SendRstStream(QuicStreamId id,
572 QuicRstStreamErrorCode error,
573 QuicStreamOffset bytes_written) {
574 QuicSession::SendRstStream(id, error, bytes_written);
575 OnClosedStream();
578 void QuicClientSession::OnClosedStream() {
579 if (GetNumOpenStreams() < get_max_open_streams() &&
580 !stream_requests_.empty() &&
581 crypto_stream_->encryption_established() &&
582 !goaway_received() &&
583 !going_away_ &&
584 connection()->connected()) {
585 StreamRequest* request = stream_requests_.front();
586 stream_requests_.pop_front();
587 request->OnRequestCompleteSuccess(CreateOutgoingReliableStreamImpl());
590 if (GetNumOpenStreams() == 0) {
591 stream_factory_->OnIdleSession(this);
595 void QuicClientSession::OnCryptoHandshakeEvent(CryptoHandshakeEvent event) {
596 if (!callback_.is_null() &&
597 (!require_confirmation_ ||
598 event == HANDSHAKE_CONFIRMED || event == ENCRYPTION_REESTABLISHED)) {
599 // TODO(rtenneti): Currently for all CryptoHandshakeEvent events, callback_
600 // could be called because there are no error events in CryptoHandshakeEvent
601 // enum. If error events are added to CryptoHandshakeEvent, then the
602 // following code needs to changed.
603 base::ResetAndReturn(&callback_).Run(OK);
605 if (event == HANDSHAKE_CONFIRMED) {
606 UMA_HISTOGRAM_TIMES("Net.QuicSession.HandshakeConfirmedTime",
607 base::TimeTicks::Now() - handshake_start_);
608 if (server_info_) {
609 // TODO(rtenneti): Should we delete this histogram?
610 // Track how long it has taken to finish handshake once we start waiting
611 // for reading of QUIC server information from disk cache. We could use
612 // this data to compare total time taken if we were to cancel the disk
613 // cache read vs waiting for the read to complete.
614 base::TimeTicks wait_for_data_start_time =
615 server_info_->wait_for_data_start_time();
616 if (!wait_for_data_start_time.is_null()) {
617 UMA_HISTOGRAM_TIMES(
618 "Net.QuicServerInfo.WaitForDataReady.HandshakeConfirmedTime",
619 base::TimeTicks::Now() - wait_for_data_start_time);
622 // Track how long it has taken to finish handshake after we have finished
623 // DNS host resolution.
624 if (!dns_resolution_end_time_.is_null()) {
625 UMA_HISTOGRAM_TIMES(
626 "Net.QuicSession.HostResolution.HandshakeConfirmedTime",
627 base::TimeTicks::Now() - dns_resolution_end_time_);
630 ObserverSet::iterator it = observers_.begin();
631 while (it != observers_.end()) {
632 Observer* observer = *it;
633 ++it;
634 observer->OnCryptoHandshakeConfirmed();
636 if (server_info_)
637 server_info_->OnExternalCacheHit();
639 QuicSession::OnCryptoHandshakeEvent(event);
642 void QuicClientSession::OnCryptoHandshakeMessageSent(
643 const CryptoHandshakeMessage& message) {
644 logger_->OnCryptoHandshakeMessageSent(message);
647 void QuicClientSession::OnCryptoHandshakeMessageReceived(
648 const CryptoHandshakeMessage& message) {
649 logger_->OnCryptoHandshakeMessageReceived(message);
652 void QuicClientSession::OnConnectionClosed(QuicErrorCode error,
653 bool from_peer) {
654 DCHECK(!connection()->connected());
655 logger_->OnConnectionClosed(error, from_peer);
656 if (from_peer) {
657 UMA_HISTOGRAM_SPARSE_SLOWLY(
658 "Net.QuicSession.ConnectionCloseErrorCodeServer", error);
659 } else {
660 UMA_HISTOGRAM_SPARSE_SLOWLY(
661 "Net.QuicSession.ConnectionCloseErrorCodeClient", error);
664 if (error == QUIC_CONNECTION_TIMED_OUT) {
665 UMA_HISTOGRAM_COUNTS(
666 "Net.QuicSession.ConnectionClose.NumOpenStreams.TimedOut",
667 GetNumOpenStreams());
668 if (IsCryptoHandshakeConfirmed()) {
669 if (GetNumOpenStreams() > 0) {
670 UMA_HISTOGRAM_BOOLEAN(
671 "Net.QuicSession.TimedOutWithOpenStreams.HasUnackedPackets",
672 connection()->sent_packet_manager().HasUnackedPackets());
673 UMA_HISTOGRAM_COUNTS(
674 "Net.QuicSession.TimedOutWithOpenStreams.ConsecutiveRTOCount",
675 connection()->sent_packet_manager().consecutive_rto_count());
676 UMA_HISTOGRAM_COUNTS(
677 "Net.QuicSession.TimedOutWithOpenStreams.ConsecutiveTLPCount",
678 connection()->sent_packet_manager().consecutive_tlp_count());
680 if (connection()->sent_packet_manager().HasUnackedPackets()) {
681 UMA_HISTOGRAM_TIMES(
682 "Net.QuicSession.LocallyTimedOutWithOpenStreams."
683 "TimeSinceLastReceived.UnackedPackets",
684 NetworkActivityMonitor::GetInstance()->GetTimeSinceLastReceived());
685 } else {
686 UMA_HISTOGRAM_TIMES(
687 "Net.QuicSession.LocallyTimedOutWithOpenStreams."
688 "TimeSinceLastReceived.NoUnackedPackets",
689 NetworkActivityMonitor::GetInstance()->GetTimeSinceLastReceived());
692 } else {
693 UMA_HISTOGRAM_COUNTS(
694 "Net.QuicSession.ConnectionClose.NumOpenStreams.HandshakeTimedOut",
695 GetNumOpenStreams());
696 UMA_HISTOGRAM_COUNTS(
697 "Net.QuicSession.ConnectionClose.NumTotalStreams.HandshakeTimedOut",
698 num_total_streams_);
702 if (!IsCryptoHandshakeConfirmed()) {
703 if (error == QUIC_PUBLIC_RESET) {
704 RecordHandshakeFailureReason(HANDSHAKE_FAILURE_PUBLIC_RESET);
705 } else if (connection()->GetStats().packets_received == 0) {
706 RecordHandshakeFailureReason(HANDSHAKE_FAILURE_BLACK_HOLE);
707 UMA_HISTOGRAM_SPARSE_SLOWLY(
708 "Net.QuicSession.ConnectionClose.HandshakeFailureBlackHole.QuicError",
709 error);
710 } else {
711 RecordHandshakeFailureReason(HANDSHAKE_FAILURE_UNKNOWN);
712 UMA_HISTOGRAM_SPARSE_SLOWLY(
713 "Net.QuicSession.ConnectionClose.HandshakeFailureUnknown.QuicError",
714 error);
718 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicSession.QuicVersion",
719 connection()->version());
720 NotifyFactoryOfSessionGoingAway();
721 if (!callback_.is_null()) {
722 base::ResetAndReturn(&callback_).Run(ERR_QUIC_PROTOCOL_ERROR);
724 socket_->Close();
725 QuicSession::OnConnectionClosed(error, from_peer);
726 DCHECK(streams()->empty());
727 CloseAllStreams(ERR_UNEXPECTED);
728 CloseAllObservers(ERR_UNEXPECTED);
729 NotifyFactoryOfSessionClosedLater();
732 void QuicClientSession::OnSuccessfulVersionNegotiation(
733 const QuicVersion& version) {
734 logger_->OnSuccessfulVersionNegotiation(version);
735 QuicSession::OnSuccessfulVersionNegotiation(version);
738 void QuicClientSession::OnProofValid(
739 const QuicCryptoClientConfig::CachedState& cached) {
740 DCHECK(cached.proof_valid());
742 if (!server_info_) {
743 return;
746 QuicServerInfo::State* state = server_info_->mutable_state();
748 state->server_config = cached.server_config();
749 state->source_address_token = cached.source_address_token();
750 state->server_config_sig = cached.signature();
751 state->certs = cached.certs();
753 server_info_->Persist();
756 void QuicClientSession::OnProofVerifyDetailsAvailable(
757 const ProofVerifyDetails& verify_details) {
758 const ProofVerifyDetailsChromium* verify_details_chromium =
759 reinterpret_cast<const ProofVerifyDetailsChromium*>(&verify_details);
760 CertVerifyResult* result_copy = new CertVerifyResult;
761 result_copy->CopyFrom(verify_details_chromium->cert_verify_result);
762 cert_verify_result_.reset(result_copy);
763 pinning_failure_log_ = verify_details_chromium->pinning_failure_log;
764 logger_->OnCertificateVerified(*cert_verify_result_);
767 void QuicClientSession::StartReading() {
768 packet_reader_.StartReading();
771 void QuicClientSession::CloseSessionOnError(int error) {
772 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicSession.CloseSessionOnError", -error);
773 CloseSessionOnErrorInner(error, QUIC_INTERNAL_ERROR);
774 NotifyFactoryOfSessionClosed();
777 void QuicClientSession::CloseSessionOnErrorInner(int net_error,
778 QuicErrorCode quic_error) {
779 if (!callback_.is_null()) {
780 base::ResetAndReturn(&callback_).Run(net_error);
782 CloseAllStreams(net_error);
783 CloseAllObservers(net_error);
784 net_log_.AddEvent(
785 NetLog::TYPE_QUIC_SESSION_CLOSE_ON_ERROR,
786 NetLog::IntegerCallback("net_error", net_error));
788 if (connection()->connected())
789 connection()->CloseConnection(quic_error, false);
790 DCHECK(!connection()->connected());
793 void QuicClientSession::CloseAllStreams(int net_error) {
794 while (!streams()->empty()) {
795 ReliableQuicStream* stream = streams()->begin()->second;
796 QuicStreamId id = stream->id();
797 static_cast<QuicReliableClientStream*>(stream)->OnError(net_error);
798 CloseStream(id);
802 void QuicClientSession::CloseAllObservers(int net_error) {
803 while (!observers_.empty()) {
804 Observer* observer = *observers_.begin();
805 observers_.erase(observer);
806 observer->OnSessionClosed(net_error);
810 base::Value* QuicClientSession::GetInfoAsValue(
811 const std::set<HostPortPair>& aliases) {
812 base::DictionaryValue* dict = new base::DictionaryValue();
813 dict->SetString("version", QuicVersionToString(connection()->version()));
814 dict->SetInteger("open_streams", GetNumOpenStreams());
815 base::ListValue* stream_list = new base::ListValue();
816 for (base::hash_map<QuicStreamId, QuicDataStream*>::const_iterator it
817 = streams()->begin();
818 it != streams()->end();
819 ++it) {
820 stream_list->Append(new base::StringValue(
821 base::Uint64ToString(it->second->id())));
823 dict->Set("active_streams", stream_list);
825 dict->SetInteger("total_streams", num_total_streams_);
826 dict->SetString("peer_address", peer_address().ToString());
827 dict->SetString("connection_id", base::Uint64ToString(connection_id()));
828 dict->SetBoolean("connected", connection()->connected());
829 const QuicConnectionStats& stats = connection()->GetStats();
830 dict->SetInteger("packets_sent", stats.packets_sent);
831 dict->SetInteger("packets_received", stats.packets_received);
832 dict->SetInteger("packets_lost", stats.packets_lost);
833 SSLInfo ssl_info;
834 dict->SetBoolean("secure", GetSSLInfo(&ssl_info) && ssl_info.cert.get());
836 base::ListValue* alias_list = new base::ListValue();
837 for (std::set<HostPortPair>::const_iterator it = aliases.begin();
838 it != aliases.end(); it++) {
839 alias_list->Append(new base::StringValue(it->ToString()));
841 dict->Set("aliases", alias_list);
843 return dict;
846 base::WeakPtr<QuicClientSession> QuicClientSession::GetWeakPtr() {
847 return weak_factory_.GetWeakPtr();
850 void QuicClientSession::OnReadError(int result) {
851 DVLOG(1) << "Closing session on read error: " << result;
852 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicSession.ReadError", -result);
853 NotifyFactoryOfSessionGoingAway();
854 CloseSessionOnErrorInner(result, QUIC_PACKET_READ_ERROR);
855 NotifyFactoryOfSessionClosedLater();
858 bool QuicClientSession::OnPacket(const QuicEncryptedPacket& packet,
859 IPEndPoint local_address,
860 IPEndPoint peer_address) {
861 connection()->ProcessUdpPacket(local_address, peer_address, packet);
862 if (!connection()->connected()) {
863 NotifyFactoryOfSessionClosedLater();
864 return false;
866 return true;
869 void QuicClientSession::NotifyFactoryOfSessionGoingAway() {
870 going_away_ = true;
871 if (stream_factory_)
872 stream_factory_->OnSessionGoingAway(this);
875 void QuicClientSession::NotifyFactoryOfSessionClosedLater() {
876 if (!streams()->empty())
877 RecordUnexpectedOpenStreams(NOTIFY_FACTORY_OF_SESSION_CLOSED_LATER);
879 if (!going_away_)
880 RecordUnexpectedNotGoingAway(NOTIFY_FACTORY_OF_SESSION_CLOSED_LATER);
882 going_away_ = true;
883 DCHECK_EQ(0u, GetNumOpenStreams());
884 DCHECK(!connection()->connected());
885 base::MessageLoop::current()->PostTask(
886 FROM_HERE,
887 base::Bind(&QuicClientSession::NotifyFactoryOfSessionClosed,
888 weak_factory_.GetWeakPtr()));
891 void QuicClientSession::NotifyFactoryOfSessionClosed() {
892 if (!streams()->empty())
893 RecordUnexpectedOpenStreams(NOTIFY_FACTORY_OF_SESSION_CLOSED);
895 if (!going_away_)
896 RecordUnexpectedNotGoingAway(NOTIFY_FACTORY_OF_SESSION_CLOSED);
898 going_away_ = true;
899 DCHECK_EQ(0u, GetNumOpenStreams());
900 // Will delete |this|.
901 if (stream_factory_)
902 stream_factory_->OnSessionClosed(this);
905 void QuicClientSession::OnConnectTimeout() {
906 DCHECK(callback_.is_null());
907 DCHECK(IsEncryptionEstablished());
909 if (IsCryptoHandshakeConfirmed())
910 return;
912 // TODO(rch): re-enable this code once beta is cut.
913 // if (stream_factory_)
914 // stream_factory_->OnSessionConnectTimeout(this);
915 // CloseAllStreams(ERR_QUIC_HANDSHAKE_FAILED);
916 // DCHECK_EQ(0u, GetNumOpenStreams());
919 } // namespace net