blob: 8b8b11fe7cb8594160864135736b1b236a704c30 [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"
Alexander Afanasyev9bcf39e2015-01-08 21:41:48 -080029#include "face/null-face.hpp"
Junxiao Shid938a6b2014-05-11 23:40:29 -070030#include "available-strategies.hpp"
Alexander Afanasyev9bcf39e2015-01-08 21:41:48 -080031
Yuanzhi Gaob60f8122015-05-04 12:56:48 -070032#include "utils/ndn-ns3-packet-tag.hpp"
33
Junxiao Shiaf6569a2014-06-14 00:01:34 -070034#include <boost/random/uniform_int_distribution.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080035
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080036namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080037
Junxiao Shi8c8d2182014-01-30 22:33:00 -070038NFD_LOG_INIT("Forwarder");
39
Junxiao Shif3c07812014-03-11 21:48:49 -070040using fw::Strategy;
41
Junxiao Shif3c07812014-03-11 21:48:49 -070042const Name Forwarder::LOCALHOST_NAME("ndn:/localhost");
Junxiao Shi88884492014-02-15 15:57:43 -070043
Junxiao Shic041ca32014-02-25 20:01:15 -070044Forwarder::Forwarder()
Junxiao Shia4f2be82014-03-02 22:56:41 -070045 : m_faceTable(*this)
HangZhangad4afd12014-03-01 11:03:08 +080046 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060047 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080048 , m_measurements(m_nameTree)
Junxiao Shif3c07812014-03-11 21:48:49 -070049 , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this))
Alexander Afanasyev9bcf39e2015-01-08 21:41:48 -080050 , m_csFace(make_shared<NullFace>(FaceUri("contentstore://")))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080051{
Junxiao Shif3c07812014-03-11 21:48:49 -070052 fw::installStrategies(*this);
Alexander Afanasyev9bcf39e2015-01-08 21:41:48 -080053 getFaceTable().addReserved(m_csFace, FACEID_CONTENT_STORE);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080054}
55
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060056Forwarder::~Forwarder()
57{
58
59}
60
Alexander Afanasyev33b72772014-01-26 23:22:58 -080061void
Junxiao Shid3c792f2014-01-30 00:46:13 -070062Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
63{
64 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -070065 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
66 " interest=" << interest.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -070067 const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId());
Junxiao Shi33152f12014-07-16 19:54:32 -070068 ++m_counters.getNInInterests();
Junxiao Shic041ca32014-02-25 20:01:15 -070069
Junxiao Shi88884492014-02-15 15:57:43 -070070 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -070071 bool isViolatingLocalhost = !inFace.isLocal() &&
72 LOCALHOST_NAME.isPrefixOf(interest.getName());
73 if (isViolatingLocalhost) {
74 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
75 " interest=" << interest.getName() << " violates /localhost");
76 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -070077 return;
78 }
Junxiao Shic041ca32014-02-25 20:01:15 -070079
Junxiao Shid3c792f2014-01-30 00:46:13 -070080 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -070081 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -070082
Junxiao Shia110f262014-10-12 12:35:20 -070083 // detect duplicate Nonce
84 int dnw = pitEntry->findNonce(interest.getNonce(), inFace);
85 bool hasDuplicateNonce = (dnw != pit::DUPLICATE_NONCE_NONE) ||
86 m_deadNonceList.has(interest.getName(), interest.getNonce());
87 if (hasDuplicateNonce) {
Junxiao Shid3c792f2014-01-30 00:46:13 -070088 // goto Interest loop pipeline
89 this->onInterestLoop(inFace, interest, pitEntry);
90 return;
91 }
Junxiao Shic041ca32014-02-25 20:01:15 -070092
Junxiao Shid3c792f2014-01-30 00:46:13 -070093 // cancel unsatisfy & straggler timer
94 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -070095
Junxiao Shif3c07812014-03-11 21:48:49 -070096 // is pending?
Junxiao Shid3c792f2014-01-30 00:46:13 -070097 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shie17349a2014-03-25 00:55:38 -070098 bool isPending = inRecords.begin() != inRecords.end();
Junxiao Shid3c792f2014-01-30 00:46:13 -070099 if (!isPending) {
100 // CS lookup
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800101 const Data* csMatch;
102 shared_ptr<Data> match;
103 if (m_csFromNdnSim == nullptr)
104 csMatch = m_cs.find(interest);
105 else {
106 match = m_csFromNdnSim->Lookup(interest.shared_from_this());
107 csMatch = match.get();
108 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700109 if (csMatch != 0) {
Junxiao Shi7b984c62014-07-17 22:18:34 -0700110 const_cast<Data*>(csMatch)->setIncomingFaceId(FACEID_CONTENT_STORE);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700111 // XXX should we lookup PIT for other Interests that also match csMatch?
112
Alexander Afanasyev9bcf39e2015-01-08 21:41:48 -0800113 // invoke PIT satisfy callback
114 beforeSatisfyInterest(*pitEntry, *m_csFace, *csMatch);
115 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
116 pitEntry, cref(*m_csFace), cref(*csMatch)));
Junxiao Shiad3f1cb2014-08-18 11:12:30 -0700117 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700118 this->setStragglerTimer(pitEntry, true, csMatch->getFreshnessPeriod());
Junxiao Shiad3f1cb2014-08-18 11:12:30 -0700119
Junxiao Shid3c792f2014-01-30 00:46:13 -0700120 // goto outgoing Data pipeline
121 this->onOutgoingData(*csMatch, inFace);
122 return;
123 }
124 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700125
Junxiao Shid3c792f2014-01-30 00:46:13 -0700126 // insert InRecord
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700127 pitEntry->insertOrUpdateInRecord(inFace.shared_from_this(), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700128
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700129 // set PIT unsatisfy timer
130 this->setUnsatisfyTimer(pitEntry);
131
Junxiao Shid3c792f2014-01-30 00:46:13 -0700132 // FIB lookup
Junxiao Shi40631842014-03-01 13:52:37 -0700133 shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700134
Junxiao Shid3c792f2014-01-30 00:46:13 -0700135 // dispatch to strategy
Junxiao Shif3c07812014-03-11 21:48:49 -0700136 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700137 cref(inFace), cref(interest), fibEntry, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700138}
139
140void
141Forwarder::onInterestLoop(Face& inFace, const Interest& interest,
142 shared_ptr<pit::Entry> pitEntry)
143{
Junxiao Shif3c07812014-03-11 21:48:49 -0700144 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
145 " interest=" << interest.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700146
Junxiao Shif3c07812014-03-11 21:48:49 -0700147 // (drop)
148}
149
150/** \brief compare two InRecords for picking outgoing Interest
151 * \return true if b is preferred over a
152 *
153 * This function should be passed to std::max_element over InRecordCollection.
154 * The outgoing Interest picked is the last incoming Interest
155 * that does not come from outFace.
156 * If all InRecords come from outFace, it's fine to pick that. This happens when
157 * there's only one InRecord that comes from outFace. The legit use is for
158 * vehicular network; otherwise, strategy shouldn't send to the sole inFace.
159 */
160static inline bool
161compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace)
162{
163 bool isOutFaceA = a.getFace().get() == outFace;
164 bool isOutFaceB = b.getFace().get() == outFace;
165
166 if (!isOutFaceA && isOutFaceB) {
167 return false;
168 }
169 if (isOutFaceA && !isOutFaceB) {
170 return true;
171 }
172
173 return a.getLastRenewed() > b.getLastRenewed();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700174}
175
176void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700177Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
178 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700179{
Junxiao Shi223271b2014-07-03 22:06:13 -0700180 if (outFace.getId() == INVALID_FACEID) {
181 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
182 return;
183 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700184 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
185 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700186
Junxiao Shi57f0f312014-03-16 11:52:20 -0700187 // scope control
188 if (pitEntry->violatesScope(outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700189 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700190 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700191 return;
192 }
193
Junxiao Shid3c792f2014-01-30 00:46:13 -0700194 // pick Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700195 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
196 pit::InRecordCollection::const_iterator pickedInRecord = std::max_element(
197 inRecords.begin(), inRecords.end(), bind(&compare_pickInterest, _1, _2, &outFace));
198 BOOST_ASSERT(pickedInRecord != inRecords.end());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700199 shared_ptr<Interest> interest = const_pointer_cast<Interest>(
200 pickedInRecord->getInterest().shared_from_this());
201
202 if (wantNewNonce) {
203 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700204 static boost::random::uniform_int_distribution<uint32_t> dist;
205 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700206 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700207
Junxiao Shid3c792f2014-01-30 00:46:13 -0700208 // insert OutRecord
Junxiao Shid938a6b2014-05-11 23:40:29 -0700209 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700210
Junxiao Shid3c792f2014-01-30 00:46:13 -0700211 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700212 outFace.sendInterest(*interest);
Junxiao Shi33152f12014-07-16 19:54:32 -0700213 ++m_counters.getNOutInterests();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700214}
215
216void
Junxiao Shi09498f02014-02-26 19:41:08 -0700217Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700218{
Junxiao Shid938a6b2014-05-11 23:40:29 -0700219 if (pitEntry->hasUnexpiredOutRecords()) {
220 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
221 " cannot reject forwarded Interest");
222 return;
223 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700224 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700225
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700226 // cancel unsatisfy & straggler timer
227 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
228
Junxiao Shid3c792f2014-01-30 00:46:13 -0700229 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700230 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700231}
232
233void
234Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
235{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700236 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
237
Junxiao Shid3c792f2014-01-30 00:46:13 -0700238 // invoke PIT unsatisfied callback
Alexander Afanasyev9bcf39e2015-01-08 21:41:48 -0800239 beforeExpirePendingInterest(*pitEntry);
Junxiao Shif3c07812014-03-11 21:48:49 -0700240 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700241 pitEntry));
Junxiao Shic041ca32014-02-25 20:01:15 -0700242
Junxiao Shia110f262014-10-12 12:35:20 -0700243 // goto Interest Finalize pipeline
244 this->onInterestFinalize(pitEntry, false);
245}
246
247void
248Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
249 const time::milliseconds& dataFreshnessPeriod)
250{
251 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
252 (isSatisfied ? " satisfied" : " unsatisfied"));
253
254 // Dead Nonce List insert if necessary
255 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
256
Junxiao Shif3c07812014-03-11 21:48:49 -0700257 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700258 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600259 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700260}
261
262void
263Forwarder::onIncomingData(Face& inFace, const Data& data)
264{
265 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700266 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -0700267 const_cast<Data&>(data).setIncomingFaceId(inFace.getId());
Junxiao Shi33152f12014-07-16 19:54:32 -0700268 ++m_counters.getNInDatas();
Junxiao Shic041ca32014-02-25 20:01:15 -0700269
Junxiao Shi88884492014-02-15 15:57:43 -0700270 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -0700271 bool isViolatingLocalhost = !inFace.isLocal() &&
272 LOCALHOST_NAME.isPrefixOf(data.getName());
273 if (isViolatingLocalhost) {
274 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
275 " data=" << data.getName() << " violates /localhost");
276 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700277 return;
278 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700279
Junxiao Shid3c792f2014-01-30 00:46:13 -0700280 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700281 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
282 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700283 // goto Data unsolicited pipeline
284 this->onDataUnsolicited(inFace, data);
285 return;
286 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700287
Yuanzhi Gaob60f8122015-05-04 12:56:48 -0700288 // Remove Ptr<Packet> from the Data before inserting into cache, serving two purposes
289 // - reduce amount of memory used by cached entries
290 // - remove all tags that (e.g., hop count tag) that could have been associated with Ptr<Packet>
291 //
292 // Copying of Data is relatively cheap operation, as it copies (mostly) a collection of Blocks
293 // pointing to the same underlying memory buffer.
294 shared_ptr<Data> dataCopyWithoutPacket = make_shared<Data>(data);
295 dataCopyWithoutPacket->removeTag<ns3::ndn::Ns3PacketTag>();
296
Junxiao Shid3c792f2014-01-30 00:46:13 -0700297 // CS insert
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800298 if (m_csFromNdnSim == nullptr)
Yuanzhi Gaob60f8122015-05-04 12:56:48 -0700299 m_cs.insert(*dataCopyWithoutPacket);
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800300 else
Yuanzhi Gaob60f8122015-05-04 12:56:48 -0700301 m_csFromNdnSim->Add(dataCopyWithoutPacket);
Junxiao Shic041ca32014-02-25 20:01:15 -0700302
Junxiao Shid3c792f2014-01-30 00:46:13 -0700303 std::set<shared_ptr<Face> > pendingDownstreams;
304 // foreach PitEntry
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700305 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700306 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700307
Junxiao Shid3c792f2014-01-30 00:46:13 -0700308 // cancel unsatisfy & straggler timer
309 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700310
Junxiao Shid3c792f2014-01-30 00:46:13 -0700311 // remember pending downstreams
312 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
313 for (pit::InRecordCollection::const_iterator it = inRecords.begin();
314 it != inRecords.end(); ++it) {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700315 if (it->getExpiry() > time::steady_clock::now()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700316 pendingDownstreams.insert(it->getFace());
317 }
318 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700319
Junxiao Shid938a6b2014-05-11 23:40:29 -0700320 // invoke PIT satisfy callback
Alexander Afanasyev9bcf39e2015-01-08 21:41:48 -0800321 beforeSatisfyInterest(*pitEntry, inFace, data);
Junxiao Shi82e7f582014-09-07 15:15:40 -0700322 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700323 pitEntry, cref(inFace), cref(data)));
324
Junxiao Shia110f262014-10-12 12:35:20 -0700325 // Dead Nonce List insert if necessary (for OutRecord of inFace)
326 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
327
Junxiao Shid3c792f2014-01-30 00:46:13 -0700328 // mark PIT satisfied
329 pitEntry->deleteInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700330 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700331
Junxiao Shid3c792f2014-01-30 00:46:13 -0700332 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700333 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700334 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700335
Junxiao Shid3c792f2014-01-30 00:46:13 -0700336 // foreach pending downstream
337 for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin();
338 it != pendingDownstreams.end(); ++it) {
Junxiao Shida006f52014-05-16 11:18:00 -0700339 shared_ptr<Face> pendingDownstream = *it;
340 if (pendingDownstream.get() == &inFace) {
341 continue;
342 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700343 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700344 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700345 }
346}
347
348void
349Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
350{
351 // accept to cache?
Junxiao Shif3c07812014-03-11 21:48:49 -0700352 bool acceptToCache = inFace.isLocal();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700353 if (acceptToCache) {
354 // CS insert
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800355 if (m_csFromNdnSim == nullptr)
356 m_cs.insert(data, true);
357 else
358 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700359 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700360
Junxiao Shif3c07812014-03-11 21:48:49 -0700361 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
362 " data=" << data.getName() <<
363 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700364}
365
366void
367Forwarder::onOutgoingData(const Data& data, Face& outFace)
368{
Junxiao Shi223271b2014-07-03 22:06:13 -0700369 if (outFace.getId() == INVALID_FACEID) {
370 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
371 return;
372 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700373 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
374
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700375 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -0700376 bool isViolatingLocalhost = !outFace.isLocal() &&
377 LOCALHOST_NAME.isPrefixOf(data.getName());
378 if (isViolatingLocalhost) {
379 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
380 " data=" << data.getName() << " violates /localhost");
381 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700382 return;
383 }
384
Junxiao Shif3c07812014-03-11 21:48:49 -0700385 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700386
Junxiao Shid3c792f2014-01-30 00:46:13 -0700387 // send Data
388 outFace.sendData(data);
Junxiao Shi33152f12014-07-16 19:54:32 -0700389 ++m_counters.getNOutDatas();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700390}
391
392static inline bool
393compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
394{
395 return a.getExpiry() < b.getExpiry();
396}
397
398void
399Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
400{
401 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
402 pit::InRecordCollection::const_iterator lastExpiring =
403 std::max_element(inRecords.begin(), inRecords.end(),
404 &compare_InRecord_expiry);
405
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700406 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
407 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700408 if (lastExpiryFromNow <= time::seconds(0)) {
409 // TODO all InRecords are already expired; will this happen?
410 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700411
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700412 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700413 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700414 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
415}
416
417void
Junxiao Shia110f262014-10-12 12:35:20 -0700418Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
419 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700420{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700421 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700422
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700423 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700424 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700425 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700426}
427
428void
429Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
430{
Junxiao Shic041ca32014-02-25 20:01:15 -0700431 scheduler::cancel(pitEntry->m_unsatisfyTimer);
432 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700433}
434
Junxiao Shia110f262014-10-12 12:35:20 -0700435static inline void
436insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
437 const pit::OutRecord& outRecord)
438{
439 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
440}
441
442void
443Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
444 const time::milliseconds& dataFreshnessPeriod,
445 Face* upstream)
446{
447 // need Dead Nonce List insert?
448 bool needDnl = false;
449 if (isSatisfied) {
450 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
451 // Data never becomes stale if it doesn't have FreshnessPeriod field
452 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
453 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
454 }
455 else {
456 needDnl = true;
457 }
458
459 if (!needDnl) {
460 return;
461 }
462
463 // Dead Nonce List insert
464 if (upstream == 0) {
465 // insert all outgoing Nonces
466 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
467 std::for_each(outRecords.begin(), outRecords.end(),
468 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
469 }
470 else {
471 // insert outgoing Nonce of a specific face
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700472 pit::OutRecordCollection::const_iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700473 if (outRecord != pitEntry.getOutRecords().end()) {
474 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
475 }
476 }
477}
478
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800479} // namespace nfd