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