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_
11 #include "base/basictypes.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "net/base/ip_endpoint.h"
15 #include "net/base/linked_hash_map.h"
16 #include "net/quic/quic_blocked_writer_interface.h"
17 #include "net/quic/quic_connection.h"
18 #include "net/quic/quic_protocol.h"
19 #include "net/tools/quic/quic_server_session.h"
20 #include "net/tools/quic/quic_time_wait_list_manager.h"
25 class QuicCryptoServerConfig
;
26 class QuicServerSession
;
31 class QuicDispatcherPeer
;
34 extern int32 FLAGS_quic_session_map_threshold_for_stateless_rejects
;
36 class ProcessPacketInterface
{
38 virtual ~ProcessPacketInterface() {}
39 virtual void ProcessPacket(const IPEndPoint
& server_address
,
40 const IPEndPoint
& client_address
,
41 const QuicEncryptedPacket
& packet
) = 0;
44 class QuicDispatcher
: public QuicServerSessionVisitor
,
45 public ProcessPacketInterface
,
46 public QuicBlockedWriterInterface
{
48 // Creates per-connection packet writers out of the QuicDispatcher's shared
49 // QuicPacketWriter. The per-connection writers' IsWriteBlocked() state must
50 // always be the same as the shared writer's IsWriteBlocked(), or else the
51 // QuicDispatcher::OnCanWrite logic will not work. (This will hopefully be
52 // cleaned up for bug 16950226.)
53 class PacketWriterFactory
{
55 virtual ~PacketWriterFactory() {}
57 virtual QuicPacketWriter
* Create(QuicPacketWriter
* writer
,
58 QuicConnection
* connection
) = 0;
61 // Creates ordinary QuicPerConnectionPacketWriter instances.
62 class DefaultPacketWriterFactory
: public PacketWriterFactory
{
64 ~DefaultPacketWriterFactory() override
{}
66 QuicPacketWriter
* Create(QuicPacketWriter
* writer
,
67 QuicConnection
* connection
) override
;
70 // Ideally we'd have a linked_hash_set: the boolean is unused.
71 typedef linked_hash_map
<QuicBlockedWriterInterface
*, bool> WriteBlockedList
;
73 // Due to the way delete_sessions_closure_ is registered, the Dispatcher must
74 // live until server Shutdown. |supported_versions| specifies the std::list
75 // of supported QUIC versions. Takes ownership of |packet_writer_factory|,
76 // which is used to create per-connection writers.
77 QuicDispatcher(const QuicConfig
& config
,
78 const QuicCryptoServerConfig
* crypto_config
,
79 const QuicVersionVector
& supported_versions
,
80 PacketWriterFactory
* packet_writer_factory
,
81 QuicConnectionHelperInterface
* helper
);
83 ~QuicDispatcher() override
;
85 // Takes ownership of |writer|.
86 void InitializeWithWriter(QuicPacketWriter
* writer
);
88 // Process the incoming packet by creating a new session, passing it to
89 // an existing session, or passing it to the time wait list.
90 void ProcessPacket(const IPEndPoint
& server_address
,
91 const IPEndPoint
& client_address
,
92 const QuicEncryptedPacket
& packet
) override
;
94 // Called when the socket becomes writable to allow queued writes to happen.
95 void OnCanWrite() override
;
97 // Returns true if there's anything in the blocked writer list.
98 virtual bool HasPendingWrites() const;
100 // Sends ConnectionClose frames to all connected clients.
103 // QuicServerSessionVisitor interface implementation:
104 // Ensure that the closed connection is cleaned up asynchronously.
105 void OnConnectionClosed(QuicConnectionId connection_id
,
106 QuicErrorCode error
) override
;
108 // Queues the blocked writer for later resumption.
109 void OnWriteBlocked(QuicBlockedWriterInterface
* blocked_writer
) override
;
111 // Called whenever the time wait list manager adds a new connection to the
113 void OnConnectionAddedToTimeWaitList(QuicConnectionId connection_id
) override
;
115 // Called whenever the time wait list manager removes an old connection from
116 // the time-wait list.
117 void OnConnectionRemovedFromTimeWaitList(
118 QuicConnectionId connection_id
) override
;
120 typedef base::hash_map
<QuicConnectionId
, QuicServerSession
*> SessionMap
;
122 const SessionMap
& session_map() const { return session_map_
; }
124 // Deletes all sessions on the closed session list and clears the list.
125 void DeleteSessions();
127 // The largest packet sequence number we expect to receive with a connection
128 // ID for a connection that is not established yet. The current design will
129 // send a handshake and then up to 50 or so data packets, and then it may
130 // resend the handshake packet up to 10 times. (Retransmitted packets are
131 // sent with unique sequence numbers.)
132 static const QuicPacketSequenceNumber kMaxReasonableInitialSequenceNumber
=
134 static_assert(kMaxReasonableInitialSequenceNumber
>=
135 kInitialCongestionWindowSecure
+ 10,
136 "kMaxReasonableInitialSequenceNumber is unreasonably small "
137 "relative to kInitialCongestionWindowSecure.");
138 static_assert(kMaxReasonableInitialSequenceNumber
>=
139 kInitialCongestionWindowInsecure
+ 10,
140 "kMaxReasonableInitialSequenceNumber is unreasonably small "
141 "relative to kInitialCongestionWindowInsecure.");
144 virtual QuicServerSession
* CreateQuicSession(
145 QuicConnectionId connection_id
,
146 const IPEndPoint
& server_address
,
147 const IPEndPoint
& client_address
);
149 // Called by |framer_visitor_| when the public header has been parsed.
150 virtual bool OnUnauthenticatedPublicHeader(
151 const QuicPacketPublicHeader
& header
);
153 // Values to be returned by ValidityChecks() to indicate what should be done
154 // with a packet. Fates with greater values are considered to be higher
155 // priority, in that if one validity check indicates a lower-valued fate and
156 // another validity check indicates a higher-valued fate, the higher-valued
157 // fate should be obeyed.
158 enum QuicPacketFate
{
159 // Process the packet normally, which is usually to establish a connection.
161 // Put the connection ID into time-wait state and send a public reset.
163 // Drop the packet (ignore and give no response).
167 // This method is called by OnUnauthenticatedHeader on packets not associated
168 // with a known connection ID. It applies validity checks and returns a
169 // QuicPacketFate to tell what should be done with the packet.
170 virtual QuicPacketFate
ValidityChecks(const QuicPacketHeader
& header
);
172 // Create and return the time wait list manager for this dispatcher, which
173 // will be owned by the dispatcher as time_wait_list_manager_
174 virtual QuicTimeWaitListManager
* CreateQuicTimeWaitListManager();
176 QuicTimeWaitListManager
* time_wait_list_manager() {
177 return time_wait_list_manager_
.get();
180 const QuicVersionVector
& supported_versions() const {
181 return supported_versions_
;
184 const IPEndPoint
& current_server_address() {
185 return current_server_address_
;
187 const IPEndPoint
& current_client_address() {
188 return current_client_address_
;
190 const QuicEncryptedPacket
& current_packet() {
191 return *current_packet_
;
194 const QuicConfig
& config() const { return config_
; }
196 const QuicCryptoServerConfig
* crypto_config() const { return crypto_config_
; }
198 QuicFramer
* framer() { return &framer_
; }
200 QuicConnectionHelperInterface
* helper() { return helper_
.get(); }
202 QuicPacketWriter
* writer() { return writer_
.get(); }
204 const QuicConnection::PacketWriterFactory
& connection_writer_factory() {
205 return connection_writer_factory_
;
208 void SetLastError(QuicErrorCode error
);
211 class QuicFramerVisitor
;
212 friend class net::tools::test::QuicDispatcherPeer
;
214 // An adapter that creates packet writers using the dispatcher's
215 // PacketWriterFactory and shared writer. Essentially, it just curries the
216 // writer argument away from QuicDispatcher::PacketWriterFactory.
217 class PacketWriterFactoryAdapter
:
218 public QuicConnection::PacketWriterFactory
{
220 explicit PacketWriterFactoryAdapter(QuicDispatcher
* dispatcher
);
221 ~PacketWriterFactoryAdapter() override
;
223 QuicPacketWriter
* Create(QuicConnection
* connection
) const override
;
226 QuicDispatcher
* dispatcher_
;
229 // Called by |framer_visitor_| when the private header has been parsed
230 // of a data packet that is destined for the time wait manager.
231 void OnUnauthenticatedHeader(const QuicPacketHeader
& header
);
233 // Removes the session from the session map and write blocked list, and adds
234 // the ConnectionId to the time-wait list. If |session_closed_statelessly| is
235 // true, any future packets for the ConnectionId will be black-holed.
236 void CleanUpSession(SessionMap::iterator it
, bool session_closed_statelessly
);
238 bool HandlePacketForTimeWait(const QuicPacketPublicHeader
& header
);
240 const QuicConfig
& config_
;
242 const QuicCryptoServerConfig
* crypto_config_
;
244 // The list of connections waiting to write.
245 WriteBlockedList write_blocked_list_
;
247 SessionMap session_map_
;
249 // Entity that manages connection_ids in time wait state.
250 scoped_ptr
<QuicTimeWaitListManager
> time_wait_list_manager_
;
252 // The list of closed but not-yet-deleted sessions.
253 std::list
<QuicServerSession
*> closed_session_list_
;
255 // The helper used for all connections.
256 scoped_ptr
<QuicConnectionHelperInterface
> helper_
;
258 // An alarm which deletes closed sessions.
259 scoped_ptr
<QuicAlarm
> delete_sessions_alarm_
;
261 // The writer to write to the socket with.
262 scoped_ptr
<QuicPacketWriter
> writer_
;
264 // A per-connection writer that is passed to the time wait list manager.
265 scoped_ptr
<QuicPacketWriter
> time_wait_list_writer_
;
267 // Used to create per-connection packet writers, not |writer_| itself.
268 scoped_ptr
<PacketWriterFactory
> packet_writer_factory_
;
270 // Passed in to QuicConnection for it to create the per-connection writers
271 PacketWriterFactoryAdapter connection_writer_factory_
;
273 // This vector contains QUIC versions which we currently support.
274 // This should be ordered such that the highest supported version is the first
275 // element, with subsequent elements in descending order (versions can be
276 // skipped as necessary).
277 const QuicVersionVector supported_versions_
;
279 // Information about the packet currently being handled.
280 IPEndPoint current_client_address_
;
281 IPEndPoint current_server_address_
;
282 const QuicEncryptedPacket
* current_packet_
;
285 scoped_ptr
<QuicFramerVisitor
> framer_visitor_
;
287 // The last error set by SetLastError(), which is called by
288 // framer_visitor_->OnError().
289 QuicErrorCode last_error_
;
291 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher
);
297 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_