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/quic_connection.h"
16
17 namespace net {
18
19 // Default QUIC supported versions used in absence of any external
20 // configuration.
21 inline NET_EXPORT_PRIVATE quic::ParsedQuicVersionVector
DefaultSupportedQuicVersions()22 DefaultSupportedQuicVersions() {
23 // The ordering of this list does not matter for Chrome because it respects
24 // the ordering received from the server via Alt-Svc. However, cronet offers
25 // an addQuicHint() API which uses the first version from this list until
26 // it receives Alt-Svc from the server.
27 return quic::ParsedQuicVersionVector{quic::ParsedQuicVersion::RFCv1()};
28 }
29
30 // Obsolete QUIC supported versions are versions that are supported by the
31 // QUIC shared code but that Chrome refuses to use because modern clients
32 // should only use versions at least as recent as the oldest default version.
ObsoleteQuicVersions()33 inline NET_EXPORT_PRIVATE quic::ParsedQuicVersionVector ObsoleteQuicVersions() {
34 return quic::ParsedQuicVersionVector{
35 quic::ParsedQuicVersion::Q043(), quic::ParsedQuicVersion::Q046(),
36 quic::ParsedQuicVersion::Q050(), 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 // User agent description to send in the QUIC handshake.
100 std::string user_agent_id;
101 // Limit on the size of QUIC packets.
102 size_t max_packet_length = quic::kDefaultMaxPacketSize;
103 // Maximum number of server configs that are to be stored in
104 // HttpServerProperties, instead of the disk cache.
105 size_t max_server_configs_stored_in_properties = 0u;
106 // QUIC will be used for all connections in this set.
107 std::set<HostPortPair> origins_to_force_quic_on;
108 // Set of QUIC tags to send in the handshake's connection options.
109 quic::QuicTagVector connection_options;
110 // Set of QUIC tags to send in the handshake's connection options that only
111 // affect the client.
112 quic::QuicTagVector client_connection_options;
113 // Enables experimental optimization for receiving data in UDPSocket.
114 bool enable_socket_recv_optimization = false;
115
116 // Active QUIC experiments
117
118 // Retry requests which fail with QUIC_PROTOCOL_ERROR, and mark QUIC
119 // broken if the retry succeeds.
120 bool retry_without_alt_svc_on_quic_errors = true;
121 // If true, all QUIC sessions are closed when any local IP address changes.
122 bool close_sessions_on_ip_change = false;
123 // If true, all QUIC sessions are marked as goaway when any local IP address
124 // changes.
125 bool goaway_sessions_on_ip_change = false;
126 // Specifies QUIC idle connection state lifetime.
127 base::TimeDelta idle_connection_timeout = kIdleConnectionTimeout;
128 // Specifies the reduced ping timeout subsequent connections should use when
129 // a connection was timed out with open streams.
130 base::TimeDelta reduced_ping_timeout = base::Seconds(quic::kPingTimeoutSecs);
131 // Maximum time that a session can have no retransmittable packets on the
132 // wire. Set to zero if not specified and no retransmittable PING will be
133 // sent to peer when the wire has no retransmittable packets.
134 base::TimeDelta retransmittable_on_wire_timeout;
135 // Maximum time the session can be alive before crypto handshake is
136 // finished.
137 base::TimeDelta max_time_before_crypto_handshake =
138 base::Seconds(quic::kMaxTimeForCryptoHandshakeSecs);
139 // Maximum idle time before the crypto handshake has completed.
140 base::TimeDelta max_idle_time_before_crypto_handshake =
141 base::Seconds(quic::kInitialIdleTimeoutSecs);
142 // If true, connection migration v2 will be used to migrate existing
143 // sessions to network when the platform indicates that the default network
144 // is changing.
145 bool migrate_sessions_on_network_change_v2 =
146 base::FeatureList::IsEnabled(features::kMigrateSessionsOnNetworkChangeV2);
147 // If true, connection migration v2 may be used to migrate active QUIC
148 // sessions to alternative network if current network connectivity is poor.
149 bool migrate_sessions_early_v2 = false;
150 // If true, a new connection may be kicked off on an alternate network when
151 // a connection fails on the default network before handshake is confirmed.
152 bool retry_on_alternate_network_before_handshake = false;
153 // If true, an idle session will be migrated within the idle migration
154 // period.
155 bool migrate_idle_sessions = false;
156 // If true, sessions with open streams will attempt to migrate to a different
157 // port when the current path is poor.
158 bool allow_port_migration = true;
159 // A session can be migrated if its idle time is within this period.
160 base::TimeDelta idle_session_migration_period =
161 kDefaultIdleSessionMigrationPeriod;
162 // Maximum time the session could be on the non-default network before
163 // migrates back to default network. Defaults to
164 // kMaxTimeOnNonDefaultNetwork.
165 base::TimeDelta max_time_on_non_default_network = kMaxTimeOnNonDefaultNetwork;
166 // Maximum number of migrations to the non-default network on write error
167 // per network for each session.
168 int max_migrations_to_non_default_network_on_write_error =
169 kMaxMigrationsToNonDefaultNetworkOnWriteError;
170 // Maximum number of migrations to the non-default network on path
171 // degrading per network for each session.
172 int max_migrations_to_non_default_network_on_path_degrading =
173 kMaxMigrationsToNonDefaultNetworkOnPathDegrading;
174 // If true, allows migration of QUIC connections to a server-specified
175 // alternate server address.
176 bool allow_server_migration = false;
177 // If true, allows QUIC to use alternative services with a different
178 // hostname from the origin.
179 bool allow_remote_alt_svc = true;
180 // If true, the quic stream factory may race connection from stale dns
181 // result with the original dns resolution
182 bool race_stale_dns_on_connection = false;
183 // If true, estimate the initial RTT for QUIC connections based on network.
184 bool estimate_initial_rtt = false;
185 // The initial rtt that will be used in crypto handshake if no cached
186 // smoothed rtt is present.
187 base::TimeDelta initial_rtt_for_handshake;
188 // If true, QUIC with TLS will not try 0-RTT connection.
189 bool disable_tls_zero_rtt = false;
190 // If true, gQUIC requests will always require confirmation.
191 bool disable_gquic_zero_rtt = false;
192 // Network Service Type of the socket for iOS. Default is NET_SERVICE_TYPE_BE
193 // (best effort).
194 int ios_network_service_type = 0;
195 // Delay for the 1st time the alternative service is marked broken.
196 absl::optional<base::TimeDelta> initial_delay_for_broken_alternative_service;
197 // If true, the delay for broke alternative service would be initial_delay *
198 // (1 << broken_count). Otherwise, the delay would be initial_delay, 5min,
199 // 10min and so on.
200 absl::optional<bool> exponential_backoff_on_initial_delay;
201 // If true, delay main job even the request can be sent immediately on an
202 // available SPDY session.
203 bool delay_main_job_with_available_spdy_session = false;
204 };
205
206 // QuicContext contains QUIC-related variables that are shared across all of the
207 // QUIC connections, both HTTP and non-HTTP ones.
208 class NET_EXPORT_PRIVATE QuicContext {
209 public:
210 QuicContext();
211 explicit QuicContext(
212 std::unique_ptr<quic::QuicConnectionHelperInterface> helper);
213 ~QuicContext();
214
helper()215 quic::QuicConnectionHelperInterface* helper() { return helper_.get(); }
clock()216 const quic::QuicClock* clock() { return helper_->GetClock(); }
random_generator()217 quic::QuicRandom* random_generator() { return helper_->GetRandomGenerator(); }
218
params()219 QuicParams* params() { return ¶ms_; }
GetDefaultVersion()220 quic::ParsedQuicVersion GetDefaultVersion() {
221 return params_.supported_versions[0];
222 }
supported_versions()223 const quic::ParsedQuicVersionVector& supported_versions() {
224 return params_.supported_versions;
225 }
226
SetHelperForTesting(std::unique_ptr<quic::QuicConnectionHelperInterface> helper)227 void SetHelperForTesting(
228 std::unique_ptr<quic::QuicConnectionHelperInterface> helper) {
229 helper_ = std::move(helper);
230 }
231
232 private:
233 std::unique_ptr<quic::QuicConnectionHelperInterface> helper_;
234
235 QuicParams params_;
236 };
237
238 // Initializes QuicConfig based on the specified parameters.
239 quic::QuicConfig InitializeQuicConfig(const QuicParams& params);
240
241 } // namespace net
242
243 #endif // NET_QUIC_QUIC_CONTEXT_H_
244