OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_QUIC_QUIC_PROTOCOL_H_ | 5 #ifndef NET_QUIC_QUIC_PROTOCOL_H_ |
6 #define NET_QUIC_QUIC_PROTOCOL_H_ | 6 #define NET_QUIC_QUIC_PROTOCOL_H_ |
7 | 7 |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <array> | |
12 #include <limits> | 8 #include <limits> |
13 #include <list> | 9 #include <list> |
14 #include <map> | |
15 #include <memory> | 10 #include <memory> |
16 #include <ostream> | 11 #include <ostream> |
17 #include <set> | |
18 #include <string> | 12 #include <string> |
19 #include <utility> | 13 #include <utility> |
20 #include <vector> | 14 #include <vector> |
21 | 15 |
22 #include "base/logging.h" | 16 #include "base/logging.h" |
23 #include "base/macros.h" | 17 #include "base/macros.h" |
24 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
25 #include "base/strings/string_piece.h" | 19 #include "base/strings/string_piece.h" |
26 #include "net/base/int128.h" | 20 #include "net/base/int128.h" |
27 #include "net/base/iovec.h" | 21 #include "net/base/iovec.h" |
28 #include "net/base/net_export.h" | 22 #include "net/base/net_export.h" |
29 #include "net/quic/core/interval_set.h" | |
30 #include "net/quic/core/quic_bandwidth.h" | 23 #include "net/quic/core/quic_bandwidth.h" |
31 #include "net/quic/core/quic_buffer_allocator.h" | |
32 #include "net/quic/core/quic_constants.h" | 24 #include "net/quic/core/quic_constants.h" |
33 #include "net/quic/core/quic_error_codes.h" | 25 #include "net/quic/core/quic_error_codes.h" |
| 26 #include "net/quic/core/quic_frames.h" |
34 #include "net/quic/core/quic_time.h" | 27 #include "net/quic/core/quic_time.h" |
35 #include "net/quic/core/quic_types.h" | 28 #include "net/quic/core/quic_types.h" |
36 #include "net/quic/core/quic_versions.h" | 29 #include "net/quic/core/quic_versions.h" |
37 #include "net/quic/platform/api/quic_socket_address.h" | 30 #include "net/quic/platform/api/quic_socket_address.h" |
38 | 31 |
39 namespace net { | 32 namespace net { |
40 | 33 |
41 class QuicPacket; | 34 class QuicPacket; |
42 struct QuicPacketHeader; | 35 struct QuicPacketHeader; |
43 | 36 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 QuicPacketPublicHeader public_header; | 98 QuicPacketPublicHeader public_header; |
106 QuicPublicResetNonceProof nonce_proof; | 99 QuicPublicResetNonceProof nonce_proof; |
107 // TODO(fayang): remove rejected_packet_number when deprecating | 100 // TODO(fayang): remove rejected_packet_number when deprecating |
108 // FLAGS_quic_remove_packet_number_from_public_reset. | 101 // FLAGS_quic_remove_packet_number_from_public_reset. |
109 QuicPacketNumber rejected_packet_number; | 102 QuicPacketNumber rejected_packet_number; |
110 QuicSocketAddress client_address; | 103 QuicSocketAddress client_address; |
111 }; | 104 }; |
112 | 105 |
113 typedef QuicPacketPublicHeader QuicVersionNegotiationPacket; | 106 typedef QuicPacketPublicHeader QuicVersionNegotiationPacket; |
114 | 107 |
115 // A padding frame contains no payload. | |
116 struct NET_EXPORT_PRIVATE QuicPaddingFrame { | |
117 QuicPaddingFrame() : num_padding_bytes(-1) {} | |
118 explicit QuicPaddingFrame(int num_padding_bytes) | |
119 : num_padding_bytes(num_padding_bytes) {} | |
120 | |
121 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
122 const QuicPaddingFrame& s); | |
123 | |
124 // -1: full padding to the end of a max-sized packet | |
125 // otherwise: only pad up to num_padding_bytes bytes | |
126 int num_padding_bytes; | |
127 }; | |
128 | |
129 // A ping frame contains no payload, though it is retransmittable, | |
130 // and ACK'd just like other normal frames. | |
131 struct NET_EXPORT_PRIVATE QuicPingFrame {}; | |
132 | |
133 // A path MTU discovery frame contains no payload and is serialized as a ping | |
134 // frame. | |
135 struct NET_EXPORT_PRIVATE QuicMtuDiscoveryFrame {}; | |
136 | |
137 // Deleter for stream buffers. Copyable to support platforms where the deleter | |
138 // of a unique_ptr must be copyable. Otherwise it would be nice for this to be | |
139 // move-only. | |
140 class NET_EXPORT_PRIVATE StreamBufferDeleter { | |
141 public: | |
142 StreamBufferDeleter() : allocator_(nullptr) {} | |
143 explicit StreamBufferDeleter(QuicBufferAllocator* allocator) | |
144 : allocator_(allocator) {} | |
145 | |
146 // Deletes |buffer| using |allocator_|. | |
147 void operator()(char* buffer) const; | |
148 | |
149 private: | |
150 // Not owned; must be valid so long as the buffer stored in the unique_ptr | |
151 // that owns |this| is valid. | |
152 QuicBufferAllocator* allocator_; | |
153 }; | |
154 | |
155 using UniqueStreamBuffer = std::unique_ptr<char[], StreamBufferDeleter>; | |
156 | |
157 // Allocates memory of size |size| using |allocator| for a QUIC stream buffer. | |
158 NET_EXPORT_PRIVATE UniqueStreamBuffer | |
159 NewStreamBuffer(QuicBufferAllocator* allocator, size_t size); | |
160 | |
161 struct NET_EXPORT_PRIVATE QuicStreamFrame { | |
162 QuicStreamFrame(); | |
163 QuicStreamFrame(QuicStreamId stream_id, | |
164 bool fin, | |
165 QuicStreamOffset offset, | |
166 base::StringPiece data); | |
167 QuicStreamFrame(QuicStreamId stream_id, | |
168 bool fin, | |
169 QuicStreamOffset offset, | |
170 QuicPacketLength data_length, | |
171 UniqueStreamBuffer buffer); | |
172 ~QuicStreamFrame(); | |
173 | |
174 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
175 const QuicStreamFrame& s); | |
176 | |
177 QuicStreamId stream_id; | |
178 bool fin; | |
179 QuicPacketLength data_length; | |
180 const char* data_buffer; | |
181 QuicStreamOffset offset; // Location of this data in the stream. | |
182 // nullptr when the QuicStreamFrame is received, and non-null when sent. | |
183 UniqueStreamBuffer buffer; | |
184 | |
185 private: | |
186 QuicStreamFrame(QuicStreamId stream_id, | |
187 bool fin, | |
188 QuicStreamOffset offset, | |
189 const char* data_buffer, | |
190 QuicPacketLength data_length, | |
191 UniqueStreamBuffer buffer); | |
192 | |
193 DISALLOW_COPY_AND_ASSIGN(QuicStreamFrame); | |
194 }; | |
195 static_assert(sizeof(QuicStreamFrame) <= 64, | |
196 "Keep the QuicStreamFrame size to a cacheline."); | |
197 | |
198 typedef std::vector<std::pair<QuicPacketNumber, QuicTime>> PacketTimeVector; | 108 typedef std::vector<std::pair<QuicPacketNumber, QuicTime>> PacketTimeVector; |
199 | 109 |
200 struct NET_EXPORT_PRIVATE QuicStopWaitingFrame { | |
201 QuicStopWaitingFrame(); | |
202 ~QuicStopWaitingFrame(); | |
203 | |
204 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
205 std::ostream& os, | |
206 const QuicStopWaitingFrame& s); | |
207 // Path which this stop waiting frame belongs to. | |
208 QuicPathId path_id; | |
209 // The lowest packet we've sent which is unacked, and we expect an ack for. | |
210 QuicPacketNumber least_unacked; | |
211 }; | |
212 | |
213 // A sequence of packet numbers where each number is unique. Intended to be used | |
214 // in a sliding window fashion, where smaller old packet numbers are removed and | |
215 // larger new packet numbers are added, with the occasional random access. | |
216 class NET_EXPORT_PRIVATE PacketNumberQueue { | |
217 public: | |
218 using const_iterator = IntervalSet<QuicPacketNumber>::const_iterator; | |
219 using const_reverse_iterator = | |
220 IntervalSet<QuicPacketNumber>::const_reverse_iterator; | |
221 | |
222 PacketNumberQueue(); | |
223 PacketNumberQueue(const PacketNumberQueue& other); | |
224 // TODO(rtenneti): on windows RValue reference gives errors. | |
225 // PacketNumberQueue(PacketNumberQueue&& other); | |
226 ~PacketNumberQueue(); | |
227 | |
228 PacketNumberQueue& operator=(const PacketNumberQueue& other); | |
229 // PacketNumberQueue& operator=(PacketNumberQueue&& other); | |
230 | |
231 // Adds |packet_number| to the set of packets in the queue. | |
232 void Add(QuicPacketNumber packet_number); | |
233 | |
234 // Adds packets between [lower, higher) to the set of packets in the queue. It | |
235 // is undefined behavior to call this with |higher| < |lower|. | |
236 void Add(QuicPacketNumber lower, QuicPacketNumber higher); | |
237 | |
238 // Removes |packet_number| from the set of packets in the queue. | |
239 void Remove(QuicPacketNumber packet_number); | |
240 | |
241 // Removes packets numbers between [lower, higher) to the set of packets in | |
242 // the queue. It is undefined behavior to call this with |higher| < |lower|. | |
243 void Remove(QuicPacketNumber lower, QuicPacketNumber higher); | |
244 | |
245 // Removes packets with values less than |higher| from the set of packets in | |
246 // the queue. Returns true if packets were removed. | |
247 bool RemoveUpTo(QuicPacketNumber higher); | |
248 | |
249 // Mutates packet number set so that it contains only those packet numbers | |
250 // from minimum to maximum packet number not currently in the set. Do nothing | |
251 // if packet number set is empty. | |
252 void Complement(); | |
253 | |
254 // Returns true if the queue contains |packet_number|. | |
255 bool Contains(QuicPacketNumber packet_number) const; | |
256 | |
257 // Returns true if the queue is empty. | |
258 bool Empty() const; | |
259 | |
260 // Returns the minimum packet number stored in the queue. It is undefined | |
261 // behavior to call this if the queue is empty. | |
262 QuicPacketNumber Min() const; | |
263 | |
264 // Returns the maximum packet number stored in the queue. It is undefined | |
265 // behavior to call this if the queue is empty. | |
266 QuicPacketNumber Max() const; | |
267 | |
268 // Returns the number of unique packets stored in the queue. Inefficient; only | |
269 // exposed for testing. | |
270 size_t NumPacketsSlow() const; | |
271 | |
272 // Returns the number of disjoint packet number intervals contained in the | |
273 // queue. | |
274 size_t NumIntervals() const; | |
275 | |
276 // Returns the length of last interval. | |
277 QuicPacketNumber LastIntervalLength() const; | |
278 | |
279 // Returns iterators over the packet number intervals. | |
280 const_iterator begin() const; | |
281 const_iterator end() const; | |
282 const_reverse_iterator rbegin() const; | |
283 const_reverse_iterator rend() const; | |
284 const_iterator lower_bound(QuicPacketNumber packet_number) const; | |
285 | |
286 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
287 std::ostream& os, | |
288 const PacketNumberQueue& q); | |
289 | |
290 private: | |
291 IntervalSet<QuicPacketNumber> packet_number_intervals_; | |
292 }; | |
293 | |
294 struct NET_EXPORT_PRIVATE QuicAckFrame { | |
295 QuicAckFrame(); | |
296 QuicAckFrame(const QuicAckFrame& other); | |
297 ~QuicAckFrame(); | |
298 | |
299 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
300 const QuicAckFrame& s); | |
301 | |
302 // The highest packet number we've observed from the peer. | |
303 QuicPacketNumber largest_observed; | |
304 | |
305 // Time elapsed since largest_observed was received until this Ack frame was | |
306 // sent. | |
307 QuicTime::Delta ack_delay_time; | |
308 | |
309 // Vector of <packet_number, time> for when packets arrived. | |
310 PacketTimeVector received_packet_times; | |
311 | |
312 // Set of packets. | |
313 PacketNumberQueue packets; | |
314 | |
315 // Path which this ack belongs to. | |
316 QuicPathId path_id; | |
317 }; | |
318 | |
319 // True if the packet number is greater than largest_observed or is listed | |
320 // as missing. | |
321 // Always returns false for packet numbers less than least_unacked. | |
322 bool NET_EXPORT_PRIVATE | |
323 IsAwaitingPacket(const QuicAckFrame& ack_frame, | |
324 QuicPacketNumber packet_number, | |
325 QuicPacketNumber peer_least_packet_awaiting_ack); | |
326 | |
327 struct NET_EXPORT_PRIVATE QuicRstStreamFrame { | |
328 QuicRstStreamFrame(); | |
329 QuicRstStreamFrame(QuicStreamId stream_id, | |
330 QuicRstStreamErrorCode error_code, | |
331 QuicStreamOffset bytes_written); | |
332 | |
333 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
334 std::ostream& os, | |
335 const QuicRstStreamFrame& r); | |
336 | |
337 QuicStreamId stream_id; | |
338 QuicRstStreamErrorCode error_code; | |
339 | |
340 // Used to update flow control windows. On termination of a stream, both | |
341 // endpoints must inform the peer of the number of bytes they have sent on | |
342 // that stream. This can be done through normal termination (data packet with | |
343 // FIN) or through a RST. | |
344 QuicStreamOffset byte_offset; | |
345 }; | |
346 | |
347 struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame { | |
348 QuicConnectionCloseFrame(); | |
349 | |
350 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
351 std::ostream& os, | |
352 const QuicConnectionCloseFrame& c); | |
353 | |
354 QuicErrorCode error_code; | |
355 std::string error_details; | |
356 }; | |
357 | |
358 struct NET_EXPORT_PRIVATE QuicGoAwayFrame { | |
359 QuicGoAwayFrame(); | |
360 QuicGoAwayFrame(QuicErrorCode error_code, | |
361 QuicStreamId last_good_stream_id, | |
362 const std::string& reason); | |
363 | |
364 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
365 const QuicGoAwayFrame& g); | |
366 | |
367 QuicErrorCode error_code; | |
368 QuicStreamId last_good_stream_id; | |
369 std::string reason_phrase; | |
370 }; | |
371 | |
372 // Flow control updates per-stream and at the connection levoel. | |
373 // Based on SPDY's WINDOW_UPDATE frame, but uses an absolute byte offset rather | |
374 // than a window delta. | |
375 // TODO(rjshade): A possible future optimization is to make stream_id and | |
376 // byte_offset variable length, similar to stream frames. | |
377 struct NET_EXPORT_PRIVATE QuicWindowUpdateFrame { | |
378 QuicWindowUpdateFrame() {} | |
379 QuicWindowUpdateFrame(QuicStreamId stream_id, QuicStreamOffset byte_offset); | |
380 | |
381 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
382 std::ostream& os, | |
383 const QuicWindowUpdateFrame& w); | |
384 | |
385 // The stream this frame applies to. 0 is a special case meaning the overall | |
386 // connection rather than a specific stream. | |
387 QuicStreamId stream_id; | |
388 | |
389 // Byte offset in the stream or connection. The receiver of this frame must | |
390 // not send data which would result in this offset being exceeded. | |
391 QuicStreamOffset byte_offset; | |
392 }; | |
393 | |
394 // The BLOCKED frame is used to indicate to the remote endpoint that this | |
395 // endpoint believes itself to be flow-control blocked but otherwise ready to | |
396 // send data. The BLOCKED frame is purely advisory and optional. | |
397 // Based on SPDY's BLOCKED frame (undocumented as of 2014-01-28). | |
398 struct NET_EXPORT_PRIVATE QuicBlockedFrame { | |
399 QuicBlockedFrame() {} | |
400 explicit QuicBlockedFrame(QuicStreamId stream_id); | |
401 | |
402 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
403 const QuicBlockedFrame& b); | |
404 | |
405 // The stream this frame applies to. 0 is a special case meaning the overall | |
406 // connection rather than a specific stream. | |
407 QuicStreamId stream_id; | |
408 }; | |
409 | |
410 // The PATH_CLOSE frame is used to explicitly close a path. Both endpoints can | |
411 // send a PATH_CLOSE frame to initiate a path termination. A path is considered | |
412 // to be closed either a PATH_CLOSE frame is sent or received. An endpoint drops | |
413 // receive side of a closed path, and packets with retransmittable frames on a | |
414 // closed path are marked as retransmissions which will be transmitted on other | |
415 // paths. | |
416 struct NET_EXPORT_PRIVATE QuicPathCloseFrame { | |
417 QuicPathCloseFrame() {} | |
418 explicit QuicPathCloseFrame(QuicPathId path_id); | |
419 | |
420 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
421 std::ostream& os, | |
422 const QuicPathCloseFrame& p); | |
423 | |
424 QuicPathId path_id; | |
425 }; | |
426 | |
427 struct NET_EXPORT_PRIVATE QuicFrame { | |
428 QuicFrame(); | |
429 explicit QuicFrame(QuicPaddingFrame padding_frame); | |
430 explicit QuicFrame(QuicMtuDiscoveryFrame frame); | |
431 explicit QuicFrame(QuicPingFrame frame); | |
432 | |
433 explicit QuicFrame(QuicStreamFrame* stream_frame); | |
434 explicit QuicFrame(QuicAckFrame* frame); | |
435 explicit QuicFrame(QuicRstStreamFrame* frame); | |
436 explicit QuicFrame(QuicConnectionCloseFrame* frame); | |
437 explicit QuicFrame(QuicStopWaitingFrame* frame); | |
438 explicit QuicFrame(QuicGoAwayFrame* frame); | |
439 explicit QuicFrame(QuicWindowUpdateFrame* frame); | |
440 explicit QuicFrame(QuicBlockedFrame* frame); | |
441 explicit QuicFrame(QuicPathCloseFrame* frame); | |
442 | |
443 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
444 const QuicFrame& frame); | |
445 | |
446 QuicFrameType type; | |
447 union { | |
448 // Frames smaller than a pointer are inline. | |
449 QuicPaddingFrame padding_frame; | |
450 QuicMtuDiscoveryFrame mtu_discovery_frame; | |
451 QuicPingFrame ping_frame; | |
452 | |
453 // Frames larger than a pointer. | |
454 QuicStreamFrame* stream_frame; | |
455 QuicAckFrame* ack_frame; | |
456 QuicStopWaitingFrame* stop_waiting_frame; | |
457 QuicRstStreamFrame* rst_stream_frame; | |
458 QuicConnectionCloseFrame* connection_close_frame; | |
459 QuicGoAwayFrame* goaway_frame; | |
460 QuicWindowUpdateFrame* window_update_frame; | |
461 QuicBlockedFrame* blocked_frame; | |
462 QuicPathCloseFrame* path_close_frame; | |
463 }; | |
464 }; | |
465 // QuicFrameType consumes 8 bytes with padding. | |
466 static_assert(sizeof(QuicFrame) <= 16, | |
467 "Frames larger than 8 bytes should be referenced by pointer."); | |
468 | |
469 typedef std::vector<QuicFrame> QuicFrames; | |
470 | |
471 // Deletes all the sub-frames contained in |frames|. | |
472 NET_EXPORT_PRIVATE void DeleteFrames(QuicFrames* frames); | |
473 | |
474 // Deletes all the QuicStreamFrames for the specified |stream_id|. | |
475 NET_EXPORT_PRIVATE void RemoveFramesForStream(QuicFrames* frames, | |
476 QuicStreamId stream_id); | |
477 | |
478 class NET_EXPORT_PRIVATE QuicData { | 110 class NET_EXPORT_PRIVATE QuicData { |
479 public: | 111 public: |
480 QuicData(const char* buffer, size_t length); | 112 QuicData(const char* buffer, size_t length); |
481 QuicData(const char* buffer, size_t length, bool owns_buffer); | 113 QuicData(const char* buffer, size_t length, bool owns_buffer); |
482 virtual ~QuicData(); | 114 virtual ~QuicData(); |
483 | 115 |
484 base::StringPiece AsStringPiece() const { | 116 base::StringPiece AsStringPiece() const { |
485 return base::StringPiece(data(), length()); | 117 return base::StringPiece(data(), length()); |
486 } | 118 } |
487 | 119 |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
727 QuicPathId path_id; | 359 QuicPathId path_id; |
728 bool has_crypto_handshake; | 360 bool has_crypto_handshake; |
729 int num_padding_bytes; | 361 int num_padding_bytes; |
730 EncryptionLevel encryption_level; | 362 EncryptionLevel encryption_level; |
731 QuicPacketNumberLength packet_number_length; | 363 QuicPacketNumberLength packet_number_length; |
732 }; | 364 }; |
733 | 365 |
734 } // namespace net | 366 } // namespace net |
735 | 367 |
736 #endif // NET_QUIC_QUIC_PROTOCOL_H_ | 368 #endif // NET_QUIC_QUIC_PROTOCOL_H_ |
OLD | NEW |