blob: a76817eb3c58f545f11c95fd73e53f72b3e6e85c [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>
9#include <utility>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "base/hash_tables.h"
14#include "base/logging.h"
15#include "base/string_piece.h"
16#include "net/base/net_export.h"
17#include "net/quic/uint128.h"
18
19namespace net {
20
21class QuicPacket;
22
23typedef uint64 QuicGuid;
24typedef uint32 QuicStreamId;
25typedef uint64 QuicStreamOffset;
26typedef uint64 QuicPacketSequenceNumber;
27typedef uint64 QuicTransmissionTime;
28typedef uint8 QuicFecGroupNumber;
29
30// TODO(rch): Consider Quic specific names for these constants.
31const size_t kMaxPacketSize = 1200; // Maximum size in bytes of a QUIC packet.
32
33// Maximum number of open streams per connection.
34const size_t kDefaultMaxStreamsPerConnection = 100;
35
36// Size in bytes of the packet header common across all packets.
37const size_t kPacketHeaderSize = 25;
38// Index of the first byte in a QUIC packet of FEC protected data.
39const size_t kStartOfFecProtectedData = kPacketHeaderSize;
40// Index of the first byte in a QUIC packet of encrypted data.
41const size_t kStartOfEncryptedData = kPacketHeaderSize - 1;
42// Index of the first byte in a QUIC packet which is hashed.
43const size_t kStartOfHashData = 0;
44// Index into the retransmission offset in the header.
45// (After GUID and sequence number.)
46const int kRetransmissionOffset = 14;
47// Index into the transmission time offset in the header.
48const int kTransmissionTimeOffset = 15;
49
50// Size in bytes of all stream fragment fields.
51const size_t kMinStreamFragmentLength = 15;
52
53// Limit on the delta between stream IDs.
54const QuicStreamId kMaxStreamIdDelta = 100;
55
56// Reserved ID for the crypto stream.
57// TODO(rch): ensure that this is not usable by any other streams.
58const QuicStreamId kCryptoStreamId = 1;
59
60typedef std::pair<QuicPacketSequenceNumber, QuicPacket*> PacketPair;
61
62const int64 kDefaultTimeout = 600000000; // 10 minutes
63
64enum QuicFragmentType {
65 STREAM_FRAGMENT = 0,
66 PDU_FRAGMENT,
67 ACK_FRAGMENT,
68 RST_STREAM_FRAGMENT,
69 CONNECTION_CLOSE_FRAGMENT,
70 NUM_FRAGMENT_TYPES
71};
72
73enum QuicPacketFlags {
74 PACKET_FLAGS_NONE = 0,
75 PACKET_FLAGS_FEC = 1, // Payload is FEC as opposed to fragments.
76
77 PACKET_FLAGS_MAX = PACKET_FLAGS_FEC
78};
79
80enum QuicErrorCode {
81 // Stream errors.
82 QUIC_NO_ERROR = 0,
83
84 // There were data fragments after the a fin or reset.
85 QUIC_STREAM_DATA_AFTER_TERMINATION,
86 // There was some server error which halted stream processing.
87 QUIC_SERVER_ERROR_PROCESSING_STREAM,
88 // We got two fin or reset offsets which did not match.
89 QUIC_MULTIPLE_TERMINATION_OFFSETS,
90 // We got bad payload and can not respond to it at the protocol level.
91 QUIC_BAD_APPLICATION_PAYLOAD,
92
93 // Connection errors.
94
95 // Control frame is malformed.
96 QUIC_INVALID_PACKET_HEADER,
97 // Fragment data is malformed.
98 QUIC_INVALID_FRAGMENT_DATA,
99 // FEC data is malformed.
100 QUIC_INVALID_FEC_DATA,
101 // Stream rst data is malformed
102 QUIC_INVALID_RST_STREAM_DATA,
103 // Connection close data is malformed.
104 QUIC_INVALID_CONNECTION_CLOSE_DATA,
105 // Ack data is malformed.
106 QUIC_INVALID_ACK_DATA,
107 // There was an error decrypting.
108 QUIC_DECRYPTION_FAILURE,
109 // There was an error encrypting.
110 QUIC_ENCRYPTION_FAILURE,
111 // The packet exceeded kMaxPacketSize.
112 QUIC_PACKET_TOO_LARGE,
113 // Data was sent for a stream which did not exist.
114 QUIC_PACKET_FOR_NONEXISTENT_STREAM,
115 // The client is going away (browser close, etc.)
116 QUIC_CLIENT_GOING_AWAY,
117 // The server is going away (restart etc.)
118 QUIC_SERVER_GOING_AWAY,
119 // A stream ID was invalid.
120 QUIC_INVALID_STREAM_ID,
121 // Too many streams already open.
122 QUIC_TOO_MANY_OPEN_STREAMS,
123
124 // We hit our prenegotiated (or default) timeout
125 QUIC_CONNECTION_TIMED_OUT,
126
127 // Crypto errors.
128
129 // Handshake message contained out of order tags.
130 QUIC_CRYPTO_TAGS_OUT_OF_ORDER,
131 // Handshake message contained too many entires.
132 QUIC_CRYPTO_TOO_MANY_ENTRIES,
133 // Handshake message contained an invalid value length.
134 QUIC_CRYPTO_INVALID_VALUE_LENGTH,
135 // A crypto message was received after the handshake was complete.
136 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE,
137 // A crypto message was receieved with an illegal message tag.
138 QUIC_INVALID_CRYPTO_MESSAGE_TYPE,
139
140};
141
142struct NET_EXPORT_PRIVATE QuicPacketHeader {
143 // Includes the ConnectionHeader and CongestionMonitoredHeader
144 // from the design docs, as well as some elements of DecryptedData.
145 QuicGuid guid;
146 QuicPacketSequenceNumber packet_sequence_number;
147 uint8 retransmission_count;
148 QuicTransmissionTime transmission_time;
149 QuicPacketFlags flags;
150 QuicFecGroupNumber fec_group;
151};
152
153struct NET_EXPORT_PRIVATE QuicStreamFragment {
154 QuicStreamFragment();
155 QuicStreamFragment(QuicStreamId stream_id,
156 bool fin,
157 uint64 offset,
158 base::StringPiece data);
159
160 QuicStreamId stream_id;
161 bool fin;
162 uint64 offset;
163 base::StringPiece data;
164};
165
166struct NET_EXPORT_PRIVATE ReceivedPacketInfo {
167 ReceivedPacketInfo();
168 ~ReceivedPacketInfo();
169 // The highest packet sequence number we've received from the peer.
170 QuicPacketSequenceNumber largest_received;
171 // The time at which we received the above packet.
172 QuicTransmissionTime time_received;
173 // The set of packets which we're expecting and have not received.
174 // This includes any packets between the lowest and largest_received
175 // which we have neither seen nor been informed are non-retransmitting.
176 base::hash_set<QuicPacketSequenceNumber> missing_packets;
177};
178
179struct NET_EXPORT_PRIVATE SentPacketInfo {
180 SentPacketInfo();
181 ~SentPacketInfo();
182 // The lowest packet we've sent which is unacked, and we expect an ack for.
183 QuicPacketSequenceNumber least_unacked;
184 // The set of packets between least_unacked and the last packet we have sent
185 // which we will not resend.
186 base::hash_set<QuicPacketSequenceNumber> non_retransmiting;
187};
188
189// Defines for all types of congestion feedback that will be negotiated in QUIC,
190// kTCP MUST be supported by all QUIC implementations to guarentee 100%
191// compatibility.
192enum CongestionFeedbackType {
193 kNone = 0, // No feedback provided
194 kTCP, // Used to mimic TCP.
195 kInterArrival, // Use additional inter arrival information.
196 kFixRate, // Provided for testing.
197};
198
199struct NET_EXPORT_PRIVATE CongestionFeedbackMessageTCP {
200 uint16 accumulated_number_of_lost_packets;
201 uint16 receive_window; // Number of bytes >> 4.
202};
203
204struct NET_EXPORT_PRIVATE CongestionFeedbackMessageInterArrival {
205 uint16 accumulated_number_of_lost_packets;
206 int16 offset_time;
207 uint16 delta_time; // delta time is described below.
208};
209
210/*
211 * Description of delta time.
212 *
213 * 0 1
214 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
215 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
216 * |D|S| offset_time |
217 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
218 *
219 * Where:
220 * D is the time domain. If set time domain is in milliseconds, else in
221 * microseconds.
222 * S is the sign bit.
223 * offset_time is the time offset where the relative packet size is equal to
224 * 0.
225 */
226
227struct NET_EXPORT_PRIVATE CongestionFeedbackMessageFixRate {
228 uint32 bitrate_in_bytes_per_second;
229};
230
231struct NET_EXPORT_PRIVATE CongestionInfo {
232 CongestionFeedbackType type;
233 union {
234 CongestionFeedbackMessageTCP tcp;
235 CongestionFeedbackMessageInterArrival inter_arrival;
236 CongestionFeedbackMessageFixRate fix_rate;
237 };
238};
239
240struct NET_EXPORT_PRIVATE QuicAckFragment {
241 QuicAckFragment() {}
242 QuicAckFragment(QuicPacketSequenceNumber largest_received,
243 QuicTransmissionTime time_received,
244 QuicPacketSequenceNumber least_unacked) {
245 received_info.largest_received = largest_received;
246 received_info.time_received = time_received;
247 sent_info.least_unacked = least_unacked;
248 congestion_info.type = kNone;
249 }
250
251 SentPacketInfo sent_info;
252 ReceivedPacketInfo received_info;
253 CongestionInfo congestion_info;
254
255 friend std::ostream& operator<<(std::ostream& os, const QuicAckFragment& s) {
256 os << "largest_received: " << s.received_info.largest_received
257 << " time: " << s.received_info.time_received
258 << " missing: ";
259 for (base::hash_set<QuicPacketSequenceNumber>::const_iterator it =
260 s.received_info.missing_packets.begin();
261 it != s.received_info.missing_packets.end(); ++it) {
262 os << *it << " ";
263 }
264
265 os << " least_waiting: " << s.sent_info.least_unacked
266 << " no_retransmit: ";
267 for (base::hash_set<QuicPacketSequenceNumber>::const_iterator it =
268 s.sent_info.non_retransmiting.begin();
269 it != s.sent_info.non_retransmiting.end(); ++it) {
270 os << *it << " ";
271 }
272 os << "\n";
273 return os;
274 }
275};
276
277struct NET_EXPORT_PRIVATE QuicRstStreamFragment {
278 QuicRstStreamFragment() {}
279 QuicRstStreamFragment(QuicStreamId stream_id, uint64 offset,
[email protected]431bb4fd2012-10-19 17:46:09280 QuicErrorCode error_code)
281 : stream_id(stream_id), offset(offset), error_code(error_code) {
282 DCHECK_LE(error_code, std::numeric_limits<uint8>::max());
[email protected]8b37a092012-10-18 21:53:49283 }
284
285 QuicStreamId stream_id;
286 uint64 offset;
[email protected]431bb4fd2012-10-19 17:46:09287 QuicErrorCode error_code;
288 std::string error_details;
[email protected]8b37a092012-10-18 21:53:49289};
290
291struct NET_EXPORT_PRIVATE QuicConnectionCloseFragment {
[email protected]431bb4fd2012-10-19 17:46:09292 QuicErrorCode error_code;
[email protected]8b37a092012-10-18 21:53:49293 QuicAckFragment ack_fragment;
[email protected]431bb4fd2012-10-19 17:46:09294 std::string error_details;
[email protected]8b37a092012-10-18 21:53:49295};
296
297struct NET_EXPORT_PRIVATE QuicFragment {
298 QuicFragment() {}
299 explicit QuicFragment(QuicStreamFragment* stream_fragment)
300 : type(STREAM_FRAGMENT), stream_fragment(stream_fragment) {
301 }
302 explicit QuicFragment(QuicAckFragment* fragment)
303 : type(ACK_FRAGMENT), ack_fragment(fragment) {
304 }
305 explicit QuicFragment(QuicRstStreamFragment* fragment)
306 : type(RST_STREAM_FRAGMENT),
307 rst_stream_fragment(fragment) {
308 }
309 explicit QuicFragment(QuicConnectionCloseFragment* fragment)
310 : type(CONNECTION_CLOSE_FRAGMENT),
311 connection_close_fragment(fragment) {
312 }
313
314 QuicFragmentType type;
315 union {
316 QuicStreamFragment* stream_fragment;
317 QuicAckFragment* ack_fragment;
318 QuicRstStreamFragment* rst_stream_fragment;
319 QuicConnectionCloseFragment* connection_close_fragment;
320 };
321};
322
323typedef std::vector<QuicFragment> QuicFragments;
324
325struct NET_EXPORT_PRIVATE QuicFecData {
326 QuicFecData();
327 QuicFecGroupNumber fec_group;
328 QuicPacketSequenceNumber first_protected_packet_sequence_number;
329 // The last protected packet's sequence number will be one
330 // less than the sequence number of the FEC packet.
331 base::StringPiece redundancy;
332 bool operator==(const QuicFecData& other) const {
333 if (fec_group != other.fec_group) {
334 return false;
335 }
336 if (first_protected_packet_sequence_number !=
337 other.first_protected_packet_sequence_number) {
338 return false;
339 }
340 if (redundancy != other.redundancy) {
341 return false;
342 }
343 return true;
344 }
345};
346
347struct NET_EXPORT_PRIVATE QuicPacketData {
348 std::string data;
349};
350
351class NET_EXPORT_PRIVATE QuicData {
352 public:
353 QuicData(const char* buffer, size_t length)
354 : buffer_(buffer),
355 length_(length),
356 owns_buffer_(false) { }
357
358 QuicData(char* buffer, size_t length, bool owns_buffer)
359 : buffer_(buffer),
360 length_(length),
361 owns_buffer_(owns_buffer) { }
362
363 virtual ~QuicData();
364
365 base::StringPiece AsStringPiece() const {
366 return base::StringPiece(data(), length());
367 }
368
369 const char* data() const { return buffer_; }
370 size_t length() const { return length_; }
371
372 private:
373 const char* buffer_;
374 size_t length_;
375 bool owns_buffer_;
376
377 DISALLOW_COPY_AND_ASSIGN(QuicData);
378};
379
380class NET_EXPORT_PRIVATE QuicPacket : public QuicData {
381 public:
382 QuicPacket(char* buffer, size_t length, bool owns_buffer)
383 : QuicData(buffer, length, owns_buffer),
384 buffer_(buffer) { }
385
386 base::StringPiece FecProtectedData() const {
387 return base::StringPiece(data() + kStartOfFecProtectedData,
388 length() - kStartOfFecProtectedData);
389 }
390
391 base::StringPiece AssociatedData() const {
392 return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData);
393 }
394
395 base::StringPiece Plaintext() const {
396 return base::StringPiece(data() + kStartOfEncryptedData,
397 length() - kStartOfEncryptedData);
398 }
399 char* mutable_data() { return buffer_; }
400
401 private:
402 char* buffer_;
403
404 // TODO(rch): DISALLOW_COPY_AND_ASSIGN
405};
406
407class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
408 public:
409 QuicEncryptedPacket(const char* buffer, size_t length)
410 : QuicData(buffer, length) { }
411
412 QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer)
413 : QuicData(buffer, length, owns_buffer) { }
414
415 base::StringPiece AssociatedData() const {
416 return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData);
417 }
418
419 // TODO(rch): DISALLOW_COPY_AND_ASSIGN
420};
421
422} // namespace net
423
424#endif // NET_QUIC_QUIC_PROTOCOL_H_