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 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 31 | namespace nfd::fw { |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 32 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 33 | /** |
| 34 | * \brief Decision made by UnsolicitedDataPolicy |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 35 | */ |
| 36 | enum class UnsolicitedDataDecision { |
| 37 | DROP, ///< the Data should be dropped |
| 38 | CACHE ///< the Data should be cached in the ContentStore |
| 39 | }; |
| 40 | |
| 41 | std::ostream& |
| 42 | operator<<(std::ostream& os, UnsolicitedDataDecision d); |
| 43 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 44 | /** |
| 45 | * \brief Determines how to process an unsolicited Data packet |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 46 | * |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 47 | * An incoming Data packet is *unsolicited* if it does not match any PIT entry. |
| 48 | * This class assists forwarding pipelines to decide whether to drop an unsolicited Data |
| 49 | * or admit it into the ContentStore. |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 50 | */ |
| 51 | class UnsolicitedDataPolicy : noncopyable |
| 52 | { |
| 53 | public: |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 54 | virtual |
| 55 | ~UnsolicitedDataPolicy() = default; |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 56 | |
| 57 | virtual UnsolicitedDataDecision |
| 58 | decide(const Face& inFace, const Data& data) const = 0; |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 59 | |
| 60 | public: // registry |
| 61 | template<typename P> |
| 62 | static void |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 63 | registerPolicy(const std::string& policyName = P::POLICY_NAME) |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 64 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 65 | BOOST_ASSERT(!policyName.empty()); |
| 66 | auto r = getRegistry().insert_or_assign(policyName, [] { return make_unique<P>(); }); |
| 67 | BOOST_VERIFY(r.second); |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 70 | /** \return an UnsolicitedDataPolicy identified by \p policyName, |
| 71 | * or nullptr if \p policyName is unknown |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 72 | */ |
| 73 | static unique_ptr<UnsolicitedDataPolicy> |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 74 | create(const std::string& policyName); |
| 75 | |
| 76 | /** \return a list of available policy names |
| 77 | */ |
| 78 | static std::set<std::string> |
| 79 | getPolicyNames(); |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 80 | |
| 81 | private: |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 82 | using CreateFunc = std::function<unique_ptr<UnsolicitedDataPolicy>()>; |
| 83 | using Registry = std::map<std::string, CreateFunc>; // indexed by policy name |
Junxiao Shi | c29c0b0 | 2016-12-04 21:11:44 +0000 | [diff] [blame] | 84 | |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 85 | static Registry& |
| 86 | getRegistry(); |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 89 | /** |
| 90 | * \brief Drops all unsolicited Data |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 91 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 92 | class DropAllUnsolicitedDataPolicy final : public UnsolicitedDataPolicy |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 93 | { |
| 94 | public: |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 95 | UnsolicitedDataDecision |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 96 | decide(const Face& inFace, const Data& data) const final; |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 97 | |
| 98 | public: |
| 99 | static const std::string POLICY_NAME; |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 102 | /** |
| 103 | * \brief Admits unsolicited Data from local faces |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 104 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 105 | class AdmitLocalUnsolicitedDataPolicy final : public UnsolicitedDataPolicy |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 106 | { |
| 107 | public: |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 108 | UnsolicitedDataDecision |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 109 | decide(const Face& inFace, const Data& data) const final; |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 110 | |
| 111 | public: |
| 112 | static const std::string POLICY_NAME; |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 115 | /** |
| 116 | * \brief Admits unsolicited Data from non-local faces |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 117 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 118 | class AdmitNetworkUnsolicitedDataPolicy final : public UnsolicitedDataPolicy |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 119 | { |
| 120 | public: |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 121 | UnsolicitedDataDecision |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 122 | decide(const Face& inFace, const Data& data) const final; |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 123 | |
| 124 | public: |
| 125 | static const std::string POLICY_NAME; |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 128 | /** |
| 129 | * \brief Admits all unsolicited Data |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 130 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 131 | class AdmitAllUnsolicitedDataPolicy final : public UnsolicitedDataPolicy |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 132 | { |
| 133 | public: |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 134 | UnsolicitedDataDecision |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 135 | decide(const Face& inFace, const Data& data) const final; |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 136 | |
| 137 | public: |
| 138 | static const std::string POLICY_NAME; |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 141 | /** |
| 142 | * \brief The default UnsolicitedDataPolicy |
Junxiao Shi | 9685cc5 | 2016-08-29 12:47:05 +0000 | [diff] [blame] | 143 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 144 | using DefaultUnsolicitedDataPolicy = DropAllUnsolicitedDataPolicy; |
Junxiao Shi | 9685cc5 | 2016-08-29 12:47:05 +0000 | [diff] [blame] | 145 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 146 | } // namespace nfd::fw |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 147 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 148 | /** |
| 149 | * \brief Registers an unsolicited data policy |
| 150 | * \param P A subclass of nfd::fw::UnsolicitedDataPolicy. \p P must have a static data |
| 151 | * member `POLICY_NAME` convertible to std::string that contains the policy name. |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 152 | */ |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 153 | #define NFD_REGISTER_UNSOLICITED_DATA_POLICY(P) \ |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 154 | static class NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass \ |
| 155 | { \ |
| 156 | public: \ |
| 157 | NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass() \ |
| 158 | { \ |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 159 | ::nfd::fw::UnsolicitedDataPolicy::registerPolicy<P>(); \ |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 160 | } \ |
| 161 | } g_nfdAuto ## P ## UnsolicitedDataPolicyRegistrationVariable |
| 162 | |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 163 | #endif // NFD_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP |