blob: 41ab2e2d709cefc546398871903f2a69f60ada11 [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 Shi2fe3af02017-03-04 17:24:19 +0000162 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace);
163 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
164 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
165 // for p2p face: duplicate Nonce from same incoming face is not loop
166 hasDuplicateNonceInPit = hasDuplicateNonceInPit && !(dnw & fw::DUPLICATE_NONCE_IN_SAME);
167 }
Junxiao Shi330136a2016-03-10 04:53:08 -0700168 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700169 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700170 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700171 return;
172 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700173
Junxiao Shid3c792f2014-01-30 00:46:13 -0700174 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000175 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700176
Junxiao Shif3c07812014-03-11 21:48:49 -0700177 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700178 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600179 m_cs.find(interest,
180 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
181 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700182 }
mzhang4eab72492015-02-25 11:16:09 -0600183 else {
184 this->onContentStoreMiss(inFace, pitEntry, interest);
185 }
186}
Junxiao Shic041ca32014-02-25 20:01:15 -0700187
mzhang4eab72492015-02-25 11:16:09 -0600188void
Junxiao Shi330136a2016-03-10 04:53:08 -0700189Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700190{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700191 // if multi-access or ad hoc face, drop
192 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700193 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
194 " interest=" << interest.getName() <<
195 " drop");
196 return;
197 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700198
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700199 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
200 " interest=" << interest.getName() <<
201 " send-Nack-duplicate");
202
203 // send Nack with reason=DUPLICATE
204 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
205 lp::Nack nack(interest);
206 nack.setReason(lp::NackReason::DUPLICATE);
207 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700208}
209
210void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000211Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
mzhang4eab72492015-02-25 11:16:09 -0600212 const Interest& interest)
213{
214 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
215
Junxiao Shi4846f372016-04-05 13:39:30 -0700216 // insert in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000217 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700218
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700219 // set PIT unsatisfy timer
220 this->setUnsatisfyTimer(pitEntry);
221
Junxiao Shie342e8d2016-09-18 16:48:00 +0000222 // has NextHopFaceId?
223 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
224 if (nextHopTag != nullptr) {
225 // chosen NextHop face exists?
226 Face* nextHopFace = m_faceTable.get(*nextHopTag);
227 if (nextHopFace != nullptr) {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000228 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName() << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000229 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000230 // scope control is unnecessary, because privileged app explicitly wants to forward
231 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000232 }
233 return;
234 }
235
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000236 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000237 this->dispatchToStrategy(*pitEntry,
238 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700239}
240
241void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000242Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
243 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600244{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500245 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600246
Junxiao Shicde37ad2015-12-24 01:02:05 -0700247 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600248 // XXX should we lookup PIT for other Interests that also match csMatch?
249
250 // set PIT straggler timer
251 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
252
253 // goto outgoing Data pipeline
254 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
255}
256
Junxiao Shid3c792f2014-01-30 00:46:13 -0700257void
Junxiao Shic5f651f2016-11-17 22:58:12 +0000258Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700259{
Junxiao Shif3c07812014-03-11 21:48:49 -0700260 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
261 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700262
Junxiao Shi4846f372016-04-05 13:39:30 -0700263 // insert out-record
Junxiao Shic5f651f2016-11-17 22:58:12 +0000264 pitEntry->insertOrUpdateOutRecord(outFace, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700265
Junxiao Shid3c792f2014-01-30 00:46:13 -0700266 // send Interest
Junxiao Shic5f651f2016-11-17 22:58:12 +0000267 outFace.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700268 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700269}
270
271void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000272Forwarder::onInterestReject(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700273{
Junxiao Shifef73e42016-03-29 14:15:05 -0700274 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700275 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
276 " cannot reject forwarded Interest");
277 return;
278 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700279 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700280
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700281 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000282 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700283
Junxiao Shid3c792f2014-01-30 00:46:13 -0700284 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700285 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700286}
287
288void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000289Forwarder::onInterestUnsatisfied(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700290{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700291 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
292
Junxiao Shid3c792f2014-01-30 00:46:13 -0700293 // invoke PIT unsatisfied callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000294 this->dispatchToStrategy(*pitEntry,
295 [&] (fw::Strategy& strategy) { strategy.beforeExpirePendingInterest(pitEntry); });
Junxiao Shic041ca32014-02-25 20:01:15 -0700296
Junxiao Shia110f262014-10-12 12:35:20 -0700297 // goto Interest Finalize pipeline
298 this->onInterestFinalize(pitEntry, false);
299}
300
301void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000302Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
303 time::milliseconds dataFreshnessPeriod)
Junxiao Shia110f262014-10-12 12:35:20 -0700304{
305 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
306 (isSatisfied ? " satisfied" : " unsatisfied"));
307
308 // Dead Nonce List insert if necessary
309 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
310
Junxiao Shif3c07812014-03-11 21:48:49 -0700311 // PIT delete
Junxiao Shib9420cf2016-08-13 04:38:52 +0000312 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000313 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700314}
315
316void
317Forwarder::onIncomingData(Face& inFace, const Data& data)
318{
319 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700320 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000321 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700322 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700323
Junxiao Shi88884492014-02-15 15:57:43 -0700324 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700325 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700326 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700327 if (isViolatingLocalhost) {
328 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
329 " data=" << data.getName() << " violates /localhost");
330 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700331 return;
332 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700333
Junxiao Shid3c792f2014-01-30 00:46:13 -0700334 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700335 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
336 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700337 // goto Data unsolicited pipeline
338 this->onDataUnsolicited(inFace, data);
339 return;
340 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700341
Junxiao Shid3c792f2014-01-30 00:46:13 -0700342 // CS insert
343 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700344
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700345 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700346 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700347 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700348 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700349 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700350
Junxiao Shid3c792f2014-01-30 00:46:13 -0700351 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000352 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700353
Junxiao Shid3c792f2014-01-30 00:46:13 -0700354 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700355 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
356 if (inRecord.getExpiry() > now) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000357 pendingDownstreams.insert(&inRecord.getFace());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700358 }
359 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700360
Junxiao Shid938a6b2014-05-11 23:40:29 -0700361 // invoke PIT satisfy callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000362 this->dispatchToStrategy(*pitEntry,
363 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700364
Junxiao Shi4846f372016-04-05 13:39:30 -0700365 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700366 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
367
Junxiao Shid3c792f2014-01-30 00:46:13 -0700368 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700369 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700370 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700371
Junxiao Shid3c792f2014-01-30 00:46:13 -0700372 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700373 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700374 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700375
Junxiao Shid3c792f2014-01-30 00:46:13 -0700376 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700377 for (Face* pendingDownstream : pendingDownstreams) {
Teng Liangf995f382017-04-04 22:09:39 +0000378 if (pendingDownstream->getId() == inFace.getId() &&
379 pendingDownstream->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Junxiao Shida006f52014-05-16 11:18:00 -0700380 continue;
381 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700382 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700383 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700384 }
385}
386
387void
388Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
389{
390 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000391 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
392 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700393 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700394 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700395 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700396
Junxiao Shif3c07812014-03-11 21:48:49 -0700397 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
398 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000399 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700400}
401
402void
403Forwarder::onOutgoingData(const Data& data, Face& outFace)
404{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700405 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700406 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
407 return;
408 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700409 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
410
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700411 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700412 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700413 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700414 if (isViolatingLocalhost) {
415 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
416 " data=" << data.getName() << " violates /localhost");
417 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700418 return;
419 }
420
Junxiao Shif3c07812014-03-11 21:48:49 -0700421 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700422
Junxiao Shid3c792f2014-01-30 00:46:13 -0700423 // send Data
424 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700425 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700426}
427
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700428void
429Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
430{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000431 // receive Nack
432 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700433 ++m_counters.nInNacks;
434
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700435 // if multi-access or ad hoc face, drop
436 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700437 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
438 " nack=" << nack.getInterest().getName() <<
439 "~" << nack.getReason() << " face-is-multi-access");
440 return;
441 }
442
443 // PIT match
444 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
445 // if no PIT entry found, drop
446 if (pitEntry == nullptr) {
447 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
448 " nack=" << nack.getInterest().getName() <<
449 "~" << nack.getReason() << " no-PIT-entry");
450 return;
451 }
452
453 // has out-record?
454 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
455 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700456 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700457 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
458 " nack=" << nack.getInterest().getName() <<
459 "~" << nack.getReason() << " no-out-record");
460 return;
461 }
462
463 // if out-record has different Nonce, drop
464 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
465 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
466 " nack=" << nack.getInterest().getName() <<
467 "~" << nack.getReason() << " wrong-Nonce " <<
468 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
469 return;
470 }
471
472 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
473 " nack=" << nack.getInterest().getName() <<
474 "~" << nack.getReason() << " OK");
475
476 // record Nack on out-record
477 outRecord->setIncomingNack(nack);
478
479 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000480 this->dispatchToStrategy(*pitEntry,
481 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700482}
483
484void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000485Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700486 const lp::NackHeader& nack)
487{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700488 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700489 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
490 " nack=" << pitEntry->getInterest().getName() <<
491 "~" << nack.getReason() << " no-in-record");
492 return;
493 }
494
495 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700496 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700497
498 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700499 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700500 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
501 " nack=" << pitEntry->getInterest().getName() <<
502 "~" << nack.getReason() << " no-in-record");
503 return;
504 }
505
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700506 // if multi-access or ad hoc face, drop
507 if (outFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700508 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
509 " nack=" << pitEntry->getInterest().getName() <<
510 "~" << nack.getReason() << " face-is-multi-access");
511 return;
512 }
513
514 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
515 " nack=" << pitEntry->getInterest().getName() <<
516 "~" << nack.getReason() << " OK");
517
518 // create Nack packet with the Interest from in-record
519 lp::Nack nackPkt(inRecord->getInterest());
520 nackPkt.setHeader(nack);
521
522 // erase in-record
523 pitEntry->deleteInRecord(outFace);
524
525 // send Nack on face
526 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700527 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700528}
529
Junxiao Shid3c792f2014-01-30 00:46:13 -0700530static inline bool
531compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
532{
533 return a.getExpiry() < b.getExpiry();
534}
535
536void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000537Forwarder::setUnsatisfyTimer(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700538{
Junxiao Shi4846f372016-04-05 13:39:30 -0700539 pit::InRecordCollection::iterator lastExpiring =
540 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700541
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700542 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700543 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
544 if (lastExpiryFromNow <= time::seconds::zero()) {
545 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700546 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700547
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700548 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700549 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700550 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
551}
552
553void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000554Forwarder::setStragglerTimer(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
555 time::milliseconds dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700556{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700557 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700558
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700559 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700560 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700561 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700562}
563
564void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000565Forwarder::cancelUnsatisfyAndStragglerTimer(pit::Entry& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700566{
Junxiao Shib9420cf2016-08-13 04:38:52 +0000567 scheduler::cancel(pitEntry.m_unsatisfyTimer);
568 scheduler::cancel(pitEntry.m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700569}
570
Junxiao Shia110f262014-10-12 12:35:20 -0700571static inline void
572insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
573 const pit::OutRecord& outRecord)
574{
575 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
576}
577
578void
579Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000580 time::milliseconds dataFreshnessPeriod, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700581{
582 // need Dead Nonce List insert?
583 bool needDnl = false;
584 if (isSatisfied) {
585 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
586 // Data never becomes stale if it doesn't have FreshnessPeriod field
587 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
588 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
589 }
590 else {
591 needDnl = true;
592 }
593
594 if (!needDnl) {
595 return;
596 }
597
598 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000599 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700600 // insert all outgoing Nonces
601 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
602 std::for_each(outRecords.begin(), outRecords.end(),
603 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
604 }
605 else {
606 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700607 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700608 if (outRecord != pitEntry.getOutRecords().end()) {
609 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
610 }
611 }
612}
613
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800614} // namespace nfd