blob: c823ae6468169d2dd8eacd169443ff6b90b6ae18 [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) {
Spyridon Mastorakis31d4a082014-12-05 22:43:34 -0800147 if (m_csFromNdnSim == nullptr) {
148 m_cs.find(interest,
149 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
150 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
151 }
152 else {
153 shared_ptr<Data> match = m_csFromNdnSim->Lookup(interest.shared_from_this());
154 if (match != nullptr) {
155 this->onContentStoreHit(inFace, pitEntry, interest, *match);
156 }
157 else {
158 this->onContentStoreMiss(inFace, pitEntry, interest);
159 }
160 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700161 }
mzhang4eab72492015-02-25 11:16:09 -0600162 else {
163 this->onContentStoreMiss(inFace, pitEntry, interest);
164 }
165}
Junxiao Shic041ca32014-02-25 20:01:15 -0700166
mzhang4eab72492015-02-25 11:16:09 -0600167void
Junxiao Shi330136a2016-03-10 04:53:08 -0700168Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700169{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700170 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700171 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700172 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
173 " interest=" << interest.getName() <<
174 " drop");
175 return;
176 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700177
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700178 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
179 " interest=" << interest.getName() <<
180 " send-Nack-duplicate");
181
182 // send Nack with reason=DUPLICATE
183 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
184 lp::Nack nack(interest);
185 nack.setReason(lp::NackReason::DUPLICATE);
186 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700187}
188
189void
mzhang4eab72492015-02-25 11:16:09 -0600190Forwarder::onContentStoreMiss(const Face& inFace,
191 shared_ptr<pit::Entry> pitEntry,
192 const Interest& interest)
193{
194 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
195
196 shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700197 // insert InRecord
mzhang4eab72492015-02-25 11:16:09 -0600198 pitEntry->insertOrUpdateInRecord(face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700199
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700200 // set PIT unsatisfy timer
201 this->setUnsatisfyTimer(pitEntry);
202
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700203 shared_ptr<fib::Entry> fibEntry;
204 // has Link object?
205 if (!interest.hasLink()) {
206 // FIB lookup with Interest name
207 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
208 NFD_LOG_TRACE("onContentStoreMiss noLinkObject");
209 }
210 else {
211 const Link& link = interest.getLink();
212
213 // in producer region?
214 if (m_networkRegionTable.isInProducerRegion(link)) {
215 // FIB lookup with Interest name
216 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
217 NFD_LOG_TRACE("onContentStoreMiss inProducerRegion");
218 }
219 // has SelectedDelegation?
220 else if (interest.hasSelectedDelegation()) {
221 // FIB lookup with SelectedDelegation
222 fibEntry = m_fib.findLongestPrefixMatch(interest.getSelectedDelegation());
223 NFD_LOG_TRACE("onContentStoreMiss hasSelectedDelegation=" << interest.getSelectedDelegation());
224 }
225 else {
226 // FIB lookup with first delegation Name
227 fibEntry = m_fib.findLongestPrefixMatch(link.getDelegations().begin()->second);
228
229 // in default-free zone?
230 bool isDefaultFreeZone = !(fibEntry->getPrefix().size() == 0 && fibEntry->hasNextHops());
231 if (isDefaultFreeZone) {
232 // choose and set SelectedDelegation
233 for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) {
234 const Name& delegationName = delegation.second;
235 fibEntry = m_fib.findLongestPrefixMatch(delegationName);
236 if (fibEntry->hasNextHops()) {
237 const_cast<Interest&>(interest).setSelectedDelegation(delegationName);
238 NFD_LOG_TRACE("onContentStoreMiss enterDefaultFreeZone"
239 << " setSelectedDelegation=" << delegationName);
240 break;
241 }
242 }
243 }
244 else {
245 NFD_LOG_TRACE("onContentStoreMiss inConsumerRegion");
246 }
247 }
248 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700249
Junxiao Shid3c792f2014-01-30 00:46:13 -0700250 // dispatch to strategy
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700251 BOOST_ASSERT(fibEntry != nullptr);
Junxiao Shif3c07812014-03-11 21:48:49 -0700252 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700253 cref(inFace), cref(interest), fibEntry, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700254}
255
256void
mzhang4eab72492015-02-25 11:16:09 -0600257Forwarder::onContentStoreHit(const Face& inFace,
258 shared_ptr<pit::Entry> pitEntry,
259 const Interest& interest,
260 const Data& data)
261{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500262 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600263
Junxiao Shicde37ad2015-12-24 01:02:05 -0700264 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600265 // XXX should we lookup PIT for other Interests that also match csMatch?
266
267 // set PIT straggler timer
268 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
269
270 // goto outgoing Data pipeline
271 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
272}
273
Junxiao Shif3c07812014-03-11 21:48:49 -0700274/** \brief compare two InRecords for picking outgoing Interest
275 * \return true if b is preferred over a
276 *
277 * This function should be passed to std::max_element over InRecordCollection.
278 * The outgoing Interest picked is the last incoming Interest
279 * that does not come from outFace.
280 * If all InRecords come from outFace, it's fine to pick that. This happens when
281 * there's only one InRecord that comes from outFace. The legit use is for
282 * vehicular network; otherwise, strategy shouldn't send to the sole inFace.
283 */
284static inline bool
285compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace)
286{
287 bool isOutFaceA = a.getFace().get() == outFace;
288 bool isOutFaceB = b.getFace().get() == outFace;
289
290 if (!isOutFaceA && isOutFaceB) {
291 return false;
292 }
293 if (isOutFaceA && !isOutFaceB) {
294 return true;
295 }
296
297 return a.getLastRenewed() > b.getLastRenewed();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700298}
299
300void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700301Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
302 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700303{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700304 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700305 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
306 return;
307 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700308 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
309 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700310
Junxiao Shi57f0f312014-03-16 11:52:20 -0700311 // scope control
312 if (pitEntry->violatesScope(outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700313 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700314 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700315 return;
316 }
317
Junxiao Shid3c792f2014-01-30 00:46:13 -0700318 // pick Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700319 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
320 pit::InRecordCollection::const_iterator pickedInRecord = std::max_element(
321 inRecords.begin(), inRecords.end(), bind(&compare_pickInterest, _1, _2, &outFace));
322 BOOST_ASSERT(pickedInRecord != inRecords.end());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700323 shared_ptr<Interest> interest = const_pointer_cast<Interest>(
324 pickedInRecord->getInterest().shared_from_this());
325
326 if (wantNewNonce) {
327 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700328 static boost::random::uniform_int_distribution<uint32_t> dist;
329 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700330 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700331
Junxiao Shid3c792f2014-01-30 00:46:13 -0700332 // insert OutRecord
Junxiao Shid938a6b2014-05-11 23:40:29 -0700333 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700334
Junxiao Shid3c792f2014-01-30 00:46:13 -0700335 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700336 outFace.sendInterest(*interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700337 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700338}
339
340void
Junxiao Shi09498f02014-02-26 19:41:08 -0700341Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700342{
Junxiao Shid938a6b2014-05-11 23:40:29 -0700343 if (pitEntry->hasUnexpiredOutRecords()) {
344 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
345 " cannot reject forwarded Interest");
346 return;
347 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700348 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700349
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700350 // cancel unsatisfy & straggler timer
351 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
352
Junxiao Shid3c792f2014-01-30 00:46:13 -0700353 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700354 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700355}
356
357void
358Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
359{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700360 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
361
Junxiao Shid3c792f2014-01-30 00:46:13 -0700362 // invoke PIT unsatisfied callback
Junxiao Shif3c07812014-03-11 21:48:49 -0700363 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700364 pitEntry));
Junxiao Shic041ca32014-02-25 20:01:15 -0700365
Junxiao Shia110f262014-10-12 12:35:20 -0700366 // goto Interest Finalize pipeline
367 this->onInterestFinalize(pitEntry, false);
368}
369
370void
371Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
372 const time::milliseconds& dataFreshnessPeriod)
373{
374 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
375 (isSatisfied ? " satisfied" : " unsatisfied"));
376
377 // Dead Nonce List insert if necessary
378 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
379
Junxiao Shif3c07812014-03-11 21:48:49 -0700380 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700381 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600382 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700383}
384
385void
386Forwarder::onIncomingData(Face& inFace, const Data& data)
387{
388 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700389 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000390 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700391 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700392
Junxiao Shi88884492014-02-15 15:57:43 -0700393 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700394 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700395 LOCALHOST_NAME.isPrefixOf(data.getName());
396 if (isViolatingLocalhost) {
397 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
398 " data=" << data.getName() << " violates /localhost");
399 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700400 return;
401 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700402
Junxiao Shid3c792f2014-01-30 00:46:13 -0700403 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700404 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
405 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700406 // goto Data unsolicited pipeline
407 this->onDataUnsolicited(inFace, data);
408 return;
409 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700410
Junxiao Shid3c792f2014-01-30 00:46:13 -0700411 // CS insert
Spyridon Mastorakis31d4a082014-12-05 22:43:34 -0800412 if (m_csFromNdnSim == nullptr)
413 m_cs.insert(data);
414 else
415 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shic041ca32014-02-25 20:01:15 -0700416
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700417 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700418 // foreach PitEntry
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700419 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700420 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700421
Junxiao Shid3c792f2014-01-30 00:46:13 -0700422 // cancel unsatisfy & straggler timer
423 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700424
Junxiao Shid3c792f2014-01-30 00:46:13 -0700425 // remember pending downstreams
426 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700427 for (const pit::InRecord& inRecord : inRecords) {
428 if (inRecord.getExpiry() > time::steady_clock::now()) {
429 pendingDownstreams.insert(inRecord.getFace().get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700430 }
431 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700432
Junxiao Shid938a6b2014-05-11 23:40:29 -0700433 // invoke PIT satisfy callback
Junxiao Shi82e7f582014-09-07 15:15:40 -0700434 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700435 pitEntry, cref(inFace), cref(data)));
436
Junxiao Shia110f262014-10-12 12:35:20 -0700437 // Dead Nonce List insert if necessary (for OutRecord of inFace)
438 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
439
Junxiao Shid3c792f2014-01-30 00:46:13 -0700440 // mark PIT satisfied
441 pitEntry->deleteInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700442 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700443
Junxiao Shid3c792f2014-01-30 00:46:13 -0700444 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700445 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700446 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700447
Junxiao Shid3c792f2014-01-30 00:46:13 -0700448 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700449 for (Face* pendingDownstream : pendingDownstreams) {
450 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700451 continue;
452 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700453 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700454 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700455 }
456}
457
458void
459Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
460{
461 // accept to cache?
Junxiao Shicde37ad2015-12-24 01:02:05 -0700462 bool acceptToCache = inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700463 if (acceptToCache) {
464 // CS insert
Spyridon Mastorakis31d4a082014-12-05 22:43:34 -0800465 if (m_csFromNdnSim == nullptr)
466 m_cs.insert(data, true);
467 else
468 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700469 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700470
Junxiao Shif3c07812014-03-11 21:48:49 -0700471 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
472 " data=" << data.getName() <<
473 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700474}
475
476void
477Forwarder::onOutgoingData(const Data& data, Face& outFace)
478{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700479 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700480 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
481 return;
482 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700483 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
484
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700485 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700486 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700487 LOCALHOST_NAME.isPrefixOf(data.getName());
488 if (isViolatingLocalhost) {
489 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
490 " data=" << data.getName() << " violates /localhost");
491 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700492 return;
493 }
494
Junxiao Shif3c07812014-03-11 21:48:49 -0700495 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700496
Junxiao Shid3c792f2014-01-30 00:46:13 -0700497 // send Data
498 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700499 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700500}
501
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700502void
503Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
504{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000505 // receive Nack
506 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700507 ++m_counters.nInNacks;
508
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700509 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700510 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700511 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
512 " nack=" << nack.getInterest().getName() <<
513 "~" << nack.getReason() << " face-is-multi-access");
514 return;
515 }
516
517 // PIT match
518 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
519 // if no PIT entry found, drop
520 if (pitEntry == nullptr) {
521 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
522 " nack=" << nack.getInterest().getName() <<
523 "~" << nack.getReason() << " no-PIT-entry");
524 return;
525 }
526
527 // has out-record?
528 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
529 // if no out-record found, drop
530 if (outRecord == pitEntry->getOutRecords().end()) {
531 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
532 " nack=" << nack.getInterest().getName() <<
533 "~" << nack.getReason() << " no-out-record");
534 return;
535 }
536
537 // if out-record has different Nonce, drop
538 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
539 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
540 " nack=" << nack.getInterest().getName() <<
541 "~" << nack.getReason() << " wrong-Nonce " <<
542 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
543 return;
544 }
545
546 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
547 " nack=" << nack.getInterest().getName() <<
548 "~" << nack.getReason() << " OK");
549
550 // record Nack on out-record
551 outRecord->setIncomingNack(nack);
552
553 // trigger strategy: after receive NACK
554 shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
555 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveNack, _1,
556 cref(inFace), cref(nack), fibEntry, pitEntry));
557}
558
559void
560Forwarder::onOutgoingNack(shared_ptr<pit::Entry> pitEntry, const Face& outFace,
561 const lp::NackHeader& nack)
562{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700563 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700564 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
565 " nack=" << pitEntry->getInterest().getName() <<
566 "~" << nack.getReason() << " no-in-record");
567 return;
568 }
569
570 // has in-record?
571 pit::InRecordCollection::const_iterator inRecord = pitEntry->getInRecord(outFace);
572
573 // if no in-record found, drop
574 if (inRecord == pitEntry->getInRecords().end()) {
575 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
576 " nack=" << pitEntry->getInterest().getName() <<
577 "~" << nack.getReason() << " no-in-record");
578 return;
579 }
580
581 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700582 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700583 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
584 " nack=" << pitEntry->getInterest().getName() <<
585 "~" << nack.getReason() << " face-is-multi-access");
586 return;
587 }
588
589 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
590 " nack=" << pitEntry->getInterest().getName() <<
591 "~" << nack.getReason() << " OK");
592
593 // create Nack packet with the Interest from in-record
594 lp::Nack nackPkt(inRecord->getInterest());
595 nackPkt.setHeader(nack);
596
597 // erase in-record
598 pitEntry->deleteInRecord(outFace);
599
600 // send Nack on face
601 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700602 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700603}
604
Junxiao Shid3c792f2014-01-30 00:46:13 -0700605static inline bool
606compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
607{
608 return a.getExpiry() < b.getExpiry();
609}
610
611void
612Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
613{
614 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
615 pit::InRecordCollection::const_iterator lastExpiring =
616 std::max_element(inRecords.begin(), inRecords.end(),
617 &compare_InRecord_expiry);
618
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700619 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
620 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700621 if (lastExpiryFromNow <= time::seconds(0)) {
622 // TODO all InRecords are already expired; will this happen?
623 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700624
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700625 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700626 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700627 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
628}
629
630void
Junxiao Shia110f262014-10-12 12:35:20 -0700631Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
632 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700633{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700634 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700635
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700636 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700637 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700638 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700639}
640
641void
642Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
643{
Junxiao Shic041ca32014-02-25 20:01:15 -0700644 scheduler::cancel(pitEntry->m_unsatisfyTimer);
645 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700646}
647
Junxiao Shia110f262014-10-12 12:35:20 -0700648static inline void
649insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
650 const pit::OutRecord& outRecord)
651{
652 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
653}
654
655void
656Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
657 const time::milliseconds& dataFreshnessPeriod,
658 Face* upstream)
659{
660 // need Dead Nonce List insert?
661 bool needDnl = false;
662 if (isSatisfied) {
663 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
664 // Data never becomes stale if it doesn't have FreshnessPeriod field
665 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
666 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
667 }
668 else {
669 needDnl = true;
670 }
671
672 if (!needDnl) {
673 return;
674 }
675
676 // Dead Nonce List insert
677 if (upstream == 0) {
678 // insert all outgoing Nonces
679 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
680 std::for_each(outRecords.begin(), outRecords.end(),
681 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
682 }
683 else {
684 // insert outgoing Nonce of a specific face
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700685 pit::OutRecordCollection::const_iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700686 if (outRecord != pitEntry.getOutRecords().end()) {
687 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
688 }
689 }
690}
691
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800692} // namespace nfd