Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [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. |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 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 | #include "tables-config-section.hpp" |
Junxiao Shi | 593887c | 2016-12-24 02:39:29 +0000 | [diff] [blame] | 27 | #include "fw/strategy.hpp" |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 31 | const size_t TablesConfigSection::DEFAULT_CS_MAX_PACKETS = 65536; |
| 32 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 33 | TablesConfigSection::TablesConfigSection(Forwarder& forwarder) |
| 34 | : m_forwarder(forwarder) |
| 35 | , m_isConfigured(false) |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 36 | { |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 40 | TablesConfigSection::setConfigFile(ConfigFile& configFile) |
| 41 | { |
| 42 | configFile.addSectionHandler("tables", |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 43 | bind(&TablesConfigSection::processConfig, this, _1, _2)); |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 44 | } |
| 45 | |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 46 | void |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 47 | TablesConfigSection::ensureConfigured() |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 48 | { |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 49 | if (m_isConfigured) { |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 50 | return; |
| 51 | } |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 52 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 53 | m_forwarder.getCs().setLimit(DEFAULT_CS_MAX_PACKETS); |
Junxiao Shi | b4a5acd | 2016-12-07 19:59:18 +0000 | [diff] [blame] | 54 | // Don't set default cs_policy because it's already created by CS itself. |
Junxiao Shi | 9685cc5 | 2016-08-29 12:47:05 +0000 | [diff] [blame] | 55 | m_forwarder.setUnsolicitedDataPolicy(make_unique<fw::DefaultUnsolicitedDataPolicy>()); |
Steve DiBenedetto | fbea590 | 2014-07-08 15:29:12 -0600 | [diff] [blame] | 56 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 57 | m_isConfigured = true; |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 61 | TablesConfigSection::processConfig(const ConfigSection& section, bool isDryRun) |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 62 | { |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 63 | size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 64 | OptionalConfigSection csMaxPacketsNode = section.get_child_optional("cs_max_packets"); |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 65 | if (csMaxPacketsNode) { |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 66 | nCsMaxPackets = ConfigFile::parseNumber<size_t>(*csMaxPacketsNode, "cs_max_packets", "tables"); |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Junxiao Shi | b4a5acd | 2016-12-07 19:59:18 +0000 | [diff] [blame] | 69 | unique_ptr<cs::Policy> csPolicy; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 70 | OptionalConfigSection csPolicyNode = section.get_child_optional("cs_policy"); |
Junxiao Shi | b4a5acd | 2016-12-07 19:59:18 +0000 | [diff] [blame] | 71 | if (csPolicyNode) { |
| 72 | std::string policyName = csPolicyNode->get_value<std::string>(); |
| 73 | csPolicy = cs::Policy::create(policyName); |
| 74 | if (csPolicy == nullptr) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 75 | NDN_THROW(ConfigFile::Error("Unknown cs_policy '" + policyName + "' in section 'tables'")); |
Junxiao Shi | b4a5acd | 2016-12-07 19:59:18 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Junxiao Shi | 9685cc5 | 2016-08-29 12:47:05 +0000 | [diff] [blame] | 79 | unique_ptr<fw::UnsolicitedDataPolicy> unsolicitedDataPolicy; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 80 | OptionalConfigSection unsolicitedDataPolicyNode = section.get_child_optional("cs_unsolicited_policy"); |
Junxiao Shi | 9685cc5 | 2016-08-29 12:47:05 +0000 | [diff] [blame] | 81 | if (unsolicitedDataPolicyNode) { |
Junxiao Shi | b4a5acd | 2016-12-07 19:59:18 +0000 | [diff] [blame] | 82 | std::string policyName = unsolicitedDataPolicyNode->get_value<std::string>(); |
| 83 | unsolicitedDataPolicy = fw::UnsolicitedDataPolicy::create(policyName); |
Junxiao Shi | 9685cc5 | 2016-08-29 12:47:05 +0000 | [diff] [blame] | 84 | if (unsolicitedDataPolicy == nullptr) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 85 | NDN_THROW(ConfigFile::Error("Unknown cs_unsolicited_policy '" + policyName + "' in section 'tables'")); |
Junxiao Shi | 9685cc5 | 2016-08-29 12:47:05 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | else { |
| 89 | unsolicitedDataPolicy = make_unique<fw::DefaultUnsolicitedDataPolicy>(); |
| 90 | } |
| 91 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 92 | OptionalConfigSection strategyChoiceSection = section.get_child_optional("strategy_choice"); |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 93 | if (strategyChoiceSection) { |
| 94 | processStrategyChoiceSection(*strategyChoiceSection, isDryRun); |
| 95 | } |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 96 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 97 | OptionalConfigSection networkRegionSection = section.get_child_optional("network_region"); |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 98 | if (networkRegionSection) { |
| 99 | processNetworkRegionSection(*networkRegionSection, isDryRun); |
| 100 | } |
| 101 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 102 | if (isDryRun) { |
| 103 | return; |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 104 | } |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 105 | |
Junxiao Shi | b4a5acd | 2016-12-07 19:59:18 +0000 | [diff] [blame] | 106 | Cs& cs = m_forwarder.getCs(); |
| 107 | cs.setLimit(nCsMaxPackets); |
| 108 | if (cs.size() == 0 && csPolicy != nullptr) { |
| 109 | cs.setPolicy(std::move(csPolicy)); |
| 110 | } |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 111 | |
Junxiao Shi | 9685cc5 | 2016-08-29 12:47:05 +0000 | [diff] [blame] | 112 | m_forwarder.setUnsolicitedDataPolicy(std::move(unsolicitedDataPolicy)); |
| 113 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 114 | m_isConfigured = true; |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 115 | } |
| 116 | |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 117 | void |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 118 | TablesConfigSection::processStrategyChoiceSection(const ConfigSection& section, bool isDryRun) |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 119 | { |
Junxiao Shi | 593887c | 2016-12-24 02:39:29 +0000 | [diff] [blame] | 120 | using fw::Strategy; |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 121 | |
| 122 | std::map<Name, Name> choices; |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 123 | for (const auto& prefixAndStrategy : section) { |
| 124 | Name prefix(prefixAndStrategy.first); |
| 125 | Name strategy(prefixAndStrategy.second.get_value<std::string>()); |
| 126 | |
Junxiao Shi | 593887c | 2016-12-24 02:39:29 +0000 | [diff] [blame] | 127 | if (!Strategy::canCreate(strategy)) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 128 | NDN_THROW(ConfigFile::Error( |
| 129 | "Unknown strategy '" + prefixAndStrategy.second.get_value<std::string>() + |
| 130 | "' for prefix '" + prefix.toUri() + "' in section 'strategy_choice'")); |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 133 | if (!choices.emplace(prefix, strategy).second) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 134 | NDN_THROW(ConfigFile::Error( |
| 135 | "Duplicate strategy choice for prefix '" + prefix.toUri() + "' in section 'strategy_choice'")); |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 136 | } |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 137 | } |
| 138 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 139 | if (isDryRun) { |
| 140 | return; |
| 141 | } |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 142 | |
Junxiao Shi | 593887c | 2016-12-24 02:39:29 +0000 | [diff] [blame] | 143 | StrategyChoice& sc = m_forwarder.getStrategyChoice(); |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 144 | for (const auto& prefixAndStrategy : choices) { |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 145 | if (!sc.insert(prefixAndStrategy.first, prefixAndStrategy.second)) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 146 | NDN_THROW(ConfigFile::Error( |
| 147 | "Failed to set strategy '" + prefixAndStrategy.second.toUri() + "' for prefix '" + |
| 148 | prefixAndStrategy.first.toUri() + "' in section 'strategy_choice'")); |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 149 | } |
| 150 | } |
Junxiao Shi | 593887c | 2016-12-24 02:39:29 +0000 | [diff] [blame] | 151 | ///\todo redesign so that strategy parameter errors can be catched during dry-run |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 154 | void |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 155 | TablesConfigSection::processNetworkRegionSection(const ConfigSection& section, bool isDryRun) |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 156 | { |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 157 | if (isDryRun) { |
| 158 | return; |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 161 | auto& nrt = m_forwarder.getNetworkRegionTable(); |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 162 | nrt.clear(); |
| 163 | for (const auto& pair : section) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 164 | nrt.insert(Name(pair.first)); |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 165 | } |
| 166 | } |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 167 | |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 168 | } // namespace nfd |