blob: b8bd076be542b8530b4ddc287542fb20acd47139 [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>
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 Shic041ca32014-02-25 20:01:15 -070038Forwarder::Forwarder()
Junxiao Shi9685cc52016-08-29 12:47:05 +000039 : m_unsolicitedDataPolicy(new fw::DefaultUnsolicitedDataPolicy())
Junxiao Shifbe8efe2016-08-22 16:02:30 +000040 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060041 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080042 , m_measurements(m_nameTree)
Junxiao Shif3c07812014-03-11 21:48:49 -070043 , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080044{
Junxiao Shif3c07812014-03-11 21:48:49 -070045 fw::installStrategies(*this);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000046
47 m_faceTable.afterAdd.connect([this] (Face& face) {
48 face.afterReceiveInterest.connect(
49 [this, &face] (const Interest& interest) {
50 this->startProcessInterest(face, interest);
51 });
52 face.afterReceiveData.connect(
53 [this, &face] (const Data& data) {
54 this->startProcessData(face, data);
55 });
56 face.afterReceiveNack.connect(
57 [this, &face] (const lp::Nack& nack) {
58 this->startProcessNack(face, nack);
59 });
60 });
61
62 m_faceTable.beforeRemove.connect([this] (Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000063 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000064 });
Alexander Afanasyev33b72772014-01-26 23:22:58 -080065}
66
Junxiao Shidcffdaa2016-07-26 02:23:56 +000067Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060068
Junxiao Shi0355e9f2015-09-02 07:24:53 -070069void
70Forwarder::startProcessInterest(Face& face, const Interest& interest)
71{
72 // check fields used by forwarding are well-formed
73 try {
74 if (interest.hasLink()) {
75 interest.getLink();
76 }
77 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -070078 catch (const tlv::Error&) {
Junxiao Shi0355e9f2015-09-02 07:24:53 -070079 NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() <<
80 " interest=" << interest.getName() << " malformed");
81 // It's safe to call interest.getName() because Name has been fully parsed
82 return;
83 }
84
85 this->onIncomingInterest(face, interest);
86}
87
88void
89Forwarder::startProcessData(Face& face, const Data& data)
90{
91 // check fields used by forwarding are well-formed
92 // (none needed)
93
94 this->onIncomingData(face, data);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060095}
96
Alexander Afanasyev33b72772014-01-26 23:22:58 -080097void
Junxiao Shi5e5e4452015-09-24 16:56:52 -070098Forwarder::startProcessNack(Face& face, const lp::Nack& nack)
99{
100 // check fields used by forwarding are well-formed
101 try {
102 if (nack.getInterest().hasLink()) {
103 nack.getInterest().getLink();
104 }
105 }
106 catch (const tlv::Error&) {
107 NFD_LOG_DEBUG("startProcessNack face=" << face.getId() <<
108 " nack=" << nack.getInterest().getName() <<
109 "~" << nack.getReason() << " malformed");
110 return;
111 }
112
113 this->onIncomingNack(face, nack);
114}
115
116void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700117Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
118{
119 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700120 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
121 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000122 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700123 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -0700124
Junxiao Shi88884492014-02-15 15:57:43 -0700125 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700126 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700127 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700128 if (isViolatingLocalhost) {
129 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
130 " interest=" << interest.getName() << " violates /localhost");
131 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700132 return;
133 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700134
Junxiao Shi330136a2016-03-10 04:53:08 -0700135 // detect duplicate Nonce with Dead Nonce List
136 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
137 if (hasDuplicateNonceInDnl) {
138 // goto Interest loop pipeline
139 this->onInterestLoop(inFace, interest);
140 return;
141 }
142
Junxiao Shid3c792f2014-01-30 00:46:13 -0700143 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700144 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700145
Junxiao Shi330136a2016-03-10 04:53:08 -0700146 // detect duplicate Nonce in PIT entry
Junxiao Shifef73e42016-03-29 14:15:05 -0700147 bool hasDuplicateNonceInPit = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace) !=
148 fw::DUPLICATE_NONCE_NONE;
Junxiao Shi330136a2016-03-10 04:53:08 -0700149 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700150 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700151 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700152 return;
153 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700154
Junxiao Shid3c792f2014-01-30 00:46:13 -0700155 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000156 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700157
Junxiao Shif3c07812014-03-11 21:48:49 -0700158 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700159 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600160 m_cs.find(interest,
161 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
162 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700163 }
mzhang4eab72492015-02-25 11:16:09 -0600164 else {
165 this->onContentStoreMiss(inFace, pitEntry, interest);
166 }
167}
Junxiao Shic041ca32014-02-25 20:01:15 -0700168
mzhang4eab72492015-02-25 11:16:09 -0600169void
Junxiao Shi330136a2016-03-10 04:53:08 -0700170Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700171{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700172 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700173 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700174 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
175 " interest=" << interest.getName() <<
176 " drop");
177 return;
178 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700179
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700180 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
181 " interest=" << interest.getName() <<
182 " send-Nack-duplicate");
183
184 // send Nack with reason=DUPLICATE
185 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
186 lp::Nack nack(interest);
187 nack.setReason(lp::NackReason::DUPLICATE);
188 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700189}
190
191void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000192Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
mzhang4eab72492015-02-25 11:16:09 -0600193 const Interest& interest)
194{
195 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
196
Junxiao Shi4846f372016-04-05 13:39:30 -0700197 // insert in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000198 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700199
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700200 // set PIT unsatisfy timer
201 this->setUnsatisfyTimer(pitEntry);
202
Junxiao Shie342e8d2016-09-18 16:48:00 +0000203 // has NextHopFaceId?
204 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
205 if (nextHopTag != nullptr) {
206 // chosen NextHop face exists?
207 Face* nextHopFace = m_faceTable.get(*nextHopTag);
208 if (nextHopFace != nullptr) {
209 // go to outgoing Interest pipeline
210 this->onOutgoingInterest(pitEntry, *nextHopFace);
211 }
212 return;
213 }
214
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000215 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000216 this->dispatchToStrategy(*pitEntry,
217 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700218}
219
220void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000221Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
222 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());
mzhang4eab72492015-02-25 11:16:09 -0600225
Junxiao Shicde37ad2015-12-24 01:02:05 -0700226 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600227 // XXX should we lookup PIT for other Interests that also match csMatch?
228
229 // set PIT straggler timer
230 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
231
232 // goto outgoing Data pipeline
233 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
234}
235
Junxiao Shid3c792f2014-01-30 00:46:13 -0700236void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000237Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700238 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700239{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700240 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700241 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
242 return;
243 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700244 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
245 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700246
Junxiao Shi57f0f312014-03-16 11:52:20 -0700247 // scope control
Junxiao Shifef73e42016-03-29 14:15:05 -0700248 if (fw::violatesScope(*pitEntry, outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700249 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700250 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700251 return;
252 }
253
Junxiao Shid3c792f2014-01-30 00:46:13 -0700254 // pick Interest
Junxiao Shi891f47b2016-06-20 00:02:11 +0000255 // The outgoing Interest picked is the last incoming Interest that does not come from outFace.
256 // If all in-records come from outFace, it's fine to pick that.
257 // This happens when there's only one in-record that comes from outFace.
258 // The legit use is for vehicular network; otherwise, strategy shouldn't send to the sole inFace.
Junxiao Shi4846f372016-04-05 13:39:30 -0700259 pit::InRecordCollection::iterator pickedInRecord = std::max_element(
Junxiao Shi891f47b2016-06-20 00:02:11 +0000260 pitEntry->in_begin(), pitEntry->in_end(),
261 [&outFace] (const pit::InRecord& a, const pit::InRecord& b) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000262 bool isOutFaceA = &a.getFace() == &outFace;
263 bool isOutFaceB = &b.getFace() == &outFace;
Junxiao Shi891f47b2016-06-20 00:02:11 +0000264 return (isOutFaceA > isOutFaceB) ||
265 (isOutFaceA == isOutFaceB && a.getLastRenewed() < b.getLastRenewed());
266 });
Junxiao Shi4846f372016-04-05 13:39:30 -0700267 BOOST_ASSERT(pickedInRecord != pitEntry->in_end());
268 auto interest = const_pointer_cast<Interest>(pickedInRecord->getInterest().shared_from_this());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700269
270 if (wantNewNonce) {
271 interest = make_shared<Interest>(*interest);
Davide Pesavento5f47aa62016-10-07 22:09:09 +0200272 static std::uniform_int_distribution<uint32_t> dist;
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700273 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700274 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700275
Junxiao Shi4846f372016-04-05 13:39:30 -0700276 // insert out-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000277 pitEntry->insertOrUpdateOutRecord(outFace, *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700278
Junxiao Shid3c792f2014-01-30 00:46:13 -0700279 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700280 outFace.sendInterest(*interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700281 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700282}
283
284void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000285Forwarder::onInterestReject(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700286{
Junxiao Shifef73e42016-03-29 14:15:05 -0700287 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700288 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
289 " cannot reject forwarded Interest");
290 return;
291 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700292 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700293
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700294 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000295 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700296
Junxiao Shid3c792f2014-01-30 00:46:13 -0700297 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700298 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700299}
300
301void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000302Forwarder::onInterestUnsatisfied(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700303{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700304 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
305
Junxiao Shid3c792f2014-01-30 00:46:13 -0700306 // invoke PIT unsatisfied callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000307 this->dispatchToStrategy(*pitEntry,
308 [&] (fw::Strategy& strategy) { strategy.beforeExpirePendingInterest(pitEntry); });
Junxiao Shic041ca32014-02-25 20:01:15 -0700309
Junxiao Shia110f262014-10-12 12:35:20 -0700310 // goto Interest Finalize pipeline
311 this->onInterestFinalize(pitEntry, false);
312}
313
314void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000315Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
316 time::milliseconds dataFreshnessPeriod)
Junxiao Shia110f262014-10-12 12:35:20 -0700317{
318 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
319 (isSatisfied ? " satisfied" : " unsatisfied"));
320
321 // Dead Nonce List insert if necessary
322 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
323
Junxiao Shif3c07812014-03-11 21:48:49 -0700324 // PIT delete
Junxiao Shib9420cf2016-08-13 04:38:52 +0000325 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000326 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700327}
328
329void
330Forwarder::onIncomingData(Face& inFace, const Data& data)
331{
332 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700333 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000334 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700335 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700336
Junxiao Shi88884492014-02-15 15:57:43 -0700337 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700338 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700339 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700340 if (isViolatingLocalhost) {
341 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
342 " data=" << data.getName() << " violates /localhost");
343 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700344 return;
345 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700346
Junxiao Shid3c792f2014-01-30 00:46:13 -0700347 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700348 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
349 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700350 // goto Data unsolicited pipeline
351 this->onDataUnsolicited(inFace, data);
352 return;
353 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700354
Junxiao Shid3c792f2014-01-30 00:46:13 -0700355 // CS insert
356 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700357
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700358 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700359 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700360 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700361 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700362 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700363
Junxiao Shid3c792f2014-01-30 00:46:13 -0700364 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000365 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700366
Junxiao Shid3c792f2014-01-30 00:46:13 -0700367 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700368 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
369 if (inRecord.getExpiry() > now) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000370 pendingDownstreams.insert(&inRecord.getFace());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700371 }
372 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700373
Junxiao Shid938a6b2014-05-11 23:40:29 -0700374 // invoke PIT satisfy callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000375 this->dispatchToStrategy(*pitEntry,
376 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700377
Junxiao Shi4846f372016-04-05 13:39:30 -0700378 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700379 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
380
Junxiao Shid3c792f2014-01-30 00:46:13 -0700381 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700382 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700383 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700384
Junxiao Shid3c792f2014-01-30 00:46:13 -0700385 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700386 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700387 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700388
Junxiao Shid3c792f2014-01-30 00:46:13 -0700389 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700390 for (Face* pendingDownstream : pendingDownstreams) {
391 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700392 continue;
393 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700394 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700395 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700396 }
397}
398
399void
400Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
401{
402 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000403 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
404 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700405 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700406 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700407 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700408
Junxiao Shif3c07812014-03-11 21:48:49 -0700409 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
410 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000411 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700412}
413
414void
415Forwarder::onOutgoingData(const Data& data, Face& outFace)
416{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700417 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700418 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
419 return;
420 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700421 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
422
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700423 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700424 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700425 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700426 if (isViolatingLocalhost) {
427 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
428 " data=" << data.getName() << " violates /localhost");
429 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700430 return;
431 }
432
Junxiao Shif3c07812014-03-11 21:48:49 -0700433 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700434
Junxiao Shid3c792f2014-01-30 00:46:13 -0700435 // send Data
436 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700437 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700438}
439
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700440void
441Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
442{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000443 // receive Nack
444 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700445 ++m_counters.nInNacks;
446
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700447 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700448 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700449 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
450 " nack=" << nack.getInterest().getName() <<
451 "~" << nack.getReason() << " face-is-multi-access");
452 return;
453 }
454
455 // PIT match
456 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
457 // if no PIT entry found, drop
458 if (pitEntry == nullptr) {
459 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
460 " nack=" << nack.getInterest().getName() <<
461 "~" << nack.getReason() << " no-PIT-entry");
462 return;
463 }
464
465 // has out-record?
466 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
467 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700468 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700469 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
470 " nack=" << nack.getInterest().getName() <<
471 "~" << nack.getReason() << " no-out-record");
472 return;
473 }
474
475 // if out-record has different Nonce, drop
476 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
477 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
478 " nack=" << nack.getInterest().getName() <<
479 "~" << nack.getReason() << " wrong-Nonce " <<
480 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
481 return;
482 }
483
484 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
485 " nack=" << nack.getInterest().getName() <<
486 "~" << nack.getReason() << " OK");
487
488 // record Nack on out-record
489 outRecord->setIncomingNack(nack);
490
491 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000492 this->dispatchToStrategy(*pitEntry,
493 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700494}
495
496void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000497Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700498 const lp::NackHeader& nack)
499{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700500 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700501 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
502 " nack=" << pitEntry->getInterest().getName() <<
503 "~" << nack.getReason() << " no-in-record");
504 return;
505 }
506
507 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700508 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700509
510 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700511 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700512 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
513 " nack=" << pitEntry->getInterest().getName() <<
514 "~" << nack.getReason() << " no-in-record");
515 return;
516 }
517
518 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700519 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700520 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
521 " nack=" << pitEntry->getInterest().getName() <<
522 "~" << nack.getReason() << " face-is-multi-access");
523 return;
524 }
525
526 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
527 " nack=" << pitEntry->getInterest().getName() <<
528 "~" << nack.getReason() << " OK");
529
530 // create Nack packet with the Interest from in-record
531 lp::Nack nackPkt(inRecord->getInterest());
532 nackPkt.setHeader(nack);
533
534 // erase in-record
535 pitEntry->deleteInRecord(outFace);
536
537 // send Nack on face
538 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700539 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700540}
541
Junxiao Shid3c792f2014-01-30 00:46:13 -0700542static inline bool
543compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
544{
545 return a.getExpiry() < b.getExpiry();
546}
547
548void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000549Forwarder::setUnsatisfyTimer(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700550{
Junxiao Shi4846f372016-04-05 13:39:30 -0700551 pit::InRecordCollection::iterator lastExpiring =
552 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700553
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700554 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700555 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
556 if (lastExpiryFromNow <= time::seconds::zero()) {
557 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700558 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700559
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700560 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700561 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700562 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
563}
564
565void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000566Forwarder::setStragglerTimer(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
567 time::milliseconds dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700568{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700569 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700570
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700571 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700572 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700573 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700574}
575
576void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000577Forwarder::cancelUnsatisfyAndStragglerTimer(pit::Entry& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700578{
Junxiao Shib9420cf2016-08-13 04:38:52 +0000579 scheduler::cancel(pitEntry.m_unsatisfyTimer);
580 scheduler::cancel(pitEntry.m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700581}
582
Junxiao Shia110f262014-10-12 12:35:20 -0700583static inline void
584insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
585 const pit::OutRecord& outRecord)
586{
587 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
588}
589
590void
591Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000592 time::milliseconds dataFreshnessPeriod, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700593{
594 // need Dead Nonce List insert?
595 bool needDnl = false;
596 if (isSatisfied) {
597 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
598 // Data never becomes stale if it doesn't have FreshnessPeriod field
599 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
600 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
601 }
602 else {
603 needDnl = true;
604 }
605
606 if (!needDnl) {
607 return;
608 }
609
610 // Dead Nonce List insert
611 if (upstream == 0) {
612 // insert all outgoing Nonces
613 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
614 std::for_each(outRecords.begin(), outRecords.end(),
615 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
616 }
617 else {
618 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700619 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700620 if (outRecord != pitEntry.getOutRecords().end()) {
621 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
622 }
623 }
624}
625
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800626} // namespace nfd