blob: 462a29bdcf1b6f144f430f1e8ff34b93c03c27cb [file] [log] [blame]
Junxiao Shic1e12362014-01-24 20:03:26 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi75306352018-02-01 21:59:44 +00002/*
ashiqopu3ad49db2018-10-20 22:38:47 +00003 * Copyright (c) 2014-2019, 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
Junxiao Shi13ad4b72016-08-15 04:51:21 +000032#include <boost/range/adaptor/transformed.hpp>
33
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080034namespace nfd {
Junxiao Shic1e12362014-01-24 20:03:26 -070035
HangZhangad4afd12014-03-01 11:03:08 +080036namespace measurements {
37class Entry;
Junxiao Shia6de4292016-07-12 02:08:10 +000038} // namespace measurements
HangZhangad4afd12014-03-01 11:03:08 +080039namespace pit {
40class Entry;
Junxiao Shia6de4292016-07-12 02:08:10 +000041} // namespace pit
HangZhangad4afd12014-03-01 11:03:08 +080042
Junxiao Shia6de4292016-07-12 02:08:10 +000043namespace fib {
44
Davide Pesavento50a6af32019-02-21 00:04:40 -050045/** \brief Represents the Forwarding Information Base (FIB)
Junxiao Shic1e12362014-01-24 20:03:26 -070046 */
47class Fib : noncopyable
48{
49public:
HangZhangad4afd12014-03-01 11:03:08 +080050 explicit
51 Fib(NameTree& nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070052
Junxiao Shi56a21bf2014-11-02 21:11:50 -070053 size_t
Junxiao Shi13ad4b72016-08-15 04:51:21 +000054 size() const
55 {
56 return m_nItems;
57 }
Steve DiBenedettod5f87932014-02-05 15:11:39 -070058
Junxiao Shi56a21bf2014-11-02 21:11:50 -070059public: // lookup
Davide Pesavento50a6af32019-02-21 00:04:40 -050060 /** \brief Performs a longest prefix match
Junxiao Shia6de4292016-07-12 02:08:10 +000061 */
62 const Entry&
Junxiao Shic1e12362014-01-24 20:03:26 -070063 findLongestPrefixMatch(const Name& prefix) const;
Steve DiBenedettod5f87932014-02-05 15:11:39 -070064
Davide Pesavento50a6af32019-02-21 00:04:40 -050065 /** \brief Performs a longest prefix match
Junxiao Shia6de4292016-07-12 02:08:10 +000066 *
Davide Pesavento50a6af32019-02-21 00:04:40 -050067 * This is equivalent to `findLongestPrefixMatch(pitEntry.getName())`
Junxiao Shia6de4292016-07-12 02:08:10 +000068 */
69 const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070070 findLongestPrefixMatch(const pit::Entry& pitEntry) const;
71
Davide Pesavento50a6af32019-02-21 00:04:40 -050072 /** \brief Performs a longest prefix match
Junxiao Shia6de4292016-07-12 02:08:10 +000073 *
Davide Pesavento50a6af32019-02-21 00:04:40 -050074 * This is equivalent to `findLongestPrefixMatch(measurementsEntry.getName())`
Junxiao Shia6de4292016-07-12 02:08:10 +000075 */
76 const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070077 findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const;
78
Davide Pesavento50a6af32019-02-21 00:04:40 -050079 /** \brief Performs an exact match lookup
Junxiao Shia6de4292016-07-12 02:08:10 +000080 */
81 Entry*
82 findExactMatch(const Name& prefix);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070083
Junxiao Shi56a21bf2014-11-02 21:11:50 -070084public: // mutation
Junxiao Shi75306352018-02-01 21:59:44 +000085 /** \brief Maximum number of components in a FIB entry prefix.
Junxiao Shi75306352018-02-01 21:59:44 +000086 */
87 static constexpr size_t
88 getMaxDepth()
89 {
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040090 return NameTree::getMaxDepth();
Junxiao Shi75306352018-02-01 21:59:44 +000091 }
92
Davide Pesavento50a6af32019-02-21 00:04:40 -050093 /** \brief Find or insert a FIB entry
94 * \param prefix FIB entry name; it must not have more than \c getMaxDepth() components.
Junxiao Shia6de4292016-07-12 02:08:10 +000095 * \return the entry, and true for new entry or false for existing entry
Junxiao Shi56a21bf2014-11-02 21:11:50 -070096 */
Junxiao Shia6de4292016-07-12 02:08:10 +000097 std::pair<Entry*, bool>
Junxiao Shi56a21bf2014-11-02 21:11:50 -070098 insert(const Name& prefix);
99
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700100 void
HangZhangad4afd12014-03-01 11:03:08 +0800101 erase(const Name& prefix);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700102
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600103 void
Junxiao Shia6de4292016-07-12 02:08:10 +0000104 erase(const Entry& entry);
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600105
Davide Pesavento50a6af32019-02-21 00:04:40 -0500106 /** \brief Remove the NextHop record for the given \p face and \p endpointId
Junxiao Shic1e12362014-01-24 20:03:26 -0700107 */
108 void
ashiqopu77d0bfd2019-02-20 20:37:31 +0000109 removeNextHop(Entry& entry, const Face& face, EndpointId endpointId);
ashiqopu3ad49db2018-10-20 22:38:47 +0000110
Davide Pesavento50a6af32019-02-21 00:04:40 -0500111 /** \brief Remove all NextHop records for \p face
ashiqopu3ad49db2018-10-20 22:38:47 +0000112 */
113 void
114 removeNextHopByFace(Entry& entry, const Face& face);
Junxiao Shic1e12362014-01-24 20:03:26 -0700115
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700116public: // enumeration
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000117 typedef boost::transformed_range<name_tree::GetTableEntry<Entry>, const name_tree::Range> Range;
118 typedef boost::range_iterator<Range>::type const_iterator;
HangZhangad4afd12014-03-01 11:03:08 +0800119
Junxiao Shib30863e2016-08-15 21:32:01 +0000120 /** \return an iterator to the beginning
Davide Pesavento50a6af32019-02-21 00:04:40 -0500121 * \note The iteration order is implementation-defined.
Junxiao Shib30863e2016-08-15 21:32:01 +0000122 * \warning Undefined behavior may occur if a FIB/PIT/Measurements/StrategyChoice entry
Davide Pesavento50a6af32019-02-21 00:04:40 -0500123 * is inserted or erased during iteration.
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800124 */
HangZhang5d469422014-03-12 09:26:26 +0800125 const_iterator
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000126 begin() const
127 {
128 return this->getRange().begin();
129 }
HangZhang5d469422014-03-12 09:26:26 +0800130
Junxiao Shib30863e2016-08-15 21:32:01 +0000131 /** \return an iterator to the end
132 * \sa begin()
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800133 */
HangZhang5d469422014-03-12 09:26:26 +0800134 const_iterator
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000135 end() const
HangZhang5d469422014-03-12 09:26:26 +0800136 {
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000137 return this->getRange().end();
138 }
HangZhang5d469422014-03-12 09:26:26 +0800139
Junxiao Shic1e12362014-01-24 20:03:26 -0700140private:
Junxiao Shidd8f6612016-08-12 15:42:52 +0000141 /** \tparam K a parameter acceptable to NameTree::findLongestPrefixMatch
142 */
143 template<typename K>
Junxiao Shia6de4292016-07-12 02:08:10 +0000144 const Entry&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000145 findLongestPrefixMatchImpl(const K& key) const;
HangZhangcb4fc832014-03-11 16:57:11 +0800146
Junxiao Shiee5a4442014-07-27 17:13:43 -0700147 void
Junxiao Shi811c0102016-08-10 04:12:45 +0000148 erase(name_tree::Entry* nte, bool canDeleteNte = true);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700149
Davide Pesavento50a6af32019-02-21 00:04:40 -0500150 /** \brief Erase \p entry if it contains no nexthop records
ashiqopu3ad49db2018-10-20 22:38:47 +0000151 */
152 void
153 eraseIfEmpty(Entry& entry);
154
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000155 Range
156 getRange() const;
157
Junxiao Shiefceadc2014-03-09 18:52:57 -0700158private:
HangZhangad4afd12014-03-01 11:03:08 +0800159 NameTree& m_nameTree;
Davide Pesavento50a6af32019-02-21 00:04:40 -0500160 size_t m_nItems = 0;
Junxiao Shiefceadc2014-03-09 18:52:57 -0700161
Davide Pesavento50a6af32019-02-21 00:04:40 -0500162 /** \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
Junxiao Shia6de4292016-07-12 02:08:10 +0000170} // namespace fib
171
172using fib::Fib;
173
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800174} // namespace nfd
Junxiao Shic1e12362014-01-24 20:03:26 -0700175
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700176#endif // NFD_DAEMON_TABLE_FIB_HPP