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