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