Line data Source code
1 : #pragma once 2 : 3 : #include <memory> 4 : #include <ostream> 5 : 6 : #include "source/common/quic/envoy_quic_server_connection.h" 7 : #include "source/common/quic/envoy_quic_server_crypto_stream_factory.h" 8 : #include "source/common/quic/envoy_quic_server_stream.h" 9 : #include "source/common/quic/quic_filter_manager_connection_impl.h" 10 : #include "source/common/quic/quic_stat_names.h" 11 : #include "source/common/quic/send_buffer_monitor.h" 12 : 13 : #include "quiche/quic/core/http/quic_server_session_base.h" 14 : #include "quiche/quic/core/quic_crypto_server_stream.h" 15 : #include "quiche/quic/core/tls_server_handshaker.h" 16 : 17 : namespace Envoy { 18 : namespace Quic { 19 : 20 : #define QUIC_CONNECTION_STATS(COUNTER) \ 21 : COUNTER(num_server_migration_detected) \ 22 : COUNTER(num_packets_rx_on_preferred_address) 23 : 24 : struct QuicConnectionStats { 25 : QUIC_CONNECTION_STATS(GENERATE_COUNTER_STRUCT) 26 : }; 27 : 28 : using FilterChainToConnectionMap = 29 : absl::flat_hash_map<const Network::FilterChain*, 30 : std::list<std::reference_wrapper<Network::Connection>>>; 31 : using ConnectionMapIter = std::list<std::reference_wrapper<Network::Connection>>::iterator; 32 : 33 : // Used to track the matching filter chain and its position in the filter chain to connection map. 34 : struct ConnectionMapPosition { 35 : ConnectionMapPosition(FilterChainToConnectionMap& connection_map, 36 : const Network::FilterChain& filter_chain, ConnectionMapIter iterator) 37 0 : : connection_map_(connection_map), filter_chain_(filter_chain), iterator_(iterator) {} 38 : 39 : // Stores the map from filter chain of connections. 40 : FilterChainToConnectionMap& connection_map_; 41 : // The matching filter chain of a connection. 42 : const Network::FilterChain& filter_chain_; 43 : // The position of the connection in the map. 44 : ConnectionMapIter iterator_; 45 : }; 46 : 47 : // Act as a Network::Connection to HCM and a FilterManager to FilterFactoryCb. 48 : // TODO(danzh) Lifetime of quic connection and filter manager connection can be 49 : // simplified by changing the inheritance to a member variable instantiated 50 : // before quic_connection_. 51 : class EnvoyQuicServerSession : public quic::QuicServerSessionBase, 52 : public QuicFilterManagerConnectionImpl { 53 : public: 54 : EnvoyQuicServerSession( 55 : const quic::QuicConfig& config, const quic::ParsedQuicVersionVector& supported_versions, 56 : std::unique_ptr<EnvoyQuicServerConnection> connection, quic::QuicSession::Visitor* visitor, 57 : quic::QuicCryptoServerStreamBase::Helper* helper, 58 : const quic::QuicCryptoServerConfig* crypto_config, 59 : quic::QuicCompressedCertsCache* compressed_certs_cache, Event::Dispatcher& dispatcher, 60 : uint32_t send_buffer_limit, QuicStatNames& quic_stat_names, Stats::Scope& listener_scope, 61 : EnvoyQuicCryptoServerStreamFactoryInterface& crypto_server_stream_factory, 62 : std::unique_ptr<StreamInfo::StreamInfo>&& stream_info, QuicConnectionStats& connection_stats); 63 : 64 : ~EnvoyQuicServerSession() override; 65 : 66 : // Network::Connection 67 : absl::string_view requestedServerName() const override; 68 0 : void dumpState(std::ostream&, int) const override { 69 0 : // TODO(kbaichoo): Implement dumpState for H3. 70 0 : } 71 : 72 : // Called by QuicHttpServerConnectionImpl before creating data streams. 73 515 : void setHttpConnectionCallbacks(Http::ServerConnectionCallbacks& callbacks) { 74 515 : http_connection_callbacks_ = &callbacks; 75 515 : } 76 : 77 : // quic::QuicSession 78 : void OnConnectionClosed(const quic::QuicConnectionCloseFrame& frame, 79 : quic::ConnectionCloseSource source) override; 80 : void Initialize() override; 81 : void OnCanWrite() override; 82 : void OnTlsHandshakeComplete() override; 83 : void MaybeSendRstStreamFrame(quic::QuicStreamId id, quic::QuicResetStreamError error, 84 : quic::QuicStreamOffset bytes_written) override; 85 : void OnRstStream(const quic::QuicRstStreamFrame& frame) override; 86 : void ProcessUdpPacket(const quic::QuicSocketAddress& self_address, 87 : const quic::QuicSocketAddress& peer_address, 88 : const quic::QuicReceivedPacket& packet) override; 89 : std::vector<absl::string_view>::const_iterator 90 : SelectAlpn(const std::vector<absl::string_view>& alpns) const override; 91 : 92 : void setHeadersWithUnderscoreAction( 93 : envoy::config::core::v3::HttpProtocolOptions::HeadersWithUnderscoresAction 94 515 : headers_with_underscores_action) { 95 515 : headers_with_underscores_action_ = headers_with_underscores_action; 96 515 : } 97 : 98 : void storeConnectionMapPosition(FilterChainToConnectionMap& connection_map, 99 : const Network::FilterChain& filter_chain, 100 : ConnectionMapIter position); 101 : 102 : void setHttp3Options(const envoy::config::core::v3::Http3ProtocolOptions& http3_options) override; 103 : using quic::QuicSession::PerformActionOnActiveStreams; 104 : 105 : protected: 106 : // quic::QuicServerSessionBase 107 : std::unique_ptr<quic::QuicCryptoServerStreamBase> 108 : CreateQuicCryptoServerStream(const quic::QuicCryptoServerConfig* crypto_config, 109 : quic::QuicCompressedCertsCache* compressed_certs_cache) override; 110 : quic::QuicSSLConfig GetSSLConfig() const override; 111 : 112 : // quic::QuicSession 113 : // Overridden to create stream as encoder and associate it with an decoder. 114 : quic::QuicSpdyStream* CreateIncomingStream(quic::QuicStreamId id) override; 115 : quic::QuicSpdyStream* CreateIncomingStream(quic::PendingStream* pending) override; 116 : quic::QuicSpdyStream* CreateOutgoingBidirectionalStream() override; 117 : quic::QuicSpdyStream* CreateOutgoingUnidirectionalStream() override; 118 : 119 1446 : quic::HttpDatagramSupport LocalHttpDatagramSupport() override { 120 1446 : #ifdef ENVOY_ENABLE_HTTP_DATAGRAMS 121 1446 : return quic::HttpDatagramSupport::kRfc; 122 : #else 123 : return quic::HttpDatagramSupport::kNone; 124 : #endif 125 1446 : } 126 : 127 : // QuicFilterManagerConnectionImpl 128 : bool hasDataToWrite() override; 129 : // Used by base class to access quic connection after initialization. 130 : const quic::QuicConnection* quicConnection() const override; 131 : quic::QuicConnection* quicConnection() override; 132 : 133 : private: 134 : void setUpRequestDecoder(EnvoyQuicServerStream& stream); 135 : 136 : std::unique_ptr<EnvoyQuicServerConnection> quic_connection_; 137 : // These callbacks are owned by network filters and quic session should out live 138 : // them. 139 : Http::ServerConnectionCallbacks* http_connection_callbacks_{nullptr}; 140 : 141 : envoy::config::core::v3::HttpProtocolOptions::HeadersWithUnderscoresAction 142 : headers_with_underscores_action_; 143 : 144 : QuicStatNames& quic_stat_names_; 145 : Stats::Scope& listener_scope_; 146 : 147 : EnvoyQuicCryptoServerStreamFactoryInterface& crypto_server_stream_factory_; 148 : absl::optional<ConnectionMapPosition> position_; 149 : QuicConnectionStats& connection_stats_; 150 : }; 151 : 152 : } // namespace Quic 153 : } // namespace Envoy