blob: c88386d1d02f388e8c9faa78ffbfc8ea26231e0d [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 Afanasyev54c4c5d2018-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
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040049Forwarder::Forwarder(FaceTable& faceTable)
50 : m_faceTable(faceTable)
51 , m_unsolicitedDataPolicy(make_unique<fw::DefaultUnsolicitedDataPolicy>())
Junxiao Shifbe8efe2016-08-22 16:02:30 +000052 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060053 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080054 , m_measurements(m_nameTree)
Junxiao Shic34d1672016-12-09 15:57:59 +000055 , m_strategyChoice(*this)
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -040056 , m_csFace(face::makeNullFace(FaceUri("contentstore://")))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080057{
Alexander Afanasyevd1d38c82020-02-21 16:37:25 -050058 m_faceTable.addReserved(m_csFace, face::FACEID_CONTENT_STORE);
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -040059
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040060 m_faceTable.afterAdd.connect([this] (const Face& face) {
Junxiao Shidcffdaa2016-07-26 02:23:56 +000061 face.afterReceiveInterest.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000062 [this, &face] (const Interest& interest, const EndpointId& endpointId) {
63 this->startProcessInterest(FaceEndpoint(face, endpointId), interest);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000064 });
65 face.afterReceiveData.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000066 [this, &face] (const Data& data, const EndpointId& endpointId) {
67 this->startProcessData(FaceEndpoint(face, endpointId), data);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000068 });
69 face.afterReceiveNack.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000070 [this, &face] (const lp::Nack& nack, const EndpointId& endpointId) {
71 this->startProcessNack(FaceEndpoint(face, endpointId), nack);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000072 });
Eric Newberry41aba102017-11-01 16:42:13 -070073 face.onDroppedInterest.connect(
74 [this, &face] (const Interest& interest) {
ashiqopuc7079482019-02-20 05:34:37 +000075 this->onDroppedInterest(FaceEndpoint(face, 0), interest);
Eric Newberry41aba102017-11-01 16:42:13 -070076 });
Junxiao Shidcffdaa2016-07-26 02:23:56 +000077 });
78
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040079 m_faceTable.beforeRemove.connect([this] (const Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000080 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000081 });
Junxiao Shic34d1672016-12-09 15:57:59 +000082
Ju Pan2feb4592019-09-16 20:56:38 +000083 m_fib.afterNewNextHop.connect([&] (const Name& prefix, const fib::NextHop& nextHop) {
84 this->startProcessNewNextHop(prefix, nextHop);
85 });
86
Junxiao Shic34d1672016-12-09 15:57:59 +000087 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080088}
89
Junxiao Shidcffdaa2016-07-26 02:23:56 +000090Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060091
Junxiao Shi0355e9f2015-09-02 07:24:53 -070092void
ashiqopuc7079482019-02-20 05:34:37 +000093Forwarder::onIncomingInterest(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -070094{
95 // receive Interest
ashiqopuc7079482019-02-20 05:34:37 +000096 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress << " interest=" << interest.getName());
97 interest.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -070098 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -070099
Junxiao Shi88884492014-02-15 15:57:43 -0700100 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000101 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700102 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700103 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000104 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
105 << " interest=" << interest.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700106 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700107 return;
108 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700109
Junxiao Shi330136a2016-03-10 04:53:08 -0700110 // detect duplicate Nonce with Dead Nonce List
111 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
112 if (hasDuplicateNonceInDnl) {
113 // goto Interest loop pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000114 this->onInterestLoop(ingress, interest);
Junxiao Shi330136a2016-03-10 04:53:08 -0700115 return;
116 }
117
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000118 // strip forwarding hint if Interest has reached producer region
119 if (!interest.getForwardingHint().empty() &&
120 m_networkRegionTable.isInProducerRegion(interest.getForwardingHint())) {
ashiqopuc7079482019-02-20 05:34:37 +0000121 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
122 << " interest=" << interest.getName() << " reaching-producer-region");
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000123 const_cast<Interest&>(interest).setForwardingHint({});
Junxiao Shif9858fd2017-02-01 16:22:44 +0000124 }
125
Junxiao Shid3c792f2014-01-30 00:46:13 -0700126 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700127 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700128
Junxiao Shi330136a2016-03-10 04:53:08 -0700129 // detect duplicate Nonce in PIT entry
ashiqopuc7079482019-02-20 05:34:37 +0000130 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), ingress.face);
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000131 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
ashiqopuc7079482019-02-20 05:34:37 +0000132 if (ingress.face.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000133 // for p2p face: duplicate Nonce from same incoming face is not loop
134 hasDuplicateNonceInPit = hasDuplicateNonceInPit && !(dnw & fw::DUPLICATE_NONCE_IN_SAME);
135 }
Junxiao Shi330136a2016-03-10 04:53:08 -0700136 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700137 // goto Interest loop pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000138 this->onInterestLoop(ingress, interest);
Alexander Afanasyev7859f0d2019-07-29 11:45:37 -0400139 this->dispatchToStrategy(*pitEntry,
140 [&] (fw::Strategy& strategy) { strategy.afterReceiveLoopedInterest(ingress, interest, *pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700141 return;
142 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700143
Junxiao Shif3c07812014-03-11 21:48:49 -0700144 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700145 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600146 m_cs.find(interest,
ashiqopuc7079482019-02-20 05:34:37 +0000147 bind(&Forwarder::onContentStoreHit, this, ingress, pitEntry, _1, _2),
148 bind(&Forwarder::onContentStoreMiss, this, ingress, pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700149 }
mzhang4eab72492015-02-25 11:16:09 -0600150 else {
ashiqopuc7079482019-02-20 05:34:37 +0000151 this->onContentStoreMiss(ingress, pitEntry, interest);
mzhang4eab72492015-02-25 11:16:09 -0600152 }
153}
Junxiao Shic041ca32014-02-25 20:01:15 -0700154
mzhang4eab72492015-02-25 11:16:09 -0600155void
ashiqopuc7079482019-02-20 05:34:37 +0000156Forwarder::onInterestLoop(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700157{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700158 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000159 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
160 NFD_LOG_DEBUG("onInterestLoop in=" << ingress
161 << " interest=" << interest.getName() << " drop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700162 return;
163 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700164
ashiqopuc7079482019-02-20 05:34:37 +0000165 NFD_LOG_DEBUG("onInterestLoop in=" << ingress << " interest=" << interest.getName()
166 << " send-Nack-duplicate");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700167
168 // send Nack with reason=DUPLICATE
169 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
170 lp::Nack nack(interest);
171 nack.setReason(lp::NackReason::DUPLICATE);
ashiqopu075bb7d2019-03-10 01:38:21 +0000172 ingress.face.sendNack(nack, ingress.endpoint);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700173}
174
175void
ashiqopuc7079482019-02-20 05:34:37 +0000176Forwarder::onContentStoreMiss(const FaceEndpoint& ingress,
177 const shared_ptr<pit::Entry>& pitEntry, const Interest& interest)
mzhang4eab72492015-02-25 11:16:09 -0600178{
179 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000180 ++m_counters.nCsMisses;
Alexander Afanasyev48ba4c12019-07-27 18:36:48 -0400181 afterCsMiss(interest);
mzhang4eab72492015-02-25 11:16:09 -0600182
Md Ashiqur Rahman115ea632019-07-06 02:34:31 +0000183 // insert in-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000184 pitEntry->insertOrUpdateInRecord(ingress.face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700185
Teng Liang7003e0b2018-03-03 16:03:30 -0700186 // set PIT expiry timer to the time that the last PIT in-record expires
Davide Pesaventob31206e2019-04-20 22:34:12 -0400187 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(),
188 [] (const auto& a, const auto& b) {
189 return a.getExpiry() < b.getExpiry();
190 });
Teng Liang7003e0b2018-03-03 16:03:30 -0700191 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
192 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700193
Junxiao Shie342e8d2016-09-18 16:48:00 +0000194 // has NextHopFaceId?
Davide Pesaventob31206e2019-04-20 22:34:12 -0400195 auto nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
Junxiao Shie342e8d2016-09-18 16:48:00 +0000196 if (nextHopTag != nullptr) {
197 // chosen NextHop face exists?
198 Face* nextHopFace = m_faceTable.get(*nextHopTag);
199 if (nextHopFace != nullptr) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400200 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName()
201 << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000202 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000203 // scope control is unnecessary, because privileged app explicitly wants to forward
ashiqopuc7079482019-02-20 05:34:37 +0000204 this->onOutgoingInterest(pitEntry, FaceEndpoint(*nextHopFace, 0), interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000205 }
206 return;
207 }
208
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000209 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000210 this->dispatchToStrategy(*pitEntry,
Md Ashiqur Rahman115ea632019-07-06 02:34:31 +0000211 [&] (fw::Strategy& strategy) {
212 strategy.afterReceiveInterest(FaceEndpoint(ingress.face, 0), interest, pitEntry);
213 });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700214}
215
216void
ashiqopuc7079482019-02-20 05:34:37 +0000217Forwarder::onContentStoreHit(const FaceEndpoint& ingress, const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000218 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600219{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500220 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000221 ++m_counters.nCsHits;
Alexander Afanasyev48ba4c12019-07-27 18:36:48 -0400222 afterCsHit(interest, data);
mzhang4eab72492015-02-25 11:16:09 -0600223
Junxiao Shicde37ad2015-12-24 01:02:05 -0700224 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
Davide Pesaventob31206e2019-04-20 22:34:12 -0400225 // FIXME Should we lookup PIT for other Interests that also match the data?
mzhang4eab72492015-02-25 11:16:09 -0600226
Teng Liang6f09ab62018-03-01 20:04:08 -0700227 pitEntry->isSatisfied = true;
228 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
229
Teng Liang85a36632018-03-21 05:59:34 -0700230 // set PIT expiry timer to now
231 this->setExpiryTimer(pitEntry, 0_ms);
mzhang4eab72492015-02-25 11:16:09 -0600232
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -0400233 beforeSatisfyInterest(*pitEntry, *m_csFace, data);
234 this->dispatchToStrategy(*pitEntry,
235 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, FaceEndpoint(*m_csFace, 0), data); });
236
Teng Liang85a36632018-03-21 05:59:34 -0700237 // dispatch to strategy: after Content Store hit
238 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000239 [&] (fw::Strategy& strategy) { strategy.afterContentStoreHit(pitEntry, ingress, data); });
mzhang4eab72492015-02-25 11:16:09 -0600240}
241
Junxiao Shid3c792f2014-01-30 00:46:13 -0700242void
ashiqopuc7079482019-02-20 05:34:37 +0000243Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry,
244 const FaceEndpoint& egress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700245{
ashiqopuc7079482019-02-20 05:34:37 +0000246 NFD_LOG_DEBUG("onOutgoingInterest out=" << egress << " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700247
Junxiao Shi4846f372016-04-05 13:39:30 -0700248 // insert out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000249 pitEntry->insertOrUpdateOutRecord(egress.face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700250
Junxiao Shid3c792f2014-01-30 00:46:13 -0700251 // send Interest
ashiqopu075bb7d2019-03-10 01:38:21 +0000252 egress.face.sendInterest(interest, egress.endpoint);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700253 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700254}
255
256void
Teng Liang6f09ab62018-03-01 20:04:08 -0700257Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shia110f262014-10-12 12:35:20 -0700258{
ashiqopuc7079482019-02-20 05:34:37 +0000259 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName()
260 << (pitEntry->isSatisfied ? " satisfied" : " unsatisfied"));
Junxiao Shia110f262014-10-12 12:35:20 -0700261
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -0400262 if (!pitEntry->isSatisfied) {
263 beforeExpirePendingInterest(*pitEntry);
264 }
265
Junxiao Shia110f262014-10-12 12:35:20 -0700266 // Dead Nonce List insert if necessary
Davide Pesavento3dade002019-03-19 11:29:56 -0600267 this->insertDeadNonceList(*pitEntry, nullptr);
Junxiao Shia110f262014-10-12 12:35:20 -0700268
Ju Pan6eb1ac92018-10-26 16:26:15 +0000269 // Increment satisfied/unsatisfied Interests counter
270 if (pitEntry->isSatisfied) {
271 ++m_counters.nSatisfiedInterests;
272 }
273 else {
274 ++m_counters.nUnsatisfiedInterests;
275 }
276
Junxiao Shif3c07812014-03-11 21:48:49 -0700277 // PIT delete
Davide Pesavento3dade002019-03-19 11:29:56 -0600278 pitEntry->expiryTimer.cancel();
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000279 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700280}
281
282void
ashiqopuc7079482019-02-20 05:34:37 +0000283Forwarder::onIncomingData(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700284{
285 // receive Data
ashiqopuc7079482019-02-20 05:34:37 +0000286 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName());
287 data.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700288 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700289
Junxiao Shi88884492014-02-15 15:57:43 -0700290 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000291 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700292 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700293 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000294 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700295 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700296 return;
297 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700298
Junxiao Shid3c792f2014-01-30 00:46:13 -0700299 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700300 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
Teng Liang43bb2312018-03-26 04:16:42 -0700301 if (pitMatches.size() == 0) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700302 // goto Data unsolicited pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000303 this->onDataUnsolicited(ingress, data);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700304 return;
305 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700306
Junxiao Shid3c792f2014-01-30 00:46:13 -0700307 // CS insert
308 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700309
Teng Liang43bb2312018-03-26 04:16:42 -0700310 // when only one PIT entry is matched, trigger strategy: after receive Data
311 if (pitMatches.size() == 1) {
312 auto& pitEntry = pitMatches.front();
Junxiao Shic041ca32014-02-25 20:01:15 -0700313
Teng Liang43bb2312018-03-26 04:16:42 -0700314 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700315
Teng Liang7003e0b2018-03-03 16:03:30 -0700316 // set PIT expiry timer to now
317 this->setExpiryTimer(pitEntry, 0_ms);
318
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -0400319 beforeSatisfyInterest(*pitEntry, ingress.face, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700320 // trigger strategy: after receive Data
Junxiao Shib9420cf2016-08-13 04:38:52 +0000321 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000322 [&] (fw::Strategy& strategy) { strategy.afterReceiveData(pitEntry, ingress, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700323
Teng Liang43bb2312018-03-26 04:16:42 -0700324 // mark PIT satisfied
Teng Liang6f09ab62018-03-01 20:04:08 -0700325 pitEntry->isSatisfied = true;
326 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
327
Junxiao Shi4846f372016-04-05 13:39:30 -0700328 // Dead Nonce List insert if necessary (for out-record of inFace)
ashiqopuc7079482019-02-20 05:34:37 +0000329 this->insertDeadNonceList(*pitEntry, &ingress.face);
Junxiao Shia110f262014-10-12 12:35:20 -0700330
Teng Liang43bb2312018-03-26 04:16:42 -0700331 // delete PIT entry's out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000332 pitEntry->deleteOutRecord(ingress.face);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700333 }
Teng Liang43bb2312018-03-26 04:16:42 -0700334 // when more than one PIT entry is matched, trigger strategy: before satisfy Interest,
335 // and send Data to all matched out faces
336 else {
ashiqopuc7079482019-02-20 05:34:37 +0000337 std::set<std::pair<Face*, EndpointId>> pendingDownstreams;
Teng Liang43bb2312018-03-26 04:16:42 -0700338 auto now = time::steady_clock::now();
Junxiao Shic041ca32014-02-25 20:01:15 -0700339
Davide Pesaventob31206e2019-04-20 22:34:12 -0400340 for (const auto& pitEntry : pitMatches) {
Teng Liang43bb2312018-03-26 04:16:42 -0700341 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
342
343 // remember pending downstreams
344 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
345 if (inRecord.getExpiry() > now) {
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000346 pendingDownstreams.emplace(&inRecord.getFace(), 0);
Teng Liang43bb2312018-03-26 04:16:42 -0700347 }
348 }
349
350 // set PIT expiry timer to now
351 this->setExpiryTimer(pitEntry, 0_ms);
352
353 // invoke PIT satisfy callback
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -0400354 beforeSatisfyInterest(*pitEntry, ingress.face, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700355 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000356 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, ingress, data); });
Teng Liang43bb2312018-03-26 04:16:42 -0700357
358 // mark PIT satisfied
359 pitEntry->isSatisfied = true;
360 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
361
362 // Dead Nonce List insert if necessary (for out-record of inFace)
ashiqopuc7079482019-02-20 05:34:37 +0000363 this->insertDeadNonceList(*pitEntry, &ingress.face);
Teng Liang43bb2312018-03-26 04:16:42 -0700364
365 // clear PIT entry's in and out records
366 pitEntry->clearInRecords();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000367 pitEntry->deleteOutRecord(ingress.face);
Junxiao Shida006f52014-05-16 11:18:00 -0700368 }
Teng Liang43bb2312018-03-26 04:16:42 -0700369
370 // foreach pending downstream
ashiqopuc7079482019-02-20 05:34:37 +0000371 for (const auto& pendingDownstream : pendingDownstreams) {
372 if (pendingDownstream.first->getId() == ingress.face.getId() &&
373 pendingDownstream.second == ingress.endpoint &&
374 pendingDownstream.first->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Teng Liang43bb2312018-03-26 04:16:42 -0700375 continue;
376 }
377 // goto outgoing Data pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000378 this->onOutgoingData(data, FaceEndpoint(*pendingDownstream.first, pendingDownstream.second));
Teng Liang43bb2312018-03-26 04:16:42 -0700379 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700380 }
381}
382
383void
ashiqopuc7079482019-02-20 05:34:37 +0000384Forwarder::onDataUnsolicited(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700385{
386 // accept to cache?
ashiqopuc7079482019-02-20 05:34:37 +0000387 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(ingress.face, data);
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000388 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700389 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700390 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700391 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700392
ashiqopuc7079482019-02-20 05:34:37 +0000393 NFD_LOG_DEBUG("onDataUnsolicited in=" << ingress << " data=" << data.getName() << " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700394}
395
396void
ashiqopu075bb7d2019-03-10 01:38:21 +0000397Forwarder::onOutgoingData(const Data& data, const FaceEndpoint& egress)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700398{
ashiqopuc7079482019-02-20 05:34:37 +0000399 if (egress.face.getId() == face::INVALID_FACEID) {
400 NFD_LOG_WARN("onOutgoingData out=(invalid) data=" << data.getName());
Junxiao Shi223271b2014-07-03 22:06:13 -0700401 return;
402 }
ashiqopuc7079482019-02-20 05:34:37 +0000403 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700404
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700405 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000406 bool isViolatingLocalhost = egress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700407 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700408 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000409 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700410 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700411 return;
412 }
413
Junxiao Shif3c07812014-03-11 21:48:49 -0700414 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700415
Junxiao Shid3c792f2014-01-30 00:46:13 -0700416 // send Data
ashiqopu075bb7d2019-03-10 01:38:21 +0000417 egress.face.sendData(data, egress.endpoint);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700418 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700419}
420
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700421void
ashiqopuc7079482019-02-20 05:34:37 +0000422Forwarder::onIncomingNack(const FaceEndpoint& ingress, const lp::Nack& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700423{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000424 // receive Nack
ashiqopuc7079482019-02-20 05:34:37 +0000425 nack.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700426 ++m_counters.nInNacks;
427
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700428 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000429 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400430 NFD_LOG_DEBUG("onIncomingNack in=" << ingress
431 << " nack=" << nack.getInterest().getName() << "~" << nack.getReason()
432 << " link-type=" << ingress.face.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700433 return;
434 }
435
436 // PIT match
437 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
438 // if no PIT entry found, drop
439 if (pitEntry == nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000440 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
441 << "~" << nack.getReason() << " no-PIT-entry");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700442 return;
443 }
444
445 // has out-record?
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000446 auto outRecord = pitEntry->getOutRecord(ingress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700447 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700448 if (outRecord == pitEntry->out_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000449 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
450 << "~" << nack.getReason() << " no-out-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700451 return;
452 }
453
454 // if out-record has different Nonce, drop
455 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
ashiqopuc7079482019-02-20 05:34:37 +0000456 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
457 << "~" << nack.getReason() << " wrong-Nonce " << nack.getInterest().getNonce()
458 << "!=" << outRecord->getLastNonce());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700459 return;
460 }
461
ashiqopuc7079482019-02-20 05:34:37 +0000462 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
463 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700464
465 // record Nack on out-record
466 outRecord->setIncomingNack(nack);
467
Teng Liang7003e0b2018-03-03 16:03:30 -0700468 // set PIT expiry timer to now when all out-record receive Nack
469 if (!fw::hasPendingOutRecords(*pitEntry)) {
470 this->setExpiryTimer(pitEntry, 0_ms);
471 }
472
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700473 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000474 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000475 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(ingress, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700476}
477
478void
ashiqopuc7079482019-02-20 05:34:37 +0000479Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry,
480 const FaceEndpoint& egress, const lp::NackHeader& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700481{
ashiqopuc7079482019-02-20 05:34:37 +0000482 if (egress.face.getId() == face::INVALID_FACEID) {
483 NFD_LOG_WARN("onOutgoingNack out=(invalid)"
Davide Pesaventob31206e2019-04-20 22:34:12 -0400484 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700485 return;
486 }
487
488 // has in-record?
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000489 auto inRecord = pitEntry->getInRecord(egress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700490
491 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700492 if (inRecord == pitEntry->in_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000493 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
494 << " nack=" << pitEntry->getInterest().getName()
495 << "~" << nack.getReason() << " no-in-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700496 return;
497 }
498
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700499 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000500 if (egress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
501 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
Davide Pesaventob31206e2019-04-20 22:34:12 -0400502 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason()
503 << " link-type=" << egress.face.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700504 return;
505 }
506
ashiqopuc7079482019-02-20 05:34:37 +0000507 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
508 << " nack=" << pitEntry->getInterest().getName()
509 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700510
511 // create Nack packet with the Interest from in-record
512 lp::Nack nackPkt(inRecord->getInterest());
513 nackPkt.setHeader(nack);
514
515 // erase in-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000516 pitEntry->deleteInRecord(egress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700517
518 // send Nack on face
ashiqopu075bb7d2019-03-10 01:38:21 +0000519 egress.face.sendNack(nackPkt, egress.endpoint);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700520 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700521}
522
Eric Newberry41aba102017-11-01 16:42:13 -0700523void
ashiqopuc7079482019-02-20 05:34:37 +0000524Forwarder::onDroppedInterest(const FaceEndpoint& egress, const Interest& interest)
Eric Newberry41aba102017-11-01 16:42:13 -0700525{
ashiqopuc7079482019-02-20 05:34:37 +0000526 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(egress, interest);
Eric Newberry41aba102017-11-01 16:42:13 -0700527}
528
Junxiao Shid3c792f2014-01-30 00:46:13 -0700529void
Ju Pan2feb4592019-09-16 20:56:38 +0000530Forwarder::onNewNextHop(const Name& prefix, const fib::NextHop& nextHop)
531{
532 const auto affectedEntries = this->getNameTree().partialEnumerate(prefix,
533 [&] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
534 const fib::Entry* fibEntry = nte.getFibEntry();
535 const fw::Strategy* strategy = nullptr;
536 if (nte.getStrategyChoiceEntry() != nullptr) {
537 strategy = &nte.getStrategyChoiceEntry()->getStrategy();
538 }
539 // current nte has buffered Interests but no fibEntry (except for the root nte) and the strategy
540 // enables new nexthop behavior, we enumerate the current nte and keep visiting its children.
541 if (nte.getName().size() == 0 ||
542 (strategy != nullptr && strategy->wantNewNextHopTrigger() &&
543 fibEntry == nullptr && nte.hasPitEntries())) {
544 return {true, true};
545 }
546 // we don't need the current nte (no pitEntry or strategy doesn't support new nexthop), but
547 // if the current nte has no fibEntry, it's still possible that its children are affected by
548 // the new nexthop.
549 else if (fibEntry == nullptr) {
550 return {false, true};
551 }
552 // if the current nte has a fibEntry, we ignore the current nte and don't visit its
553 // children because they are already covered by the current nte's fibEntry.
554 else {
555 return {false, false};
556 }
557 });
558
559 for (const auto& nte : affectedEntries) {
560 for (const auto& pitEntry : nte.getPitEntries()) {
561 this->dispatchToStrategy(*pitEntry,
562 [&] (fw::Strategy& strategy) {
563 strategy.afterNewNextHop(nextHop, pitEntry);
564 });
565 }
566 }
567}
568
569void
Teng Liang7003e0b2018-03-03 16:03:30 -0700570Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700571{
Teng Liang39465c22018-11-12 19:43:04 -0700572 BOOST_ASSERT(pitEntry);
Teng Liang7003e0b2018-03-03 16:03:30 -0700573 BOOST_ASSERT(duration >= 0_ms);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700574
Davide Pesavento3dade002019-03-19 11:29:56 -0600575 pitEntry->expiryTimer.cancel();
576 pitEntry->expiryTimer = getScheduler().schedule(duration, [=] { onInterestFinalize(pitEntry); });
Junxiao Shia110f262014-10-12 12:35:20 -0700577}
578
579void
Teng Liang6f09ab62018-03-01 20:04:08 -0700580Forwarder::insertDeadNonceList(pit::Entry& pitEntry, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700581{
582 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000583 bool needDnl = true;
Teng Liang6f09ab62018-03-01 20:04:08 -0700584 if (pitEntry.isSatisfied) {
585 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
Junxiao Shia110f262014-10-12 12:35:20 -0700586 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Teng Liang6f09ab62018-03-01 20:04:08 -0700587 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700588 }
589
590 if (!needDnl) {
591 return;
592 }
593
594 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000595 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700596 // insert all outgoing Nonces
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400597 const auto& outRecords = pitEntry.getOutRecords();
598 std::for_each(outRecords.begin(), outRecords.end(), [&] (const auto& outRecord) {
599 m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
600 });
Junxiao Shia110f262014-10-12 12:35:20 -0700601 }
602 else {
603 // insert outgoing Nonce of a specific face
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000604 auto outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700605 if (outRecord != pitEntry.getOutRecords().end()) {
606 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
607 }
608 }
609}
610
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800611} // namespace nfd