NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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 #include "ns3/point-to-point-channel.h"
27 
30 #include "utils/ndn-time.hpp"
31 #include "utils/dummy-keychain.hpp"
33 
34 #include <limits>
35 #include <map>
36 #include <boost/lexical_cast.hpp>
37 
38 #include "ns3/ndnSIM/NFD/daemon/face/generic-link-service.hpp"
39 #include "ns3/ndnSIM/NFD/daemon/table/cs-policy-priority-fifo.hpp"
40 #include "ns3/ndnSIM/NFD/daemon/table/cs-policy-lru.hpp"
41 
42 NS_LOG_COMPONENT_DEFINE("ndn.StackHelper");
43 
44 namespace ns3 {
45 namespace ndn {
46 
48  : m_isRibManagerDisabled(false)
49  // , m_isFaceManagerDisabled(false)
50  , m_isForwarderStatusManagerDisabled(false)
51  , m_isStrategyChoiceManagerDisabled(false)
52  , m_needSetDefaultRoutes(false)
53  , m_maxCsSize(100)
54 {
56 
57  m_csPolicies.insert({"nfd::cs::lru", [] { return make_unique<nfd::cs::LruPolicy>(); }});
58  m_csPolicies.insert({"nfd::cs::priority_fifo", [] () { return make_unique<nfd::cs::PriorityFifoPolicy>(); }});
59 
60  m_csPolicyCreationFunc = m_csPolicies["nfd::cs::lru"];
61 
62  m_ndnFactory.SetTypeId("ns3::ndn::L3Protocol");
63  m_contentStoreFactory.SetTypeId("ns3::ndn::cs::Lru");
64 
65  m_netDeviceCallbacks.push_back(
66  std::make_pair(PointToPointNetDevice::GetTypeId(),
67  MakeCallback(&StackHelper::PointToPointNetDeviceCallback, this)));
68  // default callback will be fired if non of others callbacks fit or did the job
69 }
70 
72 {
73 }
74 
75 KeyChain&
77 {
78  static ::ndn::KeyChain keyChain("pib-dummy", "tpm-dummy");
79  return keyChain;
80 }
81 
82 void
84 {
85  ::ndn::time::setCustomClocks(make_shared<ns3::ndn::time::CustomSteadyClock>(),
86  make_shared<ns3::ndn::time::CustomSystemClock>());
87 }
88 
89 void
91 {
92  NS_LOG_FUNCTION(this << needSet);
93  m_needSetDefaultRoutes = needSet;
94 }
95 
96 void
97 StackHelper::SetStackAttributes(const std::string& attr1, const std::string& value1,
98  const std::string& attr2, const std::string& value2,
99  const std::string& attr3, const std::string& value3,
100  const std::string& attr4, const std::string& value4)
101 {
102  if (attr1 != "")
103  m_ndnFactory.Set(attr1, StringValue(value1));
104  if (attr2 != "")
105  m_ndnFactory.Set(attr2, StringValue(value2));
106  if (attr3 != "")
107  m_ndnFactory.Set(attr3, StringValue(value3));
108  if (attr4 != "")
109  m_ndnFactory.Set(attr4, StringValue(value4));
110 }
111 
112 void
113 StackHelper::SetOldContentStore(const std::string& contentStore, const std::string& attr1,
114  const std::string& value1, const std::string& attr2,
115  const std::string& value2, const std::string& attr3,
116  const std::string& value3, const std::string& attr4,
117  const std::string& value4)
118 {
119  m_maxCsSize = 0;
120 
121  m_contentStoreFactory.SetTypeId(contentStore);
122  if (attr1 != "")
123  m_contentStoreFactory.Set(attr1, StringValue(value1));
124  if (attr2 != "")
125  m_contentStoreFactory.Set(attr2, StringValue(value2));
126  if (attr3 != "")
127  m_contentStoreFactory.Set(attr3, StringValue(value3));
128  if (attr4 != "")
129  m_contentStoreFactory.Set(attr4, StringValue(value4));
130 }
131 
132 void
133 StackHelper::setCsSize(size_t maxSize)
134 {
135  m_maxCsSize = maxSize;
136 }
137 
138 void
139 StackHelper::setPolicy(const std::string& policy)
140 {
141  auto found = m_csPolicies.find(policy);
142  if (found != m_csPolicies.end()) {
143  m_csPolicyCreationFunc = found->second;
144  }
145  else {
146  NS_FATAL_ERROR("Cache replacement policy " << policy << " not found");
147  NS_LOG_DEBUG("Available cache replacement policies: ");
148  for (auto it = m_csPolicies.begin(); it != m_csPolicies.end(); it++) {
149  NS_LOG_DEBUG(" " << it->first);
150  }
151  }
152 }
153 
154 Ptr<FaceContainer>
155 StackHelper::Install(const NodeContainer& c) const
156 {
157  Ptr<FaceContainer> faces = Create<FaceContainer>();
158  for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i) {
159  faces->AddAll(Install(*i));
160  }
161  return faces;
162 }
163 
164 Ptr<FaceContainer>
166 {
167  return Install(NodeContainer::GetGlobal());
168 }
169 
170 Ptr<FaceContainer>
171 StackHelper::Install(Ptr<Node> node) const
172 {
173  Ptr<FaceContainer> faces = Create<FaceContainer>();
174 
175  if (node->GetObject<L3Protocol>() != 0) {
176  NS_FATAL_ERROR("Cannot re-install NDN stack on node "
177  << node->GetId());
178  return 0;
179  }
180 
181  Ptr<L3Protocol> ndn = m_ndnFactory.Create<L3Protocol>();
182 
183  if (m_isRibManagerDisabled) {
184  ndn->getConfig().put("ndnSIM.disable_rib_manager", true);
185  }
186 
187  // if (m_isFaceManagerDisabled) {
188  // ndn->getConfig().put("ndnSIM.disable_face_manager", true);
189  // }
190 
191  if (m_isForwarderStatusManagerDisabled) {
192  ndn->getConfig().put("ndnSIM.disable_forwarder_status_manager", true);
193  }
194 
195  if (m_isStrategyChoiceManagerDisabled) {
196  ndn->getConfig().put("ndnSIM.disable_strategy_choice_manager", true);
197  }
198 
199  ndn->getConfig().put("tables.cs_max_packets", (m_maxCsSize == 0) ? 1 : m_maxCsSize);
200 
201  // Create and aggregate content store if NFD's contest store has been disabled
202  if (m_maxCsSize == 0) {
203  ndn->AggregateObject(m_contentStoreFactory.Create<ContentStore>());
204  }
205  // if NFD's CS is enabled, check if a replacement policy has been specified
206  else {
207  ndn->setCsReplacementPolicy(m_csPolicyCreationFunc);
208  }
209 
210  // Aggregate L3Protocol on node (must be after setting ndnSIM CS)
211  node->AggregateObject(ndn);
212 
213  for (uint32_t index = 0; index < node->GetNDevices(); index++) {
214  Ptr<NetDevice> device = node->GetDevice(index);
215  // This check does not make sense: LoopbackNetDevice is installed only if IP stack is installed,
216  // Normally, ndnSIM works without IP stack, so no reason to check
217  // if (DynamicCast<LoopbackNetDevice> (device) != 0)
218  // continue; // don't create face for a LoopbackNetDevice
219 
220  faces->Add(this->createAndRegisterFace(node, ndn, device));
221  }
222 
223  return faces;
224 }
225 
226 void
229 {
230  m_netDeviceCallbacks.push_back(std::make_pair(netDeviceType, callback));
231 }
232 
233 void
235  FaceCreateCallback callback)
236 {
237  for (auto& i : m_netDeviceCallbacks) {
238  if (i.first == netDeviceType) {
239  i.second = callback;
240  return;
241  }
242  }
243 }
244 
245 void
247  FaceCreateCallback callback)
248 {
249  m_netDeviceCallbacks.remove_if([&] (const std::pair<TypeId, FaceCreateCallback>& i) {
250  return (i.first == netDeviceType);
251  });
252 }
253 
254 std::string
255 constructFaceUri(Ptr<NetDevice> netDevice)
256 {
257  std::string uri = "netdev://";
258  Address address = netDevice->GetAddress();
259  if (Mac48Address::IsMatchingType(address)) {
260  uri += "[" + boost::lexical_cast<std::string>(Mac48Address::ConvertFrom(address)) + "]";
261  }
262 
263  return uri;
264 }
265 
266 
267 shared_ptr<Face>
268 StackHelper::DefaultNetDeviceCallback(Ptr<Node> node, Ptr<L3Protocol> ndn,
269  Ptr<NetDevice> netDevice) const
270 {
271  NS_LOG_DEBUG("Creating default Face on node " << node->GetId());
272 
273  // Create an ndnSIM-specific transport instance
275  opts.allowFragmentation = true;
276  opts.allowReassembly = true;
277 
278  auto linkService = make_unique<::nfd::face::GenericLinkService>(opts);
279 
280  auto transport = make_unique<NetDeviceTransport>(node, netDevice,
281  constructFaceUri(netDevice),
282  "netdev://[ff:ff:ff:ff:ff:ff]");
283 
284  auto face = std::make_shared<Face>(std::move(linkService), std::move(transport));
285  face->setMetric(1);
286 
287  ndn->addFace(face);
288  NS_LOG_LOGIC("Node " << node->GetId() << ": added Face as face #"
289  << face->getLocalUri());
290 
291  return face;
292 }
293 
294 shared_ptr<Face>
295 StackHelper::PointToPointNetDeviceCallback(Ptr<Node> node, Ptr<L3Protocol> ndn,
296  Ptr<NetDevice> device) const
297 {
298  NS_LOG_DEBUG("Creating point-to-point Face on node " << node->GetId());
299 
300  Ptr<PointToPointNetDevice> netDevice = DynamicCast<PointToPointNetDevice>(device);
301  NS_ASSERT(netDevice != nullptr);
302 
303  // access the other end of the link
304  Ptr<PointToPointChannel> channel = DynamicCast<PointToPointChannel>(netDevice->GetChannel());
305  NS_ASSERT(channel != nullptr);
306 
307  Ptr<NetDevice> remoteNetDevice = channel->GetDevice(0);
308  if (remoteNetDevice->GetNode() == node)
309  remoteNetDevice = channel->GetDevice(1);
310 
311  // Create an ndnSIM-specific transport instance
313  opts.allowFragmentation = true;
314  opts.allowReassembly = true;
315 
316  auto linkService = make_unique<::nfd::face::GenericLinkService>(opts);
317 
318  auto transport = make_unique<NetDeviceTransport>(node, netDevice,
319  constructFaceUri(netDevice),
320  constructFaceUri(remoteNetDevice));
321 
322  auto face = std::make_shared<Face>(std::move(linkService), std::move(transport));
323  face->setMetric(1);
324 
325  ndn->addFace(face);
326  NS_LOG_LOGIC("Node " << node->GetId() << ": added Face as face #"
327  << face->getLocalUri());
328 
329  return face;
330 }
331 
332 Ptr<FaceContainer>
333 StackHelper::Install(const std::string& nodeName) const
334 {
335  Ptr<Node> node = Names::Find<Node>(nodeName);
336  return Install(node);
337 }
338 
339 void
340 StackHelper::Update(Ptr<Node> node)
341 {
342  if (node->GetObject<L3Protocol>() == 0) {
343  Install(node);
344  return;
345  }
346 
347  Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>();
348 
349  for (uint32_t index = 0; index < node->GetNDevices(); index++) {
350 
351  Ptr<NetDevice> device = node->GetDevice(index);
352 
353  if (ndn->getFaceByNetDevice(device) == nullptr) {
354  this->createAndRegisterFace(node, ndn, device);
355  }
356  }
357 }
358 
359 void
360 StackHelper::Update(const NodeContainer& c)
361 {
362  for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i) {
363  Update(*i);
364  }
365 }
366 
367 void
368 StackHelper::Update(const std::string& nodeName)
369 {
370  Ptr<Node> node = Names::Find<Node>(nodeName);
371  Update(node);
372 }
373 
374 void
376 {
377  Update(NodeContainer::GetGlobal());
378 }
379 
380 shared_ptr<Face>
381 StackHelper::createAndRegisterFace(Ptr<Node> node, Ptr<L3Protocol> ndn, Ptr<NetDevice> device) const
382 {
383  shared_ptr<Face> face;
384 
385  for (const auto& item : m_netDeviceCallbacks) {
386  if (device->GetInstanceTypeId() == item.first ||
387  device->GetInstanceTypeId().IsChildOf(item.first)) {
388  face = item.second(node, ndn, device);
389  if (face != 0)
390  break;
391  }
392  }
393 
394  if (face == 0) {
395  face = DefaultNetDeviceCallback(node, ndn, device);
396  }
397 
398  if (m_needSetDefaultRoutes) {
399  // default route with lowest priority possible
400  FibHelper::AddRoute(node, "/", face, std::numeric_limits<int32_t>::max());
401  }
402  return face;
403 }
404 
405 void
407 {
408  m_isRibManagerDisabled = true;
409 }
410 
411 // void
412 // StackHelper::disableFaceManager()
413 // {
414 // m_isFaceManagerDisabled = true;
415 // }
416 
417 void
419 {
420  m_isStrategyChoiceManagerDisabled = true;
421 }
422 
423 void
425 {
426  m_isForwarderStatusManagerDisabled = true;
427 }
428 
429 } // namespace ndn
430 } // namespace ns3
virtual ~StackHelper()
Destroy the NdnStackHelper.
Copyright (c) 2011-2015 Regents of the University of California.
void setPolicy(const std::string &policy)
Set the cache replacement policy for NFD&#39;s Content Store.
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 RemoveFaceCreateCallback(TypeId netDeviceType, FaceCreateCallback callback)
Remove callback to create and configure instance of the face, based on supplied Ptr<Node> and Ptr<Net...
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 disableForwarderStatusManager()
Disable Forwarder Status Manager.
std::string constructFaceUri(Ptr< NetDevice > netDevice)
Ptr< FaceContainer > InstallAll() const
Install Ndn stack on all nodes in the simulation.
Callback< shared_ptr< Face >, Ptr< Node >, Ptr< L3Protocol >, Ptr< NetDevice > > FaceCreateCallback
Copyright (c) 2011-2015 Regents of the University of California.
void AddFaceCreateCallback(TypeId netDeviceType, FaceCreateCallback callback)
Add callback to create and configure instance of the face, based on supplied Ptr<Node> and Ptr<NetDev...
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:36
Options that control the behavior of GenericLinkService.
void UpdateAll()
Update Ndn stack on all the nodes (Add faces for new devices)
static KeyChain & getKeyChain()
void UpdateFaceCreateCallback(TypeId netDeviceType, FaceCreateCallback callback)
Update callback to create and configure instance of the face, based on supplied Ptr<Node> and Ptr<Net...
void Update(Ptr< Node > node)
Update Ndn stack on a given node (Add faces for new devices)
ndn security v2 KeyChain
Definition: key-chain.cpp:76
void disableStrategyChoiceManager()
Disable Face Manager.
Base class for NDN content store.