A Discrete-Event Network Simulator
API
point-to-point-helper.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "ns3/abort.h"
22 #include "ns3/log.h"
23 #include "ns3/simulator.h"
24 #include "ns3/point-to-point-net-device.h"
25 #include "ns3/point-to-point-channel.h"
26 #include "ns3/point-to-point-remote-channel.h"
27 #include "ns3/queue.h"
28 #include "ns3/config.h"
29 #include "ns3/packet.h"
30 #include "ns3/names.h"
31 #include "ns3/mpi-interface.h"
32 #include "ns3/mpi-receiver.h"
33 
34 #include "ns3/trace-helper.h"
35 #include "point-to-point-helper.h"
36 
37 namespace ns3 {
38 
39 NS_LOG_COMPONENT_DEFINE ("PointToPointHelper");
40 
42 {
43  m_queueFactory.SetTypeId ("ns3::DropTailQueue<Packet>");
44  m_deviceFactory.SetTypeId ("ns3::PointToPointNetDevice");
45  m_channelFactory.SetTypeId ("ns3::PointToPointChannel");
46  m_remoteChannelFactory.SetTypeId ("ns3::PointToPointRemoteChannel");
47 }
48 
49 void
50 PointToPointHelper::SetQueue (std::string type,
51  std::string n1, const AttributeValue &v1,
52  std::string n2, const AttributeValue &v2,
53  std::string n3, const AttributeValue &v3,
54  std::string n4, const AttributeValue &v4)
55 {
57 
59  m_queueFactory.Set (n1, v1);
60  m_queueFactory.Set (n2, v2);
61  m_queueFactory.Set (n3, v3);
62  m_queueFactory.Set (n4, v4);
63 }
64 
65 void
67 {
68  m_deviceFactory.Set (n1, v1);
69 }
70 
71 void
73 {
74  m_channelFactory.Set (n1, v1);
75  m_remoteChannelFactory.Set (n1, v1);
76 }
77 
78 void
79 PointToPointHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
80 {
81  //
82  // All of the Pcap enable functions vector through here including the ones
83  // that are wandering through all of devices on perhaps all of the nodes in
84  // the system. We can only deal with devices of type PointToPointNetDevice.
85  //
86  Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
87  if (device == 0)
88  {
89  NS_LOG_INFO ("PointToPointHelper::EnablePcapInternal(): Device " << device << " not of type ns3::PointToPointNetDevice");
90  return;
91  }
92 
93  PcapHelper pcapHelper;
94 
95  std::string filename;
96  if (explicitFilename)
97  {
98  filename = prefix;
99  }
100  else
101  {
102  filename = pcapHelper.GetFilenameFromDevice (prefix, device);
103  }
104 
106  if(device->ShouldSniffPacketFromDevice()){
107  file = pcapHelper.CreateFile (filename, std::ios::out,
109  }else{
110  file = pcapHelper.CreateFile (filename, std::ios::out,
112  }
113  pcapHelper.HookDefaultSink<PointToPointNetDevice> (device, "PromiscSniffer", file);
114 }
115 
116 void
118  Ptr<OutputStreamWrapper> stream,
119  std::string prefix,
120  Ptr<NetDevice> nd,
121  bool explicitFilename)
122 {
123  //
124  // All of the ascii enable functions vector through here including the ones
125  // that are wandering through all of devices on perhaps all of the nodes in
126  // the system. We can only deal with devices of type PointToPointNetDevice.
127  //
128  Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
129  if (device == 0)
130  {
131  NS_LOG_INFO ("PointToPointHelper::EnableAsciiInternal(): Device " << device <<
132  " not of type ns3::PointToPointNetDevice");
133  return;
134  }
135 
136  //
137  // Our default trace sinks are going to use packet printing, so we have to
138  // make sure that is turned on.
139  //
141 
142  //
143  // If we are not provided an OutputStreamWrapper, we are expected to create
144  // one using the usual trace filename conventions and do a Hook*WithoutContext
145  // since there will be one file per context and therefore the context would
146  // be redundant.
147  //
148  if (stream == 0)
149  {
150  //
151  // Set up an output stream object to deal with private ofstream copy
152  // constructor and lifetime issues. Let the helper decide the actual
153  // name of the file given the prefix.
154  //
155  AsciiTraceHelper asciiTraceHelper;
156 
157  std::string filename;
158  if (explicitFilename)
159  {
160  filename = prefix;
161  }
162  else
163  {
164  filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
165  }
166 
167  Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
168 
169  //
170  // The MacRx trace source provides our "r" event.
171  //
172  asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<PointToPointNetDevice> (device, "MacRx", theStream);
173 
174  //
175  // The "+", '-', and 'd' events are driven by trace sources actually in the
176  // transmit queue.
177  //
178  Ptr<Queue<Packet> > queue = device->GetQueue ();
179  asciiTraceHelper.HookDefaultEnqueueSinkWithoutContext<Queue<Packet> > (queue, "Enqueue", theStream);
180  asciiTraceHelper.HookDefaultDropSinkWithoutContext<Queue<Packet> > (queue, "Drop", theStream);
181  asciiTraceHelper.HookDefaultDequeueSinkWithoutContext<Queue<Packet> > (queue, "Dequeue", theStream);
182 
183  // PhyRxDrop trace source for "d" event
184  asciiTraceHelper.HookDefaultDropSinkWithoutContext<PointToPointNetDevice> (device, "PhyRxDrop", theStream);
185 
186  return;
187  }
188 
189  //
190  // If we are provided an OutputStreamWrapper, we are expected to use it, and
191  // to providd a context. We are free to come up with our own context if we
192  // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
193  // compatibility and simplicity, we just use Config::Connect and let it deal
194  // with the context.
195  //
196  // Note that we are going to use the default trace sinks provided by the
197  // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
198  // but the default trace sinks are actually publicly available static
199  // functions that are always there waiting for just such a case.
200  //
201  uint32_t nodeid = nd->GetNode ()->GetId ();
202  uint32_t deviceid = nd->GetIfIndex ();
203  std::ostringstream oss;
204 
205  oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/MacRx";
207 
208  oss.str ("");
209  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue";
211 
212  oss.str ("");
213  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Dequeue";
215 
216  oss.str ("");
217  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Drop";
219 
220  oss.str ("");
221  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/PhyRxDrop";
223 }
224 
227 {
228  NS_ASSERT (c.GetN () == 2);
229  return Install (c.Get (0), c.Get (1));
230 }
231 
234 {
235  NetDeviceContainer container;
236 
238  devA->SetAddress (Mac48Address::Allocate ());
239  a->AddDevice (devA);
241  devA->SetQueue (queueA);
243  devB->SetAddress (Mac48Address::Allocate ());
244  b->AddDevice (devB);
246  devB->SetQueue (queueB);
247  // If MPI is enabled, we need to see if both nodes have the same system id
248  // (rank), and the rank is the same as this instance. If both are true,
249  //use a normal p2p channel, otherwise use a remote channel
250  bool useNormalChannel = true;
252 
254  {
255  uint32_t n1SystemId = a->GetSystemId ();
256  uint32_t n2SystemId = b->GetSystemId ();
257  uint32_t currSystemId = MpiInterface::GetSystemId ();
258  if (n1SystemId != currSystemId || n2SystemId != currSystemId)
259  {
260  useNormalChannel = false;
261  }
262  }
263  if (useNormalChannel)
264  {
266  }
267  else
268  {
270  Ptr<MpiReceiver> mpiRecA = CreateObject<MpiReceiver> ();
271  Ptr<MpiReceiver> mpiRecB = CreateObject<MpiReceiver> ();
272  mpiRecA->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devA));
273  mpiRecB->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devB));
274  devA->AggregateObject (mpiRecA);
275  devB->AggregateObject (mpiRecB);
276  }
277 
278  devA->Attach (channel);
279  devB->Attach (channel);
280  container.Add (devA);
281  container.Add (devB);
282 
283  return container;
284 }
285 
288 {
289  Ptr<Node> b = Names::Find<Node> (bName);
290  return Install (a, b);
291 }
292 
295 {
296  Ptr<Node> a = Names::Find<Node> (aName);
297  return Install (a, b);
298 }
299 
301 PointToPointHelper::Install (std::string aName, std::string bName)
302 {
303  Ptr<Node> a = Names::Find<Node> (aName);
304  Ptr<Node> b = Names::Find<Node> (bName);
305  return Install (a, b);
306 }
307 
308 } // namespace ns3
virtual void EnablePcapInternal(std::string prefix, Ptr< NetDevice > nd, bool promiscuous, bool explicitFilename)
Enable pcap output the indicated net device.
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
static void DefaultEnqueueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Enqueue default trace sink.
void HookDefaultDropSinkWithoutContext(Ptr< T > object, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default drop operation trace sink that does not accept nor log a trace con...
Definition: trace-helper.h:483
NetDeviceContainer Install(NodeContainer c)
Ptr< Queue< Packet > > GetQueue(void) const
Get a copy of the attached Queue.
Hold a value for an Attribute.
Definition: attribute.h:68
Manage pcap files for device models.
Definition: trace-helper.h:38
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1686
void SetQueue(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue())
Each point to point net device must have a queue to pass packets through.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
static void DefaultDropSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Drop default trace sink.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
ObjectFactory m_channelFactory
Channel Factory.
void HookDefaultEnqueueSinkWithoutContext(Ptr< T > object, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default enqueue operation trace sink that does not accept nor log a trace ...
Definition: trace-helper.h:461
std::string GetFilenameFromDevice(std::string prefix, Ptr< NetDevice > device, bool useObjectNames=true)
Let the ascii trace helper figure out a reasonable filename to use for an ascii trace file associated...
Ptr< PcapFileWrapper > CreateFile(std::string filename, std::ios::openmode filemode, DataLinkType dataLinkType, uint32_t snapLen=std::numeric_limits< uint32_t >::max(), int32_t tzCorrection=0)
Create and initialize a pcap file.
Definition: trace-helper.cc:49
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
static void AppendItemTypeIfNotPresent(std::string &typeId, const std::string &itemType)
Append the item type to the provided type ID if the latter does not end with &#39;>&#39;. ...
Definition: queue.cc:77
uint32_t GetSystemId(void) const
Definition: node.cc:121
channel
Definition: third.py:85
PointToPointHelper()
Create a PointToPointHelper to make life easier when creating point to point networks.
uint32_t GetN(void) const
Get the number of Ptr<Node> stored in this container.
std::string GetFilenameFromDevice(std::string prefix, Ptr< NetDevice > device, bool useObjectNames=true)
Let the pcap helper figure out a reasonable filename to use for a pcap file associated with a device...
Definition: trace-helper.cc:80
A Remote Point-To-Point Channel.
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:572
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
virtual void EnableAsciiInternal(Ptr< OutputStreamWrapper > stream, std::string prefix, Ptr< NetDevice > nd, bool explicitFilename)
Enable ascii trace output on the indicated net device.
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
static bool IsEnabled()
holds a vector of ns3::NetDevice pointers
A Device for a Point to Point Network Link.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
ObjectFactory m_deviceFactory
Device Factory.
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:843
static void DefaultReceiveSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Receive default trace sink.
static void DefaultDequeueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Dequeue default trace sink.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
void Set(std::string name, const AttributeValue &value)
Set an attribute to be set during construction.
ObjectFactory m_remoteChannelFactory
Remote Channel Factory.
void HookDefaultReceiveSinkWithoutContext(Ptr< T > object, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default receive operation trace sink that does not accept nor log a trace ...
Definition: trace-helper.h:527
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
Simple Point To Point Channel.
static uint32_t GetSystemId()
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
void Receive(Ptr< Packet > p)
Receive a packet from a connected PointToPointChannel.
ObjectFactory m_queueFactory
Queue Factory.
void HookDefaultSink(Ptr< T > object, std::string traceName, Ptr< PcapFileWrapper > file)
Hook a trace source to the default trace sink.
Definition: trace-helper.h:147
void HookDefaultDequeueSinkWithoutContext(Ptr< T > object, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default dequeue operation trace sink that does not accept nor log a trace ...
Definition: trace-helper.h:505