blob: ec9984bedfeee08176d9a36595c158e790214b9a [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 Pesaventoae430302023-05-11 01:42:46 -04003 * Copyright (c) 2014-2023, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::fw {
Junxiao Shifbe8efe2016-08-22 16:02:30 +000032
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040033/**
34 * \brief Decision made by UnsolicitedDataPolicy
Junxiao Shifbe8efe2016-08-22 16:02:30 +000035 */
36enum class UnsolicitedDataDecision {
37 DROP, ///< the Data should be dropped
38 CACHE ///< the Data should be cached in the ContentStore
39};
40
41std::ostream&
42operator<<(std::ostream& os, UnsolicitedDataDecision d);
43
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040044/**
45 * \brief Determines how to process an unsolicited Data packet
Junxiao Shifbe8efe2016-08-22 16:02:30 +000046 *
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040047 * 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 Shifbe8efe2016-08-22 16:02:30 +000050 */
51class UnsolicitedDataPolicy : noncopyable
52{
53public:
Junxiao Shi9db73592016-12-01 23:43:11 +000054 virtual
55 ~UnsolicitedDataPolicy() = default;
Junxiao Shifbe8efe2016-08-22 16:02:30 +000056
57 virtual UnsolicitedDataDecision
58 decide(const Face& inFace, const Data& data) const = 0;
Junxiao Shi58320f72016-09-04 04:40:34 +000059
60public: // registry
61 template<typename P>
62 static void
Davide Pesaventoae430302023-05-11 01:42:46 -040063 registerPolicy(std::string_view policyName = P::POLICY_NAME)
Junxiao Shi58320f72016-09-04 04:40:34 +000064 {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040065 BOOST_ASSERT(!policyName.empty());
Davide Pesaventoae430302023-05-11 01:42:46 -040066 auto r = getRegistry().insert_or_assign(std::string(policyName), [] { return make_unique<P>(); });
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040067 BOOST_VERIFY(r.second);
Junxiao Shi58320f72016-09-04 04:40:34 +000068 }
69
Junxiao Shi9db73592016-12-01 23:43:11 +000070 /** \return an UnsolicitedDataPolicy identified by \p policyName,
71 * or nullptr if \p policyName is unknown
Junxiao Shi58320f72016-09-04 04:40:34 +000072 */
73 static unique_ptr<UnsolicitedDataPolicy>
Junxiao Shi9db73592016-12-01 23:43:11 +000074 create(const std::string& policyName);
75
76 /** \return a list of available policy names
77 */
78 static std::set<std::string>
79 getPolicyNames();
Junxiao Shi58320f72016-09-04 04:40:34 +000080
81private:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040082 using CreateFunc = std::function<unique_ptr<UnsolicitedDataPolicy>()>;
83 using Registry = std::map<std::string, CreateFunc>; // indexed by policy name
Junxiao Shic29c0b02016-12-04 21:11:44 +000084
Junxiao Shi58320f72016-09-04 04:40:34 +000085 static Registry&
86 getRegistry();
Junxiao Shifbe8efe2016-08-22 16:02:30 +000087};
88
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040089/**
90 * \brief Drops all unsolicited Data
Junxiao Shi36b22252016-08-25 13:22:21 +000091 */
Davide Pesavento3db98072021-03-09 23:03:27 -050092class DropAllUnsolicitedDataPolicy final : public UnsolicitedDataPolicy
Junxiao Shi36b22252016-08-25 13:22:21 +000093{
94public:
Junxiao Shi9db73592016-12-01 23:43:11 +000095 UnsolicitedDataDecision
Junxiao Shi36b22252016-08-25 13:22:21 +000096 decide(const Face& inFace, const Data& data) const final;
Junxiao Shi9db73592016-12-01 23:43:11 +000097
98public:
Davide Pesaventoae430302023-05-11 01:42:46 -040099 static constexpr std::string_view POLICY_NAME{"drop-all"};
Junxiao Shi36b22252016-08-25 13:22:21 +0000100};
101
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400102/**
103 * \brief Admits unsolicited Data from local faces
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000104 */
Davide Pesavento3db98072021-03-09 23:03:27 -0500105class AdmitLocalUnsolicitedDataPolicy final : public UnsolicitedDataPolicy
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000106{
107public:
Junxiao Shi9db73592016-12-01 23:43:11 +0000108 UnsolicitedDataDecision
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000109 decide(const Face& inFace, const Data& data) const final;
Junxiao Shi9db73592016-12-01 23:43:11 +0000110
111public:
Davide Pesaventoae430302023-05-11 01:42:46 -0400112 static constexpr std::string_view POLICY_NAME{"admit-local"};
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000113};
114
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400115/**
116 * \brief Admits unsolicited Data from non-local faces
Junxiao Shi36b22252016-08-25 13:22:21 +0000117 */
Davide Pesavento3db98072021-03-09 23:03:27 -0500118class AdmitNetworkUnsolicitedDataPolicy final : public UnsolicitedDataPolicy
Junxiao Shi36b22252016-08-25 13:22:21 +0000119{
120public:
Junxiao Shi9db73592016-12-01 23:43:11 +0000121 UnsolicitedDataDecision
Junxiao Shi36b22252016-08-25 13:22:21 +0000122 decide(const Face& inFace, const Data& data) const final;
Junxiao Shi9db73592016-12-01 23:43:11 +0000123
124public:
Davide Pesaventoae430302023-05-11 01:42:46 -0400125 static constexpr std::string_view POLICY_NAME{"admit-network"};
Junxiao Shi36b22252016-08-25 13:22:21 +0000126};
127
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400128/**
129 * \brief Admits all unsolicited Data
Junxiao Shi36b22252016-08-25 13:22:21 +0000130 */
Davide Pesavento3db98072021-03-09 23:03:27 -0500131class AdmitAllUnsolicitedDataPolicy final : public UnsolicitedDataPolicy
Junxiao Shi36b22252016-08-25 13:22:21 +0000132{
133public:
Junxiao Shi9db73592016-12-01 23:43:11 +0000134 UnsolicitedDataDecision
Junxiao Shi36b22252016-08-25 13:22:21 +0000135 decide(const Face& inFace, const Data& data) const final;
Junxiao Shi9db73592016-12-01 23:43:11 +0000136
137public:
Davide Pesaventoae430302023-05-11 01:42:46 -0400138 static constexpr std::string_view POLICY_NAME{"admit-all"};
Junxiao Shi36b22252016-08-25 13:22:21 +0000139};
140
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400141/**
142 * \brief The default UnsolicitedDataPolicy
Junxiao Shi9685cc52016-08-29 12:47:05 +0000143 */
Davide Pesavento3db98072021-03-09 23:03:27 -0500144using DefaultUnsolicitedDataPolicy = DropAllUnsolicitedDataPolicy;
Junxiao Shi9685cc52016-08-29 12:47:05 +0000145
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400146} // namespace nfd::fw
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000147
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400148/**
Davide Pesaventoae430302023-05-11 01:42:46 -0400149 * \brief Registers an unsolicited data policy.
150 * \param P A subclass of nfd::fw::UnsolicitedDataPolicy. \p P must have a static const data
151 * member `POLICY_NAME` convertible to std::string_view that contains the policy name.
Junxiao Shi58320f72016-09-04 04:40:34 +0000152 */
Junxiao Shi9db73592016-12-01 23:43:11 +0000153#define NFD_REGISTER_UNSOLICITED_DATA_POLICY(P) \
Junxiao Shi58320f72016-09-04 04:40:34 +0000154static class NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass \
155{ \
156public: \
157 NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass() \
158 { \
Junxiao Shi9db73592016-12-01 23:43:11 +0000159 ::nfd::fw::UnsolicitedDataPolicy::registerPolicy<P>(); \
Junxiao Shi58320f72016-09-04 04:40:34 +0000160 } \
161} g_nfdAuto ## P ## UnsolicitedDataPolicyRegistrationVariable
162
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000163#endif // NFD_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP