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