blob: adcb134f98a385f71ee4426e2826d81bd394af60 [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
190 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
191 shared_ptr<fib::Entry> fibEntryP = const_cast<fib::Entry&>(fibEntry).shared_from_this();
192 this->dispatchToStrategy(pitEntry,
193 [&] (fw::Strategy* strategy) {
194 strategy->afterReceiveInterest(inFace, interest, fibEntryP, pitEntry);
195 });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700196}
197
198void
mzhang4eab72492015-02-25 11:16:09 -0600199Forwarder::onContentStoreHit(const Face& inFace,
200 shared_ptr<pit::Entry> pitEntry,
201 const Interest& interest,
202 const Data& data)
203{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500204 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600205
Junxiao Shicde37ad2015-12-24 01:02:05 -0700206 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600207 // XXX should we lookup PIT for other Interests that also match csMatch?
208
209 // set PIT straggler timer
210 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
211
212 // goto outgoing Data pipeline
213 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
214}
215
Junxiao Shid3c792f2014-01-30 00:46:13 -0700216void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700217Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
218 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700219{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700220 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700221 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
222 return;
223 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700224 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
225 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700226
Junxiao Shi57f0f312014-03-16 11:52:20 -0700227 // scope control
Junxiao Shifef73e42016-03-29 14:15:05 -0700228 if (fw::violatesScope(*pitEntry, outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700229 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700230 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700231 return;
232 }
233
Junxiao Shid3c792f2014-01-30 00:46:13 -0700234 // pick Interest
Junxiao Shi891f47b2016-06-20 00:02:11 +0000235 // The outgoing Interest picked is the last incoming Interest that does not come from outFace.
236 // If all in-records come from outFace, it's fine to pick that.
237 // This happens when there's only one in-record that comes from outFace.
238 // The legit use is for vehicular network; otherwise, strategy shouldn't send to the sole inFace.
Junxiao Shi4846f372016-04-05 13:39:30 -0700239 pit::InRecordCollection::iterator pickedInRecord = std::max_element(
Junxiao Shi891f47b2016-06-20 00:02:11 +0000240 pitEntry->in_begin(), pitEntry->in_end(),
241 [&outFace] (const pit::InRecord& a, const pit::InRecord& b) {
242 bool isOutFaceA = a.getFace().get() == &outFace;
243 bool isOutFaceB = b.getFace().get() == &outFace;
244 return (isOutFaceA > isOutFaceB) ||
245 (isOutFaceA == isOutFaceB && a.getLastRenewed() < b.getLastRenewed());
246 });
Junxiao Shi4846f372016-04-05 13:39:30 -0700247 BOOST_ASSERT(pickedInRecord != pitEntry->in_end());
248 auto interest = const_pointer_cast<Interest>(pickedInRecord->getInterest().shared_from_this());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700249
250 if (wantNewNonce) {
251 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700252 static boost::random::uniform_int_distribution<uint32_t> dist;
253 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700254 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700255
Junxiao Shi4846f372016-04-05 13:39:30 -0700256 // insert out-record
Junxiao Shid938a6b2014-05-11 23:40:29 -0700257 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700258
Junxiao Shid3c792f2014-01-30 00:46:13 -0700259 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700260 outFace.sendInterest(*interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700261 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700262}
263
264void
Junxiao Shi09498f02014-02-26 19:41:08 -0700265Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700266{
Junxiao Shifef73e42016-03-29 14:15:05 -0700267 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700268 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
269 " cannot reject forwarded Interest");
270 return;
271 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700272 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700273
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700274 // cancel unsatisfy & straggler timer
275 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
276
Junxiao Shid3c792f2014-01-30 00:46:13 -0700277 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700278 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700279}
280
281void
282Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
283{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700284 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
285
Junxiao Shid3c792f2014-01-30 00:46:13 -0700286 // invoke PIT unsatisfied callback
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000287 this->dispatchToStrategy(pitEntry,
288 [&] (fw::Strategy* strategy) { strategy->beforeExpirePendingInterest(pitEntry); });
Junxiao Shic041ca32014-02-25 20:01:15 -0700289
Junxiao Shia110f262014-10-12 12:35:20 -0700290 // goto Interest Finalize pipeline
291 this->onInterestFinalize(pitEntry, false);
292}
293
294void
295Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
296 const time::milliseconds& dataFreshnessPeriod)
297{
298 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
299 (isSatisfied ? " satisfied" : " unsatisfied"));
300
301 // Dead Nonce List insert if necessary
302 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
303
Junxiao Shif3c07812014-03-11 21:48:49 -0700304 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700305 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600306 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700307}
308
309void
310Forwarder::onIncomingData(Face& inFace, const Data& data)
311{
312 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700313 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000314 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700315 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700316
Junxiao Shi88884492014-02-15 15:57:43 -0700317 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700318 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700319 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700320 if (isViolatingLocalhost) {
321 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
322 " data=" << data.getName() << " violates /localhost");
323 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700324 return;
325 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700326
Junxiao Shid3c792f2014-01-30 00:46:13 -0700327 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700328 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
329 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700330 // goto Data unsolicited pipeline
331 this->onDataUnsolicited(inFace, data);
332 return;
333 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700334
Junxiao Shid3c792f2014-01-30 00:46:13 -0700335 // CS insert
336 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700337
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700338 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700339 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700340 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700341 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700342 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700343
Junxiao Shid3c792f2014-01-30 00:46:13 -0700344 // cancel unsatisfy & straggler timer
345 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700346
Junxiao Shid3c792f2014-01-30 00:46:13 -0700347 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700348 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
349 if (inRecord.getExpiry() > now) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700350 pendingDownstreams.insert(inRecord.getFace().get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700351 }
352 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700353
Junxiao Shid938a6b2014-05-11 23:40:29 -0700354 // invoke PIT satisfy callback
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000355 this->dispatchToStrategy(pitEntry,
356 [&] (fw::Strategy* strategy) { strategy->beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700357
Junxiao Shi4846f372016-04-05 13:39:30 -0700358 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700359 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
360
Junxiao Shid3c792f2014-01-30 00:46:13 -0700361 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700362 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700363 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700364
Junxiao Shid3c792f2014-01-30 00:46:13 -0700365 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700366 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700367 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700368
Junxiao Shid3c792f2014-01-30 00:46:13 -0700369 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700370 for (Face* pendingDownstream : pendingDownstreams) {
371 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700372 continue;
373 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700374 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700375 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700376 }
377}
378
379void
380Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
381{
382 // accept to cache?
Junxiao Shicde37ad2015-12-24 01:02:05 -0700383 bool acceptToCache = inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700384 if (acceptToCache) {
385 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700386 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700387 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700388
Junxiao Shif3c07812014-03-11 21:48:49 -0700389 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
390 " data=" << data.getName() <<
391 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700392}
393
394void
395Forwarder::onOutgoingData(const Data& data, Face& outFace)
396{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700397 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700398 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
399 return;
400 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700401 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
402
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700403 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700404 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700405 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700406 if (isViolatingLocalhost) {
407 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
408 " data=" << data.getName() << " violates /localhost");
409 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700410 return;
411 }
412
Junxiao Shif3c07812014-03-11 21:48:49 -0700413 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700414
Junxiao Shid3c792f2014-01-30 00:46:13 -0700415 // send Data
416 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700417 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700418}
419
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700420void
421Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
422{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000423 // receive Nack
424 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700425 ++m_counters.nInNacks;
426
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700427 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700428 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700429 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
430 " nack=" << nack.getInterest().getName() <<
431 "~" << nack.getReason() << " face-is-multi-access");
432 return;
433 }
434
435 // PIT match
436 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
437 // if no PIT entry found, drop
438 if (pitEntry == nullptr) {
439 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
440 " nack=" << nack.getInterest().getName() <<
441 "~" << nack.getReason() << " no-PIT-entry");
442 return;
443 }
444
445 // has out-record?
446 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
447 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700448 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700449 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
450 " nack=" << nack.getInterest().getName() <<
451 "~" << nack.getReason() << " no-out-record");
452 return;
453 }
454
455 // if out-record has different Nonce, drop
456 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
457 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
458 " nack=" << nack.getInterest().getName() <<
459 "~" << nack.getReason() << " wrong-Nonce " <<
460 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
461 return;
462 }
463
464 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
465 " nack=" << nack.getInterest().getName() <<
466 "~" << nack.getReason() << " OK");
467
468 // record Nack on out-record
469 outRecord->setIncomingNack(nack);
470
471 // trigger strategy: after receive NACK
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000472 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
473 shared_ptr<fib::Entry> fibEntryP = const_cast<fib::Entry&>(fibEntry).shared_from_this();
474 this->dispatchToStrategy(pitEntry,
475 [&] (fw::Strategy* strategy) {
476 strategy->afterReceiveNack(inFace, nack, fibEntryP, pitEntry);
477 });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700478}
479
480void
481Forwarder::onOutgoingNack(shared_ptr<pit::Entry> pitEntry, const Face& outFace,
482 const lp::NackHeader& nack)
483{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700484 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700485 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
486 " nack=" << pitEntry->getInterest().getName() <<
487 "~" << nack.getReason() << " no-in-record");
488 return;
489 }
490
491 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700492 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700493
494 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700495 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700496 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
497 " nack=" << pitEntry->getInterest().getName() <<
498 "~" << nack.getReason() << " no-in-record");
499 return;
500 }
501
502 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700503 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700504 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
505 " nack=" << pitEntry->getInterest().getName() <<
506 "~" << nack.getReason() << " face-is-multi-access");
507 return;
508 }
509
510 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
511 " nack=" << pitEntry->getInterest().getName() <<
512 "~" << nack.getReason() << " OK");
513
514 // create Nack packet with the Interest from in-record
515 lp::Nack nackPkt(inRecord->getInterest());
516 nackPkt.setHeader(nack);
517
518 // erase in-record
519 pitEntry->deleteInRecord(outFace);
520
521 // send Nack on face
522 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700523 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700524}
525
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000526const fib::Entry&
527Forwarder::lookupFib(const pit::Entry& pitEntry) const
528{
529 const Interest& interest = pitEntry.getInterest();
530 // has Link object?
531 if (!interest.hasLink()) {
532 // FIB lookup with Interest name
533 const fib::Entry& fibEntry = *m_fib.findLongestPrefixMatch(pitEntry);
534 NFD_LOG_TRACE("lookupFib noLinkObject found=" << fibEntry.getPrefix());
535 return fibEntry;
536 }
537
538 const Link& link = interest.getLink();
539
540 // in producer region?
541 if (m_networkRegionTable.isInProducerRegion(link)) {
542 // FIB lookup with Interest name
543 const fib::Entry& fibEntry = *m_fib.findLongestPrefixMatch(pitEntry);
544 NFD_LOG_TRACE("lookupFib inProducerRegion found=" << fibEntry.getPrefix());
545 return fibEntry;
546 }
547
548 // has SelectedDelegation?
549 if (interest.hasSelectedDelegation()) {
550 // FIB lookup with SelectedDelegation
551 Name selectedDelegation = interest.getSelectedDelegation();
552 const fib::Entry& fibEntry = *m_fib.findLongestPrefixMatch(selectedDelegation);
553 NFD_LOG_TRACE("lookupFib hasSelectedDelegation=" << selectedDelegation << " found=" << fibEntry.getPrefix());
554 return fibEntry;
555 }
556
557 // FIB lookup with first delegation Name
558 const fib::Entry& fibEntry0 = *m_fib.findLongestPrefixMatch(link.getDelegations().begin()->second);
559 // in default-free zone?
560 bool isDefaultFreeZone = !(fibEntry0.getPrefix().size() == 0 && fibEntry0.hasNextHops());
561 if (!isDefaultFreeZone) {
562 NFD_LOG_TRACE("onContentStoreMiss inConsumerRegion found=" << fibEntry0.getPrefix());
563 return fibEntry0;
564 }
565
566 // choose and set SelectedDelegation
567 for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) {
568 const Name& delegationName = delegation.second;
569 const fib::Entry& fibEntry = *m_fib.findLongestPrefixMatch(delegationName);
570 if (fibEntry.hasNextHops()) {
571 /// \todo Don't modify in-record Interests.
572 /// Set SelectedDelegation in outgoing Interest pipeline.
573 std::for_each(pitEntry.in_begin(), pitEntry.in_end(),
574 [&delegationName] (const pit::InRecord& inR) {
575 const_cast<Interest&>(inR.getInterest()).setSelectedDelegation(delegationName);
576 });
577 NFD_LOG_TRACE("onContentStoreMiss enterDefaultFreeZone"
578 << " setSelectedDelegation=" << delegationName);
579 return fibEntry;
580 }
581 }
582 BOOST_ASSERT(false);
583 return fibEntry0;
584}
585
Junxiao Shid3c792f2014-01-30 00:46:13 -0700586static inline bool
587compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
588{
589 return a.getExpiry() < b.getExpiry();
590}
591
592void
593Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
594{
Junxiao Shi4846f372016-04-05 13:39:30 -0700595 pit::InRecordCollection::iterator lastExpiring =
596 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700597
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700598 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700599 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
600 if (lastExpiryFromNow <= time::seconds::zero()) {
601 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700602 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700603
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700604 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700605 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700606 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
607}
608
609void
Junxiao Shia110f262014-10-12 12:35:20 -0700610Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
611 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700612{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700613 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700614
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700615 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700616 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700617 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700618}
619
620void
621Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
622{
Junxiao Shic041ca32014-02-25 20:01:15 -0700623 scheduler::cancel(pitEntry->m_unsatisfyTimer);
624 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700625}
626
Junxiao Shia110f262014-10-12 12:35:20 -0700627static inline void
628insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
629 const pit::OutRecord& outRecord)
630{
631 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
632}
633
634void
635Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
636 const time::milliseconds& dataFreshnessPeriod,
637 Face* upstream)
638{
639 // need Dead Nonce List insert?
640 bool needDnl = false;
641 if (isSatisfied) {
642 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
643 // Data never becomes stale if it doesn't have FreshnessPeriod field
644 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
645 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
646 }
647 else {
648 needDnl = true;
649 }
650
651 if (!needDnl) {
652 return;
653 }
654
655 // Dead Nonce List insert
656 if (upstream == 0) {
657 // insert all outgoing Nonces
658 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
659 std::for_each(outRecords.begin(), outRecords.end(),
660 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
661 }
662 else {
663 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700664 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700665 if (outRecord != pitEntry.getOutRecords().end()) {
666 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
667 }
668 }
669}
670
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800671} // namespace nfd