blob: 470a0ef680d37e028e4dffcbb01a4c8fc4e4f0ac [file] [log] [blame]
Yanbiao Li711c7932015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi02a0e0f2018-02-06 02:03:45 +00002/*
Davide Pesavento45c1f6a2025-01-01 19:30:30 -05003 * Copyright (c) 2014-2025, Regents of the University of California,
Yanbiao Li711c7932015-08-19 16:30:16 -07004 * 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 Pesaventoa3148082018-04-12 18:21:54 -040027
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/logger.hpp"
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000029#include "fw/face-table.hpp"
Davide Pesavento78ddcab2019-02-28 22:00:03 -050030#include "table/fib.hpp"
Davide Pesavento1586aff2017-02-19 23:17:51 -050031
Junxiao Shicbc8e942016-09-06 03:17:45 +000032#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000033#include <ndn-cxx/mgmt/nfd/fib-entry.hpp>
Yanbiao Li711c7932015-08-19 16:30:16 -070034
Davide Pesavento1586aff2017-02-19 23:17:51 -050035#include <boost/range/adaptor/transformed.hpp>
36
Yanbiao Li711c7932015-08-19 16:30:16 -070037namespace nfd {
38
Davide Pesaventoa3148082018-04-12 18:21:54 -040039NFD_LOG_INIT(FibManager);
Yanbiao Li711c7932015-08-19 16:30:16 -070040
Davide Pesavento78ddcab2019-02-28 22:00:03 -050041FibManager::FibManager(Fib& fib, const FaceTable& faceTable,
42 Dispatcher& dispatcher, CommandAuthenticator& authenticator)
43 : ManagerBase("fib", dispatcher, authenticator)
Yanbiao Li711c7932015-08-19 16:30:16 -070044 , m_fib(fib)
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000045 , m_faceTable(faceTable)
Yanbiao Li711c7932015-08-19 16:30:16 -070046{
Davide Pesavento1db1bb62025-01-06 01:23:41 -050047 registerCommandHandler<ndn::nfd::FibAddNextHopCommand>([this] (auto&&, auto&&... args) {
48 addNextHop(std::forward<decltype(args)>(args)...);
49 });
50 registerCommandHandler<ndn::nfd::FibRemoveNextHopCommand>([this] (auto&&, auto&&... args) {
51 removeNextHop(std::forward<decltype(args)>(args)...);
52 });
53 registerStatusDatasetHandler("list", [this] (auto&&, auto&&, auto&&... args) {
54 listEntries(std::forward<decltype(args)>(args)...);
55 });
Yanbiao Li711c7932015-08-19 16:30:16 -070056}
57
58void
Davide Pesaventoae430302023-05-11 01:42:46 -040059FibManager::addNextHop(const Interest& interest, ControlParameters parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -050060 const CommandContinuation& done)
Yanbiao Li711c7932015-08-19 16:30:16 -070061{
62 setFaceForSelfRegistration(interest, parameters);
Yanbiao Li711c7932015-08-19 16:30:16 -070063 const Name& prefix = parameters.getName();
64 FaceId faceId = parameters.getFaceId();
65 uint64_t cost = parameters.getCost();
66
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000067 if (prefix.size() > Fib::getMaxDepth()) {
Davide Pesavento21e24f92025-01-10 22:22:43 -050068 NFD_LOG_DEBUG("add-nexthop(" << prefix << ',' << faceId << ',' << cost <<
69 ") -> FAIL prefix-too-long");
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000070 return done(ControlResponse(414, "FIB entry prefix cannot exceed " +
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050071 std::to_string(Fib::getMaxDepth()) + " components"));
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000072 }
Yanbiao Li711c7932015-08-19 16:30:16 -070073
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000074 Face* face = m_faceTable.get(faceId);
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000075 if (face == nullptr) {
Davide Pesavento21e24f92025-01-10 22:22:43 -050076 NFD_LOG_DEBUG("add-nexthop(" << prefix << ',' << faceId << ',' << cost <<
77 ") -> FAIL unknown-faceid");
Yanbiao Li711c7932015-08-19 16:30:16 -070078 return done(ControlResponse(410, "Face not found"));
79 }
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000080
81 fib::Entry* entry = m_fib.insert(prefix).first;
Ju Pand8315bf2019-07-31 06:59:07 +000082 m_fib.addOrUpdateNextHop(*entry, *face, cost);
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000083
Davide Pesavento21e24f92025-01-10 22:22:43 -050084 NFD_LOG_TRACE("add-nexthop(" << prefix << ',' << faceId << ',' << cost << ") -> OK");
85 return done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
Yanbiao Li711c7932015-08-19 16:30:16 -070086}
87
88void
Davide Pesaventoae430302023-05-11 01:42:46 -040089FibManager::removeNextHop(const Interest& interest, ControlParameters parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -050090 const CommandContinuation& done)
Yanbiao Li711c7932015-08-19 16:30:16 -070091{
92 setFaceForSelfRegistration(interest, parameters);
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000093 const Name& prefix = parameters.getName();
94 FaceId faceId = parameters.getFaceId();
Yanbiao Li711c7932015-08-19 16:30:16 -070095
Davide Pesavento21e24f92025-01-10 22:22:43 -050096 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000097
98 Face* face = m_faceTable.get(faceId);
99 if (face == nullptr) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500100 NFD_LOG_TRACE("remove-nexthop(" << prefix << ',' << faceId << ") -> OK no-face");
Junxiao Shi02a0e0f2018-02-06 02:03:45 +0000101 return;
102 }
103
104 fib::Entry* entry = m_fib.findExactMatch(parameters.getName());
105 if (entry == nullptr) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500106 NFD_LOG_TRACE("remove-nexthop(" << prefix << ',' << faceId << ") -> OK no-entry");
Junxiao Shi02a0e0f2018-02-06 02:03:45 +0000107 return;
108 }
109
Ju Pand8315bf2019-07-31 06:59:07 +0000110 auto status = m_fib.removeNextHop(*entry, *face);
111 switch (status) {
112 case Fib::RemoveNextHopResult::NO_SUCH_NEXTHOP:
Davide Pesavento21e24f92025-01-10 22:22:43 -0500113 NFD_LOG_TRACE("remove-nexthop(" << prefix << ',' << faceId << ") -> OK no-nexthop");
Ju Pand8315bf2019-07-31 06:59:07 +0000114 break;
115 case Fib::RemoveNextHopResult::FIB_ENTRY_REMOVED:
Davide Pesavento21e24f92025-01-10 22:22:43 -0500116 NFD_LOG_TRACE("remove-nexthop(" << prefix << ',' << faceId << ") -> OK entry-erased");
Ju Pand8315bf2019-07-31 06:59:07 +0000117 break;
118 case Fib::RemoveNextHopResult::NEXTHOP_REMOVED:
Davide Pesavento21e24f92025-01-10 22:22:43 -0500119 NFD_LOG_TRACE("remove-nexthop(" << prefix << ',' << faceId << ") -> OK nexthop-removed");
Ju Pand8315bf2019-07-31 06:59:07 +0000120 break;
Junxiao Shi02a0e0f2018-02-06 02:03:45 +0000121 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700122}
123
124void
Davide Pesaventoae430302023-05-11 01:42:46 -0400125FibManager::listEntries(ndn::mgmt::StatusDatasetContext& context)
Yanbiao Li711c7932015-08-19 16:30:16 -0700126{
Davide Pesavento1586aff2017-02-19 23:17:51 -0500127 for (const auto& entry : m_fib) {
128 const auto& nexthops = entry.getNextHops() |
129 boost::adaptors::transformed([] (const fib::NextHop& nh) {
130 return ndn::nfd::NextHopRecord()
131 .setFaceId(nh.getFace().getId())
132 .setCost(nh.getCost());
133 });
134 context.append(ndn::nfd::FibEntry()
135 .setPrefix(entry.getPrefix())
136 .setNextHopRecords(std::begin(nexthops), std::end(nexthops))
137 .wireEncode());
Yanbiao Li711c7932015-08-19 16:30:16 -0700138 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700139 context.end();
140}
141
142void
143FibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
144{
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400145 bool isSelfRegistration = parameters.getFaceId() == face::INVALID_FACEID;
Yanbiao Li711c7932015-08-19 16:30:16 -0700146 if (isSelfRegistration) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400147 auto incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
Junxiao Shi0de23a22015-12-03 20:07:02 +0000148 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
149 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
150 // and is initialized synchronously with IncomingFaceId field enabled.
151 BOOST_ASSERT(incomingFaceIdTag != nullptr);
152 parameters.setFaceId(*incomingFaceIdTag);
Yanbiao Li711c7932015-08-19 16:30:16 -0700153 }
154}
155
Yanbiao Lidf846e52016-01-30 21:53:47 -0800156} // namespace nfd