blob: bfe66846071c473daa982cd5d6b58e5e780d7160 [file] [log] [blame]
Junxiao Shic1e12362014-01-24 20:03:26 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shia6de4292016-07-12 02:08:10 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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;
Junxiao Shia6de4292016-07-12 02:08:10 +000036} // namespace measurements
HangZhangad4afd12014-03-01 11:03:08 +080037namespace pit {
38class Entry;
Junxiao Shia6de4292016-07-12 02:08:10 +000039} // namespace pit
HangZhangad4afd12014-03-01 11:03:08 +080040
Junxiao Shia6de4292016-07-12 02:08:10 +000041namespace fib {
42
43/** \brief represents the Forwarding Information Base (FIB)
Junxiao Shic1e12362014-01-24 20:03:26 -070044 */
45class Fib : noncopyable
46{
47public:
HangZhangad4afd12014-03-01 11:03:08 +080048 explicit
49 Fib(NameTree& nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070050
Junxiao Shic1e12362014-01-24 20:03:26 -070051 ~Fib();
Steve DiBenedettod5f87932014-02-05 15:11:39 -070052
Junxiao Shi56a21bf2014-11-02 21:11:50 -070053 size_t
54 size() const;
Steve DiBenedettod5f87932014-02-05 15:11:39 -070055
Junxiao Shi56a21bf2014-11-02 21:11:50 -070056public: // lookup
Junxiao Shia6de4292016-07-12 02:08:10 +000057 /** \brief performs a longest prefix match
58 */
59 const Entry&
Junxiao Shic1e12362014-01-24 20:03:26 -070060 findLongestPrefixMatch(const Name& prefix) const;
Steve DiBenedettod5f87932014-02-05 15:11:39 -070061
Junxiao Shia6de4292016-07-12 02:08:10 +000062 /** \brief performs a longest prefix match
63 *
64 * This is equivalent to .findLongestPrefixMatch(pitEntry.getName())
65 */
66 const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070067 findLongestPrefixMatch(const pit::Entry& pitEntry) const;
68
Junxiao Shia6de4292016-07-12 02:08:10 +000069 /** \brief performs a longest prefix match
70 *
71 * This is equivalent to .findLongestPrefixMatch(measurementsEntry.getName())
72 */
73 const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070074 findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const;
75
Junxiao Shia6de4292016-07-12 02:08:10 +000076 /** \brief performs an exact match lookup
77 */
78 Entry*
79 findExactMatch(const Name& prefix);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070080
Junxiao Shi56a21bf2014-11-02 21:11:50 -070081public: // mutation
82 /** \brief inserts a FIB entry for prefix
Junxiao Shia6de4292016-07-12 02:08:10 +000083 *
Junxiao Shi56a21bf2014-11-02 21:11:50 -070084 * If an entry for exact same prefix exists, that entry is returned.
Junxiao Shia6de4292016-07-12 02:08:10 +000085 * \return the entry, and true for new entry or false for existing entry
Junxiao Shi56a21bf2014-11-02 21:11:50 -070086 */
Junxiao Shia6de4292016-07-12 02:08:10 +000087 std::pair<Entry*, bool>
Junxiao Shi56a21bf2014-11-02 21:11:50 -070088 insert(const Name& prefix);
89
Steve DiBenedettod5f87932014-02-05 15:11:39 -070090 void
HangZhangad4afd12014-03-01 11:03:08 +080091 erase(const Name& prefix);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070092
Steve DiBenedettod030cfc2014-03-10 20:04:47 -060093 void
Junxiao Shia6de4292016-07-12 02:08:10 +000094 erase(const Entry& entry);
Steve DiBenedettod030cfc2014-03-10 20:04:47 -060095
Junxiao Shia6de4292016-07-12 02:08:10 +000096 /** \brief removes the NextHop record for face in all entries
Junxiao Shi56a21bf2014-11-02 21:11:50 -070097 *
Junxiao Shia6de4292016-07-12 02:08:10 +000098 * This is usually invoked when face is destroyed.
Junxiao Shi56a21bf2014-11-02 21:11:50 -070099 * Removing the last NextHop in a FIB entry will erase the FIB entry.
100 *
101 * \todo change parameter type to Face&
Junxiao Shic1e12362014-01-24 20:03:26 -0700102 */
103 void
Junxiao Shia6de4292016-07-12 02:08:10 +0000104 removeNextHopFromAllEntries(const Face& face);
Junxiao Shic1e12362014-01-24 20:03:26 -0700105
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700106public: // enumeration
107 class const_iterator;
HangZhangad4afd12014-03-01 11:03:08 +0800108
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800109 /** \brief returns an iterator pointing to the first FIB entry
110 * \note Iteration order is implementation-specific and is undefined
111 * \note The returned iterator may get invalidated if FIB or another NameTree-based
112 * table is modified
113 */
HangZhang5d469422014-03-12 09:26:26 +0800114 const_iterator
115 begin() const;
116
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800117 /** \brief returns an iterator referring to the past-the-end FIB entry
118 * \note The returned iterator may get invalidated if FIB or another NameTree-based
119 * table is modified
120 */
HangZhang5d469422014-03-12 09:26:26 +0800121 const_iterator
122 end() const;
123
Junxiao Shia6de4292016-07-12 02:08:10 +0000124 class const_iterator : public std::iterator<std::forward_iterator_tag, const Entry>
HangZhang5d469422014-03-12 09:26:26 +0800125 {
126 public:
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800127 const_iterator() = default;
128
HangZhang5d469422014-03-12 09:26:26 +0800129 explicit
130 const_iterator(const NameTree::const_iterator& it);
131
132 ~const_iterator();
133
Junxiao Shia6de4292016-07-12 02:08:10 +0000134 const Entry&
HangZhang5d469422014-03-12 09:26:26 +0800135 operator*() const;
136
Junxiao Shia6de4292016-07-12 02:08:10 +0000137 const Entry*
HangZhang5d469422014-03-12 09:26:26 +0800138 operator->() const;
139
140 const_iterator&
141 operator++();
142
143 const_iterator
144 operator++(int);
145
146 bool
147 operator==(const const_iterator& other) const;
148
149 bool
150 operator!=(const const_iterator& other) const;
151
152 private:
153 NameTree::const_iterator m_nameTreeIterator;
154 };
155
Junxiao Shic1e12362014-01-24 20:03:26 -0700156private:
Junxiao Shia6de4292016-07-12 02:08:10 +0000157 const Entry&
158 findLongestPrefixMatch(shared_ptr<name_tree::Entry> nte) const;
HangZhangcb4fc832014-03-11 16:57:11 +0800159
Junxiao Shiee5a4442014-07-27 17:13:43 -0700160 void
161 erase(shared_ptr<name_tree::Entry> nameTreeEntry);
162
Junxiao Shiefceadc2014-03-09 18:52:57 -0700163private:
HangZhangad4afd12014-03-01 11:03:08 +0800164 NameTree& m_nameTree;
Junxiao Shi40631842014-03-01 13:52:37 -0700165 size_t m_nItems;
Junxiao Shiefceadc2014-03-09 18:52:57 -0700166
Junxiao Shia6de4292016-07-12 02:08:10 +0000167 /** \brief the empty FIB entry.
Junxiao Shi40631842014-03-01 13:52:37 -0700168 *
169 * This entry has no nexthops.
170 * It is returned by findLongestPrefixMatch if nothing is matched.
171 */
Junxiao Shia6de4292016-07-12 02:08:10 +0000172 static const unique_ptr<Entry> s_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -0700173};
174
HangZhangad4afd12014-03-01 11:03:08 +0800175inline size_t
176Fib::size() const
177{
178 return m_nItems;
179}
180
HangZhang5d469422014-03-12 09:26:26 +0800181inline Fib::const_iterator
182Fib::end() const
183{
184 return const_iterator(m_nameTree.end());
185}
186
187inline
188Fib::const_iterator::const_iterator(const NameTree::const_iterator& it)
189 : m_nameTreeIterator(it)
190{
191}
192
193inline
194Fib::const_iterator::~const_iterator()
195{
196}
197
198inline
199Fib::const_iterator
200Fib::const_iterator::operator++(int)
201{
Junxiao Shia6de4292016-07-12 02:08:10 +0000202 const_iterator temp(*this);
HangZhang5d469422014-03-12 09:26:26 +0800203 ++(*this);
204 return temp;
205}
206
207inline Fib::const_iterator&
208Fib::const_iterator::operator++()
209{
210 ++m_nameTreeIterator;
211 return *this;
212}
213
Junxiao Shia6de4292016-07-12 02:08:10 +0000214inline const Entry&
HangZhang5d469422014-03-12 09:26:26 +0800215Fib::const_iterator::operator*() const
216{
Junxiao Shia6de4292016-07-12 02:08:10 +0000217 return *m_nameTreeIterator->getFibEntry();
HangZhang5d469422014-03-12 09:26:26 +0800218}
219
Junxiao Shia6de4292016-07-12 02:08:10 +0000220inline const Entry*
HangZhang5d469422014-03-12 09:26:26 +0800221Fib::const_iterator::operator->() const
222{
223 return m_nameTreeIterator->getFibEntry();
224}
225
226inline bool
Junxiao Shia6de4292016-07-12 02:08:10 +0000227Fib::const_iterator::operator==(const const_iterator& other) const
HangZhang5d469422014-03-12 09:26:26 +0800228{
229 return m_nameTreeIterator == other.m_nameTreeIterator;
230}
231
232inline bool
Junxiao Shia6de4292016-07-12 02:08:10 +0000233Fib::const_iterator::operator!=(const const_iterator& other) const
HangZhang5d469422014-03-12 09:26:26 +0800234{
235 return m_nameTreeIterator != other.m_nameTreeIterator;
236}
237
Junxiao Shia6de4292016-07-12 02:08:10 +0000238} // namespace fib
239
240using fib::Fib;
241
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800242} // namespace nfd
Junxiao Shic1e12362014-01-24 20:03:26 -0700243
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700244#endif // NFD_DAEMON_TABLE_FIB_HPP