blob: 3be976f7ad262affd32a173e09f6b840b80c82b9 [file] [log] [blame]
Junxiao Shif2bfb442018-01-05 12:34:57 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento45c1f6a2025-01-01 19:30:30 -05003 * Copyright (c) 2014-2025, Regents of the University of California,
Junxiao Shif2bfb442018-01-05 12:34:57 +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-manager.hpp"
Davide Pesavento78ddcab2019-02-28 22:00:03 -050027#include "fw/forwarder-counters.hpp"
28#include "table/cs.hpp"
29
Junxiao Shif2bfb442018-01-05 12:34:57 +000030#include <ndn-cxx/mgmt/nfd/cs-info.hpp>
31
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050032#include <limits>
33
Junxiao Shif2bfb442018-01-05 12:34:57 +000034namespace nfd {
35
Davide Pesavento78ddcab2019-02-28 22:00:03 -050036CsManager::CsManager(Cs& cs, const ForwarderCounters& fwCounters,
Junxiao Shif2bfb442018-01-05 12:34:57 +000037 Dispatcher& dispatcher, CommandAuthenticator& authenticator)
Davide Pesavento78ddcab2019-02-28 22:00:03 -050038 : ManagerBase("cs", dispatcher, authenticator)
Junxiao Shic9b5e012018-02-07 15:04:18 +000039 , m_cs(cs)
Davide Pesavento78ddcab2019-02-28 22:00:03 -050040 , m_fwCounters(fwCounters)
Junxiao Shif2bfb442018-01-05 12:34:57 +000041{
Davide Pesavento1db1bb62025-01-06 01:23:41 -050042 registerCommandHandler<ndn::nfd::CsConfigCommand>([this] (auto&&, auto&&, auto&&... args) {
43 changeConfig(std::forward<decltype(args)>(args)...);
44 });
45 registerCommandHandler<ndn::nfd::CsEraseCommand>([this] (auto&&, auto&&, auto&&... args) {
46 erase(std::forward<decltype(args)>(args)...);
47 });
48 registerStatusDatasetHandler("info", [this] (auto&&, auto&&, auto&&... args) {
49 serveInfo(std::forward<decltype(args)>(args)...);
50 });
Junxiao Shif2bfb442018-01-05 12:34:57 +000051}
52
53void
Junxiao Shic9b5e012018-02-07 15:04:18 +000054CsManager::changeConfig(const ControlParameters& parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -050055 const CommandContinuation& done)
Junxiao Shic9b5e012018-02-07 15:04:18 +000056{
57 using ndn::nfd::CsFlagBit;
58
59 if (parameters.hasCapacity()) {
60 m_cs.setLimit(parameters.getCapacity());
61 }
62
63 if (parameters.hasFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT)) {
64 m_cs.enableAdmit(parameters.getFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT));
65 }
66
67 if (parameters.hasFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE)) {
68 m_cs.enableServe(parameters.getFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE));
69 }
70
71 ControlParameters body;
72 body.setCapacity(m_cs.getLimit());
73 body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT, m_cs.shouldAdmit(), false);
74 body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE, m_cs.shouldServe(), false);
75 done(ControlResponse(200, "OK").setBody(body.wireEncode()));
76}
77
78void
Junxiao Shibd724312018-04-17 02:23:48 +000079CsManager::erase(const ControlParameters& parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -050080 const CommandContinuation& done)
Junxiao Shibd724312018-04-17 02:23:48 +000081{
82 size_t count = parameters.hasCount() ?
83 parameters.getCount() :
84 std::numeric_limits<size_t>::max();
85 m_cs.erase(parameters.getName(), std::min(count, ERASE_LIMIT),
86 [=] (size_t nErased) {
87 ControlParameters body;
88 body.setName(parameters.getName());
89 body.setCount(nErased);
90 if (nErased == ERASE_LIMIT && count > ERASE_LIMIT) {
91 m_cs.find(Interest(parameters.getName()).setCanBePrefix(true),
92 [=] (const Interest&, const Data&) mutable {
93 body.setCapacity(ERASE_LIMIT);
94 done(ControlResponse(200, "OK").setBody(body.wireEncode()));
95 },
96 [=] (const Interest&) {
97 done(ControlResponse(200, "OK").setBody(body.wireEncode()));
98 });
99 }
100 else {
101 done(ControlResponse(200, "OK").setBody(body.wireEncode()));
102 }
103 });
104}
105
106void
Davide Pesaventoae430302023-05-11 01:42:46 -0400107CsManager::serveInfo(ndn::mgmt::StatusDatasetContext& context) const
Junxiao Shif2bfb442018-01-05 12:34:57 +0000108{
109 ndn::nfd::CsInfo info;
Junxiao Shi26667e12018-02-08 20:01:18 +0000110 info.setCapacity(m_cs.getLimit());
111 info.setEnableAdmit(m_cs.shouldAdmit());
112 info.setEnableServe(m_cs.shouldServe());
113 info.setNEntries(m_cs.size());
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500114 info.setNHits(m_fwCounters.nCsHits);
115 info.setNMisses(m_fwCounters.nCsMisses);
Junxiao Shif2bfb442018-01-05 12:34:57 +0000116
117 context.append(info.wireEncode());
118 context.end();
119}
120
121} // namespace nfd