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/queue.h"
27 #include "ns3/net-device-queue-interface.h"
28 #include "ns3/config.h"
29 #include "ns3/packet.h"
30 #include "ns3/names.h"
31 
32 #ifdef NS3_MPI
33 #include "ns3/mpi-interface.h"
34 #include "ns3/mpi-receiver.h"
35 #include "ns3/point-to-point-remote-channel.h"
36 #endif
37 
38 #include "ns3/trace-helper.h"
39 #include "point-to-point-helper.h"
40 
41 namespace ns3 {
42 
43 NS_LOG_COMPONENT_DEFINE ("PointToPointHelper");
44 
46 {
47  m_queueFactory.SetTypeId ("ns3::DropTailQueue<Packet>");
48  m_deviceFactory.SetTypeId ("ns3::PointToPointNetDevice");
49  m_channelFactory.SetTypeId ("ns3::PointToPointChannel");
50  m_enableFlowControl = true;
51 }
52 
53 void
54 PointToPointHelper::SetQueue (std::string type,
55  std::string n1, const AttributeValue &v1,
56  std::string n2, const AttributeValue &v2,
57  std::string n3, const AttributeValue &v3,
58  std::string n4, const AttributeValue &v4)
59 {
61 
63  m_queueFactory.Set (n1, v1);
64  m_queueFactory.Set (n2, v2);
65  m_queueFactory.Set (n3, v3);
66  m_queueFactory.Set (n4, v4);
67 }
68 
69 void
71 {
72  m_deviceFactory.Set (n1, v1);
73 }
74 
75 void
77 {
78  m_channelFactory.Set (n1, v1);
79 }
80 
81 void
83 {
84  m_enableFlowControl = false;
85 }
86 
87 void
88 PointToPointHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
89 {
90  //
91  // All of the Pcap enable functions vector through here including the ones
92  // that are wandering through all of devices on perhaps all of the nodes in
93  // the system. We can only deal with devices of type PointToPointNetDevice.
94  //
96  if (device == 0)
97  {
98  NS_LOG_INFO ("PointToPointHelper::EnablePcapInternal(): Device " << device << " not of type ns3::PointToPointNetDevice");
99  return;
100  }
101 
102  PcapHelper pcapHelper;
103 
104  std::string filename;
105  if (explicitFilename)
106  {
107  filename = prefix;
108  }
109  else
110  {
111  filename = pcapHelper.GetFilenameFromDevice (prefix, device);
112  }
113 
114  Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out,
116  pcapHelper.HookDefaultSink<PointToPointNetDevice> (device, "PromiscSniffer", file);
117 }
118 
119 void
121  Ptr<OutputStreamWrapper> stream,
122  std::string prefix,
123  Ptr<NetDevice> nd,
124  bool explicitFilename)
125 {
126  //
127  // All of the ascii enable functions vector through here including the ones
128  // that are wandering through all of devices on perhaps all of the nodes in
129  // the system. We can only deal with devices of type PointToPointNetDevice.
130  //
132  if (device == 0)
133  {
134  NS_LOG_INFO ("PointToPointHelper::EnableAsciiInternal(): Device " << device <<
135  " not of type ns3::PointToPointNetDevice");
136  return;
137  }
138 
139  //
140  // Our default trace sinks are going to use packet printing, so we have to
141  // make sure that is turned on.
142  //
144 
145  //
146  // If we are not provided an OutputStreamWrapper, we are expected to create
147  // one using the usual trace filename conventions and do a Hook*WithoutContext
148  // since there will be one file per context and therefore the context would
149  // be redundant.
150  //
151  if (stream == 0)
152  {
153  //
154  // Set up an output stream object to deal with private ofstream copy
155  // constructor and lifetime issues. Let the helper decide the actual
156  // name of the file given the prefix.
157  //
158  AsciiTraceHelper asciiTraceHelper;
159 
160  std::string filename;
161  if (explicitFilename)
162  {
163  filename = prefix;
164  }
165  else
166  {
167  filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
168  }
169 
170  Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
171 
172  //
173  // The MacRx trace source provides our "r" event.
174  //
175  asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<PointToPointNetDevice> (device, "MacRx", theStream);
176 
177  //
178  // The "+", '-', and 'd' events are driven by trace sources actually in the
179  // transmit queue.
180  //
181  Ptr<Queue<Packet> > queue = device->GetQueue ();
182  asciiTraceHelper.HookDefaultEnqueueSinkWithoutContext<Queue<Packet> > (queue, "Enqueue", theStream);
183  asciiTraceHelper.HookDefaultDropSinkWithoutContext<Queue<Packet> > (queue, "Drop", theStream);
184  asciiTraceHelper.HookDefaultDequeueSinkWithoutContext<Queue<Packet> > (queue, "Dequeue", theStream);
185 
186  // PhyRxDrop trace source for "d" event
187  asciiTraceHelper.HookDefaultDropSinkWithoutContext<PointToPointNetDevice> (device, "PhyRxDrop", theStream);
188 
189  return;
190  }
191 
192  //
193  // If we are provided an OutputStreamWrapper, we are expected to use it, and
194  // to providd a context. We are free to come up with our own context if we
195  // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
196  // compatibility and simplicity, we just use Config::Connect and let it deal
197  // with the context.
198  //
199  // Note that we are going to use the default trace sinks provided by the
200  // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
201  // but the default trace sinks are actually publicly available static
202  // functions that are always there waiting for just such a case.
203  //
204  uint32_t nodeid = nd->GetNode ()->GetId ();
205  uint32_t deviceid = nd->GetIfIndex ();
206  std::ostringstream oss;
207 
208  oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/MacRx";
210 
211  oss.str ("");
212  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue";
214 
215  oss.str ("");
216  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Dequeue";
218 
219  oss.str ("");
220  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Drop";
222 
223  oss.str ("");
224  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/PhyRxDrop";
226 }
227 
230 {
231  NS_ASSERT (c.GetN () == 2);
232  return Install (c.Get (0), c.Get (1));
233 }
234 
237 {
238  NetDeviceContainer container;
239 
241  devA->SetAddress (Mac48Address::Allocate ());
242  a->AddDevice (devA);
244  devA->SetQueue (queueA);
246  devB->SetAddress (Mac48Address::Allocate ());
247  b->AddDevice (devB);
249  devB->SetQueue (queueB);
251  {
252  // Aggregate NetDeviceQueueInterface objects
253  Ptr<NetDeviceQueueInterface> ndqiA = CreateObject<NetDeviceQueueInterface> ();
254  ndqiA->GetTxQueue (0)->ConnectQueueTraces (queueA);
255  devA->AggregateObject (ndqiA);
256  Ptr<NetDeviceQueueInterface> ndqiB = CreateObject<NetDeviceQueueInterface> ();
257  ndqiB->GetTxQueue (0)->ConnectQueueTraces (queueB);
258  devB->AggregateObject (ndqiB);
259  }
260 
262 
263  // If MPI is enabled, we need to see if both nodes have the same system id
264  // (rank), and the rank is the same as this instance. If both are true,
265  // use a normal p2p channel, otherwise use a remote channel
266 #ifdef NS3_MPI
267  bool useNormalChannel = true;
269  {
270  uint32_t n1SystemId = a->GetSystemId ();
271  uint32_t n2SystemId = b->GetSystemId ();
272  uint32_t currSystemId = MpiInterface::GetSystemId ();
273  if (n1SystemId != currSystemId || n2SystemId != currSystemId)
274  {
275  useNormalChannel = false;
276  }
277  }
278  if (useNormalChannel)
279  {
280  m_channelFactory.SetTypeId ("ns3::PointToPointChannel");
282  }
283  else
284  {
285  m_channelFactory.SetTypeId ("ns3::PointToPointRemoteChannel");
287  Ptr<MpiReceiver> mpiRecA = CreateObject<MpiReceiver> ();
288  Ptr<MpiReceiver> mpiRecB = CreateObject<MpiReceiver> ();
289  mpiRecA->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devA));
290  mpiRecB->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devB));
291  devA->AggregateObject (mpiRecA);
292  devB->AggregateObject (mpiRecB);
293  }
294 #else
296 #endif
297 
298  devA->Attach (channel);
299  devB->Attach (channel);
300  container.Add (devA);
301  container.Add (devB);
302 
303  return container;
304 }
305 
308 {
309  Ptr<Node> b = Names::Find<Node> (bName);
310  return Install (a, b);
311 }
312 
315 {
316  Ptr<Node> a = Names::Find<Node> (aName);
317  return Install (a, b);
318 }
319 
321 PointToPointHelper::Install (std::string aName, std::string bName)
322 {
323  Ptr<Node> a = Names::Find<Node> (aName);
324  Ptr<Node> b = Names::Find<Node> (bName);
325  return Install (a, b);
326 }
327 
328 } // namespace ns3
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
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:484
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...
static void DefaultDropSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Drop default trace sink.
static void DefaultReceiveSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Receive default trace sink.
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 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:462
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:528
static void DefaultEnqueueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Enqueue default trace sink.
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:506
static void DefaultDequeueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Dequeue default trace sink.
Hold a value for an Attribute.
Definition: attribute.h:69
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
static bool IsEnabled()
Returns enabled state of parallel environment.
static uint32_t GetSystemId()
Get the id number of this rank.
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
virtual Ptr< Node > GetNode(void) const =0
virtual uint32_t GetIfIndex(void) const =0
keep track of a set of node pointers.
uint32_t GetN(void) const
Get the number of Ptr<Node> stored in this container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t GetId(void) const
Definition: node.cc:109
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
uint32_t GetSystemId(void) const
Definition: node.cc:123
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:572
Manage pcap files for device models.
Definition: trace-helper.h:39
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
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 HookDefaultSink(Ptr< T > object, std::string traceName, Ptr< PcapFileWrapper > file)
Hook a trace source to the default trace sink.
Definition: trace-helper.h:148
Simple Point To Point Channel.
ObjectFactory m_channelFactory
Channel Factory.
PointToPointHelper()
Create a PointToPointHelper to make life easier when creating point to point networks.
bool m_enableFlowControl
whether to enable flow control
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
virtual void EnableAsciiInternal(Ptr< OutputStreamWrapper > stream, std::string prefix, Ptr< NetDevice > nd, bool explicitFilename)
Enable ascii trace output on the indicated net device.
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.
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
ObjectFactory m_queueFactory
Queue Factory.
ObjectFactory m_deviceFactory
Device Factory.
NetDeviceContainer Install(NodeContainer c)
virtual void EnablePcapInternal(std::string prefix, Ptr< NetDevice > nd, bool promiscuous, bool explicitFilename)
Enable pcap output the indicated net device.
void DisableFlowControl(void)
Disable flow control only if you know what you are doing.
A Device for a Point to Point Network Link.
Ptr< Queue< Packet > > GetQueue(void) const
Get a copy of the attached Queue.
void Receive(Ptr< Packet > p)
Receive a packet from a connected PointToPointChannel.
A Remote Point-To-Point Channel.
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 '>'.
Definition: queue.cc:73
#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
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:920
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1709
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
channel
Definition: third.py:92