Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2021, Regents of the University of California, |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 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. |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 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 | |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 29 | #include "core/common.hpp" |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 31 | #include <boost/multi_index_container.hpp> |
| 32 | #include <boost/multi_index/hashed_index.hpp> |
| 33 | #include <boost/multi_index/sequenced_index.hpp> |
| 34 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 35 | namespace nfd { |
| 36 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 37 | /** |
| 38 | * \brief Represents the Dead Nonce List. |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 39 | * |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 40 | * The Dead Nonce List is a global table that supplements the PIT for loop detection. |
| 41 | * When a Nonce is erased (dead) from a PIT entry, the Nonce and the Interest Name are added to |
| 42 | * the Dead Nonce List and kept for a duration in which most loops are expected to have occured. |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 43 | * |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 44 | * To reduce memory usage, the Interest Name and Nonce are stored as a 64-bit hash. |
| 45 | * The probability of false positives (a non-looping Interest considered as looping) is small |
| 46 | * and a collision is recoverable when the consumer retransmits with a different Nonce. |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 47 | * |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 48 | * To reduce memory usage, entries do not have associated timestamps. Instead, the lifetime |
| 49 | * of the entries is controlled by dynamically adjusting the capacity of the container. |
| 50 | * At fixed intervals, a 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 the entries, |
| 52 | * because MARKs are inserted at fixed intervals. |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 53 | */ |
| 54 | class DeadNonceList : noncopyable |
| 55 | { |
| 56 | public: |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 57 | /** |
| 58 | * \brief Constructs the Dead Nonce List |
| 59 | * \param lifetime expected lifetime of each nonce, must be no less than #MIN_LIFETIME. |
| 60 | * This should be set to a duration over which most loops would have occured. |
| 61 | * A loop cannot be detected if the total delay of the cycle is greater than lifetime. |
| 62 | * \throw std::invalid_argument if lifetime is less than #MIN_LIFETIME |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 63 | */ |
| 64 | explicit |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 65 | DeadNonceList(time::nanoseconds lifetime = DEFAULT_LIFETIME); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 66 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 67 | /** |
| 68 | * \brief Determines if name+nonce is in the list |
| 69 | * \return true if name+nonce exists, false otherwise |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 70 | */ |
| 71 | bool |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 72 | has(const Name& name, Interest::Nonce nonce) const; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 73 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 74 | /** |
| 75 | * \brief Adds name+nonce to the list |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 76 | */ |
| 77 | void |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 78 | add(const Name& name, Interest::Nonce nonce); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 79 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 80 | /** |
| 81 | * \brief Returns the number of stored nonces |
| 82 | * \note The return value does not contain non-Nonce entries in the index, if any. |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 83 | */ |
| 84 | size_t |
| 85 | size() const; |
| 86 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 87 | /** |
| 88 | * \brief Returns the expected nonce lifetime |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 89 | */ |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 90 | time::nanoseconds |
| 91 | getLifetime() const |
| 92 | { |
| 93 | return m_lifetime; |
| 94 | } |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 95 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 96 | private: |
| 97 | using Entry = uint64_t; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 98 | |
| 99 | static Entry |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 100 | makeEntry(const Name& name, Interest::Nonce nonce); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 101 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 102 | /** \brief Return the number of MARKs in the index |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 103 | */ |
| 104 | size_t |
| 105 | countMarks() const; |
| 106 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 107 | /** \brief Add a MARK, then record number of MARKs in m_actualMarkCounts |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 108 | */ |
| 109 | void |
| 110 | mark(); |
| 111 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 112 | /** \brief Adjust capacity according to m_actualMarkCounts |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 113 | * |
| 114 | * If all counts are above EXPECTED_MARK_COUNT, reduce capacity to m_capacity * CAPACITY_DOWN. |
| 115 | * If all counts are below EXPECTED_MARK_COUNT, increase capacity to m_capacity * CAPACITY_UP. |
| 116 | */ |
| 117 | void |
| 118 | adjustCapacity(); |
| 119 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 120 | /** \brief Evict some entries if index is over capacity |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 121 | */ |
| 122 | void |
| 123 | evictEntries(); |
| 124 | |
| 125 | public: |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 126 | /// Default entry lifetime |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 127 | static constexpr time::nanoseconds DEFAULT_LIFETIME = 6_s; |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 128 | /// Minimum entry lifetime |
Davide Pesavento | 7647cc7 | 2021-06-10 14:30:04 -0400 | [diff] [blame] | 129 | static constexpr time::nanoseconds MIN_LIFETIME = 50_ms; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 130 | |
| 131 | private: |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 132 | const time::nanoseconds m_lifetime; |
| 133 | |
Davide Pesavento | e08cc4c | 2021-06-08 18:22:46 -0400 | [diff] [blame] | 134 | struct Queue {}; |
| 135 | struct Hashtable {}; |
| 136 | using Container = boost::multi_index_container< |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 137 | Entry, |
| 138 | boost::multi_index::indexed_by< |
Davide Pesavento | e08cc4c | 2021-06-08 18:22:46 -0400 | [diff] [blame] | 139 | boost::multi_index::sequenced<boost::multi_index::tag<Queue>>, |
| 140 | boost::multi_index::hashed_non_unique<boost::multi_index::tag<Hashtable>, |
| 141 | boost::multi_index::identity<Entry>> |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 142 | > |
| 143 | >; |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 144 | |
Davide Pesavento | e08cc4c | 2021-06-08 18:22:46 -0400 | [diff] [blame] | 145 | Container m_index; |
| 146 | Container::index<Queue>::type& m_queue = m_index.get<Queue>(); |
| 147 | Container::index<Hashtable>::type& m_ht = m_index.get<Hashtable>(); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 148 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 149 | NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 150 | |
| 151 | // ---- current capacity and hard limits |
| 152 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 153 | /** \brief Current capacity of index |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 154 | * |
| 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 | |
Davide Pesavento | 7647cc7 | 2021-06-10 14:30:04 -0400 | [diff] [blame] | 162 | static constexpr size_t INITIAL_CAPACITY = 1 << 14; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 163 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 164 | /** \brief Minimum capacity |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 165 | * |
| 166 | * This is to ensure correct algorithm operations. |
| 167 | */ |
Davide Pesavento | 7647cc7 | 2021-06-10 14:30:04 -0400 | [diff] [blame] | 168 | static constexpr size_t MIN_CAPACITY = 1 << 10; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 169 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 170 | /** \brief Maximum capacity |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 171 | * |
| 172 | * This is to limit memory usage. |
| 173 | */ |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 174 | static constexpr size_t MAX_CAPACITY = 1 << 24; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 175 | |
| 176 | // ---- actual entry lifetime estimation |
| 177 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 178 | /** \brief The MARK for capacity |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 179 | * |
| 180 | * The MARK doesn't have a distinct type. |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 181 | * Entry is a hash. The hash function should be non-invertible, so that |
| 182 | * it's infeasible to craft a "normal" Entry that collides with the MARK. |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 183 | */ |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 184 | static constexpr Entry MARK = 0; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 185 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 186 | /// Expected number of MARKs in the index |
| 187 | static constexpr size_t EXPECTED_MARK_COUNT = 5; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 188 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 189 | /** \brief Number of MARKs in the index after each MARK insertion |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 190 | * |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 191 | * adjustCapacity() uses this to determine whether and how to adjust capcity, |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 192 | * and then clears this list. |
| 193 | */ |
| 194 | std::multiset<size_t> m_actualMarkCounts; |
| 195 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 196 | const time::nanoseconds m_markInterval; |
| 197 | scheduler::ScopedEventId m_markEvent; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 198 | |
| 199 | // ---- capacity adjustments |
| 200 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 201 | static constexpr double CAPACITY_UP = 1.2; |
| 202 | static constexpr double CAPACITY_DOWN = 0.9; |
| 203 | const time::nanoseconds m_adjustCapacityInterval; |
| 204 | scheduler::ScopedEventId m_adjustCapacityEvent; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 205 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 206 | /// Maximum number of entries to evict at each operation if the index is over capacity |
| 207 | static constexpr size_t EVICT_LIMIT = 64; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 208 | }; |
| 209 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 210 | } // namespace nfd |
| 211 | |
| 212 | #endif // NFD_DAEMON_TABLE_DEAD_NONCE_LIST_HPP |