blob: c6c8f2cf2e9df047816eef2069afa23a47fd6b9c [file] [log] [blame]
Junxiao Shic1e12362014-01-24 20:03:26 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento981db802018-04-10 18:05:04 -04002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Shiee5a4442014-07-27 17:13:43 -070024 */
Junxiao Shic1e12362014-01-24 20:03:26 -070025
26#include "fib.hpp"
Junxiao Shidbe71732014-02-21 22:23:28 -070027#include "pit-entry.hpp"
28#include "measurements-entry.hpp"
Davide Pesavento981db802018-04-10 18:05:04 -040029
30#include <ndn-cxx/util/concepts.hpp>
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -080031
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080032namespace nfd {
Junxiao Shia6de4292016-07-12 02:08:10 +000033namespace fib {
Junxiao Shi40631842014-03-01 13:52:37 -070034
Davide Pesavento981db802018-04-10 18:05:04 -040035NDN_CXX_ASSERT_FORWARD_ITERATOR(Fib::const_iterator);
Junxiao Shi40631842014-03-01 13:52:37 -070036
Junxiao Shi3aba7542016-12-24 02:42:52 +000037const unique_ptr<Entry> Fib::s_emptyEntry = make_unique<Entry>(Name());
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -080038
Junxiao Shi811c0102016-08-10 04:12:45 +000039static inline bool
40nteHasFibEntry(const name_tree::Entry& nte)
41{
42 return nte.getFibEntry() != nullptr;
43}
44
HangZhangad4afd12014-03-01 11:03:08 +080045Fib::Fib(NameTree& nameTree)
46 : m_nameTree(nameTree)
Junxiao Shic1e12362014-01-24 20:03:26 -070047{
Junxiao Shic1e12362014-01-24 20:03:26 -070048}
49
Junxiao Shidd8f6612016-08-12 15:42:52 +000050template<typename K>
Junxiao Shia6de4292016-07-12 02:08:10 +000051const Entry&
Junxiao Shidd8f6612016-08-12 15:42:52 +000052Fib::findLongestPrefixMatchImpl(const K& key) const
Junxiao Shic1e12362014-01-24 20:03:26 -070053{
Junxiao Shidd8f6612016-08-12 15:42:52 +000054 name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasFibEntry);
Junxiao Shia6de4292016-07-12 02:08:10 +000055 if (nte != nullptr) {
56 return *nte->getFibEntry();
HangZhangad4afd12014-03-01 11:03:08 +080057 }
Junxiao Shia6de4292016-07-12 02:08:10 +000058 return *s_emptyEntry;
HangZhangcb4fc832014-03-11 16:57:11 +080059}
60
Junxiao Shia6de4292016-07-12 02:08:10 +000061const Entry&
Junxiao Shidd8f6612016-08-12 15:42:52 +000062Fib::findLongestPrefixMatch(const Name& prefix) const
HangZhangcb4fc832014-03-11 16:57:11 +080063{
Junxiao Shidd8f6612016-08-12 15:42:52 +000064 return this->findLongestPrefixMatchImpl(prefix);
Junxiao Shic1e12362014-01-24 20:03:26 -070065}
66
Junxiao Shia6de4292016-07-12 02:08:10 +000067const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070068Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const
69{
Junxiao Shidd8f6612016-08-12 15:42:52 +000070 return this->findLongestPrefixMatchImpl(pitEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -070071}
72
Junxiao Shia6de4292016-07-12 02:08:10 +000073const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070074Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
75{
Junxiao Shidd8f6612016-08-12 15:42:52 +000076 return this->findLongestPrefixMatchImpl(measurementsEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -070077}
78
Junxiao Shia6de4292016-07-12 02:08:10 +000079Entry*
80Fib::findExactMatch(const Name& prefix)
Steve DiBenedettod5f87932014-02-05 15:11:39 -070081{
Junxiao Shi811c0102016-08-10 04:12:45 +000082 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shia6de4292016-07-12 02:08:10 +000083 if (nte != nullptr)
84 return nte->getFibEntry();
Junxiao Shib184e532016-05-26 18:09:57 +000085
86 return nullptr;
Steve DiBenedettod5f87932014-02-05 15:11:39 -070087}
88
Junxiao Shia6de4292016-07-12 02:08:10 +000089std::pair<Entry*, bool>
Junxiao Shi56a21bf2014-11-02 21:11:50 -070090Fib::insert(const Name& prefix)
91{
Junxiao Shi7f358432016-08-11 17:06:33 +000092 name_tree::Entry& nte = m_nameTree.lookup(prefix);
93 Entry* entry = nte.getFibEntry();
Junxiao Shia6de4292016-07-12 02:08:10 +000094 if (entry != nullptr) {
Davide Pesaventoe4b22382018-06-10 14:37:24 -040095 return {entry, false};
Junxiao Shia6de4292016-07-12 02:08:10 +000096 }
Junxiao Shib184e532016-05-26 18:09:57 +000097
Junxiao Shi7f358432016-08-11 17:06:33 +000098 nte.setFibEntry(make_unique<Entry>(prefix));
Junxiao Shi56a21bf2014-11-02 21:11:50 -070099 ++m_nItems;
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400100 return {nte.getFibEntry(), true};
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700101}
102
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700103void
Junxiao Shi811c0102016-08-10 04:12:45 +0000104Fib::erase(name_tree::Entry* nte, bool canDeleteNte)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700105{
Junxiao Shi02b73f52016-07-28 01:48:27 +0000106 BOOST_ASSERT(nte != nullptr);
107
Junxiao Shia6de4292016-07-12 02:08:10 +0000108 nte->setFibEntry(nullptr);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000109 if (canDeleteNte) {
Junxiao Shi811c0102016-08-10 04:12:45 +0000110 m_nameTree.eraseIfEmpty(nte);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000111 }
Junxiao Shiee5a4442014-07-27 17:13:43 -0700112 --m_nItems;
113}
114
115void
HangZhangad4afd12014-03-01 11:03:08 +0800116Fib::erase(const Name& prefix)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700117{
Junxiao Shi811c0102016-08-10 04:12:45 +0000118 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shia6de4292016-07-12 02:08:10 +0000119 if (nte != nullptr) {
120 this->erase(nte);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700121 }
122}
123
124void
Junxiao Shia6de4292016-07-12 02:08:10 +0000125Fib::erase(const Entry& entry)
Junxiao Shiefceadc2014-03-09 18:52:57 -0700126{
Junxiao Shi7f358432016-08-11 17:06:33 +0000127 name_tree::Entry* nte = m_nameTree.getEntry(entry);
Junxiao Shidd8f6612016-08-12 15:42:52 +0000128 if (nte == nullptr) { // don't try to erase s_emptyEntry
129 BOOST_ASSERT(&entry == s_emptyEntry.get());
130 return;
HangZhangad4afd12014-03-01 11:03:08 +0800131 }
Junxiao Shidd8f6612016-08-12 15:42:52 +0000132 this->erase(nte);
Junxiao Shic1e12362014-01-24 20:03:26 -0700133}
134
135void
Ju Pand8315bf2019-07-31 06:59:07 +0000136Fib::addOrUpdateNextHop(Entry& entry, Face& face, uint64_t cost)
137{
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400138 auto [it, isNew] = entry.addOrUpdateNextHop(face, cost);
Ju Pand8315bf2019-07-31 06:59:07 +0000139 if (isNew)
140 this->afterNewNextHop(entry.getPrefix(), *it);
141}
142
143Fib::RemoveNextHopResult
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000144Fib::removeNextHop(Entry& entry, const Face& face)
Junxiao Shic1e12362014-01-24 20:03:26 -0700145{
Ju Pand8315bf2019-07-31 06:59:07 +0000146 bool isRemoved = entry.removeNextHop(face);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000147
Ju Pand8315bf2019-07-31 06:59:07 +0000148 if (!isRemoved) {
149 return RemoveNextHopResult::NO_SUCH_NEXTHOP;
150 }
151 else if (!entry.hasNextHops()) {
Junxiao Shi7f358432016-08-11 17:06:33 +0000152 name_tree::Entry* nte = m_nameTree.getEntry(entry);
153 this->erase(nte, false);
Ju Pand8315bf2019-07-31 06:59:07 +0000154 return RemoveNextHopResult::FIB_ENTRY_REMOVED;
155 }
156 else {
157 return RemoveNextHopResult::NEXTHOP_REMOVED;
Junxiao Shi60607c72014-11-26 22:40:36 -0700158 }
Junxiao Shic1e12362014-01-24 20:03:26 -0700159}
160
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000161Fib::Range
162Fib::getRange() const
HangZhang5d469422014-03-12 09:26:26 +0800163{
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000164 return m_nameTree.fullEnumerate(&nteHasFibEntry) |
165 boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(&name_tree::Entry::getFibEntry));
HangZhang5d469422014-03-12 09:26:26 +0800166}
167
Junxiao Shia6de4292016-07-12 02:08:10 +0000168} // namespace fib
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800169} // namespace nfd