blob: eb10850a4610a14631dfa1dc81205a8fa5357848 [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 Shidd8f6612016-08-12 15:42:52 +0000150 /** \tparam K a parameter acceptable to NameTree::findLongestPrefixMatch
151 */
152 template<typename K>
Junxiao Shia6de4292016-07-12 02:08:10 +0000153 const Entry&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000154 findLongestPrefixMatchImpl(const K& key) const;
HangZhangcb4fc832014-03-11 16:57:11 +0800155
Junxiao Shiee5a4442014-07-27 17:13:43 -0700156 void
Junxiao Shi811c0102016-08-10 04:12:45 +0000157 erase(name_tree::Entry* nte, bool canDeleteNte = true);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700158
Junxiao Shiefceadc2014-03-09 18:52:57 -0700159private:
HangZhangad4afd12014-03-01 11:03:08 +0800160 NameTree& m_nameTree;
Junxiao Shi40631842014-03-01 13:52:37 -0700161 size_t m_nItems;
Junxiao Shiefceadc2014-03-09 18:52:57 -0700162
Junxiao Shia6de4292016-07-12 02:08:10 +0000163 /** \brief the empty FIB entry.
Junxiao Shi40631842014-03-01 13:52:37 -0700164 *
165 * This entry has no nexthops.
166 * It is returned by findLongestPrefixMatch if nothing is matched.
167 */
Junxiao Shia6de4292016-07-12 02:08:10 +0000168 static const unique_ptr<Entry> s_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -0700169};
170
HangZhangad4afd12014-03-01 11:03:08 +0800171inline size_t
172Fib::size() const
173{
174 return m_nItems;
175}
176
HangZhang5d469422014-03-12 09:26:26 +0800177inline Fib::const_iterator
178Fib::end() const
179{
180 return const_iterator(m_nameTree.end());
181}
182
183inline
184Fib::const_iterator::const_iterator(const NameTree::const_iterator& it)
185 : m_nameTreeIterator(it)
186{
187}
188
189inline
190Fib::const_iterator::~const_iterator()
191{
192}
193
194inline
195Fib::const_iterator
196Fib::const_iterator::operator++(int)
197{
Junxiao Shia6de4292016-07-12 02:08:10 +0000198 const_iterator temp(*this);
HangZhang5d469422014-03-12 09:26:26 +0800199 ++(*this);
200 return temp;
201}
202
203inline Fib::const_iterator&
204Fib::const_iterator::operator++()
205{
206 ++m_nameTreeIterator;
207 return *this;
208}
209
Junxiao Shia6de4292016-07-12 02:08:10 +0000210inline const Entry&
HangZhang5d469422014-03-12 09:26:26 +0800211Fib::const_iterator::operator*() const
212{
Junxiao Shia6de4292016-07-12 02:08:10 +0000213 return *m_nameTreeIterator->getFibEntry();
HangZhang5d469422014-03-12 09:26:26 +0800214}
215
Junxiao Shia6de4292016-07-12 02:08:10 +0000216inline const Entry*
HangZhang5d469422014-03-12 09:26:26 +0800217Fib::const_iterator::operator->() const
218{
219 return m_nameTreeIterator->getFibEntry();
220}
221
222inline bool
Junxiao Shia6de4292016-07-12 02:08:10 +0000223Fib::const_iterator::operator==(const const_iterator& other) const
HangZhang5d469422014-03-12 09:26:26 +0800224{
225 return m_nameTreeIterator == other.m_nameTreeIterator;
226}
227
228inline bool
Junxiao Shia6de4292016-07-12 02:08:10 +0000229Fib::const_iterator::operator!=(const const_iterator& other) const
HangZhang5d469422014-03-12 09:26:26 +0800230{
231 return m_nameTreeIterator != other.m_nameTreeIterator;
232}
233
Junxiao Shia6de4292016-07-12 02:08:10 +0000234} // namespace fib
235
236using fib::Fib;
237
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800238} // namespace nfd
Junxiao Shic1e12362014-01-24 20:03:26 -0700239
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700240#endif // NFD_DAEMON_TABLE_FIB_HPP