blob: 7d60dfabe5de0ce2a8de78e9acd3b34f51bafefa [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{
62 return static_cast<bool>(entry.getFibEntry());
63}
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);
70 if (static_cast<bool>(nameTreeEntry)) {
71 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();
80 if (static_cast<bool>(entry))
81 return entry;
82 nameTreeEntry = m_nameTree.findLongestPrefixMatch(nameTreeEntry,
83 &predicate_NameTreeEntry_hasFibEntry);
84 if (static_cast<bool>(nameTreeEntry)) {
85 return nameTreeEntry->getFibEntry();
86 }
87 return s_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -070088}
89
Steve DiBenedettod5f87932014-02-05 15:11:39 -070090shared_ptr<fib::Entry>
Junxiao Shidbe71732014-02-21 22:23:28 -070091Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const
92{
Junxiao Shi4370fde2016-02-24 12:20:46 -070093 shared_ptr<name_tree::Entry> nte = m_nameTree.findLongestPrefixMatch(pitEntry);
94 BOOST_ASSERT(nte != nullptr);
95 return findLongestPrefixMatch(nte);
Junxiao Shidbe71732014-02-21 22:23:28 -070096}
97
98shared_ptr<fib::Entry>
99Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
100{
HangZhangcb4fc832014-03-11 16:57:11 +0800101 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(measurementsEntry);
102
103 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
104
105 return findLongestPrefixMatch(nameTreeEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -0700106}
107
108shared_ptr<fib::Entry>
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700109Fib::findExactMatch(const Name& prefix) const
110{
HangZhangad4afd12014-03-01 11:03:08 +0800111 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
112 if (static_cast<bool>(nameTreeEntry))
113 return nameTreeEntry->getFibEntry();
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700114 return shared_ptr<fib::Entry>();
115}
116
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700117std::pair<shared_ptr<fib::Entry>, bool>
118Fib::insert(const Name& prefix)
119{
120 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix);
121 shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry();
122 if (static_cast<bool>(entry))
123 return std::make_pair(entry, false);
124 entry = make_shared<fib::Entry>(prefix);
125 nameTreeEntry->setFibEntry(entry);
126 ++m_nItems;
127 return std::make_pair(entry, true);
128}
129
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700130void
Junxiao Shiee5a4442014-07-27 17:13:43 -0700131Fib::erase(shared_ptr<name_tree::Entry> nameTreeEntry)
132{
133 nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>());
134 m_nameTree.eraseEntryIfEmpty(nameTreeEntry);
135 --m_nItems;
136}
137
138void
HangZhangad4afd12014-03-01 11:03:08 +0800139Fib::erase(const Name& prefix)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700140{
HangZhangad4afd12014-03-01 11:03:08 +0800141 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700142 if (static_cast<bool>(nameTreeEntry)) {
143 this->erase(nameTreeEntry);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700144 }
145}
146
147void
148Fib::erase(const fib::Entry& entry)
149{
Junxiao Shiee5a4442014-07-27 17:13:43 -0700150 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(entry);
151 if (static_cast<bool>(nameTreeEntry)) {
152 this->erase(nameTreeEntry);
HangZhangad4afd12014-03-01 11:03:08 +0800153 }
Junxiao Shic1e12362014-01-24 20:03:26 -0700154}
155
156void
157Fib::removeNextHopFromAllEntries(shared_ptr<Face> face)
158{
Junxiao Shi4b2e6cb2014-12-03 00:51:45 -0700159 std::list<fib::Entry*> toErase;
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700160
Junxiao Shi60607c72014-11-26 22:40:36 -0700161 auto&& enumerable = m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasFibEntry);
162 for (const name_tree::Entry& nte : enumerable) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700163 shared_ptr<fib::Entry> entry = nte.getFibEntry();
Junxiao Shi40631842014-03-01 13:52:37 -0700164 entry->removeNextHop(face);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700165 if (!entry->hasNextHops()) {
Junxiao Shi4b2e6cb2014-12-03 00:51:45 -0700166 toErase.push_back(entry.get());
167 // entry needs to be erased, but we must wait until the enumeration ends,
168 // because otherwise NameTree iterator would be invalidated
Junxiao Shiefceadc2014-03-09 18:52:57 -0700169 }
HangZhangad4afd12014-03-01 11:03:08 +0800170 }
Junxiao Shi60607c72014-11-26 22:40:36 -0700171
Junxiao Shi4b2e6cb2014-12-03 00:51:45 -0700172 for (fib::Entry* entry : toErase) {
173 this->erase(*entry);
Junxiao Shi60607c72014-11-26 22:40:36 -0700174 }
Junxiao Shic1e12362014-01-24 20:03:26 -0700175}
176
HangZhang5d469422014-03-12 09:26:26 +0800177Fib::const_iterator
178Fib::begin() const
179{
Junxiao Shi60607c72014-11-26 22:40:36 -0700180 return const_iterator(m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasFibEntry).begin());
HangZhang5d469422014-03-12 09:26:26 +0800181}
182
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800183} // namespace nfd