32 #include <ndn-cxx/lp/tags.hpp> 48 , m_measurements(m_nameTree)
49 , m_strategyChoice(*this)
51 m_faceTable.
afterAdd.connect([
this] (Face& face) {
52 face.afterReceiveInterest.connect(
53 [
this, &face] (
const Interest& interest) {
56 face.afterReceiveData.connect(
57 [
this, &face] (
const Data& data) {
60 face.afterReceiveNack.connect(
61 [
this, &face] (
const lp::Nack& nack) {
64 face.onDroppedInterest.connect(
65 [
this, &face] (
const Interest& interest) {
66 this->onDroppedInterest(face, interest);
80 Forwarder::onIncomingInterest(Face& inFace,
const Interest& interest)
84 " interest=" << interest.getName());
85 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
89 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
91 if (isViolatingLocalhost) {
93 " interest=" << interest.getName() <<
" violates /localhost");
99 bool hasDuplicateNonceInDnl = m_deadNonceList.
has(interest.getName(), interest.getNonce());
100 if (hasDuplicateNonceInDnl) {
102 this->onInterestLoop(inFace, interest);
107 if (!interest.getForwardingHint().empty() &&
109 NFD_LOG_DEBUG(
"onIncomingInterest face=" << inFace.getId() <<
110 " interest=" << interest.getName() <<
" reaching-producer-region");
111 const_cast<Interest&
>(interest).setForwardingHint({});
115 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
120 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
124 if (hasDuplicateNonceInPit) {
126 this->onInterestLoop(inFace, interest);
131 if (!pitEntry->hasInRecords()) {
133 bind(&Forwarder::onContentStoreHit,
this, ref(inFace), pitEntry, _1, _2),
134 bind(&Forwarder::onContentStoreMiss,
this, ref(inFace), pitEntry, _1));
137 this->onContentStoreMiss(inFace, pitEntry, interest);
142 Forwarder::onInterestLoop(Face& inFace,
const Interest& interest)
145 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
147 " interest=" << interest.getName() <<
153 " interest=" << interest.getName() <<
154 " send-Nack-duplicate");
158 lp::Nack nack(interest);
159 nack.setReason(lp::NackReason::DUPLICATE);
160 inFace.sendNack(nack);
170 Forwarder::onContentStoreMiss(
const Face& inFace,
const shared_ptr<pit::Entry>& pitEntry,
171 const Interest& interest)
173 NFD_LOG_DEBUG(
"onContentStoreMiss interest=" << interest.getName());
177 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
181 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
182 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
185 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
186 if (nextHopTag !=
nullptr) {
188 Face* nextHopFace = m_faceTable.
get(*nextHopTag);
189 if (nextHopFace !=
nullptr) {
190 NFD_LOG_DEBUG(
"onContentStoreMiss interest=" << interest.getName() <<
" nexthop-faceid=" << nextHopFace->getId());
193 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
199 this->dispatchToStrategy(*pitEntry,
204 Forwarder::onContentStoreHit(
const Face& inFace,
const shared_ptr<pit::Entry>& pitEntry,
205 const Interest& interest,
const Data& data)
207 NFD_LOG_DEBUG(
"onContentStoreHit interest=" << interest.getName());
213 pitEntry->isSatisfied =
true;
214 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
217 this->setExpiryTimer(pitEntry, 0_ms);
220 this->dispatchToStrategy(*pitEntry,
225 Forwarder::onOutgoingInterest(
const shared_ptr<pit::Entry>& pitEntry, Face& outFace,
const Interest& interest)
227 NFD_LOG_DEBUG(
"onOutgoingInterest face=" << outFace.getId() <<
228 " interest=" << pitEntry->getName());
231 pitEntry->insertOrUpdateOutRecord(outFace, interest);
234 outFace.sendInterest(interest);
239 Forwarder::onInterestFinalize(
const shared_ptr<pit::Entry>& pitEntry)
241 NFD_LOG_DEBUG(
"onInterestFinalize interest=" << pitEntry->getName() <<
242 (pitEntry->isSatisfied ?
" satisfied" :
" unsatisfied"));
245 this->insertDeadNonceList(*pitEntry, 0);
249 m_pit.erase(pitEntry.get());
253 Forwarder::onIncomingData(Face& inFace,
const Data& data)
256 NFD_LOG_DEBUG(
"onIncomingData face=" << inFace.getId() <<
" data=" << data.getName());
257 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
261 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
263 if (isViolatingLocalhost) {
265 " data=" << data.getName() <<
" violates /localhost");
272 if (pitMatches.size() == 0) {
274 this->onDataUnsolicited(inFace, data);
282 if (pitMatches.size() == 1) {
283 auto& pitEntry = pitMatches.front();
285 NFD_LOG_DEBUG(
"onIncomingData matching=" << pitEntry->getName());
288 this->setExpiryTimer(pitEntry, 0_ms);
291 this->dispatchToStrategy(*pitEntry,
295 pitEntry->isSatisfied =
true;
296 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
299 this->insertDeadNonceList(*pitEntry, &inFace);
302 pitEntry->deleteOutRecord(inFace);
307 std::set<Face*> pendingDownstreams;
308 auto now = time::steady_clock::now();
310 for (
const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
311 NFD_LOG_DEBUG(
"onIncomingData matching=" << pitEntry->getName());
314 for (
const pit::InRecord& inRecord : pitEntry->getInRecords()) {
315 if (inRecord.getExpiry() > now) {
316 pendingDownstreams.insert(&inRecord.getFace());
321 this->setExpiryTimer(pitEntry, 0_ms);
324 this->dispatchToStrategy(*pitEntry,
328 pitEntry->isSatisfied =
true;
329 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
332 this->insertDeadNonceList(*pitEntry, &inFace);
335 pitEntry->clearInRecords();
336 pitEntry->deleteOutRecord(inFace);
340 for (Face* pendingDownstream : pendingDownstreams) {
341 if (pendingDownstream->getId() == inFace.getId() &&
342 pendingDownstream->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
346 this->onOutgoingData(data, *pendingDownstream);
352 Forwarder::onDataUnsolicited(Face& inFace,
const Data& data)
358 m_cs.insert(data,
true);
362 " data=" << data.getName() <<
363 " decision=" << decision);
367 Forwarder::onOutgoingData(
const Data& data, Face& outFace)
370 NFD_LOG_WARN(
"onOutgoingData face=invalid data=" << data.getName());
373 NFD_LOG_DEBUG(
"onOutgoingData face=" << outFace.getId() <<
" data=" << data.getName());
376 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
378 if (isViolatingLocalhost) {
380 " data=" << data.getName() <<
" violates /localhost");
388 outFace.sendData(data);
393 Forwarder::onIncomingNack(Face& inFace,
const lp::Nack& nack)
396 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
400 if (inFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
402 " nack=" << nack.getInterest().getName() <<
403 "~" << nack.getReason() <<
" face-is-multi-access");
408 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
410 if (pitEntry ==
nullptr) {
412 " nack=" << nack.getInterest().getName() <<
413 "~" << nack.getReason() <<
" no-PIT-entry");
420 if (outRecord == pitEntry->out_end()) {
422 " nack=" << nack.getInterest().getName() <<
423 "~" << nack.getReason() <<
" no-out-record");
428 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
430 " nack=" << nack.getInterest().getName() <<
431 "~" << nack.getReason() <<
" wrong-Nonce " <<
432 nack.getInterest().getNonce() <<
"!=" << outRecord->getLastNonce());
437 " nack=" << nack.getInterest().getName() <<
438 "~" << nack.getReason() <<
" OK");
441 outRecord->setIncomingNack(nack);
445 this->setExpiryTimer(pitEntry, 0_ms);
449 this->dispatchToStrategy(*pitEntry,
454 Forwarder::onOutgoingNack(
const shared_ptr<pit::Entry>& pitEntry,
const Face& outFace,
455 const lp::NackHeader& nack)
459 " nack=" << pitEntry->getInterest().getName() <<
460 "~" << nack.getReason() <<
" no-in-record");
468 if (inRecord == pitEntry->in_end()) {
470 " nack=" << pitEntry->getInterest().getName() <<
471 "~" << nack.getReason() <<
" no-in-record");
476 if (outFace.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
478 " nack=" << pitEntry->getInterest().getName() <<
479 "~" << nack.getReason() <<
" face-is-multi-access");
484 " nack=" << pitEntry->getInterest().getName() <<
485 "~" << nack.getReason() <<
" OK");
488 lp::Nack nackPkt(inRecord->getInterest());
489 nackPkt.setHeader(nack);
492 pitEntry->deleteInRecord(outFace);
495 const_cast<Face&
>(outFace).sendNack(nackPkt);
500 Forwarder::onDroppedInterest(Face& outFace,
const Interest& interest)
502 m_strategyChoice.findEffectiveStrategy(interest.getName()).onDroppedInterest(outFace, interest);
506 Forwarder::setExpiryTimer(
const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
508 BOOST_ASSERT(duration >= 0_ms);
513 bind(&Forwarder::onInterestFinalize,
this, pitEntry));
524 Forwarder::insertDeadNonceList(
pit::Entry& pitEntry, Face* upstream)
530 needDnl =
static_cast<bool>(pitEntry.
getInterest().getMustBeFresh()) &&
539 if (upstream ==
nullptr) {
542 std::for_each(outRecords.begin(), outRecords.end(),
549 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
#define NFD_LOG_DEBUG(expression)
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
contains information about an Interest toward an outgoing face
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
#define NFD_LOG_WARN(expression)
static void insertNonceToDnl(DeadNonceList &dnl, const pit::Entry &pitEntry, const pit::OutRecord &outRecord)
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
represents the Dead Nonce list
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
std::list< OutRecord > OutRecordCollection
an unordered collection of out-records
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
uint32_t getLastNonce() const
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