Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 330136a | 2016-03-10 04:53:08 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Junxiao Shi | faf3eb0 | 2015-02-16 10:50:36 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 25 | |
| 26 | #include "forwarder.hpp" |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 27 | #include "algorithm.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 28 | #include "core/logger.hpp" |
Junxiao Shi | faf3eb0 | 2015-02-16 10:50:36 -0700 | [diff] [blame] | 29 | #include "strategy.hpp" |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 30 | #include "table/cleanup.hpp" |
Junxiao Shi | cbc8e94 | 2016-09-06 03:17:45 +0000 | [diff] [blame] | 31 | #include <ndn-cxx/lp/tags.hpp> |
Alexander Afanasyev | 9b9669d | 2015-01-08 21:41:48 -0800 | [diff] [blame^] | 32 | #include "face/null-face.hpp" |
| 33 | #include <boost/random/uniform_int_distribution.hpp> |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 35 | namespace nfd { |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 36 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 37 | NFD_LOG_INIT("Forwarder"); |
| 38 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 39 | Forwarder::Forwarder() |
Junxiao Shi | 9685cc5 | 2016-08-29 12:47:05 +0000 | [diff] [blame] | 40 | : m_unsolicitedDataPolicy(new fw::DefaultUnsolicitedDataPolicy()) |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 41 | , m_fib(m_nameTree) |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 42 | , m_pit(m_nameTree) |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 43 | , m_measurements(m_nameTree) |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 44 | , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this)) |
Alexander Afanasyev | 9b9669d | 2015-01-08 21:41:48 -0800 | [diff] [blame^] | 45 | , m_csFace(face::makeNullFace(FaceUri("contentstore://"))) |
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 | 9b9669d | 2015-01-08 21:41:48 -0800 | [diff] [blame^] | 48 | getFaceTable().addReserved(m_csFace, face::FACEID_CONTENT_STORE); |
Junxiao Shi | dcffdaa | 2016-07-26 02:23:56 +0000 | [diff] [blame] | 49 | |
| 50 | m_faceTable.afterAdd.connect([this] (Face& face) { |
| 51 | face.afterReceiveInterest.connect( |
| 52 | [this, &face] (const Interest& interest) { |
| 53 | this->startProcessInterest(face, interest); |
| 54 | }); |
| 55 | face.afterReceiveData.connect( |
| 56 | [this, &face] (const Data& data) { |
| 57 | this->startProcessData(face, data); |
| 58 | }); |
| 59 | face.afterReceiveNack.connect( |
| 60 | [this, &face] (const lp::Nack& nack) { |
| 61 | this->startProcessNack(face, nack); |
| 62 | }); |
| 63 | }); |
| 64 | |
| 65 | m_faceTable.beforeRemove.connect([this] (Face& face) { |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 66 | cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face); |
Junxiao Shi | dcffdaa | 2016-07-26 02:23:56 +0000 | [diff] [blame] | 67 | }); |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Junxiao Shi | dcffdaa | 2016-07-26 02:23:56 +0000 | [diff] [blame] | 70 | Forwarder::~Forwarder() = default; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 71 | |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 72 | void |
| 73 | Forwarder::startProcessInterest(Face& face, const Interest& interest) |
| 74 | { |
| 75 | // check fields used by forwarding are well-formed |
| 76 | try { |
| 77 | if (interest.hasLink()) { |
| 78 | interest.getLink(); |
| 79 | } |
| 80 | } |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 81 | catch (const tlv::Error&) { |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 82 | NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() << |
| 83 | " interest=" << interest.getName() << " malformed"); |
| 84 | // It's safe to call interest.getName() because Name has been fully parsed |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | this->onIncomingInterest(face, interest); |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | Forwarder::startProcessData(Face& face, const Data& data) |
| 93 | { |
| 94 | // check fields used by forwarding are well-formed |
| 95 | // (none needed) |
| 96 | |
| 97 | this->onIncomingData(face, data); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 98 | } |
| 99 | |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 100 | void |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 101 | Forwarder::startProcessNack(Face& face, const lp::Nack& nack) |
| 102 | { |
| 103 | // check fields used by forwarding are well-formed |
| 104 | try { |
| 105 | if (nack.getInterest().hasLink()) { |
| 106 | nack.getInterest().getLink(); |
| 107 | } |
| 108 | } |
| 109 | catch (const tlv::Error&) { |
| 110 | NFD_LOG_DEBUG("startProcessNack face=" << face.getId() << |
| 111 | " nack=" << nack.getInterest().getName() << |
| 112 | "~" << nack.getReason() << " malformed"); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | this->onIncomingNack(face, nack); |
| 117 | } |
| 118 | |
| 119 | void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 120 | Forwarder::onIncomingInterest(Face& inFace, const Interest& interest) |
| 121 | { |
| 122 | // receive Interest |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 123 | NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << |
| 124 | " interest=" << interest.getName()); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 125 | interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId())); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 126 | ++m_counters.nInInterests; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 127 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 128 | // /localhost scope control |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 129 | bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL && |
Junxiao Shi | f3410d8 | 2016-04-05 13:49:44 -0700 | [diff] [blame] | 130 | scope_prefix::LOCALHOST.isPrefixOf(interest.getName()); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 131 | if (isViolatingLocalhost) { |
| 132 | NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << |
| 133 | " interest=" << interest.getName() << " violates /localhost"); |
| 134 | // (drop) |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 135 | return; |
| 136 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 137 | |
Junxiao Shi | 330136a | 2016-03-10 04:53:08 -0700 | [diff] [blame] | 138 | // detect duplicate Nonce with Dead Nonce List |
| 139 | bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce()); |
| 140 | if (hasDuplicateNonceInDnl) { |
| 141 | // goto Interest loop pipeline |
| 142 | this->onInterestLoop(inFace, interest); |
| 143 | return; |
| 144 | } |
| 145 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 146 | // PIT insert |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 147 | shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 148 | |
Junxiao Shi | 330136a | 2016-03-10 04:53:08 -0700 | [diff] [blame] | 149 | // detect duplicate Nonce in PIT entry |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 150 | bool hasDuplicateNonceInPit = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace) != |
| 151 | fw::DUPLICATE_NONCE_NONE; |
Junxiao Shi | 330136a | 2016-03-10 04:53:08 -0700 | [diff] [blame] | 152 | if (hasDuplicateNonceInPit) { |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 153 | // goto Interest loop pipeline |
Junxiao Shi | 330136a | 2016-03-10 04:53:08 -0700 | [diff] [blame] | 154 | this->onInterestLoop(inFace, interest); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 155 | return; |
| 156 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 157 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 158 | // cancel unsatisfy & straggler timer |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 159 | this->cancelUnsatisfyAndStragglerTimer(*pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 160 | |
Spyridon Mastorakis | d2b262a | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 161 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
| 162 | bool isPending = inRecords.begin() != inRecords.end(); |
| 163 | if (!isPending) { |
| 164 | if (m_csFromNdnSim == nullptr) { |
| 165 | m_cs.find(interest, |
| 166 | bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2), |
| 167 | bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1)); |
| 168 | } |
| 169 | else { |
| 170 | shared_ptr<Data> match = m_csFromNdnSim->Lookup(interest.shared_from_this()); |
| 171 | if (match != nullptr) { |
| 172 | this->onContentStoreHit(inFace, pitEntry, interest, *match); |
| 173 | } |
| 174 | else { |
| 175 | this->onContentStoreMiss(inFace, pitEntry, interest); |
| 176 | } |
| 177 | } |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 178 | } |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 179 | else { |
| 180 | this->onContentStoreMiss(inFace, pitEntry, interest); |
| 181 | } |
| 182 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 183 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 184 | void |
Junxiao Shi | 330136a | 2016-03-10 04:53:08 -0700 | [diff] [blame] | 185 | Forwarder::onInterestLoop(Face& inFace, const Interest& interest) |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 186 | { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 187 | // if multi-access face, drop |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 188 | if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 189 | NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() << |
| 190 | " interest=" << interest.getName() << |
| 191 | " drop"); |
| 192 | return; |
| 193 | } |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 194 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 195 | NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() << |
| 196 | " interest=" << interest.getName() << |
| 197 | " send-Nack-duplicate"); |
| 198 | |
| 199 | // send Nack with reason=DUPLICATE |
| 200 | // note: Don't enter outgoing Nack pipeline because it needs an in-record. |
| 201 | lp::Nack nack(interest); |
| 202 | nack.setReason(lp::NackReason::DUPLICATE); |
| 203 | inFace.sendNack(nack); |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 207 | Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry, |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 208 | const Interest& interest) |
| 209 | { |
| 210 | NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName()); |
| 211 | |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 212 | // insert in-record |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 213 | pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 214 | |
Alexander Afanasyev | a57f8b4 | 2014-07-10 20:11:32 -0700 | [diff] [blame] | 215 | // set PIT unsatisfy timer |
| 216 | this->setUnsatisfyTimer(pitEntry); |
| 217 | |
Junxiao Shi | e342e8d | 2016-09-18 16:48:00 +0000 | [diff] [blame] | 218 | // has NextHopFaceId? |
| 219 | shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>(); |
| 220 | if (nextHopTag != nullptr) { |
| 221 | // chosen NextHop face exists? |
| 222 | Face* nextHopFace = m_faceTable.get(*nextHopTag); |
| 223 | if (nextHopFace != nullptr) { |
Junxiao Shi | c5f651f | 2016-11-17 22:58:12 +0000 | [diff] [blame] | 224 | NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName() << " nexthop-faceid=" << nextHopFace->getId()); |
Junxiao Shi | e342e8d | 2016-09-18 16:48:00 +0000 | [diff] [blame] | 225 | // go to outgoing Interest pipeline |
Junxiao Shi | c5f651f | 2016-11-17 22:58:12 +0000 | [diff] [blame] | 226 | // scope control is unnecessary, because privileged app explicitly wants to forward |
| 227 | this->onOutgoingInterest(pitEntry, *nextHopFace, interest); |
Junxiao Shi | e342e8d | 2016-09-18 16:48:00 +0000 | [diff] [blame] | 228 | } |
| 229 | return; |
| 230 | } |
| 231 | |
Junxiao Shi | 05cc50a | 2016-07-11 22:38:21 +0000 | [diff] [blame] | 232 | // dispatch to strategy: after incoming Interest |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 233 | this->dispatchToStrategy(*pitEntry, |
| 234 | [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); }); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 238 | Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry, |
| 239 | const Interest& interest, const Data& data) |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 240 | { |
Vince Lehman | faa5c0c | 2015-08-18 12:52:46 -0500 | [diff] [blame] | 241 | NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName()); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 242 | |
Alexander Afanasyev | 9b9669d | 2015-01-08 21:41:48 -0800 | [diff] [blame^] | 243 | beforeSatisfyInterest(*pitEntry, *m_csFace, data); |
| 244 | this->dispatchToStrategy(*pitEntry, |
| 245 | [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, *m_csFace, data); }); |
| 246 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 247 | data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE)); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 248 | // XXX should we lookup PIT for other Interests that also match csMatch? |
| 249 | |
| 250 | // set PIT straggler timer |
| 251 | this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod()); |
| 252 | |
| 253 | // goto outgoing Data pipeline |
| 254 | this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this())); |
| 255 | } |
| 256 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 257 | void |
Junxiao Shi | c5f651f | 2016-11-17 22:58:12 +0000 | [diff] [blame] | 258 | Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace, const Interest& interest) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 259 | { |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 260 | NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << |
| 261 | " interest=" << pitEntry->getName()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 262 | |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 263 | // insert out-record |
Junxiao Shi | c5f651f | 2016-11-17 22:58:12 +0000 | [diff] [blame] | 264 | pitEntry->insertOrUpdateOutRecord(outFace, interest); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 265 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 266 | // send Interest |
Junxiao Shi | c5f651f | 2016-11-17 22:58:12 +0000 | [diff] [blame] | 267 | outFace.sendInterest(interest); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 268 | ++m_counters.nOutInterests; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 272 | Forwarder::onInterestReject(const shared_ptr<pit::Entry>& pitEntry) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 273 | { |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 274 | if (fw::hasPendingOutRecords(*pitEntry)) { |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 275 | NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() << |
| 276 | " cannot reject forwarded Interest"); |
| 277 | return; |
| 278 | } |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 279 | NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 280 | |
Alexander Afanasyev | a57f8b4 | 2014-07-10 20:11:32 -0700 | [diff] [blame] | 281 | // cancel unsatisfy & straggler timer |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 282 | this->cancelUnsatisfyAndStragglerTimer(*pitEntry); |
Alexander Afanasyev | a57f8b4 | 2014-07-10 20:11:32 -0700 | [diff] [blame] | 283 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 284 | // set PIT straggler timer |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 285 | this->setStragglerTimer(pitEntry, false); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 289 | Forwarder::onInterestUnsatisfied(const shared_ptr<pit::Entry>& pitEntry) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 290 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 291 | NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName()); |
| 292 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 293 | // invoke PIT unsatisfied callback |
Alexander Afanasyev | 9b9669d | 2015-01-08 21:41:48 -0800 | [diff] [blame^] | 294 | beforeExpirePendingInterest(*pitEntry); |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 295 | this->dispatchToStrategy(*pitEntry, |
| 296 | [&] (fw::Strategy& strategy) { strategy.beforeExpirePendingInterest(pitEntry); }); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 297 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 298 | // goto Interest Finalize pipeline |
| 299 | this->onInterestFinalize(pitEntry, false); |
| 300 | } |
| 301 | |
| 302 | void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 303 | Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied, |
| 304 | time::milliseconds dataFreshnessPeriod) |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 305 | { |
| 306 | NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() << |
| 307 | (isSatisfied ? " satisfied" : " unsatisfied")); |
| 308 | |
| 309 | // Dead Nonce List insert if necessary |
| 310 | this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0); |
| 311 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 312 | // PIT delete |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 313 | this->cancelUnsatisfyAndStragglerTimer(*pitEntry); |
Junxiao Shi | dbef6dc | 2016-08-15 02:58:36 +0000 | [diff] [blame] | 314 | m_pit.erase(pitEntry.get()); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void |
| 318 | Forwarder::onIncomingData(Face& inFace, const Data& data) |
| 319 | { |
| 320 | // receive Data |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 321 | NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName()); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 322 | data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId())); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 323 | ++m_counters.nInData; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 324 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 325 | // /localhost scope control |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 326 | bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL && |
Junxiao Shi | f3410d8 | 2016-04-05 13:49:44 -0700 | [diff] [blame] | 327 | scope_prefix::LOCALHOST.isPrefixOf(data.getName()); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 328 | if (isViolatingLocalhost) { |
| 329 | NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << |
| 330 | " data=" << data.getName() << " violates /localhost"); |
| 331 | // (drop) |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 332 | return; |
| 333 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 334 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 335 | // PIT match |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 336 | pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data); |
| 337 | if (pitMatches.begin() == pitMatches.end()) { |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 338 | // goto Data unsolicited pipeline |
| 339 | this->onDataUnsolicited(inFace, data); |
| 340 | return; |
| 341 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 342 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 343 | // CS insert |
Spyridon Mastorakis | d2b262a | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 344 | if (m_csFromNdnSim == nullptr) |
| 345 | m_cs.insert(data); |
| 346 | else |
| 347 | m_csFromNdnSim->Add(data.shared_from_this()); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 348 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 349 | std::set<Face*> pendingDownstreams; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 350 | // foreach PitEntry |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 351 | auto now = time::steady_clock::now(); |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 352 | for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 353 | NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName()); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 354 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 355 | // cancel unsatisfy & straggler timer |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 356 | this->cancelUnsatisfyAndStragglerTimer(*pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 357 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 358 | // remember pending downstreams |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 359 | for (const pit::InRecord& inRecord : pitEntry->getInRecords()) { |
| 360 | if (inRecord.getExpiry() > now) { |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 361 | pendingDownstreams.insert(&inRecord.getFace()); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 364 | |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 365 | // invoke PIT satisfy callback |
Alexander Afanasyev | 9b9669d | 2015-01-08 21:41:48 -0800 | [diff] [blame^] | 366 | beforeSatisfyInterest(*pitEntry, inFace, data); |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 367 | this->dispatchToStrategy(*pitEntry, |
| 368 | [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); }); |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 369 | |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 370 | // Dead Nonce List insert if necessary (for out-record of inFace) |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 371 | this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace); |
| 372 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 373 | // mark PIT satisfied |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 374 | pitEntry->clearInRecords(); |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 375 | pitEntry->deleteOutRecord(inFace); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 376 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 377 | // set PIT straggler timer |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 378 | this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod()); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 379 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 380 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 381 | // foreach pending downstream |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 382 | for (Face* pendingDownstream : pendingDownstreams) { |
| 383 | if (pendingDownstream == &inFace) { |
Junxiao Shi | da006f5 | 2014-05-16 11:18:00 -0700 | [diff] [blame] | 384 | continue; |
| 385 | } |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 386 | // goto outgoing Data pipeline |
Junxiao Shi | da006f5 | 2014-05-16 11:18:00 -0700 | [diff] [blame] | 387 | this->onOutgoingData(data, *pendingDownstream); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
| 391 | void |
| 392 | Forwarder::onDataUnsolicited(Face& inFace, const Data& data) |
| 393 | { |
| 394 | // accept to cache? |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 395 | fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data); |
| 396 | if (decision == fw::UnsolicitedDataDecision::CACHE) { |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 397 | // CS insert |
Spyridon Mastorakis | d2b262a | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 398 | if (m_csFromNdnSim == nullptr) |
| 399 | m_cs.insert(data, true); |
| 400 | else |
| 401 | m_csFromNdnSim->Add(data.shared_from_this()); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 402 | } |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 403 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 404 | NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() << |
| 405 | " data=" << data.getName() << |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 406 | " decision=" << decision); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | void |
| 410 | Forwarder::onOutgoingData(const Data& data, Face& outFace) |
| 411 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 412 | if (outFace.getId() == face::INVALID_FACEID) { |
Junxiao Shi | 223271b | 2014-07-03 22:06:13 -0700 | [diff] [blame] | 413 | NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName()); |
| 414 | return; |
| 415 | } |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 416 | NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName()); |
| 417 | |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 418 | // /localhost scope control |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 419 | bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL && |
Junxiao Shi | f3410d8 | 2016-04-05 13:49:44 -0700 | [diff] [blame] | 420 | scope_prefix::LOCALHOST.isPrefixOf(data.getName()); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 421 | if (isViolatingLocalhost) { |
| 422 | NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << |
| 423 | " data=" << data.getName() << " violates /localhost"); |
| 424 | // (drop) |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 428 | // TODO traffic manager |
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 | // send Data |
| 431 | outFace.sendData(data); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 432 | ++m_counters.nOutData; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 435 | void |
| 436 | Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack) |
| 437 | { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 438 | // receive Nack |
| 439 | nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId())); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 440 | ++m_counters.nInNacks; |
| 441 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 442 | // if multi-access face, drop |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 443 | if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 444 | NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() << |
| 445 | " nack=" << nack.getInterest().getName() << |
| 446 | "~" << nack.getReason() << " face-is-multi-access"); |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | // PIT match |
| 451 | shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest()); |
| 452 | // if no PIT entry found, drop |
| 453 | if (pitEntry == nullptr) { |
| 454 | NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() << |
| 455 | " nack=" << nack.getInterest().getName() << |
| 456 | "~" << nack.getReason() << " no-PIT-entry"); |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | // has out-record? |
| 461 | pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace); |
| 462 | // if no out-record found, drop |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 463 | if (outRecord == pitEntry->out_end()) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 464 | NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() << |
| 465 | " nack=" << nack.getInterest().getName() << |
| 466 | "~" << nack.getReason() << " no-out-record"); |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | // if out-record has different Nonce, drop |
| 471 | if (nack.getInterest().getNonce() != outRecord->getLastNonce()) { |
| 472 | NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() << |
| 473 | " nack=" << nack.getInterest().getName() << |
| 474 | "~" << nack.getReason() << " wrong-Nonce " << |
| 475 | nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce()); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() << |
| 480 | " nack=" << nack.getInterest().getName() << |
| 481 | "~" << nack.getReason() << " OK"); |
| 482 | |
| 483 | // record Nack on out-record |
| 484 | outRecord->setIncomingNack(nack); |
| 485 | |
| 486 | // trigger strategy: after receive NACK |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 487 | this->dispatchToStrategy(*pitEntry, |
| 488 | [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); }); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 492 | Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace, |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 493 | const lp::NackHeader& nack) |
| 494 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 495 | if (outFace.getId() == face::INVALID_FACEID) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 496 | NFD_LOG_WARN("onOutgoingNack face=invalid" << |
| 497 | " nack=" << pitEntry->getInterest().getName() << |
| 498 | "~" << nack.getReason() << " no-in-record"); |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | // has in-record? |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 503 | pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 504 | |
| 505 | // if no in-record found, drop |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 506 | if (inRecord == pitEntry->in_end()) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 507 | NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() << |
| 508 | " nack=" << pitEntry->getInterest().getName() << |
| 509 | "~" << nack.getReason() << " no-in-record"); |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | // if multi-access face, drop |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 514 | if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 515 | NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() << |
| 516 | " nack=" << pitEntry->getInterest().getName() << |
| 517 | "~" << nack.getReason() << " face-is-multi-access"); |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() << |
| 522 | " nack=" << pitEntry->getInterest().getName() << |
| 523 | "~" << nack.getReason() << " OK"); |
| 524 | |
| 525 | // create Nack packet with the Interest from in-record |
| 526 | lp::Nack nackPkt(inRecord->getInterest()); |
| 527 | nackPkt.setHeader(nack); |
| 528 | |
| 529 | // erase in-record |
| 530 | pitEntry->deleteInRecord(outFace); |
| 531 | |
| 532 | // send Nack on face |
| 533 | const_cast<Face&>(outFace).sendNack(nackPkt); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 534 | ++m_counters.nOutNacks; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 537 | static inline bool |
| 538 | compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b) |
| 539 | { |
| 540 | return a.getExpiry() < b.getExpiry(); |
| 541 | } |
| 542 | |
| 543 | void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 544 | Forwarder::setUnsatisfyTimer(const shared_ptr<pit::Entry>& pitEntry) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 545 | { |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 546 | pit::InRecordCollection::iterator lastExpiring = |
| 547 | std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 548 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 549 | time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry(); |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 550 | time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now(); |
| 551 | if (lastExpiryFromNow <= time::seconds::zero()) { |
| 552 | // TODO all in-records are already expired; will this happen? |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 553 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 554 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 555 | scheduler::cancel(pitEntry->m_unsatisfyTimer); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 556 | pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow, |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 557 | bind(&Forwarder::onInterestUnsatisfied, this, pitEntry)); |
| 558 | } |
| 559 | |
| 560 | void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 561 | Forwarder::setStragglerTimer(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied, |
| 562 | time::milliseconds dataFreshnessPeriod) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 563 | { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 564 | time::nanoseconds stragglerTime = time::milliseconds(100); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 565 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 566 | scheduler::cancel(pitEntry->m_stragglerTimer); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 567 | pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime, |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 568 | bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod)); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 572 | Forwarder::cancelUnsatisfyAndStragglerTimer(pit::Entry& pitEntry) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 573 | { |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 574 | scheduler::cancel(pitEntry.m_unsatisfyTimer); |
| 575 | scheduler::cancel(pitEntry.m_stragglerTimer); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 578 | static inline void |
| 579 | insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry, |
| 580 | const pit::OutRecord& outRecord) |
| 581 | { |
| 582 | dnl.add(pitEntry.getName(), outRecord.getLastNonce()); |
| 583 | } |
| 584 | |
| 585 | void |
| 586 | Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied, |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 587 | time::milliseconds dataFreshnessPeriod, Face* upstream) |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 588 | { |
| 589 | // need Dead Nonce List insert? |
| 590 | bool needDnl = false; |
| 591 | if (isSatisfied) { |
| 592 | bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero(); |
| 593 | // Data never becomes stale if it doesn't have FreshnessPeriod field |
| 594 | needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) && |
| 595 | (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime()); |
| 596 | } |
| 597 | else { |
| 598 | needDnl = true; |
| 599 | } |
| 600 | |
| 601 | if (!needDnl) { |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | // Dead Nonce List insert |
| 606 | if (upstream == 0) { |
| 607 | // insert all outgoing Nonces |
| 608 | const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords(); |
| 609 | std::for_each(outRecords.begin(), outRecords.end(), |
| 610 | bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1)); |
| 611 | } |
| 612 | else { |
| 613 | // insert outgoing Nonce of a specific face |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 614 | pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream); |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 615 | if (outRecord != pitEntry.getOutRecords().end()) { |
| 616 | m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce()); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 621 | } // namespace nfd |