blob: 612b3e2279caafa33836d29215115a7ff80aaed7 [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 Shic041ca32014-02-25 20:01:15 -070039Forwarder::Forwarder()
Junxiao Shia4f2be82014-03-02 22:56:41 -070040 : m_faceTable(*this)
HangZhangad4afd12014-03-01 11:03:08 +080041 , 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);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080047}
48
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060049Forwarder::~Forwarder()
50{
Junxiao Shi0355e9f2015-09-02 07:24:53 -070051}
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060052
Junxiao Shi0355e9f2015-09-02 07:24:53 -070053void
54Forwarder::startProcessInterest(Face& face, const Interest& interest)
55{
56 // check fields used by forwarding are well-formed
57 try {
58 if (interest.hasLink()) {
59 interest.getLink();
60 }
61 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -070062 catch (const tlv::Error&) {
Junxiao Shi0355e9f2015-09-02 07:24:53 -070063 NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() <<
64 " interest=" << interest.getName() << " malformed");
65 // It's safe to call interest.getName() because Name has been fully parsed
66 return;
67 }
68
69 this->onIncomingInterest(face, interest);
70}
71
72void
73Forwarder::startProcessData(Face& face, const Data& data)
74{
75 // check fields used by forwarding are well-formed
76 // (none needed)
77
78 this->onIncomingData(face, data);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060079}
80
Alexander Afanasyev33b72772014-01-26 23:22:58 -080081void
Junxiao Shi5e5e4452015-09-24 16:56:52 -070082Forwarder::startProcessNack(Face& face, const lp::Nack& nack)
83{
84 // check fields used by forwarding are well-formed
85 try {
86 if (nack.getInterest().hasLink()) {
87 nack.getInterest().getLink();
88 }
89 }
90 catch (const tlv::Error&) {
91 NFD_LOG_DEBUG("startProcessNack face=" << face.getId() <<
92 " nack=" << nack.getInterest().getName() <<
93 "~" << nack.getReason() << " malformed");
94 return;
95 }
96
97 this->onIncomingNack(face, nack);
98}
99
100void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700101Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
102{
103 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700104 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
105 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000106 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700107 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -0700108
Junxiao Shi88884492014-02-15 15:57:43 -0700109 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700110 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700111 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700112 if (isViolatingLocalhost) {
113 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
114 " interest=" << interest.getName() << " violates /localhost");
115 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700116 return;
117 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700118
Junxiao Shi330136a2016-03-10 04:53:08 -0700119 // detect duplicate Nonce with Dead Nonce List
120 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
121 if (hasDuplicateNonceInDnl) {
122 // goto Interest loop pipeline
123 this->onInterestLoop(inFace, interest);
124 return;
125 }
126
Junxiao Shid3c792f2014-01-30 00:46:13 -0700127 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700128 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700129
Junxiao Shi330136a2016-03-10 04:53:08 -0700130 // detect duplicate Nonce in PIT entry
Junxiao Shifef73e42016-03-29 14:15:05 -0700131 bool hasDuplicateNonceInPit = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace) !=
132 fw::DUPLICATE_NONCE_NONE;
Junxiao Shi330136a2016-03-10 04:53:08 -0700133 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700134 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700135 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700136 return;
137 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700138
Junxiao Shid3c792f2014-01-30 00:46:13 -0700139 // cancel unsatisfy & straggler timer
140 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700141
Junxiao Shif3c07812014-03-11 21:48:49 -0700142 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700143 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600144 m_cs.find(interest,
145 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
146 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700147 }
mzhang4eab72492015-02-25 11:16:09 -0600148 else {
149 this->onContentStoreMiss(inFace, pitEntry, interest);
150 }
151}
Junxiao Shic041ca32014-02-25 20:01:15 -0700152
mzhang4eab72492015-02-25 11:16:09 -0600153void
Junxiao Shi330136a2016-03-10 04:53:08 -0700154Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700155{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700156 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700157 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700158 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
159 " interest=" << interest.getName() <<
160 " drop");
161 return;
162 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700163
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700164 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
165 " interest=" << interest.getName() <<
166 " send-Nack-duplicate");
167
168 // send Nack with reason=DUPLICATE
169 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
170 lp::Nack nack(interest);
171 nack.setReason(lp::NackReason::DUPLICATE);
172 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700173}
174
175void
mzhang4eab72492015-02-25 11:16:09 -0600176Forwarder::onContentStoreMiss(const Face& inFace,
177 shared_ptr<pit::Entry> pitEntry,
178 const Interest& interest)
179{
180 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
181
182 shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this());
Junxiao Shi4846f372016-04-05 13:39:30 -0700183 // insert in-record
mzhang4eab72492015-02-25 11:16:09 -0600184 pitEntry->insertOrUpdateInRecord(face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700185
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700186 // set PIT unsatisfy timer
187 this->setUnsatisfyTimer(pitEntry);
188
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700189 shared_ptr<fib::Entry> fibEntry;
190 // has Link object?
191 if (!interest.hasLink()) {
192 // FIB lookup with Interest name
193 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
194 NFD_LOG_TRACE("onContentStoreMiss noLinkObject");
195 }
196 else {
197 const Link& link = interest.getLink();
198
199 // in producer region?
200 if (m_networkRegionTable.isInProducerRegion(link)) {
201 // FIB lookup with Interest name
202 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
203 NFD_LOG_TRACE("onContentStoreMiss inProducerRegion");
204 }
205 // has SelectedDelegation?
206 else if (interest.hasSelectedDelegation()) {
207 // FIB lookup with SelectedDelegation
208 fibEntry = m_fib.findLongestPrefixMatch(interest.getSelectedDelegation());
209 NFD_LOG_TRACE("onContentStoreMiss hasSelectedDelegation=" << interest.getSelectedDelegation());
210 }
211 else {
212 // FIB lookup with first delegation Name
213 fibEntry = m_fib.findLongestPrefixMatch(link.getDelegations().begin()->second);
214
215 // in default-free zone?
216 bool isDefaultFreeZone = !(fibEntry->getPrefix().size() == 0 && fibEntry->hasNextHops());
217 if (isDefaultFreeZone) {
218 // choose and set SelectedDelegation
219 for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) {
220 const Name& delegationName = delegation.second;
221 fibEntry = m_fib.findLongestPrefixMatch(delegationName);
222 if (fibEntry->hasNextHops()) {
223 const_cast<Interest&>(interest).setSelectedDelegation(delegationName);
224 NFD_LOG_TRACE("onContentStoreMiss enterDefaultFreeZone"
225 << " setSelectedDelegation=" << delegationName);
226 break;
227 }
228 }
229 }
230 else {
231 NFD_LOG_TRACE("onContentStoreMiss inConsumerRegion");
232 }
233 }
234 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700235
Junxiao Shid3c792f2014-01-30 00:46:13 -0700236 // dispatch to strategy
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700237 BOOST_ASSERT(fibEntry != nullptr);
Junxiao Shif3c07812014-03-11 21:48:49 -0700238 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700239 cref(inFace), cref(interest), fibEntry, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700240}
241
242void
mzhang4eab72492015-02-25 11:16:09 -0600243Forwarder::onContentStoreHit(const Face& inFace,
244 shared_ptr<pit::Entry> pitEntry,
245 const Interest& interest,
246 const Data& data)
247{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500248 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600249
Junxiao Shicde37ad2015-12-24 01:02:05 -0700250 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600251 // XXX should we lookup PIT for other Interests that also match csMatch?
252
253 // set PIT straggler timer
254 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
255
256 // goto outgoing Data pipeline
257 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
258}
259
Junxiao Shi4846f372016-04-05 13:39:30 -0700260/** \brief compare two in-records for picking outgoing Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700261 * \return true if b is preferred over a
262 *
263 * This function should be passed to std::max_element over InRecordCollection.
264 * The outgoing Interest picked is the last incoming Interest
265 * that does not come from outFace.
Junxiao Shi4846f372016-04-05 13:39:30 -0700266 * If all in-records come from outFace, it's fine to pick that. This happens when
267 * there's only one in-record that comes from outFace. The legit use is for
Junxiao Shif3c07812014-03-11 21:48:49 -0700268 * vehicular network; otherwise, strategy shouldn't send to the sole inFace.
269 */
270static inline bool
271compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace)
272{
273 bool isOutFaceA = a.getFace().get() == outFace;
274 bool isOutFaceB = b.getFace().get() == outFace;
275
276 if (!isOutFaceA && isOutFaceB) {
277 return false;
278 }
279 if (isOutFaceA && !isOutFaceB) {
280 return true;
281 }
282
283 return a.getLastRenewed() > b.getLastRenewed();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700284}
285
286void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700287Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
288 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700289{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700290 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700291 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
292 return;
293 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700294 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
295 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700296
Junxiao Shi57f0f312014-03-16 11:52:20 -0700297 // scope control
Junxiao Shifef73e42016-03-29 14:15:05 -0700298 if (fw::violatesScope(*pitEntry, outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700299 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700300 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700301 return;
302 }
303
Junxiao Shid3c792f2014-01-30 00:46:13 -0700304 // pick Interest
Junxiao Shi4846f372016-04-05 13:39:30 -0700305 pit::InRecordCollection::iterator pickedInRecord = std::max_element(
306 pitEntry->in_begin(), pitEntry->in_end(), bind(&compare_pickInterest, _1, _2, &outFace));
307 BOOST_ASSERT(pickedInRecord != pitEntry->in_end());
308 auto interest = const_pointer_cast<Interest>(pickedInRecord->getInterest().shared_from_this());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700309
310 if (wantNewNonce) {
311 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700312 static boost::random::uniform_int_distribution<uint32_t> dist;
313 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700314 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700315
Junxiao Shi4846f372016-04-05 13:39:30 -0700316 // insert out-record
Junxiao Shid938a6b2014-05-11 23:40:29 -0700317 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700318
Junxiao Shid3c792f2014-01-30 00:46:13 -0700319 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700320 outFace.sendInterest(*interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700321 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700322}
323
324void
Junxiao Shi09498f02014-02-26 19:41:08 -0700325Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700326{
Junxiao Shifef73e42016-03-29 14:15:05 -0700327 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700328 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
329 " cannot reject forwarded Interest");
330 return;
331 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700332 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700333
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700334 // cancel unsatisfy & straggler timer
335 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
336
Junxiao Shid3c792f2014-01-30 00:46:13 -0700337 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700338 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700339}
340
341void
342Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
343{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700344 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
345
Junxiao Shid3c792f2014-01-30 00:46:13 -0700346 // invoke PIT unsatisfied callback
Junxiao Shif3c07812014-03-11 21:48:49 -0700347 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700348 pitEntry));
Junxiao Shic041ca32014-02-25 20:01:15 -0700349
Junxiao Shia110f262014-10-12 12:35:20 -0700350 // goto Interest Finalize pipeline
351 this->onInterestFinalize(pitEntry, false);
352}
353
354void
355Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
356 const time::milliseconds& dataFreshnessPeriod)
357{
358 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
359 (isSatisfied ? " satisfied" : " unsatisfied"));
360
361 // Dead Nonce List insert if necessary
362 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
363
Junxiao Shif3c07812014-03-11 21:48:49 -0700364 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700365 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600366 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700367}
368
369void
370Forwarder::onIncomingData(Face& inFace, const Data& data)
371{
372 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700373 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000374 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700375 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700376
Junxiao Shi88884492014-02-15 15:57:43 -0700377 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700378 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700379 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700380 if (isViolatingLocalhost) {
381 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
382 " data=" << data.getName() << " violates /localhost");
383 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700384 return;
385 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700386
Junxiao Shid3c792f2014-01-30 00:46:13 -0700387 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700388 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
389 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700390 // goto Data unsolicited pipeline
391 this->onDataUnsolicited(inFace, data);
392 return;
393 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700394
Junxiao Shid3c792f2014-01-30 00:46:13 -0700395 // CS insert
396 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700397
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700398 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700399 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700400 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700401 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700402 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700403
Junxiao Shid3c792f2014-01-30 00:46:13 -0700404 // cancel unsatisfy & straggler timer
405 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700406
Junxiao Shid3c792f2014-01-30 00:46:13 -0700407 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700408 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
409 if (inRecord.getExpiry() > now) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700410 pendingDownstreams.insert(inRecord.getFace().get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700411 }
412 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700413
Junxiao Shid938a6b2014-05-11 23:40:29 -0700414 // invoke PIT satisfy callback
Junxiao Shi82e7f582014-09-07 15:15:40 -0700415 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700416 pitEntry, cref(inFace), cref(data)));
417
Junxiao Shi4846f372016-04-05 13:39:30 -0700418 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700419 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
420
Junxiao Shid3c792f2014-01-30 00:46:13 -0700421 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700422 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700423 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700424
Junxiao Shid3c792f2014-01-30 00:46:13 -0700425 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700426 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700427 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700428
Junxiao Shid3c792f2014-01-30 00:46:13 -0700429 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700430 for (Face* pendingDownstream : pendingDownstreams) {
431 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700432 continue;
433 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700434 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700435 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700436 }
437}
438
439void
440Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
441{
442 // accept to cache?
Junxiao Shicde37ad2015-12-24 01:02:05 -0700443 bool acceptToCache = inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700444 if (acceptToCache) {
445 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700446 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700447 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700448
Junxiao Shif3c07812014-03-11 21:48:49 -0700449 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
450 " data=" << data.getName() <<
451 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700452}
453
454void
455Forwarder::onOutgoingData(const Data& data, Face& outFace)
456{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700457 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700458 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
459 return;
460 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700461 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
462
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700463 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700464 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700465 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700466 if (isViolatingLocalhost) {
467 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
468 " data=" << data.getName() << " violates /localhost");
469 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700470 return;
471 }
472
Junxiao Shif3c07812014-03-11 21:48:49 -0700473 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700474
Junxiao Shid3c792f2014-01-30 00:46:13 -0700475 // send Data
476 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700477 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700478}
479
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700480void
481Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
482{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000483 // receive Nack
484 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700485 ++m_counters.nInNacks;
486
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700487 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700488 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700489 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
490 " nack=" << nack.getInterest().getName() <<
491 "~" << nack.getReason() << " face-is-multi-access");
492 return;
493 }
494
495 // PIT match
496 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
497 // if no PIT entry found, drop
498 if (pitEntry == nullptr) {
499 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
500 " nack=" << nack.getInterest().getName() <<
501 "~" << nack.getReason() << " no-PIT-entry");
502 return;
503 }
504
505 // has out-record?
506 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
507 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700508 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700509 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
510 " nack=" << nack.getInterest().getName() <<
511 "~" << nack.getReason() << " no-out-record");
512 return;
513 }
514
515 // if out-record has different Nonce, drop
516 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
517 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
518 " nack=" << nack.getInterest().getName() <<
519 "~" << nack.getReason() << " wrong-Nonce " <<
520 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
521 return;
522 }
523
524 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
525 " nack=" << nack.getInterest().getName() <<
526 "~" << nack.getReason() << " OK");
527
528 // record Nack on out-record
529 outRecord->setIncomingNack(nack);
530
531 // trigger strategy: after receive NACK
532 shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
533 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveNack, _1,
534 cref(inFace), cref(nack), fibEntry, pitEntry));
535}
536
537void
538Forwarder::onOutgoingNack(shared_ptr<pit::Entry> pitEntry, const Face& outFace,
539 const lp::NackHeader& nack)
540{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700541 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700542 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
543 " nack=" << pitEntry->getInterest().getName() <<
544 "~" << nack.getReason() << " no-in-record");
545 return;
546 }
547
548 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700549 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700550
551 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700552 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700553 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
554 " nack=" << pitEntry->getInterest().getName() <<
555 "~" << nack.getReason() << " no-in-record");
556 return;
557 }
558
559 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700560 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700561 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
562 " nack=" << pitEntry->getInterest().getName() <<
563 "~" << nack.getReason() << " face-is-multi-access");
564 return;
565 }
566
567 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
568 " nack=" << pitEntry->getInterest().getName() <<
569 "~" << nack.getReason() << " OK");
570
571 // create Nack packet with the Interest from in-record
572 lp::Nack nackPkt(inRecord->getInterest());
573 nackPkt.setHeader(nack);
574
575 // erase in-record
576 pitEntry->deleteInRecord(outFace);
577
578 // send Nack on face
579 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700580 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700581}
582
Junxiao Shid3c792f2014-01-30 00:46:13 -0700583static inline bool
584compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
585{
586 return a.getExpiry() < b.getExpiry();
587}
588
589void
590Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
591{
Junxiao Shi4846f372016-04-05 13:39:30 -0700592 pit::InRecordCollection::iterator lastExpiring =
593 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700594
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700595 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700596 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
597 if (lastExpiryFromNow <= time::seconds::zero()) {
598 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700599 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700600
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700601 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700602 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700603 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
604}
605
606void
Junxiao Shia110f262014-10-12 12:35:20 -0700607Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
608 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700609{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700610 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700611
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700612 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700613 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700614 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700615}
616
617void
618Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
619{
Junxiao Shic041ca32014-02-25 20:01:15 -0700620 scheduler::cancel(pitEntry->m_unsatisfyTimer);
621 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700622}
623
Junxiao Shia110f262014-10-12 12:35:20 -0700624static inline void
625insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
626 const pit::OutRecord& outRecord)
627{
628 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
629}
630
631void
632Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
633 const time::milliseconds& dataFreshnessPeriod,
634 Face* upstream)
635{
636 // need Dead Nonce List insert?
637 bool needDnl = false;
638 if (isSatisfied) {
639 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
640 // Data never becomes stale if it doesn't have FreshnessPeriod field
641 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
642 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
643 }
644 else {
645 needDnl = true;
646 }
647
648 if (!needDnl) {
649 return;
650 }
651
652 // Dead Nonce List insert
653 if (upstream == 0) {
654 // insert all outgoing Nonces
655 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
656 std::for_each(outRecords.begin(), outRecords.end(),
657 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
658 }
659 else {
660 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700661 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700662 if (outRecord != pitEntry.getOutRecords().end()) {
663 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
664 }
665 }
666}
667
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800668} // namespace nfd