blob: 3258f3d66b259bed8fcd7b6aabf328324463e11e [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 Shid3c792f2014-01-30 00:46:13 -0700284void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700285Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
286 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700287{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700288 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700289 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
290 return;
291 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700292 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
293 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700294
Junxiao Shi57f0f312014-03-16 11:52:20 -0700295 // scope control
296 if (pitEntry->violatesScope(outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700297 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700298 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700299 return;
300 }
301
Junxiao Shid3c792f2014-01-30 00:46:13 -0700302 // pick Interest
Sonu Mishrab6e73622016-11-07 16:00:25 -0800303 // The outgoing Interest picked is the last incoming Interest that does not come from outFace.
304 // If all in-records come from outFace, it's fine to pick that.
305 // This happens when there's only one in-record that comes from outFace.
306 // The legit use is for vehicular network; otherwise, strategy shouldn't send to the sole inFace.
307
Junxiao Shif3c07812014-03-11 21:48:49 -0700308 pit::InRecordCollection::const_iterator pickedInRecord = std::max_element(
Sonu Mishrab6e73622016-11-07 16:00:25 -0800309 pitEntry->getInRecords().begin(), pitEntry->getInRecords().end(),
310 [&outFace] (const pit::InRecord& a, const pit::InRecord& b) {
311 bool isOutFaceA = a.getFace().get() == &outFace;
312 bool isOutFaceB = b.getFace().get() == &outFace;
313 return (isOutFaceA > isOutFaceB) ||
314 (isOutFaceA == isOutFaceB && a.getLastRenewed() < b.getLastRenewed());
315 });
316 BOOST_ASSERT(pickedInRecord != pitEntry->getInRecords().end());
317 auto interest = const_pointer_cast<Interest>(pickedInRecord->getInterest().shared_from_this());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700318
319 if (wantNewNonce) {
320 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700321 static boost::random::uniform_int_distribution<uint32_t> dist;
322 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700323 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700324
Junxiao Shid3c792f2014-01-30 00:46:13 -0700325 // insert OutRecord
Junxiao Shid938a6b2014-05-11 23:40:29 -0700326 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700327
Junxiao Shid3c792f2014-01-30 00:46:13 -0700328 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700329 outFace.sendInterest(*interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700330 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700331}
332
333void
Junxiao Shi09498f02014-02-26 19:41:08 -0700334Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700335{
Junxiao Shid938a6b2014-05-11 23:40:29 -0700336 if (pitEntry->hasUnexpiredOutRecords()) {
337 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
338 " cannot reject forwarded Interest");
339 return;
340 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700341 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700342
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700343 // cancel unsatisfy & straggler timer
344 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
345
Junxiao Shid3c792f2014-01-30 00:46:13 -0700346 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700347 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700348}
349
350void
351Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
352{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700353 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
354
Junxiao Shid3c792f2014-01-30 00:46:13 -0700355 // invoke PIT unsatisfied callback
Alexander Afanasyevd980dcd2015-01-08 21:41:48 -0800356 beforeExpirePendingInterest(*pitEntry);
Junxiao Shif3c07812014-03-11 21:48:49 -0700357 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700358 pitEntry));
Junxiao Shic041ca32014-02-25 20:01:15 -0700359
Junxiao Shia110f262014-10-12 12:35:20 -0700360 // goto Interest Finalize pipeline
361 this->onInterestFinalize(pitEntry, false);
362}
363
364void
365Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
366 const time::milliseconds& dataFreshnessPeriod)
367{
368 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
369 (isSatisfied ? " satisfied" : " unsatisfied"));
370
371 // Dead Nonce List insert if necessary
372 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
373
Junxiao Shif3c07812014-03-11 21:48:49 -0700374 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700375 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600376 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700377}
378
379void
380Forwarder::onIncomingData(Face& inFace, const Data& data)
381{
382 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700383 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000384 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700385 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700386
Junxiao Shi88884492014-02-15 15:57:43 -0700387 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700388 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700389 LOCALHOST_NAME.isPrefixOf(data.getName());
390 if (isViolatingLocalhost) {
391 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
392 " data=" << data.getName() << " violates /localhost");
393 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700394 return;
395 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700396
Junxiao Shid3c792f2014-01-30 00:46:13 -0700397 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700398 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
399 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700400 // goto Data unsolicited pipeline
401 this->onDataUnsolicited(inFace, data);
402 return;
403 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700404
Yuanzhi Gao04f90b92015-05-04 12:56:48 -0700405 // Remove Ptr<Packet> from the Data before inserting into cache, serving two purposes
406 // - reduce amount of memory used by cached entries
407 // - remove all tags that (e.g., hop count tag) that could have been associated with Ptr<Packet>
408 //
409 // Copying of Data is relatively cheap operation, as it copies (mostly) a collection of Blocks
410 // pointing to the same underlying memory buffer.
411 shared_ptr<Data> dataCopyWithoutPacket = make_shared<Data>(data);
412 dataCopyWithoutPacket->removeTag<ns3::ndn::Ns3PacketTag>();
413
Junxiao Shid3c792f2014-01-30 00:46:13 -0700414 // CS insert
Spyridon Mastorakis31d4a082014-12-05 22:43:34 -0800415 if (m_csFromNdnSim == nullptr)
Yuanzhi Gao04f90b92015-05-04 12:56:48 -0700416 m_cs.insert(*dataCopyWithoutPacket);
Spyridon Mastorakis31d4a082014-12-05 22:43:34 -0800417 else
Yuanzhi Gao04f90b92015-05-04 12:56:48 -0700418 m_csFromNdnSim->Add(dataCopyWithoutPacket);
Junxiao Shic041ca32014-02-25 20:01:15 -0700419
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700420 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700421 // foreach PitEntry
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700422 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700423 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700424
Junxiao Shid3c792f2014-01-30 00:46:13 -0700425 // cancel unsatisfy & straggler timer
426 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700427
Junxiao Shid3c792f2014-01-30 00:46:13 -0700428 // remember pending downstreams
429 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700430 for (const pit::InRecord& inRecord : inRecords) {
431 if (inRecord.getExpiry() > time::steady_clock::now()) {
432 pendingDownstreams.insert(inRecord.getFace().get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700433 }
434 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700435
Junxiao Shid938a6b2014-05-11 23:40:29 -0700436 // invoke PIT satisfy callback
Alexander Afanasyevd980dcd2015-01-08 21:41:48 -0800437 beforeSatisfyInterest(*pitEntry, inFace, data);
Junxiao Shi82e7f582014-09-07 15:15:40 -0700438 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700439 pitEntry, cref(inFace), cref(data)));
440
Junxiao Shia110f262014-10-12 12:35:20 -0700441 // Dead Nonce List insert if necessary (for OutRecord of inFace)
442 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
443
Junxiao Shid3c792f2014-01-30 00:46:13 -0700444 // mark PIT satisfied
445 pitEntry->deleteInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700446 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700447
Junxiao Shid3c792f2014-01-30 00:46:13 -0700448 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700449 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700450 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700451
Junxiao Shid3c792f2014-01-30 00:46:13 -0700452 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700453 for (Face* pendingDownstream : pendingDownstreams) {
454 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700455 continue;
456 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700457 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700458 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700459 }
460}
461
462void
463Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
464{
465 // accept to cache?
Junxiao Shicde37ad2015-12-24 01:02:05 -0700466 bool acceptToCache = inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700467 if (acceptToCache) {
468 // CS insert
Spyridon Mastorakis31d4a082014-12-05 22:43:34 -0800469 if (m_csFromNdnSim == nullptr)
470 m_cs.insert(data, true);
471 else
472 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700473 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700474
Junxiao Shif3c07812014-03-11 21:48:49 -0700475 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
476 " data=" << data.getName() <<
477 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700478}
479
480void
481Forwarder::onOutgoingData(const Data& data, Face& outFace)
482{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700483 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700484 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
485 return;
486 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700487 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
488
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700489 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700490 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3c07812014-03-11 21:48:49 -0700491 LOCALHOST_NAME.isPrefixOf(data.getName());
492 if (isViolatingLocalhost) {
493 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
494 " data=" << data.getName() << " violates /localhost");
495 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700496 return;
497 }
498
Junxiao Shif3c07812014-03-11 21:48:49 -0700499 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700500
Junxiao Shid3c792f2014-01-30 00:46:13 -0700501 // send Data
502 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700503 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700504}
505
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700506void
507Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
508{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000509 // receive Nack
510 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700511 ++m_counters.nInNacks;
512
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700513 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700514 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700515 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
516 " nack=" << nack.getInterest().getName() <<
517 "~" << nack.getReason() << " face-is-multi-access");
518 return;
519 }
520
521 // PIT match
522 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
523 // if no PIT entry found, drop
524 if (pitEntry == nullptr) {
525 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
526 " nack=" << nack.getInterest().getName() <<
527 "~" << nack.getReason() << " no-PIT-entry");
528 return;
529 }
530
531 // has out-record?
532 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
533 // if no out-record found, drop
534 if (outRecord == pitEntry->getOutRecords().end()) {
535 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
536 " nack=" << nack.getInterest().getName() <<
537 "~" << nack.getReason() << " no-out-record");
538 return;
539 }
540
541 // if out-record has different Nonce, drop
542 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
543 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
544 " nack=" << nack.getInterest().getName() <<
545 "~" << nack.getReason() << " wrong-Nonce " <<
546 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
547 return;
548 }
549
550 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
551 " nack=" << nack.getInterest().getName() <<
552 "~" << nack.getReason() << " OK");
553
554 // record Nack on out-record
555 outRecord->setIncomingNack(nack);
556
557 // trigger strategy: after receive NACK
558 shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
559 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveNack, _1,
560 cref(inFace), cref(nack), fibEntry, pitEntry));
561}
562
563void
564Forwarder::onOutgoingNack(shared_ptr<pit::Entry> pitEntry, const Face& outFace,
565 const lp::NackHeader& nack)
566{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700567 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700568 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
569 " nack=" << pitEntry->getInterest().getName() <<
570 "~" << nack.getReason() << " no-in-record");
571 return;
572 }
573
574 // has in-record?
575 pit::InRecordCollection::const_iterator inRecord = pitEntry->getInRecord(outFace);
576
577 // if no in-record found, drop
578 if (inRecord == pitEntry->getInRecords().end()) {
579 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
580 " nack=" << pitEntry->getInterest().getName() <<
581 "~" << nack.getReason() << " no-in-record");
582 return;
583 }
584
585 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700586 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700587 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
588 " nack=" << pitEntry->getInterest().getName() <<
589 "~" << nack.getReason() << " face-is-multi-access");
590 return;
591 }
592
593 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
594 " nack=" << pitEntry->getInterest().getName() <<
595 "~" << nack.getReason() << " OK");
596
597 // create Nack packet with the Interest from in-record
598 lp::Nack nackPkt(inRecord->getInterest());
599 nackPkt.setHeader(nack);
600
601 // erase in-record
602 pitEntry->deleteInRecord(outFace);
603
604 // send Nack on face
605 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700606 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700607}
608
Junxiao Shid3c792f2014-01-30 00:46:13 -0700609static inline bool
610compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
611{
612 return a.getExpiry() < b.getExpiry();
613}
614
615void
616Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
617{
618 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
619 pit::InRecordCollection::const_iterator lastExpiring =
620 std::max_element(inRecords.begin(), inRecords.end(),
621 &compare_InRecord_expiry);
622
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700623 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
624 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700625 if (lastExpiryFromNow <= time::seconds(0)) {
626 // TODO all InRecords are already expired; will this happen?
627 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700628
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700629 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700630 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700631 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
632}
633
634void
Junxiao Shia110f262014-10-12 12:35:20 -0700635Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
636 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700637{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700638 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700639
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700640 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700641 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700642 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700643}
644
645void
646Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
647{
Junxiao Shic041ca32014-02-25 20:01:15 -0700648 scheduler::cancel(pitEntry->m_unsatisfyTimer);
649 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700650}
651
Junxiao Shia110f262014-10-12 12:35:20 -0700652static inline void
653insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
654 const pit::OutRecord& outRecord)
655{
656 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
657}
658
659void
660Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
661 const time::milliseconds& dataFreshnessPeriod,
662 Face* upstream)
663{
664 // need Dead Nonce List insert?
665 bool needDnl = false;
666 if (isSatisfied) {
667 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
668 // Data never becomes stale if it doesn't have FreshnessPeriod field
669 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
670 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
671 }
672 else {
673 needDnl = true;
674 }
675
676 if (!needDnl) {
677 return;
678 }
679
680 // Dead Nonce List insert
681 if (upstream == 0) {
682 // insert all outgoing Nonces
683 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
684 std::for_each(outRecords.begin(), outRecords.end(),
685 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
686 }
687 else {
688 // insert outgoing Nonce of a specific face
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700689 pit::OutRecordCollection::const_iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700690 if (outRecord != pitEntry.getOutRecords().end()) {
691 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
692 }
693 }
694}
695
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800696} // namespace nfd