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