blob: 6d57a2d25f3e83b008ae0b69547b367fdbbd8db6 [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/*
Junxiao Shif9858fd2017-02-01 16:22:44 +00003 * Copyright (c) 2014-2017, 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"
Junxiao Shi00dc9142016-11-21 14:23:12 +000027#include "algorithm.hpp"
Junxiao Shic34d1672016-12-09 15:57:59 +000028#include "best-route-strategy2.hpp"
Junxiao Shifaf3eb02015-02-16 10:50:36 -070029#include "strategy.hpp"
Junxiao Shic34d1672016-12-09 15:57:59 +000030#include "core/logger.hpp"
Junxiao Shi02b73f52016-07-28 01:48:27 +000031#include "table/cleanup.hpp"
Junxiao Shicbc8e942016-09-06 03:17:45 +000032#include <ndn-cxx/lp/tags.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080033
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080034namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080035
Junxiao Shi8c8d2182014-01-30 22:33:00 -070036NFD_LOG_INIT("Forwarder");
37
Junxiao Shic34d1672016-12-09 15:57:59 +000038static Name
39getDefaultStrategyName()
40{
Junxiao Shi037f4ab2016-12-13 04:27:06 +000041 return fw::BestRouteStrategy2::getStrategyName();
Junxiao Shic34d1672016-12-09 15:57:59 +000042}
43
Junxiao Shic041ca32014-02-25 20:01:15 -070044Forwarder::Forwarder()
Junxiao Shi9685cc52016-08-29 12:47:05 +000045 : m_unsolicitedDataPolicy(new fw::DefaultUnsolicitedDataPolicy())
Junxiao Shifbe8efe2016-08-22 16:02:30 +000046 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060047 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080048 , m_measurements(m_nameTree)
Junxiao Shic34d1672016-12-09 15:57:59 +000049 , m_strategyChoice(*this)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080050{
Junxiao Shidcffdaa2016-07-26 02:23:56 +000051 m_faceTable.afterAdd.connect([this] (Face& face) {
52 face.afterReceiveInterest.connect(
53 [this, &face] (const Interest& interest) {
54 this->startProcessInterest(face, interest);
55 });
56 face.afterReceiveData.connect(
57 [this, &face] (const Data& data) {
58 this->startProcessData(face, data);
59 });
60 face.afterReceiveNack.connect(
61 [this, &face] (const lp::Nack& nack) {
62 this->startProcessNack(face, nack);
63 });
Eric Newberry41aba102017-11-01 16:42:13 -070064 face.onDroppedInterest.connect(
65 [this, &face] (const Interest& interest) {
66 this->onDroppedInterest(face, interest);
67 });
Junxiao Shidcffdaa2016-07-26 02:23:56 +000068 });
69
70 m_faceTable.beforeRemove.connect([this] (Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000071 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000072 });
Junxiao Shic34d1672016-12-09 15:57:59 +000073
74 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080075}
76
Junxiao Shidcffdaa2016-07-26 02:23:56 +000077Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060078
Junxiao Shi0355e9f2015-09-02 07:24:53 -070079void
Junxiao Shid3c792f2014-01-30 00:46:13 -070080Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
81{
82 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -070083 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
84 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +000085 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -070086 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -070087
Junxiao Shi88884492014-02-15 15:57:43 -070088 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -070089 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -070090 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -070091 if (isViolatingLocalhost) {
92 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
93 " interest=" << interest.getName() << " violates /localhost");
94 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -070095 return;
96 }
Junxiao Shic041ca32014-02-25 20:01:15 -070097
Junxiao Shi330136a2016-03-10 04:53:08 -070098 // detect duplicate Nonce with Dead Nonce List
99 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
100 if (hasDuplicateNonceInDnl) {
101 // goto Interest loop pipeline
102 this->onInterestLoop(inFace, interest);
103 return;
104 }
105
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000106 // strip forwarding hint if Interest has reached producer region
107 if (!interest.getForwardingHint().empty() &&
108 m_networkRegionTable.isInProducerRegion(interest.getForwardingHint())) {
Junxiao Shif9858fd2017-02-01 16:22:44 +0000109 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
110 " interest=" << interest.getName() << " reaching-producer-region");
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000111 const_cast<Interest&>(interest).setForwardingHint({});
Junxiao Shif9858fd2017-02-01 16:22:44 +0000112 }
113
Junxiao Shid3c792f2014-01-30 00:46:13 -0700114 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700115 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700116
Junxiao Shi330136a2016-03-10 04:53:08 -0700117 // detect duplicate Nonce in PIT entry
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000118 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace);
119 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
120 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
121 // for p2p face: duplicate Nonce from same incoming face is not loop
122 hasDuplicateNonceInPit = hasDuplicateNonceInPit && !(dnw & fw::DUPLICATE_NONCE_IN_SAME);
123 }
Junxiao Shi330136a2016-03-10 04:53:08 -0700124 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700125 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700126 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700127 return;
128 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700129
Junxiao Shid3c792f2014-01-30 00:46:13 -0700130 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000131 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
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,
136 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
137 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700138 }
mzhang4eab72492015-02-25 11:16:09 -0600139 else {
140 this->onContentStoreMiss(inFace, pitEntry, interest);
141 }
142}
Junxiao Shic041ca32014-02-25 20:01:15 -0700143
mzhang4eab72492015-02-25 11:16:09 -0600144void
Junxiao Shi330136a2016-03-10 04:53:08 -0700145Forwarder::onInterestLoop(Face& inFace, 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
148 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700149 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
150 " interest=" << interest.getName() <<
151 " drop");
152 return;
153 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700154
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700155 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
156 " interest=" << interest.getName() <<
157 " send-Nack-duplicate");
158
159 // send Nack with reason=DUPLICATE
160 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
161 lp::Nack nack(interest);
162 nack.setReason(lp::NackReason::DUPLICATE);
163 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700164}
165
166void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000167Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
mzhang4eab72492015-02-25 11:16:09 -0600168 const Interest& interest)
169{
170 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000171 ++m_counters.nCsMisses;
mzhang4eab72492015-02-25 11:16:09 -0600172
Junxiao Shi4846f372016-04-05 13:39:30 -0700173 // insert in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000174 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700175
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700176 // set PIT unsatisfy timer
177 this->setUnsatisfyTimer(pitEntry);
178
Junxiao Shie342e8d2016-09-18 16:48:00 +0000179 // has NextHopFaceId?
180 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
181 if (nextHopTag != nullptr) {
182 // chosen NextHop face exists?
183 Face* nextHopFace = m_faceTable.get(*nextHopTag);
184 if (nextHopFace != nullptr) {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000185 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName() << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000186 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000187 // scope control is unnecessary, because privileged app explicitly wants to forward
188 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000189 }
190 return;
191 }
192
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000193 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000194 this->dispatchToStrategy(*pitEntry,
195 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700196}
197
198void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000199Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
200 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600201{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500202 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000203 ++m_counters.nCsHits;
mzhang4eab72492015-02-25 11:16:09 -0600204
Junxiao Shicde37ad2015-12-24 01:02:05 -0700205 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600206 // XXX should we lookup PIT for other Interests that also match csMatch?
207
208 // set PIT straggler timer
209 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
210
211 // goto outgoing Data pipeline
212 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
213}
214
Junxiao Shid3c792f2014-01-30 00:46:13 -0700215void
Junxiao Shic5f651f2016-11-17 22:58:12 +0000216Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700217{
Junxiao Shif3c07812014-03-11 21:48:49 -0700218 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
219 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700220
Junxiao Shi4846f372016-04-05 13:39:30 -0700221 // insert out-record
Junxiao Shic5f651f2016-11-17 22:58:12 +0000222 pitEntry->insertOrUpdateOutRecord(outFace, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700223
Junxiao Shid3c792f2014-01-30 00:46:13 -0700224 // send Interest
Junxiao Shic5f651f2016-11-17 22:58:12 +0000225 outFace.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700226 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700227}
228
229void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000230Forwarder::onInterestReject(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700231{
Junxiao Shifef73e42016-03-29 14:15:05 -0700232 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700233 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
234 " cannot reject forwarded Interest");
235 return;
236 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700237 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700238
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700239 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000240 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700241
Junxiao Shid3c792f2014-01-30 00:46:13 -0700242 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700243 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700244}
245
246void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000247Forwarder::onInterestUnsatisfied(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700248{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700249 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
250
Junxiao Shid3c792f2014-01-30 00:46:13 -0700251 // invoke PIT unsatisfied callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000252 this->dispatchToStrategy(*pitEntry,
253 [&] (fw::Strategy& strategy) { strategy.beforeExpirePendingInterest(pitEntry); });
Junxiao Shic041ca32014-02-25 20:01:15 -0700254
Junxiao Shia110f262014-10-12 12:35:20 -0700255 // goto Interest Finalize pipeline
256 this->onInterestFinalize(pitEntry, false);
257}
258
259void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000260Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
Eric Newberryf4056d02017-05-26 17:31:53 +0000261 ndn::optional<time::milliseconds> dataFreshnessPeriod)
Junxiao Shia110f262014-10-12 12:35:20 -0700262{
263 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
264 (isSatisfied ? " satisfied" : " unsatisfied"));
265
266 // Dead Nonce List insert if necessary
267 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
268
Junxiao Shif3c07812014-03-11 21:48:49 -0700269 // PIT delete
Junxiao Shib9420cf2016-08-13 04:38:52 +0000270 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000271 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700272}
273
274void
275Forwarder::onIncomingData(Face& inFace, const Data& data)
276{
277 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700278 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000279 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700280 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700281
Junxiao Shi88884492014-02-15 15:57:43 -0700282 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700283 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700284 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700285 if (isViolatingLocalhost) {
286 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
287 " data=" << data.getName() << " violates /localhost");
288 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700289 return;
290 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700291
Junxiao Shid3c792f2014-01-30 00:46:13 -0700292 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700293 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
294 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700295 // goto Data unsolicited pipeline
296 this->onDataUnsolicited(inFace, data);
297 return;
298 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700299
Junxiao Shid3c792f2014-01-30 00:46:13 -0700300 // CS insert
301 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700302
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700303 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700304 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700305 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700306 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700307 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700308
Junxiao Shid3c792f2014-01-30 00:46:13 -0700309 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000310 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700311
Junxiao Shid3c792f2014-01-30 00:46:13 -0700312 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700313 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
314 if (inRecord.getExpiry() > now) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000315 pendingDownstreams.insert(&inRecord.getFace());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700316 }
317 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700318
Junxiao Shid938a6b2014-05-11 23:40:29 -0700319 // invoke PIT satisfy callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000320 this->dispatchToStrategy(*pitEntry,
321 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700322
Junxiao Shi4846f372016-04-05 13:39:30 -0700323 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700324 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
325
Junxiao Shid3c792f2014-01-30 00:46:13 -0700326 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700327 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700328 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700329
Junxiao Shid3c792f2014-01-30 00:46:13 -0700330 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700331 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700332 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700333
Junxiao Shid3c792f2014-01-30 00:46:13 -0700334 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700335 for (Face* pendingDownstream : pendingDownstreams) {
Teng Liangf995f382017-04-04 22:09:39 +0000336 if (pendingDownstream->getId() == inFace.getId() &&
337 pendingDownstream->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Junxiao Shida006f52014-05-16 11:18:00 -0700338 continue;
339 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700340 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700341 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700342 }
343}
344
345void
346Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
347{
348 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000349 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
350 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700351 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700352 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700353 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700354
Junxiao Shif3c07812014-03-11 21:48:49 -0700355 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
356 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000357 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700358}
359
360void
361Forwarder::onOutgoingData(const Data& data, Face& outFace)
362{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700363 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700364 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
365 return;
366 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700367 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
368
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700369 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700370 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700371 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700372 if (isViolatingLocalhost) {
373 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
374 " data=" << data.getName() << " violates /localhost");
375 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700376 return;
377 }
378
Junxiao Shif3c07812014-03-11 21:48:49 -0700379 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700380
Junxiao Shid3c792f2014-01-30 00:46:13 -0700381 // send Data
382 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700383 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700384}
385
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700386void
387Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
388{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000389 // receive Nack
390 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700391 ++m_counters.nInNacks;
392
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700393 // if multi-access or ad hoc face, drop
394 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700395 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
396 " nack=" << nack.getInterest().getName() <<
397 "~" << nack.getReason() << " face-is-multi-access");
398 return;
399 }
400
401 // PIT match
402 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
403 // if no PIT entry found, drop
404 if (pitEntry == nullptr) {
405 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
406 " nack=" << nack.getInterest().getName() <<
407 "~" << nack.getReason() << " no-PIT-entry");
408 return;
409 }
410
411 // has out-record?
412 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
413 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700414 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700415 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
416 " nack=" << nack.getInterest().getName() <<
417 "~" << nack.getReason() << " no-out-record");
418 return;
419 }
420
421 // if out-record has different Nonce, drop
422 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
423 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
424 " nack=" << nack.getInterest().getName() <<
425 "~" << nack.getReason() << " wrong-Nonce " <<
426 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
427 return;
428 }
429
430 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
431 " nack=" << nack.getInterest().getName() <<
432 "~" << nack.getReason() << " OK");
433
434 // record Nack on out-record
435 outRecord->setIncomingNack(nack);
436
437 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000438 this->dispatchToStrategy(*pitEntry,
439 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700440}
441
442void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000443Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700444 const lp::NackHeader& nack)
445{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700446 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700447 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
448 " nack=" << pitEntry->getInterest().getName() <<
449 "~" << nack.getReason() << " no-in-record");
450 return;
451 }
452
453 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700454 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700455
456 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700457 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700458 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
459 " nack=" << pitEntry->getInterest().getName() <<
460 "~" << nack.getReason() << " no-in-record");
461 return;
462 }
463
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700464 // if multi-access or ad hoc face, drop
465 if (outFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700466 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
467 " nack=" << pitEntry->getInterest().getName() <<
468 "~" << nack.getReason() << " face-is-multi-access");
469 return;
470 }
471
472 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
473 " nack=" << pitEntry->getInterest().getName() <<
474 "~" << nack.getReason() << " OK");
475
476 // create Nack packet with the Interest from in-record
477 lp::Nack nackPkt(inRecord->getInterest());
478 nackPkt.setHeader(nack);
479
480 // erase in-record
481 pitEntry->deleteInRecord(outFace);
482
483 // send Nack on face
484 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700485 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700486}
487
Eric Newberry41aba102017-11-01 16:42:13 -0700488void
489Forwarder::onDroppedInterest(Face& outFace, const Interest& interest)
490{
491 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(outFace, interest);
492}
493
Junxiao Shid3c792f2014-01-30 00:46:13 -0700494static inline bool
495compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
496{
497 return a.getExpiry() < b.getExpiry();
498}
499
500void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000501Forwarder::setUnsatisfyTimer(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700502{
Junxiao Shi4846f372016-04-05 13:39:30 -0700503 pit::InRecordCollection::iterator lastExpiring =
504 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700505
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700506 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700507 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
508 if (lastExpiryFromNow <= time::seconds::zero()) {
509 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700510 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700511
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700512 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700513 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700514 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
515}
516
517void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000518Forwarder::setStragglerTimer(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
Eric Newberryf4056d02017-05-26 17:31:53 +0000519 ndn::optional<time::milliseconds> dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700520{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700521 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700522
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700523 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700524 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700525 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700526}
527
528void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000529Forwarder::cancelUnsatisfyAndStragglerTimer(pit::Entry& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700530{
Junxiao Shib9420cf2016-08-13 04:38:52 +0000531 scheduler::cancel(pitEntry.m_unsatisfyTimer);
532 scheduler::cancel(pitEntry.m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700533}
534
Junxiao Shia110f262014-10-12 12:35:20 -0700535static inline void
536insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
537 const pit::OutRecord& outRecord)
538{
539 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
540}
541
542void
543Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
Eric Newberryf4056d02017-05-26 17:31:53 +0000544 ndn::optional<time::milliseconds> dataFreshnessPeriod, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700545{
546 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000547 bool needDnl = true;
Junxiao Shia110f262014-10-12 12:35:20 -0700548 if (isSatisfied) {
Eric Newberryf4056d02017-05-26 17:31:53 +0000549 BOOST_ASSERT(dataFreshnessPeriod);
550 BOOST_ASSERT(*dataFreshnessPeriod >= time::milliseconds::zero());
Junxiao Shia110f262014-10-12 12:35:20 -0700551 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Eric Newberryf4056d02017-05-26 17:31:53 +0000552 *dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700553 }
554
555 if (!needDnl) {
556 return;
557 }
558
559 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000560 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700561 // insert all outgoing Nonces
562 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
563 std::for_each(outRecords.begin(), outRecords.end(),
564 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
565 }
566 else {
567 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700568 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700569 if (outRecord != pitEntry.getOutRecords().end()) {
570 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
571 }
572 }
573}
574
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800575} // namespace nfd