blob: ecdce2e4e2ef1f0eb902549326f391af6c9dba27 [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
Junxiao Shid3c792f2014-01-30 00:46:13 -0700343 // CS insert
Spyridon Mastorakisd2b262a2014-12-05 22:43:34 -0800344 if (m_csFromNdnSim == nullptr)
345 m_cs.insert(data);
346 else
347 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shic041ca32014-02-25 20:01:15 -0700348
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700349 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700350 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700351 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700352 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700353 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700354
Junxiao Shid3c792f2014-01-30 00:46:13 -0700355 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000356 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700357
Junxiao Shid3c792f2014-01-30 00:46:13 -0700358 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700359 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
360 if (inRecord.getExpiry() > now) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000361 pendingDownstreams.insert(&inRecord.getFace());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700362 }
363 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700364
Junxiao Shid938a6b2014-05-11 23:40:29 -0700365 // invoke PIT satisfy callback
Alexander Afanasyev9b9669d2015-01-08 21:41:48 -0800366 beforeSatisfyInterest(*pitEntry, inFace, data);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000367 this->dispatchToStrategy(*pitEntry,
368 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700369
Junxiao Shi4846f372016-04-05 13:39:30 -0700370 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700371 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
372
Junxiao Shid3c792f2014-01-30 00:46:13 -0700373 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700374 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700375 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700376
Junxiao Shid3c792f2014-01-30 00:46:13 -0700377 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700378 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700379 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700380
Junxiao Shid3c792f2014-01-30 00:46:13 -0700381 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700382 for (Face* pendingDownstream : pendingDownstreams) {
383 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700384 continue;
385 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700386 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700387 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700388 }
389}
390
391void
392Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
393{
394 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000395 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
396 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700397 // CS insert
Spyridon Mastorakisd2b262a2014-12-05 22:43:34 -0800398 if (m_csFromNdnSim == nullptr)
399 m_cs.insert(data, true);
400 else
401 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700402 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700403
Junxiao Shif3c07812014-03-11 21:48:49 -0700404 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
405 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000406 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700407}
408
409void
410Forwarder::onOutgoingData(const Data& data, Face& outFace)
411{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700412 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700413 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
414 return;
415 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700416 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
417
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700418 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700419 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700420 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700421 if (isViolatingLocalhost) {
422 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
423 " data=" << data.getName() << " violates /localhost");
424 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700425 return;
426 }
427
Junxiao Shif3c07812014-03-11 21:48:49 -0700428 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700429
Junxiao Shid3c792f2014-01-30 00:46:13 -0700430 // send Data
431 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700432 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700433}
434
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700435void
436Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
437{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000438 // receive Nack
439 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700440 ++m_counters.nInNacks;
441
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700442 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700443 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700444 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
445 " nack=" << nack.getInterest().getName() <<
446 "~" << nack.getReason() << " face-is-multi-access");
447 return;
448 }
449
450 // PIT match
451 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
452 // if no PIT entry found, drop
453 if (pitEntry == nullptr) {
454 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
455 " nack=" << nack.getInterest().getName() <<
456 "~" << nack.getReason() << " no-PIT-entry");
457 return;
458 }
459
460 // has out-record?
461 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
462 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700463 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700464 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
465 " nack=" << nack.getInterest().getName() <<
466 "~" << nack.getReason() << " no-out-record");
467 return;
468 }
469
470 // if out-record has different Nonce, drop
471 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
472 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
473 " nack=" << nack.getInterest().getName() <<
474 "~" << nack.getReason() << " wrong-Nonce " <<
475 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
476 return;
477 }
478
479 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
480 " nack=" << nack.getInterest().getName() <<
481 "~" << nack.getReason() << " OK");
482
483 // record Nack on out-record
484 outRecord->setIncomingNack(nack);
485
486 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000487 this->dispatchToStrategy(*pitEntry,
488 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700489}
490
491void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000492Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700493 const lp::NackHeader& nack)
494{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700495 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700496 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
497 " nack=" << pitEntry->getInterest().getName() <<
498 "~" << nack.getReason() << " no-in-record");
499 return;
500 }
501
502 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700503 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700504
505 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700506 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700507 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
508 " nack=" << pitEntry->getInterest().getName() <<
509 "~" << nack.getReason() << " no-in-record");
510 return;
511 }
512
513 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700514 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700515 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
516 " nack=" << pitEntry->getInterest().getName() <<
517 "~" << nack.getReason() << " face-is-multi-access");
518 return;
519 }
520
521 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
522 " nack=" << pitEntry->getInterest().getName() <<
523 "~" << nack.getReason() << " OK");
524
525 // create Nack packet with the Interest from in-record
526 lp::Nack nackPkt(inRecord->getInterest());
527 nackPkt.setHeader(nack);
528
529 // erase in-record
530 pitEntry->deleteInRecord(outFace);
531
532 // send Nack on face
533 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700534 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700535}
536
Junxiao Shid3c792f2014-01-30 00:46:13 -0700537static inline bool
538compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
539{
540 return a.getExpiry() < b.getExpiry();
541}
542
543void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000544Forwarder::setUnsatisfyTimer(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700545{
Junxiao Shi4846f372016-04-05 13:39:30 -0700546 pit::InRecordCollection::iterator lastExpiring =
547 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700548
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700549 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700550 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
551 if (lastExpiryFromNow <= time::seconds::zero()) {
552 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700553 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700554
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700555 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700556 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700557 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
558}
559
560void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000561Forwarder::setStragglerTimer(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
562 time::milliseconds dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700563{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700564 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700565
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700566 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700567 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700568 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700569}
570
571void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000572Forwarder::cancelUnsatisfyAndStragglerTimer(pit::Entry& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700573{
Junxiao Shib9420cf2016-08-13 04:38:52 +0000574 scheduler::cancel(pitEntry.m_unsatisfyTimer);
575 scheduler::cancel(pitEntry.m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700576}
577
Junxiao Shia110f262014-10-12 12:35:20 -0700578static inline void
579insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
580 const pit::OutRecord& outRecord)
581{
582 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
583}
584
585void
586Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000587 time::milliseconds dataFreshnessPeriod, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700588{
589 // need Dead Nonce List insert?
590 bool needDnl = false;
591 if (isSatisfied) {
592 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
593 // Data never becomes stale if it doesn't have FreshnessPeriod field
594 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
595 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
596 }
597 else {
598 needDnl = true;
599 }
600
601 if (!needDnl) {
602 return;
603 }
604
605 // Dead Nonce List insert
606 if (upstream == 0) {
607 // insert all outgoing Nonces
608 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
609 std::for_each(outRecords.begin(), outRecords.end(),
610 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
611 }
612 else {
613 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700614 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700615 if (outRecord != pitEntry.getOutRecords().end()) {
616 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
617 }
618 }
619}
620
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800621} // namespace nfd