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 "core/logger.hpp"
29 #include "fw/face-table.hpp"
30 
31 #include <ndn-cxx/lp/tags.hpp>
33 
34 #include <boost/range/adaptor/transformed.hpp>
35 
36 namespace nfd {
37 
39 
41  const FaceTable& faceTable,
42  Dispatcher& dispatcher,
43  CommandAuthenticator& authenticator)
44  : NfdManagerBase(dispatcher, authenticator, "fib")
45  , m_fib(fib)
46  , m_faceTable(faceTable)
47 {
48  registerCommandHandler<ndn::nfd::FibAddNextHopCommand>("add-nexthop",
49  bind(&FibManager::addNextHop, this, _2, _3, _4, _5));
50  registerCommandHandler<ndn::nfd::FibRemoveNextHopCommand>("remove-nexthop",
51  bind(&FibManager::removeNextHop, this, _2, _3, _4, _5));
52 
53  registerStatusDatasetHandler("list", bind(&FibManager::listEntries, this, _1, _2, _3));
54 }
55 
56 void
57 FibManager::addNextHop(const Name& topPrefix, const Interest& interest,
58  ControlParameters parameters,
60 {
61  setFaceForSelfRegistration(interest, parameters);
62  const Name& prefix = parameters.getName();
63  FaceId faceId = parameters.getFaceId();
64  uint64_t cost = parameters.getCost();
65 
66  if (prefix.size() > Fib::getMaxDepth()) {
67  NFD_LOG_DEBUG("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost <<
68  "): FAIL prefix-too-long");
69  return done(ControlResponse(414, "FIB entry prefix cannot exceed " +
70  ndn::to_string(Fib::getMaxDepth()) + " components"));
71  }
72 
73  Face* face = m_faceTable.get(faceId);
74  if (face == nullptr) {
75  NFD_LOG_DEBUG("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost <<
76  "): FAIL unknown-faceid");
77  return done(ControlResponse(410, "Face not found"));
78  }
79 
80  fib::Entry* entry = m_fib.insert(prefix).first;
81  entry->addOrUpdateNextHop(*face, 0, cost);
82 
83  NFD_LOG_TRACE("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost << "): OK");
84  return done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
85 }
86 
87 void
88 FibManager::removeNextHop(const Name& topPrefix, const Interest& interest,
89  ControlParameters parameters,
91 {
92  setFaceForSelfRegistration(interest, parameters);
93  const Name& prefix = parameters.getName();
94  FaceId faceId = parameters.getFaceId();
95 
96  done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
97 
98  Face* face = m_faceTable.get(faceId);
99  if (face == nullptr) {
100  NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-face");
101  return;
102  }
103 
104  fib::Entry* entry = m_fib.findExactMatch(parameters.getName());
105  if (entry == nullptr) {
106  NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-entry");
107  return;
108  }
109 
110  entry->removeNextHop(*face, 0);
111  if (!entry->hasNextHops()) {
112  m_fib.erase(*entry);
113  NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK entry-erased");
114  }
115  else {
116  NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK nexthop-removed");
117  }
118 }
119 
120 void
121 FibManager::listEntries(const Name& topPrefix, const Interest& interest,
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() == 0);
143  if (isSelfRegistration) {
144  shared_ptr<lp::IncomingFaceIdTag> 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
std::pair< Entry *, bool > insert(const Name &prefix)
find or insert a FIB entry
Definition: fib.cpp:91
Entry * findExactMatch(const Name &prefix)
performs an exact match lookup
Definition: fib.cpp:81
void erase(const Name &prefix)
Definition: fib.cpp:117
represents parameters in a ControlCommand request or response
represents a dispatcher on server side of NFD Management protocol
Definition: dispatcher.hpp:130
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Represents an Interest packet.
Definition: interest.hpp:44
static constexpr size_t getMaxDepth()
Maximum number of components in a FIB entry prefix.
Definition: fib.hpp:90
Face * get(FaceId id) const
get face by FaceId
Definition: face-table.cpp:45
provides a tag type for simple types
Definition: tag.hpp:58
ndn Face
Definition: face-impl.hpp:42
FibManager(Fib &fib, const FaceTable &faceTable, Dispatcher &dispatcher, CommandAuthenticator &authenticator)
Definition: fib-manager.cpp:40
mgmt::ControlResponse ControlResponse
container of all faces
Definition: face-table.hpp:37
implement the FIB Management of NFD Management Protocol.
Definition: fib-manager.hpp:41
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
#define NFD_LOG_TRACE
Definition: logger.hpp:37
void end()
end the response successfully after appending zero or more blocks
represents the Forwarding Information Base (FIB)
Definition: fib.hpp:49
Represents an absolute name.
Definition: name.hpp:43
std::function< void(const ControlResponse &resp)> CommandContinuation
a function to be called after ControlCommandHandler completes
Definition: dispatcher.hpp:95
size_t size() const
Get number of components.
Definition: name.hpp:147
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
Provides ControlCommand authorization according to NFD configuration file.
void append(const Block &block)
append a Block to the response
std::string to_string(const V &v)
Definition: backports.hpp:67
provides a context for generating response to a StatusDataset request
uint64_t FaceId
identifies a face
Definition: face.hpp:39
a collection of common functions shared by all NFD managers, such as communicating with the dispatche...
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
void registerStatusDatasetHandler(const std::string &verb, const ndn::mgmt::StatusDatasetHandler &handler)