Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +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-manager.hpp" |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 27 | #include "fw/forwarder-counters.hpp" |
| 28 | #include "table/cs.hpp" |
| 29 | |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 30 | #include <ndn-cxx/mgmt/nfd/cs-info.hpp> |
| 31 | |
| 32 | namespace nfd { |
| 33 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 34 | CsManager::CsManager(Cs& cs, const ForwarderCounters& fwCounters, |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 35 | Dispatcher& dispatcher, CommandAuthenticator& authenticator) |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 36 | : ManagerBase("cs", dispatcher, authenticator) |
Junxiao Shi | c9b5e01 | 2018-02-07 15:04:18 +0000 | [diff] [blame] | 37 | , m_cs(cs) |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 38 | , m_fwCounters(fwCounters) |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 39 | { |
Junxiao Shi | c9b5e01 | 2018-02-07 15:04:18 +0000 | [diff] [blame] | 40 | registerCommandHandler<ndn::nfd::CsConfigCommand>("config", |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 41 | [this] (auto&&, auto&&, auto&&, auto&&... args) { changeConfig(std::forward<decltype(args)>(args)...); }); |
Junxiao Shi | bd72431 | 2018-04-17 02:23:48 +0000 | [diff] [blame] | 42 | registerCommandHandler<ndn::nfd::CsEraseCommand>("erase", |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 43 | [this] (auto&&, auto&&, auto&&, auto&&... args) { erase(std::forward<decltype(args)>(args)...); }); |
| 44 | registerStatusDatasetHandler("info", |
| 45 | [this] (auto&&, auto&&, auto&&... args) { serveInfo(std::forward<decltype(args)>(args)...); }); |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void |
Junxiao Shi | c9b5e01 | 2018-02-07 15:04:18 +0000 | [diff] [blame] | 49 | CsManager::changeConfig(const ControlParameters& parameters, |
| 50 | const ndn::mgmt::CommandContinuation& done) |
| 51 | { |
| 52 | using ndn::nfd::CsFlagBit; |
| 53 | |
| 54 | if (parameters.hasCapacity()) { |
| 55 | m_cs.setLimit(parameters.getCapacity()); |
| 56 | } |
| 57 | |
| 58 | if (parameters.hasFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT)) { |
| 59 | m_cs.enableAdmit(parameters.getFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT)); |
| 60 | } |
| 61 | |
| 62 | if (parameters.hasFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE)) { |
| 63 | m_cs.enableServe(parameters.getFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE)); |
| 64 | } |
| 65 | |
| 66 | ControlParameters body; |
| 67 | body.setCapacity(m_cs.getLimit()); |
| 68 | body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT, m_cs.shouldAdmit(), false); |
| 69 | body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE, m_cs.shouldServe(), false); |
| 70 | done(ControlResponse(200, "OK").setBody(body.wireEncode())); |
| 71 | } |
| 72 | |
| 73 | void |
Junxiao Shi | bd72431 | 2018-04-17 02:23:48 +0000 | [diff] [blame] | 74 | CsManager::erase(const ControlParameters& parameters, |
| 75 | const ndn::mgmt::CommandContinuation& done) |
| 76 | { |
| 77 | size_t count = parameters.hasCount() ? |
| 78 | parameters.getCount() : |
| 79 | std::numeric_limits<size_t>::max(); |
| 80 | m_cs.erase(parameters.getName(), std::min(count, ERASE_LIMIT), |
| 81 | [=] (size_t nErased) { |
| 82 | ControlParameters body; |
| 83 | body.setName(parameters.getName()); |
| 84 | body.setCount(nErased); |
| 85 | if (nErased == ERASE_LIMIT && count > ERASE_LIMIT) { |
| 86 | m_cs.find(Interest(parameters.getName()).setCanBePrefix(true), |
| 87 | [=] (const Interest&, const Data&) mutable { |
| 88 | body.setCapacity(ERASE_LIMIT); |
| 89 | done(ControlResponse(200, "OK").setBody(body.wireEncode())); |
| 90 | }, |
| 91 | [=] (const Interest&) { |
| 92 | done(ControlResponse(200, "OK").setBody(body.wireEncode())); |
| 93 | }); |
| 94 | } |
| 95 | else { |
| 96 | done(ControlResponse(200, "OK").setBody(body.wireEncode())); |
| 97 | } |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | void |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 102 | CsManager::serveInfo(ndn::mgmt::StatusDatasetContext& context) const |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 103 | { |
| 104 | ndn::nfd::CsInfo info; |
Junxiao Shi | 26667e1 | 2018-02-08 20:01:18 +0000 | [diff] [blame] | 105 | info.setCapacity(m_cs.getLimit()); |
| 106 | info.setEnableAdmit(m_cs.shouldAdmit()); |
| 107 | info.setEnableServe(m_cs.shouldServe()); |
| 108 | info.setNEntries(m_cs.size()); |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 109 | info.setNHits(m_fwCounters.nCsHits); |
| 110 | info.setNMisses(m_fwCounters.nCsMisses); |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 111 | |
| 112 | context.append(info.wireEncode()); |
| 113 | context.end(); |
| 114 | } |
| 115 | |
| 116 | } // namespace nfd |