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