1 // Copyright (c) 2012 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 // A server side dispatcher which dispatches a given client's data to their
8 #ifndef NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
9 #define NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
13 #include "base/basictypes.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "net/base/ip_endpoint.h"
17 #include "net/base/linked_hash_map.h"
18 #include "net/quic/quic_blocked_writer_interface.h"
19 #include "net/quic/quic_connection.h"
20 #include "net/quic/quic_protocol.h"
21 #include "net/tools/quic/quic_server_session.h"
22 #include "net/tools/quic/quic_time_wait_list_manager.h"
27 class QuicCryptoServerConfig
;
28 class QuicServerSession
;
33 class QuicDispatcherPeer
;
36 extern int32 FLAGS_quic_session_map_threshold_for_stateless_rejects
;
38 class ProcessPacketInterface
{
40 virtual ~ProcessPacketInterface() {}
41 virtual void ProcessPacket(const IPEndPoint
& server_address
,
42 const IPEndPoint
& client_address
,
43 const QuicEncryptedPacket
& packet
) = 0;
46 class QuicDispatcher
: public QuicServerSessionVisitor
,
47 public ProcessPacketInterface
,
48 public QuicBlockedWriterInterface
{
50 // Creates per-connection packet writers out of the QuicDispatcher's shared
51 // QuicPacketWriter. The per-connection writers' IsWriteBlocked() state must
52 // always be the same as the shared writer's IsWriteBlocked(), or else the
53 // QuicDispatcher::OnCanWrite logic will not work. (This will hopefully be
54 // cleaned up for bug 16950226.)
55 class PacketWriterFactory
{
57 virtual ~PacketWriterFactory() {}
59 virtual QuicPacketWriter
* Create(QuicPacketWriter
* writer
,
60 QuicConnection
* connection
) = 0;
63 // Creates ordinary QuicPerConnectionPacketWriter instances.
64 class DefaultPacketWriterFactory
: public PacketWriterFactory
{
66 ~DefaultPacketWriterFactory() override
{}
68 QuicPacketWriter
* Create(QuicPacketWriter
* writer
,
69 QuicConnection
* connection
) override
;
72 // Ideally we'd have a linked_hash_set: the boolean is unused.
73 typedef linked_hash_map
<QuicBlockedWriterInterface
*, bool> WriteBlockedList
;
75 // Due to the way delete_sessions_closure_ is registered, the Dispatcher must
76 // live until server Shutdown. |supported_versions| specifies the std::list
77 // of supported QUIC versions. Takes ownership of |packet_writer_factory|,
78 // which is used to create per-connection writers.
79 QuicDispatcher(const QuicConfig
& config
,
80 const QuicCryptoServerConfig
* crypto_config
,
81 const QuicVersionVector
& supported_versions
,
82 PacketWriterFactory
* packet_writer_factory
,
83 QuicConnectionHelperInterface
* helper
);
85 ~QuicDispatcher() override
;
87 // Takes ownership of |writer|.
88 void InitializeWithWriter(QuicPacketWriter
* writer
);
90 // Process the incoming packet by creating a new session, passing it to
91 // an existing session, or passing it to the time wait list.
92 void ProcessPacket(const IPEndPoint
& server_address
,
93 const IPEndPoint
& client_address
,
94 const QuicEncryptedPacket
& packet
) override
;
96 // Called when the socket becomes writable to allow queued writes to happen.
97 void OnCanWrite() override
;
99 // Returns true if there's anything in the blocked writer list.
100 virtual bool HasPendingWrites() const;
102 // Sends ConnectionClose frames to all connected clients.
105 // QuicServerSessionVisitor interface implementation:
106 // Ensure that the closed connection is cleaned up asynchronously.
107 void OnConnectionClosed(QuicConnectionId connection_id
,
108 QuicErrorCode error
) override
;
110 // Queues the blocked writer for later resumption.
111 void OnWriteBlocked(QuicBlockedWriterInterface
* blocked_writer
) override
;
113 // Called whenever the time wait list manager adds a new connection to the
115 void OnConnectionAddedToTimeWaitList(QuicConnectionId connection_id
) override
;
117 // Called whenever the time wait list manager removes an old connection from
118 // the time-wait list.
119 void OnConnectionRemovedFromTimeWaitList(
120 QuicConnectionId connection_id
) override
;
122 typedef base::hash_map
<QuicConnectionId
, QuicServerSession
*> SessionMap
;
124 const SessionMap
& session_map() const { return session_map_
; }
126 // Deletes all sessions on the closed session list and clears the list.
127 void DeleteSessions();
129 // The largest packet number we expect to receive with a connection
130 // ID for a connection that is not established yet. The current design will
131 // send a handshake and then up to 50 or so data packets, and then it may
132 // resend the handshake packet up to 10 times. (Retransmitted packets are
133 // sent with unique packet numbers.)
134 static const QuicPacketNumber kMaxReasonableInitialPacketNumber
= 100;
135 static_assert(kMaxReasonableInitialPacketNumber
>=
136 kInitialCongestionWindowSecure
+ 10,
137 "kMaxReasonableInitialPacketNumber is unreasonably small "
138 "relative to kInitialCongestionWindowSecure.");
139 static_assert(kMaxReasonableInitialPacketNumber
>=
140 kInitialCongestionWindowInsecure
+ 10,
141 "kMaxReasonableInitialPacketNumber is unreasonably small "
142 "relative to kInitialCongestionWindowInsecure.");
145 virtual QuicServerSession
* CreateQuicSession(
146 QuicConnectionId connection_id
,
147 const IPEndPoint
& server_address
,
148 const IPEndPoint
& client_address
);
150 // Called by |framer_visitor_| when the public header has been parsed.
151 virtual bool OnUnauthenticatedPublicHeader(
152 const QuicPacketPublicHeader
& header
);
154 // Values to be returned by ValidityChecks() to indicate what should be done
155 // with a packet. Fates with greater values are considered to be higher
156 // priority, in that if one validity check indicates a lower-valued fate and
157 // another validity check indicates a higher-valued fate, the higher-valued
158 // fate should be obeyed.
159 enum QuicPacketFate
{
160 // Process the packet normally, which is usually to establish a connection.
162 // Put the connection ID into time-wait state and send a public reset.
164 // Drop the packet (ignore and give no response).
168 // This method is called by OnUnauthenticatedHeader on packets not associated
169 // with a known connection ID. It applies validity checks and returns a
170 // QuicPacketFate to tell what should be done with the packet.
171 virtual QuicPacketFate
ValidityChecks(const QuicPacketHeader
& header
);
173 // Create and return the time wait list manager for this dispatcher, which
174 // will be owned by the dispatcher as time_wait_list_manager_
175 virtual QuicTimeWaitListManager
* CreateQuicTimeWaitListManager();
177 QuicTimeWaitListManager
* time_wait_list_manager() {
178 return time_wait_list_manager_
.get();
181 const QuicVersionVector
& supported_versions() const {
182 return supported_versions_
;
185 const IPEndPoint
& current_server_address() {
186 return current_server_address_
;
188 const IPEndPoint
& current_client_address() {
189 return current_client_address_
;
191 const QuicEncryptedPacket
& current_packet() {
192 return *current_packet_
;
195 const QuicConfig
& config() const { return config_
; }
197 const QuicCryptoServerConfig
* crypto_config() const { return crypto_config_
; }
199 QuicFramer
* framer() { return &framer_
; }
201 QuicConnectionHelperInterface
* helper() { return helper_
.get(); }
203 QuicPacketWriter
* writer() { return writer_
.get(); }
205 const QuicConnection::PacketWriterFactory
& connection_writer_factory() {
206 return connection_writer_factory_
;
209 void SetLastError(QuicErrorCode error
);
212 class QuicFramerVisitor
;
213 friend class net::tools::test::QuicDispatcherPeer
;
215 // An adapter that creates packet writers using the dispatcher's
216 // PacketWriterFactory and shared writer. Essentially, it just curries the
217 // writer argument away from QuicDispatcher::PacketWriterFactory.
218 class PacketWriterFactoryAdapter
:
219 public QuicConnection::PacketWriterFactory
{
221 explicit PacketWriterFactoryAdapter(QuicDispatcher
* dispatcher
);
222 ~PacketWriterFactoryAdapter() override
;
224 QuicPacketWriter
* Create(QuicConnection
* connection
) const override
;
227 QuicDispatcher
* dispatcher_
;
230 // Called by |framer_visitor_| when the private header has been parsed
231 // of a data packet that is destined for the time wait manager.
232 void OnUnauthenticatedHeader(const QuicPacketHeader
& header
);
234 // Removes the session from the session map and write blocked list, and adds
235 // the ConnectionId to the time-wait list. If |session_closed_statelessly| is
236 // true, any future packets for the ConnectionId will be black-holed.
237 void CleanUpSession(SessionMap::iterator it
, bool session_closed_statelessly
);
239 bool HandlePacketForTimeWait(const QuicPacketPublicHeader
& header
);
241 const QuicConfig
& config_
;
243 const QuicCryptoServerConfig
* crypto_config_
;
245 // The list of connections waiting to write.
246 WriteBlockedList write_blocked_list_
;
248 SessionMap session_map_
;
250 // Entity that manages connection_ids in time wait state.
251 scoped_ptr
<QuicTimeWaitListManager
> time_wait_list_manager_
;
253 // The list of closed but not-yet-deleted sessions.
254 std::vector
<QuicServerSession
*> closed_session_list_
;
256 // The helper used for all connections.
257 scoped_ptr
<QuicConnectionHelperInterface
> helper_
;
259 // An alarm which deletes closed sessions.
260 scoped_ptr
<QuicAlarm
> delete_sessions_alarm_
;
262 // The writer to write to the socket with.
263 scoped_ptr
<QuicPacketWriter
> writer_
;
265 // A per-connection writer that is passed to the time wait list manager.
266 scoped_ptr
<QuicPacketWriter
> time_wait_list_writer_
;
268 // Used to create per-connection packet writers, not |writer_| itself.
269 scoped_ptr
<PacketWriterFactory
> packet_writer_factory_
;
271 // Passed in to QuicConnection for it to create the per-connection writers
272 PacketWriterFactoryAdapter connection_writer_factory_
;
274 // This vector contains QUIC versions which we currently support.
275 // This should be ordered such that the highest supported version is the first
276 // element, with subsequent elements in descending order (versions can be
277 // skipped as necessary).
278 const QuicVersionVector supported_versions_
;
280 // Information about the packet currently being handled.
281 IPEndPoint current_client_address_
;
282 IPEndPoint current_server_address_
;
283 const QuicEncryptedPacket
* current_packet_
;
286 scoped_ptr
<QuicFramerVisitor
> framer_visitor_
;
288 // The last error set by SetLastError(), which is called by
289 // framer_visitor_->OnError().
290 QuicErrorCode last_error_
;
292 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher
);
298 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_