Fix a case when the initial rtt could be 0 when the client tries to
[chromium-blink-merge.git] / net / quic / quic_protocol.h
blobad920d0f21028ad3bfca01f9ac35665204d800af
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_
8 #include <stddef.h>
9 #include <limits>
10 #include <list>
11 #include <map>
12 #include <ostream>
13 #include <set>
14 #include <string>
15 #include <utility>
16 #include <vector>
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"
29 namespace net {
31 class QuicAckNotifier;
32 class QuicPacket;
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 // TODO(rch): Consider Quic specific names for these constants.
52 // Default and initial maximum size in bytes of a QUIC packet.
53 const QuicByteCount kDefaultMaxPacketSize = 1350;
54 // The maximum packet size of any QUIC packet, based on ethernet's max size,
55 // minus the IP and UDP headers. IPv6 has a 40 byte header, UPD adds an
56 // additional 8 bytes. This is a total overhead of 48 bytes. Ethernet's
57 // max packet size is 1500 bytes, 1500 - 48 = 1452.
58 const QuicByteCount kMaxPacketSize = 1452;
59 // Default maximum packet size used in Linux TCP implementations.
60 const QuicByteCount kDefaultTCPMSS = 1460;
62 // Maximum size of the initial congestion window in packets.
63 const QuicPacketCount kDefaultInitialWindow = 10;
64 const QuicPacketCount kMaxInitialWindow = 100;
66 // Default size of initial flow control window, for both stream and session.
67 const uint32 kDefaultFlowControlSendWindow = 16 * 1024; // 16 KB
69 // Maximum size of the congestion window, in packets, for TCP congestion control
70 // algorithms.
71 const size_t kMaxTcpCongestionWindow = 200;
73 // Default size of the socket receive buffer in bytes.
74 const QuicByteCount kDefaultSocketReceiveBuffer = 256 * 1024;
75 // Minimum size of the socket receive buffer in bytes.
76 // Smaller values are ignored.
77 const QuicByteCount kMinSocketReceiveBuffer = 16 * 1024;
79 // Don't allow a client to suggest an RTT shorter than 10ms.
80 const uint32 kMinInitialRoundTripTimeUs = 10 * kNumMicrosPerMilli;
82 // Don't allow a client to suggest an RTT longer than 15 seconds.
83 const uint32 kMaxInitialRoundTripTimeUs = 15 * kNumMicrosPerSecond;
85 // Maximum number of open streams per connection.
86 const size_t kDefaultMaxStreamsPerConnection = 100;
88 // Number of bytes reserved for public flags in the packet header.
89 const size_t kPublicFlagsSize = 1;
90 // Number of bytes reserved for version number in the packet header.
91 const size_t kQuicVersionSize = 4;
92 // Number of bytes reserved for private flags in the packet header.
93 const size_t kPrivateFlagsSize = 1;
94 // Number of bytes reserved for FEC group in the packet header.
95 const size_t kFecGroupSize = 1;
97 // Signifies that the QuicPacket will contain version of the protocol.
98 const bool kIncludeVersion = true;
100 // Index of the first byte in a QUIC packet which is used in hash calculation.
101 const size_t kStartOfHashData = 0;
103 // Limit on the delta between stream IDs.
104 const QuicStreamId kMaxStreamIdDelta = 200;
105 // Limit on the delta between header IDs.
106 const QuicHeaderId kMaxHeaderIdDelta = 200;
108 // Reserved ID for the crypto stream.
109 const QuicStreamId kCryptoStreamId = 1;
111 // Reserved ID for the headers stream.
112 const QuicStreamId kHeadersStreamId = 3;
114 // Maximum delayed ack time, in ms.
115 const int64 kMaxDelayedAckTimeMs = 25;
117 // The timeout before the handshake succeeds.
118 const int64 kInitialIdleTimeoutSecs = 5;
119 // The default idle timeout.
120 const int64 kDefaultIdleTimeoutSecs = 30;
121 // The maximum idle timeout that can be negotiated.
122 const int64 kMaximumIdleTimeoutSecs = 60 * 10; // 10 minutes.
123 // The default timeout for a connection until the crypto handshake succeeds.
124 const int64 kMaxTimeForCryptoHandshakeSecs = 10; // 10 secs.
126 // Default limit on the number of undecryptable packets the connection buffers
127 // before the CHLO/SHLO arrive.
128 const size_t kDefaultMaxUndecryptablePackets = 10;
130 // Default ping timeout.
131 const int64 kPingTimeoutSecs = 15; // 15 secs.
133 // Minimum number of RTTs between Server Config Updates (SCUP) sent to client.
134 const int kMinIntervalBetweenServerConfigUpdatesRTTs = 10;
136 // Minimum time between Server Config Updates (SCUP) sent to client.
137 const int kMinIntervalBetweenServerConfigUpdatesMs = 1000;
139 // Minimum number of packets between Server Config Updates (SCUP).
140 const int kMinPacketsBetweenServerConfigUpdates = 100;
142 // The number of open streams that a server will accept is set to be slightly
143 // larger than the negotiated limit. Immediately closing the connection if the
144 // client opens slightly too many streams is not ideal: the client may have sent
145 // a FIN that was lost, and simultaneously opened a new stream. The number of
146 // streams a server accepts is a fixed increment over the negotiated limit, or a
147 // percentage increase, whichever is larger.
148 const float kMaxStreamsMultiplier = 1.1f;
149 const int kMaxStreamsMinimumIncrement = 10;
151 // We define an unsigned 16-bit floating point value, inspired by IEEE floats
152 // (http://en.wikipedia.org/wiki/Half_precision_floating-point_format),
153 // with 5-bit exponent (bias 1), 11-bit mantissa (effective 12 with hidden
154 // bit) and denormals, but without signs, transfinites or fractions. Wire format
155 // 16 bits (little-endian byte order) are split into exponent (high 5) and
156 // mantissa (low 11) and decoded as:
157 // uint64 value;
158 // if (exponent == 0) value = mantissa;
159 // else value = (mantissa | 1 << 11) << (exponent - 1)
160 const int kUFloat16ExponentBits = 5;
161 const int kUFloat16MaxExponent = (1 << kUFloat16ExponentBits) - 2; // 30
162 const int kUFloat16MantissaBits = 16 - kUFloat16ExponentBits; // 11
163 const int kUFloat16MantissaEffectiveBits = kUFloat16MantissaBits + 1; // 12
164 const uint64 kUFloat16MaxValue = // 0x3FFC0000000
165 ((GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits) - 1) <<
166 kUFloat16MaxExponent;
168 enum TransmissionType {
169 NOT_RETRANSMISSION,
170 FIRST_TRANSMISSION_TYPE = NOT_RETRANSMISSION,
171 HANDSHAKE_RETRANSMISSION, // Retransmits due to handshake timeouts.
172 ALL_UNACKED_RETRANSMISSION, // Retransmits all unacked packets.
173 ALL_INITIAL_RETRANSMISSION, // Retransmits all initially encrypted packets.
174 LOSS_RETRANSMISSION, // Retransmits due to loss detection.
175 RTO_RETRANSMISSION, // Retransmits due to retransmit time out.
176 TLP_RETRANSMISSION, // Tail loss probes.
177 LAST_TRANSMISSION_TYPE = TLP_RETRANSMISSION,
180 enum HasRetransmittableData {
181 NO_RETRANSMITTABLE_DATA,
182 HAS_RETRANSMITTABLE_DATA,
185 enum IsHandshake {
186 NOT_HANDSHAKE,
187 IS_HANDSHAKE
190 // Indicates FEC protection level for data being written.
191 enum FecProtection {
192 MUST_FEC_PROTECT, // Callee must FEC protect this data.
193 MAY_FEC_PROTECT // Callee does not have to but may FEC protect this data.
196 // Indicates FEC policy.
197 enum FecPolicy {
198 FEC_PROTECT_ALWAYS, // All data in the stream should be FEC protected.
199 FEC_PROTECT_OPTIONAL // Data in the stream does not need FEC protection.
202 enum QuicFrameType {
203 // Regular frame types. The values set here cannot change without the
204 // introduction of a new QUIC version.
205 PADDING_FRAME = 0,
206 RST_STREAM_FRAME = 1,
207 CONNECTION_CLOSE_FRAME = 2,
208 GOAWAY_FRAME = 3,
209 WINDOW_UPDATE_FRAME = 4,
210 BLOCKED_FRAME = 5,
211 STOP_WAITING_FRAME = 6,
212 PING_FRAME = 7,
214 // STREAM, ACK, and CONGESTION_FEEDBACK frames are special frames. They are
215 // encoded differently on the wire and their values do not need to be stable.
216 STREAM_FRAME,
217 ACK_FRAME,
218 CONGESTION_FEEDBACK_FRAME,
219 NUM_FRAME_TYPES
222 enum QuicConnectionIdLength {
223 PACKET_0BYTE_CONNECTION_ID = 0,
224 PACKET_1BYTE_CONNECTION_ID = 1,
225 PACKET_4BYTE_CONNECTION_ID = 4,
226 PACKET_8BYTE_CONNECTION_ID = 8
229 enum InFecGroup {
230 NOT_IN_FEC_GROUP,
231 IN_FEC_GROUP,
234 enum QuicSequenceNumberLength {
235 PACKET_1BYTE_SEQUENCE_NUMBER = 1,
236 PACKET_2BYTE_SEQUENCE_NUMBER = 2,
237 PACKET_4BYTE_SEQUENCE_NUMBER = 4,
238 PACKET_6BYTE_SEQUENCE_NUMBER = 6
241 // Used to indicate a QuicSequenceNumberLength using two flag bits.
242 enum QuicSequenceNumberLengthFlags {
243 PACKET_FLAGS_1BYTE_SEQUENCE = 0, // 00
244 PACKET_FLAGS_2BYTE_SEQUENCE = 1, // 01
245 PACKET_FLAGS_4BYTE_SEQUENCE = 1 << 1, // 10
246 PACKET_FLAGS_6BYTE_SEQUENCE = 1 << 1 | 1, // 11
249 // The public flags are specified in one byte.
250 enum QuicPacketPublicFlags {
251 PACKET_PUBLIC_FLAGS_NONE = 0,
253 // Bit 0: Does the packet header contains version info?
254 PACKET_PUBLIC_FLAGS_VERSION = 1 << 0,
256 // Bit 1: Is this packet a public reset packet?
257 PACKET_PUBLIC_FLAGS_RST = 1 << 1,
259 // Bits 2 and 3 specify the length of the ConnectionId as follows:
260 // ----00--: 0 bytes
261 // ----01--: 1 byte
262 // ----10--: 4 bytes
263 // ----11--: 8 bytes
264 PACKET_PUBLIC_FLAGS_0BYTE_CONNECTION_ID = 0,
265 PACKET_PUBLIC_FLAGS_1BYTE_CONNECTION_ID = 1 << 2,
266 PACKET_PUBLIC_FLAGS_4BYTE_CONNECTION_ID = 1 << 3,
267 PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID = 1 << 3 | 1 << 2,
269 // Bits 4 and 5 describe the packet sequence number length as follows:
270 // --00----: 1 byte
271 // --01----: 2 bytes
272 // --10----: 4 bytes
273 // --11----: 6 bytes
274 PACKET_PUBLIC_FLAGS_1BYTE_SEQUENCE = PACKET_FLAGS_1BYTE_SEQUENCE << 4,
275 PACKET_PUBLIC_FLAGS_2BYTE_SEQUENCE = PACKET_FLAGS_2BYTE_SEQUENCE << 4,
276 PACKET_PUBLIC_FLAGS_4BYTE_SEQUENCE = PACKET_FLAGS_4BYTE_SEQUENCE << 4,
277 PACKET_PUBLIC_FLAGS_6BYTE_SEQUENCE = PACKET_FLAGS_6BYTE_SEQUENCE << 4,
279 // All bits set (bits 6 and 7 are not currently used): 00111111
280 PACKET_PUBLIC_FLAGS_MAX = (1 << 6) - 1
283 // The private flags are specified in one byte.
284 enum QuicPacketPrivateFlags {
285 PACKET_PRIVATE_FLAGS_NONE = 0,
287 // Bit 0: Does this packet contain an entropy bit?
288 PACKET_PRIVATE_FLAGS_ENTROPY = 1 << 0,
290 // Bit 1: Payload is part of an FEC group?
291 PACKET_PRIVATE_FLAGS_FEC_GROUP = 1 << 1,
293 // Bit 2: Payload is FEC as opposed to frames?
294 PACKET_PRIVATE_FLAGS_FEC = 1 << 2,
296 // All bits set (bits 3-7 are not currently used): 00000111
297 PACKET_PRIVATE_FLAGS_MAX = (1 << 3) - 1
300 // The available versions of QUIC. Guaranteed that the integer value of the enum
301 // will match the version number.
302 // When adding a new version to this enum you should add it to
303 // kSupportedQuicVersions (if appropriate), and also add a new case to the
304 // helper methods QuicVersionToQuicTag, QuicTagToQuicVersion, and
305 // QuicVersionToString.
306 enum QuicVersion {
307 // Special case to indicate unknown/unsupported QUIC version.
308 QUIC_VERSION_UNSUPPORTED = 0,
310 QUIC_VERSION_19 = 19, // Connection level flow control.
311 QUIC_VERSION_21 = 21, // Headers/crypto streams are flow controlled.
312 QUIC_VERSION_22 = 22, // Send Server Config Update messages on crypto stream.
313 QUIC_VERSION_23 = 23, // Timestamp in the ack frame.
316 // This vector contains QUIC versions which we currently support.
317 // This should be ordered such that the highest supported version is the first
318 // element, with subsequent elements in descending order (versions can be
319 // skipped as necessary).
321 // IMPORTANT: if you are adding to this list, follow the instructions at
322 // http://sites/quic/adding-and-removing-versions
323 static const QuicVersion kSupportedQuicVersions[] = {QUIC_VERSION_23,
324 QUIC_VERSION_22,
325 QUIC_VERSION_19};
327 typedef std::vector<QuicVersion> QuicVersionVector;
329 // Returns a vector of QUIC versions in kSupportedQuicVersions.
330 NET_EXPORT_PRIVATE QuicVersionVector QuicSupportedVersions();
332 // QuicTag is written to and read from the wire, but we prefer to use
333 // the more readable QuicVersion at other levels.
334 // Helper function which translates from a QuicVersion to a QuicTag. Returns 0
335 // if QuicVersion is unsupported.
336 NET_EXPORT_PRIVATE QuicTag QuicVersionToQuicTag(const QuicVersion version);
338 // Returns appropriate QuicVersion from a QuicTag.
339 // Returns QUIC_VERSION_UNSUPPORTED if version_tag cannot be understood.
340 NET_EXPORT_PRIVATE QuicVersion QuicTagToQuicVersion(const QuicTag version_tag);
342 // Helper function which translates from a QuicVersion to a string.
343 // Returns strings corresponding to enum names (e.g. QUIC_VERSION_6).
344 NET_EXPORT_PRIVATE std::string QuicVersionToString(const QuicVersion version);
346 // Returns comma separated list of string representations of QuicVersion enum
347 // values in the supplied |versions| vector.
348 NET_EXPORT_PRIVATE std::string QuicVersionVectorToString(
349 const QuicVersionVector& versions);
351 // Version and Crypto tags are written to the wire with a big-endian
352 // representation of the name of the tag. For example
353 // the client hello tag (CHLO) will be written as the
354 // following 4 bytes: 'C' 'H' 'L' 'O'. Since it is
355 // stored in memory as a little endian uint32, we need
356 // to reverse the order of the bytes.
358 // MakeQuicTag returns a value given the four bytes. For example:
359 // MakeQuicTag('C', 'H', 'L', 'O');
360 NET_EXPORT_PRIVATE QuicTag MakeQuicTag(char a, char b, char c, char d);
362 // Returns true if the tag vector contains the specified tag.
363 NET_EXPORT_PRIVATE bool ContainsQuicTag(const QuicTagVector& tag_vector,
364 QuicTag tag);
366 // Size in bytes of the data or fec packet header.
367 NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(const QuicPacketHeader& header);
369 NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(
370 QuicConnectionIdLength connection_id_length,
371 bool include_version,
372 QuicSequenceNumberLength sequence_number_length,
373 InFecGroup is_in_fec_group);
375 // Index of the first byte in a QUIC packet of FEC protected data.
376 NET_EXPORT_PRIVATE size_t GetStartOfFecProtectedData(
377 QuicConnectionIdLength connection_id_length,
378 bool include_version,
379 QuicSequenceNumberLength sequence_number_length);
380 // Index of the first byte in a QUIC packet of encrypted data.
381 NET_EXPORT_PRIVATE size_t GetStartOfEncryptedData(
382 QuicConnectionIdLength connection_id_length,
383 bool include_version,
384 QuicSequenceNumberLength sequence_number_length);
386 enum QuicRstStreamErrorCode {
387 QUIC_STREAM_NO_ERROR = 0,
389 // There was some error which halted stream processing.
390 QUIC_ERROR_PROCESSING_STREAM,
391 // We got two fin or reset offsets which did not match.
392 QUIC_MULTIPLE_TERMINATION_OFFSETS,
393 // We got bad payload and can not respond to it at the protocol level.
394 QUIC_BAD_APPLICATION_PAYLOAD,
395 // Stream closed due to connection error. No reset frame is sent when this
396 // happens.
397 QUIC_STREAM_CONNECTION_ERROR,
398 // GoAway frame sent. No more stream can be created.
399 QUIC_STREAM_PEER_GOING_AWAY,
400 // The stream has been cancelled.
401 QUIC_STREAM_CANCELLED,
402 // Closing stream locally, sending a RST to allow for proper flow control
403 // accounting. Sent in response to a RST from the peer.
404 QUIC_RST_ACKNOWLEDGEMENT,
406 // No error. Used as bound while iterating.
407 QUIC_STREAM_LAST_ERROR,
410 // Because receiving an unknown QuicRstStreamErrorCode results in connection
411 // teardown, we use this to make sure any errors predating a given version are
412 // downgraded to the most appropriate existing error.
413 NET_EXPORT_PRIVATE QuicRstStreamErrorCode AdjustErrorForVersion(
414 QuicRstStreamErrorCode error_code,
415 QuicVersion version);
417 // These values must remain stable as they are uploaded to UMA histograms.
418 // To add a new error code, use the current value of QUIC_LAST_ERROR and
419 // increment QUIC_LAST_ERROR.
420 enum QuicErrorCode {
421 QUIC_NO_ERROR = 0,
423 // Connection has reached an invalid state.
424 QUIC_INTERNAL_ERROR = 1,
425 // There were data frames after the a fin or reset.
426 QUIC_STREAM_DATA_AFTER_TERMINATION = 2,
427 // Control frame is malformed.
428 QUIC_INVALID_PACKET_HEADER = 3,
429 // Frame data is malformed.
430 QUIC_INVALID_FRAME_DATA = 4,
431 // The packet contained no payload.
432 QUIC_MISSING_PAYLOAD = 48,
433 // FEC data is malformed.
434 QUIC_INVALID_FEC_DATA = 5,
435 // STREAM frame data is malformed.
436 QUIC_INVALID_STREAM_DATA = 46,
437 // STREAM frame data is not encrypted.
438 QUIC_UNENCRYPTED_STREAM_DATA = 61,
439 // RST_STREAM frame data is malformed.
440 QUIC_INVALID_RST_STREAM_DATA = 6,
441 // CONNECTION_CLOSE frame data is malformed.
442 QUIC_INVALID_CONNECTION_CLOSE_DATA = 7,
443 // GOAWAY frame data is malformed.
444 QUIC_INVALID_GOAWAY_DATA = 8,
445 // WINDOW_UPDATE frame data is malformed.
446 QUIC_INVALID_WINDOW_UPDATE_DATA = 57,
447 // BLOCKED frame data is malformed.
448 QUIC_INVALID_BLOCKED_DATA = 58,
449 // STOP_WAITING frame data is malformed.
450 QUIC_INVALID_STOP_WAITING_DATA = 60,
451 // ACK frame data is malformed.
452 QUIC_INVALID_ACK_DATA = 9,
453 // CONGESTION_FEEDBACK frame data is malformed.
454 QUIC_INVALID_CONGESTION_FEEDBACK_DATA = 47,
455 // Version negotiation packet is malformed.
456 QUIC_INVALID_VERSION_NEGOTIATION_PACKET = 10,
457 // Public RST packet is malformed.
458 QUIC_INVALID_PUBLIC_RST_PACKET = 11,
459 // There was an error decrypting.
460 QUIC_DECRYPTION_FAILURE = 12,
461 // There was an error encrypting.
462 QUIC_ENCRYPTION_FAILURE = 13,
463 // The packet exceeded kMaxPacketSize.
464 QUIC_PACKET_TOO_LARGE = 14,
465 // Data was sent for a stream which did not exist.
466 QUIC_PACKET_FOR_NONEXISTENT_STREAM = 15,
467 // The peer is going away. May be a client or server.
468 QUIC_PEER_GOING_AWAY = 16,
469 // A stream ID was invalid.
470 QUIC_INVALID_STREAM_ID = 17,
471 // A priority was invalid.
472 QUIC_INVALID_PRIORITY = 49,
473 // Too many streams already open.
474 QUIC_TOO_MANY_OPEN_STREAMS = 18,
475 // The peer must send a FIN/RST for each stream, and has not been doing so.
476 QUIC_TOO_MANY_UNFINISHED_STREAMS = 66,
477 // Received public reset for this connection.
478 QUIC_PUBLIC_RESET = 19,
479 // Invalid protocol version.
480 QUIC_INVALID_VERSION = 20,
482 // deprecated: QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED = 21
484 // The Header ID for a stream was too far from the previous.
485 QUIC_INVALID_HEADER_ID = 22,
486 // Negotiable parameter received during handshake had invalid value.
487 QUIC_INVALID_NEGOTIATED_VALUE = 23,
488 // There was an error decompressing data.
489 QUIC_DECOMPRESSION_FAILURE = 24,
490 // We hit our prenegotiated (or default) timeout
491 QUIC_CONNECTION_TIMED_OUT = 25,
492 // We hit our overall connection timeout
493 QUIC_CONNECTION_OVERALL_TIMED_OUT = 67,
494 // There was an error encountered migrating addresses
495 QUIC_ERROR_MIGRATING_ADDRESS = 26,
496 // There was an error while writing to the socket.
497 QUIC_PACKET_WRITE_ERROR = 27,
498 // There was an error while reading from the socket.
499 QUIC_PACKET_READ_ERROR = 51,
500 // We received a STREAM_FRAME with no data and no fin flag set.
501 QUIC_INVALID_STREAM_FRAME = 50,
502 // We received invalid data on the headers stream.
503 QUIC_INVALID_HEADERS_STREAM_DATA = 56,
504 // The peer received too much data, violating flow control.
505 QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA = 59,
506 // The peer sent too much data, violating flow control.
507 QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA = 63,
508 // The peer received an invalid flow control window.
509 QUIC_FLOW_CONTROL_INVALID_WINDOW = 64,
510 // The connection has been IP pooled into an existing connection.
511 QUIC_CONNECTION_IP_POOLED = 62,
512 // The connection has too many outstanding sent packets.
513 QUIC_TOO_MANY_OUTSTANDING_SENT_PACKETS = 68,
514 // The connection has too many outstanding received packets.
515 QUIC_TOO_MANY_OUTSTANDING_RECEIVED_PACKETS = 69,
517 // Crypto errors.
519 // Hanshake failed.
520 QUIC_HANDSHAKE_FAILED = 28,
521 // Handshake message contained out of order tags.
522 QUIC_CRYPTO_TAGS_OUT_OF_ORDER = 29,
523 // Handshake message contained too many entries.
524 QUIC_CRYPTO_TOO_MANY_ENTRIES = 30,
525 // Handshake message contained an invalid value length.
526 QUIC_CRYPTO_INVALID_VALUE_LENGTH = 31,
527 // A crypto message was received after the handshake was complete.
528 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE = 32,
529 // A crypto message was received with an illegal message tag.
530 QUIC_INVALID_CRYPTO_MESSAGE_TYPE = 33,
531 // A crypto message was received with an illegal parameter.
532 QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER = 34,
533 // An invalid channel id signature was supplied.
534 QUIC_INVALID_CHANNEL_ID_SIGNATURE = 52,
535 // A crypto message was received with a mandatory parameter missing.
536 QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND = 35,
537 // A crypto message was received with a parameter that has no overlap
538 // with the local parameter.
539 QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP = 36,
540 // A crypto message was received that contained a parameter with too few
541 // values.
542 QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND = 37,
543 // An internal error occured in crypto processing.
544 QUIC_CRYPTO_INTERNAL_ERROR = 38,
545 // A crypto handshake message specified an unsupported version.
546 QUIC_CRYPTO_VERSION_NOT_SUPPORTED = 39,
547 // There was no intersection between the crypto primitives supported by the
548 // peer and ourselves.
549 QUIC_CRYPTO_NO_SUPPORT = 40,
550 // The server rejected our client hello messages too many times.
551 QUIC_CRYPTO_TOO_MANY_REJECTS = 41,
552 // The client rejected the server's certificate chain or signature.
553 QUIC_PROOF_INVALID = 42,
554 // A crypto message was received with a duplicate tag.
555 QUIC_CRYPTO_DUPLICATE_TAG = 43,
556 // A crypto message was received with the wrong encryption level (i.e. it
557 // should have been encrypted but was not.)
558 QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT = 44,
559 // The server config for a server has expired.
560 QUIC_CRYPTO_SERVER_CONFIG_EXPIRED = 45,
561 // We failed to setup the symmetric keys for a connection.
562 QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED = 53,
563 // A handshake message arrived, but we are still validating the
564 // previous handshake message.
565 QUIC_CRYPTO_MESSAGE_WHILE_VALIDATING_CLIENT_HELLO = 54,
566 // A server config update arrived before the handshake is complete.
567 QUIC_CRYPTO_UPDATE_BEFORE_HANDSHAKE_COMPLETE = 65,
568 // This connection involved a version negotiation which appears to have been
569 // tampered with.
570 QUIC_VERSION_NEGOTIATION_MISMATCH = 55,
572 // No error. Used as bound while iterating.
573 QUIC_LAST_ERROR = 70,
576 struct NET_EXPORT_PRIVATE QuicPacketPublicHeader {
577 QuicPacketPublicHeader();
578 explicit QuicPacketPublicHeader(const QuicPacketPublicHeader& other);
579 ~QuicPacketPublicHeader();
581 // Universal header. All QuicPacket headers will have a connection_id and
582 // public flags.
583 QuicConnectionId connection_id;
584 QuicConnectionIdLength connection_id_length;
585 bool reset_flag;
586 bool version_flag;
587 QuicSequenceNumberLength sequence_number_length;
588 QuicVersionVector versions;
591 // Header for Data or FEC packets.
592 struct NET_EXPORT_PRIVATE QuicPacketHeader {
593 QuicPacketHeader();
594 explicit QuicPacketHeader(const QuicPacketPublicHeader& header);
596 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
597 std::ostream& os, const QuicPacketHeader& s);
599 QuicPacketPublicHeader public_header;
600 bool fec_flag;
601 bool entropy_flag;
602 QuicPacketEntropyHash entropy_hash;
603 QuicPacketSequenceNumber packet_sequence_number;
604 InFecGroup is_in_fec_group;
605 QuicFecGroupNumber fec_group;
608 struct NET_EXPORT_PRIVATE QuicPublicResetPacket {
609 QuicPublicResetPacket();
610 explicit QuicPublicResetPacket(const QuicPacketPublicHeader& header);
612 QuicPacketPublicHeader public_header;
613 QuicPublicResetNonceProof nonce_proof;
614 QuicPacketSequenceNumber rejected_sequence_number;
615 IPEndPoint client_address;
618 enum QuicVersionNegotiationState {
619 START_NEGOTIATION = 0,
620 // Server-side this implies we've sent a version negotiation packet and are
621 // waiting on the client to select a compatible version. Client-side this
622 // implies we've gotten a version negotiation packet, are retransmitting the
623 // initial packets with a supported version and are waiting for our first
624 // packet from the server.
625 NEGOTIATION_IN_PROGRESS,
626 // This indicates this endpoint has received a packet from the peer with a
627 // version this endpoint supports. Version negotiation is complete, and the
628 // version number will no longer be sent with future packets.
629 NEGOTIATED_VERSION
632 typedef QuicPacketPublicHeader QuicVersionNegotiationPacket;
634 // A padding frame contains no payload.
635 struct NET_EXPORT_PRIVATE QuicPaddingFrame {
638 // A ping frame contains no payload, though it is retransmittable,
639 // and ACK'd just like other normal frames.
640 struct NET_EXPORT_PRIVATE QuicPingFrame {
643 struct NET_EXPORT_PRIVATE QuicStreamFrame {
644 QuicStreamFrame();
645 QuicStreamFrame(const QuicStreamFrame& frame);
646 QuicStreamFrame(QuicStreamId stream_id,
647 bool fin,
648 QuicStreamOffset offset,
649 IOVector data);
651 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
652 std::ostream& os, const QuicStreamFrame& s);
654 // Returns a copy of the IOVector |data| as a heap-allocated string.
655 // Caller must take ownership of the returned string.
656 std::string* GetDataAsString() const;
658 QuicStreamId stream_id;
659 bool fin;
660 QuicStreamOffset offset; // Location of this data in the stream.
661 IOVector data;
663 // If this is set, then when this packet is ACKed the AckNotifier will be
664 // informed.
665 QuicAckNotifier* notifier;
668 // TODO(ianswett): Re-evaluate the trade-offs of hash_set vs set when framing
669 // is finalized.
670 typedef std::set<QuicPacketSequenceNumber> SequenceNumberSet;
671 typedef std::list<QuicPacketSequenceNumber> SequenceNumberList;
673 typedef std::list<
674 std::pair<QuicPacketSequenceNumber, QuicTime> > PacketTimeList;
676 struct NET_EXPORT_PRIVATE QuicStopWaitingFrame {
677 QuicStopWaitingFrame();
678 ~QuicStopWaitingFrame();
680 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
681 std::ostream& os, const QuicStopWaitingFrame& s);
682 // Entropy hash of all packets up to, but not including, the least unacked
683 // packet.
684 QuicPacketEntropyHash entropy_hash;
685 // The lowest packet we've sent which is unacked, and we expect an ack for.
686 QuicPacketSequenceNumber least_unacked;
689 struct NET_EXPORT_PRIVATE QuicAckFrame {
690 QuicAckFrame();
691 ~QuicAckFrame();
693 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
694 std::ostream& os, const QuicAckFrame& s);
696 // Entropy hash of all packets up to largest observed not including missing
697 // packets.
698 QuicPacketEntropyHash entropy_hash;
700 // The highest packet sequence number we've observed from the peer.
702 // In general, this should be the largest packet number we've received. In
703 // the case of truncated acks, we may have to advertise a lower "upper bound"
704 // than largest received, to avoid implicitly acking missing packets that
705 // don't fit in the missing packet list due to size limitations. In this
706 // case, largest_observed may be a packet which is also in the missing packets
707 // list.
708 QuicPacketSequenceNumber largest_observed;
710 // Time elapsed since largest_observed was received until this Ack frame was
711 // sent.
712 QuicTime::Delta delta_time_largest_observed;
714 // TODO(satyamshekhar): Can be optimized using an interval set like data
715 // structure.
716 // The set of packets which we're expecting and have not received.
717 SequenceNumberSet missing_packets;
719 // Whether the ack had to be truncated when sent.
720 bool is_truncated;
722 // Packets which have been revived via FEC.
723 // All of these must also be in missing_packets.
724 SequenceNumberSet revived_packets;
726 // List of <sequence_number, time> for when packets arrived.
727 PacketTimeList received_packet_times;
730 // True if the sequence number is greater than largest_observed or is listed
731 // as missing.
732 // Always returns false for sequence numbers less than least_unacked.
733 bool NET_EXPORT_PRIVATE IsAwaitingPacket(
734 const QuicAckFrame& ack_frame,
735 QuicPacketSequenceNumber sequence_number);
737 // Inserts missing packets between [lower, higher).
738 void NET_EXPORT_PRIVATE InsertMissingPacketsBetween(
739 QuicAckFrame* ack_frame,
740 QuicPacketSequenceNumber lower,
741 QuicPacketSequenceNumber higher);
743 // Defines for all types of congestion feedback that will be negotiated in QUIC,
744 // kTCP MUST be supported by all QUIC implementations to guarantee 100%
745 // compatibility.
746 // TODO(cyr): Remove this when removing QUIC_VERSION_22.
747 enum CongestionFeedbackType {
748 kTCP, // Used to mimic TCP.
751 // Defines for all types of congestion control algorithms that can be used in
752 // QUIC. Note that this is separate from the congestion feedback type -
753 // some congestion control algorithms may use the same feedback type
754 // (Reno and Cubic are the classic example for that).
755 enum CongestionControlType {
756 kCubic,
757 kReno,
758 kBBR,
761 enum LossDetectionType {
762 kNack, // Used to mimic TCP's loss detection.
763 kTime, // Time based loss detection.
766 // TODO(cyr): Remove this when removing QUIC_VERSION_22.
767 struct NET_EXPORT_PRIVATE CongestionFeedbackMessageTCP {
768 CongestionFeedbackMessageTCP();
770 QuicByteCount receive_window;
773 // TODO(cyr): Remove this when removing QUIC_VERSION_22.
774 struct NET_EXPORT_PRIVATE QuicCongestionFeedbackFrame {
775 QuicCongestionFeedbackFrame();
776 ~QuicCongestionFeedbackFrame();
778 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
779 std::ostream& os, const QuicCongestionFeedbackFrame& c);
781 CongestionFeedbackType type;
782 // This should really be a union, but since the timestamp struct
783 // is non-trivial, C++ prohibits it.
784 CongestionFeedbackMessageTCP tcp;
787 struct NET_EXPORT_PRIVATE QuicRstStreamFrame {
788 QuicRstStreamFrame();
789 QuicRstStreamFrame(QuicStreamId stream_id,
790 QuicRstStreamErrorCode error_code,
791 QuicStreamOffset bytes_written);
793 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
794 std::ostream& os, const QuicRstStreamFrame& r);
796 QuicStreamId stream_id;
797 QuicRstStreamErrorCode error_code;
798 std::string error_details;
800 // Used to update flow control windows. On termination of a stream, both
801 // endpoints must inform the peer of the number of bytes they have sent on
802 // that stream. This can be done through normal termination (data packet with
803 // FIN) or through a RST.
804 QuicStreamOffset byte_offset;
807 struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame {
808 QuicConnectionCloseFrame();
810 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
811 std::ostream& os, const QuicConnectionCloseFrame& c);
813 QuicErrorCode error_code;
814 std::string error_details;
817 struct NET_EXPORT_PRIVATE QuicGoAwayFrame {
818 QuicGoAwayFrame();
819 QuicGoAwayFrame(QuicErrorCode error_code,
820 QuicStreamId last_good_stream_id,
821 const std::string& reason);
823 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
824 std::ostream& os, const QuicGoAwayFrame& g);
826 QuicErrorCode error_code;
827 QuicStreamId last_good_stream_id;
828 std::string reason_phrase;
831 // Flow control updates per-stream and at the connection levoel.
832 // Based on SPDY's WINDOW_UPDATE frame, but uses an absolute byte offset rather
833 // than a window delta.
834 // TODO(rjshade): A possible future optimization is to make stream_id and
835 // byte_offset variable length, similar to stream frames.
836 struct NET_EXPORT_PRIVATE QuicWindowUpdateFrame {
837 QuicWindowUpdateFrame() {}
838 QuicWindowUpdateFrame(QuicStreamId stream_id, QuicStreamOffset byte_offset);
840 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
841 std::ostream& os, const QuicWindowUpdateFrame& w);
843 // The stream this frame applies to. 0 is a special case meaning the overall
844 // connection rather than a specific stream.
845 QuicStreamId stream_id;
847 // Byte offset in the stream or connection. The receiver of this frame must
848 // not send data which would result in this offset being exceeded.
849 QuicStreamOffset byte_offset;
852 // The BLOCKED frame is used to indicate to the remote endpoint that this
853 // endpoint believes itself to be flow-control blocked but otherwise ready to
854 // send data. The BLOCKED frame is purely advisory and optional.
855 // Based on SPDY's BLOCKED frame (undocumented as of 2014-01-28).
856 struct NET_EXPORT_PRIVATE QuicBlockedFrame {
857 QuicBlockedFrame() {}
858 explicit QuicBlockedFrame(QuicStreamId stream_id);
860 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
861 std::ostream& os, const QuicBlockedFrame& b);
863 // The stream this frame applies to. 0 is a special case meaning the overall
864 // connection rather than a specific stream.
865 QuicStreamId stream_id;
868 // EncryptionLevel enumerates the stages of encryption that a QUIC connection
869 // progresses through. When retransmitting a packet, the encryption level needs
870 // to be specified so that it is retransmitted at a level which the peer can
871 // understand.
872 enum EncryptionLevel {
873 ENCRYPTION_NONE = 0,
874 ENCRYPTION_INITIAL = 1,
875 ENCRYPTION_FORWARD_SECURE = 2,
877 NUM_ENCRYPTION_LEVELS,
880 struct NET_EXPORT_PRIVATE QuicFrame {
881 QuicFrame();
882 explicit QuicFrame(QuicPaddingFrame* padding_frame);
883 explicit QuicFrame(QuicStreamFrame* stream_frame);
884 explicit QuicFrame(QuicAckFrame* frame);
886 // TODO(cyr): Remove this when removing QUIC_VERSION_22.
887 explicit QuicFrame(QuicCongestionFeedbackFrame* frame);
889 explicit QuicFrame(QuicRstStreamFrame* frame);
890 explicit QuicFrame(QuicConnectionCloseFrame* frame);
891 explicit QuicFrame(QuicStopWaitingFrame* frame);
892 explicit QuicFrame(QuicPingFrame* frame);
893 explicit QuicFrame(QuicGoAwayFrame* frame);
894 explicit QuicFrame(QuicWindowUpdateFrame* frame);
895 explicit QuicFrame(QuicBlockedFrame* frame);
897 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
898 std::ostream& os, const QuicFrame& frame);
900 QuicFrameType type;
901 union {
902 QuicPaddingFrame* padding_frame;
903 QuicStreamFrame* stream_frame;
904 QuicAckFrame* ack_frame;
906 // TODO(cyr): Remove this when removing QUIC_VERSION_22.
907 QuicCongestionFeedbackFrame* congestion_feedback_frame;
908 QuicStopWaitingFrame* stop_waiting_frame;
910 QuicPingFrame* ping_frame;
911 QuicRstStreamFrame* rst_stream_frame;
912 QuicConnectionCloseFrame* connection_close_frame;
913 QuicGoAwayFrame* goaway_frame;
914 QuicWindowUpdateFrame* window_update_frame;
915 QuicBlockedFrame* blocked_frame;
919 typedef std::vector<QuicFrame> QuicFrames;
921 struct NET_EXPORT_PRIVATE QuicFecData {
922 QuicFecData();
924 // The FEC group number is also the sequence number of the first
925 // FEC protected packet. The last protected packet's sequence number will
926 // be one less than the sequence number of the FEC packet.
927 QuicFecGroupNumber fec_group;
928 base::StringPiece redundancy;
931 class NET_EXPORT_PRIVATE QuicData {
932 public:
933 QuicData(const char* buffer, size_t length);
934 QuicData(char* buffer, size_t length, bool owns_buffer);
935 virtual ~QuicData();
937 base::StringPiece AsStringPiece() const {
938 return base::StringPiece(data(), length());
941 const char* data() const { return buffer_; }
942 size_t length() const { return length_; }
944 private:
945 const char* buffer_;
946 size_t length_;
947 bool owns_buffer_;
949 DISALLOW_COPY_AND_ASSIGN(QuicData);
952 class NET_EXPORT_PRIVATE QuicPacket : public QuicData {
953 public:
954 static QuicPacket* NewDataPacket(
955 char* buffer,
956 size_t length,
957 bool owns_buffer,
958 QuicConnectionIdLength connection_id_length,
959 bool includes_version,
960 QuicSequenceNumberLength sequence_number_length) {
961 return new QuicPacket(buffer, length, owns_buffer, connection_id_length,
962 includes_version, sequence_number_length, false);
965 static QuicPacket* NewFecPacket(
966 char* buffer,
967 size_t length,
968 bool owns_buffer,
969 QuicConnectionIdLength connection_id_length,
970 bool includes_version,
971 QuicSequenceNumberLength sequence_number_length) {
972 return new QuicPacket(buffer, length, owns_buffer, connection_id_length,
973 includes_version, sequence_number_length, true);
976 base::StringPiece FecProtectedData() const;
977 base::StringPiece AssociatedData() const;
978 base::StringPiece BeforePlaintext() const;
979 base::StringPiece Plaintext() const;
981 bool is_fec_packet() const { return is_fec_packet_; }
983 char* mutable_data() { return buffer_; }
985 private:
986 QuicPacket(char* buffer,
987 size_t length,
988 bool owns_buffer,
989 QuicConnectionIdLength connection_id_length,
990 bool includes_version,
991 QuicSequenceNumberLength sequence_number_length,
992 bool is_fec_packet);
994 char* buffer_;
995 const bool is_fec_packet_;
996 const QuicConnectionIdLength connection_id_length_;
997 const bool includes_version_;
998 const QuicSequenceNumberLength sequence_number_length_;
1000 DISALLOW_COPY_AND_ASSIGN(QuicPacket);
1003 class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
1004 public:
1005 QuicEncryptedPacket(const char* buffer, size_t length);
1006 QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer);
1008 // Clones the packet into a new packet which owns the buffer.
1009 QuicEncryptedPacket* Clone() const;
1011 // By default, gtest prints the raw bytes of an object. The bool data
1012 // member (in the base class QuicData) causes this object to have padding
1013 // bytes, which causes the default gtest object printer to read
1014 // uninitialize memory. So we need to teach gtest how to print this object.
1015 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
1016 std::ostream& os, const QuicEncryptedPacket& s);
1018 private:
1019 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket);
1022 class NET_EXPORT_PRIVATE RetransmittableFrames {
1023 public:
1024 RetransmittableFrames();
1025 ~RetransmittableFrames();
1027 // Allocates a local copy of the referenced StringPiece has QuicStreamFrame
1028 // use it.
1029 // Takes ownership of |stream_frame|.
1030 const QuicFrame& AddStreamFrame(QuicStreamFrame* stream_frame);
1031 // Takes ownership of the frame inside |frame|.
1032 const QuicFrame& AddNonStreamFrame(const QuicFrame& frame);
1033 const QuicFrames& frames() const { return frames_; }
1035 IsHandshake HasCryptoHandshake() const {
1036 return has_crypto_handshake_;
1039 void set_encryption_level(EncryptionLevel level);
1040 EncryptionLevel encryption_level() const {
1041 return encryption_level_;
1044 private:
1045 QuicFrames frames_;
1046 EncryptionLevel encryption_level_;
1047 IsHandshake has_crypto_handshake_;
1048 // Data referenced by the StringPiece of a QuicStreamFrame.
1049 std::vector<std::string*> stream_data_;
1051 DISALLOW_COPY_AND_ASSIGN(RetransmittableFrames);
1054 struct NET_EXPORT_PRIVATE SerializedPacket {
1055 SerializedPacket(QuicPacketSequenceNumber sequence_number,
1056 QuicSequenceNumberLength sequence_number_length,
1057 QuicPacket* packet,
1058 QuicPacketEntropyHash entropy_hash,
1059 RetransmittableFrames* retransmittable_frames);
1060 ~SerializedPacket();
1062 QuicPacketSequenceNumber sequence_number;
1063 QuicSequenceNumberLength sequence_number_length;
1064 QuicPacket* packet;
1065 QuicPacketEntropyHash entropy_hash;
1066 RetransmittableFrames* retransmittable_frames;
1068 // If set, these will be called when this packet is ACKed by the peer.
1069 std::set<QuicAckNotifier*> notifiers;
1072 struct NET_EXPORT_PRIVATE TransmissionInfo {
1073 // Used by STL when assigning into a map.
1074 TransmissionInfo();
1076 // Constructs a Transmission with a new all_tranmissions set
1077 // containing |sequence_number|.
1078 TransmissionInfo(RetransmittableFrames* retransmittable_frames,
1079 QuicSequenceNumberLength sequence_number_length,
1080 TransmissionType transmission_type,
1081 QuicTime sent_time);
1083 RetransmittableFrames* retransmittable_frames;
1084 QuicSequenceNumberLength sequence_number_length;
1085 // Zero when the packet is serialized, non-zero once it's sent.
1086 QuicTime sent_time;
1087 // Zero when the packet is serialized, non-zero once it's sent.
1088 QuicByteCount bytes_sent;
1089 size_t nack_count;
1090 // Reason why this packet was transmitted.
1091 TransmissionType transmission_type;
1092 // Stores the sequence numbers of all transmissions of this packet.
1093 // Must always be nullptr or have multiple elements.
1094 SequenceNumberList* all_transmissions;
1095 // In flight packets have not been abandoned or lost.
1096 bool in_flight;
1097 // True if the packet can never be acked, so it can be removed.
1098 bool is_unackable;
1101 } // namespace net
1103 #endif // NET_QUIC_QUIC_PROTOCOL_H_