blob: 1938c94dd534386e00a1065ecdcf761f5c1dfea6 [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/*
Davide Pesaventob21bed82021-02-13 00:20:06 -05003 * Copyright (c) 2014-2021, 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"
Davide Pesavento7922d122021-03-05 22:41:23 -050029#include "best-route-strategy.hpp"
Davide Pesavento58091582021-03-05 19:02:48 -050030#include "scope-prefix.hpp"
Junxiao Shifaf3eb02015-02-16 10:50:36 -070031#include "strategy.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040032#include "common/global.hpp"
33#include "common/logger.hpp"
Junxiao Shi02b73f52016-07-28 01:48:27 +000034#include "table/cleanup.hpp"
Davide Pesaventoa3148082018-04-12 18:21:54 -040035
Junxiao Shic29eb7f2020-04-14 18:20:32 +000036#include <ndn-cxx/lp/pit-token.hpp>
Junxiao Shicbc8e942016-09-06 03:17:45 +000037#include <ndn-cxx/lp/tags.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080038
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{
Davide Pesavento7922d122021-03-05 22:41:23 -050046 return fw::BestRouteStrategy::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 Afanasyev33b72772014-01-26 23:22:58 -080056{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040057 m_faceTable.afterAdd.connect([this] (const Face& face) {
Junxiao Shidcffdaa2016-07-26 02:23:56 +000058 face.afterReceiveInterest.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000059 [this, &face] (const Interest& interest, const EndpointId& endpointId) {
60 this->startProcessInterest(FaceEndpoint(face, endpointId), interest);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000061 });
62 face.afterReceiveData.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000063 [this, &face] (const Data& data, const EndpointId& endpointId) {
64 this->startProcessData(FaceEndpoint(face, endpointId), data);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000065 });
66 face.afterReceiveNack.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000067 [this, &face] (const lp::Nack& nack, const EndpointId& endpointId) {
68 this->startProcessNack(FaceEndpoint(face, endpointId), nack);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000069 });
Eric Newberry41aba102017-11-01 16:42:13 -070070 face.onDroppedInterest.connect(
71 [this, &face] (const Interest& interest) {
Teng Liangebc20f62020-06-23 16:55:20 -070072 this->onDroppedInterest(face, interest);
Eric Newberry41aba102017-11-01 16:42:13 -070073 });
Junxiao Shidcffdaa2016-07-26 02:23:56 +000074 });
75
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040076 m_faceTable.beforeRemove.connect([this] (const Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000077 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000078 });
Junxiao Shic34d1672016-12-09 15:57:59 +000079
Davide Pesaventob21bed82021-02-13 00:20:06 -050080 m_fib.afterNewNextHop.connect([this] (const Name& prefix, const fib::NextHop& nextHop) {
81 this->onNewNextHop(prefix, nextHop);
Ju Pan2feb4592019-09-16 20:56:38 +000082 });
83
Junxiao Shic34d1672016-12-09 15:57:59 +000084 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080085}
86
Junxiao Shidcffdaa2016-07-26 02:23:56 +000087Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060088
Junxiao Shi0355e9f2015-09-02 07:24:53 -070089void
ashiqopuc7079482019-02-20 05:34:37 +000090Forwarder::onIncomingInterest(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -070091{
92 // receive Interest
ashiqopuc7079482019-02-20 05:34:37 +000093 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress << " interest=" << interest.getName());
94 interest.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -070095 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -070096
Eric Newberry9d283ad2020-04-12 23:37:17 -070097 // drop if HopLimit zero, decrement otherwise (if present)
98 if (interest.getHopLimit()) {
99 if (*interest.getHopLimit() < 1) {
100 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress << " interest=" << interest.getName()
101 << " hop-limit=0");
102 ++const_cast<PacketCounter&>(ingress.face.getCounters().nInHopLimitZero);
103 return;
104 }
105
106 const_cast<Interest&>(interest).setHopLimit(*interest.getHopLimit() - 1);
107 }
108
Junxiao Shi88884492014-02-15 15:57:43 -0700109 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000110 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700111 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700112 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000113 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
114 << " interest=" << interest.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700115 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700116 return;
117 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700118
Junxiao Shi330136a2016-03-10 04:53:08 -0700119 // detect duplicate Nonce with Dead Nonce List
120 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
121 if (hasDuplicateNonceInDnl) {
122 // goto Interest loop pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000123 this->onInterestLoop(ingress, interest);
Junxiao Shi330136a2016-03-10 04:53:08 -0700124 return;
125 }
126
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000127 // strip forwarding hint if Interest has reached producer region
128 if (!interest.getForwardingHint().empty() &&
129 m_networkRegionTable.isInProducerRegion(interest.getForwardingHint())) {
ashiqopuc7079482019-02-20 05:34:37 +0000130 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
131 << " interest=" << interest.getName() << " reaching-producer-region");
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000132 const_cast<Interest&>(interest).setForwardingHint({});
Junxiao Shif9858fd2017-02-01 16:22:44 +0000133 }
134
Junxiao Shid3c792f2014-01-30 00:46:13 -0700135 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700136 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700137
Junxiao Shi330136a2016-03-10 04:53:08 -0700138 // detect duplicate Nonce in PIT entry
ashiqopuc7079482019-02-20 05:34:37 +0000139 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), ingress.face);
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000140 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
ashiqopuc7079482019-02-20 05:34:37 +0000141 if (ingress.face.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000142 // for p2p face: duplicate Nonce from same incoming face is not loop
143 hasDuplicateNonceInPit = hasDuplicateNonceInPit && !(dnw & fw::DUPLICATE_NONCE_IN_SAME);
144 }
Junxiao Shi330136a2016-03-10 04:53:08 -0700145 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700146 // goto Interest loop pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000147 this->onInterestLoop(ingress, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700148 return;
149 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700150
Junxiao Shif3c07812014-03-11 21:48:49 -0700151 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700152 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600153 m_cs.find(interest,
ashiqopuc7079482019-02-20 05:34:37 +0000154 bind(&Forwarder::onContentStoreHit, this, ingress, pitEntry, _1, _2),
155 bind(&Forwarder::onContentStoreMiss, this, ingress, pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700156 }
mzhang4eab72492015-02-25 11:16:09 -0600157 else {
ashiqopuc7079482019-02-20 05:34:37 +0000158 this->onContentStoreMiss(ingress, pitEntry, interest);
mzhang4eab72492015-02-25 11:16:09 -0600159 }
160}
Junxiao Shic041ca32014-02-25 20:01:15 -0700161
mzhang4eab72492015-02-25 11:16:09 -0600162void
ashiqopuc7079482019-02-20 05:34:37 +0000163Forwarder::onInterestLoop(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700164{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700165 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000166 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
167 NFD_LOG_DEBUG("onInterestLoop in=" << ingress
168 << " interest=" << interest.getName() << " drop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700169 return;
170 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700171
ashiqopuc7079482019-02-20 05:34:37 +0000172 NFD_LOG_DEBUG("onInterestLoop in=" << ingress << " interest=" << interest.getName()
173 << " send-Nack-duplicate");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700174
175 // send Nack with reason=DUPLICATE
176 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
177 lp::Nack nack(interest);
178 nack.setReason(lp::NackReason::DUPLICATE);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700179 ingress.face.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700180}
181
182void
ashiqopuc7079482019-02-20 05:34:37 +0000183Forwarder::onContentStoreMiss(const FaceEndpoint& ingress,
184 const shared_ptr<pit::Entry>& pitEntry, const Interest& interest)
mzhang4eab72492015-02-25 11:16:09 -0600185{
186 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000187 ++m_counters.nCsMisses;
mzhang4eab72492015-02-25 11:16:09 -0600188
Md Ashiqur Rahman115ea632019-07-06 02:34:31 +0000189 // insert in-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000190 pitEntry->insertOrUpdateInRecord(ingress.face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700191
Teng Liang7003e0b2018-03-03 16:03:30 -0700192 // set PIT expiry timer to the time that the last PIT in-record expires
Davide Pesaventob31206e2019-04-20 22:34:12 -0400193 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(),
194 [] (const auto& a, const auto& b) {
195 return a.getExpiry() < b.getExpiry();
196 });
Teng Liang7003e0b2018-03-03 16:03:30 -0700197 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
198 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700199
Junxiao Shie342e8d2016-09-18 16:48:00 +0000200 // has NextHopFaceId?
Davide Pesaventob31206e2019-04-20 22:34:12 -0400201 auto nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
Junxiao Shie342e8d2016-09-18 16:48:00 +0000202 if (nextHopTag != nullptr) {
203 // chosen NextHop face exists?
204 Face* nextHopFace = m_faceTable.get(*nextHopTag);
205 if (nextHopFace != nullptr) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400206 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName()
207 << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000208 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000209 // scope control is unnecessary, because privileged app explicitly wants to forward
Teng Liangebc20f62020-06-23 16:55:20 -0700210 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000211 }
212 return;
213 }
214
Davide Pesavento05590472021-02-17 15:53:27 -0500215 // dispatch to strategy: after receive Interest
216 m_strategyChoice.findEffectiveStrategy(*pitEntry)
217 .afterReceiveInterest(FaceEndpoint(ingress.face, 0), interest, pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700218}
219
220void
ashiqopuc7079482019-02-20 05:34:37 +0000221Forwarder::onContentStoreHit(const FaceEndpoint& ingress, const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000222 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600223{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500224 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000225 ++m_counters.nCsHits;
mzhang4eab72492015-02-25 11:16:09 -0600226
Junxiao Shicde37ad2015-12-24 01:02:05 -0700227 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
Junxiao Shic29eb7f2020-04-14 18:20:32 +0000228 data.setTag(interest.getTag<lp::PitToken>());
Davide Pesaventob31206e2019-04-20 22:34:12 -0400229 // FIXME Should we lookup PIT for other Interests that also match the data?
mzhang4eab72492015-02-25 11:16:09 -0600230
Teng Liang6f09ab62018-03-01 20:04:08 -0700231 pitEntry->isSatisfied = true;
232 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
233
Teng Liang85a36632018-03-21 05:59:34 -0700234 // set PIT expiry timer to now
235 this->setExpiryTimer(pitEntry, 0_ms);
mzhang4eab72492015-02-25 11:16:09 -0600236
Teng Liang85a36632018-03-21 05:59:34 -0700237 // dispatch to strategy: after Content Store hit
Davide Pesavento05590472021-02-17 15:53:27 -0500238 m_strategyChoice.findEffectiveStrategy(*pitEntry).afterContentStoreHit(pitEntry, ingress, data);
mzhang4eab72492015-02-25 11:16:09 -0600239}
240
Eric Newberry2377ada2020-09-28 22:40:14 -0700241pit::OutRecord*
ashiqopuc7079482019-02-20 05:34:37 +0000242Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry,
Teng Liangebc20f62020-06-23 16:55:20 -0700243 Face& egress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700244{
Eric Newberry9d283ad2020-04-12 23:37:17 -0700245 // drop if HopLimit == 0 but sending on non-local face
Teng Liangebc20f62020-06-23 16:55:20 -0700246 if (interest.getHopLimit() == 0 && egress.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL) {
247 NFD_LOG_DEBUG("onOutgoingInterest out=" << egress.getId() << " interest=" << pitEntry->getName()
Eric Newberry9d283ad2020-04-12 23:37:17 -0700248 << " non-local hop-limit=0");
Teng Liangebc20f62020-06-23 16:55:20 -0700249 ++const_cast<PacketCounter&>(egress.getCounters().nOutHopLimitZero);
Eric Newberry2377ada2020-09-28 22:40:14 -0700250 return nullptr;
Eric Newberry9d283ad2020-04-12 23:37:17 -0700251 }
252
Teng Liangebc20f62020-06-23 16:55:20 -0700253 NFD_LOG_DEBUG("onOutgoingInterest out=" << egress.getId() << " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700254
Junxiao Shi4846f372016-04-05 13:39:30 -0700255 // insert out-record
Eric Newberry2377ada2020-09-28 22:40:14 -0700256 auto it = pitEntry->insertOrUpdateOutRecord(egress, interest);
257 BOOST_ASSERT(it != pitEntry->out_end());
Junxiao Shic041ca32014-02-25 20:01:15 -0700258
Junxiao Shid3c792f2014-01-30 00:46:13 -0700259 // send Interest
Teng Liangebc20f62020-06-23 16:55:20 -0700260 egress.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700261 ++m_counters.nOutInterests;
Eric Newberry2377ada2020-09-28 22:40:14 -0700262 return &*it;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700263}
264
265void
Teng Liang6f09ab62018-03-01 20:04:08 -0700266Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shia110f262014-10-12 12:35:20 -0700267{
ashiqopuc7079482019-02-20 05:34:37 +0000268 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName()
269 << (pitEntry->isSatisfied ? " satisfied" : " unsatisfied"));
Junxiao Shia110f262014-10-12 12:35:20 -0700270
271 // Dead Nonce List insert if necessary
Davide Pesavento3dade002019-03-19 11:29:56 -0600272 this->insertDeadNonceList(*pitEntry, nullptr);
Junxiao Shia110f262014-10-12 12:35:20 -0700273
Ju Pan6eb1ac92018-10-26 16:26:15 +0000274 // Increment satisfied/unsatisfied Interests counter
275 if (pitEntry->isSatisfied) {
276 ++m_counters.nSatisfiedInterests;
277 }
278 else {
279 ++m_counters.nUnsatisfiedInterests;
280 }
281
Junxiao Shif3c07812014-03-11 21:48:49 -0700282 // PIT delete
Davide Pesavento3dade002019-03-19 11:29:56 -0600283 pitEntry->expiryTimer.cancel();
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000284 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700285}
286
287void
ashiqopuc7079482019-02-20 05:34:37 +0000288Forwarder::onIncomingData(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700289{
290 // receive Data
ashiqopuc7079482019-02-20 05:34:37 +0000291 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName());
292 data.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700293 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700294
Junxiao Shi88884492014-02-15 15:57:43 -0700295 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000296 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700297 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700298 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000299 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700300 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700301 return;
302 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700303
Junxiao Shid3c792f2014-01-30 00:46:13 -0700304 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700305 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
Teng Liang43bb2312018-03-26 04:16:42 -0700306 if (pitMatches.size() == 0) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700307 // goto Data unsolicited pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000308 this->onDataUnsolicited(ingress, data);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700309 return;
310 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700311
Junxiao Shid3c792f2014-01-30 00:46:13 -0700312 // CS insert
313 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700314
Teng Liang43bb2312018-03-26 04:16:42 -0700315 // when only one PIT entry is matched, trigger strategy: after receive Data
316 if (pitMatches.size() == 1) {
317 auto& pitEntry = pitMatches.front();
Junxiao Shic041ca32014-02-25 20:01:15 -0700318
Teng Liang43bb2312018-03-26 04:16:42 -0700319 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700320
Teng Liang7003e0b2018-03-03 16:03:30 -0700321 // set PIT expiry timer to now
322 this->setExpiryTimer(pitEntry, 0_ms);
323
Teng Liang43bb2312018-03-26 04:16:42 -0700324 // trigger strategy: after receive Data
Davide Pesavento05590472021-02-17 15:53:27 -0500325 m_strategyChoice.findEffectiveStrategy(*pitEntry).afterReceiveData(pitEntry, ingress, data);
Junxiao Shid938a6b2014-05-11 23:40:29 -0700326
Teng Liang43bb2312018-03-26 04:16:42 -0700327 // mark PIT satisfied
Teng Liang6f09ab62018-03-01 20:04:08 -0700328 pitEntry->isSatisfied = true;
329 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
330
Teng Liangebc20f62020-06-23 16:55:20 -0700331 // Dead Nonce List insert if necessary (for out-record of ingress face)
ashiqopuc7079482019-02-20 05:34:37 +0000332 this->insertDeadNonceList(*pitEntry, &ingress.face);
Junxiao Shia110f262014-10-12 12:35:20 -0700333
Teng Liang43bb2312018-03-26 04:16:42 -0700334 // delete PIT entry's out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000335 pitEntry->deleteOutRecord(ingress.face);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700336 }
Teng Liang43bb2312018-03-26 04:16:42 -0700337 // when more than one PIT entry is matched, trigger strategy: before satisfy Interest,
338 // and send Data to all matched out faces
339 else {
Teng Liangebc20f62020-06-23 16:55:20 -0700340 std::set<Face*> pendingDownstreams;
Teng Liang43bb2312018-03-26 04:16:42 -0700341 auto now = time::steady_clock::now();
Junxiao Shic041ca32014-02-25 20:01:15 -0700342
Davide Pesaventob31206e2019-04-20 22:34:12 -0400343 for (const auto& pitEntry : pitMatches) {
Teng Liang43bb2312018-03-26 04:16:42 -0700344 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
345
346 // remember pending downstreams
347 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
348 if (inRecord.getExpiry() > now) {
Teng Liangebc20f62020-06-23 16:55:20 -0700349 pendingDownstreams.insert(&inRecord.getFace());
Teng Liang43bb2312018-03-26 04:16:42 -0700350 }
351 }
352
353 // set PIT expiry timer to now
354 this->setExpiryTimer(pitEntry, 0_ms);
355
356 // invoke PIT satisfy callback
Davide Pesavento05590472021-02-17 15:53:27 -0500357 m_strategyChoice.findEffectiveStrategy(*pitEntry).beforeSatisfyInterest(pitEntry, ingress, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700358
359 // mark PIT satisfied
360 pitEntry->isSatisfied = true;
361 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
362
Teng Liangebc20f62020-06-23 16:55:20 -0700363 // Dead Nonce List insert if necessary (for out-record of ingress face)
ashiqopuc7079482019-02-20 05:34:37 +0000364 this->insertDeadNonceList(*pitEntry, &ingress.face);
Teng Liang43bb2312018-03-26 04:16:42 -0700365
366 // clear PIT entry's in and out records
367 pitEntry->clearInRecords();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000368 pitEntry->deleteOutRecord(ingress.face);
Junxiao Shida006f52014-05-16 11:18:00 -0700369 }
Teng Liang43bb2312018-03-26 04:16:42 -0700370
371 // foreach pending downstream
ashiqopuc7079482019-02-20 05:34:37 +0000372 for (const auto& pendingDownstream : pendingDownstreams) {
Teng Liangebc20f62020-06-23 16:55:20 -0700373 if (pendingDownstream->getId() == ingress.face.getId() &&
374 pendingDownstream->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Teng Liang43bb2312018-03-26 04:16:42 -0700375 continue;
376 }
377 // goto outgoing Data pipeline
Teng Liangebc20f62020-06-23 16:55:20 -0700378 this->onOutgoingData(data, *pendingDownstream);
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?
Alex Lane6bead9b2020-05-12 19:08:20 -0500387 auto 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
Teng Liangebc20f62020-06-23 16:55:20 -0700393 NFD_LOG_DEBUG("onDataUnsolicited in=" << ingress << " data=" << data.getName()
394 << " decision=" << decision);
Alex Lane6bead9b2020-05-12 19:08:20 -0500395 ++m_counters.nUnsolicitedData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700396}
397
Eric Newberry2377ada2020-09-28 22:40:14 -0700398bool
Teng Liangebc20f62020-06-23 16:55:20 -0700399Forwarder::onOutgoingData(const Data& data, Face& egress)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700400{
Teng Liangebc20f62020-06-23 16:55:20 -0700401 if (egress.getId() == face::INVALID_FACEID) {
ashiqopuc7079482019-02-20 05:34:37 +0000402 NFD_LOG_WARN("onOutgoingData out=(invalid) data=" << data.getName());
Eric Newberry2377ada2020-09-28 22:40:14 -0700403 return false;
Junxiao Shi223271b2014-07-03 22:06:13 -0700404 }
Teng Liangebc20f62020-06-23 16:55:20 -0700405 NFD_LOG_DEBUG("onOutgoingData out=" << egress.getId() << " data=" << data.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700406
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700407 // /localhost scope control
Teng Liangebc20f62020-06-23 16:55:20 -0700408 bool isViolatingLocalhost = egress.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700409 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700410 if (isViolatingLocalhost) {
Teng Liangebc20f62020-06-23 16:55:20 -0700411 NFD_LOG_DEBUG("onOutgoingData out=" << egress.getId() << " data=" << data.getName()
412 << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700413 // (drop)
Eric Newberry2377ada2020-09-28 22:40:14 -0700414 return false;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700415 }
416
Junxiao Shif3c07812014-03-11 21:48:49 -0700417 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700418
Junxiao Shid3c792f2014-01-30 00:46:13 -0700419 // send Data
Teng Liangebc20f62020-06-23 16:55:20 -0700420 egress.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700421 ++m_counters.nOutData;
Eric Newberry2377ada2020-09-28 22:40:14 -0700422
423 return true;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700424}
425
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700426void
ashiqopuc7079482019-02-20 05:34:37 +0000427Forwarder::onIncomingNack(const FaceEndpoint& ingress, const lp::Nack& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700428{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000429 // receive Nack
ashiqopuc7079482019-02-20 05:34:37 +0000430 nack.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700431 ++m_counters.nInNacks;
432
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700433 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000434 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400435 NFD_LOG_DEBUG("onIncomingNack in=" << ingress
436 << " nack=" << nack.getInterest().getName() << "~" << nack.getReason()
437 << " link-type=" << ingress.face.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700438 return;
439 }
440
441 // PIT match
442 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
443 // if no PIT entry found, drop
444 if (pitEntry == nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000445 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
446 << "~" << nack.getReason() << " no-PIT-entry");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700447 return;
448 }
449
450 // has out-record?
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000451 auto outRecord = pitEntry->getOutRecord(ingress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700452 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700453 if (outRecord == pitEntry->out_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000454 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
455 << "~" << nack.getReason() << " no-out-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700456 return;
457 }
458
459 // if out-record has different Nonce, drop
460 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
ashiqopuc7079482019-02-20 05:34:37 +0000461 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
462 << "~" << nack.getReason() << " wrong-Nonce " << nack.getInterest().getNonce()
463 << "!=" << outRecord->getLastNonce());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700464 return;
465 }
466
ashiqopuc7079482019-02-20 05:34:37 +0000467 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
468 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700469
470 // record Nack on out-record
471 outRecord->setIncomingNack(nack);
472
Teng Liang7003e0b2018-03-03 16:03:30 -0700473 // set PIT expiry timer to now when all out-record receive Nack
474 if (!fw::hasPendingOutRecords(*pitEntry)) {
475 this->setExpiryTimer(pitEntry, 0_ms);
476 }
477
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700478 // trigger strategy: after receive NACK
Davide Pesavento05590472021-02-17 15:53:27 -0500479 m_strategyChoice.findEffectiveStrategy(*pitEntry).afterReceiveNack(ingress, nack, pitEntry);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700480}
481
Eric Newberry2377ada2020-09-28 22:40:14 -0700482bool
ashiqopuc7079482019-02-20 05:34:37 +0000483Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry,
Teng Liangebc20f62020-06-23 16:55:20 -0700484 Face& egress, const lp::NackHeader& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700485{
Teng Liangebc20f62020-06-23 16:55:20 -0700486 if (egress.getId() == face::INVALID_FACEID) {
ashiqopuc7079482019-02-20 05:34:37 +0000487 NFD_LOG_WARN("onOutgoingNack out=(invalid)"
Davide Pesaventob31206e2019-04-20 22:34:12 -0400488 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason());
Eric Newberry2377ada2020-09-28 22:40:14 -0700489 return false;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700490 }
491
492 // has in-record?
Teng Liangebc20f62020-06-23 16:55:20 -0700493 auto inRecord = pitEntry->getInRecord(egress);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700494
495 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700496 if (inRecord == pitEntry->in_end()) {
Teng Liangebc20f62020-06-23 16:55:20 -0700497 NFD_LOG_DEBUG("onOutgoingNack out=" << egress.getId()
ashiqopuc7079482019-02-20 05:34:37 +0000498 << " nack=" << pitEntry->getInterest().getName()
499 << "~" << nack.getReason() << " no-in-record");
Eric Newberry2377ada2020-09-28 22:40:14 -0700500 return false;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700501 }
502
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700503 // if multi-access or ad hoc face, drop
Teng Liangebc20f62020-06-23 16:55:20 -0700504 if (egress.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
505 NFD_LOG_DEBUG("onOutgoingNack out=" << egress.getId()
Davide Pesaventob31206e2019-04-20 22:34:12 -0400506 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason()
Teng Liangebc20f62020-06-23 16:55:20 -0700507 << " link-type=" << egress.getLinkType());
Eric Newberry2377ada2020-09-28 22:40:14 -0700508 return false;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700509 }
510
Teng Liangebc20f62020-06-23 16:55:20 -0700511 NFD_LOG_DEBUG("onOutgoingNack out=" << egress.getId()
ashiqopuc7079482019-02-20 05:34:37 +0000512 << " nack=" << pitEntry->getInterest().getName()
513 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700514
515 // create Nack packet with the Interest from in-record
516 lp::Nack nackPkt(inRecord->getInterest());
517 nackPkt.setHeader(nack);
518
519 // erase in-record
Teng Liangebc20f62020-06-23 16:55:20 -0700520 pitEntry->deleteInRecord(egress);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700521
522 // send Nack on face
Teng Liangebc20f62020-06-23 16:55:20 -0700523 egress.sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700524 ++m_counters.nOutNacks;
Eric Newberry2377ada2020-09-28 22:40:14 -0700525
526 return true;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700527}
528
Eric Newberry41aba102017-11-01 16:42:13 -0700529void
Teng Liangebc20f62020-06-23 16:55:20 -0700530Forwarder::onDroppedInterest(const Face& egress, const Interest& interest)
Eric Newberry41aba102017-11-01 16:42:13 -0700531{
ashiqopuc7079482019-02-20 05:34:37 +0000532 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(egress, interest);
Eric Newberry41aba102017-11-01 16:42:13 -0700533}
534
Junxiao Shid3c792f2014-01-30 00:46:13 -0700535void
Ju Pan2feb4592019-09-16 20:56:38 +0000536Forwarder::onNewNextHop(const Name& prefix, const fib::NextHop& nextHop)
537{
538 const auto affectedEntries = this->getNameTree().partialEnumerate(prefix,
539 [&] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
Davide Pesaventob21bed82021-02-13 00:20:06 -0500540 // we ignore an NTE and skip visiting its descendants if that NTE has an
541 // associated FIB entry (1st condition), since in that case the new nexthop
542 // won't affect any PIT entries anywhere in that subtree, *unless* this is
543 // the initial NTE from which the enumeration started (2nd condition), which
544 // must always be considered
545 if (nte.getFibEntry() != nullptr && nte.getName().size() > prefix.size()) {
Ju Pan2feb4592019-09-16 20:56:38 +0000546 return {false, false};
547 }
Davide Pesaventob21bed82021-02-13 00:20:06 -0500548 return {nte.hasPitEntries(), true};
Ju Pan2feb4592019-09-16 20:56:38 +0000549 });
550
551 for (const auto& nte : affectedEntries) {
552 for (const auto& pitEntry : nte.getPitEntries()) {
Davide Pesavento05590472021-02-17 15:53:27 -0500553 m_strategyChoice.findEffectiveStrategy(*pitEntry).afterNewNextHop(nextHop, pitEntry);
Ju Pan2feb4592019-09-16 20:56:38 +0000554 }
555 }
556}
557
558void
Teng Liang7003e0b2018-03-03 16:03:30 -0700559Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700560{
Teng Liang39465c22018-11-12 19:43:04 -0700561 BOOST_ASSERT(pitEntry);
Junxiao Shie1cf4ba2020-06-19 16:40:53 -0600562 duration = std::max(duration, 0_ms);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700563
Davide Pesavento3dade002019-03-19 11:29:56 -0600564 pitEntry->expiryTimer.cancel();
565 pitEntry->expiryTimer = getScheduler().schedule(duration, [=] { onInterestFinalize(pitEntry); });
Junxiao Shia110f262014-10-12 12:35:20 -0700566}
567
568void
Teng Liang6f09ab62018-03-01 20:04:08 -0700569Forwarder::insertDeadNonceList(pit::Entry& pitEntry, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700570{
571 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000572 bool needDnl = true;
Teng Liang6f09ab62018-03-01 20:04:08 -0700573 if (pitEntry.isSatisfied) {
574 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
Junxiao Shia110f262014-10-12 12:35:20 -0700575 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Teng Liang6f09ab62018-03-01 20:04:08 -0700576 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700577 }
578
579 if (!needDnl) {
580 return;
581 }
582
583 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000584 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700585 // insert all outgoing Nonces
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400586 const auto& outRecords = pitEntry.getOutRecords();
587 std::for_each(outRecords.begin(), outRecords.end(), [&] (const auto& outRecord) {
588 m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
589 });
Junxiao Shia110f262014-10-12 12:35:20 -0700590 }
591 else {
592 // insert outgoing Nonce of a specific face
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000593 auto outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700594 if (outRecord != pitEntry.getOutRecords().end()) {
595 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
596 }
597 }
598}
599
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800600} // namespace nfd