blob: ac864e8f1a5092e366158dc35bb5154108361562 [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/*
3 * Copyright (c) 2014-2018, 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 Shi75306352018-02-01 21:59:44 +000032#include "core/fib-max-depth.hpp"
33
Junxiao Shi13ad4b72016-08-15 04:51:21 +000034#include <boost/range/adaptor/transformed.hpp>
35
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080036namespace nfd {
Junxiao Shic1e12362014-01-24 20:03:26 -070037
HangZhangad4afd12014-03-01 11:03:08 +080038namespace measurements {
39class Entry;
Junxiao Shia6de4292016-07-12 02:08:10 +000040} // namespace measurements
HangZhangad4afd12014-03-01 11:03:08 +080041namespace pit {
42class Entry;
Junxiao Shia6de4292016-07-12 02:08:10 +000043} // namespace pit
HangZhangad4afd12014-03-01 11:03:08 +080044
Junxiao Shia6de4292016-07-12 02:08:10 +000045namespace fib {
46
47/** \brief represents the Forwarding Information Base (FIB)
Junxiao Shic1e12362014-01-24 20:03:26 -070048 */
49class Fib : noncopyable
50{
51public:
HangZhangad4afd12014-03-01 11:03:08 +080052 explicit
53 Fib(NameTree& nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070054
Junxiao Shi56a21bf2014-11-02 21:11:50 -070055 size_t
Junxiao Shi13ad4b72016-08-15 04:51:21 +000056 size() const
57 {
58 return m_nItems;
59 }
Steve DiBenedettod5f87932014-02-05 15:11:39 -070060
Junxiao Shi56a21bf2014-11-02 21:11:50 -070061public: // lookup
Junxiao Shia6de4292016-07-12 02:08:10 +000062 /** \brief performs a longest prefix match
63 */
64 const Entry&
Junxiao Shic1e12362014-01-24 20:03:26 -070065 findLongestPrefixMatch(const Name& prefix) const;
Steve DiBenedettod5f87932014-02-05 15:11:39 -070066
Junxiao Shia6de4292016-07-12 02:08:10 +000067 /** \brief performs a longest prefix match
68 *
69 * This is equivalent to .findLongestPrefixMatch(pitEntry.getName())
70 */
71 const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070072 findLongestPrefixMatch(const pit::Entry& pitEntry) const;
73
Junxiao Shia6de4292016-07-12 02:08:10 +000074 /** \brief performs a longest prefix match
75 *
76 * This is equivalent to .findLongestPrefixMatch(measurementsEntry.getName())
77 */
78 const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070079 findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const;
80
Junxiao Shia6de4292016-07-12 02:08:10 +000081 /** \brief performs an exact match lookup
82 */
83 Entry*
84 findExactMatch(const Name& prefix);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070085
Junxiao Shi56a21bf2014-11-02 21:11:50 -070086public: // mutation
Junxiao Shi75306352018-02-01 21:59:44 +000087 /** \brief Maximum number of components in a FIB entry prefix.
88 *
89 * This constant is currently advisory, but will become mandatory later.
90 */
91 static constexpr size_t
92 getMaxDepth()
93 {
94 static_assert(FIB_MAX_DEPTH == NameTree::getMaxDepth(), "");
95 return FIB_MAX_DEPTH;
96 }
97
Junxiao Shi56a21bf2014-11-02 21:11:50 -070098 /** \brief inserts a FIB entry for prefix
Junxiao Shia6de4292016-07-12 02:08:10 +000099 *
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700100 * If an entry for exact same prefix exists, that entry is returned.
Junxiao Shia6de4292016-07-12 02:08:10 +0000101 * \return the entry, and true for new entry or false for existing entry
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700102 */
Junxiao Shia6de4292016-07-12 02:08:10 +0000103 std::pair<Entry*, bool>
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700104 insert(const Name& prefix);
105
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700106 void
HangZhangad4afd12014-03-01 11:03:08 +0800107 erase(const Name& prefix);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700108
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600109 void
Junxiao Shia6de4292016-07-12 02:08:10 +0000110 erase(const Entry& entry);
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600111
Junxiao Shi02b73f52016-07-28 01:48:27 +0000112 /** \brief removes the NextHop record for face
Junxiao Shic1e12362014-01-24 20:03:26 -0700113 */
114 void
Junxiao Shi02b73f52016-07-28 01:48:27 +0000115 removeNextHop(Entry& entry, const Face& face);
Junxiao Shic1e12362014-01-24 20:03:26 -0700116
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700117public: // enumeration
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000118 typedef boost::transformed_range<name_tree::GetTableEntry<Entry>, const name_tree::Range> Range;
119 typedef boost::range_iterator<Range>::type const_iterator;
HangZhangad4afd12014-03-01 11:03:08 +0800120
Junxiao Shib30863e2016-08-15 21:32:01 +0000121 /** \return an iterator to the beginning
122 * \note Iteration order is implementation-defined.
123 * \warning Undefined behavior may occur if a FIB/PIT/Measurements/StrategyChoice entry
124 * is inserted or erased during enumeration.
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800125 */
HangZhang5d469422014-03-12 09:26:26 +0800126 const_iterator
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000127 begin() const
128 {
129 return this->getRange().begin();
130 }
HangZhang5d469422014-03-12 09:26:26 +0800131
Junxiao Shib30863e2016-08-15 21:32:01 +0000132 /** \return an iterator to the end
133 * \sa begin()
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800134 */
HangZhang5d469422014-03-12 09:26:26 +0800135 const_iterator
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000136 end() const
HangZhang5d469422014-03-12 09:26:26 +0800137 {
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000138 return this->getRange().end();
139 }
HangZhang5d469422014-03-12 09:26:26 +0800140
Junxiao Shic1e12362014-01-24 20:03:26 -0700141private:
Junxiao Shidd8f6612016-08-12 15:42:52 +0000142 /** \tparam K a parameter acceptable to NameTree::findLongestPrefixMatch
143 */
144 template<typename K>
Junxiao Shia6de4292016-07-12 02:08:10 +0000145 const Entry&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000146 findLongestPrefixMatchImpl(const K& key) const;
HangZhangcb4fc832014-03-11 16:57:11 +0800147
Junxiao Shiee5a4442014-07-27 17:13:43 -0700148 void
Junxiao Shi811c0102016-08-10 04:12:45 +0000149 erase(name_tree::Entry* nte, bool canDeleteNte = true);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700150
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000151 Range
152 getRange() const;
153
Junxiao Shiefceadc2014-03-09 18:52:57 -0700154private:
HangZhangad4afd12014-03-01 11:03:08 +0800155 NameTree& m_nameTree;
Junxiao Shi40631842014-03-01 13:52:37 -0700156 size_t m_nItems;
Junxiao Shiefceadc2014-03-09 18:52:57 -0700157
Junxiao Shia6de4292016-07-12 02:08:10 +0000158 /** \brief the empty FIB entry.
Junxiao Shi40631842014-03-01 13:52:37 -0700159 *
160 * This entry has no nexthops.
161 * It is returned by findLongestPrefixMatch if nothing is matched.
162 */
Junxiao Shia6de4292016-07-12 02:08:10 +0000163 static const unique_ptr<Entry> s_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -0700164};
165
Junxiao Shia6de4292016-07-12 02:08:10 +0000166} // namespace fib
167
168using fib::Fib;
169
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800170} // namespace nfd
Junxiao Shic1e12362014-01-24 20:03:26 -0700171
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700172#endif // NFD_DAEMON_TABLE_FIB_HPP