1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  *
7  */
8 
9 #pragma once
10 
11 #include <folly/Range.h>
12 #include <folly/String.h>
13 #include <chrono>
14 #include <cstdint>
15 #include <ostream>
16 
17 namespace quic {
18 
19 using Clock = std::chrono::steady_clock;
20 using TimePoint = std::chrono::time_point<Clock>;
21 using DurationRep = std::chrono::microseconds::rep;
22 using namespace std::chrono_literals;
23 
24 // Default QUIC packet size for both read and write.
25 // TODO(xtt): make them configurable
26 constexpr uint64_t kDefaultV4UDPSendPacketLen = 1252;
27 constexpr uint64_t kDefaultV6UDPSendPacketLen = 1232;
28 // With Android NDK r15c for some apps we use gnu-libstdc++ instead of
29 // llvm-libc++. And gnu-libstdc++ doesn't like to make std::min constexpr.
30 constexpr uint16_t kDefaultUDPSendPacketLen =
31     (kDefaultV4UDPSendPacketLen < kDefaultV6UDPSendPacketLen
32          ? kDefaultV4UDPSendPacketLen
33          : kDefaultV6UDPSendPacketLen);
34 // The max we will tolerate a peer's max_packet_size to be.
35 constexpr uint16_t kDefaultMaxUDPPayload = 1452;
36 
37 // This is the minimum the max_packet_size transport parameter is allowed to be,
38 // per the spec. Note this actually refers to the max UDP payload size, not the
39 // maximum QUIC packet size.
40 constexpr uint16_t kMinMaxUDPPayload = 1200;
41 
42 // How many bytes to reduce from udpSendPacketLen when socket write leads to
43 // EMSGSIZE.
44 constexpr uint16_t kDefaultMsgSizeBackOffSize = 50;
45 
46 // Size of read buffer we provide to AsyncUDPSocket. The packet size cannot be
47 // larger than this, unless configured otherwise.
48 constexpr uint16_t kDefaultUDPReadBufferSize = 1500;
49 
50 // Default base PMTU used by d6d probing
51 constexpr uint16_t kDefaultD6DBasePMTU = kDefaultUDPSendPacketLen;
52 
53 // Default maximum number of consecutive d6d probe losses we can
54 // tolerate
55 constexpr uint16_t kDefaultD6DMaxOutstandingProbes = 2;
56 
57 // The default d6d raise timeout, recommended by the spec
58 constexpr std::chrono::seconds kDefaultD6DRaiseTimeout = 600s;
59 
60 // The minimum d6d raise timeout
61 constexpr std::chrono::seconds kMinD6DRaiseTimeout = 50s;
62 
63 // The default d6d probe timeout, recommended by the spec
64 constexpr std::chrono::seconds kDefaultD6DProbeTimeout = 15s;
65 
66 // The minimum d6d probe timeout, recommended by the spec
67 constexpr std::chrono::seconds kMinD6DProbeTimeout = 1s;
68 
69 // The default d6d start delay
70 constexpr std::chrono::milliseconds kDefaultD6DKickStartDelay = 1000ms;
71 
72 // Default delay for the next probe when the last one is acked
73 constexpr std::chrono::milliseconds kDefaultD6DProbeDelayWhenAcked = 500ms;
74 
75 // Default delay for the next probe when the last one is lost
76 constexpr std::chrono::milliseconds kDefaultD6DProbeDelayWhenLost = 2000ms;
77 
78 // The default pmtu step size, currently only useful for ConstantStep raiser
79 constexpr uint16_t kDefaultD6DProbeStepSize = 10;
80 
81 // Default window of detecting blackhole caused by invalid pmtu
82 constexpr std::chrono::seconds kDefaultD6DBlackholeDetectionWindow = 5s;
83 
84 // Default threshold for detecting blackhole caused by invalid pmtu
85 constexpr uint64_t kDefaultD6DBlackholeDetectionThreshold = 8;
86 
87 // Number of GRO buffers to use
88 // 1 means GRO is not enabled
89 // 64 is the max possible value
90 constexpr uint16_t kMinNumGROBuffers = 1;
91 constexpr uint16_t kMaxNumGROBuffers = 16;
92 constexpr uint16_t kDefaultNumGROBuffers = kMinNumGROBuffers;
93 
94 constexpr uint16_t kMaxNumCoalescedPackets = 5;
95 // As per version 20 of the spec, transport parameters for private use must
96 // have ids with first byte being 0xff.
97 constexpr uint16_t kCustomTransportParameterThreshold = 0xff00;
98 
99 // The length of the integrity tag present in a retry packet.
100 constexpr uint32_t kRetryIntegrityTagLen = 16;
101 
102 // If the amount of data in the buffer of a QuicSocket equals or exceeds this
103 // threshold, then the callback registered through
104 // notifyPendingWriteOnConnection() will not be called
105 constexpr uint64_t kDefaultBufferSpaceAvailable =
106     std::numeric_limits<uint64_t>::max();
107 
108 // The default min rtt to use for a new connection
109 constexpr std::chrono::microseconds kDefaultMinRtt =
110     std::chrono::microseconds::max();
111 
112 // Default knob space for transport knobs (used for internal use-cases only)
113 constexpr uint64_t kDefaultQuicTransportKnobSpace = 0xfaceb001;
114 
115 // Default knob id for transport knobs (used for internal use-cases only)
116 constexpr uint64_t kDefaultQuicTransportKnobId = 1;
117 
118 enum class TransportKnobParamId : uint64_t {
119   // Disabling pmtu blackhole detection
120   ZERO_PMTU_BLACKHOLE_DETECTION = 0x8830,
121   // Force udp payload size to be equal to max
122   // udp payload size
123   FORCIBLY_SET_UDP_PAYLOAD_SIZE = 0xba92,
124   // Set congestion control algorithm
125   CC_ALGORITHM_KNOB = 0xccaa,
126   // Set congestion control aggressiveness (experimental)
127   CC_AGRESSIVENESS_KNOB = 0xccab,
128   // Set pacing rtt factor used only during startup phase
129   STARTUP_RTT_FACTOR_KNOB = 0x1111,
130   // Set pacing rtt factor used when not in startup
131   DEFAULT_RTT_FACTOR_KNOB = 0x2222,
132   // Set total buffer size (in bytes) for not yet sent packets
133   NOTSENT_BUFFER_SIZE_KNOB = 0x3333,
134   // Set max pacing rate in bytes per second to be used if pacing is enabled
135   MAX_PACING_RATE_KNOB = 0x4444,
136   // Set auto background mode (experimental)
137   AUTO_BACKGROUND_MODE = 0x5555,
138 };
139 
140 enum class FrameType : uint64_t {
141   PADDING = 0x00,
142   PING = 0x01,
143   ACK = 0x02,
144   ACK_ECN = 0x03,
145   RST_STREAM = 0x04,
146   STOP_SENDING = 0x05,
147   CRYPTO_FRAME = 0x06, // librtmp has a #define CRYPTO
148   NEW_TOKEN = 0x07,
149   // STREAM frame can have values from 0x08 to 0x0f which indicate which fields
150   // are present in the frame.
151   STREAM = 0x08,
152   STREAM_FIN = 0x09,
153   STREAM_LEN = 0x0a,
154   STREAM_LEN_FIN = 0x0b,
155   STREAM_OFF = 0x0c,
156   STREAM_OFF_FIN = 0x0d,
157   STREAM_OFF_LEN = 0x0e,
158   STREAM_OFF_LEN_FIN = 0x0f,
159   MAX_DATA = 0x10,
160   MAX_STREAM_DATA = 0x11,
161   MAX_STREAMS_BIDI = 0x12,
162   MAX_STREAMS_UNI = 0x13,
163   DATA_BLOCKED = 0x14,
164   STREAM_DATA_BLOCKED = 0x15,
165   STREAMS_BLOCKED_BIDI = 0x16,
166   STREAMS_BLOCKED_UNI = 0x17,
167   NEW_CONNECTION_ID = 0x18,
168   RETIRE_CONNECTION_ID = 0x19,
169   PATH_CHALLENGE = 0x1A,
170   PATH_RESPONSE = 0x1B,
171   CONNECTION_CLOSE = 0x1C,
172   // CONNECTION_CLOSE_APP_ERR frametype is use to indicate application errors
173   CONNECTION_CLOSE_APP_ERR = 0x1D,
174   HANDSHAKE_DONE = 0x1E,
175   DATAGRAM = 0x30,
176   DATAGRAM_LEN = 0x31,
177   KNOB = 0x1550,
178   ACK_FREQUENCY = 0xAF,
179 };
180 
toFrameError(FrameType frame)181 inline constexpr uint16_t toFrameError(FrameType frame) {
182   return 0x0100 | static_cast<uint8_t>(frame);
183 }
184 
185 enum class TransportErrorCode : uint16_t {
186   NO_ERROR = 0x0000,
187   INTERNAL_ERROR = 0x0001,
188   SERVER_BUSY = 0x0002,
189   FLOW_CONTROL_ERROR = 0x0003,
190   STREAM_LIMIT_ERROR = 0x0004,
191   STREAM_STATE_ERROR = 0x0005,
192   FINAL_SIZE_ERROR = 0x0006,
193   FRAME_ENCODING_ERROR = 0x0007,
194   TRANSPORT_PARAMETER_ERROR = 0x0008,
195   PROTOCOL_VIOLATION = 0x000A,
196   INVALID_MIGRATION = 0x000C,
197   CRYPTO_ERROR = 0x100,
198   CRYPTO_ERROR_MAX = 0x1ff,
199   INVALID_TOKEN = 0xb,
200 };
201 
202 /**
203  * Application error codes are opaque to QUIC transport.  Each application
204  * protocol can define its own error codes.
205  */
206 using ApplicationErrorCode = uint16_t;
207 
208 /**
209  * Example application error codes, or codes that can be used by very simple
210  * applications.  Note: by convention error code 0 means no error.
211  *
212  * It is convenient to use not strongly typed enums so they are implicitly
213  * castable to ints, but to get the scoping semantics we enclose it in a
214  * namespace of the same name.
215  */
216 namespace GenericApplicationErrorCode {
217 enum GenericApplicationErrorCode : uint16_t {
218   NO_ERROR = 0x0000,
219   UNKNOWN = 0xFFFF
220 };
221 }
222 
223 enum class LocalErrorCode : uint32_t {
224   // Local errors
225   NO_ERROR = 0x00000000,
226   CONNECT_FAILED = 0x40000000,
227   CODEC_ERROR = 0x40000001,
228   STREAM_CLOSED = 0x40000002,
229   STREAM_NOT_EXISTS = 0x40000003,
230   CREATING_EXISTING_STREAM = 0x40000004,
231   SHUTTING_DOWN = 0x40000005,
232   RESET_CRYPTO_STREAM = 0x40000006,
233   CWND_OVERFLOW = 0x40000007,
234   INFLIGHT_BYTES_OVERFLOW = 0x40000008,
235   LOST_BYTES_OVERFLOW = 0x40000009,
236   // This is a retryable error. When encountering this error,
237   // the user should retry the request.
238   NEW_VERSION_NEGOTIATED = 0x4000000A,
239   INVALID_WRITE_CALLBACK = 0x4000000B,
240   TLS_HANDSHAKE_FAILED = 0x4000000C,
241   APP_ERROR = 0x4000000D,
242   INTERNAL_ERROR = 0x4000000E,
243   TRANSPORT_ERROR = 0x4000000F,
244   INVALID_WRITE_DATA = 0x40000010,
245   INVALID_STATE_TRANSITION = 0x40000011,
246   CONNECTION_CLOSED = 0x40000012,
247   EARLY_DATA_REJECTED = 0x40000013,
248   CONNECTION_RESET = 0x40000014,
249   IDLE_TIMEOUT = 0x40000015,
250   PACKET_NUMBER_ENCODING = 0x40000016,
251   INVALID_OPERATION = 0x40000017,
252   STREAM_LIMIT_EXCEEDED = 0x40000018,
253   CONNECTION_ABANDONED = 0x40000019,
254   CALLBACK_ALREADY_INSTALLED = 0x4000001A,
255   KNOB_FRAME_UNSUPPORTED = 0x4000001B,
256   PACER_NOT_AVAILABLE = 0x4000001C,
257 };
258 
259 enum class QuicNodeType : bool {
260   Client,
261   Server,
262 };
263 
264 enum class QuicVersion : uint32_t {
265   VERSION_NEGOTIATION = 0x00000000,
266   // Before updating the MVFST version, please check
267   // QuicTransportBase::isKnobSupported() and make sure that knob support is not
268   // broken.
269   MVFST = 0xfaceb002,
270   QUIC_DRAFT_LEGACY = 0xff00001b, // Draft-27
271   QUIC_DRAFT = 0xff00001d, // Draft-29
272   QUIC_V1 = 0x00000001,
273   MVFST_EXPERIMENTAL = 0xfaceb00e, // Experimental alias for MVFST
274   MVFST_ALIAS = 0xfaceb010,
275   MVFST_INVALID = 0xfaceb00f,
276 };
277 
278 using QuicVersionType = std::underlying_type<QuicVersion>::type;
279 
280 /**
281  * Parameter ids for private transport parameter
282  */
283 
284 constexpr uint16_t kD6DBasePMTUParameterId = 0xFF77;
285 
286 constexpr uint16_t kD6DRaiseTimeoutParameterId = 0xFF95;
287 
288 constexpr uint16_t kD6DProbeTimeoutParameterId = 0xFF12;
289 
290 constexpr uint32_t kDrainFactor = 3;
291 
292 // batching mode
293 enum class QuicBatchingMode : uint32_t {
294   BATCHING_MODE_NONE = 0,
295   BATCHING_MODE_GSO = 1,
296   BATCHING_MODE_SENDMMSG = 2,
297   BATCHING_MODE_SENDMMSG_GSO = 3,
298 };
299 
300 QuicBatchingMode getQuicBatchingMode(uint32_t val);
301 
302 // default QUIC batching size - currently used only
303 // by BATCHING_MODE_GSO
304 constexpr uint32_t kDefaultQuicMaxBatchSize = 16;
305 
306 // thread local delay
307 constexpr std::chrono::microseconds kDefaultThreadLocalDelay = 1ms;
308 
309 // rfc6298:
310 constexpr int kRttAlpha = 8;
311 constexpr int kRttBeta = 4;
312 
313 // Draft-17 recommends 100ms as initial RTT. We delibrately ignore that
314 // recommendation. This is not a bug.
315 constexpr std::chrono::microseconds kDefaultInitialRtt = 50000us;
316 
317 // HHWheelTimer tick interval
318 constexpr std::chrono::microseconds kGranularity = 10000us;
319 
320 constexpr uint32_t kReorderingThreshold = 3;
321 
322 // Current draft has 9 / 8. But our friends at Google told us they saw
323 // improvement with 5 / 4. Our tests also showed reduced retransmission with
324 // 5 / 4 without significantly huriting application latency.
325 constexpr DurationRep kDefaultTimeReorderingThreshDividend = 5;
326 constexpr DurationRep kDefaultTimeReorderingThreshDivisor = 4;
327 
328 constexpr auto kPacketToSendForPTO = 2;
329 
330 // Maximum number of packets to write per writeConnectionDataToSocket call.
331 constexpr uint64_t kDefaultWriteConnectionDataPacketLimit = 5;
332 // Minimum number of packets to write per burst in pacing
333 constexpr uint64_t kDefaultMinBurstPackets = 5;
334 // Default timer tick interval for pacing timer
335 // the microsecond timers are accurate to  about 5 usec
336 // but the notifications can get delayed if the event loop is busy
337 // this is subject to testing but I would suggest a value >= 200usec
338 constexpr std::chrono::microseconds kDefaultPacingTimerTickInterval{1000};
339 // Fraction of RTT that is used to limit how long a write function can loop
340 constexpr DurationRep kDefaultWriteLimitRttFraction = 25;
341 
342 // Congestion control:
343 constexpr folly::StringPiece kCongestionControlCubicStr = "cubic";
344 constexpr folly::StringPiece kCongestionControlBbrStr = "bbr";
345 constexpr folly::StringPiece kCongestionControlCopaStr = "copa";
346 constexpr folly::StringPiece kCongestionControlCopa2Str = "copa2";
347 constexpr folly::StringPiece kCongestionControlNewRenoStr = "newreno";
348 constexpr folly::StringPiece kCongestionControlNoneStr = "none";
349 constexpr folly::StringPiece kCongestionControlCcpStr = "ccp";
350 
351 constexpr DurationRep kPersistentCongestionThreshold = 3;
352 enum class CongestionControlType : uint8_t {
353   Cubic,
354   NewReno,
355   Copa,
356   Copa2,
357   BBR,
358   CCP,
359   None,
360   // NOTE: MAX should always be at the end
361   MAX
362 };
363 folly::StringPiece congestionControlTypeToString(CongestionControlType type);
364 folly::Optional<CongestionControlType> congestionControlStrToType(
365     folly::StringPiece str);
366 
367 // This is an approximation of a small enough number for cwnd to be blocked.
368 constexpr size_t kBlockedSizeBytes = 20;
369 
370 constexpr uint64_t kInitCwndInMss = 10;
371 constexpr uint64_t kMinCwndInMss = 2;
372 // Min cwnd for BBR is 4 MSS regard less of transport settings
373 constexpr uint64_t kMinCwndInMssForBbr{4};
374 
375 // Default max cwnd limit
376 constexpr uint64_t kDefaultMaxCwndInMss = 2000;
377 // Max cwnd limit for perf test purpose
378 constexpr uint64_t kLargeMaxCwndInMss = 860000;
379 
380 // When server receives early data attempt without valid source address token,
381 // server will limit bytes in flight to avoid amplification attack until CFIN
382 // is received which proves sender owns the address.
383 constexpr uint64_t kLimitedCwndInMss = 3;
384 
385 /* Hybrid slow start: */
386 // The first kAckSampling Acks within a RTT round will be used to sample delays
387 constexpr uint8_t kAckSampling = 8;
388 // Hystart won't exit slow start if Cwnd < kLowSsthresh
389 constexpr uint64_t kLowSsthreshInMss = 16;
390 // ACKs within kAckCountingGap are considered closely spaced, i.e., AckTrain
391 constexpr std::chrono::microseconds kAckCountingGap(2);
392 // Hystart's upper bound for DelayIncrease
393 constexpr std::chrono::microseconds kDelayIncreaseUpperBound(8);
394 // Hystart's lower bound for DelayIncrease
395 constexpr std::chrono::microseconds kDelayIncreaseLowerBound(2);
396 
397 /* Cubic */
398 // Default cwnd reduction factor:
399 constexpr double kDefaultCubicReductionFactor = 0.8;
400 // Time elapsed scaling factor
401 constexpr double kTimeScalingFactor = 0.4;
402 // Default emulated connection numbers for each real connection
403 constexpr uint8_t kDefaultEmulatedConnection = 2;
404 // Default W_max reduction factor when loss happens before Cwnd gets back to
405 // previous W_max:
406 constexpr float kDefaultLastMaxReductionFactor = 0.85f;
407 // Factor to control TCP estimate cwnd increase after Ack.
408 constexpr float kCubicTCPFriendlyEstimateIncreaseFactor =
409     3 * (1 - kDefaultCubicReductionFactor) / (1 + kDefaultCubicReductionFactor);
410 
411 /* Flow Control */
412 // Default flow control window for HTTP/2 + 1K for headers
413 constexpr uint64_t kDefaultStreamWindowSize = (64 + 1) * 1024;
414 constexpr uint64_t kDefaultConnectionWindowSize = 1024 * 1024;
415 
416 /* Stream Limits */
417 constexpr uint64_t kDefaultMaxStreamsBidirectional = 2048;
418 constexpr uint64_t kDefaultMaxStreamsUnidirectional = 2048;
419 constexpr uint64_t kMaxStreamId = 1ull << 62;
420 constexpr uint64_t kMaxMaxStreams = 1ull << 60;
421 
422 /* Idle timeout parameters */
423 // Default idle timeout to advertise.
424 constexpr auto kDefaultIdleTimeout = 60000ms;
425 constexpr auto kMaxIdleTimeout = 600000ms;
426 
427 // Time format related:
428 constexpr uint8_t kQuicTimeExpoBits = 5;
429 constexpr uint8_t kQuicTimeMantissaBits = 16 - kQuicTimeExpoBits;
430 // This is the largest possible value with a exponent = 0:
431 constexpr uint16_t kLargestQuicTimeWithoutExpo = 0xFFF;
432 // Largest possible value with a positive exponent:
433 constexpr uint64_t kLargestQuicTime = 0x0FFFull << (0x1F - 1);
434 
435 // Limit of non-retransmittable packets received before an Ack has to be
436 // emitted.
437 constexpr uint8_t kNonRtxRxPacketsPendingBeforeAck = 20;
438 // Default threshold before switching to the after init Ack frequency.
439 constexpr uint64_t kDefaultRxPacketsBeforeAckInitThreshold = 100;
440 // Default before init Ack frequency.
441 constexpr uint16_t kDefaultRxPacketsBeforeAckBeforeInit = 10;
442 // Default after init Ack frequency.
443 constexpr uint16_t kDefaultRxPacketsBeforeAckAfterInit = 10;
444 
445 /* Ack timer */
446 // Ack timeout = SRTT * kAckTimerFactor
447 constexpr double kAckTimerFactor = 0.25;
448 // max ack timeout: 25ms
449 constexpr std::chrono::microseconds kMaxAckTimeout = 25000us;
450 // max_ack_delay cannot be equal or greater that 2^14
451 constexpr uint64_t kMaxAckDelay = 1ULL << 14;
452 
453 constexpr uint64_t kAckPurgingThresh = 10;
454 
455 // Default number of packets to buffer if keys are not present.
456 constexpr uint32_t kDefaultMaxBufferedPackets = 20;
457 
458 // Default exponent to use while computing ack delay.
459 constexpr uint64_t kDefaultAckDelayExponent = 3;
460 constexpr uint64_t kMaxAckDelayExponent = 20;
461 
462 // Default connection id size of the connection id we will send.
463 constexpr size_t kDefaultConnectionIdSize = 8;
464 
465 // Minimum size of the health check token. This is used to reduce the impact of
466 // amplification attacks.
467 constexpr size_t kMinHealthCheckTokenSize = 5;
468 
469 // Maximum size of the reason phrase.
470 constexpr size_t kMaxReasonPhraseLength = 1024;
471 
472 // Minimum size of an initial packet
473 constexpr size_t kMinInitialPacketSize = 1200;
474 
475 // Default maximum PTOs that will happen before tearing down the connection
476 constexpr uint16_t kDefaultMaxNumPTO = 7;
477 
478 // Maximum early data size that we need to negotiate in TLS
479 constexpr uint32_t kRequiredMaxEarlyDataSize = 0xffffffff;
480 
481 // min connId size for one chosen by 'mvfst' as a peer (for version 1 of CID)
482 constexpr size_t kMinSelfConnectionIdV1Size = 4;
483 
484 // min connId size for one chosen by 'mvfst' as a peer (for version 2 of CID)
485 constexpr size_t kMinSelfConnectionIdV2Size = 6;
486 
487 // 22 bytes longer than minimum connection id.
488 constexpr uint16_t kMinStatelessPacketSize = 22 + kMinSelfConnectionIdV1Size;
489 
490 constexpr std::chrono::milliseconds kHappyEyeballsV4Delay = 100ms;
491 
492 constexpr std::chrono::milliseconds kHappyEyeballsConnAttemptDelayWithCache =
493     15s;
494 
495 constexpr size_t kMaxNumTokenSourceAddresses = 3;
496 
497 // Amount of time to retain zero rtt keys until they are dropped after handshake
498 // completion.
499 constexpr std::chrono::seconds kTimeToRetainZeroRttKeys = 20s;
500 
501 constexpr std::chrono::seconds kTimeToRetainLastCongestionAndRttState = 60s;
502 
503 constexpr uint32_t kMaxNumMigrationsAllowed = 6;
504 
505 constexpr auto kExpectedNumOfParamsInTheTicket = 8;
506 
507 constexpr auto kStatelessResetTokenSecretLength = 32;
508 
509 constexpr auto kRetryTokenSecretLength = 32;
510 
511 // Number of milliseconds the retry token is valid for
512 // Set it to 5 minutes
513 constexpr uint64_t kMaxRetryTokenValidMs = 1000 * 60 * 5;
514 
515 constexpr uint64_t kDefaultActiveConnectionIdLimit = 2;
516 
517 constexpr uint64_t kMaxPacketNumber = (1ull << 62) - 1;
518 
519 // Use up to 3 bytes for the initial packet number.
520 constexpr uint32_t kMaxInitialPacketNum = 0xffffff;
521 
522 // The maximum size of a DATAGRAM frame (including the frame type,
523 // length, and payload) the endpoint is willing to receive, in bytes.
524 // Disabled by default
525 constexpr uint16_t kDefaultMaxDatagramFrameSize = 0;
526 constexpr uint16_t kMaxDatagramFrameSize = 65535;
527 // Maximum overhead for a QUIC packet containing a single datagram frame
528 // i.e. Max Short Header + Max Datagram Frame Header
529 constexpr uint16_t kMaxDatagramPacketOverhead = 25 + 16;
530 // The Maximum number of datagrams (in/out) to buffer
531 constexpr uint32_t kDefaultMaxDatagramsBuffered = 75;
532 
533 enum class ZeroRttSourceTokenMatchingPolicy : uint8_t {
534   REJECT_IF_NO_EXACT_MATCH = 0,
535   LIMIT_IF_NO_EXACT_MATCH = 1,
536   ALWAYS_REJECT = 2,
537 };
538 
nodeToString(QuicNodeType node)539 inline folly::StringPiece nodeToString(QuicNodeType node) {
540   if (node == QuicNodeType::Client) {
541     return "Client";
542   } else {
543     return "Server";
544   }
545 }
546 
547 template <class T>
548 inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
549   for (auto it = v.cbegin(); it != v.cend(); ++it) {
550     os << *it;
551     if (std::next(it) != v.cend()) {
552       os << ",";
553     }
554   }
555   return os;
556 }
557 
558 inline std::ostream& operator<<(std::ostream& os, const QuicVersion& v) {
559   os << static_cast<std::underlying_type<QuicVersion>::type>(v);
560   return os;
561 }
562 
563 enum class WriteDataReason {
564   NO_WRITE,
565   PROBES,
566   ACK,
567   CRYPTO_STREAM,
568   STREAM,
569   BLOCKED,
570   STREAM_WINDOW_UPDATE,
571   CONN_WINDOW_UPDATE,
572   SIMPLE,
573   RESET,
574   PATHCHALLENGE,
575   PING,
576   DATAGRAM,
577 };
578 
579 enum class NoWriteReason {
580   WRITE_OK,
581   EMPTY_SCHEDULER,
582   NO_FRAME,
583   NO_BODY,
584   SOCKET_FAILURE,
585 };
586 
587 enum class NoReadReason {
588   READ_OK,
589   TRUNCATED,
590   EMPTY_DATA,
591   RETRIABLE_ERROR,
592   NONRETRIABLE_ERROR,
593   STALE_DATA,
594 };
595 
596 folly::StringPiece writeDataReasonString(WriteDataReason reason);
597 folly::StringPiece writeNoWriteReasonString(NoWriteReason reason);
598 folly::StringPiece readNoReadReasonString(NoReadReason reason);
599 
600 /**
601  * Filter the versions that are currently supported.
602  */
603 std::vector<QuicVersion> filterSupportedVersions(
604     const std::vector<QuicVersion>&);
605 
606 /**
607  * Represent the different encryption levels used by QUIC.
608  */
609 enum class EncryptionLevel : uint8_t {
610   Initial,
611   Handshake,
612   EarlyData,
613   AppData,
614   MAX,
615 };
616 
617 /**
618  * This is a temporary type used during our data path experiment. It  may not
619  * exist for long time.
620  */
621 enum class DataPathType : uint8_t {
622   ChainedMemory = 0,
623   ContinuousMemory = 1,
624 };
625 
626 // Stream priority level, can only be in [0, 7]
627 using PriorityLevel = uint8_t;
628 constexpr uint8_t kDefaultMaxPriority = 7;
629 
630 } // namespace quic
631