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