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 | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 30 | namespace nfd { |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 31 | |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 32 | 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] | 33 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 34 | Fib::Fib(NameTree& nameTree) |
| 35 | : m_nameTree(nameTree) |
| 36 | , m_nItems(0) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 37 | { |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | Fib::~Fib() |
| 41 | { |
| 42 | } |
| 43 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 44 | static inline bool |
| 45 | predicate_NameTreeEntry_hasFibEntry(const name_tree::Entry& entry) |
| 46 | { |
| 47 | return static_cast<bool>(entry.getFibEntry()); |
| 48 | } |
| 49 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 50 | shared_ptr<fib::Entry> |
| 51 | Fib::findLongestPrefixMatch(const Name& prefix) const |
| 52 | { |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 53 | shared_ptr<name_tree::Entry> nameTreeEntry = |
| 54 | m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasFibEntry); |
| 55 | if (static_cast<bool>(nameTreeEntry)) { |
| 56 | return nameTreeEntry->getFibEntry(); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 57 | } |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 58 | return s_emptyEntry; |
| 59 | } |
| 60 | |
| 61 | shared_ptr<fib::Entry> |
| 62 | Fib::findLongestPrefixMatch(shared_ptr<name_tree::Entry> nameTreeEntry) const |
| 63 | { |
| 64 | shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry(); |
| 65 | if (static_cast<bool>(entry)) |
| 66 | return entry; |
| 67 | nameTreeEntry = m_nameTree.findLongestPrefixMatch(nameTreeEntry, |
| 68 | &predicate_NameTreeEntry_hasFibEntry); |
| 69 | if (static_cast<bool>(nameTreeEntry)) { |
| 70 | return nameTreeEntry->getFibEntry(); |
| 71 | } |
| 72 | return s_emptyEntry; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 75 | shared_ptr<fib::Entry> |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 76 | Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const |
| 77 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 78 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry); |
| 79 | |
| 80 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 81 | |
| 82 | return findLongestPrefixMatch(nameTreeEntry); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | shared_ptr<fib::Entry> |
| 86 | Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const |
| 87 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 88 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(measurementsEntry); |
| 89 | |
| 90 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 91 | |
| 92 | return findLongestPrefixMatch(nameTreeEntry); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | shared_ptr<fib::Entry> |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 96 | Fib::findExactMatch(const Name& prefix) const |
| 97 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 98 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 99 | if (static_cast<bool>(nameTreeEntry)) |
| 100 | return nameTreeEntry->getFibEntry(); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 101 | return shared_ptr<fib::Entry>(); |
| 102 | } |
| 103 | |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 104 | std::pair<shared_ptr<fib::Entry>, bool> |
| 105 | Fib::insert(const Name& prefix) |
| 106 | { |
| 107 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix); |
| 108 | shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry(); |
| 109 | if (static_cast<bool>(entry)) |
| 110 | return std::make_pair(entry, false); |
| 111 | entry = make_shared<fib::Entry>(prefix); |
| 112 | nameTreeEntry->setFibEntry(entry); |
| 113 | ++m_nItems; |
| 114 | return std::make_pair(entry, true); |
| 115 | } |
| 116 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 117 | void |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 118 | Fib::erase(shared_ptr<name_tree::Entry> nameTreeEntry) |
| 119 | { |
| 120 | nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>()); |
| 121 | m_nameTree.eraseEntryIfEmpty(nameTreeEntry); |
| 122 | --m_nItems; |
| 123 | } |
| 124 | |
| 125 | void |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 126 | Fib::erase(const Name& prefix) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 127 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 128 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 129 | if (static_cast<bool>(nameTreeEntry)) { |
| 130 | this->erase(nameTreeEntry); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | Fib::erase(const fib::Entry& entry) |
| 136 | { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 137 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(entry); |
| 138 | if (static_cast<bool>(nameTreeEntry)) { |
| 139 | this->erase(nameTreeEntry); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 140 | } |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void |
| 144 | Fib::removeNextHopFromAllEntries(shared_ptr<Face> face) |
| 145 | { |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 146 | for (NameTree::const_iterator it = m_nameTree.fullEnumerate( |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 147 | &predicate_NameTreeEntry_hasFibEntry); it != m_nameTree.end();) { |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 148 | shared_ptr<fib::Entry> entry = it->getFibEntry(); |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 149 | |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 150 | ++it; // advance the iterator before `erase` invalidates it |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 151 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 152 | entry->removeNextHop(face); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 153 | if (!entry->hasNextHops()) { |
| 154 | this->erase(*entry); |
| 155 | } |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 156 | } |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 157 | } |
| 158 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 159 | Fib::const_iterator |
| 160 | Fib::begin() const |
| 161 | { |
| 162 | return const_iterator(m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasFibEntry)); |
| 163 | } |
| 164 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 165 | } // namespace nfd |