blob: c9f2d1d6d0671b9a9f6181032821dbb359dddc12 [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 Afanasyeva0d39a72018-08-06 17:28:36 -040037#include "face/null-face.hpp"
38
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080039namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080040
Davide Pesaventoa3148082018-04-12 18:21:54 -040041NFD_LOG_INIT(Forwarder);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070042
Junxiao Shic34d1672016-12-09 15:57:59 +000043static Name
44getDefaultStrategyName()
45{
Junxiao Shi037f4ab2016-12-13 04:27:06 +000046 return fw::BestRouteStrategy2::getStrategyName();
Junxiao Shic34d1672016-12-09 15:57:59 +000047}
48
Junxiao Shic041ca32014-02-25 20:01:15 -070049Forwarder::Forwarder()
Davide Pesavento3dade002019-03-19 11:29:56 -060050 : m_unsolicitedDataPolicy(make_unique<fw::DefaultUnsolicitedDataPolicy>())
Junxiao Shifbe8efe2016-08-22 16:02:30 +000051 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060052 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080053 , m_measurements(m_nameTree)
Junxiao Shic34d1672016-12-09 15:57:59 +000054 , m_strategyChoice(*this)
Alexander Afanasyeva0d39a72018-08-06 17:28:36 -040055 , m_csFace(face::makeNullFace(FaceUri("contentstore://")))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080056{
Alexander Afanasyeva0d39a72018-08-06 17:28:36 -040057 getFaceTable().addReserved(m_csFace, face::FACEID_CONTENT_STORE);
58
Junxiao Shidcffdaa2016-07-26 02:23:56 +000059 m_faceTable.afterAdd.connect([this] (Face& face) {
60 face.afterReceiveInterest.connect(
61 [this, &face] (const Interest& interest) {
ashiqopuc7079482019-02-20 05:34:37 +000062 this->startProcessInterest(FaceEndpoint(face, 0), interest);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000063 });
64 face.afterReceiveData.connect(
65 [this, &face] (const Data& data) {
ashiqopuc7079482019-02-20 05:34:37 +000066 this->startProcessData(FaceEndpoint(face, 0), data);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000067 });
68 face.afterReceiveNack.connect(
69 [this, &face] (const lp::Nack& nack) {
ashiqopuc7079482019-02-20 05:34:37 +000070 this->startProcessNack(FaceEndpoint(face, 0), nack);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000071 });
Eric Newberry41aba102017-11-01 16:42:13 -070072 face.onDroppedInterest.connect(
73 [this, &face] (const Interest& interest) {
ashiqopuc7079482019-02-20 05:34:37 +000074 this->onDroppedInterest(FaceEndpoint(face, 0), interest);
Eric Newberry41aba102017-11-01 16:42:13 -070075 });
Junxiao Shidcffdaa2016-07-26 02:23:56 +000076 });
77
78 m_faceTable.beforeRemove.connect([this] (Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000079 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000080 });
Junxiao Shic34d1672016-12-09 15:57:59 +000081
82 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080083}
84
Junxiao Shidcffdaa2016-07-26 02:23:56 +000085Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060086
Junxiao Shi0355e9f2015-09-02 07:24:53 -070087void
ashiqopuc7079482019-02-20 05:34:37 +000088Forwarder::onIncomingInterest(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -070089{
90 // receive Interest
ashiqopuc7079482019-02-20 05:34:37 +000091 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress << " interest=" << interest.getName());
92 interest.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.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
ashiqopuc7079482019-02-20 05:34:37 +000096 bool isViolatingLocalhost = ingress.face.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) {
ashiqopuc7079482019-02-20 05:34:37 +000099 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
100 << " interest=" << interest.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700101 // (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
ashiqopuc7079482019-02-20 05:34:37 +0000109 this->onInterestLoop(ingress, interest);
Junxiao Shi330136a2016-03-10 04:53:08 -0700110 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())) {
ashiqopuc7079482019-02-20 05:34:37 +0000116 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
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
ashiqopuc7079482019-02-20 05:34:37 +0000125 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), ingress.face);
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000126 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
ashiqopuc7079482019-02-20 05:34:37 +0000127 if (ingress.face.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000128 // 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
ashiqopuc7079482019-02-20 05:34:37 +0000133 this->onInterestLoop(ingress, 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()) {
mzhang4eab72492015-02-25 11:16:09 -0600139 m_cs.find(interest,
ashiqopuc7079482019-02-20 05:34:37 +0000140 bind(&Forwarder::onContentStoreHit, this, ingress, pitEntry, _1, _2),
141 bind(&Forwarder::onContentStoreMiss, this, ingress, pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700142 }
mzhang4eab72492015-02-25 11:16:09 -0600143 else {
ashiqopuc7079482019-02-20 05:34:37 +0000144 this->onContentStoreMiss(ingress, pitEntry, interest);
mzhang4eab72492015-02-25 11:16:09 -0600145 }
146}
Junxiao Shic041ca32014-02-25 20:01:15 -0700147
mzhang4eab72492015-02-25 11:16:09 -0600148void
ashiqopuc7079482019-02-20 05:34:37 +0000149Forwarder::onInterestLoop(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700150{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700151 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000152 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
153 NFD_LOG_DEBUG("onInterestLoop in=" << ingress
154 << " interest=" << interest.getName() << " drop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700155 return;
156 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700157
ashiqopuc7079482019-02-20 05:34:37 +0000158 NFD_LOG_DEBUG("onInterestLoop in=" << ingress << " interest=" << interest.getName()
159 << " send-Nack-duplicate");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700160
161 // send Nack with reason=DUPLICATE
162 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
163 lp::Nack nack(interest);
164 nack.setReason(lp::NackReason::DUPLICATE);
ashiqopuc7079482019-02-20 05:34:37 +0000165 ingress.face.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700166}
167
168void
ashiqopuc7079482019-02-20 05:34:37 +0000169Forwarder::onContentStoreMiss(const FaceEndpoint& ingress,
170 const shared_ptr<pit::Entry>& pitEntry, const Interest& interest)
mzhang4eab72492015-02-25 11:16:09 -0600171{
172 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000173 ++m_counters.nCsMisses;
Alexander Afanasyevc895b762019-07-27 18:36:48 -0400174 afterCsMiss(interest);
mzhang4eab72492015-02-25 11:16:09 -0600175
Junxiao Shi4846f372016-04-05 13:39:30 -0700176 // insert in-record
Davide Pesaventob31206e2019-04-20 22:34:12 -0400177 // FIXME Strategies are not prepared to handle non-zero EndpointIds, so always insert
178 // the in-record with EndpointId=0 for now. Eventually, this pipeline will need
179 // to be refactored so that strategies can control the in-record insertion.
180 pitEntry->insertOrUpdateInRecord(ingress.face, 0, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700181
Teng Liang7003e0b2018-03-03 16:03:30 -0700182 // set PIT expiry timer to the time that the last PIT in-record expires
Davide Pesaventob31206e2019-04-20 22:34:12 -0400183 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(),
184 [] (const auto& a, const auto& b) {
185 return a.getExpiry() < b.getExpiry();
186 });
Teng Liang7003e0b2018-03-03 16:03:30 -0700187 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
188 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700189
Junxiao Shie342e8d2016-09-18 16:48:00 +0000190 // has NextHopFaceId?
Davide Pesaventob31206e2019-04-20 22:34:12 -0400191 auto nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
Junxiao Shie342e8d2016-09-18 16:48:00 +0000192 if (nextHopTag != nullptr) {
193 // chosen NextHop face exists?
194 Face* nextHopFace = m_faceTable.get(*nextHopTag);
195 if (nextHopFace != nullptr) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400196 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName()
197 << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000198 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000199 // scope control is unnecessary, because privileged app explicitly wants to forward
ashiqopuc7079482019-02-20 05:34:37 +0000200 this->onOutgoingInterest(pitEntry, FaceEndpoint(*nextHopFace, 0), interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000201 }
202 return;
203 }
204
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000205 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000206 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000207 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(ingress, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700208}
209
210void
ashiqopuc7079482019-02-20 05:34:37 +0000211Forwarder::onContentStoreHit(const FaceEndpoint& ingress, const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000212 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600213{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500214 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000215 ++m_counters.nCsHits;
Alexander Afanasyevc895b762019-07-27 18:36:48 -0400216 afterCsHit(interest, data);
mzhang4eab72492015-02-25 11:16:09 -0600217
Junxiao Shicde37ad2015-12-24 01:02:05 -0700218 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
Davide Pesaventob31206e2019-04-20 22:34:12 -0400219 // FIXME Should we lookup PIT for other Interests that also match the data?
mzhang4eab72492015-02-25 11:16:09 -0600220
Teng Liang6f09ab62018-03-01 20:04:08 -0700221 pitEntry->isSatisfied = true;
222 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
223
Teng Liang85a36632018-03-21 05:59:34 -0700224 // set PIT expiry timer to now
225 this->setExpiryTimer(pitEntry, 0_ms);
mzhang4eab72492015-02-25 11:16:09 -0600226
Alexander Afanasyeva0d39a72018-08-06 17:28:36 -0400227 beforeSatisfyInterest(*pitEntry, *m_csFace, data);
228 this->dispatchToStrategy(*pitEntry,
229 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, FaceEndpoint(*m_csFace, 0), data); });
230
Teng Liang85a36632018-03-21 05:59:34 -0700231 // dispatch to strategy: after Content Store hit
232 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000233 [&] (fw::Strategy& strategy) { strategy.afterContentStoreHit(pitEntry, ingress, data); });
mzhang4eab72492015-02-25 11:16:09 -0600234}
235
Junxiao Shid3c792f2014-01-30 00:46:13 -0700236void
ashiqopuc7079482019-02-20 05:34:37 +0000237Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry,
238 const FaceEndpoint& egress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700239{
ashiqopuc7079482019-02-20 05:34:37 +0000240 NFD_LOG_DEBUG("onOutgoingInterest out=" << egress << " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700241
Junxiao Shi4846f372016-04-05 13:39:30 -0700242 // insert out-record
ashiqopuc7079482019-02-20 05:34:37 +0000243 pitEntry->insertOrUpdateOutRecord(egress.face, egress.endpoint, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700244
Junxiao Shid3c792f2014-01-30 00:46:13 -0700245 // send Interest
ashiqopuc7079482019-02-20 05:34:37 +0000246 egress.face.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700247 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700248}
249
250void
Teng Liang6f09ab62018-03-01 20:04:08 -0700251Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shia110f262014-10-12 12:35:20 -0700252{
ashiqopuc7079482019-02-20 05:34:37 +0000253 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName()
254 << (pitEntry->isSatisfied ? " satisfied" : " unsatisfied"));
Junxiao Shia110f262014-10-12 12:35:20 -0700255
Alexander Afanasyeva0d39a72018-08-06 17:28:36 -0400256 if (!pitEntry->isSatisfied) {
257 beforeExpirePendingInterest(*pitEntry);
258 }
259
Junxiao Shia110f262014-10-12 12:35:20 -0700260 // Dead Nonce List insert if necessary
Davide Pesavento3dade002019-03-19 11:29:56 -0600261 this->insertDeadNonceList(*pitEntry, nullptr);
Junxiao Shia110f262014-10-12 12:35:20 -0700262
Ju Pan6eb1ac92018-10-26 16:26:15 +0000263 // Increment satisfied/unsatisfied Interests counter
264 if (pitEntry->isSatisfied) {
265 ++m_counters.nSatisfiedInterests;
266 }
267 else {
268 ++m_counters.nUnsatisfiedInterests;
269 }
270
Junxiao Shif3c07812014-03-11 21:48:49 -0700271 // PIT delete
Davide Pesavento3dade002019-03-19 11:29:56 -0600272 pitEntry->expiryTimer.cancel();
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000273 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700274}
275
276void
ashiqopuc7079482019-02-20 05:34:37 +0000277Forwarder::onIncomingData(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700278{
279 // receive Data
ashiqopuc7079482019-02-20 05:34:37 +0000280 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName());
281 data.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.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
ashiqopuc7079482019-02-20 05:34:37 +0000285 bool isViolatingLocalhost = ingress.face.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) {
ashiqopuc7079482019-02-20 05:34:37 +0000288 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700289 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700290 return;
291 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700292
Junxiao Shid3c792f2014-01-30 00:46:13 -0700293 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700294 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
Teng Liang43bb2312018-03-26 04:16:42 -0700295 if (pitMatches.size() == 0) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700296 // goto Data unsolicited pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000297 this->onDataUnsolicited(ingress, data);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700298 return;
299 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700300
Junxiao Shid3c792f2014-01-30 00:46:13 -0700301 // CS insert
302 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700303
Teng Liang43bb2312018-03-26 04:16:42 -0700304 // when only one PIT entry is matched, trigger strategy: after receive Data
305 if (pitMatches.size() == 1) {
306 auto& pitEntry = pitMatches.front();
Junxiao Shic041ca32014-02-25 20:01:15 -0700307
Teng Liang43bb2312018-03-26 04:16:42 -0700308 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700309
Teng Liang7003e0b2018-03-03 16:03:30 -0700310 // set PIT expiry timer to now
311 this->setExpiryTimer(pitEntry, 0_ms);
312
Alexander Afanasyeva0d39a72018-08-06 17:28:36 -0400313 beforeSatisfyInterest(*pitEntry, ingress.face, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700314 // trigger strategy: after receive Data
Junxiao Shib9420cf2016-08-13 04:38:52 +0000315 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000316 [&] (fw::Strategy& strategy) { strategy.afterReceiveData(pitEntry, ingress, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700317
Teng Liang43bb2312018-03-26 04:16:42 -0700318 // mark PIT satisfied
Teng Liang6f09ab62018-03-01 20:04:08 -0700319 pitEntry->isSatisfied = true;
320 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
321
Junxiao Shi4846f372016-04-05 13:39:30 -0700322 // Dead Nonce List insert if necessary (for out-record of inFace)
ashiqopuc7079482019-02-20 05:34:37 +0000323 this->insertDeadNonceList(*pitEntry, &ingress.face);
Junxiao Shia110f262014-10-12 12:35:20 -0700324
Teng Liang43bb2312018-03-26 04:16:42 -0700325 // delete PIT entry's out-record
ashiqopuc7079482019-02-20 05:34:37 +0000326 pitEntry->deleteOutRecord(ingress.face, ingress.endpoint);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700327 }
Teng Liang43bb2312018-03-26 04:16:42 -0700328 // when more than one PIT entry is matched, trigger strategy: before satisfy Interest,
329 // and send Data to all matched out faces
330 else {
ashiqopuc7079482019-02-20 05:34:37 +0000331 std::set<std::pair<Face*, EndpointId>> pendingDownstreams;
Teng Liang43bb2312018-03-26 04:16:42 -0700332 auto now = time::steady_clock::now();
Junxiao Shic041ca32014-02-25 20:01:15 -0700333
Davide Pesaventob31206e2019-04-20 22:34:12 -0400334 for (const auto& pitEntry : pitMatches) {
Teng Liang43bb2312018-03-26 04:16:42 -0700335 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
336
337 // remember pending downstreams
338 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
339 if (inRecord.getExpiry() > now) {
ashiqopuc7079482019-02-20 05:34:37 +0000340 pendingDownstreams.emplace(&inRecord.getFace(), inRecord.getEndpointId());
Teng Liang43bb2312018-03-26 04:16:42 -0700341 }
342 }
343
344 // set PIT expiry timer to now
345 this->setExpiryTimer(pitEntry, 0_ms);
346
347 // invoke PIT satisfy callback
Alexander Afanasyeva0d39a72018-08-06 17:28:36 -0400348 beforeSatisfyInterest(*pitEntry, ingress.face, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700349 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000350 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, ingress, data); });
Teng Liang43bb2312018-03-26 04:16:42 -0700351
352 // mark PIT satisfied
353 pitEntry->isSatisfied = true;
354 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
355
356 // Dead Nonce List insert if necessary (for out-record of inFace)
ashiqopuc7079482019-02-20 05:34:37 +0000357 this->insertDeadNonceList(*pitEntry, &ingress.face);
Teng Liang43bb2312018-03-26 04:16:42 -0700358
359 // clear PIT entry's in and out records
360 pitEntry->clearInRecords();
ashiqopuc7079482019-02-20 05:34:37 +0000361 pitEntry->deleteOutRecord(ingress.face, ingress.endpoint);
Junxiao Shida006f52014-05-16 11:18:00 -0700362 }
Teng Liang43bb2312018-03-26 04:16:42 -0700363
364 // foreach pending downstream
ashiqopuc7079482019-02-20 05:34:37 +0000365 for (const auto& pendingDownstream : pendingDownstreams) {
366 if (pendingDownstream.first->getId() == ingress.face.getId() &&
367 pendingDownstream.second == ingress.endpoint &&
368 pendingDownstream.first->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Teng Liang43bb2312018-03-26 04:16:42 -0700369 continue;
370 }
371 // goto outgoing Data pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000372 this->onOutgoingData(data, FaceEndpoint(*pendingDownstream.first, pendingDownstream.second));
Teng Liang43bb2312018-03-26 04:16:42 -0700373 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700374 }
375}
376
377void
ashiqopuc7079482019-02-20 05:34:37 +0000378Forwarder::onDataUnsolicited(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700379{
380 // accept to cache?
ashiqopuc7079482019-02-20 05:34:37 +0000381 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(ingress.face, data);
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000382 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700383 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700384 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700385 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700386
ashiqopuc7079482019-02-20 05:34:37 +0000387 NFD_LOG_DEBUG("onDataUnsolicited in=" << ingress << " data=" << data.getName() << " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700388}
389
390void
ashiqopuc7079482019-02-20 05:34:37 +0000391Forwarder::onOutgoingData(const Data& data, FaceEndpoint egress)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700392{
ashiqopuc7079482019-02-20 05:34:37 +0000393 if (egress.face.getId() == face::INVALID_FACEID) {
394 NFD_LOG_WARN("onOutgoingData out=(invalid) data=" << data.getName());
Junxiao Shi223271b2014-07-03 22:06:13 -0700395 return;
396 }
ashiqopuc7079482019-02-20 05:34:37 +0000397 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700398
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700399 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000400 bool isViolatingLocalhost = egress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700401 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700402 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000403 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700404 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700405 return;
406 }
407
Junxiao Shif3c07812014-03-11 21:48:49 -0700408 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700409
Junxiao Shid3c792f2014-01-30 00:46:13 -0700410 // send Data
ashiqopuc7079482019-02-20 05:34:37 +0000411 egress.face.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700412 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700413}
414
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700415void
ashiqopuc7079482019-02-20 05:34:37 +0000416Forwarder::onIncomingNack(const FaceEndpoint& ingress, const lp::Nack& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700417{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000418 // receive Nack
ashiqopuc7079482019-02-20 05:34:37 +0000419 nack.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700420 ++m_counters.nInNacks;
421
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700422 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000423 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400424 NFD_LOG_DEBUG("onIncomingNack in=" << ingress
425 << " nack=" << nack.getInterest().getName() << "~" << nack.getReason()
426 << " link-type=" << ingress.face.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700427 return;
428 }
429
430 // PIT match
431 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
432 // if no PIT entry found, drop
433 if (pitEntry == nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000434 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
435 << "~" << nack.getReason() << " no-PIT-entry");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700436 return;
437 }
438
439 // has out-record?
Davide Pesaventob31206e2019-04-20 22:34:12 -0400440 auto outRecord = pitEntry->getOutRecord(ingress.face, ingress.endpoint);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700441 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700442 if (outRecord == pitEntry->out_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000443 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
444 << "~" << nack.getReason() << " no-out-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700445 return;
446 }
447
448 // if out-record has different Nonce, drop
449 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
ashiqopuc7079482019-02-20 05:34:37 +0000450 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
451 << "~" << nack.getReason() << " wrong-Nonce " << nack.getInterest().getNonce()
452 << "!=" << outRecord->getLastNonce());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700453 return;
454 }
455
ashiqopuc7079482019-02-20 05:34:37 +0000456 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
457 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700458
459 // record Nack on out-record
460 outRecord->setIncomingNack(nack);
461
Teng Liang7003e0b2018-03-03 16:03:30 -0700462 // set PIT expiry timer to now when all out-record receive Nack
463 if (!fw::hasPendingOutRecords(*pitEntry)) {
464 this->setExpiryTimer(pitEntry, 0_ms);
465 }
466
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700467 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000468 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000469 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(ingress, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700470}
471
472void
ashiqopuc7079482019-02-20 05:34:37 +0000473Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry,
474 const FaceEndpoint& egress, const lp::NackHeader& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700475{
ashiqopuc7079482019-02-20 05:34:37 +0000476 if (egress.face.getId() == face::INVALID_FACEID) {
477 NFD_LOG_WARN("onOutgoingNack out=(invalid)"
Davide Pesaventob31206e2019-04-20 22:34:12 -0400478 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700479 return;
480 }
481
482 // has in-record?
Davide Pesaventob31206e2019-04-20 22:34:12 -0400483 auto inRecord = pitEntry->getInRecord(egress.face, egress.endpoint);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700484
485 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700486 if (inRecord == pitEntry->in_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000487 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
488 << " nack=" << pitEntry->getInterest().getName()
489 << "~" << nack.getReason() << " no-in-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700490 return;
491 }
492
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700493 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000494 if (egress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
495 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
Davide Pesaventob31206e2019-04-20 22:34:12 -0400496 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason()
497 << " link-type=" << egress.face.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700498 return;
499 }
500
ashiqopuc7079482019-02-20 05:34:37 +0000501 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
502 << " nack=" << pitEntry->getInterest().getName()
503 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700504
505 // create Nack packet with the Interest from in-record
506 lp::Nack nackPkt(inRecord->getInterest());
507 nackPkt.setHeader(nack);
508
509 // erase in-record
ashiqopuc7079482019-02-20 05:34:37 +0000510 pitEntry->deleteInRecord(egress.face, egress.endpoint);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700511
512 // send Nack on face
ashiqopuc7079482019-02-20 05:34:37 +0000513 egress.face.sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700514 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700515}
516
Eric Newberry41aba102017-11-01 16:42:13 -0700517void
ashiqopuc7079482019-02-20 05:34:37 +0000518Forwarder::onDroppedInterest(const FaceEndpoint& egress, const Interest& interest)
Eric Newberry41aba102017-11-01 16:42:13 -0700519{
ashiqopuc7079482019-02-20 05:34:37 +0000520 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(egress, interest);
Eric Newberry41aba102017-11-01 16:42:13 -0700521}
522
Junxiao Shid3c792f2014-01-30 00:46:13 -0700523void
Teng Liang7003e0b2018-03-03 16:03:30 -0700524Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700525{
Teng Liang39465c22018-11-12 19:43:04 -0700526 BOOST_ASSERT(pitEntry);
Teng Liang7003e0b2018-03-03 16:03:30 -0700527 BOOST_ASSERT(duration >= 0_ms);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700528
Davide Pesavento3dade002019-03-19 11:29:56 -0600529 pitEntry->expiryTimer.cancel();
530 pitEntry->expiryTimer = getScheduler().schedule(duration, [=] { onInterestFinalize(pitEntry); });
Junxiao Shia110f262014-10-12 12:35:20 -0700531}
532
533void
Teng Liang6f09ab62018-03-01 20:04:08 -0700534Forwarder::insertDeadNonceList(pit::Entry& pitEntry, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700535{
536 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000537 bool needDnl = true;
Teng Liang6f09ab62018-03-01 20:04:08 -0700538 if (pitEntry.isSatisfied) {
539 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
Junxiao Shia110f262014-10-12 12:35:20 -0700540 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Teng Liang6f09ab62018-03-01 20:04:08 -0700541 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700542 }
543
544 if (!needDnl) {
545 return;
546 }
547
548 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000549 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700550 // insert all outgoing Nonces
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400551 const auto& outRecords = pitEntry.getOutRecords();
552 std::for_each(outRecords.begin(), outRecords.end(), [&] (const auto& outRecord) {
553 m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
554 });
Junxiao Shia110f262014-10-12 12:35:20 -0700555 }
556 else {
557 // insert outgoing Nonce of a specific face
ashiqopud3ae85d2019-02-17 02:29:55 +0000558 auto outRecord = pitEntry.getOutRecord(*upstream, 0);
Junxiao Shia110f262014-10-12 12:35:20 -0700559 if (outRecord != pitEntry.getOutRecords().end()) {
560 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
561 }
562 }
563}
564
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800565} // namespace nfd