blob: ad2e0ad9cb94b0ae6618922c7a4614aa81cb3824 [file] [log] [blame]
Junxiao Shi0e42c572014-10-05 20:33:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#ifndef NFD_DAEMON_TABLE_DEAD_NONCE_LIST_HPP
27#define NFD_DAEMON_TABLE_DEAD_NONCE_LIST_HPP
28
29#include "common.hpp"
30#include <boost/multi_index_container.hpp>
31#include <boost/multi_index/sequenced_index.hpp>
32#include <boost/multi_index/hashed_index.hpp>
33#include "core/scheduler.hpp"
34
35namespace nfd {
36
37/** \brief represents the Dead Nonce list
38 *
39 * The Dead Nonce List is a global table that supplements PIT for loop detection.
40 * When a Nonce is erased (dead) from PIT entry, the Nonce and the Interest Name is added to
41 * Dead Nonce List, and kept for a duration in which most loops are expected to have occured.
42 *
43 * To reduce memory usage, the Interest Name and Nonce are stored as a 64-bit hash.
44 * There could be false positives (non-looping Interest could be considered looping),
45 * but the probability is small, and the error is recoverable when consumer retransmits
46 * with a different Nonce.
47 *
48 * To reduce memory usage, entries do not have associated timestamps. Instead,
49 * lifetime of entries is controlled by dynamically adjusting the capacity of the container.
50 * At fixed intervals, the MARK, an entry with a special value, is inserted into the container.
51 * The number of MARKs stored in the container reflects the lifetime of entries,
52 * because MARKs are inserted at fixed intervals.
53 */
54class DeadNonceList : noncopyable
55{
56public:
57 /** \brief constructs the Dead Nonce List
58 * \param lifetime duration of the expected lifetime of each nonce,
59 * must be no less than MIN_LIFETIME.
60 * This should be set to the duration in which most loops would have occured.
61 * A loop cannot be detected if delay of the cycle is greater than lifetime.
62 * \throw std::invalid_argument if lifetime is less than MIN_LIFETIME
63 */
64 explicit
65 DeadNonceList(const time::nanoseconds& lifetime = DEFAULT_LIFETIME);
66
67 ~DeadNonceList();
68
69 /** \brief determines if name+nonce exists
70 * \return true if name+nonce exists
71 */
72 bool
73 has(const Name& name, uint32_t nonce) const;
74
75 /** \brief records name+nonce
76 */
77 void
78 add(const Name& name, uint32_t nonce);
79
80 /** \return number of stored Nonces
81 * \note The return value does not contain non-Nonce entries in the index, if any.
82 */
83 size_t
84 size() const;
85
86private: // Entry and Index
87 typedef uint64_t Entry;
88
89 static Entry
90 makeEntry(const Name& name, uint32_t nonce);
91
92 typedef boost::multi_index_container<
93 Entry,
94 boost::multi_index::indexed_by<
95 boost::multi_index::sequenced<>,
96 boost::multi_index::hashed_non_unique<
97 boost::multi_index::identity<Entry>
98 >
99 >
100 > Index;
101
102 typedef Index::nth_index<0>::type Queue;
103 typedef Index::nth_index<1>::type Hashtable;
104
105private: // actual lifetime estimation and capacity control
106 /** \return number of MARKs in the index
107 */
108 size_t
109 countMarks() const;
110
111 /** \brief add a MARK, then record number of MARKs in m_actualMarkCounts
112 */
113 void
114 mark();
115
116 /** \brief adjust capacity according to m_actualMarkCounts
117 *
118 * If all counts are above EXPECTED_MARK_COUNT, reduce capacity to m_capacity * CAPACITY_DOWN.
119 * If all counts are below EXPECTED_MARK_COUNT, increase capacity to m_capacity * CAPACITY_UP.
120 */
121 void
122 adjustCapacity();
123
124 /** \brief evict some entries if index is over capacity
125 */
126 void
127 evictEntries();
128
129public:
130 /// default entry lifetime
131 static const time::nanoseconds DEFAULT_LIFETIME;
132
133 /// minimum entry lifetime
134 static const time::nanoseconds MIN_LIFETIME;
135
136private:
137 time::nanoseconds m_lifetime;
138 Index m_index;
139 Queue& m_queue;
140 Hashtable& m_ht;
141
142PUBLIC_WITH_TESTS_ELSE_PRIVATE: // actual lifetime estimation and capacity control
143
144 // ---- current capacity and hard limits
145
146 /** \brief current capacity of index
147 *
148 * The index size is maintained to be near this capacity.
149 *
150 * The capacity is adjusted so that every Entry is expected to be kept for m_lifetime.
151 * This is achieved by mark() and adjustCapacity().
152 */
153 size_t m_capacity;
154
155 static const size_t INITIAL_CAPACITY;
156
157 /** \brief minimum capacity
158 *
159 * This is to ensure correct algorithm operations.
160 */
161 static const size_t MIN_CAPACITY;
162
163 /** \brief maximum capacity
164 *
165 * This is to limit memory usage.
166 */
167 static const size_t MAX_CAPACITY;
168
169 // ---- actual entry lifetime estimation
170
171 /** \brief the MARK for capacity
172 *
173 * The MARK doesn't have a distinct type.
174 * Entry is a hash. The hash function should have non-invertible property,
175 * so it's unlikely for a usual Entry to have collision with the MARK.
176 */
177 static const Entry MARK;
178
179 /** \brief expected number of MARKs in the index
180 */
181 static const size_t EXPECTED_MARK_COUNT;
182
183 /** \brief number of MARKs in the index after each MARK insertion
184 *
185 * adjustCapacity uses this to determine whether and how to adjust capcity,
186 * and then clears this list.
187 */
188 std::multiset<size_t> m_actualMarkCounts;
189
190 time::nanoseconds m_markInterval;
191
192 scheduler::EventId m_markEvent;
193
194 // ---- capacity adjustments
195
196 static const double CAPACITY_UP;
197
198 static const double CAPACITY_DOWN;
199
200 time::nanoseconds m_adjustCapacityInterval;
201
202 scheduler::EventId m_adjustCapacityEvent;
203
204 /** \brief maximum number of entries to evict at each operation if index is over capacity
205 */
206 static const size_t EVICT_LIMIT;
207};
208
209} // namespace nfd
210
211#endif // NFD_DAEMON_TABLE_DEAD_NONCE_LIST_HPP