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 | 4370fde | 2016-02-24 12:20:46 -0700 | [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 | |
| 26 | #include "fib.hpp" |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 27 | #include "pit-entry.hpp" |
| 28 | #include "measurements-entry.hpp" |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 30 | #include <boost/concept/assert.hpp> |
| 31 | #include <boost/concept_check.hpp> |
| 32 | #include <type_traits> |
| 33 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 34 | namespace nfd { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 35 | namespace fib { |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 36 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 37 | const unique_ptr<Entry> Fib::s_emptyEntry = make_unique<Entry>(Name()); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 38 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 39 | // http://en.cppreference.com/w/cpp/concept/ForwardIterator |
| 40 | BOOST_CONCEPT_ASSERT((boost::ForwardIterator<Fib::const_iterator>)); |
| 41 | // boost::ForwardIterator follows SGI standard http://www.sgi.com/tech/stl/ForwardIterator.html, |
| 42 | // which doesn't require DefaultConstructible |
| 43 | #ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE |
| 44 | static_assert(std::is_default_constructible<Fib::const_iterator>::value, |
| 45 | "Fib::const_iterator must be default-constructible"); |
| 46 | #else |
| 47 | BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Fib::const_iterator>)); |
| 48 | #endif // HAVE_IS_DEFAULT_CONSTRUCTIBLE |
| 49 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 50 | Fib::Fib(NameTree& nameTree) |
| 51 | : m_nameTree(nameTree) |
| 52 | , m_nItems(0) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 53 | { |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | Fib::~Fib() |
| 57 | { |
| 58 | } |
| 59 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 60 | static inline bool |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 61 | predicate_NameTreeEntry_hasFibEntry(const name_tree::Entry& nte) |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 62 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 63 | return nte.getFibEntry() != nullptr; |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 66 | const Entry& |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 67 | Fib::findLongestPrefixMatch(const Name& prefix) const |
| 68 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 69 | shared_ptr<name_tree::Entry> nte = |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 70 | m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasFibEntry); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 71 | if (nte != nullptr) { |
| 72 | return *nte->getFibEntry(); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 73 | } |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 74 | return *s_emptyEntry; |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 75 | } |
| 76 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 77 | const Entry& |
| 78 | Fib::findLongestPrefixMatch(shared_ptr<name_tree::Entry> nte) const |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 79 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 80 | Entry* entry = nte->getFibEntry(); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 81 | if (entry != nullptr) |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 82 | return *entry; |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 83 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 84 | nte = m_nameTree.findLongestPrefixMatch(nte, &predicate_NameTreeEntry_hasFibEntry); |
| 85 | if (nte != nullptr) { |
| 86 | return *nte->getFibEntry(); |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 87 | } |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 88 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 89 | return *s_emptyEntry; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 92 | const Entry& |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 93 | Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const |
| 94 | { |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 95 | shared_ptr<name_tree::Entry> nte = m_nameTree.findLongestPrefixMatch(pitEntry); |
| 96 | BOOST_ASSERT(nte != nullptr); |
| 97 | return findLongestPrefixMatch(nte); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 100 | const Entry& |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 101 | Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const |
| 102 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 103 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(measurementsEntry); |
| 104 | BOOST_ASSERT(nte != nullptr); |
| 105 | return findLongestPrefixMatch(nte); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 108 | Entry* |
| 109 | Fib::findExactMatch(const Name& prefix) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 110 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 111 | shared_ptr<name_tree::Entry> nte = m_nameTree.findExactMatch(prefix); |
| 112 | if (nte != nullptr) |
| 113 | return nte->getFibEntry(); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 114 | |
| 115 | return nullptr; |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 118 | std::pair<Entry*, bool> |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 119 | Fib::insert(const Name& prefix) |
| 120 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 121 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(prefix); |
| 122 | Entry* entry = nte->getFibEntry(); |
| 123 | if (entry != nullptr) { |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 124 | return std::make_pair(entry, false); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 125 | } |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 126 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 127 | nte->setFibEntry(make_unique<Entry>(prefix)); |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 128 | ++m_nItems; |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 129 | return std::make_pair(nte->getFibEntry(), true); |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 132 | void |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 133 | Fib::erase(shared_ptr<name_tree::Entry> nte, bool canDeleteNte) |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 134 | { |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 135 | BOOST_ASSERT(nte != nullptr); |
| 136 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 137 | nte->setFibEntry(nullptr); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 138 | if (canDeleteNte) { |
| 139 | m_nameTree.eraseEntryIfEmpty(nte); |
| 140 | } |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 141 | --m_nItems; |
| 142 | } |
| 143 | |
| 144 | void |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 145 | Fib::erase(const Name& prefix) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 146 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 147 | shared_ptr<name_tree::Entry> nte = m_nameTree.findExactMatch(prefix); |
| 148 | if (nte != nullptr) { |
| 149 | this->erase(nte); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 154 | Fib::erase(const Entry& entry) |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 155 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 156 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(entry); |
| 157 | if (nte != nullptr) { |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 158 | // don't try to erase s_emptyEntry |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 159 | this->erase(nte); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 160 | } |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 164 | Fib::removeNextHop(Entry& entry, const Face& face) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 165 | { |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 166 | entry.removeNextHop(face); |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 167 | |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 168 | if (!entry.hasNextHops()) { |
| 169 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(entry); |
| 170 | this->erase(nte, false); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 171 | } |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 172 | } |
| 173 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 174 | Fib::const_iterator |
| 175 | Fib::begin() const |
| 176 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 177 | return const_iterator(m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasFibEntry).begin()); |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 178 | } |
| 179 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 180 | } // namespace fib |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 181 | } // namespace nfd |