NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 {
47 
48  m_ndnFactory.SetTypeId("ns3::ndn::L3Protocol");
49  m_contentStoreFactory.SetTypeId("ns3::ndn::cs::Lru");
50 
51  m_netDeviceCallbacks.push_back(
52  std::make_pair(PointToPointNetDevice::GetTypeId(),
53  MakeCallback(&StackHelper::PointToPointNetDeviceCallback, this)));
54  // default callback will be fired if non of others callbacks fit or did the job
55 }
56 
58 {
59 }
60 
61 KeyChain&
63 {
64  static ::ndn::KeyChain keyChain("pib-dummy", "tpm-dummy");
65  return keyChain;
66 }
67 
68 void
70 {
71  ::ndn::time::setCustomClocks(make_shared<ns3::ndn::time::CustomSteadyClock>(),
72  make_shared<ns3::ndn::time::CustomSystemClock>());
73 }
74 
75 void
77 {
78  NS_LOG_FUNCTION(this << needSet);
79  m_needSetDefaultRoutes = needSet;
80 }
81 
82 void
83 StackHelper::SetStackAttributes(const std::string& attr1, const std::string& value1,
84  const std::string& attr2, const std::string& value2,
85  const std::string& attr3, const std::string& value3,
86  const std::string& attr4, const std::string& value4)
87 {
88  if (attr1 != "")
89  m_ndnFactory.Set(attr1, StringValue(value1));
90  if (attr2 != "")
91  m_ndnFactory.Set(attr2, StringValue(value2));
92  if (attr3 != "")
93  m_ndnFactory.Set(attr3, StringValue(value3));
94  if (attr4 != "")
95  m_ndnFactory.Set(attr4, StringValue(value4));
96 }
97 
98 void
99 StackHelper::SetOldContentStore(const std::string& contentStore, const std::string& attr1,
100  const std::string& value1, const std::string& attr2,
101  const std::string& value2, const std::string& attr3,
102  const std::string& value3, const std::string& attr4,
103  const std::string& value4)
104 {
105  m_maxCsSize = 0;
106 
107  m_contentStoreFactory.SetTypeId(contentStore);
108  if (attr1 != "")
109  m_contentStoreFactory.Set(attr1, StringValue(value1));
110  if (attr2 != "")
111  m_contentStoreFactory.Set(attr2, StringValue(value2));
112  if (attr3 != "")
113  m_contentStoreFactory.Set(attr3, StringValue(value3));
114  if (attr4 != "")
115  m_contentStoreFactory.Set(attr4, StringValue(value4));
116 }
117 
118 void
119 StackHelper::setCsSize(size_t maxSize)
120 {
121  m_maxCsSize = maxSize;
122 }
123 
124 Ptr<FaceContainer>
125 StackHelper::Install(const NodeContainer& c) const
126 {
127  Ptr<FaceContainer> faces = Create<FaceContainer>();
128  for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i) {
129  faces->AddAll(Install(*i));
130  }
131  return faces;
132 }
133 
134 Ptr<FaceContainer>
136 {
137  return Install(NodeContainer::GetGlobal());
138 }
139 
140 Ptr<FaceContainer>
141 StackHelper::Install(Ptr<Node> node) const
142 {
143  Ptr<FaceContainer> faces = Create<FaceContainer>();
144 
145  if (node->GetObject<L3Protocol>() != 0) {
146  NS_FATAL_ERROR("StackHelper::Install (): Installing "
147  "a NdnStack to a node with an existing Ndn object");
148  return 0;
149  }
150 
151  Ptr<L3Protocol> ndn = m_ndnFactory.Create<L3Protocol>();
152 
153  ndn->getConfig().put("tables.cs_max_packets", (m_maxCsSize == 0) ? 1 : m_maxCsSize);
154 
155  // NFD initialization
156  ndn->initialize();
157 
158  // Create and aggregate content store if NFD's contest store has been disabled
159  if (m_maxCsSize == 0) {
160  ndn->AggregateObject(m_contentStoreFactory.Create<ContentStore>());
161  }
162 
163  // Aggregate L3Protocol on node (must be after setting ndnSIM CS)
164  node->AggregateObject(ndn);
165 
166  for (uint32_t index = 0; index < node->GetNDevices(); index++) {
167  Ptr<NetDevice> device = node->GetDevice(index);
168  // This check does not make sense: LoopbackNetDevice is installed only if IP stack is installed,
169  // Normally, ndnSIM works without IP stack, so no reason to check
170  // if (DynamicCast<LoopbackNetDevice> (device) != 0)
171  // continue; // don't create face for a LoopbackNetDevice
172 
173  shared_ptr<NetDeviceFace> face;
174 
175  for (const auto& item : m_netDeviceCallbacks) {
176  if (device->GetInstanceTypeId() == item.first ||
177  device->GetInstanceTypeId().IsChildOf(item.first)) {
178  face = item.second(node, ndn, device);
179  if (face != 0)
180  break;
181  }
182  }
183 
184  if (face == 0) {
185  face = DefaultNetDeviceCallback(node, ndn, device);
186  }
187 
188  if (m_needSetDefaultRoutes) {
189  // default route with lowest priority possible
190  FibHelper::AddRoute(node, "/", face, std::numeric_limits<int32_t>::max());
191  }
192 
193  faces->Add(face);
194  }
195 
196  return faces;
197 }
198 
199 void
202 {
203  m_netDeviceCallbacks.push_back(std::make_pair(netDeviceType, callback));
204 }
205 
206 void
209 {
210  for (auto& i : m_netDeviceCallbacks) {
211  if (i.first == netDeviceType) {
212  i.second = callback;
213  return;
214  }
215  }
216 }
217 
218 void
221 {
222  m_netDeviceCallbacks.remove_if([&] (const std::pair<TypeId, NetDeviceFaceCreateCallback>& i) {
223  return (i.first == netDeviceType);
224  });
225 }
226 
227 shared_ptr<NetDeviceFace>
228 StackHelper::DefaultNetDeviceCallback(Ptr<Node> node, Ptr<L3Protocol> ndn,
229  Ptr<NetDevice> netDevice) const
230 {
231  NS_LOG_DEBUG("Creating default NetDeviceFace on node " << node->GetId());
232 
233  shared_ptr<NetDeviceFace> face = std::make_shared<NetDeviceFace>(node, netDevice);
234 
235  ndn->addFace(face);
236  NS_LOG_LOGIC("Node " << node->GetId() << ": added NetDeviceFace as face #"
237  << face->getLocalUri());
238 
239  return face;
240 }
241 
242 shared_ptr<NetDeviceFace>
243 StackHelper::PointToPointNetDeviceCallback(Ptr<Node> node, Ptr<L3Protocol> ndn,
244  Ptr<NetDevice> device) const
245 {
246  NS_LOG_DEBUG("Creating point-to-point NetDeviceFace on node " << node->GetId());
247 
248  shared_ptr<NetDeviceFace> face = std::make_shared<NetDeviceFace>(node, device);
249 
250  ndn->addFace(face);
251  NS_LOG_LOGIC("Node " << node->GetId() << ": added NetDeviceFace as face #"
252  << face->getLocalUri());
253 
254  return face;
255 }
256 
257 Ptr<FaceContainer>
258 StackHelper::Install(const std::string& nodeName) const
259 {
260  Ptr<Node> node = Names::Find<Node>(nodeName);
261  return Install(node);
262 }
263 
264 } // namespace ndn
265 } // namespace ns3
void AddNetDeviceFaceCreateCallback(TypeId netDeviceType, NetDeviceFaceCreateCallback callback)
Add callback to create and configure instance of the face, based on supplied Ptr and Ptr
void UpdateNetDeviceFaceCreateCallback(TypeId netDeviceType, NetDeviceFaceCreateCallback callback)
Update callback to create and configure instance of the face, based on supplied Ptr and Ptr
virtual ~StackHelper()
Destroy the NdnStackHelper.
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'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.
void RemoveNetDeviceFaceCreateCallback(TypeId netDeviceType, NetDeviceFaceCreateCallback callback)
Remove callback to create and configure instance of the face, based on supplied Ptr and Ptr
Callback< shared_ptr< NetDeviceFace >, Ptr< Node >, Ptr< L3Protocol >, Ptr< NetDevice > > NetDeviceFaceCreateCallback
Ptr< FaceContainer > InstallAll() const
Install Ndn stack on all nodes in the simulation.
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.
static KeyChain & getKeyChain()
static void AddRoute(const std::string &nodeName, const Name &prefix, uint32_t faceId, int32_t metric)
Add forwarding entry to FIB.
Base class for NDN content store.