Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / net / tools / quic / quic_time_wait_list_manager.h
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 // Handles packets for connection_ids in time wait state by discarding the
6 // packet and sending the clients a public reset packet with exponential
7 // backoff.
8
9 #ifndef NET_TOOLS_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_
10 #define NET_TOOLS_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_
11
12 #include <deque>
13
14 #include "base/basictypes.h"
15 #include "base/containers/hash_tables.h"
16 #include "base/strings/string_piece.h"
17 #include "net/base/linked_hash_map.h"
18 #include "net/quic/quic_blocked_writer_interface.h"
19 #include "net/quic/quic_framer.h"
20 #include "net/quic/quic_packet_writer.h"
21 #include "net/quic/quic_protocol.h"
22 #include "net/tools/epoll_server/epoll_server.h"
23 #include "net/tools/quic/quic_epoll_clock.h"
24
25 namespace net {
26 namespace tools {
27
28 class ConnectionIdCleanUpAlarm;
29 class QuicServerSessionVisitor;
30
31 namespace test {
32 class QuicTimeWaitListManagerPeer;
33 }  // namespace test
34
35 // Maintains a list of all connection_ids that have been recently closed. A
36 // connection_id lives in this state for kTimeWaitPeriod. All packets received
37 // for connection_ids in this state are handed over to the
38 // QuicTimeWaitListManager by the QuicDispatcher.  Decides whether to send a
39 // public reset packet, a copy of the previously sent connection close packet,
40 // or nothing to the client which sent a packet with the connection_id in time
41 // wait state.  After the connection_id expires its time wait period, a new
42 // connection/session will be created if a packet is received for this
43 // connection_id.
44 class QuicTimeWaitListManager : public QuicBlockedWriterInterface {
45  public:
46   // writer - the entity that writes to the socket. (Owned by the dispatcher)
47   // visitor - the entity that manages blocked writers. (The dispatcher)
48   // epoll_server - used to run clean up alarms. (Owned by the dispatcher)
49   QuicTimeWaitListManager(QuicPacketWriter* writer,
50                           QuicServerSessionVisitor* visitor,
51                           EpollServer* epoll_server,
52                           const QuicVersionVector& supported_versions);
53   virtual ~QuicTimeWaitListManager();
54
55   // Adds the given connection_id to time wait state for kTimeWaitPeriod.
56   // Henceforth, any packet bearing this connection_id should not be processed
57   // while the connection_id remains in this list. If a non-NULL |close_packet|
58   // is provided, it is sent again when packets are received for added
59   // connection_ids. If NULL, a public reset packet is sent with the specified
60   // |version|. DCHECKs that connection_id is not already on the list.
61   void AddConnectionIdToTimeWait(QuicConnectionId connection_id,
62                                  QuicVersion version,
63                                  QuicEncryptedPacket* close_packet);  // Owned.
64
65   // Returns true if the connection_id is in time wait state, false otherwise.
66   // Packets received for this connection_id should not lead to creation of new
67   // QuicSessions.
68   bool IsConnectionIdInTimeWait(QuicConnectionId connection_id) const;
69
70   // Called when a packet is received for a connection_id that is in time wait
71   // state. Sends a public reset packet to the client which sent this
72   // connection_id. Sending of the public reset packet is throttled by using
73   // exponential back off. DCHECKs for the connection_id to be in time wait
74   // state. virtual to override in tests.
75   virtual void ProcessPacket(const IPEndPoint& server_address,
76                              const IPEndPoint& client_address,
77                              QuicConnectionId connection_id,
78                              QuicPacketSequenceNumber sequence_number,
79                              const QuicEncryptedPacket& packet);
80
81   // Called by the dispatcher when the underlying socket becomes writable again,
82   // since we might need to send pending public reset packets which we didn't
83   // send because the underlying socket was write blocked.
84   virtual void OnCanWrite() OVERRIDE;
85
86   // Used to delete connection_id entries that have outlived their time wait
87   // period.
88   void CleanUpOldConnectionIds();
89
90   // Given a ConnectionId that exists in the time wait list, returns the
91   // QuicVersion associated with it.
92   QuicVersion GetQuicVersionFromConnectionId(QuicConnectionId connection_id);
93
94  protected:
95   virtual QuicEncryptedPacket* BuildPublicReset(
96       const QuicPublicResetPacket& packet);
97
98  private:
99   friend class test::QuicTimeWaitListManagerPeer;
100
101   // Internal structure to store pending public reset packets.
102   class QueuedPacket;
103
104   // Decides if a packet should be sent for this connection_id based on the
105   // number of received packets.
106   bool ShouldSendResponse(int received_packet_count);
107
108   // Creates a public reset packet and sends it or queues it to be sent later.
109   void SendPublicReset(const IPEndPoint& server_address,
110                        const IPEndPoint& client_address,
111                        QuicConnectionId connection_id,
112                        QuicPacketSequenceNumber rejected_sequence_number);
113
114   // Either sends the packet and deletes it or makes pending_packets_queue_ the
115   // owner of the packet.
116   void SendOrQueuePacket(QueuedPacket* packet);
117
118   // Sends the packet out. Returns true if the packet was successfully consumed.
119   // If the writer got blocked and did not buffer the packet, we'll need to keep
120   // the packet and retry sending. In case of all other errors we drop the
121   // packet.
122   bool WriteToWire(QueuedPacket* packet);
123
124   // Register the alarm with the epoll server to wake up at appropriate time.
125   void SetConnectionIdCleanUpAlarm();
126
127   // A map from a recently closed connection_id to the number of packets
128   // received after the termination of the connection bound to the
129   // connection_id.
130   struct ConnectionIdData {
131     ConnectionIdData(int num_packets_,
132                      QuicVersion version_,
133                      QuicTime time_added_,
134                      QuicEncryptedPacket* close_packet)
135         : num_packets(num_packets_),
136           version(version_),
137           time_added(time_added_),
138           close_packet(close_packet) {}
139     int num_packets;
140     QuicVersion version;
141     QuicTime time_added;
142     QuicEncryptedPacket* close_packet;
143   };
144
145   // linked_hash_map allows lookup by ConnectionId and traversal in add order.
146   typedef linked_hash_map<QuicConnectionId, ConnectionIdData> ConnectionIdMap;
147   ConnectionIdMap connection_id_map_;
148
149   // Pending public reset packets that need to be sent out to the client
150   // when we are given a chance to write by the dispatcher.
151   std::deque<QueuedPacket*> pending_packets_queue_;
152
153   // Used to schedule alarms to delete old connection_ids which have been in the
154   // list for too long.
155   EpollServer* epoll_server_;
156
157   // Time period for which connection_ids should remain in time wait state.
158   const QuicTime::Delta kTimeWaitPeriod_;
159
160   // Alarm registered with the epoll server to clean up connection_ids that have
161   // out lived their duration in time wait state.
162   scoped_ptr<ConnectionIdCleanUpAlarm> connection_id_clean_up_alarm_;
163
164   // Clock to efficiently measure approximate time from the epoll server.
165   QuicEpollClock clock_;
166
167   // Interface that writes given buffer to the socket.
168   QuicPacketWriter* writer_;
169
170   // Interface that manages blocked writers.
171   QuicServerSessionVisitor* visitor_;
172
173   DISALLOW_COPY_AND_ASSIGN(QuicTimeWaitListManager);
174 };
175
176 }  // namespace tools
177 }  // namespace net
178
179 #endif  // NET_TOOLS_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_