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