blob: 9a8fd22d3c0b5279b3ae4dd2c540d14316a87137 [file] [log] [blame]
[email protected]8b37a092012-10-18 21:53:491// 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 NET_QUIC_QUIC_PROTOCOL_H_
6#define NET_QUIC_QUIC_PROTOCOL_H_
7
8#include <limits>
[email protected]a674b4c2012-12-05 03:44:309#include <map>
[email protected]5640d0a2012-10-22 18:17:0210#include <ostream>
[email protected]8b37a092012-10-18 21:53:4911#include <utility>
12#include <vector>
13
14#include "base/basictypes.h"
15#include "base/hash_tables.h"
16#include "base/logging.h"
17#include "base/string_piece.h"
[email protected]165e0752012-11-16 07:49:4418#include "net/base/int128.h"
[email protected]8b37a092012-10-18 21:53:4919#include "net/base/net_export.h"
[email protected]2a960e02012-11-11 14:48:1020#include "net/quic/quic_time.h"
[email protected]8b37a092012-10-18 21:53:4921
22namespace net {
23
24class QuicPacket;
25
26typedef uint64 QuicGuid;
27typedef uint32 QuicStreamId;
28typedef uint64 QuicStreamOffset;
29typedef uint64 QuicPacketSequenceNumber;
[email protected]8b37a092012-10-18 21:53:4930typedef uint8 QuicFecGroupNumber;
31
32// TODO(rch): Consider Quic specific names for these constants.
33const size_t kMaxPacketSize = 1200; // Maximum size in bytes of a QUIC packet.
34
35// Maximum number of open streams per connection.
36const size_t kDefaultMaxStreamsPerConnection = 100;
37
38// Size in bytes of the packet header common across all packets.
[email protected]05e845d2012-11-11 16:39:2439const size_t kPacketHeaderSize = 16;
[email protected]8b37a092012-10-18 21:53:4940// Index of the first byte in a QUIC packet of FEC protected data.
41const size_t kStartOfFecProtectedData = kPacketHeaderSize;
42// Index of the first byte in a QUIC packet of encrypted data.
43const size_t kStartOfEncryptedData = kPacketHeaderSize - 1;
44// Index of the first byte in a QUIC packet which is hashed.
45const size_t kStartOfHashData = 0;
[email protected]082b65b2012-11-10 19:11:3146// Index into the sequence number offset in the header.
47const int kSequenceNumberOffset = 8;
[email protected]082b65b2012-11-10 19:11:3148// Index into the flags offset in the header.
[email protected]05e845d2012-11-11 16:39:2449const int kFlagsOffset = 14;
[email protected]082b65b2012-11-10 19:11:3150// Index into the fec group offset in the header.
[email protected]05e845d2012-11-11 16:39:2451const int kFecGroupOffset = 15;
[email protected]8b37a092012-10-18 21:53:4952
[email protected]be24ab22012-10-22 03:01:5253// Size in bytes of all stream frame fields.
54const size_t kMinStreamFrameLength = 15;
[email protected]8b37a092012-10-18 21:53:4955
56// Limit on the delta between stream IDs.
57const QuicStreamId kMaxStreamIdDelta = 100;
58
59// Reserved ID for the crypto stream.
60// TODO(rch): ensure that this is not usable by any other streams.
61const QuicStreamId kCryptoStreamId = 1;
62
63typedef std::pair<QuicPacketSequenceNumber, QuicPacket*> PacketPair;
64
[email protected]2a960e02012-11-11 14:48:1065const int64 kDefaultTimeoutUs = 600000000; // 10 minutes.
[email protected]8b37a092012-10-18 21:53:4966
[email protected]be24ab22012-10-22 03:01:5267enum QuicFrameType {
68 STREAM_FRAME = 0,
69 PDU_FRAME,
70 ACK_FRAME,
71 RST_STREAM_FRAME,
72 CONNECTION_CLOSE_FRAME,
73 NUM_FRAME_TYPES
[email protected]8b37a092012-10-18 21:53:4974};
75
76enum QuicPacketFlags {
77 PACKET_FLAGS_NONE = 0,
[email protected]be24ab22012-10-22 03:01:5278 PACKET_FLAGS_FEC = 1, // Payload is FEC as opposed to frames.
[email protected]8b37a092012-10-18 21:53:4979
80 PACKET_FLAGS_MAX = PACKET_FLAGS_FEC
81};
82
83enum QuicErrorCode {
84 // Stream errors.
85 QUIC_NO_ERROR = 0,
86
[email protected]be24ab22012-10-22 03:01:5287 // There were data frames after the a fin or reset.
[email protected]8b37a092012-10-18 21:53:4988 QUIC_STREAM_DATA_AFTER_TERMINATION,
89 // There was some server error which halted stream processing.
90 QUIC_SERVER_ERROR_PROCESSING_STREAM,
91 // We got two fin or reset offsets which did not match.
92 QUIC_MULTIPLE_TERMINATION_OFFSETS,
93 // We got bad payload and can not respond to it at the protocol level.
94 QUIC_BAD_APPLICATION_PAYLOAD,
95
96 // Connection errors.
97
98 // Control frame is malformed.
99 QUIC_INVALID_PACKET_HEADER,
[email protected]be24ab22012-10-22 03:01:52100 // Frame data is malformed.
101 QUIC_INVALID_FRAME_DATA,
[email protected]8b37a092012-10-18 21:53:49102 // FEC data is malformed.
103 QUIC_INVALID_FEC_DATA,
104 // Stream rst data is malformed
105 QUIC_INVALID_RST_STREAM_DATA,
106 // Connection close data is malformed.
107 QUIC_INVALID_CONNECTION_CLOSE_DATA,
108 // Ack data is malformed.
109 QUIC_INVALID_ACK_DATA,
110 // There was an error decrypting.
111 QUIC_DECRYPTION_FAILURE,
112 // There was an error encrypting.
113 QUIC_ENCRYPTION_FAILURE,
114 // The packet exceeded kMaxPacketSize.
115 QUIC_PACKET_TOO_LARGE,
116 // Data was sent for a stream which did not exist.
117 QUIC_PACKET_FOR_NONEXISTENT_STREAM,
118 // The client is going away (browser close, etc.)
119 QUIC_CLIENT_GOING_AWAY,
120 // The server is going away (restart etc.)
121 QUIC_SERVER_GOING_AWAY,
122 // A stream ID was invalid.
123 QUIC_INVALID_STREAM_ID,
124 // Too many streams already open.
125 QUIC_TOO_MANY_OPEN_STREAMS,
126
127 // We hit our prenegotiated (or default) timeout
128 QUIC_CONNECTION_TIMED_OUT,
129
130 // Crypto errors.
131
132 // Handshake message contained out of order tags.
133 QUIC_CRYPTO_TAGS_OUT_OF_ORDER,
134 // Handshake message contained too many entires.
135 QUIC_CRYPTO_TOO_MANY_ENTRIES,
136 // Handshake message contained an invalid value length.
137 QUIC_CRYPTO_INVALID_VALUE_LENGTH,
138 // A crypto message was received after the handshake was complete.
139 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE,
140 // A crypto message was receieved with an illegal message tag.
141 QUIC_INVALID_CRYPTO_MESSAGE_TYPE,
142
143};
144
145struct NET_EXPORT_PRIVATE QuicPacketHeader {
146 // Includes the ConnectionHeader and CongestionMonitoredHeader
147 // from the design docs, as well as some elements of DecryptedData.
148 QuicGuid guid;
149 QuicPacketSequenceNumber packet_sequence_number;
[email protected]8b37a092012-10-18 21:53:49150 QuicPacketFlags flags;
151 QuicFecGroupNumber fec_group;
152};
153
[email protected]be24ab22012-10-22 03:01:52154struct NET_EXPORT_PRIVATE QuicStreamFrame {
155 QuicStreamFrame();
156 QuicStreamFrame(QuicStreamId stream_id,
[email protected]a5061242012-10-23 23:29:37157 bool fin,
158 uint64 offset,
159 base::StringPiece data);
[email protected]8b37a092012-10-18 21:53:49160
161 QuicStreamId stream_id;
162 bool fin;
163 uint64 offset;
164 base::StringPiece data;
165};
166
[email protected]044ac2b2012-11-13 21:41:06167typedef base::hash_set<QuicPacketSequenceNumber> SequenceSet;
168
[email protected]8b37a092012-10-18 21:53:49169struct NET_EXPORT_PRIVATE ReceivedPacketInfo {
170 ReceivedPacketInfo();
171 ~ReceivedPacketInfo();
[email protected]a674b4c2012-12-05 03:44:30172
173 void RecordAck(QuicPacketSequenceNumber sequence_number, QuicTime time);
174 bool ContainsAck(QuicPacketSequenceNumber sequence_number) const;
175 void ClearAcksBefore(QuicPacketSequenceNumber least_unacked);
176
[email protected]8b37a092012-10-18 21:53:49177 // The highest packet sequence number we've received from the peer.
178 QuicPacketSequenceNumber largest_received;
[email protected]a674b4c2012-12-05 03:44:30179
180 // The set of all received packets and their arrival times.
181 std::map<QuicPacketSequenceNumber, QuicTime> received_packet_times;
[email protected]8b37a092012-10-18 21:53:49182};
183
184struct NET_EXPORT_PRIVATE SentPacketInfo {
185 SentPacketInfo();
186 ~SentPacketInfo();
187 // The lowest packet we've sent which is unacked, and we expect an ack for.
188 QuicPacketSequenceNumber least_unacked;
[email protected]8b37a092012-10-18 21:53:49189};
190
191// Defines for all types of congestion feedback that will be negotiated in QUIC,
192// kTCP MUST be supported by all QUIC implementations to guarentee 100%
193// compatibility.
194enum CongestionFeedbackType {
195 kNone = 0, // No feedback provided
196 kTCP, // Used to mimic TCP.
197 kInterArrival, // Use additional inter arrival information.
198 kFixRate, // Provided for testing.
199};
200
201struct NET_EXPORT_PRIVATE CongestionFeedbackMessageTCP {
202 uint16 accumulated_number_of_lost_packets;
203 uint16 receive_window; // Number of bytes >> 4.
204};
205
206struct NET_EXPORT_PRIVATE CongestionFeedbackMessageInterArrival {
207 uint16 accumulated_number_of_lost_packets;
208 int16 offset_time;
209 uint16 delta_time; // delta time is described below.
210};
211
212/*
213 * Description of delta time.
214 *
215 * 0 1
216 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
217 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
218 * |D|S| offset_time |
219 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
220 *
221 * Where:
222 * D is the time domain. If set time domain is in milliseconds, else in
223 * microseconds.
224 * S is the sign bit.
225 * offset_time is the time offset where the relative packet size is equal to
226 * 0.
227 */
228
229struct NET_EXPORT_PRIVATE CongestionFeedbackMessageFixRate {
230 uint32 bitrate_in_bytes_per_second;
231};
232
233struct NET_EXPORT_PRIVATE CongestionInfo {
234 CongestionFeedbackType type;
235 union {
236 CongestionFeedbackMessageTCP tcp;
237 CongestionFeedbackMessageInterArrival inter_arrival;
238 CongestionFeedbackMessageFixRate fix_rate;
239 };
240};
241
[email protected]be24ab22012-10-22 03:01:52242struct NET_EXPORT_PRIVATE QuicAckFrame {
243 QuicAckFrame() {}
[email protected]a674b4c2012-12-05 03:44:30244 // Testing convenience method to construct a QuicAckFrame with all packets
245 // from least_unacked to largest_received acked at time_received.
[email protected]be24ab22012-10-22 03:01:52246 QuicAckFrame(QuicPacketSequenceNumber largest_received,
[email protected]2a960e02012-11-11 14:48:10247 QuicTime time_received,
[email protected]a674b4c2012-12-05 03:44:30248 QuicPacketSequenceNumber least_unacked);
[email protected]8b37a092012-10-18 21:53:49249
[email protected]5640d0a2012-10-22 18:17:02250 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os,
251 const QuicAckFrame& s);
252
[email protected]8b37a092012-10-18 21:53:49253 SentPacketInfo sent_info;
254 ReceivedPacketInfo received_info;
255 CongestionInfo congestion_info;
[email protected]8b37a092012-10-18 21:53:49256};
257
[email protected]be24ab22012-10-22 03:01:52258struct NET_EXPORT_PRIVATE QuicRstStreamFrame {
259 QuicRstStreamFrame() {}
260 QuicRstStreamFrame(QuicStreamId stream_id, uint64 offset,
[email protected]f702d572012-12-04 15:56:20261 QuicErrorCode error_code)
[email protected]431bb4fd2012-10-19 17:46:09262 : stream_id(stream_id), offset(offset), error_code(error_code) {
263 DCHECK_LE(error_code, std::numeric_limits<uint8>::max());
[email protected]8b37a092012-10-18 21:53:49264 }
265
266 QuicStreamId stream_id;
267 uint64 offset;
[email protected]431bb4fd2012-10-19 17:46:09268 QuicErrorCode error_code;
269 std::string error_details;
[email protected]8b37a092012-10-18 21:53:49270};
271
[email protected]be24ab22012-10-22 03:01:52272struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame {
[email protected]431bb4fd2012-10-19 17:46:09273 QuicErrorCode error_code;
[email protected]be24ab22012-10-22 03:01:52274 QuicAckFrame ack_frame;
[email protected]431bb4fd2012-10-19 17:46:09275 std::string error_details;
[email protected]8b37a092012-10-18 21:53:49276};
277
[email protected]be24ab22012-10-22 03:01:52278struct NET_EXPORT_PRIVATE QuicFrame {
279 QuicFrame() {}
280 explicit QuicFrame(QuicStreamFrame* stream_frame)
281 : type(STREAM_FRAME), stream_frame(stream_frame) {
[email protected]8b37a092012-10-18 21:53:49282 }
[email protected]be24ab22012-10-22 03:01:52283 explicit QuicFrame(QuicAckFrame* frame)
284 : type(ACK_FRAME), ack_frame(frame) {
[email protected]8b37a092012-10-18 21:53:49285 }
[email protected]be24ab22012-10-22 03:01:52286 explicit QuicFrame(QuicRstStreamFrame* frame)
287 : type(RST_STREAM_FRAME),
288 rst_stream_frame(frame) {
[email protected]8b37a092012-10-18 21:53:49289 }
[email protected]be24ab22012-10-22 03:01:52290 explicit QuicFrame(QuicConnectionCloseFrame* frame)
291 : type(CONNECTION_CLOSE_FRAME),
292 connection_close_frame(frame) {
[email protected]8b37a092012-10-18 21:53:49293 }
294
[email protected]be24ab22012-10-22 03:01:52295 QuicFrameType type;
[email protected]8b37a092012-10-18 21:53:49296 union {
[email protected]be24ab22012-10-22 03:01:52297 QuicStreamFrame* stream_frame;
298 QuicAckFrame* ack_frame;
299 QuicRstStreamFrame* rst_stream_frame;
300 QuicConnectionCloseFrame* connection_close_frame;
[email protected]8b37a092012-10-18 21:53:49301 };
302};
303
[email protected]be24ab22012-10-22 03:01:52304typedef std::vector<QuicFrame> QuicFrames;
[email protected]8b37a092012-10-18 21:53:49305
306struct NET_EXPORT_PRIVATE QuicFecData {
307 QuicFecData();
[email protected]a5061242012-10-23 23:29:37308
309 bool operator==(const QuicFecData& other) const;
310
[email protected]8b37a092012-10-18 21:53:49311 QuicFecGroupNumber fec_group;
[email protected]a5061242012-10-23 23:29:37312 QuicPacketSequenceNumber min_protected_packet_sequence_number;
[email protected]8b37a092012-10-18 21:53:49313 // The last protected packet's sequence number will be one
314 // less than the sequence number of the FEC packet.
315 base::StringPiece redundancy;
[email protected]8b37a092012-10-18 21:53:49316};
317
318struct NET_EXPORT_PRIVATE QuicPacketData {
319 std::string data;
320};
321
322class NET_EXPORT_PRIVATE QuicData {
323 public:
324 QuicData(const char* buffer, size_t length)
325 : buffer_(buffer),
326 length_(length),
327 owns_buffer_(false) { }
328
329 QuicData(char* buffer, size_t length, bool owns_buffer)
330 : buffer_(buffer),
331 length_(length),
332 owns_buffer_(owns_buffer) { }
333
334 virtual ~QuicData();
335
336 base::StringPiece AsStringPiece() const {
337 return base::StringPiece(data(), length());
338 }
339
340 const char* data() const { return buffer_; }
341 size_t length() const { return length_; }
342
343 private:
344 const char* buffer_;
345 size_t length_;
346 bool owns_buffer_;
347
348 DISALLOW_COPY_AND_ASSIGN(QuicData);
349};
350
351class NET_EXPORT_PRIVATE QuicPacket : public QuicData {
352 public:
[email protected]082b65b2012-11-10 19:11:31353 QuicPacket(
354 char* buffer, size_t length, bool owns_buffer, QuicPacketFlags flags)
[email protected]8b37a092012-10-18 21:53:49355 : QuicData(buffer, length, owns_buffer),
[email protected]082b65b2012-11-10 19:11:31356 buffer_(buffer),
357 flags_(flags) { }
[email protected]8b37a092012-10-18 21:53:49358
359 base::StringPiece FecProtectedData() const {
360 return base::StringPiece(data() + kStartOfFecProtectedData,
361 length() - kStartOfFecProtectedData);
362 }
363
364 base::StringPiece AssociatedData() const {
365 return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData);
366 }
367
368 base::StringPiece Plaintext() const {
369 return base::StringPiece(data() + kStartOfEncryptedData,
370 length() - kStartOfEncryptedData);
371 }
[email protected]082b65b2012-11-10 19:11:31372
373 bool IsFecPacket() const {
374 return flags_ == PACKET_FLAGS_FEC;
375 }
376
[email protected]8b37a092012-10-18 21:53:49377 char* mutable_data() { return buffer_; }
378
379 private:
380 char* buffer_;
[email protected]082b65b2012-11-10 19:11:31381 const QuicPacketFlags flags_;
[email protected]8b37a092012-10-18 21:53:49382
[email protected]2e740db2012-10-20 19:35:19383 DISALLOW_COPY_AND_ASSIGN(QuicPacket);
[email protected]8b37a092012-10-18 21:53:49384};
385
386class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
387 public:
388 QuicEncryptedPacket(const char* buffer, size_t length)
389 : QuicData(buffer, length) { }
390
391 QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer)
392 : QuicData(buffer, length, owns_buffer) { }
393
394 base::StringPiece AssociatedData() const {
395 return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData);
396 }
397
[email protected]2e740db2012-10-20 19:35:19398 private:
399 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket);
[email protected]8b37a092012-10-18 21:53:49400};
401
402} // namespace net
403
404#endif // NET_QUIC_QUIC_PROTOCOL_H_