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