Update V8 to version 4.6.61.
[chromium-blink-merge.git] / net / quic / quic_session.cc
blobf6dc6d0bf147d4b65e97d92f3d25ed156ab15380
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_session.h"
7 #include "base/stl_util.h"
8 #ifdef TEMP_INSTRUMENTATION_473893
9 #include "base/debug/alias.h"
10 #endif
11 #include "net/quic/crypto/proof_verifier.h"
12 #include "net/quic/quic_connection.h"
13 #include "net/quic/quic_flags.h"
14 #include "net/quic/quic_flow_controller.h"
15 #include "net/ssl/ssl_info.h"
17 using base::StringPiece;
18 using base::hash_map;
19 using base::hash_set;
20 using std::make_pair;
21 using std::map;
22 using std::max;
23 using std::string;
24 using std::vector;
26 namespace net {
28 #define ENDPOINT \
29 (perspective() == Perspective::IS_SERVER ? "Server: " : " Client: ")
31 // We want to make sure we delete any closed streams in a safe manner.
32 // To avoid deleting a stream in mid-operation, we have a simple shim between
33 // us and the stream, so we can delete any streams when we return from
34 // processing.
36 // We could just override the base methods, but this makes it easier to make
37 // sure we don't miss any.
38 class VisitorShim : public QuicConnectionVisitorInterface {
39 public:
40 explicit VisitorShim(QuicSession* session) : session_(session) {}
42 void OnStreamFrame(const QuicStreamFrame& frame) override {
43 session_->OnStreamFrame(frame);
44 session_->PostProcessAfterData();
46 void OnRstStream(const QuicRstStreamFrame& frame) override {
47 session_->OnRstStream(frame);
48 session_->PostProcessAfterData();
51 void OnGoAway(const QuicGoAwayFrame& frame) override {
52 session_->OnGoAway(frame);
53 session_->PostProcessAfterData();
56 void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override {
57 session_->OnWindowUpdateFrame(frame);
58 session_->PostProcessAfterData();
61 void OnBlockedFrame(const QuicBlockedFrame& frame) override {
62 session_->OnBlockedFrame(frame);
63 session_->PostProcessAfterData();
66 void OnCanWrite() override {
67 session_->OnCanWrite();
68 session_->PostProcessAfterData();
71 void OnCongestionWindowChange(QuicTime now) override {
72 session_->OnCongestionWindowChange(now);
75 void OnSuccessfulVersionNegotiation(const QuicVersion& version) override {
76 session_->OnSuccessfulVersionNegotiation(version);
79 void OnConnectionClosed(QuicErrorCode error, bool from_peer) override {
80 session_->OnConnectionClosed(error, from_peer);
81 // The session will go away, so don't bother with cleanup.
84 void OnWriteBlocked() override { session_->OnWriteBlocked(); }
86 bool WillingAndAbleToWrite() const override {
87 return session_->WillingAndAbleToWrite();
90 bool HasPendingHandshake() const override {
91 return session_->HasPendingHandshake();
94 bool HasOpenDynamicStreams() const override {
95 return session_->HasOpenDynamicStreams();
98 private:
99 QuicSession* session_;
102 QuicSession::QuicSession(QuicConnection* connection, const QuicConfig& config)
103 : connection_(connection),
104 visitor_shim_(new VisitorShim(this)),
105 config_(config),
106 max_open_streams_(config_.MaxStreamsPerConnection()),
107 next_stream_id_(perspective() == Perspective::IS_SERVER ? 2 : 3),
108 largest_peer_created_stream_id_(
109 perspective() == Perspective::IS_SERVER ? 1 : 0),
110 error_(QUIC_NO_ERROR),
111 flow_controller_(connection_.get(),
113 perspective(),
114 kMinimumFlowControlSendWindow,
115 config_.GetInitialSessionFlowControlWindowToSend(),
116 false),
117 goaway_received_(false),
118 goaway_sent_(false),
119 has_pending_handshake_(false) {
122 void QuicSession::Initialize() {
123 connection_->set_visitor(visitor_shim_.get());
124 connection_->SetFromConfig(config_);
126 DCHECK_EQ(kCryptoStreamId, GetCryptoStream()->id());
127 static_stream_map_[kCryptoStreamId] = GetCryptoStream();
130 QuicSession::~QuicSession() {
131 #ifdef TEMP_INSTRUMENTATION_473893
132 liveness_ = DEAD;
133 stack_trace_ = base::debug::StackTrace();
135 // Probably not necessary, but just in case compiler tries to optimize out the
136 // writes to liveness_ and stack_trace_.
137 base::debug::Alias(&liveness_);
138 base::debug::Alias(&stack_trace_);
139 #endif
141 STLDeleteElements(&closed_streams_);
142 STLDeleteValues(&dynamic_stream_map_);
144 DLOG_IF(WARNING,
145 locally_closed_streams_highest_offset_.size() > max_open_streams_)
146 << "Surprisingly high number of locally closed streams still waiting for "
147 "final byte offset: " << locally_closed_streams_highest_offset_.size();
150 void QuicSession::OnStreamFrame(const QuicStreamFrame& frame) {
151 // TODO(rch) deal with the error case of stream id 0.
152 QuicStreamId stream_id = frame.stream_id;
153 ReliableQuicStream* stream = GetStream(stream_id);
154 if (!stream) {
155 // The stream no longer exists, but we may still be interested in the
156 // final stream byte offset sent by the peer. A frame with a FIN can give
157 // us this offset.
158 if (frame.fin) {
159 QuicStreamOffset final_byte_offset = frame.offset + frame.data.size();
160 UpdateFlowControlOnFinalReceivedByteOffset(stream_id, final_byte_offset);
162 return;
164 stream->OnStreamFrame(frame);
167 void QuicSession::OnRstStream(const QuicRstStreamFrame& frame) {
168 if (ContainsKey(static_stream_map_, frame.stream_id)) {
169 connection()->SendConnectionCloseWithDetails(
170 QUIC_INVALID_STREAM_ID, "Attempt to reset a static stream");
171 return;
174 ReliableQuicStream* stream = GetDynamicStream(frame.stream_id);
175 if (!stream) {
176 // The RST frame contains the final byte offset for the stream: we can now
177 // update the connection level flow controller if needed.
178 UpdateFlowControlOnFinalReceivedByteOffset(frame.stream_id,
179 frame.byte_offset);
180 return; // Errors are handled by GetStream.
183 stream->OnStreamReset(frame);
186 void QuicSession::OnGoAway(const QuicGoAwayFrame& frame) {
187 DCHECK(frame.last_good_stream_id < next_stream_id_);
188 goaway_received_ = true;
191 void QuicSession::OnConnectionClosed(QuicErrorCode error, bool from_peer) {
192 DCHECK(!connection_->connected());
193 if (error_ == QUIC_NO_ERROR) {
194 error_ = error;
197 while (!dynamic_stream_map_.empty()) {
198 StreamMap::iterator it = dynamic_stream_map_.begin();
199 QuicStreamId id = it->first;
200 it->second->OnConnectionClosed(error, from_peer);
201 // The stream should call CloseStream as part of OnConnectionClosed.
202 if (dynamic_stream_map_.find(id) != dynamic_stream_map_.end()) {
203 LOG(DFATAL) << ENDPOINT
204 << "Stream failed to close under OnConnectionClosed";
205 CloseStream(id);
210 void QuicSession::OnSuccessfulVersionNegotiation(const QuicVersion& version) {
213 void QuicSession::OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) {
214 // Stream may be closed by the time we receive a WINDOW_UPDATE, so we can't
215 // assume that it still exists.
216 QuicStreamId stream_id = frame.stream_id;
217 if (stream_id == kConnectionLevelId) {
218 // This is a window update that applies to the connection, rather than an
219 // individual stream.
220 DVLOG(1) << ENDPOINT << "Received connection level flow control window "
221 "update with byte offset: "
222 << frame.byte_offset;
223 if (flow_controller_.UpdateSendWindowOffset(frame.byte_offset)) {
224 // Connection level flow control window has increased, so blocked streams
225 // can write again.
226 // TODO(ianswett): I suspect this can be delayed until the packet
227 // processing is complete.
228 if (!FLAGS_quic_dont_write_when_flow_unblocked) {
229 OnCanWrite();
232 return;
234 ReliableQuicStream* stream = GetStream(stream_id);
235 if (stream) {
236 stream->OnWindowUpdateFrame(frame);
240 void QuicSession::OnBlockedFrame(const QuicBlockedFrame& frame) {
241 // TODO(rjshade): Compare our flow control receive windows for specified
242 // streams: if we have a large window then maybe something
243 // had gone wrong with the flow control accounting.
244 DVLOG(1) << ENDPOINT
245 << "Received BLOCKED frame with stream id: " << frame.stream_id;
248 void QuicSession::OnCanWrite() {
249 // We limit the number of writes to the number of pending streams. If more
250 // streams become pending, WillingAndAbleToWrite will be true, which will
251 // cause the connection to request resumption before yielding to other
252 // connections.
253 size_t num_writes = write_blocked_streams_.NumBlockedStreams();
254 if (flow_controller_.IsBlocked()) {
255 // If we are connection level flow control blocked, then only allow the
256 // crypto and headers streams to try writing as all other streams will be
257 // blocked.
258 num_writes = 0;
259 if (write_blocked_streams_.crypto_stream_blocked()) {
260 num_writes += 1;
262 if (write_blocked_streams_.headers_stream_blocked()) {
263 num_writes += 1;
266 if (num_writes == 0) {
267 return;
270 QuicConnection::ScopedPacketBundler ack_bundler(
271 connection_.get(), QuicConnection::NO_ACK);
272 for (size_t i = 0; i < num_writes; ++i) {
273 if (!(write_blocked_streams_.HasWriteBlockedCryptoOrHeadersStream() ||
274 write_blocked_streams_.HasWriteBlockedDataStreams())) {
275 // Writing one stream removed another!? Something's broken.
276 LOG(DFATAL) << "WriteBlockedStream is missing";
277 connection_->CloseConnection(QUIC_INTERNAL_ERROR, false);
278 return;
280 if (!connection_->CanWriteStreamData()) {
281 return;
283 QuicStreamId stream_id = write_blocked_streams_.PopFront();
284 if (stream_id == kCryptoStreamId) {
285 has_pending_handshake_ = false; // We just popped it.
287 ReliableQuicStream* stream = GetStream(stream_id);
288 if (stream != nullptr && !stream->flow_controller()->IsBlocked()) {
289 // If the stream can't write all bytes, it'll re-add itself to the blocked
290 // list.
291 stream->OnCanWrite();
296 bool QuicSession::WillingAndAbleToWrite() const {
297 // If the crypto or headers streams are blocked, we want to schedule a write -
298 // they don't get blocked by connection level flow control. Otherwise only
299 // schedule a write if we are not flow control blocked at the connection
300 // level.
301 return write_blocked_streams_.HasWriteBlockedCryptoOrHeadersStream() ||
302 (!flow_controller_.IsBlocked() &&
303 write_blocked_streams_.HasWriteBlockedDataStreams());
306 bool QuicSession::HasPendingHandshake() const {
307 return has_pending_handshake_;
310 bool QuicSession::HasOpenDynamicStreams() const {
311 return GetNumOpenStreams() > 0;
314 QuicConsumedData QuicSession::WritevData(
315 QuicStreamId id,
316 const QuicIOVector& iov,
317 QuicStreamOffset offset,
318 bool fin,
319 FecProtection fec_protection,
320 QuicAckNotifier::DelegateInterface* ack_notifier_delegate) {
321 return connection_->SendStreamData(id, iov, offset, fin, fec_protection,
322 ack_notifier_delegate);
325 void QuicSession::SendRstStream(QuicStreamId id,
326 QuicRstStreamErrorCode error,
327 QuicStreamOffset bytes_written) {
328 if (ContainsKey(static_stream_map_, id)) {
329 LOG(DFATAL) << "Cannot send RST for a static stream with ID " << id;
330 return;
333 if (connection()->connected()) {
334 // Only send a RST_STREAM frame if still connected.
335 connection_->SendRstStream(id, error, bytes_written);
337 CloseStreamInner(id, true);
340 void QuicSession::SendGoAway(QuicErrorCode error_code, const string& reason) {
341 if (goaway_sent_) {
342 return;
344 goaway_sent_ = true;
345 connection_->SendGoAway(error_code, largest_peer_created_stream_id_, reason);
348 void QuicSession::CloseStream(QuicStreamId stream_id) {
349 CloseStreamInner(stream_id, false);
352 void QuicSession::CloseStreamInner(QuicStreamId stream_id,
353 bool locally_reset) {
354 DVLOG(1) << ENDPOINT << "Closing stream " << stream_id;
356 StreamMap::iterator it = dynamic_stream_map_.find(stream_id);
357 if (it == dynamic_stream_map_.end()) {
358 // When CloseStreamInner has been called recursively (via
359 // ReliableQuicStream::OnClose), the stream will already have been deleted
360 // from stream_map_, so return immediately.
361 DVLOG(1) << ENDPOINT << "Stream is already closed: " << stream_id;
362 return;
364 ReliableQuicStream* stream = it->second;
366 // Tell the stream that a RST has been sent.
367 if (locally_reset) {
368 stream->set_rst_sent(true);
371 closed_streams_.push_back(it->second);
373 // If we haven't received a FIN or RST for this stream, we need to keep track
374 // of the how many bytes the stream's flow controller believes it has
375 // received, for accurate connection level flow control accounting.
376 if (!stream->HasFinalReceivedByteOffset()) {
377 locally_closed_streams_highest_offset_[stream_id] =
378 stream->flow_controller()->highest_received_byte_offset();
381 dynamic_stream_map_.erase(it);
382 draining_streams_.erase(stream_id);
383 stream->OnClose();
384 // Decrease the number of streams being emulated when a new one is opened.
385 connection_->SetNumOpenStreams(dynamic_stream_map_.size());
388 void QuicSession::UpdateFlowControlOnFinalReceivedByteOffset(
389 QuicStreamId stream_id, QuicStreamOffset final_byte_offset) {
390 map<QuicStreamId, QuicStreamOffset>::iterator it =
391 locally_closed_streams_highest_offset_.find(stream_id);
392 if (it == locally_closed_streams_highest_offset_.end()) {
393 return;
396 DVLOG(1) << ENDPOINT << "Received final byte offset " << final_byte_offset
397 << " for stream " << stream_id;
398 QuicByteCount offset_diff = final_byte_offset - it->second;
399 if (flow_controller_.UpdateHighestReceivedOffset(
400 flow_controller_.highest_received_byte_offset() + offset_diff)) {
401 // If the final offset violates flow control, close the connection now.
402 if (flow_controller_.FlowControlViolation()) {
403 connection_->SendConnectionClose(
404 QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA);
405 return;
409 flow_controller_.AddBytesConsumed(offset_diff);
410 locally_closed_streams_highest_offset_.erase(it);
413 bool QuicSession::IsEncryptionEstablished() {
414 return GetCryptoStream()->encryption_established();
417 bool QuicSession::IsCryptoHandshakeConfirmed() {
418 return GetCryptoStream()->handshake_confirmed();
421 void QuicSession::OnConfigNegotiated() {
422 connection_->SetFromConfig(config_);
424 uint32 max_streams = config_.MaxStreamsPerConnection();
425 if (perspective() == Perspective::IS_SERVER) {
426 // A server should accept a small number of additional streams beyond the
427 // limit sent to the client. This helps avoid early connection termination
428 // when FIN/RSTs for old streams are lost or arrive out of order.
429 // Use a minimum number of additional streams, or a percentage increase,
430 // whichever is larger.
431 max_streams =
432 max(max_streams + kMaxStreamsMinimumIncrement,
433 static_cast<uint32>(max_streams * kMaxStreamsMultiplier));
435 if (config_.HasReceivedConnectionOptions()) {
436 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kAFCW)) {
437 EnableAutoTuneReceiveWindow();
439 // The following variations change the initial receive flow control window
440 // size for streams. For simplicity reasons, do not try to effect
441 // existing streams but only future ones.
442 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW5)) {
443 config_.SetInitialStreamFlowControlWindowToSend(32 * 1024);
445 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW6)) {
446 config_.SetInitialStreamFlowControlWindowToSend(64 * 1024);
448 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW7)) {
449 config_.SetInitialStreamFlowControlWindowToSend(128 * 1024);
453 set_max_open_streams(max_streams);
455 if (config_.HasReceivedInitialStreamFlowControlWindowBytes()) {
456 // Streams which were created before the SHLO was received (0-RTT
457 // requests) are now informed of the peer's initial flow control window.
458 OnNewStreamFlowControlWindow(
459 config_.ReceivedInitialStreamFlowControlWindowBytes());
461 if (config_.HasReceivedInitialSessionFlowControlWindowBytes()) {
462 OnNewSessionFlowControlWindow(
463 config_.ReceivedInitialSessionFlowControlWindowBytes());
467 void QuicSession::EnableAutoTuneReceiveWindow() {
468 flow_controller_.set_auto_tune_receive_window(true);
469 // Inform all existing streams about the new window.
470 for (auto const& kv : static_stream_map_) {
471 kv.second->flow_controller()->set_auto_tune_receive_window(true);
473 for (auto const& kv : dynamic_stream_map_) {
474 kv.second->flow_controller()->set_auto_tune_receive_window(true);
478 void QuicSession::OnNewStreamFlowControlWindow(QuicStreamOffset new_window) {
479 if (new_window < kMinimumFlowControlSendWindow) {
480 LOG(ERROR) << "Peer sent us an invalid stream flow control send window: "
481 << new_window
482 << ", below default: " << kMinimumFlowControlSendWindow;
483 if (connection_->connected()) {
484 connection_->SendConnectionClose(QUIC_FLOW_CONTROL_INVALID_WINDOW);
486 return;
489 // Inform all existing streams about the new window.
490 for (auto const& kv : static_stream_map_) {
491 kv.second->UpdateSendWindowOffset(new_window);
493 for (auto const& kv : dynamic_stream_map_) {
494 kv.second->UpdateSendWindowOffset(new_window);
498 void QuicSession::OnNewSessionFlowControlWindow(QuicStreamOffset new_window) {
499 if (new_window < kMinimumFlowControlSendWindow) {
500 LOG(ERROR) << "Peer sent us an invalid session flow control send window: "
501 << new_window
502 << ", below default: " << kMinimumFlowControlSendWindow;
503 if (connection_->connected()) {
504 connection_->SendConnectionClose(QUIC_FLOW_CONTROL_INVALID_WINDOW);
506 return;
509 flow_controller_.UpdateSendWindowOffset(new_window);
512 void QuicSession::OnCryptoHandshakeEvent(CryptoHandshakeEvent event) {
513 switch (event) {
514 // TODO(satyamshekhar): Move the logic of setting the encrypter/decrypter
515 // to QuicSession since it is the glue.
516 case ENCRYPTION_FIRST_ESTABLISHED:
517 break;
519 case ENCRYPTION_REESTABLISHED:
520 // Retransmit originally packets that were sent, since they can't be
521 // decrypted by the peer.
522 connection_->RetransmitUnackedPackets(ALL_INITIAL_RETRANSMISSION);
523 break;
525 case HANDSHAKE_CONFIRMED:
526 LOG_IF(DFATAL, !config_.negotiated()) << ENDPOINT
527 << "Handshake confirmed without parameter negotiation.";
528 // Discard originally encrypted packets, since they can't be decrypted by
529 // the peer.
530 connection_->NeuterUnencryptedPackets();
531 break;
533 default:
534 LOG(ERROR) << ENDPOINT << "Got unknown handshake event: " << event;
538 void QuicSession::OnCryptoHandshakeMessageSent(
539 const CryptoHandshakeMessage& message) {
542 void QuicSession::OnCryptoHandshakeMessageReceived(
543 const CryptoHandshakeMessage& message) {
546 QuicConfig* QuicSession::config() {
547 return &config_;
550 void QuicSession::ActivateStream(ReliableQuicStream* stream) {
551 DVLOG(1) << ENDPOINT << "num_streams: " << dynamic_stream_map_.size()
552 << ". activating " << stream->id();
553 DCHECK(!ContainsKey(dynamic_stream_map_, stream->id()));
554 DCHECK(!ContainsKey(static_stream_map_, stream->id()));
555 dynamic_stream_map_[stream->id()] = stream;
556 // Increase the number of streams being emulated when a new one is opened.
557 connection_->SetNumOpenStreams(dynamic_stream_map_.size());
560 QuicStreamId QuicSession::GetNextStreamId() {
561 QuicStreamId id = next_stream_id_;
562 next_stream_id_ += 2;
563 return id;
566 ReliableQuicStream* QuicSession::GetStream(const QuicStreamId stream_id) {
567 StreamMap::iterator it = static_stream_map_.find(stream_id);
568 if (it != static_stream_map_.end()) {
569 return it->second;
571 return GetDynamicStream(stream_id);
574 void QuicSession::StreamDraining(QuicStreamId stream_id) {
575 DCHECK(ContainsKey(dynamic_stream_map_, stream_id));
576 if (!ContainsKey(draining_streams_, stream_id)) {
577 draining_streams_.insert(stream_id);
581 ReliableQuicStream* QuicSession::GetDynamicStream(
582 const QuicStreamId stream_id) {
583 if (static_stream_map_.find(stream_id) != static_stream_map_.end()) {
584 DLOG(FATAL) << "Attempt to call GetDynamicStream for a static stream";
585 return nullptr;
588 StreamMap::iterator it = dynamic_stream_map_.find(stream_id);
589 if (it != dynamic_stream_map_.end()) {
590 return it->second;
593 if (IsClosedStream(stream_id)) {
594 return nullptr;
597 if (stream_id % 2 == next_stream_id_ % 2) {
598 // We've received a frame for a locally-created stream that is not
599 // currently active. This is an error.
600 if (connection()->connected()) {
601 connection()->SendConnectionClose(QUIC_PACKET_FOR_NONEXISTENT_STREAM);
603 return nullptr;
606 return GetIncomingDynamicStream(stream_id);
609 ReliableQuicStream* QuicSession::GetIncomingDynamicStream(
610 QuicStreamId stream_id) {
611 if (IsClosedStream(stream_id)) {
612 return nullptr;
614 implicitly_created_streams_.erase(stream_id);
615 if (stream_id > largest_peer_created_stream_id_) {
616 if (FLAGS_exact_stream_id_delta) {
617 // Check if the number of streams that will be created (including
618 // implicitly open streams) would cause the number of open streams to
619 // exceed the limit. Note that the peer can create only
620 // alternately-numbered streams.
621 if ((stream_id - largest_peer_created_stream_id_) / 2 +
622 GetNumOpenStreams() >
623 get_max_open_streams()) {
624 DVLOG(1) << "Failed to create a new incoming stream with id:"
625 << stream_id << ". Already " << GetNumOpenStreams()
626 << " streams open, would exceed max " << get_max_open_streams()
627 << ".";
628 // We may already have sent a connection close due to multiple reset
629 // streams in the same packet.
630 if (connection()->connected()) {
631 connection()->SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS);
633 return nullptr;
635 } else {
636 // Limit on the delta between stream IDs.
637 const QuicStreamId kMaxStreamIdDelta = 200;
638 if (stream_id - largest_peer_created_stream_id_ > kMaxStreamIdDelta) {
639 // We may already have sent a connection close due to multiple reset
640 // streams in the same packet.
641 if (connection()->connected()) {
642 LOG(ERROR) << "Trying to get stream: " << stream_id
643 << ", largest peer created stream: "
644 << largest_peer_created_stream_id_
645 << ", max delta: " << kMaxStreamIdDelta;
646 connection()->SendConnectionClose(QUIC_INVALID_STREAM_ID);
648 return nullptr;
651 for (QuicStreamId id = largest_peer_created_stream_id_ + 2;
652 id < stream_id;
653 id += 2) {
654 implicitly_created_streams_.insert(id);
656 largest_peer_created_stream_id_ = stream_id;
658 ReliableQuicStream* stream = CreateIncomingDynamicStream(stream_id);
659 if (stream == nullptr) {
660 return nullptr;
662 ActivateStream(stream);
663 return stream;
666 void QuicSession::set_max_open_streams(size_t max_open_streams) {
667 DVLOG(1) << "Setting max_open_streams_ to " << max_open_streams;
668 max_open_streams_ = max_open_streams;
671 bool QuicSession::IsClosedStream(QuicStreamId id) {
672 DCHECK_NE(0u, id);
673 if (ContainsKey(static_stream_map_, id) ||
674 ContainsKey(dynamic_stream_map_, id)) {
675 // Stream is active
676 return false;
678 if (id % 2 == next_stream_id_ % 2) {
679 // Locally created streams are strictly in-order. If the id is in the
680 // range of created streams and it's not active, it must have been closed.
681 return id < next_stream_id_;
683 // For peer created streams, we also need to consider implicitly created
684 // streams.
685 return id <= largest_peer_created_stream_id_ &&
686 !ContainsKey(implicitly_created_streams_, id);
689 size_t QuicSession::GetNumOpenStreams() const {
690 return dynamic_stream_map_.size() + implicitly_created_streams_.size() -
691 draining_streams_.size();
694 void QuicSession::MarkConnectionLevelWriteBlocked(QuicStreamId id,
695 QuicPriority priority) {
696 #ifndef NDEBUG
697 ReliableQuicStream* stream = GetStream(id);
698 if (stream != nullptr) {
699 LOG_IF(DFATAL, priority != stream->EffectivePriority())
700 << ENDPOINT << "Stream " << id
701 << "Priorities do not match. Got: " << priority
702 << " Expected: " << stream->EffectivePriority();
703 } else {
704 LOG(DFATAL) << "Marking unknown stream " << id << " blocked.";
706 #endif
708 if (id == kCryptoStreamId) {
709 DCHECK(!has_pending_handshake_);
710 has_pending_handshake_ = true;
711 // TODO(jar): Be sure to use the highest priority for the crypto stream,
712 // perhaps by adding a "special" priority for it that is higher than
713 // kHighestPriority.
714 priority = kHighestPriority;
716 write_blocked_streams_.PushBack(id, priority);
719 bool QuicSession::HasDataToWrite() const {
720 return write_blocked_streams_.HasWriteBlockedCryptoOrHeadersStream() ||
721 write_blocked_streams_.HasWriteBlockedDataStreams() ||
722 connection_->HasQueuedData();
725 void QuicSession::PostProcessAfterData() {
726 STLDeleteElements(&closed_streams_);
728 if (connection()->connected() &&
729 locally_closed_streams_highest_offset_.size() > max_open_streams_) {
730 // A buggy client may fail to send FIN/RSTs. Don't tolerate this.
731 connection_->SendConnectionClose(QUIC_TOO_MANY_UNFINISHED_STREAMS);
735 bool QuicSession::IsConnectionFlowControlBlocked() const {
736 return flow_controller_.IsBlocked();
739 bool QuicSession::IsStreamFlowControlBlocked() {
740 for (auto const& kv : static_stream_map_) {
741 if (kv.second->flow_controller()->IsBlocked()) {
742 return true;
745 for (auto const& kv : dynamic_stream_map_) {
746 if (kv.second->flow_controller()->IsBlocked()) {
747 return true;
750 return false;
753 void QuicSession::CrashIfInvalid() const {
754 #ifdef TEMP_INSTRUMENTATION_473893
755 Liveness liveness = liveness_;
757 if (liveness == ALIVE)
758 return;
760 // Copy relevant variables onto the stack to guarantee they will be available
761 // in minidumps, and then crash.
762 base::debug::StackTrace stack_trace = stack_trace_;
764 base::debug::Alias(&liveness);
765 base::debug::Alias(&stack_trace);
767 CHECK_EQ(ALIVE, liveness);
768 #endif
771 } // namespace net