blob: 9e0b8bb0518bfe5d6489b11284568b74fe47f976 [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
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
Junxiao Shid3c792f2014-01-30 00:46:13 -070076Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
77{
78 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -070079 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
80 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +000081 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -070082 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -070083
Junxiao Shi88884492014-02-15 15:57:43 -070084 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -070085 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -070086 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -070087 if (isViolatingLocalhost) {
88 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
89 " interest=" << interest.getName() << " violates /localhost");
90 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -070091 return;
92 }
Junxiao Shic041ca32014-02-25 20:01:15 -070093
Junxiao Shi330136a2016-03-10 04:53:08 -070094 // detect duplicate Nonce with Dead Nonce List
95 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
96 if (hasDuplicateNonceInDnl) {
97 // goto Interest loop pipeline
98 this->onInterestLoop(inFace, interest);
99 return;
100 }
101
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000102 // strip forwarding hint if Interest has reached producer region
103 if (!interest.getForwardingHint().empty() &&
104 m_networkRegionTable.isInProducerRegion(interest.getForwardingHint())) {
Junxiao Shif9858fd2017-02-01 16:22:44 +0000105 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
106 " interest=" << interest.getName() << " reaching-producer-region");
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000107 const_cast<Interest&>(interest).setForwardingHint({});
Junxiao Shif9858fd2017-02-01 16:22:44 +0000108 }
109
Junxiao Shid3c792f2014-01-30 00:46:13 -0700110 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700111 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700112
Junxiao Shi330136a2016-03-10 04:53:08 -0700113 // detect duplicate Nonce in PIT entry
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000114 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace);
115 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
116 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
117 // for p2p face: duplicate Nonce from same incoming face is not loop
118 hasDuplicateNonceInPit = hasDuplicateNonceInPit && !(dnw & fw::DUPLICATE_NONCE_IN_SAME);
119 }
Junxiao Shi330136a2016-03-10 04:53:08 -0700120 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700121 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700122 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700123 return;
124 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700125
Junxiao Shid3c792f2014-01-30 00:46:13 -0700126 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000127 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700128
Junxiao Shif3c07812014-03-11 21:48:49 -0700129 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700130 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600131 m_cs.find(interest,
132 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
133 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700134 }
mzhang4eab72492015-02-25 11:16:09 -0600135 else {
136 this->onContentStoreMiss(inFace, pitEntry, interest);
137 }
138}
Junxiao Shic041ca32014-02-25 20:01:15 -0700139
mzhang4eab72492015-02-25 11:16:09 -0600140void
Junxiao Shi330136a2016-03-10 04:53:08 -0700141Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700142{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700143 // if multi-access or ad hoc face, drop
144 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700145 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
146 " interest=" << interest.getName() <<
147 " drop");
148 return;
149 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700150
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700151 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
152 " interest=" << interest.getName() <<
153 " send-Nack-duplicate");
154
155 // send Nack with reason=DUPLICATE
156 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
157 lp::Nack nack(interest);
158 nack.setReason(lp::NackReason::DUPLICATE);
159 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700160}
161
162void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000163Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
mzhang4eab72492015-02-25 11:16:09 -0600164 const Interest& interest)
165{
166 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
167
Junxiao Shi4846f372016-04-05 13:39:30 -0700168 // insert in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000169 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700170
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700171 // set PIT unsatisfy timer
172 this->setUnsatisfyTimer(pitEntry);
173
Junxiao Shie342e8d2016-09-18 16:48:00 +0000174 // has NextHopFaceId?
175 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
176 if (nextHopTag != nullptr) {
177 // chosen NextHop face exists?
178 Face* nextHopFace = m_faceTable.get(*nextHopTag);
179 if (nextHopFace != nullptr) {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000180 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName() << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000181 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000182 // scope control is unnecessary, because privileged app explicitly wants to forward
183 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000184 }
185 return;
186 }
187
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000188 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000189 this->dispatchToStrategy(*pitEntry,
190 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700191}
192
193void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000194Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
195 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600196{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500197 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600198
Junxiao Shicde37ad2015-12-24 01:02:05 -0700199 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600200 // XXX should we lookup PIT for other Interests that also match csMatch?
201
202 // set PIT straggler timer
203 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
204
205 // goto outgoing Data pipeline
206 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
207}
208
Junxiao Shid3c792f2014-01-30 00:46:13 -0700209void
Junxiao Shic5f651f2016-11-17 22:58:12 +0000210Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700211{
Junxiao Shif3c07812014-03-11 21:48:49 -0700212 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
213 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700214
Junxiao Shi4846f372016-04-05 13:39:30 -0700215 // insert out-record
Junxiao Shic5f651f2016-11-17 22:58:12 +0000216 pitEntry->insertOrUpdateOutRecord(outFace, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700217
Junxiao Shid3c792f2014-01-30 00:46:13 -0700218 // send Interest
Junxiao Shic5f651f2016-11-17 22:58:12 +0000219 outFace.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700220 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700221}
222
223void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000224Forwarder::onInterestReject(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700225{
Junxiao Shifef73e42016-03-29 14:15:05 -0700226 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700227 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
228 " cannot reject forwarded Interest");
229 return;
230 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700231 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700232
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700233 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000234 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700235
Junxiao Shid3c792f2014-01-30 00:46:13 -0700236 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700237 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700238}
239
240void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000241Forwarder::onInterestUnsatisfied(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700242{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700243 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
244
Junxiao Shid3c792f2014-01-30 00:46:13 -0700245 // invoke PIT unsatisfied callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000246 this->dispatchToStrategy(*pitEntry,
247 [&] (fw::Strategy& strategy) { strategy.beforeExpirePendingInterest(pitEntry); });
Junxiao Shic041ca32014-02-25 20:01:15 -0700248
Junxiao Shia110f262014-10-12 12:35:20 -0700249 // goto Interest Finalize pipeline
250 this->onInterestFinalize(pitEntry, false);
251}
252
253void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000254Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
Eric Newberryf4056d02017-05-26 17:31:53 +0000255 ndn::optional<time::milliseconds> dataFreshnessPeriod)
Junxiao Shia110f262014-10-12 12:35:20 -0700256{
257 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
258 (isSatisfied ? " satisfied" : " unsatisfied"));
259
260 // Dead Nonce List insert if necessary
261 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
262
Junxiao Shif3c07812014-03-11 21:48:49 -0700263 // PIT delete
Junxiao Shib9420cf2016-08-13 04:38:52 +0000264 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000265 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700266}
267
268void
269Forwarder::onIncomingData(Face& inFace, const Data& data)
270{
271 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700272 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000273 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700274 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700275
Junxiao Shi88884492014-02-15 15:57:43 -0700276 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700277 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700278 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700279 if (isViolatingLocalhost) {
280 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
281 " data=" << data.getName() << " violates /localhost");
282 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700283 return;
284 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700285
Junxiao Shid3c792f2014-01-30 00:46:13 -0700286 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700287 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
288 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700289 // goto Data unsolicited pipeline
290 this->onDataUnsolicited(inFace, data);
291 return;
292 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700293
Junxiao Shid3c792f2014-01-30 00:46:13 -0700294 // CS insert
295 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700296
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700297 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700298 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700299 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700300 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700301 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700302
Junxiao Shid3c792f2014-01-30 00:46:13 -0700303 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000304 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700305
Junxiao Shid3c792f2014-01-30 00:46:13 -0700306 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700307 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
308 if (inRecord.getExpiry() > now) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000309 pendingDownstreams.insert(&inRecord.getFace());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700310 }
311 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700312
Junxiao Shid938a6b2014-05-11 23:40:29 -0700313 // invoke PIT satisfy callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000314 this->dispatchToStrategy(*pitEntry,
315 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700316
Junxiao Shi4846f372016-04-05 13:39:30 -0700317 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700318 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
319
Junxiao Shid3c792f2014-01-30 00:46:13 -0700320 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700321 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700322 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700323
Junxiao Shid3c792f2014-01-30 00:46:13 -0700324 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700325 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700326 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700327
Junxiao Shid3c792f2014-01-30 00:46:13 -0700328 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700329 for (Face* pendingDownstream : pendingDownstreams) {
Teng Liangf995f382017-04-04 22:09:39 +0000330 if (pendingDownstream->getId() == inFace.getId() &&
331 pendingDownstream->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Junxiao Shida006f52014-05-16 11:18:00 -0700332 continue;
333 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700334 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700335 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700336 }
337}
338
339void
340Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
341{
342 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000343 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
344 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700345 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700346 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700347 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700348
Junxiao Shif3c07812014-03-11 21:48:49 -0700349 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
350 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000351 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700352}
353
354void
355Forwarder::onOutgoingData(const Data& data, Face& outFace)
356{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700357 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700358 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
359 return;
360 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700361 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
362
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700363 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700364 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700365 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700366 if (isViolatingLocalhost) {
367 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
368 " data=" << data.getName() << " violates /localhost");
369 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700370 return;
371 }
372
Junxiao Shif3c07812014-03-11 21:48:49 -0700373 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700374
Junxiao Shid3c792f2014-01-30 00:46:13 -0700375 // send Data
376 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700377 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700378}
379
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700380void
381Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
382{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000383 // receive Nack
384 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700385 ++m_counters.nInNacks;
386
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700387 // if multi-access or ad hoc face, drop
388 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700389 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
390 " nack=" << nack.getInterest().getName() <<
391 "~" << nack.getReason() << " face-is-multi-access");
392 return;
393 }
394
395 // PIT match
396 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
397 // if no PIT entry found, drop
398 if (pitEntry == nullptr) {
399 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
400 " nack=" << nack.getInterest().getName() <<
401 "~" << nack.getReason() << " no-PIT-entry");
402 return;
403 }
404
405 // has out-record?
406 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
407 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700408 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700409 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
410 " nack=" << nack.getInterest().getName() <<
411 "~" << nack.getReason() << " no-out-record");
412 return;
413 }
414
415 // if out-record has different Nonce, drop
416 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
417 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
418 " nack=" << nack.getInterest().getName() <<
419 "~" << nack.getReason() << " wrong-Nonce " <<
420 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
421 return;
422 }
423
424 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
425 " nack=" << nack.getInterest().getName() <<
426 "~" << nack.getReason() << " OK");
427
428 // record Nack on out-record
429 outRecord->setIncomingNack(nack);
430
431 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000432 this->dispatchToStrategy(*pitEntry,
433 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700434}
435
436void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000437Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700438 const lp::NackHeader& nack)
439{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700440 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700441 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
442 " nack=" << pitEntry->getInterest().getName() <<
443 "~" << nack.getReason() << " no-in-record");
444 return;
445 }
446
447 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700448 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700449
450 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700451 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700452 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
453 " nack=" << pitEntry->getInterest().getName() <<
454 "~" << nack.getReason() << " no-in-record");
455 return;
456 }
457
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700458 // if multi-access or ad hoc face, drop
459 if (outFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700460 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
461 " nack=" << pitEntry->getInterest().getName() <<
462 "~" << nack.getReason() << " face-is-multi-access");
463 return;
464 }
465
466 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
467 " nack=" << pitEntry->getInterest().getName() <<
468 "~" << nack.getReason() << " OK");
469
470 // create Nack packet with the Interest from in-record
471 lp::Nack nackPkt(inRecord->getInterest());
472 nackPkt.setHeader(nack);
473
474 // erase in-record
475 pitEntry->deleteInRecord(outFace);
476
477 // send Nack on face
478 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700479 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700480}
481
Junxiao Shid3c792f2014-01-30 00:46:13 -0700482static inline bool
483compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
484{
485 return a.getExpiry() < b.getExpiry();
486}
487
488void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000489Forwarder::setUnsatisfyTimer(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700490{
Junxiao Shi4846f372016-04-05 13:39:30 -0700491 pit::InRecordCollection::iterator lastExpiring =
492 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700493
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700494 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700495 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
496 if (lastExpiryFromNow <= time::seconds::zero()) {
497 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700498 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700499
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700500 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700501 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700502 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
503}
504
505void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000506Forwarder::setStragglerTimer(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
Eric Newberryf4056d02017-05-26 17:31:53 +0000507 ndn::optional<time::milliseconds> dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700508{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700509 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700510
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700511 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700512 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700513 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700514}
515
516void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000517Forwarder::cancelUnsatisfyAndStragglerTimer(pit::Entry& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700518{
Junxiao Shib9420cf2016-08-13 04:38:52 +0000519 scheduler::cancel(pitEntry.m_unsatisfyTimer);
520 scheduler::cancel(pitEntry.m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700521}
522
Junxiao Shia110f262014-10-12 12:35:20 -0700523static inline void
524insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
525 const pit::OutRecord& outRecord)
526{
527 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
528}
529
530void
531Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
Eric Newberryf4056d02017-05-26 17:31:53 +0000532 ndn::optional<time::milliseconds> dataFreshnessPeriod, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700533{
534 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000535 bool needDnl = true;
Junxiao Shia110f262014-10-12 12:35:20 -0700536 if (isSatisfied) {
Eric Newberryf4056d02017-05-26 17:31:53 +0000537 BOOST_ASSERT(dataFreshnessPeriod);
538 BOOST_ASSERT(*dataFreshnessPeriod >= time::milliseconds::zero());
Junxiao Shia110f262014-10-12 12:35:20 -0700539 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Eric Newberryf4056d02017-05-26 17:31:53 +0000540 *dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700541 }
542
543 if (!needDnl) {
544 return;
545 }
546
547 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000548 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700549 // insert all outgoing Nonces
550 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
551 std::for_each(outRecords.begin(), outRecords.end(),
552 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
553 }
554 else {
555 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700556 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700557 if (outRecord != pitEntry.getOutRecords().end()) {
558 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
559 }
560 }
561}
562
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800563} // namespace nfd