blob: b35a321ac19411238169c6426ab2473948161d01 [file] [log] [blame]
Alexander Afanasyev33b72772014-01-26 23:22:58 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi330136a2016-03-10 04:53:08 -07003 * Copyright (c) 2014-2016, 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"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060028#include "core/logger.hpp"
Junxiao Shifaf3eb02015-02-16 10:50:36 -070029#include "strategy.hpp"
Junxiao Shi02b73f52016-07-28 01:48:27 +000030#include "table/cleanup.hpp"
Junxiao Shicbc8e942016-09-06 03:17:45 +000031#include <ndn-cxx/lp/tags.hpp>
Alexander Afanasyev9b9669d2015-01-08 21:41:48 -080032#include "face/null-face.hpp"
33#include <boost/random/uniform_int_distribution.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080034
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080035namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080036
Junxiao Shi8c8d2182014-01-30 22:33:00 -070037NFD_LOG_INIT("Forwarder");
38
Junxiao Shic041ca32014-02-25 20:01:15 -070039Forwarder::Forwarder()
Junxiao Shi9685cc52016-08-29 12:47:05 +000040 : m_unsolicitedDataPolicy(new fw::DefaultUnsolicitedDataPolicy())
Junxiao Shifbe8efe2016-08-22 16:02:30 +000041 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060042 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080043 , m_measurements(m_nameTree)
Junxiao Shif3c07812014-03-11 21:48:49 -070044 , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this))
Alexander Afanasyev9b9669d2015-01-08 21:41:48 -080045 , m_csFace(face::makeNullFace(FaceUri("contentstore://")))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080046{
Junxiao Shif3c07812014-03-11 21:48:49 -070047 fw::installStrategies(*this);
Alexander Afanasyev9b9669d2015-01-08 21:41:48 -080048 getFaceTable().addReserved(m_csFace, face::FACEID_CONTENT_STORE);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000049
50 m_faceTable.afterAdd.connect([this] (Face& face) {
51 face.afterReceiveInterest.connect(
52 [this, &face] (const Interest& interest) {
53 this->startProcessInterest(face, interest);
54 });
55 face.afterReceiveData.connect(
56 [this, &face] (const Data& data) {
57 this->startProcessData(face, data);
58 });
59 face.afterReceiveNack.connect(
60 [this, &face] (const lp::Nack& nack) {
61 this->startProcessNack(face, nack);
62 });
63 });
64
65 m_faceTable.beforeRemove.connect([this] (Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000066 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000067 });
Alexander Afanasyev33b72772014-01-26 23:22:58 -080068}
69
Junxiao Shidcffdaa2016-07-26 02:23:56 +000070Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060071
Junxiao Shi0355e9f2015-09-02 07:24:53 -070072void
73Forwarder::startProcessInterest(Face& face, const Interest& interest)
74{
75 // check fields used by forwarding are well-formed
76 try {
77 if (interest.hasLink()) {
78 interest.getLink();
79 }
80 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -070081 catch (const tlv::Error&) {
Junxiao Shi0355e9f2015-09-02 07:24:53 -070082 NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() <<
83 " interest=" << interest.getName() << " malformed");
84 // It's safe to call interest.getName() because Name has been fully parsed
85 return;
86 }
87
88 this->onIncomingInterest(face, interest);
89}
90
91void
92Forwarder::startProcessData(Face& face, const Data& data)
93{
94 // check fields used by forwarding are well-formed
95 // (none needed)
96
97 this->onIncomingData(face, data);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060098}
99
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800100void
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700101Forwarder::startProcessNack(Face& face, const lp::Nack& nack)
102{
103 // check fields used by forwarding are well-formed
104 try {
105 if (nack.getInterest().hasLink()) {
106 nack.getInterest().getLink();
107 }
108 }
109 catch (const tlv::Error&) {
110 NFD_LOG_DEBUG("startProcessNack face=" << face.getId() <<
111 " nack=" << nack.getInterest().getName() <<
112 "~" << nack.getReason() << " malformed");
113 return;
114 }
115
116 this->onIncomingNack(face, nack);
117}
118
119void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700120Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
121{
122 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700123 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
124 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000125 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700126 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -0700127
Junxiao Shi88884492014-02-15 15:57:43 -0700128 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700129 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700130 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700131 if (isViolatingLocalhost) {
132 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
133 " interest=" << interest.getName() << " violates /localhost");
134 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700135 return;
136 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700137
Junxiao Shi330136a2016-03-10 04:53:08 -0700138 // detect duplicate Nonce with Dead Nonce List
139 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
140 if (hasDuplicateNonceInDnl) {
141 // goto Interest loop pipeline
142 this->onInterestLoop(inFace, interest);
143 return;
144 }
145
Junxiao Shid3c792f2014-01-30 00:46:13 -0700146 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700147 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700148
Junxiao Shi330136a2016-03-10 04:53:08 -0700149 // detect duplicate Nonce in PIT entry
Junxiao Shifef73e42016-03-29 14:15:05 -0700150 bool hasDuplicateNonceInPit = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace) !=
151 fw::DUPLICATE_NONCE_NONE;
Junxiao Shi330136a2016-03-10 04:53:08 -0700152 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700153 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700154 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700155 return;
156 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700157
Junxiao Shid3c792f2014-01-30 00:46:13 -0700158 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000159 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700160
Spyridon Mastorakisd2b262a2014-12-05 22:43:34 -0800161 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
162 bool isPending = inRecords.begin() != inRecords.end();
163 if (!isPending) {
164 if (m_csFromNdnSim == nullptr) {
165 m_cs.find(interest,
166 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
167 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
168 }
169 else {
170 shared_ptr<Data> match = m_csFromNdnSim->Lookup(interest.shared_from_this());
171 if (match != nullptr) {
172 this->onContentStoreHit(inFace, pitEntry, interest, *match);
173 }
174 else {
175 this->onContentStoreMiss(inFace, pitEntry, interest);
176 }
177 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700178 }
mzhang4eab72492015-02-25 11:16:09 -0600179 else {
180 this->onContentStoreMiss(inFace, pitEntry, interest);
181 }
182}
Junxiao Shic041ca32014-02-25 20:01:15 -0700183
mzhang4eab72492015-02-25 11:16:09 -0600184void
Junxiao Shi330136a2016-03-10 04:53:08 -0700185Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700186{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700187 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700188 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700189 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
190 " interest=" << interest.getName() <<
191 " drop");
192 return;
193 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700194
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700195 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
196 " interest=" << interest.getName() <<
197 " send-Nack-duplicate");
198
199 // send Nack with reason=DUPLICATE
200 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
201 lp::Nack nack(interest);
202 nack.setReason(lp::NackReason::DUPLICATE);
203 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700204}
205
206void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000207Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
mzhang4eab72492015-02-25 11:16:09 -0600208 const Interest& interest)
209{
210 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
211
Junxiao Shi4846f372016-04-05 13:39:30 -0700212 // insert in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000213 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700214
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700215 // set PIT unsatisfy timer
216 this->setUnsatisfyTimer(pitEntry);
217
Junxiao Shie342e8d2016-09-18 16:48:00 +0000218 // has NextHopFaceId?
219 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
220 if (nextHopTag != nullptr) {
221 // chosen NextHop face exists?
222 Face* nextHopFace = m_faceTable.get(*nextHopTag);
223 if (nextHopFace != nullptr) {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000224 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName() << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000225 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000226 // scope control is unnecessary, because privileged app explicitly wants to forward
227 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000228 }
229 return;
230 }
231
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000232 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000233 this->dispatchToStrategy(*pitEntry,
234 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700235}
236
237void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000238Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
239 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600240{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500241 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600242
Alexander Afanasyev9b9669d2015-01-08 21:41:48 -0800243 beforeSatisfyInterest(*pitEntry, *m_csFace, data);
244 this->dispatchToStrategy(*pitEntry,
245 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, *m_csFace, data); });
246
Junxiao Shicde37ad2015-12-24 01:02:05 -0700247 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600248 // XXX should we lookup PIT for other Interests that also match csMatch?
249
250 // set PIT straggler timer
251 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
252
253 // goto outgoing Data pipeline
254 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
255}
256
Junxiao Shid3c792f2014-01-30 00:46:13 -0700257void
Junxiao Shic5f651f2016-11-17 22:58:12 +0000258Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700259{
Junxiao Shif3c07812014-03-11 21:48:49 -0700260 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
261 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700262
Junxiao Shi4846f372016-04-05 13:39:30 -0700263 // insert out-record
Junxiao Shic5f651f2016-11-17 22:58:12 +0000264 pitEntry->insertOrUpdateOutRecord(outFace, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700265
Junxiao Shid3c792f2014-01-30 00:46:13 -0700266 // send Interest
Junxiao Shic5f651f2016-11-17 22:58:12 +0000267 outFace.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700268 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700269}
270
271void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000272Forwarder::onInterestReject(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700273{
Junxiao Shifef73e42016-03-29 14:15:05 -0700274 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700275 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
276 " cannot reject forwarded Interest");
277 return;
278 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700279 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700280
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700281 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000282 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700283
Junxiao Shid3c792f2014-01-30 00:46:13 -0700284 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700285 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700286}
287
288void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000289Forwarder::onInterestUnsatisfied(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700290{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700291 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
292
Junxiao Shid3c792f2014-01-30 00:46:13 -0700293 // invoke PIT unsatisfied callback
Alexander Afanasyev9b9669d2015-01-08 21:41:48 -0800294 beforeExpirePendingInterest(*pitEntry);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000295 this->dispatchToStrategy(*pitEntry,
296 [&] (fw::Strategy& strategy) { strategy.beforeExpirePendingInterest(pitEntry); });
Junxiao Shic041ca32014-02-25 20:01:15 -0700297
Junxiao Shia110f262014-10-12 12:35:20 -0700298 // goto Interest Finalize pipeline
299 this->onInterestFinalize(pitEntry, false);
300}
301
302void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000303Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
304 time::milliseconds dataFreshnessPeriod)
Junxiao Shia110f262014-10-12 12:35:20 -0700305{
306 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
307 (isSatisfied ? " satisfied" : " unsatisfied"));
308
309 // Dead Nonce List insert if necessary
310 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
311
Junxiao Shif3c07812014-03-11 21:48:49 -0700312 // PIT delete
Junxiao Shib9420cf2016-08-13 04:38:52 +0000313 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000314 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700315}
316
317void
318Forwarder::onIncomingData(Face& inFace, const Data& data)
319{
320 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700321 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000322 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700323 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700324
Junxiao Shi88884492014-02-15 15:57:43 -0700325 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700326 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700327 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700328 if (isViolatingLocalhost) {
329 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
330 " data=" << data.getName() << " violates /localhost");
331 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700332 return;
333 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700334
Junxiao Shid3c792f2014-01-30 00:46:13 -0700335 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700336 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
337 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700338 // goto Data unsolicited pipeline
339 this->onDataUnsolicited(inFace, data);
340 return;
341 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700342
Spyridon Mastorakis0620cfb2016-12-06 16:32:19 -0800343 shared_ptr<Data> dataCopyWithoutTag = make_shared<Data>(data);
344 dataCopyWithoutTag->removeTag<lp::HopCountTag>();
345
Junxiao Shid3c792f2014-01-30 00:46:13 -0700346 // CS insert
Spyridon Mastorakisd2b262a2014-12-05 22:43:34 -0800347 if (m_csFromNdnSim == nullptr)
Spyridon Mastorakis0620cfb2016-12-06 16:32:19 -0800348 m_cs.insert(*dataCopyWithoutTag);
Spyridon Mastorakisd2b262a2014-12-05 22:43:34 -0800349 else
Spyridon Mastorakis0620cfb2016-12-06 16:32:19 -0800350 m_csFromNdnSim->Add(dataCopyWithoutTag);
Junxiao Shic041ca32014-02-25 20:01:15 -0700351
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700352 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700353 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700354 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700355 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700356 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700357
Junxiao Shid3c792f2014-01-30 00:46:13 -0700358 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000359 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700360
Junxiao Shid3c792f2014-01-30 00:46:13 -0700361 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700362 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
363 if (inRecord.getExpiry() > now) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000364 pendingDownstreams.insert(&inRecord.getFace());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700365 }
366 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700367
Junxiao Shid938a6b2014-05-11 23:40:29 -0700368 // invoke PIT satisfy callback
Alexander Afanasyev9b9669d2015-01-08 21:41:48 -0800369 beforeSatisfyInterest(*pitEntry, inFace, data);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000370 this->dispatchToStrategy(*pitEntry,
371 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700372
Junxiao Shi4846f372016-04-05 13:39:30 -0700373 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700374 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
375
Junxiao Shid3c792f2014-01-30 00:46:13 -0700376 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700377 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700378 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700379
Junxiao Shid3c792f2014-01-30 00:46:13 -0700380 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700381 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700382 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700383
Junxiao Shid3c792f2014-01-30 00:46:13 -0700384 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700385 for (Face* pendingDownstream : pendingDownstreams) {
386 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700387 continue;
388 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700389 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700390 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700391 }
392}
393
394void
395Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
396{
397 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000398 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
399 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700400 // CS insert
Spyridon Mastorakisd2b262a2014-12-05 22:43:34 -0800401 if (m_csFromNdnSim == nullptr)
402 m_cs.insert(data, true);
403 else
404 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700405 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700406
Junxiao Shif3c07812014-03-11 21:48:49 -0700407 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
408 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000409 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700410}
411
412void
413Forwarder::onOutgoingData(const Data& data, Face& outFace)
414{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700415 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700416 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
417 return;
418 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700419 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
420
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700421 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700422 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700423 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700424 if (isViolatingLocalhost) {
425 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
426 " data=" << data.getName() << " violates /localhost");
427 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700428 return;
429 }
430
Junxiao Shif3c07812014-03-11 21:48:49 -0700431 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700432
Junxiao Shid3c792f2014-01-30 00:46:13 -0700433 // send Data
434 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700435 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700436}
437
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700438void
439Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
440{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000441 // receive Nack
442 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700443 ++m_counters.nInNacks;
444
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700445 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700446 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700447 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
448 " nack=" << nack.getInterest().getName() <<
449 "~" << nack.getReason() << " face-is-multi-access");
450 return;
451 }
452
453 // PIT match
454 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
455 // if no PIT entry found, drop
456 if (pitEntry == nullptr) {
457 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
458 " nack=" << nack.getInterest().getName() <<
459 "~" << nack.getReason() << " no-PIT-entry");
460 return;
461 }
462
463 // has out-record?
464 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
465 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700466 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700467 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
468 " nack=" << nack.getInterest().getName() <<
469 "~" << nack.getReason() << " no-out-record");
470 return;
471 }
472
473 // if out-record has different Nonce, drop
474 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
475 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
476 " nack=" << nack.getInterest().getName() <<
477 "~" << nack.getReason() << " wrong-Nonce " <<
478 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
479 return;
480 }
481
482 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
483 " nack=" << nack.getInterest().getName() <<
484 "~" << nack.getReason() << " OK");
485
486 // record Nack on out-record
487 outRecord->setIncomingNack(nack);
488
489 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000490 this->dispatchToStrategy(*pitEntry,
491 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700492}
493
494void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000495Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700496 const lp::NackHeader& nack)
497{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700498 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700499 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
500 " nack=" << pitEntry->getInterest().getName() <<
501 "~" << nack.getReason() << " no-in-record");
502 return;
503 }
504
505 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700506 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700507
508 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700509 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700510 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
511 " nack=" << pitEntry->getInterest().getName() <<
512 "~" << nack.getReason() << " no-in-record");
513 return;
514 }
515
516 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700517 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700518 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
519 " nack=" << pitEntry->getInterest().getName() <<
520 "~" << nack.getReason() << " face-is-multi-access");
521 return;
522 }
523
524 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
525 " nack=" << pitEntry->getInterest().getName() <<
526 "~" << nack.getReason() << " OK");
527
528 // create Nack packet with the Interest from in-record
529 lp::Nack nackPkt(inRecord->getInterest());
530 nackPkt.setHeader(nack);
531
532 // erase in-record
533 pitEntry->deleteInRecord(outFace);
534
535 // send Nack on face
536 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700537 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700538}
539
Junxiao Shid3c792f2014-01-30 00:46:13 -0700540static inline bool
541compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
542{
543 return a.getExpiry() < b.getExpiry();
544}
545
546void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000547Forwarder::setUnsatisfyTimer(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700548{
Junxiao Shi4846f372016-04-05 13:39:30 -0700549 pit::InRecordCollection::iterator lastExpiring =
550 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700551
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700552 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700553 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
554 if (lastExpiryFromNow <= time::seconds::zero()) {
555 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700556 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700557
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700558 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700559 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700560 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
561}
562
563void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000564Forwarder::setStragglerTimer(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
565 time::milliseconds dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700566{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700567 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700568
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700569 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700570 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700571 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700572}
573
574void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000575Forwarder::cancelUnsatisfyAndStragglerTimer(pit::Entry& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700576{
Junxiao Shib9420cf2016-08-13 04:38:52 +0000577 scheduler::cancel(pitEntry.m_unsatisfyTimer);
578 scheduler::cancel(pitEntry.m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700579}
580
Junxiao Shia110f262014-10-12 12:35:20 -0700581static inline void
582insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
583 const pit::OutRecord& outRecord)
584{
585 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
586}
587
588void
589Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000590 time::milliseconds dataFreshnessPeriod, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700591{
592 // need Dead Nonce List insert?
593 bool needDnl = false;
594 if (isSatisfied) {
595 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
596 // Data never becomes stale if it doesn't have FreshnessPeriod field
597 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
598 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
599 }
600 else {
601 needDnl = true;
602 }
603
604 if (!needDnl) {
605 return;
606 }
607
608 // Dead Nonce List insert
609 if (upstream == 0) {
610 // insert all outgoing Nonces
611 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
612 std::for_each(outRecords.begin(), outRecords.end(),
613 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
614 }
615 else {
616 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700617 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700618 if (outRecord != pitEntry.getOutRecords().end()) {
619 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
620 }
621 }
622}
623
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800624} // namespace nfd