blob: 67b8f52b2994e06a544cb50d1ba7fe67a9b074f4 [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) {
ashiqopuc7079482019-02-20 05:34:37 +000071 this->onDroppedInterest(FaceEndpoint(face, 0), 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
ashiqopuc7079482019-02-20 05:34:37 +0000209 this->onOutgoingInterest(pitEntry, FaceEndpoint(*nextHopFace, 0), 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,
245 const FaceEndpoint& 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
248 if (interest.getHopLimit() == 0 && egress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL) {
249 NFD_LOG_DEBUG("onOutgoingInterest out=" << egress << " interest=" << pitEntry->getName()
250 << " non-local hop-limit=0");
251 ++const_cast<PacketCounter&>(egress.face.getCounters().nOutHopLimitZero);
252 return;
253 }
254
ashiqopuc7079482019-02-20 05:34:37 +0000255 NFD_LOG_DEBUG("onOutgoingInterest out=" << egress << " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700256
Junxiao Shi4846f372016-04-05 13:39:30 -0700257 // insert out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000258 pitEntry->insertOrUpdateOutRecord(egress.face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700259
Junxiao Shid3c792f2014-01-30 00:46:13 -0700260 // send Interest
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700261 egress.face.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
Junxiao Shi4846f372016-04-05 13:39:30 -0700332 // Dead Nonce List insert if necessary (for out-record of inFace)
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 {
ashiqopuc7079482019-02-20 05:34:37 +0000341 std::set<std::pair<Face*, EndpointId>> 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) {
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000350 pendingDownstreams.emplace(&inRecord.getFace(), 0);
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
365 // Dead Nonce List insert if necessary (for out-record of inFace)
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) {
375 if (pendingDownstream.first->getId() == ingress.face.getId() &&
376 pendingDownstream.second == ingress.endpoint &&
377 pendingDownstream.first->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Teng Liang43bb2312018-03-26 04:16:42 -0700378 continue;
379 }
380 // goto outgoing Data pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000381 this->onOutgoingData(data, FaceEndpoint(*pendingDownstream.first, pendingDownstream.second));
Teng Liang43bb2312018-03-26 04:16:42 -0700382 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700383 }
384}
385
386void
ashiqopuc7079482019-02-20 05:34:37 +0000387Forwarder::onDataUnsolicited(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700388{
389 // accept to cache?
Alex Lane6bead9b2020-05-12 19:08:20 -0500390 auto decision = m_unsolicitedDataPolicy->decide(ingress.face, data);
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000391 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700392 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700393 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700394 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700395
ashiqopuc7079482019-02-20 05:34:37 +0000396 NFD_LOG_DEBUG("onDataUnsolicited in=" << ingress << " data=" << data.getName() << " decision=" << decision);
Alex Lane6bead9b2020-05-12 19:08:20 -0500397 ++m_counters.nUnsolicitedData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700398}
399
400void
ashiqopu075bb7d2019-03-10 01:38:21 +0000401Forwarder::onOutgoingData(const Data& data, const FaceEndpoint& egress)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700402{
ashiqopuc7079482019-02-20 05:34:37 +0000403 if (egress.face.getId() == face::INVALID_FACEID) {
404 NFD_LOG_WARN("onOutgoingData out=(invalid) data=" << data.getName());
Junxiao Shi223271b2014-07-03 22:06:13 -0700405 return;
406 }
ashiqopuc7079482019-02-20 05:34:37 +0000407 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700408
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700409 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000410 bool isViolatingLocalhost = egress.face.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) {
ashiqopuc7079482019-02-20 05:34:37 +0000413 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700414 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700415 return;
416 }
417
Junxiao Shif3c07812014-03-11 21:48:49 -0700418 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700419
Junxiao Shid3c792f2014-01-30 00:46:13 -0700420 // send Data
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700421 egress.face.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700422 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700423}
424
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700425void
ashiqopuc7079482019-02-20 05:34:37 +0000426Forwarder::onIncomingNack(const FaceEndpoint& ingress, const lp::Nack& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700427{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000428 // receive Nack
ashiqopuc7079482019-02-20 05:34:37 +0000429 nack.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700430 ++m_counters.nInNacks;
431
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700432 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000433 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400434 NFD_LOG_DEBUG("onIncomingNack in=" << ingress
435 << " nack=" << nack.getInterest().getName() << "~" << nack.getReason()
436 << " link-type=" << ingress.face.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700437 return;
438 }
439
440 // PIT match
441 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
442 // if no PIT entry found, drop
443 if (pitEntry == nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000444 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
445 << "~" << nack.getReason() << " no-PIT-entry");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700446 return;
447 }
448
449 // has out-record?
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000450 auto outRecord = pitEntry->getOutRecord(ingress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700451 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700452 if (outRecord == pitEntry->out_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000453 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
454 << "~" << nack.getReason() << " no-out-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700455 return;
456 }
457
458 // if out-record has different Nonce, drop
459 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
ashiqopuc7079482019-02-20 05:34:37 +0000460 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
461 << "~" << nack.getReason() << " wrong-Nonce " << nack.getInterest().getNonce()
462 << "!=" << outRecord->getLastNonce());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700463 return;
464 }
465
ashiqopuc7079482019-02-20 05:34:37 +0000466 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
467 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700468
469 // record Nack on out-record
470 outRecord->setIncomingNack(nack);
471
Teng Liang7003e0b2018-03-03 16:03:30 -0700472 // set PIT expiry timer to now when all out-record receive Nack
473 if (!fw::hasPendingOutRecords(*pitEntry)) {
474 this->setExpiryTimer(pitEntry, 0_ms);
475 }
476
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700477 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000478 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000479 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(ingress, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700480}
481
482void
ashiqopuc7079482019-02-20 05:34:37 +0000483Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry,
484 const FaceEndpoint& egress, const lp::NackHeader& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700485{
ashiqopuc7079482019-02-20 05:34:37 +0000486 if (egress.face.getId() == face::INVALID_FACEID) {
487 NFD_LOG_WARN("onOutgoingNack out=(invalid)"
Davide Pesaventob31206e2019-04-20 22:34:12 -0400488 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700489 return;
490 }
491
492 // has in-record?
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000493 auto inRecord = pitEntry->getInRecord(egress.face);
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()) {
ashiqopuc7079482019-02-20 05:34:37 +0000497 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
498 << " nack=" << pitEntry->getInterest().getName()
499 << "~" << nack.getReason() << " no-in-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700500 return;
501 }
502
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700503 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000504 if (egress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
505 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
Davide Pesaventob31206e2019-04-20 22:34:12 -0400506 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason()
507 << " link-type=" << egress.face.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700508 return;
509 }
510
ashiqopuc7079482019-02-20 05:34:37 +0000511 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
512 << " 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
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000520 pitEntry->deleteInRecord(egress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700521
522 // send Nack on face
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700523 egress.face.sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700524 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700525}
526
Eric Newberry41aba102017-11-01 16:42:13 -0700527void
ashiqopuc7079482019-02-20 05:34:37 +0000528Forwarder::onDroppedInterest(const FaceEndpoint& egress, const Interest& interest)
Eric Newberry41aba102017-11-01 16:42:13 -0700529{
ashiqopuc7079482019-02-20 05:34:37 +0000530 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(egress, interest);
Eric Newberry41aba102017-11-01 16:42:13 -0700531}
532
Junxiao Shid3c792f2014-01-30 00:46:13 -0700533void
Ju Pan2feb4592019-09-16 20:56:38 +0000534Forwarder::onNewNextHop(const Name& prefix, const fib::NextHop& nextHop)
535{
536 const auto affectedEntries = this->getNameTree().partialEnumerate(prefix,
537 [&] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
538 const fib::Entry* fibEntry = nte.getFibEntry();
539 const fw::Strategy* strategy = nullptr;
540 if (nte.getStrategyChoiceEntry() != nullptr) {
541 strategy = &nte.getStrategyChoiceEntry()->getStrategy();
542 }
543 // current nte has buffered Interests but no fibEntry (except for the root nte) and the strategy
544 // enables new nexthop behavior, we enumerate the current nte and keep visiting its children.
545 if (nte.getName().size() == 0 ||
546 (strategy != nullptr && strategy->wantNewNextHopTrigger() &&
547 fibEntry == nullptr && nte.hasPitEntries())) {
548 return {true, true};
549 }
550 // we don't need the current nte (no pitEntry or strategy doesn't support new nexthop), but
551 // if the current nte has no fibEntry, it's still possible that its children are affected by
552 // the new nexthop.
553 else if (fibEntry == nullptr) {
554 return {false, true};
555 }
556 // if the current nte has a fibEntry, we ignore the current nte and don't visit its
557 // children because they are already covered by the current nte's fibEntry.
558 else {
559 return {false, false};
560 }
561 });
562
563 for (const auto& nte : affectedEntries) {
564 for (const auto& pitEntry : nte.getPitEntries()) {
565 this->dispatchToStrategy(*pitEntry,
566 [&] (fw::Strategy& strategy) {
567 strategy.afterNewNextHop(nextHop, pitEntry);
568 });
569 }
570 }
571}
572
573void
Teng Liang7003e0b2018-03-03 16:03:30 -0700574Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700575{
Teng Liang39465c22018-11-12 19:43:04 -0700576 BOOST_ASSERT(pitEntry);
Teng Liang7003e0b2018-03-03 16:03:30 -0700577 BOOST_ASSERT(duration >= 0_ms);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700578
Davide Pesavento3dade002019-03-19 11:29:56 -0600579 pitEntry->expiryTimer.cancel();
580 pitEntry->expiryTimer = getScheduler().schedule(duration, [=] { onInterestFinalize(pitEntry); });
Junxiao Shia110f262014-10-12 12:35:20 -0700581}
582
583void
Teng Liang6f09ab62018-03-01 20:04:08 -0700584Forwarder::insertDeadNonceList(pit::Entry& pitEntry, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700585{
586 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000587 bool needDnl = true;
Teng Liang6f09ab62018-03-01 20:04:08 -0700588 if (pitEntry.isSatisfied) {
589 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
Junxiao Shia110f262014-10-12 12:35:20 -0700590 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Teng Liang6f09ab62018-03-01 20:04:08 -0700591 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700592 }
593
594 if (!needDnl) {
595 return;
596 }
597
598 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000599 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700600 // insert all outgoing Nonces
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400601 const auto& outRecords = pitEntry.getOutRecords();
602 std::for_each(outRecords.begin(), outRecords.end(), [&] (const auto& outRecord) {
603 m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
604 });
Junxiao Shia110f262014-10-12 12:35:20 -0700605 }
606 else {
607 // insert outgoing Nonce of a specific face
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000608 auto outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700609 if (outRecord != pitEntry.getOutRecords().end()) {
610 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
611 }
612 }
613}
614
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800615} // namespace nfd