blob: b94108fbc5d2df362058311efd86e40184d3f983 [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"
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 Shicbc8e942016-09-06 03:17:45 +000035#include <ndn-cxx/lp/tags.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080036
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -040037#include "face/null-face.hpp"
38
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080039namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080040
Davide Pesaventoa3148082018-04-12 18:21:54 -040041NFD_LOG_INIT(Forwarder);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070042
Junxiao Shic34d1672016-12-09 15:57:59 +000043static Name
44getDefaultStrategyName()
45{
Junxiao Shi037f4ab2016-12-13 04:27:06 +000046 return fw::BestRouteStrategy2::getStrategyName();
Junxiao Shic34d1672016-12-09 15:57:59 +000047}
48
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040049Forwarder::Forwarder(FaceTable& faceTable)
50 : m_faceTable(faceTable)
51 , m_unsolicitedDataPolicy(make_unique<fw::DefaultUnsolicitedDataPolicy>())
Junxiao Shifbe8efe2016-08-22 16:02:30 +000052 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060053 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080054 , m_measurements(m_nameTree)
Junxiao Shic34d1672016-12-09 15:57:59 +000055 , m_strategyChoice(*this)
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -040056 , m_csFace(face::makeNullFace(FaceUri("contentstore://")))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080057{
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -040058 getFaceTable().addReserved(m_csFace, face::FACEID_CONTENT_STORE);
59
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040060 m_faceTable.afterAdd.connect([this] (const Face& face) {
Junxiao Shidcffdaa2016-07-26 02:23:56 +000061 face.afterReceiveInterest.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000062 [this, &face] (const Interest& interest, const EndpointId& endpointId) {
63 this->startProcessInterest(FaceEndpoint(face, endpointId), interest);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000064 });
65 face.afterReceiveData.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000066 [this, &face] (const Data& data, const EndpointId& endpointId) {
67 this->startProcessData(FaceEndpoint(face, endpointId), data);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000068 });
69 face.afterReceiveNack.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000070 [this, &face] (const lp::Nack& nack, const EndpointId& endpointId) {
71 this->startProcessNack(FaceEndpoint(face, endpointId), nack);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000072 });
Eric Newberry41aba102017-11-01 16:42:13 -070073 face.onDroppedInterest.connect(
74 [this, &face] (const Interest& interest) {
ashiqopuc7079482019-02-20 05:34:37 +000075 this->onDroppedInterest(FaceEndpoint(face, 0), interest);
Eric Newberry41aba102017-11-01 16:42:13 -070076 });
Junxiao Shidcffdaa2016-07-26 02:23:56 +000077 });
78
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040079 m_faceTable.beforeRemove.connect([this] (const Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000080 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000081 });
Junxiao Shic34d1672016-12-09 15:57:59 +000082
Ju Pan2feb4592019-09-16 20:56:38 +000083 m_fib.afterNewNextHop.connect([&] (const Name& prefix, const fib::NextHop& nextHop) {
84 this->startProcessNewNextHop(prefix, nextHop);
85 });
86
Junxiao Shic34d1672016-12-09 15:57:59 +000087 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080088}
89
Junxiao Shidcffdaa2016-07-26 02:23:56 +000090Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060091
Junxiao Shi0355e9f2015-09-02 07:24:53 -070092void
ashiqopuc7079482019-02-20 05:34:37 +000093Forwarder::onIncomingInterest(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -070094{
95 // receive Interest
ashiqopuc7079482019-02-20 05:34:37 +000096 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress << " interest=" << interest.getName());
97 interest.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -070098 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -070099
Junxiao Shi88884492014-02-15 15:57:43 -0700100 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000101 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700102 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700103 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000104 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
105 << " interest=" << interest.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700106 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700107 return;
108 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700109
Junxiao Shi330136a2016-03-10 04:53:08 -0700110 // detect duplicate Nonce with Dead Nonce List
111 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
112 if (hasDuplicateNonceInDnl) {
113 // goto Interest loop pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000114 this->onInterestLoop(ingress, interest);
Junxiao Shi330136a2016-03-10 04:53:08 -0700115 return;
116 }
117
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000118 // strip forwarding hint if Interest has reached producer region
119 if (!interest.getForwardingHint().empty() &&
120 m_networkRegionTable.isInProducerRegion(interest.getForwardingHint())) {
ashiqopuc7079482019-02-20 05:34:37 +0000121 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
122 << " interest=" << interest.getName() << " reaching-producer-region");
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000123 const_cast<Interest&>(interest).setForwardingHint({});
Junxiao Shif9858fd2017-02-01 16:22:44 +0000124 }
125
Junxiao Shid3c792f2014-01-30 00:46:13 -0700126 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700127 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700128
Junxiao Shi330136a2016-03-10 04:53:08 -0700129 // detect duplicate Nonce in PIT entry
ashiqopuc7079482019-02-20 05:34:37 +0000130 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), ingress.face);
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000131 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
ashiqopuc7079482019-02-20 05:34:37 +0000132 if (ingress.face.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000133 // for p2p face: duplicate Nonce from same incoming face is not loop
134 hasDuplicateNonceInPit = hasDuplicateNonceInPit && !(dnw & fw::DUPLICATE_NONCE_IN_SAME);
135 }
Junxiao Shi330136a2016-03-10 04:53:08 -0700136 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700137 // goto Interest loop pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000138 this->onInterestLoop(ingress, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700139 return;
140 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700141
Junxiao Shif3c07812014-03-11 21:48:49 -0700142 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700143 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600144 m_cs.find(interest,
ashiqopuc7079482019-02-20 05:34:37 +0000145 bind(&Forwarder::onContentStoreHit, this, ingress, pitEntry, _1, _2),
146 bind(&Forwarder::onContentStoreMiss, this, ingress, pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700147 }
mzhang4eab72492015-02-25 11:16:09 -0600148 else {
ashiqopuc7079482019-02-20 05:34:37 +0000149 this->onContentStoreMiss(ingress, pitEntry, interest);
mzhang4eab72492015-02-25 11:16:09 -0600150 }
151}
Junxiao Shic041ca32014-02-25 20:01:15 -0700152
mzhang4eab72492015-02-25 11:16:09 -0600153void
ashiqopuc7079482019-02-20 05:34:37 +0000154Forwarder::onInterestLoop(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700155{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700156 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000157 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
158 NFD_LOG_DEBUG("onInterestLoop in=" << ingress
159 << " interest=" << interest.getName() << " drop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700160 return;
161 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700162
ashiqopuc7079482019-02-20 05:34:37 +0000163 NFD_LOG_DEBUG("onInterestLoop in=" << ingress << " interest=" << interest.getName()
164 << " send-Nack-duplicate");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700165
166 // send Nack with reason=DUPLICATE
167 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
168 lp::Nack nack(interest);
169 nack.setReason(lp::NackReason::DUPLICATE);
ashiqopu075bb7d2019-03-10 01:38:21 +0000170 ingress.face.sendNack(nack, ingress.endpoint);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700171}
172
173void
ashiqopuc7079482019-02-20 05:34:37 +0000174Forwarder::onContentStoreMiss(const FaceEndpoint& ingress,
175 const shared_ptr<pit::Entry>& pitEntry, const Interest& interest)
mzhang4eab72492015-02-25 11:16:09 -0600176{
177 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000178 ++m_counters.nCsMisses;
Alexander Afanasyev48ba4c12019-07-27 18:36:48 -0400179 afterCsMiss(interest);
mzhang4eab72492015-02-25 11:16:09 -0600180
Md Ashiqur Rahman115ea632019-07-06 02:34:31 +0000181 // insert in-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000182 pitEntry->insertOrUpdateInRecord(ingress.face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700183
Teng Liang7003e0b2018-03-03 16:03:30 -0700184 // set PIT expiry timer to the time that the last PIT in-record expires
Davide Pesaventob31206e2019-04-20 22:34:12 -0400185 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(),
186 [] (const auto& a, const auto& b) {
187 return a.getExpiry() < b.getExpiry();
188 });
Teng Liang7003e0b2018-03-03 16:03:30 -0700189 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
190 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700191
Junxiao Shie342e8d2016-09-18 16:48:00 +0000192 // has NextHopFaceId?
Davide Pesaventob31206e2019-04-20 22:34:12 -0400193 auto nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
Junxiao Shie342e8d2016-09-18 16:48:00 +0000194 if (nextHopTag != nullptr) {
195 // chosen NextHop face exists?
196 Face* nextHopFace = m_faceTable.get(*nextHopTag);
197 if (nextHopFace != nullptr) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400198 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName()
199 << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000200 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000201 // scope control is unnecessary, because privileged app explicitly wants to forward
ashiqopuc7079482019-02-20 05:34:37 +0000202 this->onOutgoingInterest(pitEntry, FaceEndpoint(*nextHopFace, 0), interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000203 }
204 return;
205 }
206
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000207 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000208 this->dispatchToStrategy(*pitEntry,
Md Ashiqur Rahman115ea632019-07-06 02:34:31 +0000209 [&] (fw::Strategy& strategy) {
210 strategy.afterReceiveInterest(FaceEndpoint(ingress.face, 0), interest, pitEntry);
211 });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700212}
213
214void
ashiqopuc7079482019-02-20 05:34:37 +0000215Forwarder::onContentStoreHit(const FaceEndpoint& ingress, const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000216 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600217{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500218 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000219 ++m_counters.nCsHits;
Alexander Afanasyev48ba4c12019-07-27 18:36:48 -0400220 afterCsHit(interest, data);
mzhang4eab72492015-02-25 11:16:09 -0600221
Junxiao Shicde37ad2015-12-24 01:02:05 -0700222 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
Davide Pesaventob31206e2019-04-20 22:34:12 -0400223 // FIXME Should we lookup PIT for other Interests that also match the data?
mzhang4eab72492015-02-25 11:16:09 -0600224
Teng Liang6f09ab62018-03-01 20:04:08 -0700225 pitEntry->isSatisfied = true;
226 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
227
Teng Liang85a36632018-03-21 05:59:34 -0700228 // set PIT expiry timer to now
229 this->setExpiryTimer(pitEntry, 0_ms);
mzhang4eab72492015-02-25 11:16:09 -0600230
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -0400231 beforeSatisfyInterest(*pitEntry, *m_csFace, data);
232 this->dispatchToStrategy(*pitEntry,
233 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, FaceEndpoint(*m_csFace, 0), data); });
234
Teng Liang85a36632018-03-21 05:59:34 -0700235 // dispatch to strategy: after Content Store hit
236 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000237 [&] (fw::Strategy& strategy) { strategy.afterContentStoreHit(pitEntry, ingress, data); });
mzhang4eab72492015-02-25 11:16:09 -0600238}
239
Junxiao Shid3c792f2014-01-30 00:46:13 -0700240void
ashiqopuc7079482019-02-20 05:34:37 +0000241Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry,
242 const FaceEndpoint& egress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700243{
ashiqopuc7079482019-02-20 05:34:37 +0000244 NFD_LOG_DEBUG("onOutgoingInterest out=" << egress << " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700245
Junxiao Shi4846f372016-04-05 13:39:30 -0700246 // insert out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000247 pitEntry->insertOrUpdateOutRecord(egress.face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700248
Junxiao Shid3c792f2014-01-30 00:46:13 -0700249 // send Interest
ashiqopu075bb7d2019-03-10 01:38:21 +0000250 egress.face.sendInterest(interest, egress.endpoint);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700251 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700252}
253
254void
Teng Liang6f09ab62018-03-01 20:04:08 -0700255Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shia110f262014-10-12 12:35:20 -0700256{
ashiqopuc7079482019-02-20 05:34:37 +0000257 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName()
258 << (pitEntry->isSatisfied ? " satisfied" : " unsatisfied"));
Junxiao Shia110f262014-10-12 12:35:20 -0700259
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -0400260 if (!pitEntry->isSatisfied) {
261 beforeExpirePendingInterest(*pitEntry);
262 }
263
Junxiao Shia110f262014-10-12 12:35:20 -0700264 // Dead Nonce List insert if necessary
Davide Pesavento3dade002019-03-19 11:29:56 -0600265 this->insertDeadNonceList(*pitEntry, nullptr);
Junxiao Shia110f262014-10-12 12:35:20 -0700266
Ju Pan6eb1ac92018-10-26 16:26:15 +0000267 // Increment satisfied/unsatisfied Interests counter
268 if (pitEntry->isSatisfied) {
269 ++m_counters.nSatisfiedInterests;
270 }
271 else {
272 ++m_counters.nUnsatisfiedInterests;
273 }
274
Junxiao Shif3c07812014-03-11 21:48:49 -0700275 // PIT delete
Davide Pesavento3dade002019-03-19 11:29:56 -0600276 pitEntry->expiryTimer.cancel();
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000277 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700278}
279
280void
ashiqopuc7079482019-02-20 05:34:37 +0000281Forwarder::onIncomingData(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700282{
283 // receive Data
ashiqopuc7079482019-02-20 05:34:37 +0000284 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName());
285 data.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700286 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700287
Junxiao Shi88884492014-02-15 15:57:43 -0700288 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000289 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700290 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700291 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000292 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700293 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700294 return;
295 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700296
Junxiao Shid3c792f2014-01-30 00:46:13 -0700297 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700298 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
Teng Liang43bb2312018-03-26 04:16:42 -0700299 if (pitMatches.size() == 0) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700300 // goto Data unsolicited pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000301 this->onDataUnsolicited(ingress, data);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700302 return;
303 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700304
Junxiao Shid3c792f2014-01-30 00:46:13 -0700305 // CS insert
306 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700307
Teng Liang43bb2312018-03-26 04:16:42 -0700308 // when only one PIT entry is matched, trigger strategy: after receive Data
309 if (pitMatches.size() == 1) {
310 auto& pitEntry = pitMatches.front();
Junxiao Shic041ca32014-02-25 20:01:15 -0700311
Teng Liang43bb2312018-03-26 04:16:42 -0700312 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700313
Teng Liang7003e0b2018-03-03 16:03:30 -0700314 // set PIT expiry timer to now
315 this->setExpiryTimer(pitEntry, 0_ms);
316
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -0400317 beforeSatisfyInterest(*pitEntry, ingress.face, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700318 // trigger strategy: after receive Data
Junxiao Shib9420cf2016-08-13 04:38:52 +0000319 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000320 [&] (fw::Strategy& strategy) { strategy.afterReceiveData(pitEntry, ingress, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700321
Teng Liang43bb2312018-03-26 04:16:42 -0700322 // mark PIT satisfied
Teng Liang6f09ab62018-03-01 20:04:08 -0700323 pitEntry->isSatisfied = true;
324 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
325
Junxiao Shi4846f372016-04-05 13:39:30 -0700326 // Dead Nonce List insert if necessary (for out-record of inFace)
ashiqopuc7079482019-02-20 05:34:37 +0000327 this->insertDeadNonceList(*pitEntry, &ingress.face);
Junxiao Shia110f262014-10-12 12:35:20 -0700328
Teng Liang43bb2312018-03-26 04:16:42 -0700329 // delete PIT entry's out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000330 pitEntry->deleteOutRecord(ingress.face);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700331 }
Teng Liang43bb2312018-03-26 04:16:42 -0700332 // when more than one PIT entry is matched, trigger strategy: before satisfy Interest,
333 // and send Data to all matched out faces
334 else {
ashiqopuc7079482019-02-20 05:34:37 +0000335 std::set<std::pair<Face*, EndpointId>> pendingDownstreams;
Teng Liang43bb2312018-03-26 04:16:42 -0700336 auto now = time::steady_clock::now();
Junxiao Shic041ca32014-02-25 20:01:15 -0700337
Davide Pesaventob31206e2019-04-20 22:34:12 -0400338 for (const auto& pitEntry : pitMatches) {
Teng Liang43bb2312018-03-26 04:16:42 -0700339 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
340
341 // remember pending downstreams
342 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
343 if (inRecord.getExpiry() > now) {
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000344 pendingDownstreams.emplace(&inRecord.getFace(), 0);
Teng Liang43bb2312018-03-26 04:16:42 -0700345 }
346 }
347
348 // set PIT expiry timer to now
349 this->setExpiryTimer(pitEntry, 0_ms);
350
351 // invoke PIT satisfy callback
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -0400352 beforeSatisfyInterest(*pitEntry, ingress.face, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700353 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000354 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, ingress, data); });
Teng Liang43bb2312018-03-26 04:16:42 -0700355
356 // mark PIT satisfied
357 pitEntry->isSatisfied = true;
358 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
359
360 // Dead Nonce List insert if necessary (for out-record of inFace)
ashiqopuc7079482019-02-20 05:34:37 +0000361 this->insertDeadNonceList(*pitEntry, &ingress.face);
Teng Liang43bb2312018-03-26 04:16:42 -0700362
363 // clear PIT entry's in and out records
364 pitEntry->clearInRecords();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000365 pitEntry->deleteOutRecord(ingress.face);
Junxiao Shida006f52014-05-16 11:18:00 -0700366 }
Teng Liang43bb2312018-03-26 04:16:42 -0700367
368 // foreach pending downstream
ashiqopuc7079482019-02-20 05:34:37 +0000369 for (const auto& pendingDownstream : pendingDownstreams) {
370 if (pendingDownstream.first->getId() == ingress.face.getId() &&
371 pendingDownstream.second == ingress.endpoint &&
372 pendingDownstream.first->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Teng Liang43bb2312018-03-26 04:16:42 -0700373 continue;
374 }
375 // goto outgoing Data pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000376 this->onOutgoingData(data, FaceEndpoint(*pendingDownstream.first, pendingDownstream.second));
Teng Liang43bb2312018-03-26 04:16:42 -0700377 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700378 }
379}
380
381void
ashiqopuc7079482019-02-20 05:34:37 +0000382Forwarder::onDataUnsolicited(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700383{
384 // accept to cache?
ashiqopuc7079482019-02-20 05:34:37 +0000385 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(ingress.face, data);
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000386 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700387 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700388 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700389 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700390
ashiqopuc7079482019-02-20 05:34:37 +0000391 NFD_LOG_DEBUG("onDataUnsolicited in=" << ingress << " data=" << data.getName() << " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700392}
393
394void
ashiqopu075bb7d2019-03-10 01:38:21 +0000395Forwarder::onOutgoingData(const Data& data, const FaceEndpoint& egress)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700396{
ashiqopuc7079482019-02-20 05:34:37 +0000397 if (egress.face.getId() == face::INVALID_FACEID) {
398 NFD_LOG_WARN("onOutgoingData out=(invalid) data=" << data.getName());
Junxiao Shi223271b2014-07-03 22:06:13 -0700399 return;
400 }
ashiqopuc7079482019-02-20 05:34:37 +0000401 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700402
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700403 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000404 bool isViolatingLocalhost = egress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700405 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700406 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000407 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700408 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700409 return;
410 }
411
Junxiao Shif3c07812014-03-11 21:48:49 -0700412 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700413
Junxiao Shid3c792f2014-01-30 00:46:13 -0700414 // send Data
ashiqopu075bb7d2019-03-10 01:38:21 +0000415 egress.face.sendData(data, egress.endpoint);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700416 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700417}
418
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700419void
ashiqopuc7079482019-02-20 05:34:37 +0000420Forwarder::onIncomingNack(const FaceEndpoint& ingress, const lp::Nack& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700421{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000422 // receive Nack
ashiqopuc7079482019-02-20 05:34:37 +0000423 nack.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700424 ++m_counters.nInNacks;
425
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700426 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000427 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400428 NFD_LOG_DEBUG("onIncomingNack in=" << ingress
429 << " nack=" << nack.getInterest().getName() << "~" << nack.getReason()
430 << " link-type=" << ingress.face.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700431 return;
432 }
433
434 // PIT match
435 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
436 // if no PIT entry found, drop
437 if (pitEntry == nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000438 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
439 << "~" << nack.getReason() << " no-PIT-entry");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700440 return;
441 }
442
443 // has out-record?
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000444 auto outRecord = pitEntry->getOutRecord(ingress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700445 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700446 if (outRecord == pitEntry->out_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000447 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
448 << "~" << nack.getReason() << " no-out-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700449 return;
450 }
451
452 // if out-record has different Nonce, drop
453 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
ashiqopuc7079482019-02-20 05:34:37 +0000454 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
455 << "~" << nack.getReason() << " wrong-Nonce " << nack.getInterest().getNonce()
456 << "!=" << outRecord->getLastNonce());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700457 return;
458 }
459
ashiqopuc7079482019-02-20 05:34:37 +0000460 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
461 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700462
463 // record Nack on out-record
464 outRecord->setIncomingNack(nack);
465
Teng Liang7003e0b2018-03-03 16:03:30 -0700466 // set PIT expiry timer to now when all out-record receive Nack
467 if (!fw::hasPendingOutRecords(*pitEntry)) {
468 this->setExpiryTimer(pitEntry, 0_ms);
469 }
470
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700471 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000472 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000473 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(ingress, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700474}
475
476void
ashiqopuc7079482019-02-20 05:34:37 +0000477Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry,
478 const FaceEndpoint& egress, const lp::NackHeader& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700479{
ashiqopuc7079482019-02-20 05:34:37 +0000480 if (egress.face.getId() == face::INVALID_FACEID) {
481 NFD_LOG_WARN("onOutgoingNack out=(invalid)"
Davide Pesaventob31206e2019-04-20 22:34:12 -0400482 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700483 return;
484 }
485
486 // has in-record?
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000487 auto inRecord = pitEntry->getInRecord(egress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700488
489 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700490 if (inRecord == pitEntry->in_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000491 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
492 << " nack=" << pitEntry->getInterest().getName()
493 << "~" << nack.getReason() << " no-in-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700494 return;
495 }
496
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700497 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000498 if (egress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
499 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
Davide Pesaventob31206e2019-04-20 22:34:12 -0400500 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason()
501 << " link-type=" << egress.face.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700502 return;
503 }
504
ashiqopuc7079482019-02-20 05:34:37 +0000505 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
506 << " nack=" << pitEntry->getInterest().getName()
507 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700508
509 // create Nack packet with the Interest from in-record
510 lp::Nack nackPkt(inRecord->getInterest());
511 nackPkt.setHeader(nack);
512
513 // erase in-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000514 pitEntry->deleteInRecord(egress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700515
516 // send Nack on face
ashiqopu075bb7d2019-03-10 01:38:21 +0000517 egress.face.sendNack(nackPkt, egress.endpoint);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700518 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700519}
520
Eric Newberry41aba102017-11-01 16:42:13 -0700521void
ashiqopuc7079482019-02-20 05:34:37 +0000522Forwarder::onDroppedInterest(const FaceEndpoint& egress, const Interest& interest)
Eric Newberry41aba102017-11-01 16:42:13 -0700523{
ashiqopuc7079482019-02-20 05:34:37 +0000524 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(egress, interest);
Eric Newberry41aba102017-11-01 16:42:13 -0700525}
526
Junxiao Shid3c792f2014-01-30 00:46:13 -0700527void
Ju Pan2feb4592019-09-16 20:56:38 +0000528Forwarder::onNewNextHop(const Name& prefix, const fib::NextHop& nextHop)
529{
530 const auto affectedEntries = this->getNameTree().partialEnumerate(prefix,
531 [&] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
532 const fib::Entry* fibEntry = nte.getFibEntry();
533 const fw::Strategy* strategy = nullptr;
534 if (nte.getStrategyChoiceEntry() != nullptr) {
535 strategy = &nte.getStrategyChoiceEntry()->getStrategy();
536 }
537 // current nte has buffered Interests but no fibEntry (except for the root nte) and the strategy
538 // enables new nexthop behavior, we enumerate the current nte and keep visiting its children.
539 if (nte.getName().size() == 0 ||
540 (strategy != nullptr && strategy->wantNewNextHopTrigger() &&
541 fibEntry == nullptr && nte.hasPitEntries())) {
542 return {true, true};
543 }
544 // we don't need the current nte (no pitEntry or strategy doesn't support new nexthop), but
545 // if the current nte has no fibEntry, it's still possible that its children are affected by
546 // the new nexthop.
547 else if (fibEntry == nullptr) {
548 return {false, true};
549 }
550 // if the current nte has a fibEntry, we ignore the current nte and don't visit its
551 // children because they are already covered by the current nte's fibEntry.
552 else {
553 return {false, false};
554 }
555 });
556
557 for (const auto& nte : affectedEntries) {
558 for (const auto& pitEntry : nte.getPitEntries()) {
559 this->dispatchToStrategy(*pitEntry,
560 [&] (fw::Strategy& strategy) {
561 strategy.afterNewNextHop(nextHop, pitEntry);
562 });
563 }
564 }
565}
566
567void
Teng Liang7003e0b2018-03-03 16:03:30 -0700568Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700569{
Teng Liang39465c22018-11-12 19:43:04 -0700570 BOOST_ASSERT(pitEntry);
Teng Liang7003e0b2018-03-03 16:03:30 -0700571 BOOST_ASSERT(duration >= 0_ms);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700572
Davide Pesavento3dade002019-03-19 11:29:56 -0600573 pitEntry->expiryTimer.cancel();
574 pitEntry->expiryTimer = getScheduler().schedule(duration, [=] { onInterestFinalize(pitEntry); });
Junxiao Shia110f262014-10-12 12:35:20 -0700575}
576
577void
Teng Liang6f09ab62018-03-01 20:04:08 -0700578Forwarder::insertDeadNonceList(pit::Entry& pitEntry, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700579{
580 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000581 bool needDnl = true;
Teng Liang6f09ab62018-03-01 20:04:08 -0700582 if (pitEntry.isSatisfied) {
583 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
Junxiao Shia110f262014-10-12 12:35:20 -0700584 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Teng Liang6f09ab62018-03-01 20:04:08 -0700585 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700586 }
587
588 if (!needDnl) {
589 return;
590 }
591
592 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000593 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700594 // insert all outgoing Nonces
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400595 const auto& outRecords = pitEntry.getOutRecords();
596 std::for_each(outRecords.begin(), outRecords.end(), [&] (const auto& outRecord) {
597 m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
598 });
Junxiao Shia110f262014-10-12 12:35:20 -0700599 }
600 else {
601 // insert outgoing Nonce of a specific face
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000602 auto outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700603 if (outRecord != pitEntry.getOutRecords().end()) {
604 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
605 }
606 }
607}
608
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800609} // namespace nfd