blob: 1e01e024ca7c2a229a79d1a6ea9c858ab073dd7e [file] [log] [blame]
Alexander Afanasyev33b72772014-01-26 23:22:58 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shif9858fd2017-02-01 16:22:44 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Junxiao Shifaf3eb02015-02-16 10:50:36 -07004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Shiaf6569a2014-06-14 00:01:34 -070024 */
Alexander Afanasyev33b72772014-01-26 23:22:58 -080025
26#include "forwarder.hpp"
Junxiao Shi00dc9142016-11-21 14:23:12 +000027#include "algorithm.hpp"
Junxiao Shic34d1672016-12-09 15:57:59 +000028#include "best-route-strategy2.hpp"
Junxiao Shifaf3eb02015-02-16 10:50:36 -070029#include "strategy.hpp"
Junxiao Shic34d1672016-12-09 15:57:59 +000030#include "core/logger.hpp"
Junxiao Shi02b73f52016-07-28 01:48:27 +000031#include "table/cleanup.hpp"
Junxiao Shicbc8e942016-09-06 03:17:45 +000032#include <ndn-cxx/lp/tags.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080033
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080034namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080035
Junxiao Shi8c8d2182014-01-30 22:33:00 -070036NFD_LOG_INIT("Forwarder");
37
Junxiao Shic34d1672016-12-09 15:57:59 +000038static Name
39getDefaultStrategyName()
40{
Junxiao Shi037f4ab2016-12-13 04:27:06 +000041 return fw::BestRouteStrategy2::getStrategyName();
Junxiao Shic34d1672016-12-09 15:57:59 +000042}
43
Junxiao Shic041ca32014-02-25 20:01:15 -070044Forwarder::Forwarder()
Junxiao Shi9685cc52016-08-29 12:47:05 +000045 : m_unsolicitedDataPolicy(new fw::DefaultUnsolicitedDataPolicy())
Junxiao Shifbe8efe2016-08-22 16:02:30 +000046 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060047 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080048 , m_measurements(m_nameTree)
Junxiao Shic34d1672016-12-09 15:57:59 +000049 , m_strategyChoice(*this)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080050{
Junxiao Shidcffdaa2016-07-26 02:23:56 +000051 m_faceTable.afterAdd.connect([this] (Face& face) {
52 face.afterReceiveInterest.connect(
53 [this, &face] (const Interest& interest) {
54 this->startProcessInterest(face, interest);
55 });
56 face.afterReceiveData.connect(
57 [this, &face] (const Data& data) {
58 this->startProcessData(face, data);
59 });
60 face.afterReceiveNack.connect(
61 [this, &face] (const lp::Nack& nack) {
62 this->startProcessNack(face, nack);
63 });
64 });
65
66 m_faceTable.beforeRemove.connect([this] (Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000067 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000068 });
Junxiao Shic34d1672016-12-09 15:57:59 +000069
70 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080071}
72
Junxiao Shidcffdaa2016-07-26 02:23:56 +000073Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060074
Junxiao Shi0355e9f2015-09-02 07:24:53 -070075void
76Forwarder::startProcessInterest(Face& face, const Interest& interest)
77{
78 // check fields used by forwarding are well-formed
79 try {
80 if (interest.hasLink()) {
81 interest.getLink();
82 }
83 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -070084 catch (const tlv::Error&) {
Junxiao Shi0355e9f2015-09-02 07:24:53 -070085 NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() <<
86 " interest=" << interest.getName() << " malformed");
87 // It's safe to call interest.getName() because Name has been fully parsed
88 return;
89 }
90
91 this->onIncomingInterest(face, interest);
92}
93
94void
95Forwarder::startProcessData(Face& face, const Data& data)
96{
97 // check fields used by forwarding are well-formed
98 // (none needed)
99
100 this->onIncomingData(face, data);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600101}
102
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800103void
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700104Forwarder::startProcessNack(Face& face, const lp::Nack& nack)
105{
106 // check fields used by forwarding are well-formed
107 try {
108 if (nack.getInterest().hasLink()) {
109 nack.getInterest().getLink();
110 }
111 }
112 catch (const tlv::Error&) {
113 NFD_LOG_DEBUG("startProcessNack face=" << face.getId() <<
114 " nack=" << nack.getInterest().getName() <<
115 "~" << nack.getReason() << " malformed");
116 return;
117 }
118
119 this->onIncomingNack(face, nack);
120}
121
122void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700123Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
124{
125 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700126 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
127 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000128 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700129 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -0700130
Junxiao Shi88884492014-02-15 15:57:43 -0700131 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700132 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700133 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700134 if (isViolatingLocalhost) {
135 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
136 " interest=" << interest.getName() << " violates /localhost");
137 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700138 return;
139 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700140
Junxiao Shi330136a2016-03-10 04:53:08 -0700141 // detect duplicate Nonce with Dead Nonce List
142 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
143 if (hasDuplicateNonceInDnl) {
144 // goto Interest loop pipeline
145 this->onInterestLoop(inFace, interest);
146 return;
147 }
148
Junxiao Shif9858fd2017-02-01 16:22:44 +0000149 // strip Link object if Interest has reached producer region
150 if (interest.hasLink() && m_networkRegionTable.isInProducerRegion(interest.getLink())) {
151 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
152 " interest=" << interest.getName() << " reaching-producer-region");
153 Interest& interestRef = const_cast<Interest&>(interest);
154 interestRef.unsetLink();
155 interestRef.unsetSelectedDelegation();
156 }
157
Junxiao Shid3c792f2014-01-30 00:46:13 -0700158 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700159 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700160
Junxiao Shi330136a2016-03-10 04:53:08 -0700161 // detect duplicate Nonce in PIT entry
Junxiao Shifef73e42016-03-29 14:15:05 -0700162 bool hasDuplicateNonceInPit = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace) !=
163 fw::DUPLICATE_NONCE_NONE;
Junxiao Shi330136a2016-03-10 04:53:08 -0700164 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700165 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700166 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700167 return;
168 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700169
Junxiao Shid3c792f2014-01-30 00:46:13 -0700170 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000171 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700172
Junxiao Shif3c07812014-03-11 21:48:49 -0700173 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700174 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600175 m_cs.find(interest,
176 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
177 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700178 }
mzhang4eab72492015-02-25 11:16:09 -0600179 else {
180 this->onContentStoreMiss(inFace, pitEntry, interest);
181 }
182}
Junxiao Shic041ca32014-02-25 20:01:15 -0700183
mzhang4eab72492015-02-25 11:16:09 -0600184void
Junxiao Shi330136a2016-03-10 04:53:08 -0700185Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700186{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700187 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700188 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700189 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
190 " interest=" << interest.getName() <<
191 " drop");
192 return;
193 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700194
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700195 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 Shi0355e9f2015-09-02 07:24:53 -0700204}
205
206void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000207Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
mzhang4eab72492015-02-25 11:16:09 -0600208 const Interest& interest)
209{
210 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
211
Junxiao Shi4846f372016-04-05 13:39:30 -0700212 // insert in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000213 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700214
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700215 // set PIT unsatisfy timer
216 this->setUnsatisfyTimer(pitEntry);
217
Junxiao Shie342e8d2016-09-18 16:48:00 +0000218 // 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 Shic5f651f2016-11-17 22:58:12 +0000224 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName() << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000225 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000226 // scope control is unnecessary, because privileged app explicitly wants to forward
227 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000228 }
229 return;
230 }
231
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000232 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000233 this->dispatchToStrategy(*pitEntry,
234 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700235}
236
237void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000238Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
239 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600240{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500241 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600242
Junxiao Shicde37ad2015-12-24 01:02:05 -0700243 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600244 // XXX should we lookup PIT for other Interests that also match csMatch?
245
246 // set PIT straggler timer
247 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
248
249 // goto outgoing Data pipeline
250 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
251}
252
Junxiao Shid3c792f2014-01-30 00:46:13 -0700253void
Junxiao Shic5f651f2016-11-17 22:58:12 +0000254Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700255{
Junxiao Shif3c07812014-03-11 21:48:49 -0700256 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
257 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700258
Junxiao Shi4846f372016-04-05 13:39:30 -0700259 // insert out-record
Junxiao Shic5f651f2016-11-17 22:58:12 +0000260 pitEntry->insertOrUpdateOutRecord(outFace, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700261
Junxiao Shid3c792f2014-01-30 00:46:13 -0700262 // send Interest
Junxiao Shic5f651f2016-11-17 22:58:12 +0000263 outFace.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700264 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700265}
266
267void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000268Forwarder::onInterestReject(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700269{
Junxiao Shifef73e42016-03-29 14:15:05 -0700270 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700271 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
272 " cannot reject forwarded Interest");
273 return;
274 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700275 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700276
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700277 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000278 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700279
Junxiao Shid3c792f2014-01-30 00:46:13 -0700280 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700281 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700282}
283
284void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000285Forwarder::onInterestUnsatisfied(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700286{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700287 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
288
Junxiao Shid3c792f2014-01-30 00:46:13 -0700289 // invoke PIT unsatisfied callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000290 this->dispatchToStrategy(*pitEntry,
291 [&] (fw::Strategy& strategy) { strategy.beforeExpirePendingInterest(pitEntry); });
Junxiao Shic041ca32014-02-25 20:01:15 -0700292
Junxiao Shia110f262014-10-12 12:35:20 -0700293 // goto Interest Finalize pipeline
294 this->onInterestFinalize(pitEntry, false);
295}
296
297void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000298Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
299 time::milliseconds dataFreshnessPeriod)
Junxiao Shia110f262014-10-12 12:35:20 -0700300{
301 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
302 (isSatisfied ? " satisfied" : " unsatisfied"));
303
304 // Dead Nonce List insert if necessary
305 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
306
Junxiao Shif3c07812014-03-11 21:48:49 -0700307 // PIT delete
Junxiao Shib9420cf2016-08-13 04:38:52 +0000308 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000309 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700310}
311
312void
313Forwarder::onIncomingData(Face& inFace, const Data& data)
314{
315 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700316 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000317 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700318 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700319
Junxiao Shi88884492014-02-15 15:57:43 -0700320 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700321 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700322 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700323 if (isViolatingLocalhost) {
324 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
325 " data=" << data.getName() << " violates /localhost");
326 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700327 return;
328 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700329
Junxiao Shid3c792f2014-01-30 00:46:13 -0700330 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700331 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
332 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700333 // goto Data unsolicited pipeline
334 this->onDataUnsolicited(inFace, data);
335 return;
336 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700337
Junxiao Shid3c792f2014-01-30 00:46:13 -0700338 // CS insert
339 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700340
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700341 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700342 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700343 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700344 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700345 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700346
Junxiao Shid3c792f2014-01-30 00:46:13 -0700347 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000348 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700349
Junxiao Shid3c792f2014-01-30 00:46:13 -0700350 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700351 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
352 if (inRecord.getExpiry() > now) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000353 pendingDownstreams.insert(&inRecord.getFace());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700354 }
355 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700356
Junxiao Shid938a6b2014-05-11 23:40:29 -0700357 // invoke PIT satisfy callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000358 this->dispatchToStrategy(*pitEntry,
359 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700360
Junxiao Shi4846f372016-04-05 13:39:30 -0700361 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700362 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
363
Junxiao Shid3c792f2014-01-30 00:46:13 -0700364 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700365 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700366 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700367
Junxiao Shid3c792f2014-01-30 00:46:13 -0700368 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700369 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700370 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700371
Junxiao Shid3c792f2014-01-30 00:46:13 -0700372 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700373 for (Face* pendingDownstream : pendingDownstreams) {
374 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700375 continue;
376 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700377 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700378 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700379 }
380}
381
382void
383Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
384{
385 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000386 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
387 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700388 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700389 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700390 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700391
Junxiao Shif3c07812014-03-11 21:48:49 -0700392 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
393 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000394 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700395}
396
397void
398Forwarder::onOutgoingData(const Data& data, Face& outFace)
399{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700400 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700401 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
402 return;
403 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700404 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
405
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700406 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700407 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700408 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700409 if (isViolatingLocalhost) {
410 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
411 " data=" << data.getName() << " violates /localhost");
412 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700413 return;
414 }
415
Junxiao Shif3c07812014-03-11 21:48:49 -0700416 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700417
Junxiao Shid3c792f2014-01-30 00:46:13 -0700418 // send Data
419 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700420 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700421}
422
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700423void
424Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
425{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000426 // receive Nack
427 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700428 ++m_counters.nInNacks;
429
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700430 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700431 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700432 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
433 " nack=" << nack.getInterest().getName() <<
434 "~" << nack.getReason() << " face-is-multi-access");
435 return;
436 }
437
438 // PIT match
439 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
440 // if no PIT entry found, drop
441 if (pitEntry == nullptr) {
442 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
443 " nack=" << nack.getInterest().getName() <<
444 "~" << nack.getReason() << " no-PIT-entry");
445 return;
446 }
447
448 // has out-record?
449 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
450 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700451 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700452 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
453 " nack=" << nack.getInterest().getName() <<
454 "~" << nack.getReason() << " no-out-record");
455 return;
456 }
457
458 // if out-record has different Nonce, drop
459 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
460 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
461 " nack=" << nack.getInterest().getName() <<
462 "~" << nack.getReason() << " wrong-Nonce " <<
463 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
464 return;
465 }
466
467 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
468 " nack=" << nack.getInterest().getName() <<
469 "~" << nack.getReason() << " OK");
470
471 // record Nack on out-record
472 outRecord->setIncomingNack(nack);
473
474 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000475 this->dispatchToStrategy(*pitEntry,
476 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700477}
478
479void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000480Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700481 const lp::NackHeader& nack)
482{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700483 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700484 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
485 " nack=" << pitEntry->getInterest().getName() <<
486 "~" << nack.getReason() << " no-in-record");
487 return;
488 }
489
490 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700491 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700492
493 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700494 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700495 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
496 " nack=" << pitEntry->getInterest().getName() <<
497 "~" << nack.getReason() << " no-in-record");
498 return;
499 }
500
501 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700502 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700503 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
504 " nack=" << pitEntry->getInterest().getName() <<
505 "~" << nack.getReason() << " face-is-multi-access");
506 return;
507 }
508
509 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
510 " nack=" << pitEntry->getInterest().getName() <<
511 "~" << nack.getReason() << " OK");
512
513 // create Nack packet with the Interest from in-record
514 lp::Nack nackPkt(inRecord->getInterest());
515 nackPkt.setHeader(nack);
516
517 // erase in-record
518 pitEntry->deleteInRecord(outFace);
519
520 // send Nack on face
521 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700522 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700523}
524
Junxiao Shid3c792f2014-01-30 00:46:13 -0700525static inline bool
526compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
527{
528 return a.getExpiry() < b.getExpiry();
529}
530
531void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000532Forwarder::setUnsatisfyTimer(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700533{
Junxiao Shi4846f372016-04-05 13:39:30 -0700534 pit::InRecordCollection::iterator lastExpiring =
535 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700536
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700537 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700538 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
539 if (lastExpiryFromNow <= time::seconds::zero()) {
540 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700541 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700542
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700543 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700544 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700545 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
546}
547
548void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000549Forwarder::setStragglerTimer(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
550 time::milliseconds dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700551{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700552 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700553
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700554 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700555 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700556 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700557}
558
559void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000560Forwarder::cancelUnsatisfyAndStragglerTimer(pit::Entry& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700561{
Junxiao Shib9420cf2016-08-13 04:38:52 +0000562 scheduler::cancel(pitEntry.m_unsatisfyTimer);
563 scheduler::cancel(pitEntry.m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700564}
565
Junxiao Shia110f262014-10-12 12:35:20 -0700566static inline void
567insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
568 const pit::OutRecord& outRecord)
569{
570 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
571}
572
573void
574Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000575 time::milliseconds dataFreshnessPeriod, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700576{
577 // need Dead Nonce List insert?
578 bool needDnl = false;
579 if (isSatisfied) {
580 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
581 // Data never becomes stale if it doesn't have FreshnessPeriod field
582 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
583 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
584 }
585 else {
586 needDnl = true;
587 }
588
589 if (!needDnl) {
590 return;
591 }
592
593 // Dead Nonce List insert
594 if (upstream == 0) {
595 // insert all outgoing Nonces
596 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
597 std::for_each(outRecords.begin(), outRecords.end(),
598 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
599 }
600 else {
601 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700602 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700603 if (outRecord != pitEntry.getOutRecords().end()) {
604 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
605 }
606 }
607}
608
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800609} // namespace nfd