NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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; -*- */
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 
61  const Name& prefix = parameters.getName();
62  FaceId faceId = parameters.getFaceId();
63  uint64_t cost = parameters.getCost();
64 
65  NFD_LOG_TRACE("add-nexthop prefix: " << prefix
66  << " faceid: " << faceId
67  << " cost: " << cost);
68 
69  Face* face = m_faceTable.get(faceId);
70  if (face != nullptr) {
71  fib::Entry* entry = m_fib.insert(prefix).first;
72  entry->addNextHop(*face, cost);
73 
74  NFD_LOG_DEBUG("add-nexthop result: OK"
75  << " prefix:" << prefix
76  << " faceid: " << faceId
77  << " cost: " << cost);
78 
79  return done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
80  }
81  else {
82  NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-faceid: " << faceId);
83  return done(ControlResponse(410, "Face not found"));
84  }
85 }
86 
87 void
88 FibManager::removeNextHop(const Name& topPrefix, const Interest& interest,
89  ControlParameters parameters,
91 {
92  setFaceForSelfRegistration(interest, parameters);
93 
94  NFD_LOG_TRACE("remove-nexthop prefix: " << parameters.getName()
95  << " faceid: " << parameters.getFaceId());
96 
97  Face* face = m_faceTable.get(parameters.getFaceId());
98  if (face != nullptr) {
99  fib::Entry* entry = m_fib.findExactMatch(parameters.getName());
100  if (entry != nullptr) {
101  entry->removeNextHop(*face);
102  NFD_LOG_DEBUG("remove-nexthop result: OK prefix: " << parameters.getName()
103  << " faceid: " << parameters.getFaceId());
104 
105  if (!entry->hasNextHops()) {
106  m_fib.erase(*entry);
107  }
108  }
109  else {
110  NFD_LOG_DEBUG("remove-nexthop result: OK");
111  }
112  }
113  else {
114  NFD_LOG_DEBUG("remove-nexthop result: OK");
115  }
116 
117  done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
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
ControlParameters & setFaceId(uint64_t faceId)
uint64_t getCost() const
Definition: fib-nexthop.hpp:51
represents parameters in a ControlCommand request or response
represents a FIB entry
Definition: fib-entry.hpp:51
represents a dispatcher on server side of NFD Management protocol
Definition: dispatcher.hpp:129
void addNextHop(Face &face, uint64_t cost)
adds a NextHop record
Definition: fib-entry.cpp:53
represents an Interest packet
Definition: interest.hpp:42
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:55
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:56
provides a tag type for simple types
Definition: tag.hpp:58
void removeNextHop(const Face &face)
removes a NextHop record
Definition: fib-entry.cpp:66
ndn Face
Definition: face-impl.hpp:41
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:54
size_t wireEncode(EncodingImpl< TAG > &encoder) const
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
Face & getFace() const
Definition: fib-nexthop.hpp:45
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
bool hasNextHops() const
Definition: fib-entry.hpp:72
provides ControlCommand authorization according to NFD configuration file
void append(const Block &block)
append a Block to the response
shared_ptr< T > getTag() const
get a tag item
Definition: tag-host.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:34
void registerStatusDatasetHandler(const std::string &verb, const ndn::mgmt::StatusDatasetHandler &handler)
Face * get(FaceId id) const
get face by FaceId
Definition: face-table.cpp:44
represents a nexthop record in FIB entry
Definition: fib-nexthop.hpp:38