blob: 2ee8e1b9e82e90d9ab95179c2262c52e09d19990 [file] [log] [blame]
Alexander Afanasyev33b72772014-01-26 23:22:58 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shifc2e13d2017-07-25 02:08:48 +00002/*
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
Spyridon Mastorakis812451e2014-12-05 22:43:34 -080034#include "face/null-face.hpp"
35
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080036namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080037
Junxiao Shi8c8d2182014-01-30 22:33:00 -070038NFD_LOG_INIT("Forwarder");
39
Junxiao Shic34d1672016-12-09 15:57:59 +000040static Name
41getDefaultStrategyName()
42{
Junxiao Shi037f4ab2016-12-13 04:27:06 +000043 return fw::BestRouteStrategy2::getStrategyName();
Junxiao Shic34d1672016-12-09 15:57:59 +000044}
45
Junxiao Shic041ca32014-02-25 20:01:15 -070046Forwarder::Forwarder()
Junxiao Shi9685cc52016-08-29 12:47:05 +000047 : m_unsolicitedDataPolicy(new fw::DefaultUnsolicitedDataPolicy())
Junxiao Shifbe8efe2016-08-22 16:02:30 +000048 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060049 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080050 , m_measurements(m_nameTree)
Junxiao Shic34d1672016-12-09 15:57:59 +000051 , m_strategyChoice(*this)
Spyridon Mastorakis812451e2014-12-05 22:43:34 -080052 , m_csFace(face::makeNullFace(FaceUri("contentstore://")))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080053{
Spyridon Mastorakis812451e2014-12-05 22:43:34 -080054 getFaceTable().addReserved(m_csFace, face::FACEID_CONTENT_STORE);
55
Junxiao Shidcffdaa2016-07-26 02:23:56 +000056 m_faceTable.afterAdd.connect([this] (Face& face) {
57 face.afterReceiveInterest.connect(
58 [this, &face] (const Interest& interest) {
59 this->startProcessInterest(face, interest);
60 });
61 face.afterReceiveData.connect(
62 [this, &face] (const Data& data) {
63 this->startProcessData(face, data);
64 });
65 face.afterReceiveNack.connect(
66 [this, &face] (const lp::Nack& nack) {
67 this->startProcessNack(face, nack);
68 });
Eric Newberry41aba102017-11-01 16:42:13 -070069 face.onDroppedInterest.connect(
70 [this, &face] (const Interest& interest) {
71 this->onDroppedInterest(face, interest);
72 });
Junxiao Shidcffdaa2016-07-26 02:23:56 +000073 });
74
75 m_faceTable.beforeRemove.connect([this] (Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000076 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000077 });
Junxiao Shic34d1672016-12-09 15:57:59 +000078
79 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080080}
81
Junxiao Shidcffdaa2016-07-26 02:23:56 +000082Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060083
Junxiao Shi0355e9f2015-09-02 07:24:53 -070084void
Junxiao Shid3c792f2014-01-30 00:46:13 -070085Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
86{
87 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -070088 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
89 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +000090 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -070091 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -070092
Junxiao Shi88884492014-02-15 15:57:43 -070093 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -070094 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -070095 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -070096 if (isViolatingLocalhost) {
97 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
98 " interest=" << interest.getName() << " violates /localhost");
99 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700100 return;
101 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700102
Junxiao Shi330136a2016-03-10 04:53:08 -0700103 // detect duplicate Nonce with Dead Nonce List
104 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
105 if (hasDuplicateNonceInDnl) {
106 // goto Interest loop pipeline
107 this->onInterestLoop(inFace, interest);
108 return;
109 }
110
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000111 // strip forwarding hint if Interest has reached producer region
112 if (!interest.getForwardingHint().empty() &&
113 m_networkRegionTable.isInProducerRegion(interest.getForwardingHint())) {
Junxiao Shif9858fd2017-02-01 16:22:44 +0000114 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
115 " interest=" << interest.getName() << " reaching-producer-region");
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000116 const_cast<Interest&>(interest).setForwardingHint({});
Junxiao Shif9858fd2017-02-01 16:22:44 +0000117 }
118
Junxiao Shid3c792f2014-01-30 00:46:13 -0700119 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700120 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700121
Junxiao Shi330136a2016-03-10 04:53:08 -0700122 // detect duplicate Nonce in PIT entry
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000123 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace);
124 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
125 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
126 // for p2p face: duplicate Nonce from same incoming face is not loop
127 hasDuplicateNonceInPit = hasDuplicateNonceInPit && !(dnw & fw::DUPLICATE_NONCE_IN_SAME);
128 }
Junxiao Shi330136a2016-03-10 04:53:08 -0700129 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700130 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700131 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700132 return;
133 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700134
Junxiao Shid3c792f2014-01-30 00:46:13 -0700135 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000136 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700137
Spyridon Mastorakis812451e2014-12-05 22:43:34 -0800138 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
139 bool isPending = inRecords.begin() != inRecords.end();
140 if (!isPending) {
141 if (m_csFromNdnSim == nullptr) {
142 m_cs.find(interest,
143 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
144 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
145 }
146 else {
147 shared_ptr<Data> match = m_csFromNdnSim->Lookup(interest.shared_from_this());
148 if (match != nullptr) {
149 this->onContentStoreHit(inFace, pitEntry, interest, *match);
150 }
151 else {
152 this->onContentStoreMiss(inFace, pitEntry, interest);
153 }
154 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700155 }
mzhang4eab72492015-02-25 11:16:09 -0600156 else {
157 this->onContentStoreMiss(inFace, pitEntry, interest);
158 }
159}
Junxiao Shic041ca32014-02-25 20:01:15 -0700160
mzhang4eab72492015-02-25 11:16:09 -0600161void
Junxiao Shi330136a2016-03-10 04:53:08 -0700162Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700163{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700164 // if multi-access or ad hoc face, drop
165 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700166 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
167 " interest=" << interest.getName() <<
168 " drop");
169 return;
170 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700171
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700172 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
173 " interest=" << interest.getName() <<
174 " send-Nack-duplicate");
175
176 // send Nack with reason=DUPLICATE
177 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
178 lp::Nack nack(interest);
179 nack.setReason(lp::NackReason::DUPLICATE);
180 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700181}
182
183void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000184Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
mzhang4eab72492015-02-25 11:16:09 -0600185 const Interest& interest)
186{
187 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000188 ++m_counters.nCsMisses;
mzhang4eab72492015-02-25 11:16:09 -0600189
Junxiao Shi4846f372016-04-05 13:39:30 -0700190 // insert in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000191 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700192
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700193 // set PIT unsatisfy timer
194 this->setUnsatisfyTimer(pitEntry);
195
Junxiao Shie342e8d2016-09-18 16:48:00 +0000196 // has NextHopFaceId?
197 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
198 if (nextHopTag != nullptr) {
199 // chosen NextHop face exists?
200 Face* nextHopFace = m_faceTable.get(*nextHopTag);
201 if (nextHopFace != nullptr) {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000202 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName() << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000203 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000204 // scope control is unnecessary, because privileged app explicitly wants to forward
205 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000206 }
207 return;
208 }
209
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000210 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000211 this->dispatchToStrategy(*pitEntry,
212 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700213}
214
215void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000216Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
217 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600218{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500219 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000220 ++m_counters.nCsHits;
mzhang4eab72492015-02-25 11:16:09 -0600221
Spyridon Mastorakis812451e2014-12-05 22:43:34 -0800222 beforeSatisfyInterest(*pitEntry, *m_csFace, data);
223 this->dispatchToStrategy(*pitEntry,
224 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, *m_csFace, data); });
225
Junxiao Shicde37ad2015-12-24 01:02:05 -0700226 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600227 // XXX should we lookup PIT for other Interests that also match csMatch?
228
229 // set PIT straggler timer
230 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
231
232 // goto outgoing Data pipeline
233 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
234}
235
Junxiao Shid3c792f2014-01-30 00:46:13 -0700236void
Junxiao Shic5f651f2016-11-17 22:58:12 +0000237Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700238{
Junxiao Shif3c07812014-03-11 21:48:49 -0700239 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
240 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700241
Junxiao Shi4846f372016-04-05 13:39:30 -0700242 // insert out-record
Junxiao Shic5f651f2016-11-17 22:58:12 +0000243 pitEntry->insertOrUpdateOutRecord(outFace, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700244
Junxiao Shid3c792f2014-01-30 00:46:13 -0700245 // send Interest
Junxiao Shic5f651f2016-11-17 22:58:12 +0000246 outFace.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700247 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700248}
249
250void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000251Forwarder::onInterestReject(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700252{
Junxiao Shifef73e42016-03-29 14:15:05 -0700253 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700254 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
255 " cannot reject forwarded Interest");
256 return;
257 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700258 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700259
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700260 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000261 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700262
Junxiao Shid3c792f2014-01-30 00:46:13 -0700263 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700264 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700265}
266
267void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000268Forwarder::onInterestUnsatisfied(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700269{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700270 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
271
Junxiao Shid3c792f2014-01-30 00:46:13 -0700272 // invoke PIT unsatisfied callback
Spyridon Mastorakis812451e2014-12-05 22:43:34 -0800273 beforeExpirePendingInterest(*pitEntry);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000274 this->dispatchToStrategy(*pitEntry,
275 [&] (fw::Strategy& strategy) { strategy.beforeExpirePendingInterest(pitEntry); });
Junxiao Shic041ca32014-02-25 20:01:15 -0700276
Junxiao Shia110f262014-10-12 12:35:20 -0700277 // goto Interest Finalize pipeline
278 this->onInterestFinalize(pitEntry, false);
279}
280
281void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000282Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
Eric Newberryf4056d02017-05-26 17:31:53 +0000283 ndn::optional<time::milliseconds> dataFreshnessPeriod)
Junxiao Shia110f262014-10-12 12:35:20 -0700284{
285 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
286 (isSatisfied ? " satisfied" : " unsatisfied"));
287
288 // Dead Nonce List insert if necessary
289 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
290
Junxiao Shif3c07812014-03-11 21:48:49 -0700291 // PIT delete
Junxiao Shib9420cf2016-08-13 04:38:52 +0000292 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000293 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700294}
295
296void
297Forwarder::onIncomingData(Face& inFace, const Data& data)
298{
299 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700300 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000301 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700302 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700303
Junxiao Shi88884492014-02-15 15:57:43 -0700304 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700305 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700306 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700307 if (isViolatingLocalhost) {
308 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
309 " data=" << data.getName() << " violates /localhost");
310 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700311 return;
312 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700313
Junxiao Shid3c792f2014-01-30 00:46:13 -0700314 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700315 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
316 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700317 // goto Data unsolicited pipeline
318 this->onDataUnsolicited(inFace, data);
319 return;
320 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700321
Junxiao Shid3c792f2014-01-30 00:46:13 -0700322 // CS insert
Spyridon Mastorakis812451e2014-12-05 22:43:34 -0800323 if (m_csFromNdnSim == nullptr)
324 m_cs.insert(data);
325 else
326 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shic041ca32014-02-25 20:01:15 -0700327
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700328 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700329 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700330 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700331 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700332 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700333
Junxiao Shid3c792f2014-01-30 00:46:13 -0700334 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000335 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700336
Junxiao Shid3c792f2014-01-30 00:46:13 -0700337 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700338 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
339 if (inRecord.getExpiry() > now) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000340 pendingDownstreams.insert(&inRecord.getFace());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700341 }
342 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700343
Junxiao Shid938a6b2014-05-11 23:40:29 -0700344 // invoke PIT satisfy callback
Spyridon Mastorakis812451e2014-12-05 22:43:34 -0800345 beforeSatisfyInterest(*pitEntry, inFace, data);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000346 this->dispatchToStrategy(*pitEntry,
347 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700348
Junxiao Shi4846f372016-04-05 13:39:30 -0700349 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700350 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
351
Junxiao Shid3c792f2014-01-30 00:46:13 -0700352 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700353 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700354 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700355
Junxiao Shid3c792f2014-01-30 00:46:13 -0700356 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700357 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700358 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700359
Junxiao Shid3c792f2014-01-30 00:46:13 -0700360 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700361 for (Face* pendingDownstream : pendingDownstreams) {
Teng Liangf995f382017-04-04 22:09:39 +0000362 if (pendingDownstream->getId() == inFace.getId() &&
363 pendingDownstream->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Junxiao Shida006f52014-05-16 11:18:00 -0700364 continue;
365 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700366 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700367 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700368 }
369}
370
371void
372Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
373{
374 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000375 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
376 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700377 // CS insert
Spyridon Mastorakis812451e2014-12-05 22:43:34 -0800378 if (m_csFromNdnSim == nullptr)
379 m_cs.insert(data, true);
380 else
381 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700382 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700383
Junxiao Shif3c07812014-03-11 21:48:49 -0700384 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
385 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000386 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700387}
388
389void
390Forwarder::onOutgoingData(const Data& data, Face& outFace)
391{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700392 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700393 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
394 return;
395 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700396 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
397
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700398 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700399 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700400 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700401 if (isViolatingLocalhost) {
402 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
403 " data=" << data.getName() << " violates /localhost");
404 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700405 return;
406 }
407
Junxiao Shif3c07812014-03-11 21:48:49 -0700408 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700409
Junxiao Shid3c792f2014-01-30 00:46:13 -0700410 // send Data
411 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700412 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700413}
414
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700415void
416Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
417{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000418 // receive Nack
419 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700420 ++m_counters.nInNacks;
421
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700422 // if multi-access or ad hoc face, drop
423 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700424 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
425 " nack=" << nack.getInterest().getName() <<
426 "~" << nack.getReason() << " face-is-multi-access");
427 return;
428 }
429
430 // PIT match
431 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
432 // if no PIT entry found, drop
433 if (pitEntry == nullptr) {
434 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
435 " nack=" << nack.getInterest().getName() <<
436 "~" << nack.getReason() << " no-PIT-entry");
437 return;
438 }
439
440 // has out-record?
441 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
442 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700443 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700444 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
445 " nack=" << nack.getInterest().getName() <<
446 "~" << nack.getReason() << " no-out-record");
447 return;
448 }
449
450 // if out-record has different Nonce, drop
451 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
452 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
453 " nack=" << nack.getInterest().getName() <<
454 "~" << nack.getReason() << " wrong-Nonce " <<
455 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
456 return;
457 }
458
459 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
460 " nack=" << nack.getInterest().getName() <<
461 "~" << nack.getReason() << " OK");
462
463 // record Nack on out-record
464 outRecord->setIncomingNack(nack);
465
466 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000467 this->dispatchToStrategy(*pitEntry,
468 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700469}
470
471void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000472Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700473 const lp::NackHeader& nack)
474{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700475 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700476 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
477 " nack=" << pitEntry->getInterest().getName() <<
478 "~" << nack.getReason() << " no-in-record");
479 return;
480 }
481
482 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700483 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700484
485 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700486 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700487 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
488 " nack=" << pitEntry->getInterest().getName() <<
489 "~" << nack.getReason() << " no-in-record");
490 return;
491 }
492
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700493 // if multi-access or ad hoc face, drop
494 if (outFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700495 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
496 " nack=" << pitEntry->getInterest().getName() <<
497 "~" << nack.getReason() << " face-is-multi-access");
498 return;
499 }
500
501 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
502 " nack=" << pitEntry->getInterest().getName() <<
503 "~" << nack.getReason() << " OK");
504
505 // create Nack packet with the Interest from in-record
506 lp::Nack nackPkt(inRecord->getInterest());
507 nackPkt.setHeader(nack);
508
509 // erase in-record
510 pitEntry->deleteInRecord(outFace);
511
512 // send Nack on face
513 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700514 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700515}
516
Eric Newberry41aba102017-11-01 16:42:13 -0700517void
518Forwarder::onDroppedInterest(Face& outFace, const Interest& interest)
519{
520 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(outFace, interest);
521}
522
Junxiao Shid3c792f2014-01-30 00:46:13 -0700523static inline bool
524compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
525{
526 return a.getExpiry() < b.getExpiry();
527}
528
529void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000530Forwarder::setUnsatisfyTimer(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700531{
Junxiao Shi4846f372016-04-05 13:39:30 -0700532 pit::InRecordCollection::iterator lastExpiring =
533 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700534
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700535 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700536 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
537 if (lastExpiryFromNow <= time::seconds::zero()) {
538 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700539 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700540
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700541 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700542 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700543 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
544}
545
546void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000547Forwarder::setStragglerTimer(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
Eric Newberryf4056d02017-05-26 17:31:53 +0000548 ndn::optional<time::milliseconds> dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700549{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700550 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700551
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700552 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700553 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700554 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700555}
556
557void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000558Forwarder::cancelUnsatisfyAndStragglerTimer(pit::Entry& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700559{
Junxiao Shib9420cf2016-08-13 04:38:52 +0000560 scheduler::cancel(pitEntry.m_unsatisfyTimer);
561 scheduler::cancel(pitEntry.m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700562}
563
Junxiao Shia110f262014-10-12 12:35:20 -0700564static inline void
565insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
566 const pit::OutRecord& outRecord)
567{
568 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
569}
570
571void
572Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
Eric Newberryf4056d02017-05-26 17:31:53 +0000573 ndn::optional<time::milliseconds> dataFreshnessPeriod, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700574{
575 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000576 bool needDnl = true;
Junxiao Shia110f262014-10-12 12:35:20 -0700577 if (isSatisfied) {
Eric Newberryf4056d02017-05-26 17:31:53 +0000578 BOOST_ASSERT(dataFreshnessPeriod);
579 BOOST_ASSERT(*dataFreshnessPeriod >= time::milliseconds::zero());
Junxiao Shia110f262014-10-12 12:35:20 -0700580 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Eric Newberryf4056d02017-05-26 17:31:53 +0000581 *dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700582 }
583
584 if (!needDnl) {
585 return;
586 }
587
588 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000589 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700590 // insert all outgoing Nonces
591 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
592 std::for_each(outRecords.begin(), outRecords.end(),
593 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
594 }
595 else {
596 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700597 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700598 if (outRecord != pitEntry.getOutRecords().end()) {
599 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
600 }
601 }
602}
603
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800604} // namespace nfd