blob: 8301014070d64d3dae798736d777f32da0c2cd0b [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"
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 Shic29eb7f2020-04-14 18:20:32 +000035#include <ndn-cxx/lp/pit-token.hpp>
Junxiao Shicbc8e942016-09-06 03:17:45 +000036#include <ndn-cxx/lp/tags.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080037
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080038namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080039
Davide Pesaventoa3148082018-04-12 18:21:54 -040040NFD_LOG_INIT(Forwarder);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070041
Junxiao Shic34d1672016-12-09 15:57:59 +000042static Name
43getDefaultStrategyName()
44{
Junxiao Shi037f4ab2016-12-13 04:27:06 +000045 return fw::BestRouteStrategy2::getStrategyName();
Junxiao Shic34d1672016-12-09 15:57:59 +000046}
47
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040048Forwarder::Forwarder(FaceTable& faceTable)
49 : m_faceTable(faceTable)
50 , 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 Afanasyev33b72772014-01-26 23:22:58 -080055{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040056 m_faceTable.afterAdd.connect([this] (const Face& face) {
Junxiao Shidcffdaa2016-07-26 02:23:56 +000057 face.afterReceiveInterest.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000058 [this, &face] (const Interest& interest, const EndpointId& endpointId) {
59 this->startProcessInterest(FaceEndpoint(face, endpointId), interest);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000060 });
61 face.afterReceiveData.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000062 [this, &face] (const Data& data, const EndpointId& endpointId) {
63 this->startProcessData(FaceEndpoint(face, endpointId), data);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000064 });
65 face.afterReceiveNack.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000066 [this, &face] (const lp::Nack& nack, const EndpointId& endpointId) {
67 this->startProcessNack(FaceEndpoint(face, endpointId), nack);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000068 });
Eric Newberry41aba102017-11-01 16:42:13 -070069 face.onDroppedInterest.connect(
70 [this, &face] (const Interest& interest) {
Teng Liangebc20f62020-06-23 16:55:20 -070071 this->onDroppedInterest(face, interest);
Eric Newberry41aba102017-11-01 16:42:13 -070072 });
Junxiao Shidcffdaa2016-07-26 02:23:56 +000073 });
74
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040075 m_faceTable.beforeRemove.connect([this] (const Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000076 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000077 });
Junxiao Shic34d1672016-12-09 15:57:59 +000078
Davide Pesaventob21bed82021-02-13 00:20:06 -050079 m_fib.afterNewNextHop.connect([this] (const Name& prefix, const fib::NextHop& nextHop) {
80 this->onNewNextHop(prefix, nextHop);
Ju Pan2feb4592019-09-16 20:56:38 +000081 });
82
Junxiao Shic34d1672016-12-09 15:57:59 +000083 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080084}
85
Junxiao Shidcffdaa2016-07-26 02:23:56 +000086Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060087
Junxiao Shi0355e9f2015-09-02 07:24:53 -070088void
ashiqopuc7079482019-02-20 05:34:37 +000089Forwarder::onIncomingInterest(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -070090{
91 // receive Interest
ashiqopuc7079482019-02-20 05:34:37 +000092 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress << " interest=" << interest.getName());
93 interest.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -070094 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -070095
Eric Newberry9d283ad2020-04-12 23:37:17 -070096 // drop if HopLimit zero, decrement otherwise (if present)
97 if (interest.getHopLimit()) {
98 if (*interest.getHopLimit() < 1) {
99 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress << " interest=" << interest.getName()
100 << " hop-limit=0");
101 ++const_cast<PacketCounter&>(ingress.face.getCounters().nInHopLimitZero);
102 return;
103 }
104
105 const_cast<Interest&>(interest).setHopLimit(*interest.getHopLimit() - 1);
106 }
107
Junxiao Shi88884492014-02-15 15:57:43 -0700108 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000109 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700110 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700111 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000112 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
113 << " interest=" << interest.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700114 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700115 return;
116 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700117
Junxiao Shi330136a2016-03-10 04:53:08 -0700118 // detect duplicate Nonce with Dead Nonce List
119 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
120 if (hasDuplicateNonceInDnl) {
121 // goto Interest loop pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000122 this->onInterestLoop(ingress, interest);
Junxiao Shi330136a2016-03-10 04:53:08 -0700123 return;
124 }
125
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000126 // strip forwarding hint if Interest has reached producer region
127 if (!interest.getForwardingHint().empty() &&
128 m_networkRegionTable.isInProducerRegion(interest.getForwardingHint())) {
ashiqopuc7079482019-02-20 05:34:37 +0000129 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
130 << " interest=" << interest.getName() << " reaching-producer-region");
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000131 const_cast<Interest&>(interest).setForwardingHint({});
Junxiao Shif9858fd2017-02-01 16:22:44 +0000132 }
133
Junxiao Shid3c792f2014-01-30 00:46:13 -0700134 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700135 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700136
Junxiao Shi330136a2016-03-10 04:53:08 -0700137 // detect duplicate Nonce in PIT entry
ashiqopuc7079482019-02-20 05:34:37 +0000138 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), ingress.face);
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000139 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
ashiqopuc7079482019-02-20 05:34:37 +0000140 if (ingress.face.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000141 // for p2p face: duplicate Nonce from same incoming face is not loop
142 hasDuplicateNonceInPit = hasDuplicateNonceInPit && !(dnw & fw::DUPLICATE_NONCE_IN_SAME);
143 }
Junxiao Shi330136a2016-03-10 04:53:08 -0700144 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700145 // goto Interest loop pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000146 this->onInterestLoop(ingress, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700147 return;
148 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700149
Junxiao Shif3c07812014-03-11 21:48:49 -0700150 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700151 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600152 m_cs.find(interest,
ashiqopuc7079482019-02-20 05:34:37 +0000153 bind(&Forwarder::onContentStoreHit, this, ingress, pitEntry, _1, _2),
154 bind(&Forwarder::onContentStoreMiss, this, ingress, pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700155 }
mzhang4eab72492015-02-25 11:16:09 -0600156 else {
ashiqopuc7079482019-02-20 05:34:37 +0000157 this->onContentStoreMiss(ingress, pitEntry, interest);
mzhang4eab72492015-02-25 11:16:09 -0600158 }
159}
Junxiao Shic041ca32014-02-25 20:01:15 -0700160
mzhang4eab72492015-02-25 11:16:09 -0600161void
ashiqopuc7079482019-02-20 05:34:37 +0000162Forwarder::onInterestLoop(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700163{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700164 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000165 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
166 NFD_LOG_DEBUG("onInterestLoop in=" << ingress
167 << " interest=" << interest.getName() << " drop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700168 return;
169 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700170
ashiqopuc7079482019-02-20 05:34:37 +0000171 NFD_LOG_DEBUG("onInterestLoop in=" << ingress << " interest=" << interest.getName()
172 << " send-Nack-duplicate");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700173
174 // send Nack with reason=DUPLICATE
175 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
176 lp::Nack nack(interest);
177 nack.setReason(lp::NackReason::DUPLICATE);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700178 ingress.face.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700179}
180
181void
ashiqopuc7079482019-02-20 05:34:37 +0000182Forwarder::onContentStoreMiss(const FaceEndpoint& ingress,
183 const shared_ptr<pit::Entry>& pitEntry, const Interest& interest)
mzhang4eab72492015-02-25 11:16:09 -0600184{
185 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000186 ++m_counters.nCsMisses;
mzhang4eab72492015-02-25 11:16:09 -0600187
Md Ashiqur Rahman115ea632019-07-06 02:34:31 +0000188 // insert in-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000189 pitEntry->insertOrUpdateInRecord(ingress.face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700190
Teng Liang7003e0b2018-03-03 16:03:30 -0700191 // set PIT expiry timer to the time that the last PIT in-record expires
Davide Pesaventob31206e2019-04-20 22:34:12 -0400192 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(),
193 [] (const auto& a, const auto& b) {
194 return a.getExpiry() < b.getExpiry();
195 });
Teng Liang7003e0b2018-03-03 16:03:30 -0700196 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
197 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700198
Junxiao Shie342e8d2016-09-18 16:48:00 +0000199 // has NextHopFaceId?
Davide Pesaventob31206e2019-04-20 22:34:12 -0400200 auto nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
Junxiao Shie342e8d2016-09-18 16:48:00 +0000201 if (nextHopTag != nullptr) {
202 // chosen NextHop face exists?
203 Face* nextHopFace = m_faceTable.get(*nextHopTag);
204 if (nextHopFace != nullptr) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400205 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName()
206 << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000207 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000208 // scope control is unnecessary, because privileged app explicitly wants to forward
Teng Liangebc20f62020-06-23 16:55:20 -0700209 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000210 }
211 return;
212 }
213
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000214 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000215 this->dispatchToStrategy(*pitEntry,
Davide Pesaventob21bed82021-02-13 00:20:06 -0500216 [&] (auto& strategy) {
Md Ashiqur Rahman115ea632019-07-06 02:34:31 +0000217 strategy.afterReceiveInterest(FaceEndpoint(ingress.face, 0), interest, pitEntry);
218 });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700219}
220
221void
ashiqopuc7079482019-02-20 05:34:37 +0000222Forwarder::onContentStoreHit(const FaceEndpoint& ingress, const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000223 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600224{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500225 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000226 ++m_counters.nCsHits;
mzhang4eab72492015-02-25 11:16:09 -0600227
Junxiao Shicde37ad2015-12-24 01:02:05 -0700228 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
Junxiao Shic29eb7f2020-04-14 18:20:32 +0000229 data.setTag(interest.getTag<lp::PitToken>());
Davide Pesaventob31206e2019-04-20 22:34:12 -0400230 // FIXME Should we lookup PIT for other Interests that also match the data?
mzhang4eab72492015-02-25 11:16:09 -0600231
Teng Liang6f09ab62018-03-01 20:04:08 -0700232 pitEntry->isSatisfied = true;
233 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
234
Teng Liang85a36632018-03-21 05:59:34 -0700235 // set PIT expiry timer to now
236 this->setExpiryTimer(pitEntry, 0_ms);
mzhang4eab72492015-02-25 11:16:09 -0600237
Teng Liang85a36632018-03-21 05:59:34 -0700238 // dispatch to strategy: after Content Store hit
239 this->dispatchToStrategy(*pitEntry,
Davide Pesaventob21bed82021-02-13 00:20:06 -0500240 [&] (auto& strategy) { strategy.afterContentStoreHit(pitEntry, ingress, data); });
mzhang4eab72492015-02-25 11:16:09 -0600241}
242
Eric Newberry2377ada2020-09-28 22:40:14 -0700243pit::OutRecord*
ashiqopuc7079482019-02-20 05:34:37 +0000244Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry,
Teng Liangebc20f62020-06-23 16:55:20 -0700245 Face& egress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700246{
Eric Newberry9d283ad2020-04-12 23:37:17 -0700247 // drop if HopLimit == 0 but sending on non-local face
Teng Liangebc20f62020-06-23 16:55:20 -0700248 if (interest.getHopLimit() == 0 && egress.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL) {
249 NFD_LOG_DEBUG("onOutgoingInterest out=" << egress.getId() << " interest=" << pitEntry->getName()
Eric Newberry9d283ad2020-04-12 23:37:17 -0700250 << " non-local hop-limit=0");
Teng Liangebc20f62020-06-23 16:55:20 -0700251 ++const_cast<PacketCounter&>(egress.getCounters().nOutHopLimitZero);
Eric Newberry2377ada2020-09-28 22:40:14 -0700252 return nullptr;
Eric Newberry9d283ad2020-04-12 23:37:17 -0700253 }
254
Teng Liangebc20f62020-06-23 16:55:20 -0700255 NFD_LOG_DEBUG("onOutgoingInterest out=" << egress.getId() << " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700256
Junxiao Shi4846f372016-04-05 13:39:30 -0700257 // insert out-record
Eric Newberry2377ada2020-09-28 22:40:14 -0700258 auto it = pitEntry->insertOrUpdateOutRecord(egress, interest);
259 BOOST_ASSERT(it != pitEntry->out_end());
Junxiao Shic041ca32014-02-25 20:01:15 -0700260
Junxiao Shid3c792f2014-01-30 00:46:13 -0700261 // send Interest
Teng Liangebc20f62020-06-23 16:55:20 -0700262 egress.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700263 ++m_counters.nOutInterests;
Eric Newberry2377ada2020-09-28 22:40:14 -0700264 return &*it;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700265}
266
267void
Teng Liang6f09ab62018-03-01 20:04:08 -0700268Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shia110f262014-10-12 12:35:20 -0700269{
ashiqopuc7079482019-02-20 05:34:37 +0000270 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName()
271 << (pitEntry->isSatisfied ? " satisfied" : " unsatisfied"));
Junxiao Shia110f262014-10-12 12:35:20 -0700272
273 // Dead Nonce List insert if necessary
Davide Pesavento3dade002019-03-19 11:29:56 -0600274 this->insertDeadNonceList(*pitEntry, nullptr);
Junxiao Shia110f262014-10-12 12:35:20 -0700275
Ju Pan6eb1ac92018-10-26 16:26:15 +0000276 // Increment satisfied/unsatisfied Interests counter
277 if (pitEntry->isSatisfied) {
278 ++m_counters.nSatisfiedInterests;
279 }
280 else {
281 ++m_counters.nUnsatisfiedInterests;
282 }
283
Junxiao Shif3c07812014-03-11 21:48:49 -0700284 // PIT delete
Davide Pesavento3dade002019-03-19 11:29:56 -0600285 pitEntry->expiryTimer.cancel();
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000286 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700287}
288
289void
ashiqopuc7079482019-02-20 05:34:37 +0000290Forwarder::onIncomingData(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700291{
292 // receive Data
ashiqopuc7079482019-02-20 05:34:37 +0000293 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName());
294 data.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700295 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700296
Junxiao Shi88884492014-02-15 15:57:43 -0700297 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000298 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700299 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700300 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000301 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700302 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700303 return;
304 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700305
Junxiao Shid3c792f2014-01-30 00:46:13 -0700306 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700307 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
Teng Liang43bb2312018-03-26 04:16:42 -0700308 if (pitMatches.size() == 0) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700309 // goto Data unsolicited pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000310 this->onDataUnsolicited(ingress, data);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700311 return;
312 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700313
Junxiao Shid3c792f2014-01-30 00:46:13 -0700314 // CS insert
315 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700316
Teng Liang43bb2312018-03-26 04:16:42 -0700317 // when only one PIT entry is matched, trigger strategy: after receive Data
318 if (pitMatches.size() == 1) {
319 auto& pitEntry = pitMatches.front();
Junxiao Shic041ca32014-02-25 20:01:15 -0700320
Teng Liang43bb2312018-03-26 04:16:42 -0700321 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700322
Teng Liang7003e0b2018-03-03 16:03:30 -0700323 // set PIT expiry timer to now
324 this->setExpiryTimer(pitEntry, 0_ms);
325
Teng Liang43bb2312018-03-26 04:16:42 -0700326 // trigger strategy: after receive Data
Junxiao Shib9420cf2016-08-13 04:38:52 +0000327 this->dispatchToStrategy(*pitEntry,
Davide Pesaventob21bed82021-02-13 00:20:06 -0500328 [&] (auto& strategy) { strategy.afterReceiveData(pitEntry, ingress, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700329
Teng Liang43bb2312018-03-26 04:16:42 -0700330 // mark PIT satisfied
Teng Liang6f09ab62018-03-01 20:04:08 -0700331 pitEntry->isSatisfied = true;
332 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
333
Teng Liangebc20f62020-06-23 16:55:20 -0700334 // Dead Nonce List insert if necessary (for out-record of ingress face)
ashiqopuc7079482019-02-20 05:34:37 +0000335 this->insertDeadNonceList(*pitEntry, &ingress.face);
Junxiao Shia110f262014-10-12 12:35:20 -0700336
Teng Liang43bb2312018-03-26 04:16:42 -0700337 // delete PIT entry's out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000338 pitEntry->deleteOutRecord(ingress.face);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700339 }
Teng Liang43bb2312018-03-26 04:16:42 -0700340 // when more than one PIT entry is matched, trigger strategy: before satisfy Interest,
341 // and send Data to all matched out faces
342 else {
Teng Liangebc20f62020-06-23 16:55:20 -0700343 std::set<Face*> pendingDownstreams;
Teng Liang43bb2312018-03-26 04:16:42 -0700344 auto now = time::steady_clock::now();
Junxiao Shic041ca32014-02-25 20:01:15 -0700345
Davide Pesaventob31206e2019-04-20 22:34:12 -0400346 for (const auto& pitEntry : pitMatches) {
Teng Liang43bb2312018-03-26 04:16:42 -0700347 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
348
349 // remember pending downstreams
350 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
351 if (inRecord.getExpiry() > now) {
Teng Liangebc20f62020-06-23 16:55:20 -0700352 pendingDownstreams.insert(&inRecord.getFace());
Teng Liang43bb2312018-03-26 04:16:42 -0700353 }
354 }
355
356 // set PIT expiry timer to now
357 this->setExpiryTimer(pitEntry, 0_ms);
358
359 // invoke PIT satisfy callback
360 this->dispatchToStrategy(*pitEntry,
Davide Pesaventob21bed82021-02-13 00:20:06 -0500361 [&] (auto& strategy) { strategy.beforeSatisfyInterest(pitEntry, ingress, data); });
Teng Liang43bb2312018-03-26 04:16:42 -0700362
363 // mark PIT satisfied
364 pitEntry->isSatisfied = true;
365 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
366
Teng Liangebc20f62020-06-23 16:55:20 -0700367 // Dead Nonce List insert if necessary (for out-record of ingress face)
ashiqopuc7079482019-02-20 05:34:37 +0000368 this->insertDeadNonceList(*pitEntry, &ingress.face);
Teng Liang43bb2312018-03-26 04:16:42 -0700369
370 // clear PIT entry's in and out records
371 pitEntry->clearInRecords();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000372 pitEntry->deleteOutRecord(ingress.face);
Junxiao Shida006f52014-05-16 11:18:00 -0700373 }
Teng Liang43bb2312018-03-26 04:16:42 -0700374
375 // foreach pending downstream
ashiqopuc7079482019-02-20 05:34:37 +0000376 for (const auto& pendingDownstream : pendingDownstreams) {
Teng Liangebc20f62020-06-23 16:55:20 -0700377 if (pendingDownstream->getId() == ingress.face.getId() &&
378 pendingDownstream->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Teng Liang43bb2312018-03-26 04:16:42 -0700379 continue;
380 }
381 // goto outgoing Data pipeline
Teng Liangebc20f62020-06-23 16:55:20 -0700382 this->onOutgoingData(data, *pendingDownstream);
Teng Liang43bb2312018-03-26 04:16:42 -0700383 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700384 }
385}
386
387void
ashiqopuc7079482019-02-20 05:34:37 +0000388Forwarder::onDataUnsolicited(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700389{
390 // accept to cache?
Alex Lane6bead9b2020-05-12 19:08:20 -0500391 auto decision = m_unsolicitedDataPolicy->decide(ingress.face, data);
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000392 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700393 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700394 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700395 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700396
Teng Liangebc20f62020-06-23 16:55:20 -0700397 NFD_LOG_DEBUG("onDataUnsolicited in=" << ingress << " data=" << data.getName()
398 << " decision=" << decision);
Alex Lane6bead9b2020-05-12 19:08:20 -0500399 ++m_counters.nUnsolicitedData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700400}
401
Eric Newberry2377ada2020-09-28 22:40:14 -0700402bool
Teng Liangebc20f62020-06-23 16:55:20 -0700403Forwarder::onOutgoingData(const Data& data, Face& egress)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700404{
Teng Liangebc20f62020-06-23 16:55:20 -0700405 if (egress.getId() == face::INVALID_FACEID) {
ashiqopuc7079482019-02-20 05:34:37 +0000406 NFD_LOG_WARN("onOutgoingData out=(invalid) data=" << data.getName());
Eric Newberry2377ada2020-09-28 22:40:14 -0700407 return false;
Junxiao Shi223271b2014-07-03 22:06:13 -0700408 }
Teng Liangebc20f62020-06-23 16:55:20 -0700409 NFD_LOG_DEBUG("onOutgoingData out=" << egress.getId() << " data=" << data.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700410
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700411 // /localhost scope control
Teng Liangebc20f62020-06-23 16:55:20 -0700412 bool isViolatingLocalhost = egress.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700413 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700414 if (isViolatingLocalhost) {
Teng Liangebc20f62020-06-23 16:55:20 -0700415 NFD_LOG_DEBUG("onOutgoingData out=" << egress.getId() << " data=" << data.getName()
416 << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700417 // (drop)
Eric Newberry2377ada2020-09-28 22:40:14 -0700418 return false;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700419 }
420
Junxiao Shif3c07812014-03-11 21:48:49 -0700421 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700422
Junxiao Shid3c792f2014-01-30 00:46:13 -0700423 // send Data
Teng Liangebc20f62020-06-23 16:55:20 -0700424 egress.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700425 ++m_counters.nOutData;
Eric Newberry2377ada2020-09-28 22:40:14 -0700426
427 return true;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700428}
429
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700430void
ashiqopuc7079482019-02-20 05:34:37 +0000431Forwarder::onIncomingNack(const FaceEndpoint& ingress, const lp::Nack& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700432{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000433 // receive Nack
ashiqopuc7079482019-02-20 05:34:37 +0000434 nack.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700435 ++m_counters.nInNacks;
436
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700437 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000438 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400439 NFD_LOG_DEBUG("onIncomingNack in=" << ingress
440 << " nack=" << nack.getInterest().getName() << "~" << nack.getReason()
441 << " link-type=" << ingress.face.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700442 return;
443 }
444
445 // PIT match
446 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
447 // if no PIT entry found, drop
448 if (pitEntry == nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000449 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
450 << "~" << nack.getReason() << " no-PIT-entry");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700451 return;
452 }
453
454 // has out-record?
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000455 auto outRecord = pitEntry->getOutRecord(ingress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700456 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700457 if (outRecord == pitEntry->out_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000458 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
459 << "~" << nack.getReason() << " no-out-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700460 return;
461 }
462
463 // if out-record has different Nonce, drop
464 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
ashiqopuc7079482019-02-20 05:34:37 +0000465 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
466 << "~" << nack.getReason() << " wrong-Nonce " << nack.getInterest().getNonce()
467 << "!=" << outRecord->getLastNonce());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700468 return;
469 }
470
ashiqopuc7079482019-02-20 05:34:37 +0000471 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
472 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700473
474 // record Nack on out-record
475 outRecord->setIncomingNack(nack);
476
Teng Liang7003e0b2018-03-03 16:03:30 -0700477 // set PIT expiry timer to now when all out-record receive Nack
478 if (!fw::hasPendingOutRecords(*pitEntry)) {
479 this->setExpiryTimer(pitEntry, 0_ms);
480 }
481
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700482 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000483 this->dispatchToStrategy(*pitEntry,
Davide Pesaventob21bed82021-02-13 00:20:06 -0500484 [&] (auto& strategy) { strategy.afterReceiveNack(ingress, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700485}
486
Eric Newberry2377ada2020-09-28 22:40:14 -0700487bool
ashiqopuc7079482019-02-20 05:34:37 +0000488Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry,
Teng Liangebc20f62020-06-23 16:55:20 -0700489 Face& egress, const lp::NackHeader& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700490{
Teng Liangebc20f62020-06-23 16:55:20 -0700491 if (egress.getId() == face::INVALID_FACEID) {
ashiqopuc7079482019-02-20 05:34:37 +0000492 NFD_LOG_WARN("onOutgoingNack out=(invalid)"
Davide Pesaventob31206e2019-04-20 22:34:12 -0400493 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason());
Eric Newberry2377ada2020-09-28 22:40:14 -0700494 return false;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700495 }
496
497 // has in-record?
Teng Liangebc20f62020-06-23 16:55:20 -0700498 auto inRecord = pitEntry->getInRecord(egress);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700499
500 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700501 if (inRecord == pitEntry->in_end()) {
Teng Liangebc20f62020-06-23 16:55:20 -0700502 NFD_LOG_DEBUG("onOutgoingNack out=" << egress.getId()
ashiqopuc7079482019-02-20 05:34:37 +0000503 << " nack=" << pitEntry->getInterest().getName()
504 << "~" << nack.getReason() << " no-in-record");
Eric Newberry2377ada2020-09-28 22:40:14 -0700505 return false;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700506 }
507
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700508 // if multi-access or ad hoc face, drop
Teng Liangebc20f62020-06-23 16:55:20 -0700509 if (egress.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
510 NFD_LOG_DEBUG("onOutgoingNack out=" << egress.getId()
Davide Pesaventob31206e2019-04-20 22:34:12 -0400511 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason()
Teng Liangebc20f62020-06-23 16:55:20 -0700512 << " link-type=" << egress.getLinkType());
Eric Newberry2377ada2020-09-28 22:40:14 -0700513 return false;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700514 }
515
Teng Liangebc20f62020-06-23 16:55:20 -0700516 NFD_LOG_DEBUG("onOutgoingNack out=" << egress.getId()
ashiqopuc7079482019-02-20 05:34:37 +0000517 << " nack=" << pitEntry->getInterest().getName()
518 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700519
520 // create Nack packet with the Interest from in-record
521 lp::Nack nackPkt(inRecord->getInterest());
522 nackPkt.setHeader(nack);
523
524 // erase in-record
Teng Liangebc20f62020-06-23 16:55:20 -0700525 pitEntry->deleteInRecord(egress);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700526
527 // send Nack on face
Teng Liangebc20f62020-06-23 16:55:20 -0700528 egress.sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700529 ++m_counters.nOutNacks;
Eric Newberry2377ada2020-09-28 22:40:14 -0700530
531 return true;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700532}
533
Eric Newberry41aba102017-11-01 16:42:13 -0700534void
Teng Liangebc20f62020-06-23 16:55:20 -0700535Forwarder::onDroppedInterest(const Face& egress, const Interest& interest)
Eric Newberry41aba102017-11-01 16:42:13 -0700536{
ashiqopuc7079482019-02-20 05:34:37 +0000537 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(egress, interest);
Eric Newberry41aba102017-11-01 16:42:13 -0700538}
539
Junxiao Shid3c792f2014-01-30 00:46:13 -0700540void
Ju Pan2feb4592019-09-16 20:56:38 +0000541Forwarder::onNewNextHop(const Name& prefix, const fib::NextHop& nextHop)
542{
543 const auto affectedEntries = this->getNameTree().partialEnumerate(prefix,
544 [&] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
Davide Pesaventob21bed82021-02-13 00:20:06 -0500545 // we ignore an NTE and skip visiting its descendants if that NTE has an
546 // associated FIB entry (1st condition), since in that case the new nexthop
547 // won't affect any PIT entries anywhere in that subtree, *unless* this is
548 // the initial NTE from which the enumeration started (2nd condition), which
549 // must always be considered
550 if (nte.getFibEntry() != nullptr && nte.getName().size() > prefix.size()) {
Ju Pan2feb4592019-09-16 20:56:38 +0000551 return {false, false};
552 }
Davide Pesaventob21bed82021-02-13 00:20:06 -0500553 return {nte.hasPitEntries(), true};
Ju Pan2feb4592019-09-16 20:56:38 +0000554 });
555
556 for (const auto& nte : affectedEntries) {
557 for (const auto& pitEntry : nte.getPitEntries()) {
558 this->dispatchToStrategy(*pitEntry,
Davide Pesaventob21bed82021-02-13 00:20:06 -0500559 [&] (auto& strategy) { strategy.afterNewNextHop(nextHop, pitEntry); });
Ju Pan2feb4592019-09-16 20:56:38 +0000560 }
561 }
562}
563
564void
Teng Liang7003e0b2018-03-03 16:03:30 -0700565Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700566{
Teng Liang39465c22018-11-12 19:43:04 -0700567 BOOST_ASSERT(pitEntry);
Junxiao Shie1cf4ba2020-06-19 16:40:53 -0600568 duration = std::max(duration, 0_ms);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700569
Davide Pesavento3dade002019-03-19 11:29:56 -0600570 pitEntry->expiryTimer.cancel();
571 pitEntry->expiryTimer = getScheduler().schedule(duration, [=] { onInterestFinalize(pitEntry); });
Junxiao Shia110f262014-10-12 12:35:20 -0700572}
573
574void
Teng Liang6f09ab62018-03-01 20:04:08 -0700575Forwarder::insertDeadNonceList(pit::Entry& pitEntry, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700576{
577 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000578 bool needDnl = true;
Teng Liang6f09ab62018-03-01 20:04:08 -0700579 if (pitEntry.isSatisfied) {
580 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
Junxiao Shia110f262014-10-12 12:35:20 -0700581 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Teng Liang6f09ab62018-03-01 20:04:08 -0700582 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700583 }
584
585 if (!needDnl) {
586 return;
587 }
588
589 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000590 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700591 // insert all outgoing Nonces
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400592 const auto& outRecords = pitEntry.getOutRecords();
593 std::for_each(outRecords.begin(), outRecords.end(), [&] (const auto& outRecord) {
594 m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
595 });
Junxiao Shia110f262014-10-12 12:35:20 -0700596 }
597 else {
598 // insert outgoing Nonce of a specific face
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000599 auto outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700600 if (outRecord != pitEntry.getOutRecords().end()) {
601 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
602 }
603 }
604}
605
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800606} // namespace nfd