blob: 6825cfd033b55e2576b7da7e62ace76c3ae8601c [file] [log] [blame]
[email protected]d245c342012-02-23 20:49:151// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]aea80602009-09-18 00:55:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]dab9c7d2010-02-06 21:44:325#include "net/spdy/spdy_session_pool.h"
[email protected]aea80602009-09-18 00:55:086
7#include "base/logging.h"
[email protected]8b114dd72011-03-25 05:33:028#include "base/metrics/histogram.h"
[email protected]1ce7b66b2010-10-12 20:32:449#include "base/values.h"
[email protected]ab739042011-04-07 15:22:2810#include "net/base/address_list.h"
[email protected]f4580332010-09-25 21:20:2711#include "net/http/http_network_session.h"
[email protected]53bfa31c2011-11-15 19:20:3112#include "net/http/http_server_properties.h"
[email protected]dab9c7d2010-02-06 21:44:3213#include "net/spdy/spdy_session.h"
[email protected]aea80602009-09-18 00:55:0814
[email protected]8b114dd72011-03-25 05:33:0215
[email protected]aea80602009-09-18 00:55:0816namespace net {
17
[email protected]8b114dd72011-03-25 05:33:0218namespace {
19
20enum SpdySessionGetTypes {
21 CREATED_NEW = 0,
22 FOUND_EXISTING = 1,
23 FOUND_EXISTING_FROM_IP_POOL = 2,
24 IMPORTED_FROM_SOCKET = 3,
25 SPDY_SESSION_GET_MAX = 4
26};
27
[email protected]41d64e82013-07-03 22:44:2628} // namespace
[email protected]aea80602009-09-18 00:55:0829
[email protected]61b4efc2012-04-27 18:12:5030SpdySessionPool::SpdySessionPool(
31 HostResolver* resolver,
32 SSLConfigService* ssl_config_service,
[email protected]30d4c022013-07-18 22:58:1633 const base::WeakPtr<HttpServerProperties>& http_server_properties,
[email protected]f9cf5572012-12-04 15:52:0934 bool force_single_domain,
35 bool enable_ip_pooling,
36 bool enable_credential_frames,
37 bool enable_compression,
38 bool enable_ping_based_connection_checking,
39 NextProto default_protocol,
[email protected]a3827a02013-02-22 00:04:3240 size_t stream_initial_recv_window_size,
[email protected]f9cf5572012-12-04 15:52:0941 size_t initial_max_concurrent_streams,
42 size_t max_concurrent_streams_limit,
43 SpdySessionPool::TimeFunc time_func,
[email protected]61b4efc2012-04-27 18:12:5044 const std::string& trusted_spdy_proxy)
[email protected]53bfa31c2011-11-15 19:20:3145 : http_server_properties_(http_server_properties),
46 ssl_config_service_(ssl_config_service),
[email protected]b9ec6882011-07-01 07:40:2647 resolver_(resolver),
[email protected]61b4efc2012-04-27 18:12:5048 verify_domain_authentication_(true),
[email protected]e8bde622012-06-14 23:20:2449 enable_sending_initial_settings_(true),
[email protected]f9cf5572012-12-04 15:52:0950 force_single_domain_(force_single_domain),
51 enable_ip_pooling_(enable_ip_pooling),
52 enable_credential_frames_(enable_credential_frames),
53 enable_compression_(enable_compression),
54 enable_ping_based_connection_checking_(
55 enable_ping_based_connection_checking),
56 default_protocol_(default_protocol),
[email protected]a3827a02013-02-22 00:04:3257 stream_initial_recv_window_size_(stream_initial_recv_window_size),
[email protected]f9cf5572012-12-04 15:52:0958 initial_max_concurrent_streams_(initial_max_concurrent_streams),
59 max_concurrent_streams_limit_(max_concurrent_streams_limit),
60 time_func_(time_func),
[email protected]61b4efc2012-04-27 18:12:5061 trusted_spdy_proxy_(
62 HostPortPair::FromString(trusted_spdy_proxy)) {
[email protected]232a5812011-03-04 22:42:0863 NetworkChangeNotifier::AddIPAddressObserver(this);
[email protected]90499482013-06-01 00:39:5064 if (ssl_config_service_.get())
[email protected]7abf7d22010-09-04 01:41:5965 ssl_config_service_->AddObserver(this);
[email protected]7fda9a402012-09-10 14:11:0766 CertDatabase::GetInstance()->AddObserver(this);
[email protected]b846acd2010-06-07 18:13:1067}
68
[email protected]955fc2e72010-02-08 20:37:3069SpdySessionPool::~SpdySessionPool() {
[email protected]d1eda932009-11-04 01:03:1070 CloseAllSessions();
[email protected]b846acd2010-06-07 18:13:1071
[email protected]90499482013-06-01 00:39:5072 if (ssl_config_service_.get())
[email protected]7abf7d22010-09-04 01:41:5973 ssl_config_service_->RemoveObserver(this);
[email protected]232a5812011-03-04 22:42:0874 NetworkChangeNotifier::RemoveIPAddressObserver(this);
[email protected]7fda9a402012-09-10 14:11:0775 CertDatabase::GetInstance()->RemoveObserver(this);
[email protected]d1eda932009-11-04 01:03:1076}
[email protected]aea80602009-09-18 00:55:0877
[email protected]04644d42013-07-08 04:29:5978net::Error SpdySessionPool::CreateAvailableSessionFromSocket(
79 const SpdySessionKey& key,
80 scoped_ptr<ClientSocketHandle> connection,
81 const BoundNetLog& net_log,
82 int certificate_error_code,
[email protected]795cbf82013-07-22 09:37:2783 base::WeakPtr<SpdySession>* available_session,
[email protected]04644d42013-07-08 04:29:5984 bool is_secure) {
85 UMA_HISTOGRAM_ENUMERATION(
86 "Net.SpdySessionGet", IMPORTED_FROM_SOCKET, SPDY_SESSION_GET_MAX);
87
88 scoped_refptr<SpdySession> new_session(
89 new SpdySession(key,
90 http_server_properties_,
91 verify_domain_authentication_,
92 enable_sending_initial_settings_,
93 enable_credential_frames_,
94 enable_compression_,
95 enable_ping_based_connection_checking_,
96 default_protocol_,
97 stream_initial_recv_window_size_,
98 initial_max_concurrent_streams_,
99 max_concurrent_streams_limit_,
100 time_func_,
101 trusted_spdy_proxy_,
102 net_log.net_log()));
103
104 Error error = new_session->InitializeWithSocket(
105 connection.Pass(), this, is_secure, certificate_error_code);
106 DCHECK_NE(error, ERR_IO_PENDING);
107
108 if (error != OK) {
109 new_session = NULL;
[email protected]795cbf82013-07-22 09:37:27110 available_session->reset();
[email protected]04644d42013-07-08 04:29:59111 return error;
112 }
113
114 sessions_.insert(new_session);
[email protected]795cbf82013-07-22 09:37:27115 *available_session = new_session->GetWeakPtr();
[email protected]04644d42013-07-08 04:29:59116 MapKeyToAvailableSession(key, *available_session);
117
118 net_log.AddEvent(
119 NetLog::TYPE_SPDY_SESSION_POOL_IMPORTED_SESSION_FROM_SOCKET,
120 (*available_session)->net_log().source().ToEventParametersCallback());
121
122 // Look up the IP address for this session so that we can match
123 // future sessions (potentially to different domains) which can
124 // potentially be pooled with this one. Because GetPeerAddress()
125 // reports the proxy's address instead of the origin server, check
126 // to see if this is a direct connection.
127 if (enable_ip_pooling_ && key.proxy_server().is_direct()) {
128 IPEndPoint address;
129 if ((*available_session)->GetPeerAddress(&address) == OK)
130 aliases_[address] = key;
131 }
132
133 return error;
134}
135
[email protected]795cbf82013-07-22 09:37:27136base::WeakPtr<SpdySession> SpdySessionPool::FindAvailableSession(
[email protected]04644d42013-07-08 04:29:59137 const SpdySessionKey& key,
[email protected]e3ceb682011-06-28 23:55:46138 const BoundNetLog& net_log) {
[email protected]04644d42013-07-08 04:29:59139 AvailableSessionMap::iterator it = LookupAvailableSessionByKey(key);
140 if (it != available_sessions_.end()) {
141 UMA_HISTOGRAM_ENUMERATION(
142 "Net.SpdySessionGet", FOUND_EXISTING, SPDY_SESSION_GET_MAX);
[email protected]00cd9c42010-11-02 20:15:57143 net_log.AddEvent(
[email protected]41d64e82013-07-03 22:44:26144 NetLog::TYPE_SPDY_SESSION_POOL_FOUND_EXISTING_SESSION,
145 it->second->net_log().source().ToEventParametersCallback());
146 return it->second;
147 }
148
[email protected]04644d42013-07-08 04:29:59149 if (!enable_ip_pooling_)
[email protected]795cbf82013-07-22 09:37:27150 return base::WeakPtr<SpdySession>();
[email protected]04644d42013-07-08 04:29:59151
152 // Look up the key's from the resolver's cache.
153 net::HostResolver::RequestInfo resolve_info(key.host_port_pair());
154 AddressList addresses;
155 int rv = resolver_->ResolveFromCache(resolve_info, &addresses, net_log);
156 DCHECK_NE(rv, ERR_IO_PENDING);
157 if (rv != OK)
[email protected]795cbf82013-07-22 09:37:27158 return base::WeakPtr<SpdySession>();
[email protected]04644d42013-07-08 04:29:59159
160 // Check if we have a session through a domain alias.
161 for (AddressList::const_iterator address_it = addresses.begin();
162 address_it != addresses.end();
163 ++address_it) {
164 AliasMap::const_iterator alias_it = aliases_.find(*address_it);
165 if (alias_it == aliases_.end())
166 continue;
167
168 // We found an alias.
169 const SpdySessionKey& alias_key = alias_it->second;
170
171 // We can reuse this session only if the proxy and privacy
172 // settings match.
173 if (!(alias_key.proxy_server() == key.proxy_server()) ||
174 !(alias_key.privacy_mode() == key.privacy_mode()))
175 continue;
176
177 AvailableSessionMap::iterator available_session_it =
178 LookupAvailableSessionByKey(alias_key);
179 if (available_session_it == available_sessions_.end()) {
180 NOTREACHED(); // It shouldn't be in the aliases table if we can't get it!
181 continue;
182 }
183
[email protected]795cbf82013-07-22 09:37:27184 const base::WeakPtr<SpdySession>& available_session =
[email protected]04644d42013-07-08 04:29:59185 available_session_it->second;
[email protected]795cbf82013-07-22 09:37:27186 DCHECK(ContainsKey(sessions_, available_session.get()));
[email protected]04644d42013-07-08 04:29:59187 // If the session is a secure one, we need to verify that the
188 // server is authenticated to serve traffic for |host_port_proxy_pair| too.
189 if (!available_session->VerifyDomainAuthentication(
190 key.host_port_pair().host())) {
191 UMA_HISTOGRAM_ENUMERATION("Net.SpdyIPPoolDomainMatch", 0, 2);
192 continue;
193 }
194
195 UMA_HISTOGRAM_ENUMERATION("Net.SpdyIPPoolDomainMatch", 1, 2);
[email protected]41d64e82013-07-03 22:44:26196 UMA_HISTOGRAM_ENUMERATION("Net.SpdySessionGet",
197 FOUND_EXISTING_FROM_IP_POOL,
198 SPDY_SESSION_GET_MAX);
199 net_log.AddEvent(
200 NetLog::TYPE_SPDY_SESSION_POOL_FOUND_EXISTING_SESSION_FROM_IP_POOL,
[email protected]04644d42013-07-08 04:29:59201 available_session->net_log().source().ToEventParametersCallback());
[email protected]41d64e82013-07-03 22:44:26202 // Add this session to the map so that we can find it next time.
[email protected]04644d42013-07-08 04:29:59203 MapKeyToAvailableSession(key, available_session);
204 available_session->AddPooledAlias(key);
205 return available_session;
[email protected]6cd3bd202010-08-30 05:23:06206 }
[email protected]aea80602009-09-18 00:55:08207
[email protected]795cbf82013-07-22 09:37:27208 return base::WeakPtr<SpdySession>();
[email protected]aea80602009-09-18 00:55:08209}
210
[email protected]04644d42013-07-08 04:29:59211void SpdySessionPool::MakeSessionUnavailable(
[email protected]795cbf82013-07-22 09:37:27212 const base::WeakPtr<SpdySession>& available_session) {
[email protected]04644d42013-07-08 04:29:59213 UnmapKey(available_session->spdy_session_key());
214 RemoveAliases(available_session->spdy_session_key());
215 const std::set<SpdySessionKey>& aliases = available_session->pooled_aliases();
216 for (std::set<SpdySessionKey>::const_iterator it = aliases.begin();
217 it != aliases.end(); ++it) {
218 UnmapKey(*it);
219 RemoveAliases(*it);
[email protected]46da33be2011-07-19 21:58:04220 }
[email protected]04644d42013-07-08 04:29:59221 DCHECK(!IsSessionAvailable(available_session));
222}
[email protected]46da33be2011-07-19 21:58:04223
[email protected]04644d42013-07-08 04:29:59224void SpdySessionPool::RemoveUnavailableSession(
[email protected]795cbf82013-07-22 09:37:27225 const base::WeakPtr<SpdySession>& unavailable_session) {
[email protected]04644d42013-07-08 04:29:59226 DCHECK(!IsSessionAvailable(unavailable_session));
227
228 unavailable_session->net_log().AddEvent(
229 NetLog::TYPE_SPDY_SESSION_POOL_REMOVE_SESSION,
230 unavailable_session->net_log().source().ToEventParametersCallback());
231
[email protected]795cbf82013-07-22 09:37:27232 SessionSet::iterator it = sessions_.find(unavailable_session.get());
[email protected]04644d42013-07-08 04:29:59233 CHECK(it != sessions_.end());
234 sessions_.erase(it);
[email protected]9f7c4fd2009-11-24 18:50:15235}
236
[email protected]41d64e82013-07-03 22:44:26237// Make a copy of |sessions_| in the Close* functions below to avoid
[email protected]04644d42013-07-08 04:29:59238// reentrancy problems. Since arbitrary functions get called by close
239// handlers, it doesn't suffice to simply increment the iterator
240// before closing.
[email protected]8b114dd72011-03-25 05:33:02241
[email protected]41d64e82013-07-03 22:44:26242void SpdySessionPool::CloseCurrentSessions(net::Error error) {
[email protected]04644d42013-07-08 04:29:59243 CloseCurrentSessionsHelper(error, "Closing current sessions.",
244 false /* idle_only */);
[email protected]41d64e82013-07-03 22:44:26245}
246
247void SpdySessionPool::CloseCurrentIdleSessions() {
[email protected]04644d42013-07-08 04:29:59248 CloseCurrentSessionsHelper(ERR_ABORTED, "Closing idle sessions.",
249 true /* idle_only */);
[email protected]41d64e82013-07-03 22:44:26250}
251
252void SpdySessionPool::CloseAllSessions() {
253 while (!sessions_.empty()) {
[email protected]04644d42013-07-08 04:29:59254 CloseCurrentSessionsHelper(ERR_ABORTED, "Closing all sessions.",
255 false /* idle_only */);
[email protected]6cbfa852012-03-14 06:35:54256 }
257}
258
[email protected]ea5ef4c2013-06-13 22:50:27259base::Value* SpdySessionPool::SpdySessionPoolInfoToValue() const {
260 base::ListValue* list = new base::ListValue();
[email protected]1ce7b66b2010-10-12 20:32:44261
[email protected]04644d42013-07-08 04:29:59262 for (AvailableSessionMap::const_iterator it = available_sessions_.begin();
263 it != available_sessions_.end(); ++it) {
[email protected]41d64e82013-07-03 22:44:26264 // Only add the session if the key in the map matches the main
265 // host_port_proxy_pair (not an alias).
266 const SpdySessionKey& key = it->first;
267 const SpdySessionKey& session_key = it->second->spdy_session_key();
268 if (key.Equals(session_key))
269 list->Append(it->second->GetInfoAsValue());
[email protected]1ce7b66b2010-10-12 20:32:44270 }
271 return list;
272}
273
[email protected]66761b952010-06-25 21:30:38274void SpdySessionPool::OnIPAddressChanged() {
[email protected]7af985a2012-12-14 22:40:42275 CloseCurrentSessions(ERR_NETWORK_CHANGED);
[email protected]6dd1134e2013-04-30 21:13:09276 http_server_properties_->ClearAllSpdySettings();
[email protected]66761b952010-06-25 21:30:38277}
278
[email protected]7abf7d22010-09-04 01:41:59279void SpdySessionPool::OnSSLConfigChanged() {
[email protected]7af985a2012-12-14 22:40:42280 CloseCurrentSessions(ERR_NETWORK_CHANGED);
[email protected]7abf7d22010-09-04 01:41:59281}
282
[email protected]7fda9a402012-09-10 14:11:07283void SpdySessionPool::OnCertAdded(const X509Certificate* cert) {
[email protected]7af985a2012-12-14 22:40:42284 CloseCurrentSessions(ERR_NETWORK_CHANGED);
[email protected]c940d372011-04-13 17:20:18285}
286
287void SpdySessionPool::OnCertTrustChanged(const X509Certificate* cert) {
288 // Per wtc, we actually only need to CloseCurrentSessions when trust is
289 // reduced. CloseCurrentSessions now because OnCertTrustChanged does not
290 // tell us this.
291 // See comments in ClientSocketPoolManager::OnCertTrustChanged.
[email protected]7af985a2012-12-14 22:40:42292 CloseCurrentSessions(ERR_NETWORK_CHANGED);
[email protected]62635c72011-03-10 04:16:25293}
294
[email protected]04644d42013-07-08 04:29:59295bool SpdySessionPool::IsSessionAvailable(
[email protected]795cbf82013-07-22 09:37:27296 const base::WeakPtr<SpdySession>& session) const {
[email protected]04644d42013-07-08 04:29:59297 for (AvailableSessionMap::const_iterator it = available_sessions_.begin();
298 it != available_sessions_.end(); ++it) {
[email protected]795cbf82013-07-22 09:37:27299 if (it->second.get() == session.get())
[email protected]04644d42013-07-08 04:29:59300 return true;
301 }
302 return false;
303}
304
[email protected]e6d017652013-05-17 18:01:40305const SpdySessionKey& SpdySessionPool::NormalizeListKey(
[email protected]04644d42013-07-08 04:29:59306 const SpdySessionKey& key) const {
[email protected]f9cf5572012-12-04 15:52:09307 if (!force_single_domain_)
[email protected]04644d42013-07-08 04:29:59308 return key;
[email protected]ea1cc2cd2011-02-22 16:47:38309
[email protected]e6d017652013-05-17 18:01:40310 static SpdySessionKey* single_domain_key = NULL;
311 if (!single_domain_key) {
[email protected]ea1cc2cd2011-02-22 16:47:38312 HostPortPair single_domain = HostPortPair("singledomain.com", 80);
[email protected]e6d017652013-05-17 18:01:40313 single_domain_key = new SpdySessionKey(single_domain,
314 ProxyServer::Direct(),
315 kPrivacyModeDisabled);
[email protected]ea1cc2cd2011-02-22 16:47:38316 }
[email protected]e6d017652013-05-17 18:01:40317 return *single_domain_key;
[email protected]ea1cc2cd2011-02-22 16:47:38318}
319
[email protected]04644d42013-07-08 04:29:59320void SpdySessionPool::MapKeyToAvailableSession(
321 const SpdySessionKey& key,
[email protected]795cbf82013-07-22 09:37:27322 const base::WeakPtr<SpdySession>& session) {
323 DCHECK(ContainsKey(sessions_, scoped_refptr<SpdySession>(session.get())));
[email protected]04644d42013-07-08 04:29:59324 const SpdySessionKey& normalized_key = NormalizeListKey(key);
325 std::pair<AvailableSessionMap::iterator, bool> result =
326 available_sessions_.insert(std::make_pair(normalized_key, session));
[email protected]41d64e82013-07-03 22:44:26327 CHECK(result.second);
[email protected]aea80602009-09-18 00:55:08328}
329
[email protected]04644d42013-07-08 04:29:59330SpdySessionPool::AvailableSessionMap::iterator
331SpdySessionPool::LookupAvailableSessionByKey(
332 const SpdySessionKey& key) {
333 const SpdySessionKey& normalized_key = NormalizeListKey(key);
334 return available_sessions_.find(normalized_key);
[email protected]d1eda932009-11-04 01:03:10335}
336
[email protected]04644d42013-07-08 04:29:59337void SpdySessionPool::UnmapKey(const SpdySessionKey& key) {
338 AvailableSessionMap::iterator it = LookupAvailableSessionByKey(key);
339 CHECK(it != available_sessions_.end());
340 available_sessions_.erase(it);
[email protected]8b114dd72011-03-25 05:33:02341}
342
[email protected]04644d42013-07-08 04:29:59343void SpdySessionPool::RemoveAliases(const SpdySessionKey& key) {
[email protected]8b114dd72011-03-25 05:33:02344 // Walk the aliases map, find references to this pair.
345 // TODO(mbelshe): Figure out if this is too expensive.
[email protected]04644d42013-07-08 04:29:59346 for (AliasMap::iterator it = aliases_.begin(); it != aliases_.end(); ) {
347 if (it->second.Equals(key)) {
348 AliasMap::iterator old_it = it;
349 ++it;
350 aliases_.erase(old_it);
351 } else {
352 ++it;
[email protected]8b114dd72011-03-25 05:33:02353 }
[email protected]04644d42013-07-08 04:29:59354 }
355}
356
357void SpdySessionPool::CloseCurrentSessionsHelper(
358 Error error,
359 const std::string& description,
360 bool idle_only) {
361 SessionSet current_sessions = sessions_;
362 for (SessionSet::const_iterator it = current_sessions.begin();
363 it != current_sessions.end(); ++it) {
364 if (!ContainsKey(sessions_, *it))
365 continue;
366
[email protected]04644d42013-07-08 04:29:59367 if (idle_only && (*it)->is_active())
368 continue;
369
370 (*it)->CloseSessionOnError(error, description);
[email protected]795cbf82013-07-22 09:37:27371 DCHECK(!IsSessionAvailable((*it)->GetWeakPtr()));
[email protected]04644d42013-07-08 04:29:59372 DCHECK(!ContainsKey(sessions_, *it));
[email protected]8b114dd72011-03-25 05:33:02373 }
[email protected]aea80602009-09-18 00:55:08374}
375
[email protected]aea80602009-09-18 00:55:08376} // namespace net