34 #include <ndn-cxx/lp/tags.hpp> 50 , m_measurements(m_nameTree)
51 , m_strategyChoice(*this)
53 m_faceTable.
afterAdd.connect([
this] (Face& face) {
54 face.afterReceiveInterest.connect(
55 [
this, &face] (
const Interest& interest) {
58 face.afterReceiveData.connect(
59 [
this, &face] (
const Data& data) {
62 face.afterReceiveNack.connect(
63 [
this, &face] (
const lp::Nack& nack) {
66 face.onDroppedInterest.connect(
67 [
this, &face] (
const Interest& interest) {
68 this->onDroppedInterest(face, interest);
82 Forwarder::onIncomingInterest(Face& inFace,
const Interest& interest)
86 " interest=" << interest.getName());
87 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
91 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
93 if (isViolatingLocalhost) {
95 " interest=" << interest.getName() <<
" violates /localhost");
101 bool hasDuplicateNonceInDnl = m_deadNonceList.
has(interest.getName(), interest.getNonce());
102 if (hasDuplicateNonceInDnl) {
104 this->onInterestLoop(inFace, interest);
109 if (!interest.getForwardingHint().empty() &&
111 NFD_LOG_DEBUG(
"onIncomingInterest face=" << inFace.getId() <<
112 " interest=" << interest.getName() <<
" reaching-producer-region");
113 const_cast<Interest&
>(interest).setForwardingHint({});
117 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
122 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
126 if (hasDuplicateNonceInPit) {
128 this->onInterestLoop(inFace, interest);
133 if (!pitEntry->hasInRecords()) {
135 bind(&Forwarder::onContentStoreHit,
this, std::ref(inFace), pitEntry, _1, _2),
136 bind(&Forwarder::onContentStoreMiss,
this, std::ref(inFace), pitEntry, _1));
139 this->onContentStoreMiss(inFace, pitEntry, interest);
144 Forwarder::onInterestLoop(Face& inFace,
const Interest& interest)
147 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
149 " interest=" << interest.getName() <<
155 " interest=" << interest.getName() <<
156 " send-Nack-duplicate");
160 lp::Nack nack(interest);
161 nack.setReason(lp::NackReason::DUPLICATE);
162 inFace.sendNack(nack);
172 Forwarder::onContentStoreMiss(
const Face& inFace,
const shared_ptr<pit::Entry>& pitEntry,
173 const Interest& interest)
175 NFD_LOG_DEBUG(
"onContentStoreMiss interest=" << interest.getName());
179 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
183 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
184 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
187 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
188 if (nextHopTag !=
nullptr) {
190 Face* nextHopFace = m_faceTable.
get(*nextHopTag);
191 if (nextHopFace !=
nullptr) {
192 NFD_LOG_DEBUG(
"onContentStoreMiss interest=" << interest.getName() <<
" nexthop-faceid=" << nextHopFace->getId());
195 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
201 this->dispatchToStrategy(*pitEntry,
206 Forwarder::onContentStoreHit(
const Face& inFace,
const shared_ptr<pit::Entry>& pitEntry,
207 const Interest& interest,
const Data& data)
209 NFD_LOG_DEBUG(
"onContentStoreHit interest=" << interest.getName());
215 pitEntry->isSatisfied =
true;
216 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
219 this->setExpiryTimer(pitEntry, 0_ms);
222 this->dispatchToStrategy(*pitEntry,
227 Forwarder::onOutgoingInterest(
const shared_ptr<pit::Entry>& pitEntry, Face& outFace,
const Interest& interest)
229 NFD_LOG_DEBUG(
"onOutgoingInterest face=" << outFace.getId() <<
230 " interest=" << pitEntry->getName());
233 pitEntry->insertOrUpdateOutRecord(outFace, interest);
236 outFace.sendInterest(interest);
241 Forwarder::onInterestFinalize(
const shared_ptr<pit::Entry>& pitEntry)
243 NFD_LOG_DEBUG(
"onInterestFinalize interest=" << pitEntry->getName() <<
244 (pitEntry->isSatisfied ?
" satisfied" :
" unsatisfied"));
247 this->insertDeadNonceList(*pitEntry, 0);
251 m_pit.erase(pitEntry.get());
255 Forwarder::onIncomingData(Face& inFace,
const Data& data)
258 NFD_LOG_DEBUG(
"onIncomingData face=" << inFace.getId() <<
" data=" << data.getName());
259 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
263 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
265 if (isViolatingLocalhost) {
267 " data=" << data.getName() <<
" violates /localhost");
274 if (pitMatches.size() == 0) {
276 this->onDataUnsolicited(inFace, data);
284 if (pitMatches.size() == 1) {
285 auto& pitEntry = pitMatches.front();
287 NFD_LOG_DEBUG(
"onIncomingData matching=" << pitEntry->getName());
290 this->setExpiryTimer(pitEntry, 0_ms);
293 this->dispatchToStrategy(*pitEntry,
297 pitEntry->isSatisfied =
true;
298 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
301 this->insertDeadNonceList(*pitEntry, &inFace);
304 pitEntry->deleteOutRecord(inFace);
309 std::set<Face*> pendingDownstreams;
310 auto now = time::steady_clock::now();
312 for (
const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
313 NFD_LOG_DEBUG(
"onIncomingData matching=" << pitEntry->getName());
316 for (
const pit::InRecord& inRecord : pitEntry->getInRecords()) {
317 if (inRecord.getExpiry() > now) {
318 pendingDownstreams.insert(&inRecord.getFace());
323 this->setExpiryTimer(pitEntry, 0_ms);
326 this->dispatchToStrategy(*pitEntry,
330 pitEntry->isSatisfied =
true;
331 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
334 this->insertDeadNonceList(*pitEntry, &inFace);
337 pitEntry->clearInRecords();
338 pitEntry->deleteOutRecord(inFace);
342 for (Face* pendingDownstream : pendingDownstreams) {
343 if (pendingDownstream->getId() == inFace.getId() &&
344 pendingDownstream->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
348 this->onOutgoingData(data, *pendingDownstream);
354 Forwarder::onDataUnsolicited(Face& inFace,
const Data& data)
360 m_cs.insert(data,
true);
364 " data=" << data.getName() <<
365 " decision=" << decision);
369 Forwarder::onOutgoingData(
const Data& data, Face& outFace)
372 NFD_LOG_WARN(
"onOutgoingData face=invalid data=" << data.getName());
375 NFD_LOG_DEBUG(
"onOutgoingData face=" << outFace.getId() <<
" data=" << data.getName());
378 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
380 if (isViolatingLocalhost) {
382 " data=" << data.getName() <<
" violates /localhost");
390 outFace.sendData(data);
395 Forwarder::onIncomingNack(Face& inFace,
const lp::Nack& nack)
398 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
402 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
404 " nack=" << nack.getInterest().getName() <<
405 "~" << nack.getReason() <<
" face-is-multi-access");
410 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
412 if (pitEntry ==
nullptr) {
414 " nack=" << nack.getInterest().getName() <<
415 "~" << nack.getReason() <<
" no-PIT-entry");
422 if (outRecord == pitEntry->out_end()) {
424 " nack=" << nack.getInterest().getName() <<
425 "~" << nack.getReason() <<
" no-out-record");
430 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
432 " nack=" << nack.getInterest().getName() <<
433 "~" << nack.getReason() <<
" wrong-Nonce " <<
434 nack.getInterest().getNonce() <<
"!=" << outRecord->getLastNonce());
439 " nack=" << nack.getInterest().getName() <<
440 "~" << nack.getReason() <<
" OK");
443 outRecord->setIncomingNack(nack);
447 this->setExpiryTimer(pitEntry, 0_ms);
451 this->dispatchToStrategy(*pitEntry,
456 Forwarder::onOutgoingNack(
const shared_ptr<pit::Entry>& pitEntry,
const Face& outFace,
457 const lp::NackHeader& nack)
461 " nack=" << pitEntry->getInterest().getName() <<
462 "~" << nack.getReason() <<
" no-in-record");
470 if (inRecord == pitEntry->in_end()) {
472 " nack=" << pitEntry->getInterest().getName() <<
473 "~" << nack.getReason() <<
" no-in-record");
478 if (outFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
480 " nack=" << pitEntry->getInterest().getName() <<
481 "~" << nack.getReason() <<
" face-is-multi-access");
486 " nack=" << pitEntry->getInterest().getName() <<
487 "~" << nack.getReason() <<
" OK");
490 lp::Nack nackPkt(inRecord->getInterest());
491 nackPkt.setHeader(nack);
494 pitEntry->deleteInRecord(outFace);
497 const_cast<Face&
>(outFace).sendNack(nackPkt);
502 Forwarder::onDroppedInterest(Face& outFace,
const Interest& interest)
504 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(outFace, interest);
508 Forwarder::setExpiryTimer(
const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
510 BOOST_ASSERT(duration >= 0_ms);
514 pitEntry->expiryTimer =
scheduler::schedule(duration, [=] { onInterestFinalize(pitEntry); });
518 Forwarder::insertDeadNonceList(
pit::Entry& pitEntry, Face* upstream)
524 needDnl =
static_cast<bool>(pitEntry.
getInterest().getMustBeFresh()) &&
533 if (upstream ==
nullptr) {
536 std::for_each(outRecords.begin(), outRecords.end(), [&] (
const auto& outRecord) {
537 m_deadNonceList.
add(pitEntry.
getName(), outRecord.getLastNonce());
544 m_deadNonceList.
add(pitEntry.
getName(), outRecord->getLastNonce());
bool isSatisfied
indicate if PIT entry is satisfied
OutRecordCollection::iterator getOutRecord(const Face &face)
get the out-record for face
void cleanupOnFaceRemoval(NameTree &nt, Fib &fib, Pit &pit, const Face &face)
cleanup tables when a face is destroyed
PacketCounter nInInterests
void cancel(const EventId &eventId)
Cancel a scheduled event.
virtual void afterReceiveInterest(const Face &inFace, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry)=0
trigger after Interest is received
virtual void afterReceiveData(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data)
trigger after Data is received
static const Name & getStrategyName()
time::steady_clock::TimePoint getExpiry() const
gives the time point this record expires
virtual void beforeSatisfyInterest(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data)
trigger before PIT entry is satisfied
Face * get(FaceId id) const
get face by FaceId
const time::nanoseconds & getLifetime() const
void add(const Name &name, uint32_t nonce)
records name+nonce
static bool compare_InRecord_expiry(const pit::InRecord &a, const pit::InRecord &b)
PacketCounter nOutInterests
bool has(const Name &name, uint32_t nonce) const
determines if name+nonce exists
DropAllUnsolicitedDataPolicy DefaultUnsolicitedDataPolicy
the default UnsolicitedDataPolicy
Table::const_iterator iterator
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
contains information about an Interest from an incoming face
virtual void afterReceiveNack(const Face &inFace, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry)
trigger after Nack is received
signal::Signal< FaceTable, Face & > beforeRemove
fires before a face is removed
void startProcessInterest(Face &face, const Interest &interest)
start incoming Interest processing
signal::Signal< FaceTable, Face & > afterAdd
fires after a face is added
bool hasPendingOutRecords(const pit::Entry &pitEntry)
determine whether pitEntry has any pending out-records
const Interest & getInterest() const
const Name LOCALHOST
ndn:/localhost
no duplicate Nonce is found
int findDuplicateNonce(const pit::Entry &pitEntry, uint32_t nonce, const Face &face)
determine whether pitEntry has duplicate Nonce nonce
virtual void afterContentStoreHit(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data)
trigger after a Data is matched in CS
static Name getDefaultStrategyName()
represents a forwarding strategy
This file contains common algorithms used by forwarding strategies.
#define NFD_LOG_INIT(name)
UnsolicitedDataDecision
a decision made by UnsolicitedDataPolicy
EventId schedule(time::nanoseconds after, const EventCallback &event)
Schedule an event.
the Data should be cached in the ContentStore
bool isInProducerRegion(const DelegationList &forwardingHint) const
determines whether an Interest has reached a producer region
An unordered iterable of all PIT entries matching Data.
time::milliseconds dataFreshnessPeriod
Data freshness period.
const OutRecordCollection & getOutRecords() const
void startProcessData(Face &face, const Data &data)
start incoming Data processing
const FaceId FACEID_CONTENT_STORE
identifies a packet comes from the ContentStore
const FaceId INVALID_FACEID
indicates an invalid FaceId
const Name & getName() const
void startProcessNack(Face &face, const lp::Nack &nack)
start incoming Nack processing