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