Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 330136a | 2016-03-10 04:53:08 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 25 | |
| 26 | #include "forwarder.hpp" |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 27 | #include "pit-algorithm.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 28 | #include "core/logger.hpp" |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 29 | #include "core/random.hpp" |
Junxiao Shi | faf3eb0 | 2015-02-16 10:50:36 -0700 | [diff] [blame] | 30 | #include "strategy.hpp" |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 31 | #include <boost/random/uniform_int_distribution.hpp> |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 33 | namespace nfd { |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 34 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 35 | NFD_LOG_INIT("Forwarder"); |
| 36 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 37 | using fw::Strategy; |
| 38 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 39 | Forwarder::Forwarder() |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 40 | : m_faceTable(*this) |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 41 | , m_fib(m_nameTree) |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 42 | , m_pit(m_nameTree) |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 43 | , m_measurements(m_nameTree) |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 44 | , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this)) |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 45 | { |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 46 | fw::installStrategies(*this); |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 49 | Forwarder::~Forwarder() |
| 50 | { |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 51 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 52 | |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 53 | void |
| 54 | Forwarder::startProcessInterest(Face& face, const Interest& interest) |
| 55 | { |
| 56 | // check fields used by forwarding are well-formed |
| 57 | try { |
| 58 | if (interest.hasLink()) { |
| 59 | interest.getLink(); |
| 60 | } |
| 61 | } |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 62 | catch (const tlv::Error&) { |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 63 | NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() << |
| 64 | " interest=" << interest.getName() << " malformed"); |
| 65 | // It's safe to call interest.getName() because Name has been fully parsed |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | this->onIncomingInterest(face, interest); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | Forwarder::startProcessData(Face& face, const Data& data) |
| 74 | { |
| 75 | // check fields used by forwarding are well-formed |
| 76 | // (none needed) |
| 77 | |
| 78 | this->onIncomingData(face, data); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 79 | } |
| 80 | |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 81 | void |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 82 | Forwarder::startProcessNack(Face& face, const lp::Nack& nack) |
| 83 | { |
| 84 | // check fields used by forwarding are well-formed |
| 85 | try { |
| 86 | if (nack.getInterest().hasLink()) { |
| 87 | nack.getInterest().getLink(); |
| 88 | } |
| 89 | } |
| 90 | catch (const tlv::Error&) { |
| 91 | NFD_LOG_DEBUG("startProcessNack face=" << face.getId() << |
| 92 | " nack=" << nack.getInterest().getName() << |
| 93 | "~" << nack.getReason() << " malformed"); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | this->onIncomingNack(face, nack); |
| 98 | } |
| 99 | |
| 100 | void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 101 | Forwarder::onIncomingInterest(Face& inFace, const Interest& interest) |
| 102 | { |
| 103 | // receive Interest |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 104 | NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << |
| 105 | " interest=" << interest.getName()); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 106 | interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId())); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 107 | ++m_counters.nInInterests; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 108 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 109 | // /localhost scope control |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 110 | bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL && |
Junxiao Shi | f3410d8 | 2016-04-05 13:49:44 -0700 | [diff] [blame] | 111 | scope_prefix::LOCALHOST.isPrefixOf(interest.getName()); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 112 | if (isViolatingLocalhost) { |
| 113 | NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << |
| 114 | " interest=" << interest.getName() << " violates /localhost"); |
| 115 | // (drop) |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 116 | return; |
| 117 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 118 | |
Junxiao Shi | 330136a | 2016-03-10 04:53:08 -0700 | [diff] [blame] | 119 | // detect duplicate Nonce with Dead Nonce List |
| 120 | bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce()); |
| 121 | if (hasDuplicateNonceInDnl) { |
| 122 | // goto Interest loop pipeline |
| 123 | this->onInterestLoop(inFace, interest); |
| 124 | return; |
| 125 | } |
| 126 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 127 | // PIT insert |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 128 | shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 129 | |
Junxiao Shi | 330136a | 2016-03-10 04:53:08 -0700 | [diff] [blame] | 130 | // detect duplicate Nonce in PIT entry |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 131 | bool hasDuplicateNonceInPit = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace) != |
| 132 | fw::DUPLICATE_NONCE_NONE; |
Junxiao Shi | 330136a | 2016-03-10 04:53:08 -0700 | [diff] [blame] | 133 | if (hasDuplicateNonceInPit) { |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 134 | // goto Interest loop pipeline |
Junxiao Shi | 330136a | 2016-03-10 04:53:08 -0700 | [diff] [blame] | 135 | this->onInterestLoop(inFace, interest); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 136 | return; |
| 137 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 138 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 139 | // cancel unsatisfy & straggler timer |
| 140 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 141 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 142 | // is pending? |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 143 | if (!pitEntry->hasInRecords()) { |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 144 | m_cs.find(interest, |
| 145 | bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2), |
| 146 | bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1)); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 147 | } |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 148 | else { |
| 149 | this->onContentStoreMiss(inFace, pitEntry, interest); |
| 150 | } |
| 151 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 152 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 153 | void |
Junxiao Shi | 330136a | 2016-03-10 04:53:08 -0700 | [diff] [blame] | 154 | Forwarder::onInterestLoop(Face& inFace, const Interest& interest) |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 155 | { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 156 | // if multi-access face, drop |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 157 | if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 158 | NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() << |
| 159 | " interest=" << interest.getName() << |
| 160 | " drop"); |
| 161 | return; |
| 162 | } |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 163 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 164 | NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() << |
| 165 | " interest=" << interest.getName() << |
| 166 | " send-Nack-duplicate"); |
| 167 | |
| 168 | // send Nack with reason=DUPLICATE |
| 169 | // note: Don't enter outgoing Nack pipeline because it needs an in-record. |
| 170 | lp::Nack nack(interest); |
| 171 | nack.setReason(lp::NackReason::DUPLICATE); |
| 172 | inFace.sendNack(nack); |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 176 | Forwarder::onContentStoreMiss(const Face& inFace, |
| 177 | shared_ptr<pit::Entry> pitEntry, |
| 178 | const Interest& interest) |
| 179 | { |
| 180 | NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName()); |
| 181 | |
| 182 | shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this()); |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 183 | // insert in-record |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 184 | pitEntry->insertOrUpdateInRecord(face, interest); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 185 | |
Alexander Afanasyev | a57f8b4 | 2014-07-10 20:11:32 -0700 | [diff] [blame] | 186 | // set PIT unsatisfy timer |
| 187 | this->setUnsatisfyTimer(pitEntry); |
| 188 | |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 189 | shared_ptr<fib::Entry> fibEntry; |
| 190 | // has Link object? |
| 191 | if (!interest.hasLink()) { |
| 192 | // FIB lookup with Interest name |
| 193 | fibEntry = m_fib.findLongestPrefixMatch(*pitEntry); |
| 194 | NFD_LOG_TRACE("onContentStoreMiss noLinkObject"); |
| 195 | } |
| 196 | else { |
| 197 | const Link& link = interest.getLink(); |
| 198 | |
| 199 | // in producer region? |
| 200 | if (m_networkRegionTable.isInProducerRegion(link)) { |
| 201 | // FIB lookup with Interest name |
| 202 | fibEntry = m_fib.findLongestPrefixMatch(*pitEntry); |
| 203 | NFD_LOG_TRACE("onContentStoreMiss inProducerRegion"); |
| 204 | } |
| 205 | // has SelectedDelegation? |
| 206 | else if (interest.hasSelectedDelegation()) { |
| 207 | // FIB lookup with SelectedDelegation |
| 208 | fibEntry = m_fib.findLongestPrefixMatch(interest.getSelectedDelegation()); |
| 209 | NFD_LOG_TRACE("onContentStoreMiss hasSelectedDelegation=" << interest.getSelectedDelegation()); |
| 210 | } |
| 211 | else { |
| 212 | // FIB lookup with first delegation Name |
| 213 | fibEntry = m_fib.findLongestPrefixMatch(link.getDelegations().begin()->second); |
| 214 | |
| 215 | // in default-free zone? |
| 216 | bool isDefaultFreeZone = !(fibEntry->getPrefix().size() == 0 && fibEntry->hasNextHops()); |
| 217 | if (isDefaultFreeZone) { |
| 218 | // choose and set SelectedDelegation |
| 219 | for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) { |
| 220 | const Name& delegationName = delegation.second; |
| 221 | fibEntry = m_fib.findLongestPrefixMatch(delegationName); |
| 222 | if (fibEntry->hasNextHops()) { |
| 223 | const_cast<Interest&>(interest).setSelectedDelegation(delegationName); |
| 224 | NFD_LOG_TRACE("onContentStoreMiss enterDefaultFreeZone" |
| 225 | << " setSelectedDelegation=" << delegationName); |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | else { |
| 231 | NFD_LOG_TRACE("onContentStoreMiss inConsumerRegion"); |
| 232 | } |
| 233 | } |
| 234 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 235 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 236 | // dispatch to strategy |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 237 | BOOST_ASSERT(fibEntry != nullptr); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 238 | this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1, |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 239 | cref(inFace), cref(interest), fibEntry, pitEntry)); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 243 | Forwarder::onContentStoreHit(const Face& inFace, |
| 244 | shared_ptr<pit::Entry> pitEntry, |
| 245 | const Interest& interest, |
| 246 | const Data& data) |
| 247 | { |
Vince Lehman | faa5c0c | 2015-08-18 12:52:46 -0500 | [diff] [blame] | 248 | NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName()); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 249 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 250 | data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE)); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 251 | // XXX should we lookup PIT for other Interests that also match csMatch? |
| 252 | |
| 253 | // set PIT straggler timer |
| 254 | this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod()); |
| 255 | |
| 256 | // goto outgoing Data pipeline |
| 257 | this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this())); |
| 258 | } |
| 259 | |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 260 | /** \brief compare two in-records for picking outgoing Interest |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 261 | * \return true if b is preferred over a |
| 262 | * |
| 263 | * This function should be passed to std::max_element over InRecordCollection. |
| 264 | * The outgoing Interest picked is the last incoming Interest |
| 265 | * that does not come from outFace. |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 266 | * If all in-records come from outFace, it's fine to pick that. This happens when |
| 267 | * there's only one in-record that comes from outFace. The legit use is for |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 268 | * vehicular network; otherwise, strategy shouldn't send to the sole inFace. |
| 269 | */ |
| 270 | static inline bool |
| 271 | compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace) |
| 272 | { |
| 273 | bool isOutFaceA = a.getFace().get() == outFace; |
| 274 | bool isOutFaceB = b.getFace().get() == outFace; |
| 275 | |
| 276 | if (!isOutFaceA && isOutFaceB) { |
| 277 | return false; |
| 278 | } |
| 279 | if (isOutFaceA && !isOutFaceB) { |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | return a.getLastRenewed() > b.getLastRenewed(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 287 | Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace, |
| 288 | bool wantNewNonce) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 289 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 290 | if (outFace.getId() == face::INVALID_FACEID) { |
Junxiao Shi | 223271b | 2014-07-03 22:06:13 -0700 | [diff] [blame] | 291 | NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName()); |
| 292 | return; |
| 293 | } |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 294 | NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << |
| 295 | " interest=" << pitEntry->getName()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 296 | |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 297 | // scope control |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 298 | if (fw::violatesScope(*pitEntry, outFace)) { |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 299 | NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 300 | " interest=" << pitEntry->getName() << " violates scope"); |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 301 | return; |
| 302 | } |
| 303 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 304 | // pick Interest |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 305 | pit::InRecordCollection::iterator pickedInRecord = std::max_element( |
| 306 | pitEntry->in_begin(), pitEntry->in_end(), bind(&compare_pickInterest, _1, _2, &outFace)); |
| 307 | BOOST_ASSERT(pickedInRecord != pitEntry->in_end()); |
| 308 | auto interest = const_pointer_cast<Interest>(pickedInRecord->getInterest().shared_from_this()); |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 309 | |
| 310 | if (wantNewNonce) { |
| 311 | interest = make_shared<Interest>(*interest); |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 312 | static boost::random::uniform_int_distribution<uint32_t> dist; |
| 313 | interest->setNonce(dist(getGlobalRng())); |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 314 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 315 | |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 316 | // insert out-record |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 317 | pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 318 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 319 | // send Interest |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 320 | outFace.sendInterest(*interest); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 321 | ++m_counters.nOutInterests; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 325 | Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 326 | { |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 327 | if (fw::hasPendingOutRecords(*pitEntry)) { |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 328 | NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() << |
| 329 | " cannot reject forwarded Interest"); |
| 330 | return; |
| 331 | } |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 332 | NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 333 | |
Alexander Afanasyev | a57f8b4 | 2014-07-10 20:11:32 -0700 | [diff] [blame] | 334 | // cancel unsatisfy & straggler timer |
| 335 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
| 336 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 337 | // set PIT straggler timer |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 338 | this->setStragglerTimer(pitEntry, false); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void |
| 342 | Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry) |
| 343 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 344 | NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName()); |
| 345 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 346 | // invoke PIT unsatisfied callback |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 347 | this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1, |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 348 | pitEntry)); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 349 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 350 | // goto Interest Finalize pipeline |
| 351 | this->onInterestFinalize(pitEntry, false); |
| 352 | } |
| 353 | |
| 354 | void |
| 355 | Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied, |
| 356 | const time::milliseconds& dataFreshnessPeriod) |
| 357 | { |
| 358 | NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() << |
| 359 | (isSatisfied ? " satisfied" : " unsatisfied")); |
| 360 | |
| 361 | // Dead Nonce List insert if necessary |
| 362 | this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0); |
| 363 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 364 | // PIT delete |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 365 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 366 | m_pit.erase(pitEntry); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void |
| 370 | Forwarder::onIncomingData(Face& inFace, const Data& data) |
| 371 | { |
| 372 | // receive Data |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 373 | NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName()); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 374 | data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId())); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 375 | ++m_counters.nInData; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 376 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 377 | // /localhost scope control |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 378 | bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL && |
Junxiao Shi | f3410d8 | 2016-04-05 13:49:44 -0700 | [diff] [blame] | 379 | scope_prefix::LOCALHOST.isPrefixOf(data.getName()); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 380 | if (isViolatingLocalhost) { |
| 381 | NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << |
| 382 | " data=" << data.getName() << " violates /localhost"); |
| 383 | // (drop) |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 384 | return; |
| 385 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 386 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 387 | // PIT match |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 388 | pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data); |
| 389 | if (pitMatches.begin() == pitMatches.end()) { |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 390 | // goto Data unsolicited pipeline |
| 391 | this->onDataUnsolicited(inFace, data); |
| 392 | return; |
| 393 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 394 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 395 | // CS insert |
| 396 | m_cs.insert(data); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 397 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 398 | std::set<Face*> pendingDownstreams; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 399 | // foreach PitEntry |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 400 | auto now = time::steady_clock::now(); |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 401 | for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 402 | NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName()); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 403 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 404 | // cancel unsatisfy & straggler timer |
| 405 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 406 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 407 | // remember pending downstreams |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 408 | for (const pit::InRecord& inRecord : pitEntry->getInRecords()) { |
| 409 | if (inRecord.getExpiry() > now) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 410 | pendingDownstreams.insert(inRecord.getFace().get()); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 411 | } |
| 412 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 413 | |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 414 | // invoke PIT satisfy callback |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 415 | this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1, |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 416 | pitEntry, cref(inFace), cref(data))); |
| 417 | |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 418 | // Dead Nonce List insert if necessary (for out-record of inFace) |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 419 | this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace); |
| 420 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 421 | // mark PIT satisfied |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 422 | pitEntry->clearInRecords(); |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 423 | pitEntry->deleteOutRecord(inFace); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 424 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 425 | // set PIT straggler timer |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 426 | this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod()); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 427 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 428 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 429 | // foreach pending downstream |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 430 | for (Face* pendingDownstream : pendingDownstreams) { |
| 431 | if (pendingDownstream == &inFace) { |
Junxiao Shi | da006f5 | 2014-05-16 11:18:00 -0700 | [diff] [blame] | 432 | continue; |
| 433 | } |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 434 | // goto outgoing Data pipeline |
Junxiao Shi | da006f5 | 2014-05-16 11:18:00 -0700 | [diff] [blame] | 435 | this->onOutgoingData(data, *pendingDownstream); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | void |
| 440 | Forwarder::onDataUnsolicited(Face& inFace, const Data& data) |
| 441 | { |
| 442 | // accept to cache? |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 443 | bool acceptToCache = inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 444 | if (acceptToCache) { |
| 445 | // CS insert |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 446 | m_cs.insert(data, true); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 447 | } |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 448 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 449 | NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() << |
| 450 | " data=" << data.getName() << |
| 451 | (acceptToCache ? " cached" : " not cached")); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void |
| 455 | Forwarder::onOutgoingData(const Data& data, Face& outFace) |
| 456 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 457 | if (outFace.getId() == face::INVALID_FACEID) { |
Junxiao Shi | 223271b | 2014-07-03 22:06:13 -0700 | [diff] [blame] | 458 | NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName()); |
| 459 | return; |
| 460 | } |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 461 | NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName()); |
| 462 | |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 463 | // /localhost scope control |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 464 | bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL && |
Junxiao Shi | f3410d8 | 2016-04-05 13:49:44 -0700 | [diff] [blame] | 465 | scope_prefix::LOCALHOST.isPrefixOf(data.getName()); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 466 | if (isViolatingLocalhost) { |
| 467 | NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << |
| 468 | " data=" << data.getName() << " violates /localhost"); |
| 469 | // (drop) |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 470 | return; |
| 471 | } |
| 472 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 473 | // TODO traffic manager |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 474 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 475 | // send Data |
| 476 | outFace.sendData(data); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 477 | ++m_counters.nOutData; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 480 | void |
| 481 | Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack) |
| 482 | { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 483 | // receive Nack |
| 484 | nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId())); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 485 | ++m_counters.nInNacks; |
| 486 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 487 | // if multi-access face, drop |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 488 | if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 489 | NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() << |
| 490 | " nack=" << nack.getInterest().getName() << |
| 491 | "~" << nack.getReason() << " face-is-multi-access"); |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | // PIT match |
| 496 | shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest()); |
| 497 | // if no PIT entry found, drop |
| 498 | if (pitEntry == nullptr) { |
| 499 | NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() << |
| 500 | " nack=" << nack.getInterest().getName() << |
| 501 | "~" << nack.getReason() << " no-PIT-entry"); |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | // has out-record? |
| 506 | pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace); |
| 507 | // if no out-record found, drop |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 508 | if (outRecord == pitEntry->out_end()) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 509 | NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() << |
| 510 | " nack=" << nack.getInterest().getName() << |
| 511 | "~" << nack.getReason() << " no-out-record"); |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | // if out-record has different Nonce, drop |
| 516 | if (nack.getInterest().getNonce() != outRecord->getLastNonce()) { |
| 517 | NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() << |
| 518 | " nack=" << nack.getInterest().getName() << |
| 519 | "~" << nack.getReason() << " wrong-Nonce " << |
| 520 | nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce()); |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() << |
| 525 | " nack=" << nack.getInterest().getName() << |
| 526 | "~" << nack.getReason() << " OK"); |
| 527 | |
| 528 | // record Nack on out-record |
| 529 | outRecord->setIncomingNack(nack); |
| 530 | |
| 531 | // trigger strategy: after receive NACK |
| 532 | shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry); |
| 533 | this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveNack, _1, |
| 534 | cref(inFace), cref(nack), fibEntry, pitEntry)); |
| 535 | } |
| 536 | |
| 537 | void |
| 538 | Forwarder::onOutgoingNack(shared_ptr<pit::Entry> pitEntry, const Face& outFace, |
| 539 | const lp::NackHeader& nack) |
| 540 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 541 | if (outFace.getId() == face::INVALID_FACEID) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 542 | NFD_LOG_WARN("onOutgoingNack face=invalid" << |
| 543 | " nack=" << pitEntry->getInterest().getName() << |
| 544 | "~" << nack.getReason() << " no-in-record"); |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | // has in-record? |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 549 | pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 550 | |
| 551 | // if no in-record found, drop |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 552 | if (inRecord == pitEntry->in_end()) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 553 | NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() << |
| 554 | " nack=" << pitEntry->getInterest().getName() << |
| 555 | "~" << nack.getReason() << " no-in-record"); |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | // if multi-access face, drop |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 560 | if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 561 | NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() << |
| 562 | " nack=" << pitEntry->getInterest().getName() << |
| 563 | "~" << nack.getReason() << " face-is-multi-access"); |
| 564 | return; |
| 565 | } |
| 566 | |
| 567 | NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() << |
| 568 | " nack=" << pitEntry->getInterest().getName() << |
| 569 | "~" << nack.getReason() << " OK"); |
| 570 | |
| 571 | // create Nack packet with the Interest from in-record |
| 572 | lp::Nack nackPkt(inRecord->getInterest()); |
| 573 | nackPkt.setHeader(nack); |
| 574 | |
| 575 | // erase in-record |
| 576 | pitEntry->deleteInRecord(outFace); |
| 577 | |
| 578 | // send Nack on face |
| 579 | const_cast<Face&>(outFace).sendNack(nackPkt); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 580 | ++m_counters.nOutNacks; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 583 | static inline bool |
| 584 | compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b) |
| 585 | { |
| 586 | return a.getExpiry() < b.getExpiry(); |
| 587 | } |
| 588 | |
| 589 | void |
| 590 | Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry) |
| 591 | { |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 592 | pit::InRecordCollection::iterator lastExpiring = |
| 593 | std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 594 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 595 | time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry(); |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 596 | time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now(); |
| 597 | if (lastExpiryFromNow <= time::seconds::zero()) { |
| 598 | // TODO all in-records are already expired; will this happen? |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 599 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 600 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 601 | scheduler::cancel(pitEntry->m_unsatisfyTimer); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 602 | pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow, |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 603 | bind(&Forwarder::onInterestUnsatisfied, this, pitEntry)); |
| 604 | } |
| 605 | |
| 606 | void |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 607 | Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied, |
| 608 | const time::milliseconds& dataFreshnessPeriod) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 609 | { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 610 | time::nanoseconds stragglerTime = time::milliseconds(100); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 611 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 612 | scheduler::cancel(pitEntry->m_stragglerTimer); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 613 | pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime, |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 614 | bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod)); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | void |
| 618 | Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry) |
| 619 | { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 620 | scheduler::cancel(pitEntry->m_unsatisfyTimer); |
| 621 | scheduler::cancel(pitEntry->m_stragglerTimer); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 624 | static inline void |
| 625 | insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry, |
| 626 | const pit::OutRecord& outRecord) |
| 627 | { |
| 628 | dnl.add(pitEntry.getName(), outRecord.getLastNonce()); |
| 629 | } |
| 630 | |
| 631 | void |
| 632 | Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied, |
| 633 | const time::milliseconds& dataFreshnessPeriod, |
| 634 | Face* upstream) |
| 635 | { |
| 636 | // need Dead Nonce List insert? |
| 637 | bool needDnl = false; |
| 638 | if (isSatisfied) { |
| 639 | bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero(); |
| 640 | // Data never becomes stale if it doesn't have FreshnessPeriod field |
| 641 | needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) && |
| 642 | (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime()); |
| 643 | } |
| 644 | else { |
| 645 | needDnl = true; |
| 646 | } |
| 647 | |
| 648 | if (!needDnl) { |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | // Dead Nonce List insert |
| 653 | if (upstream == 0) { |
| 654 | // insert all outgoing Nonces |
| 655 | const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords(); |
| 656 | std::for_each(outRecords.begin(), outRecords.end(), |
| 657 | bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1)); |
| 658 | } |
| 659 | else { |
| 660 | // insert outgoing Nonce of a specific face |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 661 | pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream); |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 662 | if (outRecord != pitEntry.getOutRecords().end()) { |
| 663 | m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce()); |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 668 | } // namespace nfd |