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 | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame^] | 96 | /** \brief removes the NextHop record for face |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 97 | */ |
| 98 | void |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame^] | 99 | removeNextHop(Entry& entry, const Face& face); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 100 | |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 101 | public: // enumeration |
| 102 | class const_iterator; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 104 | /** \brief returns an iterator pointing to the first FIB entry |
| 105 | * \note Iteration order is implementation-specific and is undefined |
| 106 | * \note The returned iterator may get invalidated if FIB or another NameTree-based |
| 107 | * table is modified |
| 108 | */ |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 109 | const_iterator |
| 110 | begin() const; |
| 111 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 112 | /** \brief returns an iterator referring to the past-the-end FIB entry |
| 113 | * \note The returned iterator may get invalidated if FIB or another NameTree-based |
| 114 | * table is modified |
| 115 | */ |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 116 | const_iterator |
| 117 | end() const; |
| 118 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 119 | class const_iterator : public std::iterator<std::forward_iterator_tag, const Entry> |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 120 | { |
| 121 | public: |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 122 | const_iterator() = default; |
| 123 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 124 | explicit |
| 125 | const_iterator(const NameTree::const_iterator& it); |
| 126 | |
| 127 | ~const_iterator(); |
| 128 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 129 | const Entry& |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 130 | operator*() const; |
| 131 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 132 | const Entry* |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 133 | operator->() const; |
| 134 | |
| 135 | const_iterator& |
| 136 | operator++(); |
| 137 | |
| 138 | const_iterator |
| 139 | operator++(int); |
| 140 | |
| 141 | bool |
| 142 | operator==(const const_iterator& other) const; |
| 143 | |
| 144 | bool |
| 145 | operator!=(const const_iterator& other) const; |
| 146 | |
| 147 | private: |
| 148 | NameTree::const_iterator m_nameTreeIterator; |
| 149 | }; |
| 150 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 151 | private: |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 152 | const Entry& |
| 153 | findLongestPrefixMatch(shared_ptr<name_tree::Entry> nte) const; |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 154 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 155 | void |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame^] | 156 | erase(shared_ptr<name_tree::Entry> nte, bool canDeleteNte = true); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 157 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 158 | private: |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 159 | NameTree& m_nameTree; |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 160 | size_t m_nItems; |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 161 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 162 | /** \brief the empty FIB entry. |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 163 | * |
| 164 | * This entry has no nexthops. |
| 165 | * It is returned by findLongestPrefixMatch if nothing is matched. |
| 166 | */ |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 167 | static const unique_ptr<Entry> s_emptyEntry; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 170 | inline size_t |
| 171 | Fib::size() const |
| 172 | { |
| 173 | return m_nItems; |
| 174 | } |
| 175 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 176 | inline Fib::const_iterator |
| 177 | Fib::end() const |
| 178 | { |
| 179 | return const_iterator(m_nameTree.end()); |
| 180 | } |
| 181 | |
| 182 | inline |
| 183 | Fib::const_iterator::const_iterator(const NameTree::const_iterator& it) |
| 184 | : m_nameTreeIterator(it) |
| 185 | { |
| 186 | } |
| 187 | |
| 188 | inline |
| 189 | Fib::const_iterator::~const_iterator() |
| 190 | { |
| 191 | } |
| 192 | |
| 193 | inline |
| 194 | Fib::const_iterator |
| 195 | Fib::const_iterator::operator++(int) |
| 196 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 197 | const_iterator temp(*this); |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 198 | ++(*this); |
| 199 | return temp; |
| 200 | } |
| 201 | |
| 202 | inline Fib::const_iterator& |
| 203 | Fib::const_iterator::operator++() |
| 204 | { |
| 205 | ++m_nameTreeIterator; |
| 206 | return *this; |
| 207 | } |
| 208 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 209 | inline const Entry& |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 210 | Fib::const_iterator::operator*() const |
| 211 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 212 | return *m_nameTreeIterator->getFibEntry(); |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 213 | } |
| 214 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 215 | inline const Entry* |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 216 | Fib::const_iterator::operator->() const |
| 217 | { |
| 218 | return m_nameTreeIterator->getFibEntry(); |
| 219 | } |
| 220 | |
| 221 | inline bool |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 222 | Fib::const_iterator::operator==(const const_iterator& other) const |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 223 | { |
| 224 | return m_nameTreeIterator == other.m_nameTreeIterator; |
| 225 | } |
| 226 | |
| 227 | inline bool |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 228 | Fib::const_iterator::operator!=(const const_iterator& other) const |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 229 | { |
| 230 | return m_nameTreeIterator != other.m_nameTreeIterator; |
| 231 | } |
| 232 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 233 | } // namespace fib |
| 234 | |
| 235 | using fib::Fib; |
| 236 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 237 | } // namespace nfd |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 238 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 239 | #endif // NFD_DAEMON_TABLE_FIB_HPP |