blob: 946652e0aab9594dbc012cbc1fda0962a71ca650 [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 Shi40631842014-03-01 13:52:37 -070035
HangZhangcb4fc832014-03-11 16:57:11 +080036const shared_ptr<fib::Entry> Fib::s_emptyEntry = make_shared<fib::Entry>(Name());
Junxiao Shi40631842014-03-01 13:52:37 -070037
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -080038// http://en.cppreference.com/w/cpp/concept/ForwardIterator
39BOOST_CONCEPT_ASSERT((boost::ForwardIterator<Fib::const_iterator>));
40// boost::ForwardIterator follows SGI standard http://www.sgi.com/tech/stl/ForwardIterator.html,
41// which doesn't require DefaultConstructible
42#ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE
43static_assert(std::is_default_constructible<Fib::const_iterator>::value,
44 "Fib::const_iterator must be default-constructible");
45#else
46BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Fib::const_iterator>));
47#endif // HAVE_IS_DEFAULT_CONSTRUCTIBLE
48
HangZhangad4afd12014-03-01 11:03:08 +080049Fib::Fib(NameTree& nameTree)
50 : m_nameTree(nameTree)
51 , m_nItems(0)
Junxiao Shic1e12362014-01-24 20:03:26 -070052{
Junxiao Shic1e12362014-01-24 20:03:26 -070053}
54
55Fib::~Fib()
56{
57}
58
Junxiao Shi40631842014-03-01 13:52:37 -070059static inline bool
60predicate_NameTreeEntry_hasFibEntry(const name_tree::Entry& entry)
61{
Junxiao Shib184e532016-05-26 18:09:57 +000062 return entry.getFibEntry() != nullptr;
Junxiao Shi40631842014-03-01 13:52:37 -070063}
64
Junxiao Shic1e12362014-01-24 20:03:26 -070065shared_ptr<fib::Entry>
66Fib::findLongestPrefixMatch(const Name& prefix) const
67{
Junxiao Shi40631842014-03-01 13:52:37 -070068 shared_ptr<name_tree::Entry> nameTreeEntry =
69 m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasFibEntry);
Junxiao Shib184e532016-05-26 18:09:57 +000070 if (nameTreeEntry != nullptr) {
Junxiao Shi40631842014-03-01 13:52:37 -070071 return nameTreeEntry->getFibEntry();
HangZhangad4afd12014-03-01 11:03:08 +080072 }
HangZhangcb4fc832014-03-11 16:57:11 +080073 return s_emptyEntry;
74}
75
76shared_ptr<fib::Entry>
77Fib::findLongestPrefixMatch(shared_ptr<name_tree::Entry> nameTreeEntry) const
78{
79 shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry();
Junxiao Shib184e532016-05-26 18:09:57 +000080 if (entry != nullptr)
HangZhangcb4fc832014-03-11 16:57:11 +080081 return entry;
Junxiao Shib184e532016-05-26 18:09:57 +000082
HangZhangcb4fc832014-03-11 16:57:11 +080083 nameTreeEntry = m_nameTree.findLongestPrefixMatch(nameTreeEntry,
84 &predicate_NameTreeEntry_hasFibEntry);
Junxiao Shib184e532016-05-26 18:09:57 +000085 if (nameTreeEntry != nullptr) {
HangZhangcb4fc832014-03-11 16:57:11 +080086 return nameTreeEntry->getFibEntry();
87 }
Junxiao Shib184e532016-05-26 18:09:57 +000088
HangZhangcb4fc832014-03-11 16:57:11 +080089 return s_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -070090}
91
Steve DiBenedettod5f87932014-02-05 15:11:39 -070092shared_ptr<fib::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
100shared_ptr<fib::Entry>
101Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
102{
Junxiao Shib184e532016-05-26 18:09:57 +0000103 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(measurementsEntry);
104 BOOST_ASSERT(nameTreeEntry != nullptr);
HangZhangcb4fc832014-03-11 16:57:11 +0800105
106 return findLongestPrefixMatch(nameTreeEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -0700107}
108
109shared_ptr<fib::Entry>
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700110Fib::findExactMatch(const Name& prefix) const
111{
HangZhangad4afd12014-03-01 11:03:08 +0800112 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000113 if (nameTreeEntry != nullptr)
HangZhangad4afd12014-03-01 11:03:08 +0800114 return nameTreeEntry->getFibEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000115
116 return nullptr;
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700117}
118
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700119std::pair<shared_ptr<fib::Entry>, bool>
120Fib::insert(const Name& prefix)
121{
122 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix);
123 shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000124 if (entry != nullptr)
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700125 return std::make_pair(entry, false);
Junxiao Shib184e532016-05-26 18:09:57 +0000126
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700127 entry = make_shared<fib::Entry>(prefix);
128 nameTreeEntry->setFibEntry(entry);
129 ++m_nItems;
130 return std::make_pair(entry, true);
131}
132
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700133void
Junxiao Shiee5a4442014-07-27 17:13:43 -0700134Fib::erase(shared_ptr<name_tree::Entry> nameTreeEntry)
135{
136 nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>());
137 m_nameTree.eraseEntryIfEmpty(nameTreeEntry);
138 --m_nItems;
139}
140
141void
HangZhangad4afd12014-03-01 11:03:08 +0800142Fib::erase(const Name& prefix)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700143{
HangZhangad4afd12014-03-01 11:03:08 +0800144 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000145 if (nameTreeEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700146 this->erase(nameTreeEntry);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700147 }
148}
149
150void
151Fib::erase(const fib::Entry& entry)
152{
Junxiao Shib184e532016-05-26 18:09:57 +0000153 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(entry);
154 if (nameTreeEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700155 this->erase(nameTreeEntry);
HangZhangad4afd12014-03-01 11:03:08 +0800156 }
Junxiao Shic1e12362014-01-24 20:03:26 -0700157}
158
159void
160Fib::removeNextHopFromAllEntries(shared_ptr<Face> face)
161{
Junxiao Shi4b2e6cb2014-12-03 00:51:45 -0700162 std::list<fib::Entry*> toErase;
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700163
Junxiao Shi60607c72014-11-26 22:40:36 -0700164 auto&& enumerable = m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasFibEntry);
165 for (const name_tree::Entry& nte : enumerable) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700166 shared_ptr<fib::Entry> entry = nte.getFibEntry();
Junxiao Shi40631842014-03-01 13:52:37 -0700167 entry->removeNextHop(face);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700168 if (!entry->hasNextHops()) {
Junxiao Shi4b2e6cb2014-12-03 00:51:45 -0700169 toErase.push_back(entry.get());
170 // entry needs to be erased, but we must wait until the enumeration ends,
171 // because otherwise NameTree iterator would be invalidated
Junxiao Shiefceadc2014-03-09 18:52:57 -0700172 }
HangZhangad4afd12014-03-01 11:03:08 +0800173 }
Junxiao Shi60607c72014-11-26 22:40:36 -0700174
Junxiao Shi4b2e6cb2014-12-03 00:51:45 -0700175 for (fib::Entry* entry : toErase) {
176 this->erase(*entry);
Junxiao Shi60607c72014-11-26 22:40:36 -0700177 }
Junxiao Shic1e12362014-01-24 20:03:26 -0700178}
179
HangZhang5d469422014-03-12 09:26:26 +0800180Fib::const_iterator
181Fib::begin() const
182{
Junxiao Shi60607c72014-11-26 22:40:36 -0700183 return const_iterator(m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasFibEntry).begin());
HangZhang5d469422014-03-12 09:26:26 +0800184}
185
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800186} // namespace nfd