blob: 516de5796140323e9e7ca9494c1695a22d2de226 [file] [log] [blame]
Junxiao Shic1e12362014-01-24 20:03:26 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 Regents of the University of California,
4 * 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Junxiao Shic1e12362014-01-24 20:03:26 -070024
25#include "fib.hpp"
Junxiao Shidbe71732014-02-21 22:23:28 -070026#include "pit-entry.hpp"
27#include "measurements-entry.hpp"
Junxiao Shic1e12362014-01-24 20:03:26 -070028
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080029namespace nfd {
Junxiao Shi40631842014-03-01 13:52:37 -070030
HangZhangcb4fc832014-03-11 16:57:11 +080031const shared_ptr<fib::Entry> Fib::s_emptyEntry = make_shared<fib::Entry>(Name());
Junxiao Shi40631842014-03-01 13:52:37 -070032
HangZhangad4afd12014-03-01 11:03:08 +080033Fib::Fib(NameTree& nameTree)
34 : m_nameTree(nameTree)
35 , m_nItems(0)
Junxiao Shic1e12362014-01-24 20:03:26 -070036{
Junxiao Shic1e12362014-01-24 20:03:26 -070037}
38
39Fib::~Fib()
40{
41}
42
Junxiao Shi40631842014-03-01 13:52:37 -070043static inline bool
44predicate_NameTreeEntry_hasFibEntry(const name_tree::Entry& entry)
45{
46 return static_cast<bool>(entry.getFibEntry());
47}
48
Junxiao Shic1e12362014-01-24 20:03:26 -070049std::pair<shared_ptr<fib::Entry>, bool>
50Fib::insert(const Name& prefix)
51{
HangZhangad4afd12014-03-01 11:03:08 +080052 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix);
53 shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry();
54 if (static_cast<bool>(entry))
55 return std::make_pair(entry, false);
56 entry = make_shared<fib::Entry>(prefix);
57 nameTreeEntry->setFibEntry(entry);
Junxiao Shiefceadc2014-03-09 18:52:57 -070058 ++m_nItems;
Junxiao Shic1e12362014-01-24 20:03:26 -070059 return std::make_pair(entry, true);
60}
61
Junxiao Shic1e12362014-01-24 20:03:26 -070062shared_ptr<fib::Entry>
63Fib::findLongestPrefixMatch(const Name& prefix) const
64{
Junxiao Shi40631842014-03-01 13:52:37 -070065 shared_ptr<name_tree::Entry> nameTreeEntry =
66 m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasFibEntry);
67 if (static_cast<bool>(nameTreeEntry)) {
68 return nameTreeEntry->getFibEntry();
HangZhangad4afd12014-03-01 11:03:08 +080069 }
HangZhangcb4fc832014-03-11 16:57:11 +080070 return s_emptyEntry;
71}
72
73shared_ptr<fib::Entry>
74Fib::findLongestPrefixMatch(shared_ptr<name_tree::Entry> nameTreeEntry) const
75{
76 shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry();
77 if (static_cast<bool>(entry))
78 return entry;
79 nameTreeEntry = m_nameTree.findLongestPrefixMatch(nameTreeEntry,
80 &predicate_NameTreeEntry_hasFibEntry);
81 if (static_cast<bool>(nameTreeEntry)) {
82 return nameTreeEntry->getFibEntry();
83 }
84 return s_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -070085}
86
Steve DiBenedettod5f87932014-02-05 15:11:39 -070087shared_ptr<fib::Entry>
Junxiao Shidbe71732014-02-21 22:23:28 -070088Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const
89{
HangZhangcb4fc832014-03-11 16:57:11 +080090 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry);
91
92 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
93
94 return findLongestPrefixMatch(nameTreeEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -070095}
96
97shared_ptr<fib::Entry>
98Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
99{
HangZhangcb4fc832014-03-11 16:57:11 +0800100 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(measurementsEntry);
101
102 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
103
104 return findLongestPrefixMatch(nameTreeEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -0700105}
106
107shared_ptr<fib::Entry>
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700108Fib::findExactMatch(const Name& prefix) const
109{
HangZhangad4afd12014-03-01 11:03:08 +0800110 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
111 if (static_cast<bool>(nameTreeEntry))
112 return nameTreeEntry->getFibEntry();
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700113 return shared_ptr<fib::Entry>();
114}
115
116void
HangZhangad4afd12014-03-01 11:03:08 +0800117Fib::erase(const Name& prefix)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700118{
HangZhangad4afd12014-03-01 11:03:08 +0800119 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
120 if (static_cast<bool>(nameTreeEntry))
121 {
Junxiao Shiefceadc2014-03-09 18:52:57 -0700122 nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>());
123 --m_nItems;
124 }
125}
126
127void
128Fib::erase(const fib::Entry& entry)
129{
130 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(entry);
131 if (static_cast<bool>(nameTreeEntry))
132 {
133 nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>());
134 --m_nItems;
HangZhangad4afd12014-03-01 11:03:08 +0800135 }
Junxiao Shic1e12362014-01-24 20:03:26 -0700136}
137
138void
139Fib::removeNextHopFromAllEntries(shared_ptr<Face> face)
140{
Junxiao Shiefceadc2014-03-09 18:52:57 -0700141 for (NameTree::const_iterator it = m_nameTree.fullEnumerate(
142 &predicate_NameTreeEntry_hasFibEntry); it != m_nameTree.end(); ++it) {
Haowei Yuane1079fc2014-03-08 14:41:25 -0600143 shared_ptr<fib::Entry> entry = it->getFibEntry();
Junxiao Shi40631842014-03-01 13:52:37 -0700144 entry->removeNextHop(face);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700145 if (!entry->hasNextHops()) {
146 this->erase(*entry);
147 }
HangZhangad4afd12014-03-01 11:03:08 +0800148 }
Junxiao Shic1e12362014-01-24 20:03:26 -0700149}
150
HangZhang5d469422014-03-12 09:26:26 +0800151Fib::const_iterator
152Fib::begin() const
153{
154 return const_iterator(m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasFibEntry));
155}
156
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800157} // namespace nfd