blob: 20428c5ad0560ed3940c395be936c4b36bb65521 [file] [log] [blame]
Alexander Afanasyev33b72772014-01-26 23:22:58 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shifaf3eb02015-02-16 10:50:36 -07003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shiaf6569a2014-06-14 00:01:34 -070024 */
Alexander Afanasyev33b72772014-01-26 23:22:58 -080025
26#include "forwarder.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027#include "core/logger.hpp"
Junxiao Shiaf6569a2014-06-14 00:01:34 -070028#include "core/random.hpp"
Junxiao Shifaf3eb02015-02-16 10:50:36 -070029#include "strategy.hpp"
Junxiao Shiaf6569a2014-06-14 00:01:34 -070030#include <boost/random/uniform_int_distribution.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080031
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080032namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080033
Junxiao Shi8c8d2182014-01-30 22:33:00 -070034NFD_LOG_INIT("Forwarder");
35
Junxiao Shif3c07812014-03-11 21:48:49 -070036using fw::Strategy;
37
Junxiao Shif3c07812014-03-11 21:48:49 -070038const Name Forwarder::LOCALHOST_NAME("ndn:/localhost");
Junxiao Shi88884492014-02-15 15:57:43 -070039
Junxiao Shic041ca32014-02-25 20:01:15 -070040Forwarder::Forwarder()
Junxiao Shia4f2be82014-03-02 22:56:41 -070041 : m_faceTable(*this)
HangZhangad4afd12014-03-01 11:03:08 +080042 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060043 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080044 , m_measurements(m_nameTree)
Junxiao Shif3c07812014-03-11 21:48:49 -070045 , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080046{
Junxiao Shif3c07812014-03-11 21:48:49 -070047 fw::installStrategies(*this);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080048}
49
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060050Forwarder::~Forwarder()
51{
Junxiao Shi0355e9f2015-09-02 07:24:53 -070052}
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060053
Junxiao Shi0355e9f2015-09-02 07:24:53 -070054void
55Forwarder::startProcessInterest(Face& face, const Interest& interest)
56{
57 // check fields used by forwarding are well-formed
58 try {
59 if (interest.hasLink()) {
60 interest.getLink();
61 }
62 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -070063 catch (const tlv::Error&) {
Junxiao Shi0355e9f2015-09-02 07:24:53 -070064 NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() <<
65 " interest=" << interest.getName() << " malformed");
66 // It's safe to call interest.getName() because Name has been fully parsed
67 return;
68 }
69
70 this->onIncomingInterest(face, interest);
71}
72
73void
74Forwarder::startProcessData(Face& face, const Data& data)
75{
76 // check fields used by forwarding are well-formed
77 // (none needed)
78
79 this->onIncomingData(face, data);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060080}
81
Alexander Afanasyev33b72772014-01-26 23:22:58 -080082void
Junxiao Shi5e5e4452015-09-24 16:56:52 -070083Forwarder::startProcessNack(Face& face, const lp::Nack& nack)
84{
85 // check fields used by forwarding are well-formed
86 try {
87 if (nack.getInterest().hasLink()) {
88 nack.getInterest().getLink();
89 }
90 }
91 catch (const tlv::Error&) {
92 NFD_LOG_DEBUG("startProcessNack face=" << face.getId() <<
93 " nack=" << nack.getInterest().getName() <<
94 "~" << nack.getReason() << " malformed");
95 return;
96 }
97
98 this->onIncomingNack(face, nack);
99}
100
101void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700102Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
103{
104 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700105 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
106 " interest=" << interest.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -0700107 const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId());
Junxiao Shi33152f12014-07-16 19:54:32 -0700108 ++m_counters.getNInInterests();
Junxiao Shic041ca32014-02-25 20:01:15 -0700109
Junxiao Shi88884492014-02-15 15:57:43 -0700110 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -0700111 bool isViolatingLocalhost = !inFace.isLocal() &&
112 LOCALHOST_NAME.isPrefixOf(interest.getName());
113 if (isViolatingLocalhost) {
114 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
115 " interest=" << interest.getName() << " violates /localhost");
116 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700117 return;
118 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700119
Junxiao Shid3c792f2014-01-30 00:46:13 -0700120 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700121 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700122
Junxiao Shia110f262014-10-12 12:35:20 -0700123 // detect duplicate Nonce
124 int dnw = pitEntry->findNonce(interest.getNonce(), inFace);
125 bool hasDuplicateNonce = (dnw != pit::DUPLICATE_NONCE_NONE) ||
126 m_deadNonceList.has(interest.getName(), interest.getNonce());
127 if (hasDuplicateNonce) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700128 // goto Interest loop pipeline
129 this->onInterestLoop(inFace, interest, pitEntry);
130 return;
131 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700132
Junxiao Shid3c792f2014-01-30 00:46:13 -0700133 // cancel unsatisfy & straggler timer
134 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700135
Junxiao Shif3c07812014-03-11 21:48:49 -0700136 // is pending?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700137 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shie17349a2014-03-25 00:55:38 -0700138 bool isPending = inRecords.begin() != inRecords.end();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700139 if (!isPending) {
mzhang4eab72492015-02-25 11:16:09 -0600140 m_cs.find(interest,
141 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
142 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700143 }
mzhang4eab72492015-02-25 11:16:09 -0600144 else {
145 this->onContentStoreMiss(inFace, pitEntry, interest);
146 }
147}
Junxiao Shic041ca32014-02-25 20:01:15 -0700148
mzhang4eab72492015-02-25 11:16:09 -0600149void
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700150Forwarder::onInterestLoop(Face& inFace, const Interest& interest,
151 shared_ptr<pit::Entry> pitEntry)
152{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700153 // if multi-access face, drop
154 if (inFace.isMultiAccess()) {
155 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
156 " interest=" << interest.getName() <<
157 " drop");
158 return;
159 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700160
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700161 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
162 " interest=" << interest.getName() <<
163 " send-Nack-duplicate");
164
165 // send Nack with reason=DUPLICATE
166 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
167 lp::Nack nack(interest);
168 nack.setReason(lp::NackReason::DUPLICATE);
169 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700170}
171
172void
mzhang4eab72492015-02-25 11:16:09 -0600173Forwarder::onContentStoreMiss(const Face& inFace,
174 shared_ptr<pit::Entry> pitEntry,
175 const Interest& interest)
176{
177 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
178
179 shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700180 // insert InRecord
mzhang4eab72492015-02-25 11:16:09 -0600181 pitEntry->insertOrUpdateInRecord(face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700182
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700183 // set PIT unsatisfy timer
184 this->setUnsatisfyTimer(pitEntry);
185
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700186 shared_ptr<fib::Entry> fibEntry;
187 // has Link object?
188 if (!interest.hasLink()) {
189 // FIB lookup with Interest name
190 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
191 NFD_LOG_TRACE("onContentStoreMiss noLinkObject");
192 }
193 else {
194 const Link& link = interest.getLink();
195
196 // in producer region?
197 if (m_networkRegionTable.isInProducerRegion(link)) {
198 // FIB lookup with Interest name
199 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
200 NFD_LOG_TRACE("onContentStoreMiss inProducerRegion");
201 }
202 // has SelectedDelegation?
203 else if (interest.hasSelectedDelegation()) {
204 // FIB lookup with SelectedDelegation
205 fibEntry = m_fib.findLongestPrefixMatch(interest.getSelectedDelegation());
206 NFD_LOG_TRACE("onContentStoreMiss hasSelectedDelegation=" << interest.getSelectedDelegation());
207 }
208 else {
209 // FIB lookup with first delegation Name
210 fibEntry = m_fib.findLongestPrefixMatch(link.getDelegations().begin()->second);
211
212 // in default-free zone?
213 bool isDefaultFreeZone = !(fibEntry->getPrefix().size() == 0 && fibEntry->hasNextHops());
214 if (isDefaultFreeZone) {
215 // choose and set SelectedDelegation
216 for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) {
217 const Name& delegationName = delegation.second;
218 fibEntry = m_fib.findLongestPrefixMatch(delegationName);
219 if (fibEntry->hasNextHops()) {
220 const_cast<Interest&>(interest).setSelectedDelegation(delegationName);
221 NFD_LOG_TRACE("onContentStoreMiss enterDefaultFreeZone"
222 << " setSelectedDelegation=" << delegationName);
223 break;
224 }
225 }
226 }
227 else {
228 NFD_LOG_TRACE("onContentStoreMiss inConsumerRegion");
229 }
230 }
231 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700232
Junxiao Shid3c792f2014-01-30 00:46:13 -0700233 // dispatch to strategy
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700234 BOOST_ASSERT(fibEntry != nullptr);
Junxiao Shif3c07812014-03-11 21:48:49 -0700235 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700236 cref(inFace), cref(interest), fibEntry, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700237}
238
239void
mzhang4eab72492015-02-25 11:16:09 -0600240Forwarder::onContentStoreHit(const Face& inFace,
241 shared_ptr<pit::Entry> pitEntry,
242 const Interest& interest,
243 const Data& data)
244{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500245 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600246
247 const_pointer_cast<Data>(data.shared_from_this())->setIncomingFaceId(FACEID_CONTENT_STORE);
248 // XXX should we lookup PIT for other Interests that also match csMatch?
249
250 // set PIT straggler timer
251 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
252
253 // goto outgoing Data pipeline
254 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
255}
256
Junxiao Shif3c07812014-03-11 21:48:49 -0700257/** \brief compare two InRecords for picking outgoing Interest
258 * \return true if b is preferred over a
259 *
260 * This function should be passed to std::max_element over InRecordCollection.
261 * The outgoing Interest picked is the last incoming Interest
262 * that does not come from outFace.
263 * If all InRecords come from outFace, it's fine to pick that. This happens when
264 * there's only one InRecord that comes from outFace. The legit use is for
265 * vehicular network; otherwise, strategy shouldn't send to the sole inFace.
266 */
267static inline bool
268compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace)
269{
270 bool isOutFaceA = a.getFace().get() == outFace;
271 bool isOutFaceB = b.getFace().get() == outFace;
272
273 if (!isOutFaceA && isOutFaceB) {
274 return false;
275 }
276 if (isOutFaceA && !isOutFaceB) {
277 return true;
278 }
279
280 return a.getLastRenewed() > b.getLastRenewed();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700281}
282
283void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700284Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
285 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700286{
Junxiao Shi223271b2014-07-03 22:06:13 -0700287 if (outFace.getId() == INVALID_FACEID) {
288 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
289 return;
290 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700291 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
292 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700293
Junxiao Shi57f0f312014-03-16 11:52:20 -0700294 // scope control
295 if (pitEntry->violatesScope(outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700296 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700297 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700298 return;
299 }
300
Junxiao Shid3c792f2014-01-30 00:46:13 -0700301 // pick Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700302 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
303 pit::InRecordCollection::const_iterator pickedInRecord = std::max_element(
304 inRecords.begin(), inRecords.end(), bind(&compare_pickInterest, _1, _2, &outFace));
305 BOOST_ASSERT(pickedInRecord != inRecords.end());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700306 shared_ptr<Interest> interest = const_pointer_cast<Interest>(
307 pickedInRecord->getInterest().shared_from_this());
308
309 if (wantNewNonce) {
310 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700311 static boost::random::uniform_int_distribution<uint32_t> dist;
312 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700313 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700314
Junxiao Shid3c792f2014-01-30 00:46:13 -0700315 // insert OutRecord
Junxiao Shid938a6b2014-05-11 23:40:29 -0700316 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700317
Junxiao Shid3c792f2014-01-30 00:46:13 -0700318 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700319 outFace.sendInterest(*interest);
Junxiao Shi33152f12014-07-16 19:54:32 -0700320 ++m_counters.getNOutInterests();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700321}
322
323void
Junxiao Shi09498f02014-02-26 19:41:08 -0700324Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700325{
Junxiao Shid938a6b2014-05-11 23:40:29 -0700326 if (pitEntry->hasUnexpiredOutRecords()) {
327 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
328 " cannot reject forwarded Interest");
329 return;
330 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700331 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700332
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700333 // cancel unsatisfy & straggler timer
334 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
335
Junxiao Shid3c792f2014-01-30 00:46:13 -0700336 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700337 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700338}
339
340void
341Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
342{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700343 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
344
Junxiao Shid3c792f2014-01-30 00:46:13 -0700345 // invoke PIT unsatisfied callback
Junxiao Shif3c07812014-03-11 21:48:49 -0700346 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700347 pitEntry));
Junxiao Shic041ca32014-02-25 20:01:15 -0700348
Junxiao Shia110f262014-10-12 12:35:20 -0700349 // goto Interest Finalize pipeline
350 this->onInterestFinalize(pitEntry, false);
351}
352
353void
354Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
355 const time::milliseconds& dataFreshnessPeriod)
356{
357 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
358 (isSatisfied ? " satisfied" : " unsatisfied"));
359
360 // Dead Nonce List insert if necessary
361 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
362
Junxiao Shif3c07812014-03-11 21:48:49 -0700363 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700364 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600365 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700366}
367
368void
369Forwarder::onIncomingData(Face& inFace, const Data& data)
370{
371 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700372 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -0700373 const_cast<Data&>(data).setIncomingFaceId(inFace.getId());
Junxiao Shi33152f12014-07-16 19:54:32 -0700374 ++m_counters.getNInDatas();
Junxiao Shic041ca32014-02-25 20:01:15 -0700375
Junxiao Shi88884492014-02-15 15:57:43 -0700376 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -0700377 bool isViolatingLocalhost = !inFace.isLocal() &&
378 LOCALHOST_NAME.isPrefixOf(data.getName());
379 if (isViolatingLocalhost) {
380 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
381 " data=" << data.getName() << " violates /localhost");
382 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700383 return;
384 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700385
Junxiao Shid3c792f2014-01-30 00:46:13 -0700386 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700387 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
388 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700389 // goto Data unsolicited pipeline
390 this->onDataUnsolicited(inFace, data);
391 return;
392 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700393
Junxiao Shid3c792f2014-01-30 00:46:13 -0700394 // CS insert
395 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700396
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700397 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700398 // foreach PitEntry
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700399 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700400 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700401
Junxiao Shid3c792f2014-01-30 00:46:13 -0700402 // cancel unsatisfy & straggler timer
403 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700404
Junxiao Shid3c792f2014-01-30 00:46:13 -0700405 // remember pending downstreams
406 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700407 for (const pit::InRecord& inRecord : inRecords) {
408 if (inRecord.getExpiry() > time::steady_clock::now()) {
409 pendingDownstreams.insert(inRecord.getFace().get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700410 }
411 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700412
Junxiao Shid938a6b2014-05-11 23:40:29 -0700413 // invoke PIT satisfy callback
Junxiao Shi82e7f582014-09-07 15:15:40 -0700414 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700415 pitEntry, cref(inFace), cref(data)));
416
Junxiao Shia110f262014-10-12 12:35:20 -0700417 // Dead Nonce List insert if necessary (for OutRecord of inFace)
418 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
419
Junxiao Shid3c792f2014-01-30 00:46:13 -0700420 // mark PIT satisfied
421 pitEntry->deleteInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700422 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700423
Junxiao Shid3c792f2014-01-30 00:46:13 -0700424 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700425 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700426 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700427
Junxiao Shid3c792f2014-01-30 00:46:13 -0700428 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700429 for (Face* pendingDownstream : pendingDownstreams) {
430 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700431 continue;
432 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700433 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700434 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700435 }
436}
437
438void
439Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
440{
441 // accept to cache?
Junxiao Shif3c07812014-03-11 21:48:49 -0700442 bool acceptToCache = inFace.isLocal();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700443 if (acceptToCache) {
444 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700445 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700446 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700447
Junxiao Shif3c07812014-03-11 21:48:49 -0700448 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
449 " data=" << data.getName() <<
450 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700451}
452
453void
454Forwarder::onOutgoingData(const Data& data, Face& outFace)
455{
Junxiao Shi223271b2014-07-03 22:06:13 -0700456 if (outFace.getId() == INVALID_FACEID) {
457 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
458 return;
459 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700460 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
461
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700462 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -0700463 bool isViolatingLocalhost = !outFace.isLocal() &&
464 LOCALHOST_NAME.isPrefixOf(data.getName());
465 if (isViolatingLocalhost) {
466 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
467 " data=" << data.getName() << " violates /localhost");
468 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700469 return;
470 }
471
Junxiao Shif3c07812014-03-11 21:48:49 -0700472 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700473
Junxiao Shid3c792f2014-01-30 00:46:13 -0700474 // send Data
475 outFace.sendData(data);
Junxiao Shi33152f12014-07-16 19:54:32 -0700476 ++m_counters.getNOutDatas();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700477}
478
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700479void
480Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
481{
482 // if multi-access face, drop
483 if (inFace.isMultiAccess()) {
484 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
485 " nack=" << nack.getInterest().getName() <<
486 "~" << nack.getReason() << " face-is-multi-access");
487 return;
488 }
489
490 // PIT match
491 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
492 // if no PIT entry found, drop
493 if (pitEntry == nullptr) {
494 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
495 " nack=" << nack.getInterest().getName() <<
496 "~" << nack.getReason() << " no-PIT-entry");
497 return;
498 }
499
500 // has out-record?
501 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
502 // if no out-record found, drop
503 if (outRecord == pitEntry->getOutRecords().end()) {
504 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
505 " nack=" << nack.getInterest().getName() <<
506 "~" << nack.getReason() << " no-out-record");
507 return;
508 }
509
510 // if out-record has different Nonce, drop
511 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
512 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
513 " nack=" << nack.getInterest().getName() <<
514 "~" << nack.getReason() << " wrong-Nonce " <<
515 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
516 return;
517 }
518
519 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
520 " nack=" << nack.getInterest().getName() <<
521 "~" << nack.getReason() << " OK");
522
523 // record Nack on out-record
524 outRecord->setIncomingNack(nack);
525
526 // trigger strategy: after receive NACK
527 shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
528 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveNack, _1,
529 cref(inFace), cref(nack), fibEntry, pitEntry));
530}
531
532void
533Forwarder::onOutgoingNack(shared_ptr<pit::Entry> pitEntry, const Face& outFace,
534 const lp::NackHeader& nack)
535{
536 if (outFace.getId() == INVALID_FACEID) {
537 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
538 " nack=" << pitEntry->getInterest().getName() <<
539 "~" << nack.getReason() << " no-in-record");
540 return;
541 }
542
543 // has in-record?
544 pit::InRecordCollection::const_iterator inRecord = pitEntry->getInRecord(outFace);
545
546 // if no in-record found, drop
547 if (inRecord == pitEntry->getInRecords().end()) {
548 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
549 " nack=" << pitEntry->getInterest().getName() <<
550 "~" << nack.getReason() << " no-in-record");
551 return;
552 }
553
554 // if multi-access face, drop
555 if (outFace.isMultiAccess()) {
556 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
557 " nack=" << pitEntry->getInterest().getName() <<
558 "~" << nack.getReason() << " face-is-multi-access");
559 return;
560 }
561
562 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
563 " nack=" << pitEntry->getInterest().getName() <<
564 "~" << nack.getReason() << " OK");
565
566 // create Nack packet with the Interest from in-record
567 lp::Nack nackPkt(inRecord->getInterest());
568 nackPkt.setHeader(nack);
569
570 // erase in-record
571 pitEntry->deleteInRecord(outFace);
572
573 // send Nack on face
574 const_cast<Face&>(outFace).sendNack(nackPkt);
575}
576
Junxiao Shid3c792f2014-01-30 00:46:13 -0700577static inline bool
578compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
579{
580 return a.getExpiry() < b.getExpiry();
581}
582
583void
584Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
585{
586 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
587 pit::InRecordCollection::const_iterator lastExpiring =
588 std::max_element(inRecords.begin(), inRecords.end(),
589 &compare_InRecord_expiry);
590
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700591 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
592 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700593 if (lastExpiryFromNow <= time::seconds(0)) {
594 // TODO all InRecords are already expired; will this happen?
595 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700596
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700597 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700598 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700599 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
600}
601
602void
Junxiao Shia110f262014-10-12 12:35:20 -0700603Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
604 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700605{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700606 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700607
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700608 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700609 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700610 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700611}
612
613void
614Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
615{
Junxiao Shic041ca32014-02-25 20:01:15 -0700616 scheduler::cancel(pitEntry->m_unsatisfyTimer);
617 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700618}
619
Junxiao Shia110f262014-10-12 12:35:20 -0700620static inline void
621insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
622 const pit::OutRecord& outRecord)
623{
624 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
625}
626
627void
628Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
629 const time::milliseconds& dataFreshnessPeriod,
630 Face* upstream)
631{
632 // need Dead Nonce List insert?
633 bool needDnl = false;
634 if (isSatisfied) {
635 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
636 // Data never becomes stale if it doesn't have FreshnessPeriod field
637 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
638 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
639 }
640 else {
641 needDnl = true;
642 }
643
644 if (!needDnl) {
645 return;
646 }
647
648 // Dead Nonce List insert
649 if (upstream == 0) {
650 // insert all outgoing Nonces
651 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
652 std::for_each(outRecords.begin(), outRecords.end(),
653 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
654 }
655 else {
656 // insert outgoing Nonce of a specific face
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700657 pit::OutRecordCollection::const_iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700658 if (outRecord != pitEntry.getOutRecords().end()) {
659 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
660 }
661 }
662}
663
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800664} // namespace nfd