Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 4064127 | 2023-03-16 13:31:12 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [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. |
| 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 "cs-module.hpp" |
| 27 | #include "format-helpers.hpp" |
| 28 | |
Davide Pesavento | 4064127 | 2023-03-16 13:31:12 -0400 | [diff] [blame] | 29 | #include <ndn-cxx/mgmt/nfd/status-dataset.hpp> |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 30 | #include <ndn-cxx/util/indented-stream.hpp> |
| 31 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 32 | namespace nfd::tools::nfdc { |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 33 | |
| 34 | void |
Junxiao Shi | cdf7845 | 2018-03-02 23:14:15 +0000 | [diff] [blame] | 35 | CsModule::registerCommands(CommandParser& parser) |
| 36 | { |
| 37 | CommandDefinition defCsConfig("cs", "config"); |
| 38 | defCsConfig |
| 39 | .setTitle("change CS configuration") |
| 40 | .addArg("capacity", ArgValueType::UNSIGNED, Required::NO, Positional::NO) |
| 41 | .addArg("admit", ArgValueType::BOOLEAN, Required::NO, Positional::NO) |
| 42 | .addArg("serve", ArgValueType::BOOLEAN, Required::NO, Positional::NO); |
| 43 | parser.addCommand(defCsConfig, &CsModule::config); |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 44 | |
| 45 | CommandDefinition defCsErase("cs", "erase"); |
| 46 | defCsErase |
| 47 | .setTitle("erase cached Data") |
| 48 | .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES) |
| 49 | .addArg("count", ArgValueType::UNSIGNED, Required::NO, Positional::NO); |
| 50 | parser.addCommand(defCsErase, &CsModule::erase); |
Junxiao Shi | cdf7845 | 2018-03-02 23:14:15 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void |
| 54 | CsModule::config(ExecuteContext& ctx) |
| 55 | { |
| 56 | using boost::logic::indeterminate; |
| 57 | |
| 58 | auto capacity = ctx.args.getOptional<uint64_t>("capacity"); |
| 59 | auto enableAdmit = ctx.args.getTribool("admit"); |
| 60 | auto enableServe = ctx.args.getTribool("serve"); |
| 61 | |
| 62 | ControlParameters p; |
| 63 | if (capacity) { |
| 64 | p.setCapacity(*capacity); |
| 65 | } |
| 66 | if (!indeterminate(enableAdmit)) { |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 67 | p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT, bool(enableAdmit)); |
Junxiao Shi | cdf7845 | 2018-03-02 23:14:15 +0000 | [diff] [blame] | 68 | } |
| 69 | if (!indeterminate(enableServe)) { |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 70 | p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE, bool(enableServe)); |
Junxiao Shi | cdf7845 | 2018-03-02 23:14:15 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | ctx.controller.start<ndn::nfd::CsConfigCommand>(p, |
| 74 | [&] (const ControlParameters& resp) { |
| 75 | text::ItemAttributes ia; |
| 76 | ctx.out << "cs-config-updated " |
| 77 | << ia("capacity") << resp.getCapacity() |
| 78 | << ia("admit") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT)} |
| 79 | << ia("serve") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE)} |
| 80 | << '\n'; |
| 81 | }, |
| 82 | ctx.makeCommandFailureHandler("updating CS config"), |
| 83 | ctx.makeCommandOptions()); |
| 84 | |
| 85 | ctx.face.processEvents(); |
| 86 | } |
| 87 | |
| 88 | void |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 89 | CsModule::erase(ExecuteContext& ctx) |
| 90 | { |
| 91 | auto prefix = ctx.args.get<Name>("prefix"); |
| 92 | auto count = ctx.args.getOptional<uint64_t>("count"); |
| 93 | |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 94 | uint64_t numErased = 0; |
| 95 | bool wasLimited = false; |
| 96 | bool wasSuccessful = true; |
| 97 | |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 98 | ControlParameters params; |
| 99 | params.setName(prefix); |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 100 | |
| 101 | // The cs/erase command can have a limit on the number of CS entries erased in a single operation. |
| 102 | // Therefore, we may need to run cs/erase multiple times to achieve the desired number of erases. |
| 103 | do { |
| 104 | if (count) { |
| 105 | params.setCount(*count - numErased); |
| 106 | } |
| 107 | |
| 108 | wasSuccessful = false; |
| 109 | |
| 110 | ctx.controller.start<ndn::nfd::CsEraseCommand>( |
| 111 | params, |
| 112 | [&] (const ControlParameters& resp) { |
| 113 | wasSuccessful = true; |
| 114 | numErased += resp.getCount(); |
| 115 | wasLimited = resp.hasCapacity(); |
| 116 | }, |
| 117 | ctx.makeCommandFailureHandler("erasing cached Data"), |
| 118 | ctx.makeCommandOptions()); |
| 119 | |
| 120 | ctx.face.processEvents(); |
| 121 | } while (wasSuccessful && wasLimited); |
| 122 | |
| 123 | if (wasSuccessful) { |
| 124 | text::ItemAttributes ia; |
| 125 | ctx.out << "cs-erased " |
| 126 | << ia("prefix") << prefix |
| 127 | << ia("count") << numErased |
| 128 | << '\n'; |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 129 | } |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void |
Davide Pesavento | 63b3ae8 | 2023-03-24 23:53:24 -0400 | [diff] [blame] | 133 | CsModule::fetchStatus(ndn::nfd::Controller& controller, |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 134 | const std::function<void()>& onSuccess, |
Davide Pesavento | 63b3ae8 | 2023-03-24 23:53:24 -0400 | [diff] [blame] | 135 | const ndn::nfd::DatasetFailureCallback& onFailure, |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 136 | const CommandOptions& options) |
| 137 | { |
| 138 | controller.fetch<ndn::nfd::CsInfoDataset>( |
| 139 | [this, onSuccess] (const CsInfo& result) { |
| 140 | m_status = result; |
| 141 | onSuccess(); |
| 142 | }, |
| 143 | onFailure, options); |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | CsModule::formatStatusXml(std::ostream& os) const |
| 148 | { |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 149 | formatItemXml(os, m_status); |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | CsModule::formatItemXml(std::ostream& os, const CsInfo& item) |
| 154 | { |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 155 | os << "<cs>"; |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 156 | os << "<capacity>" << item.getCapacity() << "</capacity>"; |
| 157 | os << xml::Flag{"admitEnabled", item.getEnableAdmit()}; |
| 158 | os << xml::Flag{"serveEnabled", item.getEnableServe()}; |
| 159 | os << "<nEntries>" << item.getNEntries() << "</nEntries>"; |
| 160 | os << "<nHits>" << item.getNHits() << "</nHits>"; |
| 161 | os << "<nMisses>" << item.getNMisses() << "</nMisses>"; |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 162 | os << "</cs>"; |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | CsModule::formatStatusText(std::ostream& os) const |
| 167 | { |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 168 | os << "CS information:\n"; |
| 169 | ndn::util::IndentedStream indented(os, " "); |
| 170 | formatItemText(indented, m_status); |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | CsModule::formatItemText(std::ostream& os, const CsInfo& item) |
| 175 | { |
| 176 | text::ItemAttributes ia(true, 8); |
| 177 | os << ia("capacity") << item.getCapacity() |
| 178 | << ia("admit") << text::OnOff{item.getEnableAdmit()} |
| 179 | << ia("serve") << text::OnOff{item.getEnableServe()} |
| 180 | << ia("nEntries") << item.getNEntries() |
| 181 | << ia("nHits") << item.getNHits() |
| 182 | << ia("nMisses") << item.getNMisses() |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 183 | << ia.end(); |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 186 | } // namespace nfd::tools::nfdc |