blob: 3a19949f98f0d1fe016a1d96f04fec661bef1380 [file] [log] [blame]
Junxiao Shic1e12362014-01-24 20:03:26 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shiee5a4442014-07-27 17:13:43 -07003 * Copyright (c) 2014, 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
HangZhang5d469422014-03-12 09:26:26 +080098 const_iterator
99 begin() const;
100
101 const_iterator
102 end() const;
103
104 class const_iterator : public std::iterator<std::forward_iterator_tag, fib::Entry>
105 {
106 public:
107 explicit
108 const_iterator(const NameTree::const_iterator& it);
109
110 ~const_iterator();
111
112 const fib::Entry&
113 operator*() const;
114
115 shared_ptr<fib::Entry>
116 operator->() const;
117
118 const_iterator&
119 operator++();
120
121 const_iterator
122 operator++(int);
123
124 bool
125 operator==(const const_iterator& other) const;
126
127 bool
128 operator!=(const const_iterator& other) const;
129
130 private:
131 NameTree::const_iterator m_nameTreeIterator;
132 };
133
Junxiao Shic1e12362014-01-24 20:03:26 -0700134private:
HangZhangcb4fc832014-03-11 16:57:11 +0800135 shared_ptr<fib::Entry>
136 findLongestPrefixMatch(shared_ptr<name_tree::Entry> nameTreeEntry) const;
137
Junxiao Shiee5a4442014-07-27 17:13:43 -0700138 void
139 erase(shared_ptr<name_tree::Entry> nameTreeEntry);
140
Junxiao Shiefceadc2014-03-09 18:52:57 -0700141private:
HangZhangad4afd12014-03-01 11:03:08 +0800142 NameTree& m_nameTree;
Junxiao Shi40631842014-03-01 13:52:37 -0700143 size_t m_nItems;
Junxiao Shiefceadc2014-03-09 18:52:57 -0700144
Junxiao Shi40631842014-03-01 13:52:37 -0700145 /** \brief The empty FIB entry.
146 *
147 * This entry has no nexthops.
148 * It is returned by findLongestPrefixMatch if nothing is matched.
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700149 * Returning empty entry instead of nullptr makes forwarding and strategy implementation easier.
Junxiao Shi40631842014-03-01 13:52:37 -0700150 */
HangZhangcb4fc832014-03-11 16:57:11 +0800151 static const shared_ptr<fib::Entry> s_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -0700152};
153
HangZhangad4afd12014-03-01 11:03:08 +0800154inline size_t
155Fib::size() const
156{
157 return m_nItems;
158}
159
HangZhang5d469422014-03-12 09:26:26 +0800160inline Fib::const_iterator
161Fib::end() const
162{
163 return const_iterator(m_nameTree.end());
164}
165
166inline
167Fib::const_iterator::const_iterator(const NameTree::const_iterator& it)
168 : m_nameTreeIterator(it)
169{
170}
171
172inline
173Fib::const_iterator::~const_iterator()
174{
175}
176
177inline
178Fib::const_iterator
179Fib::const_iterator::operator++(int)
180{
181 Fib::const_iterator temp(*this);
182 ++(*this);
183 return temp;
184}
185
186inline Fib::const_iterator&
187Fib::const_iterator::operator++()
188{
189 ++m_nameTreeIterator;
190 return *this;
191}
192
193inline const fib::Entry&
194Fib::const_iterator::operator*() const
195{
196 return *(m_nameTreeIterator->getFibEntry());
197}
198
199inline shared_ptr<fib::Entry>
200Fib::const_iterator::operator->() const
201{
202 return m_nameTreeIterator->getFibEntry();
203}
204
205inline bool
206Fib::const_iterator::operator==(const Fib::const_iterator& other) const
207{
208 return m_nameTreeIterator == other.m_nameTreeIterator;
209}
210
211inline bool
212Fib::const_iterator::operator!=(const Fib::const_iterator& other) const
213{
214 return m_nameTreeIterator != other.m_nameTreeIterator;
215}
216
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800217} // namespace nfd
Junxiao Shic1e12362014-01-24 20:03:26 -0700218
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700219#endif // NFD_DAEMON_TABLE_FIB_HPP