blob: 25759aa3050923047c36043955be0abdfa8bb59e [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]5640d0a2012-10-22 18:17:029#include <ostream>
[email protected]8b37a092012-10-18 21:53:4910#include <utility>
11#include <vector>
12
13#include "base/basictypes.h"
14#include "base/hash_tables.h"
15#include "base/logging.h"
16#include "base/string_piece.h"
[email protected]165e0752012-11-16 07:49:4417#include "net/base/int128.h"
[email protected]8b37a092012-10-18 21:53:4918#include "net/base/net_export.h"
[email protected]2a960e02012-11-11 14:48:1019#include "net/quic/quic_time.h"
[email protected]8b37a092012-10-18 21:53:4920
21namespace net {
22
23class QuicPacket;
24
25typedef uint64 QuicGuid;
26typedef uint32 QuicStreamId;
27typedef uint64 QuicStreamOffset;
28typedef uint64 QuicPacketSequenceNumber;
[email protected]8b37a092012-10-18 21:53:4929typedef uint8 QuicFecGroupNumber;
30
31// TODO(rch): Consider Quic specific names for these constants.
32const size_t kMaxPacketSize = 1200; // Maximum size in bytes of a QUIC packet.
33
34// Maximum number of open streams per connection.
35const size_t kDefaultMaxStreamsPerConnection = 100;
36
37// Size in bytes of the packet header common across all packets.
[email protected]05e845d2012-11-11 16:39:2438const size_t kPacketHeaderSize = 16;
[email protected]8b37a092012-10-18 21:53:4939// Index of the first byte in a QUIC packet of FEC protected data.
40const size_t kStartOfFecProtectedData = kPacketHeaderSize;
41// Index of the first byte in a QUIC packet of encrypted data.
42const size_t kStartOfEncryptedData = kPacketHeaderSize - 1;
43// Index of the first byte in a QUIC packet which is hashed.
44const size_t kStartOfHashData = 0;
[email protected]082b65b2012-11-10 19:11:3145// Index into the sequence number offset in the header.
46const int kSequenceNumberOffset = 8;
[email protected]082b65b2012-11-10 19:11:3147// Index into the flags offset in the header.
[email protected]05e845d2012-11-11 16:39:2448const int kFlagsOffset = 14;
[email protected]082b65b2012-11-10 19:11:3149// Index into the fec group offset in the header.
[email protected]05e845d2012-11-11 16:39:2450const int kFecGroupOffset = 15;
[email protected]8b37a092012-10-18 21:53:4951
[email protected]be24ab22012-10-22 03:01:5252// Size in bytes of all stream frame fields.
53const size_t kMinStreamFrameLength = 15;
[email protected]8b37a092012-10-18 21:53:4954
55// Limit on the delta between stream IDs.
56const QuicStreamId kMaxStreamIdDelta = 100;
57
58// Reserved ID for the crypto stream.
59// TODO(rch): ensure that this is not usable by any other streams.
60const QuicStreamId kCryptoStreamId = 1;
61
62typedef std::pair<QuicPacketSequenceNumber, QuicPacket*> PacketPair;
63
[email protected]2a960e02012-11-11 14:48:1064const int64 kDefaultTimeoutUs = 600000000; // 10 minutes.
[email protected]8b37a092012-10-18 21:53:4965
[email protected]be24ab22012-10-22 03:01:5266enum QuicFrameType {
67 STREAM_FRAME = 0,
68 PDU_FRAME,
69 ACK_FRAME,
70 RST_STREAM_FRAME,
71 CONNECTION_CLOSE_FRAME,
72 NUM_FRAME_TYPES
[email protected]8b37a092012-10-18 21:53:4973};
74
75enum QuicPacketFlags {
76 PACKET_FLAGS_NONE = 0,
[email protected]be24ab22012-10-22 03:01:5277 PACKET_FLAGS_FEC = 1, // Payload is FEC as opposed to frames.
[email protected]8b37a092012-10-18 21:53:4978
79 PACKET_FLAGS_MAX = PACKET_FLAGS_FEC
80};
81
82enum QuicErrorCode {
83 // Stream errors.
84 QUIC_NO_ERROR = 0,
85
[email protected]be24ab22012-10-22 03:01:5286 // There were data frames after the a fin or reset.
[email protected]8b37a092012-10-18 21:53:4987 QUIC_STREAM_DATA_AFTER_TERMINATION,
88 // There was some server error which halted stream processing.
89 QUIC_SERVER_ERROR_PROCESSING_STREAM,
90 // We got two fin or reset offsets which did not match.
91 QUIC_MULTIPLE_TERMINATION_OFFSETS,
92 // We got bad payload and can not respond to it at the protocol level.
93 QUIC_BAD_APPLICATION_PAYLOAD,
94
95 // Connection errors.
96
97 // Control frame is malformed.
98 QUIC_INVALID_PACKET_HEADER,
[email protected]be24ab22012-10-22 03:01:5299 // Frame data is malformed.
100 QUIC_INVALID_FRAME_DATA,
[email protected]8b37a092012-10-18 21:53:49101 // FEC data is malformed.
102 QUIC_INVALID_FEC_DATA,
103 // Stream rst data is malformed
104 QUIC_INVALID_RST_STREAM_DATA,
105 // Connection close data is malformed.
106 QUIC_INVALID_CONNECTION_CLOSE_DATA,
107 // Ack data is malformed.
108 QUIC_INVALID_ACK_DATA,
109 // There was an error decrypting.
110 QUIC_DECRYPTION_FAILURE,
111 // There was an error encrypting.
112 QUIC_ENCRYPTION_FAILURE,
113 // The packet exceeded kMaxPacketSize.
114 QUIC_PACKET_TOO_LARGE,
115 // Data was sent for a stream which did not exist.
116 QUIC_PACKET_FOR_NONEXISTENT_STREAM,
117 // The client is going away (browser close, etc.)
118 QUIC_CLIENT_GOING_AWAY,
119 // The server is going away (restart etc.)
120 QUIC_SERVER_GOING_AWAY,
121 // A stream ID was invalid.
122 QUIC_INVALID_STREAM_ID,
123 // Too many streams already open.
124 QUIC_TOO_MANY_OPEN_STREAMS,
125
126 // We hit our prenegotiated (or default) timeout
127 QUIC_CONNECTION_TIMED_OUT,
128
129 // Crypto errors.
130
131 // Handshake message contained out of order tags.
132 QUIC_CRYPTO_TAGS_OUT_OF_ORDER,
133 // Handshake message contained too many entires.
134 QUIC_CRYPTO_TOO_MANY_ENTRIES,
135 // Handshake message contained an invalid value length.
136 QUIC_CRYPTO_INVALID_VALUE_LENGTH,
137 // A crypto message was received after the handshake was complete.
138 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE,
139 // A crypto message was receieved with an illegal message tag.
140 QUIC_INVALID_CRYPTO_MESSAGE_TYPE,
141
142};
143
144struct NET_EXPORT_PRIVATE QuicPacketHeader {
145 // Includes the ConnectionHeader and CongestionMonitoredHeader
146 // from the design docs, as well as some elements of DecryptedData.
147 QuicGuid guid;
148 QuicPacketSequenceNumber packet_sequence_number;
[email protected]8b37a092012-10-18 21:53:49149 QuicPacketFlags flags;
150 QuicFecGroupNumber fec_group;
151};
152
[email protected]be24ab22012-10-22 03:01:52153struct NET_EXPORT_PRIVATE QuicStreamFrame {
154 QuicStreamFrame();
155 QuicStreamFrame(QuicStreamId stream_id,
[email protected]a5061242012-10-23 23:29:37156 bool fin,
157 uint64 offset,
158 base::StringPiece data);
[email protected]8b37a092012-10-18 21:53:49159
160 QuicStreamId stream_id;
161 bool fin;
162 uint64 offset;
163 base::StringPiece data;
164};
165
[email protected]044ac2b2012-11-13 21:41:06166typedef base::hash_set<QuicPacketSequenceNumber> SequenceSet;
167
[email protected]8b37a092012-10-18 21:53:49168struct NET_EXPORT_PRIVATE ReceivedPacketInfo {
169 ReceivedPacketInfo();
170 ~ReceivedPacketInfo();
171 // The highest packet sequence number we've received from the peer.
172 QuicPacketSequenceNumber largest_received;
173 // The time at which we received the above packet.
[email protected]2a960e02012-11-11 14:48:10174 QuicTime time_received;
[email protected]8b37a092012-10-18 21:53:49175 // The set of packets which we're expecting and have not received.
176 // This includes any packets between the lowest and largest_received
177 // which we have neither seen nor been informed are non-retransmitting.
[email protected]044ac2b2012-11-13 21:41:06178 SequenceSet missing_packets;
[email protected]8b37a092012-10-18 21:53:49179};
180
181struct NET_EXPORT_PRIVATE SentPacketInfo {
182 SentPacketInfo();
183 ~SentPacketInfo();
184 // The lowest packet we've sent which is unacked, and we expect an ack for.
185 QuicPacketSequenceNumber least_unacked;
186 // The set of packets between least_unacked and the last packet we have sent
187 // which we will not resend.
[email protected]044ac2b2012-11-13 21:41:06188 SequenceSet non_retransmiting;
[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() {}
244 QuicAckFrame(QuicPacketSequenceNumber largest_received,
[email protected]2a960e02012-11-11 14:48:10245 QuicTime time_received,
[email protected]a5061242012-10-23 23:29:37246 QuicPacketSequenceNumber least_unacked) {
[email protected]8b37a092012-10-18 21:53:49247 received_info.largest_received = largest_received;
248 received_info.time_received = time_received;
249 sent_info.least_unacked = least_unacked;
250 congestion_info.type = kNone;
251 }
252
[email protected]5640d0a2012-10-22 18:17:02253 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os,
254 const QuicAckFrame& s);
255
[email protected]8b37a092012-10-18 21:53:49256 SentPacketInfo sent_info;
257 ReceivedPacketInfo received_info;
258 CongestionInfo congestion_info;
[email protected]8b37a092012-10-18 21:53:49259};
260
[email protected]be24ab22012-10-22 03:01:52261struct NET_EXPORT_PRIVATE QuicRstStreamFrame {
262 QuicRstStreamFrame() {}
263 QuicRstStreamFrame(QuicStreamId stream_id, uint64 offset,
[email protected]431bb4fd2012-10-19 17:46:09264 QuicErrorCode error_code)
265 : stream_id(stream_id), offset(offset), error_code(error_code) {
266 DCHECK_LE(error_code, std::numeric_limits<uint8>::max());
[email protected]8b37a092012-10-18 21:53:49267 }
268
269 QuicStreamId stream_id;
270 uint64 offset;
[email protected]431bb4fd2012-10-19 17:46:09271 QuicErrorCode error_code;
272 std::string error_details;
[email protected]8b37a092012-10-18 21:53:49273};
274
[email protected]be24ab22012-10-22 03:01:52275struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame {
[email protected]431bb4fd2012-10-19 17:46:09276 QuicErrorCode error_code;
[email protected]be24ab22012-10-22 03:01:52277 QuicAckFrame ack_frame;
[email protected]431bb4fd2012-10-19 17:46:09278 std::string error_details;
[email protected]8b37a092012-10-18 21:53:49279};
280
[email protected]be24ab22012-10-22 03:01:52281struct NET_EXPORT_PRIVATE QuicFrame {
282 QuicFrame() {}
283 explicit QuicFrame(QuicStreamFrame* stream_frame)
284 : type(STREAM_FRAME), stream_frame(stream_frame) {
[email protected]8b37a092012-10-18 21:53:49285 }
[email protected]be24ab22012-10-22 03:01:52286 explicit QuicFrame(QuicAckFrame* frame)
287 : type(ACK_FRAME), ack_frame(frame) {
[email protected]8b37a092012-10-18 21:53:49288 }
[email protected]be24ab22012-10-22 03:01:52289 explicit QuicFrame(QuicRstStreamFrame* frame)
290 : type(RST_STREAM_FRAME),
291 rst_stream_frame(frame) {
[email protected]8b37a092012-10-18 21:53:49292 }
[email protected]be24ab22012-10-22 03:01:52293 explicit QuicFrame(QuicConnectionCloseFrame* frame)
294 : type(CONNECTION_CLOSE_FRAME),
295 connection_close_frame(frame) {
[email protected]8b37a092012-10-18 21:53:49296 }
297
[email protected]be24ab22012-10-22 03:01:52298 QuicFrameType type;
[email protected]8b37a092012-10-18 21:53:49299 union {
[email protected]be24ab22012-10-22 03:01:52300 QuicStreamFrame* stream_frame;
301 QuicAckFrame* ack_frame;
302 QuicRstStreamFrame* rst_stream_frame;
303 QuicConnectionCloseFrame* connection_close_frame;
[email protected]8b37a092012-10-18 21:53:49304 };
305};
306
[email protected]be24ab22012-10-22 03:01:52307typedef std::vector<QuicFrame> QuicFrames;
[email protected]8b37a092012-10-18 21:53:49308
309struct NET_EXPORT_PRIVATE QuicFecData {
310 QuicFecData();
[email protected]a5061242012-10-23 23:29:37311
312 bool operator==(const QuicFecData& other) const;
313
[email protected]8b37a092012-10-18 21:53:49314 QuicFecGroupNumber fec_group;
[email protected]a5061242012-10-23 23:29:37315 QuicPacketSequenceNumber min_protected_packet_sequence_number;
[email protected]8b37a092012-10-18 21:53:49316 // The last protected packet's sequence number will be one
317 // less than the sequence number of the FEC packet.
318 base::StringPiece redundancy;
[email protected]8b37a092012-10-18 21:53:49319};
320
321struct NET_EXPORT_PRIVATE QuicPacketData {
322 std::string data;
323};
324
325class NET_EXPORT_PRIVATE QuicData {
326 public:
327 QuicData(const char* buffer, size_t length)
328 : buffer_(buffer),
329 length_(length),
330 owns_buffer_(false) { }
331
332 QuicData(char* buffer, size_t length, bool owns_buffer)
333 : buffer_(buffer),
334 length_(length),
335 owns_buffer_(owns_buffer) { }
336
337 virtual ~QuicData();
338
339 base::StringPiece AsStringPiece() const {
340 return base::StringPiece(data(), length());
341 }
342
343 const char* data() const { return buffer_; }
344 size_t length() const { return length_; }
345
346 private:
347 const char* buffer_;
348 size_t length_;
349 bool owns_buffer_;
350
351 DISALLOW_COPY_AND_ASSIGN(QuicData);
352};
353
354class NET_EXPORT_PRIVATE QuicPacket : public QuicData {
355 public:
[email protected]082b65b2012-11-10 19:11:31356 QuicPacket(
357 char* buffer, size_t length, bool owns_buffer, QuicPacketFlags flags)
[email protected]8b37a092012-10-18 21:53:49358 : QuicData(buffer, length, owns_buffer),
[email protected]082b65b2012-11-10 19:11:31359 buffer_(buffer),
360 flags_(flags) { }
[email protected]8b37a092012-10-18 21:53:49361
362 base::StringPiece FecProtectedData() const {
363 return base::StringPiece(data() + kStartOfFecProtectedData,
364 length() - kStartOfFecProtectedData);
365 }
366
367 base::StringPiece AssociatedData() const {
368 return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData);
369 }
370
371 base::StringPiece Plaintext() const {
372 return base::StringPiece(data() + kStartOfEncryptedData,
373 length() - kStartOfEncryptedData);
374 }
[email protected]082b65b2012-11-10 19:11:31375
376 bool IsFecPacket() const {
377 return flags_ == PACKET_FLAGS_FEC;
378 }
379
[email protected]8b37a092012-10-18 21:53:49380 char* mutable_data() { return buffer_; }
381
382 private:
383 char* buffer_;
[email protected]082b65b2012-11-10 19:11:31384 const QuicPacketFlags flags_;
[email protected]8b37a092012-10-18 21:53:49385
[email protected]2e740db2012-10-20 19:35:19386 DISALLOW_COPY_AND_ASSIGN(QuicPacket);
[email protected]8b37a092012-10-18 21:53:49387};
388
389class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
390 public:
391 QuicEncryptedPacket(const char* buffer, size_t length)
392 : QuicData(buffer, length) { }
393
394 QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer)
395 : QuicData(buffer, length, owns_buffer) { }
396
397 base::StringPiece AssociatedData() const {
398 return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData);
399 }
400
[email protected]2e740db2012-10-20 19:35:19401 private:
402 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket);
[email protected]8b37a092012-10-18 21:53:49403};
404
405} // namespace net
406
407#endif // NET_QUIC_QUIC_PROTOCOL_H_