blob: f77a09823ed1f4fb9ba755ca1e0ad04a7169328f [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 Liang63086442018-02-25 20:39:42 -07003 * Copyright (c) 2014-2018, 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 Afanasyevcd67c4c2018-08-06 17:28:36 -040034#include "face/null-face.hpp"
35
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080036namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080037
Junxiao Shi8c8d2182014-01-30 22:33:00 -070038NFD_LOG_INIT("Forwarder");
39
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 Afanasyevcd67c4c2018-08-06 17:28:36 -040052 , m_csFace(face::makeNullFace(FaceUri("contentstore://")))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080053{
Alexander Afanasyevcd67c4c2018-08-06 17:28:36 -040054 getFaceTable().addReserved(m_csFace, face::FACEID_CONTENT_STORE);
55
Junxiao Shidcffdaa2016-07-26 02:23:56 +000056 m_faceTable.afterAdd.connect([this] (Face& face) {
57 face.afterReceiveInterest.connect(
58 [this, &face] (const Interest& interest) {
59 this->startProcessInterest(face, interest);
60 });
61 face.afterReceiveData.connect(
62 [this, &face] (const Data& data) {
63 this->startProcessData(face, data);
64 });
65 face.afterReceiveNack.connect(
66 [this, &face] (const lp::Nack& nack) {
67 this->startProcessNack(face, nack);
68 });
Eric Newberry41aba102017-11-01 16:42:13 -070069 face.onDroppedInterest.connect(
70 [this, &face] (const Interest& interest) {
71 this->onDroppedInterest(face, interest);
72 });
Junxiao Shidcffdaa2016-07-26 02:23:56 +000073 });
74
75 m_faceTable.beforeRemove.connect([this] (Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000076 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000077 });
Junxiao Shic34d1672016-12-09 15:57:59 +000078
79 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080080}
81
Junxiao Shidcffdaa2016-07-26 02:23:56 +000082Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060083
Junxiao Shi0355e9f2015-09-02 07:24:53 -070084void
Junxiao Shid3c792f2014-01-30 00:46:13 -070085Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
86{
87 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -070088 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
89 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +000090 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -070091 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -070092
Junxiao Shi88884492014-02-15 15:57:43 -070093 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -070094 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -070095 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -070096 if (isViolatingLocalhost) {
97 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
98 " interest=" << interest.getName() << " violates /localhost");
99 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700100 return;
101 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700102
Junxiao Shi330136a2016-03-10 04:53:08 -0700103 // detect duplicate Nonce with Dead Nonce List
104 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
105 if (hasDuplicateNonceInDnl) {
106 // goto Interest loop pipeline
107 this->onInterestLoop(inFace, interest);
108 return;
109 }
110
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000111 // strip forwarding hint if Interest has reached producer region
112 if (!interest.getForwardingHint().empty() &&
113 m_networkRegionTable.isInProducerRegion(interest.getForwardingHint())) {
Junxiao Shif9858fd2017-02-01 16:22:44 +0000114 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
115 " interest=" << interest.getName() << " reaching-producer-region");
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000116 const_cast<Interest&>(interest).setForwardingHint({});
Junxiao Shif9858fd2017-02-01 16:22:44 +0000117 }
118
Junxiao Shid3c792f2014-01-30 00:46:13 -0700119 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700120 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700121
Junxiao Shi330136a2016-03-10 04:53:08 -0700122 // detect duplicate Nonce in PIT entry
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000123 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace);
124 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
125 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
126 // for p2p face: duplicate Nonce from same incoming face is not loop
127 hasDuplicateNonceInPit = hasDuplicateNonceInPit && !(dnw & fw::DUPLICATE_NONCE_IN_SAME);
128 }
Junxiao Shi330136a2016-03-10 04:53:08 -0700129 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700130 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700131 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700132 return;
133 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700134
Junxiao Shif3c07812014-03-11 21:48:49 -0700135 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700136 if (!pitEntry->hasInRecords()) {
Spyridon Mastorakis8b8d1262014-12-05 22:43:34 -0800137 if (m_csFromNdnSim == nullptr) {
138 m_cs.find(interest,
139 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
140 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
141 }
142 else {
143 shared_ptr<Data> match = m_csFromNdnSim->Lookup(interest.shared_from_this());
144 if (match != nullptr) {
145 this->onContentStoreHit(inFace, pitEntry, interest, *match);
146 }
147 else {
148 this->onContentStoreMiss(inFace, pitEntry, interest);
149 }
150 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700151 }
mzhang4eab72492015-02-25 11:16:09 -0600152 else {
153 this->onContentStoreMiss(inFace, pitEntry, interest);
154 }
155}
Junxiao Shic041ca32014-02-25 20:01:15 -0700156
mzhang4eab72492015-02-25 11:16:09 -0600157void
Junxiao Shi330136a2016-03-10 04:53:08 -0700158Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700159{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700160 // if multi-access or ad hoc face, drop
161 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700162 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
163 " interest=" << interest.getName() <<
164 " drop");
165 return;
166 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700167
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700168 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
169 " interest=" << interest.getName() <<
170 " send-Nack-duplicate");
171
172 // send Nack with reason=DUPLICATE
173 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
174 lp::Nack nack(interest);
175 nack.setReason(lp::NackReason::DUPLICATE);
176 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700177}
178
Teng Liang7003e0b2018-03-03 16:03:30 -0700179static inline bool
180compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
181{
182 return a.getExpiry() < b.getExpiry();
183}
184
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700185void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000186Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
mzhang4eab72492015-02-25 11:16:09 -0600187 const Interest& interest)
188{
189 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000190 ++m_counters.nCsMisses;
mzhang4eab72492015-02-25 11:16:09 -0600191
Junxiao Shi4846f372016-04-05 13:39:30 -0700192 // insert in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000193 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700194
Teng Liang7003e0b2018-03-03 16:03:30 -0700195 // set PIT expiry timer to the time that the last PIT in-record expires
196 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
197 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
198 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700199
Junxiao Shie342e8d2016-09-18 16:48:00 +0000200 // has NextHopFaceId?
201 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
202 if (nextHopTag != nullptr) {
203 // chosen NextHop face exists?
204 Face* nextHopFace = m_faceTable.get(*nextHopTag);
205 if (nextHopFace != nullptr) {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000206 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName() << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000207 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000208 // scope control is unnecessary, because privileged app explicitly wants to forward
209 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000210 }
211 return;
212 }
213
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000214 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000215 this->dispatchToStrategy(*pitEntry,
216 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700217}
218
219void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000220Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
221 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600222{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500223 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000224 ++m_counters.nCsHits;
mzhang4eab72492015-02-25 11:16:09 -0600225
Junxiao Shicde37ad2015-12-24 01:02:05 -0700226 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600227 // XXX should we lookup PIT for other Interests that also match csMatch?
228
Teng Liang6f09ab62018-03-01 20:04:08 -0700229 pitEntry->isSatisfied = true;
230 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
231
Teng Liang85a36632018-03-21 05:59:34 -0700232 // set PIT expiry timer to now
233 this->setExpiryTimer(pitEntry, 0_ms);
mzhang4eab72492015-02-25 11:16:09 -0600234
Alexander Afanasyevcd67c4c2018-08-06 17:28:36 -0400235 beforeSatisfyInterest(*pitEntry, *m_csFace, data);
236 this->dispatchToStrategy(*pitEntry,
237 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, *m_csFace, data); });
238
Teng Liang85a36632018-03-21 05:59:34 -0700239 // dispatch to strategy: after Content Store hit
240 this->dispatchToStrategy(*pitEntry,
241 [&] (fw::Strategy& strategy) { strategy.afterContentStoreHit(pitEntry, inFace, data); });
mzhang4eab72492015-02-25 11:16:09 -0600242}
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
Teng Liang6f09ab62018-03-01 20:04:08 -0700259Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shia110f262014-10-12 12:35:20 -0700260{
261 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
Teng Liang6f09ab62018-03-01 20:04:08 -0700262 (pitEntry->isSatisfied ? " satisfied" : " unsatisfied"));
Junxiao Shia110f262014-10-12 12:35:20 -0700263
Alexander Afanasyevcd67c4c2018-08-06 17:28:36 -0400264 if (!pitEntry->isSatisfied) {
265 beforeExpirePendingInterest(*pitEntry);
266 }
267
Junxiao Shia110f262014-10-12 12:35:20 -0700268 // Dead Nonce List insert if necessary
Teng Liang6f09ab62018-03-01 20:04:08 -0700269 this->insertDeadNonceList(*pitEntry, 0);
Junxiao Shia110f262014-10-12 12:35:20 -0700270
Junxiao Shif3c07812014-03-11 21:48:49 -0700271 // PIT delete
Teng Liang7003e0b2018-03-03 16:03:30 -0700272 scheduler::cancel(pitEntry->expiryTimer);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000273 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700274}
275
276void
277Forwarder::onIncomingData(Face& inFace, const Data& data)
278{
279 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700280 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000281 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700282 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700283
Junxiao Shi88884492014-02-15 15:57:43 -0700284 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700285 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700286 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700287 if (isViolatingLocalhost) {
288 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
289 " data=" << data.getName() << " violates /localhost");
290 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700291 return;
292 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700293
Junxiao Shid3c792f2014-01-30 00:46:13 -0700294 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700295 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
Teng Liang43bb2312018-03-26 04:16:42 -0700296 if (pitMatches.size() == 0) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700297 // goto Data unsolicited pipeline
298 this->onDataUnsolicited(inFace, data);
299 return;
300 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700301
Junxiao Shid3c792f2014-01-30 00:46:13 -0700302 // CS insert
Spyridon Mastorakis8b8d1262014-12-05 22:43:34 -0800303 if (m_csFromNdnSim == nullptr)
304 m_cs.insert(data);
305 else
306 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shic041ca32014-02-25 20:01:15 -0700307
Teng Liang43bb2312018-03-26 04:16:42 -0700308 // when only one PIT entry is matched, trigger strategy: after receive Data
309 if (pitMatches.size() == 1) {
310 auto& pitEntry = pitMatches.front();
Junxiao Shic041ca32014-02-25 20:01:15 -0700311
Teng Liang43bb2312018-03-26 04:16:42 -0700312 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700313
Teng Liang7003e0b2018-03-03 16:03:30 -0700314 // set PIT expiry timer to now
315 this->setExpiryTimer(pitEntry, 0_ms);
316
Alexander Afanasyevcd67c4c2018-08-06 17:28:36 -0400317 beforeSatisfyInterest(*pitEntry, inFace, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700318 // trigger strategy: after receive Data
Junxiao Shib9420cf2016-08-13 04:38:52 +0000319 this->dispatchToStrategy(*pitEntry,
Teng Liang43bb2312018-03-26 04:16:42 -0700320 [&] (fw::Strategy& strategy) { strategy.afterReceiveData(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700321
Teng Liang43bb2312018-03-26 04:16:42 -0700322 // mark PIT satisfied
Teng Liang6f09ab62018-03-01 20:04:08 -0700323 pitEntry->isSatisfied = true;
324 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
325
Junxiao Shi4846f372016-04-05 13:39:30 -0700326 // Dead Nonce List insert if necessary (for out-record of inFace)
Teng Liang6f09ab62018-03-01 20:04:08 -0700327 this->insertDeadNonceList(*pitEntry, &inFace);
Junxiao Shia110f262014-10-12 12:35:20 -0700328
Teng Liang43bb2312018-03-26 04:16:42 -0700329 // delete PIT entry's out-record
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700330 pitEntry->deleteOutRecord(inFace);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700331 }
Teng Liang43bb2312018-03-26 04:16:42 -0700332 // when more than one PIT entry is matched, trigger strategy: before satisfy Interest,
333 // and send Data to all matched out faces
334 else {
335 std::set<Face*> pendingDownstreams;
336 auto now = time::steady_clock::now();
Junxiao Shic041ca32014-02-25 20:01:15 -0700337
Teng Liang43bb2312018-03-26 04:16:42 -0700338 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
339 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
340
341 // remember pending downstreams
342 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
343 if (inRecord.getExpiry() > now) {
344 pendingDownstreams.insert(&inRecord.getFace());
345 }
346 }
347
348 // set PIT expiry timer to now
349 this->setExpiryTimer(pitEntry, 0_ms);
350
351 // invoke PIT satisfy callback
Alexander Afanasyevcd67c4c2018-08-06 17:28:36 -0400352 beforeSatisfyInterest(*pitEntry, inFace, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700353 this->dispatchToStrategy(*pitEntry,
354 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
355
356 // mark PIT satisfied
357 pitEntry->isSatisfied = true;
358 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
359
360 // Dead Nonce List insert if necessary (for out-record of inFace)
361 this->insertDeadNonceList(*pitEntry, &inFace);
362
363 // clear PIT entry's in and out records
364 pitEntry->clearInRecords();
365 pitEntry->deleteOutRecord(inFace);
Junxiao Shida006f52014-05-16 11:18:00 -0700366 }
Teng Liang43bb2312018-03-26 04:16:42 -0700367
368 // foreach pending downstream
369 for (Face* pendingDownstream : pendingDownstreams) {
370 if (pendingDownstream->getId() == inFace.getId() &&
371 pendingDownstream->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
372 continue;
373 }
374 // goto outgoing Data pipeline
375 this->onOutgoingData(data, *pendingDownstream);
376 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700377 }
378}
379
380void
381Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
382{
383 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000384 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
385 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700386 // CS insert
Spyridon Mastorakis8b8d1262014-12-05 22:43:34 -0800387 if (m_csFromNdnSim == nullptr)
388 m_cs.insert(data, true);
389 else
390 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700391 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700392
Junxiao Shif3c07812014-03-11 21:48:49 -0700393 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
394 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000395 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700396}
397
398void
399Forwarder::onOutgoingData(const Data& data, Face& outFace)
400{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700401 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700402 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
403 return;
404 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700405 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
406
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700407 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700408 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700409 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700410 if (isViolatingLocalhost) {
411 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
412 " data=" << data.getName() << " violates /localhost");
413 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700414 return;
415 }
416
Junxiao Shif3c07812014-03-11 21:48:49 -0700417 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700418
Junxiao Shid3c792f2014-01-30 00:46:13 -0700419 // send Data
420 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700421 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700422}
423
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700424void
425Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
426{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000427 // receive Nack
428 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700429 ++m_counters.nInNacks;
430
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700431 // if multi-access or ad hoc face, drop
432 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700433 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
434 " nack=" << nack.getInterest().getName() <<
435 "~" << nack.getReason() << " face-is-multi-access");
436 return;
437 }
438
439 // PIT match
440 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
441 // if no PIT entry found, drop
442 if (pitEntry == nullptr) {
443 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
444 " nack=" << nack.getInterest().getName() <<
445 "~" << nack.getReason() << " no-PIT-entry");
446 return;
447 }
448
449 // has out-record?
450 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
451 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700452 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700453 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
454 " nack=" << nack.getInterest().getName() <<
455 "~" << nack.getReason() << " no-out-record");
456 return;
457 }
458
459 // if out-record has different Nonce, drop
460 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
461 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
462 " nack=" << nack.getInterest().getName() <<
463 "~" << nack.getReason() << " wrong-Nonce " <<
464 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
465 return;
466 }
467
468 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
469 " nack=" << nack.getInterest().getName() <<
470 "~" << nack.getReason() << " OK");
471
472 // record Nack on out-record
473 outRecord->setIncomingNack(nack);
474
Teng Liang7003e0b2018-03-03 16:03:30 -0700475 // set PIT expiry timer to now when all out-record receive Nack
476 if (!fw::hasPendingOutRecords(*pitEntry)) {
477 this->setExpiryTimer(pitEntry, 0_ms);
478 }
479
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700480 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000481 this->dispatchToStrategy(*pitEntry,
482 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700483}
484
485void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000486Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700487 const lp::NackHeader& nack)
488{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700489 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700490 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
491 " nack=" << pitEntry->getInterest().getName() <<
492 "~" << nack.getReason() << " no-in-record");
493 return;
494 }
495
496 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700497 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700498
499 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700500 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700501 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
502 " nack=" << pitEntry->getInterest().getName() <<
503 "~" << nack.getReason() << " no-in-record");
504 return;
505 }
506
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700507 // if multi-access or ad hoc face, drop
508 if (outFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700509 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
510 " nack=" << pitEntry->getInterest().getName() <<
511 "~" << nack.getReason() << " face-is-multi-access");
512 return;
513 }
514
515 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
516 " nack=" << pitEntry->getInterest().getName() <<
517 "~" << nack.getReason() << " OK");
518
519 // create Nack packet with the Interest from in-record
520 lp::Nack nackPkt(inRecord->getInterest());
521 nackPkt.setHeader(nack);
522
523 // erase in-record
524 pitEntry->deleteInRecord(outFace);
525
526 // send Nack on face
527 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700528 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700529}
530
Eric Newberry41aba102017-11-01 16:42:13 -0700531void
532Forwarder::onDroppedInterest(Face& outFace, const Interest& interest)
533{
534 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(outFace, interest);
535}
536
Junxiao Shid3c792f2014-01-30 00:46:13 -0700537void
Teng Liang7003e0b2018-03-03 16:03:30 -0700538Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700539{
Teng Liang7003e0b2018-03-03 16:03:30 -0700540 BOOST_ASSERT(duration >= 0_ms);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700541
Teng Liang7003e0b2018-03-03 16:03:30 -0700542 scheduler::cancel(pitEntry->expiryTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700543
Teng Liang7003e0b2018-03-03 16:03:30 -0700544 pitEntry->expiryTimer = scheduler::schedule(duration,
Teng Liang6f09ab62018-03-01 20:04:08 -0700545 bind(&Forwarder::onInterestFinalize, this, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700546}
547
Junxiao Shia110f262014-10-12 12:35:20 -0700548static inline void
549insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
550 const pit::OutRecord& outRecord)
551{
552 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
553}
554
555void
Teng Liang6f09ab62018-03-01 20:04:08 -0700556Forwarder::insertDeadNonceList(pit::Entry& pitEntry, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700557{
558 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000559 bool needDnl = true;
Teng Liang6f09ab62018-03-01 20:04:08 -0700560 if (pitEntry.isSatisfied) {
561 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
Junxiao Shia110f262014-10-12 12:35:20 -0700562 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Teng Liang6f09ab62018-03-01 20:04:08 -0700563 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700564 }
565
566 if (!needDnl) {
567 return;
568 }
569
570 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000571 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700572 // insert all outgoing Nonces
573 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
574 std::for_each(outRecords.begin(), outRecords.end(),
575 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
576 }
577 else {
578 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700579 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700580 if (outRecord != pitEntry.getOutRecords().end()) {
581 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
582 }
583 }
584}
585
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800586} // namespace nfd