blob: 5e99d6a36d935b56d1aec386ecaec5e4124dc67c [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/*
Alex Lane653eb072023-07-27 22:11:46 -04003 * Copyright (c) 2014-2023, 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
Davide Pesaventoa9b09b62022-06-04 14:07:25 -040032#include <boost/lexical_cast/try_lexical_convert.hpp>
33
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040034namespace nfd::fw {
Junxiao Shi8c8d2182014-01-30 22:33:00 -070035
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -040036class StrategyParameters;
37
Davide Pesavento0498ce82021-06-14 02:02:21 -040038/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040039 * \brief Base class of all forwarding strategies.
Junxiao Shid3c792f2014-01-30 00:46:13 -070040 */
Junxiao Shic34d1672016-12-09 15:57:59 +000041class Strategy : noncopyable
Junxiao Shid3c792f2014-01-30 00:46:13 -070042{
Junxiao Shic34d1672016-12-09 15:57:59 +000043public: // registry
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040044 /**
45 * \brief Register a strategy type.
46 * \tparam S subclass of Strategy
47 * \param strategyName strategy program name, must contain version
48 * \note It is permitted to register the same strategy type under multiple names,
49 * which is useful in tests and for creating aliases.
Junxiao Shic34d1672016-12-09 15:57:59 +000050 */
51 template<typename S>
52 static void
Junxiao Shi037f4ab2016-12-13 04:27:06 +000053 registerType(const Name& strategyName = S::getStrategyName())
Junxiao Shic34d1672016-12-09 15:57:59 +000054 {
Junxiao Shi91f6ee02016-12-29 21:44:44 +000055 BOOST_ASSERT(strategyName.size() > 1);
Junxiao Shi18739c42016-12-22 08:03:00 +000056 BOOST_ASSERT(strategyName.at(-1).isVersion());
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040057 auto r = getRegistry().insert_or_assign(strategyName, [] (auto&&... args) {
Davide Pesavento3dade002019-03-19 11:29:56 -060058 return make_unique<S>(std::forward<decltype(args)>(args)...);
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040059 });
60 BOOST_VERIFY(r.second);
Junxiao Shic34d1672016-12-09 15:57:59 +000061 }
62
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040063 /**
64 * \brief Returns whether a strategy instance can be created from \p instanceName.
65 * \param instanceName strategy instance name, may contain version and parameters
66 * \note This function finds a strategy type using the same rules as create(),
67 * but does not attempt to construct an instance.
Junxiao Shic34d1672016-12-09 15:57:59 +000068 */
69 static bool
Junxiao Shi18739c42016-12-22 08:03:00 +000070 canCreate(const Name& instanceName);
Junxiao Shic34d1672016-12-09 15:57:59 +000071
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040072 /**
73 * \brief Returns a strategy instance created from \p instanceName.
74 * \retval nullptr if `canCreate(instanceName) == false`
75 * \throw std::invalid_argument strategy type constructor does not accept the
76 * specified version or parameters
Junxiao Shic34d1672016-12-09 15:57:59 +000077 */
78 static unique_ptr<Strategy>
Junxiao Shi18739c42016-12-22 08:03:00 +000079 create(const Name& instanceName, Forwarder& forwarder);
Junxiao Shic34d1672016-12-09 15:57:59 +000080
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040081 /**
82 * \brief Returns whether two names will instantiate the same strategy type.
Junxiao Shi55e21b92017-01-23 03:27:47 +000083 */
84 static bool
85 areSameType(const Name& instanceNameA, const Name& instanceNameB);
86
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040087 /**
88 * \brief Returns all registered versioned strategy names.
Junxiao Shic34d1672016-12-09 15:57:59 +000089 */
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040090 [[nodiscard]] static std::set<Name>
Junxiao Shic34d1672016-12-09 15:57:59 +000091 listRegistered();
92
Ju Pan2feb4592019-09-16 20:56:38 +000093public: // constructor, destructor, strategy info
Davide Pesavento3dade002019-03-19 11:29:56 -060094 /** \brief Construct a strategy instance.
Junxiao Shi18739c42016-12-22 08:03:00 +000095 * \param forwarder a reference to the forwarder, used to enable actions and accessors.
Davide Pesavento3dade002019-03-19 11:29:56 -060096 * \note Strategy subclass constructor must not retain a reference to \p forwarder.
Junxiao Shie93d6a32014-09-07 16:13:22 -070097 */
Junxiao Shi18739c42016-12-22 08:03:00 +000098 explicit
99 Strategy(Forwarder& forwarder);
Junxiao Shidbe71732014-02-21 22:23:28 -0700100
Junxiao Shid3c792f2014-01-30 00:46:13 -0700101 virtual
102 ~Strategy();
Junxiao Shidbe71732014-02-21 22:23:28 -0700103
Junxiao Shi037f4ab2016-12-13 04:27:06 +0000104#ifdef DOXYGEN
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400105 /**
106 * \brief Returns the strategy's program name.
Junxiao Shi18739c42016-12-22 08:03:00 +0000107 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400108 * The strategy name is defined by the strategy program.
109 * It must end with a version component.
Junxiao Shi037f4ab2016-12-13 04:27:06 +0000110 */
111 static const Name&
112 getStrategyName();
113#endif
114
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400115 /**
116 * \brief Returns the strategy's instance name.
Junxiao Shi18739c42016-12-22 08:03:00 +0000117 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400118 * The instance name is assigned during instantiation.
119 * It contains a version component and may have extra parameter components.
Junxiao Shib9420cf2016-08-13 04:38:52 +0000120 */
Junxiao Shibb5105f2014-03-03 12:06:45 -0700121 const Name&
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400122 getInstanceName() const noexcept
Junxiao Shib9420cf2016-08-13 04:38:52 +0000123 {
124 return m_name;
125 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700126
Junxiao Shi679e9272014-02-15 20:10:21 -0700127public: // triggers
Davide Pesavento0498ce82021-06-14 02:02:21 -0400128 /**
129 * \brief Trigger after an Interest is received.
Junxiao Shi679e9272014-02-15 20:10:21 -0700130 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400131 * The Interest:
Eric Newberryc68b2e82020-04-16 12:40:30 -0700132 * - has not exceeded HopLimit
Junxiao Shi679e9272014-02-15 20:10:21 -0700133 * - does not violate Scope
134 * - is not looped
135 * - cannot be satisfied by ContentStore
136 * - is under a namespace managed by this strategy
137 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400138 * The PIT entry is set to expire after InterestLifetime has elapsed at each downstream.
Teng Liang7003e0b2018-03-03 16:03:30 -0700139 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400140 * The strategy should decide whether and where to forward this Interest.
Junxiao Shi679e9272014-02-15 20:10:21 -0700141 * - If the strategy decides to forward this Interest,
Davide Pesavento0498ce82021-06-14 02:02:21 -0400142 * invoke sendInterest() for each upstream, either now or shortly after via a scheduler event,
143 * but before the PIT entry expires.
144 * Optionally, the strategy can invoke setExpiryTimer() to adjust how long it would wait for a response.
145 * - If the strategy has already forwarded this Interest previously and decides to continue
146 * waiting, do nothing.
147 * Optionally, the strategy can invoke setExpiryTimer() to adjust how long it would wait for a response.
Teng Liang7003e0b2018-03-03 16:03:30 -0700148 * - If the strategy concludes that this Interest cannot be satisfied,
Davide Pesavento0498ce82021-06-14 02:02:21 -0400149 * invoke rejectPendingInterest() to erase the PIT entry.
Junxiao Shi82e7f582014-09-07 15:15:40 -0700150 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400151 * \warning The strategy must not retain a copy of the \p pitEntry shared_ptr after this function
152 * returns, otherwise undefined behavior may occur. However, the strategy is allowed to
153 * construct and keep a weak_ptr to \p pitEntry.
Junxiao Shi679e9272014-02-15 20:10:21 -0700154 */
Junxiao Shid3c792f2014-01-30 00:46:13 -0700155 virtual void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400156 afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000157 const shared_ptr<pit::Entry>& pitEntry) = 0;
Junxiao Shidbe71732014-02-21 22:23:28 -0700158
Davide Pesavento0498ce82021-06-14 02:02:21 -0400159 /**
160 * \brief Trigger after a matching Data is found in the Content Store.
Junxiao Shi22be22c2014-02-16 22:53:48 -0700161 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400162 * In the base class, this method sends \p data to \p ingress.
Teng Liang7003e0b2018-03-03 16:03:30 -0700163 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400164 * \warning The strategy must not retain a copy of the \p pitEntry shared_ptr after this function
165 * returns, otherwise undefined behavior may occur. However, the strategy is allowed to
166 * construct and keep a weak_ptr to \p pitEntry.
Junxiao Shi22be22c2014-02-16 22:53:48 -0700167 */
168 virtual void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400169 afterContentStoreHit(const Data& data, const FaceEndpoint& ingress,
170 const shared_ptr<pit::Entry>& pitEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -0700171
Davide Pesavento0498ce82021-06-14 02:02:21 -0400172 /**
173 * \brief Trigger before a PIT entry is satisfied.
Teng Liang85a36632018-03-21 05:59:34 -0700174 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400175 * This trigger is invoked when an incoming Data satisfies more than one PIT entry.
176 * The strategy can collect measurements information, but cannot manipulate Data forwarding.
177 * When an incoming Data satisfies only one PIT entry, afterReceiveData() is invoked instead
178 * and given full control over Data forwarding. If a strategy does not override afterReceiveData(),
179 * the default implementation invokes beforeSatisfyInterest().
180 *
181 * Normally, PIT entries are erased after receiving the first matching Data.
182 * If the strategy wishes to collect responses from additional upstream nodes,
183 * it should invoke setExpiryTimer() within this function to prolong the PIT entry lifetime.
184 * If a Data arrives from another upstream during the extended PIT entry lifetime, this trigger
185 * will be invoked again. At that time, the strategy must invoke setExpiryTimer() again to
186 * continue collecting more responses.
187 *
188 * In the base class, this method does nothing.
189 *
190 * \warning The strategy must not retain a copy of the \p pitEntry shared_ptr after this function
191 * returns, otherwise undefined behavior may occur. However, the strategy is allowed to
192 * construct and keep a weak_ptr to \p pitEntry.
Teng Liang85a36632018-03-21 05:59:34 -0700193 */
194 virtual void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400195 beforeSatisfyInterest(const Data& data, const FaceEndpoint& ingress,
196 const shared_ptr<pit::Entry>& pitEntry);
Teng Liang85a36632018-03-21 05:59:34 -0700197
Davide Pesavento0498ce82021-06-14 02:02:21 -0400198 /**
199 * \brief Trigger after Data is received.
Teng Liang43bb2312018-03-26 04:16:42 -0700200 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400201 * This trigger is invoked when an incoming Data satisfies exactly one PIT entry,
202 * and gives the strategy full control over Data forwarding.
Teng Liang43bb2312018-03-26 04:16:42 -0700203 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400204 * When this trigger is invoked:
Teng Liang43bb2312018-03-26 04:16:42 -0700205 * - The Data has been verified to satisfy the PIT entry.
206 * - The PIT entry expiry timer is set to now
207 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400208 * Inside this function:
209 * - A strategy should return Data to downstream nodes via sendData() or sendDataToAll().
Teng Liang43bb2312018-03-26 04:16:42 -0700210 * - A strategy can modify the Data as long as it still satisfies the PIT entry, such as
211 * adding or removing congestion marks.
Davide Pesavento0498ce82021-06-14 02:02:21 -0400212 * - A strategy can delay Data forwarding by prolonging the PIT entry lifetime via setExpiryTimer(),
213 * and later forward the Data before the PIT entry is erased.
Teng Liang43bb2312018-03-26 04:16:42 -0700214 * - A strategy can collect measurements about the upstream.
215 * - A strategy can collect responses from additional upstream nodes by prolonging the PIT entry
Davide Pesavento0498ce82021-06-14 02:02:21 -0400216 * lifetime via setExpiryTimer() every time a Data is received. Note that only one Data should
Teng Liang43bb2312018-03-26 04:16:42 -0700217 * be returned to each downstream node.
218 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400219 * In the base class, this method invokes beforeSatisfyInterest() and then returns the Data
220 * to all downstream faces via sendDataToAll().
221 *
222 * \warning The strategy must not retain a copy of the \p pitEntry shared_ptr after this function
223 * returns, otherwise undefined behavior may occur. However, the strategy is allowed to
224 * construct and keep a weak_ptr to \p pitEntry.
Teng Liang43bb2312018-03-26 04:16:42 -0700225 */
226 virtual void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400227 afterReceiveData(const Data& data, const FaceEndpoint& ingress,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000228 const shared_ptr<pit::Entry>& pitEntry);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700229
Davide Pesavento0498ce82021-06-14 02:02:21 -0400230 /**
231 * \brief Trigger after a Nack is received.
Eric Newberry41aba102017-11-01 16:42:13 -0700232 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400233 * This trigger is invoked when an incoming Nack is received in response to
234 * an forwarded Interest.
235 * The Nack has been confirmed to be a response to the last Interest forwarded
236 * to that upstream, i.e. the PIT out-record exists and has a matching Nonce.
237 * The NackHeader has been recorded in the PIT out-record.
238 *
239 * If the PIT entry is not yet satisfied, its expiry timer remains unchanged.
240 * Otherwise, the PIT entry will normally expire immediately after this function returns.
241 *
242 * If the strategy wishes to collect responses from additional upstream nodes,
243 * it should invoke setExpiryTimer() within this function to prolong the PIT entry lifetime.
244 * If a Nack arrives from another upstream during the extended PIT entry lifetime, this trigger
245 * will be invoked again. At that time, the strategy must invoke setExpiryTimer() again to
246 * continue collecting more responses.
247 *
248 * In the base class, this method does nothing.
249 *
250 * \warning The strategy must not retain a copy of the \p pitEntry shared_ptr after this function
251 * returns, otherwise undefined behavior may occur. However, the strategy is allowed to
252 * construct and keep a weak_ptr to \p pitEntry.
Eric Newberry41aba102017-11-01 16:42:13 -0700253 */
254 virtual void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400255 afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
256 const shared_ptr<pit::Entry>& pitEntry);
Eric Newberry41aba102017-11-01 16:42:13 -0700257
Davide Pesavento0498ce82021-06-14 02:02:21 -0400258 /**
259 * \brief Trigger after an Interest is dropped (e.g., for exceeding allowed retransmissions).
Ju Pan2feb4592019-09-16 20:56:38 +0000260 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400261 * In the base class, this method does nothing.
262 */
263 virtual void
264 onDroppedInterest(const Interest& interest, Face& egress);
265
266 /**
267 * \brief Trigger after a new nexthop is added.
268 *
269 * The strategy should decide whether to send the buffered Interests to the new nexthop.
270 *
271 * In the base class, this method does nothing.
Ju Pan2feb4592019-09-16 20:56:38 +0000272 */
273 virtual void
274 afterNewNextHop(const fib::NextHop& nextHop, const shared_ptr<pit::Entry>& pitEntry);
275
Junxiao Shid3c792f2014-01-30 00:46:13 -0700276protected: // actions
Davide Pesavento0498ce82021-06-14 02:02:21 -0400277 /**
278 * \brief Send an Interest packet.
279 * \param interest the Interest packet
280 * \param egress face through which to send out the Interest
281 * \param pitEntry the PIT entry
282 * \return A pointer to the out-record created or nullptr if the Interest was dropped
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700283 */
Davide Pesavento264af772021-02-09 21:48:24 -0500284 NFD_VIRTUAL_WITH_TESTS pit::OutRecord*
Davide Pesavento0498ce82021-06-14 02:02:21 -0400285 sendInterest(const Interest& interest, Face& egress, const shared_ptr<pit::Entry>& pitEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -0700286
Davide Pesavento0498ce82021-06-14 02:02:21 -0400287 /**
288 * \brief Send a Data packet.
289 * \param data the Data packet
290 * \param egress face through which to send out the Data
291 * \param pitEntry the PIT entry
292 * \return Whether the Data was sent (true) or dropped (false)
Teng Liang85a36632018-03-21 05:59:34 -0700293 */
Davide Pesavento264af772021-02-09 21:48:24 -0500294 NFD_VIRTUAL_WITH_TESTS bool
Davide Pesavento0498ce82021-06-14 02:02:21 -0400295 sendData(const Data& data, Face& egress, const shared_ptr<pit::Entry>& pitEntry);
Teng Liang85a36632018-03-21 05:59:34 -0700296
Davide Pesavento0498ce82021-06-14 02:02:21 -0400297 /**
298 * \brief Send a Data packet to all matched and qualified faces.
Teng Liang43bb2312018-03-26 04:16:42 -0700299 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400300 * A matched face qualifies if it is ad-hoc OR it is NOT \p inFace.
Teng Liang43bb2312018-03-26 04:16:42 -0700301 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400302 * \param data the Data packet
303 * \param pitEntry the PIT entry
304 * \param inFace face on which the Data arrived
Teng Liang43bb2312018-03-26 04:16:42 -0700305 */
Davide Pesavento264af772021-02-09 21:48:24 -0500306 NFD_VIRTUAL_WITH_TESTS void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400307 sendDataToAll(const Data& data, const shared_ptr<pit::Entry>& pitEntry, const Face& inFace);
Teng Liang85a36632018-03-21 05:59:34 -0700308
Davide Pesavento0498ce82021-06-14 02:02:21 -0400309 /**
310 * \brief Schedule the PIT entry for immediate deletion.
Junxiao Shi679e9272014-02-15 20:10:21 -0700311 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400312 * This helper function sets the PIT entry expiry time to zero.
313 * The strategy should invoke this function when it concludes that the Interest cannot
314 * be forwarded and it does not want to wait for responses from existing upstream nodes.
Junxiao Shid3c792f2014-01-30 00:46:13 -0700315 */
Davide Pesavento264af772021-02-09 21:48:24 -0500316 NFD_VIRTUAL_WITH_TESTS void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000317 rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry)
318 {
Teng Liang7003e0b2018-03-03 16:03:30 -0700319 this->setExpiryTimer(pitEntry, 0_ms);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000320 }
Junxiao Shidbe71732014-02-21 22:23:28 -0700321
Davide Pesavento0498ce82021-06-14 02:02:21 -0400322 /**
323 * \brief Send a Nack packet.
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700324 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400325 * The egress face must have a PIT in-record, otherwise this method has no effect.
Teng Liangebc20f62020-06-23 16:55:20 -0700326 *
Davide Pesavento0498ce82021-06-14 02:02:21 -0400327 * \param header the Nack header
328 * \param egress face through which to send out the Nack
329 * \param pitEntry the PIT entry
330 * \return Whether the Nack was sent (true) or dropped (false)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700331 */
Davide Pesavento264af772021-02-09 21:48:24 -0500332 NFD_VIRTUAL_WITH_TESTS bool
Davide Pesavento0498ce82021-06-14 02:02:21 -0400333 sendNack(const lp::NackHeader& header, Face& egress, const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shib9420cf2016-08-13 04:38:52 +0000334 {
Davide Pesavento0498ce82021-06-14 02:02:21 -0400335 return m_forwarder.onOutgoingNack(header, egress, pitEntry);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000336 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700337
Davide Pesavento0498ce82021-06-14 02:02:21 -0400338 /**
339 * \brief Send Nack to every face that has an in-record, except those in \p exceptFaces
340 * \param header the Nack header
341 * \param pitEntry the PIT entry
342 * \param exceptFaces list of faces that should be excluded from sending Nacks
343 * \note This is not an action, but a helper that invokes the sendNack() action.
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700344 */
345 void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400346 sendNacks(const lp::NackHeader& header, const shared_ptr<pit::Entry>& pitEntry,
Teng Liangebc20f62020-06-23 16:55:20 -0700347 std::initializer_list<const Face*> exceptFaces = {});
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700348
Davide Pesavento0498ce82021-06-14 02:02:21 -0400349 /**
350 * \brief Schedule the PIT entry to be erased after \p duration.
Teng Liang7003e0b2018-03-03 16:03:30 -0700351 */
352 void
353 setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
354 {
355 m_forwarder.setExpiryTimer(pitEntry, duration);
356 }
357
Junxiao Shidbe71732014-02-21 22:23:28 -0700358protected: // accessors
Davide Pesavento0498ce82021-06-14 02:02:21 -0400359 /**
360 * \brief Performs a FIB lookup, considering Link object if present.
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000361 */
Junxiao Shi8d843142016-07-11 22:42:42 +0000362 const fib::Entry&
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000363 lookupFib(const pit::Entry& pitEntry) const;
Junxiao Shi8d843142016-07-11 22:42:42 +0000364
Junxiao Shidbe71732014-02-21 22:23:28 -0700365 MeasurementsAccessor&
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400366 getMeasurements() noexcept
Junxiao Shib9420cf2016-08-13 04:38:52 +0000367 {
368 return m_measurements;
369 }
Junxiao Shidbe71732014-02-21 22:23:28 -0700370
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000371 Face*
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400372 getFace(FaceId id) const noexcept
Junxiao Shib9420cf2016-08-13 04:38:52 +0000373 {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400374 return getFaceTable().get(id);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000375 }
Junxiao Shi2d9bdc82014-03-02 20:55:42 -0700376
Junxiao Shi49e11e72014-12-14 19:46:05 -0700377 const FaceTable&
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400378 getFaceTable() const noexcept
Junxiao Shib9420cf2016-08-13 04:38:52 +0000379 {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400380 return m_forwarder.m_faceTable;
Junxiao Shib9420cf2016-08-13 04:38:52 +0000381 }
Junxiao Shi49e11e72014-12-14 19:46:05 -0700382
Junxiao Shi18739c42016-12-22 08:03:00 +0000383protected: // instance name
384 struct ParsedInstanceName
385 {
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400386 Name strategyName; ///< Strategy name without parameters
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400387 std::optional<uint64_t> version; ///< The strategy version number, if present
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400388 PartialName parameters; ///< Parameter components, may be empty
Junxiao Shi18739c42016-12-22 08:03:00 +0000389 };
390
Eric Newberryc68b2e82020-04-16 12:40:30 -0700391 /** \brief Parse a strategy instance name
Junxiao Shi18739c42016-12-22 08:03:00 +0000392 * \param input strategy instance name, may contain version and parameters
393 * \throw std::invalid_argument input format is unacceptable
394 */
395 static ParsedInstanceName
396 parseInstanceName(const Name& input);
397
Eric Newberryc68b2e82020-04-16 12:40:30 -0700398 /** \brief Construct a strategy instance name
Junxiao Shi18739c42016-12-22 08:03:00 +0000399 * \param input strategy instance name, may contain version and parameters
400 * \param strategyName strategy name with version but without parameters;
401 * typically this should be \p getStrategyName()
402 *
403 * If \p input contains a version component, return \p input unchanged.
404 * Otherwise, return \p input plus the version component taken from \p strategyName.
405 * This allows a strategy instance to be constructed with an unversioned name,
406 * but its final instance name should contain the version.
407 */
408 static Name
409 makeInstanceName(const Name& input, const Name& strategyName);
410
Eric Newberryc68b2e82020-04-16 12:40:30 -0700411 /** \brief Set strategy instance name
Junxiao Shi18739c42016-12-22 08:03:00 +0000412 * \note This must be called by strategy subclass constructor.
413 */
414 void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400415 setInstanceName(const Name& name) noexcept
Junxiao Shi18739c42016-12-22 08:03:00 +0000416 {
417 m_name = name;
418 }
Junxiao Shi49e11e72014-12-14 19:46:05 -0700419
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400420NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
421 /**
422 * \brief Parse strategy parameters encoded in a strategy instance name
423 * \param params encoded parameters, typically obtained from a call to parseInstanceName()
424 * \throw std::invalid_argument the encoding format is invalid or unsupported by this implementation
425 */
426 static StrategyParameters
427 parseParameters(const PartialName& params);
428
Junxiao Shic34d1672016-12-09 15:57:59 +0000429private: // registry
Davide Pesavento0498ce82021-06-14 02:02:21 -0400430 using CreateFunc = std::function<unique_ptr<Strategy>(Forwarder&, const Name& /*strategyName*/)>;
431 using Registry = std::map<Name, CreateFunc>; // indexed by strategy name
Junxiao Shic34d1672016-12-09 15:57:59 +0000432
433 static Registry&
434 getRegistry();
435
436 static Registry::const_iterator
Junxiao Shi18739c42016-12-22 08:03:00 +0000437 find(const Name& instanceName);
438
439protected: // accessors
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400440 signal::Signal<FaceTable, Face>& afterAddFace;
441 signal::Signal<FaceTable, Face>& beforeRemoveFace;
Junxiao Shic34d1672016-12-09 15:57:59 +0000442
443private: // instance fields
Junxiao Shibb5105f2014-03-03 12:06:45 -0700444 Name m_name;
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700445 Forwarder& m_forwarder;
Junxiao Shidbe71732014-02-21 22:23:28 -0700446 MeasurementsAccessor m_measurements;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700447};
448
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400449class StrategyParameters : public std::map<std::string, std::string>
450{
451public:
452 // Note: only arithmetic types are supported by getOrDefault() for now
453
454 template<typename T>
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400455 std::enable_if_t<std::is_signed_v<T>, T>
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400456 getOrDefault(const key_type& key, const T& defaultVal) const
457 {
458 auto it = find(key);
459 if (it == end()) {
460 return defaultVal;
461 }
462
463 T val{};
464 if (!boost::conversion::try_lexical_convert(it->second, val)) {
465 NDN_THROW(std::invalid_argument(key + " value is malformed"));
466 }
467 return val;
468 }
469
470 template<typename T>
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400471 std::enable_if_t<std::is_unsigned_v<T>, T>
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400472 getOrDefault(const key_type& key, const T& defaultVal) const
473 {
474 auto it = find(key);
475 if (it == end()) {
476 return defaultVal;
477 }
478
479 if (it->second.find('-') != std::string::npos) {
480 NDN_THROW(std::invalid_argument(key + " cannot be negative"));
481 }
482
483 T val{};
484 if (!boost::conversion::try_lexical_convert(it->second, val)) {
485 NDN_THROW(std::invalid_argument(key + " value is malformed"));
486 }
487 return val;
488 }
489};
490
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400491} // namespace nfd::fw
Junxiao Shid3c792f2014-01-30 00:46:13 -0700492
Alex Lane653eb072023-07-27 22:11:46 -0400493/**
494 * \brief Registers a forwarding strategy.
Junxiao Shic34d1672016-12-09 15:57:59 +0000495 *
Alex Lane653eb072023-07-27 22:11:46 -0400496 * This macro should appear once in the `.cpp` of each strategy.
Junxiao Shic34d1672016-12-09 15:57:59 +0000497 */
498#define NFD_REGISTER_STRATEGY(S) \
499static class NfdAuto ## S ## StrategyRegistrationClass \
500{ \
501public: \
502 NfdAuto ## S ## StrategyRegistrationClass() \
503 { \
504 ::nfd::fw::Strategy::registerType<S>(); \
505 } \
506} g_nfdAuto ## S ## StrategyRegistrationVariable
507
Alex Lane653eb072023-07-27 22:11:46 -0400508/// Logs the reception of \p interest on \p ingress, followed by \p msg, at DEBUG level.
509#define NFD_LOG_INTEREST_FROM(interest, ingress, msg) \
510 NFD_LOG_DEBUG("interest=" << (interest).getName() << \
511 " nonce=" << (interest).getNonce() << \
512 " from=" << (ingress) << \
513 ' ' << msg)
514
515/// Logs the reception of \p data on \p ingress, followed by \p msg, at DEBUG level.
516#define NFD_LOG_DATA_FROM(data, ingress, msg) \
517 NFD_LOG_DEBUG("data=" << (data).getName() << \
518 " from=" << (ingress) << \
519 ' ' << msg)
520
521/// Logs the reception of \p nack on \p ingress, followed by \p msg, at DEBUG level.
522#define NFD_LOG_NACK_FROM(nack, ingress, msg) \
523 NFD_LOG_DEBUG("nack=" << (nack).getInterest().getName() << \
524 " nonce=" << (nack).getInterest().getNonce() << \
525 " reason=" << (nack).getReason() << \
526 " from=" << (ingress) << \
527 ' ' << msg)
528
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700529#endif // NFD_DAEMON_FW_STRATEGY_HPP