blob: 2b0098b05841957a77bc38a28a7743e46c4eb6ca [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"
Yuanzhi Gao04f90b92015-05-04 12:56:48 -070031
32#include "utils/ndn-ns3-packet-tag.hpp"
33
Junxiao Shiaf6569a2014-06-14 00:01:34 -070034#include <boost/random/uniform_int_distribution.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080035
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080036namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080037
Junxiao Shi8c8d2182014-01-30 22:33:00 -070038NFD_LOG_INIT("Forwarder");
39
Junxiao Shif3c07812014-03-11 21:48:49 -070040using fw::Strategy;
41
Junxiao Shif3c07812014-03-11 21:48:49 -070042const Name Forwarder::LOCALHOST_NAME("ndn:/localhost");
Junxiao Shi88884492014-02-15 15:57:43 -070043
Junxiao Shic041ca32014-02-25 20:01:15 -070044Forwarder::Forwarder()
Junxiao Shia4f2be82014-03-02 22:56:41 -070045 : m_faceTable(*this)
HangZhangad4afd12014-03-01 11:03:08 +080046 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060047 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080048 , m_measurements(m_nameTree)
Junxiao Shif3c07812014-03-11 21:48:49 -070049 , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this))
Alexander Afanasyevd980dcd2015-01-08 21:41:48 -080050 , m_csFace(face::makeNullFace(FaceUri("contentstore://")))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080051{
Junxiao Shif3c07812014-03-11 21:48:49 -070052 fw::installStrategies(*this);
Alexander Afanasyevd980dcd2015-01-08 21:41:48 -080053 getFaceTable().addReserved(m_csFace, face::FACEID_CONTENT_STORE);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080054}
55
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060056Forwarder::~Forwarder()
57{
Junxiao Shi0355e9f2015-09-02 07:24:53 -070058}
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060059
Junxiao Shi0355e9f2015-09-02 07:24:53 -070060void
61Forwarder::startProcessInterest(Face& face, const Interest& interest)
62{
63 // check fields used by forwarding are well-formed
64 try {
65 if (interest.hasLink()) {
66 interest.getLink();
67 }
68 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -070069 catch (const tlv::Error&) {
Junxiao Shi0355e9f2015-09-02 07:24:53 -070070 NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() <<
71 " interest=" << interest.getName() << " malformed");
72 // It's safe to call interest.getName() because Name has been fully parsed
73 return;
74 }
75
76 this->onIncomingInterest(face, interest);
77}
78
79void
80Forwarder::startProcessData(Face& face, const Data& data)
81{
82 // check fields used by forwarding are well-formed
83 // (none needed)
84
85 this->onIncomingData(face, data);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060086}
87
Alexander Afanasyev33b72772014-01-26 23:22:58 -080088void
Junxiao Shi5e5e4452015-09-24 16:56:52 -070089Forwarder::startProcessNack(Face& face, const lp::Nack& nack)
90{
91 // check fields used by forwarding are well-formed
92 try {
93 if (nack.getInterest().hasLink()) {
94 nack.getInterest().getLink();
95 }
96 }
97 catch (const tlv::Error&) {
98 NFD_LOG_DEBUG("startProcessNack face=" << face.getId() <<
99 " nack=" << nack.getInterest().getName() <<
100 "~" << nack.getReason() << " malformed");
101 return;
102 }
103
104 this->onIncomingNack(face, nack);
105}
106
107void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700108Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
109{
110 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700111 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
112 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000113 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700114 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -0700115
Junxiao Shi88884492014-02-15 15:57:43 -0700116 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700117 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700118 LOCALHOST_NAME.isPrefixOf(interest.getName());
119 if (isViolatingLocalhost) {
120 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
121 " interest=" << interest.getName() << " violates /localhost");
122 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700123 return;
124 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700125
Junxiao Shi330136a2016-03-10 04:53:08 -0700126 // detect duplicate Nonce with Dead Nonce List
127 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
128 if (hasDuplicateNonceInDnl) {
129 // goto Interest loop pipeline
130 this->onInterestLoop(inFace, interest);
131 return;
132 }
133
Junxiao Shid3c792f2014-01-30 00:46:13 -0700134 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700135 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700136
Junxiao Shi330136a2016-03-10 04:53:08 -0700137 // detect duplicate Nonce in PIT entry
138 bool hasDuplicateNonceInPit = pitEntry->findNonce(interest.getNonce(), inFace) !=
139 pit::DUPLICATE_NONCE_NONE;
140 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700141 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700142 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700143 return;
144 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700145
Junxiao Shid3c792f2014-01-30 00:46:13 -0700146 // cancel unsatisfy & straggler timer
147 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700148
Junxiao Shif3c07812014-03-11 21:48:49 -0700149 // is pending?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700150 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shie17349a2014-03-25 00:55:38 -0700151 bool isPending = inRecords.begin() != inRecords.end();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700152 if (!isPending) {
Spyridon Mastorakis31d4a082014-12-05 22:43:34 -0800153 if (m_csFromNdnSim == nullptr) {
154 m_cs.find(interest,
155 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
156 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
157 }
158 else {
159 shared_ptr<Data> match = m_csFromNdnSim->Lookup(interest.shared_from_this());
160 if (match != nullptr) {
161 this->onContentStoreHit(inFace, pitEntry, interest, *match);
162 }
163 else {
164 this->onContentStoreMiss(inFace, pitEntry, interest);
165 }
166 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700167 }
mzhang4eab72492015-02-25 11:16:09 -0600168 else {
169 this->onContentStoreMiss(inFace, pitEntry, interest);
170 }
171}
Junxiao Shic041ca32014-02-25 20:01:15 -0700172
mzhang4eab72492015-02-25 11:16:09 -0600173void
Junxiao Shi330136a2016-03-10 04:53:08 -0700174Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700175{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700176 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700177 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700178 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
179 " interest=" << interest.getName() <<
180 " drop");
181 return;
182 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700183
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700184 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
185 " interest=" << interest.getName() <<
186 " send-Nack-duplicate");
187
188 // send Nack with reason=DUPLICATE
189 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
190 lp::Nack nack(interest);
191 nack.setReason(lp::NackReason::DUPLICATE);
192 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700193}
194
195void
mzhang4eab72492015-02-25 11:16:09 -0600196Forwarder::onContentStoreMiss(const Face& inFace,
197 shared_ptr<pit::Entry> pitEntry,
198 const Interest& interest)
199{
200 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
201
202 shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700203 // insert InRecord
mzhang4eab72492015-02-25 11:16:09 -0600204 pitEntry->insertOrUpdateInRecord(face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700205
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700206 // set PIT unsatisfy timer
207 this->setUnsatisfyTimer(pitEntry);
208
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700209 shared_ptr<fib::Entry> fibEntry;
210 // has Link object?
211 if (!interest.hasLink()) {
212 // FIB lookup with Interest name
213 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
214 NFD_LOG_TRACE("onContentStoreMiss noLinkObject");
215 }
216 else {
217 const Link& link = interest.getLink();
218
219 // in producer region?
220 if (m_networkRegionTable.isInProducerRegion(link)) {
221 // FIB lookup with Interest name
222 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
223 NFD_LOG_TRACE("onContentStoreMiss inProducerRegion");
224 }
225 // has SelectedDelegation?
226 else if (interest.hasSelectedDelegation()) {
227 // FIB lookup with SelectedDelegation
228 fibEntry = m_fib.findLongestPrefixMatch(interest.getSelectedDelegation());
229 NFD_LOG_TRACE("onContentStoreMiss hasSelectedDelegation=" << interest.getSelectedDelegation());
230 }
231 else {
232 // FIB lookup with first delegation Name
233 fibEntry = m_fib.findLongestPrefixMatch(link.getDelegations().begin()->second);
234
235 // in default-free zone?
236 bool isDefaultFreeZone = !(fibEntry->getPrefix().size() == 0 && fibEntry->hasNextHops());
237 if (isDefaultFreeZone) {
238 // choose and set SelectedDelegation
239 for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) {
240 const Name& delegationName = delegation.second;
241 fibEntry = m_fib.findLongestPrefixMatch(delegationName);
242 if (fibEntry->hasNextHops()) {
243 const_cast<Interest&>(interest).setSelectedDelegation(delegationName);
244 NFD_LOG_TRACE("onContentStoreMiss enterDefaultFreeZone"
245 << " setSelectedDelegation=" << delegationName);
246 break;
247 }
248 }
249 }
250 else {
251 NFD_LOG_TRACE("onContentStoreMiss inConsumerRegion");
252 }
253 }
254 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700255
Junxiao Shid3c792f2014-01-30 00:46:13 -0700256 // dispatch to strategy
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700257 BOOST_ASSERT(fibEntry != nullptr);
Junxiao Shif3c07812014-03-11 21:48:49 -0700258 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700259 cref(inFace), cref(interest), fibEntry, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700260}
261
262void
mzhang4eab72492015-02-25 11:16:09 -0600263Forwarder::onContentStoreHit(const Face& inFace,
264 shared_ptr<pit::Entry> pitEntry,
265 const Interest& interest,
266 const Data& data)
267{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500268 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600269
Alexander Afanasyevd980dcd2015-01-08 21:41:48 -0800270 beforeSatisfyInterest(*pitEntry, *m_csFace, data);
271 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
272 pitEntry, cref(*m_csFace), cref(data)));
273
Junxiao Shicde37ad2015-12-24 01:02:05 -0700274 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600275 // XXX should we lookup PIT for other Interests that also match csMatch?
276
277 // set PIT straggler timer
278 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
279
280 // goto outgoing Data pipeline
281 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
282}
283
Junxiao Shif3c07812014-03-11 21:48:49 -0700284/** \brief compare two InRecords for picking outgoing Interest
285 * \return true if b is preferred over a
286 *
287 * This function should be passed to std::max_element over InRecordCollection.
288 * The outgoing Interest picked is the last incoming Interest
289 * that does not come from outFace.
290 * If all InRecords come from outFace, it's fine to pick that. This happens when
291 * there's only one InRecord that comes from outFace. The legit use is for
292 * vehicular network; otherwise, strategy shouldn't send to the sole inFace.
293 */
294static inline bool
295compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace)
296{
297 bool isOutFaceA = a.getFace().get() == outFace;
298 bool isOutFaceB = b.getFace().get() == outFace;
299
300 if (!isOutFaceA && isOutFaceB) {
301 return false;
302 }
303 if (isOutFaceA && !isOutFaceB) {
304 return true;
305 }
306
307 return a.getLastRenewed() > b.getLastRenewed();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700308}
309
310void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700311Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
312 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700313{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700314 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700315 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
316 return;
317 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700318 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
319 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700320
Junxiao Shi57f0f312014-03-16 11:52:20 -0700321 // scope control
322 if (pitEntry->violatesScope(outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700323 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700324 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700325 return;
326 }
327
Junxiao Shid3c792f2014-01-30 00:46:13 -0700328 // pick Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700329 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
330 pit::InRecordCollection::const_iterator pickedInRecord = std::max_element(
331 inRecords.begin(), inRecords.end(), bind(&compare_pickInterest, _1, _2, &outFace));
332 BOOST_ASSERT(pickedInRecord != inRecords.end());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700333 shared_ptr<Interest> interest = const_pointer_cast<Interest>(
334 pickedInRecord->getInterest().shared_from_this());
335
336 if (wantNewNonce) {
337 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700338 static boost::random::uniform_int_distribution<uint32_t> dist;
339 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700340 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700341
Junxiao Shid3c792f2014-01-30 00:46:13 -0700342 // insert OutRecord
Junxiao Shid938a6b2014-05-11 23:40:29 -0700343 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700344
Junxiao Shid3c792f2014-01-30 00:46:13 -0700345 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700346 outFace.sendInterest(*interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700347 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700348}
349
350void
Junxiao Shi09498f02014-02-26 19:41:08 -0700351Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700352{
Junxiao Shid938a6b2014-05-11 23:40:29 -0700353 if (pitEntry->hasUnexpiredOutRecords()) {
354 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
355 " cannot reject forwarded Interest");
356 return;
357 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700358 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700359
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700360 // cancel unsatisfy & straggler timer
361 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
362
Junxiao Shid3c792f2014-01-30 00:46:13 -0700363 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700364 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700365}
366
367void
368Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
369{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700370 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
371
Junxiao Shid3c792f2014-01-30 00:46:13 -0700372 // invoke PIT unsatisfied callback
Alexander Afanasyevd980dcd2015-01-08 21:41:48 -0800373 beforeExpirePendingInterest(*pitEntry);
Junxiao Shif3c07812014-03-11 21:48:49 -0700374 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700375 pitEntry));
Junxiao Shic041ca32014-02-25 20:01:15 -0700376
Junxiao Shia110f262014-10-12 12:35:20 -0700377 // goto Interest Finalize pipeline
378 this->onInterestFinalize(pitEntry, false);
379}
380
381void
382Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
383 const time::milliseconds& dataFreshnessPeriod)
384{
385 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
386 (isSatisfied ? " satisfied" : " unsatisfied"));
387
388 // Dead Nonce List insert if necessary
389 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
390
Junxiao Shif3c07812014-03-11 21:48:49 -0700391 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700392 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600393 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700394}
395
396void
397Forwarder::onIncomingData(Face& inFace, const Data& data)
398{
399 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700400 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000401 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700402 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700403
Junxiao Shi88884492014-02-15 15:57:43 -0700404 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700405 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700406 LOCALHOST_NAME.isPrefixOf(data.getName());
407 if (isViolatingLocalhost) {
408 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
409 " data=" << data.getName() << " violates /localhost");
410 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700411 return;
412 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700413
Junxiao Shid3c792f2014-01-30 00:46:13 -0700414 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700415 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
416 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700417 // goto Data unsolicited pipeline
418 this->onDataUnsolicited(inFace, data);
419 return;
420 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700421
Yuanzhi Gao04f90b92015-05-04 12:56:48 -0700422 // Remove Ptr<Packet> from the Data before inserting into cache, serving two purposes
423 // - reduce amount of memory used by cached entries
424 // - remove all tags that (e.g., hop count tag) that could have been associated with Ptr<Packet>
425 //
426 // Copying of Data is relatively cheap operation, as it copies (mostly) a collection of Blocks
427 // pointing to the same underlying memory buffer.
428 shared_ptr<Data> dataCopyWithoutPacket = make_shared<Data>(data);
429 dataCopyWithoutPacket->removeTag<ns3::ndn::Ns3PacketTag>();
430
Junxiao Shid3c792f2014-01-30 00:46:13 -0700431 // CS insert
Spyridon Mastorakis31d4a082014-12-05 22:43:34 -0800432 if (m_csFromNdnSim == nullptr)
Yuanzhi Gao04f90b92015-05-04 12:56:48 -0700433 m_cs.insert(*dataCopyWithoutPacket);
Spyridon Mastorakis31d4a082014-12-05 22:43:34 -0800434 else
Yuanzhi Gao04f90b92015-05-04 12:56:48 -0700435 m_csFromNdnSim->Add(dataCopyWithoutPacket);
Junxiao Shic041ca32014-02-25 20:01:15 -0700436
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700437 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700438 // foreach PitEntry
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700439 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700440 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700441
Junxiao Shid3c792f2014-01-30 00:46:13 -0700442 // cancel unsatisfy & straggler timer
443 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700444
Junxiao Shid3c792f2014-01-30 00:46:13 -0700445 // remember pending downstreams
446 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700447 for (const pit::InRecord& inRecord : inRecords) {
448 if (inRecord.getExpiry() > time::steady_clock::now()) {
449 pendingDownstreams.insert(inRecord.getFace().get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700450 }
451 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700452
Junxiao Shid938a6b2014-05-11 23:40:29 -0700453 // invoke PIT satisfy callback
Alexander Afanasyevd980dcd2015-01-08 21:41:48 -0800454 beforeSatisfyInterest(*pitEntry, inFace, data);
Junxiao Shi82e7f582014-09-07 15:15:40 -0700455 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700456 pitEntry, cref(inFace), cref(data)));
457
Junxiao Shia110f262014-10-12 12:35:20 -0700458 // Dead Nonce List insert if necessary (for OutRecord of inFace)
459 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
460
Junxiao Shid3c792f2014-01-30 00:46:13 -0700461 // mark PIT satisfied
462 pitEntry->deleteInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700463 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700464
Junxiao Shid3c792f2014-01-30 00:46:13 -0700465 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700466 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700467 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700468
Junxiao Shid3c792f2014-01-30 00:46:13 -0700469 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700470 for (Face* pendingDownstream : pendingDownstreams) {
471 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700472 continue;
473 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700474 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700475 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700476 }
477}
478
479void
480Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
481{
482 // accept to cache?
Junxiao Shicde37ad2015-12-24 01:02:05 -0700483 bool acceptToCache = inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700484 if (acceptToCache) {
485 // CS insert
Spyridon Mastorakis31d4a082014-12-05 22:43:34 -0800486 if (m_csFromNdnSim == nullptr)
487 m_cs.insert(data, true);
488 else
489 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700490 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700491
Junxiao Shif3c07812014-03-11 21:48:49 -0700492 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
493 " data=" << data.getName() <<
494 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700495}
496
497void
498Forwarder::onOutgoingData(const Data& data, Face& outFace)
499{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700500 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700501 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
502 return;
503 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700504 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
505
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700506 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700507 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700508 LOCALHOST_NAME.isPrefixOf(data.getName());
509 if (isViolatingLocalhost) {
510 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
511 " data=" << data.getName() << " violates /localhost");
512 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700513 return;
514 }
515
Junxiao Shif3c07812014-03-11 21:48:49 -0700516 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700517
Junxiao Shid3c792f2014-01-30 00:46:13 -0700518 // send Data
519 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700520 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700521}
522
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700523void
524Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
525{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000526 // receive Nack
527 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700528 ++m_counters.nInNacks;
529
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700530 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700531 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700532 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
533 " nack=" << nack.getInterest().getName() <<
534 "~" << nack.getReason() << " face-is-multi-access");
535 return;
536 }
537
538 // PIT match
539 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
540 // if no PIT entry found, drop
541 if (pitEntry == nullptr) {
542 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
543 " nack=" << nack.getInterest().getName() <<
544 "~" << nack.getReason() << " no-PIT-entry");
545 return;
546 }
547
548 // has out-record?
549 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
550 // if no out-record found, drop
551 if (outRecord == pitEntry->getOutRecords().end()) {
552 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
553 " nack=" << nack.getInterest().getName() <<
554 "~" << nack.getReason() << " no-out-record");
555 return;
556 }
557
558 // if out-record has different Nonce, drop
559 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
560 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
561 " nack=" << nack.getInterest().getName() <<
562 "~" << nack.getReason() << " wrong-Nonce " <<
563 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
564 return;
565 }
566
567 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
568 " nack=" << nack.getInterest().getName() <<
569 "~" << nack.getReason() << " OK");
570
571 // record Nack on out-record
572 outRecord->setIncomingNack(nack);
573
574 // trigger strategy: after receive NACK
575 shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
576 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveNack, _1,
577 cref(inFace), cref(nack), fibEntry, pitEntry));
578}
579
580void
581Forwarder::onOutgoingNack(shared_ptr<pit::Entry> pitEntry, const Face& outFace,
582 const lp::NackHeader& nack)
583{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700584 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700585 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
586 " nack=" << pitEntry->getInterest().getName() <<
587 "~" << nack.getReason() << " no-in-record");
588 return;
589 }
590
591 // has in-record?
592 pit::InRecordCollection::const_iterator inRecord = pitEntry->getInRecord(outFace);
593
594 // if no in-record found, drop
595 if (inRecord == pitEntry->getInRecords().end()) {
596 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
597 " nack=" << pitEntry->getInterest().getName() <<
598 "~" << nack.getReason() << " no-in-record");
599 return;
600 }
601
602 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700603 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700604 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
605 " nack=" << pitEntry->getInterest().getName() <<
606 "~" << nack.getReason() << " face-is-multi-access");
607 return;
608 }
609
610 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
611 " nack=" << pitEntry->getInterest().getName() <<
612 "~" << nack.getReason() << " OK");
613
614 // create Nack packet with the Interest from in-record
615 lp::Nack nackPkt(inRecord->getInterest());
616 nackPkt.setHeader(nack);
617
618 // erase in-record
619 pitEntry->deleteInRecord(outFace);
620
621 // send Nack on face
622 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700623 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700624}
625
Junxiao Shid3c792f2014-01-30 00:46:13 -0700626static inline bool
627compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
628{
629 return a.getExpiry() < b.getExpiry();
630}
631
632void
633Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
634{
635 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
636 pit::InRecordCollection::const_iterator lastExpiring =
637 std::max_element(inRecords.begin(), inRecords.end(),
638 &compare_InRecord_expiry);
639
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700640 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
641 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700642 if (lastExpiryFromNow <= time::seconds(0)) {
643 // TODO all InRecords are already expired; will this happen?
644 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700645
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700646 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700647 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700648 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
649}
650
651void
Junxiao Shia110f262014-10-12 12:35:20 -0700652Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
653 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700654{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700655 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700656
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700657 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700658 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700659 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700660}
661
662void
663Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
664{
Junxiao Shic041ca32014-02-25 20:01:15 -0700665 scheduler::cancel(pitEntry->m_unsatisfyTimer);
666 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700667}
668
Junxiao Shia110f262014-10-12 12:35:20 -0700669static inline void
670insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
671 const pit::OutRecord& outRecord)
672{
673 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
674}
675
676void
677Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
678 const time::milliseconds& dataFreshnessPeriod,
679 Face* upstream)
680{
681 // need Dead Nonce List insert?
682 bool needDnl = false;
683 if (isSatisfied) {
684 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
685 // Data never becomes stale if it doesn't have FreshnessPeriod field
686 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
687 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
688 }
689 else {
690 needDnl = true;
691 }
692
693 if (!needDnl) {
694 return;
695 }
696
697 // Dead Nonce List insert
698 if (upstream == 0) {
699 // insert all outgoing Nonces
700 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
701 std::for_each(outRecords.begin(), outRecords.end(),
702 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
703 }
704 else {
705 // insert outgoing Nonce of a specific face
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700706 pit::OutRecordCollection::const_iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700707 if (outRecord != pitEntry.getOutRecords().end()) {
708 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
709 }
710 }
711}
712
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800713} // namespace nfd