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 12 matching lines...) Expand all Loading... |
23 using std::max; | 23 using std::max; |
24 using std::string; | 24 using std::string; |
25 using std::vector; | 25 using std::vector; |
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 // We want to make sure we delete any closed streams in a safe manner. | |
34 // To avoid deleting a stream in mid-operation, we have a simple shim between | |
35 // us and the stream, so we can delete any streams when we return from | |
36 // processing. | |
37 // | |
38 // We could just override the base methods, but this makes it easier to make | |
39 // sure we don't miss any. | |
40 class VisitorShim : public QuicConnectionVisitorInterface { | |
41 public: | |
42 explicit VisitorShim(QuicSession* session) : session_(session) {} | |
43 | |
44 void OnStreamFrame(const QuicStreamFrame& frame) override { | |
45 session_->OnStreamFrame(frame); | |
46 session_->PostProcessAfterData(); | |
47 } | |
48 void OnRstStream(const QuicRstStreamFrame& frame) override { | |
49 session_->OnRstStream(frame); | |
50 session_->PostProcessAfterData(); | |
51 } | |
52 | |
53 void OnGoAway(const QuicGoAwayFrame& frame) override { | |
54 session_->OnGoAway(frame); | |
55 session_->PostProcessAfterData(); | |
56 } | |
57 | |
58 void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override { | |
59 session_->OnWindowUpdateFrame(frame); | |
60 session_->PostProcessAfterData(); | |
61 } | |
62 | |
63 void OnBlockedFrame(const QuicBlockedFrame& frame) override { | |
64 session_->OnBlockedFrame(frame); | |
65 session_->PostProcessAfterData(); | |
66 } | |
67 | |
68 void OnCanWrite() override { | |
69 session_->OnCanWrite(); | |
70 session_->PostProcessAfterData(); | |
71 } | |
72 | |
73 void OnCongestionWindowChange(QuicTime now) override { | |
74 session_->OnCongestionWindowChange(now); | |
75 } | |
76 | |
77 void OnSuccessfulVersionNegotiation(const QuicVersion& version) override { | |
78 session_->OnSuccessfulVersionNegotiation(version); | |
79 } | |
80 | |
81 void OnConnectionClosed(QuicErrorCode error, bool from_peer) override { | |
82 session_->OnConnectionClosed(error, from_peer); | |
83 // The session will go away, so don't bother with cleanup. | |
84 } | |
85 | |
86 void OnWriteBlocked() override { session_->OnWriteBlocked(); } | |
87 | |
88 void OnConnectionMigration() override { session_->OnConnectionMigration(); } | |
89 | |
90 bool WillingAndAbleToWrite() const override { | |
91 return session_->WillingAndAbleToWrite(); | |
92 } | |
93 | |
94 bool HasPendingHandshake() const override { | |
95 return session_->HasPendingHandshake(); | |
96 } | |
97 | |
98 bool HasOpenDynamicStreams() const override { | |
99 return session_->HasOpenDynamicStreams(); | |
100 } | |
101 | |
102 private: | |
103 QuicSession* session_; | |
104 }; | |
105 | |
106 QuicSession::QuicSession(QuicConnection* connection, const QuicConfig& config) | 33 QuicSession::QuicSession(QuicConnection* connection, const QuicConfig& config) |
107 : connection_(connection), | 34 : connection_(connection), |
108 visitor_shim_(new VisitorShim(this)), | |
109 config_(config), | 35 config_(config), |
110 max_open_streams_(config_.MaxStreamsPerConnection()), | 36 max_open_outgoing_streams_(config_.MaxStreamsPerConnection()), |
| 37 max_open_incoming_streams_(config_.MaxStreamsPerConnection()), |
111 next_outgoing_stream_id_(perspective() == Perspective::IS_SERVER ? 2 : 3), | 38 next_outgoing_stream_id_(perspective() == Perspective::IS_SERVER ? 2 : 3), |
112 largest_peer_created_stream_id_( | 39 largest_peer_created_stream_id_( |
113 perspective() == Perspective::IS_SERVER ? 1 : 0), | 40 perspective() == Perspective::IS_SERVER ? 1 : 0), |
114 num_dynamic_incoming_streams_(0), | 41 num_dynamic_incoming_streams_(0), |
115 num_draining_incoming_streams_(0), | 42 num_draining_incoming_streams_(0), |
116 num_locally_closed_incoming_streams_highest_offset_(0), | 43 num_locally_closed_incoming_streams_highest_offset_(0), |
117 error_(QUIC_NO_ERROR), | 44 error_(QUIC_NO_ERROR), |
118 flow_controller_(connection_.get(), | 45 flow_controller_(connection_.get(), |
119 0, | 46 0, |
120 perspective(), | 47 perspective(), |
121 kMinimumFlowControlSendWindow, | 48 kMinimumFlowControlSendWindow, |
122 config_.GetInitialSessionFlowControlWindowToSend(), | 49 config_.GetInitialSessionFlowControlWindowToSend(), |
123 false), | 50 false), |
124 currently_writing_stream_id_(0) {} | 51 currently_writing_stream_id_(0) {} |
125 | 52 |
126 void QuicSession::Initialize() { | 53 void QuicSession::Initialize() { |
127 connection_->set_visitor(visitor_shim_.get()); | 54 connection_->set_visitor(this); |
128 connection_->SetFromConfig(config_); | 55 connection_->SetFromConfig(config_); |
129 | 56 |
130 DCHECK_EQ(kCryptoStreamId, GetCryptoStream()->id()); | 57 DCHECK_EQ(kCryptoStreamId, GetCryptoStream()->id()); |
131 static_stream_map_[kCryptoStreamId] = GetCryptoStream(); | 58 static_stream_map_[kCryptoStreamId] = GetCryptoStream(); |
132 } | 59 } |
133 | 60 |
134 QuicSession::~QuicSession() { | 61 QuicSession::~QuicSession() { |
135 STLDeleteElements(&closed_streams_); | 62 STLDeleteElements(&closed_streams_); |
136 STLDeleteValues(&dynamic_stream_map_); | 63 STLDeleteValues(&dynamic_stream_map_); |
137 | 64 |
138 DLOG_IF(WARNING, num_locally_closed_incoming_streams_highest_offset() > | 65 DLOG_IF(WARNING, num_locally_closed_incoming_streams_highest_offset() > |
139 max_open_streams_) | 66 max_open_incoming_streams_) |
140 << "Surprisingly high number of locally closed peer initiated streams" | 67 << "Surprisingly high number of locally closed peer initiated streams" |
141 "still waiting for final byte offset: " | 68 "still waiting for final byte offset: " |
142 << num_locally_closed_incoming_streams_highest_offset(); | 69 << num_locally_closed_incoming_streams_highest_offset(); |
143 DLOG_IF(WARNING, | 70 DLOG_IF(WARNING, GetNumLocallyClosedOutgoingStreamsHighestOffset() > |
144 GetNumLocallyClosedOutgoingStreamsHighestOffset() > max_open_streams_) | 71 max_open_outgoing_streams_) |
145 << "Surprisingly high number of locally closed self initiated streams" | 72 << "Surprisingly high number of locally closed self initiated streams" |
146 "still waiting for final byte offset: " | 73 "still waiting for final byte offset: " |
147 << GetNumLocallyClosedOutgoingStreamsHighestOffset(); | 74 << GetNumLocallyClosedOutgoingStreamsHighestOffset(); |
148 } | 75 } |
149 | 76 |
150 void QuicSession::OnStreamFrame(const QuicStreamFrame& frame) { | 77 void QuicSession::OnStreamFrame(const QuicStreamFrame& frame) { |
151 // TODO(rch) deal with the error case of stream id 0. | 78 // TODO(rch) deal with the error case of stream id 0. |
152 QuicStreamId stream_id = frame.stream_id; | 79 QuicStreamId stream_id = frame.stream_id; |
153 ReliableQuicStream* stream = GetStream(stream_id); | 80 ReliableQuicStream* stream = GetStream(stream_id); |
154 if (!stream) { | 81 if (!stream) { |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 | 365 |
439 bool QuicSession::IsCryptoHandshakeConfirmed() { | 366 bool QuicSession::IsCryptoHandshakeConfirmed() { |
440 return GetCryptoStream()->handshake_confirmed(); | 367 return GetCryptoStream()->handshake_confirmed(); |
441 } | 368 } |
442 | 369 |
443 void QuicSession::OnConfigNegotiated() { | 370 void QuicSession::OnConfigNegotiated() { |
444 connection_->SetFromConfig(config_); | 371 connection_->SetFromConfig(config_); |
445 | 372 |
446 uint32_t max_streams = config_.MaxStreamsPerConnection(); | 373 uint32_t max_streams = config_.MaxStreamsPerConnection(); |
447 if (perspective() == Perspective::IS_SERVER) { | 374 if (perspective() == Perspective::IS_SERVER) { |
448 // A server should accept a small number of additional streams beyond the | 375 if (!FLAGS_quic_different_max_num_open_streams) { |
449 // limit sent to the client. This helps avoid early connection termination | 376 max_streams = |
450 // when FIN/RSTs for old streams are lost or arrive out of order. | 377 max(max_streams + kMaxStreamsMinimumIncrement, |
451 // Use a minimum number of additional streams, or a percentage increase, | 378 static_cast<uint32_t>(max_streams * kMaxStreamsMultiplier)); |
452 // whichever is larger. | 379 } |
453 max_streams = | |
454 max(max_streams + kMaxStreamsMinimumIncrement, | |
455 static_cast<uint32_t>(max_streams * kMaxStreamsMultiplier)); | |
456 | 380 |
457 if (config_.HasReceivedConnectionOptions()) { | 381 if (config_.HasReceivedConnectionOptions()) { |
458 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kAFCW)) { | 382 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kAFCW)) { |
459 // The following variations change the initial receive flow control | 383 // The following variations change the initial receive flow control |
460 // window sizes. | 384 // window sizes. |
461 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW5)) { | 385 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW5)) { |
462 AdjustInitialFlowControlWindows(32 * 1024); | 386 AdjustInitialFlowControlWindows(32 * 1024); |
463 } | 387 } |
464 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW6)) { | 388 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW6)) { |
465 AdjustInitialFlowControlWindows(64 * 1024); | 389 AdjustInitialFlowControlWindows(64 * 1024); |
466 } | 390 } |
467 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW7)) { | 391 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW7)) { |
468 AdjustInitialFlowControlWindows(128 * 1024); | 392 AdjustInitialFlowControlWindows(128 * 1024); |
469 } | 393 } |
470 EnableAutoTuneReceiveWindow(); | 394 EnableAutoTuneReceiveWindow(); |
471 } | 395 } |
472 } | 396 } |
473 } | 397 } |
474 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); |
475 | 413 |
476 if (config_.HasReceivedInitialStreamFlowControlWindowBytes()) { | 414 if (config_.HasReceivedInitialStreamFlowControlWindowBytes()) { |
477 // Streams which were created before the SHLO was received (0-RTT | 415 // Streams which were created before the SHLO was received (0-RTT |
478 // 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. |
479 OnNewStreamFlowControlWindow( | 417 OnNewStreamFlowControlWindow( |
480 config_.ReceivedInitialStreamFlowControlWindowBytes()); | 418 config_.ReceivedInitialStreamFlowControlWindowBytes()); |
481 } | 419 } |
482 if (config_.HasReceivedInitialSessionFlowControlWindowBytes()) { | 420 if (config_.HasReceivedInitialSessionFlowControlWindowBytes()) { |
483 OnNewSessionFlowControlWindow( | 421 OnNewSessionFlowControlWindow( |
484 config_.ReceivedInitialSessionFlowControlWindowBytes()); | 422 config_.ReceivedInitialSessionFlowControlWindowBytes()); |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 return true; | 614 return true; |
677 } | 615 } |
678 | 616 |
679 // 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 |
680 // 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 |
681 // only alternately-numbered streams. | 619 // only alternately-numbered streams. |
682 size_t additional_available_streams = | 620 size_t additional_available_streams = |
683 (stream_id - largest_peer_created_stream_id_) / 2 - 1; | 621 (stream_id - largest_peer_created_stream_id_) / 2 - 1; |
684 size_t new_num_available_streams = | 622 size_t new_num_available_streams = |
685 GetNumAvailableStreams() + additional_available_streams; | 623 GetNumAvailableStreams() + additional_available_streams; |
686 if (new_num_available_streams > get_max_available_streams()) { | 624 if (new_num_available_streams > MaxAvailableStreams()) { |
687 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 |
688 << ". There are already " << GetNumAvailableStreams() | 626 << ". There are already " << GetNumAvailableStreams() |
689 << " streams available, which would become " | 627 << " streams available, which would become " |
690 << new_num_available_streams << ", which exceeds the limit " | 628 << new_num_available_streams << ", which exceeds the limit " |
691 << get_max_available_streams() << "."; | 629 << MaxAvailableStreams() << "."; |
692 string details = IntToString(new_num_available_streams) + " above " + | 630 string details = IntToString(new_num_available_streams) + " above " + |
693 IntToString(get_max_available_streams()); | 631 IntToString(MaxAvailableStreams()); |
694 CloseConnectionWithDetails(QUIC_TOO_MANY_AVAILABLE_STREAMS, | 632 CloseConnectionWithDetails(QUIC_TOO_MANY_AVAILABLE_STREAMS, |
695 details.c_str()); | 633 details.c_str()); |
696 return false; | 634 return false; |
697 } | 635 } |
698 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; |
699 id += 2) { | 637 id += 2) { |
700 available_streams_.insert(id); | 638 available_streams_.insert(id); |
701 } | 639 } |
702 largest_peer_created_stream_id_ = stream_id; | 640 largest_peer_created_stream_id_ = stream_id; |
703 | 641 |
(...skipping 29 matching lines...) Expand all Loading... |
733 return nullptr; | 671 return nullptr; |
734 } | 672 } |
735 | 673 |
736 available_streams_.erase(stream_id); | 674 available_streams_.erase(stream_id); |
737 | 675 |
738 if (!MaybeIncreaseLargestPeerStreamId(stream_id)) { | 676 if (!MaybeIncreaseLargestPeerStreamId(stream_id)) { |
739 return nullptr; | 677 return nullptr; |
740 } | 678 } |
741 // 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 |
742 // open streams to exceed the limit. | 680 // open streams to exceed the limit. |
743 size_t num_current_open_streams = | 681 size_t num_open_incoming_streams = |
744 FLAGS_quic_distinguish_incoming_outgoing_streams | 682 FLAGS_quic_distinguish_incoming_outgoing_streams |
745 ? GetNumOpenIncomingStreams() | 683 ? GetNumOpenIncomingStreams() |
746 : dynamic_stream_map_.size() - draining_streams_.size() + | 684 : dynamic_stream_map_.size() - draining_streams_.size() + |
747 locally_closed_streams_highest_offset_.size(); | 685 locally_closed_streams_highest_offset_.size(); |
748 if (num_current_open_streams >= get_max_open_streams()) { | 686 if (num_open_incoming_streams >= max_open_incoming_streams()) { |
749 if (connection()->version() <= QUIC_VERSION_27) { | 687 if (connection()->version() <= QUIC_VERSION_27) { |
750 CloseConnectionWithDetails(QUIC_TOO_MANY_OPEN_STREAMS, | 688 CloseConnectionWithDetails(QUIC_TOO_MANY_OPEN_STREAMS, |
751 "Old style stream rejection"); | 689 "Old style stream rejection"); |
752 } else { | 690 } else { |
753 // Refuse to open the stream. | 691 // Refuse to open the stream. |
754 SendRstStream(stream_id, QUIC_REFUSED_STREAM, 0); | 692 SendRstStream(stream_id, QUIC_REFUSED_STREAM, 0); |
755 } | 693 } |
756 return nullptr; | 694 return nullptr; |
757 } | 695 } |
758 | 696 |
759 ReliableQuicStream* stream = CreateIncomingDynamicStream(stream_id); | 697 ReliableQuicStream* stream = CreateIncomingDynamicStream(stream_id); |
760 if (stream == nullptr) { | 698 if (stream == nullptr) { |
761 return nullptr; | 699 return nullptr; |
762 } | 700 } |
763 ActivateStream(stream); | 701 ActivateStream(stream); |
764 return stream; | 702 return stream; |
765 } | 703 } |
766 | 704 |
767 void QuicSession::set_max_open_streams(size_t max_open_streams) { | 705 void QuicSession::set_max_open_incoming_streams( |
768 DVLOG(1) << "Setting max_open_streams_ to " << max_open_streams; | 706 size_t max_open_incoming_streams) { |
769 DVLOG(1) << "Setting get_max_available_streams() to " | 707 DVLOG(1) << "Setting max_open_incoming_streams_ to " |
770 << get_max_available_streams(); | 708 << max_open_incoming_streams; |
771 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; |
772 } | 718 } |
773 | 719 |
774 bool QuicSession::goaway_sent() const { | 720 bool QuicSession::goaway_sent() const { |
775 return connection_->goaway_sent(); | 721 return connection_->goaway_sent(); |
776 } | 722 } |
777 | 723 |
778 bool QuicSession::goaway_received() const { | 724 bool QuicSession::goaway_received() const { |
779 return connection_->goaway_received(); | 725 return connection_->goaway_received(); |
780 } | 726 } |
781 | 727 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
875 } | 821 } |
876 } | 822 } |
877 for (auto const& kv : dynamic_stream_map_) { | 823 for (auto const& kv : dynamic_stream_map_) { |
878 if (kv.second->flow_controller()->IsBlocked()) { | 824 if (kv.second->flow_controller()->IsBlocked()) { |
879 return true; | 825 return true; |
880 } | 826 } |
881 } | 827 } |
882 return false; | 828 return false; |
883 } | 829 } |
884 | 830 |
| 831 size_t QuicSession::MaxAvailableStreams() const { |
| 832 return max_open_incoming_streams_ * kMaxAvailableStreamsMultiplier; |
| 833 } |
| 834 |
885 bool QuicSession::IsIncomingStream(QuicStreamId id) const { | 835 bool QuicSession::IsIncomingStream(QuicStreamId id) const { |
886 return id % 2 != next_outgoing_stream_id_ % 2; | 836 return id % 2 != next_outgoing_stream_id_ % 2; |
887 } | 837 } |
888 | 838 |
889 } // namespace net | 839 } // namespace net |
OLD | NEW |