Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 2 | /* |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Junxiao Shi | faf3eb0 | 2015-02-16 10:50:36 -0700 | [diff] [blame] | 4 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FW_STRATEGY_HPP |
| 27 | #define NFD_DAEMON_FW_STRATEGY_HPP |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 28 | |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 29 | #include "forwarder.hpp" |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 30 | #include "table/measurements-accessor.hpp" |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 33 | namespace fw { |
| 34 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 35 | /** \brief represents a forwarding strategy |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 36 | */ |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 37 | class Strategy : noncopyable |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 38 | { |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 39 | public: // registry |
| 40 | /** \brief register a strategy type |
| 41 | * \tparam S subclass of Strategy |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 42 | * \param strategyName strategy program name, must contain version |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 43 | * \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 Shi | 037f4ab | 2016-12-13 04:27:06 +0000 | [diff] [blame] | 48 | registerType(const Name& strategyName = S::getStrategyName()) |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 49 | { |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 50 | BOOST_ASSERT(strategyName.size() > 1); |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 51 | BOOST_ASSERT(strategyName.at(-1).isVersion()); |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 52 | Registry& registry = getRegistry(); |
| 53 | BOOST_ASSERT(registry.count(strategyName) == 0); |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 54 | registry[strategyName] = [] (auto&&... args) { |
| 55 | return make_unique<S>(std::forward<decltype(args)>(args)...); |
| 56 | }; |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 59 | /** \return whether a strategy instance can be created from \p instanceName |
| 60 | * \param instanceName strategy instance name, may contain version and parameters |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 61 | * \note This function finds a strategy type using same rules as \p create , |
| 62 | * but does not attempt to construct an instance. |
| 63 | */ |
| 64 | static bool |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 65 | canCreate(const Name& instanceName); |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 66 | |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 67 | /** \return a strategy instance created from \p instanceName |
| 68 | * \retval nullptr if !canCreate(instanceName) |
| 69 | * \throw std::invalid_argument strategy type constructor does not accept |
| 70 | * specified version or parameters |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 71 | */ |
| 72 | static unique_ptr<Strategy> |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 73 | create(const Name& instanceName, Forwarder& forwarder); |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 74 | |
Junxiao Shi | 55e21b9 | 2017-01-23 03:27:47 +0000 | [diff] [blame] | 75 | /** \return whether \p instanceNameA and \p instanceNameA will initiate same strategy type |
| 76 | */ |
| 77 | static bool |
| 78 | areSameType(const Name& instanceNameA, const Name& instanceNameB); |
| 79 | |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 80 | /** \return registered versioned strategy names |
| 81 | */ |
| 82 | static std::set<Name> |
| 83 | listRegistered(); |
| 84 | |
Ju Pan | 2feb459 | 2019-09-16 20:56:38 +0000 | [diff] [blame] | 85 | public: // constructor, destructor, strategy info |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 86 | /** \brief Construct a strategy instance. |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 87 | * \param forwarder a reference to the forwarder, used to enable actions and accessors. |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 88 | * \note Strategy subclass constructor must not retain a reference to \p forwarder. |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 89 | */ |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 90 | explicit |
| 91 | Strategy(Forwarder& forwarder); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 92 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 93 | virtual |
| 94 | ~Strategy(); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 95 | |
Junxiao Shi | 037f4ab | 2016-12-13 04:27:06 +0000 | [diff] [blame] | 96 | #ifdef DOXYGEN |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 97 | /** \return strategy program name |
| 98 | * |
| 99 | * The strategy name is defined by the strategy program. |
| 100 | * It must end with a version component. |
Junxiao Shi | 037f4ab | 2016-12-13 04:27:06 +0000 | [diff] [blame] | 101 | */ |
| 102 | static const Name& |
| 103 | getStrategyName(); |
| 104 | #endif |
| 105 | |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 106 | /** \return strategy instance name |
| 107 | * |
| 108 | * The instance name is assigned during instantiation. |
| 109 | * It contains a version component, and may have extra parameter components. |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 110 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 111 | const Name& |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 112 | getInstanceName() const |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 113 | { |
| 114 | return m_name; |
| 115 | } |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 116 | |
Ju Pan | 2feb459 | 2019-09-16 20:56:38 +0000 | [diff] [blame] | 117 | /** \return whether the afterNewNextHop trigger should be invoked for this strategy. |
| 118 | */ |
| 119 | bool |
| 120 | wantNewNextHopTrigger() const |
| 121 | { |
| 122 | return m_wantNewNextHopTrigger; |
| 123 | } |
| 124 | |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 125 | public: // triggers |
| 126 | /** \brief trigger after Interest is received |
| 127 | * |
| 128 | * The Interest: |
| 129 | * - does not violate Scope |
| 130 | * - is not looped |
| 131 | * - cannot be satisfied by ContentStore |
| 132 | * - is under a namespace managed by this strategy |
| 133 | * |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 134 | * The PIT entry is set to expire after InterestLifetime has elapsed at each downstream. |
| 135 | * |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 136 | * The strategy should decide whether and where to forward this Interest. |
| 137 | * - If the strategy decides to forward this Interest, |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 138 | * invoke \c sendInterest for each upstream, either now or shortly after via a scheduler event, |
| 139 | * but before PIT entry expires. |
| 140 | * Optionally, the strategy can invoke \c setExpiryTimer to adjust how long it would wait for a response. |
| 141 | * - If the strategy has already forwarded this Interest previously and decides to continue waiting, |
| 142 | * do nothing. |
| 143 | * Optionally, the strategy can invoke \c setExpiryTimer to adjust how long it would wait for a response. |
| 144 | * - If the strategy concludes that this Interest cannot be satisfied, |
| 145 | * invoke \c rejectPendingInterest to erase the PIT entry. |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 146 | * |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 147 | * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior |
| 148 | * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>. |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 149 | */ |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 150 | virtual void |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 151 | afterReceiveInterest(const FaceEndpoint& ingress, const Interest& interest, |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 152 | const shared_ptr<pit::Entry>& pitEntry) = 0; |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 153 | |
Junxiao Shi | 22be22c | 2014-02-16 22:53:48 -0700 | [diff] [blame] | 154 | /** \brief trigger before PIT entry is satisfied |
| 155 | * |
Teng Liang | 43bb231 | 2018-03-26 04:16:42 -0700 | [diff] [blame] | 156 | * This trigger is invoked when an incoming Data satisfies more than one PIT entry. |
| 157 | * The strategy can collect measurements information, but cannot manipulate Data forwarding. |
| 158 | * When an incoming Data satisfies only one PIT entry, \c afterReceiveData is invoked instead |
| 159 | * and given full control over Data forwarding. If a strategy does not override \c afterReceiveData, |
| 160 | * the default implementation invokes \c beforeSatisfyInterest. |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 161 | * |
Teng Liang | 43bb231 | 2018-03-26 04:16:42 -0700 | [diff] [blame] | 162 | * Normally, PIT entries would be erased after receiving the first matching Data. |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 163 | * If the strategy wishes to collect responses from additional upstream nodes, |
Teng Liang | 43bb231 | 2018-03-26 04:16:42 -0700 | [diff] [blame] | 164 | * it should invoke \c setExpiryTimer within this function to prolong the PIT entry lifetime. |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 165 | * If a Data arrives from another upstream during the extended PIT entry lifetime, this trigger will be invoked again. |
| 166 | * At that time, this function must invoke \c setExpiryTimer again to continue collecting more responses. |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 167 | * |
Junxiao Shi | 22be22c | 2014-02-16 22:53:48 -0700 | [diff] [blame] | 168 | * In this base class this method does nothing. |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 169 | * |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 170 | * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior |
| 171 | * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>. |
Junxiao Shi | 22be22c | 2014-02-16 22:53:48 -0700 | [diff] [blame] | 172 | */ |
| 173 | virtual void |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 174 | beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry, |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 175 | const FaceEndpoint& ingress, const Data& data); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 176 | |
Teng Liang | 85a3663 | 2018-03-21 05:59:34 -0700 | [diff] [blame] | 177 | /** \brief trigger after a Data is matched in CS |
| 178 | * |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 179 | * In the base class this method sends \p data to \p ingress |
Teng Liang | 85a3663 | 2018-03-21 05:59:34 -0700 | [diff] [blame] | 180 | */ |
| 181 | virtual void |
| 182 | afterContentStoreHit(const shared_ptr<pit::Entry>& pitEntry, |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 183 | const FaceEndpoint& ingress, const Data& data); |
Teng Liang | 85a3663 | 2018-03-21 05:59:34 -0700 | [diff] [blame] | 184 | |
Teng Liang | 43bb231 | 2018-03-26 04:16:42 -0700 | [diff] [blame] | 185 | /** \brief trigger after Data is received |
| 186 | * |
| 187 | * This trigger is invoked when an incoming Data satisfies exactly one PIT entry, |
| 188 | * and gives the strategy full control over Data forwarding. |
| 189 | * |
| 190 | * When this trigger is invoked: |
| 191 | * - The Data has been verified to satisfy the PIT entry. |
| 192 | * - The PIT entry expiry timer is set to now |
| 193 | * |
| 194 | * Within this function: |
| 195 | * - A strategy should return Data to downstream nodes via \c sendData or \c sendDataToAll. |
| 196 | * - A strategy can modify the Data as long as it still satisfies the PIT entry, such as |
| 197 | * adding or removing congestion marks. |
| 198 | * - A strategy can delay Data forwarding by prolonging the PIT entry lifetime via \c setExpiryTimer, |
| 199 | * and forward Data before the PIT entry is erased. |
| 200 | * - A strategy can collect measurements about the upstream. |
| 201 | * - A strategy can collect responses from additional upstream nodes by prolonging the PIT entry |
| 202 | * lifetime via \c setExpiryTimer every time a Data is received. Note that only one Data should |
| 203 | * be returned to each downstream node. |
| 204 | * |
| 205 | * In the base class this method invokes \c beforeSatisfyInterest trigger and then returns |
| 206 | * the Data to downstream faces via \c sendDataToAll. |
| 207 | */ |
| 208 | virtual void |
| 209 | afterReceiveData(const shared_ptr<pit::Entry>& pitEntry, |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 210 | const FaceEndpoint& ingress, const Data& data); |
Teng Liang | 43bb231 | 2018-03-26 04:16:42 -0700 | [diff] [blame] | 211 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 212 | /** \brief trigger after Nack is received |
| 213 | * |
| 214 | * This trigger is invoked when an incoming Nack is received in response to |
| 215 | * an forwarded Interest. |
| 216 | * The Nack has been confirmed to be a response to the last Interest forwarded |
| 217 | * to that upstream, i.e. the PIT out-record exists and has a matching Nonce. |
| 218 | * The NackHeader has been recorded in the PIT out-record. |
| 219 | * |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 220 | * If the PIT entry is not yet satisfied, its expiry timer remains unchanged. |
| 221 | * Otherwise, the PIT entry normally would expire immediately after this function returns. |
| 222 | * |
| 223 | * If the strategy wishes to collect responses from additional upstream nodes, |
| 224 | * it should invoke \c setExpiryTimer within this function to retain the PIT entry. |
| 225 | * If a Nack arrives from another upstream during the extended PIT entry lifetime, this trigger will be invoked again. |
| 226 | * At that time, this function must invoke \c setExpiryTimer again to continue collecting more responses. |
| 227 | * |
Teng Liang | 85a3663 | 2018-03-21 05:59:34 -0700 | [diff] [blame] | 228 | * In the base class this method does nothing. |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 229 | * |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 230 | * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior |
| 231 | * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>. |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 232 | */ |
| 233 | virtual void |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 234 | afterReceiveNack(const FaceEndpoint& ingress, const lp::Nack& nack, |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 235 | const shared_ptr<pit::Entry>& pitEntry); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 236 | |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 237 | /** \brief trigger after Interest dropped for exceeding allowed retransmissions |
| 238 | * |
| 239 | * In the base class this method does nothing. |
| 240 | */ |
| 241 | virtual void |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 242 | onDroppedInterest(const FaceEndpoint& egress, const Interest& interest); |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 243 | |
Ju Pan | 2feb459 | 2019-09-16 20:56:38 +0000 | [diff] [blame] | 244 | /** \brief trigger after new nexthop is added |
| 245 | * |
| 246 | * The strategy should decide whether to send the buffered Interests to the new nexthop. |
| 247 | * In the base class, this method does nothing. |
| 248 | */ |
| 249 | virtual void |
| 250 | afterNewNextHop(const fib::NextHop& nextHop, const shared_ptr<pit::Entry>& pitEntry); |
| 251 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 252 | protected: // actions |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 253 | /** \brief send Interest to egress |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 254 | * \param pitEntry PIT entry |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 255 | * \param egress face through which to send out the Interest and destination endpoint |
Junxiao Shi | c5f651f | 2016-11-17 22:58:12 +0000 | [diff] [blame] | 256 | * \param interest the Interest packet |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 257 | */ |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 258 | VIRTUAL_WITH_TESTS void |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 259 | sendInterest(const shared_ptr<pit::Entry>& pitEntry, |
Junxiao Shi | 606d5dd | 2019-09-23 12:47:44 -0600 | [diff] [blame] | 260 | const FaceEndpoint& egress, const Interest& interest); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 261 | |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 262 | /** \brief send \p data to \p egress |
Teng Liang | 85a3663 | 2018-03-21 05:59:34 -0700 | [diff] [blame] | 263 | * \param pitEntry PIT entry |
| 264 | * \param data the Data packet |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 265 | * \param egress face through which to send out the Data and destination endpoint |
Teng Liang | 85a3663 | 2018-03-21 05:59:34 -0700 | [diff] [blame] | 266 | */ |
| 267 | VIRTUAL_WITH_TESTS void |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 268 | sendData(const shared_ptr<pit::Entry>& pitEntry, const Data& data, const FaceEndpoint& egress); |
Teng Liang | 85a3663 | 2018-03-21 05:59:34 -0700 | [diff] [blame] | 269 | |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 270 | /** \brief send \p data to all matched and qualified face-endpoint pairs |
Teng Liang | 43bb231 | 2018-03-26 04:16:42 -0700 | [diff] [blame] | 271 | * |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 272 | * A matched face is qualified if it is ad-hoc or it is NOT \p ingress |
Teng Liang | 43bb231 | 2018-03-26 04:16:42 -0700 | [diff] [blame] | 273 | * |
| 274 | * \param pitEntry PIT entry |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 275 | * \param ingress face through which the Data comes from and endpoint of the sender |
Teng Liang | 43bb231 | 2018-03-26 04:16:42 -0700 | [diff] [blame] | 276 | * \param data the Data packet |
| 277 | */ |
| 278 | VIRTUAL_WITH_TESTS void |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 279 | sendDataToAll(const shared_ptr<pit::Entry>& pitEntry, |
| 280 | const FaceEndpoint& ingress, const Data& data); |
Teng Liang | 85a3663 | 2018-03-21 05:59:34 -0700 | [diff] [blame] | 281 | |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 282 | /** \brief schedule the PIT entry for immediate deletion |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 283 | * |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 284 | * This helper function sets the PIT entry expiry time to zero. |
| 285 | * The strategy should invoke this function when it concludes that the Interest cannot |
| 286 | * be forwarded and it does not want to wait for responses from existing upstream nodes. |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 287 | */ |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 288 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 289 | rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry) |
| 290 | { |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 291 | this->setExpiryTimer(pitEntry, 0_ms); |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 292 | } |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 293 | |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 294 | /** \brief send Nack to egress |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 295 | * \param pitEntry PIT entry |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 296 | * \param egress face through which to send out the Nack and destination endpoint |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 297 | * \param header Nack header |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 298 | * |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 299 | * The egress must have a PIT in-record, otherwise this method has no effect. |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 300 | */ |
| 301 | VIRTUAL_WITH_TESTS void |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 302 | sendNack(const shared_ptr<pit::Entry>& pitEntry, |
| 303 | const FaceEndpoint& egress, const lp::NackHeader& header) |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 304 | { |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 305 | m_forwarder.onOutgoingNack(pitEntry, egress, header); |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 306 | } |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 307 | |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 308 | /** \brief send Nack to every face-endpoint pair that has an in-record, except those in \p exceptFaceEndpoints |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 309 | * \param pitEntry PIT entry |
| 310 | * \param header NACK header |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 311 | * \param exceptFaceEndpoints list of face-endpoint pairs that should be excluded from sending Nacks |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 312 | * \note This is not an action, but a helper that invokes the sendNack action. |
| 313 | */ |
| 314 | void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 315 | sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header, |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 316 | std::initializer_list<FaceEndpoint> exceptFaceEndpoints = {}); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 317 | |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 318 | /** \brief Schedule the PIT entry to be erased after \p duration |
| 319 | */ |
| 320 | void |
| 321 | setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration) |
| 322 | { |
| 323 | m_forwarder.setExpiryTimer(pitEntry, duration); |
| 324 | } |
| 325 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 326 | protected: // accessors |
Junxiao Shi | cf0f3ce | 2016-09-02 13:01:59 +0000 | [diff] [blame] | 327 | /** \brief performs a FIB lookup, considering Link object if present |
| 328 | */ |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 329 | const fib::Entry& |
Junxiao Shi | cf0f3ce | 2016-09-02 13:01:59 +0000 | [diff] [blame] | 330 | lookupFib(const pit::Entry& pitEntry) const; |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 331 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 332 | MeasurementsAccessor& |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 333 | getMeasurements() |
| 334 | { |
| 335 | return m_measurements; |
| 336 | } |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 337 | |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 338 | Face* |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 339 | getFace(FaceId id) const |
| 340 | { |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 341 | return getFaceTable().get(id); |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 342 | } |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 343 | |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 344 | const FaceTable& |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 345 | getFaceTable() const |
| 346 | { |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 347 | return m_forwarder.m_faceTable; |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 348 | } |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 349 | |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 350 | protected: // instance name |
| 351 | struct ParsedInstanceName |
| 352 | { |
| 353 | Name strategyName; ///< strategy name without parameters |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 354 | optional<uint64_t> version; ///< whether strategyName contains a version component |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 355 | PartialName parameters; ///< parameter components |
| 356 | }; |
| 357 | |
| 358 | /** \brief parse a strategy instance name |
| 359 | * \param input strategy instance name, may contain version and parameters |
| 360 | * \throw std::invalid_argument input format is unacceptable |
| 361 | */ |
| 362 | static ParsedInstanceName |
| 363 | parseInstanceName(const Name& input); |
| 364 | |
| 365 | /** \brief construct a strategy instance name |
| 366 | * \param input strategy instance name, may contain version and parameters |
| 367 | * \param strategyName strategy name with version but without parameters; |
| 368 | * typically this should be \p getStrategyName() |
| 369 | * |
| 370 | * If \p input contains a version component, return \p input unchanged. |
| 371 | * Otherwise, return \p input plus the version component taken from \p strategyName. |
| 372 | * This allows a strategy instance to be constructed with an unversioned name, |
| 373 | * but its final instance name should contain the version. |
| 374 | */ |
| 375 | static Name |
| 376 | makeInstanceName(const Name& input, const Name& strategyName); |
| 377 | |
| 378 | /** \brief set strategy instance name |
| 379 | * \note This must be called by strategy subclass constructor. |
| 380 | */ |
| 381 | void |
| 382 | setInstanceName(const Name& name) |
| 383 | { |
| 384 | m_name = name; |
| 385 | } |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 386 | |
Ju Pan | 2feb459 | 2019-09-16 20:56:38 +0000 | [diff] [blame] | 387 | PUBLIC_WITH_TESTS_ELSE_PROTECTED: // setter |
| 388 | /** \brief set whether the afterNewNextHop trigger should be invoked for this strategy |
| 389 | */ |
| 390 | void |
| 391 | enableNewNextHopTrigger(bool enabled) |
| 392 | { |
| 393 | m_wantNewNextHopTrigger = enabled; |
| 394 | } |
| 395 | |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 396 | private: // registry |
| 397 | typedef std::function<unique_ptr<Strategy>(Forwarder& forwarder, const Name& strategyName)> CreateFunc; |
| 398 | typedef std::map<Name, CreateFunc> Registry; // indexed by strategy name |
| 399 | |
| 400 | static Registry& |
| 401 | getRegistry(); |
| 402 | |
| 403 | static Registry::const_iterator |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 404 | find(const Name& instanceName); |
| 405 | |
| 406 | protected: // accessors |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 407 | signal::Signal<FaceTable, Face>& afterAddFace; |
| 408 | signal::Signal<FaceTable, Face>& beforeRemoveFace; |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 409 | |
| 410 | private: // instance fields |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 411 | Name m_name; |
| 412 | |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 413 | /** \brief reference to the forwarder |
| 414 | * |
| 415 | * Triggers can access forwarder indirectly via actions. |
| 416 | */ |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 417 | Forwarder& m_forwarder; |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 418 | |
| 419 | MeasurementsAccessor m_measurements; |
Ju Pan | 2feb459 | 2019-09-16 20:56:38 +0000 | [diff] [blame] | 420 | |
| 421 | bool m_wantNewNextHopTrigger = false; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 422 | }; |
| 423 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 424 | } // namespace fw |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 425 | } // namespace nfd |
| 426 | |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 427 | /** \brief registers a strategy |
| 428 | * |
| 429 | * This macro should appear once in .cpp of each strategy. |
| 430 | */ |
| 431 | #define NFD_REGISTER_STRATEGY(S) \ |
| 432 | static class NfdAuto ## S ## StrategyRegistrationClass \ |
| 433 | { \ |
| 434 | public: \ |
| 435 | NfdAuto ## S ## StrategyRegistrationClass() \ |
| 436 | { \ |
| 437 | ::nfd::fw::Strategy::registerType<S>(); \ |
| 438 | } \ |
| 439 | } g_nfdAuto ## S ## StrategyRegistrationVariable |
| 440 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 441 | #endif // NFD_DAEMON_FW_STRATEGY_HPP |