blob: 8ba6ffe2087b265e4c9590587fc363775710bfe1 [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 Shidd8f6612016-08-12 15:42:52 +000062template<typename K>
Junxiao Shia6de4292016-07-12 02:08:10 +000063const Entry&
Junxiao Shidd8f6612016-08-12 15:42:52 +000064Fib::findLongestPrefixMatchImpl(const K& key) const
Junxiao Shic1e12362014-01-24 20:03:26 -070065{
Junxiao Shidd8f6612016-08-12 15:42:52 +000066 name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasFibEntry);
Junxiao Shia6de4292016-07-12 02:08:10 +000067 if (nte != nullptr) {
68 return *nte->getFibEntry();
HangZhangad4afd12014-03-01 11:03:08 +080069 }
Junxiao Shia6de4292016-07-12 02:08:10 +000070 return *s_emptyEntry;
HangZhangcb4fc832014-03-11 16:57:11 +080071}
72
Junxiao Shia6de4292016-07-12 02:08:10 +000073const Entry&
Junxiao Shidd8f6612016-08-12 15:42:52 +000074Fib::findLongestPrefixMatch(const Name& prefix) const
HangZhangcb4fc832014-03-11 16:57:11 +080075{
Junxiao Shidd8f6612016-08-12 15:42:52 +000076 return this->findLongestPrefixMatchImpl(prefix);
Junxiao Shic1e12362014-01-24 20:03:26 -070077}
78
Junxiao Shia6de4292016-07-12 02:08:10 +000079const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070080Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const
81{
Junxiao Shidd8f6612016-08-12 15:42:52 +000082 return this->findLongestPrefixMatchImpl(pitEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -070083}
84
Junxiao Shia6de4292016-07-12 02:08:10 +000085const Entry&
Junxiao Shidbe71732014-02-21 22:23:28 -070086Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
87{
Junxiao Shidd8f6612016-08-12 15:42:52 +000088 return this->findLongestPrefixMatchImpl(measurementsEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -070089}
90
Junxiao Shia6de4292016-07-12 02:08:10 +000091Entry*
92Fib::findExactMatch(const Name& prefix)
Steve DiBenedettod5f87932014-02-05 15:11:39 -070093{
Junxiao Shi811c0102016-08-10 04:12:45 +000094 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shia6de4292016-07-12 02:08:10 +000095 if (nte != nullptr)
96 return nte->getFibEntry();
Junxiao Shib184e532016-05-26 18:09:57 +000097
98 return nullptr;
Steve DiBenedettod5f87932014-02-05 15:11:39 -070099}
100
Junxiao Shia6de4292016-07-12 02:08:10 +0000101std::pair<Entry*, bool>
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700102Fib::insert(const Name& prefix)
103{
Junxiao Shi7f358432016-08-11 17:06:33 +0000104 name_tree::Entry& nte = m_nameTree.lookup(prefix);
105 Entry* entry = nte.getFibEntry();
Junxiao Shia6de4292016-07-12 02:08:10 +0000106 if (entry != nullptr) {
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700107 return std::make_pair(entry, false);
Junxiao Shia6de4292016-07-12 02:08:10 +0000108 }
Junxiao Shib184e532016-05-26 18:09:57 +0000109
Junxiao Shi7f358432016-08-11 17:06:33 +0000110 nte.setFibEntry(make_unique<Entry>(prefix));
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700111 ++m_nItems;
Junxiao Shi7f358432016-08-11 17:06:33 +0000112 return std::make_pair(nte.getFibEntry(), true);
Junxiao Shi56a21bf2014-11-02 21:11:50 -0700113}
114
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700115void
Junxiao Shi811c0102016-08-10 04:12:45 +0000116Fib::erase(name_tree::Entry* nte, bool canDeleteNte)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700117{
Junxiao Shi02b73f52016-07-28 01:48:27 +0000118 BOOST_ASSERT(nte != nullptr);
119
Junxiao Shia6de4292016-07-12 02:08:10 +0000120 nte->setFibEntry(nullptr);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000121 if (canDeleteNte) {
Junxiao Shi811c0102016-08-10 04:12:45 +0000122 m_nameTree.eraseIfEmpty(nte);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000123 }
Junxiao Shiee5a4442014-07-27 17:13:43 -0700124 --m_nItems;
125}
126
127void
HangZhangad4afd12014-03-01 11:03:08 +0800128Fib::erase(const Name& prefix)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700129{
Junxiao Shi811c0102016-08-10 04:12:45 +0000130 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shia6de4292016-07-12 02:08:10 +0000131 if (nte != nullptr) {
132 this->erase(nte);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700133 }
134}
135
136void
Junxiao Shia6de4292016-07-12 02:08:10 +0000137Fib::erase(const Entry& entry)
Junxiao Shiefceadc2014-03-09 18:52:57 -0700138{
Junxiao Shi7f358432016-08-11 17:06:33 +0000139 name_tree::Entry* nte = m_nameTree.getEntry(entry);
Junxiao Shidd8f6612016-08-12 15:42:52 +0000140 if (nte == nullptr) { // don't try to erase s_emptyEntry
141 BOOST_ASSERT(&entry == s_emptyEntry.get());
142 return;
HangZhangad4afd12014-03-01 11:03:08 +0800143 }
Junxiao Shidd8f6612016-08-12 15:42:52 +0000144 this->erase(nte);
Junxiao Shic1e12362014-01-24 20:03:26 -0700145}
146
147void
Junxiao Shi02b73f52016-07-28 01:48:27 +0000148Fib::removeNextHop(Entry& entry, const Face& face)
Junxiao Shic1e12362014-01-24 20:03:26 -0700149{
Junxiao Shi02b73f52016-07-28 01:48:27 +0000150 entry.removeNextHop(face);
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700151
Junxiao Shi02b73f52016-07-28 01:48:27 +0000152 if (!entry.hasNextHops()) {
Junxiao Shi7f358432016-08-11 17:06:33 +0000153 name_tree::Entry* nte = m_nameTree.getEntry(entry);
154 this->erase(nte, false);
Junxiao Shi60607c72014-11-26 22:40:36 -0700155 }
Junxiao Shic1e12362014-01-24 20:03:26 -0700156}
157
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000158Fib::Range
159Fib::getRange() const
HangZhang5d469422014-03-12 09:26:26 +0800160{
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000161 return m_nameTree.fullEnumerate(&nteHasFibEntry) |
162 boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(&name_tree::Entry::getFibEntry));
HangZhang5d469422014-03-12 09:26:26 +0800163}
164
Junxiao Shia6de4292016-07-12 02:08:10 +0000165} // namespace fib
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800166} // namespace nfd