Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 981db80 | 2018-04-10 18:05:04 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, 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" |
Davide Pesavento | 981db80 | 2018-04-10 18:05:04 -0400 | [diff] [blame] | 29 | |
| 30 | #include <ndn-cxx/util/concepts.hpp> |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 32 | namespace nfd { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 33 | namespace fib { |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 34 | |
Davide Pesavento | 981db80 | 2018-04-10 18:05:04 -0400 | [diff] [blame] | 35 | NDN_CXX_ASSERT_FORWARD_ITERATOR(Fib::const_iterator); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 36 | |
Junxiao Shi | 3aba754 | 2016-12-24 02:42:52 +0000 | [diff] [blame] | 37 | const unique_ptr<Entry> Fib::s_emptyEntry = make_unique<Entry>(Name()); |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 38 | |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 39 | static inline bool |
| 40 | nteHasFibEntry(const name_tree::Entry& nte) |
| 41 | { |
| 42 | return nte.getFibEntry() != nullptr; |
| 43 | } |
| 44 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 45 | Fib::Fib(NameTree& nameTree) |
| 46 | : m_nameTree(nameTree) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 47 | { |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 50 | template<typename K> |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 51 | const Entry& |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 52 | Fib::findLongestPrefixMatchImpl(const K& key) const |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 53 | { |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 54 | name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasFibEntry); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 55 | if (nte != nullptr) { |
| 56 | return *nte->getFibEntry(); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 57 | } |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 58 | return *s_emptyEntry; |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 59 | } |
| 60 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 61 | const Entry& |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 62 | Fib::findLongestPrefixMatch(const Name& prefix) const |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 63 | { |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 64 | return this->findLongestPrefixMatchImpl(prefix); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 67 | const Entry& |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 68 | Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const |
| 69 | { |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 70 | return this->findLongestPrefixMatchImpl(pitEntry); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 73 | const Entry& |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 74 | Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const |
| 75 | { |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 76 | return this->findLongestPrefixMatchImpl(measurementsEntry); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 79 | Entry* |
| 80 | Fib::findExactMatch(const Name& prefix) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 81 | { |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 82 | name_tree::Entry* nte = m_nameTree.findExactMatch(prefix); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 83 | if (nte != nullptr) |
| 84 | return nte->getFibEntry(); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 85 | |
| 86 | return nullptr; |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 89 | std::pair<Entry*, bool> |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 90 | Fib::insert(const Name& prefix) |
| 91 | { |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 92 | name_tree::Entry& nte = m_nameTree.lookup(prefix); |
| 93 | Entry* entry = nte.getFibEntry(); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 94 | if (entry != nullptr) { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 95 | return {entry, false}; |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 96 | } |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 97 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 98 | nte.setFibEntry(make_unique<Entry>(prefix)); |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 99 | ++m_nItems; |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 100 | return {nte.getFibEntry(), true}; |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 103 | void |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 104 | Fib::erase(name_tree::Entry* nte, bool canDeleteNte) |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 105 | { |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 106 | BOOST_ASSERT(nte != nullptr); |
| 107 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 108 | nte->setFibEntry(nullptr); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 109 | if (canDeleteNte) { |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 110 | m_nameTree.eraseIfEmpty(nte); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 111 | } |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 112 | --m_nItems; |
| 113 | } |
| 114 | |
| 115 | void |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 116 | Fib::erase(const Name& prefix) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 117 | { |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 118 | name_tree::Entry* nte = m_nameTree.findExactMatch(prefix); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 119 | if (nte != nullptr) { |
| 120 | this->erase(nte); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 125 | Fib::erase(const Entry& entry) |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 126 | { |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 127 | name_tree::Entry* nte = m_nameTree.getEntry(entry); |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 128 | if (nte == nullptr) { // don't try to erase s_emptyEntry |
| 129 | BOOST_ASSERT(&entry == s_emptyEntry.get()); |
| 130 | return; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 131 | } |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 132 | this->erase(nte); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 136 | Fib::addOrUpdateNextHop(Entry& entry, Face& face, uint64_t cost) |
| 137 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 138 | auto [it, isNew] = entry.addOrUpdateNextHop(face, cost); |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 139 | if (isNew) |
| 140 | this->afterNewNextHop(entry.getPrefix(), *it); |
| 141 | } |
| 142 | |
| 143 | Fib::RemoveNextHopResult |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 144 | Fib::removeNextHop(Entry& entry, const Face& face) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 145 | { |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 146 | bool isRemoved = entry.removeNextHop(face); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 147 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 148 | if (!isRemoved) { |
| 149 | return RemoveNextHopResult::NO_SUCH_NEXTHOP; |
| 150 | } |
| 151 | else if (!entry.hasNextHops()) { |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 152 | name_tree::Entry* nte = m_nameTree.getEntry(entry); |
| 153 | this->erase(nte, false); |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 154 | return RemoveNextHopResult::FIB_ENTRY_REMOVED; |
| 155 | } |
| 156 | else { |
| 157 | return RemoveNextHopResult::NEXTHOP_REMOVED; |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 158 | } |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Junxiao Shi | 13ad4b7 | 2016-08-15 04:51:21 +0000 | [diff] [blame] | 161 | Fib::Range |
| 162 | Fib::getRange() const |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 163 | { |
Junxiao Shi | 13ad4b7 | 2016-08-15 04:51:21 +0000 | [diff] [blame] | 164 | return m_nameTree.fullEnumerate(&nteHasFibEntry) | |
| 165 | boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(&name_tree::Entry::getFibEntry)); |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 166 | } |
| 167 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 168 | } // namespace fib |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 169 | } // namespace nfd |