Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / quic / quic_protocol.h
blob0d9fb84ba6f3dd69ccdba86e0008dd7981e4b460
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 size_t kDefaultInitialWindow = 10;
64 const uint32 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 // Size of the socket receive buffer in bytes.
74 const QuicByteCount kDefaultSocketReceiveBuffer = 256 * 1024;
76 // Don't allow a client to suggest an RTT longer than 15 seconds.
77 const uint32 kMaxInitialRoundTripTimeUs = 15 * kNumMicrosPerSecond;
79 // Maximum number of open streams per connection.
80 const size_t kDefaultMaxStreamsPerConnection = 100;
82 // Number of bytes reserved for public flags in the packet header.
83 const size_t kPublicFlagsSize = 1;
84 // Number of bytes reserved for version number in the packet header.
85 const size_t kQuicVersionSize = 4;
86 // Number of bytes reserved for private flags in the packet header.
87 const size_t kPrivateFlagsSize = 1;
88 // Number of bytes reserved for FEC group in the packet header.
89 const size_t kFecGroupSize = 1;
91 // Signifies that the QuicPacket will contain version of the protocol.
92 const bool kIncludeVersion = true;
94 // Index of the first byte in a QUIC packet which is used in hash calculation.
95 const size_t kStartOfHashData = 0;
97 // Limit on the delta between stream IDs.
98 const QuicStreamId kMaxStreamIdDelta = 200;
99 // Limit on the delta between header IDs.
100 const QuicHeaderId kMaxHeaderIdDelta = 200;
102 // Reserved ID for the crypto stream.
103 const QuicStreamId kCryptoStreamId = 1;
105 // Reserved ID for the headers stream.
106 const QuicStreamId kHeadersStreamId = 3;
108 // Maximum delayed ack time, in ms.
109 const int kMaxDelayedAckTimeMs = 25;
111 // This is the default network timeout a for connection till the crypto
112 // handshake succeeds and the negotiated timeout from the handshake is received.
113 const int64 kDefaultInitialTimeoutSecs = 120; // 2 mins.
114 const int64 kDefaultTimeoutSecs = 60 * 10; // 10 minutes.
115 const int64 kDefaultMaxTimeForCryptoHandshakeSecs = 5; // 5 secs.
117 // Default ping timeout.
118 const int64 kPingTimeoutSecs = 15; // 15 secs.
120 // Minimum number of RTTs between Server Config Updates (SCUP) sent to client.
121 const int kMinIntervalBetweenServerConfigUpdatesRTTs = 10;
123 // Minimum time between Server Config Updates (SCUP) sent to client.
124 const int kMinIntervalBetweenServerConfigUpdatesMs = 1000;
126 // We define an unsigned 16-bit floating point value, inspired by IEEE floats
127 // (http://en.wikipedia.org/wiki/Half_precision_floating-point_format),
128 // with 5-bit exponent (bias 1), 11-bit mantissa (effective 12 with hidden
129 // bit) and denormals, but without signs, transfinites or fractions. Wire format
130 // 16 bits (little-endian byte order) are split into exponent (high 5) and
131 // mantissa (low 11) and decoded as:
132 // uint64 value;
133 // if (exponent == 0) value = mantissa;
134 // else value = (mantissa | 1 << 11) << (exponent - 1)
135 const int kUFloat16ExponentBits = 5;
136 const int kUFloat16MaxExponent = (1 << kUFloat16ExponentBits) - 2; // 30
137 const int kUFloat16MantissaBits = 16 - kUFloat16ExponentBits; // 11
138 const int kUFloat16MantissaEffectiveBits = kUFloat16MantissaBits + 1; // 12
139 const uint64 kUFloat16MaxValue = // 0x3FFC0000000
140 ((GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits) - 1) <<
141 kUFloat16MaxExponent;
143 enum TransmissionType {
144 NOT_RETRANSMISSION,
145 FIRST_TRANSMISSION_TYPE = NOT_RETRANSMISSION,
146 HANDSHAKE_RETRANSMISSION, // Retransmits due to handshake timeouts.
147 ALL_UNACKED_RETRANSMISSION, // Retransmits of all unacked packets.
148 LOSS_RETRANSMISSION, // Retransmits due to loss detection.
149 RTO_RETRANSMISSION, // Retransmits due to retransmit time out.
150 TLP_RETRANSMISSION, // Tail loss probes.
151 LAST_TRANSMISSION_TYPE = TLP_RETRANSMISSION,
154 enum RetransmissionType {
155 INITIAL_ENCRYPTION_ONLY,
156 ALL_PACKETS
159 enum HasRetransmittableData {
160 NO_RETRANSMITTABLE_DATA,
161 HAS_RETRANSMITTABLE_DATA,
164 enum IsHandshake {
165 NOT_HANDSHAKE,
166 IS_HANDSHAKE
169 // Indicates FEC protection level for data being written.
170 enum FecProtection {
171 MUST_FEC_PROTECT, // Callee must FEC protect this data.
172 MAY_FEC_PROTECT // Callee does not have to but may FEC protect this data.
175 // Indicates FEC policy.
176 enum FecPolicy {
177 FEC_PROTECT_ALWAYS, // All data in the stream should be FEC protected.
178 FEC_PROTECT_OPTIONAL // Data in the stream does not need FEC protection.
181 enum QuicFrameType {
182 // Regular frame types. The values set here cannot change without the
183 // introduction of a new QUIC version.
184 PADDING_FRAME = 0,
185 RST_STREAM_FRAME = 1,
186 CONNECTION_CLOSE_FRAME = 2,
187 GOAWAY_FRAME = 3,
188 WINDOW_UPDATE_FRAME = 4,
189 BLOCKED_FRAME = 5,
190 STOP_WAITING_FRAME = 6,
191 PING_FRAME = 7,
193 // STREAM, ACK, and CONGESTION_FEEDBACK frames are special frames. They are
194 // encoded differently on the wire and their values do not need to be stable.
195 STREAM_FRAME,
196 ACK_FRAME,
197 CONGESTION_FEEDBACK_FRAME,
198 NUM_FRAME_TYPES
201 enum QuicConnectionIdLength {
202 PACKET_0BYTE_CONNECTION_ID = 0,
203 PACKET_1BYTE_CONNECTION_ID = 1,
204 PACKET_4BYTE_CONNECTION_ID = 4,
205 PACKET_8BYTE_CONNECTION_ID = 8
208 enum InFecGroup {
209 NOT_IN_FEC_GROUP,
210 IN_FEC_GROUP,
213 enum QuicSequenceNumberLength {
214 PACKET_1BYTE_SEQUENCE_NUMBER = 1,
215 PACKET_2BYTE_SEQUENCE_NUMBER = 2,
216 PACKET_4BYTE_SEQUENCE_NUMBER = 4,
217 PACKET_6BYTE_SEQUENCE_NUMBER = 6
220 // Used to indicate a QuicSequenceNumberLength using two flag bits.
221 enum QuicSequenceNumberLengthFlags {
222 PACKET_FLAGS_1BYTE_SEQUENCE = 0, // 00
223 PACKET_FLAGS_2BYTE_SEQUENCE = 1, // 01
224 PACKET_FLAGS_4BYTE_SEQUENCE = 1 << 1, // 10
225 PACKET_FLAGS_6BYTE_SEQUENCE = 1 << 1 | 1, // 11
228 // The public flags are specified in one byte.
229 enum QuicPacketPublicFlags {
230 PACKET_PUBLIC_FLAGS_NONE = 0,
232 // Bit 0: Does the packet header contains version info?
233 PACKET_PUBLIC_FLAGS_VERSION = 1 << 0,
235 // Bit 1: Is this packet a public reset packet?
236 PACKET_PUBLIC_FLAGS_RST = 1 << 1,
238 // Bits 2 and 3 specify the length of the ConnectionId as follows:
239 // ----00--: 0 bytes
240 // ----01--: 1 byte
241 // ----10--: 4 bytes
242 // ----11--: 8 bytes
243 PACKET_PUBLIC_FLAGS_0BYTE_CONNECTION_ID = 0,
244 PACKET_PUBLIC_FLAGS_1BYTE_CONNECTION_ID = 1 << 2,
245 PACKET_PUBLIC_FLAGS_4BYTE_CONNECTION_ID = 1 << 3,
246 PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID = 1 << 3 | 1 << 2,
248 // Bits 4 and 5 describe the packet sequence number length as follows:
249 // --00----: 1 byte
250 // --01----: 2 bytes
251 // --10----: 4 bytes
252 // --11----: 6 bytes
253 PACKET_PUBLIC_FLAGS_1BYTE_SEQUENCE = PACKET_FLAGS_1BYTE_SEQUENCE << 4,
254 PACKET_PUBLIC_FLAGS_2BYTE_SEQUENCE = PACKET_FLAGS_2BYTE_SEQUENCE << 4,
255 PACKET_PUBLIC_FLAGS_4BYTE_SEQUENCE = PACKET_FLAGS_4BYTE_SEQUENCE << 4,
256 PACKET_PUBLIC_FLAGS_6BYTE_SEQUENCE = PACKET_FLAGS_6BYTE_SEQUENCE << 4,
258 // All bits set (bits 6 and 7 are not currently used): 00111111
259 PACKET_PUBLIC_FLAGS_MAX = (1 << 6) - 1
262 // The private flags are specified in one byte.
263 enum QuicPacketPrivateFlags {
264 PACKET_PRIVATE_FLAGS_NONE = 0,
266 // Bit 0: Does this packet contain an entropy bit?
267 PACKET_PRIVATE_FLAGS_ENTROPY = 1 << 0,
269 // Bit 1: Payload is part of an FEC group?
270 PACKET_PRIVATE_FLAGS_FEC_GROUP = 1 << 1,
272 // Bit 2: Payload is FEC as opposed to frames?
273 PACKET_PRIVATE_FLAGS_FEC = 1 << 2,
275 // All bits set (bits 3-7 are not currently used): 00000111
276 PACKET_PRIVATE_FLAGS_MAX = (1 << 3) - 1
279 // The available versions of QUIC. Guaranteed that the integer value of the enum
280 // will match the version number.
281 // When adding a new version to this enum you should add it to
282 // kSupportedQuicVersions (if appropriate), and also add a new case to the
283 // helper methods QuicVersionToQuicTag, QuicTagToQuicVersion, and
284 // QuicVersionToString.
285 enum QuicVersion {
286 // Special case to indicate unknown/unsupported QUIC version.
287 QUIC_VERSION_UNSUPPORTED = 0,
289 QUIC_VERSION_16 = 16, // STOP_WAITING frame.
290 QUIC_VERSION_18 = 18, // PING frame.
291 QUIC_VERSION_19 = 19, // Connection level flow control.
292 QUIC_VERSION_20 = 20, // Independent stream/connection flow control windows.
293 QUIC_VERSION_21 = 21, // Headers/crypto streams are flow controlled.
294 QUIC_VERSION_22 = 22, // Send Server Config Update messages on crypto stream.
295 QUIC_VERSION_23 = 23, // Timestamp in the ack frame.
298 // This vector contains QUIC versions which we currently support.
299 // This should be ordered such that the highest supported version is the first
300 // element, with subsequent elements in descending order (versions can be
301 // skipped as necessary).
303 // IMPORTANT: if you are adding to this list, follow the instructions at
304 // http://sites/quic/adding-and-removing-versions
305 static const QuicVersion kSupportedQuicVersions[] = {QUIC_VERSION_23,
306 QUIC_VERSION_22,
307 QUIC_VERSION_21,
308 QUIC_VERSION_20,
309 QUIC_VERSION_19,
310 QUIC_VERSION_18,
311 QUIC_VERSION_16};
313 typedef std::vector<QuicVersion> QuicVersionVector;
315 // Returns a vector of QUIC versions in kSupportedQuicVersions.
316 NET_EXPORT_PRIVATE QuicVersionVector QuicSupportedVersions();
318 // QuicTag is written to and read from the wire, but we prefer to use
319 // the more readable QuicVersion at other levels.
320 // Helper function which translates from a QuicVersion to a QuicTag. Returns 0
321 // if QuicVersion is unsupported.
322 NET_EXPORT_PRIVATE QuicTag QuicVersionToQuicTag(const QuicVersion version);
324 // Returns appropriate QuicVersion from a QuicTag.
325 // Returns QUIC_VERSION_UNSUPPORTED if version_tag cannot be understood.
326 NET_EXPORT_PRIVATE QuicVersion QuicTagToQuicVersion(const QuicTag version_tag);
328 // Helper function which translates from a QuicVersion to a string.
329 // Returns strings corresponding to enum names (e.g. QUIC_VERSION_6).
330 NET_EXPORT_PRIVATE std::string QuicVersionToString(const QuicVersion version);
332 // Returns comma separated list of string representations of QuicVersion enum
333 // values in the supplied |versions| vector.
334 NET_EXPORT_PRIVATE std::string QuicVersionVectorToString(
335 const QuicVersionVector& versions);
337 // Version and Crypto tags are written to the wire with a big-endian
338 // representation of the name of the tag. For example
339 // the client hello tag (CHLO) will be written as the
340 // following 4 bytes: 'C' 'H' 'L' 'O'. Since it is
341 // stored in memory as a little endian uint32, we need
342 // to reverse the order of the bytes.
344 // MakeQuicTag returns a value given the four bytes. For example:
345 // MakeQuicTag('C', 'H', 'L', 'O');
346 NET_EXPORT_PRIVATE QuicTag MakeQuicTag(char a, char b, char c, char d);
348 // Returns true if the tag vector contains the specified tag.
349 NET_EXPORT_PRIVATE bool ContainsQuicTag(const QuicTagVector& tag_vector,
350 QuicTag tag);
352 // Size in bytes of the data or fec packet header.
353 NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(const QuicPacketHeader& header);
355 NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(
356 QuicConnectionIdLength connection_id_length,
357 bool include_version,
358 QuicSequenceNumberLength sequence_number_length,
359 InFecGroup is_in_fec_group);
361 // Index of the first byte in a QUIC packet of FEC protected data.
362 NET_EXPORT_PRIVATE size_t GetStartOfFecProtectedData(
363 QuicConnectionIdLength connection_id_length,
364 bool include_version,
365 QuicSequenceNumberLength sequence_number_length);
366 // Index of the first byte in a QUIC packet of encrypted data.
367 NET_EXPORT_PRIVATE size_t GetStartOfEncryptedData(
368 QuicConnectionIdLength connection_id_length,
369 bool include_version,
370 QuicSequenceNumberLength sequence_number_length);
372 enum QuicRstStreamErrorCode {
373 QUIC_STREAM_NO_ERROR = 0,
375 // There was some error which halted stream processing.
376 QUIC_ERROR_PROCESSING_STREAM,
377 // We got two fin or reset offsets which did not match.
378 QUIC_MULTIPLE_TERMINATION_OFFSETS,
379 // We got bad payload and can not respond to it at the protocol level.
380 QUIC_BAD_APPLICATION_PAYLOAD,
381 // Stream closed due to connection error. No reset frame is sent when this
382 // happens.
383 QUIC_STREAM_CONNECTION_ERROR,
384 // GoAway frame sent. No more stream can be created.
385 QUIC_STREAM_PEER_GOING_AWAY,
386 // The stream has been cancelled.
387 QUIC_STREAM_CANCELLED,
388 // Sending a RST to allow for proper flow control accounting.
389 QUIC_RST_FLOW_CONTROL_ACCOUNTING,
391 // No error. Used as bound while iterating.
392 QUIC_STREAM_LAST_ERROR,
395 // Because receiving an unknown QuicRstStreamErrorCode results in connection
396 // teardown, we use this to make sure any errors predating a given version are
397 // downgraded to the most appropriate existing error.
398 NET_EXPORT_PRIVATE QuicRstStreamErrorCode AdjustErrorForVersion(
399 QuicRstStreamErrorCode error_code,
400 QuicVersion version);
402 // These values must remain stable as they are uploaded to UMA histograms.
403 // To add a new error code, use the current value of QUIC_LAST_ERROR and
404 // increment QUIC_LAST_ERROR.
405 enum QuicErrorCode {
406 QUIC_NO_ERROR = 0,
408 // Connection has reached an invalid state.
409 QUIC_INTERNAL_ERROR = 1,
410 // There were data frames after the a fin or reset.
411 QUIC_STREAM_DATA_AFTER_TERMINATION = 2,
412 // Control frame is malformed.
413 QUIC_INVALID_PACKET_HEADER = 3,
414 // Frame data is malformed.
415 QUIC_INVALID_FRAME_DATA = 4,
416 // The packet contained no payload.
417 QUIC_MISSING_PAYLOAD = 48,
418 // FEC data is malformed.
419 QUIC_INVALID_FEC_DATA = 5,
420 // STREAM frame data is malformed.
421 QUIC_INVALID_STREAM_DATA = 46,
422 // STREAM frame data is not encrypted.
423 QUIC_UNENCRYPTED_STREAM_DATA = 61,
424 // RST_STREAM frame data is malformed.
425 QUIC_INVALID_RST_STREAM_DATA = 6,
426 // CONNECTION_CLOSE frame data is malformed.
427 QUIC_INVALID_CONNECTION_CLOSE_DATA = 7,
428 // GOAWAY frame data is malformed.
429 QUIC_INVALID_GOAWAY_DATA = 8,
430 // WINDOW_UPDATE frame data is malformed.
431 QUIC_INVALID_WINDOW_UPDATE_DATA = 57,
432 // BLOCKED frame data is malformed.
433 QUIC_INVALID_BLOCKED_DATA = 58,
434 // STOP_WAITING frame data is malformed.
435 QUIC_INVALID_STOP_WAITING_DATA = 60,
436 // ACK frame data is malformed.
437 QUIC_INVALID_ACK_DATA = 9,
438 // CONGESTION_FEEDBACK frame data is malformed.
439 QUIC_INVALID_CONGESTION_FEEDBACK_DATA = 47,
440 // Version negotiation packet is malformed.
441 QUIC_INVALID_VERSION_NEGOTIATION_PACKET = 10,
442 // Public RST packet is malformed.
443 QUIC_INVALID_PUBLIC_RST_PACKET = 11,
444 // There was an error decrypting.
445 QUIC_DECRYPTION_FAILURE = 12,
446 // There was an error encrypting.
447 QUIC_ENCRYPTION_FAILURE = 13,
448 // The packet exceeded kMaxPacketSize.
449 QUIC_PACKET_TOO_LARGE = 14,
450 // Data was sent for a stream which did not exist.
451 QUIC_PACKET_FOR_NONEXISTENT_STREAM = 15,
452 // The peer is going away. May be a client or server.
453 QUIC_PEER_GOING_AWAY = 16,
454 // A stream ID was invalid.
455 QUIC_INVALID_STREAM_ID = 17,
456 // A priority was invalid.
457 QUIC_INVALID_PRIORITY = 49,
458 // Too many streams already open.
459 QUIC_TOO_MANY_OPEN_STREAMS = 18,
460 // The peer must send a FIN/RST for each stream, and has not been doing so.
461 QUIC_TOO_MANY_UNFINISHED_STREAMS = 66,
462 // Received public reset for this connection.
463 QUIC_PUBLIC_RESET = 19,
464 // Invalid protocol version.
465 QUIC_INVALID_VERSION = 20,
467 // deprecated: QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED = 21
469 // The Header ID for a stream was too far from the previous.
470 QUIC_INVALID_HEADER_ID = 22,
471 // Negotiable parameter received during handshake had invalid value.
472 QUIC_INVALID_NEGOTIATED_VALUE = 23,
473 // There was an error decompressing data.
474 QUIC_DECOMPRESSION_FAILURE = 24,
475 // We hit our prenegotiated (or default) timeout
476 QUIC_CONNECTION_TIMED_OUT = 25,
477 // There was an error encountered migrating addresses
478 QUIC_ERROR_MIGRATING_ADDRESS = 26,
479 // There was an error while writing to the socket.
480 QUIC_PACKET_WRITE_ERROR = 27,
481 // There was an error while reading from the socket.
482 QUIC_PACKET_READ_ERROR = 51,
483 // We received a STREAM_FRAME with no data and no fin flag set.
484 QUIC_INVALID_STREAM_FRAME = 50,
485 // We received invalid data on the headers stream.
486 QUIC_INVALID_HEADERS_STREAM_DATA = 56,
487 // The peer received too much data, violating flow control.
488 QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA = 59,
489 // The peer sent too much data, violating flow control.
490 QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA = 63,
491 // The peer received an invalid flow control window.
492 QUIC_FLOW_CONTROL_INVALID_WINDOW = 64,
493 // The connection has been IP pooled into an existing connection.
494 QUIC_CONNECTION_IP_POOLED = 62,
496 // Crypto errors.
498 // Hanshake failed.
499 QUIC_HANDSHAKE_FAILED = 28,
500 // Handshake message contained out of order tags.
501 QUIC_CRYPTO_TAGS_OUT_OF_ORDER = 29,
502 // Handshake message contained too many entries.
503 QUIC_CRYPTO_TOO_MANY_ENTRIES = 30,
504 // Handshake message contained an invalid value length.
505 QUIC_CRYPTO_INVALID_VALUE_LENGTH = 31,
506 // A crypto message was received after the handshake was complete.
507 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE = 32,
508 // A crypto message was received with an illegal message tag.
509 QUIC_INVALID_CRYPTO_MESSAGE_TYPE = 33,
510 // A crypto message was received with an illegal parameter.
511 QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER = 34,
512 // An invalid channel id signature was supplied.
513 QUIC_INVALID_CHANNEL_ID_SIGNATURE = 52,
514 // A crypto message was received with a mandatory parameter missing.
515 QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND = 35,
516 // A crypto message was received with a parameter that has no overlap
517 // with the local parameter.
518 QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP = 36,
519 // A crypto message was received that contained a parameter with too few
520 // values.
521 QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND = 37,
522 // An internal error occured in crypto processing.
523 QUIC_CRYPTO_INTERNAL_ERROR = 38,
524 // A crypto handshake message specified an unsupported version.
525 QUIC_CRYPTO_VERSION_NOT_SUPPORTED = 39,
526 // There was no intersection between the crypto primitives supported by the
527 // peer and ourselves.
528 QUIC_CRYPTO_NO_SUPPORT = 40,
529 // The server rejected our client hello messages too many times.
530 QUIC_CRYPTO_TOO_MANY_REJECTS = 41,
531 // The client rejected the server's certificate chain or signature.
532 QUIC_PROOF_INVALID = 42,
533 // A crypto message was received with a duplicate tag.
534 QUIC_CRYPTO_DUPLICATE_TAG = 43,
535 // A crypto message was received with the wrong encryption level (i.e. it
536 // should have been encrypted but was not.)
537 QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT = 44,
538 // The server config for a server has expired.
539 QUIC_CRYPTO_SERVER_CONFIG_EXPIRED = 45,
540 // We failed to setup the symmetric keys for a connection.
541 QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED = 53,
542 // A handshake message arrived, but we are still validating the
543 // previous handshake message.
544 QUIC_CRYPTO_MESSAGE_WHILE_VALIDATING_CLIENT_HELLO = 54,
545 // A server config update arrived before the handshake is complete.
546 QUIC_CRYPTO_UPDATE_BEFORE_HANDSHAKE_COMPLETE = 65,
547 // This connection involved a version negotiation which appears to have been
548 // tampered with.
549 QUIC_VERSION_NEGOTIATION_MISMATCH = 55,
551 // No error. Used as bound while iterating.
552 QUIC_LAST_ERROR = 67,
555 struct NET_EXPORT_PRIVATE QuicPacketPublicHeader {
556 QuicPacketPublicHeader();
557 explicit QuicPacketPublicHeader(const QuicPacketPublicHeader& other);
558 ~QuicPacketPublicHeader();
560 // Universal header. All QuicPacket headers will have a connection_id and
561 // public flags.
562 QuicConnectionId connection_id;
563 QuicConnectionIdLength connection_id_length;
564 bool reset_flag;
565 bool version_flag;
566 QuicSequenceNumberLength sequence_number_length;
567 QuicVersionVector versions;
570 // Header for Data or FEC packets.
571 struct NET_EXPORT_PRIVATE QuicPacketHeader {
572 QuicPacketHeader();
573 explicit QuicPacketHeader(const QuicPacketPublicHeader& header);
575 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
576 std::ostream& os, const QuicPacketHeader& s);
578 QuicPacketPublicHeader public_header;
579 bool fec_flag;
580 bool entropy_flag;
581 QuicPacketEntropyHash entropy_hash;
582 QuicPacketSequenceNumber packet_sequence_number;
583 InFecGroup is_in_fec_group;
584 QuicFecGroupNumber fec_group;
587 struct NET_EXPORT_PRIVATE QuicPublicResetPacket {
588 QuicPublicResetPacket();
589 explicit QuicPublicResetPacket(const QuicPacketPublicHeader& header);
591 QuicPacketPublicHeader public_header;
592 QuicPublicResetNonceProof nonce_proof;
593 QuicPacketSequenceNumber rejected_sequence_number;
594 IPEndPoint client_address;
597 enum QuicVersionNegotiationState {
598 START_NEGOTIATION = 0,
599 // Server-side this implies we've sent a version negotiation packet and are
600 // waiting on the client to select a compatible version. Client-side this
601 // implies we've gotten a version negotiation packet, are retransmitting the
602 // initial packets with a supported version and are waiting for our first
603 // packet from the server.
604 NEGOTIATION_IN_PROGRESS,
605 // This indicates this endpoint has received a packet from the peer with a
606 // version this endpoint supports. Version negotiation is complete, and the
607 // version number will no longer be sent with future packets.
608 NEGOTIATED_VERSION
611 typedef QuicPacketPublicHeader QuicVersionNegotiationPacket;
613 // A padding frame contains no payload.
614 struct NET_EXPORT_PRIVATE QuicPaddingFrame {
617 // A ping frame contains no payload, though it is retransmittable,
618 // and ACK'd just like other normal frames.
619 struct NET_EXPORT_PRIVATE QuicPingFrame {
622 struct NET_EXPORT_PRIVATE QuicStreamFrame {
623 QuicStreamFrame();
624 QuicStreamFrame(const QuicStreamFrame& frame);
625 QuicStreamFrame(QuicStreamId stream_id,
626 bool fin,
627 QuicStreamOffset offset,
628 IOVector data);
630 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
631 std::ostream& os, const QuicStreamFrame& s);
633 // Returns a copy of the IOVector |data| as a heap-allocated string.
634 // Caller must take ownership of the returned string.
635 std::string* GetDataAsString() const;
637 QuicStreamId stream_id;
638 bool fin;
639 QuicStreamOffset offset; // Location of this data in the stream.
640 IOVector data;
642 // If this is set, then when this packet is ACKed the AckNotifier will be
643 // informed.
644 QuicAckNotifier* notifier;
647 // TODO(ianswett): Re-evaluate the trade-offs of hash_set vs set when framing
648 // is finalized.
649 typedef std::set<QuicPacketSequenceNumber> SequenceNumberSet;
651 typedef std::list<std::pair<QuicPacketSequenceNumber, QuicTime>> PacketTimeList;
653 struct NET_EXPORT_PRIVATE QuicStopWaitingFrame {
654 QuicStopWaitingFrame();
655 ~QuicStopWaitingFrame();
657 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
658 std::ostream& os, const QuicStopWaitingFrame& s);
659 // Entropy hash of all packets up to, but not including, the least unacked
660 // packet.
661 QuicPacketEntropyHash entropy_hash;
662 // The lowest packet we've sent which is unacked, and we expect an ack for.
663 QuicPacketSequenceNumber least_unacked;
666 struct NET_EXPORT_PRIVATE QuicAckFrame {
667 QuicAckFrame();
668 ~QuicAckFrame();
670 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
671 std::ostream& os, const QuicAckFrame& s);
673 // Entropy hash of all packets up to largest observed not including missing
674 // packets.
675 QuicPacketEntropyHash entropy_hash;
677 // The highest packet sequence number we've observed from the peer.
679 // In general, this should be the largest packet number we've received. In
680 // the case of truncated acks, we may have to advertise a lower "upper bound"
681 // than largest received, to avoid implicitly acking missing packets that
682 // don't fit in the missing packet list due to size limitations. In this
683 // case, largest_observed may be a packet which is also in the missing packets
684 // list.
685 QuicPacketSequenceNumber largest_observed;
687 // Time elapsed since largest_observed was received until this Ack frame was
688 // sent.
689 QuicTime::Delta delta_time_largest_observed;
691 // TODO(satyamshekhar): Can be optimized using an interval set like data
692 // structure.
693 // The set of packets which we're expecting and have not received.
694 SequenceNumberSet missing_packets;
696 // Whether the ack had to be truncated when sent.
697 bool is_truncated;
699 // Packets which have been revived via FEC.
700 // All of these must also be in missing_packets.
701 SequenceNumberSet revived_packets;
703 // List of <sequence_number, time> for when packets arrived.
704 PacketTimeList received_packet_times;
707 // True if the sequence number is greater than largest_observed or is listed
708 // as missing.
709 // Always returns false for sequence numbers less than least_unacked.
710 bool NET_EXPORT_PRIVATE IsAwaitingPacket(
711 const QuicAckFrame& ack_frame,
712 QuicPacketSequenceNumber sequence_number);
714 // Inserts missing packets between [lower, higher).
715 void NET_EXPORT_PRIVATE InsertMissingPacketsBetween(
716 QuicAckFrame* ack_frame,
717 QuicPacketSequenceNumber lower,
718 QuicPacketSequenceNumber higher);
720 // Defines for all types of congestion feedback that will be negotiated in QUIC,
721 // kTCP MUST be supported by all QUIC implementations to guarantee 100%
722 // compatibility.
723 // TODO(cyr): Remove this when removing QUIC_VERSION_22.
724 enum CongestionFeedbackType {
725 kTCP, // Used to mimic TCP.
728 // Defines for all types of congestion control algorithms that can be used in
729 // QUIC. Note that this is separate from the congestion feedback type -
730 // some congestion control algorithms may use the same feedback type
731 // (Reno and Cubic are the classic example for that).
732 enum CongestionControlType {
733 kCubic,
734 kReno,
735 kBBR,
738 enum LossDetectionType {
739 kNack, // Used to mimic TCP's loss detection.
740 kTime, // Time based loss detection.
743 // TODO(cyr): Remove this when removing QUIC_VERSION_22.
744 struct NET_EXPORT_PRIVATE CongestionFeedbackMessageTCP {
745 CongestionFeedbackMessageTCP();
747 QuicByteCount receive_window;
750 // TODO(cyr): Remove this when removing QUIC_VERSION_22.
751 struct NET_EXPORT_PRIVATE QuicCongestionFeedbackFrame {
752 QuicCongestionFeedbackFrame();
753 ~QuicCongestionFeedbackFrame();
755 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
756 std::ostream& os, const QuicCongestionFeedbackFrame& c);
758 CongestionFeedbackType type;
759 // This should really be a union, but since the timestamp struct
760 // is non-trivial, C++ prohibits it.
761 CongestionFeedbackMessageTCP tcp;
764 struct NET_EXPORT_PRIVATE QuicRstStreamFrame {
765 QuicRstStreamFrame();
766 QuicRstStreamFrame(QuicStreamId stream_id,
767 QuicRstStreamErrorCode error_code,
768 QuicStreamOffset bytes_written);
770 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
771 std::ostream& os, const QuicRstStreamFrame& r);
773 QuicStreamId stream_id;
774 QuicRstStreamErrorCode error_code;
775 std::string error_details;
777 // Used to update flow control windows. On termination of a stream, both
778 // endpoints must inform the peer of the number of bytes they have sent on
779 // that stream. This can be done through normal termination (data packet with
780 // FIN) or through a RST.
781 QuicStreamOffset byte_offset;
784 struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame {
785 QuicConnectionCloseFrame();
787 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
788 std::ostream& os, const QuicConnectionCloseFrame& c);
790 QuicErrorCode error_code;
791 std::string error_details;
794 struct NET_EXPORT_PRIVATE QuicGoAwayFrame {
795 QuicGoAwayFrame();
796 QuicGoAwayFrame(QuicErrorCode error_code,
797 QuicStreamId last_good_stream_id,
798 const std::string& reason);
800 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
801 std::ostream& os, const QuicGoAwayFrame& g);
803 QuicErrorCode error_code;
804 QuicStreamId last_good_stream_id;
805 std::string reason_phrase;
808 // Flow control updates per-stream and at the connection levoel.
809 // Based on SPDY's WINDOW_UPDATE frame, but uses an absolute byte offset rather
810 // than a window delta.
811 // TODO(rjshade): A possible future optimization is to make stream_id and
812 // byte_offset variable length, similar to stream frames.
813 struct NET_EXPORT_PRIVATE QuicWindowUpdateFrame {
814 QuicWindowUpdateFrame() {}
815 QuicWindowUpdateFrame(QuicStreamId stream_id, QuicStreamOffset byte_offset);
817 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
818 std::ostream& os, const QuicWindowUpdateFrame& w);
820 // The stream this frame applies to. 0 is a special case meaning the overall
821 // connection rather than a specific stream.
822 QuicStreamId stream_id;
824 // Byte offset in the stream or connection. The receiver of this frame must
825 // not send data which would result in this offset being exceeded.
826 QuicStreamOffset byte_offset;
829 // The BLOCKED frame is used to indicate to the remote endpoint that this
830 // endpoint believes itself to be flow-control blocked but otherwise ready to
831 // send data. The BLOCKED frame is purely advisory and optional.
832 // Based on SPDY's BLOCKED frame (undocumented as of 2014-01-28).
833 struct NET_EXPORT_PRIVATE QuicBlockedFrame {
834 QuicBlockedFrame() {}
835 explicit QuicBlockedFrame(QuicStreamId stream_id);
837 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
838 std::ostream& os, const QuicBlockedFrame& b);
840 // The stream this frame applies to. 0 is a special case meaning the overall
841 // connection rather than a specific stream.
842 QuicStreamId stream_id;
845 // EncryptionLevel enumerates the stages of encryption that a QUIC connection
846 // progresses through. When retransmitting a packet, the encryption level needs
847 // to be specified so that it is retransmitted at a level which the peer can
848 // understand.
849 enum EncryptionLevel {
850 ENCRYPTION_NONE = 0,
851 ENCRYPTION_INITIAL = 1,
852 ENCRYPTION_FORWARD_SECURE = 2,
854 NUM_ENCRYPTION_LEVELS,
857 struct NET_EXPORT_PRIVATE QuicFrame {
858 QuicFrame();
859 explicit QuicFrame(QuicPaddingFrame* padding_frame);
860 explicit QuicFrame(QuicStreamFrame* stream_frame);
861 explicit QuicFrame(QuicAckFrame* frame);
863 // TODO(cyr): Remove this when removing QUIC_VERSION_22.
864 explicit QuicFrame(QuicCongestionFeedbackFrame* frame);
866 explicit QuicFrame(QuicRstStreamFrame* frame);
867 explicit QuicFrame(QuicConnectionCloseFrame* frame);
868 explicit QuicFrame(QuicStopWaitingFrame* frame);
869 explicit QuicFrame(QuicPingFrame* frame);
870 explicit QuicFrame(QuicGoAwayFrame* frame);
871 explicit QuicFrame(QuicWindowUpdateFrame* frame);
872 explicit QuicFrame(QuicBlockedFrame* frame);
874 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
875 std::ostream& os, const QuicFrame& frame);
877 QuicFrameType type;
878 union {
879 QuicPaddingFrame* padding_frame;
880 QuicStreamFrame* stream_frame;
881 QuicAckFrame* ack_frame;
883 // TODO(cyr): Remove this when removing QUIC_VERSION_22.
884 QuicCongestionFeedbackFrame* congestion_feedback_frame;
885 QuicStopWaitingFrame* stop_waiting_frame;
887 QuicPingFrame* ping_frame;
888 QuicRstStreamFrame* rst_stream_frame;
889 QuicConnectionCloseFrame* connection_close_frame;
890 QuicGoAwayFrame* goaway_frame;
891 QuicWindowUpdateFrame* window_update_frame;
892 QuicBlockedFrame* blocked_frame;
896 typedef std::vector<QuicFrame> QuicFrames;
898 struct NET_EXPORT_PRIVATE QuicFecData {
899 QuicFecData();
901 // The FEC group number is also the sequence number of the first
902 // FEC protected packet. The last protected packet's sequence number will
903 // be one less than the sequence number of the FEC packet.
904 QuicFecGroupNumber fec_group;
905 base::StringPiece redundancy;
908 class NET_EXPORT_PRIVATE QuicData {
909 public:
910 QuicData(const char* buffer, size_t length);
911 QuicData(char* buffer, size_t length, bool owns_buffer);
912 virtual ~QuicData();
914 base::StringPiece AsStringPiece() const {
915 return base::StringPiece(data(), length());
918 const char* data() const { return buffer_; }
919 size_t length() const { return length_; }
921 private:
922 const char* buffer_;
923 size_t length_;
924 bool owns_buffer_;
926 DISALLOW_COPY_AND_ASSIGN(QuicData);
929 class NET_EXPORT_PRIVATE QuicPacket : public QuicData {
930 public:
931 static QuicPacket* NewDataPacket(
932 char* buffer,
933 size_t length,
934 bool owns_buffer,
935 QuicConnectionIdLength connection_id_length,
936 bool includes_version,
937 QuicSequenceNumberLength sequence_number_length) {
938 return new QuicPacket(buffer, length, owns_buffer, connection_id_length,
939 includes_version, sequence_number_length, false);
942 static QuicPacket* NewFecPacket(
943 char* buffer,
944 size_t length,
945 bool owns_buffer,
946 QuicConnectionIdLength connection_id_length,
947 bool includes_version,
948 QuicSequenceNumberLength sequence_number_length) {
949 return new QuicPacket(buffer, length, owns_buffer, connection_id_length,
950 includes_version, sequence_number_length, true);
953 base::StringPiece FecProtectedData() const;
954 base::StringPiece AssociatedData() const;
955 base::StringPiece BeforePlaintext() const;
956 base::StringPiece Plaintext() const;
958 bool is_fec_packet() const { return is_fec_packet_; }
960 char* mutable_data() { return buffer_; }
962 private:
963 QuicPacket(char* buffer,
964 size_t length,
965 bool owns_buffer,
966 QuicConnectionIdLength connection_id_length,
967 bool includes_version,
968 QuicSequenceNumberLength sequence_number_length,
969 bool is_fec_packet);
971 char* buffer_;
972 const bool is_fec_packet_;
973 const QuicConnectionIdLength connection_id_length_;
974 const bool includes_version_;
975 const QuicSequenceNumberLength sequence_number_length_;
977 DISALLOW_COPY_AND_ASSIGN(QuicPacket);
980 class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
981 public:
982 QuicEncryptedPacket(const char* buffer, size_t length);
983 QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer);
985 // Clones the packet into a new packet which owns the buffer.
986 QuicEncryptedPacket* Clone() const;
988 // By default, gtest prints the raw bytes of an object. The bool data
989 // member (in the base class QuicData) causes this object to have padding
990 // bytes, which causes the default gtest object printer to read
991 // uninitialize memory. So we need to teach gtest how to print this object.
992 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
993 std::ostream& os, const QuicEncryptedPacket& s);
995 private:
996 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket);
999 class NET_EXPORT_PRIVATE RetransmittableFrames {
1000 public:
1001 RetransmittableFrames();
1002 ~RetransmittableFrames();
1004 // Allocates a local copy of the referenced StringPiece has QuicStreamFrame
1005 // use it.
1006 // Takes ownership of |stream_frame|.
1007 const QuicFrame& AddStreamFrame(QuicStreamFrame* stream_frame);
1008 // Takes ownership of the frame inside |frame|.
1009 const QuicFrame& AddNonStreamFrame(const QuicFrame& frame);
1010 const QuicFrames& frames() const { return frames_; }
1012 IsHandshake HasCryptoHandshake() const {
1013 return has_crypto_handshake_;
1016 void set_encryption_level(EncryptionLevel level);
1017 EncryptionLevel encryption_level() const {
1018 return encryption_level_;
1021 private:
1022 QuicFrames frames_;
1023 EncryptionLevel encryption_level_;
1024 IsHandshake has_crypto_handshake_;
1025 // Data referenced by the StringPiece of a QuicStreamFrame.
1026 std::vector<std::string*> stream_data_;
1028 DISALLOW_COPY_AND_ASSIGN(RetransmittableFrames);
1031 struct NET_EXPORT_PRIVATE SerializedPacket {
1032 SerializedPacket(QuicPacketSequenceNumber sequence_number,
1033 QuicSequenceNumberLength sequence_number_length,
1034 QuicPacket* packet,
1035 QuicPacketEntropyHash entropy_hash,
1036 RetransmittableFrames* retransmittable_frames);
1037 ~SerializedPacket();
1039 QuicPacketSequenceNumber sequence_number;
1040 QuicSequenceNumberLength sequence_number_length;
1041 QuicPacket* packet;
1042 QuicPacketEntropyHash entropy_hash;
1043 RetransmittableFrames* retransmittable_frames;
1045 // If set, these will be called when this packet is ACKed by the peer.
1046 std::set<QuicAckNotifier*> notifiers;
1049 struct NET_EXPORT_PRIVATE TransmissionInfo {
1050 // Used by STL when assigning into a map.
1051 TransmissionInfo();
1053 // Constructs a Transmission with a new all_tranmissions set
1054 // containing |sequence_number|.
1055 TransmissionInfo(RetransmittableFrames* retransmittable_frames,
1056 QuicPacketSequenceNumber sequence_number,
1057 QuicSequenceNumberLength sequence_number_length);
1059 // Constructs a Transmission with the specified |all_tranmissions| set
1060 // and inserts |sequence_number| into it.
1061 TransmissionInfo(RetransmittableFrames* retransmittable_frames,
1062 QuicPacketSequenceNumber sequence_number,
1063 QuicSequenceNumberLength sequence_number_length,
1064 TransmissionType transmission_type,
1065 SequenceNumberSet* all_transmissions);
1067 RetransmittableFrames* retransmittable_frames;
1068 QuicSequenceNumberLength sequence_number_length;
1069 // Zero when the packet is serialized, non-zero once it's sent.
1070 QuicTime sent_time;
1071 // Zero when the packet is serialized, non-zero once it's sent.
1072 QuicByteCount bytes_sent;
1073 size_t nack_count;
1074 // Reason why this packet was transmitted.
1075 TransmissionType transmission_type;
1076 // Stores the sequence numbers of all transmissions of this packet.
1077 // Can never be null.
1078 SequenceNumberSet* all_transmissions;
1079 // In flight packets have not been abandoned or lost.
1080 bool in_flight;
1083 } // namespace net
1085 #endif // NET_QUIC_QUIC_PROTOCOL_H_