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