diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp index 4fc9c380..ae4369e0 100644 --- a/daemon/fw/forwarder.cpp +++ b/daemon/fw/forwarder.cpp @@ -35,10 +35,9 @@ namespace nfd { NFD_LOG_INIT("Forwarder"); -using fw::Strategy; - Forwarder::Forwarder() - : m_fib(m_nameTree) + : m_unsolicitedDataPolicy(new fw::AdmitLocalUnsolicitedDataPolicy()) + , m_fib(m_nameTree) , m_pit(m_nameTree) , m_measurements(m_nameTree) , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this)) @@ -389,15 +388,15 @@ void Forwarder::onDataUnsolicited(Face& inFace, const Data& data) { // accept to cache? - bool acceptToCache = inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL; - if (acceptToCache) { + fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data); + if (decision == fw::UnsolicitedDataDecision::CACHE) { // CS insert m_cs.insert(data, true); } NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() << " data=" << data.getName() << - (acceptToCache ? " cached" : " not cached")); + " decision=" << decision); } void diff --git a/daemon/fw/forwarder.hpp b/daemon/fw/forwarder.hpp index 855c24e8..00645bad 100644 --- a/daemon/fw/forwarder.hpp +++ b/daemon/fw/forwarder.hpp @@ -30,6 +30,7 @@ #include "core/scheduler.hpp" #include "forwarder-counters.hpp" #include "face-table.hpp" +#include "unsolicited-data-policy.hpp" #include "table/fib.hpp" #include "table/pit.hpp" #include "table/cs.hpp" @@ -62,7 +63,7 @@ public: return m_counters; } -public: // faces +public: // faces and policies FaceTable& getFaceTable() { @@ -89,6 +90,19 @@ public: // faces m_faceTable.add(face); } + fw::UnsolicitedDataPolicy& + getUnsolicitedDataPolicy() const + { + return *m_unsolicitedDataPolicy; + } + + void + setUnsolicitedDataPolicy(unique_ptr policy) + { + BOOST_ASSERT(policy != nullptr); + m_unsolicitedDataPolicy = std::move(policy); + } + public: // forwarding entrypoints and tables /** \brief start incoming Interest processing * \param face face on which Interest is received @@ -273,8 +287,8 @@ private: ForwarderCounters m_counters; FaceTable m_faceTable; + unique_ptr m_unsolicitedDataPolicy; - // tables NameTree m_nameTree; Fib m_fib; Pit m_pit; diff --git a/daemon/fw/unsolicited-data-policy.cpp b/daemon/fw/unsolicited-data-policy.cpp new file mode 100644 index 00000000..894cc865 --- /dev/null +++ b/daemon/fw/unsolicited-data-policy.cpp @@ -0,0 +1,53 @@ +/* -*- 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 "unsolicited-data-policy.hpp" + +namespace nfd { +namespace fw { + +std::ostream& +operator<<(std::ostream& os, UnsolicitedDataDecision d) +{ + switch (d) { + case UnsolicitedDataDecision::DROP: + return os << "drop"; + case UnsolicitedDataDecision::CACHE: + return os << "cache"; + } + return os << static_cast(d); +} + +UnsolicitedDataDecision +AdmitLocalUnsolicitedDataPolicy::decide(const Face& inFace, const Data& data) const +{ + if (inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) { + return UnsolicitedDataDecision::CACHE; + } + return UnsolicitedDataDecision::DROP; +} + +} // namespace fw +} // namespace nfd diff --git a/daemon/fw/unsolicited-data-policy.hpp b/daemon/fw/unsolicited-data-policy.hpp new file mode 100644 index 00000000..54112ecf --- /dev/null +++ b/daemon/fw/unsolicited-data-policy.hpp @@ -0,0 +1,71 @@ +/* -*- 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_FW_UNSOLICITED_DATA_POLICY_HPP +#define NFD_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP + +#include "face/face.hpp" + +namespace nfd { +namespace fw { + +/** \brief a decision made by UnsolicitedDataPolicy + */ +enum class UnsolicitedDataDecision { + DROP, ///< the Data should be dropped + CACHE ///< the Data should be cached in the ContentStore +}; + +std::ostream& +operator<<(std::ostream& os, UnsolicitedDataDecision d); + +/** \brief determines how to process an unsolicited Data + * + * An incoming Data is unsolicited if it does not match any PIT entry. + * This class assists forwarding pipelines to decide whether to drop an unsolicited Data + * or admit it into the ContentStore. + */ +class UnsolicitedDataPolicy : noncopyable +{ +public: + virtual ~UnsolicitedDataPolicy() = default; + + virtual UnsolicitedDataDecision + decide(const Face& inFace, const Data& data) const = 0; +}; + +/** \brief admits unsolicited Data from local faces + */ +class AdmitLocalUnsolicitedDataPolicy : public UnsolicitedDataPolicy +{ +public: + virtual UnsolicitedDataDecision + decide(const Face& inFace, const Data& data) const final; +}; + +} // namespace fw +} // namespace nfd + +#endif // NFD_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP