Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [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. |
| 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_CS_POLICY_HPP |
| 27 | #define NFD_DAEMON_TABLE_CS_POLICY_HPP |
| 28 | |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 29 | #include "cs-entry.hpp" |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 30 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 31 | #include <functional> |
| 32 | #include <map> |
| 33 | #include <set> |
| 34 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 35 | namespace nfd::cs { |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 36 | |
| 37 | class Cs; |
| 38 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 39 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 40 | * \brief Represents a CS replacement policy. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 41 | */ |
| 42 | class Policy : noncopyable |
| 43 | { |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 44 | public: // registry |
| 45 | template<typename P> |
| 46 | static void |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 47 | registerPolicy(std::string_view policyName = P::POLICY_NAME) |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 48 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 49 | BOOST_ASSERT(!policyName.empty()); |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 50 | auto r = getRegistry().insert_or_assign(std::string(policyName), [] { return make_unique<P>(); }); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 51 | BOOST_VERIFY(r.second); |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 54 | /** |
| 55 | * \brief Returns a cs::Policy identified by \p policyName, |
| 56 | * or nullptr if \p policyName is unknown. |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 57 | */ |
| 58 | static unique_ptr<Policy> |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 59 | create(const std::string& policyName); |
| 60 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 61 | /** |
| 62 | * \brief Returns a list of available policy names. |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 63 | */ |
| 64 | static std::set<std::string> |
| 65 | getPolicyNames(); |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 66 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 67 | public: |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 68 | virtual |
Junxiao Shi | 2af9ca3 | 2016-08-25 21:51:02 +0000 | [diff] [blame] | 69 | ~Policy() = default; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 70 | |
| 71 | const std::string& |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 72 | getName() const noexcept |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 73 | { |
| 74 | return m_policyName; |
| 75 | } |
| 76 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 77 | /** |
| 78 | * \brief Returns a pointer to the associated CS instance. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 79 | */ |
| 80 | Cs* |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 81 | getCs() const noexcept |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 82 | { |
| 83 | return m_cs; |
| 84 | } |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 85 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 86 | /** |
| 87 | * \brief Sets the associated CS instance. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 88 | */ |
| 89 | void |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 90 | setCs(Cs* cs) noexcept |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 91 | { |
| 92 | m_cs = cs; |
| 93 | } |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 94 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 95 | /** |
| 96 | * \brief Gets hard limit (in number of entries). |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 97 | */ |
| 98 | size_t |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 99 | getLimit() const noexcept |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 100 | { |
| 101 | return m_limit; |
| 102 | } |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 103 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 104 | /** \brief Sets hard limit (in number of entries). |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 105 | * \post getLimit() == nMaxEntries |
| 106 | * \post cs.size() <= getLimit() |
| 107 | * |
| 108 | * The policy may evict entries if necessary. |
| 109 | */ |
| 110 | void |
| 111 | setLimit(size_t nMaxEntries); |
| 112 | |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 113 | public: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 114 | /** \brief A reference to a CS entry. |
| 115 | * \note `operator<` of EntryRef compares the Data name enclosed in the Entry. |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 116 | */ |
| 117 | using EntryRef = Table::const_iterator; |
| 118 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 119 | /** \brief %Signal emitted when an entry is being evicted. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 120 | * |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 121 | * A policy implementation should emit this signal to cause CS to erase an entry from its index. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 122 | * CS should connect to this signal and erase the entry upon signal emission. |
| 123 | */ |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 124 | signal::Signal<Policy, EntryRef> beforeEvict; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 125 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 126 | /** \brief Invoked by CS after a new entry is inserted. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 127 | * \post cs.size() <= getLimit() |
| 128 | * |
| 129 | * The policy may evict entries if necessary. |
| 130 | * During this process, \p i might be evicted. |
| 131 | */ |
| 132 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 133 | afterInsert(EntryRef i); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 134 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 135 | /** \brief Invoked by CS after an existing entry is refreshed by same Data. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 136 | * |
| 137 | * The policy may witness this refresh to make better eviction decisions in the future. |
| 138 | */ |
| 139 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 140 | afterRefresh(EntryRef i); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 141 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 142 | /** \brief Invoked by CS before an entry is erased due to management command. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 143 | * \warning CS must not invoke this method if an entry is erased due to eviction. |
| 144 | */ |
| 145 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 146 | beforeErase(EntryRef i); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 147 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 148 | /** \brief Invoked by CS before an entry is used to match a lookup. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 149 | * |
| 150 | * The policy may witness this usage to make better eviction decisions in the future. |
| 151 | */ |
| 152 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 153 | beforeUse(EntryRef i); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 154 | |
| 155 | protected: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 156 | /** \brief Invoked after a new entry is created in CS. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 157 | * |
| 158 | * When overridden in a subclass, a policy implementation should decide whether to accept \p i. |
| 159 | * If \p i is accepted, it should be inserted into a cleanup index. |
| 160 | * Otherwise, \p beforeEvict signal should be emitted with \p i to inform CS to erase the entry. |
| 161 | * A policy implementation may decide to evict other entries by emitting \p beforeEvict signal, |
| 162 | * in order to keep CS size under limit. |
| 163 | */ |
| 164 | virtual void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 165 | doAfterInsert(EntryRef i) = 0; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 166 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 167 | /** \brief Invoked after an existing entry is refreshed by same Data. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 168 | * |
| 169 | * When overridden in a subclass, a policy implementation may witness this operation |
| 170 | * and adjust its cleanup index. |
| 171 | */ |
| 172 | virtual void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 173 | doAfterRefresh(EntryRef i) = 0; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 174 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 175 | /** \brief Invoked before an entry is erased due to management command. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 176 | * \note This will not be invoked for an entry being evicted by policy. |
| 177 | * |
| 178 | * When overridden in a subclass, a policy implementation should erase \p i |
| 179 | * from its cleanup index without emitted \p afterErase signal. |
| 180 | */ |
| 181 | virtual void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 182 | doBeforeErase(EntryRef i) = 0; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 183 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 184 | /** \brief Invoked before an entry is used to match a lookup. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 185 | * |
| 186 | * When overridden in a subclass, a policy implementation may witness this operation |
| 187 | * and adjust its cleanup index. |
| 188 | */ |
| 189 | virtual void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 190 | doBeforeUse(EntryRef i) = 0; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 191 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 192 | /** \brief Evicts zero or more entries. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 193 | * \post CS size does not exceed hard limit |
| 194 | */ |
| 195 | virtual void |
| 196 | evictEntries() = 0; |
| 197 | |
| 198 | protected: |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 199 | explicit |
| 200 | Policy(std::string_view policyName); |
| 201 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 202 | DECLARE_SIGNAL_EMIT(beforeEvict) |
| 203 | |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 204 | private: // registry |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 205 | using CreateFunc = std::function<unique_ptr<Policy>()>; |
| 206 | using Registry = std::map<std::string, CreateFunc>; // indexed by policy name |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 207 | |
| 208 | static Registry& |
| 209 | getRegistry(); |
| 210 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 211 | private: |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 212 | const std::string m_policyName; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 213 | size_t m_limit; |
| 214 | Cs* m_cs; |
| 215 | }; |
| 216 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 217 | } // namespace nfd::cs |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 218 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 219 | /** \brief Registers a CS policy. |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 220 | * \param P a subclass of nfd::cs::Policy |
| 221 | */ |
| 222 | #define NFD_REGISTER_CS_POLICY(P) \ |
| 223 | static class NfdAuto ## P ## CsPolicyRegistrationClass \ |
| 224 | { \ |
| 225 | public: \ |
| 226 | NfdAuto ## P ## CsPolicyRegistrationClass() \ |
| 227 | { \ |
| 228 | ::nfd::cs::Policy::registerPolicy<P>(); \ |
| 229 | } \ |
| 230 | } g_nfdAuto ## P ## CsPolicyRegistrationVariable |
| 231 | |
Junxiao Shi | 2af9ca3 | 2016-08-25 21:51:02 +0000 | [diff] [blame] | 232 | #endif // NFD_DAEMON_TABLE_CS_POLICY_HPP |