blob: ef792aed326245f387b85c3028f4e505fc0fe6a9 [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 Shiaf6569a2014-06-14 00:01:34 -070031#include <boost/random/uniform_int_distribution.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080032
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080033namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080034
Junxiao Shi8c8d2182014-01-30 22:33:00 -070035NFD_LOG_INIT("Forwarder");
36
Junxiao Shif3c07812014-03-11 21:48:49 -070037using fw::Strategy;
38
Junxiao Shif3c07812014-03-11 21:48:49 -070039const Name Forwarder::LOCALHOST_NAME("ndn:/localhost");
Junxiao Shi88884492014-02-15 15:57:43 -070040
Junxiao Shic041ca32014-02-25 20:01:15 -070041Forwarder::Forwarder()
Junxiao Shia4f2be82014-03-02 22:56:41 -070042 : m_faceTable(*this)
HangZhangad4afd12014-03-01 11:03:08 +080043 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060044 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080045 , m_measurements(m_nameTree)
Junxiao Shif3c07812014-03-11 21:48:49 -070046 , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080047{
Junxiao Shif3c07812014-03-11 21:48:49 -070048 fw::installStrategies(*this);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080049}
50
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060051Forwarder::~Forwarder()
52{
Junxiao Shi0355e9f2015-09-02 07:24:53 -070053}
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060054
Junxiao Shi0355e9f2015-09-02 07:24:53 -070055void
56Forwarder::startProcessInterest(Face& face, const Interest& interest)
57{
58 // check fields used by forwarding are well-formed
59 try {
60 if (interest.hasLink()) {
61 interest.getLink();
62 }
63 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -070064 catch (const tlv::Error&) {
Junxiao Shi0355e9f2015-09-02 07:24:53 -070065 NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() <<
66 " interest=" << interest.getName() << " malformed");
67 // It's safe to call interest.getName() because Name has been fully parsed
68 return;
69 }
70
71 this->onIncomingInterest(face, interest);
72}
73
74void
75Forwarder::startProcessData(Face& face, const Data& data)
76{
77 // check fields used by forwarding are well-formed
78 // (none needed)
79
80 this->onIncomingData(face, data);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060081}
82
Alexander Afanasyev33b72772014-01-26 23:22:58 -080083void
Junxiao Shi5e5e4452015-09-24 16:56:52 -070084Forwarder::startProcessNack(Face& face, const lp::Nack& nack)
85{
86 // check fields used by forwarding are well-formed
87 try {
88 if (nack.getInterest().hasLink()) {
89 nack.getInterest().getLink();
90 }
91 }
92 catch (const tlv::Error&) {
93 NFD_LOG_DEBUG("startProcessNack face=" << face.getId() <<
94 " nack=" << nack.getInterest().getName() <<
95 "~" << nack.getReason() << " malformed");
96 return;
97 }
98
99 this->onIncomingNack(face, nack);
100}
101
102void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700103Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
104{
105 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700106 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
107 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000108 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700109 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -0700110
Junxiao Shi88884492014-02-15 15:57:43 -0700111 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700112 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700113 LOCALHOST_NAME.isPrefixOf(interest.getName());
114 if (isViolatingLocalhost) {
115 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
116 " interest=" << interest.getName() << " violates /localhost");
117 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700118 return;
119 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700120
Junxiao Shi330136a2016-03-10 04:53:08 -0700121 // detect duplicate Nonce with Dead Nonce List
122 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
123 if (hasDuplicateNonceInDnl) {
124 // goto Interest loop pipeline
125 this->onInterestLoop(inFace, interest);
126 return;
127 }
128
Junxiao Shid3c792f2014-01-30 00:46:13 -0700129 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700130 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700131
Junxiao Shi330136a2016-03-10 04:53:08 -0700132 // detect duplicate Nonce in PIT entry
Junxiao Shifef73e42016-03-29 14:15:05 -0700133 bool hasDuplicateNonceInPit = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace) !=
134 fw::DUPLICATE_NONCE_NONE;
Junxiao Shi330136a2016-03-10 04:53:08 -0700135 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700136 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700137 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700138 return;
139 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700140
Junxiao Shid3c792f2014-01-30 00:46:13 -0700141 // cancel unsatisfy & straggler timer
142 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700143
Junxiao Shif3c07812014-03-11 21:48:49 -0700144 // is pending?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700145 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shie17349a2014-03-25 00:55:38 -0700146 bool isPending = inRecords.begin() != inRecords.end();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700147 if (!isPending) {
mzhang4eab72492015-02-25 11:16:09 -0600148 m_cs.find(interest,
149 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
150 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700151 }
mzhang4eab72492015-02-25 11:16:09 -0600152 else {
153 this->onContentStoreMiss(inFace, pitEntry, interest);
154 }
155}
Junxiao Shic041ca32014-02-25 20:01:15 -0700156
mzhang4eab72492015-02-25 11:16:09 -0600157void
Junxiao Shi330136a2016-03-10 04:53:08 -0700158Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700159{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700160 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700161 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700162 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
163 " interest=" << interest.getName() <<
164 " drop");
165 return;
166 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700167
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700168 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
169 " interest=" << interest.getName() <<
170 " send-Nack-duplicate");
171
172 // send Nack with reason=DUPLICATE
173 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
174 lp::Nack nack(interest);
175 nack.setReason(lp::NackReason::DUPLICATE);
176 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700177}
178
179void
mzhang4eab72492015-02-25 11:16:09 -0600180Forwarder::onContentStoreMiss(const Face& inFace,
181 shared_ptr<pit::Entry> pitEntry,
182 const Interest& interest)
183{
184 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
185
186 shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700187 // insert InRecord
mzhang4eab72492015-02-25 11:16:09 -0600188 pitEntry->insertOrUpdateInRecord(face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700189
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700190 // set PIT unsatisfy timer
191 this->setUnsatisfyTimer(pitEntry);
192
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700193 shared_ptr<fib::Entry> fibEntry;
194 // has Link object?
195 if (!interest.hasLink()) {
196 // FIB lookup with Interest name
197 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
198 NFD_LOG_TRACE("onContentStoreMiss noLinkObject");
199 }
200 else {
201 const Link& link = interest.getLink();
202
203 // in producer region?
204 if (m_networkRegionTable.isInProducerRegion(link)) {
205 // FIB lookup with Interest name
206 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
207 NFD_LOG_TRACE("onContentStoreMiss inProducerRegion");
208 }
209 // has SelectedDelegation?
210 else if (interest.hasSelectedDelegation()) {
211 // FIB lookup with SelectedDelegation
212 fibEntry = m_fib.findLongestPrefixMatch(interest.getSelectedDelegation());
213 NFD_LOG_TRACE("onContentStoreMiss hasSelectedDelegation=" << interest.getSelectedDelegation());
214 }
215 else {
216 // FIB lookup with first delegation Name
217 fibEntry = m_fib.findLongestPrefixMatch(link.getDelegations().begin()->second);
218
219 // in default-free zone?
220 bool isDefaultFreeZone = !(fibEntry->getPrefix().size() == 0 && fibEntry->hasNextHops());
221 if (isDefaultFreeZone) {
222 // choose and set SelectedDelegation
223 for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) {
224 const Name& delegationName = delegation.second;
225 fibEntry = m_fib.findLongestPrefixMatch(delegationName);
226 if (fibEntry->hasNextHops()) {
227 const_cast<Interest&>(interest).setSelectedDelegation(delegationName);
228 NFD_LOG_TRACE("onContentStoreMiss enterDefaultFreeZone"
229 << " setSelectedDelegation=" << delegationName);
230 break;
231 }
232 }
233 }
234 else {
235 NFD_LOG_TRACE("onContentStoreMiss inConsumerRegion");
236 }
237 }
238 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700239
Junxiao Shid3c792f2014-01-30 00:46:13 -0700240 // dispatch to strategy
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700241 BOOST_ASSERT(fibEntry != nullptr);
Junxiao Shif3c07812014-03-11 21:48:49 -0700242 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700243 cref(inFace), cref(interest), fibEntry, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700244}
245
246void
mzhang4eab72492015-02-25 11:16:09 -0600247Forwarder::onContentStoreHit(const Face& inFace,
248 shared_ptr<pit::Entry> pitEntry,
249 const Interest& interest,
250 const Data& data)
251{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500252 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600253
Junxiao Shicde37ad2015-12-24 01:02:05 -0700254 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600255 // XXX should we lookup PIT for other Interests that also match csMatch?
256
257 // set PIT straggler timer
258 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
259
260 // goto outgoing Data pipeline
261 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
262}
263
Junxiao Shif3c07812014-03-11 21:48:49 -0700264/** \brief compare two InRecords for picking outgoing Interest
265 * \return true if b is preferred over a
266 *
267 * This function should be passed to std::max_element over InRecordCollection.
268 * The outgoing Interest picked is the last incoming Interest
269 * that does not come from outFace.
270 * If all InRecords come from outFace, it's fine to pick that. This happens when
271 * there's only one InRecord that comes from outFace. The legit use is for
272 * vehicular network; otherwise, strategy shouldn't send to the sole inFace.
273 */
274static inline bool
275compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace)
276{
277 bool isOutFaceA = a.getFace().get() == outFace;
278 bool isOutFaceB = b.getFace().get() == outFace;
279
280 if (!isOutFaceA && isOutFaceB) {
281 return false;
282 }
283 if (isOutFaceA && !isOutFaceB) {
284 return true;
285 }
286
287 return a.getLastRenewed() > b.getLastRenewed();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700288}
289
290void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700291Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
292 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700293{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700294 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700295 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
296 return;
297 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700298 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
299 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700300
Junxiao Shi57f0f312014-03-16 11:52:20 -0700301 // scope control
Junxiao Shifef73e42016-03-29 14:15:05 -0700302 if (fw::violatesScope(*pitEntry, outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700303 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700304 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700305 return;
306 }
307
Junxiao Shid3c792f2014-01-30 00:46:13 -0700308 // pick Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700309 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
310 pit::InRecordCollection::const_iterator pickedInRecord = std::max_element(
311 inRecords.begin(), inRecords.end(), bind(&compare_pickInterest, _1, _2, &outFace));
312 BOOST_ASSERT(pickedInRecord != inRecords.end());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700313 shared_ptr<Interest> interest = const_pointer_cast<Interest>(
314 pickedInRecord->getInterest().shared_from_this());
315
316 if (wantNewNonce) {
317 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700318 static boost::random::uniform_int_distribution<uint32_t> dist;
319 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700320 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700321
Junxiao Shid3c792f2014-01-30 00:46:13 -0700322 // insert OutRecord
Junxiao Shid938a6b2014-05-11 23:40:29 -0700323 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700324
Junxiao Shid3c792f2014-01-30 00:46:13 -0700325 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700326 outFace.sendInterest(*interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700327 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700328}
329
330void
Junxiao Shi09498f02014-02-26 19:41:08 -0700331Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700332{
Junxiao Shifef73e42016-03-29 14:15:05 -0700333 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700334 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
335 " cannot reject forwarded Interest");
336 return;
337 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700338 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700339
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700340 // cancel unsatisfy & straggler timer
341 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
342
Junxiao Shid3c792f2014-01-30 00:46:13 -0700343 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700344 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700345}
346
347void
348Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
349{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700350 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
351
Junxiao Shid3c792f2014-01-30 00:46:13 -0700352 // invoke PIT unsatisfied callback
Junxiao Shif3c07812014-03-11 21:48:49 -0700353 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700354 pitEntry));
Junxiao Shic041ca32014-02-25 20:01:15 -0700355
Junxiao Shia110f262014-10-12 12:35:20 -0700356 // goto Interest Finalize pipeline
357 this->onInterestFinalize(pitEntry, false);
358}
359
360void
361Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
362 const time::milliseconds& dataFreshnessPeriod)
363{
364 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
365 (isSatisfied ? " satisfied" : " unsatisfied"));
366
367 // Dead Nonce List insert if necessary
368 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
369
Junxiao Shif3c07812014-03-11 21:48:49 -0700370 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700371 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600372 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700373}
374
375void
376Forwarder::onIncomingData(Face& inFace, const Data& data)
377{
378 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700379 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000380 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700381 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700382
Junxiao Shi88884492014-02-15 15:57:43 -0700383 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700384 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700385 LOCALHOST_NAME.isPrefixOf(data.getName());
386 if (isViolatingLocalhost) {
387 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
388 " data=" << data.getName() << " violates /localhost");
389 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700390 return;
391 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700392
Junxiao Shid3c792f2014-01-30 00:46:13 -0700393 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700394 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
395 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700396 // goto Data unsolicited pipeline
397 this->onDataUnsolicited(inFace, data);
398 return;
399 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700400
Junxiao Shid3c792f2014-01-30 00:46:13 -0700401 // CS insert
402 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700403
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700404 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700405 // foreach PitEntry
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700406 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700407 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700408
Junxiao Shid3c792f2014-01-30 00:46:13 -0700409 // cancel unsatisfy & straggler timer
410 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700411
Junxiao Shid3c792f2014-01-30 00:46:13 -0700412 // remember pending downstreams
413 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700414 for (const pit::InRecord& inRecord : inRecords) {
415 if (inRecord.getExpiry() > time::steady_clock::now()) {
416 pendingDownstreams.insert(inRecord.getFace().get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700417 }
418 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700419
Junxiao Shid938a6b2014-05-11 23:40:29 -0700420 // invoke PIT satisfy callback
Junxiao Shi82e7f582014-09-07 15:15:40 -0700421 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700422 pitEntry, cref(inFace), cref(data)));
423
Junxiao Shia110f262014-10-12 12:35:20 -0700424 // Dead Nonce List insert if necessary (for OutRecord of inFace)
425 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
426
Junxiao Shid3c792f2014-01-30 00:46:13 -0700427 // mark PIT satisfied
428 pitEntry->deleteInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700429 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700430
Junxiao Shid3c792f2014-01-30 00:46:13 -0700431 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700432 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700433 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700434
Junxiao Shid3c792f2014-01-30 00:46:13 -0700435 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700436 for (Face* pendingDownstream : pendingDownstreams) {
437 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700438 continue;
439 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700440 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700441 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700442 }
443}
444
445void
446Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
447{
448 // accept to cache?
Junxiao Shicde37ad2015-12-24 01:02:05 -0700449 bool acceptToCache = inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700450 if (acceptToCache) {
451 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700452 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700453 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700454
Junxiao Shif3c07812014-03-11 21:48:49 -0700455 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
456 " data=" << data.getName() <<
457 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700458}
459
460void
461Forwarder::onOutgoingData(const Data& data, Face& outFace)
462{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700463 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700464 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
465 return;
466 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700467 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
468
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700469 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700470 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700471 LOCALHOST_NAME.isPrefixOf(data.getName());
472 if (isViolatingLocalhost) {
473 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
474 " data=" << data.getName() << " violates /localhost");
475 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700476 return;
477 }
478
Junxiao Shif3c07812014-03-11 21:48:49 -0700479 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700480
Junxiao Shid3c792f2014-01-30 00:46:13 -0700481 // send Data
482 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700483 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700484}
485
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700486void
487Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
488{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000489 // receive Nack
490 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700491 ++m_counters.nInNacks;
492
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700493 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700494 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700495 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
496 " nack=" << nack.getInterest().getName() <<
497 "~" << nack.getReason() << " face-is-multi-access");
498 return;
499 }
500
501 // PIT match
502 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
503 // if no PIT entry found, drop
504 if (pitEntry == nullptr) {
505 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
506 " nack=" << nack.getInterest().getName() <<
507 "~" << nack.getReason() << " no-PIT-entry");
508 return;
509 }
510
511 // has out-record?
512 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
513 // if no out-record found, drop
514 if (outRecord == pitEntry->getOutRecords().end()) {
515 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
516 " nack=" << nack.getInterest().getName() <<
517 "~" << nack.getReason() << " no-out-record");
518 return;
519 }
520
521 // if out-record has different Nonce, drop
522 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
523 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
524 " nack=" << nack.getInterest().getName() <<
525 "~" << nack.getReason() << " wrong-Nonce " <<
526 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
527 return;
528 }
529
530 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
531 " nack=" << nack.getInterest().getName() <<
532 "~" << nack.getReason() << " OK");
533
534 // record Nack on out-record
535 outRecord->setIncomingNack(nack);
536
537 // trigger strategy: after receive NACK
538 shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
539 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveNack, _1,
540 cref(inFace), cref(nack), fibEntry, pitEntry));
541}
542
543void
544Forwarder::onOutgoingNack(shared_ptr<pit::Entry> pitEntry, const Face& outFace,
545 const lp::NackHeader& nack)
546{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700547 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700548 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
549 " nack=" << pitEntry->getInterest().getName() <<
550 "~" << nack.getReason() << " no-in-record");
551 return;
552 }
553
554 // has in-record?
555 pit::InRecordCollection::const_iterator inRecord = pitEntry->getInRecord(outFace);
556
557 // if no in-record found, drop
558 if (inRecord == pitEntry->getInRecords().end()) {
559 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
560 " nack=" << pitEntry->getInterest().getName() <<
561 "~" << nack.getReason() << " no-in-record");
562 return;
563 }
564
565 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700566 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700567 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
568 " nack=" << pitEntry->getInterest().getName() <<
569 "~" << nack.getReason() << " face-is-multi-access");
570 return;
571 }
572
573 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
574 " nack=" << pitEntry->getInterest().getName() <<
575 "~" << nack.getReason() << " OK");
576
577 // create Nack packet with the Interest from in-record
578 lp::Nack nackPkt(inRecord->getInterest());
579 nackPkt.setHeader(nack);
580
581 // erase in-record
582 pitEntry->deleteInRecord(outFace);
583
584 // send Nack on face
585 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700586 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700587}
588
Junxiao Shid3c792f2014-01-30 00:46:13 -0700589static inline bool
590compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
591{
592 return a.getExpiry() < b.getExpiry();
593}
594
595void
596Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
597{
598 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
599 pit::InRecordCollection::const_iterator lastExpiring =
600 std::max_element(inRecords.begin(), inRecords.end(),
601 &compare_InRecord_expiry);
602
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700603 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
604 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700605 if (lastExpiryFromNow <= time::seconds(0)) {
606 // TODO all InRecords are already expired; will this happen?
607 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700608
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700609 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700610 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700611 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
612}
613
614void
Junxiao Shia110f262014-10-12 12:35:20 -0700615Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
616 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700617{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700618 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700619
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700620 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700621 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700622 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700623}
624
625void
626Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
627{
Junxiao Shic041ca32014-02-25 20:01:15 -0700628 scheduler::cancel(pitEntry->m_unsatisfyTimer);
629 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700630}
631
Junxiao Shia110f262014-10-12 12:35:20 -0700632static inline void
633insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
634 const pit::OutRecord& outRecord)
635{
636 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
637}
638
639void
640Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
641 const time::milliseconds& dataFreshnessPeriod,
642 Face* upstream)
643{
644 // need Dead Nonce List insert?
645 bool needDnl = false;
646 if (isSatisfied) {
647 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
648 // Data never becomes stale if it doesn't have FreshnessPeriod field
649 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
650 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
651 }
652 else {
653 needDnl = true;
654 }
655
656 if (!needDnl) {
657 return;
658 }
659
660 // Dead Nonce List insert
661 if (upstream == 0) {
662 // insert all outgoing Nonces
663 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
664 std::for_each(outRecords.begin(), outRecords.end(),
665 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
666 }
667 else {
668 // insert outgoing Nonce of a specific face
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700669 pit::OutRecordCollection::const_iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700670 if (outRecord != pitEntry.getOutRecords().end()) {
671 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
672 }
673 }
674}
675
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800676} // namespace nfd