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