blob: c4b6f1dbd29e0c563c5ae37f87a298f129cf32d1 [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 Afanasyev929ef0a2018-08-06 17:28:36 -040036#include "face/null-face.hpp"
37
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080038namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080039
Davide Pesaventoa3148082018-04-12 18:21:54 -040040NFD_LOG_INIT(Forwarder);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070041
Junxiao Shic34d1672016-12-09 15:57:59 +000042static Name
43getDefaultStrategyName()
44{
Junxiao Shi037f4ab2016-12-13 04:27:06 +000045 return fw::BestRouteStrategy2::getStrategyName();
Junxiao Shic34d1672016-12-09 15:57:59 +000046}
47
Junxiao Shic041ca32014-02-25 20:01:15 -070048Forwarder::Forwarder()
Junxiao Shi9685cc52016-08-29 12:47:05 +000049 : m_unsolicitedDataPolicy(new fw::DefaultUnsolicitedDataPolicy())
Junxiao Shifbe8efe2016-08-22 16:02:30 +000050 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060051 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080052 , m_measurements(m_nameTree)
Junxiao Shic34d1672016-12-09 15:57:59 +000053 , m_strategyChoice(*this)
Alexander Afanasyev929ef0a2018-08-06 17:28:36 -040054 , m_csFace(face::makeNullFace(FaceUri("contentstore://")))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080055{
Alexander Afanasyev929ef0a2018-08-06 17:28:36 -040056 getFaceTable().addReserved(m_csFace, face::FACEID_CONTENT_STORE);
57
Junxiao Shidcffdaa2016-07-26 02:23:56 +000058 m_faceTable.afterAdd.connect([this] (Face& face) {
59 face.afterReceiveInterest.connect(
60 [this, &face] (const Interest& interest) {
61 this->startProcessInterest(face, interest);
62 });
63 face.afterReceiveData.connect(
64 [this, &face] (const Data& data) {
65 this->startProcessData(face, data);
66 });
67 face.afterReceiveNack.connect(
68 [this, &face] (const lp::Nack& nack) {
69 this->startProcessNack(face, nack);
70 });
Eric Newberry41aba102017-11-01 16:42:13 -070071 face.onDroppedInterest.connect(
72 [this, &face] (const Interest& interest) {
73 this->onDroppedInterest(face, interest);
74 });
Junxiao Shidcffdaa2016-07-26 02:23:56 +000075 });
76
77 m_faceTable.beforeRemove.connect([this] (Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000078 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000079 });
Junxiao Shic34d1672016-12-09 15:57:59 +000080
81 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080082}
83
Junxiao Shidcffdaa2016-07-26 02:23:56 +000084Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060085
Junxiao Shi0355e9f2015-09-02 07:24:53 -070086void
Junxiao Shid3c792f2014-01-30 00:46:13 -070087Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
88{
89 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -070090 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
91 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +000092 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -070093 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -070094
Junxiao Shi88884492014-02-15 15:57:43 -070095 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -070096 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -070097 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -070098 if (isViolatingLocalhost) {
99 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
100 " interest=" << interest.getName() << " violates /localhost");
101 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700102 return;
103 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700104
Junxiao Shi330136a2016-03-10 04:53:08 -0700105 // detect duplicate Nonce with Dead Nonce List
106 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
107 if (hasDuplicateNonceInDnl) {
108 // goto Interest loop pipeline
109 this->onInterestLoop(inFace, interest);
110 return;
111 }
112
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000113 // strip forwarding hint if Interest has reached producer region
114 if (!interest.getForwardingHint().empty() &&
115 m_networkRegionTable.isInProducerRegion(interest.getForwardingHint())) {
Junxiao Shif9858fd2017-02-01 16:22:44 +0000116 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
117 " interest=" << interest.getName() << " reaching-producer-region");
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000118 const_cast<Interest&>(interest).setForwardingHint({});
Junxiao Shif9858fd2017-02-01 16:22:44 +0000119 }
120
Junxiao Shid3c792f2014-01-30 00:46:13 -0700121 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700122 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700123
Junxiao Shi330136a2016-03-10 04:53:08 -0700124 // detect duplicate Nonce in PIT entry
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000125 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace);
126 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
127 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
128 // for p2p face: duplicate Nonce from same incoming face is not loop
129 hasDuplicateNonceInPit = hasDuplicateNonceInPit && !(dnw & fw::DUPLICATE_NONCE_IN_SAME);
130 }
Junxiao Shi330136a2016-03-10 04:53:08 -0700131 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700132 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700133 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700134 return;
135 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700136
Junxiao Shif3c07812014-03-11 21:48:49 -0700137 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700138 if (!pitEntry->hasInRecords()) {
Spyridon Mastorakiscef59cb2014-12-05 22:43:34 -0800139 if (m_csFromNdnSim == nullptr) {
140 m_cs.find(interest,
141 bind(&Forwarder::onContentStoreHit, this, std::ref(inFace), pitEntry, _1, _2),
142 bind(&Forwarder::onContentStoreMiss, this, std::ref(inFace), pitEntry, _1));
143 }
144 else {
145 shared_ptr<Data> match = m_csFromNdnSim->Lookup(interest.shared_from_this());
146 if (match != nullptr) {
147 this->onContentStoreHit(inFace, pitEntry, interest, *match);
148 }
149 else {
150 this->onContentStoreMiss(inFace, pitEntry, interest);
151 }
152 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700153 }
mzhang4eab72492015-02-25 11:16:09 -0600154 else {
155 this->onContentStoreMiss(inFace, pitEntry, interest);
156 }
157}
Junxiao Shic041ca32014-02-25 20:01:15 -0700158
mzhang4eab72492015-02-25 11:16:09 -0600159void
Junxiao Shi330136a2016-03-10 04:53:08 -0700160Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700161{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700162 // if multi-access or ad hoc face, drop
163 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700164 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
165 " interest=" << interest.getName() <<
166 " drop");
167 return;
168 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700169
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700170 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
171 " interest=" << interest.getName() <<
172 " send-Nack-duplicate");
173
174 // send Nack with reason=DUPLICATE
175 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
176 lp::Nack nack(interest);
177 nack.setReason(lp::NackReason::DUPLICATE);
178 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700179}
180
Teng Liang7003e0b2018-03-03 16:03:30 -0700181static inline bool
182compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
183{
184 return a.getExpiry() < b.getExpiry();
185}
186
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700187void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000188Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
mzhang4eab72492015-02-25 11:16:09 -0600189 const Interest& interest)
190{
191 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000192 ++m_counters.nCsMisses;
mzhang4eab72492015-02-25 11:16:09 -0600193
Junxiao Shi4846f372016-04-05 13:39:30 -0700194 // insert in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000195 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700196
Teng Liang7003e0b2018-03-03 16:03:30 -0700197 // set PIT expiry timer to the time that the last PIT in-record expires
198 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
199 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
200 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700201
Junxiao Shie342e8d2016-09-18 16:48:00 +0000202 // has NextHopFaceId?
203 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
204 if (nextHopTag != nullptr) {
205 // chosen NextHop face exists?
206 Face* nextHopFace = m_faceTable.get(*nextHopTag);
207 if (nextHopFace != nullptr) {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000208 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName() << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000209 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000210 // scope control is unnecessary, because privileged app explicitly wants to forward
211 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000212 }
213 return;
214 }
215
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000216 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000217 this->dispatchToStrategy(*pitEntry,
218 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700219}
220
221void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000222Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
223 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600224{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500225 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000226 ++m_counters.nCsHits;
mzhang4eab72492015-02-25 11:16:09 -0600227
Junxiao Shicde37ad2015-12-24 01:02:05 -0700228 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600229 // XXX should we lookup PIT for other Interests that also match csMatch?
230
Teng Liang6f09ab62018-03-01 20:04:08 -0700231 pitEntry->isSatisfied = true;
232 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
233
Teng Liang85a36632018-03-21 05:59:34 -0700234 // set PIT expiry timer to now
235 this->setExpiryTimer(pitEntry, 0_ms);
mzhang4eab72492015-02-25 11:16:09 -0600236
Alexander Afanasyev929ef0a2018-08-06 17:28:36 -0400237 beforeSatisfyInterest(*pitEntry, *m_csFace, data);
238 this->dispatchToStrategy(*pitEntry,
239 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, *m_csFace, data); });
240
Teng Liang85a36632018-03-21 05:59:34 -0700241 // dispatch to strategy: after Content Store hit
242 this->dispatchToStrategy(*pitEntry,
243 [&] (fw::Strategy& strategy) { strategy.afterContentStoreHit(pitEntry, inFace, data); });
mzhang4eab72492015-02-25 11:16:09 -0600244}
245
Junxiao Shid3c792f2014-01-30 00:46:13 -0700246void
Junxiao Shic5f651f2016-11-17 22:58:12 +0000247Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700248{
Junxiao Shif3c07812014-03-11 21:48:49 -0700249 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
250 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700251
Junxiao Shi4846f372016-04-05 13:39:30 -0700252 // insert out-record
Junxiao Shic5f651f2016-11-17 22:58:12 +0000253 pitEntry->insertOrUpdateOutRecord(outFace, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700254
Junxiao Shid3c792f2014-01-30 00:46:13 -0700255 // send Interest
Junxiao Shic5f651f2016-11-17 22:58:12 +0000256 outFace.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700257 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700258}
259
260void
Teng Liang6f09ab62018-03-01 20:04:08 -0700261Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shia110f262014-10-12 12:35:20 -0700262{
263 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
Teng Liang6f09ab62018-03-01 20:04:08 -0700264 (pitEntry->isSatisfied ? " satisfied" : " unsatisfied"));
Junxiao Shia110f262014-10-12 12:35:20 -0700265
Alexander Afanasyev929ef0a2018-08-06 17:28:36 -0400266 if (!pitEntry->isSatisfied) {
267 beforeExpirePendingInterest(*pitEntry);
268 }
269
Junxiao Shia110f262014-10-12 12:35:20 -0700270 // Dead Nonce List insert if necessary
Teng Liang6f09ab62018-03-01 20:04:08 -0700271 this->insertDeadNonceList(*pitEntry, 0);
Junxiao Shia110f262014-10-12 12:35:20 -0700272
Ju Pan6eb1ac92018-10-26 16:26:15 +0000273 // Increment satisfied/unsatisfied Interests counter
274 if (pitEntry->isSatisfied) {
275 ++m_counters.nSatisfiedInterests;
276 }
277 else {
278 ++m_counters.nUnsatisfiedInterests;
279 }
280
Junxiao Shif3c07812014-03-11 21:48:49 -0700281 // PIT delete
Teng Liang7003e0b2018-03-03 16:03:30 -0700282 scheduler::cancel(pitEntry->expiryTimer);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000283 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700284}
285
286void
287Forwarder::onIncomingData(Face& inFace, const Data& data)
288{
289 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700290 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000291 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700292 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700293
Junxiao Shi88884492014-02-15 15:57:43 -0700294 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700295 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700296 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700297 if (isViolatingLocalhost) {
298 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
299 " data=" << data.getName() << " violates /localhost");
300 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700301 return;
302 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700303
Junxiao Shid3c792f2014-01-30 00:46:13 -0700304 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700305 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
Teng Liang43bb2312018-03-26 04:16:42 -0700306 if (pitMatches.size() == 0) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700307 // goto Data unsolicited pipeline
308 this->onDataUnsolicited(inFace, data);
309 return;
310 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700311
Spyridon Mastorakis1429fc12016-12-06 15:20:19 -0800312 shared_ptr<Data> dataCopyWithoutTag = make_shared<Data>(data);
313 dataCopyWithoutTag->removeTag<lp::HopCountTag>();
314
Junxiao Shid3c792f2014-01-30 00:46:13 -0700315 // CS insert
Spyridon Mastorakiscef59cb2014-12-05 22:43:34 -0800316 if (m_csFromNdnSim == nullptr)
Spyridon Mastorakis1429fc12016-12-06 15:20:19 -0800317 m_cs.insert(*dataCopyWithoutTag);
Spyridon Mastorakiscef59cb2014-12-05 22:43:34 -0800318 else
Spyridon Mastorakis1429fc12016-12-06 15:20:19 -0800319 m_csFromNdnSim->Add(dataCopyWithoutTag);
Junxiao Shic041ca32014-02-25 20:01:15 -0700320
Teng Liang43bb2312018-03-26 04:16:42 -0700321 // when only one PIT entry is matched, trigger strategy: after receive Data
322 if (pitMatches.size() == 1) {
323 auto& pitEntry = pitMatches.front();
Junxiao Shic041ca32014-02-25 20:01:15 -0700324
Teng Liang43bb2312018-03-26 04:16:42 -0700325 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700326
Teng Liang7003e0b2018-03-03 16:03:30 -0700327 // set PIT expiry timer to now
328 this->setExpiryTimer(pitEntry, 0_ms);
329
Alexander Afanasyev929ef0a2018-08-06 17:28:36 -0400330 beforeSatisfyInterest(*pitEntry, inFace, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700331 // trigger strategy: after receive Data
Junxiao Shib9420cf2016-08-13 04:38:52 +0000332 this->dispatchToStrategy(*pitEntry,
Teng Liang43bb2312018-03-26 04:16:42 -0700333 [&] (fw::Strategy& strategy) { strategy.afterReceiveData(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700334
Teng Liang43bb2312018-03-26 04:16:42 -0700335 // mark PIT satisfied
Teng Liang6f09ab62018-03-01 20:04:08 -0700336 pitEntry->isSatisfied = true;
337 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
338
Junxiao Shi4846f372016-04-05 13:39:30 -0700339 // Dead Nonce List insert if necessary (for out-record of inFace)
Teng Liang6f09ab62018-03-01 20:04:08 -0700340 this->insertDeadNonceList(*pitEntry, &inFace);
Junxiao Shia110f262014-10-12 12:35:20 -0700341
Teng Liang43bb2312018-03-26 04:16:42 -0700342 // delete PIT entry's out-record
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700343 pitEntry->deleteOutRecord(inFace);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700344 }
Teng Liang43bb2312018-03-26 04:16:42 -0700345 // when more than one PIT entry is matched, trigger strategy: before satisfy Interest,
346 // and send Data to all matched out faces
347 else {
348 std::set<Face*> pendingDownstreams;
349 auto now = time::steady_clock::now();
Junxiao Shic041ca32014-02-25 20:01:15 -0700350
Teng Liang43bb2312018-03-26 04:16:42 -0700351 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
352 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
353
354 // remember pending downstreams
355 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
356 if (inRecord.getExpiry() > now) {
357 pendingDownstreams.insert(&inRecord.getFace());
358 }
359 }
360
361 // set PIT expiry timer to now
362 this->setExpiryTimer(pitEntry, 0_ms);
363
364 // invoke PIT satisfy callback
Alexander Afanasyev929ef0a2018-08-06 17:28:36 -0400365 beforeSatisfyInterest(*pitEntry, inFace, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700366 this->dispatchToStrategy(*pitEntry,
367 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
368
369 // mark PIT satisfied
370 pitEntry->isSatisfied = true;
371 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
372
373 // Dead Nonce List insert if necessary (for out-record of inFace)
374 this->insertDeadNonceList(*pitEntry, &inFace);
375
376 // clear PIT entry's in and out records
377 pitEntry->clearInRecords();
378 pitEntry->deleteOutRecord(inFace);
Junxiao Shida006f52014-05-16 11:18:00 -0700379 }
Teng Liang43bb2312018-03-26 04:16:42 -0700380
381 // foreach pending downstream
382 for (Face* pendingDownstream : pendingDownstreams) {
383 if (pendingDownstream->getId() == inFace.getId() &&
384 pendingDownstream->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
385 continue;
386 }
387 // goto outgoing Data pipeline
388 this->onOutgoingData(data, *pendingDownstream);
389 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700390 }
391}
392
393void
394Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
395{
396 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000397 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
398 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700399 // CS insert
Spyridon Mastorakiscef59cb2014-12-05 22:43:34 -0800400 if (m_csFromNdnSim == nullptr)
401 m_cs.insert(data, true);
402 else
403 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700404 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700405
Junxiao Shif3c07812014-03-11 21:48:49 -0700406 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
407 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000408 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700409}
410
411void
412Forwarder::onOutgoingData(const Data& data, Face& outFace)
413{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700414 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700415 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
416 return;
417 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700418 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
419
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700420 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700421 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700422 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700423 if (isViolatingLocalhost) {
424 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
425 " data=" << data.getName() << " violates /localhost");
426 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700427 return;
428 }
429
Junxiao Shif3c07812014-03-11 21:48:49 -0700430 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700431
Junxiao Shid3c792f2014-01-30 00:46:13 -0700432 // send Data
433 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700434 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700435}
436
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700437void
438Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
439{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000440 // receive Nack
441 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700442 ++m_counters.nInNacks;
443
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700444 // if multi-access or ad hoc face, drop
445 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700446 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
447 " nack=" << nack.getInterest().getName() <<
448 "~" << nack.getReason() << " face-is-multi-access");
449 return;
450 }
451
452 // PIT match
453 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
454 // if no PIT entry found, drop
455 if (pitEntry == nullptr) {
456 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
457 " nack=" << nack.getInterest().getName() <<
458 "~" << nack.getReason() << " no-PIT-entry");
459 return;
460 }
461
462 // has out-record?
463 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
464 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700465 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700466 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
467 " nack=" << nack.getInterest().getName() <<
468 "~" << nack.getReason() << " no-out-record");
469 return;
470 }
471
472 // if out-record has different Nonce, drop
473 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
474 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
475 " nack=" << nack.getInterest().getName() <<
476 "~" << nack.getReason() << " wrong-Nonce " <<
477 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
478 return;
479 }
480
481 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
482 " nack=" << nack.getInterest().getName() <<
483 "~" << nack.getReason() << " OK");
484
485 // record Nack on out-record
486 outRecord->setIncomingNack(nack);
487
Teng Liang7003e0b2018-03-03 16:03:30 -0700488 // set PIT expiry timer to now when all out-record receive Nack
489 if (!fw::hasPendingOutRecords(*pitEntry)) {
490 this->setExpiryTimer(pitEntry, 0_ms);
491 }
492
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700493 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000494 this->dispatchToStrategy(*pitEntry,
495 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700496}
497
498void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000499Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700500 const lp::NackHeader& nack)
501{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700502 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700503 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
504 " nack=" << pitEntry->getInterest().getName() <<
505 "~" << nack.getReason() << " no-in-record");
506 return;
507 }
508
509 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700510 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700511
512 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700513 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700514 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
515 " nack=" << pitEntry->getInterest().getName() <<
516 "~" << nack.getReason() << " no-in-record");
517 return;
518 }
519
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700520 // if multi-access or ad hoc face, drop
521 if (outFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700522 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
523 " nack=" << pitEntry->getInterest().getName() <<
524 "~" << nack.getReason() << " face-is-multi-access");
525 return;
526 }
527
528 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
529 " nack=" << pitEntry->getInterest().getName() <<
530 "~" << nack.getReason() << " OK");
531
532 // create Nack packet with the Interest from in-record
533 lp::Nack nackPkt(inRecord->getInterest());
534 nackPkt.setHeader(nack);
535
536 // erase in-record
537 pitEntry->deleteInRecord(outFace);
538
539 // send Nack on face
540 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700541 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700542}
543
Eric Newberry41aba102017-11-01 16:42:13 -0700544void
545Forwarder::onDroppedInterest(Face& outFace, const Interest& interest)
546{
547 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(outFace, interest);
548}
549
Junxiao Shid3c792f2014-01-30 00:46:13 -0700550void
Teng Liang7003e0b2018-03-03 16:03:30 -0700551Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700552{
Teng Liang39465c22018-11-12 19:43:04 -0700553 BOOST_ASSERT(pitEntry);
Teng Liang7003e0b2018-03-03 16:03:30 -0700554 BOOST_ASSERT(duration >= 0_ms);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700555
Teng Liang7003e0b2018-03-03 16:03:30 -0700556 scheduler::cancel(pitEntry->expiryTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700557
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400558 pitEntry->expiryTimer = scheduler::schedule(duration, [=] { onInterestFinalize(pitEntry); });
Junxiao Shia110f262014-10-12 12:35:20 -0700559}
560
561void
Teng Liang6f09ab62018-03-01 20:04:08 -0700562Forwarder::insertDeadNonceList(pit::Entry& pitEntry, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700563{
564 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000565 bool needDnl = true;
Teng Liang6f09ab62018-03-01 20:04:08 -0700566 if (pitEntry.isSatisfied) {
567 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
Junxiao Shia110f262014-10-12 12:35:20 -0700568 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Teng Liang6f09ab62018-03-01 20:04:08 -0700569 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700570 }
571
572 if (!needDnl) {
573 return;
574 }
575
576 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000577 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700578 // insert all outgoing Nonces
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400579 const auto& outRecords = pitEntry.getOutRecords();
580 std::for_each(outRecords.begin(), outRecords.end(), [&] (const auto& outRecord) {
581 m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
582 });
Junxiao Shia110f262014-10-12 12:35:20 -0700583 }
584 else {
585 // insert outgoing Nonce of a specific face
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400586 auto outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700587 if (outRecord != pitEntry.getOutRecords().end()) {
588 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
589 }
590 }
591}
592
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800593} // namespace nfd