22 #include "ns3/assert.h"
24 #include "ns3/object.h"
25 #include "ns3/names.h"
26 #include "ns3/packet-socket-factory.h"
27 #include "ns3/config.h"
28 #include "ns3/simulator.h"
29 #include "ns3/string.h"
30 #include "ns3/net-device.h"
31 #include "ns3/channel.h"
32 #include "ns3/callback.h"
34 #include "ns3/core-config.h"
35 #include "ns3/point-to-point-net-device.h"
36 #include "ns3/point-to-point-helper.h"
37 #include "ns3/callback.h"
39 #include "../model/ndn-net-device-face.h"
40 #include "../model/ndn-l3-protocol.h"
42 #include "ns3/ndn-forwarding-strategy.h"
43 #include "ns3/ndn-fib.h"
44 #include "ns3/ndn-pit.h"
45 #include "ns3/ndn-name.h"
46 #include "ns3/ndn-content-store.h"
48 #include "ns3/node-list.h"
51 #include "ns3/data-rate.h"
53 #include "ndn-face-container.h"
54 #include "ndn-stack-helper.h"
58 #include <boost/foreach.hpp>
59 #include <boost/lexical_cast.hpp>
61 NS_LOG_COMPONENT_DEFINE (
"ndn.StackHelper");
67 : m_limitsEnabled (false)
68 , m_needSetDefaultRoutes (false)
70 m_ndnFactory. SetTypeId (
"ns3::ndn::L3Protocol");
71 m_strategyFactory. SetTypeId (
"ns3::ndn::fw::Flooding");
72 m_contentStoreFactory.SetTypeId (
"ns3::ndn::cs::Lru");
73 m_fibFactory. SetTypeId (
"ns3::ndn::fib::Default");
74 m_pitFactory. SetTypeId (
"ns3::ndn::pit::Persistent");
76 m_netDeviceCallbacks.push_back (std::make_pair (PointToPointNetDevice::GetTypeId (), MakeCallback (&StackHelper::PointToPointNetDeviceCallback,
this)));
86 const std::string &attr2,
const std::string &value2,
87 const std::string &attr3,
const std::string &value3,
88 const std::string &attr4,
const std::string &value4)
91 m_ndnFactory.Set (attr1, StringValue (value1));
93 m_ndnFactory.Set (attr2, StringValue (value2));
95 m_ndnFactory.Set (attr3, StringValue (value3));
97 m_ndnFactory.Set (attr4, StringValue (value4));
102 const std::string &attr1,
const std::string &value1,
103 const std::string &attr2,
const std::string &value2,
104 const std::string &attr3,
const std::string &value3,
105 const std::string &attr4,
const std::string &value4)
107 m_strategyFactory.SetTypeId (strategy);
109 m_strategyFactory.Set (attr1, StringValue (value1));
111 m_strategyFactory.Set (attr2, StringValue (value2));
113 m_strategyFactory.Set (attr3, StringValue (value3));
115 m_strategyFactory.Set (attr4, StringValue (value4));
120 const std::string &attr1,
const std::string &value1,
121 const std::string &attr2,
const std::string &value2,
122 const std::string &attr3,
const std::string &value3,
123 const std::string &attr4,
const std::string &value4)
125 m_contentStoreFactory.SetTypeId (contentStore);
127 m_contentStoreFactory.Set (attr1, StringValue (value1));
129 m_contentStoreFactory.Set (attr2, StringValue (value2));
131 m_contentStoreFactory.Set (attr3, StringValue (value3));
133 m_contentStoreFactory.Set (attr4, StringValue (value4));
138 const std::string &attr1,
const std::string &value1,
139 const std::string &attr2,
const std::string &value2,
140 const std::string &attr3,
const std::string &value3,
141 const std::string &attr4,
const std::string &value4)
143 m_pitFactory.SetTypeId (pitClass);
145 m_pitFactory.Set (attr1, StringValue (value1));
147 m_pitFactory.Set (attr2, StringValue (value2));
149 m_pitFactory.Set (attr3, StringValue (value3));
151 m_pitFactory.Set (attr4, StringValue (value4));
156 const std::string &attr1,
const std::string &value1,
157 const std::string &attr2,
const std::string &value2,
158 const std::string &attr3,
const std::string &value3,
159 const std::string &attr4,
const std::string &value4)
161 m_fibFactory.SetTypeId (fibClass);
163 m_fibFactory.Set (attr1, StringValue (value1));
165 m_fibFactory.Set (attr2, StringValue (value2));
167 m_fibFactory.Set (attr3, StringValue (value3));
169 m_fibFactory.Set (attr4, StringValue (value4));
175 NS_LOG_FUNCTION (
this << needSet);
176 m_needSetDefaultRoutes = needSet;
183 uint32_t avgInterest)
185 NS_LOG_INFO (
"EnableLimits: " << enable);
186 m_limitsEnabled = enable;
188 m_avgDataSize = avgData;
189 m_avgInterestSize = avgInterest;
195 Ptr<FaceContainer> faces = Create<FaceContainer> ();
196 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
206 return Install (NodeContainer::GetGlobal ());
213 Ptr<FaceContainer> faces = Create<FaceContainer> ();
217 NS_FATAL_ERROR (
"StackHelper::Install (): Installing "
218 "a NdnStack to a node with an existing Ndn object");
223 Ptr<L3Protocol> ndn = m_ndnFactory.Create<
L3Protocol> ();
226 Ptr<Fib> fib = m_fibFactory.Create<
Fib> ();
227 ndn->AggregateObject (fib);
230 ndn->AggregateObject (m_pitFactory.Create<
Pit> ());
236 ndn->AggregateObject (m_contentStoreFactory.Create<
ContentStore> ());
239 node->AggregateObject (ndn);
241 for (uint32_t index=0; index < node->GetNDevices (); index++)
243 Ptr<NetDevice> device = node->GetDevice (index);
249 Ptr<NetDeviceFace> face;
251 for (std::list< std::pair<TypeId, NetDeviceFaceCreateCallback> >::const_iterator item = m_netDeviceCallbacks.begin ();
252 item != m_netDeviceCallbacks.end ();
255 if (device->GetInstanceTypeId () == item->first ||
256 device->GetInstanceTypeId ().IsChildOf (item->first))
258 face = item->second (node, ndn, device);
265 face = DefaultNetDeviceCallback (node, ndn, device);
268 if (m_needSetDefaultRoutes)
271 AddRoute (node,
"/", StaticCast<Face> (face), std::numeric_limits<int32_t>::max ());
284 m_netDeviceCallbacks.push_back (std::make_pair (netDeviceType, callback));
290 for (NetDeviceCallbackList::iterator i = m_netDeviceCallbacks.begin (); i != m_netDeviceCallbacks.end (); i++)
292 if (i->first == netDeviceType)
294 i->second = callback;
303 for (NetDeviceCallbackList::iterator i = m_netDeviceCallbacks.begin (); i != m_netDeviceCallbacks.end (); i++)
305 if (i->first == netDeviceType)
307 m_netDeviceCallbacks.erase (i);
314 StackHelper::DefaultNetDeviceCallback (Ptr<Node> node, Ptr<L3Protocol> ndn, Ptr<NetDevice> netDevice)
const
316 NS_LOG_DEBUG (
"Creating default NetDeviceFace on node " << node->GetId ());
318 Ptr<NetDeviceFace> face = CreateObject<NetDeviceFace> (node, netDevice);
321 NS_LOG_LOGIC (
"Node " << node->GetId () <<
": added NetDeviceFace as face #" << *face);
327 StackHelper::PointToPointNetDeviceCallback (Ptr<Node> node, Ptr<L3Protocol> ndn, Ptr<NetDevice> device)
const
329 NS_LOG_DEBUG (
"Creating point-to-point NetDeviceFace on node " << node->GetId ());
331 Ptr<NetDeviceFace> face = CreateObject<NetDeviceFace> (node, device);
334 NS_LOG_LOGIC (
"Node " << node->GetId () <<
": added NetDeviceFace as face #" << *face);
338 Ptr<Limits> limits = face->GetObject<Limits> ();
341 NS_FATAL_ERROR (
"Limits are enabled, but the selected forwarding strategy does not support limits. Please revise your scenario");
345 NS_LOG_INFO (
"Limits are enabled");
346 Ptr<PointToPointNetDevice> p2p = DynamicCast<PointToPointNetDevice> (device);
353 DataRateValue dataRate; device->GetAttribute (
"DataRate", dataRate);
354 TimeValue linkDelay; device->GetChannel ()->GetAttribute (
"Delay", linkDelay);
356 NS_LOG_INFO(
"DataRate for this link is " << dataRate.Get());
358 double maxInterestPackets = 1.0 * dataRate.Get ().GetBitRate () / 8.0 / (m_avgDataSize + m_avgInterestSize);
361 NS_LOG_INFO (
"MaxLimit: " << (
int)(m_avgRtt.ToDouble (Time::S) * maxInterestPackets));
364 limits->SetLimits (maxInterestPackets, m_avgRtt.ToDouble (Time::S));
365 limits->SetLinkDelay (linkDelay.Get ().ToDouble (Time::S));
376 Ptr<Node> node = Names::Find<Node> (nodeName);
384 NS_LOG_LOGIC (
"[" << node->GetId () <<
"]$ route add " << prefix <<
" via " << *face <<
" metric " << metric);
386 Ptr<Fib> fib = node->GetObject<
Fib> ();
388 NameValue prefixValue;
389 prefixValue.DeserializeFromString (prefix, MakeNameChecker ());
390 fib->
Add (prefixValue.Get (), face, metric);
396 Ptr<L3Protocol> ndn = node->GetObject<
L3Protocol> ();
397 NS_ASSERT_MSG (ndn != 0,
"Ndn stack should be installed on the node");
399 Ptr<Face> face = ndn->GetFace (faceId);
400 NS_ASSERT_MSG (face != 0,
"Face with ID [" << faceId <<
"] does not exist on node [" << node->GetId () <<
"]");
402 AddRoute (node, prefix, face, metric);
408 Ptr<Node> node = Names::Find<Node> (nodeName);
409 NS_ASSERT_MSG (node != 0,
"Node [" << nodeName <<
"] does not exist");
411 Ptr<L3Protocol> ndn = node->GetObject<
L3Protocol> ();
412 NS_ASSERT_MSG (ndn != 0,
"Ndn stack should be installed on the node");
414 Ptr<Face> face = ndn->GetFace (faceId);
415 NS_ASSERT_MSG (face != 0,
"Face with ID [" << faceId <<
"] does not exist on node [" << nodeName <<
"]");
417 AddRoute (node, prefix, face, metric);
423 for (uint32_t deviceId = 0; deviceId < node->GetNDevices (); deviceId ++)
425 Ptr<PointToPointNetDevice> netDevice = DynamicCast<PointToPointNetDevice> (node->GetDevice (deviceId));
429 Ptr<Channel> channel = netDevice->GetChannel ();
433 if (channel->GetDevice (0)->GetNode () == otherNode ||
434 channel->GetDevice (1)->GetNode () == otherNode)
436 Ptr<L3Protocol> ndn = node->GetObject<
L3Protocol> ();
437 NS_ASSERT_MSG (ndn != 0,
"Ndn stack should be installed on the node");
439 Ptr<Face> face = ndn->GetFaceByNetDevice (netDevice);
440 NS_ASSERT_MSG (face != 0,
"There is no face associated with the p2p link");
442 AddRoute (node, prefix, face, metric);
448 NS_FATAL_ERROR (
"Cannot add route: Node# " << node->GetId () <<
" and Node# " << otherNode->GetId () <<
" are not connected");
452 StackHelper::AddRoute (
const std::string &nodeName,
const std::string &prefix,
const std::string &otherNodeName, int32_t metric)
454 Ptr<Node> node = Names::Find<Node> (nodeName);
455 NS_ASSERT_MSG (node != 0,
"Node [" << nodeName <<
"] does not exist");
457 Ptr<Node> otherNode = Names::Find<Node> (otherNodeName);
458 NS_ASSERT_MSG (otherNode != 0,
"Node [" << otherNodeName <<
"] does not exist");
460 AddRoute (node, prefix, otherNode, metric);
Base class for NDN content store.
virtual Ptr< fib::Entry > Add(const Name &prefix, Ptr< Face > face, int32_t metric)=0
Add or update FIB entry.
Class implementing Pending Interests Table.
void SetDefaultRoutes(bool needSet)
Set flag indicating necessity to install default routes in FIB.
StackHelper()
Create a new NdnStackHelper with a default NDN_FLOODING forwarding stategy.
void RemoveNetDeviceFaceCreateCallback(TypeId netDeviceType, NetDeviceFaceCreateCallback callback)
Remove callback to create and configure instance of the face, based on supplied Ptr
and Ptr
Implementation network-layer of NDN stack.
void SetContentStore(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 content store class and its attributes.
Ptr< FaceContainer > InstallAll() const
Install Ndn stack on all nodes in the simulation.
void UpdateNetDeviceFaceCreateCallback(TypeId netDeviceType, NetDeviceFaceCreateCallback callback)
Update callback to create and configure instance of the face, based on supplied Ptr
and Ptr
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 AddNetDeviceFaceCreateCallback(TypeId netDeviceType, NetDeviceFaceCreateCallback callback)
Add callback to create and configure instance of the face, based on supplied Ptr
and Ptr
Class implementing FIB functionality.
virtual ~StackHelper()
Destroy the NdnStackHelper.
static void AddRoute(const std::string &nodeName, const std::string &prefix, uint32_t faceId, int32_t metric)
Add forwarding entry to FIB.
void EnableLimits(bool enable=true, Time avgRtt=Seconds(0.1), uint32_t avgData=1100, uint32_t avgInterest=40)
Enable Interest limits (disabled by default)
void SetForwardingStrategy(const std::string &forwardingStrategyClass, 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 forwarding strategy class and its attributes.
void SetPit(const std::string &pitClass, 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 PIT class and its attributes.
Ptr< FaceContainer > Install(const std::string &nodeName) const
Install Ndn stack on the node.
Abstract base class for Ndn forwarding strategies.
void SetFib(const std::string &fibClass, 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 FIB class and its attributes.