blob: bde74287d632040c4e010da751bbef88b90a61c4 [file] [log] [blame]
Junxiao Shic1e12362014-01-24 20:03:26 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * Copyright (c) 2014-2015, 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shiee5a4442014-07-27 17:13:43 -070024 */
Junxiao Shic1e12362014-01-24 20:03:26 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_TABLE_FIB_HPP
27#define NFD_DAEMON_TABLE_FIB_HPP
Junxiao Shic1e12362014-01-24 20:03:26 -070028
29#include "fib-entry.hpp"
HangZhangad4afd12014-03-01 11:03:08 +080030#include "name-tree.hpp"
Junxiao Shidbe71732014-02-21 22:23:28 -070031
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080032namespace nfd {
Junxiao Shic1e12362014-01-24 20:03:26 -070033
HangZhangad4afd12014-03-01 11:03:08 +080034namespace measurements {
35class Entry;
36}
37namespace pit {
38class Entry;
39}
40
Junxiao Shic1e12362014-01-24 20:03:26 -070041/** \class Fib
42 * \brief represents the FIB
43 */
44class Fib : noncopyable
45{
46public:
HangZhangad4afd12014-03-01 11:03:08 +080047 explicit
48 Fib(NameTree& nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070049
Junxiao Shic1e12362014-01-24 20:03:26 -070050 ~Fib();
Steve DiBenedettod5f87932014-02-05 15:11:39 -070051
Junxiao Shi56a21bf2014-11-02 21:11:50 -070052 size_t
53 size() const;
Steve DiBenedettod5f87932014-02-05 15:11:39 -070054
Junxiao Shi56a21bf2014-11-02 21:11:50 -070055public: // lookup
Junxiao Shic1e12362014-01-24 20:03:26 -070056 /// performs a longest prefix match
57 shared_ptr<fib::Entry>
58 findLongestPrefixMatch(const Name& prefix) const;
Steve DiBenedettod5f87932014-02-05 15:11:39 -070059
Junxiao Shidbe71732014-02-21 22:23:28 -070060 /// performs a longest prefix match
61 shared_ptr<fib::Entry>
62 findLongestPrefixMatch(const pit::Entry& pitEntry) const;
63
64 /// performs a longest prefix match
65 shared_ptr<fib::Entry>
66 findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const;
67
Steve DiBenedettod5f87932014-02-05 15:11:39 -070068 shared_ptr<fib::Entry>
69 findExactMatch(const Name& prefix) const;
70
Junxiao Shi56a21bf2014-11-02 21:11:50 -070071public: // mutation
72 /** \brief inserts a FIB entry for prefix
73 * If an entry for exact same prefix exists, that entry is returned.
74 * \return{ the entry, and true for new entry, false for existing entry }
75 */
76 std::pair<shared_ptr<fib::Entry>, bool>
77 insert(const Name& prefix);
78
Steve DiBenedettod5f87932014-02-05 15:11:39 -070079 void
HangZhangad4afd12014-03-01 11:03:08 +080080 erase(const Name& prefix);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070081
Steve DiBenedettod030cfc2014-03-10 20:04:47 -060082 void
83 erase(const fib::Entry& entry);
84
Junxiao Shic1e12362014-01-24 20:03:26 -070085 /** \brief removes the NextHop record for face in all entrites
Junxiao Shi56a21bf2014-11-02 21:11:50 -070086 *
Junxiao Shic1e12362014-01-24 20:03:26 -070087 * This is usually invoked when face goes away.
Junxiao Shi56a21bf2014-11-02 21:11:50 -070088 * Removing the last NextHop in a FIB entry will erase the FIB entry.
89 *
90 * \todo change parameter type to Face&
Junxiao Shic1e12362014-01-24 20:03:26 -070091 */
92 void
93 removeNextHopFromAllEntries(shared_ptr<Face> face);
94
Junxiao Shi56a21bf2014-11-02 21:11:50 -070095public: // enumeration
96 class const_iterator;
HangZhangad4afd12014-03-01 11:03:08 +080097
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -080098 /** \brief returns an iterator pointing to the first FIB entry
99 * \note Iteration order is implementation-specific and is undefined
100 * \note The returned iterator may get invalidated if FIB or another NameTree-based
101 * table is modified
102 */
HangZhang5d469422014-03-12 09:26:26 +0800103 const_iterator
104 begin() const;
105
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800106 /** \brief returns an iterator referring to the past-the-end FIB entry
107 * \note The returned iterator may get invalidated if FIB or another NameTree-based
108 * table is modified
109 */
HangZhang5d469422014-03-12 09:26:26 +0800110 const_iterator
111 end() const;
112
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800113 class const_iterator : public std::iterator<std::forward_iterator_tag, const fib::Entry>
HangZhang5d469422014-03-12 09:26:26 +0800114 {
115 public:
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800116 const_iterator() = default;
117
HangZhang5d469422014-03-12 09:26:26 +0800118 explicit
119 const_iterator(const NameTree::const_iterator& it);
120
121 ~const_iterator();
122
123 const fib::Entry&
124 operator*() const;
125
126 shared_ptr<fib::Entry>
127 operator->() const;
128
129 const_iterator&
130 operator++();
131
132 const_iterator
133 operator++(int);
134
135 bool
136 operator==(const const_iterator& other) const;
137
138 bool
139 operator!=(const const_iterator& other) const;
140
141 private:
142 NameTree::const_iterator m_nameTreeIterator;
143 };
144
Junxiao Shic1e12362014-01-24 20:03:26 -0700145private:
HangZhangcb4fc832014-03-11 16:57:11 +0800146 shared_ptr<fib::Entry>
147 findLongestPrefixMatch(shared_ptr<name_tree::Entry> nameTreeEntry) const;
148
Junxiao Shiee5a4442014-07-27 17:13:43 -0700149 void
150 erase(shared_ptr<name_tree::Entry> nameTreeEntry);
151
Junxiao Shiefceadc2014-03-09 18:52:57 -0700152private:
HangZhangad4afd12014-03-01 11:03:08 +0800153 NameTree& m_nameTree;
Junxiao Shi40631842014-03-01 13:52:37 -0700154 size_t m_nItems;
Junxiao Shiefceadc2014-03-09 18:52:57 -0700155
Junxiao Shi40631842014-03-01 13:52:37 -0700156 /** \brief The empty FIB entry.
157 *
158 * This entry has no nexthops.
159 * It is returned by findLongestPrefixMatch if nothing is matched.
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700160 * Returning empty entry instead of nullptr makes forwarding and strategy implementation easier.
Junxiao Shi40631842014-03-01 13:52:37 -0700161 */
HangZhangcb4fc832014-03-11 16:57:11 +0800162 static const shared_ptr<fib::Entry> s_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -0700163};
164
HangZhangad4afd12014-03-01 11:03:08 +0800165inline size_t
166Fib::size() const
167{
168 return m_nItems;
169}
170
HangZhang5d469422014-03-12 09:26:26 +0800171inline Fib::const_iterator
172Fib::end() const
173{
174 return const_iterator(m_nameTree.end());
175}
176
177inline
178Fib::const_iterator::const_iterator(const NameTree::const_iterator& it)
179 : m_nameTreeIterator(it)
180{
181}
182
183inline
184Fib::const_iterator::~const_iterator()
185{
186}
187
188inline
189Fib::const_iterator
190Fib::const_iterator::operator++(int)
191{
192 Fib::const_iterator temp(*this);
193 ++(*this);
194 return temp;
195}
196
197inline Fib::const_iterator&
198Fib::const_iterator::operator++()
199{
200 ++m_nameTreeIterator;
201 return *this;
202}
203
204inline const fib::Entry&
205Fib::const_iterator::operator*() const
206{
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800207 return *this->operator->();
HangZhang5d469422014-03-12 09:26:26 +0800208}
209
210inline shared_ptr<fib::Entry>
211Fib::const_iterator::operator->() const
212{
213 return m_nameTreeIterator->getFibEntry();
214}
215
216inline bool
217Fib::const_iterator::operator==(const Fib::const_iterator& other) const
218{
219 return m_nameTreeIterator == other.m_nameTreeIterator;
220}
221
222inline bool
223Fib::const_iterator::operator!=(const Fib::const_iterator& other) const
224{
225 return m_nameTreeIterator != other.m_nameTreeIterator;
226}
227
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800228} // namespace nfd
Junxiao Shic1e12362014-01-24 20:03:26 -0700229
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700230#endif // NFD_DAEMON_TABLE_FIB_HPP