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