Land Recent QUIC Changes.
[chromium-blink-merge.git] / net / tools / quic / quic_time_wait_list_manager.h
blobca8e1ee2995fe685183075858dcbe562a503b4e9
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 guids in time wait state by discarding the packet and
6 // sending the clients a public reset packet with exponential backoff.
8 #ifndef NET_TOOLS_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_
9 #define NET_TOOLS_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_
11 #include <deque>
13 #include "base/containers/hash_tables.h"
14 #include "base/strings/string_piece.h"
15 #include "net/quic/quic_blocked_writer_interface.h"
16 #include "net/quic/quic_framer.h"
17 #include "net/quic/quic_packet_writer.h"
18 #include "net/quic/quic_protocol.h"
19 #include "net/tools/epoll_server/epoll_server.h"
20 #include "net/tools/quic/quic_epoll_clock.h"
22 namespace net {
23 namespace tools {
25 class GuidCleanUpAlarm;
27 namespace test {
28 class QuicTimeWaitListManagerPeer;
29 } // namespace test
31 // Maintains a list of all guids that have been recently closed. A guid lives in
32 // this state for kTimeWaitPeriod. All packets received for guids in this state
33 // are handed over to the QuicTimeWaitListManager by the QuicDispatcher.
34 // Decides whether to send a public reset packet, a copy of the previously sent
35 // connection close packet, or nothing to the client which sent a packet
36 // with the guid in time wait state. After the guid expires its time wait
37 // period, a new connection/session will be created if a packet is received
38 // for this guid.
39 class QuicTimeWaitListManager : public QuicBlockedWriterInterface {
40 public:
41 // writer - the entity that writes to the socket. (Owned by the dispatcher)
42 // epoll_server - used to run clean up alarms. (Owned by the dispatcher)
43 QuicTimeWaitListManager(QuicPacketWriter* writer,
44 EpollServer* epoll_server,
45 const QuicVersionVector& supported_versions);
46 virtual ~QuicTimeWaitListManager();
48 // Adds the given guid to time wait state for kTimeWaitPeriod. Henceforth,
49 // any packet bearing this guid should not be processed while the guid remains
50 // in this list. If a non-NULL |close_packet| is provided, it is sent again
51 // when packets are received for added guids. If NULL, a public reset packet
52 // is sent with the specified |version|. DCHECKs that guid is not already on
53 // the list.
54 void AddGuidToTimeWait(QuicGuid guid,
55 QuicVersion version,
56 QuicEncryptedPacket* close_packet); // Owned.
58 // Returns true if the guid is in time wait state, false otherwise. Packets
59 // received for this guid should not lead to creation of new QuicSessions.
60 bool IsGuidInTimeWait(QuicGuid guid) const;
62 // Called when a packet is received for a guid that is in time wait state.
63 // Sends a public reset packet to the client which sent this guid. Sending
64 // of the public reset packet is throttled by using exponential back off.
65 // DCHECKs for the guid to be in time wait state.
66 // virtual to override in tests.
67 virtual void ProcessPacket(const IPEndPoint& server_address,
68 const IPEndPoint& client_address,
69 QuicGuid guid,
70 QuicPacketSequenceNumber sequence_number);
72 // Called by the dispatcher when the underlying socket becomes writable again,
73 // since we might need to send pending public reset packets which we didn't
74 // send because the underlying socket was write blocked.
75 virtual bool OnCanWrite() OVERRIDE;
77 // Used to delete guid entries that have outlived their time wait period.
78 void CleanUpOldGuids();
80 // Given a GUID that exists in the time wait list, returns the QuicVersion
81 // associated with it.
82 QuicVersion GetQuicVersionFromGuid(QuicGuid guid);
84 private:
85 friend class test::QuicTimeWaitListManagerPeer;
87 // Stores the guid and the time it was added to time wait state.
88 struct GuidAddTime;
89 // Internal structure to store pending public reset packets.
90 class QueuedPacket;
92 // Decides if a packet should be sent for this guid based on the number of
93 // received packets.
94 bool ShouldSendResponse(int received_packet_count);
96 // Creates a public reset packet and sends it or queues it to be sent later.
97 void SendPublicReset(const IPEndPoint& server_address,
98 const IPEndPoint& client_address,
99 QuicGuid guid,
100 QuicPacketSequenceNumber rejected_sequence_number);
102 // Either sends the packet and deletes it or makes pending_packets_queue_ the
103 // owner of the packet.
104 void SendOrQueuePacket(QueuedPacket* packet);
106 // Should only be called when write_blocked_ == false. We only care if the
107 // writing was unsuccessful because the socket got blocked, which can be
108 // tested using write_blocked_ == true. In case of all other errors we drop
109 // the packet. Hence, we return void.
110 void WriteToWire(QueuedPacket* packet);
112 // Register the alarm with the epoll server to wake up at appropriate time.
113 void SetGuidCleanUpAlarm();
115 // A map from a recently closed guid to the number of packets received after
116 // the termination of the connection bound to the guid.
117 struct GuidData {
118 GuidData(int num_packets_,
119 QuicVersion version_,
120 QuicEncryptedPacket* close_packet)
121 : num_packets(num_packets_),
122 version(version_),
123 close_packet(close_packet) {}
124 int num_packets;
125 QuicVersion version;
126 QuicEncryptedPacket* close_packet;
128 base::hash_map<QuicGuid, GuidData> guid_map_;
129 typedef base::hash_map<QuicGuid, GuidData>::iterator GuidMapIterator;
131 // Maintains a list of GuidAddTime elements which it owns, in the
132 // order they should be deleted.
133 std::deque<GuidAddTime*> time_ordered_guid_list_;
135 // Pending public reset packets that need to be sent out to the client
136 // when we are given a chance to write by the dispatcher.
137 std::deque<QueuedPacket*> pending_packets_queue_;
139 // Used to schedule alarms to delete old guids which have been in the list for
140 // too long. Owned by the dispatcher.
141 EpollServer* epoll_server_;
143 // Time period for which guids should remain in time wait state.
144 const QuicTime::Delta kTimeWaitPeriod_;
146 // Alarm registered with the epoll server to clean up guids that have out
147 // lived their duration in time wait state.
148 scoped_ptr<GuidCleanUpAlarm> guid_clean_up_alarm_;
150 // Clock to efficiently measure approximate time from the epoll server.
151 QuicEpollClock clock_;
153 // Interface that writes given buffer to the socket. Owned by the dispatcher.
154 QuicPacketWriter* writer_;
156 // True if the underlying udp socket is write blocked, i.e will return EAGAIN
157 // on sendmsg.
158 bool is_write_blocked_;
160 DISALLOW_COPY_AND_ASSIGN(QuicTimeWaitListManager);
163 } // namespace tools
164 } // namespace net
166 #endif // NET_TOOLS_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_