blob: 2fbc93c846b5cee728adf4396a3d6c887c2c0081 [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 Shiaf6569a2014-06-14 00:01:34 -070032#include <boost/random/uniform_int_distribution.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 Shif3c07812014-03-11 21:48:49 -070038using fw::Strategy;
39
Junxiao Shic041ca32014-02-25 20:01:15 -070040Forwarder::Forwarder()
Junxiao Shidcffdaa2016-07-26 02:23:56 +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
157 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
mzhang4eab72492015-02-25 11:16:09 -0600193Forwarder::onContentStoreMiss(const Face& inFace,
194 shared_ptr<pit::Entry> pitEntry,
195 const Interest& interest)
196{
197 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
198
199 shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this());
Junxiao Shi4846f372016-04-05 13:39:30 -0700200 // insert in-record
mzhang4eab72492015-02-25 11:16:09 -0600201 pitEntry->insertOrUpdateInRecord(face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700202
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700203 // set PIT unsatisfy timer
204 this->setUnsatisfyTimer(pitEntry);
205
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000206 // dispatch to strategy: after incoming Interest
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000207 this->dispatchToStrategy(pitEntry,
Junxiao Shi8d843142016-07-11 22:42:42 +0000208 [&] (fw::Strategy* strategy) { strategy->afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700209}
210
211void
mzhang4eab72492015-02-25 11:16:09 -0600212Forwarder::onContentStoreHit(const Face& inFace,
213 shared_ptr<pit::Entry> pitEntry,
214 const Interest& interest,
215 const Data& data)
216{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500217 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600218
Junxiao Shicde37ad2015-12-24 01:02:05 -0700219 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600220 // XXX should we lookup PIT for other Interests that also match csMatch?
221
222 // set PIT straggler timer
223 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
224
225 // goto outgoing Data pipeline
226 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
227}
228
Junxiao Shid3c792f2014-01-30 00:46:13 -0700229void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700230Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
231 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700232{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700233 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700234 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
235 return;
236 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700237 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
238 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700239
Junxiao Shi57f0f312014-03-16 11:52:20 -0700240 // scope control
Junxiao Shifef73e42016-03-29 14:15:05 -0700241 if (fw::violatesScope(*pitEntry, outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700242 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700243 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700244 return;
245 }
246
Junxiao Shid3c792f2014-01-30 00:46:13 -0700247 // pick Interest
Junxiao Shi891f47b2016-06-20 00:02:11 +0000248 // The outgoing Interest picked is the last incoming Interest that does not come from outFace.
249 // If all in-records come from outFace, it's fine to pick that.
250 // This happens when there's only one in-record that comes from outFace.
251 // The legit use is for vehicular network; otherwise, strategy shouldn't send to the sole inFace.
Junxiao Shi4846f372016-04-05 13:39:30 -0700252 pit::InRecordCollection::iterator pickedInRecord = std::max_element(
Junxiao Shi891f47b2016-06-20 00:02:11 +0000253 pitEntry->in_begin(), pitEntry->in_end(),
254 [&outFace] (const pit::InRecord& a, const pit::InRecord& b) {
255 bool isOutFaceA = a.getFace().get() == &outFace;
256 bool isOutFaceB = b.getFace().get() == &outFace;
257 return (isOutFaceA > isOutFaceB) ||
258 (isOutFaceA == isOutFaceB && a.getLastRenewed() < b.getLastRenewed());
259 });
Junxiao Shi4846f372016-04-05 13:39:30 -0700260 BOOST_ASSERT(pickedInRecord != pitEntry->in_end());
261 auto interest = const_pointer_cast<Interest>(pickedInRecord->getInterest().shared_from_this());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700262
263 if (wantNewNonce) {
264 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700265 static boost::random::uniform_int_distribution<uint32_t> dist;
266 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700267 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700268
Junxiao Shi4846f372016-04-05 13:39:30 -0700269 // insert out-record
Junxiao Shid938a6b2014-05-11 23:40:29 -0700270 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700271
Junxiao Shid3c792f2014-01-30 00:46:13 -0700272 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700273 outFace.sendInterest(*interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700274 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700275}
276
277void
Junxiao Shi09498f02014-02-26 19:41:08 -0700278Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700279{
Junxiao Shifef73e42016-03-29 14:15:05 -0700280 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700281 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
282 " cannot reject forwarded Interest");
283 return;
284 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700285 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700286
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700287 // cancel unsatisfy & straggler timer
288 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
289
Junxiao Shid3c792f2014-01-30 00:46:13 -0700290 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700291 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700292}
293
294void
295Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
296{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700297 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
298
Junxiao Shid3c792f2014-01-30 00:46:13 -0700299 // invoke PIT unsatisfied callback
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000300 this->dispatchToStrategy(pitEntry,
301 [&] (fw::Strategy* strategy) { strategy->beforeExpirePendingInterest(pitEntry); });
Junxiao Shic041ca32014-02-25 20:01:15 -0700302
Junxiao Shia110f262014-10-12 12:35:20 -0700303 // goto Interest Finalize pipeline
304 this->onInterestFinalize(pitEntry, false);
305}
306
307void
308Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
309 const time::milliseconds& dataFreshnessPeriod)
310{
311 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
312 (isSatisfied ? " satisfied" : " unsatisfied"));
313
314 // Dead Nonce List insert if necessary
315 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
316
Junxiao Shif3c07812014-03-11 21:48:49 -0700317 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700318 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600319 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700320}
321
322void
323Forwarder::onIncomingData(Face& inFace, const Data& data)
324{
325 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700326 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000327 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700328 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700329
Junxiao Shi88884492014-02-15 15:57:43 -0700330 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700331 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700332 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700333 if (isViolatingLocalhost) {
334 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
335 " data=" << data.getName() << " violates /localhost");
336 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700337 return;
338 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700339
Junxiao Shid3c792f2014-01-30 00:46:13 -0700340 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700341 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
342 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700343 // goto Data unsolicited pipeline
344 this->onDataUnsolicited(inFace, data);
345 return;
346 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700347
Junxiao Shid3c792f2014-01-30 00:46:13 -0700348 // CS insert
349 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700350
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700351 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700352 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700353 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700354 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700355 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700356
Junxiao Shid3c792f2014-01-30 00:46:13 -0700357 // cancel unsatisfy & straggler timer
358 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700359
Junxiao Shid3c792f2014-01-30 00:46:13 -0700360 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700361 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
362 if (inRecord.getExpiry() > now) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700363 pendingDownstreams.insert(inRecord.getFace().get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700364 }
365 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700366
Junxiao Shid938a6b2014-05-11 23:40:29 -0700367 // invoke PIT satisfy callback
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000368 this->dispatchToStrategy(pitEntry,
369 [&] (fw::Strategy* strategy) { strategy->beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700370
Junxiao Shi4846f372016-04-05 13:39:30 -0700371 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700372 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
373
Junxiao Shid3c792f2014-01-30 00:46:13 -0700374 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700375 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700376 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700377
Junxiao Shid3c792f2014-01-30 00:46:13 -0700378 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700379 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700380 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700381
Junxiao Shid3c792f2014-01-30 00:46:13 -0700382 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700383 for (Face* pendingDownstream : pendingDownstreams) {
384 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700385 continue;
386 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700387 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700388 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700389 }
390}
391
392void
393Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
394{
395 // accept to cache?
Junxiao Shicde37ad2015-12-24 01:02:05 -0700396 bool acceptToCache = inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700397 if (acceptToCache) {
398 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700399 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700400 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700401
Junxiao Shif3c07812014-03-11 21:48:49 -0700402 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
403 " data=" << data.getName() <<
404 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700405}
406
407void
408Forwarder::onOutgoingData(const Data& data, Face& outFace)
409{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700410 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700411 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
412 return;
413 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700414 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
415
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700416 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700417 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700418 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700419 if (isViolatingLocalhost) {
420 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
421 " data=" << data.getName() << " violates /localhost");
422 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700423 return;
424 }
425
Junxiao Shif3c07812014-03-11 21:48:49 -0700426 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700427
Junxiao Shid3c792f2014-01-30 00:46:13 -0700428 // send Data
429 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700430 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700431}
432
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700433void
434Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
435{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000436 // receive Nack
437 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700438 ++m_counters.nInNacks;
439
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700440 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700441 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700442 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
443 " nack=" << nack.getInterest().getName() <<
444 "~" << nack.getReason() << " face-is-multi-access");
445 return;
446 }
447
448 // PIT match
449 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
450 // if no PIT entry found, drop
451 if (pitEntry == nullptr) {
452 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
453 " nack=" << nack.getInterest().getName() <<
454 "~" << nack.getReason() << " no-PIT-entry");
455 return;
456 }
457
458 // has out-record?
459 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
460 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700461 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700462 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
463 " nack=" << nack.getInterest().getName() <<
464 "~" << nack.getReason() << " no-out-record");
465 return;
466 }
467
468 // if out-record has different Nonce, drop
469 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
470 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
471 " nack=" << nack.getInterest().getName() <<
472 "~" << nack.getReason() << " wrong-Nonce " <<
473 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
474 return;
475 }
476
477 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
478 " nack=" << nack.getInterest().getName() <<
479 "~" << nack.getReason() << " OK");
480
481 // record Nack on out-record
482 outRecord->setIncomingNack(nack);
483
484 // trigger strategy: after receive NACK
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000485 this->dispatchToStrategy(pitEntry,
Junxiao Shi8d843142016-07-11 22:42:42 +0000486 [&] (fw::Strategy* strategy) { strategy->afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700487}
488
489void
490Forwarder::onOutgoingNack(shared_ptr<pit::Entry> pitEntry, const Face& outFace,
491 const lp::NackHeader& nack)
492{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700493 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700494 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
495 " nack=" << pitEntry->getInterest().getName() <<
496 "~" << nack.getReason() << " no-in-record");
497 return;
498 }
499
500 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700501 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700502
503 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700504 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700505 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
506 " nack=" << pitEntry->getInterest().getName() <<
507 "~" << nack.getReason() << " no-in-record");
508 return;
509 }
510
511 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700512 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700513 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
514 " nack=" << pitEntry->getInterest().getName() <<
515 "~" << nack.getReason() << " face-is-multi-access");
516 return;
517 }
518
519 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
520 " nack=" << pitEntry->getInterest().getName() <<
521 "~" << nack.getReason() << " OK");
522
523 // create Nack packet with the Interest from in-record
524 lp::Nack nackPkt(inRecord->getInterest());
525 nackPkt.setHeader(nack);
526
527 // erase in-record
528 pitEntry->deleteInRecord(outFace);
529
530 // send Nack on face
531 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700532 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700533}
534
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000535const fib::Entry&
536Forwarder::lookupFib(const pit::Entry& pitEntry) const
537{
538 const Interest& interest = pitEntry.getInterest();
539 // has Link object?
540 if (!interest.hasLink()) {
541 // FIB lookup with Interest name
Junxiao Shia6de4292016-07-12 02:08:10 +0000542 const fib::Entry& fibEntry = m_fib.findLongestPrefixMatch(pitEntry);
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000543 NFD_LOG_TRACE("lookupFib noLinkObject found=" << fibEntry.getPrefix());
544 return fibEntry;
545 }
546
547 const Link& link = interest.getLink();
548
549 // in producer region?
550 if (m_networkRegionTable.isInProducerRegion(link)) {
551 // FIB lookup with Interest name
Junxiao Shia6de4292016-07-12 02:08:10 +0000552 const fib::Entry& fibEntry = m_fib.findLongestPrefixMatch(pitEntry);
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000553 NFD_LOG_TRACE("lookupFib inProducerRegion found=" << fibEntry.getPrefix());
554 return fibEntry;
555 }
556
557 // has SelectedDelegation?
558 if (interest.hasSelectedDelegation()) {
559 // FIB lookup with SelectedDelegation
560 Name selectedDelegation = interest.getSelectedDelegation();
Junxiao Shia6de4292016-07-12 02:08:10 +0000561 const fib::Entry& fibEntry = m_fib.findLongestPrefixMatch(selectedDelegation);
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000562 NFD_LOG_TRACE("lookupFib hasSelectedDelegation=" << selectedDelegation << " found=" << fibEntry.getPrefix());
563 return fibEntry;
564 }
565
566 // FIB lookup with first delegation Name
Junxiao Shia6de4292016-07-12 02:08:10 +0000567 const fib::Entry& fibEntry0 = m_fib.findLongestPrefixMatch(link.getDelegations().begin()->second);
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000568 // in default-free zone?
569 bool isDefaultFreeZone = !(fibEntry0.getPrefix().size() == 0 && fibEntry0.hasNextHops());
570 if (!isDefaultFreeZone) {
571 NFD_LOG_TRACE("onContentStoreMiss inConsumerRegion found=" << fibEntry0.getPrefix());
572 return fibEntry0;
573 }
574
575 // choose and set SelectedDelegation
576 for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) {
577 const Name& delegationName = delegation.second;
Junxiao Shia6de4292016-07-12 02:08:10 +0000578 const fib::Entry& fibEntry = m_fib.findLongestPrefixMatch(delegationName);
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000579 if (fibEntry.hasNextHops()) {
580 /// \todo Don't modify in-record Interests.
581 /// Set SelectedDelegation in outgoing Interest pipeline.
582 std::for_each(pitEntry.in_begin(), pitEntry.in_end(),
583 [&delegationName] (const pit::InRecord& inR) {
584 const_cast<Interest&>(inR.getInterest()).setSelectedDelegation(delegationName);
585 });
586 NFD_LOG_TRACE("onContentStoreMiss enterDefaultFreeZone"
587 << " setSelectedDelegation=" << delegationName);
588 return fibEntry;
589 }
590 }
591 BOOST_ASSERT(false);
592 return fibEntry0;
593}
594
Junxiao Shid3c792f2014-01-30 00:46:13 -0700595static inline bool
596compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
597{
598 return a.getExpiry() < b.getExpiry();
599}
600
601void
602Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
603{
Junxiao Shi4846f372016-04-05 13:39:30 -0700604 pit::InRecordCollection::iterator lastExpiring =
605 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700606
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700607 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700608 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
609 if (lastExpiryFromNow <= time::seconds::zero()) {
610 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700611 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700612
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700613 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700614 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700615 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
616}
617
618void
Junxiao Shia110f262014-10-12 12:35:20 -0700619Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
620 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700621{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700622 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700623
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700624 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700625 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700626 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700627}
628
629void
630Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
631{
Junxiao Shic041ca32014-02-25 20:01:15 -0700632 scheduler::cancel(pitEntry->m_unsatisfyTimer);
633 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700634}
635
Junxiao Shia110f262014-10-12 12:35:20 -0700636static inline void
637insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
638 const pit::OutRecord& outRecord)
639{
640 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
641}
642
643void
644Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
645 const time::milliseconds& dataFreshnessPeriod,
646 Face* upstream)
647{
648 // need Dead Nonce List insert?
649 bool needDnl = false;
650 if (isSatisfied) {
651 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
652 // Data never becomes stale if it doesn't have FreshnessPeriod field
653 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
654 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
655 }
656 else {
657 needDnl = true;
658 }
659
660 if (!needDnl) {
661 return;
662 }
663
664 // Dead Nonce List insert
665 if (upstream == 0) {
666 // insert all outgoing Nonces
667 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
668 std::for_each(outRecords.begin(), outRecords.end(),
669 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
670 }
671 else {
672 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700673 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700674 if (outRecord != pitEntry.getOutRecords().end()) {
675 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
676 }
677 }
678}
679
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800680} // namespace nfd