Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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" |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 27 | |
| 28 | namespace nfd { |
| 29 | |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 30 | const size_t TablesConfigSection::DEFAULT_CS_MAX_PACKETS = 65536; |
| 31 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 32 | TablesConfigSection::TablesConfigSection(Forwarder& forwarder) |
| 33 | : m_forwarder(forwarder) |
| 34 | , m_isConfigured(false) |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 35 | { |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 39 | TablesConfigSection::setConfigFile(ConfigFile& configFile) |
| 40 | { |
| 41 | configFile.addSectionHandler("tables", |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 42 | bind(&TablesConfigSection::processConfig, this, _1, _2)); |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 43 | } |
| 44 | |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 45 | void |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 46 | TablesConfigSection::ensureConfigured() |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 47 | { |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 48 | if (m_isConfigured) { |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 49 | return; |
| 50 | } |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 51 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 52 | m_forwarder.getCs().setLimit(DEFAULT_CS_MAX_PACKETS); |
Steve DiBenedetto | fbea590 | 2014-07-08 15:29:12 -0600 | [diff] [blame] | 53 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 54 | m_isConfigured = true; |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 58 | TablesConfigSection::processConfig(const ConfigSection& section, bool isDryRun) |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 59 | { |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 60 | typedef boost::optional<const ConfigSection&> OptionalNode; |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 61 | |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 62 | size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS; |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 63 | OptionalNode csMaxPacketsNode = section.get_child_optional("cs_max_packets"); |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 64 | if (csMaxPacketsNode) { |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 65 | nCsMaxPackets = ConfigFile::parseNumber<size_t>(*csMaxPacketsNode, "cs_max_packets", "tables"); |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 68 | OptionalNode strategyChoiceSection = section.get_child_optional("strategy_choice"); |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 69 | if (strategyChoiceSection) { |
| 70 | processStrategyChoiceSection(*strategyChoiceSection, isDryRun); |
| 71 | } |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 72 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 73 | OptionalNode networkRegionSection = section.get_child_optional("network_region"); |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 74 | if (networkRegionSection) { |
| 75 | processNetworkRegionSection(*networkRegionSection, isDryRun); |
| 76 | } |
| 77 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 78 | if (isDryRun) { |
| 79 | return; |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 80 | } |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 81 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 82 | m_forwarder.getCs().setLimit(nCsMaxPackets); |
| 83 | |
| 84 | m_isConfigured = true; |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 85 | } |
| 86 | |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 87 | void |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 88 | TablesConfigSection::processStrategyChoiceSection(const ConfigSection& section, bool isDryRun) |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 89 | { |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 90 | StrategyChoice& sc = m_forwarder.getStrategyChoice(); |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 91 | |
| 92 | std::map<Name, Name> choices; |
| 93 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 94 | for (const auto& prefixAndStrategy : section) { |
| 95 | Name prefix(prefixAndStrategy.first); |
| 96 | Name strategy(prefixAndStrategy.second.get_value<std::string>()); |
| 97 | |
| 98 | if (!sc.hasStrategy(strategy)) { |
| 99 | BOOST_THROW_EXCEPTION(ConfigFile::Error( |
| 100 | "Unknown strategy \"" + prefixAndStrategy.second.get_value<std::string>() + |
| 101 | "\" for prefix \"" + prefix.toUri() + "\" in \"strategy_choice\" section")); |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 104 | if (!choices.emplace(prefix, strategy).second) { |
| 105 | BOOST_THROW_EXCEPTION(ConfigFile::Error( |
| 106 | "Duplicate strategy choice for prefix \"" + prefix.toUri() + |
| 107 | "\" in \"strategy_choice\" section")); |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 108 | } |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 111 | if (isDryRun) { |
| 112 | return; |
| 113 | } |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 114 | |
| 115 | for (const auto& prefixAndStrategy : choices) { |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 116 | if (!sc.insert(prefixAndStrategy.first, prefixAndStrategy.second)) { |
| 117 | BOOST_THROW_EXCEPTION(ConfigFile::Error( |
| 118 | "Failed to set strategy \"" + prefixAndStrategy.second.toUri() + "\" for " |
| 119 | "prefix \"" + prefixAndStrategy.first.toUri() + "\" in \"strategy_choicev\"")); |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 120 | } |
| 121 | } |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 124 | void |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 125 | TablesConfigSection::processNetworkRegionSection(const ConfigSection& section, bool isDryRun) |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 126 | { |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 127 | if (isDryRun) { |
| 128 | return; |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 129 | } |
| 130 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 131 | NetworkRegionTable& nrt = m_forwarder.getNetworkRegionTable(); |
| 132 | nrt.clear(); |
| 133 | for (const auto& pair : section) { |
| 134 | Name region(pair.first); |
| 135 | nrt.insert(region); |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 136 | } |
| 137 | } |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 138 | |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 139 | } // namespace nfd |