Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 24 | |
| 25 | #include "fib.hpp" |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 26 | #include "pit-entry.hpp" |
| 27 | #include "measurements-entry.hpp" |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 29 | namespace nfd { |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 30 | |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 31 | 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] | 32 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 33 | Fib::Fib(NameTree& nameTree) |
| 34 | : m_nameTree(nameTree) |
| 35 | , m_nItems(0) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 36 | { |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | Fib::~Fib() |
| 40 | { |
| 41 | } |
| 42 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 43 | static inline bool |
| 44 | predicate_NameTreeEntry_hasFibEntry(const name_tree::Entry& entry) |
| 45 | { |
| 46 | return static_cast<bool>(entry.getFibEntry()); |
| 47 | } |
| 48 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 49 | std::pair<shared_ptr<fib::Entry>, bool> |
| 50 | Fib::insert(const Name& prefix) |
| 51 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 52 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix); |
| 53 | shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry(); |
| 54 | if (static_cast<bool>(entry)) |
| 55 | return std::make_pair(entry, false); |
| 56 | entry = make_shared<fib::Entry>(prefix); |
| 57 | nameTreeEntry->setFibEntry(entry); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 58 | ++m_nItems; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 59 | return std::make_pair(entry, true); |
| 60 | } |
| 61 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 62 | shared_ptr<fib::Entry> |
| 63 | Fib::findLongestPrefixMatch(const Name& prefix) const |
| 64 | { |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 65 | shared_ptr<name_tree::Entry> nameTreeEntry = |
| 66 | m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasFibEntry); |
| 67 | if (static_cast<bool>(nameTreeEntry)) { |
| 68 | return nameTreeEntry->getFibEntry(); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 69 | } |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 70 | return s_emptyEntry; |
| 71 | } |
| 72 | |
| 73 | shared_ptr<fib::Entry> |
| 74 | Fib::findLongestPrefixMatch(shared_ptr<name_tree::Entry> nameTreeEntry) const |
| 75 | { |
| 76 | shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry(); |
| 77 | if (static_cast<bool>(entry)) |
| 78 | return entry; |
| 79 | nameTreeEntry = m_nameTree.findLongestPrefixMatch(nameTreeEntry, |
| 80 | &predicate_NameTreeEntry_hasFibEntry); |
| 81 | if (static_cast<bool>(nameTreeEntry)) { |
| 82 | return nameTreeEntry->getFibEntry(); |
| 83 | } |
| 84 | return s_emptyEntry; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 87 | shared_ptr<fib::Entry> |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 88 | Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const |
| 89 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 90 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry); |
| 91 | |
| 92 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 93 | |
| 94 | return findLongestPrefixMatch(nameTreeEntry); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | shared_ptr<fib::Entry> |
| 98 | Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const |
| 99 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 100 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(measurementsEntry); |
| 101 | |
| 102 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 103 | |
| 104 | return findLongestPrefixMatch(nameTreeEntry); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | shared_ptr<fib::Entry> |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 108 | Fib::findExactMatch(const Name& prefix) const |
| 109 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 110 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 111 | if (static_cast<bool>(nameTreeEntry)) |
| 112 | return nameTreeEntry->getFibEntry(); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 113 | return shared_ptr<fib::Entry>(); |
| 114 | } |
| 115 | |
| 116 | void |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 117 | Fib::erase(const Name& prefix) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 118 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 119 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 120 | if (static_cast<bool>(nameTreeEntry)) |
| 121 | { |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 122 | nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>()); |
| 123 | --m_nItems; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | Fib::erase(const fib::Entry& entry) |
| 129 | { |
| 130 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(entry); |
| 131 | if (static_cast<bool>(nameTreeEntry)) |
| 132 | { |
| 133 | nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>()); |
| 134 | --m_nItems; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 135 | } |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void |
| 139 | Fib::removeNextHopFromAllEntries(shared_ptr<Face> face) |
| 140 | { |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 141 | for (NameTree::const_iterator it = m_nameTree.fullEnumerate( |
| 142 | &predicate_NameTreeEntry_hasFibEntry); it != m_nameTree.end(); ++it) { |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 143 | shared_ptr<fib::Entry> entry = it->getFibEntry(); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 144 | entry->removeNextHop(face); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 145 | if (!entry->hasNextHops()) { |
| 146 | this->erase(*entry); |
| 147 | } |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 148 | } |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 149 | } |
| 150 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 151 | Fib::const_iterator |
| 152 | Fib::begin() const |
| 153 | { |
| 154 | return const_iterator(m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasFibEntry)); |
| 155 | } |
| 156 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 157 | } // namespace nfd |