blob: 0b4c6530a75f6657d25d63c9302bef0b30b85f6c [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"
Junxiao Shifef73e42016-03-29 14:15:05 -070027#include "pit-algorithm.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060028#include "core/logger.hpp"
Junxiao Shiaf6569a2014-06-14 00:01:34 -070029#include "core/random.hpp"
Junxiao Shifaf3eb02015-02-16 10:50:36 -070030#include "strategy.hpp"
Junxiao Shiaf6569a2014-06-14 00:01:34 -070031#include <boost/random/uniform_int_distribution.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080032
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080033namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080034
Junxiao Shi8c8d2182014-01-30 22:33:00 -070035NFD_LOG_INIT("Forwarder");
36
Junxiao Shif3c07812014-03-11 21:48:49 -070037using fw::Strategy;
38
Junxiao Shic041ca32014-02-25 20:01:15 -070039Forwarder::Forwarder()
Junxiao Shia4f2be82014-03-02 22:56:41 -070040 : m_faceTable(*this)
HangZhangad4afd12014-03-01 11:03:08 +080041 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060042 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080043 , m_measurements(m_nameTree)
Junxiao Shif3c07812014-03-11 21:48:49 -070044 , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080045{
Junxiao Shif3c07812014-03-11 21:48:49 -070046 fw::installStrategies(*this);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080047}
48
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060049Forwarder::~Forwarder()
50{
Junxiao Shi0355e9f2015-09-02 07:24:53 -070051}
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060052
Junxiao Shi0355e9f2015-09-02 07:24:53 -070053void
54Forwarder::startProcessInterest(Face& face, const Interest& interest)
55{
56 // check fields used by forwarding are well-formed
57 try {
58 if (interest.hasLink()) {
59 interest.getLink();
60 }
61 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -070062 catch (const tlv::Error&) {
Junxiao Shi0355e9f2015-09-02 07:24:53 -070063 NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() <<
64 " interest=" << interest.getName() << " malformed");
65 // It's safe to call interest.getName() because Name has been fully parsed
66 return;
67 }
68
69 this->onIncomingInterest(face, interest);
70}
71
72void
73Forwarder::startProcessData(Face& face, const Data& data)
74{
75 // check fields used by forwarding are well-formed
76 // (none needed)
77
78 this->onIncomingData(face, data);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060079}
80
Alexander Afanasyev33b72772014-01-26 23:22:58 -080081void
Junxiao Shi5e5e4452015-09-24 16:56:52 -070082Forwarder::startProcessNack(Face& face, const lp::Nack& nack)
83{
84 // check fields used by forwarding are well-formed
85 try {
86 if (nack.getInterest().hasLink()) {
87 nack.getInterest().getLink();
88 }
89 }
90 catch (const tlv::Error&) {
91 NFD_LOG_DEBUG("startProcessNack face=" << face.getId() <<
92 " nack=" << nack.getInterest().getName() <<
93 "~" << nack.getReason() << " malformed");
94 return;
95 }
96
97 this->onIncomingNack(face, nack);
98}
99
100void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700101Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
102{
103 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700104 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
105 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000106 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700107 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -0700108
Junxiao Shi88884492014-02-15 15:57:43 -0700109 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700110 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700111 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700112 if (isViolatingLocalhost) {
113 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
114 " interest=" << interest.getName() << " violates /localhost");
115 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700116 return;
117 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700118
Junxiao Shi330136a2016-03-10 04:53:08 -0700119 // detect duplicate Nonce with Dead Nonce List
120 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
121 if (hasDuplicateNonceInDnl) {
122 // goto Interest loop pipeline
123 this->onInterestLoop(inFace, interest);
124 return;
125 }
126
Junxiao Shid3c792f2014-01-30 00:46:13 -0700127 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700128 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700129
Junxiao Shi330136a2016-03-10 04:53:08 -0700130 // detect duplicate Nonce in PIT entry
Junxiao Shifef73e42016-03-29 14:15:05 -0700131 bool hasDuplicateNonceInPit = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace) !=
132 fw::DUPLICATE_NONCE_NONE;
Junxiao Shi330136a2016-03-10 04:53:08 -0700133 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700134 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700135 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700136 return;
137 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700138
Junxiao Shid3c792f2014-01-30 00:46:13 -0700139 // cancel unsatisfy & straggler timer
140 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700141
Junxiao Shif3c07812014-03-11 21:48:49 -0700142 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700143 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600144 m_cs.find(interest,
145 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
146 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700147 }
mzhang4eab72492015-02-25 11:16:09 -0600148 else {
149 this->onContentStoreMiss(inFace, pitEntry, interest);
150 }
151}
Junxiao Shic041ca32014-02-25 20:01:15 -0700152
mzhang4eab72492015-02-25 11:16:09 -0600153void
Junxiao Shi330136a2016-03-10 04:53:08 -0700154Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700155{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700156 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700157 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700158 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
159 " interest=" << interest.getName() <<
160 " drop");
161 return;
162 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700163
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700164 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
165 " interest=" << interest.getName() <<
166 " send-Nack-duplicate");
167
168 // send Nack with reason=DUPLICATE
169 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
170 lp::Nack nack(interest);
171 nack.setReason(lp::NackReason::DUPLICATE);
172 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700173}
174
175void
mzhang4eab72492015-02-25 11:16:09 -0600176Forwarder::onContentStoreMiss(const Face& inFace,
177 shared_ptr<pit::Entry> pitEntry,
178 const Interest& interest)
179{
180 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
181
182 shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this());
Junxiao Shi4846f372016-04-05 13:39:30 -0700183 // insert in-record
mzhang4eab72492015-02-25 11:16:09 -0600184 pitEntry->insertOrUpdateInRecord(face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700185
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700186 // set PIT unsatisfy timer
187 this->setUnsatisfyTimer(pitEntry);
188
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000189 // dispatch to strategy: after incoming Interest
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000190 this->dispatchToStrategy(pitEntry,
Junxiao Shi8d843142016-07-11 22:42:42 +0000191 [&] (fw::Strategy* strategy) { strategy->afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700192}
193
194void
mzhang4eab72492015-02-25 11:16:09 -0600195Forwarder::onContentStoreHit(const Face& inFace,
196 shared_ptr<pit::Entry> pitEntry,
197 const Interest& interest,
198 const Data& data)
199{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500200 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600201
Junxiao Shicde37ad2015-12-24 01:02:05 -0700202 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600203 // XXX should we lookup PIT for other Interests that also match csMatch?
204
205 // set PIT straggler timer
206 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
207
208 // goto outgoing Data pipeline
209 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
210}
211
Junxiao Shid3c792f2014-01-30 00:46:13 -0700212void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700213Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
214 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700215{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700216 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700217 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
218 return;
219 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700220 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
221 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700222
Junxiao Shi57f0f312014-03-16 11:52:20 -0700223 // scope control
Junxiao Shifef73e42016-03-29 14:15:05 -0700224 if (fw::violatesScope(*pitEntry, outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700225 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700226 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700227 return;
228 }
229
Junxiao Shid3c792f2014-01-30 00:46:13 -0700230 // pick Interest
Junxiao Shi891f47b2016-06-20 00:02:11 +0000231 // The outgoing Interest picked is the last incoming Interest that does not come from outFace.
232 // If all in-records come from outFace, it's fine to pick that.
233 // This happens when there's only one in-record that comes from outFace.
234 // The legit use is for vehicular network; otherwise, strategy shouldn't send to the sole inFace.
Junxiao Shi4846f372016-04-05 13:39:30 -0700235 pit::InRecordCollection::iterator pickedInRecord = std::max_element(
Junxiao Shi891f47b2016-06-20 00:02:11 +0000236 pitEntry->in_begin(), pitEntry->in_end(),
237 [&outFace] (const pit::InRecord& a, const pit::InRecord& b) {
238 bool isOutFaceA = a.getFace().get() == &outFace;
239 bool isOutFaceB = b.getFace().get() == &outFace;
240 return (isOutFaceA > isOutFaceB) ||
241 (isOutFaceA == isOutFaceB && a.getLastRenewed() < b.getLastRenewed());
242 });
Junxiao Shi4846f372016-04-05 13:39:30 -0700243 BOOST_ASSERT(pickedInRecord != pitEntry->in_end());
244 auto interest = const_pointer_cast<Interest>(pickedInRecord->getInterest().shared_from_this());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700245
246 if (wantNewNonce) {
247 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700248 static boost::random::uniform_int_distribution<uint32_t> dist;
249 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700250 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700251
Junxiao Shi4846f372016-04-05 13:39:30 -0700252 // insert out-record
Junxiao Shid938a6b2014-05-11 23:40:29 -0700253 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700254
Junxiao Shid3c792f2014-01-30 00:46:13 -0700255 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700256 outFace.sendInterest(*interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700257 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700258}
259
260void
Junxiao Shi09498f02014-02-26 19:41:08 -0700261Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700262{
Junxiao Shifef73e42016-03-29 14:15:05 -0700263 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700264 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
265 " cannot reject forwarded Interest");
266 return;
267 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700268 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700269
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700270 // cancel unsatisfy & straggler timer
271 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
272
Junxiao Shid3c792f2014-01-30 00:46:13 -0700273 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700274 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700275}
276
277void
278Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
279{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700280 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
281
Junxiao Shid3c792f2014-01-30 00:46:13 -0700282 // invoke PIT unsatisfied callback
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000283 this->dispatchToStrategy(pitEntry,
284 [&] (fw::Strategy* strategy) { strategy->beforeExpirePendingInterest(pitEntry); });
Junxiao Shic041ca32014-02-25 20:01:15 -0700285
Junxiao Shia110f262014-10-12 12:35:20 -0700286 // goto Interest Finalize pipeline
287 this->onInterestFinalize(pitEntry, false);
288}
289
290void
291Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
292 const time::milliseconds& dataFreshnessPeriod)
293{
294 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
295 (isSatisfied ? " satisfied" : " unsatisfied"));
296
297 // Dead Nonce List insert if necessary
298 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
299
Junxiao Shif3c07812014-03-11 21:48:49 -0700300 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700301 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600302 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700303}
304
305void
306Forwarder::onIncomingData(Face& inFace, const Data& data)
307{
308 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700309 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000310 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700311 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700312
Junxiao Shi88884492014-02-15 15:57:43 -0700313 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700314 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700315 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700316 if (isViolatingLocalhost) {
317 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
318 " data=" << data.getName() << " violates /localhost");
319 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700320 return;
321 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700322
Junxiao Shid3c792f2014-01-30 00:46:13 -0700323 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700324 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
325 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700326 // goto Data unsolicited pipeline
327 this->onDataUnsolicited(inFace, data);
328 return;
329 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700330
Junxiao Shid3c792f2014-01-30 00:46:13 -0700331 // CS insert
332 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700333
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700334 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700335 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700336 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700337 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700338 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700339
Junxiao Shid3c792f2014-01-30 00:46:13 -0700340 // cancel unsatisfy & straggler timer
341 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700342
Junxiao Shid3c792f2014-01-30 00:46:13 -0700343 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700344 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
345 if (inRecord.getExpiry() > now) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700346 pendingDownstreams.insert(inRecord.getFace().get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700347 }
348 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700349
Junxiao Shid938a6b2014-05-11 23:40:29 -0700350 // invoke PIT satisfy callback
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000351 this->dispatchToStrategy(pitEntry,
352 [&] (fw::Strategy* strategy) { strategy->beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700353
Junxiao Shi4846f372016-04-05 13:39:30 -0700354 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700355 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
356
Junxiao Shid3c792f2014-01-30 00:46:13 -0700357 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700358 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700359 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700360
Junxiao Shid3c792f2014-01-30 00:46:13 -0700361 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700362 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700363 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700364
Junxiao Shid3c792f2014-01-30 00:46:13 -0700365 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700366 for (Face* pendingDownstream : pendingDownstreams) {
367 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700368 continue;
369 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700370 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700371 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700372 }
373}
374
375void
376Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
377{
378 // accept to cache?
Junxiao Shicde37ad2015-12-24 01:02:05 -0700379 bool acceptToCache = inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700380 if (acceptToCache) {
381 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700382 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700383 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700384
Junxiao Shif3c07812014-03-11 21:48:49 -0700385 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
386 " data=" << data.getName() <<
387 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700388}
389
390void
391Forwarder::onOutgoingData(const Data& data, Face& outFace)
392{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700393 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700394 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
395 return;
396 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700397 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
398
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700399 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700400 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700401 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700402 if (isViolatingLocalhost) {
403 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
404 " data=" << data.getName() << " violates /localhost");
405 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700406 return;
407 }
408
Junxiao Shif3c07812014-03-11 21:48:49 -0700409 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700410
Junxiao Shid3c792f2014-01-30 00:46:13 -0700411 // send Data
412 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700413 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700414}
415
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700416void
417Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
418{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000419 // receive Nack
420 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700421 ++m_counters.nInNacks;
422
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700423 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700424 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700425 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
426 " nack=" << nack.getInterest().getName() <<
427 "~" << nack.getReason() << " face-is-multi-access");
428 return;
429 }
430
431 // PIT match
432 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
433 // if no PIT entry found, drop
434 if (pitEntry == nullptr) {
435 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
436 " nack=" << nack.getInterest().getName() <<
437 "~" << nack.getReason() << " no-PIT-entry");
438 return;
439 }
440
441 // has out-record?
442 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
443 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700444 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700445 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
446 " nack=" << nack.getInterest().getName() <<
447 "~" << nack.getReason() << " no-out-record");
448 return;
449 }
450
451 // if out-record has different Nonce, drop
452 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
453 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
454 " nack=" << nack.getInterest().getName() <<
455 "~" << nack.getReason() << " wrong-Nonce " <<
456 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
457 return;
458 }
459
460 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
461 " nack=" << nack.getInterest().getName() <<
462 "~" << nack.getReason() << " OK");
463
464 // record Nack on out-record
465 outRecord->setIncomingNack(nack);
466
467 // trigger strategy: after receive NACK
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000468 this->dispatchToStrategy(pitEntry,
Junxiao Shi8d843142016-07-11 22:42:42 +0000469 [&] (fw::Strategy* strategy) { strategy->afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700470}
471
472void
473Forwarder::onOutgoingNack(shared_ptr<pit::Entry> pitEntry, const Face& outFace,
474 const lp::NackHeader& nack)
475{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700476 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700477 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
478 " nack=" << pitEntry->getInterest().getName() <<
479 "~" << nack.getReason() << " no-in-record");
480 return;
481 }
482
483 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700484 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700485
486 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700487 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700488 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
489 " nack=" << pitEntry->getInterest().getName() <<
490 "~" << nack.getReason() << " no-in-record");
491 return;
492 }
493
494 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700495 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700496 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
497 " nack=" << pitEntry->getInterest().getName() <<
498 "~" << nack.getReason() << " face-is-multi-access");
499 return;
500 }
501
502 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
503 " nack=" << pitEntry->getInterest().getName() <<
504 "~" << nack.getReason() << " OK");
505
506 // create Nack packet with the Interest from in-record
507 lp::Nack nackPkt(inRecord->getInterest());
508 nackPkt.setHeader(nack);
509
510 // erase in-record
511 pitEntry->deleteInRecord(outFace);
512
513 // send Nack on face
514 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700515 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700516}
517
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000518const fib::Entry&
519Forwarder::lookupFib(const pit::Entry& pitEntry) const
520{
521 const Interest& interest = pitEntry.getInterest();
522 // has Link object?
523 if (!interest.hasLink()) {
524 // FIB lookup with Interest name
Junxiao Shia6de4292016-07-12 02:08:10 +0000525 const fib::Entry& fibEntry = m_fib.findLongestPrefixMatch(pitEntry);
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000526 NFD_LOG_TRACE("lookupFib noLinkObject found=" << fibEntry.getPrefix());
527 return fibEntry;
528 }
529
530 const Link& link = interest.getLink();
531
532 // in producer region?
533 if (m_networkRegionTable.isInProducerRegion(link)) {
534 // FIB lookup with Interest name
Junxiao Shia6de4292016-07-12 02:08:10 +0000535 const fib::Entry& fibEntry = m_fib.findLongestPrefixMatch(pitEntry);
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000536 NFD_LOG_TRACE("lookupFib inProducerRegion found=" << fibEntry.getPrefix());
537 return fibEntry;
538 }
539
540 // has SelectedDelegation?
541 if (interest.hasSelectedDelegation()) {
542 // FIB lookup with SelectedDelegation
543 Name selectedDelegation = interest.getSelectedDelegation();
Junxiao Shia6de4292016-07-12 02:08:10 +0000544 const fib::Entry& fibEntry = m_fib.findLongestPrefixMatch(selectedDelegation);
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000545 NFD_LOG_TRACE("lookupFib hasSelectedDelegation=" << selectedDelegation << " found=" << fibEntry.getPrefix());
546 return fibEntry;
547 }
548
549 // FIB lookup with first delegation Name
Junxiao Shia6de4292016-07-12 02:08:10 +0000550 const fib::Entry& fibEntry0 = m_fib.findLongestPrefixMatch(link.getDelegations().begin()->second);
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000551 // in default-free zone?
552 bool isDefaultFreeZone = !(fibEntry0.getPrefix().size() == 0 && fibEntry0.hasNextHops());
553 if (!isDefaultFreeZone) {
554 NFD_LOG_TRACE("onContentStoreMiss inConsumerRegion found=" << fibEntry0.getPrefix());
555 return fibEntry0;
556 }
557
558 // choose and set SelectedDelegation
559 for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) {
560 const Name& delegationName = delegation.second;
Junxiao Shia6de4292016-07-12 02:08:10 +0000561 const fib::Entry& fibEntry = m_fib.findLongestPrefixMatch(delegationName);
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000562 if (fibEntry.hasNextHops()) {
563 /// \todo Don't modify in-record Interests.
564 /// Set SelectedDelegation in outgoing Interest pipeline.
565 std::for_each(pitEntry.in_begin(), pitEntry.in_end(),
566 [&delegationName] (const pit::InRecord& inR) {
567 const_cast<Interest&>(inR.getInterest()).setSelectedDelegation(delegationName);
568 });
569 NFD_LOG_TRACE("onContentStoreMiss enterDefaultFreeZone"
570 << " setSelectedDelegation=" << delegationName);
571 return fibEntry;
572 }
573 }
574 BOOST_ASSERT(false);
575 return fibEntry0;
576}
577
Junxiao Shid3c792f2014-01-30 00:46:13 -0700578static inline bool
579compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
580{
581 return a.getExpiry() < b.getExpiry();
582}
583
584void
585Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
586{
Junxiao Shi4846f372016-04-05 13:39:30 -0700587 pit::InRecordCollection::iterator lastExpiring =
588 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700589
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700590 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700591 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
592 if (lastExpiryFromNow <= time::seconds::zero()) {
593 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700594 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700595
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700596 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700597 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700598 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
599}
600
601void
Junxiao Shia110f262014-10-12 12:35:20 -0700602Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
603 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700604{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700605 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700606
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700607 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700608 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700609 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700610}
611
612void
613Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
614{
Junxiao Shic041ca32014-02-25 20:01:15 -0700615 scheduler::cancel(pitEntry->m_unsatisfyTimer);
616 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700617}
618
Junxiao Shia110f262014-10-12 12:35:20 -0700619static inline void
620insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
621 const pit::OutRecord& outRecord)
622{
623 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
624}
625
626void
627Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
628 const time::milliseconds& dataFreshnessPeriod,
629 Face* upstream)
630{
631 // need Dead Nonce List insert?
632 bool needDnl = false;
633 if (isSatisfied) {
634 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
635 // Data never becomes stale if it doesn't have FreshnessPeriod field
636 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
637 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
638 }
639 else {
640 needDnl = true;
641 }
642
643 if (!needDnl) {
644 return;
645 }
646
647 // Dead Nonce List insert
648 if (upstream == 0) {
649 // insert all outgoing Nonces
650 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
651 std::for_each(outRecords.begin(), outRecords.end(),
652 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
653 }
654 else {
655 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700656 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700657 if (outRecord != pitEntry.getOutRecords().end()) {
658 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
659 }
660 }
661}
662
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800663} // namespace nfd