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) {
80 if (interest.hasLink()) {
84 catch (
const tlv::Error&) {
86 " interest=" << interest.getName() <<
" malformed");
91 this->onIncomingInterest(face, interest);
100 this->onIncomingData(face, data);
108 if (nack.getInterest().hasLink()) {
109 nack.getInterest().getLink();
112 catch (
const tlv::Error&) {
114 " nack=" << nack.getInterest().getName() <<
115 "~" << nack.getReason() <<
" malformed");
119 this->onIncomingNack(face, nack);
123 Forwarder::onIncomingInterest(Face& inFace,
const Interest& interest)
126 NFD_LOG_DEBUG(
"onIncomingInterest face=" << inFace.getId() <<
127 " interest=" << interest.getName());
128 interest.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
132 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
134 if (isViolatingLocalhost) {
135 NFD_LOG_DEBUG(
"onIncomingInterest face=" << inFace.getId() <<
136 " interest=" << interest.getName() <<
" violates /localhost");
142 bool hasDuplicateNonceInDnl = m_deadNonceList.
has(interest.getName(), interest.getNonce());
143 if (hasDuplicateNonceInDnl) {
145 this->onInterestLoop(inFace, interest);
150 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
155 if (hasDuplicateNonceInPit) {
157 this->onInterestLoop(inFace, interest);
162 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
165 if (!pitEntry->hasInRecords()) {
167 bind(&Forwarder::onContentStoreHit,
this, ref(inFace), pitEntry, _1, _2),
168 bind(&Forwarder::onContentStoreMiss,
this, ref(inFace), pitEntry, _1));
171 this->onContentStoreMiss(inFace, pitEntry, interest);
176 Forwarder::onInterestLoop(Face& inFace,
const Interest& interest)
179 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
181 " interest=" << interest.getName() <<
187 " interest=" << interest.getName() <<
188 " send-Nack-duplicate");
192 lp::Nack nack(interest);
193 nack.setReason(lp::NackReason::DUPLICATE);
194 inFace.sendNack(nack);
198 Forwarder::onContentStoreMiss(
const Face& inFace,
const shared_ptr<pit::Entry>& pitEntry,
199 const Interest& interest)
201 NFD_LOG_DEBUG(
"onContentStoreMiss interest=" << interest.getName());
204 pitEntry->insertOrUpdateInRecord(const_cast<Face&>(inFace), interest);
207 this->setUnsatisfyTimer(pitEntry);
210 shared_ptr<lp::NextHopFaceIdTag> nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
211 if (nextHopTag !=
nullptr) {
213 Face* nextHopFace = m_faceTable.
get(*nextHopTag);
214 if (nextHopFace !=
nullptr) {
215 NFD_LOG_DEBUG(
"onContentStoreMiss interest=" << interest.getName() <<
" nexthop-faceid=" << nextHopFace->getId());
218 this->onOutgoingInterest(pitEntry, *nextHopFace, interest);
224 this->dispatchToStrategy(*pitEntry,
225 [&] (fw::Strategy& strategy) { strategy.afterReceiveInterest(inFace, interest, pitEntry); });
229 Forwarder::onContentStoreHit(
const Face& inFace,
const shared_ptr<pit::Entry>& pitEntry,
230 const Interest& interest,
const Data& data)
232 NFD_LOG_DEBUG(
"onContentStoreHit interest=" << interest.getName());
238 this->setStragglerTimer(pitEntry,
true, data.getFreshnessPeriod());
241 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
245 Forwarder::onOutgoingInterest(
const shared_ptr<pit::Entry>& pitEntry, Face& outFace,
const Interest& interest)
247 NFD_LOG_DEBUG(
"onOutgoingInterest face=" << outFace.getId() <<
248 " interest=" << pitEntry->getName());
251 pitEntry->insertOrUpdateOutRecord(outFace, interest);
254 outFace.sendInterest(interest);
259 Forwarder::onInterestReject(
const shared_ptr<pit::Entry>& pitEntry)
262 NFD_LOG_ERROR(
"onInterestReject interest=" << pitEntry->getName() <<
263 " cannot reject forwarded Interest");
266 NFD_LOG_DEBUG(
"onInterestReject interest=" << pitEntry->getName());
269 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
272 this->setStragglerTimer(pitEntry,
false);
276 Forwarder::onInterestUnsatisfied(
const shared_ptr<pit::Entry>& pitEntry)
278 NFD_LOG_DEBUG(
"onInterestUnsatisfied interest=" << pitEntry->getName());
281 this->dispatchToStrategy(*pitEntry,
282 [&] (fw::Strategy& strategy) { strategy.beforeExpirePendingInterest(pitEntry); });
285 this->onInterestFinalize(pitEntry,
false);
289 Forwarder::onInterestFinalize(
const shared_ptr<pit::Entry>& pitEntry,
bool isSatisfied,
290 time::milliseconds dataFreshnessPeriod)
292 NFD_LOG_DEBUG(
"onInterestFinalize interest=" << pitEntry->getName() <<
293 (isSatisfied ?
" satisfied" :
" unsatisfied"));
296 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
299 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
300 m_pit.erase(pitEntry.get());
304 Forwarder::onIncomingData(Face& inFace,
const Data& data)
307 NFD_LOG_DEBUG(
"onIncomingData face=" << inFace.getId() <<
" data=" << data.getName());
308 data.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
312 bool isViolatingLocalhost = inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
314 if (isViolatingLocalhost) {
316 " data=" << data.getName() <<
" violates /localhost");
323 if (pitMatches.begin() == pitMatches.end()) {
325 this->onDataUnsolicited(inFace, data);
332 std::set<Face*> pendingDownstreams;
334 auto now = time::steady_clock::now();
335 for (
const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
336 NFD_LOG_DEBUG(
"onIncomingData matching=" << pitEntry->getName());
339 this->cancelUnsatisfyAndStragglerTimer(*pitEntry);
342 for (
const pit::InRecord& inRecord : pitEntry->getInRecords()) {
343 if (inRecord.getExpiry() > now) {
344 pendingDownstreams.insert(&inRecord.getFace());
349 this->dispatchToStrategy(*pitEntry,
350 [&] (fw::Strategy& strategy) { strategy.beforeSatisfyInterest(pitEntry, inFace, data); });
353 this->insertDeadNonceList(*pitEntry,
true, data.getFreshnessPeriod(), &inFace);
356 pitEntry->clearInRecords();
357 pitEntry->deleteOutRecord(inFace);
360 this->setStragglerTimer(pitEntry,
true, data.getFreshnessPeriod());
364 for (Face* pendingDownstream : pendingDownstreams) {
365 if (pendingDownstream == &inFace) {
369 this->onOutgoingData(data, *pendingDownstream);
374 Forwarder::onDataUnsolicited(Face& inFace,
const Data& data)
380 m_cs.insert(data,
true);
384 " data=" << data.getName() <<
385 " decision=" << decision);
389 Forwarder::onOutgoingData(
const Data& data, Face& outFace)
392 NFD_LOG_WARN(
"onOutgoingData face=invalid data=" << data.getName());
395 NFD_LOG_DEBUG(
"onOutgoingData face=" << outFace.getId() <<
" data=" << data.getName());
398 bool isViolatingLocalhost = outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
400 if (isViolatingLocalhost) {
402 " data=" << data.getName() <<
" violates /localhost");
410 outFace.sendData(data);
415 Forwarder::onIncomingNack(Face& inFace,
const lp::Nack& nack)
418 nack.setTag(make_shared<lp::IncomingFaceIdTag>(inFace.getId()));
422 if (inFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
424 " nack=" << nack.getInterest().getName() <<
425 "~" << nack.getReason() <<
" face-is-multi-access");
430 shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest());
432 if (pitEntry ==
nullptr) {
434 " nack=" << nack.getInterest().getName() <<
435 "~" << nack.getReason() <<
" no-PIT-entry");
442 if (outRecord == pitEntry->out_end()) {
444 " nack=" << nack.getInterest().getName() <<
445 "~" << nack.getReason() <<
" no-out-record");
450 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
452 " nack=" << nack.getInterest().getName() <<
453 "~" << nack.getReason() <<
" wrong-Nonce " <<
454 nack.getInterest().getNonce() <<
"!=" << outRecord->getLastNonce());
459 " nack=" << nack.getInterest().getName() <<
460 "~" << nack.getReason() <<
" OK");
463 outRecord->setIncomingNack(nack);
466 this->dispatchToStrategy(*pitEntry,
467 [&] (fw::Strategy& strategy) { strategy.afterReceiveNack(inFace, nack, pitEntry); });
471 Forwarder::onOutgoingNack(
const shared_ptr<pit::Entry>& pitEntry,
const Face& outFace,
472 const lp::NackHeader& nack)
476 " nack=" << pitEntry->getInterest().getName() <<
477 "~" << nack.getReason() <<
" no-in-record");
485 if (inRecord == pitEntry->in_end()) {
487 " nack=" << pitEntry->getInterest().getName() <<
488 "~" << nack.getReason() <<
" no-in-record");
493 if (outFace.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) {
495 " nack=" << pitEntry->getInterest().getName() <<
496 "~" << nack.getReason() <<
" face-is-multi-access");
501 " nack=" << pitEntry->getInterest().getName() <<
502 "~" << nack.getReason() <<
" OK");
505 lp::Nack nackPkt(inRecord->getInterest());
506 nackPkt.setHeader(nack);
509 pitEntry->deleteInRecord(outFace);
512 const_cast<Face&
>(outFace).sendNack(nackPkt);
523 Forwarder::setUnsatisfyTimer(
const shared_ptr<pit::Entry>& pitEntry)
528 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
529 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
530 if (lastExpiryFromNow <= time::seconds::zero()) {
536 bind(&Forwarder::onInterestUnsatisfied,
this, pitEntry));
540 Forwarder::setStragglerTimer(
const shared_ptr<pit::Entry>& pitEntry,
bool isSatisfied,
541 time::milliseconds dataFreshnessPeriod)
543 time::nanoseconds stragglerTime = time::milliseconds(100);
547 bind(&Forwarder::onInterestFinalize,
this, pitEntry, isSatisfied, dataFreshnessPeriod));
551 Forwarder::cancelUnsatisfyAndStragglerTimer(pit::Entry& pitEntry)
565 Forwarder::insertDeadNonceList(pit::Entry& pitEntry,
bool isSatisfied,
566 time::milliseconds dataFreshnessPeriod, Face* upstream)
569 bool needDnl =
false;
571 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
573 needDnl =
static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
574 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.
getLifetime());
588 std::for_each(outRecords.begin(), outRecords.end(),
594 if (outRecord != pitEntry.getOutRecords().end()) {
595 m_deadNonceList.
add(pitEntry.getName(), outRecord->getLastNonce());
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
contains information about an Interest toward an outgoing face
static const Name & getStrategyName()
time::steady_clock::TimePoint getExpiry() const
gives the time point this record expires
Face * get(FaceId id) const
get face by FaceId
#define NFD_LOG_ERROR(expression)
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
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 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
static Name getDefaultStrategyName()
Copyright (c) 2014-2016, Regents of the University of California, Arizona Board of Regents...
#define NFD_LOG_INIT(name)
EventId schedule(const time::nanoseconds &after, const Scheduler::Event &event)
schedule an event
UnsolicitedDataDecision
a decision made by UnsolicitedDataPolicy
the Data should be cached in the ContentStore
uint32_t getLastNonce() const
void startProcessData(Face &face, const Data &data)
start incoming Data processing
const FaceId FACEID_CONTENT_STORE
identifies a packet comes from the ContentStore
std::vector< shared_ptr< Entry > > DataMatchResult
const FaceId INVALID_FACEID
indicates an invalid FaceId
const Name & getName() const
void startProcessNack(Face &face, const lp::Nack &nack)
start incoming Nack processing