blob: 1f34ce214c316fc2d85cf02f9018e7f7d76f80cc [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 Shi18739c42016-12-22 08:03:00 +000050 BOOST_ASSERT(strategyName.at(-1).isVersion());
Junxiao Shic34d1672016-12-09 15:57:59 +000051 Registry& registry = getRegistry();
52 BOOST_ASSERT(registry.count(strategyName) == 0);
53 registry[strategyName] = &make_unique<S, Forwarder&, const Name&>;
54 }
55
Junxiao Shi18739c42016-12-22 08:03:00 +000056 /** \return whether a strategy instance can be created from \p instanceName
57 * \param instanceName strategy instance name, may contain version and parameters
Junxiao Shic34d1672016-12-09 15:57:59 +000058 * \note This function finds a strategy type using same rules as \p create ,
59 * but does not attempt to construct an instance.
60 */
61 static bool
Junxiao Shi18739c42016-12-22 08:03:00 +000062 canCreate(const Name& instanceName);
Junxiao Shic34d1672016-12-09 15:57:59 +000063
Junxiao Shi18739c42016-12-22 08:03:00 +000064 /** \return a strategy instance created from \p instanceName
65 * \retval nullptr if !canCreate(instanceName)
66 * \throw std::invalid_argument strategy type constructor does not accept
67 * specified version or parameters
Junxiao Shic34d1672016-12-09 15:57:59 +000068 */
69 static unique_ptr<Strategy>
Junxiao Shi18739c42016-12-22 08:03:00 +000070 create(const Name& instanceName, Forwarder& forwarder);
Junxiao Shic34d1672016-12-09 15:57:59 +000071
72 /** \return registered versioned strategy names
73 */
74 static std::set<Name>
75 listRegistered();
76
77public: // constructor, destructor, strategy name
Junxiao Shie93d6a32014-09-07 16:13:22 -070078 /** \brief construct a strategy instance
Junxiao Shi18739c42016-12-22 08:03:00 +000079 * \param forwarder a reference to the forwarder, used to enable actions and accessors.
80 * \note Strategy subclass constructor should not retain a reference to the forwarder.
Junxiao Shie93d6a32014-09-07 16:13:22 -070081 */
Junxiao Shi18739c42016-12-22 08:03:00 +000082 explicit
83 Strategy(Forwarder& forwarder);
Junxiao Shidbe71732014-02-21 22:23:28 -070084
Junxiao Shid3c792f2014-01-30 00:46:13 -070085 virtual
86 ~Strategy();
Junxiao Shidbe71732014-02-21 22:23:28 -070087
Junxiao Shi037f4ab2016-12-13 04:27:06 +000088#ifdef DOXYGEN
Junxiao Shi18739c42016-12-22 08:03:00 +000089 /** \return strategy program name
90 *
91 * The strategy name is defined by the strategy program.
92 * It must end with a version component.
Junxiao Shi037f4ab2016-12-13 04:27:06 +000093 */
94 static const Name&
95 getStrategyName();
96#endif
97
Junxiao Shi18739c42016-12-22 08:03:00 +000098 /** \return strategy instance name
99 *
100 * The instance name is assigned during instantiation.
101 * It contains a version component, and may have extra parameter components.
Junxiao Shib9420cf2016-08-13 04:38:52 +0000102 */
Junxiao Shibb5105f2014-03-03 12:06:45 -0700103 const Name&
Junxiao Shi18739c42016-12-22 08:03:00 +0000104 getInstanceName() const
Junxiao Shib9420cf2016-08-13 04:38:52 +0000105 {
106 return m_name;
107 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700108
Junxiao Shi679e9272014-02-15 20:10:21 -0700109public: // triggers
110 /** \brief trigger after Interest is received
111 *
112 * The Interest:
113 * - does not violate Scope
114 * - is not looped
115 * - cannot be satisfied by ContentStore
116 * - is under a namespace managed by this strategy
117 *
118 * The strategy should decide whether and where to forward this Interest.
119 * - If the strategy decides to forward this Interest,
120 * invoke this->sendInterest one or more times, either now or shortly after
121 * - If strategy concludes that this Interest cannot be forwarded,
Junxiao Shi09498f02014-02-26 19:41:08 -0700122 * invoke this->rejectPendingInterest so that PIT entry will be deleted shortly
Junxiao Shi82e7f582014-09-07 15:15:40 -0700123 *
Junxiao Shi15e98b02016-08-12 11:21:44 +0000124 * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior
125 * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>.
Junxiao Shi679e9272014-02-15 20:10:21 -0700126 */
Junxiao Shid3c792f2014-01-30 00:46:13 -0700127 virtual void
Junxiao Shi8d843142016-07-11 22:42:42 +0000128 afterReceiveInterest(const Face& inFace, const Interest& interest,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000129 const shared_ptr<pit::Entry>& pitEntry) = 0;
Junxiao Shidbe71732014-02-21 22:23:28 -0700130
Junxiao Shi22be22c2014-02-16 22:53:48 -0700131 /** \brief trigger before PIT entry is satisfied
132 *
Junxiao Shi82e7f582014-09-07 15:15:40 -0700133 * This trigger is invoked when an incoming Data satisfies the PIT entry.
134 * It can be invoked even if the PIT entry has already been satisfied.
135 *
Junxiao Shi22be22c2014-02-16 22:53:48 -0700136 * In this base class this method does nothing.
Junxiao Shi82e7f582014-09-07 15:15:40 -0700137 *
Junxiao Shi15e98b02016-08-12 11:21:44 +0000138 * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior
139 * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>.
Junxiao Shi22be22c2014-02-16 22:53:48 -0700140 */
141 virtual void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000142 beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shi82e7f582014-09-07 15:15:40 -0700143 const Face& inFace, const Data& data);
Junxiao Shidbe71732014-02-21 22:23:28 -0700144
Junxiao Shi679e9272014-02-15 20:10:21 -0700145 /** \brief trigger before PIT entry expires
146 *
147 * PIT entry expires when InterestLifetime has elapsed for all InRecords,
148 * and it is not satisfied by an incoming Data.
149 *
150 * This trigger is not invoked for PIT entry already satisfied.
151 *
152 * In this base class this method does nothing.
Junxiao Shi82e7f582014-09-07 15:15:40 -0700153 *
Junxiao Shi15e98b02016-08-12 11:21:44 +0000154 * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior
155 * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>,
156 * although this isn't useful here because PIT entry would be deleted shortly after.
Junxiao Shi679e9272014-02-15 20:10:21 -0700157 */
158 virtual void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000159 beforeExpirePendingInterest(const shared_ptr<pit::Entry>& pitEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -0700160
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700161 /** \brief trigger after Nack is received
162 *
163 * This trigger is invoked when an incoming Nack is received in response to
164 * an forwarded Interest.
165 * The Nack has been confirmed to be a response to the last Interest forwarded
166 * to that upstream, i.e. the PIT out-record exists and has a matching Nonce.
167 * The NackHeader has been recorded in the PIT out-record.
168 *
169 * In this base class this method does nothing.
170 *
Junxiao Shi15e98b02016-08-12 11:21:44 +0000171 * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior
172 * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>.
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700173 */
174 virtual void
175 afterReceiveNack(const Face& inFace, const lp::Nack& nack,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000176 const shared_ptr<pit::Entry>& pitEntry);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700177
Junxiao Shid3c792f2014-01-30 00:46:13 -0700178protected: // actions
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700179 /** \brief send Interest to outFace
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -0500180 * \param pitEntry PIT entry
181 * \param outFace face through which to send out the Interest
Junxiao Shic5f651f2016-11-17 22:58:12 +0000182 * \param interest the Interest packet
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700183 */
Junxiao Shi727ed292014-02-19 23:26:45 -0700184 VIRTUAL_WITH_TESTS void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000185 sendInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace,
Junxiao Shic5f651f2016-11-17 22:58:12 +0000186 const Interest& interest)
Junxiao Shib9420cf2016-08-13 04:38:52 +0000187 {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000188 m_forwarder.onOutgoingInterest(pitEntry, outFace, interest);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000189 }
Junxiao Shidbe71732014-02-21 22:23:28 -0700190
Junxiao Shid3c792f2014-01-30 00:46:13 -0700191 /** \brief decide that a pending Interest cannot be forwarded
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -0500192 * \param pitEntry PIT entry
Junxiao Shi679e9272014-02-15 20:10:21 -0700193 *
Junxiao Shid3c792f2014-01-30 00:46:13 -0700194 * This shall not be called if the pending Interest has been
195 * forwarded earlier, and does not need to be resent now.
196 */
Junxiao Shi727ed292014-02-19 23:26:45 -0700197 VIRTUAL_WITH_TESTS void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000198 rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry)
199 {
200 m_forwarder.onInterestReject(pitEntry);
201 }
Junxiao Shidbe71732014-02-21 22:23:28 -0700202
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700203 /** \brief send Nack to outFace
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -0500204 * \param pitEntry PIT entry
205 * \param outFace face through which to send out the Nack
206 * \param header Nack header
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700207 *
208 * The outFace must have a PIT in-record, otherwise this method has no effect.
209 */
210 VIRTUAL_WITH_TESTS void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000211 sendNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
212 const lp::NackHeader& header)
213 {
214 m_forwarder.onOutgoingNack(pitEntry, outFace, header);
215 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700216
217 /** \brief send Nack to every face that has an in-record,
218 * except those in \p exceptFaces
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -0500219 * \param pitEntry PIT entry
220 * \param header NACK header
221 * \param exceptFaces list of faces that should be excluded from sending Nacks
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700222 * \note This is not an action, but a helper that invokes the sendNack action.
223 */
224 void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000225 sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700226 std::initializer_list<const Face*> exceptFaces = std::initializer_list<const Face*>());
227
Junxiao Shidbe71732014-02-21 22:23:28 -0700228protected: // accessors
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000229 /** \brief performs a FIB lookup, considering Link object if present
230 */
Junxiao Shi8d843142016-07-11 22:42:42 +0000231 const fib::Entry&
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000232 lookupFib(const pit::Entry& pitEntry) const;
Junxiao Shi8d843142016-07-11 22:42:42 +0000233
Junxiao Shidbe71732014-02-21 22:23:28 -0700234 MeasurementsAccessor&
Junxiao Shib9420cf2016-08-13 04:38:52 +0000235 getMeasurements()
236 {
237 return m_measurements;
238 }
Junxiao Shidbe71732014-02-21 22:23:28 -0700239
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000240 Face*
Junxiao Shib9420cf2016-08-13 04:38:52 +0000241 getFace(FaceId id) const
242 {
243 return m_forwarder.getFace(id);
244 }
Junxiao Shi2d9bdc82014-03-02 20:55:42 -0700245
Junxiao Shi49e11e72014-12-14 19:46:05 -0700246 const FaceTable&
Junxiao Shib9420cf2016-08-13 04:38:52 +0000247 getFaceTable() const
248 {
249 return m_forwarder.getFaceTable();
250 }
Junxiao Shi49e11e72014-12-14 19:46:05 -0700251
Junxiao Shi18739c42016-12-22 08:03:00 +0000252protected: // instance name
253 struct ParsedInstanceName
254 {
255 Name strategyName; ///< strategy name without parameters
256 ndn::optional<uint64_t> version; ///< whether strategyName contains a version component
257 PartialName parameters; ///< parameter components
258 };
259
260 /** \brief parse a strategy instance name
261 * \param input strategy instance name, may contain version and parameters
262 * \throw std::invalid_argument input format is unacceptable
263 */
264 static ParsedInstanceName
265 parseInstanceName(const Name& input);
266
267 /** \brief construct a strategy instance name
268 * \param input strategy instance name, may contain version and parameters
269 * \param strategyName strategy name with version but without parameters;
270 * typically this should be \p getStrategyName()
271 *
272 * If \p input contains a version component, return \p input unchanged.
273 * Otherwise, return \p input plus the version component taken from \p strategyName.
274 * This allows a strategy instance to be constructed with an unversioned name,
275 * but its final instance name should contain the version.
276 */
277 static Name
278 makeInstanceName(const Name& input, const Name& strategyName);
279
280 /** \brief set strategy instance name
281 * \note This must be called by strategy subclass constructor.
282 */
283 void
284 setInstanceName(const Name& name)
285 {
286 m_name = name;
287 }
Junxiao Shi49e11e72014-12-14 19:46:05 -0700288
Junxiao Shic34d1672016-12-09 15:57:59 +0000289private: // registry
290 typedef std::function<unique_ptr<Strategy>(Forwarder& forwarder, const Name& strategyName)> CreateFunc;
291 typedef std::map<Name, CreateFunc> Registry; // indexed by strategy name
292
293 static Registry&
294 getRegistry();
295
296 static Registry::const_iterator
Junxiao Shi18739c42016-12-22 08:03:00 +0000297 find(const Name& instanceName);
298
299protected: // accessors
300 signal::Signal<FaceTable, Face&>& afterAddFace;
301 signal::Signal<FaceTable, Face&>& beforeRemoveFace;
Junxiao Shic34d1672016-12-09 15:57:59 +0000302
303private: // instance fields
Junxiao Shibb5105f2014-03-03 12:06:45 -0700304 Name m_name;
305
Junxiao Shi679e9272014-02-15 20:10:21 -0700306 /** \brief reference to the forwarder
307 *
308 * Triggers can access forwarder indirectly via actions.
309 */
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700310 Forwarder& m_forwarder;
Junxiao Shidbe71732014-02-21 22:23:28 -0700311
312 MeasurementsAccessor m_measurements;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700313};
314
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700315} // namespace fw
Junxiao Shid3c792f2014-01-30 00:46:13 -0700316} // namespace nfd
317
Junxiao Shic34d1672016-12-09 15:57:59 +0000318/** \brief registers a strategy
319 *
320 * This macro should appear once in .cpp of each strategy.
321 */
322#define NFD_REGISTER_STRATEGY(S) \
323static class NfdAuto ## S ## StrategyRegistrationClass \
324{ \
325public: \
326 NfdAuto ## S ## StrategyRegistrationClass() \
327 { \
328 ::nfd::fw::Strategy::registerType<S>(); \
329 } \
330} g_nfdAuto ## S ## StrategyRegistrationVariable
331
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700332#endif // NFD_DAEMON_FW_STRATEGY_HPP