NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
ndn-stack-helper.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
20 #include "ndn-stack-helper.hpp"
21 
22 #include "ns3/log.h"
23 #include "ns3/names.h"
24 #include "ns3/string.h"
25 #include "ns3/point-to-point-net-device.h"
26 
29 #include "utils/ndn-time.hpp"
30 #include "utils/dummy-keychain.hpp"
32 
33 #include <limits>
34 #include <map>
35 #include <boost/lexical_cast.hpp>
36 
37 NS_LOG_COMPONENT_DEFINE("ndn.StackHelper");
38 
39 namespace ns3 {
40 namespace ndn {
41 
43  : m_needSetDefaultRoutes(false)
44  , m_maxCsSize(100)
45  , m_isRibManagerDisabled(false)
46  , m_isFaceManagerDisabled(false)
47  , m_isStatusServerDisabled(false)
48  , m_isStrategyChoiceManagerDisabled(false)
49 {
51 
52  m_ndnFactory.SetTypeId("ns3::ndn::L3Protocol");
53  m_contentStoreFactory.SetTypeId("ns3::ndn::cs::Lru");
54 
55  m_netDeviceCallbacks.push_back(
56  std::make_pair(PointToPointNetDevice::GetTypeId(),
57  MakeCallback(&StackHelper::PointToPointNetDeviceCallback, this)));
58  // default callback will be fired if non of others callbacks fit or did the job
59 }
60 
62 {
63 }
64 
65 KeyChain&
67 {
68  static ::ndn::KeyChain keyChain("pib-dummy", "tpm-dummy");
69  return keyChain;
70 }
71 
72 void
74 {
75  ::ndn::time::setCustomClocks(make_shared<ns3::ndn::time::CustomSteadyClock>(),
76  make_shared<ns3::ndn::time::CustomSystemClock>());
77 }
78 
79 void
81 {
82  NS_LOG_FUNCTION(this << needSet);
83  m_needSetDefaultRoutes = needSet;
84 }
85 
86 void
87 StackHelper::SetStackAttributes(const std::string& attr1, const std::string& value1,
88  const std::string& attr2, const std::string& value2,
89  const std::string& attr3, const std::string& value3,
90  const std::string& attr4, const std::string& value4)
91 {
92  if (attr1 != "")
93  m_ndnFactory.Set(attr1, StringValue(value1));
94  if (attr2 != "")
95  m_ndnFactory.Set(attr2, StringValue(value2));
96  if (attr3 != "")
97  m_ndnFactory.Set(attr3, StringValue(value3));
98  if (attr4 != "")
99  m_ndnFactory.Set(attr4, StringValue(value4));
100 }
101 
102 void
103 StackHelper::SetOldContentStore(const std::string& contentStore, const std::string& attr1,
104  const std::string& value1, const std::string& attr2,
105  const std::string& value2, const std::string& attr3,
106  const std::string& value3, const std::string& attr4,
107  const std::string& value4)
108 {
109  m_maxCsSize = 0;
110 
111  m_contentStoreFactory.SetTypeId(contentStore);
112  if (attr1 != "")
113  m_contentStoreFactory.Set(attr1, StringValue(value1));
114  if (attr2 != "")
115  m_contentStoreFactory.Set(attr2, StringValue(value2));
116  if (attr3 != "")
117  m_contentStoreFactory.Set(attr3, StringValue(value3));
118  if (attr4 != "")
119  m_contentStoreFactory.Set(attr4, StringValue(value4));
120 }
121 
122 void
123 StackHelper::setCsSize(size_t maxSize)
124 {
125  m_maxCsSize = maxSize;
126 }
127 
128 Ptr<FaceContainer>
129 StackHelper::Install(const NodeContainer& c) const
130 {
131  Ptr<FaceContainer> faces = Create<FaceContainer>();
132  for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i) {
133  faces->AddAll(Install(*i));
134  }
135  return faces;
136 }
137 
138 Ptr<FaceContainer>
140 {
141  return Install(NodeContainer::GetGlobal());
142 }
143 
144 Ptr<FaceContainer>
145 StackHelper::Install(Ptr<Node> node) const
146 {
147  Ptr<FaceContainer> faces = Create<FaceContainer>();
148 
149  if (node->GetObject<L3Protocol>() != 0) {
150  NS_FATAL_ERROR("Cannot re-install NDN stack on node "
151  << node->GetId());
152  return 0;
153  }
154 
155  Ptr<L3Protocol> ndn = m_ndnFactory.Create<L3Protocol>();
156 
157  if (m_isRibManagerDisabled) {
158  ndn->getConfig().put("ndnSIM.disable_rib_manager", true);
159  }
160 
161  if (m_isFaceManagerDisabled) {
162  ndn->getConfig().put("ndnSIM.disable_face_manager", true);
163  }
164 
165  if (m_isStatusServerDisabled) {
166  ndn->getConfig().put("ndnSIM.disable_status_server", true);
167  }
168 
169  if (m_isStrategyChoiceManagerDisabled) {
170  ndn->getConfig().put("ndnSIM.disable_strategy_choice_manager", true);
171  }
172 
173  ndn->getConfig().put("tables.cs_max_packets", (m_maxCsSize == 0) ? 1 : m_maxCsSize);
174 
175  // Create and aggregate content store if NFD's contest store has been disabled
176  if (m_maxCsSize == 0) {
177  ndn->AggregateObject(m_contentStoreFactory.Create<ContentStore>());
178  }
179 
180  // Aggregate L3Protocol on node (must be after setting ndnSIM CS)
181  node->AggregateObject(ndn);
182 
183  for (uint32_t index = 0; index < node->GetNDevices(); index++) {
184  Ptr<NetDevice> device = node->GetDevice(index);
185  // This check does not make sense: LoopbackNetDevice is installed only if IP stack is installed,
186  // Normally, ndnSIM works without IP stack, so no reason to check
187  // if (DynamicCast<LoopbackNetDevice> (device) != 0)
188  // continue; // don't create face for a LoopbackNetDevice
189 
190  faces->Add(this->createAndRegisterFace(node, ndn, device));
191  }
192 
193  return faces;
194 }
195 
196 void
199 {
200  m_netDeviceCallbacks.push_back(std::make_pair(netDeviceType, callback));
201 }
202 
203 void
206 {
207  for (auto& i : m_netDeviceCallbacks) {
208  if (i.first == netDeviceType) {
209  i.second = callback;
210  return;
211  }
212  }
213 }
214 
215 void
218 {
219  m_netDeviceCallbacks.remove_if([&] (const std::pair<TypeId, NetDeviceFaceCreateCallback>& i) {
220  return (i.first == netDeviceType);
221  });
222 }
223 
224 shared_ptr<NetDeviceFace>
225 StackHelper::DefaultNetDeviceCallback(Ptr<Node> node, Ptr<L3Protocol> ndn,
226  Ptr<NetDevice> netDevice) const
227 {
228  NS_LOG_DEBUG("Creating default NetDeviceFace on node " << node->GetId());
229 
230  shared_ptr<NetDeviceFace> face = std::make_shared<NetDeviceFace>(node, netDevice);
231 
232  ndn->addFace(face);
233  NS_LOG_LOGIC("Node " << node->GetId() << ": added NetDeviceFace as face #"
234  << face->getLocalUri());
235 
236  return face;
237 }
238 
239 shared_ptr<NetDeviceFace>
240 StackHelper::PointToPointNetDeviceCallback(Ptr<Node> node, Ptr<L3Protocol> ndn,
241  Ptr<NetDevice> device) const
242 {
243  NS_LOG_DEBUG("Creating point-to-point NetDeviceFace on node " << node->GetId());
244 
245  shared_ptr<NetDeviceFace> face = std::make_shared<NetDeviceFace>(node, device);
246 
247  ndn->addFace(face);
248  NS_LOG_LOGIC("Node " << node->GetId() << ": added NetDeviceFace as face #"
249  << face->getLocalUri());
250 
251  return face;
252 }
253 
254 Ptr<FaceContainer>
255 StackHelper::Install(const std::string& nodeName) const
256 {
257  Ptr<Node> node = Names::Find<Node>(nodeName);
258  return Install(node);
259 }
260 
261 void
262 StackHelper::Update(Ptr<Node> node)
263 {
264  if (node->GetObject<L3Protocol>() == 0) {
265  Install(node);
266  return;
267  }
268 
269  Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>();
270 
271  for (uint32_t index = 0; index < node->GetNDevices(); index++) {
272 
273  Ptr<NetDevice> device = node->GetDevice(index);
274 
275  if (ndn->getFaceByNetDevice(device) == nullptr) {
276  this->createAndRegisterFace(node, ndn, device);
277  }
278  }
279 }
280 
281 void
282 StackHelper::Update(const NodeContainer& c)
283 {
284  for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i) {
285  Update(*i);
286  }
287 }
288 
289 void
290 StackHelper::Update(const std::string& nodeName)
291 {
292  Ptr<Node> node = Names::Find<Node>(nodeName);
293  Update(node);
294 }
295 
296 void
298 {
299  Update(NodeContainer::GetGlobal());
300 }
301 
302 shared_ptr<NetDeviceFace>
303 StackHelper::createAndRegisterFace(Ptr<Node> node, Ptr<L3Protocol> ndn, Ptr<NetDevice> device) const
304 {
305  shared_ptr<NetDeviceFace> face;
306 
307  for (const auto& item : m_netDeviceCallbacks) {
308  if (device->GetInstanceTypeId() == item.first ||
309  device->GetInstanceTypeId().IsChildOf(item.first)) {
310  face = item.second(node, ndn, device);
311  if (face != 0)
312  break;
313  }
314  }
315 
316  if (face == 0) {
317  face = DefaultNetDeviceCallback(node, ndn, device);
318  }
319 
320  if (m_needSetDefaultRoutes) {
321  // default route with lowest priority possible
322  FibHelper::AddRoute(node, "/", face, std::numeric_limits<int32_t>::max());
323  }
324  return face;
325 }
326 
327 void
329 {
330  m_isRibManagerDisabled = true;
331 }
332 
333 void
335 {
336  m_isFaceManagerDisabled = true;
337 }
338 
339 void
341 {
342  m_isStrategyChoiceManagerDisabled = true;
343 }
344 
345 void
347 {
348  m_isStatusServerDisabled = true;
349 }
350 
351 } // namespace ndn
352 } // namespace ns3
void AddNetDeviceFaceCreateCallback(TypeId netDeviceType, NetDeviceFaceCreateCallback callback)
Add callback to create and configure instance of the face, based on supplied Ptr<Node> and Ptr<NetDev...
void UpdateNetDeviceFaceCreateCallback(TypeId netDeviceType, NetDeviceFaceCreateCallback callback)
Update callback to create and configure instance of the face, based on supplied Ptr<Node> and Ptr<Net...
virtual ~StackHelper()
Destroy the NdnStackHelper.
Copyright (c) 2011-2015 Regents of the University of California.
Ptr< FaceContainer > Install(const std::string &nodeName) const
Install Ndn stack on the node.
StackHelper()
Create a new NdnStackHelper with a default NDN_FLOODING forwarding stategy.
void setCsSize(size_t maxSize)
Set maximum size for NFD&#39;s Content Store (in number of packets)
void SetDefaultRoutes(bool needSet)
Set flag indicating necessity to install default routes in FIB.
nfd::ConfigSection & getConfig()
Get NFD config (boost::property_tree)
void SetStackAttributes(const std::string &attr1="", const std::string &value1="", const std::string &attr2="", const std::string &value2="", const std::string &attr3="", const std::string &value3="", const std::string &attr4="", const std::string &value4="")
Set parameters of NdnL3Protocol.
static void AddRoute(Ptr< Node > node, const Name &prefix, shared_ptr< Face > face, int32_t metric)
Add forwarding entry to FIB.
void RemoveNetDeviceFaceCreateCallback(TypeId netDeviceType, NetDeviceFaceCreateCallback callback)
Remove callback to create and configure instance of the face, based on supplied Ptr<Node> and Ptr<Net...
Callback< shared_ptr< NetDeviceFace >, Ptr< Node >, Ptr< L3Protocol >, Ptr< NetDevice > > NetDeviceFaceCreateCallback
void disableFaceManager()
Disable Face Manager.
Ptr< FaceContainer > InstallAll() const
Install Ndn stack on all nodes in the simulation.
Copyright (c) 2011-2015 Regents of the University of California.
void disableRibManager()
Disable the RIB manager of NFD.
void SetOldContentStore(const std::string &contentStoreClass, const std::string &attr1="", const std::string &value1="", const std::string &attr2="", const std::string &value2="", const std::string &attr3="", const std::string &value3="", const std::string &attr4="", const std::string &value4="")
Set ndnSIM 1.0 content store implementation and its attributes.
Implementation network-layer of NDN stack.
void setCustomClocks(shared_ptr< CustomSteadyClock > steadyClock=nullptr, shared_ptr< CustomSystemClock > systemClock=nullptr)
Set custom system and steady clocks.
Definition: time.cpp:35
void disableStatusServer()
Disable Status Server.
void UpdateAll()
Update Ndn stack on all the nodes (Add faces for new devices)
static KeyChain & getKeyChain()
void Update(Ptr< Node > node)
Update Ndn stack on a given node (Add faces for new devices)
void disableStrategyChoiceManager()
Disable Strategy Choice Manager.
Base class for NDN content store.