blob: df2bfeb814760d2bdf7db0dbb2ebc69310a59667 [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 Shi02b73f52016-07-28 01:48:27 +000096 /** \brief removes the NextHop record for face
Junxiao Shic1e12362014-01-24 20:03:26 -070097 */
98 void
Junxiao Shi02b73f52016-07-28 01:48:27 +000099 removeNextHop(Entry& entry, const Face& face);
Junxiao Shic1e12362014-01-24 20:03:26 -0700100
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700101public: // enumeration
102 class const_iterator;
HangZhangad4afd12014-03-01 11:03:08 +0800103
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800104 /** \brief returns an iterator pointing to the first FIB entry
105 * \note Iteration order is implementation-specific and is undefined
106 * \note The returned iterator may get invalidated if FIB or another NameTree-based
107 * table is modified
108 */
HangZhang5d469422014-03-12 09:26:26 +0800109 const_iterator
110 begin() const;
111
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800112 /** \brief returns an iterator referring to the past-the-end FIB entry
113 * \note The returned iterator may get invalidated if FIB or another NameTree-based
114 * table is modified
115 */
HangZhang5d469422014-03-12 09:26:26 +0800116 const_iterator
117 end() const;
118
Junxiao Shia6de4292016-07-12 02:08:10 +0000119 class const_iterator : public std::iterator<std::forward_iterator_tag, const Entry>
HangZhang5d469422014-03-12 09:26:26 +0800120 {
121 public:
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800122 const_iterator() = default;
123
HangZhang5d469422014-03-12 09:26:26 +0800124 explicit
125 const_iterator(const NameTree::const_iterator& it);
126
127 ~const_iterator();
128
Junxiao Shia6de4292016-07-12 02:08:10 +0000129 const Entry&
HangZhang5d469422014-03-12 09:26:26 +0800130 operator*() const;
131
Junxiao Shia6de4292016-07-12 02:08:10 +0000132 const Entry*
HangZhang5d469422014-03-12 09:26:26 +0800133 operator->() const;
134
135 const_iterator&
136 operator++();
137
138 const_iterator
139 operator++(int);
140
141 bool
142 operator==(const const_iterator& other) const;
143
144 bool
145 operator!=(const const_iterator& other) const;
146
147 private:
148 NameTree::const_iterator m_nameTreeIterator;
149 };
150
Junxiao Shic1e12362014-01-24 20:03:26 -0700151private:
Junxiao Shia6de4292016-07-12 02:08:10 +0000152 const Entry&
153 findLongestPrefixMatch(shared_ptr<name_tree::Entry> nte) const;
HangZhangcb4fc832014-03-11 16:57:11 +0800154
Junxiao Shiee5a4442014-07-27 17:13:43 -0700155 void
Junxiao Shi02b73f52016-07-28 01:48:27 +0000156 erase(shared_ptr<name_tree::Entry> nte, bool canDeleteNte = true);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700157
Junxiao Shiefceadc2014-03-09 18:52:57 -0700158private:
HangZhangad4afd12014-03-01 11:03:08 +0800159 NameTree& m_nameTree;
Junxiao Shi40631842014-03-01 13:52:37 -0700160 size_t m_nItems;
Junxiao Shiefceadc2014-03-09 18:52:57 -0700161
Junxiao Shia6de4292016-07-12 02:08:10 +0000162 /** \brief the empty FIB entry.
Junxiao Shi40631842014-03-01 13:52:37 -0700163 *
164 * This entry has no nexthops.
165 * It is returned by findLongestPrefixMatch if nothing is matched.
166 */
Junxiao Shia6de4292016-07-12 02:08:10 +0000167 static const unique_ptr<Entry> s_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -0700168};
169
HangZhangad4afd12014-03-01 11:03:08 +0800170inline size_t
171Fib::size() const
172{
173 return m_nItems;
174}
175
HangZhang5d469422014-03-12 09:26:26 +0800176inline Fib::const_iterator
177Fib::end() const
178{
179 return const_iterator(m_nameTree.end());
180}
181
182inline
183Fib::const_iterator::const_iterator(const NameTree::const_iterator& it)
184 : m_nameTreeIterator(it)
185{
186}
187
188inline
189Fib::const_iterator::~const_iterator()
190{
191}
192
193inline
194Fib::const_iterator
195Fib::const_iterator::operator++(int)
196{
Junxiao Shia6de4292016-07-12 02:08:10 +0000197 const_iterator temp(*this);
HangZhang5d469422014-03-12 09:26:26 +0800198 ++(*this);
199 return temp;
200}
201
202inline Fib::const_iterator&
203Fib::const_iterator::operator++()
204{
205 ++m_nameTreeIterator;
206 return *this;
207}
208
Junxiao Shia6de4292016-07-12 02:08:10 +0000209inline const Entry&
HangZhang5d469422014-03-12 09:26:26 +0800210Fib::const_iterator::operator*() const
211{
Junxiao Shia6de4292016-07-12 02:08:10 +0000212 return *m_nameTreeIterator->getFibEntry();
HangZhang5d469422014-03-12 09:26:26 +0800213}
214
Junxiao Shia6de4292016-07-12 02:08:10 +0000215inline const Entry*
HangZhang5d469422014-03-12 09:26:26 +0800216Fib::const_iterator::operator->() const
217{
218 return m_nameTreeIterator->getFibEntry();
219}
220
221inline bool
Junxiao Shia6de4292016-07-12 02:08:10 +0000222Fib::const_iterator::operator==(const const_iterator& other) const
HangZhang5d469422014-03-12 09:26:26 +0800223{
224 return m_nameTreeIterator == other.m_nameTreeIterator;
225}
226
227inline bool
Junxiao Shia6de4292016-07-12 02:08:10 +0000228Fib::const_iterator::operator!=(const const_iterator& other) const
HangZhang5d469422014-03-12 09:26:26 +0800229{
230 return m_nameTreeIterator != other.m_nameTreeIterator;
231}
232
Junxiao Shia6de4292016-07-12 02:08:10 +0000233} // namespace fib
234
235using fib::Fib;
236
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800237} // namespace nfd
Junxiao Shic1e12362014-01-24 20:03:26 -0700238
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700239#endif // NFD_DAEMON_TABLE_FIB_HPP