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 #ifndef NET_QUIC_QUIC_PROTOCOL_H_
6 #define NET_QUIC_QUIC_PROTOCOL_H_
17 #include "base/basictypes.h"
18 #include "base/containers/hash_tables.h"
19 #include "base/logging.h"
20 #include "base/strings/string_piece.h"
21 #include "net/base/int128.h"
22 #include "net/base/ip_endpoint.h"
23 #include "net/base/net_export.h"
24 #include "net/quic/iovector.h"
25 #include "net/quic/quic_bandwidth.h"
26 #include "net/quic/quic_time.h"
32 class QuicAckNotifier
;
34 struct QuicPacketHeader
;
36 typedef uint64 QuicGuid
;
37 typedef uint32 QuicStreamId
;
38 typedef uint64 QuicStreamOffset
;
39 typedef uint64 QuicPacketSequenceNumber
;
40 typedef QuicPacketSequenceNumber QuicFecGroupNumber
;
41 typedef uint64 QuicPublicResetNonceProof
;
42 typedef uint8 QuicPacketEntropyHash
;
43 typedef uint32 QuicHeaderId
;
44 // QuicTag is the type of a tag in the wire protocol.
45 typedef uint32 QuicTag
;
46 typedef std::vector
<QuicTag
> QuicTagVector
;
47 typedef std::map
<QuicTag
, std::string
> QuicTagValueMap
;
48 // TODO(rtenneti): Didn't use SpdyPriority because SpdyPriority is uint8 and
49 // QuicPriority is uint32. Use SpdyPriority when we change the QUIC_VERSION.
50 typedef uint32 QuicPriority
;
52 // TODO(rch): Consider Quic specific names for these constants.
53 // Default and initial maximum size in bytes of a QUIC packet.
54 const QuicByteCount kDefaultMaxPacketSize
= 1200;
55 // The maximum packet size of any QUIC packet, based on ethernet's max size,
56 // minus the IP and UDP headers. IPv6 has a 40 byte header, UPD adds an
57 // additional 8 bytes. This is a total overhead of 48 bytes. Ethernet's
58 // max packet size is 1500 bytes, 1500 - 48 = 1452.
59 const QuicByteCount kMaxPacketSize
= 1452;
61 // Maximum size of the initial congestion window in packets.
62 const size_t kDefaultInitialWindow
= 10;
63 const size_t kMaxInitialWindow
= 100;
65 // Maximum size of the congestion window, in packets, for TCP congestion control
67 const size_t kMaxTcpCongestionWindow
= 200;
69 // Don't allow a client to suggest an RTT longer than 15 seconds.
70 const size_t kMaxInitialRoundTripTimeUs
= 15 * kNumMicrosPerSecond
;
72 // Maximum number of open streams per connection.
73 const size_t kDefaultMaxStreamsPerConnection
= 100;
75 // Number of bytes reserved for public flags in the packet header.
76 const size_t kPublicFlagsSize
= 1;
77 // Number of bytes reserved for version number in the packet header.
78 const size_t kQuicVersionSize
= 4;
79 // Number of bytes reserved for private flags in the packet header.
80 const size_t kPrivateFlagsSize
= 1;
81 // Number of bytes reserved for FEC group in the packet header.
82 const size_t kFecGroupSize
= 1;
83 // TODO(wtc): remove this when we drop support for QUIC_VERSION_13.
84 // Number of bytes reserved for the nonce proof in public reset packet.
85 const size_t kPublicResetNonceSize
= 8;
87 // Signifies that the QuicPacket will contain version of the protocol.
88 const bool kIncludeVersion
= true;
90 // Index of the first byte in a QUIC packet which is used in hash calculation.
91 const size_t kStartOfHashData
= 0;
93 // Limit on the delta between stream IDs.
94 const QuicStreamId kMaxStreamIdDelta
= 100;
95 // Limit on the delta between header IDs.
96 const QuicHeaderId kMaxHeaderIdDelta
= 100;
98 // Reserved ID for the crypto stream.
99 const QuicStreamId kCryptoStreamId
= 1;
101 // Reserved ID for the headers stream.
102 const QuicStreamId kHeadersStreamId
= 3;
104 // This is the default network timeout a for connection till the crypto
105 // handshake succeeds and the negotiated timeout from the handshake is received.
106 const int64 kDefaultInitialTimeoutSecs
= 120; // 2 mins.
107 const int64 kDefaultTimeoutSecs
= 60 * 10; // 10 minutes.
108 const int64 kDefaultMaxTimeForCryptoHandshakeSecs
= 5; // 5 secs.
110 // We define an unsigned 16-bit floating point value, inspired by IEEE floats
111 // (http://en.wikipedia.org/wiki/Half_precision_floating-point_format),
112 // with 5-bit exponent (bias 1), 11-bit mantissa (effective 12 with hidden
113 // bit) and denormals, but without signs, transfinites or fractions. Wire format
114 // 16 bits (little-endian byte order) are split into exponent (high 5) and
115 // mantissa (low 11) and decoded as:
117 // if (exponent == 0) value = mantissa;
118 // else value = (mantissa | 1 << 11) << (exponent - 1)
119 const int kUFloat16ExponentBits
= 5;
120 const int kUFloat16MaxExponent
= (1 << kUFloat16ExponentBits
) - 2; // 30
121 const int kUFloat16MantissaBits
= 16 - kUFloat16ExponentBits
; // 11
122 const int kUFloat16MantissaEffectiveBits
= kUFloat16MantissaBits
+ 1; // 12
123 const uint64 kUFloat16MaxValue
= // 0x3FFC0000000
124 ((GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits
) - 1) <<
125 kUFloat16MaxExponent
;
127 enum TransmissionType
{
134 enum RetransmissionType
{
135 INITIAL_ENCRYPTION_ONLY
,
139 enum HasRetransmittableData
{
140 NO_RETRANSMITTABLE_DATA
,
141 HAS_RETRANSMITTABLE_DATA
,
150 // Regular frame types. The values set here cannot change without the
151 // introduction of a new QUIC version.
153 RST_STREAM_FRAME
= 1,
154 CONNECTION_CLOSE_FRAME
= 2,
156 WINDOW_UPDATE_FRAME
= 4,
158 STOP_WAITING_FRAME
= 6,
160 // STREAM, ACK, and CONGESTION_FEEDBACK frames are special frames. They are
161 // encoded differently on the wire and their values do not need to be stable.
164 CONGESTION_FEEDBACK_FRAME
,
168 enum QuicGuidLength
{
169 PACKET_0BYTE_GUID
= 0,
170 PACKET_1BYTE_GUID
= 1,
171 PACKET_4BYTE_GUID
= 4,
172 PACKET_8BYTE_GUID
= 8
180 enum QuicSequenceNumberLength
{
181 PACKET_1BYTE_SEQUENCE_NUMBER
= 1,
182 PACKET_2BYTE_SEQUENCE_NUMBER
= 2,
183 PACKET_4BYTE_SEQUENCE_NUMBER
= 4,
184 PACKET_6BYTE_SEQUENCE_NUMBER
= 6
187 // Used to indicate a QuicSequenceNumberLength using two flag bits.
188 enum QuicSequenceNumberLengthFlags
{
189 PACKET_FLAGS_1BYTE_SEQUENCE
= 0, // 00
190 PACKET_FLAGS_2BYTE_SEQUENCE
= 1, // 01
191 PACKET_FLAGS_4BYTE_SEQUENCE
= 1 << 1, // 10
192 PACKET_FLAGS_6BYTE_SEQUENCE
= 1 << 1 | 1, // 11
195 // The public flags are specified in one byte.
196 enum QuicPacketPublicFlags
{
197 PACKET_PUBLIC_FLAGS_NONE
= 0,
199 // Bit 0: Does the packet header contains version info?
200 PACKET_PUBLIC_FLAGS_VERSION
= 1 << 0,
202 // Bit 1: Is this packet a public reset packet?
203 PACKET_PUBLIC_FLAGS_RST
= 1 << 1,
205 // Bits 2 and 3 specify the length of the GUID as follows:
210 PACKET_PUBLIC_FLAGS_0BYTE_GUID
= 0,
211 PACKET_PUBLIC_FLAGS_1BYTE_GUID
= 1 << 2,
212 PACKET_PUBLIC_FLAGS_4BYTE_GUID
= 1 << 3,
213 PACKET_PUBLIC_FLAGS_8BYTE_GUID
= 1 << 3 | 1 << 2,
215 // Bits 4 and 5 describe the packet sequence number length as follows:
220 PACKET_PUBLIC_FLAGS_1BYTE_SEQUENCE
= PACKET_FLAGS_1BYTE_SEQUENCE
<< 4,
221 PACKET_PUBLIC_FLAGS_2BYTE_SEQUENCE
= PACKET_FLAGS_2BYTE_SEQUENCE
<< 4,
222 PACKET_PUBLIC_FLAGS_4BYTE_SEQUENCE
= PACKET_FLAGS_4BYTE_SEQUENCE
<< 4,
223 PACKET_PUBLIC_FLAGS_6BYTE_SEQUENCE
= PACKET_FLAGS_6BYTE_SEQUENCE
<< 4,
225 // All bits set (bits 6 and 7 are not currently used): 00111111
226 PACKET_PUBLIC_FLAGS_MAX
= (1 << 6) - 1
229 // The private flags are specified in one byte.
230 enum QuicPacketPrivateFlags
{
231 PACKET_PRIVATE_FLAGS_NONE
= 0,
233 // Bit 0: Does this packet contain an entropy bit?
234 PACKET_PRIVATE_FLAGS_ENTROPY
= 1 << 0,
236 // Bit 1: Payload is part of an FEC group?
237 PACKET_PRIVATE_FLAGS_FEC_GROUP
= 1 << 1,
239 // Bit 2: Payload is FEC as opposed to frames?
240 PACKET_PRIVATE_FLAGS_FEC
= 1 << 2,
242 // All bits set (bits 3-7 are not currently used): 00000111
243 PACKET_PRIVATE_FLAGS_MAX
= (1 << 3) - 1
246 // The available versions of QUIC. Guaranteed that the integer value of the enum
247 // will match the version number.
248 // When adding a new version to this enum you should add it to
249 // kSupportedQuicVersions (if appropriate), and also add a new case to the
250 // helper methods QuicVersionToQuicTag, QuicTagToQuicVersion, and
251 // QuicVersionToString.
253 // Special case to indicate unknown/unsupported QUIC version.
254 QUIC_VERSION_UNSUPPORTED
= 0,
256 QUIC_VERSION_12
= 12,
257 QUIC_VERSION_13
= 13,
258 QUIC_VERSION_14
= 14,
259 QUIC_VERSION_15
= 15,
260 QUIC_VERSION_16
= 16, // Current version.
263 // This vector contains QUIC versions which we currently support.
264 // This should be ordered such that the highest supported version is the first
265 // element, with subsequent elements in descending order (versions can be
266 // skipped as necessary).
268 // IMPORTANT: if you are addding to this list, follow the instructions at
269 // http://sites/quic/adding-and-removing-versions
270 static const QuicVersion kSupportedQuicVersions
[] = {QUIC_VERSION_16
,
276 typedef std::vector
<QuicVersion
> QuicVersionVector
;
278 // Returns a vector of QUIC versions in kSupportedQuicVersions.
279 NET_EXPORT_PRIVATE QuicVersionVector
QuicSupportedVersions();
281 // QuicTag is written to and read from the wire, but we prefer to use
282 // the more readable QuicVersion at other levels.
283 // Helper function which translates from a QuicVersion to a QuicTag. Returns 0
284 // if QuicVersion is unsupported.
285 NET_EXPORT_PRIVATE QuicTag
QuicVersionToQuicTag(const QuicVersion version
);
287 // Returns appropriate QuicVersion from a QuicTag.
288 // Returns QUIC_VERSION_UNSUPPORTED if version_tag cannot be understood.
289 NET_EXPORT_PRIVATE QuicVersion
QuicTagToQuicVersion(const QuicTag version_tag
);
291 // Helper function which translates from a QuicVersion to a string.
292 // Returns strings corresponding to enum names (e.g. QUIC_VERSION_6).
293 NET_EXPORT_PRIVATE
std::string
QuicVersionToString(const QuicVersion version
);
295 // Returns comma separated list of string representations of QuicVersion enum
296 // values in the supplied |versions| vector.
297 NET_EXPORT_PRIVATE
std::string
QuicVersionVectorToString(
298 const QuicVersionVector
& versions
);
300 // Version and Crypto tags are written to the wire with a big-endian
301 // representation of the name of the tag. For example
302 // the client hello tag (CHLO) will be written as the
303 // following 4 bytes: 'C' 'H' 'L' 'O'. Since it is
304 // stored in memory as a little endian uint32, we need
305 // to reverse the order of the bytes.
307 // MakeQuicTag returns a value given the four bytes. For example:
308 // MakeQuicTag('C', 'H', 'L', 'O');
309 NET_EXPORT_PRIVATE QuicTag
MakeQuicTag(char a
, char b
, char c
, char d
);
311 // Size in bytes of the data or fec packet header.
312 NET_EXPORT_PRIVATE
size_t GetPacketHeaderSize(const QuicPacketHeader
& header
);
314 NET_EXPORT_PRIVATE
size_t GetPacketHeaderSize(
315 QuicGuidLength guid_length
,
316 bool include_version
,
317 QuicSequenceNumberLength sequence_number_length
,
318 InFecGroup is_in_fec_group
);
320 // Index of the first byte in a QUIC packet of FEC protected data.
321 NET_EXPORT_PRIVATE
size_t GetStartOfFecProtectedData(
322 QuicGuidLength guid_length
,
323 bool include_version
,
324 QuicSequenceNumberLength sequence_number_length
);
325 // Index of the first byte in a QUIC packet of encrypted data.
326 NET_EXPORT_PRIVATE
size_t GetStartOfEncryptedData(
327 QuicGuidLength guid_length
,
328 bool include_version
,
329 QuicSequenceNumberLength sequence_number_length
);
331 enum QuicRstStreamErrorCode
{
332 QUIC_STREAM_NO_ERROR
= 0,
334 // There was some error which halted stream processing.
335 QUIC_ERROR_PROCESSING_STREAM
,
336 // We got two fin or reset offsets which did not match.
337 QUIC_MULTIPLE_TERMINATION_OFFSETS
,
338 // We got bad payload and can not respond to it at the protocol level.
339 QUIC_BAD_APPLICATION_PAYLOAD
,
340 // Stream closed due to connection error. No reset frame is sent when this
342 QUIC_STREAM_CONNECTION_ERROR
,
343 // GoAway frame sent. No more stream can be created.
344 QUIC_STREAM_PEER_GOING_AWAY
,
345 // The stream has been cancelled.
346 QUIC_STREAM_CANCELLED
,
348 // No error. Used as bound while iterating.
349 QUIC_STREAM_LAST_ERROR
,
352 // These values must remain stable as they are uploaded to UMA histograms.
353 // To add a new error code, use the current value of QUIC_LAST_ERROR and
354 // increment QUIC_LAST_ERROR.
358 // Connection has reached an invalid state.
359 QUIC_INTERNAL_ERROR
= 1,
360 // There were data frames after the a fin or reset.
361 QUIC_STREAM_DATA_AFTER_TERMINATION
= 2,
362 // Control frame is malformed.
363 QUIC_INVALID_PACKET_HEADER
= 3,
364 // Frame data is malformed.
365 QUIC_INVALID_FRAME_DATA
= 4,
366 // The packet contained no payload.
367 QUIC_MISSING_PAYLOAD
= 48,
368 // FEC data is malformed.
369 QUIC_INVALID_FEC_DATA
= 5,
370 // STREAM frame data is malformed.
371 QUIC_INVALID_STREAM_DATA
= 46,
372 // RST_STREAM frame data is malformed.
373 QUIC_INVALID_RST_STREAM_DATA
= 6,
374 // CONNECTION_CLOSE frame data is malformed.
375 QUIC_INVALID_CONNECTION_CLOSE_DATA
= 7,
376 // GOAWAY frame data is malformed.
377 QUIC_INVALID_GOAWAY_DATA
= 8,
378 // WINDOW_UPDATE frame data is malformed.
379 QUIC_INVALID_WINDOW_UPDATE_DATA
= 57,
380 // BLOCKED frame data is malformed.
381 QUIC_INVALID_BLOCKED_DATA
= 58,
382 // STOP_WAITING frame data is malformed.
383 QUIC_INVALID_STOP_WAITING_DATA
= 60,
384 // ACK frame data is malformed.
385 QUIC_INVALID_ACK_DATA
= 9,
386 // CONGESTION_FEEDBACK frame data is malformed.
387 QUIC_INVALID_CONGESTION_FEEDBACK_DATA
= 47,
388 // Version negotiation packet is malformed.
389 QUIC_INVALID_VERSION_NEGOTIATION_PACKET
= 10,
390 // Public RST packet is malformed.
391 QUIC_INVALID_PUBLIC_RST_PACKET
= 11,
392 // There was an error decrypting.
393 QUIC_DECRYPTION_FAILURE
= 12,
394 // There was an error encrypting.
395 QUIC_ENCRYPTION_FAILURE
= 13,
396 // The packet exceeded kMaxPacketSize.
397 QUIC_PACKET_TOO_LARGE
= 14,
398 // Data was sent for a stream which did not exist.
399 QUIC_PACKET_FOR_NONEXISTENT_STREAM
= 15,
400 // The peer is going away. May be a client or server.
401 QUIC_PEER_GOING_AWAY
= 16,
402 // A stream ID was invalid.
403 QUIC_INVALID_STREAM_ID
= 17,
404 // A priority was invalid.
405 QUIC_INVALID_PRIORITY
= 49,
406 // Too many streams already open.
407 QUIC_TOO_MANY_OPEN_STREAMS
= 18,
408 // Received public reset for this connection.
409 QUIC_PUBLIC_RESET
= 19,
410 // Invalid protocol version.
411 QUIC_INVALID_VERSION
= 20,
412 // Stream reset before headers decompressed.
413 QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED
= 21,
414 // The Header ID for a stream was too far from the previous.
415 QUIC_INVALID_HEADER_ID
= 22,
416 // Negotiable parameter received during handshake had invalid value.
417 QUIC_INVALID_NEGOTIATED_VALUE
= 23,
418 // There was an error decompressing data.
419 QUIC_DECOMPRESSION_FAILURE
= 24,
420 // We hit our prenegotiated (or default) timeout
421 QUIC_CONNECTION_TIMED_OUT
= 25,
422 // There was an error encountered migrating addresses
423 QUIC_ERROR_MIGRATING_ADDRESS
= 26,
424 // There was an error while writing to the socket.
425 QUIC_PACKET_WRITE_ERROR
= 27,
426 // There was an error while reading from the socket.
427 QUIC_PACKET_READ_ERROR
= 51,
428 // We received a STREAM_FRAME with no data and no fin flag set.
429 QUIC_INVALID_STREAM_FRAME
= 50,
430 // We received invalid data on the headers stream.
431 QUIC_INVALID_HEADERS_STREAM_DATA
= 56,
432 // The peer violated the flow control protocol.
433 QUIC_FLOW_CONTROL_ERROR
= 59,
438 QUIC_HANDSHAKE_FAILED
= 28,
439 // Handshake message contained out of order tags.
440 QUIC_CRYPTO_TAGS_OUT_OF_ORDER
= 29,
441 // Handshake message contained too many entries.
442 QUIC_CRYPTO_TOO_MANY_ENTRIES
= 30,
443 // Handshake message contained an invalid value length.
444 QUIC_CRYPTO_INVALID_VALUE_LENGTH
= 31,
445 // A crypto message was received after the handshake was complete.
446 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE
= 32,
447 // A crypto message was received with an illegal message tag.
448 QUIC_INVALID_CRYPTO_MESSAGE_TYPE
= 33,
449 // A crypto message was received with an illegal parameter.
450 QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER
= 34,
451 // An invalid channel id signature was supplied.
452 QUIC_INVALID_CHANNEL_ID_SIGNATURE
= 52,
453 // A crypto message was received with a mandatory parameter missing.
454 QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND
= 35,
455 // A crypto message was received with a parameter that has no overlap
456 // with the local parameter.
457 QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP
= 36,
458 // A crypto message was received that contained a parameter with too few
460 QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND
= 37,
461 // An internal error occured in crypto processing.
462 QUIC_CRYPTO_INTERNAL_ERROR
= 38,
463 // A crypto handshake message specified an unsupported version.
464 QUIC_CRYPTO_VERSION_NOT_SUPPORTED
= 39,
465 // There was no intersection between the crypto primitives supported by the
466 // peer and ourselves.
467 QUIC_CRYPTO_NO_SUPPORT
= 40,
468 // The server rejected our client hello messages too many times.
469 QUIC_CRYPTO_TOO_MANY_REJECTS
= 41,
470 // The client rejected the server's certificate chain or signature.
471 QUIC_PROOF_INVALID
= 42,
472 // A crypto message was received with a duplicate tag.
473 QUIC_CRYPTO_DUPLICATE_TAG
= 43,
474 // A crypto message was received with the wrong encryption level (i.e. it
475 // should have been encrypted but was not.)
476 QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT
= 44,
477 // The server config for a server has expired.
478 QUIC_CRYPTO_SERVER_CONFIG_EXPIRED
= 45,
479 // We failed to setup the symmetric keys for a connection.
480 QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED
= 53,
481 // A handshake message arrived, but we are still validating the
482 // previous handshake message.
483 QUIC_CRYPTO_MESSAGE_WHILE_VALIDATING_CLIENT_HELLO
= 54,
484 // This connection involved a version negotiation which appears to have been
486 QUIC_VERSION_NEGOTIATION_MISMATCH
= 55,
488 // No error. Used as bound while iterating.
489 QUIC_LAST_ERROR
= 61,
492 struct NET_EXPORT_PRIVATE QuicPacketPublicHeader
{
493 QuicPacketPublicHeader();
494 explicit QuicPacketPublicHeader(const QuicPacketPublicHeader
& other
);
495 ~QuicPacketPublicHeader();
497 // Universal header. All QuicPacket headers will have a guid and public flags.
499 QuicGuidLength guid_length
;
502 QuicSequenceNumberLength sequence_number_length
;
503 QuicVersionVector versions
;
506 // Header for Data or FEC packets.
507 struct NET_EXPORT_PRIVATE QuicPacketHeader
{
509 explicit QuicPacketHeader(const QuicPacketPublicHeader
& header
);
511 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
512 std::ostream
& os
, const QuicPacketHeader
& s
);
514 QuicPacketPublicHeader public_header
;
517 QuicPacketEntropyHash entropy_hash
;
518 QuicPacketSequenceNumber packet_sequence_number
;
519 InFecGroup is_in_fec_group
;
520 QuicFecGroupNumber fec_group
;
523 struct NET_EXPORT_PRIVATE QuicPublicResetPacket
{
524 QuicPublicResetPacket();
525 explicit QuicPublicResetPacket(const QuicPacketPublicHeader
& header
);
527 QuicPacketPublicHeader public_header
;
528 QuicPublicResetNonceProof nonce_proof
;
529 QuicPacketSequenceNumber rejected_sequence_number
;
530 IPEndPoint client_address
;
533 enum QuicVersionNegotiationState
{
534 START_NEGOTIATION
= 0,
535 // Server-side this implies we've sent a version negotiation packet and are
536 // waiting on the client to select a compatible version. Client-side this
537 // implies we've gotten a version negotiation packet, are retransmitting the
538 // initial packets with a supported version and are waiting for our first
539 // packet from the server.
540 NEGOTIATION_IN_PROGRESS
,
541 // This indicates this endpoint has received a packet from the peer with a
542 // version this endpoint supports. Version negotiation is complete, and the
543 // version number will no longer be sent with future packets.
547 typedef QuicPacketPublicHeader QuicVersionNegotiationPacket
;
549 // A padding frame contains no payload.
550 struct NET_EXPORT_PRIVATE QuicPaddingFrame
{
553 struct NET_EXPORT_PRIVATE QuicStreamFrame
{
555 QuicStreamFrame(const QuicStreamFrame
& frame
);
556 QuicStreamFrame(QuicStreamId stream_id
,
558 QuicStreamOffset offset
,
561 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
562 std::ostream
& os
, const QuicStreamFrame
& s
);
564 // Returns a copy of the IOVector |data| as a heap-allocated string.
565 // Caller must take ownership of the returned string.
566 std::string
* GetDataAsString() const;
568 QuicStreamId stream_id
;
570 QuicStreamOffset offset
; // Location of this data in the stream.
573 // If this is set, then when this packet is ACKed the AckNotifier will be
575 QuicAckNotifier
* notifier
;
578 // TODO(ianswett): Re-evaluate the trade-offs of hash_set vs set when framing
580 typedef std::set
<QuicPacketSequenceNumber
> SequenceNumberSet
;
581 // TODO(pwestin): Add a way to enforce the max size of this map.
582 typedef std::map
<QuicPacketSequenceNumber
, QuicTime
> TimeMap
;
584 struct NET_EXPORT_PRIVATE ReceivedPacketInfo
{
585 ReceivedPacketInfo();
586 ~ReceivedPacketInfo();
588 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
589 std::ostream
& os
, const ReceivedPacketInfo
& s
);
591 // Entropy hash of all packets up to largest observed not including missing
593 QuicPacketEntropyHash entropy_hash
;
595 // The highest packet sequence number we've observed from the peer.
597 // In general, this should be the largest packet number we've received. In
598 // the case of truncated acks, we may have to advertise a lower "upper bound"
599 // than largest received, to avoid implicitly acking missing packets that
600 // don't fit in the missing packet list due to size limitations. In this
601 // case, largest_observed may be a packet which is also in the missing packets
603 QuicPacketSequenceNumber largest_observed
;
605 // Time elapsed since largest_observed was received until this Ack frame was
607 QuicTime::Delta delta_time_largest_observed
;
609 // TODO(satyamshekhar): Can be optimized using an interval set like data
611 // The set of packets which we're expecting and have not received.
612 SequenceNumberSet missing_packets
;
614 // Whether the ack had to be truncated when sent.
617 // Packets which have been revived via FEC.
618 // All of these must also be in missing_packets.
619 SequenceNumberSet revived_packets
;
622 // True if the sequence number is greater than largest_observed or is listed
624 // Always returns false for sequence numbers less than least_unacked.
625 bool NET_EXPORT_PRIVATE
IsAwaitingPacket(
626 const ReceivedPacketInfo
& received_info
,
627 QuicPacketSequenceNumber sequence_number
);
629 // Inserts missing packets between [lower, higher).
630 void NET_EXPORT_PRIVATE
InsertMissingPacketsBetween(
631 ReceivedPacketInfo
* received_info
,
632 QuicPacketSequenceNumber lower
,
633 QuicPacketSequenceNumber higher
);
635 struct NET_EXPORT_PRIVATE QuicStopWaitingFrame
{
636 QuicStopWaitingFrame();
637 ~QuicStopWaitingFrame();
639 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
640 std::ostream
& os
, const QuicStopWaitingFrame
& s
);
642 // Entropy hash of all packets up to, but not including, the least unacked
644 QuicPacketEntropyHash entropy_hash
;
645 // The lowest packet we've sent which is unacked, and we expect an ack for.
646 QuicPacketSequenceNumber least_unacked
;
649 struct NET_EXPORT_PRIVATE QuicAckFrame
{
651 // Testing convenience method to construct a QuicAckFrame with all packets
652 // from least_unacked to largest_observed acked.
653 QuicAckFrame(QuicPacketSequenceNumber largest_observed
,
654 QuicTime largest_observed_receive_time
,
655 QuicPacketSequenceNumber least_unacked
);
657 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
658 std::ostream
& os
, const QuicAckFrame
& s
);
660 QuicStopWaitingFrame sent_info
;
661 ReceivedPacketInfo received_info
;
664 // Defines for all types of congestion feedback that will be negotiated in QUIC,
665 // kTCP MUST be supported by all QUIC implementations to guarantee 100%
667 enum CongestionFeedbackType
{
668 kTCP
, // Used to mimic TCP.
669 kInterArrival
, // Use additional inter arrival information.
670 kFixRate
, // Provided for testing.
673 struct NET_EXPORT_PRIVATE CongestionFeedbackMessageTCP
{
674 CongestionFeedbackMessageTCP();
676 QuicByteCount receive_window
;
679 struct NET_EXPORT_PRIVATE CongestionFeedbackMessageInterArrival
{
680 CongestionFeedbackMessageInterArrival();
681 ~CongestionFeedbackMessageInterArrival();
683 // The set of received packets since the last feedback was sent, along with
684 // their arrival times.
685 TimeMap received_packet_times
;
688 struct NET_EXPORT_PRIVATE CongestionFeedbackMessageFixRate
{
689 CongestionFeedbackMessageFixRate();
690 QuicBandwidth bitrate
;
693 struct NET_EXPORT_PRIVATE QuicCongestionFeedbackFrame
{
694 QuicCongestionFeedbackFrame();
695 ~QuicCongestionFeedbackFrame();
697 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
698 std::ostream
& os
, const QuicCongestionFeedbackFrame
& c
);
700 CongestionFeedbackType type
;
701 // This should really be a union, but since the inter arrival struct
702 // is non-trivial, C++ prohibits it.
703 CongestionFeedbackMessageTCP tcp
;
704 CongestionFeedbackMessageInterArrival inter_arrival
;
705 CongestionFeedbackMessageFixRate fix_rate
;
708 struct NET_EXPORT_PRIVATE QuicRstStreamFrame
{
709 QuicRstStreamFrame();
710 QuicRstStreamFrame(QuicStreamId stream_id
,
711 QuicRstStreamErrorCode error_code
,
712 QuicStreamOffset bytes_written
);
714 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
715 std::ostream
& os
, const QuicRstStreamFrame
& r
);
717 QuicStreamId stream_id
;
718 QuicRstStreamErrorCode error_code
;
719 std::string error_details
;
721 // Used to update flow control windows. On termination of a stream, both
722 // endpoints must inform the peer of the number of bytes they have sent on
723 // that stream. This can be done through normal termination (data packet with
724 // FIN) or through a RST.
725 QuicStreamOffset byte_offset
;
728 struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame
{
729 QuicConnectionCloseFrame();
731 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
732 std::ostream
& os
, const QuicConnectionCloseFrame
& c
);
734 QuicErrorCode error_code
;
735 std::string error_details
;
738 struct NET_EXPORT_PRIVATE QuicGoAwayFrame
{
740 QuicGoAwayFrame(QuicErrorCode error_code
,
741 QuicStreamId last_good_stream_id
,
742 const std::string
& reason
);
744 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
745 std::ostream
& os
, const QuicGoAwayFrame
& g
);
747 QuicErrorCode error_code
;
748 QuicStreamId last_good_stream_id
;
749 std::string reason_phrase
;
752 // Flow control updates per-stream and at the connection levoel.
753 // Based on SPDY's WINDOW_UPDATE frame, but uses an absolute byte offset rather
754 // than a window delta.
755 // TODO(rjshade): A possible future optimization is to make stream_id and
756 // byte_offset variable length, similar to stream frames.
757 struct NET_EXPORT_PRIVATE QuicWindowUpdateFrame
{
758 QuicWindowUpdateFrame() {}
759 QuicWindowUpdateFrame(QuicStreamId stream_id
, QuicStreamOffset byte_offset
);
761 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
762 std::ostream
& os
, const QuicWindowUpdateFrame
& w
);
764 // The stream this frame applies to. 0 is a special case meaning the overall
765 // connection rather than a specific stream.
766 QuicStreamId stream_id
;
768 // Byte offset in the stream or connection. The receiver of this frame must
769 // not send data which would result in this offset being exceeded.
770 QuicStreamOffset byte_offset
;
773 // The BLOCKED frame is used to indicate to the remote endpoint that this
774 // endpoint believes itself to be flow-control blocked but otherwise ready to
775 // send data. The BLOCKED frame is purely advisory and optional.
776 // Based on SPDY's BLOCKED frame (undocumented as of 2014-01-28).
777 struct NET_EXPORT_PRIVATE QuicBlockedFrame
{
778 QuicBlockedFrame() {}
779 explicit QuicBlockedFrame(QuicStreamId stream_id
);
781 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
782 std::ostream
& os
, const QuicBlockedFrame
& b
);
784 // The stream this frame applies to. 0 is a special case meaning the overall
785 // connection rather than a specific stream.
786 QuicStreamId stream_id
;
789 // EncryptionLevel enumerates the stages of encryption that a QUIC connection
790 // progresses through. When retransmitting a packet, the encryption level needs
791 // to be specified so that it is retransmitted at a level which the peer can
793 enum EncryptionLevel
{
795 ENCRYPTION_INITIAL
= 1,
796 ENCRYPTION_FORWARD_SECURE
= 2,
798 NUM_ENCRYPTION_LEVELS
,
801 struct NET_EXPORT_PRIVATE QuicFrame
{
803 explicit QuicFrame(QuicPaddingFrame
* padding_frame
);
804 explicit QuicFrame(QuicStreamFrame
* stream_frame
);
805 explicit QuicFrame(QuicAckFrame
* frame
);
806 explicit QuicFrame(QuicCongestionFeedbackFrame
* frame
);
807 explicit QuicFrame(QuicRstStreamFrame
* frame
);
808 explicit QuicFrame(QuicConnectionCloseFrame
* frame
);
809 explicit QuicFrame(QuicStopWaitingFrame
* frame
);
810 explicit QuicFrame(QuicGoAwayFrame
* frame
);
811 explicit QuicFrame(QuicWindowUpdateFrame
* frame
);
812 explicit QuicFrame(QuicBlockedFrame
* frame
);
814 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
815 std::ostream
& os
, const QuicFrame
& frame
);
819 QuicPaddingFrame
* padding_frame
;
820 QuicStreamFrame
* stream_frame
;
821 QuicAckFrame
* ack_frame
;
822 QuicCongestionFeedbackFrame
* congestion_feedback_frame
;
823 QuicStopWaitingFrame
* stop_waiting_frame
;
824 QuicRstStreamFrame
* rst_stream_frame
;
825 QuicConnectionCloseFrame
* connection_close_frame
;
826 QuicGoAwayFrame
* goaway_frame
;
827 QuicWindowUpdateFrame
* window_update_frame
;
828 QuicBlockedFrame
* blocked_frame
;
832 typedef std::vector
<QuicFrame
> QuicFrames
;
834 struct NET_EXPORT_PRIVATE QuicFecData
{
837 // The FEC group number is also the sequence number of the first
838 // FEC protected packet. The last protected packet's sequence number will
839 // be one less than the sequence number of the FEC packet.
840 QuicFecGroupNumber fec_group
;
841 base::StringPiece redundancy
;
844 class NET_EXPORT_PRIVATE QuicData
{
846 QuicData(const char* buffer
, size_t length
);
847 QuicData(char* buffer
, size_t length
, bool owns_buffer
);
850 base::StringPiece
AsStringPiece() const {
851 return base::StringPiece(data(), length());
854 const char* data() const { return buffer_
; }
855 size_t length() const { return length_
; }
862 DISALLOW_COPY_AND_ASSIGN(QuicData
);
865 class NET_EXPORT_PRIVATE QuicPacket
: public QuicData
{
867 static QuicPacket
* NewDataPacket(
871 QuicGuidLength guid_length
,
872 bool includes_version
,
873 QuicSequenceNumberLength sequence_number_length
) {
874 return new QuicPacket(buffer
, length
, owns_buffer
, guid_length
,
875 includes_version
, sequence_number_length
, false);
878 static QuicPacket
* NewFecPacket(
882 QuicGuidLength guid_length
,
883 bool includes_version
,
884 QuicSequenceNumberLength sequence_number_length
) {
885 return new QuicPacket(buffer
, length
, owns_buffer
, guid_length
,
886 includes_version
, sequence_number_length
, true);
889 base::StringPiece
FecProtectedData() const;
890 base::StringPiece
AssociatedData() const;
891 base::StringPiece
BeforePlaintext() const;
892 base::StringPiece
Plaintext() const;
894 bool is_fec_packet() const { return is_fec_packet_
; }
896 char* mutable_data() { return buffer_
; }
899 QuicPacket(char* buffer
,
902 QuicGuidLength guid_length
,
903 bool includes_version
,
904 QuicSequenceNumberLength sequence_number_length
,
908 const bool is_fec_packet_
;
909 const QuicGuidLength guid_length_
;
910 const bool includes_version_
;
911 const QuicSequenceNumberLength sequence_number_length_
;
913 DISALLOW_COPY_AND_ASSIGN(QuicPacket
);
916 class NET_EXPORT_PRIVATE QuicEncryptedPacket
: public QuicData
{
918 QuicEncryptedPacket(const char* buffer
, size_t length
);
919 QuicEncryptedPacket(char* buffer
, size_t length
, bool owns_buffer
);
921 // Clones the packet into a new packet which owns the buffer.
922 QuicEncryptedPacket
* Clone() const;
924 // By default, gtest prints the raw bytes of an object. The bool data
925 // member (in the base class QuicData) causes this object to have padding
926 // bytes, which causes the default gtest object printer to read
927 // uninitialize memory. So we need to teach gtest how to print this object.
928 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
929 std::ostream
& os
, const QuicEncryptedPacket
& s
);
932 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket
);
935 class NET_EXPORT_PRIVATE RetransmittableFrames
{
937 RetransmittableFrames();
938 ~RetransmittableFrames();
940 // Allocates a local copy of the referenced StringPiece has QuicStreamFrame
942 // Takes ownership of |stream_frame|.
943 const QuicFrame
& AddStreamFrame(QuicStreamFrame
* stream_frame
);
944 // Takes ownership of the frame inside |frame|.
945 const QuicFrame
& AddNonStreamFrame(const QuicFrame
& frame
);
946 const QuicFrames
& frames() const { return frames_
; }
948 IsHandshake
HasCryptoHandshake() const;
950 void set_encryption_level(EncryptionLevel level
);
951 EncryptionLevel
encryption_level() const {
952 return encryption_level_
;
957 EncryptionLevel encryption_level_
;
958 // Data referenced by the StringPiece of a QuicStreamFrame.
959 std::vector
<std::string
*> stream_data_
;
961 DISALLOW_COPY_AND_ASSIGN(RetransmittableFrames
);
964 struct NET_EXPORT_PRIVATE SerializedPacket
{
965 SerializedPacket(QuicPacketSequenceNumber sequence_number
,
966 QuicSequenceNumberLength sequence_number_length
,
968 QuicPacketEntropyHash entropy_hash
,
969 RetransmittableFrames
* retransmittable_frames
);
972 QuicPacketSequenceNumber sequence_number
;
973 QuicSequenceNumberLength sequence_number_length
;
975 QuicPacketEntropyHash entropy_hash
;
976 RetransmittableFrames
* retransmittable_frames
;
978 // If set, these will be called when this packet is ACKed by the peer.
979 std::set
<QuicAckNotifier
*> notifiers
;
982 // A struct for functions which consume data payloads and fins.
983 struct NET_EXPORT_PRIVATE QuicConsumedData
{
984 QuicConsumedData(size_t bytes_consumed
, bool fin_consumed
);
986 // By default, gtest prints the raw bytes of an object. The bool data
987 // member causes this object to have padding bytes, which causes the
988 // default gtest object printer to read uninitialize memory. So we need
989 // to teach gtest how to print this object.
990 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
991 std::ostream
& os
, const QuicConsumedData
& s
);
993 // How many bytes were consumed.
994 size_t bytes_consumed
;
996 // True if an incoming fin was consumed.
1002 WRITE_STATUS_BLOCKED
,
1006 // A struct used to return the result of write calls including either the number
1007 // of bytes written or the error code, depending upon the status.
1008 struct NET_EXPORT_PRIVATE WriteResult
{
1009 WriteResult(WriteStatus status
, int bytes_written_or_error_code
);
1013 int bytes_written
; // only valid when status is OK
1014 int error_code
; // only valid when status is ERROR
1020 #endif // NET_QUIC_QUIC_PROTOCOL_H_