blob: 1c7f7ccc4e3d37b1acb878f21b300433b8eb3e1e [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
Junxiao Shia110f262014-10-12 12:35:20 -070086 /** \return expected lifetime
87 */
88 const time::nanoseconds&
89 getLifetime() const;
90
Junxiao Shi0e42c572014-10-05 20:33:48 -070091private: // Entry and Index
92 typedef uint64_t Entry;
93
94 static Entry
95 makeEntry(const Name& name, uint32_t nonce);
96
97 typedef boost::multi_index_container<
98 Entry,
99 boost::multi_index::indexed_by<
100 boost::multi_index::sequenced<>,
101 boost::multi_index::hashed_non_unique<
102 boost::multi_index::identity<Entry>
103 >
104 >
105 > Index;
106
107 typedef Index::nth_index<0>::type Queue;
108 typedef Index::nth_index<1>::type Hashtable;
109
110private: // actual lifetime estimation and capacity control
111 /** \return number of MARKs in the index
112 */
113 size_t
114 countMarks() const;
115
116 /** \brief add a MARK, then record number of MARKs in m_actualMarkCounts
117 */
118 void
119 mark();
120
121 /** \brief adjust capacity according to m_actualMarkCounts
122 *
123 * If all counts are above EXPECTED_MARK_COUNT, reduce capacity to m_capacity * CAPACITY_DOWN.
124 * If all counts are below EXPECTED_MARK_COUNT, increase capacity to m_capacity * CAPACITY_UP.
125 */
126 void
127 adjustCapacity();
128
129 /** \brief evict some entries if index is over capacity
130 */
131 void
132 evictEntries();
133
134public:
135 /// default entry lifetime
136 static const time::nanoseconds DEFAULT_LIFETIME;
137
138 /// minimum entry lifetime
139 static const time::nanoseconds MIN_LIFETIME;
140
141private:
142 time::nanoseconds m_lifetime;
143 Index m_index;
144 Queue& m_queue;
145 Hashtable& m_ht;
146
147PUBLIC_WITH_TESTS_ELSE_PRIVATE: // actual lifetime estimation and capacity control
148
149 // ---- current capacity and hard limits
150
151 /** \brief current capacity of index
152 *
153 * The index size is maintained to be near this capacity.
154 *
155 * The capacity is adjusted so that every Entry is expected to be kept for m_lifetime.
156 * This is achieved by mark() and adjustCapacity().
157 */
158 size_t m_capacity;
159
160 static const size_t INITIAL_CAPACITY;
161
162 /** \brief minimum capacity
163 *
164 * This is to ensure correct algorithm operations.
165 */
166 static const size_t MIN_CAPACITY;
167
168 /** \brief maximum capacity
169 *
170 * This is to limit memory usage.
171 */
172 static const size_t MAX_CAPACITY;
173
174 // ---- actual entry lifetime estimation
175
176 /** \brief the MARK for capacity
177 *
178 * The MARK doesn't have a distinct type.
179 * Entry is a hash. The hash function should have non-invertible property,
180 * so it's unlikely for a usual Entry to have collision with the MARK.
181 */
182 static const Entry MARK;
183
184 /** \brief expected number of MARKs in the index
185 */
186 static const size_t EXPECTED_MARK_COUNT;
187
188 /** \brief number of MARKs in the index after each MARK insertion
189 *
190 * adjustCapacity uses this to determine whether and how to adjust capcity,
191 * and then clears this list.
192 */
193 std::multiset<size_t> m_actualMarkCounts;
194
195 time::nanoseconds m_markInterval;
196
197 scheduler::EventId m_markEvent;
198
199 // ---- capacity adjustments
200
201 static const double CAPACITY_UP;
202
203 static const double CAPACITY_DOWN;
204
205 time::nanoseconds m_adjustCapacityInterval;
206
207 scheduler::EventId m_adjustCapacityEvent;
208
209 /** \brief maximum number of entries to evict at each operation if index is over capacity
210 */
211 static const size_t EVICT_LIMIT;
212};
213
Junxiao Shia110f262014-10-12 12:35:20 -0700214inline const time::nanoseconds&
215DeadNonceList::getLifetime() const
216{
217 return m_lifetime;
218}
219
Junxiao Shi0e42c572014-10-05 20:33:48 -0700220} // namespace nfd
221
222#endif // NFD_DAEMON_TABLE_DEAD_NONCE_LIST_HPP