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.
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_TOOLS_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_
10 #define NET_TOOLS_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_
14 #include "base/basictypes.h"
15 #include "net/base/linked_hash_map.h"
16 #include "net/quic/quic_blocked_writer_interface.h"
17 #include "net/quic/quic_connection.h"
18 #include "net/quic/quic_framer.h"
19 #include "net/quic/quic_packet_writer.h"
20 #include "net/quic/quic_protocol.h"
25 class QuicServerSessionVisitor
;
28 class QuicTimeWaitListManagerPeer
;
31 // Maintains a list of all connection_ids that have been recently closed. A
32 // connection_id lives in this state for time_wait_period_. All packets received
33 // for connection_ids in this state are handed over to the
34 // QuicTimeWaitListManager by the QuicDispatcher. Decides whether to send a
35 // public reset packet, a copy of the previously sent connection close packet,
36 // or nothing to the client which sent a packet with the connection_id in time
37 // wait state. After the connection_id expires its time wait period, a new
38 // connection/session will be created if a packet is received for this
40 class QuicTimeWaitListManager
: public QuicBlockedWriterInterface
{
42 // writer - the entity that writes to the socket. (Owned by the dispatcher)
43 // visitor - the entity that manages blocked writers. (The dispatcher)
44 // helper - used to run clean up alarms. (Owned by the dispatcher)
45 QuicTimeWaitListManager(QuicPacketWriter
* writer
,
46 QuicServerSessionVisitor
* visitor
,
47 QuicConnectionHelperInterface
* helper
,
48 const QuicVersionVector
& supported_versions
);
49 ~QuicTimeWaitListManager() override
;
51 // Adds the given connection_id to time wait state for time_wait_period_.
52 // Henceforth, any packet bearing this connection_id should not be processed
53 // while the connection_id remains in this list. If a non-nullptr
54 // |close_packet| is provided, the TimeWaitListManager takes ownership of it
55 // and sends it again when packets are received for added connection_ids. If
56 // nullptr, a public reset packet is sent with the specified |version|.
57 // DCHECKs that connection_id is not already on the list. "virtual" to
58 // override in tests. If "connection_rejected_statelessly" is true, it means
59 // that the connection was closed due to a stateless reject, and no close
60 // packet is expected. Any packets that are received for connection_id will
62 // TODO(jokulik): In the future, we plan send (redundant) SREJ packets back to
63 // the client in response to stray data-packets that arrive after the first
64 // SREJ. This requires some new plumbing, so we black-hole for now.
65 virtual void AddConnectionIdToTimeWait(QuicConnectionId connection_id
,
67 bool connection_rejected_statelessly
,
68 QuicEncryptedPacket
* close_packet
);
70 // Returns true if the connection_id is in time wait state, false otherwise.
71 // Packets received for this connection_id should not lead to creation of new
73 bool IsConnectionIdInTimeWait(QuicConnectionId connection_id
) const;
75 // Called when a packet is received for a connection_id that is in time wait
76 // state. Sends a public reset packet to the client which sent this
77 // connection_id. Sending of the public reset packet is throttled by using
78 // exponential back off. DCHECKs for the connection_id to be in time wait
79 // state. virtual to override in tests.
80 virtual void ProcessPacket(const IPEndPoint
& server_address
,
81 const IPEndPoint
& client_address
,
82 QuicConnectionId connection_id
,
83 QuicPacketSequenceNumber sequence_number
,
84 const QuicEncryptedPacket
& packet
);
86 // Called by the dispatcher when the underlying socket becomes writable again,
87 // since we might need to send pending public reset packets which we didn't
88 // send because the underlying socket was write blocked.
89 void OnCanWrite() override
;
91 // Used to delete connection_id entries that have outlived their time wait
93 void CleanUpOldConnectionIds();
95 // If necessary, trims the oldest connections from the time-wait list until
96 // the size is under the configured maximum.
97 void TrimTimeWaitListIfNeeded();
99 // Given a ConnectionId that exists in the time wait list, returns the
100 // QuicVersion associated with it.
101 QuicVersion
GetQuicVersionFromConnectionId(QuicConnectionId connection_id
);
103 // The number of connections on the time-wait list.
104 size_t num_connections() const { return connection_id_map_
.size(); }
107 virtual QuicEncryptedPacket
* BuildPublicReset(
108 const QuicPublicResetPacket
& packet
);
111 friend class test::QuicTimeWaitListManagerPeer
;
113 // Internal structure to store pending public reset packets.
116 // Decides if a packet should be sent for this connection_id based on the
117 // number of received packets.
118 bool ShouldSendResponse(int received_packet_count
);
120 // Creates a public reset packet and sends it or queues it to be sent later.
121 void SendPublicReset(const IPEndPoint
& server_address
,
122 const IPEndPoint
& client_address
,
123 QuicConnectionId connection_id
,
124 QuicPacketSequenceNumber rejected_sequence_number
);
126 // Either sends the packet and deletes it or makes pending_packets_queue_ the
127 // owner of the packet.
128 void SendOrQueuePacket(QueuedPacket
* packet
);
130 // Sends the packet out. Returns true if the packet was successfully consumed.
131 // If the writer got blocked and did not buffer the packet, we'll need to keep
132 // the packet and retry sending. In case of all other errors we drop the
134 bool WriteToWire(QueuedPacket
* packet
);
136 // Register the alarm server to wake up at appropriate time.
137 void SetConnectionIdCleanUpAlarm();
139 // Removes the oldest connection from the time-wait list if it was added prior
140 // to "expiration_time". To unconditionally remove the oldest connection, use
141 // a QuicTime::Delta:Infinity(). This function modifies the
142 // connection_id_map_. If you plan to call this function in a loop, any
143 // iterators that you hold before the call to this function may be invalid
144 // afterward. Returns true if the oldest connection was expired. Returns
145 // false if the map is empty or the oldest connection has not expired.
146 bool MaybeExpireOldestConnection(QuicTime expiration_time
);
148 // A map from a recently closed connection_id to the number of packets
149 // received after the termination of the connection bound to the
151 struct ConnectionIdData
{
152 ConnectionIdData(int num_packets_
,
153 QuicVersion version_
,
154 QuicTime time_added_
,
155 QuicEncryptedPacket
* close_packet
,
156 bool connection_rejected_statelessly
)
157 : num_packets(num_packets_
),
159 time_added(time_added_
),
160 close_packet(close_packet
),
161 connection_rejected_statelessly(connection_rejected_statelessly
) {}
165 QuicEncryptedPacket
* close_packet
;
166 bool connection_rejected_statelessly
;
169 // linked_hash_map allows lookup by ConnectionId and traversal in add order.
170 typedef linked_hash_map
<QuicConnectionId
, ConnectionIdData
> ConnectionIdMap
;
171 ConnectionIdMap connection_id_map_
;
173 // Pending public reset packets that need to be sent out to the client
174 // when we are given a chance to write by the dispatcher.
175 std::deque
<QueuedPacket
*> pending_packets_queue_
;
177 // Time period for which connection_ids should remain in time wait state.
178 const QuicTime::Delta time_wait_period_
;
180 // Alarm to clean up connection_ids that have out lived their duration in
182 scoped_ptr
<QuicAlarm
> connection_id_clean_up_alarm_
;
184 // Clock to efficiently measure approximate time.
185 const QuicClock
* clock_
;
187 // Interface that writes given buffer to the socket.
188 QuicPacketWriter
* writer_
;
190 // Interface that manages blocked writers.
191 QuicServerSessionVisitor
* visitor_
;
193 DISALLOW_COPY_AND_ASSIGN(QuicTimeWaitListManager
);
199 #endif // NET_TOOLS_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_