Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #ifndef NFD_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP |
| 27 | #define NFD_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP |
| 28 | |
| 29 | #include "face/face.hpp" |
| 30 | |
| 31 | namespace nfd { |
| 32 | namespace fw { |
| 33 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 34 | /** |
| 35 | * \brief Decision made by UnsolicitedDataPolicy |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 36 | */ |
| 37 | enum class UnsolicitedDataDecision { |
| 38 | DROP, ///< the Data should be dropped |
| 39 | CACHE ///< the Data should be cached in the ContentStore |
| 40 | }; |
| 41 | |
| 42 | std::ostream& |
| 43 | operator<<(std::ostream& os, UnsolicitedDataDecision d); |
| 44 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 45 | /** |
| 46 | * \brief Determines how to process an unsolicited Data packet |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 47 | * |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 48 | * An incoming Data packet is *unsolicited* if it does not match any PIT entry. |
| 49 | * This class assists forwarding pipelines to decide whether to drop an unsolicited Data |
| 50 | * or admit it into the ContentStore. |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 51 | */ |
| 52 | class UnsolicitedDataPolicy : noncopyable |
| 53 | { |
| 54 | public: |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 55 | virtual |
| 56 | ~UnsolicitedDataPolicy() = default; |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 57 | |
| 58 | virtual UnsolicitedDataDecision |
| 59 | decide(const Face& inFace, const Data& data) const = 0; |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 60 | |
| 61 | public: // registry |
| 62 | template<typename P> |
| 63 | static void |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 64 | registerPolicy(const std::string& policyName = P::POLICY_NAME) |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 65 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 66 | BOOST_ASSERT(!policyName.empty()); |
| 67 | auto r = getRegistry().insert_or_assign(policyName, [] { return make_unique<P>(); }); |
| 68 | BOOST_VERIFY(r.second); |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 71 | /** \return an UnsolicitedDataPolicy identified by \p policyName, |
| 72 | * or nullptr if \p policyName is unknown |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 73 | */ |
| 74 | static unique_ptr<UnsolicitedDataPolicy> |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 75 | create(const std::string& policyName); |
| 76 | |
| 77 | /** \return a list of available policy names |
| 78 | */ |
| 79 | static std::set<std::string> |
| 80 | getPolicyNames(); |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 81 | |
| 82 | private: |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 83 | using CreateFunc = std::function<unique_ptr<UnsolicitedDataPolicy>()>; |
| 84 | using Registry = std::map<std::string, CreateFunc>; // indexed by policy name |
Junxiao Shi | c29c0b0 | 2016-12-04 21:11:44 +0000 | [diff] [blame] | 85 | |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 86 | static Registry& |
| 87 | getRegistry(); |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 90 | /** |
| 91 | * \brief Drops all unsolicited Data |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 92 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 93 | class DropAllUnsolicitedDataPolicy final : public UnsolicitedDataPolicy |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 94 | { |
| 95 | public: |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 96 | UnsolicitedDataDecision |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 97 | decide(const Face& inFace, const Data& data) const final; |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 98 | |
| 99 | public: |
| 100 | static const std::string POLICY_NAME; |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 103 | /** |
| 104 | * \brief Admits unsolicited Data from local faces |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 105 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 106 | class AdmitLocalUnsolicitedDataPolicy final : public UnsolicitedDataPolicy |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 107 | { |
| 108 | public: |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 109 | UnsolicitedDataDecision |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 110 | decide(const Face& inFace, const Data& data) const final; |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 111 | |
| 112 | public: |
| 113 | static const std::string POLICY_NAME; |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 116 | /** |
| 117 | * \brief Admits unsolicited Data from non-local faces |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 118 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 119 | class AdmitNetworkUnsolicitedDataPolicy final : public UnsolicitedDataPolicy |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 120 | { |
| 121 | public: |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 122 | UnsolicitedDataDecision |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 123 | decide(const Face& inFace, const Data& data) const final; |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 124 | |
| 125 | public: |
| 126 | static const std::string POLICY_NAME; |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 129 | /** |
| 130 | * \brief Admits all unsolicited Data |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 131 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 132 | class AdmitAllUnsolicitedDataPolicy final : public UnsolicitedDataPolicy |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 133 | { |
| 134 | public: |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 135 | UnsolicitedDataDecision |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 136 | decide(const Face& inFace, const Data& data) const final; |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 137 | |
| 138 | public: |
| 139 | static const std::string POLICY_NAME; |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 142 | /** |
| 143 | * \brief The default UnsolicitedDataPolicy |
Junxiao Shi | 9685cc5 | 2016-08-29 12:47:05 +0000 | [diff] [blame] | 144 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 145 | using DefaultUnsolicitedDataPolicy = DropAllUnsolicitedDataPolicy; |
Junxiao Shi | 9685cc5 | 2016-08-29 12:47:05 +0000 | [diff] [blame] | 146 | |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 147 | } // namespace fw |
| 148 | } // namespace nfd |
| 149 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 150 | /** |
| 151 | * \brief Registers an unsolicited data policy |
| 152 | * \param P A subclass of nfd::fw::UnsolicitedDataPolicy. \p P must have a static data |
| 153 | * member `POLICY_NAME` convertible to std::string that contains the policy name. |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 154 | */ |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 155 | #define NFD_REGISTER_UNSOLICITED_DATA_POLICY(P) \ |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 156 | static class NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass \ |
| 157 | { \ |
| 158 | public: \ |
| 159 | NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass() \ |
| 160 | { \ |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 161 | ::nfd::fw::UnsolicitedDataPolicy::registerPolicy<P>(); \ |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 162 | } \ |
| 163 | } g_nfdAuto ## P ## UnsolicitedDataPolicyRegistrationVariable |
| 164 | |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 165 | #endif // NFD_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP |