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 #include <ndn-cxx/lp/tags.hpp>
29 #include <ndn-cxx/mgmt/nfd/fib-entry.hpp>
30 
31 namespace nfd {
32 
33 NFD_LOG_INIT("FibManager");
34 
36  const FaceTable& faceTable,
37  Dispatcher& dispatcher,
38  CommandAuthenticator& authenticator)
39  : NfdManagerBase(dispatcher, authenticator, "fib")
40  , m_fib(fib)
41  , m_faceTable(faceTable)
42 {
43  registerCommandHandler<ndn::nfd::FibAddNextHopCommand>("add-nexthop",
44  bind(&FibManager::addNextHop, this, _2, _3, _4, _5));
45  registerCommandHandler<ndn::nfd::FibRemoveNextHopCommand>("remove-nexthop",
46  bind(&FibManager::removeNextHop, this, _2, _3, _4, _5));
47 
48  registerStatusDatasetHandler("list", bind(&FibManager::listEntries, this, _1, _2, _3));
49 }
50 
51 void
52 FibManager::addNextHop(const Name& topPrefix, const Interest& interest,
53  ControlParameters parameters,
55 {
56  setFaceForSelfRegistration(interest, parameters);
57 
58  const Name& prefix = parameters.getName();
59  FaceId faceId = parameters.getFaceId();
60  uint64_t cost = parameters.getCost();
61 
62  NFD_LOG_TRACE("add-nexthop prefix: " << prefix
63  << " faceid: " << faceId
64  << " cost: " << cost);
65 
66  Face* face = m_faceTable.get(faceId);
67  if (face != nullptr) {
68  fib::Entry* entry = m_fib.insert(prefix).first;
69  entry->addNextHop(*face, cost);
70 
71  NFD_LOG_DEBUG("add-nexthop result: OK"
72  << " prefix:" << prefix
73  << " faceid: " << faceId
74  << " cost: " << cost);
75 
76  return done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
77  }
78  else {
79  NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-faceid: " << faceId);
80  return done(ControlResponse(410, "Face not found"));
81  }
82 }
83 
84 void
85 FibManager::removeNextHop(const Name& topPrefix, const Interest& interest,
86  ControlParameters parameters,
88 {
89  setFaceForSelfRegistration(interest, parameters);
90 
91  NFD_LOG_TRACE("remove-nexthop prefix: " << parameters.getName()
92  << " faceid: " << parameters.getFaceId());
93 
94  Face* face = m_faceTable.get(parameters.getFaceId());
95  if (face != nullptr) {
96  fib::Entry* entry = m_fib.findExactMatch(parameters.getName());
97  if (entry != nullptr) {
98  entry->removeNextHop(*face);
99  NFD_LOG_DEBUG("remove-nexthop result: OK prefix: " << parameters.getName()
100  << " faceid: " << parameters.getFaceId());
101 
102  if (!entry->hasNextHops()) {
103  m_fib.erase(*entry);
104  }
105  }
106  else {
107  NFD_LOG_DEBUG("remove-nexthop result: OK");
108  }
109  }
110  else {
111  NFD_LOG_DEBUG("remove-nexthop result: OK");
112  }
113 
114  done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
115 }
116 
117 void
118 FibManager::listEntries(const Name& topPrefix, const Interest& interest,
120 {
121  for (auto&& entry : m_fib) {
122  auto prefix = entry.getPrefix();
123  ndn::nfd::FibEntry record;
124  const auto& nextHops = entry.getNextHops();
125 
126  for (auto&& next : nextHops) {
127  ndn::nfd::NextHopRecord nextHopRecord;
128  nextHopRecord.setFaceId(next.getFace().getId());
129  nextHopRecord.setCost(next.getCost());
130 
131  record.addNextHopRecord(nextHopRecord);
132  }
133 
134  record.setPrefix(prefix);
135  context.append(record.wireEncode());
136  }
137 
138  context.end();
139 }
140 
141 void
142 FibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
143 {
144  bool isSelfRegistration = (parameters.getFaceId() == 0);
145  if (isSelfRegistration) {
146  shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
147  // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
148  // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
149  // and is initialized synchronously with IncomingFaceId field enabled.
150  BOOST_ASSERT(incomingFaceIdTag != nullptr);
151  parameters.setFaceId(*incomingFaceIdTag);
152  }
153 }
154 
155 } // namespace nfd
ControlParameters & setFaceId(uint64_t faceId)
shared_ptr< T > getTag() const
get a tag item
Definition: tag-host.hpp:67
std::pair< Entry *, bool > insert(const Name &prefix)
inserts a FIB entry for prefix
Definition: fib.cpp:102
Entry * findExactMatch(const Name &prefix)
performs an exact match lookup
Definition: fib.cpp:92
void erase(const Name &prefix)
Definition: fib.cpp:128
NextHopRecord & setFaceId(uint64_t faceId)
Definition: fib-entry.cpp:59
represents parameters in a ControlCommand request or response
FibEntry & setPrefix(const Name &prefix)
Definition: fib-entry.cpp:170
generalization of a network interface
Definition: face.hpp:67
represents a FIB entry
Definition: fib-entry.hpp:51
represents a dispatcher on server side of NFD Management protocol
Definition: dispatcher.hpp:129
size_t wireEncode(EncodingImpl< TAG > &encoder) const
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
Face * get(FaceId id) const
get face by FaceId
Definition: face-table.cpp:42
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
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:54
size_t wireEncode(EncodingImpl< TAG > &block) const
Definition: fib-entry.cpp:187
FibManager(Fib &fib, const FaceTable &faceTable, Dispatcher &dispatcher, CommandAuthenticator &authenticator)
Definition: fib-manager.cpp:35
ndn::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:47
NextHopRecord & setCost(uint64_t cost)
Definition: fib-entry.cpp:67
Name abstraction to represent an absolute name.
Definition: name.hpp:46
FibEntry & addNextHopRecord(const NextHopRecord &nextHopRecord)
Definition: fib-entry.cpp:178
std::function< void(const ControlResponse &resp)> CommandContinuation
a function to be called after ControlCommandHandler completes
Definition: dispatcher.hpp:95
provides ControlCommand authorization according to NFD configuration file
void append(const Block &block)
append a Block to the response
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)
bool hasNextHops() const
Definition: fib-entry.hpp:72