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_
19 #include "base/basictypes.h"
20 #include "base/containers/hash_tables.h"
21 #include "base/logging.h"
22 #include "base/strings/string_piece.h"
23 #include "net/base/int128.h"
24 #include "net/base/iovec.h"
25 #include "net/base/ip_endpoint.h"
26 #include "net/base/net_export.h"
27 #include "net/quic/quic_bandwidth.h"
28 #include "net/quic/quic_time.h"
29 #include "net/quic/quic_types.h"
33 class QuicAckNotifier
;
35 struct QuicPacketHeader
;
37 typedef uint64 QuicConnectionId
;
38 typedef uint32 QuicStreamId
;
39 typedef uint64 QuicStreamOffset
;
40 typedef uint64 QuicPacketSequenceNumber
;
41 typedef QuicPacketSequenceNumber QuicFecGroupNumber
;
42 typedef uint64 QuicPublicResetNonceProof
;
43 typedef uint8 QuicPacketEntropyHash
;
44 typedef uint32 QuicHeaderId
;
45 // QuicTag is the type of a tag in the wire protocol.
46 typedef uint32 QuicTag
;
47 typedef std::vector
<QuicTag
> QuicTagVector
;
48 typedef std::map
<QuicTag
, std::string
> QuicTagValueMap
;
49 // TODO(rtenneti): Didn't use SpdyPriority because SpdyPriority is uint8 and
50 // QuicPriority is uint32. Use SpdyPriority when we change the QUIC_VERSION.
51 typedef uint32 QuicPriority
;
53 // Default initial maximum size in bytes of a QUIC packet.
54 const QuicByteCount kDefaultMaxPacketSize
= 1350;
55 // Default initial maximum size in bytes of a QUIC packet for servers.
56 const QuicByteCount kDefaultServerMaxPacketSize
= 1000;
57 // The maximum packet size of any QUIC packet, based on ethernet's max size,
58 // minus the IP and UDP headers. IPv6 has a 40 byte header, UPD adds an
59 // additional 8 bytes. This is a total overhead of 48 bytes. Ethernet's
60 // max packet size is 1500 bytes, 1500 - 48 = 1452.
61 const QuicByteCount kMaxPacketSize
= 1452;
62 // Default maximum packet size used in the Linux TCP implementation.
63 // Used in QUIC for congestion window computations in bytes.
64 const QuicByteCount kDefaultTCPMSS
= 1460;
66 // We match SPDY's use of 32 when secure (since we'd compete with SPDY).
67 const QuicPacketCount kInitialCongestionWindowSecure
= 32;
68 // Be conservative, and just use double a typical TCP ICWND for HTTP.
69 const QuicPacketCount kInitialCongestionWindowInsecure
= 20;
71 // Minimum size of initial flow control window, for both stream and session.
72 const uint32 kMinimumFlowControlSendWindow
= 16 * 1024; // 16 KB
74 // Minimum size of the CWND, in packets, when doing bandwidth resumption.
75 const QuicPacketCount kMinCongestionWindowForBandwidthResumption
= 10;
77 // Maximum size of the CWND, in packets.
78 const QuicPacketCount kMaxCongestionWindow
= 200;
80 // Maximum number of tracked packets.
81 const QuicPacketCount kMaxTrackedPackets
= 5000;
83 // Default size of the socket receive buffer in bytes.
84 const QuicByteCount kDefaultSocketReceiveBuffer
= 256 * 1024;
85 // Minimum size of the socket receive buffer in bytes.
86 // Smaller values are ignored.
87 const QuicByteCount kMinSocketReceiveBuffer
= 16 * 1024;
89 // Fraction of the receive buffer that can be used for encrypted bytes.
90 // Allows a 5% overhead for IP and UDP framing, as well as ack only packets.
91 static const float kUsableRecieveBufferFraction
= 0.95f
;
92 // Fraction of the receive buffer that can be used, based on conservative
93 // estimates and testing on Linux.
94 // An alternative to kUsableRecieveBufferFraction.
95 static const float kConservativeReceiveBufferFraction
= 0.6f
;
97 // Don't allow a client to suggest an RTT shorter than 10ms.
98 const uint32 kMinInitialRoundTripTimeUs
= 10 * kNumMicrosPerMilli
;
100 // Don't allow a client to suggest an RTT longer than 15 seconds.
101 const uint32 kMaxInitialRoundTripTimeUs
= 15 * kNumMicrosPerSecond
;
103 // Maximum number of open streams per connection.
104 const size_t kDefaultMaxStreamsPerConnection
= 100;
106 // Number of bytes reserved for public flags in the packet header.
107 const size_t kPublicFlagsSize
= 1;
108 // Number of bytes reserved for version number in the packet header.
109 const size_t kQuicVersionSize
= 4;
110 // Number of bytes reserved for private flags in the packet header.
111 const size_t kPrivateFlagsSize
= 1;
112 // Number of bytes reserved for FEC group in the packet header.
113 const size_t kFecGroupSize
= 1;
115 // Signifies that the QuicPacket will contain version of the protocol.
116 const bool kIncludeVersion
= true;
118 // Index of the first byte in a QUIC packet which is used in hash calculation.
119 const size_t kStartOfHashData
= 0;
121 // Reserved ID for the crypto stream.
122 const QuicStreamId kCryptoStreamId
= 1;
124 // Reserved ID for the headers stream.
125 const QuicStreamId kHeadersStreamId
= 3;
127 // Maximum delayed ack time, in ms.
128 const int64 kMaxDelayedAckTimeMs
= 25;
130 // The timeout before the handshake succeeds.
131 const int64 kInitialIdleTimeoutSecs
= 5;
132 // The default idle timeout.
133 const int64 kDefaultIdleTimeoutSecs
= 30;
134 // The maximum idle timeout that can be negotiated.
135 const int64 kMaximumIdleTimeoutSecs
= 60 * 10; // 10 minutes.
136 // The default timeout for a connection until the crypto handshake succeeds.
137 const int64 kMaxTimeForCryptoHandshakeSecs
= 10; // 10 secs.
139 // Default limit on the number of undecryptable packets the connection buffers
140 // before the CHLO/SHLO arrive.
141 const size_t kDefaultMaxUndecryptablePackets
= 10;
143 // Default ping timeout.
144 const int64 kPingTimeoutSecs
= 15; // 15 secs.
146 // Minimum number of RTTs between Server Config Updates (SCUP) sent to client.
147 const int kMinIntervalBetweenServerConfigUpdatesRTTs
= 10;
149 // Minimum time between Server Config Updates (SCUP) sent to client.
150 const int kMinIntervalBetweenServerConfigUpdatesMs
= 1000;
152 // Minimum number of packets between Server Config Updates (SCUP).
153 const int kMinPacketsBetweenServerConfigUpdates
= 100;
155 // The number of open streams that a server will accept is set to be slightly
156 // larger than the negotiated limit. Immediately closing the connection if the
157 // client opens slightly too many streams is not ideal: the client may have sent
158 // a FIN that was lost, and simultaneously opened a new stream. The number of
159 // streams a server accepts is a fixed increment over the negotiated limit, or a
160 // percentage increase, whichever is larger.
161 const float kMaxStreamsMultiplier
= 1.1f
;
162 const int kMaxStreamsMinimumIncrement
= 10;
164 // We define an unsigned 16-bit floating point value, inspired by IEEE floats
165 // (http://en.wikipedia.org/wiki/Half_precision_floating-point_format),
166 // with 5-bit exponent (bias 1), 11-bit mantissa (effective 12 with hidden
167 // bit) and denormals, but without signs, transfinites or fractions. Wire format
168 // 16 bits (little-endian byte order) are split into exponent (high 5) and
169 // mantissa (low 11) and decoded as:
171 // if (exponent == 0) value = mantissa;
172 // else value = (mantissa | 1 << 11) << (exponent - 1)
173 const int kUFloat16ExponentBits
= 5;
174 const int kUFloat16MaxExponent
= (1 << kUFloat16ExponentBits
) - 2; // 30
175 const int kUFloat16MantissaBits
= 16 - kUFloat16ExponentBits
; // 11
176 const int kUFloat16MantissaEffectiveBits
= kUFloat16MantissaBits
+ 1; // 12
177 const uint64 kUFloat16MaxValue
= // 0x3FFC0000000
178 ((UINT64_C(1) << kUFloat16MantissaEffectiveBits
) - 1) <<
179 kUFloat16MaxExponent
;
181 enum TransmissionType
{
183 FIRST_TRANSMISSION_TYPE
= NOT_RETRANSMISSION
,
184 HANDSHAKE_RETRANSMISSION
, // Retransmits due to handshake timeouts.
185 ALL_UNACKED_RETRANSMISSION
, // Retransmits all unacked packets.
186 ALL_INITIAL_RETRANSMISSION
, // Retransmits all initially encrypted packets.
187 LOSS_RETRANSMISSION
, // Retransmits due to loss detection.
188 RTO_RETRANSMISSION
, // Retransmits due to retransmit time out.
189 TLP_RETRANSMISSION
, // Tail loss probes.
190 LAST_TRANSMISSION_TYPE
= TLP_RETRANSMISSION
,
193 enum HasRetransmittableData
{
194 NO_RETRANSMITTABLE_DATA
,
195 HAS_RETRANSMITTABLE_DATA
,
203 enum class Perspective
{ IS_SERVER
, IS_CLIENT
};
205 NET_EXPORT_PRIVATE
std::ostream
& operator<<(std::ostream
& os
,
206 const Perspective
& s
);
208 // Indicates FEC protection level for data being written.
210 MUST_FEC_PROTECT
, // Callee must FEC protect this data.
211 MAY_FEC_PROTECT
// Callee does not have to but may FEC protect this data.
214 // Indicates FEC policy.
216 FEC_PROTECT_ALWAYS
, // All data in the stream should be FEC protected.
217 FEC_PROTECT_OPTIONAL
// Data in the stream does not need FEC protection.
220 // Indicates FEC policy about when to send FEC packet.
222 // Send FEC packet when FEC group is full or when FEC alarm goes off.
224 // Send FEC packet only when FEC alarm goes off.
229 // Regular frame types. The values set here cannot change without the
230 // introduction of a new QUIC version.
232 RST_STREAM_FRAME
= 1,
233 CONNECTION_CLOSE_FRAME
= 2,
235 WINDOW_UPDATE_FRAME
= 4,
237 STOP_WAITING_FRAME
= 6,
240 // STREAM and ACK frames are special frames. They are encoded differently on
241 // the wire and their values do not need to be stable.
244 // The path MTU discovery frame is encoded as a PING frame on the wire.
249 enum QuicConnectionIdLength
{
250 PACKET_0BYTE_CONNECTION_ID
= 0,
251 PACKET_1BYTE_CONNECTION_ID
= 1,
252 PACKET_4BYTE_CONNECTION_ID
= 4,
253 PACKET_8BYTE_CONNECTION_ID
= 8
261 enum QuicSequenceNumberLength
{
262 PACKET_1BYTE_SEQUENCE_NUMBER
= 1,
263 PACKET_2BYTE_SEQUENCE_NUMBER
= 2,
264 PACKET_4BYTE_SEQUENCE_NUMBER
= 4,
265 PACKET_6BYTE_SEQUENCE_NUMBER
= 6
268 // Used to indicate a QuicSequenceNumberLength using two flag bits.
269 enum QuicSequenceNumberLengthFlags
{
270 PACKET_FLAGS_1BYTE_SEQUENCE
= 0, // 00
271 PACKET_FLAGS_2BYTE_SEQUENCE
= 1, // 01
272 PACKET_FLAGS_4BYTE_SEQUENCE
= 1 << 1, // 10
273 PACKET_FLAGS_6BYTE_SEQUENCE
= 1 << 1 | 1, // 11
276 // The public flags are specified in one byte.
277 enum QuicPacketPublicFlags
{
278 PACKET_PUBLIC_FLAGS_NONE
= 0,
280 // Bit 0: Does the packet header contains version info?
281 PACKET_PUBLIC_FLAGS_VERSION
= 1 << 0,
283 // Bit 1: Is this packet a public reset packet?
284 PACKET_PUBLIC_FLAGS_RST
= 1 << 1,
286 // Bits 2 and 3 specify the length of the ConnectionId as follows:
291 PACKET_PUBLIC_FLAGS_0BYTE_CONNECTION_ID
= 0,
292 PACKET_PUBLIC_FLAGS_1BYTE_CONNECTION_ID
= 1 << 2,
293 PACKET_PUBLIC_FLAGS_4BYTE_CONNECTION_ID
= 1 << 3,
294 PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID
= 1 << 3 | 1 << 2,
296 // Bits 4 and 5 describe the packet sequence number length as follows:
301 PACKET_PUBLIC_FLAGS_1BYTE_SEQUENCE
= PACKET_FLAGS_1BYTE_SEQUENCE
<< 4,
302 PACKET_PUBLIC_FLAGS_2BYTE_SEQUENCE
= PACKET_FLAGS_2BYTE_SEQUENCE
<< 4,
303 PACKET_PUBLIC_FLAGS_4BYTE_SEQUENCE
= PACKET_FLAGS_4BYTE_SEQUENCE
<< 4,
304 PACKET_PUBLIC_FLAGS_6BYTE_SEQUENCE
= PACKET_FLAGS_6BYTE_SEQUENCE
<< 4,
306 // All bits set (bits 6 and 7 are not currently used): 00111111
307 PACKET_PUBLIC_FLAGS_MAX
= (1 << 6) - 1
310 // The private flags are specified in one byte.
311 enum QuicPacketPrivateFlags
{
312 PACKET_PRIVATE_FLAGS_NONE
= 0,
314 // Bit 0: Does this packet contain an entropy bit?
315 PACKET_PRIVATE_FLAGS_ENTROPY
= 1 << 0,
317 // Bit 1: Payload is part of an FEC group?
318 PACKET_PRIVATE_FLAGS_FEC_GROUP
= 1 << 1,
320 // Bit 2: Payload is FEC as opposed to frames?
321 PACKET_PRIVATE_FLAGS_FEC
= 1 << 2,
323 // All bits set (bits 3-7 are not currently used): 00000111
324 PACKET_PRIVATE_FLAGS_MAX
= (1 << 3) - 1
327 // The available versions of QUIC. Guaranteed that the integer value of the enum
328 // will match the version number.
329 // When adding a new version to this enum you should add it to
330 // kSupportedQuicVersions (if appropriate), and also add a new case to the
331 // helper methods QuicVersionToQuicTag, QuicTagToQuicVersion, and
332 // QuicVersionToString.
334 // Special case to indicate unknown/unsupported QUIC version.
335 QUIC_VERSION_UNSUPPORTED
= 0,
337 QUIC_VERSION_24
= 24, // SPDY/4 header compression.
338 QUIC_VERSION_25
= 25, // SPDY/4 header keys, and removal of error_details
339 // from QuicRstStreamFrame
342 // This vector contains QUIC versions which we currently support.
343 // This should be ordered such that the highest supported version is the first
344 // element, with subsequent elements in descending order (versions can be
345 // skipped as necessary).
347 // IMPORTANT: if you are adding to this list, follow the instructions at
348 // http://sites/quic/adding-and-removing-versions
349 static const QuicVersion kSupportedQuicVersions
[] = {QUIC_VERSION_25
,
352 typedef std::vector
<QuicVersion
> QuicVersionVector
;
354 // Returns a vector of QUIC versions in kSupportedQuicVersions.
355 NET_EXPORT_PRIVATE QuicVersionVector
QuicSupportedVersions();
357 // QuicTag is written to and read from the wire, but we prefer to use
358 // the more readable QuicVersion at other levels.
359 // Helper function which translates from a QuicVersion to a QuicTag. Returns 0
360 // if QuicVersion is unsupported.
361 NET_EXPORT_PRIVATE QuicTag
QuicVersionToQuicTag(const QuicVersion version
);
363 // Returns appropriate QuicVersion from a QuicTag.
364 // Returns QUIC_VERSION_UNSUPPORTED if version_tag cannot be understood.
365 NET_EXPORT_PRIVATE QuicVersion
QuicTagToQuicVersion(const QuicTag version_tag
);
367 // Helper function which translates from a QuicVersion to a string.
368 // Returns strings corresponding to enum names (e.g. QUIC_VERSION_6).
369 NET_EXPORT_PRIVATE
std::string
QuicVersionToString(const QuicVersion version
);
371 // Returns comma separated list of string representations of QuicVersion enum
372 // values in the supplied |versions| vector.
373 NET_EXPORT_PRIVATE
std::string
QuicVersionVectorToString(
374 const QuicVersionVector
& versions
);
376 // Version and Crypto tags are written to the wire with a big-endian
377 // representation of the name of the tag. For example
378 // the client hello tag (CHLO) will be written as the
379 // following 4 bytes: 'C' 'H' 'L' 'O'. Since it is
380 // stored in memory as a little endian uint32, we need
381 // to reverse the order of the bytes.
383 // MakeQuicTag returns a value given the four bytes. For example:
384 // MakeQuicTag('C', 'H', 'L', 'O');
385 NET_EXPORT_PRIVATE QuicTag
MakeQuicTag(char a
, char b
, char c
, char d
);
387 // Returns true if the tag vector contains the specified tag.
388 NET_EXPORT_PRIVATE
bool ContainsQuicTag(const QuicTagVector
& tag_vector
,
391 // Size in bytes of the data or fec packet header.
392 NET_EXPORT_PRIVATE
size_t GetPacketHeaderSize(const QuicPacketHeader
& header
);
394 NET_EXPORT_PRIVATE
size_t GetPacketHeaderSize(
395 QuicConnectionIdLength connection_id_length
,
396 bool include_version
,
397 QuicSequenceNumberLength sequence_number_length
,
398 InFecGroup is_in_fec_group
);
400 // Index of the first byte in a QUIC packet of FEC protected data.
401 NET_EXPORT_PRIVATE
size_t GetStartOfFecProtectedData(
402 QuicConnectionIdLength connection_id_length
,
403 bool include_version
,
404 QuicSequenceNumberLength sequence_number_length
);
405 // Index of the first byte in a QUIC packet of encrypted data.
406 NET_EXPORT_PRIVATE
size_t GetStartOfEncryptedData(
407 QuicConnectionIdLength connection_id_length
,
408 bool include_version
,
409 QuicSequenceNumberLength sequence_number_length
);
411 enum QuicRstStreamErrorCode
{
412 QUIC_STREAM_NO_ERROR
= 0,
414 // There was some error which halted stream processing.
415 QUIC_ERROR_PROCESSING_STREAM
,
416 // We got two fin or reset offsets which did not match.
417 QUIC_MULTIPLE_TERMINATION_OFFSETS
,
418 // We got bad payload and can not respond to it at the protocol level.
419 QUIC_BAD_APPLICATION_PAYLOAD
,
420 // Stream closed due to connection error. No reset frame is sent when this
422 QUIC_STREAM_CONNECTION_ERROR
,
423 // GoAway frame sent. No more stream can be created.
424 QUIC_STREAM_PEER_GOING_AWAY
,
425 // The stream has been cancelled.
426 QUIC_STREAM_CANCELLED
,
427 // Closing stream locally, sending a RST to allow for proper flow control
428 // accounting. Sent in response to a RST from the peer.
429 QUIC_RST_ACKNOWLEDGEMENT
,
431 // No error. Used as bound while iterating.
432 QUIC_STREAM_LAST_ERROR
,
435 // Because receiving an unknown QuicRstStreamErrorCode results in connection
436 // teardown, we use this to make sure any errors predating a given version are
437 // downgraded to the most appropriate existing error.
438 NET_EXPORT_PRIVATE QuicRstStreamErrorCode
AdjustErrorForVersion(
439 QuicRstStreamErrorCode error_code
,
440 QuicVersion version
);
442 // These values must remain stable as they are uploaded to UMA histograms.
443 // To add a new error code, use the current value of QUIC_LAST_ERROR and
444 // increment QUIC_LAST_ERROR.
448 // Connection has reached an invalid state.
449 QUIC_INTERNAL_ERROR
= 1,
450 // There were data frames after the a fin or reset.
451 QUIC_STREAM_DATA_AFTER_TERMINATION
= 2,
452 // Control frame is malformed.
453 QUIC_INVALID_PACKET_HEADER
= 3,
454 // Frame data is malformed.
455 QUIC_INVALID_FRAME_DATA
= 4,
456 // The packet contained no payload.
457 QUIC_MISSING_PAYLOAD
= 48,
458 // FEC data is malformed.
459 QUIC_INVALID_FEC_DATA
= 5,
460 // STREAM frame data is malformed.
461 QUIC_INVALID_STREAM_DATA
= 46,
462 // STREAM frame data is not encrypted.
463 QUIC_UNENCRYPTED_STREAM_DATA
= 61,
464 // RST_STREAM frame data is malformed.
465 QUIC_INVALID_RST_STREAM_DATA
= 6,
466 // CONNECTION_CLOSE frame data is malformed.
467 QUIC_INVALID_CONNECTION_CLOSE_DATA
= 7,
468 // GOAWAY frame data is malformed.
469 QUIC_INVALID_GOAWAY_DATA
= 8,
470 // WINDOW_UPDATE frame data is malformed.
471 QUIC_INVALID_WINDOW_UPDATE_DATA
= 57,
472 // BLOCKED frame data is malformed.
473 QUIC_INVALID_BLOCKED_DATA
= 58,
474 // STOP_WAITING frame data is malformed.
475 QUIC_INVALID_STOP_WAITING_DATA
= 60,
476 // ACK frame data is malformed.
477 QUIC_INVALID_ACK_DATA
= 9,
479 // deprecated: QUIC_INVALID_CONGESTION_FEEDBACK_DATA = 47,
481 // Version negotiation packet is malformed.
482 QUIC_INVALID_VERSION_NEGOTIATION_PACKET
= 10,
483 // Public RST packet is malformed.
484 QUIC_INVALID_PUBLIC_RST_PACKET
= 11,
485 // There was an error decrypting.
486 QUIC_DECRYPTION_FAILURE
= 12,
487 // There was an error encrypting.
488 QUIC_ENCRYPTION_FAILURE
= 13,
489 // The packet exceeded kMaxPacketSize.
490 QUIC_PACKET_TOO_LARGE
= 14,
491 // Data was sent for a stream which did not exist.
492 QUIC_PACKET_FOR_NONEXISTENT_STREAM
= 15,
493 // The peer is going away. May be a client or server.
494 QUIC_PEER_GOING_AWAY
= 16,
495 // A stream ID was invalid.
496 QUIC_INVALID_STREAM_ID
= 17,
497 // A priority was invalid.
498 QUIC_INVALID_PRIORITY
= 49,
499 // Too many streams already open.
500 QUIC_TOO_MANY_OPEN_STREAMS
= 18,
501 // The peer must send a FIN/RST for each stream, and has not been doing so.
502 QUIC_TOO_MANY_UNFINISHED_STREAMS
= 66,
503 // Received public reset for this connection.
504 QUIC_PUBLIC_RESET
= 19,
505 // Invalid protocol version.
506 QUIC_INVALID_VERSION
= 20,
508 // deprecated: QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED = 21
510 // The Header ID for a stream was too far from the previous.
511 QUIC_INVALID_HEADER_ID
= 22,
512 // Negotiable parameter received during handshake had invalid value.
513 QUIC_INVALID_NEGOTIATED_VALUE
= 23,
514 // There was an error decompressing data.
515 QUIC_DECOMPRESSION_FAILURE
= 24,
516 // We hit our prenegotiated (or default) timeout
517 QUIC_CONNECTION_TIMED_OUT
= 25,
518 // We hit our overall connection timeout
519 QUIC_CONNECTION_OVERALL_TIMED_OUT
= 67,
520 // There was an error encountered migrating addresses
521 QUIC_ERROR_MIGRATING_ADDRESS
= 26,
522 // There was an error while writing to the socket.
523 QUIC_PACKET_WRITE_ERROR
= 27,
524 // There was an error while reading from the socket.
525 QUIC_PACKET_READ_ERROR
= 51,
526 // We received a STREAM_FRAME with no data and no fin flag set.
527 QUIC_INVALID_STREAM_FRAME
= 50,
528 // We received invalid data on the headers stream.
529 QUIC_INVALID_HEADERS_STREAM_DATA
= 56,
530 // The peer received too much data, violating flow control.
531 QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA
= 59,
532 // The peer sent too much data, violating flow control.
533 QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA
= 63,
534 // The peer received an invalid flow control window.
535 QUIC_FLOW_CONTROL_INVALID_WINDOW
= 64,
536 // The connection has been IP pooled into an existing connection.
537 QUIC_CONNECTION_IP_POOLED
= 62,
538 // The connection has too many outstanding sent packets.
539 QUIC_TOO_MANY_OUTSTANDING_SENT_PACKETS
= 68,
540 // The connection has too many outstanding received packets.
541 QUIC_TOO_MANY_OUTSTANDING_RECEIVED_PACKETS
= 69,
542 // The quic connection job to load server config is cancelled.
543 QUIC_CONNECTION_CANCELLED
= 70,
544 // Disabled QUIC because of high packet loss rate.
545 QUIC_BAD_PACKET_LOSS_RATE
= 71,
546 // Disabled QUIC because of too many PUBLIC_RESETs post handshake.
547 QUIC_PUBLIC_RESETS_POST_HANDSHAKE
= 73,
548 // Disabled QUIC because of too many timeouts with streams open.
549 QUIC_TIMEOUTS_WITH_OPEN_STREAMS
= 74,
550 // Closed because we failed to serialize a packet.
551 QUIC_FAILED_TO_SERIALIZE_PACKET
= 75,
556 QUIC_HANDSHAKE_FAILED
= 28,
557 // Handshake message contained out of order tags.
558 QUIC_CRYPTO_TAGS_OUT_OF_ORDER
= 29,
559 // Handshake message contained too many entries.
560 QUIC_CRYPTO_TOO_MANY_ENTRIES
= 30,
561 // Handshake message contained an invalid value length.
562 QUIC_CRYPTO_INVALID_VALUE_LENGTH
= 31,
563 // A crypto message was received after the handshake was complete.
564 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE
= 32,
565 // A crypto message was received with an illegal message tag.
566 QUIC_INVALID_CRYPTO_MESSAGE_TYPE
= 33,
567 // A crypto message was received with an illegal parameter.
568 QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER
= 34,
569 // An invalid channel id signature was supplied.
570 QUIC_INVALID_CHANNEL_ID_SIGNATURE
= 52,
571 // A crypto message was received with a mandatory parameter missing.
572 QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND
= 35,
573 // A crypto message was received with a parameter that has no overlap
574 // with the local parameter.
575 QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP
= 36,
576 // A crypto message was received that contained a parameter with too few
578 QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND
= 37,
579 // An internal error occured in crypto processing.
580 QUIC_CRYPTO_INTERNAL_ERROR
= 38,
581 // A crypto handshake message specified an unsupported version.
582 QUIC_CRYPTO_VERSION_NOT_SUPPORTED
= 39,
583 // A crypto handshake message resulted in a stateless reject.
584 QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT
= 72,
585 // There was no intersection between the crypto primitives supported by the
586 // peer and ourselves.
587 QUIC_CRYPTO_NO_SUPPORT
= 40,
588 // The server rejected our client hello messages too many times.
589 QUIC_CRYPTO_TOO_MANY_REJECTS
= 41,
590 // The client rejected the server's certificate chain or signature.
591 QUIC_PROOF_INVALID
= 42,
592 // A crypto message was received with a duplicate tag.
593 QUIC_CRYPTO_DUPLICATE_TAG
= 43,
594 // A crypto message was received with the wrong encryption level (i.e. it
595 // should have been encrypted but was not.)
596 QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT
= 44,
597 // The server config for a server has expired.
598 QUIC_CRYPTO_SERVER_CONFIG_EXPIRED
= 45,
599 // We failed to setup the symmetric keys for a connection.
600 QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED
= 53,
601 // A handshake message arrived, but we are still validating the
602 // previous handshake message.
603 QUIC_CRYPTO_MESSAGE_WHILE_VALIDATING_CLIENT_HELLO
= 54,
604 // A server config update arrived before the handshake is complete.
605 QUIC_CRYPTO_UPDATE_BEFORE_HANDSHAKE_COMPLETE
= 65,
606 // This connection involved a version negotiation which appears to have been
608 QUIC_VERSION_NEGOTIATION_MISMATCH
= 55,
610 // No error. Used as bound while iterating.
611 QUIC_LAST_ERROR
= 76,
614 struct NET_EXPORT_PRIVATE QuicPacketPublicHeader
{
615 QuicPacketPublicHeader();
616 explicit QuicPacketPublicHeader(const QuicPacketPublicHeader
& other
);
617 ~QuicPacketPublicHeader();
619 // Universal header. All QuicPacket headers will have a connection_id and
621 QuicConnectionId connection_id
;
622 QuicConnectionIdLength connection_id_length
;
625 QuicSequenceNumberLength sequence_number_length
;
626 QuicVersionVector versions
;
629 // An integer which cannot be a packet sequence number.
630 const QuicPacketSequenceNumber kInvalidPacketSequenceNumber
= 0;
632 // Header for Data or FEC packets.
633 struct NET_EXPORT_PRIVATE QuicPacketHeader
{
635 explicit QuicPacketHeader(const QuicPacketPublicHeader
& header
);
637 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
638 std::ostream
& os
, const QuicPacketHeader
& s
);
640 QuicPacketPublicHeader public_header
;
641 QuicPacketSequenceNumber packet_sequence_number
;
644 QuicPacketEntropyHash entropy_hash
;
645 InFecGroup is_in_fec_group
;
646 QuicFecGroupNumber fec_group
;
649 struct NET_EXPORT_PRIVATE QuicPublicResetPacket
{
650 QuicPublicResetPacket();
651 explicit QuicPublicResetPacket(const QuicPacketPublicHeader
& header
);
653 QuicPacketPublicHeader public_header
;
654 QuicPublicResetNonceProof nonce_proof
;
655 QuicPacketSequenceNumber rejected_sequence_number
;
656 IPEndPoint client_address
;
659 enum QuicVersionNegotiationState
{
660 START_NEGOTIATION
= 0,
661 // Server-side this implies we've sent a version negotiation packet and are
662 // waiting on the client to select a compatible version. Client-side this
663 // implies we've gotten a version negotiation packet, are retransmitting the
664 // initial packets with a supported version and are waiting for our first
665 // packet from the server.
666 NEGOTIATION_IN_PROGRESS
,
667 // This indicates this endpoint has received a packet from the peer with a
668 // version this endpoint supports. Version negotiation is complete, and the
669 // version number will no longer be sent with future packets.
673 typedef QuicPacketPublicHeader QuicVersionNegotiationPacket
;
675 // A padding frame contains no payload.
676 struct NET_EXPORT_PRIVATE QuicPaddingFrame
{
679 // A ping frame contains no payload, though it is retransmittable,
680 // and ACK'd just like other normal frames.
681 struct NET_EXPORT_PRIVATE QuicPingFrame
{
684 // A path MTU discovery frame contains no payload and is serialized as a ping
686 struct NET_EXPORT_PRIVATE QuicMtuDiscoveryFrame
{};
688 struct NET_EXPORT_PRIVATE QuicStreamFrame
{
690 QuicStreamFrame(const QuicStreamFrame
& frame
);
691 QuicStreamFrame(QuicStreamId stream_id
,
693 QuicStreamOffset offset
,
694 base::StringPiece data
);
696 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
697 std::ostream
& os
, const QuicStreamFrame
& s
);
699 QuicStreamId stream_id
;
701 QuicStreamOffset offset
; // Location of this data in the stream.
702 base::StringPiece data
;
705 // TODO(ianswett): Re-evaluate the trade-offs of hash_set vs set when framing
707 typedef std::set
<QuicPacketSequenceNumber
> SequenceNumberSet
;
708 typedef std::list
<QuicPacketSequenceNumber
> SequenceNumberList
;
711 std::pair
<QuicPacketSequenceNumber
, QuicTime
> > PacketTimeList
;
713 struct NET_EXPORT_PRIVATE QuicStopWaitingFrame
{
714 QuicStopWaitingFrame();
715 ~QuicStopWaitingFrame();
717 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
718 std::ostream
& os
, const QuicStopWaitingFrame
& s
);
719 // Entropy hash of all packets up to, but not including, the least unacked
721 QuicPacketEntropyHash entropy_hash
;
722 // The lowest packet we've sent which is unacked, and we expect an ack for.
723 QuicPacketSequenceNumber least_unacked
;
726 struct NET_EXPORT_PRIVATE QuicAckFrame
{
730 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
731 std::ostream
& os
, const QuicAckFrame
& s
);
733 // Entropy hash of all packets up to largest observed not including missing
735 QuicPacketEntropyHash entropy_hash
;
737 // The highest packet sequence number we've observed from the peer.
739 // In general, this should be the largest packet number we've received. In
740 // the case of truncated acks, we may have to advertise a lower "upper bound"
741 // than largest received, to avoid implicitly acking missing packets that
742 // don't fit in the missing packet list due to size limitations. In this
743 // case, largest_observed may be a packet which is also in the missing packets
745 QuicPacketSequenceNumber largest_observed
;
747 // Time elapsed since largest_observed was received until this Ack frame was
749 QuicTime::Delta delta_time_largest_observed
;
751 // TODO(satyamshekhar): Can be optimized using an interval set like data
753 // The set of packets which we're expecting and have not received.
754 SequenceNumberSet missing_packets
;
756 // Whether the ack had to be truncated when sent.
759 // Packets which have been revived via FEC.
760 // All of these must also be in missing_packets.
761 SequenceNumberSet revived_packets
;
763 // List of <sequence_number, time> for when packets arrived.
764 PacketTimeList received_packet_times
;
767 // True if the sequence number is greater than largest_observed or is listed
769 // Always returns false for sequence numbers less than least_unacked.
770 bool NET_EXPORT_PRIVATE
IsAwaitingPacket(
771 const QuicAckFrame
& ack_frame
,
772 QuicPacketSequenceNumber sequence_number
);
774 // Inserts missing packets between [lower, higher).
775 void NET_EXPORT_PRIVATE
InsertMissingPacketsBetween(
776 QuicAckFrame
* ack_frame
,
777 QuicPacketSequenceNumber lower
,
778 QuicPacketSequenceNumber higher
);
780 // Defines for all types of congestion control algorithms that can be used in
781 // QUIC. Note that this is separate from the congestion feedback type -
782 // some congestion control algorithms may use the same feedback type
783 // (Reno and Cubic are the classic example for that).
784 enum CongestionControlType
{
792 enum LossDetectionType
{
793 kNack
, // Used to mimic TCP's loss detection.
794 kTime
, // Time based loss detection.
797 struct NET_EXPORT_PRIVATE QuicRstStreamFrame
{
798 QuicRstStreamFrame();
799 QuicRstStreamFrame(QuicStreamId stream_id
,
800 QuicRstStreamErrorCode error_code
,
801 QuicStreamOffset bytes_written
);
803 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
804 std::ostream
& os
, const QuicRstStreamFrame
& r
);
806 QuicStreamId stream_id
;
807 QuicRstStreamErrorCode error_code
;
808 // Only used in versions <= QUIC_VERSION_24.
809 std::string error_details
;
811 // Used to update flow control windows. On termination of a stream, both
812 // endpoints must inform the peer of the number of bytes they have sent on
813 // that stream. This can be done through normal termination (data packet with
814 // FIN) or through a RST.
815 QuicStreamOffset byte_offset
;
818 struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame
{
819 QuicConnectionCloseFrame();
821 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
822 std::ostream
& os
, const QuicConnectionCloseFrame
& c
);
824 QuicErrorCode error_code
;
825 std::string error_details
;
828 struct NET_EXPORT_PRIVATE QuicGoAwayFrame
{
830 QuicGoAwayFrame(QuicErrorCode error_code
,
831 QuicStreamId last_good_stream_id
,
832 const std::string
& reason
);
834 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
835 std::ostream
& os
, const QuicGoAwayFrame
& g
);
837 QuicErrorCode error_code
;
838 QuicStreamId last_good_stream_id
;
839 std::string reason_phrase
;
842 // Flow control updates per-stream and at the connection levoel.
843 // Based on SPDY's WINDOW_UPDATE frame, but uses an absolute byte offset rather
844 // than a window delta.
845 // TODO(rjshade): A possible future optimization is to make stream_id and
846 // byte_offset variable length, similar to stream frames.
847 struct NET_EXPORT_PRIVATE QuicWindowUpdateFrame
{
848 QuicWindowUpdateFrame() {}
849 QuicWindowUpdateFrame(QuicStreamId stream_id
, QuicStreamOffset byte_offset
);
851 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
852 std::ostream
& os
, const QuicWindowUpdateFrame
& w
);
854 // The stream this frame applies to. 0 is a special case meaning the overall
855 // connection rather than a specific stream.
856 QuicStreamId stream_id
;
858 // Byte offset in the stream or connection. The receiver of this frame must
859 // not send data which would result in this offset being exceeded.
860 QuicStreamOffset byte_offset
;
863 // The BLOCKED frame is used to indicate to the remote endpoint that this
864 // endpoint believes itself to be flow-control blocked but otherwise ready to
865 // send data. The BLOCKED frame is purely advisory and optional.
866 // Based on SPDY's BLOCKED frame (undocumented as of 2014-01-28).
867 struct NET_EXPORT_PRIVATE QuicBlockedFrame
{
868 QuicBlockedFrame() {}
869 explicit QuicBlockedFrame(QuicStreamId stream_id
);
871 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
872 std::ostream
& os
, const QuicBlockedFrame
& b
);
874 // The stream this frame applies to. 0 is a special case meaning the overall
875 // connection rather than a specific stream.
876 QuicStreamId stream_id
;
879 // EncryptionLevel enumerates the stages of encryption that a QUIC connection
880 // progresses through. When retransmitting a packet, the encryption level needs
881 // to be specified so that it is retransmitted at a level which the peer can
883 enum EncryptionLevel
{
885 ENCRYPTION_INITIAL
= 1,
886 ENCRYPTION_FORWARD_SECURE
= 2,
888 NUM_ENCRYPTION_LEVELS
,
891 struct NET_EXPORT_PRIVATE QuicFrame
{
893 explicit QuicFrame(QuicPaddingFrame
* padding_frame
);
894 explicit QuicFrame(QuicStreamFrame
* stream_frame
);
895 explicit QuicFrame(QuicAckFrame
* frame
);
896 explicit QuicFrame(QuicMtuDiscoveryFrame
* frame
);
898 explicit QuicFrame(QuicRstStreamFrame
* frame
);
899 explicit QuicFrame(QuicConnectionCloseFrame
* frame
);
900 explicit QuicFrame(QuicStopWaitingFrame
* frame
);
901 explicit QuicFrame(QuicPingFrame
* frame
);
902 explicit QuicFrame(QuicGoAwayFrame
* frame
);
903 explicit QuicFrame(QuicWindowUpdateFrame
* frame
);
904 explicit QuicFrame(QuicBlockedFrame
* frame
);
906 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
907 std::ostream
& os
, const QuicFrame
& frame
);
911 QuicPaddingFrame
* padding_frame
;
912 QuicStreamFrame
* stream_frame
;
913 QuicAckFrame
* ack_frame
;
914 QuicMtuDiscoveryFrame
* mtu_discovery_frame
;
916 QuicStopWaitingFrame
* stop_waiting_frame
;
918 QuicPingFrame
* ping_frame
;
919 QuicRstStreamFrame
* rst_stream_frame
;
920 QuicConnectionCloseFrame
* connection_close_frame
;
921 QuicGoAwayFrame
* goaway_frame
;
922 QuicWindowUpdateFrame
* window_update_frame
;
923 QuicBlockedFrame
* blocked_frame
;
927 typedef std::vector
<QuicFrame
> QuicFrames
;
929 struct NET_EXPORT_PRIVATE QuicFecData
{
932 // The FEC group number is also the sequence number of the first
933 // FEC protected packet. The last protected packet's sequence number will
934 // be one less than the sequence number of the FEC packet.
935 QuicFecGroupNumber fec_group
;
936 base::StringPiece redundancy
;
939 class NET_EXPORT_PRIVATE QuicData
{
941 QuicData(const char* buffer
, size_t length
);
942 QuicData(char* buffer
, size_t length
, bool owns_buffer
);
945 base::StringPiece
AsStringPiece() const {
946 return base::StringPiece(data(), length());
949 const char* data() const { return buffer_
; }
950 size_t length() const { return length_
; }
951 bool owns_buffer() const { return owns_buffer_
; }
958 DISALLOW_COPY_AND_ASSIGN(QuicData
);
961 class NET_EXPORT_PRIVATE QuicPacket
: public QuicData
{
963 QuicPacket(char* buffer
,
966 QuicConnectionIdLength connection_id_length
,
967 bool includes_version
,
968 QuicSequenceNumberLength sequence_number_length
);
970 base::StringPiece
FecProtectedData() const;
971 base::StringPiece
AssociatedData() const;
972 base::StringPiece
BeforePlaintext() const;
973 base::StringPiece
Plaintext() const;
975 char* mutable_data() { return buffer_
; }
979 const QuicConnectionIdLength connection_id_length_
;
980 const bool includes_version_
;
981 const QuicSequenceNumberLength sequence_number_length_
;
983 DISALLOW_COPY_AND_ASSIGN(QuicPacket
);
986 class NET_EXPORT_PRIVATE QuicEncryptedPacket
: public QuicData
{
988 QuicEncryptedPacket(const char* buffer
, size_t length
);
989 QuicEncryptedPacket(char* buffer
, size_t length
, bool owns_buffer
);
991 // Clones the packet into a new packet which owns the buffer.
992 QuicEncryptedPacket
* Clone() const;
994 // By default, gtest prints the raw bytes of an object. The bool data
995 // member (in the base class QuicData) causes this object to have padding
996 // bytes, which causes the default gtest object printer to read
997 // uninitialize memory. So we need to teach gtest how to print this object.
998 NET_EXPORT_PRIVATE
friend std::ostream
& operator<<(
999 std::ostream
& os
, const QuicEncryptedPacket
& s
);
1002 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket
);
1005 class NET_EXPORT_PRIVATE RetransmittableFrames
{
1007 explicit RetransmittableFrames(EncryptionLevel level
);
1008 ~RetransmittableFrames();
1010 // Takes ownership of the frame inside |frame|.
1011 const QuicFrame
& AddFrame(const QuicFrame
& frame
);
1012 // Takes ownership of the frame inside |frame| and |buffer|.
1013 const QuicFrame
& AddFrame(const QuicFrame
& frame
, char* buffer
);
1014 // Removes all stream frames associated with |stream_id|.
1015 void RemoveFramesForStream(QuicStreamId stream_id
);
1017 const QuicFrames
& frames() const { return frames_
; }
1019 IsHandshake
HasCryptoHandshake() const {
1020 return has_crypto_handshake_
;
1023 EncryptionLevel
encryption_level() const {
1024 return encryption_level_
;
1027 bool needs_padding() const { return needs_padding_
; }
1029 void set_needs_padding(bool needs_padding
) { needs_padding_
= needs_padding
; }
1033 const EncryptionLevel encryption_level_
;
1034 IsHandshake has_crypto_handshake_
;
1035 bool needs_padding_
;
1036 // Data referenced by the StringPiece of a QuicStreamFrame.
1037 std::vector
<const char*> stream_data_
;
1039 DISALLOW_COPY_AND_ASSIGN(RetransmittableFrames
);
1042 struct NET_EXPORT_PRIVATE SerializedPacket
{
1043 SerializedPacket(QuicPacketSequenceNumber sequence_number
,
1044 QuicSequenceNumberLength sequence_number_length
,
1045 QuicEncryptedPacket
* packet
,
1046 QuicPacketEntropyHash entropy_hash
,
1047 RetransmittableFrames
* retransmittable_frames
,
1049 bool has_stop_waiting
);
1050 ~SerializedPacket();
1052 QuicEncryptedPacket
* packet
;
1053 RetransmittableFrames
* retransmittable_frames
;
1054 QuicPacketSequenceNumber sequence_number
;
1055 QuicSequenceNumberLength sequence_number_length
;
1056 QuicPacketEntropyHash entropy_hash
;
1059 bool has_stop_waiting
;
1061 // Optional notifiers which will be informed when this packet has been ACKed.
1062 std::list
<QuicAckNotifier
*> notifiers
;
1065 struct NET_EXPORT_PRIVATE TransmissionInfo
{
1066 // Used by STL when assigning into a map.
1069 // Constructs a Transmission with a new all_tranmissions set
1070 // containing |sequence_number|.
1071 TransmissionInfo(RetransmittableFrames
* retransmittable_frames
,
1072 QuicSequenceNumberLength sequence_number_length
,
1073 TransmissionType transmission_type
,
1075 QuicByteCount bytes_sent
,
1076 bool is_fec_packet
);
1078 RetransmittableFrames
* retransmittable_frames
;
1079 QuicSequenceNumberLength sequence_number_length
;
1081 QuicByteCount bytes_sent
;
1082 QuicPacketCount nack_count
;
1083 // Reason why this packet was transmitted.
1084 TransmissionType transmission_type
;
1085 // Stores the sequence numbers of all transmissions of this packet.
1086 // Must always be nullptr or have multiple elements.
1087 SequenceNumberList
* all_transmissions
;
1088 // In flight packets have not been abandoned or lost.
1090 // True if the packet can never be acked, so it can be removed.
1092 // True if the packet is an FEC packet.
1096 // Convenience wrapper to wrap an iovec array and the total length, which must
1097 // be less than or equal to the actual total length of the iovecs.
1098 struct NET_EXPORT_PRIVATE QuicIOVector
{
1099 QuicIOVector(const struct iovec
* iov
, int iov_count
, size_t total_length
)
1100 : iov(iov
), iov_count(iov_count
), total_length(total_length
) {}
1102 const struct iovec
* iov
;
1103 const int iov_count
;
1104 const size_t total_length
;
1109 #endif // NET_QUIC_QUIC_PROTOCOL_H_