blob: dea1d51f4d4ef204ef67d262e70802019d9148cd [file] [log] [blame]
Junxiao Shi0e42c572014-10-05 20:33:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento50a6af32019-02-21 00:04:40 -05002/*
3 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shi9f5b01d2016-08-05 03:54:28 +00004 * 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.
Junxiao Shi0e42c572014-10-05 20:33:48 -070010 *
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
Junxiao Shi9f5b01d2016-08-05 03:54:28 +000029#include "core/common.hpp"
Junxiao Shi0e42c572014-10-05 20:33:48 -070030
Davide Pesavento50a6af32019-02-21 00:04:40 -050031#include <boost/multi_index_container.hpp>
32#include <boost/multi_index/hashed_index.hpp>
33#include <boost/multi_index/sequenced_index.hpp>
34
Junxiao Shi0e42c572014-10-05 20:33:48 -070035namespace nfd {
36
Davide Pesavento50a6af32019-02-21 00:04:40 -050037/** \brief Represents the Dead Nonce List
Junxiao Shi0e42c572014-10-05 20:33:48 -070038 *
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:
Davide Pesavento50a6af32019-02-21 00:04:40 -050057 /** \brief Constructs the Dead Nonce List
Junxiao Shi0e42c572014-10-05 20:33:48 -070058 * \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
Davide Pesavento50a6af32019-02-21 00:04:40 -050065 DeadNonceList(time::nanoseconds lifetime = DEFAULT_LIFETIME);
Junxiao Shi0e42c572014-10-05 20:33:48 -070066
67 ~DeadNonceList();
68
Davide Pesavento50a6af32019-02-21 00:04:40 -050069 /** \brief Determines if name+nonce exists
Junxiao Shi0e42c572014-10-05 20:33:48 -070070 * \return true if name+nonce exists
71 */
72 bool
73 has(const Name& name, uint32_t nonce) const;
74
Davide Pesavento50a6af32019-02-21 00:04:40 -050075 /** \brief Records name+nonce
Junxiao Shi0e42c572014-10-05 20:33:48 -070076 */
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 */
Davide Pesavento50a6af32019-02-21 00:04:40 -050088 time::nanoseconds
89 getLifetime() const
90 {
91 return m_lifetime;
92 }
Junxiao Shia110f262014-10-12 12:35:20 -070093
Junxiao Shi0e42c572014-10-05 20:33:48 -070094private: // Entry and Index
95 typedef uint64_t Entry;
96
97 static Entry
98 makeEntry(const Name& name, uint32_t nonce);
99
100 typedef boost::multi_index_container<
101 Entry,
102 boost::multi_index::indexed_by<
103 boost::multi_index::sequenced<>,
104 boost::multi_index::hashed_non_unique<
105 boost::multi_index::identity<Entry>
106 >
107 >
108 > Index;
109
110 typedef Index::nth_index<0>::type Queue;
111 typedef Index::nth_index<1>::type Hashtable;
112
113private: // actual lifetime estimation and capacity control
Davide Pesavento50a6af32019-02-21 00:04:40 -0500114 /** \brief Return the number of MARKs in the index
Junxiao Shi0e42c572014-10-05 20:33:48 -0700115 */
116 size_t
117 countMarks() const;
118
Davide Pesavento50a6af32019-02-21 00:04:40 -0500119 /** \brief Add a MARK, then record number of MARKs in m_actualMarkCounts
Junxiao Shi0e42c572014-10-05 20:33:48 -0700120 */
121 void
122 mark();
123
Davide Pesavento50a6af32019-02-21 00:04:40 -0500124 /** \brief Adjust capacity according to m_actualMarkCounts
Junxiao Shi0e42c572014-10-05 20:33:48 -0700125 *
126 * If all counts are above EXPECTED_MARK_COUNT, reduce capacity to m_capacity * CAPACITY_DOWN.
127 * If all counts are below EXPECTED_MARK_COUNT, increase capacity to m_capacity * CAPACITY_UP.
128 */
129 void
130 adjustCapacity();
131
Davide Pesavento50a6af32019-02-21 00:04:40 -0500132 /** \brief Evict some entries if index is over capacity
Junxiao Shi0e42c572014-10-05 20:33:48 -0700133 */
134 void
135 evictEntries();
136
137public:
Davide Pesavento50a6af32019-02-21 00:04:40 -0500138 /// Default entry lifetime
Junxiao Shi0e42c572014-10-05 20:33:48 -0700139 static const time::nanoseconds DEFAULT_LIFETIME;
Davide Pesavento50a6af32019-02-21 00:04:40 -0500140 /// Minimum entry lifetime
Junxiao Shi0e42c572014-10-05 20:33:48 -0700141 static const time::nanoseconds MIN_LIFETIME;
142
143private:
144 time::nanoseconds m_lifetime;
145 Index m_index;
146 Queue& m_queue;
147 Hashtable& m_ht;
148
149PUBLIC_WITH_TESTS_ELSE_PRIVATE: // actual lifetime estimation and capacity control
150
151 // ---- current capacity and hard limits
152
Davide Pesavento50a6af32019-02-21 00:04:40 -0500153 /** \brief Current capacity of index
Junxiao Shi0e42c572014-10-05 20:33:48 -0700154 *
155 * The index size is maintained to be near this capacity.
156 *
157 * The capacity is adjusted so that every Entry is expected to be kept for m_lifetime.
158 * This is achieved by mark() and adjustCapacity().
159 */
160 size_t m_capacity;
161
162 static const size_t INITIAL_CAPACITY;
163
Davide Pesavento50a6af32019-02-21 00:04:40 -0500164 /** \brief Minimum capacity
Junxiao Shi0e42c572014-10-05 20:33:48 -0700165 *
166 * This is to ensure correct algorithm operations.
167 */
168 static const size_t MIN_CAPACITY;
169
Davide Pesavento50a6af32019-02-21 00:04:40 -0500170 /** \brief Maximum capacity
Junxiao Shi0e42c572014-10-05 20:33:48 -0700171 *
172 * This is to limit memory usage.
173 */
174 static const size_t MAX_CAPACITY;
175
176 // ---- actual entry lifetime estimation
177
Davide Pesavento50a6af32019-02-21 00:04:40 -0500178 /** \brief The MARK for capacity
Junxiao Shi0e42c572014-10-05 20:33:48 -0700179 *
180 * The MARK doesn't have a distinct type.
181 * Entry is a hash. The hash function should have non-invertible property,
182 * so it's unlikely for a usual Entry to have collision with the MARK.
183 */
184 static const Entry MARK;
185
Davide Pesavento50a6af32019-02-21 00:04:40 -0500186 /** \brief Expected number of MARKs in the index
Junxiao Shi0e42c572014-10-05 20:33:48 -0700187 */
188 static const size_t EXPECTED_MARK_COUNT;
189
Davide Pesavento50a6af32019-02-21 00:04:40 -0500190 /** \brief Number of MARKs in the index after each MARK insertion
Junxiao Shi0e42c572014-10-05 20:33:48 -0700191 *
Davide Pesavento50a6af32019-02-21 00:04:40 -0500192 * adjustCapacity() uses this to determine whether and how to adjust capcity,
Junxiao Shi0e42c572014-10-05 20:33:48 -0700193 * and then clears this list.
194 */
195 std::multiset<size_t> m_actualMarkCounts;
196
197 time::nanoseconds m_markInterval;
Junxiao Shi0e42c572014-10-05 20:33:48 -0700198 scheduler::EventId m_markEvent;
199
200 // ---- capacity adjustments
201
202 static const double CAPACITY_UP;
Junxiao Shi0e42c572014-10-05 20:33:48 -0700203 static const double CAPACITY_DOWN;
Junxiao Shi0e42c572014-10-05 20:33:48 -0700204 time::nanoseconds m_adjustCapacityInterval;
Junxiao Shi0e42c572014-10-05 20:33:48 -0700205 scheduler::EventId m_adjustCapacityEvent;
206
Davide Pesavento50a6af32019-02-21 00:04:40 -0500207 /// Maximum number of entries to evict at each operation if index is over capacity
Junxiao Shi0e42c572014-10-05 20:33:48 -0700208 static const size_t EVICT_LIMIT;
209};
210
Junxiao Shi0e42c572014-10-05 20:33:48 -0700211} // namespace nfd
212
213#endif // NFD_DAEMON_TABLE_DEAD_NONCE_LIST_HPP