blob: c9a3fb858aef33a0feccd2d7a3c3f7d520ebb4f4 [file] [log] [blame]
Alexander Afanasyev33b72772014-01-26 23:22:58 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shifc2e13d2017-07-25 02:08:48 +00002/*
Teng Liang39465c22018-11-12 19:43:04 -07003 * Copyright (c) 2014-2019, 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"
Davide Pesaventoa3148082018-04-12 18:21:54 -040027
Junxiao Shi00dc9142016-11-21 14:23:12 +000028#include "algorithm.hpp"
Junxiao Shic34d1672016-12-09 15:57:59 +000029#include "best-route-strategy2.hpp"
Junxiao Shifaf3eb02015-02-16 10:50:36 -070030#include "strategy.hpp"
Junxiao Shic34d1672016-12-09 15:57:59 +000031#include "core/logger.hpp"
Junxiao Shi02b73f52016-07-28 01:48:27 +000032#include "table/cleanup.hpp"
Davide Pesaventoa3148082018-04-12 18:21:54 -040033
Junxiao Shicbc8e942016-09-06 03:17:45 +000034#include <ndn-cxx/lp/tags.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080035
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080036namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080037
Davide Pesaventoa3148082018-04-12 18:21:54 -040038NFD_LOG_INIT(Forwarder);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070039
Junxiao Shic34d1672016-12-09 15:57:59 +000040static Name
41getDefaultStrategyName()
42{
Junxiao Shi037f4ab2016-12-13 04:27:06 +000043 return fw::BestRouteStrategy2::getStrategyName();
Junxiao Shic34d1672016-12-09 15:57:59 +000044}
45
Junxiao Shic041ca32014-02-25 20:01:15 -070046Forwarder::Forwarder()
Junxiao Shi9685cc52016-08-29 12:47:05 +000047 : m_unsolicitedDataPolicy(new fw::DefaultUnsolicitedDataPolicy())
Junxiao Shifbe8efe2016-08-22 16:02:30 +000048 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060049 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080050 , m_measurements(m_nameTree)
Junxiao Shic34d1672016-12-09 15:57:59 +000051 , m_strategyChoice(*this)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080052{
Junxiao Shidcffdaa2016-07-26 02:23:56 +000053 m_faceTable.afterAdd.connect([this] (Face& face) {
54 face.afterReceiveInterest.connect(
55 [this, &face] (const Interest& interest) {
56 this->startProcessInterest(face, interest);
57 });
58 face.afterReceiveData.connect(
59 [this, &face] (const Data& data) {
60 this->startProcessData(face, data);
61 });
62 face.afterReceiveNack.connect(
63 [this, &face] (const lp::Nack& nack) {
64 this->startProcessNack(face, nack);
65 });
Eric Newberry41aba102017-11-01 16:42:13 -070066 face.onDroppedInterest.connect(
67 [this, &face] (const Interest& interest) {
68 this->onDroppedInterest(face, interest);
69 });
Junxiao Shidcffdaa2016-07-26 02:23:56 +000070 });
71
72 m_faceTable.beforeRemove.connect([this] (Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000073 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000074 });
Junxiao Shic34d1672016-12-09 15:57:59 +000075
76 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080077}
78
Junxiao Shidcffdaa2016-07-26 02:23:56 +000079Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060080
Junxiao Shi0355e9f2015-09-02 07:24:53 -070081void
Junxiao Shid3c792f2014-01-30 00:46:13 -070082Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
83{
84 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -070085 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
86 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +000087 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -070088 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -070089
Junxiao Shi88884492014-02-15 15:57:43 -070090 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -070091 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -070092 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -070093 if (isViolatingLocalhost) {
94 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
95 " interest=" << interest.getName() << " violates /localhost");
96 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -070097 return;
98 }
Junxiao Shic041ca32014-02-25 20:01:15 -070099
Junxiao Shi330136a2016-03-10 04:53:08 -0700100 // detect duplicate Nonce with Dead Nonce List
101 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
102 if (hasDuplicateNonceInDnl) {
103 // goto Interest loop pipeline
104 this->onInterestLoop(inFace, interest);
105 return;
106 }
107
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000108 // strip forwarding hint if Interest has reached producer region
109 if (!interest.getForwardingHint().empty() &&
110 m_networkRegionTable.isInProducerRegion(interest.getForwardingHint())) {
Junxiao Shif9858fd2017-02-01 16:22:44 +0000111 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
112 " interest=" << interest.getName() << " reaching-producer-region");
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000113 const_cast<Interest&>(interest).setForwardingHint({});
Junxiao Shif9858fd2017-02-01 16:22:44 +0000114 }
115
Junxiao Shid3c792f2014-01-30 00:46:13 -0700116 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700117 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700118
Junxiao Shi330136a2016-03-10 04:53:08 -0700119 // detect duplicate Nonce in PIT entry
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000120 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace);
121 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
122 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
123 // for p2p face: duplicate Nonce from same incoming face is not loop
124 hasDuplicateNonceInPit = hasDuplicateNonceInPit && !(dnw & fw::DUPLICATE_NONCE_IN_SAME);
125 }
Junxiao Shi330136a2016-03-10 04:53:08 -0700126 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700127 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700128 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700129 return;
130 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700131
Junxiao Shif3c07812014-03-11 21:48:49 -0700132 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700133 if (!pitEntry->hasInRecords()) {
Spyridon Mastorakiscef59cb2014-12-05 22:43:34 -0800134 if (m_csFromNdnSim == nullptr) {
135 m_cs.find(interest,
136 bind(&Forwarder::onContentStoreHit, this, std::ref(inFace), pitEntry, _1, _2),
137 bind(&Forwarder::onContentStoreMiss, this, std::ref(inFace), pitEntry, _1));
138 }
139 else {
140 shared_ptr<Data> match = m_csFromNdnSim->Lookup(interest.shared_from_this());
141 if (match != nullptr) {
142 this->onContentStoreHit(inFace, pitEntry, interest, *match);
143 }
144 else {
145 this->onContentStoreMiss(inFace, pitEntry, interest);
146 }
147 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700148 }
mzhang4eab72492015-02-25 11:16:09 -0600149 else {
150 this->onContentStoreMiss(inFace, pitEntry, interest);
151 }
152}
Junxiao Shic041ca32014-02-25 20:01:15 -0700153
mzhang4eab72492015-02-25 11:16:09 -0600154void
Junxiao Shi330136a2016-03-10 04:53:08 -0700155Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700156{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700157 // if multi-access or ad hoc face, drop
158 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700159 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
160 " interest=" << interest.getName() <<
161 " drop");
162 return;
163 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700164
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700165 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
166 " interest=" << interest.getName() <<
167 " send-Nack-duplicate");
168
169 // send Nack with reason=DUPLICATE
170 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
171 lp::Nack nack(interest);
172 nack.setReason(lp::NackReason::DUPLICATE);
173 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700174}
175
Teng Liang7003e0b2018-03-03 16:03:30 -0700176static inline bool
177compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
178{
179 return a.getExpiry() < b.getExpiry();
180}
181
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700182void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000183Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
mzhang4eab72492015-02-25 11:16:09 -0600184 const Interest& interest)
185{
186 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000187 ++m_counters.nCsMisses;
mzhang4eab72492015-02-25 11:16:09 -0600188
Junxiao Shi4846f372016-04-05 13:39:30 -0700189 // insert in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000190 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700191
Teng Liang7003e0b2018-03-03 16:03:30 -0700192 // set PIT expiry timer to the time that the last PIT in-record expires
193 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
194 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
195 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700196
Junxiao Shie342e8d2016-09-18 16:48:00 +0000197 // has NextHopFaceId?
198 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
199 if (nextHopTag != nullptr) {
200 // chosen NextHop face exists?
201 Face* nextHopFace = m_faceTable.get(*nextHopTag);
202 if (nextHopFace != nullptr) {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000203 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName() << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000204 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000205 // scope control is unnecessary, because privileged app explicitly wants to forward
206 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000207 }
208 return;
209 }
210
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000211 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000212 this->dispatchToStrategy(*pitEntry,
213 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700214}
215
216void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000217Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
218 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600219{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500220 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000221 ++m_counters.nCsHits;
mzhang4eab72492015-02-25 11:16:09 -0600222
Junxiao Shicde37ad2015-12-24 01:02:05 -0700223 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600224 // XXX should we lookup PIT for other Interests that also match csMatch?
225
Teng Liang6f09ab62018-03-01 20:04:08 -0700226 pitEntry->isSatisfied = true;
227 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
228
Teng Liang85a36632018-03-21 05:59:34 -0700229 // set PIT expiry timer to now
230 this->setExpiryTimer(pitEntry, 0_ms);
mzhang4eab72492015-02-25 11:16:09 -0600231
Teng Liang85a36632018-03-21 05:59:34 -0700232 // dispatch to strategy: after Content Store hit
233 this->dispatchToStrategy(*pitEntry,
234 [&] (fw::Strategy& strategy) { strategy.afterContentStoreHit(pitEntry, inFace, data); });
mzhang4eab72492015-02-25 11:16:09 -0600235}
236
Junxiao Shid3c792f2014-01-30 00:46:13 -0700237void
Junxiao Shic5f651f2016-11-17 22:58:12 +0000238Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700239{
Junxiao Shif3c07812014-03-11 21:48:49 -0700240 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
241 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700242
Junxiao Shi4846f372016-04-05 13:39:30 -0700243 // insert out-record
Junxiao Shic5f651f2016-11-17 22:58:12 +0000244 pitEntry->insertOrUpdateOutRecord(outFace, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700245
Junxiao Shid3c792f2014-01-30 00:46:13 -0700246 // send Interest
Junxiao Shic5f651f2016-11-17 22:58:12 +0000247 outFace.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700248 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700249}
250
251void
Teng Liang6f09ab62018-03-01 20:04:08 -0700252Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shia110f262014-10-12 12:35:20 -0700253{
254 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
Teng Liang6f09ab62018-03-01 20:04:08 -0700255 (pitEntry->isSatisfied ? " satisfied" : " unsatisfied"));
Junxiao Shia110f262014-10-12 12:35:20 -0700256
257 // Dead Nonce List insert if necessary
Teng Liang6f09ab62018-03-01 20:04:08 -0700258 this->insertDeadNonceList(*pitEntry, 0);
Junxiao Shia110f262014-10-12 12:35:20 -0700259
Ju Pan6eb1ac92018-10-26 16:26:15 +0000260 // Increment satisfied/unsatisfied Interests counter
261 if (pitEntry->isSatisfied) {
262 ++m_counters.nSatisfiedInterests;
263 }
264 else {
265 ++m_counters.nUnsatisfiedInterests;
266 }
267
Junxiao Shif3c07812014-03-11 21:48:49 -0700268 // PIT delete
Teng Liang7003e0b2018-03-03 16:03:30 -0700269 scheduler::cancel(pitEntry->expiryTimer);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000270 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700271}
272
273void
274Forwarder::onIncomingData(Face& inFace, const Data& data)
275{
276 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700277 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000278 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700279 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700280
Junxiao Shi88884492014-02-15 15:57:43 -0700281 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700282 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700283 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700284 if (isViolatingLocalhost) {
285 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
286 " data=" << data.getName() << " violates /localhost");
287 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700288 return;
289 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700290
Junxiao Shid3c792f2014-01-30 00:46:13 -0700291 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700292 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
Teng Liang43bb2312018-03-26 04:16:42 -0700293 if (pitMatches.size() == 0) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700294 // goto Data unsolicited pipeline
295 this->onDataUnsolicited(inFace, data);
296 return;
297 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700298
Junxiao Shid3c792f2014-01-30 00:46:13 -0700299 // CS insert
Spyridon Mastorakiscef59cb2014-12-05 22:43:34 -0800300 if (m_csFromNdnSim == nullptr)
301 m_cs.insert(data);
302 else
303 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shic041ca32014-02-25 20:01:15 -0700304
Teng Liang43bb2312018-03-26 04:16:42 -0700305 // when only one PIT entry is matched, trigger strategy: after receive Data
306 if (pitMatches.size() == 1) {
307 auto& pitEntry = pitMatches.front();
Junxiao Shic041ca32014-02-25 20:01:15 -0700308
Teng Liang43bb2312018-03-26 04:16:42 -0700309 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700310
Teng Liang7003e0b2018-03-03 16:03:30 -0700311 // set PIT expiry timer to now
312 this->setExpiryTimer(pitEntry, 0_ms);
313
Teng Liang43bb2312018-03-26 04:16:42 -0700314 // trigger strategy: after receive Data
Junxiao Shib9420cf2016-08-13 04:38:52 +0000315 this->dispatchToStrategy(*pitEntry,
Teng Liang43bb2312018-03-26 04:16:42 -0700316 [&] (fw::Strategy& strategy) { strategy.afterReceiveData(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700317
Teng Liang43bb2312018-03-26 04:16:42 -0700318 // mark PIT satisfied
Teng Liang6f09ab62018-03-01 20:04:08 -0700319 pitEntry->isSatisfied = true;
320 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
321
Junxiao Shi4846f372016-04-05 13:39:30 -0700322 // Dead Nonce List insert if necessary (for out-record of inFace)
Teng Liang6f09ab62018-03-01 20:04:08 -0700323 this->insertDeadNonceList(*pitEntry, &inFace);
Junxiao Shia110f262014-10-12 12:35:20 -0700324
Teng Liang43bb2312018-03-26 04:16:42 -0700325 // delete PIT entry's out-record
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700326 pitEntry->deleteOutRecord(inFace);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700327 }
Teng Liang43bb2312018-03-26 04:16:42 -0700328 // when more than one PIT entry is matched, trigger strategy: before satisfy Interest,
329 // and send Data to all matched out faces
330 else {
331 std::set<Face*> pendingDownstreams;
332 auto now = time::steady_clock::now();
Junxiao Shic041ca32014-02-25 20:01:15 -0700333
Teng Liang43bb2312018-03-26 04:16:42 -0700334 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
335 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
336
337 // remember pending downstreams
338 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
339 if (inRecord.getExpiry() > now) {
340 pendingDownstreams.insert(&inRecord.getFace());
341 }
342 }
343
344 // set PIT expiry timer to now
345 this->setExpiryTimer(pitEntry, 0_ms);
346
347 // invoke PIT satisfy callback
348 this->dispatchToStrategy(*pitEntry,
349 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
350
351 // mark PIT satisfied
352 pitEntry->isSatisfied = true;
353 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
354
355 // Dead Nonce List insert if necessary (for out-record of inFace)
356 this->insertDeadNonceList(*pitEntry, &inFace);
357
358 // clear PIT entry's in and out records
359 pitEntry->clearInRecords();
360 pitEntry->deleteOutRecord(inFace);
Junxiao Shida006f52014-05-16 11:18:00 -0700361 }
Teng Liang43bb2312018-03-26 04:16:42 -0700362
363 // foreach pending downstream
364 for (Face* pendingDownstream : pendingDownstreams) {
365 if (pendingDownstream->getId() == inFace.getId() &&
366 pendingDownstream->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
367 continue;
368 }
369 // goto outgoing Data pipeline
370 this->onOutgoingData(data, *pendingDownstream);
371 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700372 }
373}
374
375void
376Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
377{
378 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000379 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
380 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700381 // CS insert
Spyridon Mastorakiscef59cb2014-12-05 22:43:34 -0800382 if (m_csFromNdnSim == nullptr)
383 m_cs.insert(data, true);
384 else
385 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700386 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700387
Junxiao Shif3c07812014-03-11 21:48:49 -0700388 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
389 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000390 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700391}
392
393void
394Forwarder::onOutgoingData(const Data& data, Face& outFace)
395{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700396 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700397 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
398 return;
399 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700400 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
401
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700402 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700403 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700404 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700405 if (isViolatingLocalhost) {
406 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
407 " data=" << data.getName() << " violates /localhost");
408 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700409 return;
410 }
411
Junxiao Shif3c07812014-03-11 21:48:49 -0700412 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700413
Junxiao Shid3c792f2014-01-30 00:46:13 -0700414 // send Data
415 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700416 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700417}
418
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700419void
420Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
421{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000422 // receive Nack
423 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700424 ++m_counters.nInNacks;
425
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700426 // if multi-access or ad hoc face, drop
427 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700428 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
429 " nack=" << nack.getInterest().getName() <<
430 "~" << nack.getReason() << " face-is-multi-access");
431 return;
432 }
433
434 // PIT match
435 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
436 // if no PIT entry found, drop
437 if (pitEntry == nullptr) {
438 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
439 " nack=" << nack.getInterest().getName() <<
440 "~" << nack.getReason() << " no-PIT-entry");
441 return;
442 }
443
444 // has out-record?
445 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
446 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700447 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700448 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
449 " nack=" << nack.getInterest().getName() <<
450 "~" << nack.getReason() << " no-out-record");
451 return;
452 }
453
454 // if out-record has different Nonce, drop
455 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
456 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
457 " nack=" << nack.getInterest().getName() <<
458 "~" << nack.getReason() << " wrong-Nonce " <<
459 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
460 return;
461 }
462
463 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
464 " nack=" << nack.getInterest().getName() <<
465 "~" << nack.getReason() << " OK");
466
467 // record Nack on out-record
468 outRecord->setIncomingNack(nack);
469
Teng Liang7003e0b2018-03-03 16:03:30 -0700470 // set PIT expiry timer to now when all out-record receive Nack
471 if (!fw::hasPendingOutRecords(*pitEntry)) {
472 this->setExpiryTimer(pitEntry, 0_ms);
473 }
474
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700475 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000476 this->dispatchToStrategy(*pitEntry,
477 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700478}
479
480void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000481Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700482 const lp::NackHeader& nack)
483{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700484 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700485 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
486 " nack=" << pitEntry->getInterest().getName() <<
487 "~" << nack.getReason() << " no-in-record");
488 return;
489 }
490
491 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700492 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700493
494 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700495 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700496 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
497 " nack=" << pitEntry->getInterest().getName() <<
498 "~" << nack.getReason() << " no-in-record");
499 return;
500 }
501
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700502 // if multi-access or ad hoc face, drop
503 if (outFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700504 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
505 " nack=" << pitEntry->getInterest().getName() <<
506 "~" << nack.getReason() << " face-is-multi-access");
507 return;
508 }
509
510 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
511 " nack=" << pitEntry->getInterest().getName() <<
512 "~" << nack.getReason() << " OK");
513
514 // create Nack packet with the Interest from in-record
515 lp::Nack nackPkt(inRecord->getInterest());
516 nackPkt.setHeader(nack);
517
518 // erase in-record
519 pitEntry->deleteInRecord(outFace);
520
521 // send Nack on face
522 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700523 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700524}
525
Eric Newberry41aba102017-11-01 16:42:13 -0700526void
527Forwarder::onDroppedInterest(Face& outFace, const Interest& interest)
528{
529 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(outFace, interest);
530}
531
Junxiao Shid3c792f2014-01-30 00:46:13 -0700532void
Teng Liang7003e0b2018-03-03 16:03:30 -0700533Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700534{
Teng Liang39465c22018-11-12 19:43:04 -0700535 BOOST_ASSERT(pitEntry);
Teng Liang7003e0b2018-03-03 16:03:30 -0700536 BOOST_ASSERT(duration >= 0_ms);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700537
Teng Liang7003e0b2018-03-03 16:03:30 -0700538 scheduler::cancel(pitEntry->expiryTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700539
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400540 pitEntry->expiryTimer = scheduler::schedule(duration, [=] { onInterestFinalize(pitEntry); });
Junxiao Shia110f262014-10-12 12:35:20 -0700541}
542
543void
Teng Liang6f09ab62018-03-01 20:04:08 -0700544Forwarder::insertDeadNonceList(pit::Entry& pitEntry, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700545{
546 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000547 bool needDnl = true;
Teng Liang6f09ab62018-03-01 20:04:08 -0700548 if (pitEntry.isSatisfied) {
549 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
Junxiao Shia110f262014-10-12 12:35:20 -0700550 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Teng Liang6f09ab62018-03-01 20:04:08 -0700551 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700552 }
553
554 if (!needDnl) {
555 return;
556 }
557
558 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000559 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700560 // insert all outgoing Nonces
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400561 const auto& outRecords = pitEntry.getOutRecords();
562 std::for_each(outRecords.begin(), outRecords.end(), [&] (const auto& outRecord) {
563 m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
564 });
Junxiao Shia110f262014-10-12 12:35:20 -0700565 }
566 else {
567 // insert outgoing Nonce of a specific face
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400568 auto outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700569 if (outRecord != pitEntry.getOutRecords().end()) {
570 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
571 }
572 }
573}
574
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800575} // namespace nfd