fib-manager.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, Regents of the University of California,
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"
27 
28 #include "common/logger.hpp"
29 #include "fw/face-table.hpp"
30 #include "table/fib.hpp"
31 
32 #include <ndn-cxx/lp/tags.hpp>
33 #include <ndn-cxx/mgmt/nfd/fib-entry.hpp>
34 
35 #include <boost/range/adaptor/transformed.hpp>
36 
37 namespace nfd {
38 
39 NFD_LOG_INIT(FibManager);
40 
41 FibManager::FibManager(Fib& fib, const FaceTable& faceTable,
42  Dispatcher& dispatcher, CommandAuthenticator& authenticator)
43  : ManagerBase("fib", dispatcher, authenticator)
44  , m_fib(fib)
45  , m_faceTable(faceTable)
46 {
47  registerCommandHandler<ndn::nfd::FibAddNextHopCommand>("add-nexthop",
48  [this] (auto&&, auto&&, auto&&... args) { addNextHop(std::forward<decltype(args)>(args)...); });
49  registerCommandHandler<ndn::nfd::FibRemoveNextHopCommand>("remove-nexthop",
50  [this] (auto&&, auto&&, auto&&... args) { removeNextHop(std::forward<decltype(args)>(args)...); });
52  [this] (auto&&, auto&&, auto&&... args) { listEntries(std::forward<decltype(args)>(args)...); });
53 }
54 
55 void
56 FibManager::addNextHop(const Interest& interest, ControlParameters parameters,
57  const ndn::mgmt::CommandContinuation& done)
58 {
59  setFaceForSelfRegistration(interest, parameters);
60  const Name& prefix = parameters.getName();
61  FaceId faceId = parameters.getFaceId();
62  uint64_t cost = parameters.getCost();
63 
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 " +
68  std::to_string(Fib::getMaxDepth()) + " components"));
69  }
70 
71  Face* face = m_faceTable.get(faceId);
72  if (face == nullptr) {
73  NFD_LOG_DEBUG("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost <<
74  "): FAIL unknown-faceid");
75  return done(ControlResponse(410, "Face not found"));
76  }
77 
78  fib::Entry* entry = m_fib.insert(prefix).first;
79  m_fib.addOrUpdateNextHop(*entry, *face, cost);
80 
81  NFD_LOG_TRACE("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost << "): OK");
82  return done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
83 }
84 
85 void
86 FibManager::removeNextHop(const Interest& interest, ControlParameters parameters,
87  const ndn::mgmt::CommandContinuation& done)
88 {
89  setFaceForSelfRegistration(interest, parameters);
90  const Name& prefix = parameters.getName();
91  FaceId faceId = parameters.getFaceId();
92 
93  done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
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 
107  auto status = m_fib.removeNextHop(*entry, *face);
108  switch (status) {
110  NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-nexthop");
111  break;
113  NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK entry-erased");
114  break;
116  NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK nexthop-removed");
117  break;
118  }
119 }
120 
121 void
122 FibManager::listEntries(ndn::mgmt::StatusDatasetContext& context)
123 {
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());
135  }
136  context.end();
137 }
138 
139 void
140 FibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
141 {
142  bool isSelfRegistration = parameters.getFaceId() == face::INVALID_FACEID;
143  if (isSelfRegistration) {
144  auto incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
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);
150  }
151 }
152 
153 } // namespace nfd
Provides ControlCommand authorization according to NFD's configuration file.
Container of all faces.
Definition: face-table.hpp:42
Face * get(FaceId id) const noexcept
Get face by FaceId.
Definition: face-table.cpp:38
FibManager(fib::Fib &fib, const FaceTable &faceTable, Dispatcher &dispatcher, CommandAuthenticator &authenticator)
Definition: fib-manager.cpp:41
A collection of common functions shared by all NFD managers, such as communicating with the dispatche...
void registerStatusDatasetHandler(const std::string &verb, const ndn::mgmt::StatusDatasetHandler &handler)
Represents the Forwarding Information Base (FIB).
Definition: fib.hpp:51
@ NEXTHOP_REMOVED
the nexthop is removed and the fib entry stays
@ FIB_ENTRY_REMOVED
the nexthop is removed and the fib entry is removed
@ NO_SUCH_NEXTHOP
the nexthop is not found
std::pair< Entry *, bool > insert(const Name &prefix)
Find or insert a FIB entry.
Definition: fib.cpp:85
Entry * findExactMatch(const Name &prefix)
Performs an exact match lookup.
Definition: fib.cpp:75
RemoveNextHopResult removeNextHop(Entry &entry, const Face &face)
Remove the NextHop record for face from entry.
Definition: fib.cpp:139
static constexpr size_t getMaxDepth()
Maximum number of components in a FIB entry prefix.
Definition: fib.hpp:91
void addOrUpdateNextHop(Entry &entry, Face &face, uint64_t cost)
Add a NextHop record.
Definition: fib.cpp:131
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
#define NFD_LOG_TRACE
Definition: logger.hpp:37
constexpr FaceId INVALID_FACEID
Indicates an invalid FaceId.
Definition: face-common.hpp:51
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:48
-status-http-server
Definition: common.hpp:71