A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("PointToPointHelper");
38 
39 namespace ns3 {
40 
42 {
43  m_queueFactory.SetTypeId ("ns3::DropTailQueue");
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  m_queueFactory.Set (n1, v1);
58  m_queueFactory.Set (n2, v2);
59  m_queueFactory.Set (n3, v3);
60  m_queueFactory.Set (n4, v4);
61 }
62 
63 void
65 {
66  m_deviceFactory.Set (n1, v1);
67 }
68 
69 void
71 {
72  m_channelFactory.Set (n1, v1);
73  m_remoteChannelFactory.Set (n1, v1);
74 }
75 
76 void
77 PointToPointHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
78 {
79  //
80  // All of the Pcap enable functions vector through here including the ones
81  // that are wandering through all of devices on perhaps all of the nodes in
82  // the system. We can only deal with devices of type PointToPointNetDevice.
83  //
84  Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
85  if (device == 0)
86  {
87  NS_LOG_INFO ("PointToPointHelper::EnablePcapInternal(): Device " << device << " not of type ns3::PointToPointNetDevice");
88  return;
89  }
90 
91  PcapHelper pcapHelper;
92 
93  std::string filename;
94  if (explicitFilename)
95  {
96  filename = prefix;
97  }
98  else
99  {
100  filename = pcapHelper.GetFilenameFromDevice (prefix, device);
101  }
102 
103  Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out,
105  pcapHelper.HookDefaultSink<PointToPointNetDevice> (device, "PromiscSniffer", file);
106 }
107 
108 void
110  Ptr<OutputStreamWrapper> stream,
111  std::string prefix,
112  Ptr<NetDevice> nd,
113  bool explicitFilename)
114 {
115  //
116  // All of the ascii enable functions vector through here including the ones
117  // that are wandering through all of devices on perhaps all of the nodes in
118  // the system. We can only deal with devices of type PointToPointNetDevice.
119  //
120  Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
121  if (device == 0)
122  {
123  NS_LOG_INFO ("PointToPointHelper::EnableAsciiInternal(): Device " << device <<
124  " not of type ns3::PointToPointNetDevice");
125  return;
126  }
127 
128  //
129  // Our default trace sinks are going to use packet printing, so we have to
130  // make sure that is turned on.
131  //
133 
134  //
135  // If we are not provided an OutputStreamWrapper, we are expected to create
136  // one using the usual trace filename conventions and do a Hook*WithoutContext
137  // since there will be one file per context and therefore the context would
138  // be redundant.
139  //
140  if (stream == 0)
141  {
142  //
143  // Set up an output stream object to deal with private ofstream copy
144  // constructor and lifetime issues. Let the helper decide the actual
145  // name of the file given the prefix.
146  //
147  AsciiTraceHelper asciiTraceHelper;
148 
149  std::string filename;
150  if (explicitFilename)
151  {
152  filename = prefix;
153  }
154  else
155  {
156  filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
157  }
158 
159  Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
160 
161  //
162  // The MacRx trace source provides our "r" event.
163  //
164  asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<PointToPointNetDevice> (device, "MacRx", theStream);
165 
166  //
167  // The "+", '-', and 'd' events are driven by trace sources actually in the
168  // transmit queue.
169  //
170  Ptr<Queue> queue = device->GetQueue ();
171  asciiTraceHelper.HookDefaultEnqueueSinkWithoutContext<Queue> (queue, "Enqueue", theStream);
172  asciiTraceHelper.HookDefaultDropSinkWithoutContext<Queue> (queue, "Drop", theStream);
173  asciiTraceHelper.HookDefaultDequeueSinkWithoutContext<Queue> (queue, "Dequeue", theStream);
174 
175  // PhyRxDrop trace source for "d" event
176  asciiTraceHelper.HookDefaultDropSinkWithoutContext<PointToPointNetDevice> (device, "PhyRxDrop", theStream);
177 
178  return;
179  }
180 
181  //
182  // If we are provided an OutputStreamWrapper, we are expected to use it, and
183  // to providd a context. We are free to come up with our own context if we
184  // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
185  // compatibility and simplicity, we just use Config::Connect and let it deal
186  // with the context.
187  //
188  // Note that we are going to use the default trace sinks provided by the
189  // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
190  // but the default trace sinks are actually publicly available static
191  // functions that are always there waiting for just such a case.
192  //
193  uint32_t nodeid = nd->GetNode ()->GetId ();
194  uint32_t deviceid = nd->GetIfIndex ();
195  std::ostringstream oss;
196 
197  oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/MacRx";
199 
200  oss.str ("");
201  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue";
203 
204  oss.str ("");
205  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Dequeue";
207 
208  oss.str ("");
209  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Drop";
211 
212  oss.str ("");
213  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/PhyRxDrop";
215 }
216 
219 {
220  NS_ASSERT (c.GetN () == 2);
221  return Install (c.Get (0), c.Get (1));
222 }
223 
226 {
227  NetDeviceContainer container;
228 
230  devA->SetAddress (Mac48Address::Allocate ());
231  a->AddDevice (devA);
232  Ptr<Queue> queueA = m_queueFactory.Create<Queue> ();
233  devA->SetQueue (queueA);
235  devB->SetAddress (Mac48Address::Allocate ());
236  b->AddDevice (devB);
237  Ptr<Queue> queueB = m_queueFactory.Create<Queue> ();
238  devB->SetQueue (queueB);
239  // If MPI is enabled, we need to see if both nodes have the same system id
240  // (rank), and the rank is the same as this instance. If both are true,
241  //use a normal p2p channel, otherwise use a remote channel
242  bool useNormalChannel = true;
243  Ptr<PointToPointChannel> channel = 0;
244 
246  {
247  uint32_t n1SystemId = a->GetSystemId ();
248  uint32_t n2SystemId = b->GetSystemId ();
249  uint32_t currSystemId = MpiInterface::GetSystemId ();
250  if (n1SystemId != currSystemId || n2SystemId != currSystemId)
251  {
252  useNormalChannel = false;
253  }
254  }
255  if (useNormalChannel)
256  {
258  }
259  else
260  {
262  Ptr<MpiReceiver> mpiRecA = CreateObject<MpiReceiver> ();
263  Ptr<MpiReceiver> mpiRecB = CreateObject<MpiReceiver> ();
264  mpiRecA->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devA));
265  mpiRecB->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devB));
266  devA->AggregateObject (mpiRecA);
267  devB->AggregateObject (mpiRecB);
268  }
269 
270  devA->Attach (channel);
271  devB->Attach (channel);
272  container.Add (devA);
273  container.Add (devB);
274 
275  return container;
276 }
277 
280 {
281  Ptr<Node> b = Names::Find<Node> (bName);
282  return Install (a, b);
283 }
284 
287 {
288  Ptr<Node> a = Names::Find<Node> (aName);
289  return Install (a, b);
290 }
291 
293 PointToPointHelper::Install (std::string aName, std::string bName)
294 {
295  Ptr<Node> a = Names::Find<Node> (aName);
296  Ptr<Node> b = Names::Find<Node> (bName);
297  return Install (a, b);
298 }
299 
300 } // 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:128
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
Ptr< Queue > GetQueue(void) const
Get a copy of the attached Queue.
static void DefaultEnqueueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Ptr< PcapFileWrapper > CreateFile(std::string filename, std::ios::openmode filemode, uint32_t dataLinkType, uint32_t snapLen=65535, int32_t tzCorrection=0)
Create and initialize a pcap file.
Definition: trace-helper.cc:49
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:321
NetDeviceContainer Install(NodeContainer c)
Hold a value for an Attribute.
Definition: attribute.h:56
Manage pcap files for device models.
Definition: trace-helper.h:38
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Build bound Callbacks which take varying numbers of arguments, and potentially returning a value...
Definition: callback.h:1463
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)
Definition: assert.h:64
static void DefaultDropSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
void SetTypeId(TypeId tid)
#define NS_LOG_INFO(msg)
Definition: log.h:298
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits. ...
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
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:299
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...
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
uint32_t GetSystemId(void) const
Definition: node.cc:111
Abstract base class for packet Queues.
Definition: queue.h:45
PointToPointHelper()
Create a PointToPointHelper to make life easier when creating point to point networks.
uint32_t GetN(void) const
Get the number of Ptr stored in this container.
NS_LOG_COMPONENT_DEFINE("PointToPointHelper")
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
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
static void EnablePrinting(void)
By default, packets do not keep around enough metadata to perform the operations requested by the Pri...
Definition: packet.cc:552
Ptr< Object > Create(void) const
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:1238
static void DefaultReceiveSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
static void DefaultDequeueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
keep track of a set of node pointers.
void Set(std::string name, const AttributeValue &value)
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:365
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)
Definition: node.cc:118
Simple Point To Point Channel.
static uint32_t GetSystemId()
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
void Receive(Ptr< Packet > p)
Receive a packet from a connected PointToPointChannel.
void HookDefaultSink(Ptr< T > object, std::string traceName, Ptr< PcapFileWrapper > file)
Hook a trace source to the default trace sink.
Definition: trace-helper.h:114
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:343