1 // Copyright 2019 The Chromium Authors
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_CONTEXT_H_
6 #define NET_QUIC_QUIC_CONTEXT_H_
7
8 #include <memory>
9
10 #include "base/containers/contains.h"
11 #include "base/feature_list.h"
12 #include "base/time/time.h"
13 #include "net/base/features.h"
14 #include "net/base/host_port_pair.h"
15 #include "net/third_party/quiche/src/quiche/quic/core/crypto/quic_crypto_client_config.h"
16 #include "net/third_party/quiche/src/quiche/quic/core/quic_connection.h"
17
18 namespace net {
19
20 // Default QUIC supported versions used in absence of any external
21 // configuration.
22 inline NET_EXPORT_PRIVATE quic::ParsedQuicVersionVector
DefaultSupportedQuicVersions()23 DefaultSupportedQuicVersions() {
24 // The ordering of this list does not matter for Chrome because it respects
25 // the ordering received from the server via Alt-Svc. However, cronet offers
26 // an addQuicHint() API which uses the first version from this list until
27 // it receives Alt-Svc from the server.
28 return quic::ParsedQuicVersionVector{quic::ParsedQuicVersion::RFCv1()};
29 }
30
31 // Obsolete QUIC supported versions are versions that are supported by the
32 // QUIC shared code but that Chrome refuses to use because modern clients
33 // should only use versions at least as recent as the oldest default version.
ObsoleteQuicVersions()34 inline NET_EXPORT_PRIVATE quic::ParsedQuicVersionVector ObsoleteQuicVersions() {
35 return quic::ParsedQuicVersionVector{quic::ParsedQuicVersion::Q046(),
36 quic::ParsedQuicVersion::Draft29()};
37 }
38
39 // All of the QUIC versions that Chrome can support. This is the subset of
40 // QUIC versions that the QUIC shared code supports that are not on the list
41 // of versions that Chrome considers obsolete.
42 inline NET_EXPORT_PRIVATE quic::ParsedQuicVersionVector
AllSupportedQuicVersions()43 AllSupportedQuicVersions() {
44 quic::ParsedQuicVersionVector obsolete_versions = ObsoleteQuicVersions();
45 quic::ParsedQuicVersionVector all_supported_versions =
46 quic::AllSupportedVersions();
47 quic::ParsedQuicVersionVector filtered_versions;
48 for (const auto& version : all_supported_versions) {
49 if (!base::Contains(obsolete_versions, version)) {
50 filtered_versions.push_back(version);
51 }
52 }
53 return filtered_versions;
54 }
55
56 // When a connection is idle for 30 seconds it will be closed.
57 constexpr base::TimeDelta kIdleConnectionTimeout = base::Seconds(30);
58
59 // Sessions can migrate if they have been idle for less than this period.
60 constexpr base::TimeDelta kDefaultIdleSessionMigrationPeriod =
61 base::Seconds(30);
62
63 // The default maximum time allowed to have no retransmittable packets on the
64 // wire (after sending the first retransmittable packet) if
65 // |migrate_session_early_v2_| is true. PING frames will be sent as needed to
66 // enforce this.
67 constexpr base::TimeDelta kDefaultRetransmittableOnWireTimeout =
68 base::Milliseconds(200);
69
70 // The default maximum time QUIC session could be on non-default network before
71 // migrate back to default network.
72 constexpr base::TimeDelta kMaxTimeOnNonDefaultNetwork = base::Seconds(128);
73
74 // The default maximum number of migrations to non default network on write
75 // error per network.
76 const int64_t kMaxMigrationsToNonDefaultNetworkOnWriteError = 5;
77
78 // The default maximum number of migrations to non default network on path
79 // degrading per network.
80 const int64_t kMaxMigrationsToNonDefaultNetworkOnPathDegrading = 5;
81
82 // QUIC's socket receive buffer size.
83 // We should adaptively set this buffer size, but for now, we'll use a size
84 // that seems large enough to receive data at line rate for most connections,
85 // and does not consume "too much" memory.
86 const int32_t kQuicSocketReceiveBufferSize = 1024 * 1024; // 1MB
87
88 // Structure containing simple configuration options and experiments for QUIC.
89 struct NET_EXPORT QuicParams {
90 QuicParams();
91 QuicParams(const QuicParams& other);
92 ~QuicParams();
93
94 // QUIC runtime configuration options.
95
96 // Versions of QUIC which may be used.
97 quic::ParsedQuicVersionVector supported_versions =
98 DefaultSupportedQuicVersions();
99 // Limit on the size of QUIC packets.
100 size_t max_packet_length = quic::kDefaultMaxPacketSize;
101 // Maximum number of server configs that are to be stored in
102 // HttpServerProperties, instead of the disk cache.
103 size_t max_server_configs_stored_in_properties = 0u;
104 // QUIC will be used for all connections in this set.
105 std::set<HostPortPair> origins_to_force_quic_on;
106 // WebTransport developer mode disables the requirement that all QUIC
107 // connections are anchored to a system certificate root, but only for
108 // WebTransport connections.
109 bool webtransport_developer_mode = false;
110 // Set of QUIC tags to send in the handshake's connection options.
111 quic::QuicTagVector connection_options;
112 // Set of QUIC tags to send in the handshake's connection options that only
113 // affect the client.
114 quic::QuicTagVector client_connection_options;
115 // Enables experimental optimization for receiving data in UDPSocket.
116 bool enable_socket_recv_optimization = false;
117
118 // Active QUIC experiments
119
120 // Retry requests which fail with QUIC_PROTOCOL_ERROR, and mark QUIC
121 // broken if the retry succeeds.
122 bool retry_without_alt_svc_on_quic_errors = true;
123 // If true, all QUIC sessions are closed when any local IP address changes.
124 bool close_sessions_on_ip_change = false;
125 // If true, all QUIC sessions are marked as goaway when any local IP address
126 // changes.
127 bool goaway_sessions_on_ip_change = false;
128 // Specifies QUIC idle connection state lifetime.
129 base::TimeDelta idle_connection_timeout = kIdleConnectionTimeout;
130 // Specifies the reduced ping timeout subsequent connections should use when
131 // a connection was timed out with open streams.
132 base::TimeDelta reduced_ping_timeout = base::Seconds(quic::kPingTimeoutSecs);
133 // Maximum time that a session can have no retransmittable packets on the
134 // wire. Set to zero if not specified and no retransmittable PING will be
135 // sent to peer when the wire has no retransmittable packets.
136 base::TimeDelta retransmittable_on_wire_timeout;
137 // Maximum time the session can be alive before crypto handshake is
138 // finished.
139 base::TimeDelta max_time_before_crypto_handshake =
140 base::Seconds(quic::kMaxTimeForCryptoHandshakeSecs);
141 // Maximum idle time before the crypto handshake has completed.
142 base::TimeDelta max_idle_time_before_crypto_handshake =
143 base::Seconds(quic::kInitialIdleTimeoutSecs);
144 // If true, connection migration v2 will be used to migrate existing
145 // sessions to network when the platform indicates that the default network
146 // is changing.
147 // Use the value of the flag as the default value. This is needed because unit
148 // tests does not go through network_session_configuration which causes
149 // discrepancy.
150 bool migrate_sessions_on_network_change_v2 =
151 base::FeatureList::IsEnabled(features::kMigrateSessionsOnNetworkChangeV2);
152 // If true, connection migration v2 may be used to migrate active QUIC
153 // sessions to alternative network if current network connectivity is poor.
154 bool migrate_sessions_early_v2 = false;
155 // If true, a new connection may be kicked off on an alternate network when
156 // a connection fails on the default network before handshake is confirmed.
157 bool retry_on_alternate_network_before_handshake = false;
158 // If true, an idle session will be migrated within the idle migration
159 // period.
160 bool migrate_idle_sessions = false;
161 // If true, sessions with open streams will attempt to migrate to a different
162 // port when the current path is poor.
163 bool allow_port_migration = true;
164 // A session can be migrated if its idle time is within this period.
165 base::TimeDelta idle_session_migration_period =
166 kDefaultIdleSessionMigrationPeriod;
167 // Probing frequency for the multi-port alt path, represented in the number of
168 // seconds. When this param is 0, quiche will ignore it and use its own
169 // default.
170 int multi_port_probing_interval = 0;
171 // Maximum time the session could be on the non-default network before
172 // migrates back to default network. Defaults to
173 // kMaxTimeOnNonDefaultNetwork.
174 base::TimeDelta max_time_on_non_default_network = kMaxTimeOnNonDefaultNetwork;
175 // Maximum number of migrations to the non-default network on write error
176 // per network for each session.
177 int max_migrations_to_non_default_network_on_write_error =
178 kMaxMigrationsToNonDefaultNetworkOnWriteError;
179 // Maximum number of migrations to the non-default network on path
180 // degrading per network for each session.
181 int max_migrations_to_non_default_network_on_path_degrading =
182 kMaxMigrationsToNonDefaultNetworkOnPathDegrading;
183 // If true, allows migration of QUIC connections to a server-specified
184 // alternate server address.
185 bool allow_server_migration = false;
186 // If true, allows QUIC to use alternative services with a different
187 // hostname from the origin.
188 bool allow_remote_alt_svc = true;
189 // If true, estimate the initial RTT for QUIC connections based on network.
190 bool estimate_initial_rtt = false;
191 // The initial rtt that will be used in crypto handshake if no cached
192 // smoothed rtt is present.
193 base::TimeDelta initial_rtt_for_handshake;
194 // If true, QUIC with TLS will not try 0-RTT connection.
195 bool disable_tls_zero_rtt = false;
196 // If true, gQUIC requests will always require confirmation.
197 bool disable_gquic_zero_rtt = false;
198 // Network Service Type of the socket for iOS. Default is NET_SERVICE_TYPE_BE
199 // (best effort).
200 int ios_network_service_type = 0;
201 // Delay for the 1st time the alternative service is marked broken.
202 std::optional<base::TimeDelta> initial_delay_for_broken_alternative_service;
203 // If true, the delay for broke alternative service would be initial_delay *
204 // (1 << broken_count). Otherwise, the delay would be initial_delay, 5min,
205 // 10min and so on.
206 std::optional<bool> exponential_backoff_on_initial_delay;
207 // If true, delay main job even the request can be sent immediately on an
208 // available SPDY session.
209 bool delay_main_job_with_available_spdy_session = false;
210
211 // If true, ALPS uses new codepoint to negotiates application settings.
212 bool use_new_alps_codepoint = false;
213 };
214
215 // QuicContext contains QUIC-related variables that are shared across all of the
216 // QUIC connections, both HTTP and non-HTTP ones.
217 class NET_EXPORT_PRIVATE QuicContext {
218 public:
219 QuicContext();
220 explicit QuicContext(
221 std::unique_ptr<quic::QuicConnectionHelperInterface> helper);
222 virtual ~QuicContext();
223
helper()224 quic::QuicConnectionHelperInterface* helper() { return helper_.get(); }
clock()225 const quic::QuicClock* clock() { return helper_->GetClock(); }
random_generator()226 quic::QuicRandom* random_generator() { return helper_->GetRandomGenerator(); }
227
params()228 QuicParams* params() { return ¶ms_; }
GetDefaultVersion()229 quic::ParsedQuicVersion GetDefaultVersion() {
230 return params_.supported_versions[0];
231 }
supported_versions()232 const quic::ParsedQuicVersionVector& supported_versions() {
233 return params_.supported_versions;
234 }
235
SetHelperForTesting(std::unique_ptr<quic::QuicConnectionHelperInterface> helper)236 void SetHelperForTesting(
237 std::unique_ptr<quic::QuicConnectionHelperInterface> helper) {
238 helper_ = std::move(helper);
239 }
240
241 private:
242 std::unique_ptr<quic::QuicConnectionHelperInterface> helper_;
243
244 QuicParams params_;
245 };
246
247 // Initializes QuicConfig based on the specified parameters.
248 quic::QuicConfig InitializeQuicConfig(const QuicParams& params);
249
250 // Configures QuicCryptoClientConfig with Chromium-specific settings.
251 void ConfigureQuicCryptoClientConfig(
252 quic::QuicCryptoClientConfig& crypto_config);
253
254 } // namespace net
255
256 #endif // NET_QUIC_QUIC_CONTEXT_H_
257