blob: bfeedf8a93645a986204845482454499da923f6d [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/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040039
HangZhangad4afd12014-03-01 11:03:08 +080040namespace pit {
41class Entry;
Junxiao Shia6de4292016-07-12 02:08:10 +000042} // namespace pit
HangZhangad4afd12014-03-01 11:03:08 +080043
Junxiao Shia6de4292016-07-12 02:08:10 +000044namespace fib {
45
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040046/**
47 * \brief Represents the Forwarding Information Base (FIB).
48 * \sa fib::Entry
Junxiao Shic1e12362014-01-24 20:03:26 -070049 */
50class Fib : noncopyable
51{
52public:
HangZhangad4afd12014-03-01 11:03:08 +080053 explicit
54 Fib(NameTree& nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070055
Junxiao Shi56a21bf2014-11-02 21:11:50 -070056 size_t
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040057 size() const noexcept
Junxiao Shi13ad4b72016-08-15 04:51:21 +000058 {
59 return m_nItems;
60 }
Steve DiBenedettod5f87932014-02-05 15:11:39 -070061
Junxiao Shi56a21bf2014-11-02 21:11:50 -070062public: // lookup
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040063 /** \brief Performs a longest prefix match.
Junxiao Shia6de4292016-07-12 02:08:10 +000064 */
65 const Entry&
Junxiao Shic1e12362014-01-24 20:03:26 -070066 findLongestPrefixMatch(const Name& prefix) const;
Steve DiBenedettod5f87932014-02-05 15:11:39 -070067
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040068 /** \brief Performs a longest prefix match.
Junxiao Shia6de4292016-07-12 02:08:10 +000069 *
Davide Pesavento50a6af32019-02-21 00:04:40 -050070 * This is equivalent to `findLongestPrefixMatch(pitEntry.getName())`
Junxiao Shia6de4292016-07-12 02:08:10 +000071 */
72 const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070073 findLongestPrefixMatch(const pit::Entry& pitEntry) const;
74
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040075 /** \brief Performs a longest prefix match.
Junxiao Shia6de4292016-07-12 02:08:10 +000076 *
Davide Pesavento50a6af32019-02-21 00:04:40 -050077 * This is equivalent to `findLongestPrefixMatch(measurementsEntry.getName())`
Junxiao Shia6de4292016-07-12 02:08:10 +000078 */
79 const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070080 findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const;
81
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040082 /** \brief Performs an exact match lookup.
Junxiao Shia6de4292016-07-12 02:08:10 +000083 */
84 Entry*
85 findExactMatch(const Name& prefix);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070086
Junxiao Shi56a21bf2014-11-02 21:11:50 -070087public: // mutation
Junxiao Shi75306352018-02-01 21:59:44 +000088 /** \brief Maximum number of components in a FIB entry prefix.
Junxiao Shi75306352018-02-01 21:59:44 +000089 */
90 static constexpr size_t
91 getMaxDepth()
92 {
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040093 return NameTree::getMaxDepth();
Junxiao Shi75306352018-02-01 21:59:44 +000094 }
95
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040096 /** \brief Find or insert a FIB entry.
Davide Pesavento50a6af32019-02-21 00:04:40 -050097 * \param prefix FIB entry name; it must not have more than \c getMaxDepth() components.
Junxiao Shia6de4292016-07-12 02:08:10 +000098 * \return the entry, and true for new entry or false for existing entry
Junxiao Shi56a21bf2014-11-02 21:11:50 -070099 */
Junxiao Shia6de4292016-07-12 02:08:10 +0000100 std::pair<Entry*, bool>
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700101 insert(const Name& prefix);
102
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700103 void
HangZhangad4afd12014-03-01 11:03:08 +0800104 erase(const Name& prefix);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700105
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600106 void
Junxiao Shia6de4292016-07-12 02:08:10 +0000107 erase(const Entry& entry);
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600108
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400109 /** \brief Add a NextHop record.
Ju Pand8315bf2019-07-31 06:59:07 +0000110 *
111 * If a NextHop record for \p face already exists in \p entry, its cost is set to \p cost.
Junxiao Shic1e12362014-01-24 20:03:26 -0700112 */
113 void
Ju Pand8315bf2019-07-31 06:59:07 +0000114 addOrUpdateNextHop(Entry& entry, Face& face, uint64_t cost);
115
116 enum class RemoveNextHopResult {
117 NO_SUCH_NEXTHOP, ///< the nexthop is not found
118 NEXTHOP_REMOVED, ///< the nexthop is removed and the fib entry stays
119 FIB_ENTRY_REMOVED ///< the nexthop is removed and the fib entry is removed
120 };
121
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400122 /** \brief Remove the NextHop record for \p face from \p entry.
Ju Pand8315bf2019-07-31 06:59:07 +0000123 */
124 RemoveNextHopResult
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000125 removeNextHop(Entry& entry, const Face& face);
Junxiao Shic1e12362014-01-24 20:03:26 -0700126
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700127public: // enumeration
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400128 using Range = boost::transformed_range<name_tree::GetTableEntry<Entry>, const name_tree::Range>;
129 using const_iterator = boost::range_iterator<Range>::type;
HangZhangad4afd12014-03-01 11:03:08 +0800130
Junxiao Shib30863e2016-08-15 21:32:01 +0000131 /** \return an iterator to the beginning
Davide Pesavento50a6af32019-02-21 00:04:40 -0500132 * \note The iteration order is implementation-defined.
Junxiao Shib30863e2016-08-15 21:32:01 +0000133 * \warning Undefined behavior may occur if a FIB/PIT/Measurements/StrategyChoice entry
Davide Pesavento50a6af32019-02-21 00:04:40 -0500134 * is inserted or erased during iteration.
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800135 */
HangZhang5d469422014-03-12 09:26:26 +0800136 const_iterator
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000137 begin() const
138 {
139 return this->getRange().begin();
140 }
HangZhang5d469422014-03-12 09:26:26 +0800141
Junxiao Shib30863e2016-08-15 21:32:01 +0000142 /** \return an iterator to the end
143 * \sa begin()
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800144 */
HangZhang5d469422014-03-12 09:26:26 +0800145 const_iterator
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000146 end() const
HangZhang5d469422014-03-12 09:26:26 +0800147 {
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000148 return this->getRange().end();
149 }
HangZhang5d469422014-03-12 09:26:26 +0800150
Ju Pand8315bf2019-07-31 06:59:07 +0000151public: // signal
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400152 /** \brief Signals on Fib entry nexthop creation.
Ju Pand8315bf2019-07-31 06:59:07 +0000153 */
154 signal::Signal<Fib, Name, NextHop> afterNewNextHop;
155
Junxiao Shic1e12362014-01-24 20:03:26 -0700156private:
Junxiao Shidd8f6612016-08-12 15:42:52 +0000157 /** \tparam K a parameter acceptable to NameTree::findLongestPrefixMatch
158 */
159 template<typename K>
Junxiao Shia6de4292016-07-12 02:08:10 +0000160 const Entry&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000161 findLongestPrefixMatchImpl(const K& key) const;
HangZhangcb4fc832014-03-11 16:57:11 +0800162
Junxiao Shiee5a4442014-07-27 17:13:43 -0700163 void
Junxiao Shi811c0102016-08-10 04:12:45 +0000164 erase(name_tree::Entry* nte, bool canDeleteNte = true);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700165
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000166 Range
167 getRange() const;
168
Junxiao Shiefceadc2014-03-09 18:52:57 -0700169private:
HangZhangad4afd12014-03-01 11:03:08 +0800170 NameTree& m_nameTree;
Davide Pesavento50a6af32019-02-21 00:04:40 -0500171 size_t m_nItems = 0;
Junxiao Shiefceadc2014-03-09 18:52:57 -0700172
Davide Pesavento50a6af32019-02-21 00:04:40 -0500173 /** \brief The empty FIB entry.
Junxiao Shi40631842014-03-01 13:52:37 -0700174 *
175 * This entry has no nexthops.
176 * It is returned by findLongestPrefixMatch if nothing is matched.
177 */
Junxiao Shia6de4292016-07-12 02:08:10 +0000178 static const unique_ptr<Entry> s_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -0700179};
180
Junxiao Shia6de4292016-07-12 02:08:10 +0000181} // namespace fib
182
183using fib::Fib;
184
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800185} // namespace nfd
Junxiao Shic1e12362014-01-24 20:03:26 -0700186
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700187#endif // NFD_DAEMON_TABLE_FIB_HPP