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-2019, 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 #include <ndn-cxx/util/concepts.hpp>
31 
32 namespace nfd {
33 namespace fib {
34 
36 
37 const unique_ptr<Entry> Fib::s_emptyEntry = make_unique<Entry>(Name());
38 
39 static inline bool
41 {
42  return nte.getFibEntry() != nullptr;
43 }
44 
45 Fib::Fib(NameTree& nameTree)
46  : m_nameTree(nameTree)
47 {
48 }
49 
50 template<typename K>
51 const Entry&
52 Fib::findLongestPrefixMatchImpl(const K& key) const
53 {
54  name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasFibEntry);
55  if (nte != nullptr) {
56  return *nte->getFibEntry();
57  }
58  return *s_emptyEntry;
59 }
60 
61 const Entry&
62 Fib::findLongestPrefixMatch(const Name& prefix) const
63 {
64  return this->findLongestPrefixMatchImpl(prefix);
65 }
66 
67 const Entry&
69 {
70  return this->findLongestPrefixMatchImpl(pitEntry);
71 }
72 
73 const Entry&
74 Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
75 {
76  return this->findLongestPrefixMatchImpl(measurementsEntry);
77 }
78 
79 Entry*
80 Fib::findExactMatch(const Name& prefix)
81 {
82  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
83  if (nte != nullptr)
84  return nte->getFibEntry();
85 
86  return nullptr;
87 }
88 
89 std::pair<Entry*, bool>
90 Fib::insert(const Name& prefix)
91 {
92  name_tree::Entry& nte = m_nameTree.lookup(prefix);
93  Entry* entry = nte.getFibEntry();
94  if (entry != nullptr) {
95  return {entry, false};
96  }
97 
98  nte.setFibEntry(make_unique<Entry>(prefix));
99  ++m_nItems;
100  return {nte.getFibEntry(), true};
101 }
102 
103 void
104 Fib::erase(name_tree::Entry* nte, bool canDeleteNte)
105 {
106  BOOST_ASSERT(nte != nullptr);
107 
108  nte->setFibEntry(nullptr);
109  if (canDeleteNte) {
110  m_nameTree.eraseIfEmpty(nte);
111  }
112  --m_nItems;
113 }
114 
115 void
116 Fib::erase(const Name& prefix)
117 {
118  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
119  if (nte != nullptr) {
120  this->erase(nte);
121  }
122 }
123 
124 void
125 Fib::erase(const Entry& entry)
126 {
127  name_tree::Entry* nte = m_nameTree.getEntry(entry);
128  if (nte == nullptr) { // don't try to erase s_emptyEntry
129  BOOST_ASSERT(&entry == s_emptyEntry.get());
130  return;
131  }
132  this->erase(nte);
133 }
134 
135 void
136 Fib::eraseIfEmpty(Entry& entry)
137 {
138  if (!entry.hasNextHops()) {
139  name_tree::Entry* nte = m_nameTree.getEntry(entry);
140  this->erase(nte, false);
141  }
142 }
143 
144 void
145 Fib::removeNextHop(Entry& entry, const Face& face, EndpointId endpointId)
146 {
147  entry.removeNextHop(face, endpointId);
148  this->eraseIfEmpty(entry);
149 }
150 
151 void
152 Fib::removeNextHopByFace(Entry& entry, const Face& face)
153 {
154  entry.removeNextHopByFace(face);
155  this->eraseIfEmpty(entry);
156 }
157 
159 Fib::getRange() const
160 {
161  return m_nameTree.fullEnumerate(&nteHasFibEntry) |
162  boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(&name_tree::Entry::getFibEntry));
163 }
164 
165 } // namespace fib
166 } // namespace nfd
Entry * findExactMatch(const Name &prefix)
Performs an exact match lookup.
Definition: fib.cpp:80
void erase(const Name &prefix)
Definition: fib.cpp:116
Represents a Measurements entry.
Fib(NameTree &nameTree)
Definition: fib.cpp:45
represents a FIB entry
Definition: fib-entry.hpp:51
void removeNextHopByFace(Entry &entry, const Face &face)
Remove all NextHop records for face.
Definition: fib.cpp:152
std::pair< Entry *, bool > insert(const Name &prefix)
Find or insert a FIB entry.
Definition: fib.cpp:90
void removeNextHopByFace(const Face &face)
removes all NextHop records on face for any endpointId
Definition: fib-entry.cpp:73
NDN_CXX_ASSERT_FORWARD_ITERATOR(Fib::const_iterator)
boost::transformed_range< name_tree::GetTableEntry< Entry >, const name_tree::Range > Range
Definition: fib.hpp:117
fib::Entry * getFibEntry() const
An Interest table entry.
Definition: pit-entry.hpp:58
uint64_t EndpointId
identifies an endpoint on the link
Definition: transport.hpp:38
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
void setFibEntry(unique_ptr< fib::Entry > fibEntry)
void removeNextHop(Entry &entry, const Face &face, EndpointId endpointId)
Remove the NextHop record for the given face and endpointId.
Definition: fib.cpp:145
An entry in the name tree.
boost::range_iterator< Range >::type const_iterator
Definition: fib.hpp:118
const Entry & findLongestPrefixMatch(const Name &prefix) const
Performs a longest prefix match.
Definition: fib.cpp:62
void removeNextHop(const Face &face, EndpointId endpointId)
removes the NextHop record for face with the given endpointId
Definition: fib-entry.cpp:64
a functor to get a table entry from a name tree entry
static bool nteHasFibEntry(const name_tree::Entry &nte)
Definition: fib.cpp:40
bool hasNextHops() const
Definition: fib-entry.hpp:72