blob: 4066e3f7e0e1ade81c78a3a58594bc0c66944336 [file] [log] [blame]
Alexander Afanasyev33b72772014-01-26 23:22:58 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shiaf6569a2014-06-14 00:01:34 -07003 * Copyright (c) 2014, 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 Shid938a6b2014-05-11 23:40:29 -070029#include "available-strategies.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{
52
53}
54
Alexander Afanasyev33b72772014-01-26 23:22:58 -080055void
Junxiao Shid3c792f2014-01-30 00:46:13 -070056Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
57{
58 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -070059 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
60 " interest=" << interest.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -070061 const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId());
Junxiao Shi33152f12014-07-16 19:54:32 -070062 ++m_counters.getNInInterests();
Junxiao Shic041ca32014-02-25 20:01:15 -070063
Junxiao Shi88884492014-02-15 15:57:43 -070064 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -070065 bool isViolatingLocalhost = !inFace.isLocal() &&
66 LOCALHOST_NAME.isPrefixOf(interest.getName());
67 if (isViolatingLocalhost) {
68 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
69 " interest=" << interest.getName() << " violates /localhost");
70 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -070071 return;
72 }
Junxiao Shic041ca32014-02-25 20:01:15 -070073
Junxiao Shid3c792f2014-01-30 00:46:13 -070074 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -070075 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -070076
Junxiao Shia110f262014-10-12 12:35:20 -070077 // detect duplicate Nonce
78 int dnw = pitEntry->findNonce(interest.getNonce(), inFace);
79 bool hasDuplicateNonce = (dnw != pit::DUPLICATE_NONCE_NONE) ||
80 m_deadNonceList.has(interest.getName(), interest.getNonce());
81 if (hasDuplicateNonce) {
Junxiao Shid3c792f2014-01-30 00:46:13 -070082 // goto Interest loop pipeline
83 this->onInterestLoop(inFace, interest, pitEntry);
84 return;
85 }
Junxiao Shic041ca32014-02-25 20:01:15 -070086
Junxiao Shid3c792f2014-01-30 00:46:13 -070087 // cancel unsatisfy & straggler timer
88 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -070089
Junxiao Shif3c07812014-03-11 21:48:49 -070090 // is pending?
Junxiao Shid3c792f2014-01-30 00:46:13 -070091 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shie17349a2014-03-25 00:55:38 -070092 bool isPending = inRecords.begin() != inRecords.end();
Junxiao Shid3c792f2014-01-30 00:46:13 -070093 if (!isPending) {
94 // CS lookup
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -080095 const Data* csMatch;
96 shared_ptr<Data> match;
97 if (m_csFromNdnSim == nullptr)
98 csMatch = m_cs.find(interest);
99 else {
100 match = m_csFromNdnSim->Lookup(interest.shared_from_this());
101 csMatch = match.get();
102 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700103 if (csMatch != 0) {
Junxiao Shi7b984c62014-07-17 22:18:34 -0700104 const_cast<Data*>(csMatch)->setIncomingFaceId(FACEID_CONTENT_STORE);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700105 // XXX should we lookup PIT for other Interests that also match csMatch?
106
Junxiao Shiad3f1cb2014-08-18 11:12:30 -0700107 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700108 this->setStragglerTimer(pitEntry, true, csMatch->getFreshnessPeriod());
Junxiao Shiad3f1cb2014-08-18 11:12:30 -0700109
Junxiao Shid3c792f2014-01-30 00:46:13 -0700110 // goto outgoing Data pipeline
111 this->onOutgoingData(*csMatch, inFace);
112 return;
113 }
114 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700115
Junxiao Shid3c792f2014-01-30 00:46:13 -0700116 // insert InRecord
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700117 pitEntry->insertOrUpdateInRecord(inFace.shared_from_this(), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700118
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700119 // set PIT unsatisfy timer
120 this->setUnsatisfyTimer(pitEntry);
121
Junxiao Shid3c792f2014-01-30 00:46:13 -0700122 // FIB lookup
Junxiao Shi40631842014-03-01 13:52:37 -0700123 shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700124
Junxiao Shid3c792f2014-01-30 00:46:13 -0700125 // dispatch to strategy
Junxiao Shif3c07812014-03-11 21:48:49 -0700126 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700127 cref(inFace), cref(interest), fibEntry, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700128}
129
130void
131Forwarder::onInterestLoop(Face& inFace, const Interest& interest,
132 shared_ptr<pit::Entry> pitEntry)
133{
Junxiao Shif3c07812014-03-11 21:48:49 -0700134 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
135 " interest=" << interest.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700136
Junxiao Shif3c07812014-03-11 21:48:49 -0700137 // (drop)
138}
139
140/** \brief compare two InRecords for picking outgoing Interest
141 * \return true if b is preferred over a
142 *
143 * This function should be passed to std::max_element over InRecordCollection.
144 * The outgoing Interest picked is the last incoming Interest
145 * that does not come from outFace.
146 * If all InRecords come from outFace, it's fine to pick that. This happens when
147 * there's only one InRecord that comes from outFace. The legit use is for
148 * vehicular network; otherwise, strategy shouldn't send to the sole inFace.
149 */
150static inline bool
151compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace)
152{
153 bool isOutFaceA = a.getFace().get() == outFace;
154 bool isOutFaceB = b.getFace().get() == outFace;
155
156 if (!isOutFaceA && isOutFaceB) {
157 return false;
158 }
159 if (isOutFaceA && !isOutFaceB) {
160 return true;
161 }
162
163 return a.getLastRenewed() > b.getLastRenewed();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700164}
165
166void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700167Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
168 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700169{
Junxiao Shi223271b2014-07-03 22:06:13 -0700170 if (outFace.getId() == INVALID_FACEID) {
171 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
172 return;
173 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700174 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
175 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700176
Junxiao Shi57f0f312014-03-16 11:52:20 -0700177 // scope control
178 if (pitEntry->violatesScope(outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700179 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700180 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700181 return;
182 }
183
Junxiao Shid3c792f2014-01-30 00:46:13 -0700184 // pick Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700185 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
186 pit::InRecordCollection::const_iterator pickedInRecord = std::max_element(
187 inRecords.begin(), inRecords.end(), bind(&compare_pickInterest, _1, _2, &outFace));
188 BOOST_ASSERT(pickedInRecord != inRecords.end());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700189 shared_ptr<Interest> interest = const_pointer_cast<Interest>(
190 pickedInRecord->getInterest().shared_from_this());
191
192 if (wantNewNonce) {
193 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700194 static boost::random::uniform_int_distribution<uint32_t> dist;
195 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700196 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700197
Junxiao Shid3c792f2014-01-30 00:46:13 -0700198 // insert OutRecord
Junxiao Shid938a6b2014-05-11 23:40:29 -0700199 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700200
Junxiao Shid3c792f2014-01-30 00:46:13 -0700201 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700202 outFace.sendInterest(*interest);
Junxiao Shi33152f12014-07-16 19:54:32 -0700203 ++m_counters.getNOutInterests();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700204}
205
206void
Junxiao Shi09498f02014-02-26 19:41:08 -0700207Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700208{
Junxiao Shid938a6b2014-05-11 23:40:29 -0700209 if (pitEntry->hasUnexpiredOutRecords()) {
210 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
211 " cannot reject forwarded Interest");
212 return;
213 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700214 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700215
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700216 // cancel unsatisfy & straggler timer
217 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
218
Junxiao Shid3c792f2014-01-30 00:46:13 -0700219 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700220 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700221}
222
223void
224Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
225{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700226 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
227
Junxiao Shid3c792f2014-01-30 00:46:13 -0700228 // invoke PIT unsatisfied callback
Junxiao Shif3c07812014-03-11 21:48:49 -0700229 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700230 pitEntry));
Junxiao Shic041ca32014-02-25 20:01:15 -0700231
Junxiao Shia110f262014-10-12 12:35:20 -0700232 // goto Interest Finalize pipeline
233 this->onInterestFinalize(pitEntry, false);
234}
235
236void
237Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
238 const time::milliseconds& dataFreshnessPeriod)
239{
240 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
241 (isSatisfied ? " satisfied" : " unsatisfied"));
242
243 // Dead Nonce List insert if necessary
244 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
245
Junxiao Shif3c07812014-03-11 21:48:49 -0700246 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700247 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600248 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700249}
250
251void
252Forwarder::onIncomingData(Face& inFace, const Data& data)
253{
254 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700255 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -0700256 const_cast<Data&>(data).setIncomingFaceId(inFace.getId());
Junxiao Shi33152f12014-07-16 19:54:32 -0700257 ++m_counters.getNInDatas();
Junxiao Shic041ca32014-02-25 20:01:15 -0700258
Junxiao Shi88884492014-02-15 15:57:43 -0700259 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -0700260 bool isViolatingLocalhost = !inFace.isLocal() &&
261 LOCALHOST_NAME.isPrefixOf(data.getName());
262 if (isViolatingLocalhost) {
263 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
264 " data=" << data.getName() << " violates /localhost");
265 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700266 return;
267 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700268
Junxiao Shid3c792f2014-01-30 00:46:13 -0700269 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700270 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
271 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700272 // goto Data unsolicited pipeline
273 this->onDataUnsolicited(inFace, data);
274 return;
275 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700276
Junxiao Shid3c792f2014-01-30 00:46:13 -0700277 // CS insert
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800278 if (m_csFromNdnSim == nullptr)
279 m_cs.insert(data);
280 else
281 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shic041ca32014-02-25 20:01:15 -0700282
Junxiao Shid3c792f2014-01-30 00:46:13 -0700283 std::set<shared_ptr<Face> > pendingDownstreams;
284 // foreach PitEntry
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700285 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700286 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700287
Junxiao Shid3c792f2014-01-30 00:46:13 -0700288 // cancel unsatisfy & straggler timer
289 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700290
Junxiao Shid3c792f2014-01-30 00:46:13 -0700291 // remember pending downstreams
292 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
293 for (pit::InRecordCollection::const_iterator it = inRecords.begin();
294 it != inRecords.end(); ++it) {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700295 if (it->getExpiry() > time::steady_clock::now()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700296 pendingDownstreams.insert(it->getFace());
297 }
298 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700299
Junxiao Shid938a6b2014-05-11 23:40:29 -0700300 // invoke PIT satisfy callback
Junxiao Shi82e7f582014-09-07 15:15:40 -0700301 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700302 pitEntry, cref(inFace), cref(data)));
303
Junxiao Shia110f262014-10-12 12:35:20 -0700304 // Dead Nonce List insert if necessary (for OutRecord of inFace)
305 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
306
Junxiao Shid3c792f2014-01-30 00:46:13 -0700307 // mark PIT satisfied
308 pitEntry->deleteInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700309 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700310
Junxiao Shid3c792f2014-01-30 00:46:13 -0700311 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700312 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700313 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700314
Junxiao Shid3c792f2014-01-30 00:46:13 -0700315 // foreach pending downstream
316 for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin();
317 it != pendingDownstreams.end(); ++it) {
Junxiao Shida006f52014-05-16 11:18:00 -0700318 shared_ptr<Face> pendingDownstream = *it;
319 if (pendingDownstream.get() == &inFace) {
320 continue;
321 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700322 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700323 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700324 }
325}
326
327void
328Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
329{
330 // accept to cache?
Junxiao Shif3c07812014-03-11 21:48:49 -0700331 bool acceptToCache = inFace.isLocal();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700332 if (acceptToCache) {
333 // CS insert
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800334 if (m_csFromNdnSim == nullptr)
335 m_cs.insert(data, true);
336 else
337 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700338 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700339
Junxiao Shif3c07812014-03-11 21:48:49 -0700340 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
341 " data=" << data.getName() <<
342 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700343}
344
345void
346Forwarder::onOutgoingData(const Data& data, Face& outFace)
347{
Junxiao Shi223271b2014-07-03 22:06:13 -0700348 if (outFace.getId() == INVALID_FACEID) {
349 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
350 return;
351 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700352 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
353
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700354 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -0700355 bool isViolatingLocalhost = !outFace.isLocal() &&
356 LOCALHOST_NAME.isPrefixOf(data.getName());
357 if (isViolatingLocalhost) {
358 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
359 " data=" << data.getName() << " violates /localhost");
360 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700361 return;
362 }
363
Junxiao Shif3c07812014-03-11 21:48:49 -0700364 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700365
Junxiao Shid3c792f2014-01-30 00:46:13 -0700366 // send Data
367 outFace.sendData(data);
Junxiao Shi33152f12014-07-16 19:54:32 -0700368 ++m_counters.getNOutDatas();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700369}
370
371static inline bool
372compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
373{
374 return a.getExpiry() < b.getExpiry();
375}
376
377void
378Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
379{
380 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
381 pit::InRecordCollection::const_iterator lastExpiring =
382 std::max_element(inRecords.begin(), inRecords.end(),
383 &compare_InRecord_expiry);
384
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700385 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
386 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700387 if (lastExpiryFromNow <= time::seconds(0)) {
388 // TODO all InRecords are already expired; will this happen?
389 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700390
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700391 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700392 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700393 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
394}
395
396void
Junxiao Shia110f262014-10-12 12:35:20 -0700397Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
398 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700399{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700400 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700401
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700402 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700403 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700404 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700405}
406
407void
408Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
409{
Junxiao Shic041ca32014-02-25 20:01:15 -0700410 scheduler::cancel(pitEntry->m_unsatisfyTimer);
411 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700412}
413
Junxiao Shia110f262014-10-12 12:35:20 -0700414static inline void
415insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
416 const pit::OutRecord& outRecord)
417{
418 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
419}
420
421void
422Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
423 const time::milliseconds& dataFreshnessPeriod,
424 Face* upstream)
425{
426 // need Dead Nonce List insert?
427 bool needDnl = false;
428 if (isSatisfied) {
429 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
430 // Data never becomes stale if it doesn't have FreshnessPeriod field
431 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
432 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
433 }
434 else {
435 needDnl = true;
436 }
437
438 if (!needDnl) {
439 return;
440 }
441
442 // Dead Nonce List insert
443 if (upstream == 0) {
444 // insert all outgoing Nonces
445 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
446 std::for_each(outRecords.begin(), outRecords.end(),
447 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
448 }
449 else {
450 // insert outgoing Nonce of a specific face
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700451 pit::OutRecordCollection::const_iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700452 if (outRecord != pitEntry.getOutRecords().end()) {
453 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
454 }
455 }
456}
457
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800458} // namespace nfd