QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2017 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_QUARTC_QUARTC_SESSION_H_ |
| 6 | #define QUICHE_QUIC_QUARTC_QUARTC_SESSION_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | |
| 11 | #include "net/third_party/quiche/src/quic/core/quic_crypto_client_stream.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/quic_crypto_server_stream.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/quic_crypto_stream.h" |
| 14 | #include "net/third_party/quiche/src/quic/core/quic_error_codes.h" |
| 15 | #include "net/third_party/quiche/src/quic/core/quic_session.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
QUICHE team | 34df585 | 2019-05-29 16:27:22 -0700 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/quic/platform/api/quic_containers.h" |
QUICHE team | ea19735 | 2019-07-16 16:54:52 -0700 | [diff] [blame] | 18 | #include "net/third_party/quiche/src/quic/platform/api/quic_mem_slice_storage.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 19 | #include "net/third_party/quiche/src/quic/quartc/quartc_packet_writer.h" |
| 20 | #include "net/third_party/quiche/src/quic/quartc/quartc_stream.h" |
dmcardle | c60e87a | 2019-12-12 09:43:19 -0800 | [diff] [blame] | 21 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 22 | |
| 23 | namespace quic { |
| 24 | |
| 25 | // QuartcSession owns and manages a QUIC connection. |
| 26 | class QuartcSession : public QuicSession, |
| 27 | public QuartcPacketTransport::Delegate { |
| 28 | public: |
| 29 | QuartcSession(std::unique_ptr<QuicConnection> connection, |
| 30 | Visitor* visitor, |
| 31 | const QuicConfig& config, |
| 32 | const ParsedQuicVersionVector& supported_versions, |
| 33 | const QuicClock* clock); |
| 34 | QuartcSession(const QuartcSession&) = delete; |
| 35 | QuartcSession& operator=(const QuartcSession&) = delete; |
| 36 | ~QuartcSession() override; |
| 37 | |
| 38 | // QuicSession overrides. |
| 39 | QuartcStream* CreateOutgoingBidirectionalStream(); |
| 40 | |
| 41 | // Sends short unreliable message using quic message frame (message must fit |
| 42 | // in one quic packet). If connection is blocked by congestion control, |
| 43 | // message will be queued and resent later after receiving an OnCanWrite |
| 44 | // notification. |
| 45 | // |
| 46 | // Message size must be <= GetLargestMessagePayload(). |
| 47 | // |
| 48 | // Supported in quic version 45 or later. |
| 49 | // |
| 50 | // Returns false and logs error if message is too long or session does not |
| 51 | // support SendMessage API. Other unexpected errors during send will not be |
| 52 | // returned, because messages can be sent later if connection is congestion |
| 53 | // controlled. |
QUICHE team | 136e785 | 2019-05-13 14:10:34 -0700 | [diff] [blame] | 54 | // |
| 55 | // |datagram_id| is used to notify when message was sent in |
| 56 | // Delegate::OnMessageSent. |
| 57 | // |
| 58 | // TODO(sukhanov): We can not use QUIC message ID for notifications, because |
| 59 | // QUIC does not take ownership of messages and if connection is congestion |
| 60 | // controlled, message is not sent and does not get message id until it is |
| 61 | // sent successfully. It also creates problem of flow control between |
| 62 | // messages and streams if they are used together. We discussed it with QUIC |
| 63 | // team and there are multiple solutions, but for now we have to use our |
| 64 | // own datagram identification. |
| 65 | bool SendOrQueueMessage(QuicMemSliceSpan message, int64_t datagram_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 66 | |
| 67 | // Returns largest message payload acceptable in SendQuartcMessage. |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 68 | QuicPacketLength GetCurrentLargestMessagePayload() const { |
| 69 | return connection()->GetCurrentLargestMessagePayload(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // Return true if transport support message frame. |
| 73 | bool CanSendMessage() const { |
renjietang | d1d0085 | 2019-09-06 10:43:12 -0700 | [diff] [blame] | 74 | return VersionSupportsMessageFrames(transport_version()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 75 | } |
| 76 | |
fayang | d58736d | 2019-11-27 13:35:31 -0800 | [diff] [blame] | 77 | void SetDefaultEncryptionLevel(EncryptionLevel level) override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 78 | |
| 79 | // QuicConnectionVisitorInterface overrides. |
| 80 | void OnCongestionWindowChange(QuicTime now) override; |
| 81 | bool ShouldKeepConnectionAlive() const override; |
| 82 | |
| 83 | void OnCanWrite() override; |
QUICHE team | 65d3e32 | 2019-04-29 14:19:54 -0700 | [diff] [blame] | 84 | bool SendProbingData() override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 85 | |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 86 | void OnConnectionClosed(const QuicConnectionCloseFrame& frame, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 87 | ConnectionCloseSource source) override; |
| 88 | |
| 89 | // QuartcSession methods. |
| 90 | virtual void StartCryptoHandshake() = 0; |
| 91 | |
| 92 | // Closes the connection with the given human-readable error details. |
| 93 | // The connection closes with the QUIC_CONNECTION_CANCELLED error code to |
| 94 | // indicate the application closed it. |
| 95 | // |
| 96 | // Informs the peer that the connection has been closed. This prevents the |
| 97 | // peer from waiting until the connection times out. |
| 98 | // |
| 99 | // Cleans up the underlying QuicConnection's state. Closing the connection |
| 100 | // makes it safe to delete the QuartcSession. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 101 | void CloseConnection(const std::string& details); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 102 | |
| 103 | // If the given stream is still open, sends a reset frame to cancel it. |
| 104 | // Note: This method cancels a stream by QuicStreamId rather than by pointer |
| 105 | // (or by a method on QuartcStream) because QuartcSession (and not |
| 106 | // the caller) owns the streams. Streams may finish and be deleted before the |
| 107 | // caller tries to cancel them, rendering the caller's pointers invalid. |
| 108 | void CancelStream(QuicStreamId stream_id); |
| 109 | |
| 110 | // Callbacks called by the QuartcSession to notify the user of the |
| 111 | // QuartcSession of certain events. |
| 112 | class Delegate { |
| 113 | public: |
| 114 | virtual ~Delegate() {} |
| 115 | |
| 116 | // Called when the crypto handshake is complete. Crypto handshake on the |
| 117 | // client is only completed _after_ SHLO is received, but we can actually |
| 118 | // start sending media data right after CHLO is sent. |
| 119 | virtual void OnCryptoHandshakeComplete() = 0; |
| 120 | |
| 121 | // Connection can be writable even before crypto handshake is complete. |
| 122 | // In particular, on the client, we can start sending data after sending |
| 123 | // full CHLO, without waiting for SHLO. This reduces a send delay by 1-rtt. |
| 124 | // |
| 125 | // This may be called multiple times. |
| 126 | virtual void OnConnectionWritable() = 0; |
| 127 | |
| 128 | // Called when a new stream is received from the remote endpoint. |
| 129 | virtual void OnIncomingStream(QuartcStream* stream) = 0; |
| 130 | |
| 131 | // Called when network parameters change in response to an ack frame. |
| 132 | virtual void OnCongestionControlChange(QuicBandwidth bandwidth_estimate, |
| 133 | QuicBandwidth pacing_rate, |
| 134 | QuicTime::Delta latest_rtt) = 0; |
| 135 | |
| 136 | // Called when the connection is closed. This means all of the streams will |
| 137 | // be closed and no new streams can be created. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 138 | virtual void OnConnectionClosed(const QuicConnectionCloseFrame& frame, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 139 | ConnectionCloseSource source) = 0; |
| 140 | |
| 141 | // Called when message (sent as SendMessage) is received. |
dmcardle | c60e87a | 2019-12-12 09:43:19 -0800 | [diff] [blame] | 142 | virtual void OnMessageReceived(quiche::QuicheStringPiece message) = 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 143 | |
QUICHE team | 136e785 | 2019-05-13 14:10:34 -0700 | [diff] [blame] | 144 | // Called when message is sent to QUIC. |
| 145 | // |
| 146 | // Takes into account delay due to congestion control, but does not take |
| 147 | // into account any additional socket delays. |
| 148 | // |
| 149 | // Passed |datagram_id| is the same used in SendOrQueueMessage. |
| 150 | // |
| 151 | // TODO(sukhanov): We can take into account socket delay, but it's not clear |
| 152 | // if it's worth doing if we eventually plan to move congestion control to |
| 153 | // QUIC in QRTP model. If we need to do it, mellem@ thinks it's fairly |
| 154 | // strtaightforward: QUIC does not know about socket delay, but ICE does. We |
| 155 | // can tell ICE the QUIC packet number for each packet sent, and it will |
| 156 | // echo it back to us when the packet actually goes out. We just need to |
| 157 | // plumb that signal up to RTP's congestion control. |
| 158 | virtual void OnMessageSent(int64_t datagram_id) = 0; |
| 159 | |
QUICHE team | 68d15a8 | 2019-05-31 15:27:25 -0700 | [diff] [blame] | 160 | // Called when message with |datagram_id| gets acked. |receive_timestamp| |
| 161 | // indicates when the peer received this message, according to its own |
| 162 | // clock. |
| 163 | virtual void OnMessageAcked(int64_t datagram_id, |
| 164 | QuicTime receive_timestamp) = 0; |
| 165 | |
| 166 | // Called when message with |datagram_id| is lost. |
| 167 | virtual void OnMessageLost(int64_t datagram_id) = 0; |
QUICHE team | 34df585 | 2019-05-29 16:27:22 -0700 | [diff] [blame] | 168 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 169 | // TODO(zhihuang): Add proof verification. |
| 170 | }; |
| 171 | |
| 172 | // The |delegate| is not owned by QuartcSession. |
| 173 | void SetDelegate(Delegate* session_delegate); |
| 174 | |
| 175 | // Called when CanWrite() changes from false to true. |
| 176 | void OnTransportCanWrite() override; |
| 177 | |
| 178 | // Called when a packet has been received and should be handled by the |
| 179 | // QuicConnection. |
| 180 | void OnTransportReceived(const char* data, size_t data_len) override; |
| 181 | |
dmcardle | c60e87a | 2019-12-12 09:43:19 -0800 | [diff] [blame] | 182 | void OnMessageReceived(quiche::QuicheStringPiece message) override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 183 | |
QUICHE team | 34df585 | 2019-05-29 16:27:22 -0700 | [diff] [blame] | 184 | // Called when message with |message_id| gets acked. |
QUICHE team | 9467db0 | 2019-05-30 09:38:45 -0700 | [diff] [blame] | 185 | void OnMessageAcked(QuicMessageId message_id, |
| 186 | QuicTime receive_timestamp) override; |
QUICHE team | 34df585 | 2019-05-29 16:27:22 -0700 | [diff] [blame] | 187 | |
QUICHE team | 68d15a8 | 2019-05-31 15:27:25 -0700 | [diff] [blame] | 188 | void OnMessageLost(QuicMessageId message_id) override; |
| 189 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 190 | // Returns number of queued (not sent) messages submitted by |
| 191 | // SendOrQueueMessage. Messages are queued if connection is congestion |
| 192 | // controlled. |
| 193 | size_t send_message_queue_size() const { return send_message_queue_.size(); } |
| 194 | |
| 195 | protected: |
| 196 | // QuicSession override. |
| 197 | QuicStream* CreateIncomingStream(QuicStreamId id) override; |
renjietang | baea59c | 2019-05-29 15:08:14 -0700 | [diff] [blame] | 198 | QuicStream* CreateIncomingStream(PendingStream* pending) override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 199 | |
| 200 | std::unique_ptr<QuartcStream> CreateDataStream(QuicStreamId id, |
| 201 | spdy::SpdyPriority priority); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 202 | // Activates a QuartcStream. The session takes ownership of the stream, but |
| 203 | // returns an unowned pointer to the stream for convenience. |
| 204 | QuartcStream* ActivateDataStream(std::unique_ptr<QuartcStream> stream); |
| 205 | |
| 206 | void ResetStream(QuicStreamId stream_id, QuicRstStreamErrorCode error); |
| 207 | |
| 208 | const QuicClock* clock() { return clock_; } |
| 209 | |
| 210 | private: |
| 211 | std::unique_ptr<QuartcStream> InitializeDataStream( |
| 212 | std::unique_ptr<QuartcStream> stream, |
| 213 | spdy::SpdyPriority priority); |
| 214 | |
QUICHE team | 136e785 | 2019-05-13 14:10:34 -0700 | [diff] [blame] | 215 | // Holds message until it's sent. |
| 216 | struct QueuedMessage { |
QUICHE team | ea19735 | 2019-07-16 16:54:52 -0700 | [diff] [blame] | 217 | QueuedMessage() : message(nullptr, 0, nullptr, 0), datagram_id(0) {} |
| 218 | |
| 219 | QuicMemSliceStorage message; |
QUICHE team | 136e785 | 2019-05-13 14:10:34 -0700 | [diff] [blame] | 220 | int64_t datagram_id; |
| 221 | }; |
| 222 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 223 | void ProcessSendMessageQueue(); |
| 224 | |
| 225 | // Take ownership of the QuicConnection. Note: if |connection_| changes, |
| 226 | // the new value of |connection_| must be given to |packet_writer_| before any |
| 227 | // packets are written. Otherwise, |packet_writer_| will crash. |
| 228 | std::unique_ptr<QuicConnection> connection_; |
| 229 | |
| 230 | // For recording packet receipt time |
| 231 | const QuicClock* clock_; |
| 232 | |
| 233 | // Not owned by QuartcSession. |
| 234 | Delegate* session_delegate_ = nullptr; |
| 235 | |
| 236 | // Options passed to the packet writer for each packet. |
| 237 | std::unique_ptr<QuartcPerPacketOptions> per_packet_options_; |
| 238 | |
| 239 | // Queue of pending messages sent by SendQuartcMessage that were not sent |
| 240 | // yet or blocked by congestion control. Messages are queued in the order |
| 241 | // of sent by SendOrQueueMessage(). |
wub | a750aab | 2020-02-10 06:43:15 -0800 | [diff] [blame] | 242 | QuicCircularDeque<QueuedMessage> send_message_queue_; |
QUICHE team | 34df585 | 2019-05-29 16:27:22 -0700 | [diff] [blame] | 243 | |
| 244 | // Maps message ids to datagram ids, so we could translate message ACKs |
| 245 | // received from QUIC to datagram ACKs that are propagated up the stack. |
| 246 | QuicUnorderedMap<QuicMessageId, int64_t> message_to_datagram_id_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | class QuartcClientSession : public QuartcSession, |
| 250 | public QuicCryptoClientStream::ProofHandler { |
| 251 | public: |
| 252 | QuartcClientSession( |
| 253 | std::unique_ptr<QuicConnection> connection, |
| 254 | const QuicConfig& config, |
| 255 | const ParsedQuicVersionVector& supported_versions, |
| 256 | const QuicClock* clock, |
| 257 | std::unique_ptr<QuartcPacketWriter> packet_writer, |
| 258 | std::unique_ptr<QuicCryptoClientConfig> client_crypto_config, |
dmcardle | c60e87a | 2019-12-12 09:43:19 -0800 | [diff] [blame] | 259 | quiche::QuicheStringPiece server_crypto_config); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 260 | QuartcClientSession(const QuartcClientSession&) = delete; |
| 261 | QuartcClientSession& operator=(const QuartcClientSession&) = delete; |
| 262 | |
| 263 | ~QuartcClientSession() override; |
| 264 | |
| 265 | // Initialize should not be called on a QuartcSession. Instead, call |
| 266 | // StartCryptoHandshake(). |
| 267 | // TODO(mellem): Move creation of the crypto stream into Initialize() and |
| 268 | // remove StartCryptoHandshake() to bring QuartcSession in line with other |
| 269 | // implementations of QuicSession, which can be started by calling |
| 270 | // Initialize(). |
| 271 | void Initialize() override; |
| 272 | |
| 273 | // Accessors for the client crypto stream. |
| 274 | QuicCryptoStream* GetMutableCryptoStream() override; |
| 275 | const QuicCryptoStream* GetCryptoStream() const override; |
| 276 | |
| 277 | // Initializes the session and sends a handshake. |
| 278 | void StartCryptoHandshake() override; |
| 279 | |
| 280 | // ProofHandler overrides. |
| 281 | void OnProofValid(const QuicCryptoClientConfig::CachedState& cached) override; |
| 282 | |
| 283 | // Called by the client crypto handshake when proof verification details |
| 284 | // become available, either because proof verification is complete, or when |
| 285 | // cached details are used. |
| 286 | void OnProofVerifyDetailsAvailable( |
| 287 | const ProofVerifyDetails& verify_details) override; |
| 288 | |
| 289 | private: |
| 290 | // Packet writer used by |connection_|. |
| 291 | std::unique_ptr<QuartcPacketWriter> packet_writer_; |
| 292 | |
| 293 | // Config for QUIC crypto stream. |
| 294 | std::unique_ptr<QuicCryptoClientConfig> client_crypto_config_; |
| 295 | |
| 296 | // Client perspective crypto stream. |
| 297 | std::unique_ptr<QuicCryptoClientStream> crypto_stream_; |
| 298 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 299 | const std::string server_config_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | class QuartcServerSession : public QuartcSession { |
| 303 | public: |
| 304 | QuartcServerSession(std::unique_ptr<QuicConnection> connection, |
| 305 | Visitor* visitor, |
| 306 | const QuicConfig& config, |
| 307 | const ParsedQuicVersionVector& supported_versions, |
| 308 | const QuicClock* clock, |
| 309 | const QuicCryptoServerConfig* server_crypto_config, |
| 310 | QuicCompressedCertsCache* const compressed_certs_cache, |
nharper | 5f23a2d | 2020-02-20 10:44:09 -0800 | [diff] [blame] | 311 | QuicCryptoServerStreamBase::Helper* const stream_helper); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 312 | QuartcServerSession(const QuartcServerSession&) = delete; |
| 313 | QuartcServerSession& operator=(const QuartcServerSession&) = delete; |
| 314 | |
| 315 | // Accessors for the server crypto stream. |
| 316 | QuicCryptoStream* GetMutableCryptoStream() override; |
| 317 | const QuicCryptoStream* GetCryptoStream() const override; |
| 318 | |
| 319 | // Initializes the session and prepares to receive a handshake. |
| 320 | void StartCryptoHandshake() override; |
| 321 | |
| 322 | private: |
| 323 | // Config for QUIC crypto stream. |
| 324 | const QuicCryptoServerConfig* server_crypto_config_; |
| 325 | |
| 326 | // Used by QUIC crypto server stream to track most recently compressed certs. |
| 327 | QuicCompressedCertsCache* const compressed_certs_cache_; |
| 328 | |
| 329 | // This helper is needed to create QuicCryptoServerStream. |
nharper | 5f23a2d | 2020-02-20 10:44:09 -0800 | [diff] [blame] | 330 | QuicCryptoServerStreamBase::Helper* const stream_helper_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 331 | |
| 332 | // Server perspective crypto stream. |
nharper | 23d4074 | 2020-01-03 14:55:01 -0800 | [diff] [blame] | 333 | std::unique_ptr<QuicCryptoServerStreamBase> crypto_stream_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | } // namespace quic |
| 337 | |
| 338 | #endif // QUICHE_QUIC_QUARTC_QUARTC_SESSION_H_ |