blob: f0186f500cc94d89010952a436ccce93257bfa69 [file] [log] [blame]
Alexander Afanasyev33b72772014-01-26 23:22:58 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi330136a2016-03-10 04:53:08 -07003 * Copyright (c) 2014-2016, 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 Shifef73e42016-03-29 14:15:05 -070027#include "pit-algorithm.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060028#include "core/logger.hpp"
Junxiao Shiaf6569a2014-06-14 00:01:34 -070029#include "core/random.hpp"
Junxiao Shifaf3eb02015-02-16 10:50:36 -070030#include "strategy.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>
Junxiao Shiaf6569a2014-06-14 00:01:34 -070033#include <boost/random/uniform_int_distribution.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080034
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080035namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080036
Junxiao Shi8c8d2182014-01-30 22:33:00 -070037NFD_LOG_INIT("Forwarder");
38
Junxiao Shic041ca32014-02-25 20:01:15 -070039Forwarder::Forwarder()
Junxiao Shi9685cc52016-08-29 12:47:05 +000040 : m_unsolicitedDataPolicy(new fw::DefaultUnsolicitedDataPolicy())
Junxiao Shifbe8efe2016-08-22 16:02:30 +000041 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060042 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080043 , m_measurements(m_nameTree)
Junxiao Shif3c07812014-03-11 21:48:49 -070044 , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080045{
Junxiao Shif3c07812014-03-11 21:48:49 -070046 fw::installStrategies(*this);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000047
48 m_faceTable.afterAdd.connect([this] (Face& face) {
49 face.afterReceiveInterest.connect(
50 [this, &face] (const Interest& interest) {
51 this->startProcessInterest(face, interest);
52 });
53 face.afterReceiveData.connect(
54 [this, &face] (const Data& data) {
55 this->startProcessData(face, data);
56 });
57 face.afterReceiveNack.connect(
58 [this, &face] (const lp::Nack& nack) {
59 this->startProcessNack(face, nack);
60 });
61 });
62
63 m_faceTable.beforeRemove.connect([this] (Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000064 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000065 });
Alexander Afanasyev33b72772014-01-26 23:22:58 -080066}
67
Junxiao Shidcffdaa2016-07-26 02:23:56 +000068Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060069
Junxiao Shi0355e9f2015-09-02 07:24:53 -070070void
71Forwarder::startProcessInterest(Face& face, const Interest& interest)
72{
73 // check fields used by forwarding are well-formed
74 try {
75 if (interest.hasLink()) {
76 interest.getLink();
77 }
78 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -070079 catch (const tlv::Error&) {
Junxiao Shi0355e9f2015-09-02 07:24:53 -070080 NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() <<
81 " interest=" << interest.getName() << " malformed");
82 // It's safe to call interest.getName() because Name has been fully parsed
83 return;
84 }
85
86 this->onIncomingInterest(face, interest);
87}
88
89void
90Forwarder::startProcessData(Face& face, const Data& data)
91{
92 // check fields used by forwarding are well-formed
93 // (none needed)
94
95 this->onIncomingData(face, data);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060096}
97
Alexander Afanasyev33b72772014-01-26 23:22:58 -080098void
Junxiao Shi5e5e4452015-09-24 16:56:52 -070099Forwarder::startProcessNack(Face& face, const lp::Nack& nack)
100{
101 // check fields used by forwarding are well-formed
102 try {
103 if (nack.getInterest().hasLink()) {
104 nack.getInterest().getLink();
105 }
106 }
107 catch (const tlv::Error&) {
108 NFD_LOG_DEBUG("startProcessNack face=" << face.getId() <<
109 " nack=" << nack.getInterest().getName() <<
110 "~" << nack.getReason() << " malformed");
111 return;
112 }
113
114 this->onIncomingNack(face, nack);
115}
116
117void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700118Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
119{
120 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700121 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
122 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000123 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700124 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -0700125
Junxiao Shi88884492014-02-15 15:57:43 -0700126 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700127 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700128 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700129 if (isViolatingLocalhost) {
130 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
131 " interest=" << interest.getName() << " violates /localhost");
132 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700133 return;
134 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700135
Junxiao Shi330136a2016-03-10 04:53:08 -0700136 // detect duplicate Nonce with Dead Nonce List
137 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
138 if (hasDuplicateNonceInDnl) {
139 // goto Interest loop pipeline
140 this->onInterestLoop(inFace, interest);
141 return;
142 }
143
Junxiao Shid3c792f2014-01-30 00:46:13 -0700144 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700145 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700146
Junxiao Shi330136a2016-03-10 04:53:08 -0700147 // detect duplicate Nonce in PIT entry
Junxiao Shifef73e42016-03-29 14:15:05 -0700148 bool hasDuplicateNonceInPit = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace) !=
149 fw::DUPLICATE_NONCE_NONE;
Junxiao Shi330136a2016-03-10 04:53:08 -0700150 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700151 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700152 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700153 return;
154 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700155
Junxiao Shid3c792f2014-01-30 00:46:13 -0700156 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000157 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700158
Junxiao Shif3c07812014-03-11 21:48:49 -0700159 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700160 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600161 m_cs.find(interest,
162 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
163 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700164 }
mzhang4eab72492015-02-25 11:16:09 -0600165 else {
166 this->onContentStoreMiss(inFace, pitEntry, interest);
167 }
168}
Junxiao Shic041ca32014-02-25 20:01:15 -0700169
mzhang4eab72492015-02-25 11:16:09 -0600170void
Junxiao Shi330136a2016-03-10 04:53:08 -0700171Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700172{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700173 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700174 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700175 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
176 " interest=" << interest.getName() <<
177 " drop");
178 return;
179 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700180
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700181 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
182 " interest=" << interest.getName() <<
183 " send-Nack-duplicate");
184
185 // send Nack with reason=DUPLICATE
186 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
187 lp::Nack nack(interest);
188 nack.setReason(lp::NackReason::DUPLICATE);
189 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700190}
191
192void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000193Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
mzhang4eab72492015-02-25 11:16:09 -0600194 const Interest& interest)
195{
196 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
197
Junxiao Shi4846f372016-04-05 13:39:30 -0700198 // insert in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000199 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700200
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700201 // set PIT unsatisfy timer
202 this->setUnsatisfyTimer(pitEntry);
203
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000204 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000205 this->dispatchToStrategy(*pitEntry,
206 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700207}
208
209void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000210Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
211 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600212{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500213 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600214
Junxiao Shicde37ad2015-12-24 01:02:05 -0700215 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600216 // XXX should we lookup PIT for other Interests that also match csMatch?
217
218 // set PIT straggler timer
219 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
220
221 // goto outgoing Data pipeline
222 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
223}
224
Junxiao Shid3c792f2014-01-30 00:46:13 -0700225void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000226Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700227 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700228{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700229 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700230 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
231 return;
232 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700233 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
234 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700235
Junxiao Shi57f0f312014-03-16 11:52:20 -0700236 // scope control
Junxiao Shifef73e42016-03-29 14:15:05 -0700237 if (fw::violatesScope(*pitEntry, outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700238 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700239 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700240 return;
241 }
242
Junxiao Shid3c792f2014-01-30 00:46:13 -0700243 // pick Interest
Junxiao Shi891f47b2016-06-20 00:02:11 +0000244 // The outgoing Interest picked is the last incoming Interest that does not come from outFace.
245 // If all in-records come from outFace, it's fine to pick that.
246 // This happens when there's only one in-record that comes from outFace.
247 // The legit use is for vehicular network; otherwise, strategy shouldn't send to the sole inFace.
Junxiao Shi4846f372016-04-05 13:39:30 -0700248 pit::InRecordCollection::iterator pickedInRecord = std::max_element(
Junxiao Shi891f47b2016-06-20 00:02:11 +0000249 pitEntry->in_begin(), pitEntry->in_end(),
250 [&outFace] (const pit::InRecord& a, const pit::InRecord& b) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000251 bool isOutFaceA = &a.getFace() == &outFace;
252 bool isOutFaceB = &b.getFace() == &outFace;
Junxiao Shi891f47b2016-06-20 00:02:11 +0000253 return (isOutFaceA > isOutFaceB) ||
254 (isOutFaceA == isOutFaceB && a.getLastRenewed() < b.getLastRenewed());
255 });
Junxiao Shi4846f372016-04-05 13:39:30 -0700256 BOOST_ASSERT(pickedInRecord != pitEntry->in_end());
257 auto interest = const_pointer_cast<Interest>(pickedInRecord->getInterest().shared_from_this());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700258
259 if (wantNewNonce) {
260 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700261 static boost::random::uniform_int_distribution<uint32_t> dist;
262 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700263 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700264
Junxiao Shi4846f372016-04-05 13:39:30 -0700265 // insert out-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000266 pitEntry->insertOrUpdateOutRecord(outFace, *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700267
Junxiao Shid3c792f2014-01-30 00:46:13 -0700268 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700269 outFace.sendInterest(*interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700270 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700271}
272
273void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000274Forwarder::onInterestReject(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700275{
Junxiao Shifef73e42016-03-29 14:15:05 -0700276 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700277 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
278 " cannot reject forwarded Interest");
279 return;
280 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700281 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700282
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700283 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000284 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700285
Junxiao Shid3c792f2014-01-30 00:46:13 -0700286 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700287 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700288}
289
290void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000291Forwarder::onInterestUnsatisfied(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700292{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700293 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
294
Junxiao Shid3c792f2014-01-30 00:46:13 -0700295 // invoke PIT unsatisfied callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000296 this->dispatchToStrategy(*pitEntry,
297 [&] (fw::Strategy& strategy) { strategy.beforeExpirePendingInterest(pitEntry); });
Junxiao Shic041ca32014-02-25 20:01:15 -0700298
Junxiao Shia110f262014-10-12 12:35:20 -0700299 // goto Interest Finalize pipeline
300 this->onInterestFinalize(pitEntry, false);
301}
302
303void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000304Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
305 time::milliseconds dataFreshnessPeriod)
Junxiao Shia110f262014-10-12 12:35:20 -0700306{
307 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
308 (isSatisfied ? " satisfied" : " unsatisfied"));
309
310 // Dead Nonce List insert if necessary
311 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
312
Junxiao Shif3c07812014-03-11 21:48:49 -0700313 // PIT delete
Junxiao Shib9420cf2016-08-13 04:38:52 +0000314 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000315 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700316}
317
318void
319Forwarder::onIncomingData(Face& inFace, const Data& data)
320{
321 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700322 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000323 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700324 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700325
Junxiao Shi88884492014-02-15 15:57:43 -0700326 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700327 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700328 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700329 if (isViolatingLocalhost) {
330 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
331 " data=" << data.getName() << " violates /localhost");
332 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700333 return;
334 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700335
Junxiao Shid3c792f2014-01-30 00:46:13 -0700336 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700337 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
338 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700339 // goto Data unsolicited pipeline
340 this->onDataUnsolicited(inFace, data);
341 return;
342 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700343
Junxiao Shid3c792f2014-01-30 00:46:13 -0700344 // CS insert
345 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700346
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700347 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700348 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700349 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700350 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700351 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700352
Junxiao Shid3c792f2014-01-30 00:46:13 -0700353 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000354 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700355
Junxiao Shid3c792f2014-01-30 00:46:13 -0700356 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700357 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
358 if (inRecord.getExpiry() > now) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000359 pendingDownstreams.insert(&inRecord.getFace());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700360 }
361 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700362
Junxiao Shid938a6b2014-05-11 23:40:29 -0700363 // invoke PIT satisfy callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000364 this->dispatchToStrategy(*pitEntry,
365 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700366
Junxiao Shi4846f372016-04-05 13:39:30 -0700367 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700368 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
369
Junxiao Shid3c792f2014-01-30 00:46:13 -0700370 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700371 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700372 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700373
Junxiao Shid3c792f2014-01-30 00:46:13 -0700374 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700375 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700376 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700377
Junxiao Shid3c792f2014-01-30 00:46:13 -0700378 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700379 for (Face* pendingDownstream : pendingDownstreams) {
380 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700381 continue;
382 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700383 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700384 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700385 }
386}
387
388void
389Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
390{
391 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000392 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
393 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700394 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700395 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700396 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700397
Junxiao Shif3c07812014-03-11 21:48:49 -0700398 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
399 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000400 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700401}
402
403void
404Forwarder::onOutgoingData(const Data& data, Face& outFace)
405{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700406 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700407 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
408 return;
409 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700410 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
411
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700412 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700413 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700414 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700415 if (isViolatingLocalhost) {
416 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
417 " data=" << data.getName() << " violates /localhost");
418 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700419 return;
420 }
421
Junxiao Shif3c07812014-03-11 21:48:49 -0700422 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700423
Junxiao Shid3c792f2014-01-30 00:46:13 -0700424 // send Data
425 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700426 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700427}
428
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700429void
430Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
431{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000432 // receive Nack
433 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700434 ++m_counters.nInNacks;
435
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700436 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700437 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700438 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
439 " nack=" << nack.getInterest().getName() <<
440 "~" << nack.getReason() << " face-is-multi-access");
441 return;
442 }
443
444 // PIT match
445 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
446 // if no PIT entry found, drop
447 if (pitEntry == nullptr) {
448 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
449 " nack=" << nack.getInterest().getName() <<
450 "~" << nack.getReason() << " no-PIT-entry");
451 return;
452 }
453
454 // has out-record?
455 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
456 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700457 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700458 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
459 " nack=" << nack.getInterest().getName() <<
460 "~" << nack.getReason() << " no-out-record");
461 return;
462 }
463
464 // if out-record has different Nonce, drop
465 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
466 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
467 " nack=" << nack.getInterest().getName() <<
468 "~" << nack.getReason() << " wrong-Nonce " <<
469 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
470 return;
471 }
472
473 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
474 " nack=" << nack.getInterest().getName() <<
475 "~" << nack.getReason() << " OK");
476
477 // record Nack on out-record
478 outRecord->setIncomingNack(nack);
479
480 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000481 this->dispatchToStrategy(*pitEntry,
482 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700483}
484
485void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000486Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700487 const lp::NackHeader& nack)
488{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700489 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700490 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
491 " nack=" << pitEntry->getInterest().getName() <<
492 "~" << nack.getReason() << " no-in-record");
493 return;
494 }
495
496 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700497 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700498
499 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700500 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700501 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
502 " nack=" << pitEntry->getInterest().getName() <<
503 "~" << nack.getReason() << " no-in-record");
504 return;
505 }
506
507 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700508 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700509 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
510 " nack=" << pitEntry->getInterest().getName() <<
511 "~" << nack.getReason() << " face-is-multi-access");
512 return;
513 }
514
515 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
516 " nack=" << pitEntry->getInterest().getName() <<
517 "~" << nack.getReason() << " OK");
518
519 // create Nack packet with the Interest from in-record
520 lp::Nack nackPkt(inRecord->getInterest());
521 nackPkt.setHeader(nack);
522
523 // erase in-record
524 pitEntry->deleteInRecord(outFace);
525
526 // send Nack on face
527 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700528 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700529}
530
Junxiao Shid3c792f2014-01-30 00:46:13 -0700531static inline bool
532compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
533{
534 return a.getExpiry() < b.getExpiry();
535}
536
537void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000538Forwarder::setUnsatisfyTimer(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700539{
Junxiao Shi4846f372016-04-05 13:39:30 -0700540 pit::InRecordCollection::iterator lastExpiring =
541 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700542
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700543 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700544 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
545 if (lastExpiryFromNow <= time::seconds::zero()) {
546 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700547 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700548
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700549 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700550 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700551 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
552}
553
554void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000555Forwarder::setStragglerTimer(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
556 time::milliseconds dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700557{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700558 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700559
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700560 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700561 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700562 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700563}
564
565void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000566Forwarder::cancelUnsatisfyAndStragglerTimer(pit::Entry& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700567{
Junxiao Shib9420cf2016-08-13 04:38:52 +0000568 scheduler::cancel(pitEntry.m_unsatisfyTimer);
569 scheduler::cancel(pitEntry.m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700570}
571
Junxiao Shia110f262014-10-12 12:35:20 -0700572static inline void
573insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
574 const pit::OutRecord& outRecord)
575{
576 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
577}
578
579void
580Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000581 time::milliseconds dataFreshnessPeriod, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700582{
583 // need Dead Nonce List insert?
584 bool needDnl = false;
585 if (isSatisfied) {
586 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
587 // Data never becomes stale if it doesn't have FreshnessPeriod field
588 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
589 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
590 }
591 else {
592 needDnl = true;
593 }
594
595 if (!needDnl) {
596 return;
597 }
598
599 // Dead Nonce List insert
600 if (upstream == 0) {
601 // insert all outgoing Nonces
602 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
603 std::for_each(outRecords.begin(), outRecords.end(),
604 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
605 }
606 else {
607 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700608 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700609 if (outRecord != pitEntry.getOutRecords().end()) {
610 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
611 }
612 }
613}
614
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800615} // namespace nfd