blob: bf1cb39c3638fe88f5e994fae8268eddc8d0511c [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"
Alexander Afanasyev44016152015-01-08 21:41:48 -080030#include "face/null-face.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 Shif3c07812014-03-11 21:48:49 -070039const Name Forwarder::LOCALHOST_NAME("ndn:/localhost");
Junxiao Shi88884492014-02-15 15:57:43 -070040
Junxiao Shic041ca32014-02-25 20:01:15 -070041Forwarder::Forwarder()
Junxiao Shia4f2be82014-03-02 22:56:41 -070042 : m_faceTable(*this)
HangZhangad4afd12014-03-01 11:03:08 +080043 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060044 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080045 , m_measurements(m_nameTree)
Junxiao Shif3c07812014-03-11 21:48:49 -070046 , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this))
Alexander Afanasyev44016152015-01-08 21:41:48 -080047 , m_csFace(make_shared<NullFace>(FaceUri("contentstore://")))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080048{
Junxiao Shif3c07812014-03-11 21:48:49 -070049 fw::installStrategies(*this);
Alexander Afanasyev44016152015-01-08 21:41:48 -080050 getFaceTable().addReserved(m_csFace, FACEID_CONTENT_STORE);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080051}
52
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060053Forwarder::~Forwarder()
54{
55
56}
57
Alexander Afanasyev33b72772014-01-26 23:22:58 -080058void
Junxiao Shid3c792f2014-01-30 00:46:13 -070059Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
60{
61 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -070062 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
63 " interest=" << interest.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -070064 const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId());
Junxiao Shi33152f12014-07-16 19:54:32 -070065 ++m_counters.getNInInterests();
Junxiao Shic041ca32014-02-25 20:01:15 -070066
Junxiao Shi88884492014-02-15 15:57:43 -070067 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -070068 bool isViolatingLocalhost = !inFace.isLocal() &&
69 LOCALHOST_NAME.isPrefixOf(interest.getName());
70 if (isViolatingLocalhost) {
71 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
72 " interest=" << interest.getName() << " violates /localhost");
73 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -070074 return;
75 }
Junxiao Shic041ca32014-02-25 20:01:15 -070076
Junxiao Shid3c792f2014-01-30 00:46:13 -070077 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -070078 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -070079
Junxiao Shia110f262014-10-12 12:35:20 -070080 // detect duplicate Nonce
81 int dnw = pitEntry->findNonce(interest.getNonce(), inFace);
82 bool hasDuplicateNonce = (dnw != pit::DUPLICATE_NONCE_NONE) ||
83 m_deadNonceList.has(interest.getName(), interest.getNonce());
84 if (hasDuplicateNonce) {
Junxiao Shid3c792f2014-01-30 00:46:13 -070085 // goto Interest loop pipeline
86 this->onInterestLoop(inFace, interest, pitEntry);
87 return;
88 }
Junxiao Shic041ca32014-02-25 20:01:15 -070089
Junxiao Shid3c792f2014-01-30 00:46:13 -070090 // cancel unsatisfy & straggler timer
91 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -070092
Junxiao Shif3c07812014-03-11 21:48:49 -070093 // is pending?
Junxiao Shid3c792f2014-01-30 00:46:13 -070094 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shie17349a2014-03-25 00:55:38 -070095 bool isPending = inRecords.begin() != inRecords.end();
Junxiao Shid3c792f2014-01-30 00:46:13 -070096 if (!isPending) {
Spyridon Mastorakis9d9c9ae2014-12-05 22:43:34 -080097 if (m_csFromNdnSim == nullptr) {
98 m_cs.find(interest,
99 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
100 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
101 }
102 else {
103 shared_ptr<Data> match = m_csFromNdnSim->Lookup(interest.shared_from_this());
104 if (match != nullptr) {
105 this->onContentStoreHit(inFace, pitEntry, interest, *match);
106 }
107 else {
108 this->onContentStoreMiss(inFace, pitEntry, interest);
109 }
110 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700111 }
mzhang4eab72492015-02-25 11:16:09 -0600112 else {
113 this->onContentStoreMiss(inFace, pitEntry, interest);
114 }
115}
Junxiao Shic041ca32014-02-25 20:01:15 -0700116
mzhang4eab72492015-02-25 11:16:09 -0600117void
118Forwarder::onContentStoreMiss(const Face& inFace,
119 shared_ptr<pit::Entry> pitEntry,
120 const Interest& interest)
121{
122 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
123
124 shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700125 // insert InRecord
mzhang4eab72492015-02-25 11:16:09 -0600126 pitEntry->insertOrUpdateInRecord(face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700127
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700128 // set PIT unsatisfy timer
129 this->setUnsatisfyTimer(pitEntry);
130
Junxiao Shid3c792f2014-01-30 00:46:13 -0700131 // FIB lookup
Junxiao Shi40631842014-03-01 13:52:37 -0700132 shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700133
Junxiao Shid3c792f2014-01-30 00:46:13 -0700134 // dispatch to strategy
Junxiao Shif3c07812014-03-11 21:48:49 -0700135 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700136 cref(inFace), cref(interest), fibEntry, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700137}
138
139void
mzhang4eab72492015-02-25 11:16:09 -0600140Forwarder::onContentStoreHit(const Face& inFace,
141 shared_ptr<pit::Entry> pitEntry,
142 const Interest& interest,
143 const Data& data)
144{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500145 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600146
Alexander Afanasyev44016152015-01-08 21:41:48 -0800147 beforeSatisfyInterest(*pitEntry, *m_csFace, data);
148 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
149 pitEntry, cref(*m_csFace), cref(data)));
150
mzhang4eab72492015-02-25 11:16:09 -0600151 const_pointer_cast<Data>(data.shared_from_this())->setIncomingFaceId(FACEID_CONTENT_STORE);
152 // XXX should we lookup PIT for other Interests that also match csMatch?
153
154 // set PIT straggler timer
155 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
156
157 // goto outgoing Data pipeline
158 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
159}
160
161void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700162Forwarder::onInterestLoop(Face& inFace, const Interest& interest,
163 shared_ptr<pit::Entry> pitEntry)
164{
Junxiao Shif3c07812014-03-11 21:48:49 -0700165 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
166 " interest=" << interest.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700167
Junxiao Shif3c07812014-03-11 21:48:49 -0700168 // (drop)
169}
170
171/** \brief compare two InRecords for picking outgoing Interest
172 * \return true if b is preferred over a
173 *
174 * This function should be passed to std::max_element over InRecordCollection.
175 * The outgoing Interest picked is the last incoming Interest
176 * that does not come from outFace.
177 * If all InRecords come from outFace, it's fine to pick that. This happens when
178 * there's only one InRecord that comes from outFace. The legit use is for
179 * vehicular network; otherwise, strategy shouldn't send to the sole inFace.
180 */
181static inline bool
182compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace)
183{
184 bool isOutFaceA = a.getFace().get() == outFace;
185 bool isOutFaceB = b.getFace().get() == outFace;
186
187 if (!isOutFaceA && isOutFaceB) {
188 return false;
189 }
190 if (isOutFaceA && !isOutFaceB) {
191 return true;
192 }
193
194 return a.getLastRenewed() > b.getLastRenewed();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700195}
196
197void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700198Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
199 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700200{
Junxiao Shi223271b2014-07-03 22:06:13 -0700201 if (outFace.getId() == INVALID_FACEID) {
202 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
203 return;
204 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700205 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
206 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700207
Junxiao Shi57f0f312014-03-16 11:52:20 -0700208 // scope control
209 if (pitEntry->violatesScope(outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700210 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700211 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700212 return;
213 }
214
Junxiao Shid3c792f2014-01-30 00:46:13 -0700215 // pick Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700216 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
217 pit::InRecordCollection::const_iterator pickedInRecord = std::max_element(
218 inRecords.begin(), inRecords.end(), bind(&compare_pickInterest, _1, _2, &outFace));
219 BOOST_ASSERT(pickedInRecord != inRecords.end());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700220 shared_ptr<Interest> interest = const_pointer_cast<Interest>(
221 pickedInRecord->getInterest().shared_from_this());
222
223 if (wantNewNonce) {
224 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700225 static boost::random::uniform_int_distribution<uint32_t> dist;
226 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700227 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700228
Junxiao Shid3c792f2014-01-30 00:46:13 -0700229 // insert OutRecord
Junxiao Shid938a6b2014-05-11 23:40:29 -0700230 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700231
Junxiao Shid3c792f2014-01-30 00:46:13 -0700232 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700233 outFace.sendInterest(*interest);
Junxiao Shi33152f12014-07-16 19:54:32 -0700234 ++m_counters.getNOutInterests();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700235}
236
237void
Junxiao Shi09498f02014-02-26 19:41:08 -0700238Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700239{
Junxiao Shid938a6b2014-05-11 23:40:29 -0700240 if (pitEntry->hasUnexpiredOutRecords()) {
241 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
242 " cannot reject forwarded Interest");
243 return;
244 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700245 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700246
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700247 // cancel unsatisfy & straggler timer
248 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
249
Junxiao Shid3c792f2014-01-30 00:46:13 -0700250 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700251 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700252}
253
254void
255Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
256{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700257 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
258
Junxiao Shid3c792f2014-01-30 00:46:13 -0700259 // invoke PIT unsatisfied callback
Alexander Afanasyev44016152015-01-08 21:41:48 -0800260 beforeExpirePendingInterest(*pitEntry);
Junxiao Shif3c07812014-03-11 21:48:49 -0700261 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700262 pitEntry));
Junxiao Shic041ca32014-02-25 20:01:15 -0700263
Junxiao Shia110f262014-10-12 12:35:20 -0700264 // goto Interest Finalize pipeline
265 this->onInterestFinalize(pitEntry, false);
266}
267
268void
269Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
270 const time::milliseconds& dataFreshnessPeriod)
271{
272 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
273 (isSatisfied ? " satisfied" : " unsatisfied"));
274
275 // Dead Nonce List insert if necessary
276 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
277
Junxiao Shif3c07812014-03-11 21:48:49 -0700278 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700279 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600280 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700281}
282
283void
284Forwarder::onIncomingData(Face& inFace, const Data& data)
285{
286 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700287 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -0700288 const_cast<Data&>(data).setIncomingFaceId(inFace.getId());
Junxiao Shi33152f12014-07-16 19:54:32 -0700289 ++m_counters.getNInDatas();
Junxiao Shic041ca32014-02-25 20:01:15 -0700290
Junxiao Shi88884492014-02-15 15:57:43 -0700291 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -0700292 bool isViolatingLocalhost = !inFace.isLocal() &&
293 LOCALHOST_NAME.isPrefixOf(data.getName());
294 if (isViolatingLocalhost) {
295 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
296 " data=" << data.getName() << " violates /localhost");
297 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700298 return;
299 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700300
Junxiao Shid3c792f2014-01-30 00:46:13 -0700301 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700302 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
303 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700304 // goto Data unsolicited pipeline
305 this->onDataUnsolicited(inFace, data);
306 return;
307 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700308
Junxiao Shid3c792f2014-01-30 00:46:13 -0700309 // CS insert
Spyridon Mastorakis9d9c9ae2014-12-05 22:43:34 -0800310 if (m_csFromNdnSim == nullptr)
311 m_cs.insert(data);
312 else
313 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shic041ca32014-02-25 20:01:15 -0700314
Junxiao Shid3c792f2014-01-30 00:46:13 -0700315 std::set<shared_ptr<Face> > pendingDownstreams;
316 // foreach PitEntry
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700317 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700318 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700319
Junxiao Shid3c792f2014-01-30 00:46:13 -0700320 // cancel unsatisfy & straggler timer
321 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700322
Junxiao Shid3c792f2014-01-30 00:46:13 -0700323 // remember pending downstreams
324 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
325 for (pit::InRecordCollection::const_iterator it = inRecords.begin();
326 it != inRecords.end(); ++it) {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700327 if (it->getExpiry() > time::steady_clock::now()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700328 pendingDownstreams.insert(it->getFace());
329 }
330 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700331
Junxiao Shid938a6b2014-05-11 23:40:29 -0700332 // invoke PIT satisfy callback
Alexander Afanasyev44016152015-01-08 21:41:48 -0800333 beforeSatisfyInterest(*pitEntry, inFace, data);
Junxiao Shi82e7f582014-09-07 15:15:40 -0700334 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700335 pitEntry, cref(inFace), cref(data)));
336
Junxiao Shia110f262014-10-12 12:35:20 -0700337 // Dead Nonce List insert if necessary (for OutRecord of inFace)
338 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
339
Junxiao Shid3c792f2014-01-30 00:46:13 -0700340 // mark PIT satisfied
341 pitEntry->deleteInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700342 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700343
Junxiao Shid3c792f2014-01-30 00:46:13 -0700344 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700345 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700346 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700347
Junxiao Shid3c792f2014-01-30 00:46:13 -0700348 // foreach pending downstream
349 for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin();
350 it != pendingDownstreams.end(); ++it) {
Junxiao Shida006f52014-05-16 11:18:00 -0700351 shared_ptr<Face> pendingDownstream = *it;
352 if (pendingDownstream.get() == &inFace) {
353 continue;
354 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700355 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700356 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700357 }
358}
359
360void
361Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
362{
363 // accept to cache?
Junxiao Shif3c07812014-03-11 21:48:49 -0700364 bool acceptToCache = inFace.isLocal();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700365 if (acceptToCache) {
366 // CS insert
Spyridon Mastorakis9d9c9ae2014-12-05 22:43:34 -0800367 if (m_csFromNdnSim == nullptr)
368 m_cs.insert(data, true);
369 else
370 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700371 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700372
Junxiao Shif3c07812014-03-11 21:48:49 -0700373 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
374 " data=" << data.getName() <<
375 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700376}
377
378void
379Forwarder::onOutgoingData(const Data& data, Face& outFace)
380{
Junxiao Shi223271b2014-07-03 22:06:13 -0700381 if (outFace.getId() == INVALID_FACEID) {
382 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
383 return;
384 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700385 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
386
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700387 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -0700388 bool isViolatingLocalhost = !outFace.isLocal() &&
389 LOCALHOST_NAME.isPrefixOf(data.getName());
390 if (isViolatingLocalhost) {
391 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
392 " data=" << data.getName() << " violates /localhost");
393 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700394 return;
395 }
396
Junxiao Shif3c07812014-03-11 21:48:49 -0700397 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700398
Junxiao Shid3c792f2014-01-30 00:46:13 -0700399 // send Data
400 outFace.sendData(data);
Junxiao Shi33152f12014-07-16 19:54:32 -0700401 ++m_counters.getNOutDatas();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700402}
403
404static inline bool
405compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
406{
407 return a.getExpiry() < b.getExpiry();
408}
409
410void
411Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
412{
413 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
414 pit::InRecordCollection::const_iterator lastExpiring =
415 std::max_element(inRecords.begin(), inRecords.end(),
416 &compare_InRecord_expiry);
417
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700418 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
419 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700420 if (lastExpiryFromNow <= time::seconds(0)) {
421 // TODO all InRecords are already expired; will this happen?
422 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700423
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700424 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700425 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700426 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
427}
428
429void
Junxiao Shia110f262014-10-12 12:35:20 -0700430Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
431 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700432{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700433 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700434
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700435 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700436 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700437 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700438}
439
440void
441Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
442{
Junxiao Shic041ca32014-02-25 20:01:15 -0700443 scheduler::cancel(pitEntry->m_unsatisfyTimer);
444 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700445}
446
Junxiao Shia110f262014-10-12 12:35:20 -0700447static inline void
448insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
449 const pit::OutRecord& outRecord)
450{
451 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
452}
453
454void
455Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
456 const time::milliseconds& dataFreshnessPeriod,
457 Face* upstream)
458{
459 // need Dead Nonce List insert?
460 bool needDnl = false;
461 if (isSatisfied) {
462 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
463 // Data never becomes stale if it doesn't have FreshnessPeriod field
464 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
465 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
466 }
467 else {
468 needDnl = true;
469 }
470
471 if (!needDnl) {
472 return;
473 }
474
475 // Dead Nonce List insert
476 if (upstream == 0) {
477 // insert all outgoing Nonces
478 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
479 std::for_each(outRecords.begin(), outRecords.end(),
480 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
481 }
482 else {
483 // insert outgoing Nonce of a specific face
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700484 pit::OutRecordCollection::const_iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700485 if (outRecord != pitEntry.getOutRecords().end()) {
486 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
487 }
488 }
489}
490
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800491} // namespace nfd