blob: b575e3b521dcb9d60026ecbf07ec544a79034ef4 [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 Shi4846f372016-04-05 13:39:30 -0700145 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600146 m_cs.find(interest,
147 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
148 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700149 }
mzhang4eab72492015-02-25 11:16:09 -0600150 else {
151 this->onContentStoreMiss(inFace, pitEntry, interest);
152 }
153}
Junxiao Shic041ca32014-02-25 20:01:15 -0700154
mzhang4eab72492015-02-25 11:16:09 -0600155void
Junxiao Shi330136a2016-03-10 04:53:08 -0700156Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700157{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700158 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700159 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700160 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
161 " interest=" << interest.getName() <<
162 " drop");
163 return;
164 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700165
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700166 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
167 " interest=" << interest.getName() <<
168 " send-Nack-duplicate");
169
170 // send Nack with reason=DUPLICATE
171 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
172 lp::Nack nack(interest);
173 nack.setReason(lp::NackReason::DUPLICATE);
174 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700175}
176
177void
mzhang4eab72492015-02-25 11:16:09 -0600178Forwarder::onContentStoreMiss(const Face& inFace,
179 shared_ptr<pit::Entry> pitEntry,
180 const Interest& interest)
181{
182 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
183
184 shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this());
Junxiao Shi4846f372016-04-05 13:39:30 -0700185 // insert in-record
mzhang4eab72492015-02-25 11:16:09 -0600186 pitEntry->insertOrUpdateInRecord(face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700187
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700188 // set PIT unsatisfy timer
189 this->setUnsatisfyTimer(pitEntry);
190
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700191 shared_ptr<fib::Entry> fibEntry;
192 // has Link object?
193 if (!interest.hasLink()) {
194 // FIB lookup with Interest name
195 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
196 NFD_LOG_TRACE("onContentStoreMiss noLinkObject");
197 }
198 else {
199 const Link& link = interest.getLink();
200
201 // in producer region?
202 if (m_networkRegionTable.isInProducerRegion(link)) {
203 // FIB lookup with Interest name
204 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
205 NFD_LOG_TRACE("onContentStoreMiss inProducerRegion");
206 }
207 // has SelectedDelegation?
208 else if (interest.hasSelectedDelegation()) {
209 // FIB lookup with SelectedDelegation
210 fibEntry = m_fib.findLongestPrefixMatch(interest.getSelectedDelegation());
211 NFD_LOG_TRACE("onContentStoreMiss hasSelectedDelegation=" << interest.getSelectedDelegation());
212 }
213 else {
214 // FIB lookup with first delegation Name
215 fibEntry = m_fib.findLongestPrefixMatch(link.getDelegations().begin()->second);
216
217 // in default-free zone?
218 bool isDefaultFreeZone = !(fibEntry->getPrefix().size() == 0 && fibEntry->hasNextHops());
219 if (isDefaultFreeZone) {
220 // choose and set SelectedDelegation
221 for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) {
222 const Name& delegationName = delegation.second;
223 fibEntry = m_fib.findLongestPrefixMatch(delegationName);
224 if (fibEntry->hasNextHops()) {
225 const_cast<Interest&>(interest).setSelectedDelegation(delegationName);
226 NFD_LOG_TRACE("onContentStoreMiss enterDefaultFreeZone"
227 << " setSelectedDelegation=" << delegationName);
228 break;
229 }
230 }
231 }
232 else {
233 NFD_LOG_TRACE("onContentStoreMiss inConsumerRegion");
234 }
235 }
236 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700237
Junxiao Shid3c792f2014-01-30 00:46:13 -0700238 // dispatch to strategy
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700239 BOOST_ASSERT(fibEntry != nullptr);
Junxiao Shif3c07812014-03-11 21:48:49 -0700240 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700241 cref(inFace), cref(interest), fibEntry, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700242}
243
244void
mzhang4eab72492015-02-25 11:16:09 -0600245Forwarder::onContentStoreHit(const Face& inFace,
246 shared_ptr<pit::Entry> pitEntry,
247 const Interest& interest,
248 const Data& data)
249{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500250 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600251
Junxiao Shicde37ad2015-12-24 01:02:05 -0700252 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600253 // XXX should we lookup PIT for other Interests that also match csMatch?
254
255 // set PIT straggler timer
256 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
257
258 // goto outgoing Data pipeline
259 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
260}
261
Junxiao Shi4846f372016-04-05 13:39:30 -0700262/** \brief compare two in-records for picking outgoing Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700263 * \return true if b is preferred over a
264 *
265 * This function should be passed to std::max_element over InRecordCollection.
266 * The outgoing Interest picked is the last incoming Interest
267 * that does not come from outFace.
Junxiao Shi4846f372016-04-05 13:39:30 -0700268 * If all in-records come from outFace, it's fine to pick that. This happens when
269 * there's only one in-record that comes from outFace. The legit use is for
Junxiao Shif3c07812014-03-11 21:48:49 -0700270 * vehicular network; otherwise, strategy shouldn't send to the sole inFace.
271 */
272static inline bool
273compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace)
274{
275 bool isOutFaceA = a.getFace().get() == outFace;
276 bool isOutFaceB = b.getFace().get() == outFace;
277
278 if (!isOutFaceA && isOutFaceB) {
279 return false;
280 }
281 if (isOutFaceA && !isOutFaceB) {
282 return true;
283 }
284
285 return a.getLastRenewed() > b.getLastRenewed();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700286}
287
288void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700289Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
290 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700291{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700292 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700293 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
294 return;
295 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700296 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
297 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700298
Junxiao Shi57f0f312014-03-16 11:52:20 -0700299 // scope control
Junxiao Shifef73e42016-03-29 14:15:05 -0700300 if (fw::violatesScope(*pitEntry, outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700301 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700302 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700303 return;
304 }
305
Junxiao Shid3c792f2014-01-30 00:46:13 -0700306 // pick Interest
Junxiao Shi4846f372016-04-05 13:39:30 -0700307 pit::InRecordCollection::iterator pickedInRecord = std::max_element(
308 pitEntry->in_begin(), pitEntry->in_end(), bind(&compare_pickInterest, _1, _2, &outFace));
309 BOOST_ASSERT(pickedInRecord != pitEntry->in_end());
310 auto interest = const_pointer_cast<Interest>(pickedInRecord->getInterest().shared_from_this());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700311
312 if (wantNewNonce) {
313 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700314 static boost::random::uniform_int_distribution<uint32_t> dist;
315 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700316 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700317
Junxiao Shi4846f372016-04-05 13:39:30 -0700318 // insert out-record
Junxiao Shid938a6b2014-05-11 23:40:29 -0700319 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700320
Junxiao Shid3c792f2014-01-30 00:46:13 -0700321 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700322 outFace.sendInterest(*interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700323 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700324}
325
326void
Junxiao Shi09498f02014-02-26 19:41:08 -0700327Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700328{
Junxiao Shifef73e42016-03-29 14:15:05 -0700329 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700330 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
331 " cannot reject forwarded Interest");
332 return;
333 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700334 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700335
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700336 // cancel unsatisfy & straggler timer
337 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
338
Junxiao Shid3c792f2014-01-30 00:46:13 -0700339 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700340 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700341}
342
343void
344Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
345{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700346 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
347
Junxiao Shid3c792f2014-01-30 00:46:13 -0700348 // invoke PIT unsatisfied callback
Junxiao Shif3c07812014-03-11 21:48:49 -0700349 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700350 pitEntry));
Junxiao Shic041ca32014-02-25 20:01:15 -0700351
Junxiao Shia110f262014-10-12 12:35:20 -0700352 // goto Interest Finalize pipeline
353 this->onInterestFinalize(pitEntry, false);
354}
355
356void
357Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
358 const time::milliseconds& dataFreshnessPeriod)
359{
360 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
361 (isSatisfied ? " satisfied" : " unsatisfied"));
362
363 // Dead Nonce List insert if necessary
364 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
365
Junxiao Shif3c07812014-03-11 21:48:49 -0700366 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700367 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600368 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700369}
370
371void
372Forwarder::onIncomingData(Face& inFace, const Data& data)
373{
374 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700375 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000376 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700377 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700378
Junxiao Shi88884492014-02-15 15:57:43 -0700379 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700380 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700381 LOCALHOST_NAME.isPrefixOf(data.getName());
382 if (isViolatingLocalhost) {
383 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
384 " data=" << data.getName() << " violates /localhost");
385 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700386 return;
387 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700388
Junxiao Shid3c792f2014-01-30 00:46:13 -0700389 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700390 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
391 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700392 // goto Data unsolicited pipeline
393 this->onDataUnsolicited(inFace, data);
394 return;
395 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700396
Junxiao Shid3c792f2014-01-30 00:46:13 -0700397 // CS insert
398 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700399
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700400 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700401 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700402 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700403 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700404 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700405
Junxiao Shid3c792f2014-01-30 00:46:13 -0700406 // cancel unsatisfy & straggler timer
407 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700408
Junxiao Shid3c792f2014-01-30 00:46:13 -0700409 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700410 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
411 if (inRecord.getExpiry() > now) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700412 pendingDownstreams.insert(inRecord.getFace().get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700413 }
414 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700415
Junxiao Shid938a6b2014-05-11 23:40:29 -0700416 // invoke PIT satisfy callback
Junxiao Shi82e7f582014-09-07 15:15:40 -0700417 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700418 pitEntry, cref(inFace), cref(data)));
419
Junxiao Shi4846f372016-04-05 13:39:30 -0700420 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700421 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
422
Junxiao Shid3c792f2014-01-30 00:46:13 -0700423 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700424 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700425 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700426
Junxiao Shid3c792f2014-01-30 00:46:13 -0700427 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700428 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700429 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700430
Junxiao Shid3c792f2014-01-30 00:46:13 -0700431 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700432 for (Face* pendingDownstream : pendingDownstreams) {
433 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700434 continue;
435 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700436 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700437 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700438 }
439}
440
441void
442Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
443{
444 // accept to cache?
Junxiao Shicde37ad2015-12-24 01:02:05 -0700445 bool acceptToCache = inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700446 if (acceptToCache) {
447 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700448 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700449 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700450
Junxiao Shif3c07812014-03-11 21:48:49 -0700451 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
452 " data=" << data.getName() <<
453 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700454}
455
456void
457Forwarder::onOutgoingData(const Data& data, Face& outFace)
458{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700459 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700460 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
461 return;
462 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700463 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
464
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700465 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700466 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700467 LOCALHOST_NAME.isPrefixOf(data.getName());
468 if (isViolatingLocalhost) {
469 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
470 " data=" << data.getName() << " violates /localhost");
471 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700472 return;
473 }
474
Junxiao Shif3c07812014-03-11 21:48:49 -0700475 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700476
Junxiao Shid3c792f2014-01-30 00:46:13 -0700477 // send Data
478 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700479 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700480}
481
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700482void
483Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
484{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000485 // receive Nack
486 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700487 ++m_counters.nInNacks;
488
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700489 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700490 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700491 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
492 " nack=" << nack.getInterest().getName() <<
493 "~" << nack.getReason() << " face-is-multi-access");
494 return;
495 }
496
497 // PIT match
498 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
499 // if no PIT entry found, drop
500 if (pitEntry == nullptr) {
501 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
502 " nack=" << nack.getInterest().getName() <<
503 "~" << nack.getReason() << " no-PIT-entry");
504 return;
505 }
506
507 // has out-record?
508 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
509 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700510 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700511 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
512 " nack=" << nack.getInterest().getName() <<
513 "~" << nack.getReason() << " no-out-record");
514 return;
515 }
516
517 // if out-record has different Nonce, drop
518 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
519 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
520 " nack=" << nack.getInterest().getName() <<
521 "~" << nack.getReason() << " wrong-Nonce " <<
522 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
523 return;
524 }
525
526 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
527 " nack=" << nack.getInterest().getName() <<
528 "~" << nack.getReason() << " OK");
529
530 // record Nack on out-record
531 outRecord->setIncomingNack(nack);
532
533 // trigger strategy: after receive NACK
534 shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
535 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveNack, _1,
536 cref(inFace), cref(nack), fibEntry, pitEntry));
537}
538
539void
540Forwarder::onOutgoingNack(shared_ptr<pit::Entry> pitEntry, const Face& outFace,
541 const lp::NackHeader& nack)
542{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700543 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700544 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
545 " nack=" << pitEntry->getInterest().getName() <<
546 "~" << nack.getReason() << " no-in-record");
547 return;
548 }
549
550 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700551 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700552
553 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700554 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700555 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
556 " nack=" << pitEntry->getInterest().getName() <<
557 "~" << nack.getReason() << " no-in-record");
558 return;
559 }
560
561 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700562 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700563 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
564 " nack=" << pitEntry->getInterest().getName() <<
565 "~" << nack.getReason() << " face-is-multi-access");
566 return;
567 }
568
569 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
570 " nack=" << pitEntry->getInterest().getName() <<
571 "~" << nack.getReason() << " OK");
572
573 // create Nack packet with the Interest from in-record
574 lp::Nack nackPkt(inRecord->getInterest());
575 nackPkt.setHeader(nack);
576
577 // erase in-record
578 pitEntry->deleteInRecord(outFace);
579
580 // send Nack on face
581 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700582 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700583}
584
Junxiao Shid3c792f2014-01-30 00:46:13 -0700585static inline bool
586compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
587{
588 return a.getExpiry() < b.getExpiry();
589}
590
591void
592Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
593{
Junxiao Shi4846f372016-04-05 13:39:30 -0700594 pit::InRecordCollection::iterator lastExpiring =
595 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700596
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700597 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700598 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
599 if (lastExpiryFromNow <= time::seconds::zero()) {
600 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700601 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700602
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700603 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700604 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700605 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
606}
607
608void
Junxiao Shia110f262014-10-12 12:35:20 -0700609Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
610 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700611{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700612 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700613
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700614 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700615 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700616 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700617}
618
619void
620Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
621{
Junxiao Shic041ca32014-02-25 20:01:15 -0700622 scheduler::cancel(pitEntry->m_unsatisfyTimer);
623 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700624}
625
Junxiao Shia110f262014-10-12 12:35:20 -0700626static inline void
627insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
628 const pit::OutRecord& outRecord)
629{
630 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
631}
632
633void
634Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
635 const time::milliseconds& dataFreshnessPeriod,
636 Face* upstream)
637{
638 // need Dead Nonce List insert?
639 bool needDnl = false;
640 if (isSatisfied) {
641 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
642 // Data never becomes stale if it doesn't have FreshnessPeriod field
643 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
644 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
645 }
646 else {
647 needDnl = true;
648 }
649
650 if (!needDnl) {
651 return;
652 }
653
654 // Dead Nonce List insert
655 if (upstream == 0) {
656 // insert all outgoing Nonces
657 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
658 std::for_each(outRecords.begin(), outRecords.end(),
659 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
660 }
661 else {
662 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700663 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700664 if (outRecord != pitEntry.getOutRecords().end()) {
665 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
666 }
667 }
668}
669
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800670} // namespace nfd