blob: fe113b5b2674844590d7b1e3d01084e8060e56c2 [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 Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, 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 Pesavento2c9d2ca2024-01-27 16:36:51 -050031#include <functional>
32#include <map>
33#include <set>
34
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::fw {
Junxiao Shifbe8efe2016-08-22 16:02:30 +000036
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040037/**
38 * \brief Decision made by UnsolicitedDataPolicy
Junxiao Shifbe8efe2016-08-22 16:02:30 +000039 */
40enum class UnsolicitedDataDecision {
41 DROP, ///< the Data should be dropped
42 CACHE ///< the Data should be cached in the ContentStore
43};
44
45std::ostream&
46operator<<(std::ostream& os, UnsolicitedDataDecision d);
47
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040048/**
49 * \brief Determines how to process an unsolicited Data packet
Junxiao Shifbe8efe2016-08-22 16:02:30 +000050 *
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040051 * 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 Shifbe8efe2016-08-22 16:02:30 +000054 */
55class UnsolicitedDataPolicy : noncopyable
56{
57public:
Junxiao Shi9db73592016-12-01 23:43:11 +000058 virtual
59 ~UnsolicitedDataPolicy() = default;
Junxiao Shifbe8efe2016-08-22 16:02:30 +000060
61 virtual UnsolicitedDataDecision
62 decide(const Face& inFace, const Data& data) const = 0;
Junxiao Shi58320f72016-09-04 04:40:34 +000063
64public: // registry
65 template<typename P>
66 static void
Davide Pesaventoae430302023-05-11 01:42:46 -040067 registerPolicy(std::string_view policyName = P::POLICY_NAME)
Junxiao Shi58320f72016-09-04 04:40:34 +000068 {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040069 BOOST_ASSERT(!policyName.empty());
Davide Pesaventoae430302023-05-11 01:42:46 -040070 auto r = getRegistry().insert_or_assign(std::string(policyName), [] { return make_unique<P>(); });
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040071 BOOST_VERIFY(r.second);
Junxiao Shi58320f72016-09-04 04:40:34 +000072 }
73
Junxiao Shi9db73592016-12-01 23:43:11 +000074 /** \return an UnsolicitedDataPolicy identified by \p policyName,
75 * or nullptr if \p policyName is unknown
Junxiao Shi58320f72016-09-04 04:40:34 +000076 */
77 static unique_ptr<UnsolicitedDataPolicy>
Junxiao Shi9db73592016-12-01 23:43:11 +000078 create(const std::string& policyName);
79
80 /** \return a list of available policy names
81 */
82 static std::set<std::string>
83 getPolicyNames();
Junxiao Shi58320f72016-09-04 04:40:34 +000084
85private:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040086 using CreateFunc = std::function<unique_ptr<UnsolicitedDataPolicy>()>;
87 using Registry = std::map<std::string, CreateFunc>; // indexed by policy name
Junxiao Shic29c0b02016-12-04 21:11:44 +000088
Junxiao Shi58320f72016-09-04 04:40:34 +000089 static Registry&
90 getRegistry();
Junxiao Shifbe8efe2016-08-22 16:02:30 +000091};
92
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040093/**
94 * \brief Drops all unsolicited Data
Junxiao Shi36b22252016-08-25 13:22:21 +000095 */
Davide Pesavento3db98072021-03-09 23:03:27 -050096class DropAllUnsolicitedDataPolicy final : public UnsolicitedDataPolicy
Junxiao Shi36b22252016-08-25 13:22:21 +000097{
98public:
Junxiao Shi9db73592016-12-01 23:43:11 +000099 UnsolicitedDataDecision
Junxiao Shi36b22252016-08-25 13:22:21 +0000100 decide(const Face& inFace, const Data& data) const final;
Junxiao Shi9db73592016-12-01 23:43:11 +0000101
102public:
Davide Pesaventoae430302023-05-11 01:42:46 -0400103 static constexpr std::string_view POLICY_NAME{"drop-all"};
Junxiao Shi36b22252016-08-25 13:22:21 +0000104};
105
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400106/**
107 * \brief Admits unsolicited Data from local faces
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000108 */
Davide Pesavento3db98072021-03-09 23:03:27 -0500109class AdmitLocalUnsolicitedDataPolicy final : public UnsolicitedDataPolicy
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000110{
111public:
Junxiao Shi9db73592016-12-01 23:43:11 +0000112 UnsolicitedDataDecision
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000113 decide(const Face& inFace, const Data& data) const final;
Junxiao Shi9db73592016-12-01 23:43:11 +0000114
115public:
Davide Pesaventoae430302023-05-11 01:42:46 -0400116 static constexpr std::string_view POLICY_NAME{"admit-local"};
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000117};
118
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400119/**
120 * \brief Admits unsolicited Data from non-local faces
Junxiao Shi36b22252016-08-25 13:22:21 +0000121 */
Davide Pesavento3db98072021-03-09 23:03:27 -0500122class AdmitNetworkUnsolicitedDataPolicy final : public UnsolicitedDataPolicy
Junxiao Shi36b22252016-08-25 13:22:21 +0000123{
124public:
Junxiao Shi9db73592016-12-01 23:43:11 +0000125 UnsolicitedDataDecision
Junxiao Shi36b22252016-08-25 13:22:21 +0000126 decide(const Face& inFace, const Data& data) const final;
Junxiao Shi9db73592016-12-01 23:43:11 +0000127
128public:
Davide Pesaventoae430302023-05-11 01:42:46 -0400129 static constexpr std::string_view POLICY_NAME{"admit-network"};
Junxiao Shi36b22252016-08-25 13:22:21 +0000130};
131
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400132/**
133 * \brief Admits all unsolicited Data
Junxiao Shi36b22252016-08-25 13:22:21 +0000134 */
Davide Pesavento3db98072021-03-09 23:03:27 -0500135class AdmitAllUnsolicitedDataPolicy final : public UnsolicitedDataPolicy
Junxiao Shi36b22252016-08-25 13:22:21 +0000136{
137public:
Junxiao Shi9db73592016-12-01 23:43:11 +0000138 UnsolicitedDataDecision
Junxiao Shi36b22252016-08-25 13:22:21 +0000139 decide(const Face& inFace, const Data& data) const final;
Junxiao Shi9db73592016-12-01 23:43:11 +0000140
141public:
Davide Pesaventoae430302023-05-11 01:42:46 -0400142 static constexpr std::string_view POLICY_NAME{"admit-all"};
Junxiao Shi36b22252016-08-25 13:22:21 +0000143};
144
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400145/**
146 * \brief The default UnsolicitedDataPolicy
Junxiao Shi9685cc52016-08-29 12:47:05 +0000147 */
Davide Pesavento3db98072021-03-09 23:03:27 -0500148using DefaultUnsolicitedDataPolicy = DropAllUnsolicitedDataPolicy;
Junxiao Shi9685cc52016-08-29 12:47:05 +0000149
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400150} // namespace nfd::fw
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000151
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400152/**
Davide Pesaventoae430302023-05-11 01:42:46 -0400153 * \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 Shi58320f72016-09-04 04:40:34 +0000156 */
Junxiao Shi9db73592016-12-01 23:43:11 +0000157#define NFD_REGISTER_UNSOLICITED_DATA_POLICY(P) \
Junxiao Shi58320f72016-09-04 04:40:34 +0000158static class NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass \
159{ \
160public: \
161 NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass() \
162 { \
Junxiao Shi9db73592016-12-01 23:43:11 +0000163 ::nfd::fw::UnsolicitedDataPolicy::registerPolicy<P>(); \
Junxiao Shi58320f72016-09-04 04:40:34 +0000164 } \
165} g_nfdAuto ## P ## UnsolicitedDataPolicyRegistrationVariable
166
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000167#endif // NFD_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP