1 // Copyright 2014 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.
4
5 #ifndef QUICHE_QUIC_CORE_QUIC_TYPES_H_
6 #define QUICHE_QUIC_CORE_QUIC_TYPES_H_
7
8 #include <array>
9 #include <cstddef>
10 #include <cstdint>
11 #include <map>
12 #include <ostream>
13 #include <vector>
14
15 #include "absl/container/inlined_vector.h"
16 #include "absl/strings/str_format.h"
17 #include "absl/strings/string_view.h"
18 #include "absl/types/optional.h"
19 #include "quiche/quic/core/quic_connection_id.h"
20 #include "quiche/quic/core/quic_error_codes.h"
21 #include "quiche/quic/core/quic_packet_number.h"
22 #include "quiche/quic/core/quic_time.h"
23 #include "quiche/quic/platform/api/quic_export.h"
24 #include "quiche/quic/platform/api/quic_flags.h"
25 #include "quiche/common/quiche_endian.h"
26
27 namespace quic {
28
29 using QuicPacketLength = uint16_t;
30 using QuicControlFrameId = uint32_t;
31 using QuicMessageId = uint32_t;
32
33 // IMPORTANT: IETF QUIC defines stream IDs and stream counts as being unsigned
34 // 62-bit numbers. However, we have decided to only support up to 2^32-1 streams
35 // in order to reduce the size of data structures such as QuicStreamFrame
36 // and QuicTransmissionInfo, as that allows them to fit in cache lines and has
37 // visible perfomance impact.
38 using QuicStreamId = uint32_t;
39
40 // Count of stream IDs. Used in MAX_STREAMS and STREAMS_BLOCKED frames.
41 using QuicStreamCount = QuicStreamId;
42
43 using QuicByteCount = uint64_t;
44 using QuicPacketCount = uint64_t;
45 using QuicPublicResetNonceProof = uint64_t;
46 using QuicStreamOffset = uint64_t;
47 using DiversificationNonce = std::array<char, 32>;
48 using PacketTimeVector = std::vector<std::pair<QuicPacketNumber, QuicTime>>;
49
50 enum : size_t { kStatelessResetTokenLength = 16 };
51 using StatelessResetToken = std::array<char, kStatelessResetTokenLength>;
52
53 // WebTransport session IDs are stream IDs.
54 using WebTransportSessionId = uint64_t;
55 // WebTransport stream reset codes are 8-bit.
56 using WebTransportStreamError = uint8_t;
57 // WebTransport session error codes are 32-bit.
58 using WebTransportSessionError = uint32_t;
59
60 enum : size_t { kQuicPathFrameBufferSize = 8 };
61 using QuicPathFrameBuffer = std::array<uint8_t, kQuicPathFrameBufferSize>;
62
63 // The connection id sequence number specifies the order that connection
64 // ids must be used in. This is also the sequence number carried in
65 // the IETF QUIC NEW_CONNECTION_ID and RETIRE_CONNECTION_ID frames.
66 using QuicConnectionIdSequenceNumber = uint64_t;
67
68 // A custom data that represents application-specific settings.
69 // In HTTP/3 for example, it includes the encoded SETTINGS.
70 using ApplicationState = std::vector<uint8_t>;
71
72 // A struct for functions which consume data payloads and fins.
73 struct QUIC_EXPORT_PRIVATE QuicConsumedData {
QuicConsumedDataQuicConsumedData74 constexpr QuicConsumedData(size_t bytes_consumed, bool fin_consumed)
75 : bytes_consumed(bytes_consumed), fin_consumed(fin_consumed) {}
76
77 // By default, gtest prints the raw bytes of an object. The bool data
78 // member causes this object to have padding bytes, which causes the
79 // default gtest object printer to read uninitialize memory. So we need
80 // to teach gtest how to print this object.
81 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(
82 std::ostream& os, const QuicConsumedData& s);
83
84 // How many bytes were consumed.
85 size_t bytes_consumed;
86
87 // True if an incoming fin was consumed.
88 bool fin_consumed;
89 };
90
91 // QuicAsyncStatus enumerates the possible results of an asynchronous
92 // operation.
93 enum QuicAsyncStatus {
94 QUIC_SUCCESS = 0,
95 QUIC_FAILURE = 1,
96 // QUIC_PENDING results from an operation that will occur asynchronously. When
97 // the operation is complete, a callback's |Run| method will be called.
98 QUIC_PENDING = 2,
99 };
100
101 // TODO(wtc): see if WriteStatus can be replaced by QuicAsyncStatus.
102 enum WriteStatus : int16_t {
103 WRITE_STATUS_OK,
104 // Write is blocked, caller needs to retry.
105 WRITE_STATUS_BLOCKED,
106 // Write is blocked but the packet data is buffered, caller should not retry.
107 WRITE_STATUS_BLOCKED_DATA_BUFFERED,
108 // To make the IsWriteError(WriteStatus) function work properly:
109 // - Non-errors MUST be added before WRITE_STATUS_ERROR.
110 // - Errors MUST be added after WRITE_STATUS_ERROR.
111 WRITE_STATUS_ERROR,
112 WRITE_STATUS_MSG_TOO_BIG,
113 WRITE_STATUS_FAILED_TO_COALESCE_PACKET,
114 WRITE_STATUS_NUM_VALUES,
115 };
116
117 std::string HistogramEnumString(WriteStatus enum_value);
118 QUIC_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
119 const WriteStatus& status);
120
HistogramEnumDescription(WriteStatus)121 inline std::string HistogramEnumDescription(WriteStatus /*dummy*/) {
122 return "status";
123 }
124
IsWriteBlockedStatus(WriteStatus status)125 inline bool IsWriteBlockedStatus(WriteStatus status) {
126 return status == WRITE_STATUS_BLOCKED ||
127 status == WRITE_STATUS_BLOCKED_DATA_BUFFERED;
128 }
129
IsWriteError(WriteStatus status)130 inline bool IsWriteError(WriteStatus status) {
131 return status >= WRITE_STATUS_ERROR;
132 }
133
134 // A struct used to return the result of write calls including either the number
135 // of bytes written or the error code, depending upon the status.
136 struct QUIC_EXPORT_PRIVATE WriteResult {
WriteResultWriteResult137 constexpr WriteResult(WriteStatus status, int bytes_written_or_error_code)
138 : status(status), bytes_written(bytes_written_or_error_code) {}
139
WriteResultWriteResult140 constexpr WriteResult() : WriteResult(WRITE_STATUS_ERROR, 0) {}
141
142 bool operator==(const WriteResult& other) const {
143 if (status != other.status) {
144 return false;
145 }
146 switch (status) {
147 case WRITE_STATUS_OK:
148 return bytes_written == other.bytes_written;
149 case WRITE_STATUS_BLOCKED:
150 case WRITE_STATUS_BLOCKED_DATA_BUFFERED:
151 return true;
152 default:
153 return error_code == other.error_code;
154 }
155 }
156
157 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os,
158 const WriteResult& s);
159
160 WriteStatus status;
161 // Number of packets dropped as a result of this write.
162 // Only used by batch writers. Otherwise always 0.
163 uint16_t dropped_packets = 0;
164 // The delta between a packet's ideal and actual send time:
165 // actual_send_time = ideal_send_time + send_time_offset
166 // = (now + release_time_delay) + send_time_offset
167 // Only valid if |status| is WRITE_STATUS_OK.
168 QuicTime::Delta send_time_offset = QuicTime::Delta::Zero();
169 // TODO(wub): In some cases, WRITE_STATUS_ERROR may set an error_code and
170 // WRITE_STATUS_BLOCKED_DATA_BUFFERED may set bytes_written. This may need
171 // some cleaning up so that perhaps both values can be set and valid.
172 union {
173 int bytes_written; // only valid when status is WRITE_STATUS_OK
174 int error_code; // only valid when status is WRITE_STATUS_ERROR
175 };
176 };
177
178 enum TransmissionType : int8_t {
179 NOT_RETRANSMISSION,
180 FIRST_TRANSMISSION_TYPE = NOT_RETRANSMISSION,
181 HANDSHAKE_RETRANSMISSION, // Retransmits due to handshake timeouts.
182 ALL_ZERO_RTT_RETRANSMISSION, // Retransmits all packets encrypted with 0-RTT
183 // key.
184 LOSS_RETRANSMISSION, // Retransmits due to loss detection.
185 PTO_RETRANSMISSION, // Retransmission due to probe timeout.
186 PATH_RETRANSMISSION, // Retransmission proactively due to underlying
187 // network change.
188 ALL_INITIAL_RETRANSMISSION, // Retransmit all packets encrypted with INITIAL
189 // key.
190 LAST_TRANSMISSION_TYPE = ALL_INITIAL_RETRANSMISSION,
191 };
192
193 QUIC_EXPORT_PRIVATE std::string TransmissionTypeToString(
194 TransmissionType transmission_type);
195
196 QUIC_EXPORT_PRIVATE std::ostream& operator<<(
197 std::ostream& os, TransmissionType transmission_type);
198
199 enum HasRetransmittableData : uint8_t {
200 NO_RETRANSMITTABLE_DATA,
201 HAS_RETRANSMITTABLE_DATA,
202 };
203
204 enum IsHandshake : uint8_t { NOT_HANDSHAKE, IS_HANDSHAKE };
205
206 enum class Perspective : uint8_t { IS_SERVER, IS_CLIENT };
207
208 QUIC_EXPORT_PRIVATE std::string PerspectiveToString(Perspective perspective);
209 QUIC_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
210 const Perspective& perspective);
211
212 // Describes whether a ConnectionClose was originated by the peer.
213 enum class ConnectionCloseSource { FROM_PEER, FROM_SELF };
214
215 QUIC_EXPORT_PRIVATE std::string ConnectionCloseSourceToString(
216 ConnectionCloseSource connection_close_source);
217 QUIC_EXPORT_PRIVATE std::ostream& operator<<(
218 std::ostream& os, const ConnectionCloseSource& connection_close_source);
219
220 // Should a connection be closed silently or not.
221 enum class ConnectionCloseBehavior {
222 SILENT_CLOSE,
223 SILENT_CLOSE_WITH_CONNECTION_CLOSE_PACKET_SERIALIZED,
224 SEND_CONNECTION_CLOSE_PACKET
225 };
226
227 QUIC_EXPORT_PRIVATE std::string ConnectionCloseBehaviorToString(
228 ConnectionCloseBehavior connection_close_behavior);
229 QUIC_EXPORT_PRIVATE std::ostream& operator<<(
230 std::ostream& os, const ConnectionCloseBehavior& connection_close_behavior);
231
232 enum QuicFrameType : uint8_t {
233 // Regular frame types. The values set here cannot change without the
234 // introduction of a new QUIC version.
235 PADDING_FRAME = 0,
236 RST_STREAM_FRAME = 1,
237 CONNECTION_CLOSE_FRAME = 2,
238 GOAWAY_FRAME = 3,
239 WINDOW_UPDATE_FRAME = 4,
240 BLOCKED_FRAME = 5,
241 STOP_WAITING_FRAME = 6,
242 PING_FRAME = 7,
243 CRYPTO_FRAME = 8,
244 // TODO(b/157935330): stop hard coding this when deprecate T050.
245 HANDSHAKE_DONE_FRAME = 9,
246
247 // STREAM and ACK frames are special frames. They are encoded differently on
248 // the wire and their values do not need to be stable.
249 STREAM_FRAME,
250 ACK_FRAME,
251 // The path MTU discovery frame is encoded as a PING frame on the wire.
252 MTU_DISCOVERY_FRAME,
253
254 // These are for IETF-specific frames for which there is no mapping
255 // from Google QUIC frames. These are valid/allowed if and only if IETF-
256 // QUIC has been negotiated. Values are not important, they are not
257 // the values that are in the packets (see QuicIetfFrameType, below).
258 NEW_CONNECTION_ID_FRAME,
259 MAX_STREAMS_FRAME,
260 STREAMS_BLOCKED_FRAME,
261 PATH_RESPONSE_FRAME,
262 PATH_CHALLENGE_FRAME,
263 STOP_SENDING_FRAME,
264 MESSAGE_FRAME,
265 NEW_TOKEN_FRAME,
266 RETIRE_CONNECTION_ID_FRAME,
267 ACK_FREQUENCY_FRAME,
268
269 NUM_FRAME_TYPES
270 };
271
272 // Human-readable string suitable for logging.
273 QUIC_EXPORT_PRIVATE std::string QuicFrameTypeToString(QuicFrameType t);
274 QUIC_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
275 const QuicFrameType& t);
276
277 // Ietf frame types. These are defined in the IETF QUIC Specification.
278 // Explicit values are given in the enum so that we can be sure that
279 // the symbol will map to the correct stream type.
280 // All types are defined here, even if we have not yet implmented the
281 // quic/core/stream/.... stuff needed.
282 // Note: The protocol specifies that frame types are varint-62 encoded,
283 // further stating that the shortest encoding must be used. The current set of
284 // frame types all have values less than 0x40 (64) so can be encoded in a single
285 // byte, with the two most significant bits being 0. Thus, the following
286 // enumerations are valid as both the numeric values of frame types AND their
287 // encodings.
288 enum QuicIetfFrameType : uint64_t {
289 IETF_PADDING = 0x00,
290 IETF_PING = 0x01,
291 IETF_ACK = 0x02,
292 IETF_ACK_ECN = 0x03,
293 IETF_RST_STREAM = 0x04,
294 IETF_STOP_SENDING = 0x05,
295 IETF_CRYPTO = 0x06,
296 IETF_NEW_TOKEN = 0x07,
297 // the low-3 bits of the stream frame type value are actually flags
298 // declaring what parts of the frame are/are-not present, as well as
299 // some other control information. The code would then do something
300 // along the lines of "if ((frame_type & 0xf8) == 0x08)" to determine
301 // whether the frame is a stream frame or not, and then examine each
302 // bit specifically when/as needed.
303 IETF_STREAM = 0x08,
304 // 0x09 through 0x0f are various flag settings of the IETF_STREAM frame.
305 IETF_MAX_DATA = 0x10,
306 IETF_MAX_STREAM_DATA = 0x11,
307 IETF_MAX_STREAMS_BIDIRECTIONAL = 0x12,
308 IETF_MAX_STREAMS_UNIDIRECTIONAL = 0x13,
309 IETF_DATA_BLOCKED = 0x14,
310 IETF_STREAM_DATA_BLOCKED = 0x15,
311 IETF_STREAMS_BLOCKED_BIDIRECTIONAL = 0x16,
312 IETF_STREAMS_BLOCKED_UNIDIRECTIONAL = 0x17,
313 IETF_NEW_CONNECTION_ID = 0x18,
314 IETF_RETIRE_CONNECTION_ID = 0x19,
315 IETF_PATH_CHALLENGE = 0x1a,
316 IETF_PATH_RESPONSE = 0x1b,
317 // Both of the following are "Connection Close" frames,
318 // the first signals transport-layer errors, the second application-layer
319 // errors.
320 IETF_CONNECTION_CLOSE = 0x1c,
321 IETF_APPLICATION_CLOSE = 0x1d,
322
323 IETF_HANDSHAKE_DONE = 0x1e,
324
325 // The MESSAGE frame type has not yet been fully standardized.
326 // QUIC versions starting with 46 and before 99 use 0x20-0x21.
327 // IETF QUIC (v99) uses 0x30-0x31, see draft-pauly-quic-datagram.
328 IETF_EXTENSION_MESSAGE_NO_LENGTH = 0x20,
329 IETF_EXTENSION_MESSAGE = 0x21,
330 IETF_EXTENSION_MESSAGE_NO_LENGTH_V99 = 0x30,
331 IETF_EXTENSION_MESSAGE_V99 = 0x31,
332
333 // An QUIC extension frame for sender control of acknowledgement delays
334 IETF_ACK_FREQUENCY = 0xaf,
335
336 // A QUIC extension frame which augments the IETF_ACK frame definition with
337 // packet receive timestamps.
338 // TODO(ianswett): Determine a proper value to replace this temporary value.
339 IETF_ACK_RECEIVE_TIMESTAMPS = 0x22,
340 };
341 QUIC_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
342 const QuicIetfFrameType& c);
343 QUIC_EXPORT_PRIVATE std::string QuicIetfFrameTypeString(QuicIetfFrameType t);
344
345 // Masks for the bits that indicate the frame is a Stream frame vs the
346 // bits used as flags.
347 #define IETF_STREAM_FRAME_TYPE_MASK 0xfffffffffffffff8
348 #define IETF_STREAM_FRAME_FLAG_MASK 0x07
349 #define IS_IETF_STREAM_FRAME(_stype_) \
350 (((_stype_)&IETF_STREAM_FRAME_TYPE_MASK) == IETF_STREAM)
351
352 // These are the values encoded in the low-order 3 bits of the
353 // IETF_STREAMx frame type.
354 #define IETF_STREAM_FRAME_FIN_BIT 0x01
355 #define IETF_STREAM_FRAME_LEN_BIT 0x02
356 #define IETF_STREAM_FRAME_OFF_BIT 0x04
357
358 enum QuicPacketNumberLength : uint8_t {
359 PACKET_1BYTE_PACKET_NUMBER = 1,
360 PACKET_2BYTE_PACKET_NUMBER = 2,
361 PACKET_3BYTE_PACKET_NUMBER = 3, // Used in versions 45+.
362 PACKET_4BYTE_PACKET_NUMBER = 4,
363 IETF_MAX_PACKET_NUMBER_LENGTH = 4,
364 // TODO(b/145819870): Remove 6 and 8 when we remove Q043 since these values
365 // are not representable with later versions.
366 PACKET_6BYTE_PACKET_NUMBER = 6,
367 PACKET_8BYTE_PACKET_NUMBER = 8
368 };
369
370 // Used to indicate a QuicSequenceNumberLength using two flag bits.
371 enum QuicPacketNumberLengthFlags {
372 PACKET_FLAGS_1BYTE_PACKET = 0, // 00
373 PACKET_FLAGS_2BYTE_PACKET = 1, // 01
374 PACKET_FLAGS_4BYTE_PACKET = 1 << 1, // 10
375 PACKET_FLAGS_8BYTE_PACKET = 1 << 1 | 1, // 11
376 };
377
378 // The public flags are specified in one byte.
379 enum QuicPacketPublicFlags {
380 PACKET_PUBLIC_FLAGS_NONE = 0,
381
382 // Bit 0: Does the packet header contains version info?
383 PACKET_PUBLIC_FLAGS_VERSION = 1 << 0,
384
385 // Bit 1: Is this packet a public reset packet?
386 PACKET_PUBLIC_FLAGS_RST = 1 << 1,
387
388 // Bit 2: indicates the header includes a nonce.
389 PACKET_PUBLIC_FLAGS_NONCE = 1 << 2,
390
391 // Bit 3: indicates whether a ConnectionID is included.
392 PACKET_PUBLIC_FLAGS_0BYTE_CONNECTION_ID = 0,
393 PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID = 1 << 3,
394
395 // Deprecated version 32 and earlier used two bits to indicate an 8-byte
396 // connection ID. We send this from the client because of some broken
397 // middleboxes that are still checking this bit.
398 PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID_OLD = 1 << 3 | 1 << 2,
399
400 // Bits 4 and 5 describe the packet number length as follows:
401 // --00----: 1 byte
402 // --01----: 2 bytes
403 // --10----: 4 bytes
404 // --11----: 6 bytes
405 PACKET_PUBLIC_FLAGS_1BYTE_PACKET = PACKET_FLAGS_1BYTE_PACKET << 4,
406 PACKET_PUBLIC_FLAGS_2BYTE_PACKET = PACKET_FLAGS_2BYTE_PACKET << 4,
407 PACKET_PUBLIC_FLAGS_4BYTE_PACKET = PACKET_FLAGS_4BYTE_PACKET << 4,
408 PACKET_PUBLIC_FLAGS_6BYTE_PACKET = PACKET_FLAGS_8BYTE_PACKET << 4,
409
410 // Reserved, unimplemented flags:
411
412 // Bit 7: indicates the presence of a second flags byte.
413 PACKET_PUBLIC_FLAGS_TWO_OR_MORE_BYTES = 1 << 7,
414
415 // All bits set (bits 6 and 7 are not currently used): 00111111
416 PACKET_PUBLIC_FLAGS_MAX = (1 << 6) - 1,
417 };
418
419 // The private flags are specified in one byte.
420 enum QuicPacketPrivateFlags {
421 PACKET_PRIVATE_FLAGS_NONE = 0,
422
423 // Bit 0: Does this packet contain an entropy bit?
424 PACKET_PRIVATE_FLAGS_ENTROPY = 1 << 0,
425
426 // (bits 1-7 are not used): 00000001
427 PACKET_PRIVATE_FLAGS_MAX = (1 << 1) - 1
428 };
429
430 // Defines for all types of congestion control algorithms that can be used in
431 // QUIC. Note that this is separate from the congestion feedback type -
432 // some congestion control algorithms may use the same feedback type
433 // (Reno and Cubic are the classic example for that).
434 enum CongestionControlType {
435 kCubicBytes,
436 kRenoBytes,
437 kBBR,
438 kPCC,
439 kGoogCC,
440 kBBRv2,
441 };
442
443 QUIC_EXPORT_PRIVATE std::string CongestionControlTypeToString(
444 CongestionControlType cc_type);
445
446 // EncryptionLevel enumerates the stages of encryption that a QUIC connection
447 // progresses through. When retransmitting a packet, the encryption level needs
448 // to be specified so that it is retransmitted at a level which the peer can
449 // understand.
450 enum EncryptionLevel : int8_t {
451 ENCRYPTION_INITIAL = 0,
452 ENCRYPTION_HANDSHAKE = 1,
453 ENCRYPTION_ZERO_RTT = 2,
454 ENCRYPTION_FORWARD_SECURE = 3,
455
456 NUM_ENCRYPTION_LEVELS,
457 };
458
EncryptionLevelIsValid(EncryptionLevel level)459 inline bool EncryptionLevelIsValid(EncryptionLevel level) {
460 return ENCRYPTION_INITIAL <= level && level < NUM_ENCRYPTION_LEVELS;
461 }
462
463 QUIC_EXPORT_PRIVATE std::string EncryptionLevelToString(EncryptionLevel level);
464
465 QUIC_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
466 EncryptionLevel level);
467
468 // Enumeration of whether a server endpoint will request a client certificate,
469 // and whether that endpoint requires a valid client certificate to establish a
470 // connection.
471 enum class ClientCertMode : uint8_t {
472 kNone, // Do not request a client certificate. Default server behavior.
473 kRequest, // Request a certificate, but allow unauthenticated connections.
474 kRequire, // Require clients to provide a valid certificate.
475 };
476
477 QUIC_EXPORT_PRIVATE absl::string_view ClientCertModeToString(
478 ClientCertMode mode);
479
480 QUIC_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
481 ClientCertMode mode);
482
483 enum AddressChangeType : uint8_t {
484 // IP address and port remain unchanged.
485 NO_CHANGE,
486 // Port changed, but IP address remains unchanged.
487 PORT_CHANGE,
488 // IPv4 address changed, but within the /24 subnet (port may have changed.)
489 IPV4_SUBNET_CHANGE,
490 // IPv4 address changed, excluding /24 subnet change (port may have changed.)
491 IPV4_TO_IPV4_CHANGE,
492 // IP address change from an IPv4 to an IPv6 address (port may have changed.)
493 IPV4_TO_IPV6_CHANGE,
494 // IP address change from an IPv6 to an IPv4 address (port may have changed.)
495 IPV6_TO_IPV4_CHANGE,
496 // IP address change from an IPv6 to an IPv6 address (port may have changed.)
497 IPV6_TO_IPV6_CHANGE,
498 };
499
500 QUIC_EXPORT_PRIVATE std::string AddressChangeTypeToString(
501 AddressChangeType type);
502
503 QUIC_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
504 AddressChangeType type);
505
506 enum StreamSendingState {
507 // Sender has more data to send on this stream.
508 NO_FIN,
509 // Sender is done sending on this stream.
510 FIN,
511 // Sender is done sending on this stream and random padding needs to be
512 // appended after all stream frames.
513 FIN_AND_PADDING,
514 };
515
516 enum SentPacketState : uint8_t {
517 // The packet is in flight and waiting to be acked.
518 OUTSTANDING,
519 FIRST_PACKET_STATE = OUTSTANDING,
520 // The packet was never sent.
521 NEVER_SENT,
522 // The packet has been acked.
523 ACKED,
524 // This packet is not expected to be acked.
525 UNACKABLE,
526 // This packet has been delivered or unneeded.
527 NEUTERED,
528
529 // States below are corresponding to retransmission types in TransmissionType.
530
531 // This packet has been retransmitted when retransmission timer fires in
532 // HANDSHAKE mode.
533 HANDSHAKE_RETRANSMITTED,
534 // This packet is considered as lost, this is used for LOST_RETRANSMISSION.
535 LOST,
536 // This packet has been retransmitted when PTO fires.
537 PTO_RETRANSMITTED,
538 // This packet is sent on a different path or is a PING only packet.
539 // Do not update RTT stats and congestion control if the packet is the
540 // largest_acked of an incoming ACK.
541 NOT_CONTRIBUTING_RTT,
542 LAST_PACKET_STATE = NOT_CONTRIBUTING_RTT,
543 };
544
545 enum PacketHeaderFormat : uint8_t {
546 IETF_QUIC_LONG_HEADER_PACKET,
547 IETF_QUIC_SHORT_HEADER_PACKET,
548 GOOGLE_QUIC_PACKET,
549 };
550
551 QUIC_EXPORT_PRIVATE std::string PacketHeaderFormatToString(
552 PacketHeaderFormat format);
553
554 // Information about a newly acknowledged packet.
555 struct QUIC_EXPORT_PRIVATE AckedPacket {
AckedPacketAckedPacket556 constexpr AckedPacket(QuicPacketNumber packet_number,
557 QuicPacketLength bytes_acked,
558 QuicTime receive_timestamp)
559 : packet_number(packet_number),
560 bytes_acked(bytes_acked),
561 receive_timestamp(receive_timestamp) {}
562
563 friend QUIC_EXPORT_PRIVATE std::ostream& operator<<(
564 std::ostream& os, const AckedPacket& acked_packet);
565
566 QuicPacketNumber packet_number;
567 // Number of bytes sent in the packet that was acknowledged.
568 QuicPacketLength bytes_acked;
569 // The time |packet_number| was received by the peer, according to the
570 // optional timestamp the peer included in the ACK frame which acknowledged
571 // |packet_number|. Zero if no timestamp was available for this packet.
572 QuicTime receive_timestamp;
573 };
574
575 // A vector of acked packets.
576 using AckedPacketVector = absl::InlinedVector<AckedPacket, 2>;
577
578 // Information about a newly lost packet.
579 struct QUIC_EXPORT_PRIVATE LostPacket {
LostPacketLostPacket580 LostPacket(QuicPacketNumber packet_number, QuicPacketLength bytes_lost)
581 : packet_number(packet_number), bytes_lost(bytes_lost) {}
582
583 friend QUIC_EXPORT_PRIVATE std::ostream& operator<<(
584 std::ostream& os, const LostPacket& lost_packet);
585
586 QuicPacketNumber packet_number;
587 // Number of bytes sent in the packet that was lost.
588 QuicPacketLength bytes_lost;
589 };
590
591 // A vector of lost packets.
592 using LostPacketVector = absl::InlinedVector<LostPacket, 2>;
593
594 // Please note, this value cannot used directly for packet serialization.
595 enum QuicLongHeaderType : uint8_t {
596 VERSION_NEGOTIATION,
597 INITIAL,
598 ZERO_RTT_PROTECTED,
599 HANDSHAKE,
600 RETRY,
601
602 INVALID_PACKET_TYPE,
603 };
604
605 QUIC_EXPORT_PRIVATE std::string QuicLongHeaderTypeToString(
606 QuicLongHeaderType type);
607
608 enum QuicPacketHeaderTypeFlags : uint8_t {
609 // Bit 2: Key phase bit for IETF QUIC short header packets.
610 FLAGS_KEY_PHASE_BIT = 1 << 2,
611 // Bit 3: Google QUIC Demultiplexing bit, the short header always sets this
612 // bit to 0, allowing to distinguish Google QUIC packets from short header
613 // packets.
614 FLAGS_DEMULTIPLEXING_BIT = 1 << 3,
615 // Bits 4 and 5: Reserved bits for short header.
616 FLAGS_SHORT_HEADER_RESERVED_1 = 1 << 4,
617 FLAGS_SHORT_HEADER_RESERVED_2 = 1 << 5,
618 // Bit 6: the 'QUIC' bit.
619 FLAGS_FIXED_BIT = 1 << 6,
620 // Bit 7: Indicates the header is long or short header.
621 FLAGS_LONG_HEADER = 1 << 7,
622 };
623
624 enum MessageStatus {
625 MESSAGE_STATUS_SUCCESS,
626 MESSAGE_STATUS_ENCRYPTION_NOT_ESTABLISHED, // Failed to send message because
627 // encryption is not established
628 // yet.
629 MESSAGE_STATUS_UNSUPPORTED, // Failed to send message because MESSAGE frame
630 // is not supported by the connection.
631 MESSAGE_STATUS_BLOCKED, // Failed to send message because connection is
632 // congestion control blocked or underlying socket is
633 // write blocked.
634 MESSAGE_STATUS_TOO_LARGE, // Failed to send message because the message is
635 // too large to fit into a single packet.
636 MESSAGE_STATUS_INTERNAL_ERROR, // Failed to send message because connection
637 // reaches an invalid state.
638 };
639
640 QUIC_EXPORT_PRIVATE std::string MessageStatusToString(
641 MessageStatus message_status);
642
643 // Used to return the result of SendMessage calls
644 struct QUIC_EXPORT_PRIVATE MessageResult {
645 MessageResult(MessageStatus status, QuicMessageId message_id);
646
647 bool operator==(const MessageResult& other) const {
648 return status == other.status && message_id == other.message_id;
649 }
650
651 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os,
652 const MessageResult& mr);
653
654 MessageStatus status;
655 // Only valid when status is MESSAGE_STATUS_SUCCESS.
656 QuicMessageId message_id;
657 };
658
659 QUIC_EXPORT_PRIVATE std::string MessageResultToString(
660 MessageResult message_result);
661
662 enum WriteStreamDataResult {
663 WRITE_SUCCESS,
664 STREAM_MISSING, // Trying to write data of a nonexistent stream (e.g.
665 // closed).
666 WRITE_FAILED, // Trying to write nonexistent data of a stream
667 };
668
669 enum StreamType : uint8_t {
670 // Bidirectional streams allow for data to be sent in both directions.
671 BIDIRECTIONAL,
672
673 // Unidirectional streams carry data in one direction only.
674 WRITE_UNIDIRECTIONAL,
675 READ_UNIDIRECTIONAL,
676 // Not actually a stream type. Used only by QuicCryptoStream when it uses
677 // CRYPTO frames and isn't actually a QuicStream.
678 CRYPTO,
679 };
680
681 // A packet number space is the context in which a packet can be processed and
682 // acknowledged.
683 enum PacketNumberSpace : uint8_t {
684 INITIAL_DATA = 0, // Only used in IETF QUIC.
685 HANDSHAKE_DATA = 1,
686 APPLICATION_DATA = 2,
687
688 NUM_PACKET_NUMBER_SPACES,
689 };
690
691 QUIC_EXPORT_PRIVATE std::string PacketNumberSpaceToString(
692 PacketNumberSpace packet_number_space);
693
694 // Used to return the result of processing a received ACK frame.
695 enum AckResult {
696 PACKETS_NEWLY_ACKED,
697 NO_PACKETS_NEWLY_ACKED,
698 UNSENT_PACKETS_ACKED, // Peer acks unsent packets.
699 UNACKABLE_PACKETS_ACKED, // Peer acks packets that are not expected to be
700 // acked. For example, encryption is reestablished,
701 // and all sent encrypted packets cannot be
702 // decrypted by the peer. Version gets negotiated,
703 // and all sent packets in the different version
704 // cannot be processed by the peer.
705 PACKETS_ACKED_IN_WRONG_PACKET_NUMBER_SPACE,
706 };
707
708 // Indicates the fate of a serialized packet in WritePacket().
709 enum SerializedPacketFate : uint8_t {
710 DISCARD, // Discard the packet.
711 COALESCE, // Try to coalesce packet.
712 BUFFER, // Buffer packet in buffered_packets_.
713 SEND_TO_WRITER, // Send packet to writer.
714 };
715
716 QUIC_EXPORT_PRIVATE std::string SerializedPacketFateToString(
717 SerializedPacketFate fate);
718
719 QUIC_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
720 const SerializedPacketFate fate);
721
722 // There are three different forms of CONNECTION_CLOSE.
723 enum QuicConnectionCloseType {
724 GOOGLE_QUIC_CONNECTION_CLOSE = 0,
725 IETF_QUIC_TRANSPORT_CONNECTION_CLOSE = 1,
726 IETF_QUIC_APPLICATION_CONNECTION_CLOSE = 2
727 };
728
729 QUIC_EXPORT_PRIVATE std::ostream& operator<<(
730 std::ostream& os, const QuicConnectionCloseType type);
731
732 QUIC_EXPORT_PRIVATE std::string QuicConnectionCloseTypeString(
733 QuicConnectionCloseType type);
734
735 // Indicate handshake state of a connection.
736 enum HandshakeState {
737 // Initial state.
738 HANDSHAKE_START,
739 // Only used in IETF QUIC with TLS handshake. State proceeds to
740 // HANDSHAKE_PROCESSED after a packet of HANDSHAKE packet number space
741 // gets successfully processed, and the initial key can be dropped.
742 HANDSHAKE_PROCESSED,
743 // In QUIC crypto, state proceeds to HANDSHAKE_COMPLETE if client receives
744 // SHLO or server successfully processes an ENCRYPTION_FORWARD_SECURE
745 // packet, such that the handshake packets can be neutered. In IETF QUIC
746 // with TLS handshake, state proceeds to HANDSHAKE_COMPLETE once the client
747 // has both 1-RTT send and receive keys.
748 HANDSHAKE_COMPLETE,
749 // Only used in IETF QUIC with TLS handshake. State proceeds to
750 // HANDSHAKE_CONFIRMED if 1) a client receives HANDSHAKE_DONE frame or
751 // acknowledgment for 1-RTT packet or 2) server has
752 // 1-RTT send and receive keys.
753 HANDSHAKE_CONFIRMED,
754 };
755
756 struct QUIC_NO_EXPORT NextReleaseTimeResult {
757 // The ideal release time of the packet being sent.
758 QuicTime release_time;
759 // Whether it is allowed to send the packet before release_time.
760 bool allow_burst;
761 };
762
763 // QuicPacketBuffer bundles a buffer and a function that releases it. Note
764 // it does not assume ownership of buffer, i.e. it doesn't release the buffer on
765 // destruction.
766 struct QUIC_NO_EXPORT QuicPacketBuffer {
767 QuicPacketBuffer() = default;
768
QuicPacketBufferQuicPacketBuffer769 QuicPacketBuffer(char* buffer,
770 std::function<void(const char*)> release_buffer)
771 : buffer(buffer), release_buffer(std::move(release_buffer)) {}
772
773 char* buffer = nullptr;
774 std::function<void(const char*)> release_buffer;
775 };
776
777 // QuicOwnedPacketBuffer is a QuicPacketBuffer that assumes buffer ownership.
778 struct QUIC_NO_EXPORT QuicOwnedPacketBuffer : public QuicPacketBuffer {
779 QuicOwnedPacketBuffer(const QuicOwnedPacketBuffer&) = delete;
780 QuicOwnedPacketBuffer& operator=(const QuicOwnedPacketBuffer&) = delete;
781
QuicOwnedPacketBufferQuicOwnedPacketBuffer782 QuicOwnedPacketBuffer(char* buffer,
783 std::function<void(const char*)> release_buffer)
784 : QuicPacketBuffer(buffer, std::move(release_buffer)) {}
785
QuicOwnedPacketBufferQuicOwnedPacketBuffer786 QuicOwnedPacketBuffer(QuicOwnedPacketBuffer&& owned_buffer)
787 : QuicPacketBuffer(std::move(owned_buffer)) {
788 // |owned_buffer| does not own a buffer any more.
789 owned_buffer.buffer = nullptr;
790 }
791
QuicOwnedPacketBufferQuicOwnedPacketBuffer792 explicit QuicOwnedPacketBuffer(QuicPacketBuffer&& packet_buffer)
793 : QuicPacketBuffer(std::move(packet_buffer)) {}
794
~QuicOwnedPacketBufferQuicOwnedPacketBuffer795 ~QuicOwnedPacketBuffer() {
796 if (release_buffer != nullptr && buffer != nullptr) {
797 release_buffer(buffer);
798 }
799 }
800 };
801
802 // These values must remain stable as they are uploaded to UMA histograms.
803 enum class KeyUpdateReason {
804 kInvalid = 0,
805 kRemote = 1,
806 kLocalForTests = 2,
807 kLocalForInteropRunner = 3,
808 kLocalAeadConfidentialityLimit = 4,
809 kLocalKeyUpdateLimitOverride = 5,
810 kMaxValue = kLocalKeyUpdateLimitOverride,
811 };
812
813 QUIC_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
814 const KeyUpdateReason reason);
815
816 QUIC_EXPORT_PRIVATE std::string KeyUpdateReasonString(KeyUpdateReason reason);
817
818 using QuicSignatureAlgorithmVector = absl::InlinedVector<uint16_t, 8>;
819
820 // QuicSSLConfig contains configurations to be applied on a SSL object, which
821 // overrides the configurations in SSL_CTX.
822 struct QUIC_NO_EXPORT QuicSSLConfig {
823 // Whether TLS early data should be enabled. If not set, default to enabled.
824 absl::optional<bool> early_data_enabled;
825 // Whether TLS session tickets are supported. If not set, default to
826 // supported.
827 absl::optional<bool> disable_ticket_support;
828 // If set, used to configure the SSL object with
829 // SSL_set_signing_algorithm_prefs.
830 absl::optional<QuicSignatureAlgorithmVector> signing_algorithm_prefs;
831 // Client certificate mode for mTLS support. Only used at server side.
832 ClientCertMode client_cert_mode = ClientCertMode::kNone;
833 // As a client, the ECHConfigList to use with ECH. If empty, ECH is not
834 // offered.
835 std::string ech_config_list;
836 // As a client, whether ECH GREASE is enabled. If `ech_config_list` is
837 // not empty, this value does nothing.
838 bool ech_grease_enabled = false;
839 };
840
841 // QuicDelayedSSLConfig contains a subset of SSL config that can be applied
842 // after BoringSSL's early select certificate callback. This overwrites all SSL
843 // configs applied before cert selection.
844 struct QUIC_NO_EXPORT QuicDelayedSSLConfig {
845 // Client certificate mode for mTLS support. Only used at server side.
846 // absl::nullopt means do not change client certificate mode.
847 absl::optional<ClientCertMode> client_cert_mode;
848 // QUIC transport parameters as serialized by ProofSourceHandle.
849 absl::optional<std::vector<uint8_t>> quic_transport_parameters;
850 };
851
852 // ParsedClientHello contains client hello information extracted from a fully
853 // received client hello.
854 struct QUIC_NO_EXPORT ParsedClientHello {
855 std::string sni; // QUIC crypto and TLS.
856 std::string uaid; // QUIC crypto only.
857 std::vector<std::string> alpns; // QUIC crypto and TLS.
858 // The unvalidated retry token from the last received packet of a potentially
859 // multi-packet client hello. TLS only.
860 std::string retry_token;
861 bool resumption_attempted = false; // TLS only.
862 bool early_data_attempted = false; // TLS only.
863 };
864
865 QUIC_EXPORT_PRIVATE bool operator==(const ParsedClientHello& a,
866 const ParsedClientHello& b);
867
868 QUIC_EXPORT_PRIVATE std::ostream& operator<<(
869 std::ostream& os, const ParsedClientHello& parsed_chlo);
870
871 // The two bits in the IP header for Explicit Congestion Notification can take
872 // one of four values.
873 enum QuicEcnCodepoint {
874 // The NOT-ECT codepoint, indicating the packet sender is not using (or the
875 // network has disabled) ECN.
876 ECN_NOT_ECT = 0,
877 // The ECT(0) codepoint, indicating the packet sender is using classic ECN
878 // (RFC3168).
879 ECN_ECT0 = 1,
880 // The ECT(1) codepoint, indicating the packet sender is using Low Latency,
881 // Low Loss, Scalable Throughput (L4S) ECN (RFC9330).
882 ECN_ECT1 = 2,
883 // The CE ("Congestion Experienced") codepoint, indicating the packet sender
884 // is using ECN, and a router is experiencing congestion.
885 ECN_CE = 3,
886 };
887
888 QUICHE_EXPORT std::string EcnCodepointToString(QuicEcnCodepoint ecn);
889
890 // This struct reports the Explicit Congestion Notification (ECN) contents of
891 // the ACK_ECN frame. They are the cumulative number of QUIC packets received
892 // for that codepoint in a given Packet Number Space.
893 struct QUIC_EXPORT_PRIVATE QuicEcnCounts {
894 QuicEcnCounts() = default;
QuicEcnCountsQuicEcnCounts895 QuicEcnCounts(QuicPacketCount ect0, QuicPacketCount ect1, QuicPacketCount ce)
896 : ect0(ect0), ect1(ect1), ce(ce) {}
897
ToStringQuicEcnCounts898 std::string ToString() const {
899 return absl::StrFormat("ECT(0): %s, ECT(1): %s, CE: %s",
900 std::to_string(ect0), std::to_string(ect1),
901 std::to_string(ce));
902 }
903
904 bool operator==(const QuicEcnCounts& other) const {
905 return (this->ect0 == other.ect0 && this->ect1 == other.ect1 &&
906 this->ce == other.ce);
907 }
908
909 QuicPacketCount ect0 = 0;
910 QuicPacketCount ect1 = 0;
911 QuicPacketCount ce = 0;
912 };
913
914 // Type of the priorities used by a QUIC session.
915 enum class QuicPriorityType : uint8_t {
916 // HTTP priorities as defined by RFC 9218
917 kHttp,
918 // WebTransport priorities as defined by <https://w3c.github.io/webtransport/>
919 kWebTransport,
920 };
921
922 QUICHE_EXPORT std::string QuicPriorityTypeToString(QuicPriorityType type);
923 QUIC_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
924 QuicPriorityType type);
925
926 } // namespace quic
927
928 #endif // QUICHE_QUIC_CORE_QUIC_TYPES_H_
929