blob: b7b7adba40f4d27fb1dbaf006269f0f4e99a71c7 [file] [log] [blame]
Junxiao Shid3c792f2014-01-30 00:46:13 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi8d843142016-07-11 22:42:42 +00003 * Copyright (c) 2014-2016, 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
Junxiao Shibb5105f2014-03-03 12:06:45 -070035/** \brief represents a forwarding strategy
Junxiao Shid3c792f2014-01-30 00:46:13 -070036 */
Junxiao Shic34d1672016-12-09 15:57:59 +000037class Strategy : noncopyable
Junxiao Shid3c792f2014-01-30 00:46:13 -070038{
Junxiao Shic34d1672016-12-09 15:57:59 +000039public: // registry
40 /** \brief register a strategy type
41 * \tparam S subclass of Strategy
Junxiao Shi18739c42016-12-22 08:03:00 +000042 * \param strategyName strategy program name, must contain version
Junxiao Shic34d1672016-12-09 15:57:59 +000043 * \note It is permitted to register the same strategy type under multiple names,
44 * which is useful in tests and for creating aliases.
45 */
46 template<typename S>
47 static void
Junxiao Shi037f4ab2016-12-13 04:27:06 +000048 registerType(const Name& strategyName = S::getStrategyName())
Junxiao Shic34d1672016-12-09 15:57:59 +000049 {
Junxiao Shi91f6ee02016-12-29 21:44:44 +000050 BOOST_ASSERT(strategyName.size() > 1);
Junxiao Shi18739c42016-12-22 08:03:00 +000051 BOOST_ASSERT(strategyName.at(-1).isVersion());
Junxiao Shic34d1672016-12-09 15:57:59 +000052 Registry& registry = getRegistry();
53 BOOST_ASSERT(registry.count(strategyName) == 0);
54 registry[strategyName] = &make_unique<S, Forwarder&, const Name&>;
55 }
56
Junxiao Shi18739c42016-12-22 08:03:00 +000057 /** \return whether a strategy instance can be created from \p instanceName
58 * \param instanceName strategy instance name, may contain version and parameters
Junxiao Shic34d1672016-12-09 15:57:59 +000059 * \note This function finds a strategy type using same rules as \p create ,
60 * but does not attempt to construct an instance.
61 */
62 static bool
Junxiao Shi18739c42016-12-22 08:03:00 +000063 canCreate(const Name& instanceName);
Junxiao Shic34d1672016-12-09 15:57:59 +000064
Junxiao Shi18739c42016-12-22 08:03:00 +000065 /** \return a strategy instance created from \p instanceName
66 * \retval nullptr if !canCreate(instanceName)
67 * \throw std::invalid_argument strategy type constructor does not accept
68 * specified version or parameters
Junxiao Shic34d1672016-12-09 15:57:59 +000069 */
70 static unique_ptr<Strategy>
Junxiao Shi18739c42016-12-22 08:03:00 +000071 create(const Name& instanceName, Forwarder& forwarder);
Junxiao Shic34d1672016-12-09 15:57:59 +000072
73 /** \return registered versioned strategy names
74 */
75 static std::set<Name>
76 listRegistered();
77
78public: // constructor, destructor, strategy name
Junxiao Shie93d6a32014-09-07 16:13:22 -070079 /** \brief construct a strategy instance
Junxiao Shi18739c42016-12-22 08:03:00 +000080 * \param forwarder a reference to the forwarder, used to enable actions and accessors.
81 * \note Strategy subclass constructor should not retain a reference to the forwarder.
Junxiao Shie93d6a32014-09-07 16:13:22 -070082 */
Junxiao Shi18739c42016-12-22 08:03:00 +000083 explicit
84 Strategy(Forwarder& forwarder);
Junxiao Shidbe71732014-02-21 22:23:28 -070085
Junxiao Shid3c792f2014-01-30 00:46:13 -070086 virtual
87 ~Strategy();
Junxiao Shidbe71732014-02-21 22:23:28 -070088
Junxiao Shi037f4ab2016-12-13 04:27:06 +000089#ifdef DOXYGEN
Junxiao Shi18739c42016-12-22 08:03:00 +000090 /** \return strategy program name
91 *
92 * The strategy name is defined by the strategy program.
93 * It must end with a version component.
Junxiao Shi037f4ab2016-12-13 04:27:06 +000094 */
95 static const Name&
96 getStrategyName();
97#endif
98
Junxiao Shi18739c42016-12-22 08:03:00 +000099 /** \return strategy instance name
100 *
101 * The instance name is assigned during instantiation.
102 * It contains a version component, and may have extra parameter components.
Junxiao Shib9420cf2016-08-13 04:38:52 +0000103 */
Junxiao Shibb5105f2014-03-03 12:06:45 -0700104 const Name&
Junxiao Shi18739c42016-12-22 08:03:00 +0000105 getInstanceName() const
Junxiao Shib9420cf2016-08-13 04:38:52 +0000106 {
107 return m_name;
108 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700109
Junxiao Shi679e9272014-02-15 20:10:21 -0700110public: // triggers
111 /** \brief trigger after Interest is received
112 *
113 * The Interest:
114 * - does not violate Scope
115 * - is not looped
116 * - cannot be satisfied by ContentStore
117 * - is under a namespace managed by this strategy
118 *
119 * The strategy should decide whether and where to forward this Interest.
120 * - If the strategy decides to forward this Interest,
121 * invoke this->sendInterest one or more times, either now or shortly after
122 * - If strategy concludes that this Interest cannot be forwarded,
Junxiao Shi09498f02014-02-26 19:41:08 -0700123 * invoke this->rejectPendingInterest so that PIT entry will be deleted shortly
Junxiao Shi82e7f582014-09-07 15:15:40 -0700124 *
Junxiao Shi15e98b02016-08-12 11:21:44 +0000125 * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior
126 * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>.
Junxiao Shi679e9272014-02-15 20:10:21 -0700127 */
Junxiao Shid3c792f2014-01-30 00:46:13 -0700128 virtual void
Junxiao Shi8d843142016-07-11 22:42:42 +0000129 afterReceiveInterest(const Face& inFace, const Interest& interest,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000130 const shared_ptr<pit::Entry>& pitEntry) = 0;
Junxiao Shidbe71732014-02-21 22:23:28 -0700131
Junxiao Shi22be22c2014-02-16 22:53:48 -0700132 /** \brief trigger before PIT entry is satisfied
133 *
Junxiao Shi82e7f582014-09-07 15:15:40 -0700134 * This trigger is invoked when an incoming Data satisfies the PIT entry.
135 * It can be invoked even if the PIT entry has already been satisfied.
136 *
Junxiao Shi22be22c2014-02-16 22:53:48 -0700137 * In this base class this method does nothing.
Junxiao Shi82e7f582014-09-07 15:15:40 -0700138 *
Junxiao Shi15e98b02016-08-12 11:21:44 +0000139 * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior
140 * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>.
Junxiao Shi22be22c2014-02-16 22:53:48 -0700141 */
142 virtual void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000143 beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shi82e7f582014-09-07 15:15:40 -0700144 const Face& inFace, const Data& data);
Junxiao Shidbe71732014-02-21 22:23:28 -0700145
Junxiao Shi679e9272014-02-15 20:10:21 -0700146 /** \brief trigger before PIT entry expires
147 *
148 * PIT entry expires when InterestLifetime has elapsed for all InRecords,
149 * and it is not satisfied by an incoming Data.
150 *
151 * This trigger is not invoked for PIT entry already satisfied.
152 *
153 * In this base class this method does nothing.
Junxiao Shi82e7f582014-09-07 15:15:40 -0700154 *
Junxiao Shi15e98b02016-08-12 11:21:44 +0000155 * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior
156 * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>,
157 * although this isn't useful here because PIT entry would be deleted shortly after.
Junxiao Shi679e9272014-02-15 20:10:21 -0700158 */
159 virtual void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000160 beforeExpirePendingInterest(const shared_ptr<pit::Entry>& pitEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -0700161
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700162 /** \brief trigger after Nack is received
163 *
164 * This trigger is invoked when an incoming Nack is received in response to
165 * an forwarded Interest.
166 * The Nack has been confirmed to be a response to the last Interest forwarded
167 * to that upstream, i.e. the PIT out-record exists and has a matching Nonce.
168 * The NackHeader has been recorded in the PIT out-record.
169 *
170 * In this base class this method does nothing.
171 *
Junxiao Shi15e98b02016-08-12 11:21:44 +0000172 * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior
173 * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>.
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700174 */
175 virtual void
176 afterReceiveNack(const Face& inFace, const lp::Nack& nack,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000177 const shared_ptr<pit::Entry>& pitEntry);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700178
Junxiao Shid3c792f2014-01-30 00:46:13 -0700179protected: // actions
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700180 /** \brief send Interest to outFace
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -0500181 * \param pitEntry PIT entry
182 * \param outFace face through which to send out the Interest
Junxiao Shic5f651f2016-11-17 22:58:12 +0000183 * \param interest the Interest packet
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700184 */
Junxiao Shi727ed292014-02-19 23:26:45 -0700185 VIRTUAL_WITH_TESTS void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000186 sendInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace,
Junxiao Shic5f651f2016-11-17 22:58:12 +0000187 const Interest& interest)
Junxiao Shib9420cf2016-08-13 04:38:52 +0000188 {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000189 m_forwarder.onOutgoingInterest(pitEntry, outFace, interest);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000190 }
Junxiao Shidbe71732014-02-21 22:23:28 -0700191
Junxiao Shid3c792f2014-01-30 00:46:13 -0700192 /** \brief decide that a pending Interest cannot be forwarded
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -0500193 * \param pitEntry PIT entry
Junxiao Shi679e9272014-02-15 20:10:21 -0700194 *
Junxiao Shid3c792f2014-01-30 00:46:13 -0700195 * This shall not be called if the pending Interest has been
196 * forwarded earlier, and does not need to be resent now.
197 */
Junxiao Shi727ed292014-02-19 23:26:45 -0700198 VIRTUAL_WITH_TESTS void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000199 rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry)
200 {
201 m_forwarder.onInterestReject(pitEntry);
202 }
Junxiao Shidbe71732014-02-21 22:23:28 -0700203
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700204 /** \brief send Nack to outFace
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -0500205 * \param pitEntry PIT entry
206 * \param outFace face through which to send out the Nack
207 * \param header Nack header
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700208 *
209 * The outFace must have a PIT in-record, otherwise this method has no effect.
210 */
211 VIRTUAL_WITH_TESTS void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000212 sendNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
213 const lp::NackHeader& header)
214 {
215 m_forwarder.onOutgoingNack(pitEntry, outFace, header);
216 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700217
218 /** \brief send Nack to every face that has an in-record,
219 * except those in \p exceptFaces
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -0500220 * \param pitEntry PIT entry
221 * \param header NACK header
222 * \param exceptFaces list of faces that should be excluded from sending Nacks
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700223 * \note This is not an action, but a helper that invokes the sendNack action.
224 */
225 void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000226 sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700227 std::initializer_list<const Face*> exceptFaces = std::initializer_list<const Face*>());
228
Junxiao Shidbe71732014-02-21 22:23:28 -0700229protected: // accessors
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000230 /** \brief performs a FIB lookup, considering Link object if present
231 */
Junxiao Shi8d843142016-07-11 22:42:42 +0000232 const fib::Entry&
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000233 lookupFib(const pit::Entry& pitEntry) const;
Junxiao Shi8d843142016-07-11 22:42:42 +0000234
Junxiao Shidbe71732014-02-21 22:23:28 -0700235 MeasurementsAccessor&
Junxiao Shib9420cf2016-08-13 04:38:52 +0000236 getMeasurements()
237 {
238 return m_measurements;
239 }
Junxiao Shidbe71732014-02-21 22:23:28 -0700240
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000241 Face*
Junxiao Shib9420cf2016-08-13 04:38:52 +0000242 getFace(FaceId id) const
243 {
244 return m_forwarder.getFace(id);
245 }
Junxiao Shi2d9bdc82014-03-02 20:55:42 -0700246
Junxiao Shi49e11e72014-12-14 19:46:05 -0700247 const FaceTable&
Junxiao Shib9420cf2016-08-13 04:38:52 +0000248 getFaceTable() const
249 {
250 return m_forwarder.getFaceTable();
251 }
Junxiao Shi49e11e72014-12-14 19:46:05 -0700252
Junxiao Shi18739c42016-12-22 08:03:00 +0000253protected: // instance name
254 struct ParsedInstanceName
255 {
256 Name strategyName; ///< strategy name without parameters
257 ndn::optional<uint64_t> version; ///< whether strategyName contains a version component
258 PartialName parameters; ///< parameter components
259 };
260
261 /** \brief parse a strategy instance name
262 * \param input strategy instance name, may contain version and parameters
263 * \throw std::invalid_argument input format is unacceptable
264 */
265 static ParsedInstanceName
266 parseInstanceName(const Name& input);
267
268 /** \brief construct a strategy instance name
269 * \param input strategy instance name, may contain version and parameters
270 * \param strategyName strategy name with version but without parameters;
271 * typically this should be \p getStrategyName()
272 *
273 * If \p input contains a version component, return \p input unchanged.
274 * Otherwise, return \p input plus the version component taken from \p strategyName.
275 * This allows a strategy instance to be constructed with an unversioned name,
276 * but its final instance name should contain the version.
277 */
278 static Name
279 makeInstanceName(const Name& input, const Name& strategyName);
280
281 /** \brief set strategy instance name
282 * \note This must be called by strategy subclass constructor.
283 */
284 void
285 setInstanceName(const Name& name)
286 {
287 m_name = name;
288 }
Junxiao Shi49e11e72014-12-14 19:46:05 -0700289
Junxiao Shic34d1672016-12-09 15:57:59 +0000290private: // registry
291 typedef std::function<unique_ptr<Strategy>(Forwarder& forwarder, const Name& strategyName)> CreateFunc;
292 typedef std::map<Name, CreateFunc> Registry; // indexed by strategy name
293
294 static Registry&
295 getRegistry();
296
297 static Registry::const_iterator
Junxiao Shi18739c42016-12-22 08:03:00 +0000298 find(const Name& instanceName);
299
300protected: // accessors
301 signal::Signal<FaceTable, Face&>& afterAddFace;
302 signal::Signal<FaceTable, Face&>& beforeRemoveFace;
Junxiao Shic34d1672016-12-09 15:57:59 +0000303
304private: // instance fields
Junxiao Shibb5105f2014-03-03 12:06:45 -0700305 Name m_name;
306
Junxiao Shi679e9272014-02-15 20:10:21 -0700307 /** \brief reference to the forwarder
308 *
309 * Triggers can access forwarder indirectly via actions.
310 */
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700311 Forwarder& m_forwarder;
Junxiao Shidbe71732014-02-21 22:23:28 -0700312
313 MeasurementsAccessor m_measurements;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700314};
315
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700316} // namespace fw
Junxiao Shid3c792f2014-01-30 00:46:13 -0700317} // namespace nfd
318
Junxiao Shic34d1672016-12-09 15:57:59 +0000319/** \brief registers a strategy
320 *
321 * This macro should appear once in .cpp of each strategy.
322 */
323#define NFD_REGISTER_STRATEGY(S) \
324static class NfdAuto ## S ## StrategyRegistrationClass \
325{ \
326public: \
327 NfdAuto ## S ## StrategyRegistrationClass() \
328 { \
329 ::nfd::fw::Strategy::registerType<S>(); \
330 } \
331} g_nfdAuto ## S ## StrategyRegistrationVariable
332
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700333#endif // NFD_DAEMON_FW_STRATEGY_HPP