blob: cb7ac9dcd3159462923daa90ba59b3bdf361adf2 [file] [log] [blame]
Alexander Afanasyev33b72772014-01-26 23:22:58 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shifc2e13d2017-07-25 02:08:48 +00002/*
Teng Liang63086442018-02-25 20:39:42 -07003 * Copyright (c) 2014-2018, 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 Shi00dc9142016-11-21 14:23:12 +000027#include "algorithm.hpp"
Junxiao Shic34d1672016-12-09 15:57:59 +000028#include "best-route-strategy2.hpp"
Junxiao Shifaf3eb02015-02-16 10:50:36 -070029#include "strategy.hpp"
Junxiao Shic34d1672016-12-09 15:57:59 +000030#include "core/logger.hpp"
Junxiao Shi02b73f52016-07-28 01:48:27 +000031#include "table/cleanup.hpp"
Junxiao Shicbc8e942016-09-06 03:17:45 +000032#include <ndn-cxx/lp/tags.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 Shic34d1672016-12-09 15:57:59 +000038static Name
39getDefaultStrategyName()
40{
Junxiao Shi037f4ab2016-12-13 04:27:06 +000041 return fw::BestRouteStrategy2::getStrategyName();
Junxiao Shic34d1672016-12-09 15:57:59 +000042}
43
Junxiao Shic041ca32014-02-25 20:01:15 -070044Forwarder::Forwarder()
Junxiao Shi9685cc52016-08-29 12:47:05 +000045 : m_unsolicitedDataPolicy(new fw::DefaultUnsolicitedDataPolicy())
Junxiao Shifbe8efe2016-08-22 16:02:30 +000046 , 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 Shic34d1672016-12-09 15:57:59 +000049 , m_strategyChoice(*this)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080050{
Junxiao Shidcffdaa2016-07-26 02:23:56 +000051 m_faceTable.afterAdd.connect([this] (Face& face) {
52 face.afterReceiveInterest.connect(
53 [this, &face] (const Interest& interest) {
54 this->startProcessInterest(face, interest);
55 });
56 face.afterReceiveData.connect(
57 [this, &face] (const Data& data) {
58 this->startProcessData(face, data);
59 });
60 face.afterReceiveNack.connect(
61 [this, &face] (const lp::Nack& nack) {
62 this->startProcessNack(face, nack);
63 });
Eric Newberry41aba102017-11-01 16:42:13 -070064 face.onDroppedInterest.connect(
65 [this, &face] (const Interest& interest) {
66 this->onDroppedInterest(face, interest);
67 });
Junxiao Shidcffdaa2016-07-26 02:23:56 +000068 });
69
70 m_faceTable.beforeRemove.connect([this] (Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000071 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000072 });
Junxiao Shic34d1672016-12-09 15:57:59 +000073
74 m_strategyChoice.setDefaultStrategy(getDefaultStrategyName());
Alexander Afanasyev33b72772014-01-26 23:22:58 -080075}
76
Junxiao Shidcffdaa2016-07-26 02:23:56 +000077Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060078
Junxiao Shi0355e9f2015-09-02 07:24:53 -070079void
Junxiao Shid3c792f2014-01-30 00:46:13 -070080Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
81{
82 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -070083 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
84 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +000085 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -070086 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -070087
Junxiao Shi88884492014-02-15 15:57:43 -070088 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -070089 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -070090 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -070091 if (isViolatingLocalhost) {
92 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
93 " interest=" << interest.getName() << " violates /localhost");
94 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -070095 return;
96 }
Junxiao Shic041ca32014-02-25 20:01:15 -070097
Junxiao Shi330136a2016-03-10 04:53:08 -070098 // detect duplicate Nonce with Dead Nonce List
99 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
100 if (hasDuplicateNonceInDnl) {
101 // goto Interest loop pipeline
102 this->onInterestLoop(inFace, interest);
103 return;
104 }
105
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000106 // strip forwarding hint if Interest has reached producer region
107 if (!interest.getForwardingHint().empty() &&
108 m_networkRegionTable.isInProducerRegion(interest.getForwardingHint())) {
Junxiao Shif9858fd2017-02-01 16:22:44 +0000109 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
110 " interest=" << interest.getName() << " reaching-producer-region");
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000111 const_cast<Interest&>(interest).setForwardingHint({});
Junxiao Shif9858fd2017-02-01 16:22:44 +0000112 }
113
Junxiao Shid3c792f2014-01-30 00:46:13 -0700114 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700115 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700116
Junxiao Shi330136a2016-03-10 04:53:08 -0700117 // detect duplicate Nonce in PIT entry
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000118 int dnw = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace);
119 bool hasDuplicateNonceInPit = dnw != fw::DUPLICATE_NONCE_NONE;
120 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
121 // for p2p face: duplicate Nonce from same incoming face is not loop
122 hasDuplicateNonceInPit = hasDuplicateNonceInPit && !(dnw & fw::DUPLICATE_NONCE_IN_SAME);
123 }
Junxiao Shi330136a2016-03-10 04:53:08 -0700124 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700125 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700126 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700127 return;
128 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700129
Junxiao Shif3c07812014-03-11 21:48:49 -0700130 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700131 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600132 m_cs.find(interest,
133 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
134 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700135 }
mzhang4eab72492015-02-25 11:16:09 -0600136 else {
137 this->onContentStoreMiss(inFace, pitEntry, interest);
138 }
139}
Junxiao Shic041ca32014-02-25 20:01:15 -0700140
mzhang4eab72492015-02-25 11:16:09 -0600141void
Junxiao Shi330136a2016-03-10 04:53:08 -0700142Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700143{
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700144 // if multi-access or ad hoc face, drop
145 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700146 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
147 " interest=" << interest.getName() <<
148 " drop");
149 return;
150 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700151
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700152 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
153 " interest=" << interest.getName() <<
154 " send-Nack-duplicate");
155
156 // send Nack with reason=DUPLICATE
157 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
158 lp::Nack nack(interest);
159 nack.setReason(lp::NackReason::DUPLICATE);
160 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700161}
162
Teng Liang7003e0b2018-03-03 16:03:30 -0700163static inline bool
164compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
165{
166 return a.getExpiry() < b.getExpiry();
167}
168
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700169void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000170Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
mzhang4eab72492015-02-25 11:16:09 -0600171 const Interest& interest)
172{
173 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000174 ++m_counters.nCsMisses;
mzhang4eab72492015-02-25 11:16:09 -0600175
Junxiao Shi4846f372016-04-05 13:39:30 -0700176 // insert in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000177 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700178
Teng Liang7003e0b2018-03-03 16:03:30 -0700179 // set PIT expiry timer to the time that the last PIT in-record expires
180 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
181 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
182 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700183
Junxiao Shie342e8d2016-09-18 16:48:00 +0000184 // has NextHopFaceId?
185 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
186 if (nextHopTag != nullptr) {
187 // chosen NextHop face exists?
188 Face* nextHopFace = m_faceTable.get(*nextHopTag);
189 if (nextHopFace != nullptr) {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000190 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName() << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000191 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000192 // scope control is unnecessary, because privileged app explicitly wants to forward
193 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000194 }
195 return;
196 }
197
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000198 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000199 this->dispatchToStrategy(*pitEntry,
200 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700201}
202
203void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000204Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
205 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600206{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500207 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
Junxiao Shi06a1eab2017-09-04 13:13:02 +0000208 ++m_counters.nCsHits;
mzhang4eab72492015-02-25 11:16:09 -0600209
Junxiao Shicde37ad2015-12-24 01:02:05 -0700210 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600211 // XXX should we lookup PIT for other Interests that also match csMatch?
212
Teng Liang6f09ab62018-03-01 20:04:08 -0700213 pitEntry->isSatisfied = true;
214 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
215
Teng Liang7003e0b2018-03-03 16:03:30 -0700216 // finalize Interest
217 this->onInterestFinalize(pitEntry);
mzhang4eab72492015-02-25 11:16:09 -0600218
219 // goto outgoing Data pipeline
220 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
221}
222
Junxiao Shid3c792f2014-01-30 00:46:13 -0700223void
Junxiao Shic5f651f2016-11-17 22:58:12 +0000224Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700225{
Junxiao Shif3c07812014-03-11 21:48:49 -0700226 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
227 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700228
Junxiao Shi4846f372016-04-05 13:39:30 -0700229 // insert out-record
Junxiao Shic5f651f2016-11-17 22:58:12 +0000230 pitEntry->insertOrUpdateOutRecord(outFace, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700231
Junxiao Shid3c792f2014-01-30 00:46:13 -0700232 // send Interest
Junxiao Shic5f651f2016-11-17 22:58:12 +0000233 outFace.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700234 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700235}
236
237void
Teng Liang6f09ab62018-03-01 20:04:08 -0700238Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shia110f262014-10-12 12:35:20 -0700239{
240 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
Teng Liang6f09ab62018-03-01 20:04:08 -0700241 (pitEntry->isSatisfied ? " satisfied" : " unsatisfied"));
Junxiao Shia110f262014-10-12 12:35:20 -0700242
243 // Dead Nonce List insert if necessary
Teng Liang6f09ab62018-03-01 20:04:08 -0700244 this->insertDeadNonceList(*pitEntry, 0);
Junxiao Shia110f262014-10-12 12:35:20 -0700245
Junxiao Shif3c07812014-03-11 21:48:49 -0700246 // PIT delete
Teng Liang7003e0b2018-03-03 16:03:30 -0700247 scheduler::cancel(pitEntry->expiryTimer);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000248 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700249}
250
251void
252Forwarder::onIncomingData(Face& inFace, const Data& data)
253{
254 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700255 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000256 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700257 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700258
Junxiao Shi88884492014-02-15 15:57:43 -0700259 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700260 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700261 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700262 if (isViolatingLocalhost) {
263 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
264 " data=" << data.getName() << " violates /localhost");
265 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700266 return;
267 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700268
Junxiao Shid3c792f2014-01-30 00:46:13 -0700269 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700270 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
271 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700272 // goto Data unsolicited pipeline
273 this->onDataUnsolicited(inFace, data);
274 return;
275 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700276
Junxiao Shid3c792f2014-01-30 00:46:13 -0700277 // CS insert
278 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700279
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700280 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700281 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700282 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700283 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700284 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700285
Junxiao Shid3c792f2014-01-30 00:46:13 -0700286 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700287 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
288 if (inRecord.getExpiry() > now) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000289 pendingDownstreams.insert(&inRecord.getFace());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700290 }
291 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700292
Teng Liang7003e0b2018-03-03 16:03:30 -0700293 // set PIT expiry timer to now
294 this->setExpiryTimer(pitEntry, 0_ms);
295
Junxiao Shid938a6b2014-05-11 23:40:29 -0700296 // invoke PIT satisfy callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000297 this->dispatchToStrategy(*pitEntry,
298 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700299
Teng Liang6f09ab62018-03-01 20:04:08 -0700300 pitEntry->isSatisfied = true;
301 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
302
Junxiao Shi4846f372016-04-05 13:39:30 -0700303 // Dead Nonce List insert if necessary (for out-record of inFace)
Teng Liang6f09ab62018-03-01 20:04:08 -0700304 this->insertDeadNonceList(*pitEntry, &inFace);
Junxiao Shia110f262014-10-12 12:35:20 -0700305
Junxiao Shid3c792f2014-01-30 00:46:13 -0700306 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700307 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700308 pitEntry->deleteOutRecord(inFace);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700309 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700310
Junxiao Shid3c792f2014-01-30 00:46:13 -0700311 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700312 for (Face* pendingDownstream : pendingDownstreams) {
Teng Liangf995f382017-04-04 22:09:39 +0000313 if (pendingDownstream->getId() == inFace.getId() &&
314 pendingDownstream->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
Junxiao Shida006f52014-05-16 11:18:00 -0700315 continue;
316 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700317 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700318 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700319 }
320}
321
322void
323Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
324{
325 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000326 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
327 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700328 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700329 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700330 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700331
Junxiao Shif3c07812014-03-11 21:48:49 -0700332 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
333 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000334 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700335}
336
337void
338Forwarder::onOutgoingData(const Data& data, Face& outFace)
339{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700340 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700341 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
342 return;
343 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700344 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
345
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700346 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700347 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700348 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700349 if (isViolatingLocalhost) {
350 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
351 " data=" << data.getName() << " violates /localhost");
352 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700353 return;
354 }
355
Junxiao Shif3c07812014-03-11 21:48:49 -0700356 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700357
Junxiao Shid3c792f2014-01-30 00:46:13 -0700358 // send Data
359 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700360 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700361}
362
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700363void
364Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
365{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000366 // receive Nack
367 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700368 ++m_counters.nInNacks;
369
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700370 // if multi-access or ad hoc face, drop
371 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700372 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
373 " nack=" << nack.getInterest().getName() <<
374 "~" << nack.getReason() << " face-is-multi-access");
375 return;
376 }
377
378 // PIT match
379 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
380 // if no PIT entry found, drop
381 if (pitEntry == nullptr) {
382 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
383 " nack=" << nack.getInterest().getName() <<
384 "~" << nack.getReason() << " no-PIT-entry");
385 return;
386 }
387
388 // has out-record?
389 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
390 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700391 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700392 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
393 " nack=" << nack.getInterest().getName() <<
394 "~" << nack.getReason() << " no-out-record");
395 return;
396 }
397
398 // if out-record has different Nonce, drop
399 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
400 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
401 " nack=" << nack.getInterest().getName() <<
402 "~" << nack.getReason() << " wrong-Nonce " <<
403 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
404 return;
405 }
406
407 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
408 " nack=" << nack.getInterest().getName() <<
409 "~" << nack.getReason() << " OK");
410
411 // record Nack on out-record
412 outRecord->setIncomingNack(nack);
413
Teng Liang7003e0b2018-03-03 16:03:30 -0700414 // set PIT expiry timer to now when all out-record receive Nack
415 if (!fw::hasPendingOutRecords(*pitEntry)) {
416 this->setExpiryTimer(pitEntry, 0_ms);
417 }
418
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700419 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000420 this->dispatchToStrategy(*pitEntry,
421 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700422}
423
424void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000425Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700426 const lp::NackHeader& nack)
427{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700428 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700429 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
430 " nack=" << pitEntry->getInterest().getName() <<
431 "~" << nack.getReason() << " no-in-record");
432 return;
433 }
434
435 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700436 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700437
438 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700439 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700440 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
441 " nack=" << pitEntry->getInterest().getName() <<
442 "~" << nack.getReason() << " no-in-record");
443 return;
444 }
445
Teng Liang4f5cdcf2017-03-16 15:33:31 -0700446 // if multi-access or ad hoc face, drop
447 if (outFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700448 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
449 " nack=" << pitEntry->getInterest().getName() <<
450 "~" << nack.getReason() << " face-is-multi-access");
451 return;
452 }
453
454 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
455 " nack=" << pitEntry->getInterest().getName() <<
456 "~" << nack.getReason() << " OK");
457
458 // create Nack packet with the Interest from in-record
459 lp::Nack nackPkt(inRecord->getInterest());
460 nackPkt.setHeader(nack);
461
462 // erase in-record
463 pitEntry->deleteInRecord(outFace);
464
465 // send Nack on face
466 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700467 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700468}
469
Eric Newberry41aba102017-11-01 16:42:13 -0700470void
471Forwarder::onDroppedInterest(Face& outFace, const Interest& interest)
472{
473 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(outFace, interest);
474}
475
Junxiao Shid3c792f2014-01-30 00:46:13 -0700476void
Teng Liang7003e0b2018-03-03 16:03:30 -0700477Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700478{
Teng Liang7003e0b2018-03-03 16:03:30 -0700479 BOOST_ASSERT(duration >= 0_ms);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700480
Teng Liang7003e0b2018-03-03 16:03:30 -0700481 scheduler::cancel(pitEntry->expiryTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700482
Teng Liang7003e0b2018-03-03 16:03:30 -0700483 pitEntry->expiryTimer = scheduler::schedule(duration,
Teng Liang6f09ab62018-03-01 20:04:08 -0700484 bind(&Forwarder::onInterestFinalize, this, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700485}
486
Junxiao Shia110f262014-10-12 12:35:20 -0700487static inline void
488insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
489 const pit::OutRecord& outRecord)
490{
491 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
492}
493
494void
Teng Liang6f09ab62018-03-01 20:04:08 -0700495Forwarder::insertDeadNonceList(pit::Entry& pitEntry, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700496{
497 // need Dead Nonce List insert?
Eric Newberryf4056d02017-05-26 17:31:53 +0000498 bool needDnl = true;
Teng Liang6f09ab62018-03-01 20:04:08 -0700499 if (pitEntry.isSatisfied) {
500 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
Junxiao Shia110f262014-10-12 12:35:20 -0700501 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
Teng Liang6f09ab62018-03-01 20:04:08 -0700502 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
Junxiao Shia110f262014-10-12 12:35:20 -0700503 }
504
505 if (!needDnl) {
506 return;
507 }
508
509 // Dead Nonce List insert
Teng Liangf995f382017-04-04 22:09:39 +0000510 if (upstream == nullptr) {
Junxiao Shia110f262014-10-12 12:35:20 -0700511 // insert all outgoing Nonces
512 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
513 std::for_each(outRecords.begin(), outRecords.end(),
514 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
515 }
516 else {
517 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700518 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700519 if (outRecord != pitEntry.getOutRecords().end()) {
520 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
521 }
522 }
523}
524
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800525} // namespace nfd