blob: ae4369e0bb7f0e33eb18070ed883c09b5e5f4ff1 [file] [log] [blame]
Alexander Afanasyev33b72772014-01-26 23:22:58 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi330136a2016-03-10 04:53:08 -07003 * Copyright (c) 2014-2016, Regents of the University of California,
Junxiao Shifaf3eb02015-02-16 10:50:36 -07004 * 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"
Junxiao Shifef73e42016-03-29 14:15:05 -070027#include "pit-algorithm.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060028#include "core/logger.hpp"
Junxiao Shiaf6569a2014-06-14 00:01:34 -070029#include "core/random.hpp"
Junxiao Shifaf3eb02015-02-16 10:50:36 -070030#include "strategy.hpp"
Junxiao Shi02b73f52016-07-28 01:48:27 +000031#include "table/cleanup.hpp"
Junxiao Shiaf6569a2014-06-14 00:01:34 -070032#include <boost/random/uniform_int_distribution.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080033
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080034namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080035
Junxiao Shi8c8d2182014-01-30 22:33:00 -070036NFD_LOG_INIT("Forwarder");
37
Junxiao Shic041ca32014-02-25 20:01:15 -070038Forwarder::Forwarder()
Junxiao Shifbe8efe2016-08-22 16:02:30 +000039 : m_unsolicitedDataPolicy(new fw::AdmitLocalUnsolicitedDataPolicy())
40 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060041 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080042 , m_measurements(m_nameTree)
Junxiao Shif3c07812014-03-11 21:48:49 -070043 , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080044{
Junxiao Shif3c07812014-03-11 21:48:49 -070045 fw::installStrategies(*this);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000046
47 m_faceTable.afterAdd.connect([this] (Face& face) {
48 face.afterReceiveInterest.connect(
49 [this, &face] (const Interest& interest) {
50 this->startProcessInterest(face, interest);
51 });
52 face.afterReceiveData.connect(
53 [this, &face] (const Data& data) {
54 this->startProcessData(face, data);
55 });
56 face.afterReceiveNack.connect(
57 [this, &face] (const lp::Nack& nack) {
58 this->startProcessNack(face, nack);
59 });
60 });
61
62 m_faceTable.beforeRemove.connect([this] (Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000063 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000064 });
Alexander Afanasyev33b72772014-01-26 23:22:58 -080065}
66
Junxiao Shidcffdaa2016-07-26 02:23:56 +000067Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060068
Junxiao Shi0355e9f2015-09-02 07:24:53 -070069void
70Forwarder::startProcessInterest(Face& face, const Interest& interest)
71{
72 // check fields used by forwarding are well-formed
73 try {
74 if (interest.hasLink()) {
75 interest.getLink();
76 }
77 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -070078 catch (const tlv::Error&) {
Junxiao Shi0355e9f2015-09-02 07:24:53 -070079 NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() <<
80 " interest=" << interest.getName() << " malformed");
81 // It's safe to call interest.getName() because Name has been fully parsed
82 return;
83 }
84
85 this->onIncomingInterest(face, interest);
86}
87
88void
89Forwarder::startProcessData(Face& face, const Data& data)
90{
91 // check fields used by forwarding are well-formed
92 // (none needed)
93
94 this->onIncomingData(face, data);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060095}
96
Alexander Afanasyev33b72772014-01-26 23:22:58 -080097void
Junxiao Shi5e5e4452015-09-24 16:56:52 -070098Forwarder::startProcessNack(Face& face, const lp::Nack& nack)
99{
100 // check fields used by forwarding are well-formed
101 try {
102 if (nack.getInterest().hasLink()) {
103 nack.getInterest().getLink();
104 }
105 }
106 catch (const tlv::Error&) {
107 NFD_LOG_DEBUG("startProcessNack face=" << face.getId() <<
108 " nack=" << nack.getInterest().getName() <<
109 "~" << nack.getReason() << " malformed");
110 return;
111 }
112
113 this->onIncomingNack(face, nack);
114}
115
116void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700117Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
118{
119 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700120 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
121 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000122 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700123 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -0700124
Junxiao Shi88884492014-02-15 15:57:43 -0700125 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700126 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700127 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700128 if (isViolatingLocalhost) {
129 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
130 " interest=" << interest.getName() << " violates /localhost");
131 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700132 return;
133 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700134
Junxiao Shi330136a2016-03-10 04:53:08 -0700135 // detect duplicate Nonce with Dead Nonce List
136 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
137 if (hasDuplicateNonceInDnl) {
138 // goto Interest loop pipeline
139 this->onInterestLoop(inFace, interest);
140 return;
141 }
142
Junxiao Shid3c792f2014-01-30 00:46:13 -0700143 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700144 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700145
Junxiao Shi330136a2016-03-10 04:53:08 -0700146 // detect duplicate Nonce in PIT entry
Junxiao Shifef73e42016-03-29 14:15:05 -0700147 bool hasDuplicateNonceInPit = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace) !=
148 fw::DUPLICATE_NONCE_NONE;
Junxiao Shi330136a2016-03-10 04:53:08 -0700149 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700150 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700151 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700152 return;
153 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700154
Junxiao Shid3c792f2014-01-30 00:46:13 -0700155 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000156 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700157
Junxiao Shif3c07812014-03-11 21:48:49 -0700158 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700159 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600160 m_cs.find(interest,
161 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
162 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700163 }
mzhang4eab72492015-02-25 11:16:09 -0600164 else {
165 this->onContentStoreMiss(inFace, pitEntry, interest);
166 }
167}
Junxiao Shic041ca32014-02-25 20:01:15 -0700168
mzhang4eab72492015-02-25 11:16:09 -0600169void
Junxiao Shi330136a2016-03-10 04:53:08 -0700170Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700171{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700172 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700173 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700174 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
175 " interest=" << interest.getName() <<
176 " drop");
177 return;
178 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700179
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700180 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
181 " interest=" << interest.getName() <<
182 " send-Nack-duplicate");
183
184 // send Nack with reason=DUPLICATE
185 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
186 lp::Nack nack(interest);
187 nack.setReason(lp::NackReason::DUPLICATE);
188 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700189}
190
191void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000192Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
mzhang4eab72492015-02-25 11:16:09 -0600193 const Interest& interest)
194{
195 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
196
Junxiao Shi4846f372016-04-05 13:39:30 -0700197 // insert in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000198 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700199
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700200 // set PIT unsatisfy timer
201 this->setUnsatisfyTimer(pitEntry);
202
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000203 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000204 this->dispatchToStrategy(*pitEntry,
205 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700206}
207
208void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000209Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
210 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600211{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500212 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600213
Junxiao Shicde37ad2015-12-24 01:02:05 -0700214 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600215 // XXX should we lookup PIT for other Interests that also match csMatch?
216
217 // set PIT straggler timer
218 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
219
220 // goto outgoing Data pipeline
221 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
222}
223
Junxiao Shid3c792f2014-01-30 00:46:13 -0700224void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000225Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700226 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700227{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700228 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700229 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
230 return;
231 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700232 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
233 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700234
Junxiao Shi57f0f312014-03-16 11:52:20 -0700235 // scope control
Junxiao Shifef73e42016-03-29 14:15:05 -0700236 if (fw::violatesScope(*pitEntry, outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700237 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700238 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700239 return;
240 }
241
Junxiao Shid3c792f2014-01-30 00:46:13 -0700242 // pick Interest
Junxiao Shi891f47b2016-06-20 00:02:11 +0000243 // The outgoing Interest picked is the last incoming Interest that does not come from outFace.
244 // If all in-records come from outFace, it's fine to pick that.
245 // This happens when there's only one in-record that comes from outFace.
246 // The legit use is for vehicular network; otherwise, strategy shouldn't send to the sole inFace.
Junxiao Shi4846f372016-04-05 13:39:30 -0700247 pit::InRecordCollection::iterator pickedInRecord = std::max_element(
Junxiao Shi891f47b2016-06-20 00:02:11 +0000248 pitEntry->in_begin(), pitEntry->in_end(),
249 [&outFace] (const pit::InRecord& a, const pit::InRecord& b) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000250 bool isOutFaceA = &a.getFace() == &outFace;
251 bool isOutFaceB = &b.getFace() == &outFace;
Junxiao Shi891f47b2016-06-20 00:02:11 +0000252 return (isOutFaceA > isOutFaceB) ||
253 (isOutFaceA == isOutFaceB && a.getLastRenewed() < b.getLastRenewed());
254 });
Junxiao Shi4846f372016-04-05 13:39:30 -0700255 BOOST_ASSERT(pickedInRecord != pitEntry->in_end());
256 auto interest = const_pointer_cast<Interest>(pickedInRecord->getInterest().shared_from_this());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700257
258 if (wantNewNonce) {
259 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700260 static boost::random::uniform_int_distribution<uint32_t> dist;
261 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700262 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700263
Junxiao Shi4846f372016-04-05 13:39:30 -0700264 // insert out-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000265 pitEntry->insertOrUpdateOutRecord(outFace, *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700266
Junxiao Shid3c792f2014-01-30 00:46:13 -0700267 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700268 outFace.sendInterest(*interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700269 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700270}
271
272void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000273Forwarder::onInterestReject(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700274{
Junxiao Shifef73e42016-03-29 14:15:05 -0700275 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700276 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
277 " cannot reject forwarded Interest");
278 return;
279 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700280 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700281
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700282 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000283 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700284
Junxiao Shid3c792f2014-01-30 00:46:13 -0700285 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700286 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700287}
288
289void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000290Forwarder::onInterestUnsatisfied(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700291{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700292 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
293
Junxiao Shid3c792f2014-01-30 00:46:13 -0700294 // invoke PIT unsatisfied callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000295 this->dispatchToStrategy(*pitEntry,
296 [&] (fw::Strategy& strategy) { strategy.beforeExpirePendingInterest(pitEntry); });
Junxiao Shic041ca32014-02-25 20:01:15 -0700297
Junxiao Shia110f262014-10-12 12:35:20 -0700298 // goto Interest Finalize pipeline
299 this->onInterestFinalize(pitEntry, false);
300}
301
302void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000303Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
304 time::milliseconds dataFreshnessPeriod)
Junxiao Shia110f262014-10-12 12:35:20 -0700305{
306 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
307 (isSatisfied ? " satisfied" : " unsatisfied"));
308
309 // Dead Nonce List insert if necessary
310 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
311
Junxiao Shif3c07812014-03-11 21:48:49 -0700312 // PIT delete
Junxiao Shib9420cf2016-08-13 04:38:52 +0000313 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000314 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700315}
316
317void
318Forwarder::onIncomingData(Face& inFace, const Data& data)
319{
320 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700321 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000322 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700323 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700324
Junxiao Shi88884492014-02-15 15:57:43 -0700325 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700326 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700327 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700328 if (isViolatingLocalhost) {
329 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
330 " data=" << data.getName() << " violates /localhost");
331 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700332 return;
333 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700334
Junxiao Shid3c792f2014-01-30 00:46:13 -0700335 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700336 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
337 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700338 // goto Data unsolicited pipeline
339 this->onDataUnsolicited(inFace, data);
340 return;
341 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700342
Junxiao Shid3c792f2014-01-30 00:46:13 -0700343 // CS insert
344 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700345
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700346 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700347 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700348 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700349 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700350 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700351
Junxiao Shid3c792f2014-01-30 00:46:13 -0700352 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000353 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700354
Junxiao Shid3c792f2014-01-30 00:46:13 -0700355 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700356 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
357 if (inRecord.getExpiry() > now) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000358 pendingDownstreams.insert(&inRecord.getFace());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700359 }
360 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700361
Junxiao Shid938a6b2014-05-11 23:40:29 -0700362 // invoke PIT satisfy callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000363 this->dispatchToStrategy(*pitEntry,
364 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700365
Junxiao Shi4846f372016-04-05 13:39:30 -0700366 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700367 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
368
Junxiao Shid3c792f2014-01-30 00:46:13 -0700369 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700370 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700371 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700372
Junxiao Shid3c792f2014-01-30 00:46:13 -0700373 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700374 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700375 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700376
Junxiao Shid3c792f2014-01-30 00:46:13 -0700377 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700378 for (Face* pendingDownstream : pendingDownstreams) {
379 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700380 continue;
381 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700382 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700383 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700384 }
385}
386
387void
388Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
389{
390 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000391 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
392 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700393 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700394 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700395 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700396
Junxiao Shif3c07812014-03-11 21:48:49 -0700397 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
398 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000399 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700400}
401
402void
403Forwarder::onOutgoingData(const Data& data, Face& outFace)
404{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700405 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700406 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
407 return;
408 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700409 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
410
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700411 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700412 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700413 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700414 if (isViolatingLocalhost) {
415 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
416 " data=" << data.getName() << " violates /localhost");
417 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700418 return;
419 }
420
Junxiao Shif3c07812014-03-11 21:48:49 -0700421 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700422
Junxiao Shid3c792f2014-01-30 00:46:13 -0700423 // send Data
424 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700425 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700426}
427
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700428void
429Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
430{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000431 // receive Nack
432 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700433 ++m_counters.nInNacks;
434
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700435 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700436 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700437 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
438 " nack=" << nack.getInterest().getName() <<
439 "~" << nack.getReason() << " face-is-multi-access");
440 return;
441 }
442
443 // PIT match
444 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
445 // if no PIT entry found, drop
446 if (pitEntry == nullptr) {
447 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
448 " nack=" << nack.getInterest().getName() <<
449 "~" << nack.getReason() << " no-PIT-entry");
450 return;
451 }
452
453 // has out-record?
454 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
455 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700456 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700457 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
458 " nack=" << nack.getInterest().getName() <<
459 "~" << nack.getReason() << " no-out-record");
460 return;
461 }
462
463 // if out-record has different Nonce, drop
464 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
465 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
466 " nack=" << nack.getInterest().getName() <<
467 "~" << nack.getReason() << " wrong-Nonce " <<
468 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
469 return;
470 }
471
472 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
473 " nack=" << nack.getInterest().getName() <<
474 "~" << nack.getReason() << " OK");
475
476 // record Nack on out-record
477 outRecord->setIncomingNack(nack);
478
479 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000480 this->dispatchToStrategy(*pitEntry,
481 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700482}
483
484void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000485Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700486 const lp::NackHeader& nack)
487{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700488 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700489 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
490 " nack=" << pitEntry->getInterest().getName() <<
491 "~" << nack.getReason() << " no-in-record");
492 return;
493 }
494
495 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700496 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700497
498 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700499 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700500 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
501 " nack=" << pitEntry->getInterest().getName() <<
502 "~" << nack.getReason() << " no-in-record");
503 return;
504 }
505
506 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700507 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700508 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
509 " nack=" << pitEntry->getInterest().getName() <<
510 "~" << nack.getReason() << " face-is-multi-access");
511 return;
512 }
513
514 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
515 " nack=" << pitEntry->getInterest().getName() <<
516 "~" << nack.getReason() << " OK");
517
518 // create Nack packet with the Interest from in-record
519 lp::Nack nackPkt(inRecord->getInterest());
520 nackPkt.setHeader(nack);
521
522 // erase in-record
523 pitEntry->deleteInRecord(outFace);
524
525 // send Nack on face
526 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700527 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700528}
529
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000530const fib::Entry&
531Forwarder::lookupFib(const pit::Entry& pitEntry) const
532{
533 const Interest& interest = pitEntry.getInterest();
534 // has Link object?
535 if (!interest.hasLink()) {
536 // FIB lookup with Interest name
Junxiao Shia6de4292016-07-12 02:08:10 +0000537 const fib::Entry& fibEntry = m_fib.findLongestPrefixMatch(pitEntry);
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000538 NFD_LOG_TRACE("lookupFib noLinkObject found=" << fibEntry.getPrefix());
539 return fibEntry;
540 }
541
542 const Link& link = interest.getLink();
543
544 // in producer region?
545 if (m_networkRegionTable.isInProducerRegion(link)) {
546 // FIB lookup with Interest name
Junxiao Shia6de4292016-07-12 02:08:10 +0000547 const fib::Entry& fibEntry = m_fib.findLongestPrefixMatch(pitEntry);
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000548 NFD_LOG_TRACE("lookupFib inProducerRegion found=" << fibEntry.getPrefix());
549 return fibEntry;
550 }
551
552 // has SelectedDelegation?
553 if (interest.hasSelectedDelegation()) {
554 // FIB lookup with SelectedDelegation
555 Name selectedDelegation = interest.getSelectedDelegation();
Junxiao Shia6de4292016-07-12 02:08:10 +0000556 const fib::Entry& fibEntry = m_fib.findLongestPrefixMatch(selectedDelegation);
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000557 NFD_LOG_TRACE("lookupFib hasSelectedDelegation=" << selectedDelegation << " found=" << fibEntry.getPrefix());
558 return fibEntry;
559 }
560
561 // FIB lookup with first delegation Name
Junxiao Shia6de4292016-07-12 02:08:10 +0000562 const fib::Entry& fibEntry0 = m_fib.findLongestPrefixMatch(link.getDelegations().begin()->second);
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000563 // in default-free zone?
564 bool isDefaultFreeZone = !(fibEntry0.getPrefix().size() == 0 && fibEntry0.hasNextHops());
565 if (!isDefaultFreeZone) {
566 NFD_LOG_TRACE("onContentStoreMiss inConsumerRegion found=" << fibEntry0.getPrefix());
567 return fibEntry0;
568 }
569
570 // choose and set SelectedDelegation
571 for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) {
572 const Name& delegationName = delegation.second;
Junxiao Shia6de4292016-07-12 02:08:10 +0000573 const fib::Entry& fibEntry = m_fib.findLongestPrefixMatch(delegationName);
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000574 if (fibEntry.hasNextHops()) {
575 /// \todo Don't modify in-record Interests.
576 /// Set SelectedDelegation in outgoing Interest pipeline.
577 std::for_each(pitEntry.in_begin(), pitEntry.in_end(),
578 [&delegationName] (const pit::InRecord& inR) {
579 const_cast<Interest&>(inR.getInterest()).setSelectedDelegation(delegationName);
580 });
581 NFD_LOG_TRACE("onContentStoreMiss enterDefaultFreeZone"
582 << " setSelectedDelegation=" << delegationName);
583 return fibEntry;
584 }
585 }
586 BOOST_ASSERT(false);
587 return fibEntry0;
588}
589
Junxiao Shid3c792f2014-01-30 00:46:13 -0700590static inline bool
591compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
592{
593 return a.getExpiry() < b.getExpiry();
594}
595
596void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000597Forwarder::setUnsatisfyTimer(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700598{
Junxiao Shi4846f372016-04-05 13:39:30 -0700599 pit::InRecordCollection::iterator lastExpiring =
600 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700601
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700602 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700603 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
604 if (lastExpiryFromNow <= time::seconds::zero()) {
605 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700606 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700607
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700608 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700609 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700610 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
611}
612
613void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000614Forwarder::setStragglerTimer(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
615 time::milliseconds dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700616{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700617 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700618
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700619 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700620 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700621 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700622}
623
624void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000625Forwarder::cancelUnsatisfyAndStragglerTimer(pit::Entry& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700626{
Junxiao Shib9420cf2016-08-13 04:38:52 +0000627 scheduler::cancel(pitEntry.m_unsatisfyTimer);
628 scheduler::cancel(pitEntry.m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700629}
630
Junxiao Shia110f262014-10-12 12:35:20 -0700631static inline void
632insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
633 const pit::OutRecord& outRecord)
634{
635 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
636}
637
638void
639Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000640 time::milliseconds dataFreshnessPeriod, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700641{
642 // need Dead Nonce List insert?
643 bool needDnl = false;
644 if (isSatisfied) {
645 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
646 // Data never becomes stale if it doesn't have FreshnessPeriod field
647 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
648 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
649 }
650 else {
651 needDnl = true;
652 }
653
654 if (!needDnl) {
655 return;
656 }
657
658 // Dead Nonce List insert
659 if (upstream == 0) {
660 // insert all outgoing Nonces
661 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
662 std::for_each(outRecords.begin(), outRecords.end(),
663 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
664 }
665 else {
666 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700667 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700668 if (outRecord != pitEntry.getOutRecords().end()) {
669 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
670 }
671 }
672}
673
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800674} // namespace nfd