blob: 1fc674ddc6a73fa73a3aa7f08b2b75d614941184 [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;
mzhang4eab72492015-02-25 11:16:09 -0600179
Md Ashiqur Rahman115ea632019-07-06 02:34:31 +0000180 // insert in-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000181 pitEntry->insertOrUpdateInRecord(ingress.face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700182
Teng Liang7003e0b2018-03-03 16:03:30 -0700183 // set PIT expiry timer to the time that the last PIT in-record expires
Davide Pesaventob31206e2019-04-20 22:34:12 -0400184 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(),
185 [] (const auto& a, const auto& b) {
186 return a.getExpiry() < b.getExpiry();
187 });
Teng Liang7003e0b2018-03-03 16:03:30 -0700188 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
189 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700190
Junxiao Shie342e8d2016-09-18 16:48:00 +0000191 // has NextHopFaceId?
Davide Pesaventob31206e2019-04-20 22:34:12 -0400192 auto nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
Junxiao Shie342e8d2016-09-18 16:48:00 +0000193 if (nextHopTag != nullptr) {
194 // chosen NextHop face exists?
195 Face* nextHopFace = m_faceTable.get(*nextHopTag);
196 if (nextHopFace != nullptr) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400197 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName()
198 << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000199 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000200 // scope control is unnecessary, because privileged app explicitly wants to forward
ashiqopuc7079482019-02-20 05:34:37 +0000201 this->onOutgoingInterest(pitEntry, FaceEndpoint(*nextHopFace, 0), interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000202 }
203 return;
204 }
205
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000206 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000207 this->dispatchToStrategy(*pitEntry,
Md Ashiqur Rahman115ea632019-07-06 02:34:31 +0000208 [&] (fw::Strategy& strategy) {
209 strategy.afterReceiveInterest(FaceEndpoint(ingress.face, 0), interest, pitEntry);
210 });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700211}
212
213void
ashiqopuc7079482019-02-20 05:34:37 +0000214Forwarder::onContentStoreHit(const FaceEndpoint& ingress, const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000215 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600216{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500217 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000218 ++m_counters.nCsHits;
mzhang4eab72492015-02-25 11:16:09 -0600219
Junxiao Shicde37ad2015-12-24 01:02:05 -0700220 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
Davide Pesaventob31206e2019-04-20 22:34:12 -0400221 // FIXME Should we lookup PIT for other Interests that also match the data?
mzhang4eab72492015-02-25 11:16:09 -0600222
Teng Liang6f09ab62018-03-01 20:04:08 -0700223 pitEntry->isSatisfied = true;
224 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
225
Teng Liang85a36632018-03-21 05:59:34 -0700226 // set PIT expiry timer to now
227 this->setExpiryTimer(pitEntry, 0_ms);
mzhang4eab72492015-02-25 11:16:09 -0600228
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -0400229 beforeSatisfyInterest(*pitEntry, *m_csFace, data);
230 this->dispatchToStrategy(*pitEntry,
231 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, FaceEndpoint(*m_csFace, 0), data); });
232
Teng Liang85a36632018-03-21 05:59:34 -0700233 // dispatch to strategy: after Content Store hit
234 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000235 [&] (fw::Strategy& strategy) { strategy.afterContentStoreHit(pitEntry, ingress, data); });
mzhang4eab72492015-02-25 11:16:09 -0600236}
237
Junxiao Shid3c792f2014-01-30 00:46:13 -0700238void
ashiqopuc7079482019-02-20 05:34:37 +0000239Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry,
240 const FaceEndpoint& egress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700241{
ashiqopuc7079482019-02-20 05:34:37 +0000242 NFD_LOG_DEBUG("onOutgoingInterest out=" << egress << " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700243
Junxiao Shi4846f372016-04-05 13:39:30 -0700244 // insert out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000245 pitEntry->insertOrUpdateOutRecord(egress.face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700246
Junxiao Shid3c792f2014-01-30 00:46:13 -0700247 // send Interest
ashiqopu075bb7d2019-03-10 01:38:21 +0000248 egress.face.sendInterest(interest, egress.endpoint);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700249 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700250}
251
252void
Teng Liang6f09ab62018-03-01 20:04:08 -0700253Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shia110f262014-10-12 12:35:20 -0700254{
ashiqopuc7079482019-02-20 05:34:37 +0000255 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName()
256 << (pitEntry->isSatisfied ? " satisfied" : " unsatisfied"));
Junxiao Shia110f262014-10-12 12:35:20 -0700257
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -0400258 if (!pitEntry->isSatisfied) {
259 beforeExpirePendingInterest(*pitEntry);
260 }
261
Junxiao Shia110f262014-10-12 12:35:20 -0700262 // Dead Nonce List insert if necessary
Davide Pesavento3dade002019-03-19 11:29:56 -0600263 this->insertDeadNonceList(*pitEntry, nullptr);
Junxiao Shia110f262014-10-12 12:35:20 -0700264
Ju Pan6eb1ac92018-10-26 16:26:15 +0000265 // Increment satisfied/unsatisfied Interests counter
266 if (pitEntry->isSatisfied) {
267 ++m_counters.nSatisfiedInterests;
268 }
269 else {
270 ++m_counters.nUnsatisfiedInterests;
271 }
272
Junxiao Shif3c07812014-03-11 21:48:49 -0700273 // PIT delete
Davide Pesavento3dade002019-03-19 11:29:56 -0600274 pitEntry->expiryTimer.cancel();
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000275 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700276}
277
278void
ashiqopuc7079482019-02-20 05:34:37 +0000279Forwarder::onIncomingData(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700280{
281 // receive Data
ashiqopuc7079482019-02-20 05:34:37 +0000282 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName());
283 data.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700284 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700285
Junxiao Shi88884492014-02-15 15:57:43 -0700286 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000287 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700288 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700289 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000290 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700291 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700292 return;
293 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700294
Junxiao Shid3c792f2014-01-30 00:46:13 -0700295 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700296 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
Teng Liang43bb2312018-03-26 04:16:42 -0700297 if (pitMatches.size() == 0) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700298 // goto Data unsolicited pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000299 this->onDataUnsolicited(ingress, data);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700300 return;
301 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700302
Junxiao Shid3c792f2014-01-30 00:46:13 -0700303 // CS insert
304 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700305
Teng Liang43bb2312018-03-26 04:16:42 -0700306 // when only one PIT entry is matched, trigger strategy: after receive Data
307 if (pitMatches.size() == 1) {
308 auto& pitEntry = pitMatches.front();
Junxiao Shic041ca32014-02-25 20:01:15 -0700309
Teng Liang43bb2312018-03-26 04:16:42 -0700310 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700311
Teng Liang7003e0b2018-03-03 16:03:30 -0700312 // set PIT expiry timer to now
313 this->setExpiryTimer(pitEntry, 0_ms);
314
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -0400315 beforeSatisfyInterest(*pitEntry, ingress.face, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700316 // trigger strategy: after receive Data
Junxiao Shib9420cf2016-08-13 04:38:52 +0000317 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000318 [&] (fw::Strategy& strategy) { strategy.afterReceiveData(pitEntry, ingress, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700319
Teng Liang43bb2312018-03-26 04:16:42 -0700320 // mark PIT satisfied
Teng Liang6f09ab62018-03-01 20:04:08 -0700321 pitEntry->isSatisfied = true;
322 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
323
Junxiao Shi4846f372016-04-05 13:39:30 -0700324 // Dead Nonce List insert if necessary (for out-record of inFace)
ashiqopuc7079482019-02-20 05:34:37 +0000325 this->insertDeadNonceList(*pitEntry, &ingress.face);
Junxiao Shia110f262014-10-12 12:35:20 -0700326
Teng Liang43bb2312018-03-26 04:16:42 -0700327 // delete PIT entry's out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000328 pitEntry->deleteOutRecord(ingress.face);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700329 }
Teng Liang43bb2312018-03-26 04:16:42 -0700330 // when more than one PIT entry is matched, trigger strategy: before satisfy Interest,
331 // and send Data to all matched out faces
332 else {
ashiqopuc7079482019-02-20 05:34:37 +0000333 std::set<std::pair<Face*, EndpointId>> pendingDownstreams;
Teng Liang43bb2312018-03-26 04:16:42 -0700334 auto now = time::steady_clock::now();
Junxiao Shic041ca32014-02-25 20:01:15 -0700335
Davide Pesaventob31206e2019-04-20 22:34:12 -0400336 for (const auto& pitEntry : pitMatches) {
Teng Liang43bb2312018-03-26 04:16:42 -0700337 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
338
339 // remember pending downstreams
340 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
341 if (inRecord.getExpiry() > now) {
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000342 pendingDownstreams.emplace(&inRecord.getFace(), 0);
Teng Liang43bb2312018-03-26 04:16:42 -0700343 }
344 }
345
346 // set PIT expiry timer to now
347 this->setExpiryTimer(pitEntry, 0_ms);
348
349 // invoke PIT satisfy callback
Alexander Afanasyev54c4c5d2018-08-06 17:28:36 -0400350 beforeSatisfyInterest(*pitEntry, ingress.face, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700351 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000352 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, ingress, data); });
Teng Liang43bb2312018-03-26 04:16:42 -0700353
354 // mark PIT satisfied
355 pitEntry->isSatisfied = true;
356 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
357
358 // Dead Nonce List insert if necessary (for out-record of inFace)
ashiqopuc7079482019-02-20 05:34:37 +0000359 this->insertDeadNonceList(*pitEntry, &ingress.face);
Teng Liang43bb2312018-03-26 04:16:42 -0700360
361 // clear PIT entry's in and out records
362 pitEntry->clearInRecords();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000363 pitEntry->deleteOutRecord(ingress.face);
Junxiao Shida006f52014-05-16 11:18:00 -0700364 }
Teng Liang43bb2312018-03-26 04:16:42 -0700365
366 // foreach pending downstream
ashiqopuc7079482019-02-20 05:34:37 +0000367 for (const auto& pendingDownstream : pendingDownstreams) {
368 if (pendingDownstream.first->getId() == ingress.face.getId() &&
369 pendingDownstream.second == ingress.endpoint &&
370 pendingDownstream.first->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Teng Liang43bb2312018-03-26 04:16:42 -0700371 continue;
372 }
373 // goto outgoing Data pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000374 this->onOutgoingData(data, FaceEndpoint(*pendingDownstream.first, pendingDownstream.second));
Teng Liang43bb2312018-03-26 04:16:42 -0700375 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700376 }
377}
378
379void
ashiqopuc7079482019-02-20 05:34:37 +0000380Forwarder::onDataUnsolicited(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700381{
382 // accept to cache?
ashiqopuc7079482019-02-20 05:34:37 +0000383 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(ingress.face, data);
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000384 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700385 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700386 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700387 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700388
ashiqopuc7079482019-02-20 05:34:37 +0000389 NFD_LOG_DEBUG("onDataUnsolicited in=" << ingress << " data=" << data.getName() << " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700390}
391
392void
ashiqopu075bb7d2019-03-10 01:38:21 +0000393Forwarder::onOutgoingData(const Data& data, const FaceEndpoint& egress)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700394{
ashiqopuc7079482019-02-20 05:34:37 +0000395 if (egress.face.getId() == face::INVALID_FACEID) {
396 NFD_LOG_WARN("onOutgoingData out=(invalid) data=" << data.getName());
Junxiao Shi223271b2014-07-03 22:06:13 -0700397 return;
398 }
ashiqopuc7079482019-02-20 05:34:37 +0000399 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700400
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700401 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000402 bool isViolatingLocalhost = egress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700403 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700404 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000405 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700406 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700407 return;
408 }
409
Junxiao Shif3c07812014-03-11 21:48:49 -0700410 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700411
Junxiao Shid3c792f2014-01-30 00:46:13 -0700412 // send Data
ashiqopu075bb7d2019-03-10 01:38:21 +0000413 egress.face.sendData(data, egress.endpoint);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700414 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700415}
416
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700417void
ashiqopuc7079482019-02-20 05:34:37 +0000418Forwarder::onIncomingNack(const FaceEndpoint& ingress, const lp::Nack& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700419{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000420 // receive Nack
ashiqopuc7079482019-02-20 05:34:37 +0000421 nack.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700422 ++m_counters.nInNacks;
423
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700424 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000425 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400426 NFD_LOG_DEBUG("onIncomingNack in=" << ingress
427 << " nack=" << nack.getInterest().getName() << "~" << nack.getReason()
428 << " link-type=" << ingress.face.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700429 return;
430 }
431
432 // PIT match
433 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
434 // if no PIT entry found, drop
435 if (pitEntry == nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000436 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
437 << "~" << nack.getReason() << " no-PIT-entry");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700438 return;
439 }
440
441 // has out-record?
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000442 auto outRecord = pitEntry->getOutRecord(ingress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700443 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700444 if (outRecord == pitEntry->out_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000445 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
446 << "~" << nack.getReason() << " no-out-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700447 return;
448 }
449
450 // if out-record has different Nonce, drop
451 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
ashiqopuc7079482019-02-20 05:34:37 +0000452 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
453 << "~" << nack.getReason() << " wrong-Nonce " << nack.getInterest().getNonce()
454 << "!=" << outRecord->getLastNonce());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700455 return;
456 }
457
ashiqopuc7079482019-02-20 05:34:37 +0000458 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
459 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700460
461 // record Nack on out-record
462 outRecord->setIncomingNack(nack);
463
Teng Liang7003e0b2018-03-03 16:03:30 -0700464 // set PIT expiry timer to now when all out-record receive Nack
465 if (!fw::hasPendingOutRecords(*pitEntry)) {
466 this->setExpiryTimer(pitEntry, 0_ms);
467 }
468
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700469 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000470 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000471 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(ingress, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700472}
473
474void
ashiqopuc7079482019-02-20 05:34:37 +0000475Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry,
476 const FaceEndpoint& egress, const lp::NackHeader& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700477{
ashiqopuc7079482019-02-20 05:34:37 +0000478 if (egress.face.getId() == face::INVALID_FACEID) {
479 NFD_LOG_WARN("onOutgoingNack out=(invalid)"
Davide Pesaventob31206e2019-04-20 22:34:12 -0400480 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700481 return;
482 }
483
484 // has in-record?
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000485 auto inRecord = pitEntry->getInRecord(egress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700486
487 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700488 if (inRecord == pitEntry->in_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000489 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
490 << " nack=" << pitEntry->getInterest().getName()
491 << "~" << nack.getReason() << " no-in-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700492 return;
493 }
494
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700495 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000496 if (egress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
497 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
Davide Pesaventob31206e2019-04-20 22:34:12 -0400498 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason()
499 << " link-type=" << egress.face.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700500 return;
501 }
502
ashiqopuc7079482019-02-20 05:34:37 +0000503 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
504 << " nack=" << pitEntry->getInterest().getName()
505 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700506
507 // create Nack packet with the Interest from in-record
508 lp::Nack nackPkt(inRecord->getInterest());
509 nackPkt.setHeader(nack);
510
511 // erase in-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000512 pitEntry->deleteInRecord(egress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700513
514 // send Nack on face
ashiqopu075bb7d2019-03-10 01:38:21 +0000515 egress.face.sendNack(nackPkt, egress.endpoint);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700516 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700517}
518
Eric Newberry41aba102017-11-01 16:42:13 -0700519void
ashiqopuc7079482019-02-20 05:34:37 +0000520Forwarder::onDroppedInterest(const FaceEndpoint& egress, const Interest& interest)
Eric Newberry41aba102017-11-01 16:42:13 -0700521{
ashiqopuc7079482019-02-20 05:34:37 +0000522 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(egress, interest);
Eric Newberry41aba102017-11-01 16:42:13 -0700523}
524
Junxiao Shid3c792f2014-01-30 00:46:13 -0700525void
Ju Pan2feb4592019-09-16 20:56:38 +0000526Forwarder::onNewNextHop(const Name& prefix, const fib::NextHop& nextHop)
527{
528 const auto affectedEntries = this->getNameTree().partialEnumerate(prefix,
529 [&] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
530 const fib::Entry* fibEntry = nte.getFibEntry();
531 const fw::Strategy* strategy = nullptr;
532 if (nte.getStrategyChoiceEntry() != nullptr) {
533 strategy = &nte.getStrategyChoiceEntry()->getStrategy();
534 }
535 // current nte has buffered Interests but no fibEntry (except for the root nte) and the strategy
536 // enables new nexthop behavior, we enumerate the current nte and keep visiting its children.
537 if (nte.getName().size() == 0 ||
538 (strategy != nullptr && strategy->wantNewNextHopTrigger() &&
539 fibEntry == nullptr && nte.hasPitEntries())) {
540 return {true, true};
541 }
542 // we don't need the current nte (no pitEntry or strategy doesn't support new nexthop), but
543 // if the current nte has no fibEntry, it's still possible that its children are affected by
544 // the new nexthop.
545 else if (fibEntry == nullptr) {
546 return {false, true};
547 }
548 // if the current nte has a fibEntry, we ignore the current nte and don't visit its
549 // children because they are already covered by the current nte's fibEntry.
550 else {
551 return {false, false};
552 }
553 });
554
555 for (const auto& nte : affectedEntries) {
556 for (const auto& pitEntry : nte.getPitEntries()) {
557 this->dispatchToStrategy(*pitEntry,
558 [&] (fw::Strategy& strategy) {
559 strategy.afterNewNextHop(nextHop, pitEntry);
560 });
561 }
562 }
563}
564
565void
Teng Liang7003e0b2018-03-03 16:03:30 -0700566Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700567{
Teng Liang39465c22018-11-12 19:43:04 -0700568 BOOST_ASSERT(pitEntry);
Teng Liang7003e0b2018-03-03 16:03:30 -0700569 BOOST_ASSERT(duration >= 0_ms);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700570
Davide Pesavento3dade002019-03-19 11:29:56 -0600571 pitEntry->expiryTimer.cancel();
572 pitEntry->expiryTimer = getScheduler().schedule(duration, [=] { onInterestFinalize(pitEntry); });
Junxiao Shia110f262014-10-12 12:35:20 -0700573}
574
575void
Teng Liang6f09ab62018-03-01 20:04:08 -0700576Forwarder::insertDeadNonceList(pit::Entry& pitEntry, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700577{
578 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000579 bool needDnl = true;
Teng Liang6f09ab62018-03-01 20:04:08 -0700580 if (pitEntry.isSatisfied) {
581 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
Junxiao Shia110f262014-10-12 12:35:20 -0700582 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Teng Liang6f09ab62018-03-01 20:04:08 -0700583 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700584 }
585
586 if (!needDnl) {
587 return;
588 }
589
590 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000591 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700592 // insert all outgoing Nonces
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400593 const auto& outRecords = pitEntry.getOutRecords();
594 std::for_each(outRecords.begin(), outRecords.end(), [&] (const auto& outRecord) {
595 m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
596 });
Junxiao Shia110f262014-10-12 12:35:20 -0700597 }
598 else {
599 // insert outgoing Nonce of a specific face
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000600 auto outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700601 if (outRecord != pitEntry.getOutRecords().end()) {
602 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
603 }
604 }
605}
606
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800607} // namespace nfd