blob: 27bfa383b81d8adef09d99df3fcae62a9a39e15c [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 }
63 catch (tlv::Error&) {
64 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 Shid3c792f2014-01-30 00:46:13 -070083Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
84{
85 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -070086 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
87 " interest=" << interest.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -070088 const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId());
Junxiao Shi33152f12014-07-16 19:54:32 -070089 ++m_counters.getNInInterests();
Junxiao Shic041ca32014-02-25 20:01:15 -070090
Junxiao Shi88884492014-02-15 15:57:43 -070091 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -070092 bool isViolatingLocalhost = !inFace.isLocal() &&
93 LOCALHOST_NAME.isPrefixOf(interest.getName());
94 if (isViolatingLocalhost) {
95 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
96 " interest=" << interest.getName() << " violates /localhost");
97 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -070098 return;
99 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700100
Junxiao Shid3c792f2014-01-30 00:46:13 -0700101 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700102 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700103
Junxiao Shia110f262014-10-12 12:35:20 -0700104 // detect duplicate Nonce
105 int dnw = pitEntry->findNonce(interest.getNonce(), inFace);
106 bool hasDuplicateNonce = (dnw != pit::DUPLICATE_NONCE_NONE) ||
107 m_deadNonceList.has(interest.getName(), interest.getNonce());
108 if (hasDuplicateNonce) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700109 // goto Interest loop pipeline
110 this->onInterestLoop(inFace, interest, pitEntry);
111 return;
112 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700113
Junxiao Shid3c792f2014-01-30 00:46:13 -0700114 // cancel unsatisfy & straggler timer
115 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700116
Junxiao Shif3c07812014-03-11 21:48:49 -0700117 // is pending?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700118 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shie17349a2014-03-25 00:55:38 -0700119 bool isPending = inRecords.begin() != inRecords.end();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700120 if (!isPending) {
mzhang4eab72492015-02-25 11:16:09 -0600121 m_cs.find(interest,
122 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
123 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700124 }
mzhang4eab72492015-02-25 11:16:09 -0600125 else {
126 this->onContentStoreMiss(inFace, pitEntry, interest);
127 }
128}
Junxiao Shic041ca32014-02-25 20:01:15 -0700129
mzhang4eab72492015-02-25 11:16:09 -0600130void
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700131Forwarder::onInterestLoop(Face& inFace, const Interest& interest,
132 shared_ptr<pit::Entry> pitEntry)
133{
134 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
135 " interest=" << interest.getName());
136
137 // (drop)
138}
139
140void
mzhang4eab72492015-02-25 11:16:09 -0600141Forwarder::onContentStoreMiss(const Face& inFace,
142 shared_ptr<pit::Entry> pitEntry,
143 const Interest& interest)
144{
145 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
146
147 shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700148 // insert InRecord
mzhang4eab72492015-02-25 11:16:09 -0600149 pitEntry->insertOrUpdateInRecord(face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700150
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700151 // set PIT unsatisfy timer
152 this->setUnsatisfyTimer(pitEntry);
153
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700154 shared_ptr<fib::Entry> fibEntry;
155 // has Link object?
156 if (!interest.hasLink()) {
157 // FIB lookup with Interest name
158 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
159 NFD_LOG_TRACE("onContentStoreMiss noLinkObject");
160 }
161 else {
162 const Link& link = interest.getLink();
163
164 // in producer region?
165 if (m_networkRegionTable.isInProducerRegion(link)) {
166 // FIB lookup with Interest name
167 fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
168 NFD_LOG_TRACE("onContentStoreMiss inProducerRegion");
169 }
170 // has SelectedDelegation?
171 else if (interest.hasSelectedDelegation()) {
172 // FIB lookup with SelectedDelegation
173 fibEntry = m_fib.findLongestPrefixMatch(interest.getSelectedDelegation());
174 NFD_LOG_TRACE("onContentStoreMiss hasSelectedDelegation=" << interest.getSelectedDelegation());
175 }
176 else {
177 // FIB lookup with first delegation Name
178 fibEntry = m_fib.findLongestPrefixMatch(link.getDelegations().begin()->second);
179
180 // in default-free zone?
181 bool isDefaultFreeZone = !(fibEntry->getPrefix().size() == 0 && fibEntry->hasNextHops());
182 if (isDefaultFreeZone) {
183 // choose and set SelectedDelegation
184 for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) {
185 const Name& delegationName = delegation.second;
186 fibEntry = m_fib.findLongestPrefixMatch(delegationName);
187 if (fibEntry->hasNextHops()) {
188 const_cast<Interest&>(interest).setSelectedDelegation(delegationName);
189 NFD_LOG_TRACE("onContentStoreMiss enterDefaultFreeZone"
190 << " setSelectedDelegation=" << delegationName);
191 break;
192 }
193 }
194 }
195 else {
196 NFD_LOG_TRACE("onContentStoreMiss inConsumerRegion");
197 }
198 }
199 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700200
Junxiao Shid3c792f2014-01-30 00:46:13 -0700201 // dispatch to strategy
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700202 BOOST_ASSERT(fibEntry != nullptr);
Junxiao Shif3c07812014-03-11 21:48:49 -0700203 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700204 cref(inFace), cref(interest), fibEntry, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700205}
206
207void
mzhang4eab72492015-02-25 11:16:09 -0600208Forwarder::onContentStoreHit(const Face& inFace,
209 shared_ptr<pit::Entry> pitEntry,
210 const Interest& interest,
211 const Data& data)
212{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500213 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600214
215 const_pointer_cast<Data>(data.shared_from_this())->setIncomingFaceId(FACEID_CONTENT_STORE);
216 // XXX should we lookup PIT for other Interests that also match csMatch?
217
218 // set PIT straggler timer
219 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
220
221 // goto outgoing Data pipeline
222 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
223}
224
Junxiao Shif3c07812014-03-11 21:48:49 -0700225/** \brief compare two InRecords for picking outgoing Interest
226 * \return true if b is preferred over a
227 *
228 * This function should be passed to std::max_element over InRecordCollection.
229 * The outgoing Interest picked is the last incoming Interest
230 * that does not come from outFace.
231 * If all InRecords come from outFace, it's fine to pick that. This happens when
232 * there's only one InRecord that comes from outFace. The legit use is for
233 * vehicular network; otherwise, strategy shouldn't send to the sole inFace.
234 */
235static inline bool
236compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace)
237{
238 bool isOutFaceA = a.getFace().get() == outFace;
239 bool isOutFaceB = b.getFace().get() == outFace;
240
241 if (!isOutFaceA && isOutFaceB) {
242 return false;
243 }
244 if (isOutFaceA && !isOutFaceB) {
245 return true;
246 }
247
248 return a.getLastRenewed() > b.getLastRenewed();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700249}
250
251void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700252Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
253 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700254{
Junxiao Shi223271b2014-07-03 22:06:13 -0700255 if (outFace.getId() == INVALID_FACEID) {
256 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
257 return;
258 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700259 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
260 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700261
Junxiao Shi57f0f312014-03-16 11:52:20 -0700262 // scope control
263 if (pitEntry->violatesScope(outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700264 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700265 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700266 return;
267 }
268
Junxiao Shid3c792f2014-01-30 00:46:13 -0700269 // pick Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700270 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
271 pit::InRecordCollection::const_iterator pickedInRecord = std::max_element(
272 inRecords.begin(), inRecords.end(), bind(&compare_pickInterest, _1, _2, &outFace));
273 BOOST_ASSERT(pickedInRecord != inRecords.end());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700274 shared_ptr<Interest> interest = const_pointer_cast<Interest>(
275 pickedInRecord->getInterest().shared_from_this());
276
277 if (wantNewNonce) {
278 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700279 static boost::random::uniform_int_distribution<uint32_t> dist;
280 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700281 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700282
Junxiao Shid3c792f2014-01-30 00:46:13 -0700283 // insert OutRecord
Junxiao Shid938a6b2014-05-11 23:40:29 -0700284 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700285
Junxiao Shid3c792f2014-01-30 00:46:13 -0700286 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700287 outFace.sendInterest(*interest);
Junxiao Shi33152f12014-07-16 19:54:32 -0700288 ++m_counters.getNOutInterests();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700289}
290
291void
Junxiao Shi09498f02014-02-26 19:41:08 -0700292Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700293{
Junxiao Shid938a6b2014-05-11 23:40:29 -0700294 if (pitEntry->hasUnexpiredOutRecords()) {
295 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
296 " cannot reject forwarded Interest");
297 return;
298 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700299 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700300
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700301 // cancel unsatisfy & straggler timer
302 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
303
Junxiao Shid3c792f2014-01-30 00:46:13 -0700304 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700305 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700306}
307
308void
309Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
310{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700311 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
312
Junxiao Shid3c792f2014-01-30 00:46:13 -0700313 // invoke PIT unsatisfied callback
Junxiao Shif3c07812014-03-11 21:48:49 -0700314 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700315 pitEntry));
Junxiao Shic041ca32014-02-25 20:01:15 -0700316
Junxiao Shia110f262014-10-12 12:35:20 -0700317 // goto Interest Finalize pipeline
318 this->onInterestFinalize(pitEntry, false);
319}
320
321void
322Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
323 const time::milliseconds& dataFreshnessPeriod)
324{
325 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
326 (isSatisfied ? " satisfied" : " unsatisfied"));
327
328 // Dead Nonce List insert if necessary
329 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
330
Junxiao Shif3c07812014-03-11 21:48:49 -0700331 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700332 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600333 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700334}
335
336void
337Forwarder::onIncomingData(Face& inFace, const Data& data)
338{
339 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700340 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -0700341 const_cast<Data&>(data).setIncomingFaceId(inFace.getId());
Junxiao Shi33152f12014-07-16 19:54:32 -0700342 ++m_counters.getNInDatas();
Junxiao Shic041ca32014-02-25 20:01:15 -0700343
Junxiao Shi88884492014-02-15 15:57:43 -0700344 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -0700345 bool isViolatingLocalhost = !inFace.isLocal() &&
346 LOCALHOST_NAME.isPrefixOf(data.getName());
347 if (isViolatingLocalhost) {
348 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
349 " data=" << data.getName() << " violates /localhost");
350 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700351 return;
352 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700353
Junxiao Shid3c792f2014-01-30 00:46:13 -0700354 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700355 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
356 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700357 // goto Data unsolicited pipeline
358 this->onDataUnsolicited(inFace, data);
359 return;
360 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700361
Junxiao Shid3c792f2014-01-30 00:46:13 -0700362 // CS insert
363 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700364
Junxiao Shid3c792f2014-01-30 00:46:13 -0700365 std::set<shared_ptr<Face> > pendingDownstreams;
366 // foreach PitEntry
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700367 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700368 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700369
Junxiao Shid3c792f2014-01-30 00:46:13 -0700370 // cancel unsatisfy & straggler timer
371 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700372
Junxiao Shid3c792f2014-01-30 00:46:13 -0700373 // remember pending downstreams
374 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
375 for (pit::InRecordCollection::const_iterator it = inRecords.begin();
376 it != inRecords.end(); ++it) {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700377 if (it->getExpiry() > time::steady_clock::now()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700378 pendingDownstreams.insert(it->getFace());
379 }
380 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700381
Junxiao Shid938a6b2014-05-11 23:40:29 -0700382 // invoke PIT satisfy callback
Junxiao Shi82e7f582014-09-07 15:15:40 -0700383 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700384 pitEntry, cref(inFace), cref(data)));
385
Junxiao Shia110f262014-10-12 12:35:20 -0700386 // Dead Nonce List insert if necessary (for OutRecord of inFace)
387 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
388
Junxiao Shid3c792f2014-01-30 00:46:13 -0700389 // mark PIT satisfied
390 pitEntry->deleteInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700391 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700392
Junxiao Shid3c792f2014-01-30 00:46:13 -0700393 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700394 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700395 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700396
Junxiao Shid3c792f2014-01-30 00:46:13 -0700397 // foreach pending downstream
398 for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin();
399 it != pendingDownstreams.end(); ++it) {
Junxiao Shida006f52014-05-16 11:18:00 -0700400 shared_ptr<Face> pendingDownstream = *it;
401 if (pendingDownstream.get() == &inFace) {
402 continue;
403 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700404 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700405 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700406 }
407}
408
409void
410Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
411{
412 // accept to cache?
Junxiao Shif3c07812014-03-11 21:48:49 -0700413 bool acceptToCache = inFace.isLocal();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700414 if (acceptToCache) {
415 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700416 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700417 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700418
Junxiao Shif3c07812014-03-11 21:48:49 -0700419 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
420 " data=" << data.getName() <<
421 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700422}
423
424void
425Forwarder::onOutgoingData(const Data& data, Face& outFace)
426{
Junxiao Shi223271b2014-07-03 22:06:13 -0700427 if (outFace.getId() == INVALID_FACEID) {
428 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
429 return;
430 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700431 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
432
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700433 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -0700434 bool isViolatingLocalhost = !outFace.isLocal() &&
435 LOCALHOST_NAME.isPrefixOf(data.getName());
436 if (isViolatingLocalhost) {
437 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
438 " data=" << data.getName() << " violates /localhost");
439 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700440 return;
441 }
442
Junxiao Shif3c07812014-03-11 21:48:49 -0700443 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700444
Junxiao Shid3c792f2014-01-30 00:46:13 -0700445 // send Data
446 outFace.sendData(data);
Junxiao Shi33152f12014-07-16 19:54:32 -0700447 ++m_counters.getNOutDatas();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700448}
449
450static inline bool
451compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
452{
453 return a.getExpiry() < b.getExpiry();
454}
455
456void
457Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
458{
459 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
460 pit::InRecordCollection::const_iterator lastExpiring =
461 std::max_element(inRecords.begin(), inRecords.end(),
462 &compare_InRecord_expiry);
463
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700464 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
465 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700466 if (lastExpiryFromNow <= time::seconds(0)) {
467 // TODO all InRecords are already expired; will this happen?
468 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700469
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700470 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700471 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700472 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
473}
474
475void
Junxiao Shia110f262014-10-12 12:35:20 -0700476Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
477 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700478{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700479 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700480
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700481 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700482 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700483 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700484}
485
486void
487Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
488{
Junxiao Shic041ca32014-02-25 20:01:15 -0700489 scheduler::cancel(pitEntry->m_unsatisfyTimer);
490 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700491}
492
Junxiao Shia110f262014-10-12 12:35:20 -0700493static inline void
494insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
495 const pit::OutRecord& outRecord)
496{
497 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
498}
499
500void
501Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
502 const time::milliseconds& dataFreshnessPeriod,
503 Face* upstream)
504{
505 // need Dead Nonce List insert?
506 bool needDnl = false;
507 if (isSatisfied) {
508 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
509 // Data never becomes stale if it doesn't have FreshnessPeriod field
510 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
511 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
512 }
513 else {
514 needDnl = true;
515 }
516
517 if (!needDnl) {
518 return;
519 }
520
521 // Dead Nonce List insert
522 if (upstream == 0) {
523 // insert all outgoing Nonces
524 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
525 std::for_each(outRecords.begin(), outRecords.end(),
526 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
527 }
528 else {
529 // insert outgoing Nonce of a specific face
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700530 pit::OutRecordCollection::const_iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700531 if (outRecord != pitEntry.getOutRecords().end()) {
532 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
533 }
534 }
535}
536
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800537} // namespace nfd