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