blob: d18ce948fc7255640cd3257b5d544357b733b5fe [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
HangZhangad4afd12014-03-01 11:03:08 +080050Fib::Fib(NameTree& nameTree)
51 : m_nameTree(nameTree)
52 , m_nItems(0)
Junxiao Shic1e12362014-01-24 20:03:26 -070053{
Junxiao Shic1e12362014-01-24 20:03:26 -070054}
55
56Fib::~Fib()
57{
58}
59
Junxiao Shi40631842014-03-01 13:52:37 -070060static inline bool
Junxiao Shia6de4292016-07-12 02:08:10 +000061predicate_NameTreeEntry_hasFibEntry(const name_tree::Entry& nte)
Junxiao Shi40631842014-03-01 13:52:37 -070062{
Junxiao Shia6de4292016-07-12 02:08:10 +000063 return nte.getFibEntry() != nullptr;
Junxiao Shi40631842014-03-01 13:52:37 -070064}
65
Junxiao Shia6de4292016-07-12 02:08:10 +000066const Entry&
Junxiao Shic1e12362014-01-24 20:03:26 -070067Fib::findLongestPrefixMatch(const Name& prefix) const
68{
Junxiao Shia6de4292016-07-12 02:08:10 +000069 shared_ptr<name_tree::Entry> nte =
Junxiao Shi40631842014-03-01 13:52:37 -070070 m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasFibEntry);
Junxiao Shia6de4292016-07-12 02:08:10 +000071 if (nte != nullptr) {
72 return *nte->getFibEntry();
HangZhangad4afd12014-03-01 11:03:08 +080073 }
Junxiao Shia6de4292016-07-12 02:08:10 +000074 return *s_emptyEntry;
HangZhangcb4fc832014-03-11 16:57:11 +080075}
76
Junxiao Shia6de4292016-07-12 02:08:10 +000077const Entry&
78Fib::findLongestPrefixMatch(shared_ptr<name_tree::Entry> nte) const
HangZhangcb4fc832014-03-11 16:57:11 +080079{
Junxiao Shia6de4292016-07-12 02:08:10 +000080 Entry* entry = nte->getFibEntry();
Junxiao Shib184e532016-05-26 18:09:57 +000081 if (entry != nullptr)
Junxiao Shia6de4292016-07-12 02:08:10 +000082 return *entry;
Junxiao Shib184e532016-05-26 18:09:57 +000083
Junxiao Shia6de4292016-07-12 02:08:10 +000084 nte = m_nameTree.findLongestPrefixMatch(nte, &predicate_NameTreeEntry_hasFibEntry);
85 if (nte != nullptr) {
86 return *nte->getFibEntry();
HangZhangcb4fc832014-03-11 16:57:11 +080087 }
Junxiao Shib184e532016-05-26 18:09:57 +000088
Junxiao Shia6de4292016-07-12 02:08:10 +000089 return *s_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -070090}
91
Junxiao Shia6de4292016-07-12 02:08:10 +000092const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070093Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const
94{
Junxiao Shi4370fde2016-02-24 12:20:46 -070095 shared_ptr<name_tree::Entry> nte = m_nameTree.findLongestPrefixMatch(pitEntry);
96 BOOST_ASSERT(nte != nullptr);
97 return findLongestPrefixMatch(nte);
Junxiao Shidbe71732014-02-21 22:23:28 -070098}
99
Junxiao Shia6de4292016-07-12 02:08:10 +0000100const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -0700101Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
102{
Junxiao Shia6de4292016-07-12 02:08:10 +0000103 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(measurementsEntry);
104 BOOST_ASSERT(nte != nullptr);
105 return findLongestPrefixMatch(nte);
Junxiao Shidbe71732014-02-21 22:23:28 -0700106}
107
Junxiao Shia6de4292016-07-12 02:08:10 +0000108Entry*
109Fib::findExactMatch(const Name& prefix)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700110{
Junxiao Shia6de4292016-07-12 02:08:10 +0000111 shared_ptr<name_tree::Entry> nte = m_nameTree.findExactMatch(prefix);
112 if (nte != nullptr)
113 return nte->getFibEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000114
115 return nullptr;
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700116}
117
Junxiao Shia6de4292016-07-12 02:08:10 +0000118std::pair<Entry*, bool>
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700119Fib::insert(const Name& prefix)
120{
Junxiao Shia6de4292016-07-12 02:08:10 +0000121 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(prefix);
122 Entry* entry = nte->getFibEntry();
123 if (entry != nullptr) {
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700124 return std::make_pair(entry, false);
Junxiao Shia6de4292016-07-12 02:08:10 +0000125 }
Junxiao Shib184e532016-05-26 18:09:57 +0000126
Junxiao Shia6de4292016-07-12 02:08:10 +0000127 nte->setFibEntry(make_unique<Entry>(prefix));
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700128 ++m_nItems;
Junxiao Shia6de4292016-07-12 02:08:10 +0000129 return std::make_pair(nte->getFibEntry(), true);
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700130}
131
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700132void
Junxiao Shi02b73f52016-07-28 01:48:27 +0000133Fib::erase(shared_ptr<name_tree::Entry> nte, bool canDeleteNte)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700134{
Junxiao Shi02b73f52016-07-28 01:48:27 +0000135 BOOST_ASSERT(nte != nullptr);
136
Junxiao Shia6de4292016-07-12 02:08:10 +0000137 nte->setFibEntry(nullptr);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000138 if (canDeleteNte) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000139 m_nameTree.eraseIfEmpty(nte.get());
Junxiao Shi02b73f52016-07-28 01:48:27 +0000140 }
Junxiao Shiee5a4442014-07-27 17:13:43 -0700141 --m_nItems;
142}
143
144void
HangZhangad4afd12014-03-01 11:03:08 +0800145Fib::erase(const Name& prefix)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700146{
Junxiao Shia6de4292016-07-12 02:08:10 +0000147 shared_ptr<name_tree::Entry> nte = m_nameTree.findExactMatch(prefix);
148 if (nte != nullptr) {
149 this->erase(nte);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700150 }
151}
152
153void
Junxiao Shia6de4292016-07-12 02:08:10 +0000154Fib::erase(const Entry& entry)
Junxiao Shiefceadc2014-03-09 18:52:57 -0700155{
Junxiao Shia6de4292016-07-12 02:08:10 +0000156 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(entry);
157 if (nte != nullptr) {
Junxiao Shi02b73f52016-07-28 01:48:27 +0000158 // don't try to erase s_emptyEntry
Junxiao Shia6de4292016-07-12 02:08:10 +0000159 this->erase(nte);
HangZhangad4afd12014-03-01 11:03:08 +0800160 }
Junxiao Shic1e12362014-01-24 20:03:26 -0700161}
162
163void
Junxiao Shi02b73f52016-07-28 01:48:27 +0000164Fib::removeNextHop(Entry& entry, const Face& face)
Junxiao Shic1e12362014-01-24 20:03:26 -0700165{
Junxiao Shi02b73f52016-07-28 01:48:27 +0000166 entry.removeNextHop(face);
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700167
Junxiao Shi02b73f52016-07-28 01:48:27 +0000168 if (!entry.hasNextHops()) {
169 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(entry);
170 this->erase(nte, false);
Junxiao Shi60607c72014-11-26 22:40:36 -0700171 }
Junxiao Shic1e12362014-01-24 20:03:26 -0700172}
173
HangZhang5d469422014-03-12 09:26:26 +0800174Fib::const_iterator
175Fib::begin() const
176{
Junxiao Shi60607c72014-11-26 22:40:36 -0700177 return const_iterator(m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasFibEntry).begin());
HangZhang5d469422014-03-12 09:26:26 +0800178}
179
Junxiao Shia6de4292016-07-12 02:08:10 +0000180} // namespace fib
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800181} // namespace nfd