table: delete PIT in-record and out-record when face is destroyed

refs #3685

Change-Id: I3c20417c9a30db69c90c9d59a30af1529bc1f98e
This commit is contained in:
Junxiao Shi committed 2016-07-28 01:48:27 +00:00
1 parent 2570f3ec4f
commit 02b73f5705
10 files changed
+353 -99

No files matched your search

+2 -1
View File
@@ -28,6 +28,7 @@
#include "core/logger.hpp"
#include "core/random.hpp"
#include "strategy.hpp"
#include "table/cleanup.hpp"
#include <boost/random/uniform_int_distribution.hpp>
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);
});
}
+61
View File
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "cleanup.hpp"
namespace nfd {
void
cleanupOnFaceRemoval(NameTree& nt, Fib& fib, Pit& pit, const Face& face)
{
std::multimap<size_t, weak_ptr<name_tree::Entry>> 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<name_tree::Entry>(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<name_tree::Entry> nte = i->second.lock();
// nte may have been erased when its last child is erased
if (nte != nullptr) {
nt.eraseEntryIfEmpty(nte);
}
}
}
} // namespace nfd
+52
View File
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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
+12 -17
View File
@@ -130,10 +130,14 @@ Fib::insert(const Name& prefix)
}
void
Fib::erase(shared_ptr<name_tree::Entry> nte)
Fib::erase(shared_ptr<name_tree::Entry> 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<name_tree::Entry> 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<Entry*> 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<name_tree::Entry> nte = m_nameTree.lookup(entry);
this->erase(nte, false);
}
}
+3 -8
View File
@@ -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<name_tree::Entry> nte) const;
void
erase(shared_ptr<name_tree::Entry> nameTreeEntry);
erase(shared_ptr<name_tree::Entry> nte, bool canDeleteNte = true);
private:
NameTree& m_nameTree;
+23 -5
View File
@@ -124,17 +124,35 @@ Pit::findAllDataMatches(const Data& data) const
}
void
Pit::erase(shared_ptr<pit::Entry> pitEntry)
Pit::erase(shared_ptr<pit::Entry> entry)
{
shared_ptr<name_tree::Entry> 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<pit::Entry> entry, bool canDeleteNte)
{
shared_ptr<name_tree::Entry> 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<pit::Entry> 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
{
+12 -1
View File
@@ -81,7 +81,12 @@ public:
* \brief erases a PIT Entry
*/
void
erase(shared_ptr<pit::Entry> pitEntry);
erase(shared_ptr<pit::Entry> entry);
/** \brief deletes in-record and out-record for face
*/
void
deleteInOutRecords(shared_ptr<pit::Entry> 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<pit::Entry> 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.
+2 -2
View File
@@ -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);
+186
View File
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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<Face> face1 = make_shared<DummyFace>();
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> interest = makeInterest(name);
shared_ptr<pit::Entry> 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<Face> face1 = make_shared<DummyFace>();
shared_ptr<Face> face2 = make_shared<DummyFace>();
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<Face> face1 = make_shared<DummyFace>();
shared_ptr<Face> face2 = make_shared<DummyFace>();
forwarder.addFace(face1);
forwarder.addFace(face2);
// {}
BOOST_CHECK_EQUAL(pit.size(), 0);
shared_ptr<Interest> interestA = makeInterest("/A");
shared_ptr<pit::Entry> 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<Interest> interestB = makeInterest("/B");
shared_ptr<pit::Entry> entryB = pit.insert(*interestB).first;
entryB->insertOrUpdateInRecord(face1, *interestB);
entryB->insertOrUpdateOutRecord(face1, *interestB);
// {'/A':[1,2], '/B':[1]}
shared_ptr<Interest> interestC = makeInterest("/C");
shared_ptr<pit::Entry> 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<pit::Entry> 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
-65
View File
@@ -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<Face> face1 = make_shared<DummyFace>();
shared_ptr<Face> face2 = make_shared<DummyFace>();
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<Face> face1 = make_shared<DummyFace>();
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)
{