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 | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, Regents of the University of California, |
| 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; |
| 36 | } |
| 37 | namespace pit { |
| 38 | class Entry; |
| 39 | } |
| 40 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 41 | /** \class Fib |
| 42 | * \brief represents the FIB |
| 43 | */ |
| 44 | class Fib : noncopyable |
| 45 | { |
| 46 | public: |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 47 | explicit |
| 48 | Fib(NameTree& nameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 49 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 50 | ~Fib(); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 51 | |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame^] | 52 | size_t |
| 53 | size() const; |
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 | public: // lookup |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 56 | /// performs a longest prefix match |
| 57 | shared_ptr<fib::Entry> |
| 58 | findLongestPrefixMatch(const Name& prefix) const; |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 59 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 60 | /// performs a longest prefix match |
| 61 | shared_ptr<fib::Entry> |
| 62 | findLongestPrefixMatch(const pit::Entry& pitEntry) const; |
| 63 | |
| 64 | /// performs a longest prefix match |
| 65 | shared_ptr<fib::Entry> |
| 66 | findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const; |
| 67 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 68 | shared_ptr<fib::Entry> |
| 69 | findExactMatch(const Name& prefix) const; |
| 70 | |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame^] | 71 | public: // mutation |
| 72 | /** \brief inserts a FIB entry for prefix |
| 73 | * If an entry for exact same prefix exists, that entry is returned. |
| 74 | * \return{ the entry, and true for new entry, false for existing entry } |
| 75 | */ |
| 76 | std::pair<shared_ptr<fib::Entry>, bool> |
| 77 | insert(const Name& prefix); |
| 78 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 79 | void |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 80 | erase(const Name& prefix); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 81 | |
Steve DiBenedetto | d030cfc | 2014-03-10 20:04:47 -0600 | [diff] [blame] | 82 | void |
| 83 | erase(const fib::Entry& entry); |
| 84 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 85 | /** \brief removes the NextHop record for face in all entrites |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame^] | 86 | * |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 87 | * This is usually invoked when face goes away. |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame^] | 88 | * Removing the last NextHop in a FIB entry will erase the FIB entry. |
| 89 | * |
| 90 | * \todo change parameter type to Face& |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 91 | */ |
| 92 | void |
| 93 | removeNextHopFromAllEntries(shared_ptr<Face> face); |
| 94 | |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame^] | 95 | public: // enumeration |
| 96 | class const_iterator; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 97 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 98 | const_iterator |
| 99 | begin() const; |
| 100 | |
| 101 | const_iterator |
| 102 | end() const; |
| 103 | |
| 104 | class const_iterator : public std::iterator<std::forward_iterator_tag, fib::Entry> |
| 105 | { |
| 106 | public: |
| 107 | explicit |
| 108 | const_iterator(const NameTree::const_iterator& it); |
| 109 | |
| 110 | ~const_iterator(); |
| 111 | |
| 112 | const fib::Entry& |
| 113 | operator*() const; |
| 114 | |
| 115 | shared_ptr<fib::Entry> |
| 116 | operator->() const; |
| 117 | |
| 118 | const_iterator& |
| 119 | operator++(); |
| 120 | |
| 121 | const_iterator |
| 122 | operator++(int); |
| 123 | |
| 124 | bool |
| 125 | operator==(const const_iterator& other) const; |
| 126 | |
| 127 | bool |
| 128 | operator!=(const const_iterator& other) const; |
| 129 | |
| 130 | private: |
| 131 | NameTree::const_iterator m_nameTreeIterator; |
| 132 | }; |
| 133 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 134 | private: |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 135 | shared_ptr<fib::Entry> |
| 136 | findLongestPrefixMatch(shared_ptr<name_tree::Entry> nameTreeEntry) const; |
| 137 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 138 | void |
| 139 | erase(shared_ptr<name_tree::Entry> nameTreeEntry); |
| 140 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 141 | private: |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 142 | NameTree& m_nameTree; |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 143 | size_t m_nItems; |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 144 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 145 | /** \brief The empty FIB entry. |
| 146 | * |
| 147 | * This entry has no nexthops. |
| 148 | * It is returned by findLongestPrefixMatch if nothing is matched. |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame^] | 149 | * Returning empty entry instead of nullptr makes forwarding and strategy implementation easier. |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 150 | */ |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 151 | static const shared_ptr<fib::Entry> s_emptyEntry; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 154 | inline size_t |
| 155 | Fib::size() const |
| 156 | { |
| 157 | return m_nItems; |
| 158 | } |
| 159 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 160 | inline Fib::const_iterator |
| 161 | Fib::end() const |
| 162 | { |
| 163 | return const_iterator(m_nameTree.end()); |
| 164 | } |
| 165 | |
| 166 | inline |
| 167 | Fib::const_iterator::const_iterator(const NameTree::const_iterator& it) |
| 168 | : m_nameTreeIterator(it) |
| 169 | { |
| 170 | } |
| 171 | |
| 172 | inline |
| 173 | Fib::const_iterator::~const_iterator() |
| 174 | { |
| 175 | } |
| 176 | |
| 177 | inline |
| 178 | Fib::const_iterator |
| 179 | Fib::const_iterator::operator++(int) |
| 180 | { |
| 181 | Fib::const_iterator temp(*this); |
| 182 | ++(*this); |
| 183 | return temp; |
| 184 | } |
| 185 | |
| 186 | inline Fib::const_iterator& |
| 187 | Fib::const_iterator::operator++() |
| 188 | { |
| 189 | ++m_nameTreeIterator; |
| 190 | return *this; |
| 191 | } |
| 192 | |
| 193 | inline const fib::Entry& |
| 194 | Fib::const_iterator::operator*() const |
| 195 | { |
| 196 | return *(m_nameTreeIterator->getFibEntry()); |
| 197 | } |
| 198 | |
| 199 | inline shared_ptr<fib::Entry> |
| 200 | Fib::const_iterator::operator->() const |
| 201 | { |
| 202 | return m_nameTreeIterator->getFibEntry(); |
| 203 | } |
| 204 | |
| 205 | inline bool |
| 206 | Fib::const_iterator::operator==(const Fib::const_iterator& other) const |
| 207 | { |
| 208 | return m_nameTreeIterator == other.m_nameTreeIterator; |
| 209 | } |
| 210 | |
| 211 | inline bool |
| 212 | Fib::const_iterator::operator!=(const Fib::const_iterator& other) const |
| 213 | { |
| 214 | return m_nameTreeIterator != other.m_nameTreeIterator; |
| 215 | } |
| 216 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 217 | } // namespace nfd |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 218 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 219 | #endif // NFD_DAEMON_TABLE_FIB_HPP |