Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 32 | namespace nfd { |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 33 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 34 | namespace measurements { |
| 35 | class Entry; |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 36 | } // namespace measurements |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 37 | namespace pit { |
| 38 | class Entry; |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 39 | } // namespace pit |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 40 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 41 | namespace fib { |
| 42 | |
| 43 | /** \brief represents the Forwarding Information Base (FIB) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 44 | */ |
| 45 | class Fib : noncopyable |
| 46 | { |
| 47 | public: |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 48 | explicit |
| 49 | Fib(NameTree& nameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 50 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 51 | ~Fib(); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 52 | |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 53 | size_t |
| 54 | size() const; |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 55 | |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 56 | public: // lookup |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 57 | /** \brief performs a longest prefix match |
| 58 | */ |
| 59 | const Entry& |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 60 | findLongestPrefixMatch(const Name& prefix) const; |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 61 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 62 | /** \brief performs a longest prefix match |
| 63 | * |
| 64 | * This is equivalent to .findLongestPrefixMatch(pitEntry.getName()) |
| 65 | */ |
| 66 | const Entry& |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 67 | findLongestPrefixMatch(const pit::Entry& pitEntry) const; |
| 68 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 69 | /** \brief performs a longest prefix match |
| 70 | * |
| 71 | * This is equivalent to .findLongestPrefixMatch(measurementsEntry.getName()) |
| 72 | */ |
| 73 | const Entry& |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 74 | findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const; |
| 75 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 76 | /** \brief performs an exact match lookup |
| 77 | */ |
| 78 | Entry* |
| 79 | findExactMatch(const Name& prefix); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 80 | |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 81 | public: // mutation |
| 82 | /** \brief inserts a FIB entry for prefix |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 83 | * |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 84 | * If an entry for exact same prefix exists, that entry is returned. |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 85 | * \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] | 86 | */ |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 87 | std::pair<Entry*, bool> |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 88 | insert(const Name& prefix); |
| 89 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 90 | void |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 91 | erase(const Name& prefix); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 92 | |
Steve DiBenedetto | d030cfc | 2014-03-10 20:04:47 -0600 | [diff] [blame] | 93 | void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 94 | erase(const Entry& entry); |
Steve DiBenedetto | d030cfc | 2014-03-10 20:04:47 -0600 | [diff] [blame] | 95 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 96 | /** \brief removes the NextHop record for face in all entries |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 97 | * |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 98 | * This is usually invoked when face is destroyed. |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 99 | * Removing the last NextHop in a FIB entry will erase the FIB entry. |
| 100 | * |
| 101 | * \todo change parameter type to Face& |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 102 | */ |
| 103 | void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 104 | removeNextHopFromAllEntries(const Face& face); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 105 | |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 106 | public: // enumeration |
| 107 | class const_iterator; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 108 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 109 | /** \brief returns an iterator pointing to the first FIB entry |
| 110 | * \note Iteration order is implementation-specific and is undefined |
| 111 | * \note The returned iterator may get invalidated if FIB or another NameTree-based |
| 112 | * table is modified |
| 113 | */ |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 114 | const_iterator |
| 115 | begin() const; |
| 116 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 117 | /** \brief returns an iterator referring to the past-the-end FIB entry |
| 118 | * \note The returned iterator may get invalidated if FIB or another NameTree-based |
| 119 | * table is modified |
| 120 | */ |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 121 | const_iterator |
| 122 | end() const; |
| 123 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 124 | class const_iterator : public std::iterator<std::forward_iterator_tag, const Entry> |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 125 | { |
| 126 | public: |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 127 | const_iterator() = default; |
| 128 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 129 | explicit |
| 130 | const_iterator(const NameTree::const_iterator& it); |
| 131 | |
| 132 | ~const_iterator(); |
| 133 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 134 | const Entry& |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 135 | operator*() const; |
| 136 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 137 | const Entry* |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 138 | operator->() const; |
| 139 | |
| 140 | const_iterator& |
| 141 | operator++(); |
| 142 | |
| 143 | const_iterator |
| 144 | operator++(int); |
| 145 | |
| 146 | bool |
| 147 | operator==(const const_iterator& other) const; |
| 148 | |
| 149 | bool |
| 150 | operator!=(const const_iterator& other) const; |
| 151 | |
| 152 | private: |
| 153 | NameTree::const_iterator m_nameTreeIterator; |
| 154 | }; |
| 155 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 156 | private: |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 157 | const Entry& |
| 158 | findLongestPrefixMatch(shared_ptr<name_tree::Entry> nte) const; |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 159 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 160 | void |
| 161 | erase(shared_ptr<name_tree::Entry> nameTreeEntry); |
| 162 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 163 | private: |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 164 | NameTree& m_nameTree; |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 165 | size_t m_nItems; |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 166 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 167 | /** \brief the empty FIB entry. |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 168 | * |
| 169 | * This entry has no nexthops. |
| 170 | * It is returned by findLongestPrefixMatch if nothing is matched. |
| 171 | */ |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 172 | static const unique_ptr<Entry> s_emptyEntry; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 175 | inline size_t |
| 176 | Fib::size() const |
| 177 | { |
| 178 | return m_nItems; |
| 179 | } |
| 180 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 181 | inline Fib::const_iterator |
| 182 | Fib::end() const |
| 183 | { |
| 184 | return const_iterator(m_nameTree.end()); |
| 185 | } |
| 186 | |
| 187 | inline |
| 188 | Fib::const_iterator::const_iterator(const NameTree::const_iterator& it) |
| 189 | : m_nameTreeIterator(it) |
| 190 | { |
| 191 | } |
| 192 | |
| 193 | inline |
| 194 | Fib::const_iterator::~const_iterator() |
| 195 | { |
| 196 | } |
| 197 | |
| 198 | inline |
| 199 | Fib::const_iterator |
| 200 | Fib::const_iterator::operator++(int) |
| 201 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 202 | const_iterator temp(*this); |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 203 | ++(*this); |
| 204 | return temp; |
| 205 | } |
| 206 | |
| 207 | inline Fib::const_iterator& |
| 208 | Fib::const_iterator::operator++() |
| 209 | { |
| 210 | ++m_nameTreeIterator; |
| 211 | return *this; |
| 212 | } |
| 213 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 214 | inline const Entry& |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 215 | Fib::const_iterator::operator*() const |
| 216 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 217 | return *m_nameTreeIterator->getFibEntry(); |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 218 | } |
| 219 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 220 | inline const Entry* |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 221 | Fib::const_iterator::operator->() const |
| 222 | { |
| 223 | return m_nameTreeIterator->getFibEntry(); |
| 224 | } |
| 225 | |
| 226 | inline bool |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 227 | Fib::const_iterator::operator==(const const_iterator& other) const |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 228 | { |
| 229 | return m_nameTreeIterator == other.m_nameTreeIterator; |
| 230 | } |
| 231 | |
| 232 | inline bool |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 233 | Fib::const_iterator::operator!=(const const_iterator& other) const |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 234 | { |
| 235 | return m_nameTreeIterator != other.m_nameTreeIterator; |
| 236 | } |
| 237 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 238 | } // namespace fib |
| 239 | |
| 240 | using fib::Fib; |
| 241 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 242 | } // namespace nfd |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 243 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 244 | #endif // NFD_DAEMON_TABLE_FIB_HPP |