1 // Copyright 2014 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 // Handles packets for connection_ids in time wait state by discarding the
6 // packet and sending the clients a public reset packet with exponential
9 #ifndef NET_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_
10 #define NET_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_
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_connection_helper.h"
20 #include "net/quic/quic_framer.h"
21 #include "net/quic/quic_packet_writer.h"
22 #include "net/quic/quic_protocol.h"
26 class ConnectionIdCleanUpAlarm
;
27 class QuicServerSessionVisitor
;
30 class QuicTimeWaitListManagerPeer
;
33 // Maintains a list of all connection_ids that have been recently closed. A
34 // connection_id lives in this state for time_wait_period_. All packets received
35 // for connection_ids in this state are handed over to the
36 // QuicTimeWaitListManager by the QuicDispatcher. Decides whether to send a
37 // public reset packet, a copy of the previously sent connection close packet,
38 // or nothing to the client which sent a packet with the connection_id in time
39 // wait state. After the connection_id expires its time wait period, a new
40 // connection/session will be created if a packet is received for this
42 class QuicTimeWaitListManager
: public QuicBlockedWriterInterface
{
44 // writer - the entity that writes to the socket. (Owned by the dispatcher)
45 // visitor - the entity that manages blocked writers. (The dispatcher)
46 // helper - used to run clean up alarms. (Owned by the owner of the server)
47 QuicTimeWaitListManager(QuicPacketWriter
* writer
,
48 QuicServerSessionVisitor
* visitor
,
49 QuicConnectionHelperInterface
* helper
,
50 const QuicVersionVector
& supported_versions
);
51 ~QuicTimeWaitListManager() override
;
53 // Adds the given connection_id to time wait state for time_wait_period_.
54 // Henceforth, any packet bearing this connection_id should not be processed
55 // while the connection_id remains in this list. If a non-nullptr
56 // |close_packet| is provided, the TimeWaitListManager takes ownership of it
57 // and sends it again when packets are received for added connection_ids. If
58 // nullptr, a public reset packet is sent with the specified |version|.
59 // DCHECKs that connection_id is not already on the list. "virtual" to
61 virtual void AddConnectionIdToTimeWait(QuicConnectionId connection_id
,
63 QuicEncryptedPacket
* close_packet
);
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
68 bool IsConnectionIdInTimeWait(QuicConnectionId connection_id
) const;
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
);
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 void OnCanWrite() override
;
86 // Used to delete connection_id entries that have outlived their time wait
88 void CleanUpOldConnectionIds();
90 // If necessary, trims the oldest connections from the time-wait list until
91 // the size is under the configured maximum.
92 void TrimTimeWaitListIfNeeded();
94 // Given a ConnectionId that exists in the time wait list, returns the
95 // QuicVersion associated with it.
96 QuicVersion
GetQuicVersionFromConnectionId(QuicConnectionId connection_id
);
98 // The number of connections on the time-wait list.
99 size_t num_connections() const { return connection_id_map_
.size(); }
102 virtual QuicEncryptedPacket
* BuildPublicReset(
103 const QuicPublicResetPacket
& packet
);
106 friend class test::QuicTimeWaitListManagerPeer
;
108 // Internal structure to store pending public reset packets.
111 // Decides if a packet should be sent for this connection_id based on the
112 // number of received packets.
113 bool ShouldSendResponse(int received_packet_count
);
115 // Creates a public reset packet and sends it or queues it to be sent later.
116 void SendPublicReset(const IPEndPoint
& server_address
,
117 const IPEndPoint
& client_address
,
118 QuicConnectionId connection_id
,
119 QuicPacketSequenceNumber rejected_sequence_number
);
121 // Either sends the packet and deletes it or makes pending_packets_queue_ the
122 // owner of the packet.
123 void SendOrQueuePacket(QueuedPacket
* packet
);
125 // Sends the packet out. Returns true if the packet was successfully consumed.
126 // If the writer got blocked and did not buffer the packet, we'll need to keep
127 // the packet and retry sending. In case of all other errors we drop the
129 bool WriteToWire(QueuedPacket
* packet
);
131 // Register the alarm to wake up at appropriate time.
132 void SetConnectionIdCleanUpAlarm();
134 // Removes the oldest connection from the time-wait list if it was added prior
135 // to "expiration_time". To unconditionally remove the oldest connection, use
136 // a QuicTime::Delta:Infinity(). This function modifies the
137 // connection_id_map_. If you plan to call this function in a loop, any
138 // iterators that you hold before the call to this function may be invalid
139 // afterward. Returns true if the oldest connection was expired. Returns
140 // false if the map is empty or the oldest connection has not expired.
141 bool MaybeExpireOldestConnection(QuicTime expiration_time
);
143 // A map from a recently closed connection_id to the number of packets
144 // received after the termination of the connection bound to the
146 struct ConnectionIdData
{
147 ConnectionIdData(int num_packets_
,
148 QuicVersion version_
,
149 QuicTime time_added_
,
150 QuicEncryptedPacket
* close_packet
)
151 : num_packets(num_packets_
),
153 time_added(time_added_
),
154 close_packet(close_packet
) {}
158 QuicEncryptedPacket
* close_packet
;
161 // linked_hash_map allows lookup by ConnectionId and traversal in add order.
162 typedef linked_hash_map
<QuicConnectionId
, ConnectionIdData
> ConnectionIdMap
;
163 ConnectionIdMap connection_id_map_
;
165 // Pending public reset packets that need to be sent out to the client
166 // when we are given a chance to write by the dispatcher.
167 std::deque
<QueuedPacket
*> pending_packets_queue_
;
169 // Used to schedule alarms to delete old connection_ids which have been in the
170 // list for too long.
171 QuicConnectionHelperInterface
* helper_
;
173 // Time period for which connection_ids should remain in time wait state.
174 const QuicTime::Delta time_wait_period_
;
176 // Alarm registered with the connection helper to clean up connection_ids that
177 // have out lived their duration in time wait state.
178 scoped_ptr
<QuicAlarm
> connection_id_clean_up_alarm_
;
180 // Interface that writes given buffer to the socket.
181 QuicPacketWriter
* writer_
;
183 // Interface that manages blocked writers.
184 QuicServerSessionVisitor
* visitor_
;
186 DISALLOW_COPY_AND_ASSIGN(QuicTimeWaitListManager
);
191 #endif // NET_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_