| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/quic_session.h" | 5 #include "net/quic/quic_session.h" |
| 6 | 6 |
| 7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "net/quic/crypto/proof_verifier.h" | 10 #include "net/quic/crypto/proof_verifier.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 using net::SpdyPriority; | 26 using net::SpdyPriority; |
| 27 | 27 |
| 28 namespace net { | 28 namespace net { |
| 29 | 29 |
| 30 #define ENDPOINT \ | 30 #define ENDPOINT \ |
| 31 (perspective() == Perspective::IS_SERVER ? "Server: " : " Client: ") | 31 (perspective() == Perspective::IS_SERVER ? "Server: " : " Client: ") |
| 32 | 32 |
| 33 QuicSession::QuicSession(QuicConnection* connection, const QuicConfig& config) | 33 QuicSession::QuicSession(QuicConnection* connection, const QuicConfig& config) |
| 34 : connection_(connection), | 34 : connection_(connection), |
| 35 config_(config), | 35 config_(config), |
| 36 max_open_streams_(config_.MaxStreamsPerConnection()), | 36 max_open_outgoing_streams_(config_.MaxStreamsPerConnection()), |
| 37 max_open_incoming_streams_(config_.MaxStreamsPerConnection()), |
| 37 next_outgoing_stream_id_(perspective() == Perspective::IS_SERVER ? 2 : 3), | 38 next_outgoing_stream_id_(perspective() == Perspective::IS_SERVER ? 2 : 3), |
| 38 largest_peer_created_stream_id_( | 39 largest_peer_created_stream_id_( |
| 39 perspective() == Perspective::IS_SERVER ? 1 : 0), | 40 perspective() == Perspective::IS_SERVER ? 1 : 0), |
| 40 num_dynamic_incoming_streams_(0), | 41 num_dynamic_incoming_streams_(0), |
| 41 num_draining_incoming_streams_(0), | 42 num_draining_incoming_streams_(0), |
| 42 num_locally_closed_incoming_streams_highest_offset_(0), | 43 num_locally_closed_incoming_streams_highest_offset_(0), |
| 43 error_(QUIC_NO_ERROR), | 44 error_(QUIC_NO_ERROR), |
| 44 flow_controller_(connection_.get(), | 45 flow_controller_(connection_.get(), |
| 45 0, | 46 0, |
| 46 perspective(), | 47 perspective(), |
| 47 kMinimumFlowControlSendWindow, | 48 kMinimumFlowControlSendWindow, |
| 48 config_.GetInitialSessionFlowControlWindowToSend(), | 49 config_.GetInitialSessionFlowControlWindowToSend(), |
| 49 false), | 50 false), |
| 50 currently_writing_stream_id_(0) {} | 51 currently_writing_stream_id_(0) {} |
| 51 | 52 |
| 52 void QuicSession::Initialize() { | 53 void QuicSession::Initialize() { |
| 53 connection_->set_visitor(this); | 54 connection_->set_visitor(this); |
| 54 connection_->SetFromConfig(config_); | 55 connection_->SetFromConfig(config_); |
| 55 | 56 |
| 56 DCHECK_EQ(kCryptoStreamId, GetCryptoStream()->id()); | 57 DCHECK_EQ(kCryptoStreamId, GetCryptoStream()->id()); |
| 57 static_stream_map_[kCryptoStreamId] = GetCryptoStream(); | 58 static_stream_map_[kCryptoStreamId] = GetCryptoStream(); |
| 58 } | 59 } |
| 59 | 60 |
| 60 QuicSession::~QuicSession() { | 61 QuicSession::~QuicSession() { |
| 61 STLDeleteElements(&closed_streams_); | 62 STLDeleteElements(&closed_streams_); |
| 62 STLDeleteValues(&dynamic_stream_map_); | 63 STLDeleteValues(&dynamic_stream_map_); |
| 63 | 64 |
| 64 DLOG_IF(WARNING, num_locally_closed_incoming_streams_highest_offset() > | 65 DLOG_IF(WARNING, num_locally_closed_incoming_streams_highest_offset() > |
| 65 max_open_streams_) | 66 max_open_incoming_streams_) |
| 66 << "Surprisingly high number of locally closed peer initiated streams" | 67 << "Surprisingly high number of locally closed peer initiated streams" |
| 67 "still waiting for final byte offset: " | 68 "still waiting for final byte offset: " |
| 68 << num_locally_closed_incoming_streams_highest_offset(); | 69 << num_locally_closed_incoming_streams_highest_offset(); |
| 69 DLOG_IF(WARNING, | 70 DLOG_IF(WARNING, GetNumLocallyClosedOutgoingStreamsHighestOffset() > |
| 70 GetNumLocallyClosedOutgoingStreamsHighestOffset() > max_open_streams_) | 71 max_open_outgoing_streams_) |
| 71 << "Surprisingly high number of locally closed self initiated streams" | 72 << "Surprisingly high number of locally closed self initiated streams" |
| 72 "still waiting for final byte offset: " | 73 "still waiting for final byte offset: " |
| 73 << GetNumLocallyClosedOutgoingStreamsHighestOffset(); | 74 << GetNumLocallyClosedOutgoingStreamsHighestOffset(); |
| 74 } | 75 } |
| 75 | 76 |
| 76 void QuicSession::OnStreamFrame(const QuicStreamFrame& frame) { | 77 void QuicSession::OnStreamFrame(const QuicStreamFrame& frame) { |
| 77 // TODO(rch) deal with the error case of stream id 0. | 78 // TODO(rch) deal with the error case of stream id 0. |
| 78 QuicStreamId stream_id = frame.stream_id; | 79 QuicStreamId stream_id = frame.stream_id; |
| 79 ReliableQuicStream* stream = GetStream(stream_id); | 80 ReliableQuicStream* stream = GetStream(stream_id); |
| 80 if (!stream) { | 81 if (!stream) { |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 | 365 |
| 365 bool QuicSession::IsCryptoHandshakeConfirmed() { | 366 bool QuicSession::IsCryptoHandshakeConfirmed() { |
| 366 return GetCryptoStream()->handshake_confirmed(); | 367 return GetCryptoStream()->handshake_confirmed(); |
| 367 } | 368 } |
| 368 | 369 |
| 369 void QuicSession::OnConfigNegotiated() { | 370 void QuicSession::OnConfigNegotiated() { |
| 370 connection_->SetFromConfig(config_); | 371 connection_->SetFromConfig(config_); |
| 371 | 372 |
| 372 uint32_t max_streams = config_.MaxStreamsPerConnection(); | 373 uint32_t max_streams = config_.MaxStreamsPerConnection(); |
| 373 if (perspective() == Perspective::IS_SERVER) { | 374 if (perspective() == Perspective::IS_SERVER) { |
| 374 // A server should accept a small number of additional streams beyond the | 375 if (!FLAGS_quic_different_max_num_open_streams) { |
| 375 // limit sent to the client. This helps avoid early connection termination | 376 max_streams = |
| 376 // when FIN/RSTs for old streams are lost or arrive out of order. | 377 max(max_streams + kMaxStreamsMinimumIncrement, |
| 377 // Use a minimum number of additional streams, or a percentage increase, | 378 static_cast<uint32_t>(max_streams * kMaxStreamsMultiplier)); |
| 378 // whichever is larger. | 379 } |
| 379 max_streams = | |
| 380 max(max_streams + kMaxStreamsMinimumIncrement, | |
| 381 static_cast<uint32_t>(max_streams * kMaxStreamsMultiplier)); | |
| 382 | 380 |
| 383 if (config_.HasReceivedConnectionOptions()) { | 381 if (config_.HasReceivedConnectionOptions()) { |
| 384 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kAFCW)) { | 382 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kAFCW)) { |
| 385 // The following variations change the initial receive flow control | 383 // The following variations change the initial receive flow control |
| 386 // window sizes. | 384 // window sizes. |
| 387 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW5)) { | 385 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW5)) { |
| 388 AdjustInitialFlowControlWindows(32 * 1024); | 386 AdjustInitialFlowControlWindows(32 * 1024); |
| 389 } | 387 } |
| 390 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW6)) { | 388 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW6)) { |
| 391 AdjustInitialFlowControlWindows(64 * 1024); | 389 AdjustInitialFlowControlWindows(64 * 1024); |
| 392 } | 390 } |
| 393 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW7)) { | 391 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW7)) { |
| 394 AdjustInitialFlowControlWindows(128 * 1024); | 392 AdjustInitialFlowControlWindows(128 * 1024); |
| 395 } | 393 } |
| 396 EnableAutoTuneReceiveWindow(); | 394 EnableAutoTuneReceiveWindow(); |
| 397 } | 395 } |
| 398 } | 396 } |
| 399 } | 397 } |
| 400 set_max_open_streams(max_streams); | 398 |
| 399 set_max_open_outgoing_streams(max_streams); |
| 400 |
| 401 uint32_t max_incoming_streams = max_streams; |
| 402 if (FLAGS_quic_different_max_num_open_streams) { |
| 403 // A small number of additional incoming streams beyond the limit should be |
| 404 // allowed. This helps avoid early connection termination when FIN/RSTs for |
| 405 // old streams are lost or arrive out of order. |
| 406 // Use a minimum number of additional streams, or a percentage increase, |
| 407 // whichever is larger. |
| 408 max_incoming_streams = |
| 409 max(max_streams + kMaxStreamsMinimumIncrement, |
| 410 static_cast<uint32_t>(max_streams * kMaxStreamsMultiplier)); |
| 411 } |
| 412 set_max_open_incoming_streams(max_incoming_streams); |
| 401 | 413 |
| 402 if (config_.HasReceivedInitialStreamFlowControlWindowBytes()) { | 414 if (config_.HasReceivedInitialStreamFlowControlWindowBytes()) { |
| 403 // Streams which were created before the SHLO was received (0-RTT | 415 // Streams which were created before the SHLO was received (0-RTT |
| 404 // requests) are now informed of the peer's initial flow control window. | 416 // requests) are now informed of the peer's initial flow control window. |
| 405 OnNewStreamFlowControlWindow( | 417 OnNewStreamFlowControlWindow( |
| 406 config_.ReceivedInitialStreamFlowControlWindowBytes()); | 418 config_.ReceivedInitialStreamFlowControlWindowBytes()); |
| 407 } | 419 } |
| 408 if (config_.HasReceivedInitialSessionFlowControlWindowBytes()) { | 420 if (config_.HasReceivedInitialSessionFlowControlWindowBytes()) { |
| 409 OnNewSessionFlowControlWindow( | 421 OnNewSessionFlowControlWindow( |
| 410 config_.ReceivedInitialSessionFlowControlWindowBytes()); | 422 config_.ReceivedInitialSessionFlowControlWindowBytes()); |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 return true; | 614 return true; |
| 603 } | 615 } |
| 604 | 616 |
| 605 // Check if the new number of available streams would cause the number of | 617 // Check if the new number of available streams would cause the number of |
| 606 // available streams to exceed the limit. Note that the peer can create | 618 // available streams to exceed the limit. Note that the peer can create |
| 607 // only alternately-numbered streams. | 619 // only alternately-numbered streams. |
| 608 size_t additional_available_streams = | 620 size_t additional_available_streams = |
| 609 (stream_id - largest_peer_created_stream_id_) / 2 - 1; | 621 (stream_id - largest_peer_created_stream_id_) / 2 - 1; |
| 610 size_t new_num_available_streams = | 622 size_t new_num_available_streams = |
| 611 GetNumAvailableStreams() + additional_available_streams; | 623 GetNumAvailableStreams() + additional_available_streams; |
| 612 if (new_num_available_streams > get_max_available_streams()) { | 624 if (new_num_available_streams > MaxAvailableStreams()) { |
| 613 DVLOG(1) << "Failed to create a new incoming stream with id:" << stream_id | 625 DVLOG(1) << "Failed to create a new incoming stream with id:" << stream_id |
| 614 << ". There are already " << GetNumAvailableStreams() | 626 << ". There are already " << GetNumAvailableStreams() |
| 615 << " streams available, which would become " | 627 << " streams available, which would become " |
| 616 << new_num_available_streams << ", which exceeds the limit " | 628 << new_num_available_streams << ", which exceeds the limit " |
| 617 << get_max_available_streams() << "."; | 629 << MaxAvailableStreams() << "."; |
| 618 string details = IntToString(new_num_available_streams) + " above " + | 630 string details = IntToString(new_num_available_streams) + " above " + |
| 619 IntToString(get_max_available_streams()); | 631 IntToString(MaxAvailableStreams()); |
| 620 CloseConnectionWithDetails(QUIC_TOO_MANY_AVAILABLE_STREAMS, | 632 CloseConnectionWithDetails(QUIC_TOO_MANY_AVAILABLE_STREAMS, |
| 621 details.c_str()); | 633 details.c_str()); |
| 622 return false; | 634 return false; |
| 623 } | 635 } |
| 624 for (QuicStreamId id = largest_peer_created_stream_id_ + 2; id < stream_id; | 636 for (QuicStreamId id = largest_peer_created_stream_id_ + 2; id < stream_id; |
| 625 id += 2) { | 637 id += 2) { |
| 626 available_streams_.insert(id); | 638 available_streams_.insert(id); |
| 627 } | 639 } |
| 628 largest_peer_created_stream_id_ = stream_id; | 640 largest_peer_created_stream_id_ = stream_id; |
| 629 | 641 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 659 return nullptr; | 671 return nullptr; |
| 660 } | 672 } |
| 661 | 673 |
| 662 available_streams_.erase(stream_id); | 674 available_streams_.erase(stream_id); |
| 663 | 675 |
| 664 if (!MaybeIncreaseLargestPeerStreamId(stream_id)) { | 676 if (!MaybeIncreaseLargestPeerStreamId(stream_id)) { |
| 665 return nullptr; | 677 return nullptr; |
| 666 } | 678 } |
| 667 // Check if the new number of open streams would cause the number of | 679 // Check if the new number of open streams would cause the number of |
| 668 // open streams to exceed the limit. | 680 // open streams to exceed the limit. |
| 669 size_t num_current_open_streams = | 681 size_t num_open_incoming_streams = |
| 670 FLAGS_quic_distinguish_incoming_outgoing_streams | 682 FLAGS_quic_distinguish_incoming_outgoing_streams |
| 671 ? GetNumOpenIncomingStreams() | 683 ? GetNumOpenIncomingStreams() |
| 672 : dynamic_stream_map_.size() - draining_streams_.size() + | 684 : dynamic_stream_map_.size() - draining_streams_.size() + |
| 673 locally_closed_streams_highest_offset_.size(); | 685 locally_closed_streams_highest_offset_.size(); |
| 674 if (num_current_open_streams >= get_max_open_streams()) { | 686 if (num_open_incoming_streams >= max_open_incoming_streams()) { |
| 675 if (connection()->version() <= QUIC_VERSION_27) { | 687 if (connection()->version() <= QUIC_VERSION_27) { |
| 676 CloseConnectionWithDetails(QUIC_TOO_MANY_OPEN_STREAMS, | 688 CloseConnectionWithDetails(QUIC_TOO_MANY_OPEN_STREAMS, |
| 677 "Old style stream rejection"); | 689 "Old style stream rejection"); |
| 678 } else { | 690 } else { |
| 679 // Refuse to open the stream. | 691 // Refuse to open the stream. |
| 680 SendRstStream(stream_id, QUIC_REFUSED_STREAM, 0); | 692 SendRstStream(stream_id, QUIC_REFUSED_STREAM, 0); |
| 681 } | 693 } |
| 682 return nullptr; | 694 return nullptr; |
| 683 } | 695 } |
| 684 | 696 |
| 685 ReliableQuicStream* stream = CreateIncomingDynamicStream(stream_id); | 697 ReliableQuicStream* stream = CreateIncomingDynamicStream(stream_id); |
| 686 if (stream == nullptr) { | 698 if (stream == nullptr) { |
| 687 return nullptr; | 699 return nullptr; |
| 688 } | 700 } |
| 689 ActivateStream(stream); | 701 ActivateStream(stream); |
| 690 return stream; | 702 return stream; |
| 691 } | 703 } |
| 692 | 704 |
| 693 void QuicSession::set_max_open_streams(size_t max_open_streams) { | 705 void QuicSession::set_max_open_incoming_streams( |
| 694 DVLOG(1) << "Setting max_open_streams_ to " << max_open_streams; | 706 size_t max_open_incoming_streams) { |
| 695 DVLOG(1) << "Setting get_max_available_streams() to " | 707 DVLOG(1) << "Setting max_open_incoming_streams_ to " |
| 696 << get_max_available_streams(); | 708 << max_open_incoming_streams; |
| 697 max_open_streams_ = max_open_streams; | 709 max_open_incoming_streams_ = max_open_incoming_streams; |
| 710 DVLOG(1) << "MaxAvailableStreams() became " << MaxAvailableStreams(); |
| 711 } |
| 712 |
| 713 void QuicSession::set_max_open_outgoing_streams( |
| 714 size_t max_open_outgoing_streams) { |
| 715 DVLOG(1) << "Setting max_open_outgoing_streams_ to " |
| 716 << max_open_outgoing_streams; |
| 717 max_open_outgoing_streams_ = max_open_outgoing_streams; |
| 698 } | 718 } |
| 699 | 719 |
| 700 bool QuicSession::goaway_sent() const { | 720 bool QuicSession::goaway_sent() const { |
| 701 return connection_->goaway_sent(); | 721 return connection_->goaway_sent(); |
| 702 } | 722 } |
| 703 | 723 |
| 704 bool QuicSession::goaway_received() const { | 724 bool QuicSession::goaway_received() const { |
| 705 return connection_->goaway_received(); | 725 return connection_->goaway_received(); |
| 706 } | 726 } |
| 707 | 727 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 801 } | 821 } |
| 802 } | 822 } |
| 803 for (auto const& kv : dynamic_stream_map_) { | 823 for (auto const& kv : dynamic_stream_map_) { |
| 804 if (kv.second->flow_controller()->IsBlocked()) { | 824 if (kv.second->flow_controller()->IsBlocked()) { |
| 805 return true; | 825 return true; |
| 806 } | 826 } |
| 807 } | 827 } |
| 808 return false; | 828 return false; |
| 809 } | 829 } |
| 810 | 830 |
| 831 size_t QuicSession::MaxAvailableStreams() const { |
| 832 return max_open_incoming_streams_ * kMaxAvailableStreamsMultiplier; |
| 833 } |
| 834 |
| 811 bool QuicSession::IsIncomingStream(QuicStreamId id) const { | 835 bool QuicSession::IsIncomingStream(QuicStreamId id) const { |
| 812 return id % 2 != next_outgoing_stream_id_ % 2; | 836 return id % 2 != next_outgoing_stream_id_ % 2; |
| 813 } | 837 } |
| 814 | 838 |
| 815 } // namespace net | 839 } // namespace net |
| OLD | NEW |