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