blob: 950f23808dd841450314bc76b64883f8d5b159d4 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014-2016, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
*
* NFD is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef NFD_DAEMON_TABLE_FIB_HPP
#define NFD_DAEMON_TABLE_FIB_HPP
#include "fib-entry.hpp"
#include "name-tree.hpp"
namespace nfd {
namespace measurements {
class Entry;
} // namespace measurements
namespace pit {
class Entry;
} // namespace pit
namespace fib {
/** \brief represents the Forwarding Information Base (FIB)
*/
class Fib : noncopyable
{
public:
explicit
Fib(NameTree& nameTree);
size_t
size() const;
public: // lookup
/** \brief performs a longest prefix match
*/
const Entry&
findLongestPrefixMatch(const Name& prefix) const;
/** \brief performs a longest prefix match
*
* This is equivalent to .findLongestPrefixMatch(pitEntry.getName())
*/
const Entry&
findLongestPrefixMatch(const pit::Entry& pitEntry) const;
/** \brief performs a longest prefix match
*
* This is equivalent to .findLongestPrefixMatch(measurementsEntry.getName())
*/
const Entry&
findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const;
/** \brief performs an exact match lookup
*/
Entry*
findExactMatch(const Name& prefix);
public: // mutation
/** \brief inserts a FIB entry for prefix
*
* If an entry for exact same prefix exists, that entry is returned.
* \return the entry, and true for new entry or false for existing entry
*/
std::pair<Entry*, bool>
insert(const Name& prefix);
void
erase(const Name& prefix);
void
erase(const Entry& entry);
/** \brief removes the NextHop record for face
*/
void
removeNextHop(Entry& entry, const Face& face);
public: // enumeration
class const_iterator;
/** \brief returns an iterator pointing to the first FIB entry
* \note Iteration order is implementation-specific and is undefined
* \note The returned iterator may get invalidated if FIB or another NameTree-based
* table is modified
*/
const_iterator
begin() const;
/** \brief returns an iterator referring to the past-the-end FIB entry
* \note The returned iterator may get invalidated if FIB or another NameTree-based
* table is modified
*/
const_iterator
end() const;
class const_iterator : public std::iterator<std::forward_iterator_tag, const Entry>
{
public:
const_iterator() = default;
explicit
const_iterator(const NameTree::const_iterator& it);
~const_iterator();
const Entry&
operator*() const;
const Entry*
operator->() const;
const_iterator&
operator++();
const_iterator
operator++(int);
bool
operator==(const const_iterator& other) const;
bool
operator!=(const const_iterator& other) const;
private:
NameTree::const_iterator m_nameTreeIterator;
};
private:
const Entry&
findLongestPrefixMatch(const name_tree::Entry* nte) const;
void
erase(name_tree::Entry* nte, bool canDeleteNte = true);
private:
NameTree& m_nameTree;
size_t m_nItems;
/** \brief the empty FIB entry.
*
* This entry has no nexthops.
* It is returned by findLongestPrefixMatch if nothing is matched.
*/
static const unique_ptr<Entry> s_emptyEntry;
};
inline size_t
Fib::size() const
{
return m_nItems;
}
inline Fib::const_iterator
Fib::end() const
{
return const_iterator(m_nameTree.end());
}
inline
Fib::const_iterator::const_iterator(const NameTree::const_iterator& it)
: m_nameTreeIterator(it)
{
}
inline
Fib::const_iterator::~const_iterator()
{
}
inline
Fib::const_iterator
Fib::const_iterator::operator++(int)
{
const_iterator temp(*this);
++(*this);
return temp;
}
inline Fib::const_iterator&
Fib::const_iterator::operator++()
{
++m_nameTreeIterator;
return *this;
}
inline const Entry&
Fib::const_iterator::operator*() const
{
return *m_nameTreeIterator->getFibEntry();
}
inline const Entry*
Fib::const_iterator::operator->() const
{
return m_nameTreeIterator->getFibEntry();
}
inline bool
Fib::const_iterator::operator==(const const_iterator& other) const
{
return m_nameTreeIterator == other.m_nameTreeIterator;
}
inline bool
Fib::const_iterator::operator!=(const const_iterator& other) const
{
return m_nameTreeIterator != other.m_nameTreeIterator;
}
} // namespace fib
using fib::Fib;
} // namespace nfd
#endif // NFD_DAEMON_TABLE_FIB_HPP