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 "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"
28 class ConnectionIdCleanUpAlarm;
29 class QuicServerSessionVisitor;
32 class QuicTimeWaitListManagerPeer;
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
44 class QuicTimeWaitListManager : public QuicBlockedWriterInterface {
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();
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,
63 QuicEncryptedPacket* close_packet); // Owned.
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 virtual void OnCanWrite() OVERRIDE;
86 // Used to delete connection_id entries that have outlived their time wait
88 void CleanUpOldConnectionIds();
90 // Given a ConnectionId that exists in the time wait list, returns the
91 // QuicVersion associated with it.
92 QuicVersion GetQuicVersionFromConnectionId(QuicConnectionId connection_id);
95 virtual QuicEncryptedPacket* BuildPublicReset(
96 const QuicPublicResetPacket& packet);
99 friend class test::QuicTimeWaitListManagerPeer;
101 // Internal structure to store pending public reset packets.
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);
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);
114 // Either sends the packet and deletes it or makes pending_packets_queue_ the
115 // owner of the packet.
116 void SendOrQueuePacket(QueuedPacket* packet);
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
122 bool WriteToWire(QueuedPacket* packet);
124 // Register the alarm with the epoll server to wake up at appropriate time.
125 void SetConnectionIdCleanUpAlarm();
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
130 struct ConnectionIdData {
131 ConnectionIdData(int num_packets_,
132 QuicVersion version_,
133 QuicTime time_added_,
134 QuicEncryptedPacket* close_packet)
135 : num_packets(num_packets_),
137 time_added(time_added_),
138 close_packet(close_packet) {}
142 QuicEncryptedPacket* close_packet;
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_;
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_;
153 // Used to schedule alarms to delete old connection_ids which have been in the
154 // list for too long.
155 EpollServer* epoll_server_;
157 // Time period for which connection_ids should remain in time wait state.
158 const QuicTime::Delta kTimeWaitPeriod_;
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_;
164 // Clock to efficiently measure approximate time from the epoll server.
165 QuicEpollClock clock_;
167 // Interface that writes given buffer to the socket.
168 QuicPacketWriter* writer_;
170 // Interface that manages blocked writers.
171 QuicServerSessionVisitor* visitor_;
173 DISALLOW_COPY_AND_ASSIGN(QuicTimeWaitListManager);
179 #endif // NET_TOOLS_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_