blob: eaf2cdaf1300398470d70414fe161ce5a843e827 [file] [log] [blame]
Junxiao Shif2bfb442018-01-05 12:34:57 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento412c9822021-07-02 00:21:05 -04003 * Copyright (c) 2014-2021, 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
32namespace nfd {
33
Junxiao Shibd724312018-04-17 02:23:48 +000034constexpr size_t CsManager::ERASE_LIMIT;
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{
Junxiao Shic9b5e012018-02-07 15:04:18 +000042 registerCommandHandler<ndn::nfd::CsConfigCommand>("config",
Davide Pesavento412c9822021-07-02 00:21:05 -040043 std::bind(&CsManager::changeConfig, this, _4, _5));
Junxiao Shibd724312018-04-17 02:23:48 +000044 registerCommandHandler<ndn::nfd::CsEraseCommand>("erase",
Davide Pesavento412c9822021-07-02 00:21:05 -040045 std::bind(&CsManager::erase, this, _4, _5));
Junxiao Shic9b5e012018-02-07 15:04:18 +000046
Davide Pesavento412c9822021-07-02 00:21:05 -040047 registerStatusDatasetHandler("info", std::bind(&CsManager::serveInfo, this, _1, _2, _3));
Junxiao Shif2bfb442018-01-05 12:34:57 +000048}
49
50void
Junxiao Shic9b5e012018-02-07 15:04:18 +000051CsManager::changeConfig(const ControlParameters& parameters,
52 const ndn::mgmt::CommandContinuation& done)
53{
54 using ndn::nfd::CsFlagBit;
55
56 if (parameters.hasCapacity()) {
57 m_cs.setLimit(parameters.getCapacity());
58 }
59
60 if (parameters.hasFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT)) {
61 m_cs.enableAdmit(parameters.getFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT));
62 }
63
64 if (parameters.hasFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE)) {
65 m_cs.enableServe(parameters.getFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE));
66 }
67
68 ControlParameters body;
69 body.setCapacity(m_cs.getLimit());
70 body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT, m_cs.shouldAdmit(), false);
71 body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE, m_cs.shouldServe(), false);
72 done(ControlResponse(200, "OK").setBody(body.wireEncode()));
73}
74
75void
Junxiao Shibd724312018-04-17 02:23:48 +000076CsManager::erase(const ControlParameters& parameters,
77 const ndn::mgmt::CommandContinuation& done)
78{
79 size_t count = parameters.hasCount() ?
80 parameters.getCount() :
81 std::numeric_limits<size_t>::max();
82 m_cs.erase(parameters.getName(), std::min(count, ERASE_LIMIT),
83 [=] (size_t nErased) {
84 ControlParameters body;
85 body.setName(parameters.getName());
86 body.setCount(nErased);
87 if (nErased == ERASE_LIMIT && count > ERASE_LIMIT) {
88 m_cs.find(Interest(parameters.getName()).setCanBePrefix(true),
89 [=] (const Interest&, const Data&) mutable {
90 body.setCapacity(ERASE_LIMIT);
91 done(ControlResponse(200, "OK").setBody(body.wireEncode()));
92 },
93 [=] (const Interest&) {
94 done(ControlResponse(200, "OK").setBody(body.wireEncode()));
95 });
96 }
97 else {
98 done(ControlResponse(200, "OK").setBody(body.wireEncode()));
99 }
100 });
101}
102
103void
Junxiao Shif2bfb442018-01-05 12:34:57 +0000104CsManager::serveInfo(const Name& topPrefix, const Interest& interest,
105 ndn::mgmt::StatusDatasetContext& context) const
106{
107 ndn::nfd::CsInfo info;
Junxiao Shi26667e12018-02-08 20:01:18 +0000108 info.setCapacity(m_cs.getLimit());
109 info.setEnableAdmit(m_cs.shouldAdmit());
110 info.setEnableServe(m_cs.shouldServe());
111 info.setNEntries(m_cs.size());
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500112 info.setNHits(m_fwCounters.nCsHits);
113 info.setNMisses(m_fwCounters.nCsMisses);
Junxiao Shif2bfb442018-01-05 12:34:57 +0000114
115 context.append(info.wireEncode());
116 context.end();
117}
118
119} // namespace nfd