blob: 615462fe81e2587375b062d80d5252aa9a7363f9 [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:
HangZhang5d469422014-03-12 09:26:26 +080047 class const_iterator;
48
HangZhangad4afd12014-03-01 11:03:08 +080049 explicit
50 Fib(NameTree& nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070051
Junxiao Shic1e12362014-01-24 20:03:26 -070052 ~Fib();
Steve DiBenedettod5f87932014-02-05 15:11:39 -070053
Junxiao Shic1e12362014-01-24 20:03:26 -070054 /** \brief inserts a FIB entry for prefix
55 * If an entry for exact same prefix exists, that entry is returned.
56 * \return{ the entry, and true for new entry, false for existing entry }
57 */
58 std::pair<shared_ptr<fib::Entry>, bool>
59 insert(const Name& prefix);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070060
Junxiao Shic1e12362014-01-24 20:03:26 -070061 /// performs a longest prefix match
62 shared_ptr<fib::Entry>
63 findLongestPrefixMatch(const Name& prefix) const;
Steve DiBenedettod5f87932014-02-05 15:11:39 -070064
Junxiao Shidbe71732014-02-21 22:23:28 -070065 /// performs a longest prefix match
66 shared_ptr<fib::Entry>
67 findLongestPrefixMatch(const pit::Entry& pitEntry) const;
68
69 /// performs a longest prefix match
70 shared_ptr<fib::Entry>
71 findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const;
72
Steve DiBenedettod5f87932014-02-05 15:11:39 -070073 shared_ptr<fib::Entry>
74 findExactMatch(const Name& prefix) const;
75
76 void
HangZhangad4afd12014-03-01 11:03:08 +080077 erase(const Name& prefix);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070078
Steve DiBenedettod030cfc2014-03-10 20:04:47 -060079 void
80 erase(const fib::Entry& entry);
81
Junxiao Shic1e12362014-01-24 20:03:26 -070082 /** \brief removes the NextHop record for face in all entrites
83 * This is usually invoked when face goes away.
84 * Removing all NextHops in a FIB entry will not remove the FIB entry.
85 */
86 void
87 removeNextHopFromAllEntries(shared_ptr<Face> face);
88
HangZhangad4afd12014-03-01 11:03:08 +080089 size_t
90 size() const;
91
HangZhang5d469422014-03-12 09:26:26 +080092 const_iterator
93 begin() const;
94
95 const_iterator
96 end() const;
97
98 class const_iterator : public std::iterator<std::forward_iterator_tag, fib::Entry>
99 {
100 public:
101 explicit
102 const_iterator(const NameTree::const_iterator& it);
103
104 ~const_iterator();
105
106 const fib::Entry&
107 operator*() const;
108
109 shared_ptr<fib::Entry>
110 operator->() const;
111
112 const_iterator&
113 operator++();
114
115 const_iterator
116 operator++(int);
117
118 bool
119 operator==(const const_iterator& other) const;
120
121 bool
122 operator!=(const const_iterator& other) const;
123
124 private:
125 NameTree::const_iterator m_nameTreeIterator;
126 };
127
Junxiao Shic1e12362014-01-24 20:03:26 -0700128private:
HangZhangcb4fc832014-03-11 16:57:11 +0800129 shared_ptr<fib::Entry>
130 findLongestPrefixMatch(shared_ptr<name_tree::Entry> nameTreeEntry) const;
131
Junxiao Shiee5a4442014-07-27 17:13:43 -0700132 void
133 erase(shared_ptr<name_tree::Entry> nameTreeEntry);
134
Junxiao Shiefceadc2014-03-09 18:52:57 -0700135private:
HangZhangad4afd12014-03-01 11:03:08 +0800136 NameTree& m_nameTree;
Junxiao Shi40631842014-03-01 13:52:37 -0700137 size_t m_nItems;
Junxiao Shiefceadc2014-03-09 18:52:57 -0700138
Junxiao Shi40631842014-03-01 13:52:37 -0700139 /** \brief The empty FIB entry.
140 *
141 * This entry has no nexthops.
142 * It is returned by findLongestPrefixMatch if nothing is matched.
143 */
144 // Returning empty entry instead of nullptr makes forwarding and strategy implementation easier.
HangZhangcb4fc832014-03-11 16:57:11 +0800145 static const shared_ptr<fib::Entry> s_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -0700146};
147
HangZhangad4afd12014-03-01 11:03:08 +0800148inline size_t
149Fib::size() const
150{
151 return m_nItems;
152}
153
HangZhang5d469422014-03-12 09:26:26 +0800154inline Fib::const_iterator
155Fib::end() const
156{
157 return const_iterator(m_nameTree.end());
158}
159
160inline
161Fib::const_iterator::const_iterator(const NameTree::const_iterator& it)
162 : m_nameTreeIterator(it)
163{
164}
165
166inline
167Fib::const_iterator::~const_iterator()
168{
169}
170
171inline
172Fib::const_iterator
173Fib::const_iterator::operator++(int)
174{
175 Fib::const_iterator temp(*this);
176 ++(*this);
177 return temp;
178}
179
180inline Fib::const_iterator&
181Fib::const_iterator::operator++()
182{
183 ++m_nameTreeIterator;
184 return *this;
185}
186
187inline const fib::Entry&
188Fib::const_iterator::operator*() const
189{
190 return *(m_nameTreeIterator->getFibEntry());
191}
192
193inline shared_ptr<fib::Entry>
194Fib::const_iterator::operator->() const
195{
196 return m_nameTreeIterator->getFibEntry();
197}
198
199inline bool
200Fib::const_iterator::operator==(const Fib::const_iterator& other) const
201{
202 return m_nameTreeIterator == other.m_nameTreeIterator;
203}
204
205inline bool
206Fib::const_iterator::operator!=(const Fib::const_iterator& other) const
207{
208 return m_nameTreeIterator != other.m_nameTreeIterator;
209}
210
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800211} // namespace nfd
Junxiao Shic1e12362014-01-24 20:03:26 -0700212
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700213#endif // NFD_DAEMON_TABLE_FIB_HPP