blob: 486903a348939b49e2a9b5990354f5c17707d19c [file] [log] [blame]
Junxiao Shifbe8efe2016-08-22 16:02:30 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento3db98072021-03-09 23:03:27 -05002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shifbe8efe2016-08-22 16:02:30 +00004 * 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
31namespace nfd {
32namespace fw {
33
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040034/**
35 * \brief Decision made by UnsolicitedDataPolicy
Junxiao Shifbe8efe2016-08-22 16:02:30 +000036 */
37enum class UnsolicitedDataDecision {
38 DROP, ///< the Data should be dropped
39 CACHE ///< the Data should be cached in the ContentStore
40};
41
42std::ostream&
43operator<<(std::ostream& os, UnsolicitedDataDecision d);
44
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040045/**
46 * \brief Determines how to process an unsolicited Data packet
Junxiao Shifbe8efe2016-08-22 16:02:30 +000047 *
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040048 * 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 Shifbe8efe2016-08-22 16:02:30 +000051 */
52class UnsolicitedDataPolicy : noncopyable
53{
54public:
Junxiao Shi9db73592016-12-01 23:43:11 +000055 virtual
56 ~UnsolicitedDataPolicy() = default;
Junxiao Shifbe8efe2016-08-22 16:02:30 +000057
58 virtual UnsolicitedDataDecision
59 decide(const Face& inFace, const Data& data) const = 0;
Junxiao Shi58320f72016-09-04 04:40:34 +000060
61public: // registry
62 template<typename P>
63 static void
Junxiao Shi9db73592016-12-01 23:43:11 +000064 registerPolicy(const std::string& policyName = P::POLICY_NAME)
Junxiao Shi58320f72016-09-04 04:40:34 +000065 {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040066 BOOST_ASSERT(!policyName.empty());
67 auto r = getRegistry().insert_or_assign(policyName, [] { return make_unique<P>(); });
68 BOOST_VERIFY(r.second);
Junxiao Shi58320f72016-09-04 04:40:34 +000069 }
70
Junxiao Shi9db73592016-12-01 23:43:11 +000071 /** \return an UnsolicitedDataPolicy identified by \p policyName,
72 * or nullptr if \p policyName is unknown
Junxiao Shi58320f72016-09-04 04:40:34 +000073 */
74 static unique_ptr<UnsolicitedDataPolicy>
Junxiao Shi9db73592016-12-01 23:43:11 +000075 create(const std::string& policyName);
76
77 /** \return a list of available policy names
78 */
79 static std::set<std::string>
80 getPolicyNames();
Junxiao Shi58320f72016-09-04 04:40:34 +000081
82private:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040083 using CreateFunc = std::function<unique_ptr<UnsolicitedDataPolicy>()>;
84 using Registry = std::map<std::string, CreateFunc>; // indexed by policy name
Junxiao Shic29c0b02016-12-04 21:11:44 +000085
Junxiao Shi58320f72016-09-04 04:40:34 +000086 static Registry&
87 getRegistry();
Junxiao Shifbe8efe2016-08-22 16:02:30 +000088};
89
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040090/**
91 * \brief Drops all unsolicited Data
Junxiao Shi36b22252016-08-25 13:22:21 +000092 */
Davide Pesavento3db98072021-03-09 23:03:27 -050093class DropAllUnsolicitedDataPolicy final : public UnsolicitedDataPolicy
Junxiao Shi36b22252016-08-25 13:22:21 +000094{
95public:
Junxiao Shi9db73592016-12-01 23:43:11 +000096 UnsolicitedDataDecision
Junxiao Shi36b22252016-08-25 13:22:21 +000097 decide(const Face& inFace, const Data& data) const final;
Junxiao Shi9db73592016-12-01 23:43:11 +000098
99public:
100 static const std::string POLICY_NAME;
Junxiao Shi36b22252016-08-25 13:22:21 +0000101};
102
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400103/**
104 * \brief Admits unsolicited Data from local faces
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000105 */
Davide Pesavento3db98072021-03-09 23:03:27 -0500106class AdmitLocalUnsolicitedDataPolicy final : public UnsolicitedDataPolicy
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000107{
108public:
Junxiao Shi9db73592016-12-01 23:43:11 +0000109 UnsolicitedDataDecision
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000110 decide(const Face& inFace, const Data& data) const final;
Junxiao Shi9db73592016-12-01 23:43:11 +0000111
112public:
113 static const std::string POLICY_NAME;
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000114};
115
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400116/**
117 * \brief Admits unsolicited Data from non-local faces
Junxiao Shi36b22252016-08-25 13:22:21 +0000118 */
Davide Pesavento3db98072021-03-09 23:03:27 -0500119class AdmitNetworkUnsolicitedDataPolicy final : public UnsolicitedDataPolicy
Junxiao Shi36b22252016-08-25 13:22:21 +0000120{
121public:
Junxiao Shi9db73592016-12-01 23:43:11 +0000122 UnsolicitedDataDecision
Junxiao Shi36b22252016-08-25 13:22:21 +0000123 decide(const Face& inFace, const Data& data) const final;
Junxiao Shi9db73592016-12-01 23:43:11 +0000124
125public:
126 static const std::string POLICY_NAME;
Junxiao Shi36b22252016-08-25 13:22:21 +0000127};
128
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400129/**
130 * \brief Admits all unsolicited Data
Junxiao Shi36b22252016-08-25 13:22:21 +0000131 */
Davide Pesavento3db98072021-03-09 23:03:27 -0500132class AdmitAllUnsolicitedDataPolicy final : public UnsolicitedDataPolicy
Junxiao Shi36b22252016-08-25 13:22:21 +0000133{
134public:
Junxiao Shi9db73592016-12-01 23:43:11 +0000135 UnsolicitedDataDecision
Junxiao Shi36b22252016-08-25 13:22:21 +0000136 decide(const Face& inFace, const Data& data) const final;
Junxiao Shi9db73592016-12-01 23:43:11 +0000137
138public:
139 static const std::string POLICY_NAME;
Junxiao Shi36b22252016-08-25 13:22:21 +0000140};
141
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400142/**
143 * \brief The default UnsolicitedDataPolicy
Junxiao Shi9685cc52016-08-29 12:47:05 +0000144 */
Davide Pesavento3db98072021-03-09 23:03:27 -0500145using DefaultUnsolicitedDataPolicy = DropAllUnsolicitedDataPolicy;
Junxiao Shi9685cc52016-08-29 12:47:05 +0000146
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000147} // namespace fw
148} // namespace nfd
149
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400150/**
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 Shi58320f72016-09-04 04:40:34 +0000154 */
Junxiao Shi9db73592016-12-01 23:43:11 +0000155#define NFD_REGISTER_UNSOLICITED_DATA_POLICY(P) \
Junxiao Shi58320f72016-09-04 04:40:34 +0000156static class NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass \
157{ \
158public: \
159 NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass() \
160 { \
Junxiao Shi9db73592016-12-01 23:43:11 +0000161 ::nfd::fw::UnsolicitedDataPolicy::registerPolicy<P>(); \
Junxiao Shi58320f72016-09-04 04:40:34 +0000162 } \
163} g_nfdAuto ## P ## UnsolicitedDataPolicyRegistrationVariable
164
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000165#endif // NFD_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP