blob: 950f23808dd841450314bc76b64883f8d5b159d4 [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 Shi56a21bf2014-11-02 21:11:50 -070051 size_t
52 size() const;
Steve DiBenedettod5f87932014-02-05 15:11:39 -070053
Junxiao Shi56a21bf2014-11-02 21:11:50 -070054public: // lookup
Junxiao Shia6de4292016-07-12 02:08:10 +000055 /** \brief performs a longest prefix match
56 */
57 const Entry&
Junxiao Shic1e12362014-01-24 20:03:26 -070058 findLongestPrefixMatch(const Name& prefix) const;
Steve DiBenedettod5f87932014-02-05 15:11:39 -070059
Junxiao Shia6de4292016-07-12 02:08:10 +000060 /** \brief performs a longest prefix match
61 *
62 * This is equivalent to .findLongestPrefixMatch(pitEntry.getName())
63 */
64 const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070065 findLongestPrefixMatch(const pit::Entry& pitEntry) const;
66
Junxiao Shia6de4292016-07-12 02:08:10 +000067 /** \brief performs a longest prefix match
68 *
69 * This is equivalent to .findLongestPrefixMatch(measurementsEntry.getName())
70 */
71 const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070072 findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const;
73
Junxiao Shia6de4292016-07-12 02:08:10 +000074 /** \brief performs an exact match lookup
75 */
76 Entry*
77 findExactMatch(const Name& prefix);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070078
Junxiao Shi56a21bf2014-11-02 21:11:50 -070079public: // mutation
80 /** \brief inserts a FIB entry for prefix
Junxiao Shia6de4292016-07-12 02:08:10 +000081 *
Junxiao Shi56a21bf2014-11-02 21:11:50 -070082 * If an entry for exact same prefix exists, that entry is returned.
Junxiao Shia6de4292016-07-12 02:08:10 +000083 * \return the entry, and true for new entry or false for existing entry
Junxiao Shi56a21bf2014-11-02 21:11:50 -070084 */
Junxiao Shia6de4292016-07-12 02:08:10 +000085 std::pair<Entry*, bool>
Junxiao Shi56a21bf2014-11-02 21:11:50 -070086 insert(const Name& prefix);
87
Steve DiBenedettod5f87932014-02-05 15:11:39 -070088 void
HangZhangad4afd12014-03-01 11:03:08 +080089 erase(const Name& prefix);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070090
Steve DiBenedettod030cfc2014-03-10 20:04:47 -060091 void
Junxiao Shia6de4292016-07-12 02:08:10 +000092 erase(const Entry& entry);
Steve DiBenedettod030cfc2014-03-10 20:04:47 -060093
Junxiao Shi02b73f52016-07-28 01:48:27 +000094 /** \brief removes the NextHop record for face
Junxiao Shic1e12362014-01-24 20:03:26 -070095 */
96 void
Junxiao Shi02b73f52016-07-28 01:48:27 +000097 removeNextHop(Entry& entry, const Face& face);
Junxiao Shic1e12362014-01-24 20:03:26 -070098
Junxiao Shi56a21bf2014-11-02 21:11:50 -070099public: // enumeration
100 class const_iterator;
HangZhangad4afd12014-03-01 11:03:08 +0800101
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800102 /** \brief returns an iterator pointing to the first FIB entry
103 * \note Iteration order is implementation-specific and is undefined
104 * \note The returned iterator may get invalidated if FIB or another NameTree-based
105 * table is modified
106 */
HangZhang5d469422014-03-12 09:26:26 +0800107 const_iterator
108 begin() const;
109
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800110 /** \brief returns an iterator referring to the past-the-end FIB entry
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 end() const;
116
Junxiao Shia6de4292016-07-12 02:08:10 +0000117 class const_iterator : public std::iterator<std::forward_iterator_tag, const Entry>
HangZhang5d469422014-03-12 09:26:26 +0800118 {
119 public:
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800120 const_iterator() = default;
121
HangZhang5d469422014-03-12 09:26:26 +0800122 explicit
123 const_iterator(const NameTree::const_iterator& it);
124
125 ~const_iterator();
126
Junxiao Shia6de4292016-07-12 02:08:10 +0000127 const Entry&
HangZhang5d469422014-03-12 09:26:26 +0800128 operator*() const;
129
Junxiao Shia6de4292016-07-12 02:08:10 +0000130 const Entry*
HangZhang5d469422014-03-12 09:26:26 +0800131 operator->() const;
132
133 const_iterator&
134 operator++();
135
136 const_iterator
137 operator++(int);
138
139 bool
140 operator==(const const_iterator& other) const;
141
142 bool
143 operator!=(const const_iterator& other) const;
144
145 private:
146 NameTree::const_iterator m_nameTreeIterator;
147 };
148
Junxiao Shic1e12362014-01-24 20:03:26 -0700149private:
Junxiao Shia6de4292016-07-12 02:08:10 +0000150 const Entry&
Junxiao Shi811c0102016-08-10 04:12:45 +0000151 findLongestPrefixMatch(const name_tree::Entry* nte) const;
HangZhangcb4fc832014-03-11 16:57:11 +0800152
Junxiao Shiee5a4442014-07-27 17:13:43 -0700153 void
Junxiao Shi811c0102016-08-10 04:12:45 +0000154 erase(name_tree::Entry* nte, bool canDeleteNte = true);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700155
Junxiao Shiefceadc2014-03-09 18:52:57 -0700156private:
HangZhangad4afd12014-03-01 11:03:08 +0800157 NameTree& m_nameTree;
Junxiao Shi40631842014-03-01 13:52:37 -0700158 size_t m_nItems;
Junxiao Shiefceadc2014-03-09 18:52:57 -0700159
Junxiao Shia6de4292016-07-12 02:08:10 +0000160 /** \brief the empty FIB entry.
Junxiao Shi40631842014-03-01 13:52:37 -0700161 *
162 * This entry has no nexthops.
163 * It is returned by findLongestPrefixMatch if nothing is matched.
164 */
Junxiao Shia6de4292016-07-12 02:08:10 +0000165 static const unique_ptr<Entry> s_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -0700166};
167
HangZhangad4afd12014-03-01 11:03:08 +0800168inline size_t
169Fib::size() const
170{
171 return m_nItems;
172}
173
HangZhang5d469422014-03-12 09:26:26 +0800174inline Fib::const_iterator
175Fib::end() const
176{
177 return const_iterator(m_nameTree.end());
178}
179
180inline
181Fib::const_iterator::const_iterator(const NameTree::const_iterator& it)
182 : m_nameTreeIterator(it)
183{
184}
185
186inline
187Fib::const_iterator::~const_iterator()
188{
189}
190
191inline
192Fib::const_iterator
193Fib::const_iterator::operator++(int)
194{
Junxiao Shia6de4292016-07-12 02:08:10 +0000195 const_iterator temp(*this);
HangZhang5d469422014-03-12 09:26:26 +0800196 ++(*this);
197 return temp;
198}
199
200inline Fib::const_iterator&
201Fib::const_iterator::operator++()
202{
203 ++m_nameTreeIterator;
204 return *this;
205}
206
Junxiao Shia6de4292016-07-12 02:08:10 +0000207inline const Entry&
HangZhang5d469422014-03-12 09:26:26 +0800208Fib::const_iterator::operator*() const
209{
Junxiao Shia6de4292016-07-12 02:08:10 +0000210 return *m_nameTreeIterator->getFibEntry();
HangZhang5d469422014-03-12 09:26:26 +0800211}
212
Junxiao Shia6de4292016-07-12 02:08:10 +0000213inline const Entry*
HangZhang5d469422014-03-12 09:26:26 +0800214Fib::const_iterator::operator->() const
215{
216 return m_nameTreeIterator->getFibEntry();
217}
218
219inline bool
Junxiao Shia6de4292016-07-12 02:08:10 +0000220Fib::const_iterator::operator==(const const_iterator& other) const
HangZhang5d469422014-03-12 09:26:26 +0800221{
222 return m_nameTreeIterator == other.m_nameTreeIterator;
223}
224
225inline bool
Junxiao Shia6de4292016-07-12 02:08:10 +0000226Fib::const_iterator::operator!=(const const_iterator& other) const
HangZhang5d469422014-03-12 09:26:26 +0800227{
228 return m_nameTreeIterator != other.m_nameTreeIterator;
229}
230
Junxiao Shia6de4292016-07-12 02:08:10 +0000231} // namespace fib
232
233using fib::Fib;
234
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800235} // namespace nfd
Junxiao Shic1e12362014-01-24 20:03:26 -0700236
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700237#endif // NFD_DAEMON_TABLE_FIB_HPP