blob: 7e0df653bccd15e6b1662a223046e1ba1648a136 [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 Shi00dc9142016-11-21 14:23:12 +000027#include "algorithm.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060028#include "core/logger.hpp"
Junxiao Shifaf3eb02015-02-16 10:50:36 -070029#include "strategy.hpp"
Junxiao Shi02b73f52016-07-28 01:48:27 +000030#include "table/cleanup.hpp"
Junxiao Shicbc8e942016-09-06 03:17:45 +000031#include <ndn-cxx/lp/tags.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080032
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080033namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080034
Junxiao Shi8c8d2182014-01-30 22:33:00 -070035NFD_LOG_INIT("Forwarder");
36
Junxiao Shic041ca32014-02-25 20:01:15 -070037Forwarder::Forwarder()
Junxiao Shi9685cc52016-08-29 12:47:05 +000038 : m_unsolicitedDataPolicy(new fw::DefaultUnsolicitedDataPolicy())
Junxiao Shifbe8efe2016-08-22 16:02:30 +000039 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060040 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080041 , m_measurements(m_nameTree)
Junxiao Shif3c07812014-03-11 21:48:49 -070042 , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080043{
Junxiao Shif3c07812014-03-11 21:48:49 -070044 fw::installStrategies(*this);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000045
46 m_faceTable.afterAdd.connect([this] (Face& face) {
47 face.afterReceiveInterest.connect(
48 [this, &face] (const Interest& interest) {
49 this->startProcessInterest(face, interest);
50 });
51 face.afterReceiveData.connect(
52 [this, &face] (const Data& data) {
53 this->startProcessData(face, data);
54 });
55 face.afterReceiveNack.connect(
56 [this, &face] (const lp::Nack& nack) {
57 this->startProcessNack(face, nack);
58 });
59 });
60
61 m_faceTable.beforeRemove.connect([this] (Face& face) {
Junxiao Shi02b73f52016-07-28 01:48:27 +000062 cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face);
Junxiao Shidcffdaa2016-07-26 02:23:56 +000063 });
Alexander Afanasyev33b72772014-01-26 23:22:58 -080064}
65
Junxiao Shidcffdaa2016-07-26 02:23:56 +000066Forwarder::~Forwarder() = default;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060067
Junxiao Shi0355e9f2015-09-02 07:24:53 -070068void
69Forwarder::startProcessInterest(Face& face, const Interest& interest)
70{
71 // check fields used by forwarding are well-formed
72 try {
73 if (interest.hasLink()) {
74 interest.getLink();
75 }
76 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -070077 catch (const tlv::Error&) {
Junxiao Shi0355e9f2015-09-02 07:24:53 -070078 NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() <<
79 " interest=" << interest.getName() << " malformed");
80 // It's safe to call interest.getName() because Name has been fully parsed
81 return;
82 }
83
84 this->onIncomingInterest(face, interest);
85}
86
87void
88Forwarder::startProcessData(Face& face, const Data& data)
89{
90 // check fields used by forwarding are well-formed
91 // (none needed)
92
93 this->onIncomingData(face, data);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060094}
95
Alexander Afanasyev33b72772014-01-26 23:22:58 -080096void
Junxiao Shi5e5e4452015-09-24 16:56:52 -070097Forwarder::startProcessNack(Face& face, const lp::Nack& nack)
98{
99 // check fields used by forwarding are well-formed
100 try {
101 if (nack.getInterest().hasLink()) {
102 nack.getInterest().getLink();
103 }
104 }
105 catch (const tlv::Error&) {
106 NFD_LOG_DEBUG("startProcessNack face=" << face.getId() <<
107 " nack=" << nack.getInterest().getName() <<
108 "~" << nack.getReason() << " malformed");
109 return;
110 }
111
112 this->onIncomingNack(face, nack);
113}
114
115void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700116Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
117{
118 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700119 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
120 " interest=" << interest.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000121 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700122 ++m_counters.nInInterests;
Junxiao Shic041ca32014-02-25 20:01:15 -0700123
Junxiao Shi88884492014-02-15 15:57:43 -0700124 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700125 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700126 scope_prefix::LOCALHOST.isPrefixOf(interest.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700127 if (isViolatingLocalhost) {
128 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
129 " interest=" << interest.getName() << " violates /localhost");
130 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700131 return;
132 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700133
Junxiao Shi330136a2016-03-10 04:53:08 -0700134 // detect duplicate Nonce with Dead Nonce List
135 bool hasDuplicateNonceInDnl = m_deadNonceList.has(interest.getName(), interest.getNonce());
136 if (hasDuplicateNonceInDnl) {
137 // goto Interest loop pipeline
138 this->onInterestLoop(inFace, interest);
139 return;
140 }
141
Junxiao Shid3c792f2014-01-30 00:46:13 -0700142 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -0700143 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -0700144
Junxiao Shi330136a2016-03-10 04:53:08 -0700145 // detect duplicate Nonce in PIT entry
Junxiao Shifef73e42016-03-29 14:15:05 -0700146 bool hasDuplicateNonceInPit = fw::findDuplicateNonce(*pitEntry, interest.getNonce(), inFace) !=
147 fw::DUPLICATE_NONCE_NONE;
Junxiao Shi330136a2016-03-10 04:53:08 -0700148 if (hasDuplicateNonceInPit) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700149 // goto Interest loop pipeline
Junxiao Shi330136a2016-03-10 04:53:08 -0700150 this->onInterestLoop(inFace, interest);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700151 return;
152 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700153
Junxiao Shid3c792f2014-01-30 00:46:13 -0700154 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000155 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700156
Junxiao Shif3c07812014-03-11 21:48:49 -0700157 // is pending?
Junxiao Shi4846f372016-04-05 13:39:30 -0700158 if (!pitEntry->hasInRecords()) {
mzhang4eab72492015-02-25 11:16:09 -0600159 m_cs.find(interest,
160 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
161 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700162 }
mzhang4eab72492015-02-25 11:16:09 -0600163 else {
164 this->onContentStoreMiss(inFace, pitEntry, interest);
165 }
166}
Junxiao Shic041ca32014-02-25 20:01:15 -0700167
mzhang4eab72492015-02-25 11:16:09 -0600168void
Junxiao Shi330136a2016-03-10 04:53:08 -0700169Forwarder::onInterestLoop(Face& inFace, const Interest& interest)
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700170{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700171 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700172 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700173 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
174 " interest=" << interest.getName() <<
175 " drop");
176 return;
177 }
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700178
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700179 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
180 " interest=" << interest.getName() <<
181 " send-Nack-duplicate");
182
183 // send Nack with reason=DUPLICATE
184 // note: Don't enter outgoing Nack pipeline because it needs an in-record.
185 lp::Nack nack(interest);
186 nack.setReason(lp::NackReason::DUPLICATE);
187 inFace.sendNack(nack);
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700188}
189
190void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000191Forwarder::onContentStoreMiss(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
mzhang4eab72492015-02-25 11:16:09 -0600192 const Interest& interest)
193{
194 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
195
Junxiao Shi4846f372016-04-05 13:39:30 -0700196 // insert in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000197 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700198
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700199 // set PIT unsatisfy timer
200 this->setUnsatisfyTimer(pitEntry);
201
Junxiao Shie342e8d2016-09-18 16:48:00 +0000202 // has NextHopFaceId?
203 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
204 if (nextHopTag != nullptr) {
205 // chosen NextHop face exists?
206 Face* nextHopFace = m_faceTable.get(*nextHopTag);
207 if (nextHopFace != nullptr) {
Junxiao Shic5f651f2016-11-17 22:58:12 +0000208 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName() << " nexthop-faceid=" << nextHopFace->getId());
Junxiao Shie342e8d2016-09-18 16:48:00 +0000209 // go to outgoing Interest pipeline
Junxiao Shic5f651f2016-11-17 22:58:12 +0000210 // scope control is unnecessary, because privileged app explicitly wants to forward
211 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000212 }
213 return;
214 }
215
Junxiao Shi05cc50a2016-07-11 22:38:21 +0000216 // dispatch to strategy: after incoming Interest
Junxiao Shib9420cf2016-08-13 04:38:52 +0000217 this->dispatchToStrategy(*pitEntry,
218 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
Junxiao Shid3c792f2014-01-30 00:46:13 -0700219}
220
221void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000222Forwarder::onContentStoreHit(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
223 const Interest& interest, const Data& data)
mzhang4eab72492015-02-25 11:16:09 -0600224{
Vince Lehmanfaa5c0c2015-08-18 12:52:46 -0500225 NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName());
mzhang4eab72492015-02-25 11:16:09 -0600226
Junxiao Shicde37ad2015-12-24 01:02:05 -0700227 data.setTag(make_shared<lp::IncomingFaceIdTag>(face::FACEID_CONTENT_STORE));
mzhang4eab72492015-02-25 11:16:09 -0600228 // XXX should we lookup PIT for other Interests that also match csMatch?
229
230 // set PIT straggler timer
231 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
232
233 // goto outgoing Data pipeline
234 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
235}
236
Junxiao Shid3c792f2014-01-30 00:46:13 -0700237void
Junxiao Shic5f651f2016-11-17 22:58:12 +0000238Forwarder::onOutgoingInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace, const Interest& interest)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700239{
Junxiao Shif3c07812014-03-11 21:48:49 -0700240 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
241 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700242
Junxiao Shi4846f372016-04-05 13:39:30 -0700243 // insert out-record
Junxiao Shic5f651f2016-11-17 22:58:12 +0000244 pitEntry->insertOrUpdateOutRecord(outFace, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700245
Junxiao Shid3c792f2014-01-30 00:46:13 -0700246 // send Interest
Junxiao Shic5f651f2016-11-17 22:58:12 +0000247 outFace.sendInterest(interest);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700248 ++m_counters.nOutInterests;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700249}
250
251void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000252Forwarder::onInterestReject(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700253{
Junxiao Shifef73e42016-03-29 14:15:05 -0700254 if (fw::hasPendingOutRecords(*pitEntry)) {
Junxiao Shid938a6b2014-05-11 23:40:29 -0700255 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
256 " cannot reject forwarded Interest");
257 return;
258 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700259 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700260
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700261 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000262 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700263
Junxiao Shid3c792f2014-01-30 00:46:13 -0700264 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700265 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700266}
267
268void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000269Forwarder::onInterestUnsatisfied(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700270{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700271 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
272
Junxiao Shid3c792f2014-01-30 00:46:13 -0700273 // invoke PIT unsatisfied callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000274 this->dispatchToStrategy(*pitEntry,
275 [&] (fw::Strategy& strategy) { strategy.beforeExpirePendingInterest(pitEntry); });
Junxiao Shic041ca32014-02-25 20:01:15 -0700276
Junxiao Shia110f262014-10-12 12:35:20 -0700277 // goto Interest Finalize pipeline
278 this->onInterestFinalize(pitEntry, false);
279}
280
281void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000282Forwarder::onInterestFinalize(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
283 time::milliseconds dataFreshnessPeriod)
Junxiao Shia110f262014-10-12 12:35:20 -0700284{
285 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
286 (isSatisfied ? " satisfied" : " unsatisfied"));
287
288 // Dead Nonce List insert if necessary
289 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
290
Junxiao Shif3c07812014-03-11 21:48:49 -0700291 // PIT delete
Junxiao Shib9420cf2016-08-13 04:38:52 +0000292 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000293 m_pit.erase(pitEntry.get());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700294}
295
296void
297Forwarder::onIncomingData(Face& inFace, const Data& data)
298{
299 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700300 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000301 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700302 ++m_counters.nInData;
Junxiao Shic041ca32014-02-25 20:01:15 -0700303
Junxiao Shi88884492014-02-15 15:57:43 -0700304 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700305 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700306 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700307 if (isViolatingLocalhost) {
308 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
309 " data=" << data.getName() << " violates /localhost");
310 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700311 return;
312 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700313
Junxiao Shid3c792f2014-01-30 00:46:13 -0700314 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700315 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
316 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700317 // goto Data unsolicited pipeline
318 this->onDataUnsolicited(inFace, data);
319 return;
320 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700321
Junxiao Shid3c792f2014-01-30 00:46:13 -0700322 // CS insert
323 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700324
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700325 std::set<Face*> pendingDownstreams;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700326 // foreach PitEntry
Junxiao Shi4846f372016-04-05 13:39:30 -0700327 auto now = time::steady_clock::now();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700328 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700329 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700330
Junxiao Shid3c792f2014-01-30 00:46:13 -0700331 // cancel unsatisfy & straggler timer
Junxiao Shib9420cf2016-08-13 04:38:52 +0000332 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700333
Junxiao Shid3c792f2014-01-30 00:46:13 -0700334 // remember pending downstreams
Junxiao Shi4846f372016-04-05 13:39:30 -0700335 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
336 if (inRecord.getExpiry() > now) {
Junxiao Shi9cff7792016-08-01 21:45:11 +0000337 pendingDownstreams.insert(&inRecord.getFace());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700338 }
339 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700340
Junxiao Shid938a6b2014-05-11 23:40:29 -0700341 // invoke PIT satisfy callback
Junxiao Shib9420cf2016-08-13 04:38:52 +0000342 this->dispatchToStrategy(*pitEntry,
343 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
Junxiao Shid938a6b2014-05-11 23:40:29 -0700344
Junxiao Shi4846f372016-04-05 13:39:30 -0700345 // Dead Nonce List insert if necessary (for out-record of inFace)
Junxiao Shia110f262014-10-12 12:35:20 -0700346 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
347
Junxiao Shid3c792f2014-01-30 00:46:13 -0700348 // mark PIT satisfied
Junxiao Shi4846f372016-04-05 13:39:30 -0700349 pitEntry->clearInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700350 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700351
Junxiao Shid3c792f2014-01-30 00:46:13 -0700352 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700353 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700354 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700355
Junxiao Shid3c792f2014-01-30 00:46:13 -0700356 // foreach pending downstream
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700357 for (Face* pendingDownstream : pendingDownstreams) {
358 if (pendingDownstream == &inFace) {
Junxiao Shida006f52014-05-16 11:18:00 -0700359 continue;
360 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700361 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700362 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700363 }
364}
365
366void
367Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
368{
369 // accept to cache?
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000370 fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
371 if (decision == fw::UnsolicitedDataDecision::CACHE) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700372 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700373 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700374 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700375
Junxiao Shif3c07812014-03-11 21:48:49 -0700376 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
377 " data=" << data.getName() <<
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000378 " decision=" << decision);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700379}
380
381void
382Forwarder::onOutgoingData(const Data& data, Face& outFace)
383{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700384 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi223271b2014-07-03 22:06:13 -0700385 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
386 return;
387 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700388 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
389
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700390 // /localhost scope control
Junxiao Shicde37ad2015-12-24 01:02:05 -0700391 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shif3410d82016-04-05 13:49:44 -0700392 scope_prefix::LOCALHOST.isPrefixOf(data.getName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700393 if (isViolatingLocalhost) {
394 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
395 " data=" << data.getName() << " violates /localhost");
396 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700397 return;
398 }
399
Junxiao Shif3c07812014-03-11 21:48:49 -0700400 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700401
Junxiao Shid3c792f2014-01-30 00:46:13 -0700402 // send Data
403 outFace.sendData(data);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700404 ++m_counters.nOutData;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700405}
406
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700407void
408Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack)
409{
Junxiao Shi0de23a22015-12-03 20:07:02 +0000410 // receive Nack
411 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700412 ++m_counters.nInNacks;
413
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700414 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700415 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700416 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
417 " nack=" << nack.getInterest().getName() <<
418 "~" << nack.getReason() << " face-is-multi-access");
419 return;
420 }
421
422 // PIT match
423 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
424 // if no PIT entry found, drop
425 if (pitEntry == nullptr) {
426 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
427 " nack=" << nack.getInterest().getName() <<
428 "~" << nack.getReason() << " no-PIT-entry");
429 return;
430 }
431
432 // has out-record?
433 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
434 // if no out-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700435 if (outRecord == pitEntry->out_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700436 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
437 " nack=" << nack.getInterest().getName() <<
438 "~" << nack.getReason() << " no-out-record");
439 return;
440 }
441
442 // if out-record has different Nonce, drop
443 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
444 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
445 " nack=" << nack.getInterest().getName() <<
446 "~" << nack.getReason() << " wrong-Nonce " <<
447 nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce());
448 return;
449 }
450
451 NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() <<
452 " nack=" << nack.getInterest().getName() <<
453 "~" << nack.getReason() << " OK");
454
455 // record Nack on out-record
456 outRecord->setIncomingNack(nack);
457
458 // trigger strategy: after receive NACK
Junxiao Shib9420cf2016-08-13 04:38:52 +0000459 this->dispatchToStrategy(*pitEntry,
460 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700461}
462
463void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000464Forwarder::onOutgoingNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700465 const lp::NackHeader& nack)
466{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700467 if (outFace.getId() == face::INVALID_FACEID) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700468 NFD_LOG_WARN("onOutgoingNack face=invalid" <<
469 " nack=" << pitEntry->getInterest().getName() <<
470 "~" << nack.getReason() << " no-in-record");
471 return;
472 }
473
474 // has in-record?
Junxiao Shi4846f372016-04-05 13:39:30 -0700475 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(outFace);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700476
477 // if no in-record found, drop
Junxiao Shi4846f372016-04-05 13:39:30 -0700478 if (inRecord == pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700479 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
480 " nack=" << pitEntry->getInterest().getName() <<
481 "~" << nack.getReason() << " no-in-record");
482 return;
483 }
484
485 // if multi-access face, drop
Junxiao Shicde37ad2015-12-24 01:02:05 -0700486 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700487 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
488 " nack=" << pitEntry->getInterest().getName() <<
489 "~" << nack.getReason() << " face-is-multi-access");
490 return;
491 }
492
493 NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() <<
494 " nack=" << pitEntry->getInterest().getName() <<
495 "~" << nack.getReason() << " OK");
496
497 // create Nack packet with the Interest from in-record
498 lp::Nack nackPkt(inRecord->getInterest());
499 nackPkt.setHeader(nack);
500
501 // erase in-record
502 pitEntry->deleteInRecord(outFace);
503
504 // send Nack on face
505 const_cast<Face&>(outFace).sendNack(nackPkt);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700506 ++m_counters.nOutNacks;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700507}
508
Junxiao Shid3c792f2014-01-30 00:46:13 -0700509static inline bool
510compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
511{
512 return a.getExpiry() < b.getExpiry();
513}
514
515void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000516Forwarder::setUnsatisfyTimer(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700517{
Junxiao Shi4846f372016-04-05 13:39:30 -0700518 pit::InRecordCollection::iterator lastExpiring =
519 std::max_element(pitEntry->in_begin(), pitEntry->in_end(), &compare_InRecord_expiry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700520
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700521 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
Junxiao Shi4846f372016-04-05 13:39:30 -0700522 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
523 if (lastExpiryFromNow <= time::seconds::zero()) {
524 // TODO all in-records are already expired; will this happen?
Junxiao Shid3c792f2014-01-30 00:46:13 -0700525 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700526
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700527 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700528 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700529 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
530}
531
532void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000533Forwarder::setStragglerTimer(const shared_ptr<pit::Entry>& pitEntry, bool isSatisfied,
534 time::milliseconds dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700535{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700536 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700537
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700538 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700539 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700540 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700541}
542
543void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000544Forwarder::cancelUnsatisfyAndStragglerTimer(pit::Entry& pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700545{
Junxiao Shib9420cf2016-08-13 04:38:52 +0000546 scheduler::cancel(pitEntry.m_unsatisfyTimer);
547 scheduler::cancel(pitEntry.m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700548}
549
Junxiao Shia110f262014-10-12 12:35:20 -0700550static inline void
551insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
552 const pit::OutRecord& outRecord)
553{
554 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
555}
556
557void
558Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
Junxiao Shib9420cf2016-08-13 04:38:52 +0000559 time::milliseconds dataFreshnessPeriod, Face* upstream)
Junxiao Shia110f262014-10-12 12:35:20 -0700560{
561 // need Dead Nonce List insert?
562 bool needDnl = false;
563 if (isSatisfied) {
564 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
565 // Data never becomes stale if it doesn't have FreshnessPeriod field
566 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
567 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
568 }
569 else {
570 needDnl = true;
571 }
572
573 if (!needDnl) {
574 return;
575 }
576
577 // Dead Nonce List insert
578 if (upstream == 0) {
579 // insert all outgoing Nonces
580 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
581 std::for_each(outRecords.begin(), outRecords.end(),
582 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
583 }
584 else {
585 // insert outgoing Nonce of a specific face
Junxiao Shi4846f372016-04-05 13:39:30 -0700586 pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700587 if (outRecord != pitEntry.getOutRecords().end()) {
588 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
589 }
590 }
591}
592
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800593} // namespace nfd