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-2018, 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 #include "fw/face-table.hpp"
28 
29 #include <ndn-cxx/lp/tags.hpp>
30 #include <ndn-cxx/mgmt/nfd/fib-entry.hpp>
31 
32 #include <boost/range/adaptor/transformed.hpp>
33 
34 namespace nfd {
35 
36 NFD_LOG_INIT("FibManager");
37 
39  const FaceTable& faceTable,
40  Dispatcher& dispatcher,
41  CommandAuthenticator& authenticator)
42  : NfdManagerBase(dispatcher, authenticator, "fib")
43  , m_fib(fib)
44  , m_faceTable(faceTable)
45 {
46  registerCommandHandler<ndn::nfd::FibAddNextHopCommand>("add-nexthop",
47  bind(&FibManager::addNextHop, this, _2, _3, _4, _5));
48  registerCommandHandler<ndn::nfd::FibRemoveNextHopCommand>("remove-nexthop",
49  bind(&FibManager::removeNextHop, this, _2, _3, _4, _5));
50 
51  registerStatusDatasetHandler("list", bind(&FibManager::listEntries, this, _1, _2, _3));
52 }
53 
54 void
55 FibManager::addNextHop(const Name& topPrefix, const Interest& interest,
56  ControlParameters parameters,
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  ndn::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  entry->addNextHop(*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 Name& topPrefix, const Interest& interest,
87  ControlParameters parameters,
89 {
90  setFaceForSelfRegistration(interest, parameters);
91  const Name& prefix = parameters.getName();
92  FaceId faceId = parameters.getFaceId();
93 
94  done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
95 
96  Face* face = m_faceTable.get(faceId);
97  if (face == nullptr) {
98  NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-face");
99  return;
100  }
101 
102  fib::Entry* entry = m_fib.findExactMatch(parameters.getName());
103  if (entry == nullptr) {
104  NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-entry");
105  return;
106  }
107 
108  entry->removeNextHop(*face);
109  if (!entry->hasNextHops()) {
110  m_fib.erase(*entry);
111  NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK entry-erased");
112  }
113  else {
114  NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK nexthop-removed");
115  }
116 }
117 
118 void
119 FibManager::listEntries(const Name& topPrefix, const Interest& interest,
121 {
122  for (const auto& entry : m_fib) {
123  const auto& nexthops = entry.getNextHops() |
124  boost::adaptors::transformed([] (const fib::NextHop& nh) {
125  return ndn::nfd::NextHopRecord()
126  .setFaceId(nh.getFace().getId())
127  .setCost(nh.getCost());
128  });
129  context.append(ndn::nfd::FibEntry()
130  .setPrefix(entry.getPrefix())
131  .setNextHopRecords(std::begin(nexthops), std::end(nexthops))
132  .wireEncode());
133  }
134  context.end();
135 }
136 
137 void
138 FibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
139 {
140  bool isSelfRegistration = (parameters.getFaceId() == 0);
141  if (isSelfRegistration) {
142  shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
143  // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
144  // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
145  // and is initialized synchronously with IncomingFaceId field enabled.
146  BOOST_ASSERT(incomingFaceIdTag != nullptr);
147  parameters.setFaceId(*incomingFaceIdTag);
148  }
149 }
150 
151 } // namespace nfd
std::pair< Entry *, bool > insert(const Name &prefix)
inserts a FIB entry for prefix
Definition: fib.cpp:90
Entry * findExactMatch(const Name &prefix)
performs an exact match lookup
Definition: fib.cpp:80
void erase(const Name &prefix)
Definition: fib.cpp:116
represents parameters in a ControlCommand request or response
represents a dispatcher on server side of NFD Management protocol
Definition: dispatcher.hpp:129
size_t wireEncode(EncodingImpl< TAG > &encoder) const
represents an Interest packet
Definition: interest.hpp:42
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:55
static constexpr size_t getMaxDepth()
Maximum number of components in a FIB entry prefix.
Definition: fib.hpp:92
Face * get(FaceId id) const
get face by FaceId
Definition: face-table.cpp:44
provides a tag type for simple types
Definition: tag.hpp:58
ndn Face
Definition: face-impl.hpp:41
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:54
FibManager(Fib &fib, const FaceTable &faceTable, Dispatcher &dispatcher, CommandAuthenticator &authenticator)
Definition: fib-manager.cpp:38
mgmt::ControlResponse ControlResponse
container of all faces
Definition: face-table.hpp:37
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
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:42
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:154
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:84
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:34
void registerStatusDatasetHandler(const std::string &verb, const ndn::mgmt::StatusDatasetHandler &handler)