1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_dispatcher.h"
9 #include "base/debug/stack_trace.h"
10 #include "base/logging.h"
11 #include "base/stl_util.h"
12 #include "net/quic/quic_blocked_writer_interface.h"
13 #include "net/quic/quic_connection_helper.h"
14 #include "net/quic/quic_flags.h"
15 #include "net/quic/quic_per_connection_packet_writer.h"
16 #include "net/quic/quic_time_wait_list_manager.h"
17 #include "net/quic/quic_utils.h"
21 using base::StringPiece
;
25 class DeleteSessionsAlarm
: public QuicAlarm::Delegate
{
27 explicit DeleteSessionsAlarm(QuicDispatcher
* dispatcher
)
28 : dispatcher_(dispatcher
) {
31 QuicTime
OnAlarm() override
{
32 dispatcher_
->DeleteSessions();
33 return QuicTime::Zero();
37 QuicDispatcher
* dispatcher_
;
40 class QuicDispatcher::QuicFramerVisitor
: public QuicFramerVisitorInterface
{
42 explicit QuicFramerVisitor(QuicDispatcher
* dispatcher
)
43 : dispatcher_(dispatcher
),
46 // QuicFramerVisitorInterface implementation
47 void OnPacket() override
{}
48 bool OnUnauthenticatedPublicHeader(
49 const QuicPacketPublicHeader
& header
) override
{
50 connection_id_
= header
.connection_id
;
51 return dispatcher_
->OnUnauthenticatedPublicHeader(header
);
53 bool OnUnauthenticatedHeader(const QuicPacketHeader
& header
) override
{
54 dispatcher_
->OnUnauthenticatedHeader(header
);
57 void OnError(QuicFramer
* framer
) override
{
58 DVLOG(1) << QuicUtils::ErrorToString(framer
->error());
61 bool OnProtocolVersionMismatch(QuicVersion
/*received_version*/) override
{
62 if (dispatcher_
->time_wait_list_manager()->IsConnectionIdInTimeWait(
64 // Keep processing after protocol mismatch - this will be dealt with by
65 // the TimeWaitListManager.
68 DLOG(DFATAL
) << "Version mismatch, connection ID (" << connection_id_
69 << ") not in time wait list.";
74 // The following methods should never get called because we always return
75 // false from OnUnauthenticatedHeader(). As a result, we never process the
76 // payload of the packet.
77 void OnPublicResetPacket(const QuicPublicResetPacket
& /*packet*/) override
{
80 void OnVersionNegotiationPacket(
81 const QuicVersionNegotiationPacket
& /*packet*/) override
{
84 void OnDecryptedPacket(EncryptionLevel level
) override
{ DCHECK(false); }
85 bool OnPacketHeader(const QuicPacketHeader
& /*header*/) override
{
89 void OnRevivedPacket() override
{ DCHECK(false); }
90 void OnFecProtectedPayload(StringPiece
/*payload*/) override
{
93 bool OnStreamFrame(const QuicStreamFrame
& /*frame*/) override
{
97 bool OnAckFrame(const QuicAckFrame
& /*frame*/) override
{
101 bool OnStopWaitingFrame(const QuicStopWaitingFrame
& /*frame*/) override
{
105 bool OnPingFrame(const QuicPingFrame
& /*frame*/) override
{
109 bool OnRstStreamFrame(const QuicRstStreamFrame
& /*frame*/) override
{
113 bool OnConnectionCloseFrame(
114 const QuicConnectionCloseFrame
& /*frame*/) override
{
118 bool OnGoAwayFrame(const QuicGoAwayFrame
& /*frame*/) override
{
122 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame
& /*frame*/) override
{
126 bool OnBlockedFrame(const QuicBlockedFrame
& frame
) override
{
130 void OnFecData(const QuicFecData
& /*fec*/) override
{ DCHECK(false); }
131 void OnPacketComplete() override
{ DCHECK(false); }
134 QuicDispatcher
* dispatcher_
;
136 // Latched in OnUnauthenticatedPublicHeader for use later.
137 QuicConnectionId connection_id_
;
140 QuicPacketWriter
* QuicDispatcher::DefaultPacketWriterFactory::Create(
141 QuicServerPacketWriter
* writer
,
142 QuicConnection
* connection
) {
143 return new QuicPerConnectionPacketWriter(writer
, connection
);
146 QuicDispatcher::PacketWriterFactoryAdapter::PacketWriterFactoryAdapter(
147 QuicDispatcher
* dispatcher
)
148 : dispatcher_(dispatcher
) {}
150 QuicDispatcher::PacketWriterFactoryAdapter::~PacketWriterFactoryAdapter() {}
152 QuicPacketWriter
* QuicDispatcher::PacketWriterFactoryAdapter::Create(
153 QuicConnection
* connection
) const {
154 return dispatcher_
->packet_writer_factory_
->Create(
155 dispatcher_
->writer_
.get(),
159 QuicDispatcher::QuicDispatcher(const QuicConfig
& config
,
160 const QuicCryptoServerConfig
& crypto_config
,
161 const QuicVersionVector
& supported_versions
,
162 PacketWriterFactory
* packet_writer_factory
,
163 QuicConnectionHelperInterface
* helper
)
165 crypto_config_(crypto_config
),
167 delete_sessions_alarm_(
168 helper_
->CreateAlarm(new DeleteSessionsAlarm(this))),
169 packet_writer_factory_(packet_writer_factory
),
170 connection_writer_factory_(this),
171 supported_versions_(supported_versions
),
172 current_packet_(nullptr),
173 framer_(supported_versions
, /*unused*/ QuicTime::Zero(), true),
174 framer_visitor_(new QuicFramerVisitor(this)) {
175 framer_
.set_visitor(framer_visitor_
.get());
178 QuicDispatcher::~QuicDispatcher() {
179 STLDeleteValues(&session_map_
);
180 STLDeleteElements(&closed_session_list_
);
183 void QuicDispatcher::Initialize(QuicServerPacketWriter
* writer
) {
184 DCHECK(writer_
== nullptr);
185 writer_
.reset(writer
);
186 time_wait_list_manager_
.reset(CreateQuicTimeWaitListManager());
189 void QuicDispatcher::ProcessPacket(const IPEndPoint
& server_address
,
190 const IPEndPoint
& client_address
,
191 const QuicEncryptedPacket
& packet
) {
192 current_server_address_
= server_address
;
193 current_client_address_
= client_address
;
194 current_packet_
= &packet
;
195 // ProcessPacket will cause the packet to be dispatched in
196 // OnUnauthenticatedPublicHeader, or sent to the time wait list manager
197 // in OnAuthenticatedHeader.
198 framer_
.ProcessPacket(packet
);
199 // TODO(rjshade): Return a status describing if/why a packet was dropped,
200 // and log somehow. Maybe expose as a varz.
203 bool QuicDispatcher::OnUnauthenticatedPublicHeader(
204 const QuicPacketPublicHeader
& header
) {
205 QuicSession
* session
= nullptr;
207 QuicConnectionId connection_id
= header
.connection_id
;
208 SessionMap::iterator it
= session_map_
.find(connection_id
);
209 if (it
== session_map_
.end()) {
210 if (header
.reset_flag
) {
213 if (time_wait_list_manager_
->IsConnectionIdInTimeWait(connection_id
)) {
214 return HandlePacketForTimeWait(header
);
217 // Ensure the packet has a version negotiation bit set before creating a new
218 // session for it. All initial packets for a new connection are required to
219 // have the flag set. Otherwise it may be a stray packet.
220 if (header
.version_flag
) {
221 session
= CreateQuicSession(connection_id
, current_server_address_
,
222 current_client_address_
);
225 if (session
== nullptr) {
226 DVLOG(1) << "Failed to create session for " << connection_id
;
227 // Add this connection_id fo the time-wait state, to safely reject future
230 if (header
.version_flag
&&
231 !framer_
.IsSupportedVersion(header
.versions
.front())) {
232 // TODO(ianswett): Produce a no-version version negotiation packet.
236 // Use the version in the packet if possible, otherwise assume the latest.
237 QuicVersion version
= header
.version_flag
? header
.versions
.front() :
238 supported_versions_
.front();
239 time_wait_list_manager_
->AddConnectionIdToTimeWait(connection_id
, version
,
241 DCHECK(time_wait_list_manager_
->IsConnectionIdInTimeWait(connection_id
));
242 return HandlePacketForTimeWait(header
);
244 DVLOG(1) << "Created new session for " << connection_id
;
245 session_map_
.insert(make_pair(connection_id
, session
));
247 session
= it
->second
;
250 session
->connection()->ProcessUdpPacket(
251 current_server_address_
, current_client_address_
, *current_packet_
);
253 // Do not parse the packet further. The session will process it completely.
257 void QuicDispatcher::OnUnauthenticatedHeader(const QuicPacketHeader
& header
) {
258 DCHECK(time_wait_list_manager_
->IsConnectionIdInTimeWait(
259 header
.public_header
.connection_id
));
260 time_wait_list_manager_
->ProcessPacket(current_server_address_
,
261 current_client_address_
,
262 header
.public_header
.connection_id
,
263 header
.packet_sequence_number
,
267 void QuicDispatcher::CleanUpSession(SessionMap::iterator it
) {
268 QuicConnection
* connection
= it
->second
->connection();
269 QuicEncryptedPacket
* connection_close_packet
=
270 connection
->ReleaseConnectionClosePacket();
271 write_blocked_list_
.erase(connection
);
272 time_wait_list_manager_
->AddConnectionIdToTimeWait(it
->first
,
273 connection
->version(),
274 connection_close_packet
);
275 session_map_
.erase(it
);
278 void QuicDispatcher::DeleteSessions() {
279 STLDeleteElements(&closed_session_list_
);
282 void QuicDispatcher::OnCanWrite() {
283 // We finished a write: the socket should not be blocked.
284 writer_
->SetWritable();
286 // Give all the blocked writers one chance to write, until we're blocked again
287 // or there's no work left.
288 while (!write_blocked_list_
.empty() && !writer_
->IsWriteBlocked()) {
289 QuicBlockedWriterInterface
* blocked_writer
=
290 write_blocked_list_
.begin()->first
;
291 write_blocked_list_
.erase(write_blocked_list_
.begin());
292 blocked_writer
->OnCanWrite();
296 bool QuicDispatcher::HasPendingWrites() const {
297 return !write_blocked_list_
.empty();
300 void QuicDispatcher::Shutdown() {
301 while (!session_map_
.empty()) {
302 QuicSession
* session
= session_map_
.begin()->second
;
303 session
->connection()->SendConnectionClose(QUIC_PEER_GOING_AWAY
);
304 // Validate that the session removes itself from the session map on close.
305 DCHECK(session_map_
.empty() || session_map_
.begin()->second
!= session
);
310 void QuicDispatcher::OnConnectionClosed(QuicConnectionId connection_id
,
311 QuicErrorCode error
) {
312 SessionMap::iterator it
= session_map_
.find(connection_id
);
313 if (it
== session_map_
.end()) {
314 LOG(DFATAL
) << "ConnectionId " << connection_id
315 << " does not exist in the session map. "
316 << "Error: " << QuicUtils::ErrorToString(error
);
317 LOG(DFATAL
) << base::debug::StackTrace().ToString();
320 DVLOG_IF(1, error
!= QUIC_NO_ERROR
) << "Closing connection ("
322 << ") due to error: "
323 << QuicUtils::ErrorToString(error
);
324 if (closed_session_list_
.empty()) {
325 delete_sessions_alarm_
->Set(helper_
->GetClock()->ApproximateNow());
327 closed_session_list_
.push_back(it
->second
);
331 void QuicDispatcher::OnWriteBlocked(
332 QuicBlockedWriterInterface
* blocked_writer
) {
333 if (!writer_
->IsWriteBlocked()) {
335 "QuicDispatcher::OnWriteBlocked called when the writer is not blocked.";
336 // Return without adding the connection to the blocked list, to avoid
337 // infinite loops in OnCanWrite.
340 write_blocked_list_
.insert(make_pair(blocked_writer
, true));
343 void QuicDispatcher::OnConnectionAddedToTimeWaitList(
344 QuicConnectionId connection_id
) {
345 DVLOG(1) << "Connection " << connection_id
<< " added to time wait list.";
348 void QuicDispatcher::OnConnectionRemovedFromTimeWaitList(
349 QuicConnectionId connection_id
) {
350 DVLOG(1) << "Connection " << connection_id
<< " removed from time wait list.";
353 QuicSession
* QuicDispatcher::CreateQuicSession(
354 QuicConnectionId connection_id
,
355 const IPEndPoint
& server_address
,
356 const IPEndPoint
& client_address
) {
357 QuicServerSession
* session
= new QuicServerSession(
359 CreateQuicConnection(connection_id
, server_address
, client_address
),
361 session
->InitializeSession(crypto_config_
);
365 QuicConnection
* QuicDispatcher::CreateQuicConnection(
366 QuicConnectionId connection_id
,
367 const IPEndPoint
& server_address
,
368 const IPEndPoint
& client_address
) {
369 return new QuicConnection(connection_id
,
372 connection_writer_factory_
,
373 /* owns_writer= */ true,
374 /* is_server= */ true,
375 crypto_config_
.HasProofSource(),
376 supported_versions_
);
379 QuicTimeWaitListManager
* QuicDispatcher::CreateQuicTimeWaitListManager() {
380 return new QuicTimeWaitListManager(
381 writer_
.get(), this, helper_
, supported_versions());
384 bool QuicDispatcher::HandlePacketForTimeWait(
385 const QuicPacketPublicHeader
& header
) {
386 if (header
.reset_flag
) {
387 // Public reset packets do not have sequence numbers, so ignore the packet.
391 // Switch the framer to the correct version, so that the sequence number can
392 // be parsed correctly.
393 framer_
.set_version(time_wait_list_manager_
->GetQuicVersionFromConnectionId(
394 header
.connection_id
));
396 // Continue parsing the packet to extract the sequence number. Then
397 // send it to the time wait manager in OnUnathenticatedHeader.