diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp index 1bb2ebed..2fbc93c8 100644 --- a/daemon/fw/forwarder.cpp +++ b/daemon/fw/forwarder.cpp @@ -28,6 +28,7 @@ #include "core/logger.hpp" #include "core/random.hpp" #include "strategy.hpp" +#include "table/cleanup.hpp" #include namespace nfd { @@ -60,7 +61,7 @@ Forwarder::Forwarder() }); m_faceTable.beforeRemove.connect([this] (Face& face) { - m_fib.removeNextHopFromAllEntries(face); + cleanupOnFaceRemoval(m_nameTree, m_fib, m_pit, face); }); } diff --git a/daemon/table/cleanup.cpp b/daemon/table/cleanup.cpp new file mode 100644 index 00000000..0ba07253 --- /dev/null +++ b/daemon/table/cleanup.cpp @@ -0,0 +1,61 @@ +/* -*- 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 . + */ + +#include "cleanup.hpp" + +namespace nfd { + +void +cleanupOnFaceRemoval(NameTree& nt, Fib& fib, Pit& pit, const Face& face) +{ + std::multimap> maybeEmptyNtes; + + // visit FIB and PIT entries in one pass of NameTree enumeration + for (const name_tree::Entry& nte : nt) { + fib::Entry* fibEntry = nte.getFibEntry(); + if (fibEntry != nullptr) { + fib.removeNextHop(*fibEntry, face); + } + + for (const auto& pitEntry : nte.getPitEntries()) { + pit.deleteInOutRecords(pitEntry, face); + } + + if (nte.getFibEntry() == nullptr && !nte.hasPitEntries()) { + maybeEmptyNtes.emplace(nte.getPrefix().size(), const_pointer_cast(nte.shared_from_this())); + } + } + + // try to erase longer names first, so that children are erased before parent is checked + for (auto i = maybeEmptyNtes.rbegin(); i != maybeEmptyNtes.rend(); ++i) { + shared_ptr nte = i->second.lock(); + // nte may have been erased when its last child is erased + if (nte != nullptr) { + nt.eraseEntryIfEmpty(nte); + } + } +} + +} // namespace nfd diff --git a/daemon/table/cleanup.hpp b/daemon/table/cleanup.hpp new file mode 100644 index 00000000..3c84482e --- /dev/null +++ b/daemon/table/cleanup.hpp @@ -0,0 +1,52 @@ +/* -*- 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 . + */ + +#ifndef NFD_DAEMON_TABLE_CLEANUP_HPP +#define NFD_DAEMON_TABLE_CLEANUP_HPP + +#include "name-tree.hpp" +#include "fib.hpp" +#include "pit.hpp" + +namespace nfd { + +/** \brief cleanup tables when a face is destroyed + * + * This function enumerates the NameTree, calls Fib::removeNextHop for each FIB entry, + * calls Pit::deleteInOutRecords for each PIT entry, and finally + * deletes any name tree entries that have become empty. + * + * \note It's a design choice to let Fib and Pit classes decide what to do with each entry. + * This function is only responsible for implementing the enumeration procedure. + * The benefit of having this function instead of doing the enumeration in Fib and Pit + * classes is to perform both FIB and PIT cleanups in one pass of NameTree enumeration, + * so as to reduce performance overhead. + */ +void +cleanupOnFaceRemoval(NameTree& nt, Fib& fib, Pit& pit, const Face& face); + +} // namespace nfd + +#endif // NFD_DAEMON_TABLE_CLEANUP_HPP diff --git a/daemon/table/fib.cpp b/daemon/table/fib.cpp index 2df31221..890bbbe7 100644 --- a/daemon/table/fib.cpp +++ b/daemon/table/fib.cpp @@ -130,10 +130,14 @@ Fib::insert(const Name& prefix) } void -Fib::erase(shared_ptr nte) +Fib::erase(shared_ptr nte, bool canDeleteNte) { + BOOST_ASSERT(nte != nullptr); + nte->setFibEntry(nullptr); - m_nameTree.eraseEntryIfEmpty(nte); + if (canDeleteNte) { + m_nameTree.eraseEntryIfEmpty(nte); + } --m_nItems; } @@ -151,28 +155,19 @@ Fib::erase(const Entry& entry) { shared_ptr nte = m_nameTree.lookup(entry); if (nte != nullptr) { + // don't try to erase s_emptyEntry this->erase(nte); } } void -Fib::removeNextHopFromAllEntries(const Face& face) +Fib::removeNextHop(Entry& entry, const Face& face) { - std::list toErase; + entry.removeNextHop(face); - auto&& enumerable = m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasFibEntry); - for (const name_tree::Entry& nte : enumerable) { - Entry* entry = nte.getFibEntry(); - entry->removeNextHop(face); - if (!entry->hasNextHops()) { - toErase.push_back(entry); - // entry needs to be erased, but we must wait until the enumeration ends, - // because otherwise NameTree iterator would be invalidated - } - } - - for (Entry* entry : toErase) { - this->erase(*entry); + if (!entry.hasNextHops()) { + shared_ptr nte = m_nameTree.lookup(entry); + this->erase(nte, false); } } diff --git a/daemon/table/fib.hpp b/daemon/table/fib.hpp index bfe66846..df2bfeb8 100644 --- a/daemon/table/fib.hpp +++ b/daemon/table/fib.hpp @@ -93,15 +93,10 @@ public: // mutation void erase(const Entry& entry); - /** \brief removes the NextHop record for face in all entries - * - * This is usually invoked when face is destroyed. - * Removing the last NextHop in a FIB entry will erase the FIB entry. - * - * \todo change parameter type to Face& + /** \brief removes the NextHop record for face */ void - removeNextHopFromAllEntries(const Face& face); + removeNextHop(Entry& entry, const Face& face); public: // enumeration class const_iterator; @@ -158,7 +153,7 @@ private: findLongestPrefixMatch(shared_ptr nte) const; void - erase(shared_ptr nameTreeEntry); + erase(shared_ptr nte, bool canDeleteNte = true); private: NameTree& m_nameTree; diff --git a/daemon/table/pit.cpp b/daemon/table/pit.cpp index 5db1ce2e..57cbf537 100644 --- a/daemon/table/pit.cpp +++ b/daemon/table/pit.cpp @@ -124,17 +124,35 @@ Pit::findAllDataMatches(const Data& data) const } void -Pit::erase(shared_ptr pitEntry) +Pit::erase(shared_ptr entry) { - shared_ptr nameTreeEntry = m_nameTree.getEntry(*pitEntry); - BOOST_ASSERT(nameTreeEntry != nullptr); + this->erase(entry, true); +} - nameTreeEntry->erasePitEntry(pitEntry); - m_nameTree.eraseEntryIfEmpty(nameTreeEntry); +void +Pit::erase(shared_ptr entry, bool canDeleteNte) +{ + shared_ptr nte = m_nameTree.getEntry(*entry); + BOOST_ASSERT(nte != nullptr); + nte->erasePitEntry(entry); + if (canDeleteNte) { + m_nameTree.eraseEntryIfEmpty(nte); + } --m_nItems; } +void +Pit::deleteInOutRecords(shared_ptr entry, const Face& face) +{ + BOOST_ASSERT(entry != nullptr); + + entry->deleteInRecord(face); + entry->deleteOutRecord(face); + + /// \todo decide whether to delete PIT entry if there's no more in/out-record left +} + Pit::const_iterator Pit::begin() const { diff --git a/daemon/table/pit.hpp b/daemon/table/pit.hpp index cea528fb..cbfac865 100644 --- a/daemon/table/pit.hpp +++ b/daemon/table/pit.hpp @@ -81,7 +81,12 @@ public: * \brief erases a PIT Entry */ void - erase(shared_ptr pitEntry); + erase(shared_ptr entry); + + /** \brief deletes in-record and out-record for face + */ + void + deleteInOutRecords(shared_ptr entry, const Face& face); public: // enumeration class const_iterator; @@ -140,6 +145,12 @@ public: // enumeration }; private: + /** + * \brief erases a PIT Entry + */ + void + erase(shared_ptr pitEntry, bool canDeleteNte); + /** \brief finds or inserts a PIT entry for Interest * \param interest the Interest; must be created with make_shared if allowInsert * \param allowInsert whether inserting new entry is allowed. diff --git a/tests/daemon/face/dummy-face.hpp b/tests/daemon/face/dummy-face.hpp index 34f2d09b..e4c68b38 100644 --- a/tests/daemon/face/dummy-face.hpp +++ b/tests/daemon/face/dummy-face.hpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /** - * Copyright (c) 2014-2015, Regents of the University of California, + * Copyright (c) 2014-2016, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -55,7 +55,7 @@ public: ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_POINT_TO_POINT); /** \brief changes face state - * \pre current state is not CLOSED or FAILED + * \throw std::runtime_error state transition is invalid */ void setState(FaceState state); diff --git a/tests/daemon/table/cleanup.t.cpp b/tests/daemon/table/cleanup.t.cpp new file mode 100644 index 00000000..0df019c1 --- /dev/null +++ b/tests/daemon/table/cleanup.t.cpp @@ -0,0 +1,186 @@ +/* -*- 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 . + */ + +#include "table/cleanup.hpp" +#include "fw/forwarder.hpp" + +#include "tests/test-common.hpp" +#include "tests/daemon/face/dummy-face.hpp" + +namespace nfd { +namespace tests { + +using namespace nfd::tests; + +BOOST_AUTO_TEST_SUITE(Table) +BOOST_FIXTURE_TEST_SUITE(TestCleanup, BaseFixture) + +BOOST_AUTO_TEST_SUITE(FaceRemoval) + +BOOST_AUTO_TEST_CASE(Basic) +{ + NameTree nameTree(16); + Fib fib(nameTree); + Pit pit(nameTree); + shared_ptr face1 = make_shared(); + + for (uint64_t i = 0; i < 300; ++i) { + Name name = Name("/P").appendVersion(i); + + fib::Entry* fibEntry = fib.insert(name).first; + fibEntry->addNextHop(*face1, 0); + + shared_ptr interest = makeInterest(name); + shared_ptr pitEntry = pit.insert(*interest).first; + if ((i & 0x01) != 0) { + pitEntry->insertOrUpdateInRecord(face1, *interest); + } + if ((i & 0x02) != 0) { + pitEntry->insertOrUpdateOutRecord(face1, *interest); + } + } + BOOST_CHECK_EQUAL(fib.size(), 300); + BOOST_CHECK_EQUAL(pit.size(), 300); + + cleanupOnFaceRemoval(nameTree, fib, pit, *face1); + BOOST_CHECK_EQUAL(fib.size(), 0); + BOOST_CHECK_EQUAL(pit.size(), 300); + for (const pit::Entry& pitEntry : pit) { + BOOST_CHECK_EQUAL(pitEntry.hasInRecords(), false); + BOOST_CHECK_EQUAL(pitEntry.hasOutRecords(), false); + } +} + +BOOST_AUTO_TEST_CASE(RemoveFibNexthops) +{ + Forwarder forwarder; + NameTree& nameTree = forwarder.getNameTree(); + Fib& fib = forwarder.getFib(); + + shared_ptr face1 = make_shared(); + shared_ptr face2 = make_shared(); + forwarder.addFace(face1); + forwarder.addFace(face2); + + // {} + size_t nNameTreeEntriesBefore = nameTree.size(); + BOOST_CHECK_EQUAL(fib.size(), 0); + + fib::Entry* entryA = fib.insert("/A").first; + entryA->addNextHop(*face1, 0); + entryA->addNextHop(*face2, 0); + // {'/A':[1,2]} + + fib::Entry* entryB = fib.insert("/B").first; + entryB->addNextHop(*face1, 0); + // {'/A':[1,2], '/B':[1]} + + fib::Entry* entryC = fib.insert("/C").first; + entryC->addNextHop(*face2, 1); + // {'/A':[1,2], '/B':[1], '/C':[2]} + + fib::Entry* entryB1 = fib.insert("/B/1").first; + entryB1->addNextHop(*face1, 0); + // {'/A':[1,2], '/B':[1], '/B/1':[1], '/C':[2]} + + fib::Entry* entryB12 = fib.insert("/B/1/2").first; + entryB12->addNextHop(*face1, 0); + // {'/A':[1,2], '/B':[1], '/B/1':[1], '/B/1/2':[1], '/C':[2]} + + // ---- close face1 ---- + face1->close(); + BOOST_CHECK_EQUAL(face1->getState(), face::FaceState::CLOSED); + // {'/A':[2], '/C':[2]} + BOOST_CHECK_EQUAL(fib.size(), 2); + + const fib::Entry& foundA = fib.findLongestPrefixMatch("/A"); + BOOST_CHECK_EQUAL(foundA.getPrefix(), "/A"); + BOOST_CHECK_EQUAL(foundA.getNextHops().size(), 1); + BOOST_CHECK_EQUAL(&foundA.getNextHops().begin()->getFace(), face2.get()); + + BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/B").getPrefix(), "/"); + + // ---- close face2 ---- + face2->close(); + BOOST_CHECK_EQUAL(face2->getState(), face::FaceState::CLOSED); + BOOST_CHECK_EQUAL(fib.size(), 0); + BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); +} + +BOOST_AUTO_TEST_CASE(DeletePitInOutRecords) +{ + Forwarder forwarder; + Pit& pit = forwarder.getPit(); + + shared_ptr face1 = make_shared(); + shared_ptr face2 = make_shared(); + forwarder.addFace(face1); + forwarder.addFace(face2); + + // {} + BOOST_CHECK_EQUAL(pit.size(), 0); + + shared_ptr interestA = makeInterest("/A"); + shared_ptr entryA = pit.insert(*interestA).first; + entryA->insertOrUpdateInRecord(face1, *interestA); + entryA->insertOrUpdateInRecord(face2, *interestA); + entryA->insertOrUpdateOutRecord(face1, *interestA); + entryA->insertOrUpdateOutRecord(face2, *interestA); + // {'/A':[1,2]} + + shared_ptr interestB = makeInterest("/B"); + shared_ptr entryB = pit.insert(*interestB).first; + entryB->insertOrUpdateInRecord(face1, *interestB); + entryB->insertOrUpdateOutRecord(face1, *interestB); + // {'/A':[1,2], '/B':[1]} + + shared_ptr interestC = makeInterest("/C"); + shared_ptr entryC = pit.insert(*interestC).first; + entryC->insertOrUpdateInRecord(face2, *interestC); + entryC->insertOrUpdateOutRecord(face2, *interestC); + // {'/A':[1,2], '/B':[1], '/C':[2]} + BOOST_CHECK_EQUAL(pit.size(), 3); + + // ---- close face1 ---- + face1->close(); + BOOST_CHECK_EQUAL(face1->getState(), face::FaceState::CLOSED); + // {'/A':[2], '/B':[], '/C':[2]} + BOOST_CHECK_EQUAL(pit.size(), 3); + + shared_ptr foundA = pit.find(*interestA); + BOOST_REQUIRE(foundA != nullptr); + BOOST_REQUIRE_EQUAL(foundA->getInRecords().size(), 1); + BOOST_CHECK_EQUAL(foundA->getInRecords().front().getFace(), face2); + BOOST_REQUIRE_EQUAL(foundA->getOutRecords().size(), 1); + BOOST_CHECK_EQUAL(foundA->getOutRecords().front().getFace(), face2); +} + +BOOST_AUTO_TEST_SUITE_END() // FaceRemovalCleanup + +BOOST_AUTO_TEST_SUITE_END() // TestCleanup +BOOST_AUTO_TEST_SUITE_END() // Table + +} // namespace tests +} // namespace nfd diff --git a/tests/daemon/table/fib.t.cpp b/tests/daemon/table/fib.t.cpp index 46a3bca0..204c5a36 100644 --- a/tests/daemon/table/fib.t.cpp +++ b/tests/daemon/table/fib.t.cpp @@ -214,71 +214,6 @@ BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithMeasurementsEntry) BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mABCD).getPrefix(), "/A/B/C"); } -BOOST_AUTO_TEST_CASE(RemoveNextHopFromAllEntries) -{ - shared_ptr face1 = make_shared(); - shared_ptr face2 = make_shared(); - - NameTree nameTree; - Fib fib(nameTree); - // {} - - Entry* entryA = fib.insert("/A").first; - entryA->addNextHop(*face1, 0); - entryA->addNextHop(*face2, 0); - // {'/A':[1,2]} - - Entry* entryB = fib.insert("/B").first; - entryB->addNextHop(*face1, 0); - // {'/A':[1,2], '/B':[1]} - BOOST_CHECK_EQUAL(fib.size(), 2); - - Entry* entryC = fib.insert("/C").first; - entryC->addNextHop(*face2, 1); - // {'/A':[1,2], '/B':[1], '/C':[2]} - BOOST_CHECK_EQUAL(fib.size(), 3); - - Entry* entryB1 = fib.insert("/B/1").first; - entryB1->addNextHop(*face1, 0); - // {'/A':[1,2], '/B':[1], '/B/1':[1], '/C':[2]} - BOOST_CHECK_EQUAL(fib.size(), 4); - - Entry* entryB12 = fib.insert("/B/1/2").first; - entryB12->addNextHop(*face1, 0); - // {'/A':[1,2], '/B':[1], '/B/1':[1], '/B/1/2':[1], '/C':[2]} - BOOST_CHECK_EQUAL(fib.size(), 5); - - ///////////// - - fib.removeNextHopFromAllEntries(*face1); - // {'/A':[2], '/C':[2]} - BOOST_CHECK_EQUAL(fib.size(), 2); - - const Entry& foundA = fib.findLongestPrefixMatch("/A"); - BOOST_CHECK_EQUAL(foundA.getPrefix(), "/A"); - const NextHopList& nexthopsA = foundA.getNextHops(); - BOOST_CHECK_EQUAL(nexthopsA.size(), 1); - BOOST_CHECK_EQUAL(&nexthopsA.begin()->getFace(), face2.get()); - - BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/B").getPrefix(), "/"); -} - -BOOST_AUTO_TEST_CASE(RemoveNextHopFromManyEntries) -{ - NameTree nameTree(16); - Fib fib(nameTree); - shared_ptr face1 = make_shared(); - - for (uint64_t i = 0; i < 300; ++i) { - Entry* entry = fib.insert(Name("/P").appendVersion(i)).first; - entry->addNextHop(*face1, 0); - } - BOOST_CHECK_EQUAL(fib.size(), 300); - - fib.removeNextHopFromAllEntries(*face1); - BOOST_CHECK_EQUAL(fib.size(), 0); -} - void validateFindExactMatch(Fib& fib, const Name& target) {