blob: 50c79dcbb6d54f014171449246eab0435e072527 [file] [log] [blame]
Junxiao Shid3c792f2014-01-30 00:46:13 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry41aba102017-11-01 16:42:13 -07002/*
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shifaf3eb02015-02-16 10:50:36 -07004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -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/>.
Junxiao Shi82e7f582014-09-07 15:15:40 -070024 */
Junxiao Shid3c792f2014-01-30 00:46:13 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FW_STRATEGY_HPP
27#define NFD_DAEMON_FW_STRATEGY_HPP
Junxiao Shid3c792f2014-01-30 00:46:13 -070028
Junxiao Shi2d9bdc82014-03-02 20:55:42 -070029#include "forwarder.hpp"
Junxiao Shidbe71732014-02-21 22:23:28 -070030#include "table/measurements-accessor.hpp"
Junxiao Shid3c792f2014-01-30 00:46:13 -070031
32namespace nfd {
Junxiao Shi8c8d2182014-01-30 22:33:00 -070033namespace fw {
34
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -040035class StrategyParameters;
36
Davide Pesavento0498ce82021-06-14 02:02:21 -040037/**
38 * \brief Represents a forwarding strategy
Junxiao Shid3c792f2014-01-30 00:46:13 -070039 */
Junxiao Shic34d1672016-12-09 15:57:59 +000040class Strategy : noncopyable
Junxiao Shid3c792f2014-01-30 00:46:13 -070041{
Junxiao Shic34d1672016-12-09 15:57:59 +000042public: // registry
Eric Newberryc68b2e82020-04-16 12:40:30 -070043 /** \brief Register a strategy type
Junxiao Shic34d1672016-12-09 15:57:59 +000044 * \tparam S subclass of Strategy
Junxiao Shi18739c42016-12-22 08:03:00 +000045 * \param strategyName strategy program name, must contain version
Junxiao Shic34d1672016-12-09 15:57:59 +000046 * \note It is permitted to register the same strategy type under multiple names,
47 * which is useful in tests and for creating aliases.
48 */
49 template<typename S>
50 static void
Junxiao Shi037f4ab2016-12-13 04:27:06 +000051 registerType(const Name& strategyName = S::getStrategyName())
Junxiao Shic34d1672016-12-09 15:57:59 +000052 {
Junxiao Shi91f6ee02016-12-29 21:44:44 +000053 BOOST_ASSERT(strategyName.size() > 1);
Junxiao Shi18739c42016-12-22 08:03:00 +000054 BOOST_ASSERT(strategyName.at(-1).isVersion());
Junxiao Shic34d1672016-12-09 15:57:59 +000055 Registry& registry = getRegistry();
56 BOOST_ASSERT(registry.count(strategyName) == 0);
Davide Pesavento3dade002019-03-19 11:29:56 -060057 registry[strategyName] = [] (auto&&... args) {
58 return make_unique<S>(std::forward<decltype(args)>(args)...);
59 };
Junxiao Shic34d1672016-12-09 15:57:59 +000060 }
61
Eric Newberryc68b2e82020-04-16 12:40:30 -070062 /** \return Whether a strategy instance can be created from \p instanceName
Junxiao Shi18739c42016-12-22 08:03:00 +000063 * \param instanceName strategy instance name, may contain version and parameters
Junxiao Shic34d1672016-12-09 15:57:59 +000064 * \note This function finds a strategy type using same rules as \p create ,
65 * but does not attempt to construct an instance.
66 */
67 static bool
Junxiao Shi18739c42016-12-22 08:03:00 +000068 canCreate(const Name& instanceName);
Junxiao Shic34d1672016-12-09 15:57:59 +000069
Eric Newberryc68b2e82020-04-16 12:40:30 -070070 /** \return A strategy instance created from \p instanceName
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -040071 * \retval nullptr if `canCreate(instanceName) == false`
Junxiao Shi18739c42016-12-22 08:03:00 +000072 * \throw std::invalid_argument strategy type constructor does not accept
73 * specified version or parameters
Junxiao Shic34d1672016-12-09 15:57:59 +000074 */
75 static unique_ptr<Strategy>
Junxiao Shi18739c42016-12-22 08:03:00 +000076 create(const Name& instanceName, Forwarder& forwarder);
Junxiao Shic34d1672016-12-09 15:57:59 +000077
Eric Newberryc68b2e82020-04-16 12:40:30 -070078 /** \return Whether \p instanceNameA and \p instanceNameA will initiate same strategy type
Junxiao Shi55e21b92017-01-23 03:27:47 +000079 */
80 static bool
81 areSameType(const Name& instanceNameA, const Name& instanceNameB);
82
Eric Newberryc68b2e82020-04-16 12:40:30 -070083 /** \return Registered versioned strategy names
Junxiao Shic34d1672016-12-09 15:57:59 +000084 */
85 static std::set<Name>
86 listRegistered();
87
Ju Pan2feb4592019-09-16 20:56:38 +000088public: // constructor, destructor, strategy info
Davide Pesavento3dade002019-03-19 11:29:56 -060089 /** \brief Construct a strategy instance.
Junxiao Shi18739c42016-12-22 08:03:00 +000090 * \param forwarder a reference to the forwarder, used to enable actions and accessors.
Davide Pesavento3dade002019-03-19 11:29:56 -060091 * \note Strategy subclass constructor must not retain a reference to \p forwarder.
Junxiao Shie93d6a32014-09-07 16:13:22 -070092 */
Junxiao Shi18739c42016-12-22 08:03:00 +000093 explicit
94 Strategy(Forwarder& forwarder);
Junxiao Shidbe71732014-02-21 22:23:28 -070095
Junxiao Shid3c792f2014-01-30 00:46:13 -070096 virtual
97 ~Strategy();
Junxiao Shidbe71732014-02-21 22:23:28 -070098
Junxiao Shi037f4ab2016-12-13 04:27:06 +000099#ifdef DOXYGEN
Eric Newberryc68b2e82020-04-16 12:40:30 -0700100 /** \return Strategy program name
Junxiao Shi18739c42016-12-22 08:03:00 +0000101 *
102 * The strategy name is defined by the strategy program.
103 * It must end with a version component.
Junxiao Shi037f4ab2016-12-13 04:27:06 +0000104 */
105 static const Name&
106 getStrategyName();
107#endif
108
Eric Newberryc68b2e82020-04-16 12:40:30 -0700109 /** \return Strategy instance name
Junxiao Shi18739c42016-12-22 08:03:00 +0000110 *
111 * The instance name is assigned during instantiation.
112 * It contains a version component, and may have extra parameter components.
Junxiao Shib9420cf2016-08-13 04:38:52 +0000113 */
Junxiao Shibb5105f2014-03-03 12:06:45 -0700114 const Name&
Junxiao Shi18739c42016-12-22 08:03:00 +0000115 getInstanceName() const
Junxiao Shib9420cf2016-08-13 04:38:52 +0000116 {
117 return m_name;
118 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700119
Junxiao Shi679e9272014-02-15 20:10:21 -0700120public: // triggers
Davide Pesavento0498ce82021-06-14 02:02:21 -0400121 /**
122 * \brief Trigger after an Interest is received.
Junxiao Shi679e9272014-02-15 20:10:21 -0700123 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400124 * The Interest:
Eric Newberryc68b2e82020-04-16 12:40:30 -0700125 * - has not exceeded HopLimit
Junxiao Shi679e9272014-02-15 20:10:21 -0700126 * - does not violate Scope
127 * - is not looped
128 * - cannot be satisfied by ContentStore
129 * - is under a namespace managed by this strategy
130 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400131 * The PIT entry is set to expire after InterestLifetime has elapsed at each downstream.
Teng Liang7003e0b2018-03-03 16:03:30 -0700132 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400133 * The strategy should decide whether and where to forward this Interest.
Junxiao Shi679e9272014-02-15 20:10:21 -0700134 * - If the strategy decides to forward this Interest,
Davide Pesavento0498ce82021-06-14 02:02:21 -0400135 * invoke sendInterest() for each upstream, either now or shortly after via a scheduler event,
136 * but before the PIT entry expires.
137 * Optionally, the strategy can invoke setExpiryTimer() to adjust how long it would wait for a response.
138 * - If the strategy has already forwarded this Interest previously and decides to continue
139 * waiting, do nothing.
140 * Optionally, the strategy can invoke setExpiryTimer() to adjust how long it would wait for a response.
Teng Liang7003e0b2018-03-03 16:03:30 -0700141 * - If the strategy concludes that this Interest cannot be satisfied,
Davide Pesavento0498ce82021-06-14 02:02:21 -0400142 * invoke rejectPendingInterest() to erase the PIT entry.
Junxiao Shi82e7f582014-09-07 15:15:40 -0700143 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400144 * \warning The strategy must not retain a copy of the \p pitEntry shared_ptr after this function
145 * returns, otherwise undefined behavior may occur. However, the strategy is allowed to
146 * construct and keep a weak_ptr to \p pitEntry.
Junxiao Shi679e9272014-02-15 20:10:21 -0700147 */
Junxiao Shid3c792f2014-01-30 00:46:13 -0700148 virtual void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400149 afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000150 const shared_ptr<pit::Entry>& pitEntry) = 0;
Junxiao Shidbe71732014-02-21 22:23:28 -0700151
Davide Pesavento0498ce82021-06-14 02:02:21 -0400152 /**
153 * \brief Trigger after a matching Data is found in the Content Store.
Junxiao Shi22be22c2014-02-16 22:53:48 -0700154 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400155 * In the base class, this method sends \p data to \p ingress.
Teng Liang7003e0b2018-03-03 16:03:30 -0700156 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400157 * \warning The strategy must not retain a copy of the \p pitEntry shared_ptr after this function
158 * returns, otherwise undefined behavior may occur. However, the strategy is allowed to
159 * construct and keep a weak_ptr to \p pitEntry.
Junxiao Shi22be22c2014-02-16 22:53:48 -0700160 */
161 virtual void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400162 afterContentStoreHit(const Data& data, const FaceEndpoint& ingress,
163 const shared_ptr<pit::Entry>& pitEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -0700164
Davide Pesavento0498ce82021-06-14 02:02:21 -0400165 /**
166 * \brief Trigger before a PIT entry is satisfied.
Teng Liang85a36632018-03-21 05:59:34 -0700167 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400168 * This trigger is invoked when an incoming Data satisfies more than one PIT entry.
169 * The strategy can collect measurements information, but cannot manipulate Data forwarding.
170 * When an incoming Data satisfies only one PIT entry, afterReceiveData() is invoked instead
171 * and given full control over Data forwarding. If a strategy does not override afterReceiveData(),
172 * the default implementation invokes beforeSatisfyInterest().
173 *
174 * Normally, PIT entries are erased after receiving the first matching Data.
175 * If the strategy wishes to collect responses from additional upstream nodes,
176 * it should invoke setExpiryTimer() within this function to prolong the PIT entry lifetime.
177 * If a Data arrives from another upstream during the extended PIT entry lifetime, this trigger
178 * will be invoked again. At that time, the strategy must invoke setExpiryTimer() again to
179 * continue collecting more responses.
180 *
181 * In the base class, this method does nothing.
182 *
183 * \warning The strategy must not retain a copy of the \p pitEntry shared_ptr after this function
184 * returns, otherwise undefined behavior may occur. However, the strategy is allowed to
185 * construct and keep a weak_ptr to \p pitEntry.
Teng Liang85a36632018-03-21 05:59:34 -0700186 */
187 virtual void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400188 beforeSatisfyInterest(const Data& data, const FaceEndpoint& ingress,
189 const shared_ptr<pit::Entry>& pitEntry);
Teng Liang85a36632018-03-21 05:59:34 -0700190
Davide Pesavento0498ce82021-06-14 02:02:21 -0400191 /**
192 * \brief Trigger after Data is received.
Teng Liang43bb2312018-03-26 04:16:42 -0700193 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400194 * This trigger is invoked when an incoming Data satisfies exactly one PIT entry,
195 * and gives the strategy full control over Data forwarding.
Teng Liang43bb2312018-03-26 04:16:42 -0700196 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400197 * When this trigger is invoked:
Teng Liang43bb2312018-03-26 04:16:42 -0700198 * - The Data has been verified to satisfy the PIT entry.
199 * - The PIT entry expiry timer is set to now
200 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400201 * Inside this function:
202 * - A strategy should return Data to downstream nodes via sendData() or sendDataToAll().
Teng Liang43bb2312018-03-26 04:16:42 -0700203 * - A strategy can modify the Data as long as it still satisfies the PIT entry, such as
204 * adding or removing congestion marks.
Davide Pesavento0498ce82021-06-14 02:02:21 -0400205 * - A strategy can delay Data forwarding by prolonging the PIT entry lifetime via setExpiryTimer(),
206 * and later forward the Data before the PIT entry is erased.
Teng Liang43bb2312018-03-26 04:16:42 -0700207 * - A strategy can collect measurements about the upstream.
208 * - A strategy can collect responses from additional upstream nodes by prolonging the PIT entry
Davide Pesavento0498ce82021-06-14 02:02:21 -0400209 * lifetime via setExpiryTimer() every time a Data is received. Note that only one Data should
Teng Liang43bb2312018-03-26 04:16:42 -0700210 * be returned to each downstream node.
211 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400212 * In the base class, this method invokes beforeSatisfyInterest() and then returns the Data
213 * to all downstream faces via sendDataToAll().
214 *
215 * \warning The strategy must not retain a copy of the \p pitEntry shared_ptr after this function
216 * returns, otherwise undefined behavior may occur. However, the strategy is allowed to
217 * construct and keep a weak_ptr to \p pitEntry.
Teng Liang43bb2312018-03-26 04:16:42 -0700218 */
219 virtual void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400220 afterReceiveData(const Data& data, const FaceEndpoint& ingress,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000221 const shared_ptr<pit::Entry>& pitEntry);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700222
Davide Pesavento0498ce82021-06-14 02:02:21 -0400223 /**
224 * \brief Trigger after a Nack is received.
Eric Newberry41aba102017-11-01 16:42:13 -0700225 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400226 * This trigger is invoked when an incoming Nack is received in response to
227 * an forwarded Interest.
228 * The Nack has been confirmed to be a response to the last Interest forwarded
229 * to that upstream, i.e. the PIT out-record exists and has a matching Nonce.
230 * The NackHeader has been recorded in the PIT out-record.
231 *
232 * If the PIT entry is not yet satisfied, its expiry timer remains unchanged.
233 * Otherwise, the PIT entry will normally expire immediately after this function returns.
234 *
235 * If the strategy wishes to collect responses from additional upstream nodes,
236 * it should invoke setExpiryTimer() within this function to prolong the PIT entry lifetime.
237 * If a Nack arrives from another upstream during the extended PIT entry lifetime, this trigger
238 * will be invoked again. At that time, the strategy must invoke setExpiryTimer() again to
239 * continue collecting more responses.
240 *
241 * In the base class, this method does nothing.
242 *
243 * \warning The strategy must not retain a copy of the \p pitEntry shared_ptr after this function
244 * returns, otherwise undefined behavior may occur. However, the strategy is allowed to
245 * construct and keep a weak_ptr to \p pitEntry.
Eric Newberry41aba102017-11-01 16:42:13 -0700246 */
247 virtual void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400248 afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
249 const shared_ptr<pit::Entry>& pitEntry);
Eric Newberry41aba102017-11-01 16:42:13 -0700250
Davide Pesavento0498ce82021-06-14 02:02:21 -0400251 /**
252 * \brief Trigger after an Interest is dropped (e.g., for exceeding allowed retransmissions).
Ju Pan2feb4592019-09-16 20:56:38 +0000253 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400254 * In the base class, this method does nothing.
255 */
256 virtual void
257 onDroppedInterest(const Interest& interest, Face& egress);
258
259 /**
260 * \brief Trigger after a new nexthop is added.
261 *
262 * The strategy should decide whether to send the buffered Interests to the new nexthop.
263 *
264 * In the base class, this method does nothing.
Ju Pan2feb4592019-09-16 20:56:38 +0000265 */
266 virtual void
267 afterNewNextHop(const fib::NextHop& nextHop, const shared_ptr<pit::Entry>& pitEntry);
268
Junxiao Shid3c792f2014-01-30 00:46:13 -0700269protected: // actions
Davide Pesavento0498ce82021-06-14 02:02:21 -0400270 /**
271 * \brief Send an Interest packet.
272 * \param interest the Interest packet
273 * \param egress face through which to send out the Interest
274 * \param pitEntry the PIT entry
275 * \return A pointer to the out-record created or nullptr if the Interest was dropped
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700276 */
Davide Pesavento264af772021-02-09 21:48:24 -0500277 NFD_VIRTUAL_WITH_TESTS pit::OutRecord*
Davide Pesavento0498ce82021-06-14 02:02:21 -0400278 sendInterest(const Interest& interest, Face& egress, const shared_ptr<pit::Entry>& pitEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -0700279
Davide Pesavento0498ce82021-06-14 02:02:21 -0400280 /**
281 * \brief Send a Data packet.
282 * \param data the Data packet
283 * \param egress face through which to send out the Data
284 * \param pitEntry the PIT entry
285 * \return Whether the Data was sent (true) or dropped (false)
Teng Liang85a36632018-03-21 05:59:34 -0700286 */
Davide Pesavento264af772021-02-09 21:48:24 -0500287 NFD_VIRTUAL_WITH_TESTS bool
Davide Pesavento0498ce82021-06-14 02:02:21 -0400288 sendData(const Data& data, Face& egress, const shared_ptr<pit::Entry>& pitEntry);
Teng Liang85a36632018-03-21 05:59:34 -0700289
Davide Pesavento0498ce82021-06-14 02:02:21 -0400290 /**
291 * \brief Send a Data packet to all matched and qualified faces.
Teng Liang43bb2312018-03-26 04:16:42 -0700292 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400293 * A matched face qualifies if it is ad-hoc OR it is NOT \p inFace.
Teng Liang43bb2312018-03-26 04:16:42 -0700294 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400295 * \param data the Data packet
296 * \param pitEntry the PIT entry
297 * \param inFace face on which the Data arrived
Teng Liang43bb2312018-03-26 04:16:42 -0700298 */
Davide Pesavento264af772021-02-09 21:48:24 -0500299 NFD_VIRTUAL_WITH_TESTS void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400300 sendDataToAll(const Data& data, const shared_ptr<pit::Entry>& pitEntry, const Face& inFace);
Teng Liang85a36632018-03-21 05:59:34 -0700301
Davide Pesavento0498ce82021-06-14 02:02:21 -0400302 /**
303 * \brief Schedule the PIT entry for immediate deletion.
Junxiao Shi679e9272014-02-15 20:10:21 -0700304 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400305 * This helper function sets the PIT entry expiry time to zero.
306 * The strategy should invoke this function when it concludes that the Interest cannot
307 * be forwarded and it does not want to wait for responses from existing upstream nodes.
Junxiao Shid3c792f2014-01-30 00:46:13 -0700308 */
Davide Pesavento264af772021-02-09 21:48:24 -0500309 NFD_VIRTUAL_WITH_TESTS void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000310 rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry)
311 {
Teng Liang7003e0b2018-03-03 16:03:30 -0700312 this->setExpiryTimer(pitEntry, 0_ms);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000313 }
Junxiao Shidbe71732014-02-21 22:23:28 -0700314
Davide Pesavento0498ce82021-06-14 02:02:21 -0400315 /**
316 * \brief Send a Nack packet.
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700317 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400318 * The egress face must have a PIT in-record, otherwise this method has no effect.
Teng Liangebc20f62020-06-23 16:55:20 -0700319 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400320 * \param header the Nack header
321 * \param egress face through which to send out the Nack
322 * \param pitEntry the PIT entry
323 * \return Whether the Nack was sent (true) or dropped (false)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700324 */
Davide Pesavento264af772021-02-09 21:48:24 -0500325 NFD_VIRTUAL_WITH_TESTS bool
Davide Pesavento0498ce82021-06-14 02:02:21 -0400326 sendNack(const lp::NackHeader& header, Face& egress, const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shib9420cf2016-08-13 04:38:52 +0000327 {
Davide Pesavento0498ce82021-06-14 02:02:21 -0400328 return m_forwarder.onOutgoingNack(header, egress, pitEntry);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000329 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700330
Davide Pesavento0498ce82021-06-14 02:02:21 -0400331 /**
332 * \brief Send Nack to every face that has an in-record, except those in \p exceptFaces
333 * \param header the Nack header
334 * \param pitEntry the PIT entry
335 * \param exceptFaces list of faces that should be excluded from sending Nacks
336 * \note This is not an action, but a helper that invokes the sendNack() action.
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700337 */
338 void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400339 sendNacks(const lp::NackHeader& header, const shared_ptr<pit::Entry>& pitEntry,
Teng Liangebc20f62020-06-23 16:55:20 -0700340 std::initializer_list<const Face*> exceptFaces = {});
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700341
Davide Pesavento0498ce82021-06-14 02:02:21 -0400342 /**
343 * \brief Schedule the PIT entry to be erased after \p duration.
Teng Liang7003e0b2018-03-03 16:03:30 -0700344 */
345 void
346 setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
347 {
348 m_forwarder.setExpiryTimer(pitEntry, duration);
349 }
350
Junxiao Shidbe71732014-02-21 22:23:28 -0700351protected: // accessors
Davide Pesavento0498ce82021-06-14 02:02:21 -0400352 /**
353 * \brief Performs a FIB lookup, considering Link object if present.
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000354 */
Junxiao Shi8d843142016-07-11 22:42:42 +0000355 const fib::Entry&
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000356 lookupFib(const pit::Entry& pitEntry) const;
Junxiao Shi8d843142016-07-11 22:42:42 +0000357
Junxiao Shidbe71732014-02-21 22:23:28 -0700358 MeasurementsAccessor&
Junxiao Shib9420cf2016-08-13 04:38:52 +0000359 getMeasurements()
360 {
361 return m_measurements;
362 }
Junxiao Shidbe71732014-02-21 22:23:28 -0700363
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000364 Face*
Junxiao Shib9420cf2016-08-13 04:38:52 +0000365 getFace(FaceId id) const
366 {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400367 return getFaceTable().get(id);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000368 }
Junxiao Shi2d9bdc82014-03-02 20:55:42 -0700369
Junxiao Shi49e11e72014-12-14 19:46:05 -0700370 const FaceTable&
Junxiao Shib9420cf2016-08-13 04:38:52 +0000371 getFaceTable() const
372 {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400373 return m_forwarder.m_faceTable;
Junxiao Shib9420cf2016-08-13 04:38:52 +0000374 }
Junxiao Shi49e11e72014-12-14 19:46:05 -0700375
Junxiao Shi18739c42016-12-22 08:03:00 +0000376protected: // instance name
377 struct ParsedInstanceName
378 {
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400379 Name strategyName; ///< Strategy name without parameters
380 optional<uint64_t> version; ///< The strategy version number, if present
381 PartialName parameters; ///< Parameter components, may be empty
Junxiao Shi18739c42016-12-22 08:03:00 +0000382 };
383
Eric Newberryc68b2e82020-04-16 12:40:30 -0700384 /** \brief Parse a strategy instance name
Junxiao Shi18739c42016-12-22 08:03:00 +0000385 * \param input strategy instance name, may contain version and parameters
386 * \throw std::invalid_argument input format is unacceptable
387 */
388 static ParsedInstanceName
389 parseInstanceName(const Name& input);
390
Eric Newberryc68b2e82020-04-16 12:40:30 -0700391 /** \brief Construct a strategy instance name
Junxiao Shi18739c42016-12-22 08:03:00 +0000392 * \param input strategy instance name, may contain version and parameters
393 * \param strategyName strategy name with version but without parameters;
394 * typically this should be \p getStrategyName()
395 *
396 * If \p input contains a version component, return \p input unchanged.
397 * Otherwise, return \p input plus the version component taken from \p strategyName.
398 * This allows a strategy instance to be constructed with an unversioned name,
399 * but its final instance name should contain the version.
400 */
401 static Name
402 makeInstanceName(const Name& input, const Name& strategyName);
403
Eric Newberryc68b2e82020-04-16 12:40:30 -0700404 /** \brief Set strategy instance name
Junxiao Shi18739c42016-12-22 08:03:00 +0000405 * \note This must be called by strategy subclass constructor.
406 */
407 void
408 setInstanceName(const Name& name)
409 {
410 m_name = name;
411 }
Junxiao Shi49e11e72014-12-14 19:46:05 -0700412
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400413NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
414 /**
415 * \brief Parse strategy parameters encoded in a strategy instance name
416 * \param params encoded parameters, typically obtained from a call to parseInstanceName()
417 * \throw std::invalid_argument the encoding format is invalid or unsupported by this implementation
418 */
419 static StrategyParameters
420 parseParameters(const PartialName& params);
421
Junxiao Shic34d1672016-12-09 15:57:59 +0000422private: // registry
Davide Pesavento0498ce82021-06-14 02:02:21 -0400423 using CreateFunc = std::function<unique_ptr<Strategy>(Forwarder&, const Name& /*strategyName*/)>;
424 using Registry = std::map<Name, CreateFunc>; // indexed by strategy name
Junxiao Shic34d1672016-12-09 15:57:59 +0000425
426 static Registry&
427 getRegistry();
428
429 static Registry::const_iterator
Junxiao Shi18739c42016-12-22 08:03:00 +0000430 find(const Name& instanceName);
431
432protected: // accessors
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400433 signal::Signal<FaceTable, Face>& afterAddFace;
434 signal::Signal<FaceTable, Face>& beforeRemoveFace;
Junxiao Shic34d1672016-12-09 15:57:59 +0000435
436private: // instance fields
Junxiao Shibb5105f2014-03-03 12:06:45 -0700437 Name m_name;
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700438 Forwarder& m_forwarder;
Junxiao Shidbe71732014-02-21 22:23:28 -0700439 MeasurementsAccessor m_measurements;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700440};
441
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400442class StrategyParameters : public std::map<std::string, std::string>
443{
444public:
445 // Note: only arithmetic types are supported by getOrDefault() for now
446
447 template<typename T>
448 std::enable_if_t<std::is_signed<T>::value, T>
449 getOrDefault(const key_type& key, const T& defaultVal) const
450 {
451 auto it = find(key);
452 if (it == end()) {
453 return defaultVal;
454 }
455
456 T val{};
457 if (!boost::conversion::try_lexical_convert(it->second, val)) {
458 NDN_THROW(std::invalid_argument(key + " value is malformed"));
459 }
460 return val;
461 }
462
463 template<typename T>
464 std::enable_if_t<std::is_unsigned<T>::value, T>
465 getOrDefault(const key_type& key, const T& defaultVal) const
466 {
467 auto it = find(key);
468 if (it == end()) {
469 return defaultVal;
470 }
471
472 if (it->second.find('-') != std::string::npos) {
473 NDN_THROW(std::invalid_argument(key + " cannot be negative"));
474 }
475
476 T val{};
477 if (!boost::conversion::try_lexical_convert(it->second, val)) {
478 NDN_THROW(std::invalid_argument(key + " value is malformed"));
479 }
480 return val;
481 }
482};
483
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700484} // namespace fw
Junxiao Shid3c792f2014-01-30 00:46:13 -0700485} // namespace nfd
486
Eric Newberryc68b2e82020-04-16 12:40:30 -0700487/** \brief Registers a strategy
Junxiao Shic34d1672016-12-09 15:57:59 +0000488 *
489 * This macro should appear once in .cpp of each strategy.
490 */
491#define NFD_REGISTER_STRATEGY(S) \
492static class NfdAuto ## S ## StrategyRegistrationClass \
493{ \
494public: \
495 NfdAuto ## S ## StrategyRegistrationClass() \
496 { \
497 ::nfd::fw::Strategy::registerType<S>(); \
498 } \
499} g_nfdAuto ## S ## StrategyRegistrationVariable
500
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700501#endif // NFD_DAEMON_FW_STRATEGY_HPP