Add logging of received QUIC WindowUpdate and Blocked frames.
[chromium-blink-merge.git] / net / quic / quic_connection.h
blob35de041591a1f8096956f09d38fa41625d228261
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // The entity that handles framing writes for a Quic client or server.
6 // Each QuicSession will have a connection associated with it.
7 //
8 // On the server side, the Dispatcher handles the raw reads, and hands off
9 // packets via ProcessUdpPacket for framing and processing.
11 // On the client side, the Connection handles the raw reads, as well as the
12 // processing.
14 // Note: this class is not thread-safe.
16 #ifndef NET_QUIC_QUIC_CONNECTION_H_
17 #define NET_QUIC_QUIC_CONNECTION_H_
19 #include <stddef.h>
20 #include <deque>
21 #include <list>
22 #include <map>
23 #include <queue>
24 #include <string>
25 #include <vector>
27 #include "base/logging.h"
28 #include "net/base/iovec.h"
29 #include "net/base/ip_endpoint.h"
30 #include "net/quic/iovector.h"
31 #include "net/quic/quic_ack_notifier.h"
32 #include "net/quic/quic_ack_notifier_manager.h"
33 #include "net/quic/quic_alarm.h"
34 #include "net/quic/quic_blocked_writer_interface.h"
35 #include "net/quic/quic_connection_stats.h"
36 #include "net/quic/quic_packet_creator.h"
37 #include "net/quic/quic_packet_generator.h"
38 #include "net/quic/quic_packet_writer.h"
39 #include "net/quic/quic_protocol.h"
40 #include "net/quic/quic_received_packet_manager.h"
41 #include "net/quic/quic_sent_entropy_manager.h"
42 #include "net/quic/quic_sent_packet_manager.h"
43 #include "net/quic/quic_types.h"
45 namespace net {
47 class QuicClock;
48 class QuicConfig;
49 class QuicConnection;
50 class QuicDecrypter;
51 class QuicEncrypter;
52 class QuicFecGroup;
53 class QuicRandom;
55 namespace test {
56 class QuicConnectionPeer;
57 } // namespace test
59 // Class that receives callbacks from the connection when frames are received
60 // and when other interesting events happen.
61 class NET_EXPORT_PRIVATE QuicConnectionVisitorInterface {
62 public:
63 virtual ~QuicConnectionVisitorInterface() {}
65 // A simple visitor interface for dealing with data frames.
66 virtual void OnStreamFrames(const std::vector<QuicStreamFrame>& frames) = 0;
68 // The session should process all WINDOW_UPDATE frames, adjusting both stream
69 // and connection level flow control windows.
70 virtual void OnWindowUpdateFrames(
71 const std::vector<QuicWindowUpdateFrame>& frames) = 0;
73 // BLOCKED frames tell us that the peer believes it is flow control blocked on
74 // a specified stream. If the session at this end disagrees, something has
75 // gone wrong with our flow control accounting.
76 virtual void OnBlockedFrames(const std::vector<QuicBlockedFrame>& frames) = 0;
78 // Called when the stream is reset by the peer.
79 virtual void OnRstStream(const QuicRstStreamFrame& frame) = 0;
81 // Called when the connection is going away according to the peer.
82 virtual void OnGoAway(const QuicGoAwayFrame& frame) = 0;
84 // Called when the connection is closed either locally by the framer, or
85 // remotely by the peer.
86 virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer) = 0;
88 // Called when the connection failed to write because the socket was blocked.
89 virtual void OnWriteBlocked() = 0;
91 // Called once a specific QUIC version is agreed by both endpoints.
92 virtual void OnSuccessfulVersionNegotiation(const QuicVersion& version) = 0;
94 // Called when a blocked socket becomes writable.
95 virtual void OnCanWrite() = 0;
97 // Called to ask if the visitor wants to schedule write resumption as it both
98 // has pending data to write, and is able to write (e.g. based on flow control
99 // limits).
100 // Writes may be pending because they were write-blocked, congestion-throttled
101 // or yielded to other connections.
102 virtual bool WillingAndAbleToWrite() const = 0;
104 // Called to ask if any handshake messages are pending in this visitor.
105 virtual bool HasPendingHandshake() const = 0;
107 // Called to ask if any streams are open in this visitor, excluding the
108 // reserved crypto and headers stream.
109 virtual bool HasOpenDataStreams() const = 0;
112 // Interface which gets callbacks from the QuicConnection at interesting
113 // points. Implementations must not mutate the state of the connection
114 // as a result of these callbacks.
115 class NET_EXPORT_PRIVATE QuicConnectionDebugVisitor
116 : public QuicPacketGenerator::DebugDelegate,
117 public QuicSentPacketManager::DebugDelegate {
118 public:
119 virtual ~QuicConnectionDebugVisitor() {}
121 // Called when a packet has been sent.
122 virtual void OnPacketSent(QuicPacketSequenceNumber sequence_number,
123 EncryptionLevel level,
124 TransmissionType transmission_type,
125 const QuicEncryptedPacket& packet,
126 WriteResult result) {}
128 // Called when the contents of a packet have been retransmitted as
129 // a new packet.
130 virtual void OnPacketRetransmitted(
131 QuicPacketSequenceNumber old_sequence_number,
132 QuicPacketSequenceNumber new_sequence_number) {}
134 // Called when a packet has been received, but before it is
135 // validated or parsed.
136 virtual void OnPacketReceived(const IPEndPoint& self_address,
137 const IPEndPoint& peer_address,
138 const QuicEncryptedPacket& packet) {}
140 // Called when the protocol version on the received packet doensn't match
141 // current protocol version of the connection.
142 virtual void OnProtocolVersionMismatch(QuicVersion version) {}
144 // Called when the complete header of a packet has been parsed.
145 virtual void OnPacketHeader(const QuicPacketHeader& header) {}
147 // Called when a StreamFrame has been parsed.
148 virtual void OnStreamFrame(const QuicStreamFrame& frame) {}
150 // Called when a AckFrame has been parsed.
151 virtual void OnAckFrame(const QuicAckFrame& frame) {}
153 // Called when a CongestionFeedbackFrame has been parsed.
154 virtual void OnCongestionFeedbackFrame(
155 const QuicCongestionFeedbackFrame& frame) {}
157 // Called when a StopWaitingFrame has been parsed.
158 virtual void OnStopWaitingFrame(const QuicStopWaitingFrame& frame) {}
160 // Called when a Ping has been parsed.
161 virtual void OnPingFrame(const QuicPingFrame& frame) {}
163 // Called when a RstStreamFrame has been parsed.
164 virtual void OnRstStreamFrame(const QuicRstStreamFrame& frame) {}
166 // Called when a ConnectionCloseFrame has been parsed.
167 virtual void OnConnectionCloseFrame(
168 const QuicConnectionCloseFrame& frame) {}
170 // Called when a WindowUpdate has been parsed.
171 virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) {}
173 // Called when a BlockedFrame has been parsed.
174 virtual void OnBlockedFrame(const QuicBlockedFrame& frame) {}
176 // Called when a public reset packet has been received.
177 virtual void OnPublicResetPacket(const QuicPublicResetPacket& packet) {}
179 // Called when a version negotiation packet has been received.
180 virtual void OnVersionNegotiationPacket(
181 const QuicVersionNegotiationPacket& packet) {}
183 // Called after a packet has been successfully parsed which results
184 // in the revival of a packet via FEC.
185 virtual void OnRevivedPacket(const QuicPacketHeader& revived_header,
186 base::StringPiece payload) {}
189 class NET_EXPORT_PRIVATE QuicConnectionHelperInterface {
190 public:
191 virtual ~QuicConnectionHelperInterface() {}
193 // Returns a QuicClock to be used for all time related functions.
194 virtual const QuicClock* GetClock() const = 0;
196 // Returns a QuicRandom to be used for all random number related functions.
197 virtual QuicRandom* GetRandomGenerator() = 0;
199 // Creates a new platform-specific alarm which will be configured to
200 // notify |delegate| when the alarm fires. Caller takes ownership
201 // of the new alarm, which will not yet be "set" to fire.
202 virtual QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) = 0;
205 class NET_EXPORT_PRIVATE QuicConnection
206 : public QuicFramerVisitorInterface,
207 public QuicBlockedWriterInterface,
208 public QuicPacketGenerator::DelegateInterface {
209 public:
210 enum PacketType {
211 NORMAL,
212 QUEUED,
213 CONNECTION_CLOSE
216 enum AckBundling {
217 NO_ACK = 0,
218 SEND_ACK = 1,
219 BUNDLE_PENDING_ACK = 2,
222 // Constructs a new QuicConnection for |connection_id| and |address|.
223 // |helper| and |writer| must outlive this connection.
224 QuicConnection(QuicConnectionId connection_id,
225 IPEndPoint address,
226 QuicConnectionHelperInterface* helper,
227 QuicPacketWriter* writer,
228 bool is_server,
229 const QuicVersionVector& supported_versions);
230 virtual ~QuicConnection();
232 // Sets connection parameters from the supplied |config|.
233 void SetFromConfig(const QuicConfig& config);
235 // Send the data in |data| to the peer in as few packets as possible.
236 // Returns a pair with the number of bytes consumed from data, and a boolean
237 // indicating if the fin bit was consumed. This does not indicate the data
238 // has been sent on the wire: it may have been turned into a packet and queued
239 // if the socket was unexpectedly blocked. |fec_protection| indicates if
240 // data is to be FEC protected. Note that data that is sent immediately
241 // following MUST_FEC_PROTECT data may get protected by falling within the
242 // same FEC group.
243 // If |delegate| is provided, then it will be informed once ACKs have been
244 // received for all the packets written in this call.
245 // The |delegate| is not owned by the QuicConnection and must outlive it.
246 QuicConsumedData SendStreamData(QuicStreamId id,
247 const IOVector& data,
248 QuicStreamOffset offset,
249 bool fin,
250 FecProtection fec_protection,
251 QuicAckNotifier::DelegateInterface* delegate);
253 // Send a RST_STREAM frame to the peer.
254 virtual void SendRstStream(QuicStreamId id,
255 QuicRstStreamErrorCode error,
256 QuicStreamOffset bytes_written);
258 // Send a BLOCKED frame to the peer.
259 virtual void SendBlocked(QuicStreamId id);
261 // Send a WINDOW_UPDATE frame to the peer.
262 virtual void SendWindowUpdate(QuicStreamId id,
263 QuicStreamOffset byte_offset);
265 // Sends the connection close packet without affecting the state of the
266 // connection. This should only be called if the session is actively being
267 // destroyed: otherwise call SendConnectionCloseWithDetails instead.
268 virtual void SendConnectionClosePacket(QuicErrorCode error,
269 const std::string& details);
271 // Sends a connection close frame to the peer, and closes the connection by
272 // calling CloseConnection(notifying the visitor as it does so).
273 virtual void SendConnectionClose(QuicErrorCode error);
274 virtual void SendConnectionCloseWithDetails(QuicErrorCode error,
275 const std::string& details);
276 // Notifies the visitor of the close and marks the connection as disconnected.
277 virtual void CloseConnection(QuicErrorCode error, bool from_peer) OVERRIDE;
278 virtual void SendGoAway(QuicErrorCode error,
279 QuicStreamId last_good_stream_id,
280 const std::string& reason);
282 // Returns statistics tracked for this connection.
283 const QuicConnectionStats& GetStats();
285 // Processes an incoming UDP packet (consisting of a QuicEncryptedPacket) from
286 // the peer. If processing this packet permits a packet to be revived from
287 // its FEC group that packet will be revived and processed.
288 virtual void ProcessUdpPacket(const IPEndPoint& self_address,
289 const IPEndPoint& peer_address,
290 const QuicEncryptedPacket& packet);
292 // QuicBlockedWriterInterface
293 // Called when the underlying connection becomes writable to allow queued
294 // writes to happen.
295 virtual void OnCanWrite() OVERRIDE;
297 // Called when a packet has been finally sent to the network.
298 bool OnPacketSent(WriteResult result);
300 // If the socket is not blocked, writes queued packets.
301 void WriteIfNotBlocked();
303 // Do any work which logically would be done in OnPacket but can not be
304 // safely done until the packet is validated. Returns true if the packet
305 // can be handled, false otherwise.
306 bool ProcessValidatedPacket();
308 // The version of the protocol this connection is using.
309 QuicVersion version() const { return framer_.version(); }
311 // The versions of the protocol that this connection supports.
312 const QuicVersionVector& supported_versions() const {
313 return framer_.supported_versions();
316 // From QuicFramerVisitorInterface
317 virtual void OnError(QuicFramer* framer) OVERRIDE;
318 virtual bool OnProtocolVersionMismatch(QuicVersion received_version) OVERRIDE;
319 virtual void OnPacket() OVERRIDE;
320 virtual void OnPublicResetPacket(
321 const QuicPublicResetPacket& packet) OVERRIDE;
322 virtual void OnVersionNegotiationPacket(
323 const QuicVersionNegotiationPacket& packet) OVERRIDE;
324 virtual void OnRevivedPacket() OVERRIDE;
325 virtual bool OnUnauthenticatedPublicHeader(
326 const QuicPacketPublicHeader& header) OVERRIDE;
327 virtual bool OnUnauthenticatedHeader(const QuicPacketHeader& header) OVERRIDE;
328 virtual void OnDecryptedPacket(EncryptionLevel level) OVERRIDE;
329 virtual bool OnPacketHeader(const QuicPacketHeader& header) OVERRIDE;
330 virtual void OnFecProtectedPayload(base::StringPiece payload) OVERRIDE;
331 virtual bool OnStreamFrame(const QuicStreamFrame& frame) OVERRIDE;
332 virtual bool OnAckFrame(const QuicAckFrame& frame) OVERRIDE;
333 virtual bool OnCongestionFeedbackFrame(
334 const QuicCongestionFeedbackFrame& frame) OVERRIDE;
335 virtual bool OnStopWaitingFrame(const QuicStopWaitingFrame& frame) OVERRIDE;
336 virtual bool OnPingFrame(const QuicPingFrame& frame) OVERRIDE;
337 virtual bool OnRstStreamFrame(const QuicRstStreamFrame& frame) OVERRIDE;
338 virtual bool OnConnectionCloseFrame(
339 const QuicConnectionCloseFrame& frame) OVERRIDE;
340 virtual bool OnGoAwayFrame(const QuicGoAwayFrame& frame) OVERRIDE;
341 virtual bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) OVERRIDE;
342 virtual bool OnBlockedFrame(const QuicBlockedFrame& frame) OVERRIDE;
343 virtual void OnFecData(const QuicFecData& fec) OVERRIDE;
344 virtual void OnPacketComplete() OVERRIDE;
346 // QuicPacketGenerator::DelegateInterface
347 virtual bool ShouldGeneratePacket(TransmissionType transmission_type,
348 HasRetransmittableData retransmittable,
349 IsHandshake handshake) OVERRIDE;
350 virtual QuicAckFrame* CreateAckFrame() OVERRIDE;
351 virtual QuicCongestionFeedbackFrame* CreateFeedbackFrame() OVERRIDE;
352 virtual QuicStopWaitingFrame* CreateStopWaitingFrame() OVERRIDE;
353 virtual bool OnSerializedPacket(const SerializedPacket& packet) OVERRIDE;
355 // Accessors
356 void set_visitor(QuicConnectionVisitorInterface* visitor) {
357 visitor_ = visitor;
359 void set_debug_visitor(QuicConnectionDebugVisitor* debug_visitor) {
360 debug_visitor_ = debug_visitor;
361 packet_generator_.set_debug_delegate(debug_visitor);
362 sent_packet_manager_.set_debug_delegate(debug_visitor);
364 const IPEndPoint& self_address() const { return self_address_; }
365 const IPEndPoint& peer_address() const { return peer_address_; }
366 QuicConnectionId connection_id() const { return connection_id_; }
367 const QuicClock* clock() const { return clock_; }
368 QuicRandom* random_generator() const { return random_generator_; }
369 size_t max_packet_length() const;
370 void set_max_packet_length(size_t length);
372 bool connected() const { return connected_; }
374 // Must only be called on client connections.
375 const QuicVersionVector& server_supported_versions() const {
376 DCHECK(!is_server_);
377 return server_supported_versions_;
380 size_t NumFecGroups() const { return group_map_.size(); }
382 // Testing only.
383 size_t NumQueuedPackets() const { return queued_packets_.size(); }
385 QuicEncryptedPacket* ReleaseConnectionClosePacket() {
386 return connection_close_packet_.release();
389 // Flush any queued frames immediately. Preserves the batch write mode and
390 // does nothing if there are no pending frames.
391 void Flush();
393 // Returns true if the underlying UDP socket is writable, there is
394 // no queued data and the connection is not congestion-control
395 // blocked.
396 bool CanWriteStreamData();
398 // Returns true if the connection has queued packets or frames.
399 bool HasQueuedData() const;
401 // Sets (or resets) the idle state connection timeout. Also, checks and times
402 // out the connection if network timer has expired for |timeout|.
403 void SetIdleNetworkTimeout(QuicTime::Delta timeout);
404 // Sets (or resets) the total time delta the connection can be alive for.
405 // Also, checks and times out the connection if timer has expired for
406 // |timeout|. Used to limit the time a connection can be alive before crypto
407 // handshake finishes.
408 void SetOverallConnectionTimeout(QuicTime::Delta timeout);
410 // If the connection has timed out, this will close the connection and return
411 // true. Otherwise, it will return false and will reset the timeout alarm.
412 bool CheckForTimeout();
414 // Sends a ping, and resets the ping alarm.
415 void SendPing();
417 // Sets up a packet with an QuicAckFrame and sends it out.
418 void SendAck();
420 // Called when an RTO fires. Resets the retransmission alarm if there are
421 // remaining unacked packets.
422 void OnRetransmissionTimeout();
424 // Retransmits all unacked packets with retransmittable frames if
425 // |retransmission_type| is ALL_PACKETS, otherwise retransmits only initially
426 // encrypted packets. Used when the negotiated protocol version is different
427 // from what was initially assumed and when the visitor wants to re-transmit
428 // initially encrypted packets when the initial encrypter changes.
429 void RetransmitUnackedPackets(RetransmissionType retransmission_type);
431 // Calls |sent_packet_manager_|'s NeuterUnencryptedPackets. Used when the
432 // connection becomes forward secure and hasn't received acks for all packets.
433 void NeuterUnencryptedPackets();
435 // Changes the encrypter used for level |level| to |encrypter|. The function
436 // takes ownership of |encrypter|.
437 void SetEncrypter(EncryptionLevel level, QuicEncrypter* encrypter);
438 const QuicEncrypter* encrypter(EncryptionLevel level) const;
440 // SetDefaultEncryptionLevel sets the encryption level that will be applied
441 // to new packets.
442 void SetDefaultEncryptionLevel(EncryptionLevel level);
444 // SetDecrypter sets the primary decrypter, replacing any that already exists,
445 // and takes ownership. If an alternative decrypter is in place then the
446 // function DCHECKs. This is intended for cases where one knows that future
447 // packets will be using the new decrypter and the previous decrypter is now
448 // obsolete. |level| indicates the encryption level of the new decrypter.
449 void SetDecrypter(QuicDecrypter* decrypter, EncryptionLevel level);
451 // SetAlternativeDecrypter sets a decrypter that may be used to decrypt
452 // future packets and takes ownership of it. |level| indicates the encryption
453 // level of the decrypter. If |latch_once_used| is true, then the first time
454 // that the decrypter is successful it will replace the primary decrypter.
455 // Otherwise both decrypters will remain active and the primary decrypter
456 // will be the one last used.
457 void SetAlternativeDecrypter(QuicDecrypter* decrypter,
458 EncryptionLevel level,
459 bool latch_once_used);
461 const QuicDecrypter* decrypter() const;
462 const QuicDecrypter* alternative_decrypter() const;
464 bool is_server() const { return is_server_; }
466 // Returns the underlying sent packet manager.
467 const QuicSentPacketManager& sent_packet_manager() const {
468 return sent_packet_manager_;
471 bool CanWrite(HasRetransmittableData retransmittable);
473 // Stores current batch state for connection, puts the connection
474 // into batch mode, and destruction restores the stored batch state.
475 // While the bundler is in scope, any generated frames are bundled
476 // as densely as possible into packets. In addition, this bundler
477 // can be configured to ensure that an ACK frame is included in the
478 // first packet created, if there's new ack information to be sent.
479 class ScopedPacketBundler {
480 public:
481 // In addition to all outgoing frames being bundled when the
482 // bundler is in scope, setting |include_ack| to true ensures that
483 // an ACK frame is opportunistically bundled with the first
484 // outgoing packet.
485 ScopedPacketBundler(QuicConnection* connection, AckBundling send_ack);
486 ~ScopedPacketBundler();
488 private:
489 QuicConnection* connection_;
490 bool already_in_batch_mode_;
493 protected:
494 // Send a packet to the peer using encryption |level|. If |sequence_number|
495 // is present in the |retransmission_map_|, then contents of this packet will
496 // be retransmitted with a new sequence number if it's not acked by the peer.
497 // Deletes |packet| if WritePacket call succeeds, or transfers ownership to
498 // QueuedPacket, ultimately deleted in WriteQueuedPackets. Updates the
499 // entropy map corresponding to |sequence_number| using |entropy_hash|.
500 // |transmission_type| and |retransmittable| are supplied to the congestion
501 // manager, and when |forced| is true, it bypasses the congestion manager.
502 // TODO(wtc): none of the callers check the return value.
503 virtual bool SendOrQueuePacket(EncryptionLevel level,
504 const SerializedPacket& packet,
505 TransmissionType transmission_type);
507 QuicConnectionHelperInterface* helper() { return helper_; }
509 // Selects and updates the version of the protocol being used by selecting a
510 // version from |available_versions| which is also supported. Returns true if
511 // such a version exists, false otherwise.
512 bool SelectMutualVersion(const QuicVersionVector& available_versions);
514 QuicPacketWriter* writer() { return writer_; }
516 private:
517 friend class test::QuicConnectionPeer;
519 // Packets which have not been written to the wire.
520 // Owns the QuicPacket* packet.
521 struct QueuedPacket {
522 QueuedPacket(SerializedPacket packet,
523 EncryptionLevel level,
524 TransmissionType transmission_type);
526 QuicPacketSequenceNumber sequence_number;
527 QuicPacket* packet;
528 const EncryptionLevel encryption_level;
529 TransmissionType transmission_type;
530 HasRetransmittableData retransmittable;
531 IsHandshake handshake;
532 PacketType type;
533 QuicByteCount length;
536 typedef std::list<QueuedPacket> QueuedPacketList;
537 typedef std::map<QuicFecGroupNumber, QuicFecGroup*> FecGroupMap;
539 // Writes the given packet to socket, encrypted with packet's
540 // encryption_level. Returns true on successful write. Behavior is undefined
541 // if connection is not established or broken. A return value of true means
542 // the packet was transmitted and may be destroyed. If the packet is
543 // retransmittable, WritePacket sets up retransmission for a successful write.
544 // If packet is FORCE, then it will be sent immediately and the send scheduler
545 // will not be consulted.
546 bool WritePacket(QueuedPacket packet);
548 // Make sure an ack we got from our peer is sane.
549 bool ValidateAckFrame(const QuicAckFrame& incoming_ack);
551 // Make sure a stop waiting we got from our peer is sane.
552 bool ValidateStopWaitingFrame(const QuicStopWaitingFrame& stop_waiting);
554 // Sends a version negotiation packet to the peer.
555 void SendVersionNegotiationPacket();
557 // Clears any accumulated frames from the last received packet.
558 void ClearLastFrames();
560 // Writes as many queued packets as possible. The connection must not be
561 // blocked when this is called.
562 void WriteQueuedPackets();
564 // Writes as many pending retransmissions as possible.
565 void WritePendingRetransmissions();
567 // Returns true if the packet should be discarded and not sent.
568 bool ShouldDiscardPacket(EncryptionLevel level,
569 QuicPacketSequenceNumber sequence_number,
570 HasRetransmittableData retransmittable);
572 // Queues |packet| in the hopes that it can be decrypted in the
573 // future, when a new key is installed.
574 void QueueUndecryptablePacket(const QuicEncryptedPacket& packet);
576 // Attempts to process any queued undecryptable packets.
577 void MaybeProcessUndecryptablePackets();
579 // If a packet can be revived from the current FEC group, then
580 // revive and process the packet.
581 void MaybeProcessRevivedPacket();
583 void ProcessAckFrame(const QuicAckFrame& incoming_ack);
585 void ProcessStopWaitingFrame(const QuicStopWaitingFrame& stop_waiting);
587 // Update |stop_waiting| for an outgoing ack.
588 void UpdateStopWaiting(QuicStopWaitingFrame* stop_waiting);
590 // Queues an ack or sets the ack alarm when an incoming packet arrives that
591 // should be acked.
592 void MaybeQueueAck();
594 // Checks if the last packet should instigate an ack.
595 bool ShouldLastPacketInstigateAck() const;
597 // Checks if the peer is waiting for packets that have been given up on, and
598 // therefore an ack frame should be sent with a larger least_unacked.
599 void UpdateStopWaitingCount();
601 // Sends any packets which are a response to the last packet, including both
602 // acks and pending writes if an ack opened the congestion window.
603 void MaybeSendInResponseToPacket();
605 // Gets the least unacked sequence number, which is the next sequence number
606 // to be sent if there are no outstanding packets.
607 QuicPacketSequenceNumber GetLeastUnacked() const;
609 // Get the FEC group associate with the last processed packet or NULL, if the
610 // group has already been deleted.
611 QuicFecGroup* GetFecGroup();
613 // Closes any FEC groups protecting packets before |sequence_number|.
614 void CloseFecGroupsBefore(QuicPacketSequenceNumber sequence_number);
616 // Sets the ping alarm to the appropriate value, if any.
617 void SetPingAlarm();
619 // On arrival of a new packet, checks to see if the socket addresses have
620 // changed since the last packet we saw on this connection.
621 void CheckForAddressMigration(const IPEndPoint& self_address,
622 const IPEndPoint& peer_address);
624 QuicFramer framer_;
625 QuicConnectionHelperInterface* helper_; // Not owned.
626 QuicPacketWriter* writer_; // Not owned.
627 EncryptionLevel encryption_level_;
628 const QuicClock* clock_;
629 QuicRandom* random_generator_;
631 const QuicConnectionId connection_id_;
632 // Address on the last successfully processed packet received from the
633 // client.
634 IPEndPoint self_address_;
635 IPEndPoint peer_address_;
636 // Used to store latest peer port to possibly migrate to later.
637 int migrating_peer_port_;
639 bool last_packet_revived_; // True if the last packet was revived from FEC.
640 size_t last_size_; // Size of the last received packet.
641 EncryptionLevel last_decrypted_packet_level_;
642 QuicPacketHeader last_header_;
643 std::vector<QuicStreamFrame> last_stream_frames_;
644 std::vector<QuicAckFrame> last_ack_frames_;
645 std::vector<QuicCongestionFeedbackFrame> last_congestion_frames_;
646 std::vector<QuicStopWaitingFrame> last_stop_waiting_frames_;
647 std::vector<QuicRstStreamFrame> last_rst_frames_;
648 std::vector<QuicGoAwayFrame> last_goaway_frames_;
649 std::vector<QuicWindowUpdateFrame> last_window_update_frames_;
650 std::vector<QuicBlockedFrame> last_blocked_frames_;
651 std::vector<QuicConnectionCloseFrame> last_close_frames_;
653 QuicCongestionFeedbackFrame outgoing_congestion_feedback_;
655 // Track some peer state so we can do less bookkeeping
656 // Largest sequence sent by the peer which had an ack frame (latest ack info).
657 QuicPacketSequenceNumber largest_seen_packet_with_ack_;
659 // Largest sequence number sent by the peer which had a stop waiting frame.
660 QuicPacketSequenceNumber largest_seen_packet_with_stop_waiting_;
662 // Collection of packets which were received before encryption was
663 // established, but which could not be decrypted. We buffer these on
664 // the assumption that they could not be processed because they were
665 // sent with the INITIAL encryption and the CHLO message was lost.
666 std::deque<QuicEncryptedPacket*> undecryptable_packets_;
668 // When the version negotiation packet could not be sent because the socket
669 // was not writable, this is set to true.
670 bool pending_version_negotiation_packet_;
672 // When packets could not be sent because the socket was not writable,
673 // they are added to this list. All corresponding frames are in
674 // unacked_packets_ if they are to be retransmitted.
675 QueuedPacketList queued_packets_;
677 // Contains information about the current write in progress, if any.
678 scoped_ptr<QueuedPacket> pending_write_;
680 // Contains the connection close packet if the connection has been closed.
681 scoped_ptr<QuicEncryptedPacket> connection_close_packet_;
683 FecGroupMap group_map_;
685 QuicReceivedPacketManager received_packet_manager_;
686 QuicSentEntropyManager sent_entropy_manager_;
688 // Indicates whether an ack should be sent the next time we try to write.
689 bool ack_queued_;
690 // Indicates how many consecutive times an ack has arrived which indicates
691 // the peer needs to stop waiting for some packets.
692 int stop_waiting_count_;
694 // An alarm that fires when an ACK should be sent to the peer.
695 scoped_ptr<QuicAlarm> ack_alarm_;
696 // An alarm that fires when a packet needs to be retransmitted.
697 scoped_ptr<QuicAlarm> retransmission_alarm_;
698 // An alarm that is scheduled when the sent scheduler requires a
699 // a delay before sending packets and fires when the packet may be sent.
700 scoped_ptr<QuicAlarm> send_alarm_;
701 // An alarm that is scheduled when the connection can still write and there
702 // may be more data to send.
703 scoped_ptr<QuicAlarm> resume_writes_alarm_;
704 // An alarm that fires when the connection may have timed out.
705 scoped_ptr<QuicAlarm> timeout_alarm_;
706 // An alarm that fires when a ping should be sent.
707 scoped_ptr<QuicAlarm> ping_alarm_;
709 QuicConnectionVisitorInterface* visitor_;
710 QuicConnectionDebugVisitor* debug_visitor_;
711 QuicPacketGenerator packet_generator_;
713 // Network idle time before we kill of this connection.
714 QuicTime::Delta idle_network_timeout_;
715 // Overall connection timeout.
716 QuicTime::Delta overall_connection_timeout_;
718 // Statistics for this session.
719 QuicConnectionStats stats_;
721 // The time that we got a packet for this connection.
722 // This is used for timeouts, and does not indicate the packet was processed.
723 QuicTime time_of_last_received_packet_;
725 // The last time a new (non-retransmitted) packet was sent for this
726 // connection.
727 QuicTime time_of_last_sent_new_packet_;
729 // Sequence number of the last sent packet. Packets are guaranteed to be sent
730 // in sequence number order.
731 QuicPacketSequenceNumber sequence_number_of_last_sent_packet_;
733 // Sent packet manager which tracks the status of packets sent by this
734 // connection and contains the send and receive algorithms to determine when
735 // to send packets.
736 QuicSentPacketManager sent_packet_manager_;
738 // The state of connection in version negotiation finite state machine.
739 QuicVersionNegotiationState version_negotiation_state_;
741 // Tracks if the connection was created by the server.
742 bool is_server_;
744 // True by default. False if we've received or sent an explicit connection
745 // close.
746 bool connected_;
748 // Set to true if the UDP packet headers have a new IP address for the peer.
749 // If true, do not perform connection migration.
750 bool peer_ip_changed_;
752 // Set to true if the UDP packet headers have a new port for the peer.
753 // If true, and the IP has not changed, then we can migrate the connection.
754 bool peer_port_changed_;
756 // Set to true if the UDP packet headers are addressed to a different IP.
757 // We do not support connection migration when the self IP changed.
758 bool self_ip_changed_;
760 // Set to true if the UDP packet headers are addressed to a different port.
761 // We do not support connection migration when the self port changed.
762 bool self_port_changed_;
764 // If non-empty this contains the set of versions received in a
765 // version negotiation packet.
766 QuicVersionVector server_supported_versions_;
768 DISALLOW_COPY_AND_ASSIGN(QuicConnection);
771 } // namespace net
773 #endif // NET_QUIC_QUIC_CONNECTION_H_