Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 7530635 | 2018-02-01 21:59:44 +0000 | [diff] [blame] | 2 | /* |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_TABLE_FIB_HPP |
| 27 | #define NFD_DAEMON_TABLE_FIB_HPP |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 28 | |
| 29 | #include "fib-entry.hpp" |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 30 | #include "name-tree.hpp" |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 31 | |
Junxiao Shi | 7530635 | 2018-02-01 21:59:44 +0000 | [diff] [blame] | 32 | #include "core/fib-max-depth.hpp" |
| 33 | |
Junxiao Shi | 13ad4b7 | 2016-08-15 04:51:21 +0000 | [diff] [blame] | 34 | #include <boost/range/adaptor/transformed.hpp> |
| 35 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 36 | namespace nfd { |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 37 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 38 | namespace measurements { |
| 39 | class Entry; |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 40 | } // namespace measurements |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 41 | namespace pit { |
| 42 | class Entry; |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 43 | } // namespace pit |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 44 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 45 | namespace fib { |
| 46 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame^] | 47 | /** \brief Represents the Forwarding Information Base (FIB) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 48 | */ |
| 49 | class Fib : noncopyable |
| 50 | { |
| 51 | public: |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 52 | explicit |
| 53 | Fib(NameTree& nameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 54 | |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 55 | size_t |
Junxiao Shi | 13ad4b7 | 2016-08-15 04:51:21 +0000 | [diff] [blame] | 56 | size() const |
| 57 | { |
| 58 | return m_nItems; |
| 59 | } |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 60 | |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 61 | public: // lookup |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame^] | 62 | /** \brief Performs a longest prefix match |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 63 | */ |
| 64 | const Entry& |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 65 | findLongestPrefixMatch(const Name& prefix) const; |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 66 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame^] | 67 | /** \brief Performs a longest prefix match |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 68 | * |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame^] | 69 | * This is equivalent to `findLongestPrefixMatch(pitEntry.getName())` |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 70 | */ |
| 71 | const Entry& |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 72 | findLongestPrefixMatch(const pit::Entry& pitEntry) const; |
| 73 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame^] | 74 | /** \brief Performs a longest prefix match |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 75 | * |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame^] | 76 | * This is equivalent to `findLongestPrefixMatch(measurementsEntry.getName())` |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 77 | */ |
| 78 | const Entry& |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 79 | findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const; |
| 80 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame^] | 81 | /** \brief Performs an exact match lookup |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 82 | */ |
| 83 | Entry* |
| 84 | findExactMatch(const Name& prefix); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 85 | |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 86 | public: // mutation |
Junxiao Shi | 7530635 | 2018-02-01 21:59:44 +0000 | [diff] [blame] | 87 | /** \brief Maximum number of components in a FIB entry prefix. |
Junxiao Shi | 7530635 | 2018-02-01 21:59:44 +0000 | [diff] [blame] | 88 | */ |
| 89 | static constexpr size_t |
| 90 | getMaxDepth() |
| 91 | { |
Junxiao Shi | 7530635 | 2018-02-01 21:59:44 +0000 | [diff] [blame] | 92 | return FIB_MAX_DEPTH; |
| 93 | } |
| 94 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame^] | 95 | /** \brief Find or insert a FIB entry |
| 96 | * \param prefix FIB entry name; it must not have more than \c getMaxDepth() components. |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 97 | * \return the entry, and true for new entry or false for existing entry |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 98 | */ |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 99 | std::pair<Entry*, bool> |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 100 | insert(const Name& prefix); |
| 101 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 102 | void |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 103 | erase(const Name& prefix); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 104 | |
Steve DiBenedetto | d030cfc | 2014-03-10 20:04:47 -0600 | [diff] [blame] | 105 | void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 106 | erase(const Entry& entry); |
Steve DiBenedetto | d030cfc | 2014-03-10 20:04:47 -0600 | [diff] [blame] | 107 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame^] | 108 | /** \brief Remove the NextHop record for the given \p face and \p endpointId |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 109 | */ |
| 110 | void |
ashiqopu | 77d0bfd | 2019-02-20 20:37:31 +0000 | [diff] [blame] | 111 | removeNextHop(Entry& entry, const Face& face, EndpointId endpointId); |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 112 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame^] | 113 | /** \brief Remove all NextHop records for \p face |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 114 | */ |
| 115 | void |
| 116 | removeNextHopByFace(Entry& entry, const Face& face); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 117 | |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 118 | public: // enumeration |
Junxiao Shi | 13ad4b7 | 2016-08-15 04:51:21 +0000 | [diff] [blame] | 119 | typedef boost::transformed_range<name_tree::GetTableEntry<Entry>, const name_tree::Range> Range; |
| 120 | typedef boost::range_iterator<Range>::type const_iterator; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 121 | |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame] | 122 | /** \return an iterator to the beginning |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame^] | 123 | * \note The iteration order is implementation-defined. |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame] | 124 | * \warning Undefined behavior may occur if a FIB/PIT/Measurements/StrategyChoice entry |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame^] | 125 | * is inserted or erased during iteration. |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 126 | */ |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 127 | const_iterator |
Junxiao Shi | 13ad4b7 | 2016-08-15 04:51:21 +0000 | [diff] [blame] | 128 | begin() const |
| 129 | { |
| 130 | return this->getRange().begin(); |
| 131 | } |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 132 | |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame] | 133 | /** \return an iterator to the end |
| 134 | * \sa begin() |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 135 | */ |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 136 | const_iterator |
Junxiao Shi | 13ad4b7 | 2016-08-15 04:51:21 +0000 | [diff] [blame] | 137 | end() const |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 138 | { |
Junxiao Shi | 13ad4b7 | 2016-08-15 04:51:21 +0000 | [diff] [blame] | 139 | return this->getRange().end(); |
| 140 | } |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 141 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 142 | private: |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 143 | /** \tparam K a parameter acceptable to NameTree::findLongestPrefixMatch |
| 144 | */ |
| 145 | template<typename K> |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 146 | const Entry& |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 147 | findLongestPrefixMatchImpl(const K& key) const; |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 148 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 149 | void |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 150 | erase(name_tree::Entry* nte, bool canDeleteNte = true); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 151 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame^] | 152 | /** \brief Erase \p entry if it contains no nexthop records |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 153 | */ |
| 154 | void |
| 155 | eraseIfEmpty(Entry& entry); |
| 156 | |
Junxiao Shi | 13ad4b7 | 2016-08-15 04:51:21 +0000 | [diff] [blame] | 157 | Range |
| 158 | getRange() const; |
| 159 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 160 | private: |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 161 | NameTree& m_nameTree; |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame^] | 162 | size_t m_nItems = 0; |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 163 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame^] | 164 | /** \brief The empty FIB entry. |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 165 | * |
| 166 | * This entry has no nexthops. |
| 167 | * It is returned by findLongestPrefixMatch if nothing is matched. |
| 168 | */ |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 169 | static const unique_ptr<Entry> s_emptyEntry; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 172 | } // namespace fib |
| 173 | |
| 174 | using fib::Fib; |
| 175 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 176 | } // namespace nfd |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 177 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 178 | #endif // NFD_DAEMON_TABLE_FIB_HPP |