blob: 0952f44c54dd8d2963aad038706bb9fd756ddb73 [file] [log] [blame]
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5e4a02f2019-07-15 11:59:18 +00002/*
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05004 * 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 Shi5e4a02f2019-07-15 11:59:18 +000029#include "cs-entry.hpp"
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050030
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050031#include <functional>
32#include <map>
33#include <set>
34
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::cs {
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050036
37class Cs;
38
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040039/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040040 * \brief Represents a CS replacement policy.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050041 */
42class Policy : noncopyable
43{
Junxiao Shi52555b02016-11-25 21:39:06 +000044public: // registry
45 template<typename P>
46 static void
Davide Pesaventoae430302023-05-11 01:42:46 -040047 registerPolicy(std::string_view policyName = P::POLICY_NAME)
Junxiao Shi52555b02016-11-25 21:39:06 +000048 {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040049 BOOST_ASSERT(!policyName.empty());
Davide Pesaventoae430302023-05-11 01:42:46 -040050 auto r = getRegistry().insert_or_assign(std::string(policyName), [] { return make_unique<P>(); });
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040051 BOOST_VERIFY(r.second);
Junxiao Shi52555b02016-11-25 21:39:06 +000052 }
53
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040054 /**
55 * \brief Returns a cs::Policy identified by \p policyName,
56 * or nullptr if \p policyName is unknown.
Junxiao Shi52555b02016-11-25 21:39:06 +000057 */
58 static unique_ptr<Policy>
Junxiao Shi7ee00fb2016-12-04 21:23:00 +000059 create(const std::string& policyName);
60
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040061 /**
62 * \brief Returns a list of available policy names.
Junxiao Shi7ee00fb2016-12-04 21:23:00 +000063 */
64 static std::set<std::string>
65 getPolicyNames();
Junxiao Shi52555b02016-11-25 21:39:06 +000066
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050067public:
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050068 virtual
Junxiao Shi2af9ca32016-08-25 21:51:02 +000069 ~Policy() = default;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050070
71 const std::string&
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040072 getName() const noexcept
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000073 {
74 return m_policyName;
75 }
76
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040077 /**
78 * \brief Returns a pointer to the associated CS instance.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050079 */
80 Cs*
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040081 getCs() const noexcept
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000082 {
83 return m_cs;
84 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050085
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040086 /**
87 * \brief Sets the associated CS instance.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050088 */
89 void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040090 setCs(Cs* cs) noexcept
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000091 {
92 m_cs = cs;
93 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050094
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040095 /**
96 * \brief Gets hard limit (in number of entries).
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050097 */
98 size_t
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040099 getLimit() const noexcept
Junxiao Shi5e4a02f2019-07-15 11:59:18 +0000100 {
101 return m_limit;
102 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500103
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400104 /** \brief Sets hard limit (in number of entries).
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500105 * \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 Shi07f2e2f2019-07-22 09:10:06 -0600113public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400114 /** \brief A reference to a CS entry.
115 * \note `operator<` of EntryRef compares the Data name enclosed in the Entry.
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600116 */
117 using EntryRef = Table::const_iterator;
118
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400119 /** \brief %Signal emitted when an entry is being evicted.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500120 *
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600121 * A policy implementation should emit this signal to cause CS to erase an entry from its index.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500122 * CS should connect to this signal and erase the entry upon signal emission.
123 */
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600124 signal::Signal<Policy, EntryRef> beforeEvict;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500125
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400126 /** \brief Invoked by CS after a new entry is inserted.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500127 * \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 Shi07f2e2f2019-07-22 09:10:06 -0600133 afterInsert(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500134
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400135 /** \brief Invoked by CS after an existing entry is refreshed by same Data.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500136 *
137 * The policy may witness this refresh to make better eviction decisions in the future.
138 */
139 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600140 afterRefresh(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500141
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400142 /** \brief Invoked by CS before an entry is erased due to management command.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500143 * \warning CS must not invoke this method if an entry is erased due to eviction.
144 */
145 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600146 beforeErase(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500147
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400148 /** \brief Invoked by CS before an entry is used to match a lookup.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500149 *
150 * The policy may witness this usage to make better eviction decisions in the future.
151 */
152 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600153 beforeUse(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500154
155protected:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400156 /** \brief Invoked after a new entry is created in CS.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500157 *
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 Shi07f2e2f2019-07-22 09:10:06 -0600165 doAfterInsert(EntryRef i) = 0;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500166
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400167 /** \brief Invoked after an existing entry is refreshed by same Data.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500168 *
169 * When overridden in a subclass, a policy implementation may witness this operation
170 * and adjust its cleanup index.
171 */
172 virtual void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600173 doAfterRefresh(EntryRef i) = 0;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500174
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400175 /** \brief Invoked before an entry is erased due to management command.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500176 * \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 Shi07f2e2f2019-07-22 09:10:06 -0600182 doBeforeErase(EntryRef i) = 0;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500183
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400184 /** \brief Invoked before an entry is used to match a lookup.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500185 *
186 * When overridden in a subclass, a policy implementation may witness this operation
187 * and adjust its cleanup index.
188 */
189 virtual void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600190 doBeforeUse(EntryRef i) = 0;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500191
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400192 /** \brief Evicts zero or more entries.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500193 * \post CS size does not exceed hard limit
194 */
195 virtual void
196 evictEntries() = 0;
197
198protected:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400199 explicit
200 Policy(std::string_view policyName);
201
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500202 DECLARE_SIGNAL_EMIT(beforeEvict)
203
Junxiao Shi52555b02016-11-25 21:39:06 +0000204private: // registry
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600205 using CreateFunc = std::function<unique_ptr<Policy>()>;
206 using Registry = std::map<std::string, CreateFunc>; // indexed by policy name
Junxiao Shi52555b02016-11-25 21:39:06 +0000207
208 static Registry&
209 getRegistry();
210
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500211private:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400212 const std::string m_policyName;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500213 size_t m_limit;
214 Cs* m_cs;
215};
216
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400217} // namespace nfd::cs
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500218
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400219/** \brief Registers a CS policy.
Junxiao Shi52555b02016-11-25 21:39:06 +0000220 * \param P a subclass of nfd::cs::Policy
221 */
222#define NFD_REGISTER_CS_POLICY(P) \
223static class NfdAuto ## P ## CsPolicyRegistrationClass \
224{ \
225public: \
226 NfdAuto ## P ## CsPolicyRegistrationClass() \
227 { \
228 ::nfd::cs::Policy::registerPolicy<P>(); \
229 } \
230} g_nfdAuto ## P ## CsPolicyRegistrationVariable
231
Junxiao Shi2af9ca32016-08-25 21:51:02 +0000232#endif // NFD_DAEMON_TABLE_CS_POLICY_HPP