• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef QUICHE_QUIC_CORE_QUIC_CONSTANTS_H_
6 #define QUICHE_QUIC_CORE_QUIC_CONSTANTS_H_
7 
8 #include <stddef.h>
9 
10 #include <cstdint>
11 #include <limits>
12 
13 #include "quiche/quic/core/quic_types.h"
14 #include "quiche/quic/platform/api/quic_export.h"
15 
16 // Definitions of constant values used throughout the QUIC code.
17 
18 namespace quic {
19 
20 // Simple time constants.
21 inline constexpr uint64_t kNumSecondsPerMinute = 60;
22 inline constexpr uint64_t kNumSecondsPerHour = kNumSecondsPerMinute * 60;
23 inline constexpr uint64_t kNumSecondsPerWeek = kNumSecondsPerHour * 24 * 7;
24 inline constexpr uint64_t kNumMillisPerSecond = 1000;
25 inline constexpr uint64_t kNumMicrosPerMilli = 1000;
26 inline constexpr uint64_t kNumMicrosPerSecond =
27     kNumMicrosPerMilli * kNumMillisPerSecond;
28 
29 // Default number of connections for N-connection emulation.
30 inline constexpr uint32_t kDefaultNumConnections = 2;
31 // Default initial maximum size in bytes of a QUIC packet.
32 inline constexpr QuicByteCount kDefaultMaxPacketSize = 1250;
33 // Default initial maximum size in bytes of a QUIC packet for servers.
34 inline constexpr QuicByteCount kDefaultServerMaxPacketSize = 1000;
35 // Maximum transmission unit on Ethernet.
36 inline constexpr QuicByteCount kEthernetMTU = 1500;
37 // The maximum packet size of any QUIC packet over IPv6, based on ethernet's max
38 // size, minus the IP and UDP headers. IPv6 has a 40 byte header, UDP adds an
39 // additional 8 bytes.  This is a total overhead of 48 bytes.  Ethernet's
40 // max packet size is 1500 bytes,  1500 - 48 = 1452.
41 inline constexpr QuicByteCount kMaxV6PacketSize = 1452;
42 // The maximum packet size of any QUIC packet over IPv4.
43 // 1500(Ethernet) - 20(IPv4 header) - 8(UDP header) = 1472.
44 inline constexpr QuicByteCount kMaxV4PacketSize = 1472;
45 // The maximum incoming packet size allowed.
46 inline constexpr QuicByteCount kMaxIncomingPacketSize = kMaxV4PacketSize;
47 // The maximum outgoing packet size allowed.
48 inline constexpr QuicByteCount kMaxOutgoingPacketSize = kMaxV6PacketSize;
49 // ETH_MAX_MTU - MAX(sizeof(iphdr), sizeof(ip6_hdr)) - sizeof(udphdr).
50 inline constexpr QuicByteCount kMaxGsoPacketSize = 65535 - 40 - 8;
51 // The maximal IETF DATAGRAM frame size we'll accept. Choosing 2^16 ensures
52 // that it is greater than the biggest frame we could ever fit in a QUIC packet.
53 inline constexpr QuicByteCount kMaxAcceptedDatagramFrameSize = 65536;
54 // Default value of the max_packet_size transport parameter if it is not
55 // transmitted.
56 inline constexpr QuicByteCount kDefaultMaxPacketSizeTransportParam = 65527;
57 // Default maximum packet size used in the Linux TCP implementation.
58 // Used in QUIC for congestion window computations in bytes.
59 inline constexpr QuicByteCount kDefaultTCPMSS = 1460;
60 inline constexpr QuicByteCount kMaxSegmentSize = kDefaultTCPMSS;
61 // The minimum size of a packet which can elicit a version negotiation packet,
62 // as per section 8.1 of the QUIC spec.
63 inline constexpr QuicByteCount kMinPacketSizeForVersionNegotiation = 1200;
64 
65 // We match SPDY's use of 32 (since we'd compete with SPDY).
66 inline constexpr QuicPacketCount kInitialCongestionWindow = 32;
67 
68 // Do not allow initial congestion window to be greater than 200 packets.
69 inline constexpr QuicPacketCount kMaxInitialCongestionWindow = 200;
70 
71 // Do not allow initial congestion window to be smaller than 10 packets.
72 inline constexpr QuicPacketCount kMinInitialCongestionWindow = 10;
73 
74 // Minimum size of initial flow control window, for both stream and session.
75 // This is only enforced when version.AllowsLowFlowControlLimits() is false.
76 inline constexpr QuicByteCount kMinimumFlowControlSendWindow =
77     16 * 1024;  // 16 KB
78 // Default size of initial flow control window, for both stream and session.
79 inline constexpr QuicByteCount kDefaultFlowControlSendWindow =
80     16 * 1024;  // 16 KB
81 
82 // Maximum flow control receive window limits for connection and stream.
83 inline constexpr QuicByteCount kStreamReceiveWindowLimit =
84     16 * 1024 * 1024;  // 16 MB
85 inline constexpr QuicByteCount kSessionReceiveWindowLimit =
86     24 * 1024 * 1024;  // 24 MB
87 
88 // Minimum size of the CWND, in packets, when doing bandwidth resumption.
89 inline constexpr QuicPacketCount kMinCongestionWindowForBandwidthResumption =
90     10;
91 
92 // Default size of the socket receive buffer in bytes.
93 inline constexpr QuicByteCount kDefaultSocketReceiveBuffer = 1024 * 1024;
94 
95 // The lower bound of an untrusted initial rtt value.
96 inline constexpr uint32_t kMinUntrustedInitialRoundTripTimeUs =
97     10 * kNumMicrosPerMilli;
98 
99 // The lower bound of a trusted initial rtt value.
100 inline constexpr uint32_t kMinTrustedInitialRoundTripTimeUs =
101     5 * kNumMicrosPerMilli;
102 
103 // Don't allow a client to suggest an RTT longer than 1 second.
104 inline constexpr uint32_t kMaxInitialRoundTripTimeUs = kNumMicrosPerSecond;
105 
106 // Maximum number of open streams per connection.
107 inline constexpr size_t kDefaultMaxStreamsPerConnection = 100;
108 
109 // Number of bytes reserved for public flags in the packet header.
110 inline constexpr size_t kPublicFlagsSize = 1;
111 // Number of bytes reserved for version number in the packet header.
112 inline constexpr size_t kQuicVersionSize = 4;
113 
114 // Minimum number of active connection IDs that an end point can maintain.
115 inline constexpr uint32_t kMinNumOfActiveConnectionIds = 2;
116 
117 // Length of the retry integrity tag in bytes.
118 // https://tools.ietf.org/html/draft-ietf-quic-transport-25#section-17.2.5
119 inline constexpr size_t kRetryIntegrityTagLength = 16;
120 
121 // By default, UnackedPacketsMap allocates buffer of 64 after the first packet
122 // is added.
123 inline constexpr int kDefaultUnackedPacketsInitialCapacity = 64;
124 
125 // Signifies that the QuicPacket will contain version of the protocol.
126 inline constexpr bool kIncludeVersion = true;
127 // Signifies that the QuicPacket will include a diversification nonce.
128 inline constexpr bool kIncludeDiversificationNonce = true;
129 
130 // Header key used to identify final offset on data stream when sending HTTP/2
131 // trailing headers over QUIC.
132 QUIC_EXPORT_PRIVATE extern const char* const kFinalOffsetHeaderKey;
133 
134 // Default maximum delayed ack time, in ms.
135 // Uses a 25ms delayed ack timer. Helps with better signaling
136 // in low-bandwidth (< ~384 kbps), where an ack is sent per packet.
137 inline constexpr int64_t kDefaultDelayedAckTimeMs = 25;
138 
139 // Default minimum delayed ack time, in ms (used only for sender control of ack
140 // frequency).
141 inline constexpr uint32_t kDefaultMinAckDelayTimeMs = 5;
142 
143 // Default shift of the ACK delay in the IETF QUIC ACK frame.
144 inline constexpr uint32_t kDefaultAckDelayExponent = 3;
145 
146 // Minimum tail loss probe time in ms.
147 inline constexpr int64_t kMinTailLossProbeTimeoutMs = 10;
148 
149 // The timeout before the handshake succeeds.
150 inline constexpr int64_t kInitialIdleTimeoutSecs = 5;
151 // The maximum idle timeout that can be negotiated.
152 inline constexpr int64_t kMaximumIdleTimeoutSecs = 60 * 10;  // 10 minutes.
153 // The default timeout for a connection until the crypto handshake succeeds.
154 inline constexpr int64_t kMaxTimeForCryptoHandshakeSecs = 10;  // 10 secs.
155 
156 // Default limit on the number of undecryptable packets the connection buffers
157 // before the CHLO/SHLO arrive.
158 inline constexpr size_t kDefaultMaxUndecryptablePackets = 10;
159 
160 // Default ping timeout.
161 inline constexpr int64_t kPingTimeoutSecs = 15;  // 15 secs.
162 
163 // Minimum number of RTTs between Server Config Updates (SCUP) sent to client.
164 inline constexpr int kMinIntervalBetweenServerConfigUpdatesRTTs = 10;
165 
166 // Minimum time between Server Config Updates (SCUP) sent to client.
167 inline constexpr int kMinIntervalBetweenServerConfigUpdatesMs = 1000;
168 
169 // Minimum number of packets between Server Config Updates (SCUP).
170 inline constexpr int kMinPacketsBetweenServerConfigUpdates = 100;
171 
172 // The number of open streams that a server will accept is set to be slightly
173 // larger than the negotiated limit. Immediately closing the connection if the
174 // client opens slightly too many streams is not ideal: the client may have sent
175 // a FIN that was lost, and simultaneously opened a new stream. The number of
176 // streams a server accepts is a fixed increment over the negotiated limit, or a
177 // percentage increase, whichever is larger.
178 inline constexpr float kMaxStreamsMultiplier = 1.1f;
179 inline constexpr int kMaxStreamsMinimumIncrement = 10;
180 
181 // Available streams are ones with IDs less than the highest stream that has
182 // been opened which have neither been opened or reset. The limit on the number
183 // of available streams is 10 times the limit on the number of open streams.
184 inline constexpr int kMaxAvailableStreamsMultiplier = 10;
185 
186 // Track the number of promises that are not yet claimed by a
187 // corresponding get.  This must be smaller than
188 // kMaxAvailableStreamsMultiplier, because RST on a promised stream my
189 // create available streams entries.
190 inline constexpr int kMaxPromisedStreamsMultiplier =
191     kMaxAvailableStreamsMultiplier - 1;
192 
193 // The 1st PTO is armed with max of earliest in flight sent time + PTO
194 // delay and kFirstPtoSrttMultiplier * srtt from last in flight packet.
195 inline constexpr float kFirstPtoSrttMultiplier = 1.5;
196 
197 // The multiplier of RTT variation when calculating PTO timeout.
198 inline constexpr int kPtoRttvarMultiplier = 2;
199 
200 // TCP RFC calls for 1 second RTO however Linux differs from this default and
201 // define the minimum RTO to 200ms, we will use the same until we have data to
202 // support a higher or lower value.
203 inline constexpr const int64_t kMinRetransmissionTimeMs = 200;
204 // The delayed ack time must not be greater than half the min RTO.
205 static_assert(kDefaultDelayedAckTimeMs <= kMinRetransmissionTimeMs / 2,
206               "Delayed ack time must be less than or equal half the MinRTO");
207 
208 // We define an unsigned 16-bit floating point value, inspired by IEEE floats
209 // (http://en.wikipedia.org/wiki/Half_precision_floating-point_format),
210 // with 5-bit exponent (bias 1), 11-bit mantissa (effective 12 with hidden
211 // bit) and denormals, but without signs, transfinites or fractions. Wire format
212 // 16 bits (little-endian byte order) are split into exponent (high 5) and
213 // mantissa (low 11) and decoded as:
214 //   uint64_t value;
215 //   if (exponent == 0) value = mantissa;
216 //   else value = (mantissa | 1 << 11) << (exponent - 1)
217 inline constexpr int kUFloat16ExponentBits = 5;
218 inline constexpr int kUFloat16MaxExponent =
219     (1 << kUFloat16ExponentBits) - 2;                                     // 30
220 inline constexpr int kUFloat16MantissaBits = 16 - kUFloat16ExponentBits;  // 11
221 inline constexpr int kUFloat16MantissaEffectiveBits =
222     kUFloat16MantissaBits + 1;                 // 12
223 inline constexpr uint64_t kUFloat16MaxValue =  // 0x3FFC0000000
224     ((UINT64_C(1) << kUFloat16MantissaEffectiveBits) - 1)
225     << kUFloat16MaxExponent;
226 
227 // kDiversificationNonceSize is the size, in bytes, of the nonce that a server
228 // may set in the packet header to ensure that its INITIAL keys are not
229 // duplicated.
230 inline constexpr size_t kDiversificationNonceSize = 32;
231 
232 // The largest gap in packets we'll accept without closing the connection.
233 // This will likely have to be tuned.
234 inline constexpr QuicPacketCount kMaxPacketGap = 5000;
235 
236 // The max number of sequence number intervals that
237 // QuicPeerIssuedConnetionIdManager can maintain.
238 inline constexpr size_t kMaxNumConnectionIdSequenceNumberIntervals = 20;
239 
240 // The maximum number of random padding bytes to add.
241 inline constexpr QuicByteCount kMaxNumRandomPaddingBytes = 256;
242 
243 // The size of stream send buffer data slice size in bytes. A data slice is
244 // piece of stream data stored in contiguous memory, and a stream frame can
245 // contain data from multiple data slices.
246 inline constexpr QuicByteCount kQuicStreamSendBufferSliceSize = 4 * 1024;
247 
248 // For When using Random Initial Packet Numbers, they can start
249 // anyplace in the range 1...((2^31)-1) or 0x7fffffff
250 QUIC_EXPORT_PRIVATE QuicPacketNumber MaxRandomInitialPacketNumber();
251 
252 // Used to represent an invalid or no control frame id.
253 inline constexpr QuicControlFrameId kInvalidControlFrameId = 0;
254 
255 // The max length a stream can have.
256 inline constexpr QuicByteCount kMaxStreamLength = (UINT64_C(1) << 62) - 1;
257 
258 // The max value that can be encoded using IETF Var Ints.
259 inline constexpr uint64_t kMaxIetfVarInt = UINT64_C(0x3fffffffffffffff);
260 
261 // The maximum stream id value that is supported - (2^32)-1
262 inline constexpr QuicStreamId kMaxQuicStreamId = 0xffffffff;
263 
264 // The maximum value that can be stored in a 32-bit QuicStreamCount.
265 inline constexpr QuicStreamCount kMaxQuicStreamCount = 0xffffffff;
266 
267 // Number of bytes reserved for packet header type.
268 inline constexpr size_t kPacketHeaderTypeSize = 1;
269 
270 // Number of bytes reserved for connection ID length.
271 inline constexpr size_t kConnectionIdLengthSize = 1;
272 
273 // Minimum length of random bytes in IETF stateless reset packet.
274 inline constexpr size_t kMinRandomBytesLengthInStatelessReset = 24;
275 
276 // Maximum length allowed for the token in a NEW_TOKEN frame.
277 inline constexpr size_t kMaxNewTokenTokenLength = 0xffff;
278 
279 // The prefix used by a source address token in a NEW_TOKEN frame.
280 inline constexpr uint8_t kAddressTokenPrefix = 0;
281 
282 // Default initial rtt used before any samples are received.
283 inline constexpr int kInitialRttMs = 100;
284 
285 // Default threshold of packet reordering before a packet is declared lost.
286 inline constexpr QuicPacketCount kDefaultPacketReorderingThreshold = 3;
287 
288 // Default fraction (1/4) of an RTT the algorithm waits before determining a
289 // packet is lost due to early retransmission by time based loss detection.
290 inline constexpr int kDefaultLossDelayShift = 2;
291 
292 // Default fraction (1/8) of an RTT when doing IETF loss detection.
293 inline constexpr int kDefaultIetfLossDelayShift = 3;
294 
295 // Maximum number of retransmittable packets received before sending an ack.
296 inline constexpr QuicPacketCount kDefaultRetransmittablePacketsBeforeAck = 2;
297 // Wait for up to 10 retransmittable packets before sending an ack.
298 inline constexpr QuicPacketCount kMaxRetransmittablePacketsBeforeAck = 10;
299 // Minimum number of packets received before ack decimation is enabled.
300 // This intends to avoid the beginning of slow start, when CWNDs may be
301 // rapidly increasing.
302 inline constexpr QuicPacketCount kMinReceivedBeforeAckDecimation = 100;
303 // One quarter RTT delay when doing ack decimation.
304 inline constexpr float kAckDecimationDelay = 0.25;
305 
306 // The default alarm granularity assumed by QUIC code.
307 inline constexpr QuicTime::Delta kAlarmGranularity =
308     QuicTime::Delta::FromMilliseconds(1);
309 
310 // Maximum number of unretired connection IDs a connection can have.
311 inline constexpr size_t kMaxNumConnectonIdsInUse = 10u;
312 
313 // Packet number of first sending packet of a connection. Please note, this
314 // cannot be used as first received packet because peer can choose its starting
315 // packet number.
316 QUIC_EXPORT_PRIVATE QuicPacketNumber FirstSendingPacketNumber();
317 
318 // Used by clients to tell if a public reset is sent from a Google frontend.
319 QUIC_EXPORT_PRIVATE extern const char* const kEPIDGoogleFrontEnd;
320 QUIC_EXPORT_PRIVATE extern const char* const kEPIDGoogleFrontEnd0;
321 
322 inline constexpr uint64_t kHttpDatagramStreamIdDivisor = 4;
323 
324 inline constexpr QuicTime::Delta kDefaultMultiPortProbingInterval =
325     QuicTime::Delta::FromSeconds(3);
326 
327 inline constexpr size_t kMaxNumMultiPortPaths = 5;
328 
329 inline constexpr size_t kMaxDuplicatedPacketsSentToServerPreferredAddress = 5;
330 
331 }  // namespace quic
332 
333 #endif  // QUICHE_QUIC_CORE_QUIC_CONSTANTS_H_
334