blob: 7a85daff095846f1f8b506b1dccf77d83246f4ef [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"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040031#include "common/global.hpp"
32#include "common/logger.hpp"
Junxiao Shi02b73f52016-07-28 01:48:27 +000033#include "table/cleanup.hpp"
Davide Pesaventoa3148082018-04-12 18:21:54 -040034
Junxiao Shicbc8e942016-09-06 03:17:45 +000035#include <ndn-cxx/lp/tags.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080036
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080037namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080038
Davide Pesaventoa3148082018-04-12 18:21:54 -040039NFD_LOG_INIT(Forwarder);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070040
Junxiao Shic34d1672016-12-09 15:57:59 +000041static Name
42getDefaultStrategyName()
43{
Junxiao Shi037f4ab2016-12-13 04:27:06 +000044 return fw::BestRouteStrategy2::getStrategyName();
Junxiao Shic34d1672016-12-09 15:57:59 +000045}
46
Junxiao Shic041ca32014-02-25 20:01:15 -070047Forwarder::Forwarder()
Davide Pesavento3dade002019-03-19 11:29:56 -060048 : m_unsolicitedDataPolicy(make_unique<fw::DefaultUnsolicitedDataPolicy>())
Junxiao Shifbe8efe2016-08-22 16:02:30 +000049 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060050 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080051 , m_measurements(m_nameTree)
Junxiao Shic34d1672016-12-09 15:57:59 +000052 , m_strategyChoice(*this)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080053{
Junxiao Shidcffdaa2016-07-26 02:23:56 +000054 m_faceTable.afterAdd.connect([this] (Face& face) {
55 face.afterReceiveInterest.connect(
56 [this, &face] (const Interest& interest) {
ashiqopuc7079482019-02-20 05:34:37 +000057 this->startProcessInterest(FaceEndpoint(face, 0), interest);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000058 });
59 face.afterReceiveData.connect(
60 [this, &face] (const Data& data) {
ashiqopuc7079482019-02-20 05:34:37 +000061 this->startProcessData(FaceEndpoint(face, 0), data);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000062 });
63 face.afterReceiveNack.connect(
64 [this, &face] (const lp::Nack& nack) {
ashiqopuc7079482019-02-20 05:34:37 +000065 this->startProcessNack(FaceEndpoint(face, 0), nack);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000066 });
Eric Newberry41aba102017-11-01 16:42:13 -070067 face.onDroppedInterest.connect(
68 [this, &face] (const Interest& interest) {
ashiqopuc7079482019-02-20 05:34:37 +000069 this->onDroppedInterest(FaceEndpoint(face, 0), interest);
Eric Newberry41aba102017-11-01 16:42:13 -070070 });
Junxiao Shidcffdaa2016-07-26 02:23:56 +000071 });
72
73 m_faceTable.beforeRemove.connect([this] (Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000074 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000075 });
Junxiao Shic34d1672016-12-09 15:57:59 +000076
77 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080078}
79
Junxiao Shidcffdaa2016-07-26 02:23:56 +000080Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060081
Junxiao Shi0355e9f2015-09-02 07:24:53 -070082void
ashiqopuc7079482019-02-20 05:34:37 +000083Forwarder::onIncomingInterest(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -070084{
85 // receive Interest
ashiqopuc7079482019-02-20 05:34:37 +000086 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress << " interest=" << interest.getName());
87 interest.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.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
ashiqopuc7079482019-02-20 05:34:37 +000091 bool isViolatingLocalhost = ingress.face.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) {
ashiqopuc7079482019-02-20 05:34:37 +000094 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
95 << " interest=" << interest.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -070096 // (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
ashiqopuc7079482019-02-20 05:34:37 +0000104 this->onInterestLoop(ingress, interest);
Junxiao Shi330136a2016-03-10 04:53:08 -0700105 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())) {
ashiqopuc7079482019-02-20 05:34:37 +0000111 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
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
ashiqopuc7079482019-02-20 05:34:37 +0000120 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), ingress.face);
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000121 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
ashiqopuc7079482019-02-20 05:34:37 +0000122 if (ingress.face.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000123 // 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
ashiqopuc7079482019-02-20 05:34:37 +0000128 this->onInterestLoop(ingress, 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()) {
mzhang4eab72492015-02-25 11:16:09 -0600134 m_cs.find(interest,
ashiqopuc7079482019-02-20 05:34:37 +0000135 bind(&Forwarder::onContentStoreHit, this, ingress, pitEntry, _1, _2),
136 bind(&Forwarder::onContentStoreMiss, this, ingress, pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700137 }
mzhang4eab72492015-02-25 11:16:09 -0600138 else {
ashiqopuc7079482019-02-20 05:34:37 +0000139 this->onContentStoreMiss(ingress, pitEntry, interest);
mzhang4eab72492015-02-25 11:16:09 -0600140 }
141}
Junxiao Shic041ca32014-02-25 20:01:15 -0700142
mzhang4eab72492015-02-25 11:16:09 -0600143void
ashiqopuc7079482019-02-20 05:34:37 +0000144Forwarder::onInterestLoop(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700145{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700146 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000147 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
148 NFD_LOG_DEBUG("onInterestLoop in=" << ingress
149 << " interest=" << interest.getName() << " drop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700150 return;
151 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700152
ashiqopuc7079482019-02-20 05:34:37 +0000153 NFD_LOG_DEBUG("onInterestLoop in=" << ingress << " interest=" << interest.getName()
154 << " send-Nack-duplicate");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700155
156 // send Nack with reason=DUPLICATE
157 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
158 lp::Nack nack(interest);
159 nack.setReason(lp::NackReason::DUPLICATE);
ashiqopuc7079482019-02-20 05:34:37 +0000160 ingress.face.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700161}
162
Teng Liang7003e0b2018-03-03 16:03:30 -0700163static inline bool
164compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
165{
166 return a.getExpiry() < b.getExpiry();
167}
168
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700169void
ashiqopuc7079482019-02-20 05:34:37 +0000170Forwarder::onContentStoreMiss(const FaceEndpoint& ingress,
171 const shared_ptr<pit::Entry>& pitEntry, const Interest& interest)
mzhang4eab72492015-02-25 11:16:09 -0600172{
173 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000174 ++m_counters.nCsMisses;
mzhang4eab72492015-02-25 11:16:09 -0600175
Junxiao Shi4846f372016-04-05 13:39:30 -0700176 // insert in-record
ashiqopuc7079482019-02-20 05:34:37 +0000177 pitEntry->insertOrUpdateInRecord(ingress.face, ingress.endpoint, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700178
Teng Liang7003e0b2018-03-03 16:03:30 -0700179 // set PIT expiry timer to the time that the last PIT in-record expires
180 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
181 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
182 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700183
Junxiao Shie342e8d2016-09-18 16:48:00 +0000184 // has NextHopFaceId?
185 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
186 if (nextHopTag != nullptr) {
187 // chosen NextHop face exists?
188 Face* nextHopFace = m_faceTable.get(*nextHopTag);
189 if (nextHopFace != nullptr) {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000190 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName() << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000191 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000192 // scope control is unnecessary, because privileged app explicitly wants to forward
ashiqopuc7079482019-02-20 05:34:37 +0000193 this->onOutgoingInterest(pitEntry, FaceEndpoint(*nextHopFace, 0), interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000194 }
195 return;
196 }
197
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000198 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000199 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000200 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(ingress, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700201}
202
203void
ashiqopuc7079482019-02-20 05:34:37 +0000204Forwarder::onContentStoreHit(const FaceEndpoint& ingress, const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000205 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600206{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500207 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000208 ++m_counters.nCsHits;
mzhang4eab72492015-02-25 11:16:09 -0600209
Junxiao Shicde37ad2015-12-24 01:02:05 -0700210 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600211 // XXX should we lookup PIT for other Interests that also match csMatch?
212
Teng Liang6f09ab62018-03-01 20:04:08 -0700213 pitEntry->isSatisfied = true;
214 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
215
Teng Liang85a36632018-03-21 05:59:34 -0700216 // set PIT expiry timer to now
217 this->setExpiryTimer(pitEntry, 0_ms);
mzhang4eab72492015-02-25 11:16:09 -0600218
Teng Liang85a36632018-03-21 05:59:34 -0700219 // dispatch to strategy: after Content Store hit
220 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000221 [&] (fw::Strategy& strategy) { strategy.afterContentStoreHit(pitEntry, ingress, data); });
mzhang4eab72492015-02-25 11:16:09 -0600222}
223
Junxiao Shid3c792f2014-01-30 00:46:13 -0700224void
ashiqopuc7079482019-02-20 05:34:37 +0000225Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry,
226 const FaceEndpoint& egress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700227{
ashiqopuc7079482019-02-20 05:34:37 +0000228 NFD_LOG_DEBUG("onOutgoingInterest out=" << egress << " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700229
Junxiao Shi4846f372016-04-05 13:39:30 -0700230 // insert out-record
ashiqopuc7079482019-02-20 05:34:37 +0000231 pitEntry->insertOrUpdateOutRecord(egress.face, egress.endpoint, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700232
Junxiao Shid3c792f2014-01-30 00:46:13 -0700233 // send Interest
ashiqopuc7079482019-02-20 05:34:37 +0000234 egress.face.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700235 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700236}
237
238void
Teng Liang6f09ab62018-03-01 20:04:08 -0700239Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shia110f262014-10-12 12:35:20 -0700240{
ashiqopuc7079482019-02-20 05:34:37 +0000241 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName()
242 << (pitEntry->isSatisfied ? " satisfied" : " unsatisfied"));
Junxiao Shia110f262014-10-12 12:35:20 -0700243
244 // Dead Nonce List insert if necessary
Davide Pesavento3dade002019-03-19 11:29:56 -0600245 this->insertDeadNonceList(*pitEntry, nullptr);
Junxiao Shia110f262014-10-12 12:35:20 -0700246
Ju Pan6eb1ac92018-10-26 16:26:15 +0000247 // Increment satisfied/unsatisfied Interests counter
248 if (pitEntry->isSatisfied) {
249 ++m_counters.nSatisfiedInterests;
250 }
251 else {
252 ++m_counters.nUnsatisfiedInterests;
253 }
254
Junxiao Shif3c07812014-03-11 21:48:49 -0700255 // PIT delete
Davide Pesavento3dade002019-03-19 11:29:56 -0600256 pitEntry->expiryTimer.cancel();
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000257 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700258}
259
260void
ashiqopuc7079482019-02-20 05:34:37 +0000261Forwarder::onIncomingData(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700262{
263 // receive Data
ashiqopuc7079482019-02-20 05:34:37 +0000264 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName());
265 data.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700266 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700267
Junxiao Shi88884492014-02-15 15:57:43 -0700268 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000269 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700270 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700271 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000272 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700273 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700274 return;
275 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700276
Junxiao Shid3c792f2014-01-30 00:46:13 -0700277 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700278 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
Teng Liang43bb2312018-03-26 04:16:42 -0700279 if (pitMatches.size() == 0) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700280 // goto Data unsolicited pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000281 this->onDataUnsolicited(ingress, data);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700282 return;
283 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700284
Junxiao Shid3c792f2014-01-30 00:46:13 -0700285 // CS insert
286 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700287
Teng Liang43bb2312018-03-26 04:16:42 -0700288 // when only one PIT entry is matched, trigger strategy: after receive Data
289 if (pitMatches.size() == 1) {
290 auto& pitEntry = pitMatches.front();
Junxiao Shic041ca32014-02-25 20:01:15 -0700291
Teng Liang43bb2312018-03-26 04:16:42 -0700292 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700293
Teng Liang7003e0b2018-03-03 16:03:30 -0700294 // set PIT expiry timer to now
295 this->setExpiryTimer(pitEntry, 0_ms);
296
Teng Liang43bb2312018-03-26 04:16:42 -0700297 // trigger strategy: after receive Data
Junxiao Shib9420cf2016-08-13 04:38:52 +0000298 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000299 [&] (fw::Strategy& strategy) { strategy.afterReceiveData(pitEntry, ingress, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700300
Teng Liang43bb2312018-03-26 04:16:42 -0700301 // mark PIT satisfied
Teng Liang6f09ab62018-03-01 20:04:08 -0700302 pitEntry->isSatisfied = true;
303 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
304
Junxiao Shi4846f372016-04-05 13:39:30 -0700305 // Dead Nonce List insert if necessary (for out-record of inFace)
ashiqopuc7079482019-02-20 05:34:37 +0000306 this->insertDeadNonceList(*pitEntry, &ingress.face);
Junxiao Shia110f262014-10-12 12:35:20 -0700307
Teng Liang43bb2312018-03-26 04:16:42 -0700308 // delete PIT entry's out-record
ashiqopuc7079482019-02-20 05:34:37 +0000309 pitEntry->deleteOutRecord(ingress.face, ingress.endpoint);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700310 }
Teng Liang43bb2312018-03-26 04:16:42 -0700311 // when more than one PIT entry is matched, trigger strategy: before satisfy Interest,
312 // and send Data to all matched out faces
313 else {
ashiqopuc7079482019-02-20 05:34:37 +0000314 std::set<std::pair<Face*, EndpointId>> pendingDownstreams;
Teng Liang43bb2312018-03-26 04:16:42 -0700315 auto now = time::steady_clock::now();
Junxiao Shic041ca32014-02-25 20:01:15 -0700316
Teng Liang43bb2312018-03-26 04:16:42 -0700317 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
318 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
319
320 // remember pending downstreams
321 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
322 if (inRecord.getExpiry() > now) {
ashiqopuc7079482019-02-20 05:34:37 +0000323 pendingDownstreams.emplace(&inRecord.getFace(), inRecord.getEndpointId());
Teng Liang43bb2312018-03-26 04:16:42 -0700324 }
325 }
326
327 // set PIT expiry timer to now
328 this->setExpiryTimer(pitEntry, 0_ms);
329
330 // invoke PIT satisfy callback
331 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000332 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, ingress, data); });
Teng Liang43bb2312018-03-26 04:16:42 -0700333
334 // mark PIT satisfied
335 pitEntry->isSatisfied = true;
336 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
337
338 // Dead Nonce List insert if necessary (for out-record of inFace)
ashiqopuc7079482019-02-20 05:34:37 +0000339 this->insertDeadNonceList(*pitEntry, &ingress.face);
Teng Liang43bb2312018-03-26 04:16:42 -0700340
341 // clear PIT entry's in and out records
342 pitEntry->clearInRecords();
ashiqopuc7079482019-02-20 05:34:37 +0000343 pitEntry->deleteOutRecord(ingress.face, ingress.endpoint);
Junxiao Shida006f52014-05-16 11:18:00 -0700344 }
Teng Liang43bb2312018-03-26 04:16:42 -0700345
346 // foreach pending downstream
ashiqopuc7079482019-02-20 05:34:37 +0000347 for (const auto& pendingDownstream : pendingDownstreams) {
348 if (pendingDownstream.first->getId() == ingress.face.getId() &&
349 pendingDownstream.second == ingress.endpoint &&
350 pendingDownstream.first->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Teng Liang43bb2312018-03-26 04:16:42 -0700351 continue;
352 }
353 // goto outgoing Data pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000354 this->onOutgoingData(data, FaceEndpoint(*pendingDownstream.first, pendingDownstream.second));
Teng Liang43bb2312018-03-26 04:16:42 -0700355 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700356 }
357}
358
359void
ashiqopuc7079482019-02-20 05:34:37 +0000360Forwarder::onDataUnsolicited(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700361{
362 // accept to cache?
ashiqopuc7079482019-02-20 05:34:37 +0000363 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(ingress.face, data);
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000364 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700365 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700366 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700367 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700368
ashiqopuc7079482019-02-20 05:34:37 +0000369 NFD_LOG_DEBUG("onDataUnsolicited in=" << ingress << " data=" << data.getName() << " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700370}
371
372void
ashiqopuc7079482019-02-20 05:34:37 +0000373Forwarder::onOutgoingData(const Data& data, FaceEndpoint egress)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700374{
ashiqopuc7079482019-02-20 05:34:37 +0000375 if (egress.face.getId() == face::INVALID_FACEID) {
376 NFD_LOG_WARN("onOutgoingData out=(invalid) data=" << data.getName());
Junxiao Shi223271b2014-07-03 22:06:13 -0700377 return;
378 }
ashiqopuc7079482019-02-20 05:34:37 +0000379 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700380
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700381 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000382 bool isViolatingLocalhost = egress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700383 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700384 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000385 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700386 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700387 return;
388 }
389
Junxiao Shif3c07812014-03-11 21:48:49 -0700390 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700391
Junxiao Shid3c792f2014-01-30 00:46:13 -0700392 // send Data
ashiqopuc7079482019-02-20 05:34:37 +0000393 egress.face.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700394 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700395}
396
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700397void
ashiqopuc7079482019-02-20 05:34:37 +0000398Forwarder::onIncomingNack(const FaceEndpoint& ingress, const lp::Nack& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700399{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000400 // receive Nack
ashiqopuc7079482019-02-20 05:34:37 +0000401 nack.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700402 ++m_counters.nInNacks;
403
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700404 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000405 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
406 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
407 << "~" << nack.getReason() << " face-is-multi-access");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700408 return;
409 }
410
411 // PIT match
412 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
413 // if no PIT entry found, drop
414 if (pitEntry == nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000415 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
416 << "~" << nack.getReason() << " no-PIT-entry");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700417 return;
418 }
419
420 // has out-record?
ashiqopuc7079482019-02-20 05:34:37 +0000421 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(ingress.face, ingress.endpoint);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700422 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700423 if (outRecord == pitEntry->out_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000424 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
425 << "~" << nack.getReason() << " no-out-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700426 return;
427 }
428
429 // if out-record has different Nonce, drop
430 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
ashiqopuc7079482019-02-20 05:34:37 +0000431 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
432 << "~" << nack.getReason() << " wrong-Nonce " << nack.getInterest().getNonce()
433 << "!=" << outRecord->getLastNonce());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700434 return;
435 }
436
ashiqopuc7079482019-02-20 05:34:37 +0000437 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
438 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700439
440 // record Nack on out-record
441 outRecord->setIncomingNack(nack);
442
Teng Liang7003e0b2018-03-03 16:03:30 -0700443 // set PIT expiry timer to now when all out-record receive Nack
444 if (!fw::hasPendingOutRecords(*pitEntry)) {
445 this->setExpiryTimer(pitEntry, 0_ms);
446 }
447
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700448 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000449 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000450 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(ingress, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700451}
452
453void
ashiqopuc7079482019-02-20 05:34:37 +0000454Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry,
455 const FaceEndpoint& egress, const lp::NackHeader& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700456{
ashiqopuc7079482019-02-20 05:34:37 +0000457 if (egress.face.getId() == face::INVALID_FACEID) {
458 NFD_LOG_WARN("onOutgoingNack out=(invalid)"
459 << " nack=" << pitEntry->getInterest().getName()
460 << "~" << nack.getReason() << " no-in-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700461 return;
462 }
463
464 // has in-record?
ashiqopuc7079482019-02-20 05:34:37 +0000465 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(egress.face, egress.endpoint);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700466
467 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700468 if (inRecord == pitEntry->in_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000469 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
470 << " nack=" << pitEntry->getInterest().getName()
471 << "~" << nack.getReason() << " no-in-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700472 return;
473 }
474
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700475 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000476 if (egress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
477 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
478 << " nack=" << pitEntry->getInterest().getName()
479 << "~" << nack.getReason() << " face-is-multi-access");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700480 return;
481 }
482
ashiqopuc7079482019-02-20 05:34:37 +0000483 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
484 << " nack=" << pitEntry->getInterest().getName()
485 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700486
487 // create Nack packet with the Interest from in-record
488 lp::Nack nackPkt(inRecord->getInterest());
489 nackPkt.setHeader(nack);
490
491 // erase in-record
ashiqopuc7079482019-02-20 05:34:37 +0000492 pitEntry->deleteInRecord(egress.face, egress.endpoint);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700493
494 // send Nack on face
ashiqopuc7079482019-02-20 05:34:37 +0000495 egress.face.sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700496 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700497}
498
Eric Newberry41aba102017-11-01 16:42:13 -0700499void
ashiqopuc7079482019-02-20 05:34:37 +0000500Forwarder::onDroppedInterest(const FaceEndpoint& egress, const Interest& interest)
Eric Newberry41aba102017-11-01 16:42:13 -0700501{
ashiqopuc7079482019-02-20 05:34:37 +0000502 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(egress, interest);
Eric Newberry41aba102017-11-01 16:42:13 -0700503}
504
Junxiao Shid3c792f2014-01-30 00:46:13 -0700505void
Teng Liang7003e0b2018-03-03 16:03:30 -0700506Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700507{
Teng Liang39465c22018-11-12 19:43:04 -0700508 BOOST_ASSERT(pitEntry);
Teng Liang7003e0b2018-03-03 16:03:30 -0700509 BOOST_ASSERT(duration >= 0_ms);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700510
Davide Pesavento3dade002019-03-19 11:29:56 -0600511 pitEntry->expiryTimer.cancel();
512 pitEntry->expiryTimer = getScheduler().schedule(duration, [=] { onInterestFinalize(pitEntry); });
Junxiao Shia110f262014-10-12 12:35:20 -0700513}
514
515void
Teng Liang6f09ab62018-03-01 20:04:08 -0700516Forwarder::insertDeadNonceList(pit::Entry& pitEntry, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700517{
518 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000519 bool needDnl = true;
Teng Liang6f09ab62018-03-01 20:04:08 -0700520 if (pitEntry.isSatisfied) {
521 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
Junxiao Shia110f262014-10-12 12:35:20 -0700522 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Teng Liang6f09ab62018-03-01 20:04:08 -0700523 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700524 }
525
526 if (!needDnl) {
527 return;
528 }
529
530 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000531 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700532 // insert all outgoing Nonces
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400533 const auto& outRecords = pitEntry.getOutRecords();
534 std::for_each(outRecords.begin(), outRecords.end(), [&] (const auto& outRecord) {
535 m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
536 });
Junxiao Shia110f262014-10-12 12:35:20 -0700537 }
538 else {
539 // insert outgoing Nonce of a specific face
ashiqopud3ae85d2019-02-17 02:29:55 +0000540 auto outRecord = pitEntry.getOutRecord(*upstream, 0);
Junxiao Shia110f262014-10-12 12:35:20 -0700541 if (outRecord != pitEntry.getOutRecords().end()) {
542 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
543 }
544 }
545}
546
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800547} // namespace nfd