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 | /* |
| 3 | * Copyright (c) 2014-2019, 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 | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 37 | /** \brief Represents the Dead Nonce List |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 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 | */ |
| 54 | class DeadNonceList : noncopyable |
| 55 | { |
| 56 | public: |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 57 | /** \brief Constructs the Dead Nonce List |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 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 |
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 | |
| 67 | ~DeadNonceList(); |
| 68 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 69 | /** \brief Determines if name+nonce exists |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 70 | * \return true if name+nonce exists |
| 71 | */ |
| 72 | bool |
| 73 | has(const Name& name, uint32_t nonce) const; |
| 74 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 75 | /** \brief Records name+nonce |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 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 Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 86 | /** \return expected lifetime |
| 87 | */ |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 88 | time::nanoseconds |
| 89 | getLifetime() const |
| 90 | { |
| 91 | return m_lifetime; |
| 92 | } |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 93 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 94 | private: // 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 | |
| 113 | private: // actual lifetime estimation and capacity control |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 114 | /** \brief Return the number of MARKs in the index |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 115 | */ |
| 116 | size_t |
| 117 | countMarks() const; |
| 118 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 119 | /** \brief Add a MARK, then record number of MARKs in m_actualMarkCounts |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 120 | */ |
| 121 | void |
| 122 | mark(); |
| 123 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 124 | /** \brief Adjust capacity according to m_actualMarkCounts |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 125 | * |
| 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 Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 132 | /** \brief Evict some entries if index is over capacity |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 133 | */ |
| 134 | void |
| 135 | evictEntries(); |
| 136 | |
| 137 | public: |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 138 | /// Default entry lifetime |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 139 | static const time::nanoseconds DEFAULT_LIFETIME; |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 140 | /// Minimum entry lifetime |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 141 | static const time::nanoseconds MIN_LIFETIME; |
| 142 | |
| 143 | private: |
| 144 | time::nanoseconds m_lifetime; |
| 145 | Index m_index; |
| 146 | Queue& m_queue; |
| 147 | Hashtable& m_ht; |
| 148 | |
| 149 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: // actual lifetime estimation and capacity control |
| 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 | |
| 162 | static const size_t INITIAL_CAPACITY; |
| 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 | */ |
| 168 | static const size_t MIN_CAPACITY; |
| 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 | */ |
| 174 | static const size_t MAX_CAPACITY; |
| 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. |
| 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 Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 186 | /** \brief Expected number of MARKs in the index |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 187 | */ |
| 188 | static const size_t EXPECTED_MARK_COUNT; |
| 189 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 190 | /** \brief Number of MARKs in the index after each MARK insertion |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 191 | * |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 192 | * adjustCapacity() uses this to determine whether and how to adjust capcity, |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 193 | * and then clears this list. |
| 194 | */ |
| 195 | std::multiset<size_t> m_actualMarkCounts; |
| 196 | |
| 197 | time::nanoseconds m_markInterval; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 198 | scheduler::EventId m_markEvent; |
| 199 | |
| 200 | // ---- capacity adjustments |
| 201 | |
| 202 | static const double CAPACITY_UP; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 203 | static const double CAPACITY_DOWN; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 204 | time::nanoseconds m_adjustCapacityInterval; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 205 | scheduler::EventId m_adjustCapacityEvent; |
| 206 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 207 | /// Maximum number of entries to evict at each operation if index is over capacity |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 208 | static const size_t EVICT_LIMIT; |
| 209 | }; |
| 210 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 211 | } // namespace nfd |
| 212 | |
| 213 | #endif // NFD_DAEMON_TABLE_DEAD_NONCE_LIST_HPP |