blob: bd4d69278439ec3b1d22a26763c101ee3eee1f93 [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/*
Eric Newberry9d283ad2020-04-12 23:37:17 -07003 * Copyright (c) 2014-2020, 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
Ju Pan2feb4592019-09-16 20:56:38 +000079 m_fib.afterNewNextHop.connect([&] (const Name& prefix, const fib::NextHop& nextHop) {
80 this->startProcessNewNextHop(prefix, nextHop);
81 });
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,
Md Ashiqur Rahman115ea632019-07-06 02:34:31 +0000216 [&] (fw::Strategy& strategy) {
217 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,
ashiqopuc7079482019-02-20 05:34:37 +0000240 [&] (fw::Strategy& strategy) { strategy.afterContentStoreHit(pitEntry, ingress, data); });
mzhang4eab72492015-02-25 11:16:09 -0600241}
242
Junxiao Shid3c792f2014-01-30 00:46:13 -0700243void
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 Newberry9d283ad2020-04-12 23:37:17 -0700252 return;
253 }
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
Teng Liangebc20f62020-06-23 16:55:20 -0700258 pitEntry->insertOrUpdateOutRecord(egress, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700259
Junxiao Shid3c792f2014-01-30 00:46:13 -0700260 // send Interest
Teng Liangebc20f62020-06-23 16:55:20 -0700261 egress.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700262 ++m_counters.nOutInterests;
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
Junxiao Shib9420cf2016-08-13 04:38:52 +0000325 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000326 [&] (fw::Strategy& strategy) { strategy.afterReceiveData(pitEntry, ingress, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700327
Teng Liang43bb2312018-03-26 04:16:42 -0700328 // mark PIT satisfied
Teng Liang6f09ab62018-03-01 20:04:08 -0700329 pitEntry->isSatisfied = true;
330 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
331
Teng Liangebc20f62020-06-23 16:55:20 -0700332 // Dead Nonce List insert if necessary (for out-record of ingress face)
ashiqopuc7079482019-02-20 05:34:37 +0000333 this->insertDeadNonceList(*pitEntry, &ingress.face);
Junxiao Shia110f262014-10-12 12:35:20 -0700334
Teng Liang43bb2312018-03-26 04:16:42 -0700335 // delete PIT entry's out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000336 pitEntry->deleteOutRecord(ingress.face);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700337 }
Teng Liang43bb2312018-03-26 04:16:42 -0700338 // when more than one PIT entry is matched, trigger strategy: before satisfy Interest,
339 // and send Data to all matched out faces
340 else {
Teng Liangebc20f62020-06-23 16:55:20 -0700341 std::set<Face*> pendingDownstreams;
Teng Liang43bb2312018-03-26 04:16:42 -0700342 auto now = time::steady_clock::now();
Junxiao Shic041ca32014-02-25 20:01:15 -0700343
Davide Pesaventob31206e2019-04-20 22:34:12 -0400344 for (const auto& pitEntry : pitMatches) {
Teng Liang43bb2312018-03-26 04:16:42 -0700345 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
346
347 // remember pending downstreams
348 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
349 if (inRecord.getExpiry() > now) {
Teng Liangebc20f62020-06-23 16:55:20 -0700350 pendingDownstreams.insert(&inRecord.getFace());
Teng Liang43bb2312018-03-26 04:16:42 -0700351 }
352 }
353
354 // set PIT expiry timer to now
355 this->setExpiryTimer(pitEntry, 0_ms);
356
357 // invoke PIT satisfy callback
358 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000359 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, ingress, data); });
Teng Liang43bb2312018-03-26 04:16:42 -0700360
361 // mark PIT satisfied
362 pitEntry->isSatisfied = true;
363 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
364
Teng Liangebc20f62020-06-23 16:55:20 -0700365 // Dead Nonce List insert if necessary (for out-record of ingress face)
ashiqopuc7079482019-02-20 05:34:37 +0000366 this->insertDeadNonceList(*pitEntry, &ingress.face);
Teng Liang43bb2312018-03-26 04:16:42 -0700367
368 // clear PIT entry's in and out records
369 pitEntry->clearInRecords();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000370 pitEntry->deleteOutRecord(ingress.face);
Junxiao Shida006f52014-05-16 11:18:00 -0700371 }
Teng Liang43bb2312018-03-26 04:16:42 -0700372
373 // foreach pending downstream
ashiqopuc7079482019-02-20 05:34:37 +0000374 for (const auto& pendingDownstream : pendingDownstreams) {
Teng Liangebc20f62020-06-23 16:55:20 -0700375 if (pendingDownstream->getId() == ingress.face.getId() &&
376 pendingDownstream->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Teng Liang43bb2312018-03-26 04:16:42 -0700377 continue;
378 }
379 // goto outgoing Data pipeline
Teng Liangebc20f62020-06-23 16:55:20 -0700380 this->onOutgoingData(data, *pendingDownstream);
Teng Liang43bb2312018-03-26 04:16:42 -0700381 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700382 }
383}
384
385void
ashiqopuc7079482019-02-20 05:34:37 +0000386Forwarder::onDataUnsolicited(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700387{
388 // accept to cache?
Alex Lane6bead9b2020-05-12 19:08:20 -0500389 auto decision = m_unsolicitedDataPolicy->decide(ingress.face, data);
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000390 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700391 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700392 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700393 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700394
Teng Liangebc20f62020-06-23 16:55:20 -0700395 NFD_LOG_DEBUG("onDataUnsolicited in=" << ingress << " data=" << data.getName()
396 << " decision=" << decision);
Alex Lane6bead9b2020-05-12 19:08:20 -0500397 ++m_counters.nUnsolicitedData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700398}
399
400void
Teng Liangebc20f62020-06-23 16:55:20 -0700401Forwarder::onOutgoingData(const Data& data, Face& egress)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700402{
Teng Liangebc20f62020-06-23 16:55:20 -0700403 if (egress.getId() == face::INVALID_FACEID) {
ashiqopuc7079482019-02-20 05:34:37 +0000404 NFD_LOG_WARN("onOutgoingData out=(invalid) data=" << data.getName());
Junxiao Shi223271b2014-07-03 22:06:13 -0700405 return;
406 }
Teng Liangebc20f62020-06-23 16:55:20 -0700407 NFD_LOG_DEBUG("onOutgoingData out=" << egress.getId() << " data=" << data.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700408
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700409 // /localhost scope control
Teng Liangebc20f62020-06-23 16:55:20 -0700410 bool isViolatingLocalhost = egress.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700411 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700412 if (isViolatingLocalhost) {
Teng Liangebc20f62020-06-23 16:55:20 -0700413 NFD_LOG_DEBUG("onOutgoingData out=" << egress.getId() << " data=" << data.getName()
414 << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700415 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700416 return;
417 }
418
Junxiao Shif3c07812014-03-11 21:48:49 -0700419 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700420
Junxiao Shid3c792f2014-01-30 00:46:13 -0700421 // send Data
Teng Liangebc20f62020-06-23 16:55:20 -0700422 egress.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700423 ++m_counters.nOutData;
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
Junxiao Shib9420cf2016-08-13 04:38:52 +0000479 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000480 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(ingress, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700481}
482
483void
ashiqopuc7079482019-02-20 05:34:37 +0000484Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry,
Teng Liangebc20f62020-06-23 16:55:20 -0700485 Face& egress, const lp::NackHeader& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700486{
Teng Liangebc20f62020-06-23 16:55:20 -0700487 if (egress.getId() == face::INVALID_FACEID) {
ashiqopuc7079482019-02-20 05:34:37 +0000488 NFD_LOG_WARN("onOutgoingNack out=(invalid)"
Davide Pesaventob31206e2019-04-20 22:34:12 -0400489 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700490 return;
491 }
492
493 // has in-record?
Teng Liangebc20f62020-06-23 16:55:20 -0700494 auto inRecord = pitEntry->getInRecord(egress);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700495
496 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700497 if (inRecord == pitEntry->in_end()) {
Teng Liangebc20f62020-06-23 16:55:20 -0700498 NFD_LOG_DEBUG("onOutgoingNack out=" << egress.getId()
ashiqopuc7079482019-02-20 05:34:37 +0000499 << " nack=" << pitEntry->getInterest().getName()
500 << "~" << nack.getReason() << " no-in-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700501 return;
502 }
503
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700504 // if multi-access or ad hoc face, drop
Teng Liangebc20f62020-06-23 16:55:20 -0700505 if (egress.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
506 NFD_LOG_DEBUG("onOutgoingNack out=" << egress.getId()
Davide Pesaventob31206e2019-04-20 22:34:12 -0400507 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason()
Teng Liangebc20f62020-06-23 16:55:20 -0700508 << " link-type=" << egress.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700509 return;
510 }
511
Teng Liangebc20f62020-06-23 16:55:20 -0700512 NFD_LOG_DEBUG("onOutgoingNack out=" << egress.getId()
ashiqopuc7079482019-02-20 05:34:37 +0000513 << " nack=" << pitEntry->getInterest().getName()
514 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700515
516 // create Nack packet with the Interest from in-record
517 lp::Nack nackPkt(inRecord->getInterest());
518 nackPkt.setHeader(nack);
519
520 // erase in-record
Teng Liangebc20f62020-06-23 16:55:20 -0700521 pitEntry->deleteInRecord(egress);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700522
523 // send Nack on face
Teng Liangebc20f62020-06-23 16:55:20 -0700524 egress.sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700525 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700526}
527
Eric Newberry41aba102017-11-01 16:42:13 -0700528void
Teng Liangebc20f62020-06-23 16:55:20 -0700529Forwarder::onDroppedInterest(const Face& egress, const Interest& interest)
Eric Newberry41aba102017-11-01 16:42:13 -0700530{
ashiqopuc7079482019-02-20 05:34:37 +0000531 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(egress, interest);
Eric Newberry41aba102017-11-01 16:42:13 -0700532}
533
Junxiao Shid3c792f2014-01-30 00:46:13 -0700534void
Ju Pan2feb4592019-09-16 20:56:38 +0000535Forwarder::onNewNextHop(const Name& prefix, const fib::NextHop& nextHop)
536{
537 const auto affectedEntries = this->getNameTree().partialEnumerate(prefix,
538 [&] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
539 const fib::Entry* fibEntry = nte.getFibEntry();
540 const fw::Strategy* strategy = nullptr;
541 if (nte.getStrategyChoiceEntry() != nullptr) {
542 strategy = &nte.getStrategyChoiceEntry()->getStrategy();
543 }
544 // current nte has buffered Interests but no fibEntry (except for the root nte) and the strategy
545 // enables new nexthop behavior, we enumerate the current nte and keep visiting its children.
546 if (nte.getName().size() == 0 ||
547 (strategy != nullptr && strategy->wantNewNextHopTrigger() &&
548 fibEntry == nullptr && nte.hasPitEntries())) {
549 return {true, true};
550 }
551 // we don't need the current nte (no pitEntry or strategy doesn't support new nexthop), but
552 // if the current nte has no fibEntry, it's still possible that its children are affected by
553 // the new nexthop.
554 else if (fibEntry == nullptr) {
555 return {false, true};
556 }
557 // if the current nte has a fibEntry, we ignore the current nte and don't visit its
558 // children because they are already covered by the current nte's fibEntry.
559 else {
560 return {false, false};
561 }
562 });
563
564 for (const auto& nte : affectedEntries) {
565 for (const auto& pitEntry : nte.getPitEntries()) {
566 this->dispatchToStrategy(*pitEntry,
567 [&] (fw::Strategy& strategy) {
568 strategy.afterNewNextHop(nextHop, pitEntry);
569 });
570 }
571 }
572}
573
574void
Teng Liang7003e0b2018-03-03 16:03:30 -0700575Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700576{
Teng Liang39465c22018-11-12 19:43:04 -0700577 BOOST_ASSERT(pitEntry);
Junxiao Shie1cf4ba2020-06-19 16:40:53 -0600578 duration = std::max(duration, 0_ms);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700579
Davide Pesavento3dade002019-03-19 11:29:56 -0600580 pitEntry->expiryTimer.cancel();
581 pitEntry->expiryTimer = getScheduler().schedule(duration, [=] { onInterestFinalize(pitEntry); });
Junxiao Shia110f262014-10-12 12:35:20 -0700582}
583
584void
Teng Liang6f09ab62018-03-01 20:04:08 -0700585Forwarder::insertDeadNonceList(pit::Entry& pitEntry, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700586{
587 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000588 bool needDnl = true;
Teng Liang6f09ab62018-03-01 20:04:08 -0700589 if (pitEntry.isSatisfied) {
590 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
Junxiao Shia110f262014-10-12 12:35:20 -0700591 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Teng Liang6f09ab62018-03-01 20:04:08 -0700592 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700593 }
594
595 if (!needDnl) {
596 return;
597 }
598
599 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000600 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700601 // insert all outgoing Nonces
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400602 const auto& outRecords = pitEntry.getOutRecords();
603 std::for_each(outRecords.begin(), outRecords.end(), [&] (const auto& outRecord) {
604 m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
605 });
Junxiao Shia110f262014-10-12 12:35:20 -0700606 }
607 else {
608 // insert outgoing Nonce of a specific face
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000609 auto outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700610 if (outRecord != pitEntry.getOutRecords().end()) {
611 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
612 }
613 }
614}
615
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800616} // namespace nfd