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_
18 #include "base/basictypes.h"
19 #include "base/containers/hash_tables.h"
20 #include "base/logging.h"
21 #include "base/strings/string_piece.h"
22 #include "net/base/int128.h"
23 #include "net/base/ip_endpoint.h"
24 #include "net/base/net_export.h"
25 #include "net/quic/iovector.h"
26 #include "net/quic/quic_bandwidth.h"
27 #include "net/quic/quic_time.h"
31 class QuicAckNotifier
;
33 struct QuicPacketHeader
;
35 typedef uint64 QuicConnectionId
;
36 typedef uint32 QuicStreamId
;
37 typedef uint64 QuicStreamOffset
;
38 typedef uint64 QuicPacketSequenceNumber
;
39 typedef QuicPacketSequenceNumber QuicFecGroupNumber
;
40 typedef uint64 QuicPublicResetNonceProof
;
41 typedef uint8 QuicPacketEntropyHash
;
42 typedef uint32 QuicHeaderId
;
43 // QuicTag is the type of a tag in the wire protocol.
44 typedef uint32 QuicTag
;
45 typedef std::vector
<QuicTag
> QuicTagVector
;
46 typedef std::map
<QuicTag
, std::string
> QuicTagValueMap
;
47 // TODO(rtenneti): Didn't use SpdyPriority because SpdyPriority is uint8 and
48 // QuicPriority is uint32. Use SpdyPriority when we change the QUIC_VERSION.
49 typedef uint32 QuicPriority
;
51 // Default and initial maximum size in bytes of a QUIC packet.
52 const QuicByteCount kDefaultMaxPacketSize
= 1350;
53 // Default initial maximum size in bytes of a QUIC packet for servers.
54 const QuicByteCount kDefaultServerMaxPacketSize
= 1000;
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;
60 // Default maximum packet size used in the Linux TCP implementation.
61 // Used in QUIC for congestion window computations in bytes.
62 const QuicByteCount kDefaultTCPMSS
= 1460;
64 // We match SPDY's use of 32 when secure (since we'd compete with SPDY).
65 const QuicPacketCount kInitialCongestionWindowSecure
= 32;
66 // Be conservative, and just use double a typical TCP ICWND for HTTP.
67 const QuicPacketCount kInitialCongestionWindowInsecure
= 20;
69 // Minimum size of initial flow control window, for both stream and session.
70 const uint32 kMinimumFlowControlSendWindow
= 16 * 1024; // 16 KB
72 // Minimum size of the CWND, in packets, when doing bandwidth resumption.
73 const QuicPacketCount kMinCongestionWindowForBandwidthResumption
= 10;
75 // Maximum size of the CWND, in packets, for TCP congestion control algorithms.
76 const QuicPacketCount kMaxTcpCongestionWindow
= 200;
78 // Default size of the socket receive buffer in bytes.
79 const QuicByteCount kDefaultSocketReceiveBuffer
= 256 * 1024;
80 // Minimum size of the socket receive buffer in bytes.
81 // Smaller values are ignored.
82 const QuicByteCount kMinSocketReceiveBuffer
= 16 * 1024;
84 // Fraction of the receive buffer that can be used for encrypted bytes.
85 // Allows a 5% overhead for IP and UDP framing, as well as ack only packets.
86 static const float kUsableRecieveBufferFraction
= 0.95f
;
88 // Don't allow a client to suggest an RTT shorter than 10ms.
89 const uint32 kMinInitialRoundTripTimeUs
= 10 * kNumMicrosPerMilli
;
91 // Don't allow a client to suggest an RTT longer than 15 seconds.
92 const uint32 kMaxInitialRoundTripTimeUs
= 15 * kNumMicrosPerSecond
;
94 // Maximum number of open streams per connection.
95 const size_t kDefaultMaxStreamsPerConnection
= 100;
97 // Number of bytes reserved for public flags in the packet header.
98 const size_t kPublicFlagsSize
= 1;
99 // Number of bytes reserved for version number in the packet header.
100 const size_t kQuicVersionSize
= 4;
101 // Number of bytes reserved for private flags in the packet header.
102 const size_t kPrivateFlagsSize
= 1;
103 // Number of bytes reserved for FEC group in the packet header.
104 const size_t kFecGroupSize
= 1;
106 // Signifies that the QuicPacket will contain version of the protocol.
107 const bool kIncludeVersion
= true;
109 // Index of the first byte in a QUIC packet which is used in hash calculation.
110 const size_t kStartOfHashData
= 0;
112 // Limit on the delta between stream IDs.
113 const QuicStreamId kMaxStreamIdDelta
= 200;
115 // Reserved ID for the crypto stream.
116 const QuicStreamId kCryptoStreamId
= 1;
118 // Reserved ID for the headers stream.
119 const QuicStreamId kHeadersStreamId
= 3;
121 // Maximum delayed ack time, in ms.
122 const int64 kMaxDelayedAckTimeMs
= 25;
124 // The timeout before the handshake succeeds.
125 const int64 kInitialIdleTimeoutSecs
= 5;
126 // The default idle timeout.
127 const int64 kDefaultIdleTimeoutSecs
= 30;
128 // The maximum idle timeout that can be negotiated.
129 const int64 kMaximumIdleTimeoutSecs
= 60 * 10; // 10 minutes.
130 // The default timeout for a connection until the crypto handshake succeeds.
131 const int64 kMaxTimeForCryptoHandshakeSecs
= 10; // 10 secs.
133 // Default limit on the number of undecryptable packets the connection buffers
134 // before the CHLO/SHLO arrive.
135 const size_t kDefaultMaxUndecryptablePackets
= 10;
137 // Default ping timeout.
138 const int64 kPingTimeoutSecs
= 15; // 15 secs.
140 // Minimum number of RTTs between Server Config Updates (SCUP) sent to client.
141 const int kMinIntervalBetweenServerConfigUpdatesRTTs
= 10;
143 // Minimum time between Server Config Updates (SCUP) sent to client.
144 const int kMinIntervalBetweenServerConfigUpdatesMs
= 1000;
146 // Minimum number of packets between Server Config Updates (SCUP).
147 const int kMinPacketsBetweenServerConfigUpdates
= 100;
149 // The number of open streams that a server will accept is set to be slightly
150 // larger than the negotiated limit. Immediately closing the connection if the
151 // client opens slightly too many streams is not ideal: the client may have sent
152 // a FIN that was lost, and simultaneously opened a new stream. The number of
153 // streams a server accepts is a fixed increment over the negotiated limit, or a
154 // percentage increase, whichever is larger.
155 const float kMaxStreamsMultiplier
= 1.1f
;
156 const int kMaxStreamsMinimumIncrement
= 10;
158 // We define an unsigned 16-bit floating point value, inspired by IEEE floats
159 // (http://en.wikipedia.org/wiki/Half_precision_floating-point_format),
160 // with 5-bit exponent (bias 1), 11-bit mantissa (effective 12 with hidden
161 // bit) and denormals, but without signs, transfinites or fractions. Wire format
162 // 16 bits (little-endian byte order) are split into exponent (high 5) and
163 // mantissa (low 11) and decoded as:
165 // if (exponent == 0) value = mantissa;
166 // else value = (mantissa | 1 << 11) << (exponent - 1)
167 const int kUFloat16ExponentBits
= 5;
168 const int kUFloat16MaxExponent
= (1 << kUFloat16ExponentBits
) - 2; // 30
169 const int kUFloat16MantissaBits
= 16 - kUFloat16ExponentBits
; // 11
170 const int kUFloat16MantissaEffectiveBits
= kUFloat16MantissaBits
+ 1; // 12
171 const uint64 kUFloat16MaxValue
= // 0x3FFC0000000
172 ((GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits
) - 1) <<
173 kUFloat16MaxExponent
;
175 enum TransmissionType
{
177 FIRST_TRANSMISSION_TYPE
= NOT_RETRANSMISSION
,
178 HANDSHAKE_RETRANSMISSION
, // Retransmits due to handshake timeouts.
179 ALL_UNACKED_RETRANSMISSION
, // Retransmits all unacked packets.
180 ALL_INITIAL_RETRANSMISSION
, // Retransmits all initially encrypted packets.
181 LOSS_RETRANSMISSION
, // Retransmits due to loss detection.
182 RTO_RETRANSMISSION
, // Retransmits due to retransmit time out.
183 TLP_RETRANSMISSION
, // Tail loss probes.
184 LAST_TRANSMISSION_TYPE
= TLP_RETRANSMISSION
,
187 enum HasRetransmittableData
{
188 NO_RETRANSMITTABLE_DATA
,
189 HAS_RETRANSMITTABLE_DATA
,
197 enum class Perspective
{ IS_SERVER
, IS_CLIENT
};
199 NET_EXPORT_PRIVATE
std::ostream
& operator<<(std::ostream
& os
,
200 const Perspective
& s
);
202 // Indicates FEC protection level for data being written.
204 MUST_FEC_PROTECT
, // Callee must FEC protect this data.
205 MAY_FEC_PROTECT
// Callee does not have to but may FEC protect this data.
208 // Indicates FEC policy.
210 FEC_PROTECT_ALWAYS
, // All data in the stream should be FEC protected.
211 FEC_PROTECT_OPTIONAL
// Data in the stream does not need FEC protection.
215 // Regular frame types. The values set here cannot change without the
216 // introduction of a new QUIC version.
218 RST_STREAM_FRAME
= 1,
219 CONNECTION_CLOSE_FRAME
= 2,
221 WINDOW_UPDATE_FRAME
= 4,
223 STOP_WAITING_FRAME
= 6,
226 // STREAM and ACK frames are special frames. They are encoded differently on
227 // the wire and their values do not need to be stable.
233 enum QuicConnectionIdLength
{
234 PACKET_0BYTE_CONNECTION_ID
= 0,
235 PACKET_1BYTE_CONNECTION_ID
= 1,
236 PACKET_4BYTE_CONNECTION_ID
= 4,
237 PACKET_8BYTE_CONNECTION_ID
= 8
245 enum QuicSequenceNumberLength
{
246 PACKET_1BYTE_SEQUENCE_NUMBER
= 1,
247 PACKET_2BYTE_SEQUENCE_NUMBER
= 2,
248 PACKET_4BYTE_SEQUENCE_NUMBER
= 4,
249 PACKET_6BYTE_SEQUENCE_NUMBER
= 6
252 // Used to indicate a QuicSequenceNumberLength using two flag bits.
253 enum QuicSequenceNumberLengthFlags
{
254 PACKET_FLAGS_1BYTE_SEQUENCE
= 0, // 00
255 PACKET_FLAGS_2BYTE_SEQUENCE
= 1, // 01
256 PACKET_FLAGS_4BYTE_SEQUENCE
= 1 << 1, // 10
257 PACKET_FLAGS_6BYTE_SEQUENCE
= 1 << 1 | 1, // 11
260 // The public flags are specified in one byte.
261 enum QuicPacketPublicFlags
{
262 PACKET_PUBLIC_FLAGS_NONE
= 0,
264 // Bit 0: Does the packet header contains version info?
265 PACKET_PUBLIC_FLAGS_VERSION
= 1 << 0,
267 // Bit 1: Is this packet a public reset packet?
268 PACKET_PUBLIC_FLAGS_RST
= 1 << 1,
270 // Bits 2 and 3 specify the length of the ConnectionId as follows:
275 PACKET_PUBLIC_FLAGS_0BYTE_CONNECTION_ID
= 0,
276 PACKET_PUBLIC_FLAGS_1BYTE_CONNECTION_ID
= 1 << 2,
277 PACKET_PUBLIC_FLAGS_4BYTE_CONNECTION_ID
= 1 << 3,
278 PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID
= 1 << 3 | 1 << 2,
280 // Bits 4 and 5 describe the packet sequence number length as follows:
285 PACKET_PUBLIC_FLAGS_1BYTE_SEQUENCE
= PACKET_FLAGS_1BYTE_SEQUENCE
<< 4,
286 PACKET_PUBLIC_FLAGS_2BYTE_SEQUENCE
= PACKET_FLAGS_2BYTE_SEQUENCE
<< 4,
287 PACKET_PUBLIC_FLAGS_4BYTE_SEQUENCE
= PACKET_FLAGS_4BYTE_SEQUENCE
<< 4,
288 PACKET_PUBLIC_FLAGS_6BYTE_SEQUENCE
= PACKET_FLAGS_6BYTE_SEQUENCE
<< 4,
290 // All bits set (bits 6 and 7 are not currently used): 00111111
291 PACKET_PUBLIC_FLAGS_MAX
= (1 << 6) - 1
294 // The private flags are specified in one byte.
295 enum QuicPacketPrivateFlags
{
296 PACKET_PRIVATE_FLAGS_NONE
= 0,
298 // Bit 0: Does this packet contain an entropy bit?
299 PACKET_PRIVATE_FLAGS_ENTROPY
= 1 << 0,
301 // Bit 1: Payload is part of an FEC group?
302 PACKET_PRIVATE_FLAGS_FEC_GROUP
= 1 << 1,
304 // Bit 2: Payload is FEC as opposed to frames?
305 PACKET_PRIVATE_FLAGS_FEC
= 1 << 2,
307 // All bits set (bits 3-7 are not currently used): 00000111
308 PACKET_PRIVATE_FLAGS_MAX
= (1 << 3) - 1
311 // The available versions of QUIC. Guaranteed that the integer value of the enum
312 // will match the version number.
313 // When adding a new version to this enum you should add it to
314 // kSupportedQuicVersions (if appropriate), and also add a new case to the
315 // helper methods QuicVersionToQuicTag, QuicTagToQuicVersion, and
316 // QuicVersionToString.
318 // Special case to indicate unknown/unsupported QUIC version.
319 QUIC_VERSION_UNSUPPORTED
= 0,
321 QUIC_VERSION_23
= 23, // Timestamp in the ack frame.
322 QUIC_VERSION_24
= 24, // SPDY/4 header compression.
323 QUIC_VERSION_25
= 25, // SPDY/4 header keys, and removal of error_details
324 // from QuicRstStreamFrame
327 // This vector contains QUIC versions which we currently support.
328 // This should be ordered such that the highest supported version is the first
329 // element, with subsequent elements in descending order (versions can be
330 // skipped as necessary).
332 // IMPORTANT: if you are adding to this list, follow the instructions at
333 // http://sites/quic/adding-and-removing-versions
334 static const QuicVersion kSupportedQuicVersions
[] = {QUIC_VERSION_25
,
338 typedef std::vector
<QuicVersion
> QuicVersionVector
;
340 // Returns a vector of QUIC versions in kSupportedQuicVersions.
341 NET_EXPORT_PRIVATE QuicVersionVector
QuicSupportedVersions();
343 // QuicTag is written to and read from the wire, but we prefer to use
344 // the more readable QuicVersion at other levels.
345 // Helper function which translates from a QuicVersion to a QuicTag. Returns 0
346 // if QuicVersion is unsupported.
347 NET_EXPORT_PRIVATE QuicTag
QuicVersionToQuicTag(const QuicVersion version
);
349 // Returns appropriate QuicVersion from a QuicTag.
350 // Returns QUIC_VERSION_UNSUPPORTED if version_tag cannot be understood.
351 NET_EXPORT_PRIVATE QuicVersion
QuicTagToQuicVersion(const QuicTag version_tag
);
353 // Helper function which translates from a QuicVersion to a string.
354 // Returns strings corresponding to enum names (e.g. QUIC_VERSION_6).
355 NET_EXPORT_PRIVATE
std::string
QuicVersionToString(const QuicVersion version
);
357 // Returns comma separated list of string representations of QuicVersion enum
358 // values in the supplied |versions| vector.
359 NET_EXPORT_PRIVATE
std::string
QuicVersionVectorToString(
360 const QuicVersionVector
& versions
);
362 // Version and Crypto tags are written to the wire with a big-endian
363 // representation of the name of the tag. For example
364 // the client hello tag (CHLO) will be written as the
365 // following 4 bytes: 'C' 'H' 'L' 'O'. Since it is
366 // stored in memory as a little endian uint32, we need
367 // to reverse the order of the bytes.
369 // MakeQuicTag returns a value given the four bytes. For example:
370 // MakeQuicTag('C', 'H', 'L', 'O');
371 NET_EXPORT_PRIVATE QuicTag
MakeQuicTag(char a
, char b
, char c
, char d
);
373 // Returns true if the tag vector contains the specified tag.
374 NET_EXPORT_PRIVATE
bool ContainsQuicTag(const QuicTagVector
& tag_vector
,
377 // Size in bytes of the data or fec packet header.
378 NET_EXPORT_PRIVATE
size_t GetPacketHeaderSize(const QuicPacketHeader
& header
);
380 NET_EXPORT_PRIVATE
size_t GetPacketHeaderSize(
381 QuicConnectionIdLength connection_id_length
,
382 bool include_version
,
383 QuicSequenceNumberLength sequence_number_length
,
384 InFecGroup is_in_fec_group
);
386 // Index of the first byte in a QUIC packet of FEC protected data.
387 NET_EXPORT_PRIVATE
size_t GetStartOfFecProtectedData(
388 QuicConnectionIdLength connection_id_length
,
389 bool include_version
,
390 QuicSequenceNumberLength sequence_number_length
);
391 // Index of the first byte in a QUIC packet of encrypted data.
392 NET_EXPORT_PRIVATE
size_t GetStartOfEncryptedData(
393 QuicConnectionIdLength connection_id_length
,
394 bool include_version
,
395 QuicSequenceNumberLength sequence_number_length
);
397 enum QuicRstStreamErrorCode
{
398 QUIC_STREAM_NO_ERROR
= 0,
400 // There was some error which halted stream processing.
401 QUIC_ERROR_PROCESSING_STREAM
,
402 // We got two fin or reset offsets which did not match.
403 QUIC_MULTIPLE_TERMINATION_OFFSETS
,
404 // We got bad payload and can not respond to it at the protocol level.
405 QUIC_BAD_APPLICATION_PAYLOAD
,
406 // Stream closed due to connection error. No reset frame is sent when this
408 QUIC_STREAM_CONNECTION_ERROR
,
409 // GoAway frame sent. No more stream can be created.
410 QUIC_STREAM_PEER_GOING_AWAY
,
411 // The stream has been cancelled.
412 QUIC_STREAM_CANCELLED
,
413 // Closing stream locally, sending a RST to allow for proper flow control
414 // accounting. Sent in response to a RST from the peer.
415 QUIC_RST_ACKNOWLEDGEMENT
,
417 // No error. Used as bound while iterating.
418 QUIC_STREAM_LAST_ERROR
,
421 // Because receiving an unknown QuicRstStreamErrorCode results in connection
422 // teardown, we use this to make sure any errors predating a given version are
423 // downgraded to the most appropriate existing error.
424 NET_EXPORT_PRIVATE QuicRstStreamErrorCode
AdjustErrorForVersion(
425 QuicRstStreamErrorCode error_code
,
426 QuicVersion version
);
428 // These values must remain stable as they are uploaded to UMA histograms.
429 // To add a new error code, use the current value of QUIC_LAST_ERROR and
430 // increment QUIC_LAST_ERROR.
434 // Connection has reached an invalid state.
435 QUIC_INTERNAL_ERROR
= 1,
436 // There were data frames after the a fin or reset.
437 QUIC_STREAM_DATA_AFTER_TERMINATION
= 2,
438 // Control frame is malformed.
439 QUIC_INVALID_PACKET_HEADER
= 3,
440 // Frame data is malformed.
441 QUIC_INVALID_FRAME_DATA
= 4,
442 // The packet contained no payload.
443 QUIC_MISSING_PAYLOAD
= 48,
444 // FEC data is malformed.
445 QUIC_INVALID_FEC_DATA
= 5,
446 // STREAM frame data is malformed.
447 QUIC_INVALID_STREAM_DATA
= 46,
448 // STREAM frame data is not encrypted.
449 QUIC_UNENCRYPTED_STREAM_DATA
= 61,
450 // RST_STREAM frame data is malformed.
451 QUIC_INVALID_RST_STREAM_DATA
= 6,
452 // CONNECTION_CLOSE frame data is malformed.
453 QUIC_INVALID_CONNECTION_CLOSE_DATA
= 7,
454 // GOAWAY frame data is malformed.
455 QUIC_INVALID_GOAWAY_DATA
= 8,
456 // WINDOW_UPDATE frame data is malformed.
457 QUIC_INVALID_WINDOW_UPDATE_DATA
= 57,
458 // BLOCKED frame data is malformed.
459 QUIC_INVALID_BLOCKED_DATA
= 58,
460 // STOP_WAITING frame data is malformed.
461 QUIC_INVALID_STOP_WAITING_DATA
= 60,
462 // ACK frame data is malformed.
463 QUIC_INVALID_ACK_DATA
= 9,
465 // deprecated: QUIC_INVALID_CONGESTION_FEEDBACK_DATA = 47,
467 // Version negotiation packet is malformed.
468 QUIC_INVALID_VERSION_NEGOTIATION_PACKET
= 10,
469 // Public RST packet is malformed.
470 QUIC_INVALID_PUBLIC_RST_PACKET
= 11,
471 // There was an error decrypting.
472 QUIC_DECRYPTION_FAILURE
= 12,
473 // There was an error encrypting.
474 QUIC_ENCRYPTION_FAILURE
= 13,
475 // The packet exceeded kMaxPacketSize.
476 QUIC_PACKET_TOO_LARGE
= 14,
477 // Data was sent for a stream which did not exist.
478 QUIC_PACKET_FOR_NONEXISTENT_STREAM
= 15,
479 // The peer is going away. May be a client or server.
480 QUIC_PEER_GOING_AWAY
= 16,
481 // A stream ID was invalid.
482 QUIC_INVALID_STREAM_ID
= 17,
483 // A priority was invalid.
484 QUIC_INVALID_PRIORITY
= 49,
485 // Too many streams already open.
486 QUIC_TOO_MANY_OPEN_STREAMS
= 18,
487 // The peer must send a FIN/RST for each stream, and has not been doing so.
488 QUIC_TOO_MANY_UNFINISHED_STREAMS
= 66,
489 // Received public reset for this connection.
490 QUIC_PUBLIC_RESET
= 19,
491 // Invalid protocol version.
492 QUIC_INVALID_VERSION
= 20,
494 // deprecated: QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED = 21
496 // The Header ID for a stream was too far from the previous.
497 QUIC_INVALID_HEADER_ID
= 22,
498 // Negotiable parameter received during handshake had invalid value.
499 QUIC_INVALID_NEGOTIATED_VALUE
= 23,
500 // There was an error decompressing data.
501 QUIC_DECOMPRESSION_FAILURE
= 24,
502 // We hit our prenegotiated (or default) timeout
503 QUIC_CONNECTION_TIMED_OUT
= 25,
504 // We hit our overall connection timeout
505 QUIC_CONNECTION_OVERALL_TIMED_OUT
= 67,
506 // There was an error encountered migrating addresses
507 QUIC_ERROR_MIGRATING_ADDRESS
= 26,
508 // There was an error while writing to the socket.
509 QUIC_PACKET_WRITE_ERROR
= 27,
510 // There was an error while reading from the socket.
511 QUIC_PACKET_READ_ERROR
= 51,
512 // We received a STREAM_FRAME with no data and no fin flag set.
513 QUIC_INVALID_STREAM_FRAME
= 50,
514 // We received invalid data on the headers stream.
515 QUIC_INVALID_HEADERS_STREAM_DATA
= 56,
516 // The peer received too much data, violating flow control.
517 QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA
= 59,
518 // The peer sent too much data, violating flow control.
519 QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA
= 63,
520 // The peer received an invalid flow control window.
521 QUIC_FLOW_CONTROL_INVALID_WINDOW
= 64,
522 // The connection has been IP pooled into an existing connection.
523 QUIC_CONNECTION_IP_POOLED
= 62,
524 // The connection has too many outstanding sent packets.
525 QUIC_TOO_MANY_OUTSTANDING_SENT_PACKETS
= 68,
526 // The connection has too many outstanding received packets.
527 QUIC_TOO_MANY_OUTSTANDING_RECEIVED_PACKETS
= 69,
528 // The quic connection job to load server config is cancelled.
529 QUIC_CONNECTION_CANCELLED
= 70,
530 // Disabled QUIC because of high packet loss rate.
531 QUIC_BAD_PACKET_LOSS_RATE
= 71,
536 QUIC_HANDSHAKE_FAILED
= 28,
537 // Handshake message contained out of order tags.
538 QUIC_CRYPTO_TAGS_OUT_OF_ORDER
= 29,
539 // Handshake message contained too many entries.
540 QUIC_CRYPTO_TOO_MANY_ENTRIES
= 30,
541 // Handshake message contained an invalid value length.
542 QUIC_CRYPTO_INVALID_VALUE_LENGTH
= 31,
543 // A crypto message was received after the handshake was complete.
544 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE
= 32,
545 // A crypto message was received with an illegal message tag.
546 QUIC_INVALID_CRYPTO_MESSAGE_TYPE
= 33,
547 // A crypto message was received with an illegal parameter.
548 QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER
= 34,
549 // An invalid channel id signature was supplied.
550 QUIC_INVALID_CHANNEL_ID_SIGNATURE
= 52,
551 // A crypto message was received with a mandatory parameter missing.
552 QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND
= 35,
553 // A crypto message was received with a parameter that has no overlap
554 // with the local parameter.
555 QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP
= 36,
556 // A crypto message was received that contained a parameter with too few
558 QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND
= 37,
559 // An internal error occured in crypto processing.
560 QUIC_CRYPTO_INTERNAL_ERROR
= 38,
561 // A crypto handshake message specified an unsupported version.
562 QUIC_CRYPTO_VERSION_NOT_SUPPORTED
= 39,
563 // There was no intersection between the crypto primitives supported by the
564 // peer and ourselves.
565 QUIC_CRYPTO_NO_SUPPORT
= 40,
566 // The server rejected our client hello messages too many times.
567 QUIC_CRYPTO_TOO_MANY_REJECTS
= 41,
568 // The client rejected the server's certificate chain or signature.
569 QUIC_PROOF_INVALID
= 42,
570 // A crypto message was received with a duplicate tag.
571 QUIC_CRYPTO_DUPLICATE_TAG
= 43,
572 // A crypto message was received with the wrong encryption level (i.e. it
573 // should have been encrypted but was not.)
574 QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT
= 44,
575 // The server config for a server has expired.
576 QUIC_CRYPTO_SERVER_CONFIG_EXPIRED
= 45,
577 // We failed to setup the symmetric keys for a connection.
578 QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED
= 53,
579 // A handshake message arrived, but we are still validating the
580 // previous handshake message.
581 QUIC_CRYPTO_MESSAGE_WHILE_VALIDATING_CLIENT_HELLO
= 54,
582 // A server config update arrived before the handshake is complete.
583 QUIC_CRYPTO_UPDATE_BEFORE_HANDSHAKE_COMPLETE
= 65,
584 // This connection involved a version negotiation which appears to have been
586 QUIC_VERSION_NEGOTIATION_MISMATCH
= 55,
588 // No error. Used as bound while iterating.
589 QUIC_LAST_ERROR
= 72,
592 struct NET_EXPORT_PRIVATE QuicPacketPublicHeader
{
593 QuicPacketPublicHeader();
594 explicit QuicPacketPublicHeader(const QuicPacketPublicHeader
& other
);
595 ~QuicPacketPublicHeader();
597 // Universal header. All QuicPacket headers will have a connection_id and
599 QuicConnectionId connection_id
;
600 QuicConnectionIdLength connection_id_length
;
603 QuicSequenceNumberLength sequence_number_length
;
604 QuicVersionVector versions
;
607 // An integer which cannot be a packet sequence number.
608 const QuicPacketSequenceNumber kInvalidPacketSequenceNumber
= 0;
610 // Header for Data or FEC packets.
611 struct NET_EXPORT_PRIVATE QuicPacketHeader
{
613 explicit QuicPacketHeader(const QuicPacketPublicHeader
& header
);
615 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
616 std::ostream
& os
, const QuicPacketHeader
& s
);
618 QuicPacketPublicHeader public_header
;
621 QuicPacketEntropyHash entropy_hash
;
622 QuicPacketSequenceNumber packet_sequence_number
;
623 InFecGroup is_in_fec_group
;
624 QuicFecGroupNumber fec_group
;
627 struct NET_EXPORT_PRIVATE QuicPublicResetPacket
{
628 QuicPublicResetPacket();
629 explicit QuicPublicResetPacket(const QuicPacketPublicHeader
& header
);
631 QuicPacketPublicHeader public_header
;
632 QuicPublicResetNonceProof nonce_proof
;
633 QuicPacketSequenceNumber rejected_sequence_number
;
634 IPEndPoint client_address
;
637 enum QuicVersionNegotiationState
{
638 START_NEGOTIATION
= 0,
639 // Server-side this implies we've sent a version negotiation packet and are
640 // waiting on the client to select a compatible version. Client-side this
641 // implies we've gotten a version negotiation packet, are retransmitting the
642 // initial packets with a supported version and are waiting for our first
643 // packet from the server.
644 NEGOTIATION_IN_PROGRESS
,
645 // This indicates this endpoint has received a packet from the peer with a
646 // version this endpoint supports. Version negotiation is complete, and the
647 // version number will no longer be sent with future packets.
651 typedef QuicPacketPublicHeader QuicVersionNegotiationPacket
;
653 // A padding frame contains no payload.
654 struct NET_EXPORT_PRIVATE QuicPaddingFrame
{
657 // A ping frame contains no payload, though it is retransmittable,
658 // and ACK'd just like other normal frames.
659 struct NET_EXPORT_PRIVATE QuicPingFrame
{
662 struct NET_EXPORT_PRIVATE QuicStreamFrame
{
664 QuicStreamFrame(const QuicStreamFrame
& frame
);
665 QuicStreamFrame(QuicStreamId stream_id
,
667 QuicStreamOffset offset
,
670 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
671 std::ostream
& os
, const QuicStreamFrame
& s
);
673 // Returns a copy of the IOVector |data| as a heap-allocated string.
674 // Caller must take ownership of the returned string.
675 std::string
* GetDataAsString() const;
677 QuicStreamId stream_id
;
679 QuicStreamOffset offset
; // Location of this data in the stream.
683 // TODO(ianswett): Re-evaluate the trade-offs of hash_set vs set when framing
685 typedef std::set
<QuicPacketSequenceNumber
> SequenceNumberSet
;
686 typedef std::list
<QuicPacketSequenceNumber
> SequenceNumberList
;
689 std::pair
<QuicPacketSequenceNumber
, QuicTime
> > PacketTimeList
;
691 struct NET_EXPORT_PRIVATE QuicStopWaitingFrame
{
692 QuicStopWaitingFrame();
693 ~QuicStopWaitingFrame();
695 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
696 std::ostream
& os
, const QuicStopWaitingFrame
& s
);
697 // Entropy hash of all packets up to, but not including, the least unacked
699 QuicPacketEntropyHash entropy_hash
;
700 // The lowest packet we've sent which is unacked, and we expect an ack for.
701 QuicPacketSequenceNumber least_unacked
;
704 struct NET_EXPORT_PRIVATE QuicAckFrame
{
708 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
709 std::ostream
& os
, const QuicAckFrame
& s
);
711 // Entropy hash of all packets up to largest observed not including missing
713 QuicPacketEntropyHash entropy_hash
;
715 // The highest packet sequence number we've observed from the peer.
717 // In general, this should be the largest packet number we've received. In
718 // the case of truncated acks, we may have to advertise a lower "upper bound"
719 // than largest received, to avoid implicitly acking missing packets that
720 // don't fit in the missing packet list due to size limitations. In this
721 // case, largest_observed may be a packet which is also in the missing packets
723 QuicPacketSequenceNumber largest_observed
;
725 // Time elapsed since largest_observed was received until this Ack frame was
727 QuicTime::Delta delta_time_largest_observed
;
729 // TODO(satyamshekhar): Can be optimized using an interval set like data
731 // The set of packets which we're expecting and have not received.
732 SequenceNumberSet missing_packets
;
734 // Whether the ack had to be truncated when sent.
737 // Packets which have been revived via FEC.
738 // All of these must also be in missing_packets.
739 SequenceNumberSet revived_packets
;
741 // List of <sequence_number, time> for when packets arrived.
742 PacketTimeList received_packet_times
;
745 // True if the sequence number is greater than largest_observed or is listed
747 // Always returns false for sequence numbers less than least_unacked.
748 bool NET_EXPORT_PRIVATE
IsAwaitingPacket(
749 const QuicAckFrame
& ack_frame
,
750 QuicPacketSequenceNumber sequence_number
);
752 // Inserts missing packets between [lower, higher).
753 void NET_EXPORT_PRIVATE
InsertMissingPacketsBetween(
754 QuicAckFrame
* ack_frame
,
755 QuicPacketSequenceNumber lower
,
756 QuicPacketSequenceNumber higher
);
758 // Defines for all types of congestion control algorithms that can be used in
759 // QUIC. Note that this is separate from the congestion feedback type -
760 // some congestion control algorithms may use the same feedback type
761 // (Reno and Cubic are the classic example for that).
762 enum CongestionControlType
{
770 enum LossDetectionType
{
771 kNack
, // Used to mimic TCP's loss detection.
772 kTime
, // Time based loss detection.
775 struct NET_EXPORT_PRIVATE QuicRstStreamFrame
{
776 QuicRstStreamFrame();
777 QuicRstStreamFrame(QuicStreamId stream_id
,
778 QuicRstStreamErrorCode error_code
,
779 QuicStreamOffset bytes_written
);
781 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
782 std::ostream
& os
, const QuicRstStreamFrame
& r
);
784 QuicStreamId stream_id
;
785 QuicRstStreamErrorCode error_code
;
786 // Only used in versions <= QUIC_VERSION_24.
787 std::string error_details
;
789 // Used to update flow control windows. On termination of a stream, both
790 // endpoints must inform the peer of the number of bytes they have sent on
791 // that stream. This can be done through normal termination (data packet with
792 // FIN) or through a RST.
793 QuicStreamOffset byte_offset
;
796 struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame
{
797 QuicConnectionCloseFrame();
799 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
800 std::ostream
& os
, const QuicConnectionCloseFrame
& c
);
802 QuicErrorCode error_code
;
803 std::string error_details
;
806 struct NET_EXPORT_PRIVATE QuicGoAwayFrame
{
808 QuicGoAwayFrame(QuicErrorCode error_code
,
809 QuicStreamId last_good_stream_id
,
810 const std::string
& reason
);
812 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
813 std::ostream
& os
, const QuicGoAwayFrame
& g
);
815 QuicErrorCode error_code
;
816 QuicStreamId last_good_stream_id
;
817 std::string reason_phrase
;
820 // Flow control updates per-stream and at the connection levoel.
821 // Based on SPDY's WINDOW_UPDATE frame, but uses an absolute byte offset rather
822 // than a window delta.
823 // TODO(rjshade): A possible future optimization is to make stream_id and
824 // byte_offset variable length, similar to stream frames.
825 struct NET_EXPORT_PRIVATE QuicWindowUpdateFrame
{
826 QuicWindowUpdateFrame() {}
827 QuicWindowUpdateFrame(QuicStreamId stream_id
, QuicStreamOffset byte_offset
);
829 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
830 std::ostream
& os
, const QuicWindowUpdateFrame
& w
);
832 // The stream this frame applies to. 0 is a special case meaning the overall
833 // connection rather than a specific stream.
834 QuicStreamId stream_id
;
836 // Byte offset in the stream or connection. The receiver of this frame must
837 // not send data which would result in this offset being exceeded.
838 QuicStreamOffset byte_offset
;
841 // The BLOCKED frame is used to indicate to the remote endpoint that this
842 // endpoint believes itself to be flow-control blocked but otherwise ready to
843 // send data. The BLOCKED frame is purely advisory and optional.
844 // Based on SPDY's BLOCKED frame (undocumented as of 2014-01-28).
845 struct NET_EXPORT_PRIVATE QuicBlockedFrame
{
846 QuicBlockedFrame() {}
847 explicit QuicBlockedFrame(QuicStreamId stream_id
);
849 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
850 std::ostream
& os
, const QuicBlockedFrame
& b
);
852 // The stream this frame applies to. 0 is a special case meaning the overall
853 // connection rather than a specific stream.
854 QuicStreamId stream_id
;
857 // EncryptionLevel enumerates the stages of encryption that a QUIC connection
858 // progresses through. When retransmitting a packet, the encryption level needs
859 // to be specified so that it is retransmitted at a level which the peer can
861 enum EncryptionLevel
{
863 ENCRYPTION_INITIAL
= 1,
864 ENCRYPTION_FORWARD_SECURE
= 2,
866 NUM_ENCRYPTION_LEVELS
,
869 struct NET_EXPORT_PRIVATE QuicFrame
{
871 explicit QuicFrame(QuicPaddingFrame
* padding_frame
);
872 explicit QuicFrame(QuicStreamFrame
* stream_frame
);
873 explicit QuicFrame(QuicAckFrame
* frame
);
875 explicit QuicFrame(QuicRstStreamFrame
* frame
);
876 explicit QuicFrame(QuicConnectionCloseFrame
* frame
);
877 explicit QuicFrame(QuicStopWaitingFrame
* frame
);
878 explicit QuicFrame(QuicPingFrame
* frame
);
879 explicit QuicFrame(QuicGoAwayFrame
* frame
);
880 explicit QuicFrame(QuicWindowUpdateFrame
* frame
);
881 explicit QuicFrame(QuicBlockedFrame
* frame
);
883 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
884 std::ostream
& os
, const QuicFrame
& frame
);
888 QuicPaddingFrame
* padding_frame
;
889 QuicStreamFrame
* stream_frame
;
890 QuicAckFrame
* ack_frame
;
892 QuicStopWaitingFrame
* stop_waiting_frame
;
894 QuicPingFrame
* ping_frame
;
895 QuicRstStreamFrame
* rst_stream_frame
;
896 QuicConnectionCloseFrame
* connection_close_frame
;
897 QuicGoAwayFrame
* goaway_frame
;
898 QuicWindowUpdateFrame
* window_update_frame
;
899 QuicBlockedFrame
* blocked_frame
;
903 typedef std::vector
<QuicFrame
> QuicFrames
;
905 struct NET_EXPORT_PRIVATE QuicFecData
{
908 // The FEC group number is also the sequence number of the first
909 // FEC protected packet. The last protected packet's sequence number will
910 // be one less than the sequence number of the FEC packet.
911 QuicFecGroupNumber fec_group
;
912 base::StringPiece redundancy
;
915 class NET_EXPORT_PRIVATE QuicData
{
917 QuicData(const char* buffer
, size_t length
);
918 QuicData(char* buffer
, size_t length
, bool owns_buffer
);
921 base::StringPiece
AsStringPiece() const {
922 return base::StringPiece(data(), length());
925 const char* data() const { return buffer_
; }
926 size_t length() const { return length_
; }
933 DISALLOW_COPY_AND_ASSIGN(QuicData
);
936 class NET_EXPORT_PRIVATE QuicPacket
: public QuicData
{
938 QuicPacket(char* buffer
,
941 QuicConnectionIdLength connection_id_length
,
942 bool includes_version
,
943 QuicSequenceNumberLength sequence_number_length
);
945 base::StringPiece
FecProtectedData() const;
946 base::StringPiece
AssociatedData() const;
947 base::StringPiece
BeforePlaintext() const;
948 base::StringPiece
Plaintext() const;
950 char* mutable_data() { return buffer_
; }
954 const QuicConnectionIdLength connection_id_length_
;
955 const bool includes_version_
;
956 const QuicSequenceNumberLength sequence_number_length_
;
958 DISALLOW_COPY_AND_ASSIGN(QuicPacket
);
961 class NET_EXPORT_PRIVATE QuicEncryptedPacket
: public QuicData
{
963 QuicEncryptedPacket(const char* buffer
, size_t length
);
964 QuicEncryptedPacket(char* buffer
, size_t length
, bool owns_buffer
);
966 // Clones the packet into a new packet which owns the buffer.
967 QuicEncryptedPacket
* Clone() const;
969 // By default, gtest prints the raw bytes of an object. The bool data
970 // member (in the base class QuicData) causes this object to have padding
971 // bytes, which causes the default gtest object printer to read
972 // uninitialize memory. So we need to teach gtest how to print this object.
973 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
974 std::ostream
& os
, const QuicEncryptedPacket
& s
);
977 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket
);
980 class NET_EXPORT_PRIVATE RetransmittableFrames
{
982 explicit RetransmittableFrames(EncryptionLevel level
);
983 ~RetransmittableFrames();
985 // Allocates a local copy of the referenced StringPiece has QuicStreamFrame
987 // Takes ownership of |stream_frame|.
988 const QuicFrame
& AddStreamFrame(QuicStreamFrame
* stream_frame
);
989 // Takes ownership of the frame inside |frame|.
990 const QuicFrame
& AddNonStreamFrame(const QuicFrame
& frame
);
991 // Removes all stream frames associated with |stream_id|.
992 void RemoveFramesForStream(QuicStreamId stream_id
);
994 const QuicFrames
& frames() const { return frames_
; }
996 IsHandshake
HasCryptoHandshake() const {
997 return has_crypto_handshake_
;
1000 EncryptionLevel
encryption_level() const {
1001 return encryption_level_
;
1006 const EncryptionLevel encryption_level_
;
1007 IsHandshake has_crypto_handshake_
;
1008 // Data referenced by the StringPiece of a QuicStreamFrame.
1009 std::vector
<std::string
*> stream_data_
;
1011 DISALLOW_COPY_AND_ASSIGN(RetransmittableFrames
);
1014 struct NET_EXPORT_PRIVATE SerializedPacket
{
1015 SerializedPacket(QuicPacketSequenceNumber sequence_number
,
1016 QuicSequenceNumberLength sequence_number_length
,
1017 QuicEncryptedPacket
* packet
,
1018 QuicPacketEntropyHash entropy_hash
,
1019 RetransmittableFrames
* retransmittable_frames
);
1020 ~SerializedPacket();
1022 QuicPacketSequenceNumber sequence_number
;
1023 QuicSequenceNumberLength sequence_number_length
;
1024 QuicEncryptedPacket
* packet
;
1025 QuicPacketEntropyHash entropy_hash
;
1026 RetransmittableFrames
* retransmittable_frames
;
1029 // Optional notifiers which will be informed when this packet has been ACKed.
1030 std::list
<QuicAckNotifier
*> notifiers
;
1033 struct NET_EXPORT_PRIVATE TransmissionInfo
{
1034 // Used by STL when assigning into a map.
1037 // Constructs a Transmission with a new all_tranmissions set
1038 // containing |sequence_number|.
1039 TransmissionInfo(RetransmittableFrames
* retransmittable_frames
,
1040 QuicSequenceNumberLength sequence_number_length
,
1041 TransmissionType transmission_type
,
1043 QuicByteCount bytes_sent
,
1044 bool is_fec_packet
);
1046 RetransmittableFrames
* retransmittable_frames
;
1047 QuicSequenceNumberLength sequence_number_length
;
1049 QuicByteCount bytes_sent
;
1050 QuicPacketCount nack_count
;
1051 // Reason why this packet was transmitted.
1052 TransmissionType transmission_type
;
1053 // Stores the sequence numbers of all transmissions of this packet.
1054 // Must always be nullptr or have multiple elements.
1055 SequenceNumberList
* all_transmissions
;
1056 // In flight packets have not been abandoned or lost.
1058 // True if the packet can never be acked, so it can be removed.
1060 // True if the packet is an FEC packet.
1066 #endif // NET_QUIC_QUIC_PROTOCOL_H_