NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
algorithm.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2021, Regents of the University of California,
4  * 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.
10  *
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/>.
24  */
25 
26 #include "algorithm.hpp"
27 #include "scope-prefix.hpp"
28 
29 namespace nfd {
30 namespace fw {
31 
32 bool
33 wouldViolateScope(const Face& inFace, const Interest& interest, const Face& outFace)
34 {
35  if (outFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
36  // forwarding to a local face is always allowed
37  return false;
38  }
39 
40  if (scope_prefix::LOCALHOST.isPrefixOf(interest.getName())) {
41  // localhost Interests cannot be forwarded to a non-local face
42  return true;
43  }
44 
45  if (scope_prefix::LOCALHOP.isPrefixOf(interest.getName())) {
46  // localhop Interests can be forwarded to a non-local face only if it comes from a local face
47  return inFace.getScope() != ndn::nfd::FACE_SCOPE_LOCAL;
48  }
49 
50  // Interest name is not subject to scope control
51  return false;
52 }
53 
54 int
55 findDuplicateNonce(const pit::Entry& pitEntry, Interest::Nonce nonce, const Face& face)
56 {
57  int dnw = DUPLICATE_NONCE_NONE;
58 
59  for (const pit::InRecord& inRecord : pitEntry.getInRecords()) {
60  if (inRecord.getLastNonce() == nonce) {
61  if (&inRecord.getFace() == &face) {
63  }
64  else {
66  }
67  }
68  }
69 
70  for (const pit::OutRecord& outRecord : pitEntry.getOutRecords()) {
71  if (outRecord.getLastNonce() == nonce) {
72  if (&outRecord.getFace() == &face) {
74  }
75  else {
77  }
78  }
79  }
80 
81  return dnw;
82 }
83 
84 bool
86 {
87  auto now = time::steady_clock::now();
88  return std::any_of(pitEntry.out_begin(), pitEntry.out_end(),
89  [&now] (const pit::OutRecord& outRecord) {
90  return outRecord.getExpiry() >= now &&
91  outRecord.getIncomingNack() == nullptr;
92  });
93 }
94 
96 getLastOutgoing(const pit::Entry& pitEntry)
97 {
98  pit::OutRecordCollection::const_iterator lastOutgoing = std::max_element(
99  pitEntry.out_begin(), pitEntry.out_end(),
100  [] (const pit::OutRecord& a, const pit::OutRecord& b) {
101  return a.getLastRenewed() < b.getLastRenewed();
102  });
103  BOOST_ASSERT(lastOutgoing != pitEntry.out_end());
104 
105  return lastOutgoing->getLastRenewed();
106 }
107 
108 fib::NextHopList::const_iterator
110  const fib::NextHopList& nexthops,
111  const shared_ptr<pit::Entry>& pitEntry)
112 {
113  auto found = nexthops.end();
114  auto earliestRenewed = time::steady_clock::time_point::max();
115 
116  for (auto it = nexthops.begin(); it != nexthops.end(); ++it) {
117  if (!isNextHopEligible(inFace, interest, *it, pitEntry))
118  continue;
119 
120  auto outRecord = pitEntry->getOutRecord(it->getFace());
121  BOOST_ASSERT(outRecord != pitEntry->out_end());
122  if (outRecord->getLastRenewed() < earliestRenewed) {
123  found = it;
124  earliestRenewed = outRecord->getLastRenewed();
125  }
126  }
127  return found;
128 }
129 
130 bool
131 isNextHopEligible(const Face& inFace, const Interest& interest,
132  const fib::NextHop& nexthop,
133  const shared_ptr<pit::Entry>& pitEntry,
134  bool wantUnused,
136 {
137  const Face& outFace = nexthop.getFace();
138 
139  // do not forward back to the same face, unless it is ad hoc
140  if ((outFace.getId() == inFace.getId() && outFace.getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) ||
141  (wouldViolateScope(inFace, interest, outFace)))
142  return false;
143 
144  if (wantUnused) {
145  // nexthop must not have unexpired out-record
146  auto outRecord = pitEntry->getOutRecord(outFace);
147  if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) {
148  return false;
149  }
150  }
151  return true;
152 }
153 
154 } // namespace fw
155 } // namespace nfd
const InRecordCollection & getInRecords() const
Definition: pit-entry.hpp:94
const Name LOCALHOST("ndn:/localhost")
ndn:/localhost
static time_point now() noexcept
Definition: time.cpp:80
Contains information about an Interest from an incoming face.
fib::NextHopList::const_iterator findEligibleNextHopWithEarliestOutRecord(const Face &inFace, const Interest &interest, const fib::NextHopList &nexthops, const shared_ptr< pit::Entry > &pitEntry)
pick an eligible NextHop with earliest out-record
Definition: algorithm.cpp:109
OutRecordCollection::iterator out_end()
Definition: pit-entry.hpp:190
boost::chrono::time_point< steady_clock > time_point
Definition: time.hpp:230
Represents an Interest packet.
Definition: interest.hpp:48
Represents a collection of nexthops.
Definition: fib-entry.hpp:39
OutRecordCollection::iterator out_begin()
Definition: pit-entry.hpp:178
int findDuplicateNonce(const pit::Entry &pitEntry, Interest::Nonce nonce, const Face &face)
determine whether pitEntry has duplicate Nonce nonce
Definition: algorithm.cpp:55
in-record of same face
Definition: algorithm.hpp:49
ndn Face
Definition: face-impl.hpp:42
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
An Interest table entry.
Definition: pit-entry.hpp:58
const Name LOCALHOP("ndn:/localhop")
ndn:/localhop
bool hasPendingOutRecords(const pit::Entry &pitEntry)
determine whether pitEntry has any pending out-records
Definition: algorithm.cpp:85
no duplicate Nonce is found
Definition: algorithm.hpp:48
time::steady_clock::time_point getLastOutgoing(const pit::Entry &pitEntry)
Definition: algorithm.cpp:96
This file contains common algorithms used by forwarding strategies.
const Name & getName() const noexcept
Definition: interest.hpp:172
Contains information about an Interest toward an outgoing face.
bool isNextHopEligible(const Face &inFace, const Interest &interest, const fib::NextHop &nexthop, const shared_ptr< pit::Entry > &pitEntry, bool wantUnused, time::steady_clock::time_point now)
Definition: algorithm.cpp:131
in-record of other face
Definition: algorithm.hpp:50
out-record of other face
Definition: algorithm.hpp:52
bool wouldViolateScope(const Face &inFace, const Interest &interest, const Face &outFace)
determine whether forwarding the Interest in pitEntry to outFace would violate scope ...
Definition: algorithm.cpp:33
const OutRecordCollection & getOutRecords() const
Definition: pit-entry.hpp:160
Face & getFace() const
Definition: fib-nexthop.hpp:47
Represents a nexthop record in a FIB entry.
Definition: fib-nexthop.hpp:37
time::steady_clock::TimePoint getLastRenewed() const
out-record of same face
Definition: algorithm.hpp:51