NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019, 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>
34 
35 #include <boost/range/adaptor/transformed.hpp>
36 
37 namespace nfd {
38 
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  bind(&FibManager::addNextHop, this, _2, _3, _4, _5));
49  registerCommandHandler<ndn::nfd::FibRemoveNextHopCommand>("remove-nexthop",
50  bind(&FibManager::removeNextHop, this, _2, _3, _4, _5));
51 
52  registerStatusDatasetHandler("list", bind(&FibManager::listEntries, this, _1, _2, _3));
53 }
54 
55 void
56 FibManager::addNextHop(const Name& topPrefix, const Interest& interest,
57  ControlParameters parameters,
59 {
60  setFaceForSelfRegistration(interest, parameters);
61  const Name& prefix = parameters.getName();
62  FaceId faceId = parameters.getFaceId();
63  uint64_t cost = parameters.getCost();
64 
65  if (prefix.size() > Fib::getMaxDepth()) {
66  NFD_LOG_DEBUG("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost <<
67  "): FAIL prefix-too-long");
68  return done(ControlResponse(414, "FIB entry prefix cannot exceed " +
69  to_string(Fib::getMaxDepth()) + " components"));
70  }
71 
72  Face* face = m_faceTable.get(faceId);
73  if (face == nullptr) {
74  NFD_LOG_DEBUG("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost <<
75  "): FAIL unknown-faceid");
76  return done(ControlResponse(410, "Face not found"));
77  }
78 
79  fib::Entry* entry = m_fib.insert(prefix).first;
80  m_fib.addOrUpdateNextHop(*entry, *face, cost);
81 
82  NFD_LOG_TRACE("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost << "): OK");
83  return done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
84 }
85 
86 void
87 FibManager::removeNextHop(const Name& topPrefix, const Interest& interest,
88  ControlParameters parameters,
90 {
91  setFaceForSelfRegistration(interest, parameters);
92  const Name& prefix = parameters.getName();
93  FaceId faceId = parameters.getFaceId();
94 
95  done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
96 
97  Face* face = m_faceTable.get(faceId);
98  if (face == nullptr) {
99  NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-face");
100  return;
101  }
102 
103  fib::Entry* entry = m_fib.findExactMatch(parameters.getName());
104  if (entry == nullptr) {
105  NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-entry");
106  return;
107  }
108 
109  auto status = m_fib.removeNextHop(*entry, *face);
110  switch (status) {
112  NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-nexthop");
113  break;
115  NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK entry-erased");
116  break;
118  NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK nexthop-removed");
119  break;
120  }
121 }
122 
123 void
124 FibManager::listEntries(const Name& topPrefix, const Interest& interest,
126 {
127  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());
138  }
139  context.end();
140 }
141 
142 void
143 FibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
144 {
145  bool isSelfRegistration = (parameters.getFaceId() == 0);
146  if (isSelfRegistration) {
147  shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
148  // 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);
153  }
154 }
155 
156 } // namespace nfd
NFD_LOG_TRACE
#define NFD_LOG_TRACE
Definition: logger.hpp:37
nfd::fib::Fib::getMaxDepth
static constexpr size_t getMaxDepth()
Maximum number of components in a FIB entry prefix.
Definition: fib.hpp:88
ndn::mgmt::StatusDatasetContext::append
void append(const Block &block)
append a Block to the response
Definition: status-dataset-context.cpp:69
ndn::tlv::Interest
@ Interest
Definition: tlv.hpp:65
ndn::SimpleTag
provides a tag type for simple types
Definition: tag.hpp:59
ndn::Name::size
size_t size() const
Returns the number of components.
Definition: name.hpp:153
fib.hpp
ndn::nfd::FibEntry
Definition: fib-entry.hpp:102
ndn::mgmt::CommandContinuation
std::function< void(const ControlResponse &resp)> CommandContinuation
a function to be called after ControlCommandHandler completes
Definition: dispatcher.hpp:95
ndn::nfd::ControlResponse
mgmt::ControlResponse ControlResponse
Definition: control-response.hpp:30
nfd::ManagerBase::registerStatusDatasetHandler
void registerStatusDatasetHandler(const std::string &verb, const ndn::mgmt::StatusDatasetHandler &handler)
Definition: manager-base.cpp:47
ndn::nfd::ControlParameters::getCost
uint64_t getCost() const
Definition: control-parameters.hpp:263
nfd::fib::Fib::addOrUpdateNextHop
void addOrUpdateNextHop(Entry &entry, Face &face, uint64_t cost)
Add a NextHop record.
Definition: fib.cpp:136
face-table.hpp
nfd::fib::Fib::insert
std::pair< Entry *, bool > insert(const Name &prefix)
Find or insert a FIB entry.
Definition: fib.cpp:90
nfd::fib::Fib::RemoveNextHopResult::NO_SUCH_NEXTHOP
@ NO_SUCH_NEXTHOP
the nexthop is not found
nfd::FibManager::FibManager
FibManager(fib::Fib &fib, const FaceTable &faceTable, Dispatcher &dispatcher, CommandAuthenticator &authenticator)
Definition: fib-manager.cpp:41
nfd::FibManager
Implements the FIB Management of NFD Management Protocol.
Definition: fib-manager.hpp:44
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ns3::ndn::Name
Name
Definition: ndn-common.cpp:25
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
nfd::face::FaceId
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:44
nfd::fib::Fib::findExactMatch
Entry * findExactMatch(const Name &prefix)
Performs an exact match lookup.
Definition: fib.cpp:80
nfd::FaceTable
container of all faces
Definition: face-table.hpp:39
ndn::mgmt::StatusDatasetContext
provides a context for generating response to a StatusDataset request
Definition: status-dataset-context.hpp:37
ndn::tlv::nfd::ControlParameters
@ ControlParameters
Definition: tlv-nfd.hpp:35
nfd::fib::Fib::RemoveNextHopResult::NEXTHOP_REMOVED
@ NEXTHOP_REMOVED
the nexthop is removed and the fib entry stays
ndn::Interest
Represents an Interest packet.
Definition: interest.hpp:44
ndn::nfd::ControlParameters::wireEncode
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Definition: control-parameters.cpp:50
ndn::nfd::FibEntry::wireEncode
size_t wireEncode(EncodingImpl< TAG > &block) const
Definition: fib-entry.cpp:177
nfd::FaceTable::get
Face * get(FaceId id) const
get face by FaceId
Definition: face-table.cpp:45
NFD_LOG_DEBUG
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
Face
ndn Face
Definition: face-impl.hpp:41
nfd::ManagerBase
A collection of common functions shared by all NFD managers, such as communicating with the dispatche...
Definition: manager-base.hpp:48
ndn::nfd::ControlParameters
represents parameters in a ControlCommand request or response
Definition: control-parameters.hpp:82
fib-manager.hpp
ndn::nfd::ControlParameters::getName
const Name & getName() const
Definition: control-parameters.hpp:113
nfd::fib::Fib::RemoveNextHopResult::FIB_ENTRY_REMOVED
@ FIB_ENTRY_REMOVED
the nexthop is removed and the fib entry is removed
ndn::nfd::FibEntry::setNextHopRecords
FibEntry & setNextHopRecords(InputIt first, InputIt last)
Definition: fib-entry.hpp:132
ndn::mgmt::Dispatcher
represents a dispatcher on server side of NFD Management protocol
Definition: dispatcher.hpp:131
ndn::to_string
std::string to_string(const T &val)
Definition: backports.hpp:102
ndn::mgmt::StatusDatasetContext::end
void end()
end the response successfully after appending zero or more blocks
Definition: status-dataset-context.cpp:95
ndn::nfd::FibEntry::setPrefix
FibEntry & setPrefix(const Name &prefix)
Definition: fib-entry.cpp:152
ndn::tlv::nfd::NextHopRecord
@ NextHopRecord
Definition: tlv-nfd.hpp:96
nfd::fib::Fib::removeNextHop
RemoveNextHopResult removeNextHop(Entry &entry, const Face &face)
Remove the NextHop record for face from entry.
Definition: fib.cpp:147
nfd::fib::Fib
Represents the Forwarding Information Base (FIB)
Definition: fib.hpp:48
fib-entry.hpp
tags.hpp
nfd::CommandAuthenticator
Provides ControlCommand authorization according to NFD configuration file.
Definition: command-authenticator.hpp:46
ndn::nfd::ControlParameters::getFaceId
uint64_t getFaceId() const
Definition: control-parameters.hpp:143
NFD_LOG_INIT
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
logger.hpp