blob: 8ab209a2319a1da16b4dedf3f712b531ceb854a3 [file] [log] [blame]
Alexander Afanasyev33b72772014-01-26 23:22:58 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shifc2e13d2017-07-25 02:08:48 +00002/*
Eric Newberry9d283ad2020-04-12 23:37:17 -07003 * Copyright (c) 2014-2020, Regents of the University of California,
Junxiao Shifaf3eb02015-02-16 10:50:36 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shiaf6569a2014-06-14 00:01:34 -070024 */
Alexander Afanasyev33b72772014-01-26 23:22:58 -080025
26#include "forwarder.hpp"
Davide Pesaventoa3148082018-04-12 18:21:54 -040027
Junxiao Shi00dc9142016-11-21 14:23:12 +000028#include "algorithm.hpp"
Junxiao Shic34d1672016-12-09 15:57:59 +000029#include "best-route-strategy2.hpp"
Junxiao Shifaf3eb02015-02-16 10:50:36 -070030#include "strategy.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040031#include "common/global.hpp"
32#include "common/logger.hpp"
Junxiao Shi02b73f52016-07-28 01:48:27 +000033#include "table/cleanup.hpp"
Davide Pesaventoa3148082018-04-12 18:21:54 -040034
Junxiao 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
Ju Pan2feb4592019-09-16 20:56:38 +000078 m_fib.afterNewNextHop.connect([&] (const Name& prefix, const fib::NextHop& nextHop) {
79 this->startProcessNewNextHop(prefix, nextHop);
80 });
81
Junxiao Shic34d1672016-12-09 15:57:59 +000082 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080083}
84
Junxiao Shidcffdaa2016-07-26 02:23:56 +000085Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060086
Junxiao Shi0355e9f2015-09-02 07:24:53 -070087void
ashiqopuc7079482019-02-20 05:34:37 +000088Forwarder::onIncomingInterest(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -070089{
90 // receive Interest
ashiqopuc7079482019-02-20 05:34:37 +000091 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress << " interest=" << interest.getName());
92 interest.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -070093 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -070094
Eric Newberry9d283ad2020-04-12 23:37:17 -070095 // drop if HopLimit zero, decrement otherwise (if present)
96 if (interest.getHopLimit()) {
97 if (*interest.getHopLimit() < 1) {
98 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress << " interest=" << interest.getName()
99 << " hop-limit=0");
100 ++const_cast<PacketCounter&>(ingress.face.getCounters().nInHopLimitZero);
101 return;
102 }
103
104 const_cast<Interest&>(interest).setHopLimit(*interest.getHopLimit() - 1);
105 }
106
Junxiao Shi88884492014-02-15 15:57:43 -0700107 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000108 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700109 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700110 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000111 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
112 << " interest=" << interest.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700113 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700114 return;
115 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700116
Junxiao Shi330136a2016-03-10 04:53:08 -0700117 // detect duplicate Nonce with Dead Nonce List
118 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
119 if (hasDuplicateNonceInDnl) {
120 // goto Interest loop pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000121 this->onInterestLoop(ingress, interest);
Junxiao Shi330136a2016-03-10 04:53:08 -0700122 return;
123 }
124
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000125 // strip forwarding hint if Interest has reached producer region
126 if (!interest.getForwardingHint().empty() &&
127 m_networkRegionTable.isInProducerRegion(interest.getForwardingHint())) {
ashiqopuc7079482019-02-20 05:34:37 +0000128 NFD_LOG_DEBUG("onIncomingInterest in=" << ingress
129 << " interest=" << interest.getName() << " reaching-producer-region");
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000130 const_cast<Interest&>(interest).setForwardingHint({});
Junxiao Shif9858fd2017-02-01 16:22:44 +0000131 }
132
Junxiao Shid3c792f2014-01-30 00:46:13 -0700133 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700134 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700135
Junxiao Shi330136a2016-03-10 04:53:08 -0700136 // detect duplicate Nonce in PIT entry
ashiqopuc7079482019-02-20 05:34:37 +0000137 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), ingress.face);
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000138 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
ashiqopuc7079482019-02-20 05:34:37 +0000139 if (ingress.face.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000140 // for p2p face: duplicate Nonce from same incoming face is not loop
141 hasDuplicateNonceInPit = hasDuplicateNonceInPit && !(dnw & fw::DUPLICATE_NONCE_IN_SAME);
142 }
Junxiao Shi330136a2016-03-10 04:53:08 -0700143 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700144 // goto Interest loop pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000145 this->onInterestLoop(ingress, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700146 return;
147 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700148
Junxiao Shif3c07812014-03-11 21:48:49 -0700149 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700150 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600151 m_cs.find(interest,
ashiqopuc7079482019-02-20 05:34:37 +0000152 bind(&Forwarder::onContentStoreHit, this, ingress, pitEntry, _1, _2),
153 bind(&Forwarder::onContentStoreMiss, this, ingress, pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700154 }
mzhang4eab72492015-02-25 11:16:09 -0600155 else {
ashiqopuc7079482019-02-20 05:34:37 +0000156 this->onContentStoreMiss(ingress, pitEntry, interest);
mzhang4eab72492015-02-25 11:16:09 -0600157 }
158}
Junxiao Shic041ca32014-02-25 20:01:15 -0700159
mzhang4eab72492015-02-25 11:16:09 -0600160void
ashiqopuc7079482019-02-20 05:34:37 +0000161Forwarder::onInterestLoop(const FaceEndpoint& ingress, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700162{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700163 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000164 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
165 NFD_LOG_DEBUG("onInterestLoop in=" << ingress
166 << " interest=" << interest.getName() << " drop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700167 return;
168 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700169
ashiqopuc7079482019-02-20 05:34:37 +0000170 NFD_LOG_DEBUG("onInterestLoop in=" << ingress << " interest=" << interest.getName()
171 << " send-Nack-duplicate");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700172
173 // send Nack with reason=DUPLICATE
174 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
175 lp::Nack nack(interest);
176 nack.setReason(lp::NackReason::DUPLICATE);
ashiqopu075bb7d2019-03-10 01:38:21 +0000177 ingress.face.sendNack(nack, ingress.endpoint);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700178}
179
180void
ashiqopuc7079482019-02-20 05:34:37 +0000181Forwarder::onContentStoreMiss(const FaceEndpoint& ingress,
182 const shared_ptr<pit::Entry>& pitEntry, const Interest& interest)
mzhang4eab72492015-02-25 11:16:09 -0600183{
184 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000185 ++m_counters.nCsMisses;
mzhang4eab72492015-02-25 11:16:09 -0600186
Md Ashiqur Rahman115ea632019-07-06 02:34:31 +0000187 // insert in-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000188 pitEntry->insertOrUpdateInRecord(ingress.face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700189
Teng Liang7003e0b2018-03-03 16:03:30 -0700190 // set PIT expiry timer to the time that the last PIT in-record expires
Davide Pesaventob31206e2019-04-20 22:34:12 -0400191 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(),
192 [] (const auto& a, const auto& b) {
193 return a.getExpiry() < b.getExpiry();
194 });
Teng Liang7003e0b2018-03-03 16:03:30 -0700195 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
196 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700197
Junxiao Shie342e8d2016-09-18 16:48:00 +0000198 // has NextHopFaceId?
Davide Pesaventob31206e2019-04-20 22:34:12 -0400199 auto nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
Junxiao Shie342e8d2016-09-18 16:48:00 +0000200 if (nextHopTag != nullptr) {
201 // chosen NextHop face exists?
202 Face* nextHopFace = m_faceTable.get(*nextHopTag);
203 if (nextHopFace != nullptr) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400204 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName()
205 << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000206 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000207 // scope control is unnecessary, because privileged app explicitly wants to forward
ashiqopuc7079482019-02-20 05:34:37 +0000208 this->onOutgoingInterest(pitEntry, FaceEndpoint(*nextHopFace, 0), interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000209 }
210 return;
211 }
212
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000213 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000214 this->dispatchToStrategy(*pitEntry,
Md Ashiqur Rahman115ea632019-07-06 02:34:31 +0000215 [&] (fw::Strategy& strategy) {
216 strategy.afterReceiveInterest(FaceEndpoint(ingress.face, 0), interest, pitEntry);
217 });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700218}
219
220void
ashiqopuc7079482019-02-20 05:34:37 +0000221Forwarder::onContentStoreHit(const FaceEndpoint& ingress, const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000222 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600223{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500224 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000225 ++m_counters.nCsHits;
mzhang4eab72492015-02-25 11:16:09 -0600226
Junxiao Shicde37ad2015-12-24 01:02:05 -0700227 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
Davide Pesaventob31206e2019-04-20 22:34:12 -0400228 // FIXME Should we lookup PIT for other Interests that also match the data?
mzhang4eab72492015-02-25 11:16:09 -0600229
Teng Liang6f09ab62018-03-01 20:04:08 -0700230 pitEntry->isSatisfied = true;
231 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
232
Teng Liang85a36632018-03-21 05:59:34 -0700233 // set PIT expiry timer to now
234 this->setExpiryTimer(pitEntry, 0_ms);
mzhang4eab72492015-02-25 11:16:09 -0600235
Teng Liang85a36632018-03-21 05:59:34 -0700236 // dispatch to strategy: after Content Store hit
237 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000238 [&] (fw::Strategy& strategy) { strategy.afterContentStoreHit(pitEntry, ingress, data); });
mzhang4eab72492015-02-25 11:16:09 -0600239}
240
Junxiao Shid3c792f2014-01-30 00:46:13 -0700241void
ashiqopuc7079482019-02-20 05:34:37 +0000242Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry,
243 const FaceEndpoint& egress, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700244{
Eric Newberry9d283ad2020-04-12 23:37:17 -0700245 // drop if HopLimit == 0 but sending on non-local face
246 if (interest.getHopLimit() == 0 && egress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL) {
247 NFD_LOG_DEBUG("onOutgoingInterest out=" << egress << " interest=" << pitEntry->getName()
248 << " non-local hop-limit=0");
249 ++const_cast<PacketCounter&>(egress.face.getCounters().nOutHopLimitZero);
250 return;
251 }
252
ashiqopuc7079482019-02-20 05:34:37 +0000253 NFD_LOG_DEBUG("onOutgoingInterest out=" << egress << " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700254
Junxiao Shi4846f372016-04-05 13:39:30 -0700255 // insert out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000256 pitEntry->insertOrUpdateOutRecord(egress.face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700257
Junxiao Shid3c792f2014-01-30 00:46:13 -0700258 // send Interest
ashiqopu075bb7d2019-03-10 01:38:21 +0000259 egress.face.sendInterest(interest, egress.endpoint);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700260 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700261}
262
263void
Teng Liang6f09ab62018-03-01 20:04:08 -0700264Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shia110f262014-10-12 12:35:20 -0700265{
ashiqopuc7079482019-02-20 05:34:37 +0000266 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName()
267 << (pitEntry->isSatisfied ? " satisfied" : " unsatisfied"));
Junxiao Shia110f262014-10-12 12:35:20 -0700268
269 // Dead Nonce List insert if necessary
Davide Pesavento3dade002019-03-19 11:29:56 -0600270 this->insertDeadNonceList(*pitEntry, nullptr);
Junxiao Shia110f262014-10-12 12:35:20 -0700271
Ju Pan6eb1ac92018-10-26 16:26:15 +0000272 // Increment satisfied/unsatisfied Interests counter
273 if (pitEntry->isSatisfied) {
274 ++m_counters.nSatisfiedInterests;
275 }
276 else {
277 ++m_counters.nUnsatisfiedInterests;
278 }
279
Junxiao Shif3c07812014-03-11 21:48:49 -0700280 // PIT delete
Davide Pesavento3dade002019-03-19 11:29:56 -0600281 pitEntry->expiryTimer.cancel();
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000282 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700283}
284
285void
ashiqopuc7079482019-02-20 05:34:37 +0000286Forwarder::onIncomingData(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700287{
288 // receive Data
ashiqopuc7079482019-02-20 05:34:37 +0000289 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName());
290 data.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700291 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700292
Junxiao Shi88884492014-02-15 15:57:43 -0700293 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000294 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700295 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700296 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000297 NFD_LOG_DEBUG("onIncomingData in=" << ingress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700298 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700299 return;
300 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700301
Junxiao Shid3c792f2014-01-30 00:46:13 -0700302 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700303 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
Teng Liang43bb2312018-03-26 04:16:42 -0700304 if (pitMatches.size() == 0) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700305 // goto Data unsolicited pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000306 this->onDataUnsolicited(ingress, data);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700307 return;
308 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700309
Junxiao Shid3c792f2014-01-30 00:46:13 -0700310 // CS insert
311 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700312
Teng Liang43bb2312018-03-26 04:16:42 -0700313 // when only one PIT entry is matched, trigger strategy: after receive Data
314 if (pitMatches.size() == 1) {
315 auto& pitEntry = pitMatches.front();
Junxiao Shic041ca32014-02-25 20:01:15 -0700316
Teng Liang43bb2312018-03-26 04:16:42 -0700317 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700318
Teng Liang7003e0b2018-03-03 16:03:30 -0700319 // set PIT expiry timer to now
320 this->setExpiryTimer(pitEntry, 0_ms);
321
Teng Liang43bb2312018-03-26 04:16:42 -0700322 // trigger strategy: after receive Data
Junxiao Shib9420cf2016-08-13 04:38:52 +0000323 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000324 [&] (fw::Strategy& strategy) { strategy.afterReceiveData(pitEntry, ingress, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700325
Teng Liang43bb2312018-03-26 04:16:42 -0700326 // mark PIT satisfied
Teng Liang6f09ab62018-03-01 20:04:08 -0700327 pitEntry->isSatisfied = true;
328 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
329
Junxiao Shi4846f372016-04-05 13:39:30 -0700330 // Dead Nonce List insert if necessary (for out-record of inFace)
ashiqopuc7079482019-02-20 05:34:37 +0000331 this->insertDeadNonceList(*pitEntry, &ingress.face);
Junxiao Shia110f262014-10-12 12:35:20 -0700332
Teng Liang43bb2312018-03-26 04:16:42 -0700333 // delete PIT entry's out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000334 pitEntry->deleteOutRecord(ingress.face);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700335 }
Teng Liang43bb2312018-03-26 04:16:42 -0700336 // when more than one PIT entry is matched, trigger strategy: before satisfy Interest,
337 // and send Data to all matched out faces
338 else {
ashiqopuc7079482019-02-20 05:34:37 +0000339 std::set<std::pair<Face*, EndpointId>> pendingDownstreams;
Teng Liang43bb2312018-03-26 04:16:42 -0700340 auto now = time::steady_clock::now();
Junxiao Shic041ca32014-02-25 20:01:15 -0700341
Davide Pesaventob31206e2019-04-20 22:34:12 -0400342 for (const auto& pitEntry : pitMatches) {
Teng Liang43bb2312018-03-26 04:16:42 -0700343 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
344
345 // remember pending downstreams
346 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
347 if (inRecord.getExpiry() > now) {
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000348 pendingDownstreams.emplace(&inRecord.getFace(), 0);
Teng Liang43bb2312018-03-26 04:16:42 -0700349 }
350 }
351
352 // set PIT expiry timer to now
353 this->setExpiryTimer(pitEntry, 0_ms);
354
355 // invoke PIT satisfy callback
356 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000357 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, ingress, data); });
Teng Liang43bb2312018-03-26 04:16:42 -0700358
359 // mark PIT satisfied
360 pitEntry->isSatisfied = true;
361 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
362
363 // Dead Nonce List insert if necessary (for out-record of inFace)
ashiqopuc7079482019-02-20 05:34:37 +0000364 this->insertDeadNonceList(*pitEntry, &ingress.face);
Teng Liang43bb2312018-03-26 04:16:42 -0700365
366 // clear PIT entry's in and out records
367 pitEntry->clearInRecords();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000368 pitEntry->deleteOutRecord(ingress.face);
Junxiao Shida006f52014-05-16 11:18:00 -0700369 }
Teng Liang43bb2312018-03-26 04:16:42 -0700370
371 // foreach pending downstream
ashiqopuc7079482019-02-20 05:34:37 +0000372 for (const auto& pendingDownstream : pendingDownstreams) {
373 if (pendingDownstream.first->getId() == ingress.face.getId() &&
374 pendingDownstream.second == ingress.endpoint &&
375 pendingDownstream.first->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Teng Liang43bb2312018-03-26 04:16:42 -0700376 continue;
377 }
378 // goto outgoing Data pipeline
ashiqopuc7079482019-02-20 05:34:37 +0000379 this->onOutgoingData(data, FaceEndpoint(*pendingDownstream.first, pendingDownstream.second));
Teng Liang43bb2312018-03-26 04:16:42 -0700380 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700381 }
382}
383
384void
ashiqopuc7079482019-02-20 05:34:37 +0000385Forwarder::onDataUnsolicited(const FaceEndpoint& ingress, const Data& data)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700386{
387 // accept to cache?
ashiqopuc7079482019-02-20 05:34:37 +0000388 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(ingress.face, data);
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000389 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700390 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700391 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700392 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700393
ashiqopuc7079482019-02-20 05:34:37 +0000394 NFD_LOG_DEBUG("onDataUnsolicited in=" << ingress << " data=" << data.getName() << " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700395}
396
397void
ashiqopu075bb7d2019-03-10 01:38:21 +0000398Forwarder::onOutgoingData(const Data& data, const FaceEndpoint& egress)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700399{
ashiqopuc7079482019-02-20 05:34:37 +0000400 if (egress.face.getId() == face::INVALID_FACEID) {
401 NFD_LOG_WARN("onOutgoingData out=(invalid) data=" << data.getName());
Junxiao Shi223271b2014-07-03 22:06:13 -0700402 return;
403 }
ashiqopuc7079482019-02-20 05:34:37 +0000404 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700405
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700406 // /localhost scope control
ashiqopuc7079482019-02-20 05:34:37 +0000407 bool isViolatingLocalhost = egress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700408 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700409 if (isViolatingLocalhost) {
ashiqopuc7079482019-02-20 05:34:37 +0000410 NFD_LOG_DEBUG("onOutgoingData out=" << egress << " data=" << data.getName() << " violates /localhost");
Junxiao Shif3c07812014-03-11 21:48:49 -0700411 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700412 return;
413 }
414
Junxiao Shif3c07812014-03-11 21:48:49 -0700415 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700416
Junxiao Shid3c792f2014-01-30 00:46:13 -0700417 // send Data
ashiqopu075bb7d2019-03-10 01:38:21 +0000418 egress.face.sendData(data, egress.endpoint);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700419 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700420}
421
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700422void
ashiqopuc7079482019-02-20 05:34:37 +0000423Forwarder::onIncomingNack(const FaceEndpoint& ingress, const lp::Nack& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700424{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000425 // receive Nack
ashiqopuc7079482019-02-20 05:34:37 +0000426 nack.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700427 ++m_counters.nInNacks;
428
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700429 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000430 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Davide Pesaventob31206e2019-04-20 22:34:12 -0400431 NFD_LOG_DEBUG("onIncomingNack in=" << ingress
432 << " nack=" << nack.getInterest().getName() << "~" << nack.getReason()
433 << " link-type=" << ingress.face.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700434 return;
435 }
436
437 // PIT match
438 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
439 // if no PIT entry found, drop
440 if (pitEntry == nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000441 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
442 << "~" << nack.getReason() << " no-PIT-entry");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700443 return;
444 }
445
446 // has out-record?
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000447 auto outRecord = pitEntry->getOutRecord(ingress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700448 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700449 if (outRecord == pitEntry->out_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000450 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
451 << "~" << nack.getReason() << " no-out-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700452 return;
453 }
454
455 // if out-record has different Nonce, drop
456 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
ashiqopuc7079482019-02-20 05:34:37 +0000457 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
458 << "~" << nack.getReason() << " wrong-Nonce " << nack.getInterest().getNonce()
459 << "!=" << outRecord->getLastNonce());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700460 return;
461 }
462
ashiqopuc7079482019-02-20 05:34:37 +0000463 NFD_LOG_DEBUG("onIncomingNack in=" << ingress << " nack=" << nack.getInterest().getName()
464 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700465
466 // record Nack on out-record
467 outRecord->setIncomingNack(nack);
468
Teng Liang7003e0b2018-03-03 16:03:30 -0700469 // set PIT expiry timer to now when all out-record receive Nack
470 if (!fw::hasPendingOutRecords(*pitEntry)) {
471 this->setExpiryTimer(pitEntry, 0_ms);
472 }
473
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700474 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000475 this->dispatchToStrategy(*pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000476 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(ingress, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700477}
478
479void
ashiqopuc7079482019-02-20 05:34:37 +0000480Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry,
481 const FaceEndpoint& egress, const lp::NackHeader& nack)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700482{
ashiqopuc7079482019-02-20 05:34:37 +0000483 if (egress.face.getId() == face::INVALID_FACEID) {
484 NFD_LOG_WARN("onOutgoingNack out=(invalid)"
Davide Pesaventob31206e2019-04-20 22:34:12 -0400485 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700486 return;
487 }
488
489 // has in-record?
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000490 auto inRecord = pitEntry->getInRecord(egress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700491
492 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700493 if (inRecord == pitEntry->in_end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000494 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
495 << " nack=" << pitEntry->getInterest().getName()
496 << "~" << nack.getReason() << " no-in-record");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700497 return;
498 }
499
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700500 // if multi-access or ad hoc face, drop
ashiqopuc7079482019-02-20 05:34:37 +0000501 if (egress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
502 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
Davide Pesaventob31206e2019-04-20 22:34:12 -0400503 << " nack=" << pitEntry->getInterest().getName() << "~" << nack.getReason()
504 << " link-type=" << egress.face.getLinkType());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700505 return;
506 }
507
ashiqopuc7079482019-02-20 05:34:37 +0000508 NFD_LOG_DEBUG("onOutgoingNack out=" << egress
509 << " nack=" << pitEntry->getInterest().getName()
510 << "~" << nack.getReason() << " OK");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700511
512 // create Nack packet with the Interest from in-record
513 lp::Nack nackPkt(inRecord->getInterest());
514 nackPkt.setHeader(nack);
515
516 // erase in-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000517 pitEntry->deleteInRecord(egress.face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700518
519 // send Nack on face
ashiqopu075bb7d2019-03-10 01:38:21 +0000520 egress.face.sendNack(nackPkt, egress.endpoint);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700521 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700522}
523
Eric Newberry41aba102017-11-01 16:42:13 -0700524void
ashiqopuc7079482019-02-20 05:34:37 +0000525Forwarder::onDroppedInterest(const FaceEndpoint& egress, const Interest& interest)
Eric Newberry41aba102017-11-01 16:42:13 -0700526{
ashiqopuc7079482019-02-20 05:34:37 +0000527 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(egress, interest);
Eric Newberry41aba102017-11-01 16:42:13 -0700528}
529
Junxiao Shid3c792f2014-01-30 00:46:13 -0700530void
Ju Pan2feb4592019-09-16 20:56:38 +0000531Forwarder::onNewNextHop(const Name& prefix, const fib::NextHop& nextHop)
532{
533 const auto affectedEntries = this->getNameTree().partialEnumerate(prefix,
534 [&] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
535 const fib::Entry* fibEntry = nte.getFibEntry();
536 const fw::Strategy* strategy = nullptr;
537 if (nte.getStrategyChoiceEntry() != nullptr) {
538 strategy = &nte.getStrategyChoiceEntry()->getStrategy();
539 }
540 // current nte has buffered Interests but no fibEntry (except for the root nte) and the strategy
541 // enables new nexthop behavior, we enumerate the current nte and keep visiting its children.
542 if (nte.getName().size() == 0 ||
543 (strategy != nullptr && strategy->wantNewNextHopTrigger() &&
544 fibEntry == nullptr && nte.hasPitEntries())) {
545 return {true, true};
546 }
547 // we don't need the current nte (no pitEntry or strategy doesn't support new nexthop), but
548 // if the current nte has no fibEntry, it's still possible that its children are affected by
549 // the new nexthop.
550 else if (fibEntry == nullptr) {
551 return {false, true};
552 }
553 // if the current nte has a fibEntry, we ignore the current nte and don't visit its
554 // children because they are already covered by the current nte's fibEntry.
555 else {
556 return {false, false};
557 }
558 });
559
560 for (const auto& nte : affectedEntries) {
561 for (const auto& pitEntry : nte.getPitEntries()) {
562 this->dispatchToStrategy(*pitEntry,
563 [&] (fw::Strategy& strategy) {
564 strategy.afterNewNextHop(nextHop, pitEntry);
565 });
566 }
567 }
568}
569
570void
Teng Liang7003e0b2018-03-03 16:03:30 -0700571Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700572{
Teng Liang39465c22018-11-12 19:43:04 -0700573 BOOST_ASSERT(pitEntry);
Teng Liang7003e0b2018-03-03 16:03:30 -0700574 BOOST_ASSERT(duration >= 0_ms);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700575
Davide Pesavento3dade002019-03-19 11:29:56 -0600576 pitEntry->expiryTimer.cancel();
577 pitEntry->expiryTimer = getScheduler().schedule(duration, [=] { onInterestFinalize(pitEntry); });
Junxiao Shia110f262014-10-12 12:35:20 -0700578}
579
580void
Teng Liang6f09ab62018-03-01 20:04:08 -0700581Forwarder::insertDeadNonceList(pit::Entry& pitEntry, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700582{
583 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000584 bool needDnl = true;
Teng Liang6f09ab62018-03-01 20:04:08 -0700585 if (pitEntry.isSatisfied) {
586 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
Junxiao Shia110f262014-10-12 12:35:20 -0700587 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Teng Liang6f09ab62018-03-01 20:04:08 -0700588 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700589 }
590
591 if (!needDnl) {
592 return;
593 }
594
595 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000596 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700597 // insert all outgoing Nonces
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400598 const auto& outRecords = pitEntry.getOutRecords();
599 std::for_each(outRecords.begin(), outRecords.end(), [&] (const auto& outRecord) {
600 m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
601 });
Junxiao Shia110f262014-10-12 12:35:20 -0700602 }
603 else {
604 // insert outgoing Nonce of a specific face
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000605 auto outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700606 if (outRecord != pitEntry.getOutRecords().end()) {
607 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
608 }
609 }
610}
611
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800612} // namespace nfd