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