blob: 508a8908b93c514a5c34e2327f9c129414740ac7 [file] [log] [blame]
Alexander Afanasyev33b72772014-01-26 23:22:58 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shifc2e13d2017-07-25 02:08:48 +00002/*
Teng Liang39465c22018-11-12 19:43:04 -07003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shifaf3eb02015-02-16 10:50:36 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shiaf6569a2014-06-14 00:01:34 -070024 */
Alexander Afanasyev33b72772014-01-26 23:22:58 -080025
26#include "forwarder.hpp"
Davide Pesaventoa3148082018-04-12 18:21:54 -040027
Junxiao Shi00dc9142016-11-21 14:23:12 +000028#include "algorithm.hpp"
Junxiao Shic34d1672016-12-09 15:57:59 +000029#include "best-route-strategy2.hpp"
Junxiao Shifaf3eb02015-02-16 10:50:36 -070030#include "strategy.hpp"
Junxiao Shic34d1672016-12-09 15:57:59 +000031#include "core/logger.hpp"
Junxiao Shi02b73f52016-07-28 01:48:27 +000032#include "table/cleanup.hpp"
Davide Pesaventoa3148082018-04-12 18:21:54 -040033
Junxiao Shicbc8e942016-09-06 03:17:45 +000034#include <ndn-cxx/lp/tags.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080035
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080036namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080037
Davide Pesaventoa3148082018-04-12 18:21:54 -040038NFD_LOG_INIT(Forwarder);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070039
Junxiao Shic34d1672016-12-09 15:57:59 +000040static Name
41getDefaultStrategyName()
42{
Junxiao Shi037f4ab2016-12-13 04:27:06 +000043 return fw::BestRouteStrategy2::getStrategyName();
Junxiao Shic34d1672016-12-09 15:57:59 +000044}
45
Junxiao Shic041ca32014-02-25 20:01:15 -070046Forwarder::Forwarder()
Junxiao Shi9685cc52016-08-29 12:47:05 +000047 : m_unsolicitedDataPolicy(new fw::DefaultUnsolicitedDataPolicy())
Junxiao Shifbe8efe2016-08-22 16:02:30 +000048 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060049 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080050 , m_measurements(m_nameTree)
Junxiao Shic34d1672016-12-09 15:57:59 +000051 , m_strategyChoice(*this)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080052{
Junxiao Shidcffdaa2016-07-26 02:23:56 +000053 m_faceTable.afterAdd.connect([this] (Face& face) {
54 face.afterReceiveInterest.connect(
55 [this, &face] (const Interest& interest) {
ashiqopuc7079482019-02-20 05:34:37 +000056 this->startProcessInterest(FaceEndpoint(face, 0), interest);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000057 });
58 face.afterReceiveData.connect(
59 [this, &face] (const Data& data) {
ashiqopuc7079482019-02-20 05:34:37 +000060 this->startProcessData(FaceEndpoint(face, 0), data);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000061 });
62 face.afterReceiveNack.connect(
63 [this, &face] (const lp::Nack& nack) {
ashiqopuc7079482019-02-20 05:34:37 +000064 this->startProcessNack(FaceEndpoint(face, 0), nack);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000065 });
Eric Newberry41aba102017-11-01 16:42:13 -070066 face.onDroppedInterest.connect(
67 [this, &face] (const Interest& interest) {
ashiqopuc7079482019-02-20 05:34:37 +000068 this->onDroppedInterest(FaceEndpoint(face, 0), interest);
Eric Newberry41aba102017-11-01 16:42:13 -070069 });
Junxiao Shidcffdaa2016-07-26 02:23:56 +000070 });
71
72 m_faceTable.beforeRemove.connect([this] (Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000073 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000074 });
Junxiao Shic34d1672016-12-09 15:57:59 +000075
76 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080077}
78
Junxiao Shidcffdaa2016-07-26 02:23:56 +000079Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060080
Junxiao Shi0355e9f2015-09-02 07:24:53 -070081void
ashiqopuc7079482019-02-20 05:34:37 +000082Forwarder::onIncomingInterest(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -070083{
84 // receive Interest
ashiqopuc7079482019-02-20 05:34:37 +000085 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress << " interest=" << interest.getName());
86 interest.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -070087 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -070088
Junxiao Shi88884492014-02-15 15:57:43 -070089 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +000090 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -070091 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -070092 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +000093 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
94 << " interest=" << interest.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -070095 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -070096 return;
97 }
Junxiao Shic041ca32014-02-25 20:01:15 -070098
Junxiao Shi330136a2016-03-10 04:53:08 -070099 // detect duplicate Nonce with Dead Nonce List
100 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
101 if (hasDuplicateNonceInDnl) {
102 // goto Interest loop pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000103 this->onInterestLoop(ingress, interest);
Junxiao Shi330136a2016-03-10 04:53:08 -0700104 return;
105 }
106
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000107 // strip forwarding hint if Interest has reached producer region
108 if (!interest.getForwardingHint().empty() &&
109 m_networkRegionTable.isInProducerRegion(interest.getForwardingHint())) {
ashiqopuc7079482019-02-20 05:34:37 +0000110 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
111 << " interest=" << interest.getName() << " reaching-producer-region");
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000112 const_cast<Interest&>(interest).setForwardingHint({});
Junxiao Shif9858fd2017-02-01 16:22:44 +0000113 }
114
Junxiao Shid3c792f2014-01-30 00:46:13 -0700115 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700116 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700117
Junxiao Shi330136a2016-03-10 04:53:08 -0700118 // detect duplicate Nonce in PIT entry
ashiqopuc7079482019-02-20 05:34:37 +0000119 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), ingress.face);
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000120 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
ashiqopuc7079482019-02-20 05:34:37 +0000121 if (ingress.face.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000122 // for p2p face: duplicate Nonce from same incoming face is not loop
123 hasDuplicateNonceInPit = hasDuplicateNonceInPit && !(dnw & fw::DUPLICATE_NONCE_IN_SAME);
124 }
Junxiao Shi330136a2016-03-10 04:53:08 -0700125 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700126 // goto Interest loop pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000127 this->onInterestLoop(ingress, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700128 return;
129 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700130
Junxiao Shif3c07812014-03-11 21:48:49 -0700131 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700132 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600133 m_cs.find(interest,
ashiqopuc7079482019-02-20 05:34:37 +0000134 bind(&Forwarder::onContentStoreHit, this, ingress, pitEntry, _1, _2),
135 bind(&Forwarder::onContentStoreMiss, this, ingress, pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700136 }
mzhang4eab72492015-02-25 11:16:09 -0600137 else {
ashiqopuc7079482019-02-20 05:34:37 +0000138 this->onContentStoreMiss(ingress, pitEntry, interest);
mzhang4eab72492015-02-25 11:16:09 -0600139 }
140}
Junxiao Shic041ca32014-02-25 20:01:15 -0700141
mzhang4eab72492015-02-25 11:16:09 -0600142void
ashiqopuc7079482019-02-20 05:34:37 +0000143Forwarder::onInterestLoop(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700144{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700145 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000146 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
147 NFD_LOG_DEBUG("onInterestLoop in=" << ingress
148 << " interest=" << interest.getName() << " drop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700149 return;
150 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700151
ashiqopuc7079482019-02-20 05:34:37 +0000152 NFD_LOG_DEBUG("onInterestLoop in=" << ingress << " interest=" << interest.getName()
153 << " send-Nack-duplicate");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700154
155 // send Nack with reason=DUPLICATE
156 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
157 lp::Nack nack(interest);
158 nack.setReason(lp::NackReason::DUPLICATE);
ashiqopuc7079482019-02-20 05:34:37 +0000159 ingress.face.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700160}
161
Teng Liang7003e0b2018-03-03 16:03:30 -0700162static inline bool
163compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
164{
165 return a.getExpiry() < b.getExpiry();
166}
167
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700168void
ashiqopuc7079482019-02-20 05:34:37 +0000169Forwarder::onContentStoreMiss(const FaceEndpoint& ingress,
170 const shared_ptr<pit::Entry>& pitEntry, const Interest& interest)
mzhang4eab72492015-02-25 11:16:09 -0600171{
172 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000173 ++m_counters.nCsMisses;
mzhang4eab72492015-02-25 11:16:09 -0600174
Junxiao Shi4846f372016-04-05 13:39:30 -0700175 // insert in-record
ashiqopuc7079482019-02-20 05:34:37 +0000176 pitEntry->insertOrUpdateInRecord(ingress.face, ingress.endpoint, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700177
Teng Liang7003e0b2018-03-03 16:03:30 -0700178 // set PIT expiry timer to the time that the last PIT in-record expires
179 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
180 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
181 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700182
Junxiao Shie342e8d2016-09-18 16:48:00 +0000183 // has NextHopFaceId?
184 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
185 if (nextHopTag != nullptr) {
186 // chosen NextHop face exists?
187 Face* nextHopFace = m_faceTable.get(*nextHopTag);
188 if (nextHopFace != nullptr) {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000189 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName() << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000190 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000191 // scope control is unnecessary, because privileged app explicitly wants to forward
ashiqopuc7079482019-02-20 05:34:37 +0000192 this->onOutgoingInterest(pitEntry, FaceEndpoint(*nextHopFace, 0), interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000193 }
194 return;
195 }
196
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000197 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000198 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000199 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(ingress, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700200}
201
202void
ashiqopuc7079482019-02-20 05:34:37 +0000203Forwarder::onContentStoreHit(const FaceEndpoint& ingress, const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000204 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600205{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500206 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000207 ++m_counters.nCsHits;
mzhang4eab72492015-02-25 11:16:09 -0600208
Junxiao Shicde37ad2015-12-24 01:02:05 -0700209 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600210 // XXX should we lookup PIT for other Interests that also match csMatch?
211
Teng Liang6f09ab62018-03-01 20:04:08 -0700212 pitEntry->isSatisfied = true;
213 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
214
Teng Liang85a36632018-03-21 05:59:34 -0700215 // set PIT expiry timer to now
216 this->setExpiryTimer(pitEntry, 0_ms);
mzhang4eab72492015-02-25 11:16:09 -0600217
Teng Liang85a36632018-03-21 05:59:34 -0700218 // dispatch to strategy: after Content Store hit
219 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000220 [&] (fw::Strategy& strategy) { strategy.afterContentStoreHit(pitEntry, ingress, data); });
mzhang4eab72492015-02-25 11:16:09 -0600221}
222
Junxiao Shid3c792f2014-01-30 00:46:13 -0700223void
ashiqopuc7079482019-02-20 05:34:37 +0000224Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry,
225 const FaceEndpoint& egress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700226{
ashiqopuc7079482019-02-20 05:34:37 +0000227 NFD_LOG_DEBUG("onOutgoingInterest out=" << egress << " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700228
Junxiao Shi4846f372016-04-05 13:39:30 -0700229 // insert out-record
ashiqopuc7079482019-02-20 05:34:37 +0000230 pitEntry->insertOrUpdateOutRecord(egress.face, egress.endpoint, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700231
Junxiao Shid3c792f2014-01-30 00:46:13 -0700232 // send Interest
ashiqopuc7079482019-02-20 05:34:37 +0000233 egress.face.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700234 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700235}
236
237void
Teng Liang6f09ab62018-03-01 20:04:08 -0700238Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shia110f262014-10-12 12:35:20 -0700239{
ashiqopuc7079482019-02-20 05:34:37 +0000240 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName()
241 << (pitEntry->isSatisfied ? " satisfied" : " unsatisfied"));
Junxiao Shia110f262014-10-12 12:35:20 -0700242
243 // Dead Nonce List insert if necessary
Teng Liang6f09ab62018-03-01 20:04:08 -0700244 this->insertDeadNonceList(*pitEntry, 0);
Junxiao Shia110f262014-10-12 12:35:20 -0700245
Ju Pan6eb1ac92018-10-26 16:26:15 +0000246 // Increment satisfied/unsatisfied Interests counter
247 if (pitEntry->isSatisfied) {
248 ++m_counters.nSatisfiedInterests;
249 }
250 else {
251 ++m_counters.nUnsatisfiedInterests;
252 }
253
Junxiao Shif3c07812014-03-11 21:48:49 -0700254 // PIT delete
Teng Liang7003e0b2018-03-03 16:03:30 -0700255 scheduler::cancel(pitEntry->expiryTimer);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000256 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700257}
258
259void
ashiqopuc7079482019-02-20 05:34:37 +0000260Forwarder::onIncomingData(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700261{
262 // receive Data
ashiqopuc7079482019-02-20 05:34:37 +0000263 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName());
264 data.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700265 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700266
Junxiao Shi88884492014-02-15 15:57:43 -0700267 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000268 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700269 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700270 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000271 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700272 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700273 return;
274 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700275
Junxiao Shid3c792f2014-01-30 00:46:13 -0700276 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700277 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
Teng Liang43bb2312018-03-26 04:16:42 -0700278 if (pitMatches.size() == 0) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700279 // goto Data unsolicited pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000280 this->onDataUnsolicited(ingress, data);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700281 return;
282 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700283
Junxiao Shid3c792f2014-01-30 00:46:13 -0700284 // CS insert
285 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700286
Teng Liang43bb2312018-03-26 04:16:42 -0700287 // when only one PIT entry is matched, trigger strategy: after receive Data
288 if (pitMatches.size() == 1) {
289 auto& pitEntry = pitMatches.front();
Junxiao Shic041ca32014-02-25 20:01:15 -0700290
Teng Liang43bb2312018-03-26 04:16:42 -0700291 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700292
Teng Liang7003e0b2018-03-03 16:03:30 -0700293 // set PIT expiry timer to now
294 this->setExpiryTimer(pitEntry, 0_ms);
295
Teng Liang43bb2312018-03-26 04:16:42 -0700296 // trigger strategy: after receive Data
Junxiao Shib9420cf2016-08-13 04:38:52 +0000297 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000298 [&] (fw::Strategy& strategy) { strategy.afterReceiveData(pitEntry, ingress, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700299
Teng Liang43bb2312018-03-26 04:16:42 -0700300 // mark PIT satisfied
Teng Liang6f09ab62018-03-01 20:04:08 -0700301 pitEntry->isSatisfied = true;
302 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
303
Junxiao Shi4846f372016-04-05 13:39:30 -0700304 // Dead Nonce List insert if necessary (for out-record of inFace)
ashiqopuc7079482019-02-20 05:34:37 +0000305 this->insertDeadNonceList(*pitEntry, &ingress.face);
Junxiao Shia110f262014-10-12 12:35:20 -0700306
Teng Liang43bb2312018-03-26 04:16:42 -0700307 // delete PIT entry's out-record
ashiqopuc7079482019-02-20 05:34:37 +0000308 pitEntry->deleteOutRecord(ingress.face, ingress.endpoint);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700309 }
Teng Liang43bb2312018-03-26 04:16:42 -0700310 // when more than one PIT entry is matched, trigger strategy: before satisfy Interest,
311 // and send Data to all matched out faces
312 else {
ashiqopuc7079482019-02-20 05:34:37 +0000313 std::set<std::pair<Face*, EndpointId>> pendingDownstreams;
Teng Liang43bb2312018-03-26 04:16:42 -0700314 auto now = time::steady_clock::now();
Junxiao Shic041ca32014-02-25 20:01:15 -0700315
Teng Liang43bb2312018-03-26 04:16:42 -0700316 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
317 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
318
319 // remember pending downstreams
320 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
321 if (inRecord.getExpiry() > now) {
ashiqopuc7079482019-02-20 05:34:37 +0000322 pendingDownstreams.emplace(&inRecord.getFace(), inRecord.getEndpointId());
Teng Liang43bb2312018-03-26 04:16:42 -0700323 }
324 }
325
326 // set PIT expiry timer to now
327 this->setExpiryTimer(pitEntry, 0_ms);
328
329 // invoke PIT satisfy callback
330 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000331 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, ingress, data); });
Teng Liang43bb2312018-03-26 04:16:42 -0700332
333 // mark PIT satisfied
334 pitEntry->isSatisfied = true;
335 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
336
337 // Dead Nonce List insert if necessary (for out-record of inFace)
ashiqopuc7079482019-02-20 05:34:37 +0000338 this->insertDeadNonceList(*pitEntry, &ingress.face);
Teng Liang43bb2312018-03-26 04:16:42 -0700339
340 // clear PIT entry's in and out records
341 pitEntry->clearInRecords();
ashiqopuc7079482019-02-20 05:34:37 +0000342 pitEntry->deleteOutRecord(ingress.face, ingress.endpoint);
Junxiao Shida006f52014-05-16 11:18:00 -0700343 }
Teng Liang43bb2312018-03-26 04:16:42 -0700344
345 // foreach pending downstream
ashiqopuc7079482019-02-20 05:34:37 +0000346 for (const auto& pendingDownstream : pendingDownstreams) {
347 if (pendingDownstream.first->getId() == ingress.face.getId() &&
348 pendingDownstream.second == ingress.endpoint &&
349 pendingDownstream.first->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Teng Liang43bb2312018-03-26 04:16:42 -0700350 continue;
351 }
352 // goto outgoing Data pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000353 this->onOutgoingData(data, FaceEndpoint(*pendingDownstream.first, pendingDownstream.second));
Teng Liang43bb2312018-03-26 04:16:42 -0700354 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700355 }
356}
357
358void
ashiqopuc7079482019-02-20 05:34:37 +0000359Forwarder::onDataUnsolicited(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700360{
361 // accept to cache?
ashiqopuc7079482019-02-20 05:34:37 +0000362 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(ingress.face, data);
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000363 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700364 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700365 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700366 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700367
ashiqopuc7079482019-02-20 05:34:37 +0000368 NFD_LOG_DEBUG("onDataUnsolicited in=" << ingress << " data=" << data.getName() << " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700369}
370
371void
ashiqopuc7079482019-02-20 05:34:37 +0000372Forwarder::onOutgoingData(const Data& data, FaceEndpoint egress)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700373{
ashiqopuc7079482019-02-20 05:34:37 +0000374 if (egress.face.getId() == face::INVALID_FACEID) {
375 NFD_LOG_WARN("onOutgoingData out=(invalid) data=" << data.getName());
Junxiao Shi223271b2014-07-03 22:06:13 -0700376 return;
377 }
ashiqopuc7079482019-02-20 05:34:37 +0000378 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700379
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700380 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000381 bool isViolatingLocalhost = egress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700382 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700383 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000384 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700385 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700386 return;
387 }
388
Junxiao Shif3c07812014-03-11 21:48:49 -0700389 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700390
Junxiao Shid3c792f2014-01-30 00:46:13 -0700391 // send Data
ashiqopuc7079482019-02-20 05:34:37 +0000392 egress.face.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700393 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700394}
395
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700396void
ashiqopuc7079482019-02-20 05:34:37 +0000397Forwarder::onIncomingNack(const FaceEndpoint& ingress, const lp::Nack& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700398{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000399 // receive Nack
ashiqopuc7079482019-02-20 05:34:37 +0000400 nack.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700401 ++m_counters.nInNacks;
402
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700403 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000404 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
405 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
406 << "~" << nack.getReason() << " face-is-multi-access");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700407 return;
408 }
409
410 // PIT match
411 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
412 // if no PIT entry found, drop
413 if (pitEntry == nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000414 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
415 << "~" << nack.getReason() << " no-PIT-entry");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700416 return;
417 }
418
419 // has out-record?
ashiqopuc7079482019-02-20 05:34:37 +0000420 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(ingress.face, ingress.endpoint);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700421 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700422 if (outRecord == pitEntry->out_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000423 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
424 << "~" << nack.getReason() << " no-out-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700425 return;
426 }
427
428 // if out-record has different Nonce, drop
429 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
ashiqopuc7079482019-02-20 05:34:37 +0000430 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
431 << "~" << nack.getReason() << " wrong-Nonce " << nack.getInterest().getNonce()
432 << "!=" << outRecord->getLastNonce());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700433 return;
434 }
435
ashiqopuc7079482019-02-20 05:34:37 +0000436 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
437 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700438
439 // record Nack on out-record
440 outRecord->setIncomingNack(nack);
441
Teng Liang7003e0b2018-03-03 16:03:30 -0700442 // set PIT expiry timer to now when all out-record receive Nack
443 if (!fw::hasPendingOutRecords(*pitEntry)) {
444 this->setExpiryTimer(pitEntry, 0_ms);
445 }
446
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700447 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000448 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000449 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(ingress, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700450}
451
452void
ashiqopuc7079482019-02-20 05:34:37 +0000453Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry,
454 const FaceEndpoint& egress, const lp::NackHeader& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700455{
ashiqopuc7079482019-02-20 05:34:37 +0000456 if (egress.face.getId() == face::INVALID_FACEID) {
457 NFD_LOG_WARN("onOutgoingNack out=(invalid)"
458 << " nack=" << pitEntry->getInterest().getName()
459 << "~" << nack.getReason() << " no-in-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700460 return;
461 }
462
463 // has in-record?
ashiqopuc7079482019-02-20 05:34:37 +0000464 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(egress.face, egress.endpoint);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700465
466 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700467 if (inRecord == pitEntry->in_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000468 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
469 << " nack=" << pitEntry->getInterest().getName()
470 << "~" << nack.getReason() << " no-in-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700471 return;
472 }
473
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700474 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000475 if (egress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
476 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
477 << " nack=" << pitEntry->getInterest().getName()
478 << "~" << nack.getReason() << " face-is-multi-access");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700479 return;
480 }
481
ashiqopuc7079482019-02-20 05:34:37 +0000482 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
483 << " nack=" << pitEntry->getInterest().getName()
484 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700485
486 // create Nack packet with the Interest from in-record
487 lp::Nack nackPkt(inRecord->getInterest());
488 nackPkt.setHeader(nack);
489
490 // erase in-record
ashiqopuc7079482019-02-20 05:34:37 +0000491 pitEntry->deleteInRecord(egress.face, egress.endpoint);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700492
493 // send Nack on face
ashiqopuc7079482019-02-20 05:34:37 +0000494 egress.face.sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700495 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700496}
497
Eric Newberry41aba102017-11-01 16:42:13 -0700498void
ashiqopuc7079482019-02-20 05:34:37 +0000499Forwarder::onDroppedInterest(const FaceEndpoint& egress, const Interest& interest)
Eric Newberry41aba102017-11-01 16:42:13 -0700500{
ashiqopuc7079482019-02-20 05:34:37 +0000501 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(egress, interest);
Eric Newberry41aba102017-11-01 16:42:13 -0700502}
503
Junxiao Shid3c792f2014-01-30 00:46:13 -0700504void
Teng Liang7003e0b2018-03-03 16:03:30 -0700505Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700506{
Teng Liang39465c22018-11-12 19:43:04 -0700507 BOOST_ASSERT(pitEntry);
Teng Liang7003e0b2018-03-03 16:03:30 -0700508 BOOST_ASSERT(duration >= 0_ms);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700509
Teng Liang7003e0b2018-03-03 16:03:30 -0700510 scheduler::cancel(pitEntry->expiryTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700511
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400512 pitEntry->expiryTimer = scheduler::schedule(duration, [=] { onInterestFinalize(pitEntry); });
Junxiao Shia110f262014-10-12 12:35:20 -0700513}
514
515void
Teng Liang6f09ab62018-03-01 20:04:08 -0700516Forwarder::insertDeadNonceList(pit::Entry& pitEntry, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700517{
518 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000519 bool needDnl = true;
Teng Liang6f09ab62018-03-01 20:04:08 -0700520 if (pitEntry.isSatisfied) {
521 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
Junxiao Shia110f262014-10-12 12:35:20 -0700522 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Teng Liang6f09ab62018-03-01 20:04:08 -0700523 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700524 }
525
526 if (!needDnl) {
527 return;
528 }
529
530 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000531 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700532 // insert all outgoing Nonces
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400533 const auto& outRecords = pitEntry.getOutRecords();
534 std::for_each(outRecords.begin(), outRecords.end(), [&] (const auto& outRecord) {
535 m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
536 });
Junxiao Shia110f262014-10-12 12:35:20 -0700537 }
538 else {
539 // insert outgoing Nonce of a specific face
ashiqopud3ae85d2019-02-17 02:29:55 +0000540 auto outRecord = pitEntry.getOutRecord(*upstream, 0);
Junxiao Shia110f262014-10-12 12:35:20 -0700541 if (outRecord != pitEntry.getOutRecords().end()) {
542 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
543 }
544 }
545}
546
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800547} // namespace nfd