Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/http/http_server_properties_cache.h" 4 : 5 : #include "source/common/quic/envoy_quic_client_connection.h" 6 : #include "source/common/quic/envoy_quic_client_crypto_stream_factory.h" 7 : #include "source/common/quic/envoy_quic_client_stream.h" 8 : #include "source/common/quic/quic_client_transport_socket_factory.h" 9 : #include "source/common/quic/quic_filter_manager_connection_impl.h" 10 : #include "source/common/quic/quic_stat_names.h" 11 : 12 : #include "quiche/quic/core/http/quic_spdy_client_session.h" 13 : 14 : namespace Envoy { 15 : namespace Quic { 16 : 17 : // Act as a Network::ClientConnection to ClientCodec. 18 : // TODO(danzh) This class doesn't need to inherit Network::FilterManager 19 : // interface but need all other Network::Connection implementation in 20 : // QuicFilterManagerConnectionImpl. Refactor QuicFilterManagerConnectionImpl to 21 : // move FilterManager interface to EnvoyQuicServerSession. 22 : class EnvoyQuicClientSession : public QuicFilterManagerConnectionImpl, 23 : public quic::QuicSpdyClientSession, 24 : public Network::ClientConnection, 25 : public PacketsToReadDelegate { 26 : public: 27 : EnvoyQuicClientSession( 28 : const quic::QuicConfig& config, const quic::ParsedQuicVersionVector& supported_versions, 29 : std::unique_ptr<EnvoyQuicClientConnection> connection, const quic::QuicServerId& server_id, 30 : std::shared_ptr<quic::QuicCryptoClientConfig> crypto_config, Event::Dispatcher& dispatcher, 31 : uint32_t send_buffer_limit, 32 : EnvoyQuicCryptoClientStreamFactoryInterface& crypto_stream_factory, 33 : QuicStatNames& quic_stat_names, OptRef<Http::HttpServerPropertiesCache> rtt_cache, 34 : Stats::Scope& scope, 35 : const Network::TransportSocketOptionsConstSharedPtr& transport_socket_options, 36 : OptRef<Network::UpstreamTransportSocketFactory> transport_socket_factory); 37 : 38 : ~EnvoyQuicClientSession() override; 39 : 40 : // Called by QuicHttpClientConnectionImpl before creating data streams. 41 0 : void setHttpConnectionCallbacks(Http::ConnectionCallbacks& callbacks) { 42 0 : http_connection_callbacks_ = &callbacks; 43 0 : } 44 : 45 : // Network::Connection 46 : absl::string_view requestedServerName() const override; 47 0 : void dumpState(std::ostream&, int) const override { 48 0 : // TODO(kbaichoo): Implement dumpState for H3. 49 0 : } 50 : 51 : // Network::ClientConnection 52 : // Set up socket and start handshake. 53 : void connect() override; 54 : 55 : // quic::QuicSession 56 : void OnConnectionClosed(const quic::QuicConnectionCloseFrame& frame, 57 : quic::ConnectionCloseSource source) override; 58 : void Initialize() override; 59 : void OnCanWrite() override; 60 : void OnHttp3GoAway(uint64_t stream_id) override; 61 : void OnTlsHandshakeComplete() override; 62 : void MaybeSendRstStreamFrame(quic::QuicStreamId id, quic::QuicResetStreamError error, 63 : quic::QuicStreamOffset bytes_written) override; 64 : void OnRstStream(const quic::QuicRstStreamFrame& frame) override; 65 : void OnNewEncryptionKeyAvailable(quic::EncryptionLevel level, 66 : std::unique_ptr<quic::QuicEncrypter> encrypter) override; 67 : 68 0 : quic::HttpDatagramSupport LocalHttpDatagramSupport() override { 69 0 : #ifdef ENVOY_ENABLE_HTTP_DATAGRAMS 70 0 : if (Runtime::runtimeFeatureEnabled("envoy.reloadable_features.enable_connect_udp_support")) { 71 0 : return quic::HttpDatagramSupport::kRfc; 72 0 : } 73 0 : #endif 74 0 : return quic::HttpDatagramSupport::kNone; 75 0 : } 76 : std::vector<std::string> GetAlpnsToOffer() const override; 77 : 78 : // quic::QuicSpdyClientSessionBase 79 : bool ShouldKeepConnectionAlive() const override; 80 : // quic::ProofHandler 81 : void OnProofVerifyDetailsAvailable(const quic::ProofVerifyDetails& verify_details) override; 82 : 83 : // PacketsToReadDelegate 84 0 : size_t numPacketsExpectedPerEventLoop() const override { 85 : // Do one round of reading per active stream, or to see if there's a new 86 : // active stream. 87 0 : return std::max<size_t>(1, GetNumActiveStreams()) * Network::NUM_DATAGRAMS_PER_RECEIVE; 88 0 : } 89 : 90 : // QuicFilterManagerConnectionImpl 91 : void setHttp3Options(const envoy::config::core::v3::Http3ProtocolOptions& http3_options) override; 92 : 93 : // Notify any registered connection pool when new streams are available. 94 : void OnCanCreateNewOutgoingStream(bool) override; 95 : 96 : void OnServerPreferredAddressAvailable( 97 : const quic::QuicSocketAddress& server_preferred_address) override; 98 : 99 : using quic::QuicSpdyClientSession::PerformActionOnActiveStreams; 100 : 101 : protected: 102 : // quic::QuicSpdyClientSession 103 : std::unique_ptr<quic::QuicSpdyClientStream> CreateClientStream() override; 104 : // quic::QuicSpdySession 105 : quic::QuicSpdyStream* CreateIncomingStream(quic::QuicStreamId id) override; 106 : quic::QuicSpdyStream* CreateIncomingStream(quic::PendingStream* pending) override; 107 : std::unique_ptr<quic::QuicCryptoClientStreamBase> CreateQuicCryptoStream() override; 108 0 : bool ShouldCreateOutgoingBidirectionalStream() override { 109 0 : ASSERT(quic::QuicSpdyClientSession::ShouldCreateOutgoingBidirectionalStream()); 110 : // Prefer creating an "invalid" stream outside of current stream bounds to 111 : // crashing when dereferencing a nullptr in QuicHttpClientConnectionImpl::newStream 112 0 : return true; 113 0 : } 114 : // QuicFilterManagerConnectionImpl 115 : bool hasDataToWrite() override; 116 : // Used by base class to access quic connection after initialization. 117 : const quic::QuicConnection* quicConnection() const override; 118 : quic::QuicConnection* quicConnection() override; 119 : 120 : private: 121 : uint64_t streamsAvailable(); 122 : 123 : // These callbacks are owned by network filters and quic session should outlive 124 : // them. 125 : Http::ConnectionCallbacks* http_connection_callbacks_{nullptr}; 126 : std::shared_ptr<quic::QuicCryptoClientConfig> crypto_config_; 127 : EnvoyQuicCryptoClientStreamFactoryInterface& crypto_stream_factory_; 128 : QuicStatNames& quic_stat_names_; 129 : OptRef<Http::HttpServerPropertiesCache> rtt_cache_; 130 : Stats::Scope& scope_; 131 : bool disable_keepalive_{false}; 132 : Network::TransportSocketOptionsConstSharedPtr transport_socket_options_; 133 : OptRef<QuicClientTransportSocketFactory> transport_socket_factory_; 134 : std::vector<std::string> configured_alpns_; 135 : }; 136 : 137 : } // namespace Quic 138 : } // namespace Envoy