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-2022, 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::fw {
30 
31 bool
32 wouldViolateScope(const Face& inFace, const Interest& interest, const Face& outFace)
33 {
34  if (outFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
35  // forwarding to a local face is always allowed
36  return false;
37  }
38 
39  if (scope_prefix::LOCALHOST.isPrefixOf(interest.getName())) {
40  // localhost Interests cannot be forwarded to a non-local face
41  return true;
42  }
43 
44  if (scope_prefix::LOCALHOP.isPrefixOf(interest.getName())) {
45  // localhop Interests can be forwarded to a non-local face only if it comes from a local face
46  return inFace.getScope() != ndn::nfd::FACE_SCOPE_LOCAL;
47  }
48 
49  // Interest name is not subject to scope control
50  return false;
51 }
52 
53 int
54 findDuplicateNonce(const pit::Entry& pitEntry, Interest::Nonce nonce, const Face& face)
55 {
56  int dnw = DUPLICATE_NONCE_NONE;
57 
58  for (const pit::InRecord& inRecord : pitEntry.getInRecords()) {
59  if (inRecord.getLastNonce() == nonce) {
60  if (&inRecord.getFace() == &face) {
62  }
63  else {
65  }
66  }
67  }
68 
69  for (const pit::OutRecord& outRecord : pitEntry.getOutRecords()) {
70  if (outRecord.getLastNonce() == nonce) {
71  if (&outRecord.getFace() == &face) {
73  }
74  else {
76  }
77  }
78  }
79 
80  return dnw;
81 }
82 
83 bool
85 {
86  auto now = time::steady_clock::now();
87  return std::any_of(pitEntry.out_begin(), pitEntry.out_end(),
88  [&now] (const pit::OutRecord& outRecord) {
89  return outRecord.getExpiry() >= now &&
90  outRecord.getIncomingNack() == nullptr;
91  });
92 }
93 
94 time::steady_clock::time_point
95 getLastOutgoing(const pit::Entry& pitEntry)
96 {
97  pit::OutRecordCollection::const_iterator lastOutgoing = std::max_element(
98  pitEntry.out_begin(), pitEntry.out_end(),
99  [] (const pit::OutRecord& a, const pit::OutRecord& b) {
100  return a.getLastRenewed() < b.getLastRenewed();
101  });
102  BOOST_ASSERT(lastOutgoing != pitEntry.out_end());
103 
104  return lastOutgoing->getLastRenewed();
105 }
106 
107 fib::NextHopList::const_iterator
108 findEligibleNextHopWithEarliestOutRecord(const Face& inFace, const Interest& interest,
109  const fib::NextHopList& nexthops,
110  const shared_ptr<pit::Entry>& pitEntry)
111 {
112  auto found = nexthops.end();
113  auto earliestRenewed = time::steady_clock::time_point::max();
114 
115  for (auto it = nexthops.begin(); it != nexthops.end(); ++it) {
116  if (!isNextHopEligible(inFace, interest, *it, pitEntry))
117  continue;
118 
119  auto outRecord = pitEntry->getOutRecord(it->getFace());
120  BOOST_ASSERT(outRecord != pitEntry->out_end());
121  if (outRecord->getLastRenewed() < earliestRenewed) {
122  found = it;
123  earliestRenewed = outRecord->getLastRenewed();
124  }
125  }
126  return found;
127 }
128 
129 bool
130 isNextHopEligible(const Face& inFace, const Interest& interest,
131  const fib::NextHop& nexthop,
132  const shared_ptr<pit::Entry>& pitEntry,
133  bool wantUnused,
134  time::steady_clock::time_point now)
135 {
136  const Face& outFace = nexthop.getFace();
137 
138  // do not forward back to the same face, unless it is ad hoc
139  if ((outFace.getId() == inFace.getId() && outFace.getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) ||
140  (wouldViolateScope(inFace, interest, outFace)))
141  return false;
142 
143  if (wantUnused) {
144  // nexthop must not have unexpired out-record
145  auto outRecord = pitEntry->getOutRecord(outFace);
146  if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) {
147  return false;
148  }
149  }
150  return true;
151 }
152 
153 } // namespace nfd::fw
This file contains common algorithms used by forwarding strategies.
Generalization of a network interface.
Definition: face.hpp:56
ndn::nfd::FaceScope getScope() const
Returns whether the face is local or non-local for scope control purposes.
Definition: face.hpp:273
FaceId getId() const noexcept
Returns the face ID.
Definition: face.hpp:121
ndn::nfd::LinkType getLinkType() const
Returns the link type of the face (point-to-point, multi-access, ...).
Definition: face.hpp:291
Represents a nexthop record in a FIB entry.
Definition: fib-nexthop.hpp:37
Face & getFace() const
Definition: fib-nexthop.hpp:46
Represents an entry in the Interest table (PIT).
Definition: pit-entry.hpp:62
OutRecordCollection::iterator out_end()
Definition: pit-entry.hpp:193
const InRecordCollection & getInRecords() const
Definition: pit-entry.hpp:97
OutRecordCollection::iterator out_begin()
Definition: pit-entry.hpp:181
const OutRecordCollection & getOutRecords() const
Definition: pit-entry.hpp:163
Contains information about an Interest from an incoming face.
Contains information about an Interest toward an outgoing face.
std::vector< NextHop > NextHopList
Definition: fib-entry.hpp:47
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)
Determines whether a NextHop is eligible, i.e., not the same inFace.
Definition: algorithm.cpp:130
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:108
@ DUPLICATE_NONCE_OUT_SAME
out-record of same face
Definition: algorithm.hpp:50
@ DUPLICATE_NONCE_NONE
no duplicate Nonce is found
Definition: algorithm.hpp:47
@ DUPLICATE_NONCE_IN_SAME
in-record of same face
Definition: algorithm.hpp:48
@ DUPLICATE_NONCE_OUT_OTHER
out-record of other face
Definition: algorithm.hpp:51
@ DUPLICATE_NONCE_IN_OTHER
in-record of other face
Definition: algorithm.hpp:49
int findDuplicateNonce(const pit::Entry &pitEntry, Interest::Nonce nonce, const Face &face)
Determine whether pitEntry has duplicate Nonce nonce.
Definition: algorithm.cpp:54
bool hasPendingOutRecords(const pit::Entry &pitEntry)
Determine whether pitEntry has any pending out-records.
Definition: algorithm.cpp:84
time::steady_clock::time_point getLastOutgoing(const pit::Entry &pitEntry)
Definition: algorithm.cpp:95
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:32
const Name LOCALHOST
The localhost scope ndn:/localhost.
const Name LOCALHOP
The localhop scope ndn:/localhop.