blob: 1c8d53e7506eeecc0f1b193725395c2a64c8dfc9 [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"
Yuanzhi Gaob13fb572015-05-04 12:56:48 -070031
32#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 Afanasyev44016152015-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 Afanasyev44016152015-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) {
Spyridon Mastorakis9d9c9ae2014-12-05 22:43:34 -0800100 if (m_csFromNdnSim == nullptr) {
101 m_cs.find(interest,
102 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
103 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
104 }
105 else {
106 shared_ptr<Data> match = m_csFromNdnSim->Lookup(interest.shared_from_this());
107 if (match != nullptr) {
108 this->onContentStoreHit(inFace, pitEntry, interest, *match);
109 }
110 else {
111 this->onContentStoreMiss(inFace, pitEntry, interest);
112 }
113 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700114 }
mzhang4eab72492015-02-25 11:16:09 -0600115 else {
116 this->onContentStoreMiss(inFace, pitEntry, interest);
117 }
118}
Junxiao Shic041ca32014-02-25 20:01:15 -0700119
mzhang4eab72492015-02-25 11:16:09 -0600120void
121Forwarder::onContentStoreMiss(const Face& inFace,
122 shared_ptr<pit::Entry> pitEntry,
123 const Interest& interest)
124{
125 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
126
127 shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700128 // insert InRecord
mzhang4eab72492015-02-25 11:16:09 -0600129 pitEntry->insertOrUpdateInRecord(face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700130
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700131 // set PIT unsatisfy timer
132 this->setUnsatisfyTimer(pitEntry);
133
Junxiao Shid3c792f2014-01-30 00:46:13 -0700134 // FIB lookup
Junxiao Shi40631842014-03-01 13:52:37 -0700135 shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700136
Junxiao Shid3c792f2014-01-30 00:46:13 -0700137 // dispatch to strategy
Junxiao Shif3c07812014-03-11 21:48:49 -0700138 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700139 cref(inFace), cref(interest), fibEntry, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700140}
141
142void
mzhang4eab72492015-02-25 11:16:09 -0600143Forwarder::onContentStoreHit(const Face& inFace,
144 shared_ptr<pit::Entry> pitEntry,
145 const Interest& interest,
146 const Data& data)
147{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500148 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600149
Alexander Afanasyev44016152015-01-08 21:41:48 -0800150 beforeSatisfyInterest(*pitEntry, *m_csFace, data);
151 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
152 pitEntry, cref(*m_csFace), cref(data)));
153
mzhang4eab72492015-02-25 11:16:09 -0600154 const_pointer_cast<Data>(data.shared_from_this())->setIncomingFaceId(FACEID_CONTENT_STORE);
155 // XXX should we lookup PIT for other Interests that also match csMatch?
156
157 // set PIT straggler timer
158 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
159
160 // goto outgoing Data pipeline
161 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
162}
163
164void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700165Forwarder::onInterestLoop(Face& inFace, const Interest& interest,
166 shared_ptr<pit::Entry> pitEntry)
167{
Junxiao Shif3c07812014-03-11 21:48:49 -0700168 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
169 " interest=" << interest.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700170
Junxiao Shif3c07812014-03-11 21:48:49 -0700171 // (drop)
172}
173
174/** \brief compare two InRecords for picking outgoing Interest
175 * \return true if b is preferred over a
176 *
177 * This function should be passed to std::max_element over InRecordCollection.
178 * The outgoing Interest picked is the last incoming Interest
179 * that does not come from outFace.
180 * If all InRecords come from outFace, it's fine to pick that. This happens when
181 * there's only one InRecord that comes from outFace. The legit use is for
182 * vehicular network; otherwise, strategy shouldn't send to the sole inFace.
183 */
184static inline bool
185compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace)
186{
187 bool isOutFaceA = a.getFace().get() == outFace;
188 bool isOutFaceB = b.getFace().get() == outFace;
189
190 if (!isOutFaceA && isOutFaceB) {
191 return false;
192 }
193 if (isOutFaceA && !isOutFaceB) {
194 return true;
195 }
196
197 return a.getLastRenewed() > b.getLastRenewed();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700198}
199
200void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700201Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
202 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700203{
Junxiao Shi223271b2014-07-03 22:06:13 -0700204 if (outFace.getId() == INVALID_FACEID) {
205 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
206 return;
207 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700208 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
209 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700210
Junxiao Shi57f0f312014-03-16 11:52:20 -0700211 // scope control
212 if (pitEntry->violatesScope(outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700213 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700214 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700215 return;
216 }
217
Junxiao Shid3c792f2014-01-30 00:46:13 -0700218 // pick Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700219 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
220 pit::InRecordCollection::const_iterator pickedInRecord = std::max_element(
221 inRecords.begin(), inRecords.end(), bind(&compare_pickInterest, _1, _2, &outFace));
222 BOOST_ASSERT(pickedInRecord != inRecords.end());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700223 shared_ptr<Interest> interest = const_pointer_cast<Interest>(
224 pickedInRecord->getInterest().shared_from_this());
225
226 if (wantNewNonce) {
227 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700228 static boost::random::uniform_int_distribution<uint32_t> dist;
229 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700230 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700231
Junxiao Shid3c792f2014-01-30 00:46:13 -0700232 // insert OutRecord
Junxiao Shid938a6b2014-05-11 23:40:29 -0700233 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700234
Junxiao Shid3c792f2014-01-30 00:46:13 -0700235 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700236 outFace.sendInterest(*interest);
Junxiao Shi33152f12014-07-16 19:54:32 -0700237 ++m_counters.getNOutInterests();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700238}
239
240void
Junxiao Shi09498f02014-02-26 19:41:08 -0700241Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700242{
Junxiao Shid938a6b2014-05-11 23:40:29 -0700243 if (pitEntry->hasUnexpiredOutRecords()) {
244 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
245 " cannot reject forwarded Interest");
246 return;
247 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700248 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700249
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700250 // cancel unsatisfy & straggler timer
251 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
252
Junxiao Shid3c792f2014-01-30 00:46:13 -0700253 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700254 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700255}
256
257void
258Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
259{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700260 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
261
Junxiao Shid3c792f2014-01-30 00:46:13 -0700262 // invoke PIT unsatisfied callback
Alexander Afanasyev44016152015-01-08 21:41:48 -0800263 beforeExpirePendingInterest(*pitEntry);
Junxiao Shif3c07812014-03-11 21:48:49 -0700264 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700265 pitEntry));
Junxiao Shic041ca32014-02-25 20:01:15 -0700266
Junxiao Shia110f262014-10-12 12:35:20 -0700267 // goto Interest Finalize pipeline
268 this->onInterestFinalize(pitEntry, false);
269}
270
271void
272Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
273 const time::milliseconds& dataFreshnessPeriod)
274{
275 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
276 (isSatisfied ? " satisfied" : " unsatisfied"));
277
278 // Dead Nonce List insert if necessary
279 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
280
Junxiao Shif3c07812014-03-11 21:48:49 -0700281 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700282 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600283 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700284}
285
286void
287Forwarder::onIncomingData(Face& inFace, const Data& data)
288{
289 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700290 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -0700291 const_cast<Data&>(data).setIncomingFaceId(inFace.getId());
Junxiao Shi33152f12014-07-16 19:54:32 -0700292 ++m_counters.getNInDatas();
Junxiao Shic041ca32014-02-25 20:01:15 -0700293
Junxiao Shi88884492014-02-15 15:57:43 -0700294 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -0700295 bool isViolatingLocalhost = !inFace.isLocal() &&
296 LOCALHOST_NAME.isPrefixOf(data.getName());
297 if (isViolatingLocalhost) {
298 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
299 " data=" << data.getName() << " violates /localhost");
300 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700301 return;
302 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700303
Junxiao Shid3c792f2014-01-30 00:46:13 -0700304 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700305 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
306 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700307 // goto Data unsolicited pipeline
308 this->onDataUnsolicited(inFace, data);
309 return;
310 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700311
Yuanzhi Gaob13fb572015-05-04 12:56:48 -0700312 // Remove Ptr<Packet> from the Data before inserting into cache, serving two purposes
313 // - reduce amount of memory used by cached entries
314 // - remove all tags that (e.g., hop count tag) that could have been associated with Ptr<Packet>
315 //
316 // Copying of Data is relatively cheap operation, as it copies (mostly) a collection of Blocks
317 // pointing to the same underlying memory buffer.
318 shared_ptr<Data> dataCopyWithoutPacket = make_shared<Data>(data);
319 dataCopyWithoutPacket->removeTag<ns3::ndn::Ns3PacketTag>();
320
Junxiao Shid3c792f2014-01-30 00:46:13 -0700321 // CS insert
Spyridon Mastorakis9d9c9ae2014-12-05 22:43:34 -0800322 if (m_csFromNdnSim == nullptr)
Yuanzhi Gaob13fb572015-05-04 12:56:48 -0700323 m_cs.insert(*dataCopyWithoutPacket);
Spyridon Mastorakis9d9c9ae2014-12-05 22:43:34 -0800324 else
Yuanzhi Gaob13fb572015-05-04 12:56:48 -0700325 m_csFromNdnSim->Add(dataCopyWithoutPacket);
Junxiao Shic041ca32014-02-25 20:01:15 -0700326
Junxiao Shid3c792f2014-01-30 00:46:13 -0700327 std::set<shared_ptr<Face> > pendingDownstreams;
328 // foreach PitEntry
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700329 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700330 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700331
Junxiao Shid3c792f2014-01-30 00:46:13 -0700332 // cancel unsatisfy & straggler timer
333 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700334
Junxiao Shid3c792f2014-01-30 00:46:13 -0700335 // remember pending downstreams
336 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
337 for (pit::InRecordCollection::const_iterator it = inRecords.begin();
338 it != inRecords.end(); ++it) {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700339 if (it->getExpiry() > time::steady_clock::now()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700340 pendingDownstreams.insert(it->getFace());
341 }
342 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700343
Junxiao Shid938a6b2014-05-11 23:40:29 -0700344 // invoke PIT satisfy callback
Alexander Afanasyev44016152015-01-08 21:41:48 -0800345 beforeSatisfyInterest(*pitEntry, inFace, data);
Junxiao Shi82e7f582014-09-07 15:15:40 -0700346 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700347 pitEntry, cref(inFace), cref(data)));
348
Junxiao Shia110f262014-10-12 12:35:20 -0700349 // Dead Nonce List insert if necessary (for OutRecord of inFace)
350 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
351
Junxiao Shid3c792f2014-01-30 00:46:13 -0700352 // mark PIT satisfied
353 pitEntry->deleteInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700354 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700355
Junxiao Shid3c792f2014-01-30 00:46:13 -0700356 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700357 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700358 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700359
Junxiao Shid3c792f2014-01-30 00:46:13 -0700360 // foreach pending downstream
361 for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin();
362 it != pendingDownstreams.end(); ++it) {
Junxiao Shida006f52014-05-16 11:18:00 -0700363 shared_ptr<Face> pendingDownstream = *it;
364 if (pendingDownstream.get() == &inFace) {
365 continue;
366 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700367 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700368 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700369 }
370}
371
372void
373Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
374{
375 // accept to cache?
Junxiao Shif3c07812014-03-11 21:48:49 -0700376 bool acceptToCache = inFace.isLocal();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700377 if (acceptToCache) {
378 // CS insert
Spyridon Mastorakis9d9c9ae2014-12-05 22:43:34 -0800379 if (m_csFromNdnSim == nullptr)
380 m_cs.insert(data, true);
381 else
382 m_csFromNdnSim->Add(data.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700383 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700384
Junxiao Shif3c07812014-03-11 21:48:49 -0700385 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
386 " data=" << data.getName() <<
387 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700388}
389
390void
391Forwarder::onOutgoingData(const Data& data, Face& outFace)
392{
Junxiao Shi223271b2014-07-03 22:06:13 -0700393 if (outFace.getId() == INVALID_FACEID) {
394 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
395 return;
396 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700397 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
398
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700399 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -0700400 bool isViolatingLocalhost = !outFace.isLocal() &&
401 LOCALHOST_NAME.isPrefixOf(data.getName());
402 if (isViolatingLocalhost) {
403 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
404 " data=" << data.getName() << " violates /localhost");
405 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700406 return;
407 }
408
Junxiao Shif3c07812014-03-11 21:48:49 -0700409 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700410
Junxiao Shid3c792f2014-01-30 00:46:13 -0700411 // send Data
412 outFace.sendData(data);
Junxiao Shi33152f12014-07-16 19:54:32 -0700413 ++m_counters.getNOutDatas();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700414}
415
416static inline bool
417compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
418{
419 return a.getExpiry() < b.getExpiry();
420}
421
422void
423Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
424{
425 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
426 pit::InRecordCollection::const_iterator lastExpiring =
427 std::max_element(inRecords.begin(), inRecords.end(),
428 &compare_InRecord_expiry);
429
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700430 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
431 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700432 if (lastExpiryFromNow <= time::seconds(0)) {
433 // TODO all InRecords are already expired; will this happen?
434 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700435
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700436 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700437 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700438 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
439}
440
441void
Junxiao Shia110f262014-10-12 12:35:20 -0700442Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
443 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700444{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700445 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700446
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700447 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700448 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700449 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700450}
451
452void
453Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
454{
Junxiao Shic041ca32014-02-25 20:01:15 -0700455 scheduler::cancel(pitEntry->m_unsatisfyTimer);
456 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700457}
458
Junxiao Shia110f262014-10-12 12:35:20 -0700459static inline void
460insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
461 const pit::OutRecord& outRecord)
462{
463 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
464}
465
466void
467Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
468 const time::milliseconds& dataFreshnessPeriod,
469 Face* upstream)
470{
471 // need Dead Nonce List insert?
472 bool needDnl = false;
473 if (isSatisfied) {
474 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
475 // Data never becomes stale if it doesn't have FreshnessPeriod field
476 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
477 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
478 }
479 else {
480 needDnl = true;
481 }
482
483 if (!needDnl) {
484 return;
485 }
486
487 // Dead Nonce List insert
488 if (upstream == 0) {
489 // insert all outgoing Nonces
490 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
491 std::for_each(outRecords.begin(), outRecords.end(),
492 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
493 }
494 else {
495 // insert outgoing Nonce of a specific face
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700496 pit::OutRecordCollection::const_iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700497 if (outRecord != pitEntry.getOutRecords().end()) {
498 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
499 }
500 }
501}
502
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800503} // namespace nfd