From c1e1236b043dae66dd7e08cba516db807988a421 Mon Sep 17 00:00:00 2001 From: Junxiao Shi Date: Fri, 24 Jan 2014 20:03:26 -0700 Subject: [PATCH] table: Mock implementation of FIB refs #1127 Change-Id: Ie0bc1fc2ddcc61dd1f1cf10fb1935edde4aff6c5 --- daemon/common.hpp | 2 + daemon/table/fib-entry.cpp | 67 +++++++++++ daemon/table/fib-entry.hpp | 76 ++++++++++++ daemon/table/fib-nexthop.cpp | 41 +++++++ daemon/table/fib-nexthop.hpp | 43 +++++++ daemon/table/fib.cpp | 78 ++++++++++++ daemon/table/fib.hpp | 48 ++++++++ tests/table/fib.cpp | 224 +++++++++++++++++++++++++++++++++++ 8 files changed, 579 insertions(+) create mode 100644 daemon/table/fib-entry.cpp create mode 100644 daemon/table/fib-entry.hpp create mode 100644 daemon/table/fib-nexthop.cpp create mode 100644 daemon/table/fib-nexthop.hpp create mode 100644 daemon/table/fib.cpp create mode 100644 daemon/table/fib.hpp create mode 100644 tests/table/fib.cpp diff --git a/daemon/common.hpp b/daemon/common.hpp index 0dd2e7e6..d87a0673 100644 --- a/daemon/common.hpp +++ b/daemon/common.hpp @@ -16,8 +16,10 @@ #include #include #include +#include #include +#include namespace ndn { diff --git a/daemon/table/fib-entry.cpp b/daemon/table/fib-entry.cpp new file mode 100644 index 00000000..a00a5828 --- /dev/null +++ b/daemon/table/fib-entry.cpp @@ -0,0 +1,67 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#include "fib-entry.hpp" +#include + +namespace ndn { +namespace fib { + +Entry::Entry(const Name& prefix) + : m_prefix(prefix) +{ +} + +static inline bool +predicate_NextHop_eq_Face(const NextHop& nexthop, shared_ptr face) +{ + return nexthop.getFace() == face; +} + +void +Entry::addNextHop(shared_ptr face, int32_t cost) +{ + NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(), + bind(&predicate_NextHop_eq_Face, _1, face)); + if (it == m_nextHops.end()) { + m_nextHops.push_back(fib::NextHop(face)); + it = m_nextHops.end() - 1; + } + // now it refers to the NextHop for face + + it->setCost(cost); + + this->sortNextHops(); + +} + +void +Entry::removeNextHop(shared_ptr face) +{ + NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(), + bind(&predicate_NextHop_eq_Face, _1, face)); + if (it == m_nextHops.end()) { + return; + } + + m_nextHops.erase(it); +} + +static inline bool +compare_NextHop_cost(const NextHop& a, const NextHop& b) +{ + return a.getCost() < b.getCost(); +} + +void +Entry::sortNextHops() +{ + std::sort(m_nextHops.begin(), m_nextHops.end(), &compare_NextHop_cost); +} + + +} // namespace fib +} // namespace ndn diff --git a/daemon/table/fib-entry.hpp b/daemon/table/fib-entry.hpp new file mode 100644 index 00000000..67f50560 --- /dev/null +++ b/daemon/table/fib-entry.hpp @@ -0,0 +1,76 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#ifndef NFD_TABLE_FIB_ENTRY_HPP +#define NFD_TABLE_FIB_ENTRY_HPP + +#include "fib-nexthop.hpp" + +namespace ndn { +namespace fib { + +/** \class NextHopList + * \brief represents a collection of nexthops + * This type has these methods: + * iterator begin() + * iterator end() + * size_t size() + */ +typedef std::vector NextHopList; + +/** \class Entry + * \brief represents a FIB entry + */ +class Entry : noncopyable +{ +public: + Entry(const Name& prefix); + + const Name& + getPrefix() const; + + /** \brief gives the nexthops explicitly defined on this entry + * This list does not include inherited nexthops. + */ + const NextHopList& + getNextHops() const; + + /// adds a nexthop + void + addNextHop(shared_ptr face, int32_t cost); + + /// removes a nexthop + void + removeNextHop(shared_ptr face); + +private: + /// sorts the nexthop list + void + sortNextHops(); + +private: + Name m_prefix; + NextHopList m_nextHops; +}; + + +inline const Name& +Entry::getPrefix() const +{ + return m_prefix; +} + +inline const NextHopList& +Entry::getNextHops() const +{ + return m_nextHops; +} + + +} // namespace fib +} // namespace ndn + +#endif // NFD_TABLE_FIB_ENTRY_HPP diff --git a/daemon/table/fib-nexthop.cpp b/daemon/table/fib-nexthop.cpp new file mode 100644 index 00000000..06bbdcb3 --- /dev/null +++ b/daemon/table/fib-nexthop.cpp @@ -0,0 +1,41 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#include "fib-nexthop.hpp" + +namespace ndn { +namespace fib { + +NextHop::NextHop(shared_ptr face) + : m_face(face), m_cost(0) +{ +} + +NextHop::NextHop(const NextHop& other) + : m_face(other.m_face), m_cost(other.m_cost) +{ +} + +shared_ptr +NextHop::getFace() const +{ + return m_face; +} + +void +NextHop::setCost(int32_t cost) +{ + m_cost = cost; +} + +int32_t +NextHop::getCost() const +{ + return m_cost; +} + +} // namespace fib +} // namespace ndn diff --git a/daemon/table/fib-nexthop.hpp b/daemon/table/fib-nexthop.hpp new file mode 100644 index 00000000..c3e2cf0c --- /dev/null +++ b/daemon/table/fib-nexthop.hpp @@ -0,0 +1,43 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#ifndef NFD_TABLE_FIB_NEXTHOP_HPP +#define NFD_TABLE_FIB_NEXTHOP_HPP + +#include "common.hpp" +#include "face/face.hpp" + +namespace ndn { +namespace fib { + +/** \class NextHop + * \brief represents a nexthop record in FIB entry + */ +class NextHop +{ +public: + NextHop(shared_ptr face); + + NextHop(const NextHop& other); + + shared_ptr + getFace() const; + + void + setCost(int32_t cost); + + int32_t + getCost() const; + +private: + shared_ptr m_face; + int32_t m_cost; +}; + +} // namespace fib +} // namespace ndn + +#endif // NFD_TABLE_FIB_NEXTHOP_HPP diff --git a/daemon/table/fib.cpp b/daemon/table/fib.cpp new file mode 100644 index 00000000..1d8fe3a7 --- /dev/null +++ b/daemon/table/fib.cpp @@ -0,0 +1,78 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#include "fib.hpp" +#include +#include + +namespace ndn { + +Fib::Fib() +{ + std::pair, bool> pair_rootEntry_dummy = + this->insert(Name()); + m_rootEntry = pair_rootEntry_dummy.first; +} + +Fib::~Fib() +{ +} + +static inline bool +predicate_FibEntry_eq_Name(const shared_ptr& entry, + const Name& name) +{ + return entry->getPrefix().equals(name); +} + +std::pair, bool> +Fib::insert(const Name& prefix) +{ + std::list >::iterator it = std::find_if( + m_table.begin(), m_table.end(), + bind(&predicate_FibEntry_eq_Name, _1, prefix)); + if (it != m_table.end()) return std::make_pair(*it, false); + + shared_ptr entry = make_shared(prefix); + m_table.push_back(entry); + return std::make_pair(entry, true); +} + +static inline const shared_ptr& +accumulate_FibEntry_longestPrefixMatch( + const shared_ptr& bestMatch, + const shared_ptr& entry, const Name& name) +{ + if (!entry->getPrefix().isPrefixOf(name)) return bestMatch; + if (bestMatch->getPrefix().size() < entry->getPrefix().size()) return entry; + return bestMatch; +} + +shared_ptr +Fib::findLongestPrefixMatch(const Name& prefix) const +{ + shared_ptr bestMatch = + std::accumulate(m_table.begin(), m_table.end(), m_rootEntry, + bind(&accumulate_FibEntry_longestPrefixMatch, _1, _2, prefix)); + return bestMatch; +} + +static inline void +FibEntry_removeNextHop(shared_ptr entry, + shared_ptr face) +{ + entry->removeNextHop(face); +} + +void +Fib::removeNextHopFromAllEntries(shared_ptr face) +{ + std::for_each(m_table.begin(), m_table.end(), + bind(&FibEntry_removeNextHop, _1, face)); +} + + +} // namespace ndn diff --git a/daemon/table/fib.hpp b/daemon/table/fib.hpp new file mode 100644 index 00000000..0c88eaaf --- /dev/null +++ b/daemon/table/fib.hpp @@ -0,0 +1,48 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#ifndef NFD_TABLE_FIB_HPP +#define NFD_TABLE_FIB_HPP + +#include "fib-entry.hpp" +namespace ndn { + +/** \class Fib + * \brief represents the FIB + */ +class Fib : noncopyable +{ +public: + Fib(); + + ~Fib(); + + /** \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, false for existing entry } + */ + std::pair, bool> + insert(const Name& prefix); + + /// performs a longest prefix match + shared_ptr + findLongestPrefixMatch(const Name& prefix) const; + + /** \brief removes the NextHop record for face in all entrites + * This is usually invoked when face goes away. + * Removing all NextHops in a FIB entry will not remove the FIB entry. + */ + void + removeNextHopFromAllEntries(shared_ptr face); + +private: + shared_ptr m_rootEntry; + std::list > m_table; +}; + +} // namespace ndn + +#endif // NFD_TABLE_FIB_HPP diff --git a/tests/table/fib.cpp b/tests/table/fib.cpp new file mode 100644 index 00000000..f1ce0b81 --- /dev/null +++ b/tests/table/fib.cpp @@ -0,0 +1,224 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#include "table/fib.hpp" + +#include + +namespace ndn { + +class FibTestFace : public Face +{ +public: + FibTestFace(FaceId id) + : Face(id) + { + } + + virtual void + sendInterest(const Interest &interest) + { + } + + virtual void + sendData(const Data &data) + { + } +}; + +BOOST_AUTO_TEST_SUITE(TableFib) + +BOOST_AUTO_TEST_CASE(Entry) +{ + Name prefix("ndn:/pxWhfFza"); + boost::shared_ptr face1 = make_shared(1); + boost::shared_ptr face2 = make_shared(2); + + fib::Entry entry(prefix); + BOOST_CHECK(entry.getPrefix().equals(prefix)); + + const fib::NextHopList& nexthops1 = entry.getNextHops(); + // [] + BOOST_CHECK_EQUAL(nexthops1.size(), 0); + + entry.addNextHop(face1, 20); + const fib::NextHopList& nexthops2 = entry.getNextHops(); + // [(face1,20)] + BOOST_CHECK_EQUAL(nexthops2.size(), 1); + BOOST_CHECK_EQUAL(nexthops2.begin()->getFace(), face1); + BOOST_CHECK_EQUAL(nexthops2.begin()->getCost(), 20); + + entry.addNextHop(face1, 30); + const fib::NextHopList& nexthops3 = entry.getNextHops(); + // [(face1,30)] + BOOST_CHECK_EQUAL(nexthops3.size(), 1); + BOOST_CHECK_EQUAL(nexthops3.begin()->getFace(), face1); + BOOST_CHECK_EQUAL(nexthops3.begin()->getCost(), 30); + + entry.addNextHop(face2, 40); + const fib::NextHopList& nexthops4 = entry.getNextHops(); + // [(face1,30), (face2,40)] + BOOST_CHECK_EQUAL(nexthops4.size(), 2); + int i = -1; + for (fib::NextHopList::const_iterator it = nexthops4.begin(); + it != nexthops4.end(); ++it) { + ++i; + switch (i) { + case 0 : + BOOST_CHECK_EQUAL(it->getFace(), face1); + BOOST_CHECK_EQUAL(it->getCost(), 30); + break; + case 1 : + BOOST_CHECK_EQUAL(it->getFace(), face2); + BOOST_CHECK_EQUAL(it->getCost(), 40); + break; + } + } + + entry.addNextHop(face2, 10); + const fib::NextHopList& nexthops5 = entry.getNextHops(); + // [(face2,10), (face1,30)] + BOOST_CHECK_EQUAL(nexthops5.size(), 2); + i = -1; + for (fib::NextHopList::const_iterator it = nexthops5.begin(); + it != nexthops5.end(); ++it) { + ++i; + switch (i) { + case 0 : + BOOST_CHECK_EQUAL(it->getFace(), face2); + BOOST_CHECK_EQUAL(it->getCost(), 10); + break; + case 1 : + BOOST_CHECK_EQUAL(it->getFace(), face1); + BOOST_CHECK_EQUAL(it->getCost(), 30); + break; + } + } + + entry.removeNextHop(face1); + const fib::NextHopList& nexthops6 = entry.getNextHops(); + // [(face2,10)] + BOOST_CHECK_EQUAL(nexthops6.size(), 1); + BOOST_CHECK_EQUAL(nexthops6.begin()->getFace(), face2); + BOOST_CHECK_EQUAL(nexthops6.begin()->getCost(), 10); + + entry.removeNextHop(face1); + const fib::NextHopList& nexthops7 = entry.getNextHops(); + // [(face2,10)] + BOOST_CHECK_EQUAL(nexthops7.size(), 1); + BOOST_CHECK_EQUAL(nexthops7.begin()->getFace(), face2); + BOOST_CHECK_EQUAL(nexthops7.begin()->getCost(), 10); + + entry.removeNextHop(face2); + const fib::NextHopList& nexthops8 = entry.getNextHops(); + // [] + BOOST_CHECK_EQUAL(nexthops8.size(), 0); + + entry.removeNextHop(face2); + const fib::NextHopList& nexthops9 = entry.getNextHops(); + // [] + BOOST_CHECK_EQUAL(nexthops9.size(), 0); +} + +BOOST_AUTO_TEST_CASE(Insert_LongestPrefixMatch) +{ + Name nameEmpty; + Name nameA ("ndn:/A"); + Name nameAB ("ndn:/A/B"); + Name nameABC ("ndn:/A/B/C"); + Name nameABCD("ndn:/A/B/C/D"); + Name nameE ("ndn:/E"); + + std::pair, bool> insertRes; + shared_ptr entry; + + Fib fib; + // ['/'] + + entry = fib.findLongestPrefixMatch(nameA); + BOOST_CHECK_NE(entry.get(), static_cast(0)); + BOOST_CHECK(entry->getPrefix().equals(nameEmpty)); + + insertRes = fib.insert(nameA); + BOOST_CHECK_EQUAL(insertRes.second, true); + BOOST_CHECK(insertRes.first->getPrefix().equals(nameA)); + // ['/', '/A'] + + insertRes = fib.insert(nameA); + BOOST_CHECK_EQUAL(insertRes.second, false); + BOOST_CHECK(insertRes.first->getPrefix().equals(nameA)); + // ['/', '/A'] + + entry = fib.findLongestPrefixMatch(nameA); + BOOST_CHECK_NE(entry.get(), static_cast(0)); + BOOST_CHECK(entry->getPrefix().equals(nameA)); + + entry = fib.findLongestPrefixMatch(nameABCD); + BOOST_CHECK_NE(entry.get(), static_cast(0)); + BOOST_CHECK(entry->getPrefix().equals(nameA)); + + insertRes = fib.insert(nameABC); + BOOST_CHECK_EQUAL(insertRes.second, true); + BOOST_CHECK(insertRes.first->getPrefix().equals(nameABC)); + // ['/', '/A', '/A/B/C'] + + entry = fib.findLongestPrefixMatch(nameA); + BOOST_CHECK_NE(entry.get(), static_cast(0)); + BOOST_CHECK(entry->getPrefix().equals(nameA)); + + entry = fib.findLongestPrefixMatch(nameAB); + BOOST_CHECK_NE(entry.get(), static_cast(0)); + BOOST_CHECK(entry->getPrefix().equals(nameA)); + + entry = fib.findLongestPrefixMatch(nameABCD); + BOOST_CHECK_NE(entry.get(), static_cast(0)); + BOOST_CHECK(entry->getPrefix().equals(nameABC)); + + entry = fib.findLongestPrefixMatch(nameE); + BOOST_CHECK_NE(entry.get(), static_cast(0)); + BOOST_CHECK(entry->getPrefix().equals(nameEmpty)); +} + +BOOST_AUTO_TEST_CASE(RemoveNextHopFromAllEntries) +{ + boost::shared_ptr face1 = make_shared(1); + boost::shared_ptr face2 = make_shared(2); + Name nameA("ndn:/A"); + Name nameB("ndn:/B"); + + std::pair, bool> insertRes; + shared_ptr entry; + + Fib fib; + // {'/':[]} + + insertRes = fib.insert(nameA); + insertRes.first->addNextHop(face1, 0); + insertRes.first->addNextHop(face2, 0); + // {'/':[], '/A':[1,2]} + + insertRes = fib.insert(nameB); + insertRes.first->addNextHop(face1, 0); + // {'/':[], '/A':[1,2], '/B':[1]} + + fib.removeNextHopFromAllEntries(face1); + // {'/':[], '/A':[2], '/B':[]} + + entry = fib.findLongestPrefixMatch(nameA); + BOOST_CHECK(entry->getPrefix().equals(nameA)); + const fib::NextHopList& nexthopsA = entry->getNextHops(); + BOOST_CHECK_EQUAL(nexthopsA.size(), 1); + BOOST_CHECK_EQUAL(nexthopsA.begin()->getFace(), face2); + + entry = fib.findLongestPrefixMatch(nameB); + BOOST_CHECK(entry->getPrefix().equals(nameB)); + const fib::NextHopList& nexthopsB = entry->getNextHops(); + BOOST_CHECK_EQUAL(nexthopsB.size(), 0); +} + +BOOST_AUTO_TEST_SUITE_END() + +} // namespace ndn