fib.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2023, 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 "fib.hpp"
27 #include "pit-entry.hpp"
28 #include "measurements-entry.hpp"
29 
30 namespace nfd::fib {
31 
32 const unique_ptr<Entry> Fib::s_emptyEntry = make_unique<Entry>(Name());
33 
34 static inline bool
36 {
37  return nte.getFibEntry() != nullptr;
38 }
39 
40 Fib::Fib(NameTree& nameTree)
41  : m_nameTree(nameTree)
42 {
43 }
44 
45 template<typename K>
46 const Entry&
47 Fib::findLongestPrefixMatchImpl(const K& key) const
48 {
50  if (nte != nullptr) {
51  return *nte->getFibEntry();
52  }
53  return *s_emptyEntry;
54 }
55 
56 const Entry&
57 Fib::findLongestPrefixMatch(const Name& prefix) const
58 {
59  return this->findLongestPrefixMatchImpl(prefix);
60 }
61 
62 const Entry&
64 {
65  return this->findLongestPrefixMatchImpl(pitEntry);
66 }
67 
68 const Entry&
69 Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
70 {
71  return this->findLongestPrefixMatchImpl(measurementsEntry);
72 }
73 
74 Entry*
75 Fib::findExactMatch(const Name& prefix)
76 {
77  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
78  if (nte != nullptr)
79  return nte->getFibEntry();
80 
81  return nullptr;
82 }
83 
84 std::pair<Entry*, bool>
85 Fib::insert(const Name& prefix)
86 {
87  name_tree::Entry& nte = m_nameTree.lookup(prefix);
88  Entry* entry = nte.getFibEntry();
89  if (entry != nullptr) {
90  return {entry, false};
91  }
92 
93  nte.setFibEntry(make_unique<Entry>(prefix));
94  ++m_nItems;
95  return {nte.getFibEntry(), true};
96 }
97 
98 void
99 Fib::erase(name_tree::Entry* nte, bool canDeleteNte)
100 {
101  BOOST_ASSERT(nte != nullptr);
102 
103  nte->setFibEntry(nullptr);
104  if (canDeleteNte) {
105  m_nameTree.eraseIfEmpty(nte);
106  }
107  --m_nItems;
108 }
109 
110 void
111 Fib::erase(const Name& prefix)
112 {
113  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
114  if (nte != nullptr) {
115  this->erase(nte);
116  }
117 }
118 
119 void
120 Fib::erase(const Entry& entry)
121 {
122  name_tree::Entry* nte = m_nameTree.getEntry(entry);
123  if (nte == nullptr) { // don't try to erase s_emptyEntry
124  BOOST_ASSERT(&entry == s_emptyEntry.get());
125  return;
126  }
127  this->erase(nte);
128 }
129 
130 void
131 Fib::addOrUpdateNextHop(Entry& entry, Face& face, uint64_t cost)
132 {
133  auto [it, isNew] = entry.addOrUpdateNextHop(face, cost);
134  if (isNew)
135  this->afterNewNextHop(entry.getPrefix(), *it);
136 }
137 
139 Fib::removeNextHop(Entry& entry, const Face& face)
140 {
141  bool isRemoved = entry.removeNextHop(face);
142 
143  if (!isRemoved) {
145  }
146  else if (!entry.hasNextHops()) {
147  name_tree::Entry* nte = m_nameTree.getEntry(entry);
148  this->erase(nte, false);
150  }
151  else {
153  }
154 }
155 
157 Fib::getRange() const
158 {
159  return m_nameTree.fullEnumerate(&nteHasFibEntry) |
160  boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(&name_tree::Entry::getFibEntry));
161 }
162 
163 } // namespace nfd::fib
Generalization of a network interface.
Definition: face.hpp:118
Represents an entry in the FIB.
Definition: fib-entry.hpp:91
bool hasNextHops() const noexcept
Returns whether this Entry has any NextHop records.
Definition: fib-entry.hpp:112
const Name & getPrefix() const noexcept
Definition: fib-entry.hpp:97
RemoveNextHopResult
Definition: fib.hpp:116
@ NEXTHOP_REMOVED
the nexthop is removed and the fib entry stays
@ FIB_ENTRY_REMOVED
the nexthop is removed and the fib entry is removed
@ NO_SUCH_NEXTHOP
the nexthop is not found
std::pair< Entry *, bool > insert(const Name &prefix)
Find or insert a FIB entry.
Definition: fib.cpp:85
Entry * findExactMatch(const Name &prefix)
Performs an exact match lookup.
Definition: fib.cpp:75
RemoveNextHopResult removeNextHop(Entry &entry, const Face &face)
Remove the NextHop record for face from entry.
Definition: fib.cpp:139
boost::transformed_range< name_tree::GetTableEntry< Entry >, const name_tree::Range > Range
Definition: fib.hpp:128
signal::Signal< Fib, Name, NextHop > afterNewNextHop
Signals on Fib entry nexthop creation.
Definition: fib.hpp:154
void erase(const Name &prefix)
Definition: fib.cpp:111
Fib(NameTree &nameTree)
Definition: fib.cpp:40
const Entry & findLongestPrefixMatch(const Name &prefix) const
Performs a longest prefix match.
Definition: fib.cpp:57
void addOrUpdateNextHop(Entry &entry, Face &face, uint64_t cost)
Add a NextHop record.
Definition: fib.cpp:131
Represents an entry in the Measurements table.
An entry in the name tree.
fib::Entry * getFibEntry() const
void setFibEntry(unique_ptr< fib::Entry > fibEntry)
A functor to get a table entry from a name tree entry.
A common index structure for FIB, PIT, StrategyChoice, and Measurements.
Definition: name-tree.hpp:37
size_t eraseIfEmpty(Entry *entry, bool canEraseAncestors=true)
Delete the entry if it is empty.
Definition: name-tree.cpp:122
Range fullEnumerate(const EntrySelector &entrySelector=AnyEntry()) const
Enumerate all entries.
Definition: name-tree.cpp:225
Entry & lookup(const Name &name, size_t prefixLen)
Find or insert an entry by name.
Definition: name-tree.cpp:43
Entry * getEntry(const EntryT &tableEntry) const
Definition: name-tree.hpp:77
Entry * findExactMatch(const Name &name, size_t prefixLen=std::numeric_limits< size_t >::max()) const
Exact match lookup.
Definition: name-tree.cpp:149
Entry * findLongestPrefixMatch(const Name &name, const EntrySelector &entrySelector=AnyEntry()) const
Longest prefix matching.
Definition: name-tree.cpp:161
Represents an entry in the Interest table (PIT).
Definition: pit-entry.hpp:197
static bool nteHasFibEntry(const name_tree::Entry &nte)
Definition: fib.cpp:35