blob: e8a0ade5ad3fb6e0d4daaaa9406ece337adcab21 [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"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027#include "core/logger.hpp"
Junxiao Shiaf6569a2014-06-14 00:01:34 -070028#include "core/random.hpp"
Junxiao Shifaf3eb02015-02-16 10:50:36 -070029#include "strategy.hpp"
Junxiao Shiaf6569a2014-06-14 00:01:34 -070030#include <boost/random/uniform_int_distribution.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080031
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080032namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080033
Junxiao Shi8c8d2182014-01-30 22:33:00 -070034NFD_LOG_INIT("Forwarder");
35
Junxiao Shif3c07812014-03-11 21:48:49 -070036using fw::Strategy;
37
Junxiao Shif3c07812014-03-11 21:48:49 -070038const Name Forwarder::LOCALHOST_NAME("ndn:/localhost");
Junxiao Shi88884492014-02-15 15:57:43 -070039
Junxiao Shic041ca32014-02-25 20:01:15 -070040Forwarder::Forwarder()
Junxiao Shia4f2be82014-03-02 22:56:41 -070041 : m_faceTable(*this)
HangZhangad4afd12014-03-01 11:03:08 +080042 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060043 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080044 , m_measurements(m_nameTree)
Junxiao Shif3c07812014-03-11 21:48:49 -070045 , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080046{
Junxiao Shif3c07812014-03-11 21:48:49 -070047 fw::installStrategies(*this);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080048}
49
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060050Forwarder::~Forwarder()
51{
Junxiao Shi0355e9f2015-09-02 07:24:53 -070052}
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060053
Junxiao Shi0355e9f2015-09-02 07:24:53 -070054void
55Forwarder::startProcessInterest(Face& face, const Interest& interest)
56{
57 // check fields used by forwarding are well-formed
58 try {
59 if (interest.hasLink()) {
60 interest.getLink();
61 }
62 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -070063 catch (const tlv::Error&) {
Junxiao Shi0355e9f2015-09-02 07:24:53 -070064 NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() <<
65 " interest=" << interest.getName() << " malformed");
66 // It's safe to call interest.getName() because Name has been fully parsed
67 return;
68 }
69
70 this->onIncomingInterest(face, interest);
71}
72
73void
74Forwarder::startProcessData(Face& face, const Data& data)
75{
76 // check fields used by forwarding are well-formed
77 // (none needed)
78
79 this->onIncomingData(face, data);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060080}
81
Alexander Afanasyev33b72772014-01-26 23:22:58 -080082void
Junxiao Shi5e5e4452015-09-24 16:56:52 -070083Forwarder::startProcessNack(Face& face, const lp::Nack& nack)
84{
85 // check fields used by forwarding are well-formed
86 try {
87 if (nack.getInterest().hasLink()) {
88 nack.getInterest().getLink();
89 }
90 }
91 catch (const tlv::Error&) {
92 NFD_LOG_DEBUG("startProcessNack face=" << face.getId() <<
93 " nack=" << nack.getInterest().getName() <<
94 "~" << nack.getReason() << " malformed");
95 return;
96 }
97
98 this->onIncomingNack(face, nack);
99}
100
101void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700102Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
103{
104 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700105 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
106 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000107 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700108 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -0700109
Junxiao Shi88884492014-02-15 15:57:43 -0700110 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700111 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700112 LOCALHOST_NAME.isPrefixOf(interest.getName());
113 if (isViolatingLocalhost) {
114 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
115 " interest=" << interest.getName() << " violates /localhost");
116 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700117 return;
118 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700119
Junxiao Shi330136a2016-03-10 04:53:08 -0700120 // detect duplicate Nonce with Dead Nonce List
121 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
122 if (hasDuplicateNonceInDnl) {
123 // goto Interest loop pipeline
124 this->onInterestLoop(inFace, interest);
125 return;
126 }
127
Junxiao Shid3c792f2014-01-30 00:46:13 -0700128 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700129 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700130
Junxiao Shi330136a2016-03-10 04:53:08 -0700131 // detect duplicate Nonce in PIT entry
132 bool hasDuplicateNonceInPit = pitEntry->findNonce(interest.getNonce(), inFace) !=
133 pit::DUPLICATE_NONCE_NONE;
134 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700135 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700136 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700137 return;
138 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700139
Junxiao Shid3c792f2014-01-30 00:46:13 -0700140 // cancel unsatisfy & straggler timer
141 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700142
Junxiao Shif3c07812014-03-11 21:48:49 -0700143 // is pending?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700144 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shie17349a2014-03-25 00:55:38 -0700145 bool isPending = inRecords.begin() != inRecords.end();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700146 if (!isPending) {
mzhang4eab72492015-02-25 11:16:09 -0600147 m_cs.find(interest,
148 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
149 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700150 }
mzhang4eab72492015-02-25 11:16:09 -0600151 else {
152 this->onContentStoreMiss(inFace, pitEntry, interest);
153 }
154}
Junxiao Shic041ca32014-02-25 20:01:15 -0700155
mzhang4eab72492015-02-25 11:16:09 -0600156void
Junxiao Shi330136a2016-03-10 04:53:08 -0700157Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700158{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700159 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700160 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700161 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
162 " interest=" << interest.getName() <<
163 " drop");
164 return;
165 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700166
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700167 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
168 " interest=" << interest.getName() <<
169 " send-Nack-duplicate");
170
171 // send Nack with reason=DUPLICATE
172 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
173 lp::Nack nack(interest);
174 nack.setReason(lp::NackReason::DUPLICATE);
175 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700176}
177
178void
mzhang4eab72492015-02-25 11:16:09 -0600179Forwarder::onContentStoreMiss(const Face& inFace,
180 shared_ptr<pit::Entry> pitEntry,
181 const Interest& interest)
182{
183 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
184
185 shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700186 // insert InRecord
mzhang4eab72492015-02-25 11:16:09 -0600187 pitEntry->insertOrUpdateInRecord(face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700188
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700189 // set PIT unsatisfy timer
190 this->setUnsatisfyTimer(pitEntry);
191
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700192 shared_ptr<fib::Entry> fibEntry;
193 // has Link object?
194 if (!interest.hasLink()) {
195 // FIB lookup with Interest name
196 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
197 NFD_LOG_TRACE("onContentStoreMiss noLinkObject");
198 }
199 else {
200 const Link& link = interest.getLink();
201
202 // in producer region?
203 if (m_networkRegionTable.isInProducerRegion(link)) {
204 // FIB lookup with Interest name
205 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
206 NFD_LOG_TRACE("onContentStoreMiss inProducerRegion");
207 }
208 // has SelectedDelegation?
209 else if (interest.hasSelectedDelegation()) {
210 // FIB lookup with SelectedDelegation
211 fibEntry = m_fib.findLongestPrefixMatch(interest.getSelectedDelegation());
212 NFD_LOG_TRACE("onContentStoreMiss hasSelectedDelegation=" << interest.getSelectedDelegation());
213 }
214 else {
215 // FIB lookup with first delegation Name
216 fibEntry = m_fib.findLongestPrefixMatch(link.getDelegations().begin()->second);
217
218 // in default-free zone?
219 bool isDefaultFreeZone = !(fibEntry->getPrefix().size() == 0 && fibEntry->hasNextHops());
220 if (isDefaultFreeZone) {
221 // choose and set SelectedDelegation
222 for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) {
223 const Name& delegationName = delegation.second;
224 fibEntry = m_fib.findLongestPrefixMatch(delegationName);
225 if (fibEntry->hasNextHops()) {
226 const_cast<Interest&>(interest).setSelectedDelegation(delegationName);
227 NFD_LOG_TRACE("onContentStoreMiss enterDefaultFreeZone"
228 << " setSelectedDelegation=" << delegationName);
229 break;
230 }
231 }
232 }
233 else {
234 NFD_LOG_TRACE("onContentStoreMiss inConsumerRegion");
235 }
236 }
237 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700238
Junxiao Shid3c792f2014-01-30 00:46:13 -0700239 // dispatch to strategy
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700240 BOOST_ASSERT(fibEntry != nullptr);
Junxiao Shif3c07812014-03-11 21:48:49 -0700241 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700242 cref(inFace), cref(interest), fibEntry, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700243}
244
245void
mzhang4eab72492015-02-25 11:16:09 -0600246Forwarder::onContentStoreHit(const Face& inFace,
247 shared_ptr<pit::Entry> pitEntry,
248 const Interest& interest,
249 const Data& data)
250{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500251 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600252
Junxiao Shicde37ad2015-12-24 01:02:05 -0700253 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600254 // XXX should we lookup PIT for other Interests that also match csMatch?
255
256 // set PIT straggler timer
257 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
258
259 // goto outgoing Data pipeline
260 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
261}
262
Junxiao Shif3c07812014-03-11 21:48:49 -0700263/** \brief compare two InRecords for picking outgoing Interest
264 * \return true if b is preferred over a
265 *
266 * This function should be passed to std::max_element over InRecordCollection.
267 * The outgoing Interest picked is the last incoming Interest
268 * that does not come from outFace.
269 * If all InRecords come from outFace, it's fine to pick that. This happens when
270 * there's only one InRecord that comes from outFace. The legit use is for
271 * vehicular network; otherwise, strategy shouldn't send to the sole inFace.
272 */
273static inline bool
274compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace)
275{
276 bool isOutFaceA = a.getFace().get() == outFace;
277 bool isOutFaceB = b.getFace().get() == outFace;
278
279 if (!isOutFaceA && isOutFaceB) {
280 return false;
281 }
282 if (isOutFaceA && !isOutFaceB) {
283 return true;
284 }
285
286 return a.getLastRenewed() > b.getLastRenewed();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700287}
288
289void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700290Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
291 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700292{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700293 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700294 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
295 return;
296 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700297 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
298 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700299
Junxiao Shi57f0f312014-03-16 11:52:20 -0700300 // scope control
301 if (pitEntry->violatesScope(outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700302 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700303 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700304 return;
305 }
306
Junxiao Shid3c792f2014-01-30 00:46:13 -0700307 // pick Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700308 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
309 pit::InRecordCollection::const_iterator pickedInRecord = std::max_element(
310 inRecords.begin(), inRecords.end(), bind(&compare_pickInterest, _1, _2, &outFace));
311 BOOST_ASSERT(pickedInRecord != inRecords.end());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700312 shared_ptr<Interest> interest = const_pointer_cast<Interest>(
313 pickedInRecord->getInterest().shared_from_this());
314
315 if (wantNewNonce) {
316 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700317 static boost::random::uniform_int_distribution<uint32_t> dist;
318 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700319 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700320
Junxiao Shid3c792f2014-01-30 00:46:13 -0700321 // insert OutRecord
Junxiao Shid938a6b2014-05-11 23:40:29 -0700322 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700323
Junxiao Shid3c792f2014-01-30 00:46:13 -0700324 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700325 outFace.sendInterest(*interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700326 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700327}
328
329void
Junxiao Shi09498f02014-02-26 19:41:08 -0700330Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700331{
Junxiao Shid938a6b2014-05-11 23:40:29 -0700332 if (pitEntry->hasUnexpiredOutRecords()) {
333 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
334 " cannot reject forwarded Interest");
335 return;
336 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700337 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700338
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700339 // cancel unsatisfy & straggler timer
340 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
341
Junxiao Shid3c792f2014-01-30 00:46:13 -0700342 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700343 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700344}
345
346void
347Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
348{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700349 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
350
Junxiao Shid3c792f2014-01-30 00:46:13 -0700351 // invoke PIT unsatisfied callback
Junxiao Shif3c07812014-03-11 21:48:49 -0700352 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700353 pitEntry));
Junxiao Shic041ca32014-02-25 20:01:15 -0700354
Junxiao Shia110f262014-10-12 12:35:20 -0700355 // goto Interest Finalize pipeline
356 this->onInterestFinalize(pitEntry, false);
357}
358
359void
360Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
361 const time::milliseconds& dataFreshnessPeriod)
362{
363 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
364 (isSatisfied ? " satisfied" : " unsatisfied"));
365
366 // Dead Nonce List insert if necessary
367 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
368
Junxiao Shif3c07812014-03-11 21:48:49 -0700369 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700370 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600371 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700372}
373
374void
375Forwarder::onIncomingData(Face& inFace, const Data& data)
376{
377 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700378 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000379 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700380 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700381
Junxiao Shi88884492014-02-15 15:57:43 -0700382 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700383 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700384 LOCALHOST_NAME.isPrefixOf(data.getName());
385 if (isViolatingLocalhost) {
386 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
387 " data=" << data.getName() << " violates /localhost");
388 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700389 return;
390 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700391
Junxiao Shid3c792f2014-01-30 00:46:13 -0700392 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700393 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
394 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700395 // goto Data unsolicited pipeline
396 this->onDataUnsolicited(inFace, data);
397 return;
398 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700399
Junxiao Shid3c792f2014-01-30 00:46:13 -0700400 // CS insert
401 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700402
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700403 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700404 // foreach PitEntry
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700405 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700406 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700407
Junxiao Shid3c792f2014-01-30 00:46:13 -0700408 // cancel unsatisfy & straggler timer
409 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700410
Junxiao Shid3c792f2014-01-30 00:46:13 -0700411 // remember pending downstreams
412 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700413 for (const pit::InRecord& inRecord : inRecords) {
414 if (inRecord.getExpiry() > time::steady_clock::now()) {
415 pendingDownstreams.insert(inRecord.getFace().get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700416 }
417 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700418
Junxiao Shid938a6b2014-05-11 23:40:29 -0700419 // invoke PIT satisfy callback
Junxiao Shi82e7f582014-09-07 15:15:40 -0700420 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700421 pitEntry, cref(inFace), cref(data)));
422
Junxiao Shia110f262014-10-12 12:35:20 -0700423 // Dead Nonce List insert if necessary (for OutRecord of inFace)
424 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
425
Junxiao Shid3c792f2014-01-30 00:46:13 -0700426 // mark PIT satisfied
427 pitEntry->deleteInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700428 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700429
Junxiao Shid3c792f2014-01-30 00:46:13 -0700430 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700431 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700432 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700433
Junxiao Shid3c792f2014-01-30 00:46:13 -0700434 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700435 for (Face* pendingDownstream : pendingDownstreams) {
436 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700437 continue;
438 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700439 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700440 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700441 }
442}
443
444void
445Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
446{
447 // accept to cache?
Junxiao Shicde37ad2015-12-24 01:02:05 -0700448 bool acceptToCache = inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700449 if (acceptToCache) {
450 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700451 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700452 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700453
Junxiao Shif3c07812014-03-11 21:48:49 -0700454 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
455 " data=" << data.getName() <<
456 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700457}
458
459void
460Forwarder::onOutgoingData(const Data& data, Face& outFace)
461{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700462 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700463 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
464 return;
465 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700466 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
467
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700468 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700469 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700470 LOCALHOST_NAME.isPrefixOf(data.getName());
471 if (isViolatingLocalhost) {
472 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
473 " data=" << data.getName() << " violates /localhost");
474 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700475 return;
476 }
477
Junxiao Shif3c07812014-03-11 21:48:49 -0700478 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700479
Junxiao Shid3c792f2014-01-30 00:46:13 -0700480 // send Data
481 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700482 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700483}
484
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700485void
486Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
487{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000488 // receive Nack
489 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700490 ++m_counters.nInNacks;
491
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700492 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700493 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700494 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
495 " nack=" << nack.getInterest().getName() <<
496 "~" << nack.getReason() << " face-is-multi-access");
497 return;
498 }
499
500 // PIT match
501 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
502 // if no PIT entry found, drop
503 if (pitEntry == nullptr) {
504 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
505 " nack=" << nack.getInterest().getName() <<
506 "~" << nack.getReason() << " no-PIT-entry");
507 return;
508 }
509
510 // has out-record?
511 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
512 // if no out-record found, drop
513 if (outRecord == pitEntry->getOutRecords().end()) {
514 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
515 " nack=" << nack.getInterest().getName() <<
516 "~" << nack.getReason() << " no-out-record");
517 return;
518 }
519
520 // if out-record has different Nonce, drop
521 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
522 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
523 " nack=" << nack.getInterest().getName() <<
524 "~" << nack.getReason() << " wrong-Nonce " <<
525 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
526 return;
527 }
528
529 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
530 " nack=" << nack.getInterest().getName() <<
531 "~" << nack.getReason() << " OK");
532
533 // record Nack on out-record
534 outRecord->setIncomingNack(nack);
535
536 // trigger strategy: after receive NACK
537 shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
538 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveNack, _1,
539 cref(inFace), cref(nack), fibEntry, pitEntry));
540}
541
542void
543Forwarder::onOutgoingNack(shared_ptr<pit::Entry> pitEntry, const Face& outFace,
544 const lp::NackHeader& nack)
545{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700546 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700547 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
548 " nack=" << pitEntry->getInterest().getName() <<
549 "~" << nack.getReason() << " no-in-record");
550 return;
551 }
552
553 // has in-record?
554 pit::InRecordCollection::const_iterator inRecord = pitEntry->getInRecord(outFace);
555
556 // if no in-record found, drop
557 if (inRecord == pitEntry->getInRecords().end()) {
558 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
559 " nack=" << pitEntry->getInterest().getName() <<
560 "~" << nack.getReason() << " no-in-record");
561 return;
562 }
563
564 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700565 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700566 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
567 " nack=" << pitEntry->getInterest().getName() <<
568 "~" << nack.getReason() << " face-is-multi-access");
569 return;
570 }
571
572 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
573 " nack=" << pitEntry->getInterest().getName() <<
574 "~" << nack.getReason() << " OK");
575
576 // create Nack packet with the Interest from in-record
577 lp::Nack nackPkt(inRecord->getInterest());
578 nackPkt.setHeader(nack);
579
580 // erase in-record
581 pitEntry->deleteInRecord(outFace);
582
583 // send Nack on face
584 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700585 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700586}
587
Junxiao Shid3c792f2014-01-30 00:46:13 -0700588static inline bool
589compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
590{
591 return a.getExpiry() < b.getExpiry();
592}
593
594void
595Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
596{
597 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
598 pit::InRecordCollection::const_iterator lastExpiring =
599 std::max_element(inRecords.begin(), inRecords.end(),
600 &compare_InRecord_expiry);
601
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700602 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
603 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700604 if (lastExpiryFromNow <= time::seconds(0)) {
605 // TODO all InRecords are already expired; will this happen?
606 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700607
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700608 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700609 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700610 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
611}
612
613void
Junxiao Shia110f262014-10-12 12:35:20 -0700614Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
615 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700616{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700617 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700618
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700619 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700620 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700621 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700622}
623
624void
625Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
626{
Junxiao Shic041ca32014-02-25 20:01:15 -0700627 scheduler::cancel(pitEntry->m_unsatisfyTimer);
628 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700629}
630
Junxiao Shia110f262014-10-12 12:35:20 -0700631static inline void
632insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
633 const pit::OutRecord& outRecord)
634{
635 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
636}
637
638void
639Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
640 const time::milliseconds& dataFreshnessPeriod,
641 Face* upstream)
642{
643 // need Dead Nonce List insert?
644 bool needDnl = false;
645 if (isSatisfied) {
646 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
647 // Data never becomes stale if it doesn't have FreshnessPeriod field
648 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
649 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
650 }
651 else {
652 needDnl = true;
653 }
654
655 if (!needDnl) {
656 return;
657 }
658
659 // Dead Nonce List insert
660 if (upstream == 0) {
661 // insert all outgoing Nonces
662 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
663 std::for_each(outRecords.begin(), outRecords.end(),
664 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
665 }
666 else {
667 // insert outgoing Nonce of a specific face
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700668 pit::OutRecordCollection::const_iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700669 if (outRecord != pitEntry.getOutRecords().end()) {
670 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
671 }
672 }
673}
674
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800675} // namespace nfd