Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 02a0e0f | 2018-02-06 02:03:45 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [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 "fib-manager.hpp" |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 27 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 28 | #include "common/logger.hpp" |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 29 | #include "fw/face-table.hpp" |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 30 | #include "table/fib.hpp" |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 31 | |
Junxiao Shi | cbc8e94 | 2016-09-06 03:17:45 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/lp/tags.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 33 | #include <ndn-cxx/mgmt/nfd/fib-entry.hpp> |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 34 | |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 35 | #include <boost/range/adaptor/transformed.hpp> |
| 36 | |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 37 | namespace nfd { |
| 38 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 39 | NFD_LOG_INIT(FibManager); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 40 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 41 | FibManager::FibManager(Fib& fib, const FaceTable& faceTable, |
| 42 | Dispatcher& dispatcher, CommandAuthenticator& authenticator) |
| 43 | : ManagerBase("fib", dispatcher, authenticator) |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 44 | , m_fib(fib) |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 45 | , m_faceTable(faceTable) |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 46 | { |
| 47 | registerCommandHandler<ndn::nfd::FibAddNextHopCommand>("add-nexthop", |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 48 | [this] (auto&&, auto&&, auto&&... args) { addNextHop(std::forward<decltype(args)>(args)...); }); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 49 | registerCommandHandler<ndn::nfd::FibRemoveNextHopCommand>("remove-nexthop", |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 50 | [this] (auto&&, auto&&, auto&&... args) { removeNextHop(std::forward<decltype(args)>(args)...); }); |
| 51 | registerStatusDatasetHandler("list", |
| 52 | [this] (auto&&, auto&&, auto&&... args) { listEntries(std::forward<decltype(args)>(args)...); }); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 56 | FibManager::addNextHop(const Interest& interest, ControlParameters parameters, |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 57 | const ndn::mgmt::CommandContinuation& done) |
| 58 | { |
| 59 | setFaceForSelfRegistration(interest, parameters); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 60 | const Name& prefix = parameters.getName(); |
| 61 | FaceId faceId = parameters.getFaceId(); |
| 62 | uint64_t cost = parameters.getCost(); |
| 63 | |
Junxiao Shi | 02a0e0f | 2018-02-06 02:03:45 +0000 | [diff] [blame] | 64 | if (prefix.size() > Fib::getMaxDepth()) { |
| 65 | NFD_LOG_DEBUG("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost << |
| 66 | "): FAIL prefix-too-long"); |
| 67 | return done(ControlResponse(414, "FIB entry prefix cannot exceed " + |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 68 | std::to_string(Fib::getMaxDepth()) + " components")); |
Junxiao Shi | 02a0e0f | 2018-02-06 02:03:45 +0000 | [diff] [blame] | 69 | } |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 70 | |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 71 | Face* face = m_faceTable.get(faceId); |
Junxiao Shi | 02a0e0f | 2018-02-06 02:03:45 +0000 | [diff] [blame] | 72 | if (face == nullptr) { |
| 73 | NFD_LOG_DEBUG("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost << |
| 74 | "): FAIL unknown-faceid"); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 75 | return done(ControlResponse(410, "Face not found")); |
| 76 | } |
Junxiao Shi | 02a0e0f | 2018-02-06 02:03:45 +0000 | [diff] [blame] | 77 | |
| 78 | fib::Entry* entry = m_fib.insert(prefix).first; |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 79 | m_fib.addOrUpdateNextHop(*entry, *face, cost); |
Junxiao Shi | 02a0e0f | 2018-02-06 02:03:45 +0000 | [diff] [blame] | 80 | |
| 81 | NFD_LOG_TRACE("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost << "): OK"); |
| 82 | return done(ControlResponse(200, "Success").setBody(parameters.wireEncode())); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 86 | FibManager::removeNextHop(const Interest& interest, ControlParameters parameters, |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 87 | const ndn::mgmt::CommandContinuation& done) |
| 88 | { |
| 89 | setFaceForSelfRegistration(interest, parameters); |
Junxiao Shi | 02a0e0f | 2018-02-06 02:03:45 +0000 | [diff] [blame] | 90 | const Name& prefix = parameters.getName(); |
| 91 | FaceId faceId = parameters.getFaceId(); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 92 | |
| 93 | done(ControlResponse(200, "Success").setBody(parameters.wireEncode())); |
Junxiao Shi | 02a0e0f | 2018-02-06 02:03:45 +0000 | [diff] [blame] | 94 | |
| 95 | Face* face = m_faceTable.get(faceId); |
| 96 | if (face == nullptr) { |
| 97 | NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-face"); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | fib::Entry* entry = m_fib.findExactMatch(parameters.getName()); |
| 102 | if (entry == nullptr) { |
| 103 | NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-entry"); |
| 104 | return; |
| 105 | } |
| 106 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 107 | auto status = m_fib.removeNextHop(*entry, *face); |
| 108 | switch (status) { |
| 109 | case Fib::RemoveNextHopResult::NO_SUCH_NEXTHOP: |
| 110 | NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-nexthop"); |
| 111 | break; |
| 112 | case Fib::RemoveNextHopResult::FIB_ENTRY_REMOVED: |
| 113 | NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK entry-erased"); |
| 114 | break; |
| 115 | case Fib::RemoveNextHopResult::NEXTHOP_REMOVED: |
| 116 | NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK nexthop-removed"); |
| 117 | break; |
Junxiao Shi | 02a0e0f | 2018-02-06 02:03:45 +0000 | [diff] [blame] | 118 | } |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 122 | FibManager::listEntries(ndn::mgmt::StatusDatasetContext& context) |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 123 | { |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 124 | for (const auto& entry : m_fib) { |
| 125 | const auto& nexthops = entry.getNextHops() | |
| 126 | boost::adaptors::transformed([] (const fib::NextHop& nh) { |
| 127 | return ndn::nfd::NextHopRecord() |
| 128 | .setFaceId(nh.getFace().getId()) |
| 129 | .setCost(nh.getCost()); |
| 130 | }); |
| 131 | context.append(ndn::nfd::FibEntry() |
| 132 | .setPrefix(entry.getPrefix()) |
| 133 | .setNextHopRecords(std::begin(nexthops), std::end(nexthops)) |
| 134 | .wireEncode()); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 135 | } |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 136 | context.end(); |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | FibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters) |
| 141 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 142 | bool isSelfRegistration = parameters.getFaceId() == face::INVALID_FACEID; |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 143 | if (isSelfRegistration) { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 144 | auto incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>(); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 145 | // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field", |
| 146 | // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD |
| 147 | // and is initialized synchronously with IncomingFaceId field enabled. |
| 148 | BOOST_ASSERT(incomingFaceIdTag != nullptr); |
| 149 | parameters.setFaceId(*incomingFaceIdTag); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 153 | } // namespace nfd |