blob: f59963e0e737f9c8f7b3c9bd5117d49345b2b45f [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 Afanasyev18bbf812014-01-29 01:40:23 -080037namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080038
Davide Pesaventoa3148082018-04-12 18:21:54 -040039NFD_LOG_INIT(Forwarder);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070040
Junxiao Shic34d1672016-12-09 15:57:59 +000041static Name
42getDefaultStrategyName()
43{
Junxiao Shi037f4ab2016-12-13 04:27:06 +000044 return fw::BestRouteStrategy2::getStrategyName();
Junxiao Shic34d1672016-12-09 15:57:59 +000045}
46
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040047Forwarder::Forwarder(FaceTable& faceTable)
48 : m_faceTable(faceTable)
49 , m_unsolicitedDataPolicy(make_unique<fw::DefaultUnsolicitedDataPolicy>())
Junxiao Shifbe8efe2016-08-22 16:02:30 +000050 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060051 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080052 , m_measurements(m_nameTree)
Junxiao Shic34d1672016-12-09 15:57:59 +000053 , m_strategyChoice(*this)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080054{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040055 m_faceTable.afterAdd.connect([this] (const Face& face) {
Junxiao Shidcffdaa2016-07-26 02:23:56 +000056 face.afterReceiveInterest.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000057 [this, &face] (const Interest& interest, const EndpointId& endpointId) {
58 this->startProcessInterest(FaceEndpoint(face, endpointId), interest);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000059 });
60 face.afterReceiveData.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000061 [this, &face] (const Data& data, const EndpointId& endpointId) {
62 this->startProcessData(FaceEndpoint(face, endpointId), data);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000063 });
64 face.afterReceiveNack.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000065 [this, &face] (const lp::Nack& nack, const EndpointId& endpointId) {
66 this->startProcessNack(FaceEndpoint(face, endpointId), nack);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000067 });
Eric Newberry41aba102017-11-01 16:42:13 -070068 face.onDroppedInterest.connect(
69 [this, &face] (const Interest& interest) {
ashiqopuc7079482019-02-20 05:34:37 +000070 this->onDroppedInterest(FaceEndpoint(face, 0), interest);
Eric Newberry41aba102017-11-01 16:42:13 -070071 });
Junxiao Shidcffdaa2016-07-26 02:23:56 +000072 });
73
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040074 m_faceTable.beforeRemove.connect([this] (const Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000075 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000076 });
Junxiao Shic34d1672016-12-09 15:57:59 +000077
78 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080079}
80
Junxiao Shidcffdaa2016-07-26 02:23:56 +000081Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060082
Junxiao Shi0355e9f2015-09-02 07:24:53 -070083void
ashiqopuc7079482019-02-20 05:34:37 +000084Forwarder::onIncomingInterest(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -070085{
86 // receive Interest
ashiqopuc7079482019-02-20 05:34:37 +000087 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress << " interest=" << interest.getName());
88 interest.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -070089 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -070090
Junxiao Shi88884492014-02-15 15:57:43 -070091 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +000092 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -070093 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -070094 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +000095 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
96 << " interest=" << interest.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -070097 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -070098 return;
99 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700100
Junxiao Shi330136a2016-03-10 04:53:08 -0700101 // detect duplicate Nonce with Dead Nonce List
102 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
103 if (hasDuplicateNonceInDnl) {
104 // goto Interest loop pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000105 this->onInterestLoop(ingress, interest);
Junxiao Shi330136a2016-03-10 04:53:08 -0700106 return;
107 }
108
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000109 // strip forwarding hint if Interest has reached producer region
110 if (!interest.getForwardingHint().empty() &&
111 m_networkRegionTable.isInProducerRegion(interest.getForwardingHint())) {
ashiqopuc7079482019-02-20 05:34:37 +0000112 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
113 << " interest=" << interest.getName() << " reaching-producer-region");
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000114 const_cast<Interest&>(interest).setForwardingHint({});
Junxiao Shif9858fd2017-02-01 16:22:44 +0000115 }
116
Junxiao Shid3c792f2014-01-30 00:46:13 -0700117 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700118 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700119
Junxiao Shi330136a2016-03-10 04:53:08 -0700120 // detect duplicate Nonce in PIT entry
ashiqopuc7079482019-02-20 05:34:37 +0000121 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), ingress.face);
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000122 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
ashiqopuc7079482019-02-20 05:34:37 +0000123 if (ingress.face.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000124 // for p2p face: duplicate Nonce from same incoming face is not loop
125 hasDuplicateNonceInPit = hasDuplicateNonceInPit && !(dnw & fw::DUPLICATE_NONCE_IN_SAME);
126 }
Junxiao Shi330136a2016-03-10 04:53:08 -0700127 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700128 // goto Interest loop pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000129 this->onInterestLoop(ingress, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700130 return;
131 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700132
Junxiao Shif3c07812014-03-11 21:48:49 -0700133 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700134 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600135 m_cs.find(interest,
ashiqopuc7079482019-02-20 05:34:37 +0000136 bind(&Forwarder::onContentStoreHit, this, ingress, pitEntry, _1, _2),
137 bind(&Forwarder::onContentStoreMiss, this, ingress, pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700138 }
mzhang4eab72492015-02-25 11:16:09 -0600139 else {
ashiqopuc7079482019-02-20 05:34:37 +0000140 this->onContentStoreMiss(ingress, pitEntry, interest);
mzhang4eab72492015-02-25 11:16:09 -0600141 }
142}
Junxiao Shic041ca32014-02-25 20:01:15 -0700143
mzhang4eab72492015-02-25 11:16:09 -0600144void
ashiqopuc7079482019-02-20 05:34:37 +0000145Forwarder::onInterestLoop(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700146{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700147 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000148 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
149 NFD_LOG_DEBUG("onInterestLoop in=" << ingress
150 << " interest=" << interest.getName() << " drop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700151 return;
152 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700153
ashiqopuc7079482019-02-20 05:34:37 +0000154 NFD_LOG_DEBUG("onInterestLoop in=" << ingress << " interest=" << interest.getName()
155 << " send-Nack-duplicate");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700156
157 // send Nack with reason=DUPLICATE
158 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
159 lp::Nack nack(interest);
160 nack.setReason(lp::NackReason::DUPLICATE);
ashiqopu075bb7d2019-03-10 01:38:21 +0000161 ingress.face.sendNack(nack, ingress.endpoint);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700162}
163
164void
ashiqopuc7079482019-02-20 05:34:37 +0000165Forwarder::onContentStoreMiss(const FaceEndpoint& ingress,
166 const shared_ptr<pit::Entry>& pitEntry, const Interest& interest)
mzhang4eab72492015-02-25 11:16:09 -0600167{
168 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000169 ++m_counters.nCsMisses;
mzhang4eab72492015-02-25 11:16:09 -0600170
Md Ashiqur Rahman115ea632019-07-06 02:34:31 +0000171 // insert in-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000172 pitEntry->insertOrUpdateInRecord(ingress.face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700173
Teng Liang7003e0b2018-03-03 16:03:30 -0700174 // set PIT expiry timer to the time that the last PIT in-record expires
Davide Pesaventob31206e2019-04-20 22:34:12 -0400175 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(),
176 [] (const auto& a, const auto& b) {
177 return a.getExpiry() < b.getExpiry();
178 });
Teng Liang7003e0b2018-03-03 16:03:30 -0700179 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
180 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700181
Junxiao Shie342e8d2016-09-18 16:48:00 +0000182 // has NextHopFaceId?
Davide Pesaventob31206e2019-04-20 22:34:12 -0400183 auto nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
Junxiao Shie342e8d2016-09-18 16:48:00 +0000184 if (nextHopTag != nullptr) {
185 // chosen NextHop face exists?
186 Face* nextHopFace = m_faceTable.get(*nextHopTag);
187 if (nextHopFace != nullptr) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400188 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName()
189 << " 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,
Md Ashiqur Rahman115ea632019-07-06 02:34:31 +0000199 [&] (fw::Strategy& strategy) {
200 strategy.afterReceiveInterest(FaceEndpoint(ingress.face, 0), interest, pitEntry);
201 });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700202}
203
204void
ashiqopuc7079482019-02-20 05:34:37 +0000205Forwarder::onContentStoreHit(const FaceEndpoint& ingress, const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000206 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600207{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500208 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000209 ++m_counters.nCsHits;
mzhang4eab72492015-02-25 11:16:09 -0600210
Junxiao Shicde37ad2015-12-24 01:02:05 -0700211 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
Davide Pesaventob31206e2019-04-20 22:34:12 -0400212 // FIXME Should we lookup PIT for other Interests that also match the data?
mzhang4eab72492015-02-25 11:16:09 -0600213
Teng Liang6f09ab62018-03-01 20:04:08 -0700214 pitEntry->isSatisfied = true;
215 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
216
Teng Liang85a36632018-03-21 05:59:34 -0700217 // set PIT expiry timer to now
218 this->setExpiryTimer(pitEntry, 0_ms);
mzhang4eab72492015-02-25 11:16:09 -0600219
Teng Liang85a36632018-03-21 05:59:34 -0700220 // dispatch to strategy: after Content Store hit
221 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000222 [&] (fw::Strategy& strategy) { strategy.afterContentStoreHit(pitEntry, ingress, data); });
mzhang4eab72492015-02-25 11:16:09 -0600223}
224
Junxiao Shid3c792f2014-01-30 00:46:13 -0700225void
ashiqopuc7079482019-02-20 05:34:37 +0000226Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry,
227 const FaceEndpoint& egress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700228{
ashiqopuc7079482019-02-20 05:34:37 +0000229 NFD_LOG_DEBUG("onOutgoingInterest out=" << egress << " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700230
Junxiao Shi4846f372016-04-05 13:39:30 -0700231 // insert out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000232 pitEntry->insertOrUpdateOutRecord(egress.face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700233
Junxiao Shid3c792f2014-01-30 00:46:13 -0700234 // send Interest
ashiqopu075bb7d2019-03-10 01:38:21 +0000235 egress.face.sendInterest(interest, egress.endpoint);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700236 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700237}
238
239void
Teng Liang6f09ab62018-03-01 20:04:08 -0700240Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shia110f262014-10-12 12:35:20 -0700241{
ashiqopuc7079482019-02-20 05:34:37 +0000242 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName()
243 << (pitEntry->isSatisfied ? " satisfied" : " unsatisfied"));
Junxiao Shia110f262014-10-12 12:35:20 -0700244
245 // Dead Nonce List insert if necessary
Davide Pesavento3dade002019-03-19 11:29:56 -0600246 this->insertDeadNonceList(*pitEntry, nullptr);
Junxiao Shia110f262014-10-12 12:35:20 -0700247
Ju Pan6eb1ac92018-10-26 16:26:15 +0000248 // Increment satisfied/unsatisfied Interests counter
249 if (pitEntry->isSatisfied) {
250 ++m_counters.nSatisfiedInterests;
251 }
252 else {
253 ++m_counters.nUnsatisfiedInterests;
254 }
255
Junxiao Shif3c07812014-03-11 21:48:49 -0700256 // PIT delete
Davide Pesavento3dade002019-03-19 11:29:56 -0600257 pitEntry->expiryTimer.cancel();
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000258 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700259}
260
261void
ashiqopuc7079482019-02-20 05:34:37 +0000262Forwarder::onIncomingData(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700263{
264 // receive Data
ashiqopuc7079482019-02-20 05:34:37 +0000265 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName());
266 data.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700267 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700268
Junxiao Shi88884492014-02-15 15:57:43 -0700269 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000270 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700271 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700272 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000273 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700274 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700275 return;
276 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700277
Junxiao Shid3c792f2014-01-30 00:46:13 -0700278 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700279 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
Teng Liang43bb2312018-03-26 04:16:42 -0700280 if (pitMatches.size() == 0) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700281 // goto Data unsolicited pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000282 this->onDataUnsolicited(ingress, data);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700283 return;
284 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700285
Junxiao Shid3c792f2014-01-30 00:46:13 -0700286 // CS insert
287 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700288
Teng Liang43bb2312018-03-26 04:16:42 -0700289 // when only one PIT entry is matched, trigger strategy: after receive Data
290 if (pitMatches.size() == 1) {
291 auto& pitEntry = pitMatches.front();
Junxiao Shic041ca32014-02-25 20:01:15 -0700292
Teng Liang43bb2312018-03-26 04:16:42 -0700293 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700294
Teng Liang7003e0b2018-03-03 16:03:30 -0700295 // set PIT expiry timer to now
296 this->setExpiryTimer(pitEntry, 0_ms);
297
Teng Liang43bb2312018-03-26 04:16:42 -0700298 // trigger strategy: after receive Data
Junxiao Shib9420cf2016-08-13 04:38:52 +0000299 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000300 [&] (fw::Strategy& strategy) { strategy.afterReceiveData(pitEntry, ingress, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700301
Teng Liang43bb2312018-03-26 04:16:42 -0700302 // mark PIT satisfied
Teng Liang6f09ab62018-03-01 20:04:08 -0700303 pitEntry->isSatisfied = true;
304 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
305
Junxiao Shi4846f372016-04-05 13:39:30 -0700306 // Dead Nonce List insert if necessary (for out-record of inFace)
ashiqopuc7079482019-02-20 05:34:37 +0000307 this->insertDeadNonceList(*pitEntry, &ingress.face);
Junxiao Shia110f262014-10-12 12:35:20 -0700308
Teng Liang43bb2312018-03-26 04:16:42 -0700309 // delete PIT entry's out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000310 pitEntry->deleteOutRecord(ingress.face);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700311 }
Teng Liang43bb2312018-03-26 04:16:42 -0700312 // when more than one PIT entry is matched, trigger strategy: before satisfy Interest,
313 // and send Data to all matched out faces
314 else {
ashiqopuc7079482019-02-20 05:34:37 +0000315 std::set<std::pair<Face*, EndpointId>> pendingDownstreams;
Teng Liang43bb2312018-03-26 04:16:42 -0700316 auto now = time::steady_clock::now();
Junxiao Shic041ca32014-02-25 20:01:15 -0700317
Davide Pesaventob31206e2019-04-20 22:34:12 -0400318 for (const auto& pitEntry : pitMatches) {
Teng Liang43bb2312018-03-26 04:16:42 -0700319 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
320
321 // remember pending downstreams
322 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
323 if (inRecord.getExpiry() > now) {
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000324 pendingDownstreams.emplace(&inRecord.getFace(), 0);
Teng Liang43bb2312018-03-26 04:16:42 -0700325 }
326 }
327
328 // set PIT expiry timer to now
329 this->setExpiryTimer(pitEntry, 0_ms);
330
331 // invoke PIT satisfy callback
332 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000333 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, ingress, data); });
Teng Liang43bb2312018-03-26 04:16:42 -0700334
335 // mark PIT satisfied
336 pitEntry->isSatisfied = true;
337 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
338
339 // Dead Nonce List insert if necessary (for out-record of inFace)
ashiqopuc7079482019-02-20 05:34:37 +0000340 this->insertDeadNonceList(*pitEntry, &ingress.face);
Teng Liang43bb2312018-03-26 04:16:42 -0700341
342 // clear PIT entry's in and out records
343 pitEntry->clearInRecords();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000344 pitEntry->deleteOutRecord(ingress.face);
Junxiao Shida006f52014-05-16 11:18:00 -0700345 }
Teng Liang43bb2312018-03-26 04:16:42 -0700346
347 // foreach pending downstream
ashiqopuc7079482019-02-20 05:34:37 +0000348 for (const auto& pendingDownstream : pendingDownstreams) {
349 if (pendingDownstream.first->getId() == ingress.face.getId() &&
350 pendingDownstream.second == ingress.endpoint &&
351 pendingDownstream.first->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Teng Liang43bb2312018-03-26 04:16:42 -0700352 continue;
353 }
354 // goto outgoing Data pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000355 this->onOutgoingData(data, FaceEndpoint(*pendingDownstream.first, pendingDownstream.second));
Teng Liang43bb2312018-03-26 04:16:42 -0700356 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700357 }
358}
359
360void
ashiqopuc7079482019-02-20 05:34:37 +0000361Forwarder::onDataUnsolicited(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700362{
363 // accept to cache?
ashiqopuc7079482019-02-20 05:34:37 +0000364 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(ingress.face, data);
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000365 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700366 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700367 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700368 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700369
ashiqopuc7079482019-02-20 05:34:37 +0000370 NFD_LOG_DEBUG("onDataUnsolicited in=" << ingress << " data=" << data.getName() << " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700371}
372
373void
ashiqopu075bb7d2019-03-10 01:38:21 +0000374Forwarder::onOutgoingData(const Data& data, const FaceEndpoint& egress)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700375{
ashiqopuc7079482019-02-20 05:34:37 +0000376 if (egress.face.getId() == face::INVALID_FACEID) {
377 NFD_LOG_WARN("onOutgoingData out=(invalid) data=" << data.getName());
Junxiao Shi223271b2014-07-03 22:06:13 -0700378 return;
379 }
ashiqopuc7079482019-02-20 05:34:37 +0000380 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700381
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700382 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000383 bool isViolatingLocalhost = egress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700384 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700385 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000386 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700387 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700388 return;
389 }
390
Junxiao Shif3c07812014-03-11 21:48:49 -0700391 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700392
Junxiao Shid3c792f2014-01-30 00:46:13 -0700393 // send Data
ashiqopu075bb7d2019-03-10 01:38:21 +0000394 egress.face.sendData(data, egress.endpoint);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700395 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700396}
397
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700398void
ashiqopuc7079482019-02-20 05:34:37 +0000399Forwarder::onIncomingNack(const FaceEndpoint& ingress, const lp::Nack& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700400{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000401 // receive Nack
ashiqopuc7079482019-02-20 05:34:37 +0000402 nack.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700403 ++m_counters.nInNacks;
404
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700405 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000406 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400407 NFD_LOG_DEBUG("onIncomingNack in=" << ingress
408 << " nack=" << nack.getInterest().getName() << "~" << nack.getReason()
409 << " link-type=" << ingress.face.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700410 return;
411 }
412
413 // PIT match
414 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
415 // if no PIT entry found, drop
416 if (pitEntry == nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000417 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
418 << "~" << nack.getReason() << " no-PIT-entry");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700419 return;
420 }
421
422 // has out-record?
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000423 auto outRecord = pitEntry->getOutRecord(ingress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700424 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700425 if (outRecord == pitEntry->out_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000426 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
427 << "~" << nack.getReason() << " no-out-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700428 return;
429 }
430
431 // if out-record has different Nonce, drop
432 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
ashiqopuc7079482019-02-20 05:34:37 +0000433 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
434 << "~" << nack.getReason() << " wrong-Nonce " << nack.getInterest().getNonce()
435 << "!=" << outRecord->getLastNonce());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700436 return;
437 }
438
ashiqopuc7079482019-02-20 05:34:37 +0000439 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
440 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700441
442 // record Nack on out-record
443 outRecord->setIncomingNack(nack);
444
Teng Liang7003e0b2018-03-03 16:03:30 -0700445 // set PIT expiry timer to now when all out-record receive Nack
446 if (!fw::hasPendingOutRecords(*pitEntry)) {
447 this->setExpiryTimer(pitEntry, 0_ms);
448 }
449
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700450 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000451 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000452 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(ingress, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700453}
454
455void
ashiqopuc7079482019-02-20 05:34:37 +0000456Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry,
457 const FaceEndpoint& egress, const lp::NackHeader& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700458{
ashiqopuc7079482019-02-20 05:34:37 +0000459 if (egress.face.getId() == face::INVALID_FACEID) {
460 NFD_LOG_WARN("onOutgoingNack out=(invalid)"
Davide Pesaventob31206e2019-04-20 22:34:12 -0400461 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700462 return;
463 }
464
465 // has in-record?
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000466 auto inRecord = pitEntry->getInRecord(egress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700467
468 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700469 if (inRecord == pitEntry->in_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000470 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
471 << " nack=" << pitEntry->getInterest().getName()
472 << "~" << nack.getReason() << " no-in-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700473 return;
474 }
475
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700476 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000477 if (egress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
478 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
Davide Pesaventob31206e2019-04-20 22:34:12 -0400479 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason()
480 << " link-type=" << egress.face.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700481 return;
482 }
483
ashiqopuc7079482019-02-20 05:34:37 +0000484 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
485 << " nack=" << pitEntry->getInterest().getName()
486 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700487
488 // create Nack packet with the Interest from in-record
489 lp::Nack nackPkt(inRecord->getInterest());
490 nackPkt.setHeader(nack);
491
492 // erase in-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000493 pitEntry->deleteInRecord(egress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700494
495 // send Nack on face
ashiqopu075bb7d2019-03-10 01:38:21 +0000496 egress.face.sendNack(nackPkt, egress.endpoint);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700497 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700498}
499
Eric Newberry41aba102017-11-01 16:42:13 -0700500void
ashiqopuc7079482019-02-20 05:34:37 +0000501Forwarder::onDroppedInterest(const FaceEndpoint& egress, const Interest& interest)
Eric Newberry41aba102017-11-01 16:42:13 -0700502{
ashiqopuc7079482019-02-20 05:34:37 +0000503 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(egress, interest);
Eric Newberry41aba102017-11-01 16:42:13 -0700504}
505
Junxiao Shid3c792f2014-01-30 00:46:13 -0700506void
Teng Liang7003e0b2018-03-03 16:03:30 -0700507Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700508{
Teng Liang39465c22018-11-12 19:43:04 -0700509 BOOST_ASSERT(pitEntry);
Teng Liang7003e0b2018-03-03 16:03:30 -0700510 BOOST_ASSERT(duration >= 0_ms);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700511
Davide Pesavento3dade002019-03-19 11:29:56 -0600512 pitEntry->expiryTimer.cancel();
513 pitEntry->expiryTimer = getScheduler().schedule(duration, [=] { onInterestFinalize(pitEntry); });
Junxiao Shia110f262014-10-12 12:35:20 -0700514}
515
516void
Teng Liang6f09ab62018-03-01 20:04:08 -0700517Forwarder::insertDeadNonceList(pit::Entry& pitEntry, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700518{
519 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000520 bool needDnl = true;
Teng Liang6f09ab62018-03-01 20:04:08 -0700521 if (pitEntry.isSatisfied) {
522 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
Junxiao Shia110f262014-10-12 12:35:20 -0700523 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Teng Liang6f09ab62018-03-01 20:04:08 -0700524 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700525 }
526
527 if (!needDnl) {
528 return;
529 }
530
531 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000532 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700533 // insert all outgoing Nonces
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400534 const auto& outRecords = pitEntry.getOutRecords();
535 std::for_each(outRecords.begin(), outRecords.end(), [&] (const auto& outRecord) {
536 m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
537 });
Junxiao Shia110f262014-10-12 12:35:20 -0700538 }
539 else {
540 // insert outgoing Nonce of a specific face
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000541 auto outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700542 if (outRecord != pitEntry.getOutRecords().end()) {
543 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
544 }
545 }
546}
547
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800548} // namespace nfd