blob: 07ca1d0b1b53da75e089b222d4b96bf389ea4236 [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"
Alexander Afanasyevd980dcd2015-01-08 21:41:48 -080030#include "face/null-face.hpp"
Junxiao Shiaf6569a2014-06-14 00:01:34 -070031#include <boost/random/uniform_int_distribution.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080032
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080033namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080034
Junxiao Shi8c8d2182014-01-30 22:33:00 -070035NFD_LOG_INIT("Forwarder");
36
Junxiao Shif3c07812014-03-11 21:48:49 -070037using fw::Strategy;
38
Junxiao Shif3c07812014-03-11 21:48:49 -070039const Name Forwarder::LOCALHOST_NAME("ndn:/localhost");
Junxiao Shi88884492014-02-15 15:57:43 -070040
Junxiao Shic041ca32014-02-25 20:01:15 -070041Forwarder::Forwarder()
Junxiao Shia4f2be82014-03-02 22:56:41 -070042 : m_faceTable(*this)
HangZhangad4afd12014-03-01 11:03:08 +080043 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060044 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080045 , m_measurements(m_nameTree)
Junxiao Shif3c07812014-03-11 21:48:49 -070046 , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this))
Alexander Afanasyevd980dcd2015-01-08 21:41:48 -080047 , m_csFace(face::makeNullFace(FaceUri("contentstore://")))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080048{
Junxiao Shif3c07812014-03-11 21:48:49 -070049 fw::installStrategies(*this);
Alexander Afanasyevd980dcd2015-01-08 21:41:48 -080050 getFaceTable().addReserved(m_csFace, face::FACEID_CONTENT_STORE);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080051}
52
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060053Forwarder::~Forwarder()
54{
Junxiao Shi0355e9f2015-09-02 07:24:53 -070055}
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060056
Junxiao Shi0355e9f2015-09-02 07:24:53 -070057void
58Forwarder::startProcessInterest(Face& face, const Interest& interest)
59{
60 // check fields used by forwarding are well-formed
61 try {
62 if (interest.hasLink()) {
63 interest.getLink();
64 }
65 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -070066 catch (const tlv::Error&) {
Junxiao Shi0355e9f2015-09-02 07:24:53 -070067 NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() <<
68 " interest=" << interest.getName() << " malformed");
69 // It's safe to call interest.getName() because Name has been fully parsed
70 return;
71 }
72
73 this->onIncomingInterest(face, interest);
74}
75
76void
77Forwarder::startProcessData(Face& face, const Data& data)
78{
79 // check fields used by forwarding are well-formed
80 // (none needed)
81
82 this->onIncomingData(face, data);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060083}
84
Alexander Afanasyev33b72772014-01-26 23:22:58 -080085void
Junxiao Shi5e5e4452015-09-24 16:56:52 -070086Forwarder::startProcessNack(Face& face, const lp::Nack& nack)
87{
88 // check fields used by forwarding are well-formed
89 try {
90 if (nack.getInterest().hasLink()) {
91 nack.getInterest().getLink();
92 }
93 }
94 catch (const tlv::Error&) {
95 NFD_LOG_DEBUG("startProcessNack face=" << face.getId() <<
96 " nack=" << nack.getInterest().getName() <<
97 "~" << nack.getReason() << " malformed");
98 return;
99 }
100
101 this->onIncomingNack(face, nack);
102}
103
104void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700105Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
106{
107 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700108 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
109 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000110 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700111 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -0700112
Junxiao Shi88884492014-02-15 15:57:43 -0700113 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700114 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700115 LOCALHOST_NAME.isPrefixOf(interest.getName());
116 if (isViolatingLocalhost) {
117 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
118 " interest=" << interest.getName() << " violates /localhost");
119 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700120 return;
121 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700122
Junxiao Shi330136a2016-03-10 04:53:08 -0700123 // detect duplicate Nonce with Dead Nonce List
124 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
125 if (hasDuplicateNonceInDnl) {
126 // goto Interest loop pipeline
127 this->onInterestLoop(inFace, interest);
128 return;
129 }
130
Junxiao Shid3c792f2014-01-30 00:46:13 -0700131 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700132 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700133
Junxiao Shi330136a2016-03-10 04:53:08 -0700134 // detect duplicate Nonce in PIT entry
135 bool hasDuplicateNonceInPit = pitEntry->findNonce(interest.getNonce(), inFace) !=
136 pit::DUPLICATE_NONCE_NONE;
137 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700138 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700139 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700140 return;
141 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700142
Junxiao Shid3c792f2014-01-30 00:46:13 -0700143 // cancel unsatisfy & straggler timer
144 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700145
Junxiao Shif3c07812014-03-11 21:48:49 -0700146 // is pending?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700147 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shie17349a2014-03-25 00:55:38 -0700148 bool isPending = inRecords.begin() != inRecords.end();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700149 if (!isPending) {
Spyridon Mastorakis31d4a082014-12-05 22:43:34 -0800150 if (m_csFromNdnSim == nullptr) {
151 m_cs.find(interest,
152 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
153 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
154 }
155 else {
156 shared_ptr<Data> match = m_csFromNdnSim->Lookup(interest.shared_from_this());
157 if (match != nullptr) {
158 this->onContentStoreHit(inFace, pitEntry, interest, *match);
159 }
160 else {
161 this->onContentStoreMiss(inFace, pitEntry, interest);
162 }
163 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700164 }
mzhang4eab72492015-02-25 11:16:09 -0600165 else {
166 this->onContentStoreMiss(inFace, pitEntry, interest);
167 }
168}
Junxiao Shic041ca32014-02-25 20:01:15 -0700169
mzhang4eab72492015-02-25 11:16:09 -0600170void
Junxiao Shi330136a2016-03-10 04:53:08 -0700171Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700172{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700173 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700174 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700175 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
176 " interest=" << interest.getName() <<
177 " drop");
178 return;
179 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700180
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700181 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
182 " interest=" << interest.getName() <<
183 " send-Nack-duplicate");
184
185 // send Nack with reason=DUPLICATE
186 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
187 lp::Nack nack(interest);
188 nack.setReason(lp::NackReason::DUPLICATE);
189 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700190}
191
192void
mzhang4eab72492015-02-25 11:16:09 -0600193Forwarder::onContentStoreMiss(const Face& inFace,
194 shared_ptr<pit::Entry> pitEntry,
195 const Interest& interest)
196{
197 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
198
199 shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700200 // insert InRecord
mzhang4eab72492015-02-25 11:16:09 -0600201 pitEntry->insertOrUpdateInRecord(face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700202
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700203 // set PIT unsatisfy timer
204 this->setUnsatisfyTimer(pitEntry);
205
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700206 shared_ptr<fib::Entry> fibEntry;
207 // has Link object?
208 if (!interest.hasLink()) {
209 // FIB lookup with Interest name
210 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
211 NFD_LOG_TRACE("onContentStoreMiss noLinkObject");
212 }
213 else {
214 const Link& link = interest.getLink();
215
216 // in producer region?
217 if (m_networkRegionTable.isInProducerRegion(link)) {
218 // FIB lookup with Interest name
219 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
220 NFD_LOG_TRACE("onContentStoreMiss inProducerRegion");
221 }
222 // has SelectedDelegation?
223 else if (interest.hasSelectedDelegation()) {
224 // FIB lookup with SelectedDelegation
225 fibEntry = m_fib.findLongestPrefixMatch(interest.getSelectedDelegation());
226 NFD_LOG_TRACE("onContentStoreMiss hasSelectedDelegation=" << interest.getSelectedDelegation());
227 }
228 else {
229 // FIB lookup with first delegation Name
230 fibEntry = m_fib.findLongestPrefixMatch(link.getDelegations().begin()->second);
231
232 // in default-free zone?
233 bool isDefaultFreeZone = !(fibEntry->getPrefix().size() == 0 && fibEntry->hasNextHops());
234 if (isDefaultFreeZone) {
235 // choose and set SelectedDelegation
236 for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) {
237 const Name& delegationName = delegation.second;
238 fibEntry = m_fib.findLongestPrefixMatch(delegationName);
239 if (fibEntry->hasNextHops()) {
240 const_cast<Interest&>(interest).setSelectedDelegation(delegationName);
241 NFD_LOG_TRACE("onContentStoreMiss enterDefaultFreeZone"
242 << " setSelectedDelegation=" << delegationName);
243 break;
244 }
245 }
246 }
247 else {
248 NFD_LOG_TRACE("onContentStoreMiss inConsumerRegion");
249 }
250 }
251 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700252
Junxiao Shid3c792f2014-01-30 00:46:13 -0700253 // dispatch to strategy
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700254 BOOST_ASSERT(fibEntry != nullptr);
Junxiao Shif3c07812014-03-11 21:48:49 -0700255 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700256 cref(inFace), cref(interest), fibEntry, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700257}
258
259void
mzhang4eab72492015-02-25 11:16:09 -0600260Forwarder::onContentStoreHit(const Face& inFace,
261 shared_ptr<pit::Entry> pitEntry,
262 const Interest& interest,
263 const Data& data)
264{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500265 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600266
Alexander Afanasyevd980dcd2015-01-08 21:41:48 -0800267 beforeSatisfyInterest(*pitEntry, *m_csFace, data);
268 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
269 pitEntry, cref(*m_csFace), cref(data)));
270
Junxiao Shicde37ad2015-12-24 01:02:05 -0700271 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600272 // XXX should we lookup PIT for other Interests that also match csMatch?
273
274 // set PIT straggler timer
275 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
276
277 // goto outgoing Data pipeline
278 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
279}
280
Junxiao Shif3c07812014-03-11 21:48:49 -0700281/** \brief compare two InRecords for picking outgoing Interest
282 * \return true if b is preferred over a
283 *
284 * This function should be passed to std::max_element over InRecordCollection.
285 * The outgoing Interest picked is the last incoming Interest
286 * that does not come from outFace.
287 * If all InRecords come from outFace, it's fine to pick that. This happens when
288 * there's only one InRecord that comes from outFace. The legit use is for
289 * vehicular network; otherwise, strategy shouldn't send to the sole inFace.
290 */
291static inline bool
292compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace)
293{
294 bool isOutFaceA = a.getFace().get() == outFace;
295 bool isOutFaceB = b.getFace().get() == outFace;
296
297 if (!isOutFaceA && isOutFaceB) {
298 return false;
299 }
300 if (isOutFaceA && !isOutFaceB) {
301 return true;
302 }
303
304 return a.getLastRenewed() > b.getLastRenewed();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700305}
306
307void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700308Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
309 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700310{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700311 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700312 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
313 return;
314 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700315 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
316 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700317
Junxiao Shi57f0f312014-03-16 11:52:20 -0700318 // scope control
319 if (pitEntry->violatesScope(outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700320 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700321 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700322 return;
323 }
324
Junxiao Shid3c792f2014-01-30 00:46:13 -0700325 // pick Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700326 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
327 pit::InRecordCollection::const_iterator pickedInRecord = std::max_element(
328 inRecords.begin(), inRecords.end(), bind(&compare_pickInterest, _1, _2, &outFace));
329 BOOST_ASSERT(pickedInRecord != inRecords.end());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700330 shared_ptr<Interest> interest = const_pointer_cast<Interest>(
331 pickedInRecord->getInterest().shared_from_this());
332
333 if (wantNewNonce) {
334 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700335 static boost::random::uniform_int_distribution<uint32_t> dist;
336 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700337 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700338
Junxiao Shid3c792f2014-01-30 00:46:13 -0700339 // insert OutRecord
Junxiao Shid938a6b2014-05-11 23:40:29 -0700340 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700341
Junxiao Shid3c792f2014-01-30 00:46:13 -0700342 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700343 outFace.sendInterest(*interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700344 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700345}
346
347void
Junxiao Shi09498f02014-02-26 19:41:08 -0700348Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700349{
Junxiao Shid938a6b2014-05-11 23:40:29 -0700350 if (pitEntry->hasUnexpiredOutRecords()) {
351 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
352 " cannot reject forwarded Interest");
353 return;
354 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700355 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700356
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700357 // cancel unsatisfy & straggler timer
358 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
359
Junxiao Shid3c792f2014-01-30 00:46:13 -0700360 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700361 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700362}
363
364void
365Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
366{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700367 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
368
Junxiao Shid3c792f2014-01-30 00:46:13 -0700369 // invoke PIT unsatisfied callback
Alexander Afanasyevd980dcd2015-01-08 21:41:48 -0800370 beforeExpirePendingInterest(*pitEntry);
Junxiao Shif3c07812014-03-11 21:48:49 -0700371 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700372 pitEntry));
Junxiao Shic041ca32014-02-25 20:01:15 -0700373
Junxiao Shia110f262014-10-12 12:35:20 -0700374 // goto Interest Finalize pipeline
375 this->onInterestFinalize(pitEntry, false);
376}
377
378void
379Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
380 const time::milliseconds& dataFreshnessPeriod)
381{
382 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
383 (isSatisfied ? " satisfied" : " unsatisfied"));
384
385 // Dead Nonce List insert if necessary
386 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
387
Junxiao Shif3c07812014-03-11 21:48:49 -0700388 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700389 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600390 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700391}
392
393void
394Forwarder::onIncomingData(Face& inFace, const Data& data)
395{
396 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700397 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000398 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700399 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700400
Junxiao Shi88884492014-02-15 15:57:43 -0700401 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700402 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700403 LOCALHOST_NAME.isPrefixOf(data.getName());
404 if (isViolatingLocalhost) {
405 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
406 " data=" << data.getName() << " violates /localhost");
407 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700408 return;
409 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700410
Junxiao Shid3c792f2014-01-30 00:46:13 -0700411 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700412 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
413 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700414 // goto Data unsolicited pipeline
415 this->onDataUnsolicited(inFace, data);
416 return;
417 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700418
Junxiao Shid3c792f2014-01-30 00:46:13 -0700419 // CS insert
Spyridon Mastorakis31d4a082014-12-05 22:43:34 -0800420 if (m_csFromNdnSim == nullptr)
421 m_cs.insert(data);
422 else
423 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shic041ca32014-02-25 20:01:15 -0700424
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700425 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700426 // foreach PitEntry
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700427 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700428 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700429
Junxiao Shid3c792f2014-01-30 00:46:13 -0700430 // cancel unsatisfy & straggler timer
431 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700432
Junxiao Shid3c792f2014-01-30 00:46:13 -0700433 // remember pending downstreams
434 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700435 for (const pit::InRecord& inRecord : inRecords) {
436 if (inRecord.getExpiry() > time::steady_clock::now()) {
437 pendingDownstreams.insert(inRecord.getFace().get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700438 }
439 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700440
Junxiao Shid938a6b2014-05-11 23:40:29 -0700441 // invoke PIT satisfy callback
Alexander Afanasyevd980dcd2015-01-08 21:41:48 -0800442 beforeSatisfyInterest(*pitEntry, inFace, data);
Junxiao Shi82e7f582014-09-07 15:15:40 -0700443 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700444 pitEntry, cref(inFace), cref(data)));
445
Junxiao Shia110f262014-10-12 12:35:20 -0700446 // Dead Nonce List insert if necessary (for OutRecord of inFace)
447 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
448
Junxiao Shid3c792f2014-01-30 00:46:13 -0700449 // mark PIT satisfied
450 pitEntry->deleteInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700451 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700452
Junxiao Shid3c792f2014-01-30 00:46:13 -0700453 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700454 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700455 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700456
Junxiao Shid3c792f2014-01-30 00:46:13 -0700457 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700458 for (Face* pendingDownstream : pendingDownstreams) {
459 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700460 continue;
461 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700462 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700463 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700464 }
465}
466
467void
468Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
469{
470 // accept to cache?
Junxiao Shicde37ad2015-12-24 01:02:05 -0700471 bool acceptToCache = inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700472 if (acceptToCache) {
473 // CS insert
Spyridon Mastorakis31d4a082014-12-05 22:43:34 -0800474 if (m_csFromNdnSim == nullptr)
475 m_cs.insert(data, true);
476 else
477 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700478 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700479
Junxiao Shif3c07812014-03-11 21:48:49 -0700480 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
481 " data=" << data.getName() <<
482 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700483}
484
485void
486Forwarder::onOutgoingData(const Data& data, Face& outFace)
487{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700488 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700489 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
490 return;
491 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700492 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
493
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700494 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700495 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700496 LOCALHOST_NAME.isPrefixOf(data.getName());
497 if (isViolatingLocalhost) {
498 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
499 " data=" << data.getName() << " violates /localhost");
500 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700501 return;
502 }
503
Junxiao Shif3c07812014-03-11 21:48:49 -0700504 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700505
Junxiao Shid3c792f2014-01-30 00:46:13 -0700506 // send Data
507 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700508 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700509}
510
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700511void
512Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
513{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000514 // receive Nack
515 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700516 ++m_counters.nInNacks;
517
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700518 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700519 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700520 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
521 " nack=" << nack.getInterest().getName() <<
522 "~" << nack.getReason() << " face-is-multi-access");
523 return;
524 }
525
526 // PIT match
527 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
528 // if no PIT entry found, drop
529 if (pitEntry == nullptr) {
530 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
531 " nack=" << nack.getInterest().getName() <<
532 "~" << nack.getReason() << " no-PIT-entry");
533 return;
534 }
535
536 // has out-record?
537 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
538 // if no out-record found, drop
539 if (outRecord == pitEntry->getOutRecords().end()) {
540 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
541 " nack=" << nack.getInterest().getName() <<
542 "~" << nack.getReason() << " no-out-record");
543 return;
544 }
545
546 // if out-record has different Nonce, drop
547 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
548 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
549 " nack=" << nack.getInterest().getName() <<
550 "~" << nack.getReason() << " wrong-Nonce " <<
551 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
552 return;
553 }
554
555 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
556 " nack=" << nack.getInterest().getName() <<
557 "~" << nack.getReason() << " OK");
558
559 // record Nack on out-record
560 outRecord->setIncomingNack(nack);
561
562 // trigger strategy: after receive NACK
563 shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
564 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveNack, _1,
565 cref(inFace), cref(nack), fibEntry, pitEntry));
566}
567
568void
569Forwarder::onOutgoingNack(shared_ptr<pit::Entry> pitEntry, const Face& outFace,
570 const lp::NackHeader& nack)
571{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700572 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700573 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
574 " nack=" << pitEntry->getInterest().getName() <<
575 "~" << nack.getReason() << " no-in-record");
576 return;
577 }
578
579 // has in-record?
580 pit::InRecordCollection::const_iterator inRecord = pitEntry->getInRecord(outFace);
581
582 // if no in-record found, drop
583 if (inRecord == pitEntry->getInRecords().end()) {
584 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
585 " nack=" << pitEntry->getInterest().getName() <<
586 "~" << nack.getReason() << " no-in-record");
587 return;
588 }
589
590 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700591 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700592 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
593 " nack=" << pitEntry->getInterest().getName() <<
594 "~" << nack.getReason() << " face-is-multi-access");
595 return;
596 }
597
598 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
599 " nack=" << pitEntry->getInterest().getName() <<
600 "~" << nack.getReason() << " OK");
601
602 // create Nack packet with the Interest from in-record
603 lp::Nack nackPkt(inRecord->getInterest());
604 nackPkt.setHeader(nack);
605
606 // erase in-record
607 pitEntry->deleteInRecord(outFace);
608
609 // send Nack on face
610 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700611 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700612}
613
Junxiao Shid3c792f2014-01-30 00:46:13 -0700614static inline bool
615compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
616{
617 return a.getExpiry() < b.getExpiry();
618}
619
620void
621Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
622{
623 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
624 pit::InRecordCollection::const_iterator lastExpiring =
625 std::max_element(inRecords.begin(), inRecords.end(),
626 &compare_InRecord_expiry);
627
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700628 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
629 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700630 if (lastExpiryFromNow <= time::seconds(0)) {
631 // TODO all InRecords are already expired; will this happen?
632 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700633
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700634 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700635 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700636 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
637}
638
639void
Junxiao Shia110f262014-10-12 12:35:20 -0700640Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
641 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700642{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700643 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700644
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700645 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700646 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700647 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700648}
649
650void
651Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
652{
Junxiao Shic041ca32014-02-25 20:01:15 -0700653 scheduler::cancel(pitEntry->m_unsatisfyTimer);
654 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700655}
656
Junxiao Shia110f262014-10-12 12:35:20 -0700657static inline void
658insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
659 const pit::OutRecord& outRecord)
660{
661 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
662}
663
664void
665Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
666 const time::milliseconds& dataFreshnessPeriod,
667 Face* upstream)
668{
669 // need Dead Nonce List insert?
670 bool needDnl = false;
671 if (isSatisfied) {
672 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
673 // Data never becomes stale if it doesn't have FreshnessPeriod field
674 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
675 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
676 }
677 else {
678 needDnl = true;
679 }
680
681 if (!needDnl) {
682 return;
683 }
684
685 // Dead Nonce List insert
686 if (upstream == 0) {
687 // insert all outgoing Nonces
688 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
689 std::for_each(outRecords.begin(), outRecords.end(),
690 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
691 }
692 else {
693 // insert outgoing Nonce of a specific face
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700694 pit::OutRecordCollection::const_iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700695 if (outRecord != pitEntry.getOutRecords().end()) {
696 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
697 }
698 }
699}
700
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800701} // namespace nfd