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