blob: 5d7abdf7a0068ef7ffc267d0f3ea4c54708a31cb [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 Mastorakis01091f32014-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 Mastorakis01091f32014-12-05 22:43:34 -080052 , m_csFace(face::makeNullFace(FaceUri("contentstore://")))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080053{
Spyridon Mastorakis01091f32014-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 });
69 });
70
71 m_faceTable.beforeRemove.connect([this] (Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000072 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000073 });
Junxiao Shic34d1672016-12-09 15:57:59 +000074
75 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080076}
77
Junxiao Shidcffdaa2016-07-26 02:23:56 +000078Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060079
Junxiao Shi0355e9f2015-09-02 07:24:53 -070080void
Junxiao Shid3c792f2014-01-30 00:46:13 -070081Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
82{
83 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -070084 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
85 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +000086 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -070087 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -070088
Junxiao Shi88884492014-02-15 15:57:43 -070089 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -070090 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -070091 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -070092 if (isViolatingLocalhost) {
93 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
94 " interest=" << interest.getName() << " violates /localhost");
95 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -070096 return;
97 }
Junxiao Shic041ca32014-02-25 20:01:15 -070098
Junxiao Shi330136a2016-03-10 04:53:08 -070099 // detect duplicate Nonce with Dead Nonce List
100 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
101 if (hasDuplicateNonceInDnl) {
102 // goto Interest loop pipeline
103 this->onInterestLoop(inFace, interest);
104 return;
105 }
106
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000107 // strip forwarding hint if Interest has reached producer region
108 if (!interest.getForwardingHint().empty() &&
109 m_networkRegionTable.isInProducerRegion(interest.getForwardingHint())) {
Junxiao Shif9858fd2017-02-01 16:22:44 +0000110 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
111 " interest=" << interest.getName() << " reaching-producer-region");
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000112 const_cast<Interest&>(interest).setForwardingHint({});
Junxiao Shif9858fd2017-02-01 16:22:44 +0000113 }
114
Junxiao Shid3c792f2014-01-30 00:46:13 -0700115 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700116 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700117
Junxiao Shi330136a2016-03-10 04:53:08 -0700118 // detect duplicate Nonce in PIT entry
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000119 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace);
120 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
121 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
122 // for p2p face: duplicate Nonce from same incoming face is not loop
123 hasDuplicateNonceInPit = hasDuplicateNonceInPit && !(dnw & fw::DUPLICATE_NONCE_IN_SAME);
124 }
Junxiao Shi330136a2016-03-10 04:53:08 -0700125 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700126 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700127 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700128 return;
129 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700130
Junxiao Shid3c792f2014-01-30 00:46:13 -0700131 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000132 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700133
Spyridon Mastorakis01091f32014-12-05 22:43:34 -0800134 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
135 bool isPending = inRecords.begin() != inRecords.end();
136 if (!isPending) {
137 if (m_csFromNdnSim == nullptr) {
138 m_cs.find(interest,
139 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
140 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
141 }
142 else {
143 shared_ptr<Data> match = m_csFromNdnSim->Lookup(interest.shared_from_this());
144 if (match != nullptr) {
145 this->onContentStoreHit(inFace, pitEntry, interest, *match);
146 }
147 else {
148 this->onContentStoreMiss(inFace, pitEntry, interest);
149 }
150 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700151 }
mzhang4eab72492015-02-25 11:16:09 -0600152 else {
153 this->onContentStoreMiss(inFace, pitEntry, interest);
154 }
155}
Junxiao Shic041ca32014-02-25 20:01:15 -0700156
mzhang4eab72492015-02-25 11:16:09 -0600157void
Junxiao Shi330136a2016-03-10 04:53:08 -0700158Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700159{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700160 // if multi-access or ad hoc face, drop
161 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700162 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
163 " interest=" << interest.getName() <<
164 " drop");
165 return;
166 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700167
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700168 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
169 " interest=" << interest.getName() <<
170 " send-Nack-duplicate");
171
172 // send Nack with reason=DUPLICATE
173 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
174 lp::Nack nack(interest);
175 nack.setReason(lp::NackReason::DUPLICATE);
176 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700177}
178
179void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000180Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
mzhang4eab72492015-02-25 11:16:09 -0600181 const Interest& interest)
182{
183 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000184 ++m_counters.nCsMisses;
mzhang4eab72492015-02-25 11:16:09 -0600185
Junxiao Shi4846f372016-04-05 13:39:30 -0700186 // insert in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000187 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700188
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700189 // set PIT unsatisfy timer
190 this->setUnsatisfyTimer(pitEntry);
191
Junxiao Shie342e8d2016-09-18 16:48:00 +0000192 // has NextHopFaceId?
193 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
194 if (nextHopTag != nullptr) {
195 // chosen NextHop face exists?
196 Face* nextHopFace = m_faceTable.get(*nextHopTag);
197 if (nextHopFace != nullptr) {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000198 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName() << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000199 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000200 // scope control is unnecessary, because privileged app explicitly wants to forward
201 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000202 }
203 return;
204 }
205
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000206 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000207 this->dispatchToStrategy(*pitEntry,
208 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700209}
210
211void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000212Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
213 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600214{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500215 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000216 ++m_counters.nCsHits;
mzhang4eab72492015-02-25 11:16:09 -0600217
Spyridon Mastorakis01091f32014-12-05 22:43:34 -0800218 beforeSatisfyInterest(*pitEntry, *m_csFace, data);
219 this->dispatchToStrategy(*pitEntry,
220 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, *m_csFace, data); });
221
Junxiao Shicde37ad2015-12-24 01:02:05 -0700222 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600223 // XXX should we lookup PIT for other Interests that also match csMatch?
224
225 // set PIT straggler timer
226 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
227
228 // goto outgoing Data pipeline
229 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
230}
231
Junxiao Shid3c792f2014-01-30 00:46:13 -0700232void
Junxiao Shic5f651f2016-11-17 22:58:12 +0000233Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700234{
Junxiao Shif3c07812014-03-11 21:48:49 -0700235 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
236 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700237
Junxiao Shi4846f372016-04-05 13:39:30 -0700238 // insert out-record
Junxiao Shic5f651f2016-11-17 22:58:12 +0000239 pitEntry->insertOrUpdateOutRecord(outFace, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700240
Junxiao Shid3c792f2014-01-30 00:46:13 -0700241 // send Interest
Junxiao Shic5f651f2016-11-17 22:58:12 +0000242 outFace.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700243 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700244}
245
246void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000247Forwarder::onInterestReject(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700248{
Junxiao Shifef73e42016-03-29 14:15:05 -0700249 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700250 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
251 " cannot reject forwarded Interest");
252 return;
253 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700254 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700255
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700256 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000257 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700258
Junxiao Shid3c792f2014-01-30 00:46:13 -0700259 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700260 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700261}
262
263void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000264Forwarder::onInterestUnsatisfied(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700265{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700266 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
267
Junxiao Shid3c792f2014-01-30 00:46:13 -0700268 // invoke PIT unsatisfied callback
Spyridon Mastorakis01091f32014-12-05 22:43:34 -0800269 beforeExpirePendingInterest(*pitEntry);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000270 this->dispatchToStrategy(*pitEntry,
271 [&] (fw::Strategy& strategy) { strategy.beforeExpirePendingInterest(pitEntry); });
Junxiao Shic041ca32014-02-25 20:01:15 -0700272
Junxiao Shia110f262014-10-12 12:35:20 -0700273 // goto Interest Finalize pipeline
274 this->onInterestFinalize(pitEntry, false);
275}
276
277void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000278Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
Eric Newberryf4056d02017-05-26 17:31:53 +0000279 ndn::optional<time::milliseconds> dataFreshnessPeriod)
Junxiao Shia110f262014-10-12 12:35:20 -0700280{
281 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
282 (isSatisfied ? " satisfied" : " unsatisfied"));
283
284 // Dead Nonce List insert if necessary
285 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
286
Junxiao Shif3c07812014-03-11 21:48:49 -0700287 // PIT delete
Junxiao Shib9420cf2016-08-13 04:38:52 +0000288 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000289 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700290}
291
292void
293Forwarder::onIncomingData(Face& inFace, const Data& data)
294{
295 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700296 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000297 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700298 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700299
Junxiao Shi88884492014-02-15 15:57:43 -0700300 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700301 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700302 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700303 if (isViolatingLocalhost) {
304 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
305 " data=" << data.getName() << " violates /localhost");
306 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700307 return;
308 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700309
Junxiao Shid3c792f2014-01-30 00:46:13 -0700310 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700311 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
312 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700313 // goto Data unsolicited pipeline
314 this->onDataUnsolicited(inFace, data);
315 return;
316 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700317
Junxiao Shid3c792f2014-01-30 00:46:13 -0700318 // CS insert
Spyridon Mastorakis01091f32014-12-05 22:43:34 -0800319 if (m_csFromNdnSim == nullptr)
320 m_cs.insert(data);
321 else
322 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shic041ca32014-02-25 20:01:15 -0700323
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700324 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700325 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700326 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700327 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700328 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700329
Junxiao Shid3c792f2014-01-30 00:46:13 -0700330 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000331 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700332
Junxiao Shid3c792f2014-01-30 00:46:13 -0700333 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700334 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
335 if (inRecord.getExpiry() > now) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000336 pendingDownstreams.insert(&inRecord.getFace());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700337 }
338 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700339
Junxiao Shid938a6b2014-05-11 23:40:29 -0700340 // invoke PIT satisfy callback
Spyridon Mastorakis01091f32014-12-05 22:43:34 -0800341 beforeSatisfyInterest(*pitEntry, inFace, data);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000342 this->dispatchToStrategy(*pitEntry,
343 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700344
Junxiao Shi4846f372016-04-05 13:39:30 -0700345 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700346 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
347
Junxiao Shid3c792f2014-01-30 00:46:13 -0700348 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700349 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700350 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700351
Junxiao Shid3c792f2014-01-30 00:46:13 -0700352 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700353 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700354 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700355
Junxiao Shid3c792f2014-01-30 00:46:13 -0700356 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700357 for (Face* pendingDownstream : pendingDownstreams) {
Teng Liangf995f382017-04-04 22:09:39 +0000358 if (pendingDownstream->getId() == inFace.getId() &&
359 pendingDownstream->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Junxiao Shida006f52014-05-16 11:18:00 -0700360 continue;
361 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700362 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700363 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700364 }
365}
366
367void
368Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
369{
370 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000371 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
372 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700373 // CS insert
Spyridon Mastorakis01091f32014-12-05 22:43:34 -0800374 if (m_csFromNdnSim == nullptr)
375 m_cs.insert(data, true);
376 else
377 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700378 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700379
Junxiao Shif3c07812014-03-11 21:48:49 -0700380 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
381 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000382 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700383}
384
385void
386Forwarder::onOutgoingData(const Data& data, Face& outFace)
387{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700388 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700389 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
390 return;
391 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700392 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
393
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700394 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700395 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700396 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700397 if (isViolatingLocalhost) {
398 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
399 " data=" << data.getName() << " violates /localhost");
400 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700401 return;
402 }
403
Junxiao Shif3c07812014-03-11 21:48:49 -0700404 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700405
Junxiao Shid3c792f2014-01-30 00:46:13 -0700406 // send Data
407 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700408 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700409}
410
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700411void
412Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
413{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000414 // receive Nack
415 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700416 ++m_counters.nInNacks;
417
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700418 // if multi-access or ad hoc face, drop
419 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700420 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
421 " nack=" << nack.getInterest().getName() <<
422 "~" << nack.getReason() << " face-is-multi-access");
423 return;
424 }
425
426 // PIT match
427 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
428 // if no PIT entry found, drop
429 if (pitEntry == nullptr) {
430 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
431 " nack=" << nack.getInterest().getName() <<
432 "~" << nack.getReason() << " no-PIT-entry");
433 return;
434 }
435
436 // has out-record?
437 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
438 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700439 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700440 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
441 " nack=" << nack.getInterest().getName() <<
442 "~" << nack.getReason() << " no-out-record");
443 return;
444 }
445
446 // if out-record has different Nonce, drop
447 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
448 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
449 " nack=" << nack.getInterest().getName() <<
450 "~" << nack.getReason() << " wrong-Nonce " <<
451 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
452 return;
453 }
454
455 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
456 " nack=" << nack.getInterest().getName() <<
457 "~" << nack.getReason() << " OK");
458
459 // record Nack on out-record
460 outRecord->setIncomingNack(nack);
461
462 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000463 this->dispatchToStrategy(*pitEntry,
464 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700465}
466
467void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000468Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700469 const lp::NackHeader& nack)
470{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700471 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700472 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
473 " nack=" << pitEntry->getInterest().getName() <<
474 "~" << nack.getReason() << " no-in-record");
475 return;
476 }
477
478 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700479 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700480
481 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700482 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700483 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
484 " nack=" << pitEntry->getInterest().getName() <<
485 "~" << nack.getReason() << " no-in-record");
486 return;
487 }
488
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700489 // if multi-access or ad hoc face, drop
490 if (outFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700491 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
492 " nack=" << pitEntry->getInterest().getName() <<
493 "~" << nack.getReason() << " face-is-multi-access");
494 return;
495 }
496
497 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
498 " nack=" << pitEntry->getInterest().getName() <<
499 "~" << nack.getReason() << " OK");
500
501 // create Nack packet with the Interest from in-record
502 lp::Nack nackPkt(inRecord->getInterest());
503 nackPkt.setHeader(nack);
504
505 // erase in-record
506 pitEntry->deleteInRecord(outFace);
507
508 // send Nack on face
509 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700510 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700511}
512
Junxiao Shid3c792f2014-01-30 00:46:13 -0700513static inline bool
514compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
515{
516 return a.getExpiry() < b.getExpiry();
517}
518
519void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000520Forwarder::setUnsatisfyTimer(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700521{
Junxiao Shi4846f372016-04-05 13:39:30 -0700522 pit::InRecordCollection::iterator lastExpiring =
523 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700524
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700525 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700526 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
527 if (lastExpiryFromNow <= time::seconds::zero()) {
528 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700529 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700530
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700531 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700532 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700533 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
534}
535
536void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000537Forwarder::setStragglerTimer(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
Eric Newberryf4056d02017-05-26 17:31:53 +0000538 ndn::optional<time::milliseconds> dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700539{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700540 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700541
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700542 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700543 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700544 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700545}
546
547void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000548Forwarder::cancelUnsatisfyAndStragglerTimer(pit::Entry& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700549{
Junxiao Shib9420cf2016-08-13 04:38:52 +0000550 scheduler::cancel(pitEntry.m_unsatisfyTimer);
551 scheduler::cancel(pitEntry.m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700552}
553
Junxiao Shia110f262014-10-12 12:35:20 -0700554static inline void
555insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
556 const pit::OutRecord& outRecord)
557{
558 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
559}
560
561void
562Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
Eric Newberryf4056d02017-05-26 17:31:53 +0000563 ndn::optional<time::milliseconds> dataFreshnessPeriod, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700564{
565 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000566 bool needDnl = true;
Junxiao Shia110f262014-10-12 12:35:20 -0700567 if (isSatisfied) {
Eric Newberryf4056d02017-05-26 17:31:53 +0000568 BOOST_ASSERT(dataFreshnessPeriod);
569 BOOST_ASSERT(*dataFreshnessPeriod >= time::milliseconds::zero());
Junxiao Shia110f262014-10-12 12:35:20 -0700570 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Eric Newberryf4056d02017-05-26 17:31:53 +0000571 *dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700572 }
573
574 if (!needDnl) {
575 return;
576 }
577
578 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000579 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700580 // insert all outgoing Nonces
581 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
582 std::for_each(outRecords.begin(), outRecords.end(),
583 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
584 }
585 else {
586 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700587 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700588 if (outRecord != pitEntry.getOutRecords().end()) {
589 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
590 }
591 }
592}
593
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800594} // namespace nfd