blob: 1fde3559144948767b920e3e96fee08e286f295e [file] [log] [blame]
Junxiao Shi3160a3f2018-01-09 21:25:15 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento40641272023-03-16 13:31:12 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Junxiao Shi3160a3f2018-01-09 21:25:15 +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#include "cs-module.hpp"
27#include "format-helpers.hpp"
28
Davide Pesavento40641272023-03-16 13:31:12 -040029#include <ndn-cxx/mgmt/nfd/status-dataset.hpp>
Junxiao Shi7a36ac72018-03-21 15:23:22 +000030#include <ndn-cxx/util/indented-stream.hpp>
31
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::tools::nfdc {
Junxiao Shi3160a3f2018-01-09 21:25:15 +000033
34void
Junxiao Shicdf78452018-03-02 23:14:15 +000035CsModule::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 Shia4d7fe02018-07-20 06:51:41 -060044
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 Shicdf78452018-03-02 23:14:15 +000051}
52
53void
54CsModule::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 Pesaventofa2aa502019-03-22 13:30:02 -040067 p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT, bool(enableAdmit));
Junxiao Shicdf78452018-03-02 23:14:15 +000068 }
69 if (!indeterminate(enableServe)) {
Davide Pesaventofa2aa502019-03-22 13:30:02 -040070 p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE, bool(enableServe));
Junxiao Shicdf78452018-03-02 23:14:15 +000071 }
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
88void
Junxiao Shia4d7fe02018-07-20 06:51:41 -060089CsModule::erase(ExecuteContext& ctx)
90{
91 auto prefix = ctx.args.get<Name>("prefix");
92 auto count = ctx.args.getOptional<uint64_t>("count");
93
Eric Newberry4995bf92020-04-18 16:59:19 -070094 uint64_t numErased = 0;
95 bool wasLimited = false;
96 bool wasSuccessful = true;
97
Junxiao Shia4d7fe02018-07-20 06:51:41 -060098 ControlParameters params;
99 params.setName(prefix);
Eric Newberry4995bf92020-04-18 16:59:19 -0700100
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 Shia4d7fe02018-07-20 06:51:41 -0600129 }
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600130}
131
132void
Davide Pesavento63b3ae82023-03-24 23:53:24 -0400133CsModule::fetchStatus(ndn::nfd::Controller& controller,
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400134 const std::function<void()>& onSuccess,
Davide Pesavento63b3ae82023-03-24 23:53:24 -0400135 const ndn::nfd::DatasetFailureCallback& onFailure,
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000136 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
146void
147CsModule::formatStatusXml(std::ostream& os) const
148{
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000149 formatItemXml(os, m_status);
150}
151
152void
153CsModule::formatItemXml(std::ostream& os, const CsInfo& item)
154{
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000155 os << "<cs>";
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000156 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 Shi3160a3f2018-01-09 21:25:15 +0000162 os << "</cs>";
163}
164
165void
166CsModule::formatStatusText(std::ostream& os) const
167{
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000168 os << "CS information:\n";
169 ndn::util::IndentedStream indented(os, " ");
170 formatItemText(indented, m_status);
171}
172
173void
174CsModule::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 Shi3160a3f2018-01-09 21:25:15 +0000183 << ia.end();
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000184}
185
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400186} // namespace nfd::tools::nfdc