blob: 76e412779ca33f391f222c4dec75a00af19f2327 [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"
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
76Forwarder::startProcessInterest(Face& face, const Interest& interest)
77{
78 // check fields used by forwarding are well-formed
79 try {
80 if (interest.hasLink()) {
81 interest.getLink();
82 }
83 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -070084 catch (const tlv::Error&) {
Junxiao Shi0355e9f2015-09-02 07:24:53 -070085 NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() <<
86 " interest=" << interest.getName() << " malformed");
87 // It's safe to call interest.getName() because Name has been fully parsed
88 return;
89 }
90
91 this->onIncomingInterest(face, interest);
92}
93
94void
95Forwarder::startProcessData(Face& face, const Data& data)
96{
97 // check fields used by forwarding are well-formed
98 // (none needed)
99
100 this->onIncomingData(face, data);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600101}
102
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800103void
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700104Forwarder::startProcessNack(Face& face, const lp::Nack& nack)
105{
106 // check fields used by forwarding are well-formed
107 try {
108 if (nack.getInterest().hasLink()) {
109 nack.getInterest().getLink();
110 }
111 }
112 catch (const tlv::Error&) {
113 NFD_LOG_DEBUG("startProcessNack face=" << face.getId() <<
114 " nack=" << nack.getInterest().getName() <<
115 "~" << nack.getReason() << " malformed");
116 return;
117 }
118
119 this->onIncomingNack(face, nack);
120}
121
122void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700123Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
124{
125 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700126 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
127 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000128 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700129 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -0700130
Junxiao Shi88884492014-02-15 15:57:43 -0700131 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700132 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700133 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700134 if (isViolatingLocalhost) {
135 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
136 " interest=" << interest.getName() << " violates /localhost");
137 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700138 return;
139 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700140
Junxiao Shi330136a2016-03-10 04:53:08 -0700141 // detect duplicate Nonce with Dead Nonce List
142 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
143 if (hasDuplicateNonceInDnl) {
144 // goto Interest loop pipeline
145 this->onInterestLoop(inFace, interest);
146 return;
147 }
148
Junxiao Shid3c792f2014-01-30 00:46:13 -0700149 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700150 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700151
Junxiao Shi330136a2016-03-10 04:53:08 -0700152 // detect duplicate Nonce in PIT entry
Junxiao Shifef73e42016-03-29 14:15:05 -0700153 bool hasDuplicateNonceInPit = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace) !=
154 fw::DUPLICATE_NONCE_NONE;
Junxiao Shi330136a2016-03-10 04:53:08 -0700155 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700156 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700157 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700158 return;
159 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700160
Junxiao Shid3c792f2014-01-30 00:46:13 -0700161 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000162 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700163
Junxiao Shif3c07812014-03-11 21:48:49 -0700164 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700165 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600166 m_cs.find(interest,
167 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
168 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700169 }
mzhang4eab72492015-02-25 11:16:09 -0600170 else {
171 this->onContentStoreMiss(inFace, pitEntry, interest);
172 }
173}
Junxiao Shic041ca32014-02-25 20:01:15 -0700174
mzhang4eab72492015-02-25 11:16:09 -0600175void
Junxiao Shi330136a2016-03-10 04:53:08 -0700176Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700177{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700178 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700179 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700180 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
181 " interest=" << interest.getName() <<
182 " drop");
183 return;
184 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700185
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700186 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
187 " interest=" << interest.getName() <<
188 " send-Nack-duplicate");
189
190 // send Nack with reason=DUPLICATE
191 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
192 lp::Nack nack(interest);
193 nack.setReason(lp::NackReason::DUPLICATE);
194 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700195}
196
197void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000198Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
mzhang4eab72492015-02-25 11:16:09 -0600199 const Interest& interest)
200{
201 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
202
Junxiao Shi4846f372016-04-05 13:39:30 -0700203 // insert in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000204 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700205
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700206 // set PIT unsatisfy timer
207 this->setUnsatisfyTimer(pitEntry);
208
Junxiao Shie342e8d2016-09-18 16:48:00 +0000209 // has NextHopFaceId?
210 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
211 if (nextHopTag != nullptr) {
212 // chosen NextHop face exists?
213 Face* nextHopFace = m_faceTable.get(*nextHopTag);
214 if (nextHopFace != nullptr) {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000215 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName() << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000216 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000217 // scope control is unnecessary, because privileged app explicitly wants to forward
218 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000219 }
220 return;
221 }
222
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000223 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000224 this->dispatchToStrategy(*pitEntry,
225 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700226}
227
228void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000229Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
230 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600231{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500232 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600233
Junxiao Shicde37ad2015-12-24 01:02:05 -0700234 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600235 // XXX should we lookup PIT for other Interests that also match csMatch?
236
237 // set PIT straggler timer
238 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
239
240 // goto outgoing Data pipeline
241 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
242}
243
Junxiao Shid3c792f2014-01-30 00:46:13 -0700244void
Junxiao Shic5f651f2016-11-17 22:58:12 +0000245Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700246{
Junxiao Shif3c07812014-03-11 21:48:49 -0700247 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
248 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700249
Junxiao Shi4846f372016-04-05 13:39:30 -0700250 // insert out-record
Junxiao Shic5f651f2016-11-17 22:58:12 +0000251 pitEntry->insertOrUpdateOutRecord(outFace, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700252
Junxiao Shid3c792f2014-01-30 00:46:13 -0700253 // send Interest
Junxiao Shic5f651f2016-11-17 22:58:12 +0000254 outFace.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700255 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700256}
257
258void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000259Forwarder::onInterestReject(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700260{
Junxiao Shifef73e42016-03-29 14:15:05 -0700261 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700262 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
263 " cannot reject forwarded Interest");
264 return;
265 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700266 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700267
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700268 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000269 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700270
Junxiao Shid3c792f2014-01-30 00:46:13 -0700271 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700272 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700273}
274
275void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000276Forwarder::onInterestUnsatisfied(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700277{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700278 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
279
Junxiao Shid3c792f2014-01-30 00:46:13 -0700280 // invoke PIT unsatisfied callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000281 this->dispatchToStrategy(*pitEntry,
282 [&] (fw::Strategy& strategy) { strategy.beforeExpirePendingInterest(pitEntry); });
Junxiao Shic041ca32014-02-25 20:01:15 -0700283
Junxiao Shia110f262014-10-12 12:35:20 -0700284 // goto Interest Finalize pipeline
285 this->onInterestFinalize(pitEntry, false);
286}
287
288void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000289Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
290 time::milliseconds dataFreshnessPeriod)
Junxiao Shia110f262014-10-12 12:35:20 -0700291{
292 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
293 (isSatisfied ? " satisfied" : " unsatisfied"));
294
295 // Dead Nonce List insert if necessary
296 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
297
Junxiao Shif3c07812014-03-11 21:48:49 -0700298 // PIT delete
Junxiao Shib9420cf2016-08-13 04:38:52 +0000299 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000300 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700301}
302
303void
304Forwarder::onIncomingData(Face& inFace, const Data& data)
305{
306 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700307 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000308 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700309 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700310
Junxiao Shi88884492014-02-15 15:57:43 -0700311 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700312 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700313 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700314 if (isViolatingLocalhost) {
315 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
316 " data=" << data.getName() << " violates /localhost");
317 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700318 return;
319 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700320
Junxiao Shid3c792f2014-01-30 00:46:13 -0700321 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700322 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
323 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700324 // goto Data unsolicited pipeline
325 this->onDataUnsolicited(inFace, data);
326 return;
327 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700328
Junxiao Shid3c792f2014-01-30 00:46:13 -0700329 // CS insert
330 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700331
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700332 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700333 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700334 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700335 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700336 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700337
Junxiao Shid3c792f2014-01-30 00:46:13 -0700338 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000339 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700340
Junxiao Shid3c792f2014-01-30 00:46:13 -0700341 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700342 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
343 if (inRecord.getExpiry() > now) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000344 pendingDownstreams.insert(&inRecord.getFace());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700345 }
346 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700347
Junxiao Shid938a6b2014-05-11 23:40:29 -0700348 // invoke PIT satisfy callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000349 this->dispatchToStrategy(*pitEntry,
350 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700351
Junxiao Shi4846f372016-04-05 13:39:30 -0700352 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700353 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
354
Junxiao Shid3c792f2014-01-30 00:46:13 -0700355 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700356 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700357 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700358
Junxiao Shid3c792f2014-01-30 00:46:13 -0700359 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700360 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700361 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700362
Junxiao Shid3c792f2014-01-30 00:46:13 -0700363 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700364 for (Face* pendingDownstream : pendingDownstreams) {
365 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700366 continue;
367 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700368 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700369 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700370 }
371}
372
373void
374Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
375{
376 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000377 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
378 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700379 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700380 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700381 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700382
Junxiao Shif3c07812014-03-11 21:48:49 -0700383 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
384 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000385 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700386}
387
388void
389Forwarder::onOutgoingData(const Data& data, Face& outFace)
390{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700391 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700392 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
393 return;
394 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700395 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
396
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700397 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700398 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700399 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700400 if (isViolatingLocalhost) {
401 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
402 " data=" << data.getName() << " violates /localhost");
403 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700404 return;
405 }
406
Junxiao Shif3c07812014-03-11 21:48:49 -0700407 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700408
Junxiao Shid3c792f2014-01-30 00:46:13 -0700409 // send Data
410 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700411 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700412}
413
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700414void
415Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
416{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000417 // receive Nack
418 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700419 ++m_counters.nInNacks;
420
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700421 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700422 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700423 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
424 " nack=" << nack.getInterest().getName() <<
425 "~" << nack.getReason() << " face-is-multi-access");
426 return;
427 }
428
429 // PIT match
430 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
431 // if no PIT entry found, drop
432 if (pitEntry == nullptr) {
433 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
434 " nack=" << nack.getInterest().getName() <<
435 "~" << nack.getReason() << " no-PIT-entry");
436 return;
437 }
438
439 // has out-record?
440 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
441 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700442 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700443 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
444 " nack=" << nack.getInterest().getName() <<
445 "~" << nack.getReason() << " no-out-record");
446 return;
447 }
448
449 // if out-record has different Nonce, drop
450 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
451 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
452 " nack=" << nack.getInterest().getName() <<
453 "~" << nack.getReason() << " wrong-Nonce " <<
454 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
455 return;
456 }
457
458 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
459 " nack=" << nack.getInterest().getName() <<
460 "~" << nack.getReason() << " OK");
461
462 // record Nack on out-record
463 outRecord->setIncomingNack(nack);
464
465 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000466 this->dispatchToStrategy(*pitEntry,
467 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700468}
469
470void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000471Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700472 const lp::NackHeader& nack)
473{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700474 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700475 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
476 " nack=" << pitEntry->getInterest().getName() <<
477 "~" << nack.getReason() << " no-in-record");
478 return;
479 }
480
481 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700482 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700483
484 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700485 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700486 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
487 " nack=" << pitEntry->getInterest().getName() <<
488 "~" << nack.getReason() << " no-in-record");
489 return;
490 }
491
492 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700493 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700494 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
495 " nack=" << pitEntry->getInterest().getName() <<
496 "~" << nack.getReason() << " face-is-multi-access");
497 return;
498 }
499
500 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
501 " nack=" << pitEntry->getInterest().getName() <<
502 "~" << nack.getReason() << " OK");
503
504 // create Nack packet with the Interest from in-record
505 lp::Nack nackPkt(inRecord->getInterest());
506 nackPkt.setHeader(nack);
507
508 // erase in-record
509 pitEntry->deleteInRecord(outFace);
510
511 // send Nack on face
512 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700513 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700514}
515
Junxiao Shid3c792f2014-01-30 00:46:13 -0700516static inline bool
517compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
518{
519 return a.getExpiry() < b.getExpiry();
520}
521
522void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000523Forwarder::setUnsatisfyTimer(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700524{
Junxiao Shi4846f372016-04-05 13:39:30 -0700525 pit::InRecordCollection::iterator lastExpiring =
526 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700527
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700528 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700529 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
530 if (lastExpiryFromNow <= time::seconds::zero()) {
531 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700532 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700533
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700534 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700535 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700536 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
537}
538
539void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000540Forwarder::setStragglerTimer(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
541 time::milliseconds dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700542{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700543 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700544
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700545 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700546 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700547 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700548}
549
550void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000551Forwarder::cancelUnsatisfyAndStragglerTimer(pit::Entry& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700552{
Junxiao Shib9420cf2016-08-13 04:38:52 +0000553 scheduler::cancel(pitEntry.m_unsatisfyTimer);
554 scheduler::cancel(pitEntry.m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700555}
556
Junxiao Shia110f262014-10-12 12:35:20 -0700557static inline void
558insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
559 const pit::OutRecord& outRecord)
560{
561 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
562}
563
564void
565Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000566 time::milliseconds dataFreshnessPeriod, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700567{
568 // need Dead Nonce List insert?
569 bool needDnl = false;
570 if (isSatisfied) {
571 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
572 // Data never becomes stale if it doesn't have FreshnessPeriod field
573 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
574 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
575 }
576 else {
577 needDnl = true;
578 }
579
580 if (!needDnl) {
581 return;
582 }
583
584 // Dead Nonce List insert
585 if (upstream == 0) {
586 // insert all outgoing Nonces
587 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
588 std::for_each(outRecords.begin(), outRecords.end(),
589 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
590 }
591 else {
592 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700593 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700594 if (outRecord != pitEntry.getOutRecords().end()) {
595 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
596 }
597 }
598}
599
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800600} // namespace nfd