Home | History | Annotate | Download | only in quic
      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.
      4 //
      5 // A server side dispatcher which dispatches a given client's data to their
      6 // stream.
      7 
      8 #ifndef NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
      9 #define NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
     10 
     11 #include <list>
     12 
     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_protocol.h"
     20 #include "net/tools/quic/quic_server_session.h"
     21 #include "net/tools/quic/quic_time_wait_list_manager.h"
     22 
     23 #if defined(COMPILER_GCC)
     24 namespace BASE_HASH_NAMESPACE {
     25 template<>
     26 struct hash<net::QuicBlockedWriterInterface*> {
     27   std::size_t operator()(
     28       const net::QuicBlockedWriterInterface* ptr) const {
     29     return hash<size_t>()(reinterpret_cast<size_t>(ptr));
     30   }
     31 };
     32 }
     33 #endif
     34 
     35 namespace net {
     36 
     37 class EpollServer;
     38 class QuicConfig;
     39 class QuicCryptoServerConfig;
     40 class QuicSession;
     41 
     42 namespace tools {
     43 
     44 class QuicPacketWriterWrapper;
     45 
     46 namespace test {
     47 class QuicDispatcherPeer;
     48 }  // namespace test
     49 
     50 class DeleteSessionsAlarm;
     51 class QuicEpollConnectionHelper;
     52 
     53 class QuicDispatcher : public QuicServerSessionVisitor {
     54  public:
     55   // Ideally we'd have a linked_hash_set: the  boolean is unused.
     56   typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList;
     57 
     58   // Due to the way delete_sessions_closure_ is registered, the Dispatcher
     59   // must live until epoll_server Shutdown. |supported_versions| specifies the
     60   // list of supported QUIC versions.
     61   QuicDispatcher(const QuicConfig& config,
     62                  const QuicCryptoServerConfig& crypto_config,
     63                  const QuicVersionVector& supported_versions,
     64                  EpollServer* epoll_server);
     65 
     66   virtual ~QuicDispatcher();
     67 
     68   virtual void Initialize(int fd);
     69 
     70   // Process the incoming packet by creating a new session, passing it to
     71   // an existing session, or passing it to the TimeWaitListManager.
     72   virtual void ProcessPacket(const IPEndPoint& server_address,
     73                              const IPEndPoint& client_address,
     74                              const QuicEncryptedPacket& packet);
     75 
     76   // Called when the socket becomes writable to allow queued writes to happen.
     77   virtual void OnCanWrite();
     78 
     79   // Returns true if there's anything in the blocked writer list.
     80   virtual bool HasPendingWrites() const;
     81 
     82   // Sends ConnectionClose frames to all connected clients.
     83   void Shutdown();
     84 
     85   // QuicServerSessionVisitor interface implementation:
     86   // Ensure that the closed connection is cleaned up asynchronously.
     87   virtual void OnConnectionClosed(QuicConnectionId connection_id,
     88                                   QuicErrorCode error) OVERRIDE;
     89 
     90   // Queues the blocked writer for later resumption.
     91   virtual void OnWriteBlocked(QuicBlockedWriterInterface* writer) OVERRIDE;
     92 
     93   typedef base::hash_map<QuicConnectionId, QuicSession*> SessionMap;
     94 
     95   // Deletes all sessions on the closed session list and clears the list.
     96   void DeleteSessions();
     97 
     98   const SessionMap& session_map() const { return session_map_; }
     99 
    100   WriteBlockedList* write_blocked_list() { return &write_blocked_list_; }
    101 
    102  protected:
    103   // Instantiates a new low-level packet writer. Caller takes ownership of the
    104   // returned object.
    105   virtual QuicPacketWriter* CreateWriter(int fd);
    106 
    107   virtual QuicSession* CreateQuicSession(QuicConnectionId connection_id,
    108                                          const IPEndPoint& server_address,
    109                                          const IPEndPoint& client_address);
    110 
    111   virtual QuicConnection* CreateQuicConnection(
    112       QuicConnectionId connection_id,
    113       const IPEndPoint& server_address,
    114       const IPEndPoint& client_address);
    115 
    116   // Called by |framer_visitor_| when the public header has been parsed.
    117   virtual bool OnUnauthenticatedPublicHeader(
    118       const QuicPacketPublicHeader& header);
    119 
    120   // Create and return the time wait list manager for this dispatcher, which
    121   // will be owned by the dispatcher as time_wait_list_manager_
    122   virtual QuicTimeWaitListManager* CreateQuicTimeWaitListManager();
    123 
    124   // Replaces the packet writer with |writer|. Takes ownership of |writer|.
    125   void set_writer(QuicPacketWriter* writer) {
    126     writer_.reset(writer);
    127   }
    128 
    129   QuicTimeWaitListManager* time_wait_list_manager() {
    130     return time_wait_list_manager_.get();
    131   }
    132 
    133   EpollServer* epoll_server() { return epoll_server_; }
    134 
    135   const QuicVersionVector& supported_versions() const {
    136     return supported_versions_;
    137   }
    138 
    139   const QuicVersionVector& supported_versions_no_flow_control() const {
    140     return supported_versions_no_flow_control_;
    141   }
    142 
    143   const QuicVersionVector& supported_versions_no_connection_flow_control()
    144       const {
    145     return supported_versions_no_connection_flow_control_;
    146   }
    147 
    148   const IPEndPoint& current_server_address() {
    149     return current_server_address_;
    150   }
    151   const IPEndPoint& current_client_address() {
    152     return current_client_address_;
    153   }
    154   const QuicEncryptedPacket& current_packet() {
    155     return *current_packet_;
    156   }
    157 
    158   const QuicConfig& config() const { return config_; }
    159 
    160   const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; }
    161 
    162   QuicFramer* framer() { return &framer_; }
    163 
    164   QuicEpollConnectionHelper* helper() { return helper_.get(); }
    165 
    166   QuicPacketWriter* writer() { return writer_.get(); }
    167 
    168  private:
    169   class QuicFramerVisitor;
    170   friend class net::tools::test::QuicDispatcherPeer;
    171 
    172   // Called by |framer_visitor_| when the private header has been parsed
    173   // of a data packet that is destined for the time wait manager.
    174   void OnUnauthenticatedHeader(const QuicPacketHeader& header);
    175 
    176   // Removes the session from the session map and write blocked list, and
    177   // adds the ConnectionId to the time-wait list.
    178   void CleanUpSession(SessionMap::iterator it);
    179 
    180   bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header);
    181 
    182   const QuicConfig& config_;
    183 
    184   const QuicCryptoServerConfig& crypto_config_;
    185 
    186   // The list of connections waiting to write.
    187   WriteBlockedList write_blocked_list_;
    188 
    189   SessionMap session_map_;
    190 
    191   // Entity that manages connection_ids in time wait state.
    192   scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
    193 
    194   // An alarm which deletes closed sessions.
    195   scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_;
    196 
    197   // The list of closed but not-yet-deleted sessions.
    198   std::list<QuicSession*> closed_session_list_;
    199 
    200   EpollServer* epoll_server_;  // Owned by the server.
    201 
    202   // The helper used for all connections.
    203   scoped_ptr<QuicEpollConnectionHelper> helper_;
    204 
    205   // The writer to write to the socket with.
    206   scoped_ptr<QuicPacketWriter> writer_;
    207 
    208   // This vector contains QUIC versions which we currently support.
    209   // This should be ordered such that the highest supported version is the first
    210   // element, with subsequent elements in descending order (versions can be
    211   // skipped as necessary).
    212   const QuicVersionVector supported_versions_;
    213 
    214   // Versions which do not support flow control (introduced in QUIC_VERSION_17).
    215   // This is used to construct new QuicConnections when flow control is disabled
    216   // via flag.
    217   // TODO(rjshade): Remove this when
    218   // FLAGS_enable_quic_stream_flow_control_2 is removed.
    219   QuicVersionVector supported_versions_no_flow_control_;
    220   // Versions which do not support *connection* flow control (introduced in
    221   // QUIC_VERSION_19).
    222   // This is used to construct new QuicConnections when connection flow control
    223   // is disabled via flag.
    224   // TODO(rjshade): Remove this when
    225   // FLAGS_enable_quic_connection_flow_control_2 is removed.
    226   QuicVersionVector supported_versions_no_connection_flow_control_;
    227 
    228   // Information about the packet currently being handled.
    229   IPEndPoint current_client_address_;
    230   IPEndPoint current_server_address_;
    231   const QuicEncryptedPacket* current_packet_;
    232 
    233   QuicFramer framer_;
    234   scoped_ptr<QuicFramerVisitor> framer_visitor_;
    235 
    236   DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
    237 };
    238 
    239 }  // namespace tools
    240 }  // namespace net
    241 
    242 #endif  // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
    243