blob: 388cd25eb91580e03dcc0e506b637a57e62f05ca [file] [log] [blame]
Junxiao Shic1e12362014-01-24 20:03:26 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi4370fde2016-02-24 12:20:46 -07003 * Copyright (c) 2014-2016, 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"
Junxiao Shic1e12362014-01-24 20:03:26 -070029
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -080030#include <boost/concept/assert.hpp>
31#include <boost/concept_check.hpp>
32#include <type_traits>
33
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080034namespace nfd {
Junxiao Shia6de4292016-07-12 02:08:10 +000035namespace fib {
Junxiao Shi40631842014-03-01 13:52:37 -070036
Junxiao Shia6de4292016-07-12 02:08:10 +000037const unique_ptr<Entry> Fib::s_emptyEntry = make_unique<Entry>(Name());
Junxiao Shi40631842014-03-01 13:52:37 -070038
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -080039// http://en.cppreference.com/w/cpp/concept/ForwardIterator
40BOOST_CONCEPT_ASSERT((boost::ForwardIterator<Fib::const_iterator>));
41// boost::ForwardIterator follows SGI standard http://www.sgi.com/tech/stl/ForwardIterator.html,
42// which doesn't require DefaultConstructible
43#ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE
44static_assert(std::is_default_constructible<Fib::const_iterator>::value,
45 "Fib::const_iterator must be default-constructible");
46#else
47BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Fib::const_iterator>));
48#endif // HAVE_IS_DEFAULT_CONSTRUCTIBLE
49
Junxiao Shi811c0102016-08-10 04:12:45 +000050static inline bool
51nteHasFibEntry(const name_tree::Entry& nte)
52{
53 return nte.getFibEntry() != nullptr;
54}
55
HangZhangad4afd12014-03-01 11:03:08 +080056Fib::Fib(NameTree& nameTree)
57 : m_nameTree(nameTree)
58 , m_nItems(0)
Junxiao Shic1e12362014-01-24 20:03:26 -070059{
Junxiao Shic1e12362014-01-24 20:03:26 -070060}
61
Junxiao Shia6de4292016-07-12 02:08:10 +000062const Entry&
Junxiao Shic1e12362014-01-24 20:03:26 -070063Fib::findLongestPrefixMatch(const Name& prefix) const
64{
Junxiao Shi811c0102016-08-10 04:12:45 +000065 name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(prefix, &nteHasFibEntry);
Junxiao Shia6de4292016-07-12 02:08:10 +000066 if (nte != nullptr) {
67 return *nte->getFibEntry();
HangZhangad4afd12014-03-01 11:03:08 +080068 }
Junxiao Shia6de4292016-07-12 02:08:10 +000069 return *s_emptyEntry;
HangZhangcb4fc832014-03-11 16:57:11 +080070}
71
Junxiao Shia6de4292016-07-12 02:08:10 +000072const Entry&
Junxiao Shi811c0102016-08-10 04:12:45 +000073Fib::findLongestPrefixMatch(const name_tree::Entry* nte) const
HangZhangcb4fc832014-03-11 16:57:11 +080074{
Junxiao Shia6de4292016-07-12 02:08:10 +000075 Entry* entry = nte->getFibEntry();
Junxiao Shib184e532016-05-26 18:09:57 +000076 if (entry != nullptr)
Junxiao Shia6de4292016-07-12 02:08:10 +000077 return *entry;
Junxiao Shib184e532016-05-26 18:09:57 +000078
Junxiao Shi811c0102016-08-10 04:12:45 +000079 nte = m_nameTree.findLongestPrefixMatch(*nte, &nteHasFibEntry);
Junxiao Shia6de4292016-07-12 02:08:10 +000080 if (nte != nullptr) {
81 return *nte->getFibEntry();
HangZhangcb4fc832014-03-11 16:57:11 +080082 }
Junxiao Shib184e532016-05-26 18:09:57 +000083
Junxiao Shia6de4292016-07-12 02:08:10 +000084 return *s_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -070085}
86
Junxiao Shia6de4292016-07-12 02:08:10 +000087const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070088Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const
89{
Junxiao Shi811c0102016-08-10 04:12:45 +000090 name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(pitEntry);
Junxiao Shi4370fde2016-02-24 12:20:46 -070091 BOOST_ASSERT(nte != nullptr);
92 return findLongestPrefixMatch(nte);
Junxiao Shidbe71732014-02-21 22:23:28 -070093}
94
Junxiao Shia6de4292016-07-12 02:08:10 +000095const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070096Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
97{
Junxiao Shi811c0102016-08-10 04:12:45 +000098 name_tree::Entry* nte = m_nameTree.lookup(measurementsEntry).get();
Junxiao Shia6de4292016-07-12 02:08:10 +000099 BOOST_ASSERT(nte != nullptr);
100 return findLongestPrefixMatch(nte);
Junxiao Shidbe71732014-02-21 22:23:28 -0700101}
102
Junxiao Shia6de4292016-07-12 02:08:10 +0000103Entry*
104Fib::findExactMatch(const Name& prefix)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700105{
Junxiao Shi811c0102016-08-10 04:12:45 +0000106 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shia6de4292016-07-12 02:08:10 +0000107 if (nte != nullptr)
108 return nte->getFibEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000109
110 return nullptr;
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700111}
112
Junxiao Shia6de4292016-07-12 02:08:10 +0000113std::pair<Entry*, bool>
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700114Fib::insert(const Name& prefix)
115{
Junxiao Shia6de4292016-07-12 02:08:10 +0000116 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(prefix);
117 Entry* entry = nte->getFibEntry();
118 if (entry != nullptr) {
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700119 return std::make_pair(entry, false);
Junxiao Shia6de4292016-07-12 02:08:10 +0000120 }
Junxiao Shib184e532016-05-26 18:09:57 +0000121
Junxiao Shia6de4292016-07-12 02:08:10 +0000122 nte->setFibEntry(make_unique<Entry>(prefix));
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700123 ++m_nItems;
Junxiao Shia6de4292016-07-12 02:08:10 +0000124 return std::make_pair(nte->getFibEntry(), true);
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700125}
126
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700127void
Junxiao Shi811c0102016-08-10 04:12:45 +0000128Fib::erase(name_tree::Entry* nte, bool canDeleteNte)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700129{
Junxiao Shi02b73f52016-07-28 01:48:27 +0000130 BOOST_ASSERT(nte != nullptr);
131
Junxiao Shia6de4292016-07-12 02:08:10 +0000132 nte->setFibEntry(nullptr);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000133 if (canDeleteNte) {
Junxiao Shi811c0102016-08-10 04:12:45 +0000134 m_nameTree.eraseIfEmpty(nte);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000135 }
Junxiao Shiee5a4442014-07-27 17:13:43 -0700136 --m_nItems;
137}
138
139void
HangZhangad4afd12014-03-01 11:03:08 +0800140Fib::erase(const Name& prefix)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700141{
Junxiao Shi811c0102016-08-10 04:12:45 +0000142 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shia6de4292016-07-12 02:08:10 +0000143 if (nte != nullptr) {
144 this->erase(nte);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700145 }
146}
147
148void
Junxiao Shia6de4292016-07-12 02:08:10 +0000149Fib::erase(const Entry& entry)
Junxiao Shiefceadc2014-03-09 18:52:57 -0700150{
Junxiao Shia6de4292016-07-12 02:08:10 +0000151 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(entry);
152 if (nte != nullptr) {
Junxiao Shi02b73f52016-07-28 01:48:27 +0000153 // don't try to erase s_emptyEntry
Junxiao Shi811c0102016-08-10 04:12:45 +0000154 this->erase(nte.get());
HangZhangad4afd12014-03-01 11:03:08 +0800155 }
Junxiao Shic1e12362014-01-24 20:03:26 -0700156}
157
158void
Junxiao Shi02b73f52016-07-28 01:48:27 +0000159Fib::removeNextHop(Entry& entry, const Face& face)
Junxiao Shic1e12362014-01-24 20:03:26 -0700160{
Junxiao Shi02b73f52016-07-28 01:48:27 +0000161 entry.removeNextHop(face);
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700162
Junxiao Shi02b73f52016-07-28 01:48:27 +0000163 if (!entry.hasNextHops()) {
164 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(entry);
Junxiao Shi811c0102016-08-10 04:12:45 +0000165 this->erase(nte.get(), false);
Junxiao Shi60607c72014-11-26 22:40:36 -0700166 }
Junxiao Shic1e12362014-01-24 20:03:26 -0700167}
168
HangZhang5d469422014-03-12 09:26:26 +0800169Fib::const_iterator
170Fib::begin() const
171{
Junxiao Shi811c0102016-08-10 04:12:45 +0000172 return const_iterator(m_nameTree.fullEnumerate(&nteHasFibEntry).begin());
HangZhang5d469422014-03-12 09:26:26 +0800173}
174
Junxiao Shia6de4292016-07-12 02:08:10 +0000175} // namespace fib
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800176} // namespace nfd