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/net-device-queue-interface.h"
27#include "ns3/config.h"
28#include "ns3/packet.h"
29#include "ns3/names.h"
30
31#ifdef NS3_MPI
32#include "ns3/mpi-interface.h"
33#include "ns3/mpi-receiver.h"
34#include "ns3/point-to-point-remote-channel.h"
35#endif
36
37#include "ns3/trace-helper.h"
39
40namespace ns3 {
41
42NS_LOG_COMPONENT_DEFINE ("PointToPointHelper");
43
45{
46 m_queueFactory.SetTypeId ("ns3::DropTailQueue<Packet>");
47 m_deviceFactory.SetTypeId ("ns3::PointToPointNetDevice");
48 m_channelFactory.SetTypeId ("ns3::PointToPointChannel");
50}
51
52void
54{
55 m_deviceFactory.Set (n1, v1);
56}
57
58void
60{
61 m_channelFactory.Set (n1, v1);
62}
63
64void
66{
67 m_enableFlowControl = false;
68}
69
70void
71PointToPointHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
72{
73 //
74 // All of the Pcap enable functions vector through here including the ones
75 // that are wandering through all of devices on perhaps all of the nodes in
76 // the system. We can only deal with devices of type PointToPointNetDevice.
77 //
78 Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
79 if (!device)
80 {
81 NS_LOG_INFO ("PointToPointHelper::EnablePcapInternal(): Device " << device << " not of type ns3::PointToPointNetDevice");
82 return;
83 }
84
85 PcapHelper pcapHelper;
86
87 std::string filename;
88 if (explicitFilename)
89 {
90 filename = prefix;
91 }
92 else
93 {
94 filename = pcapHelper.GetFilenameFromDevice (prefix, device);
95 }
96
97 Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out,
99 pcapHelper.HookDefaultSink<PointToPointNetDevice> (device, "PromiscSniffer", file);
100}
101
102void
105 std::string prefix,
107 bool explicitFilename)
108{
109 //
110 // All of the ascii enable functions vector through here including the ones
111 // that are wandering through all of devices on perhaps all of the nodes in
112 // the system. We can only deal with devices of type PointToPointNetDevice.
113 //
114 Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
115 if (!device)
116 {
117 NS_LOG_INFO ("PointToPointHelper::EnableAsciiInternal(): Device " << device <<
118 " not of type ns3::PointToPointNetDevice");
119 return;
120 }
121
122 //
123 // Our default trace sinks are going to use packet printing, so we have to
124 // make sure that is turned on.
125 //
127
128 //
129 // If we are not provided an OutputStreamWrapper, we are expected to create
130 // one using the usual trace filename conventions and do a Hook*WithoutContext
131 // since there will be one file per context and therefore the context would
132 // be redundant.
133 //
134 if (!stream)
135 {
136 //
137 // Set up an output stream object to deal with private ofstream copy
138 // constructor and lifetime issues. Let the helper decide the actual
139 // name of the file given the prefix.
140 //
141 AsciiTraceHelper asciiTraceHelper;
142
143 std::string filename;
144 if (explicitFilename)
145 {
146 filename = prefix;
147 }
148 else
149 {
150 filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
151 }
152
153 Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
154
155 //
156 // The MacRx trace source provides our "r" event.
157 //
158 asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<PointToPointNetDevice> (device, "MacRx", theStream);
159
160 //
161 // The "+", '-', and 'd' events are driven by trace sources actually in the
162 // transmit queue.
163 //
164 Ptr<Queue<Packet> > queue = device->GetQueue ();
165 asciiTraceHelper.HookDefaultEnqueueSinkWithoutContext<Queue<Packet> > (queue, "Enqueue", theStream);
166 asciiTraceHelper.HookDefaultDropSinkWithoutContext<Queue<Packet> > (queue, "Drop", theStream);
167 asciiTraceHelper.HookDefaultDequeueSinkWithoutContext<Queue<Packet> > (queue, "Dequeue", theStream);
168
169 // PhyRxDrop trace source for "d" event
170 asciiTraceHelper.HookDefaultDropSinkWithoutContext<PointToPointNetDevice> (device, "PhyRxDrop", theStream);
171
172 return;
173 }
174
175 //
176 // If we are provided an OutputStreamWrapper, we are expected to use it, and
177 // to providd a context. We are free to come up with our own context if we
178 // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
179 // compatibility and simplicity, we just use Config::Connect and let it deal
180 // with the context.
181 //
182 // Note that we are going to use the default trace sinks provided by the
183 // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
184 // but the default trace sinks are actually publicly available static
185 // functions that are always there waiting for just such a case.
186 //
187 uint32_t nodeid = nd->GetNode ()->GetId ();
188 uint32_t deviceid = nd->GetIfIndex ();
189 std::ostringstream oss;
190
191 oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/MacRx";
193
194 oss.str ("");
195 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue";
197
198 oss.str ("");
199 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Dequeue";
201
202 oss.str ("");
203 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Drop";
205
206 oss.str ("");
207 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/PhyRxDrop";
209}
210
213{
214 NS_ASSERT (c.GetN () == 2);
215 return Install (c.Get (0), c.Get (1));
216}
217
220{
221 NetDeviceContainer container;
222
224 devA->SetAddress (Mac48Address::Allocate ());
225 a->AddDevice (devA);
227 devA->SetQueue (queueA);
229 devB->SetAddress (Mac48Address::Allocate ());
230 b->AddDevice (devB);
232 devB->SetQueue (queueB);
234 {
235 // Aggregate NetDeviceQueueInterface objects
236 Ptr<NetDeviceQueueInterface> ndqiA = CreateObject<NetDeviceQueueInterface> ();
237 ndqiA->GetTxQueue (0)->ConnectQueueTraces (queueA);
238 devA->AggregateObject (ndqiA);
239 Ptr<NetDeviceQueueInterface> ndqiB = CreateObject<NetDeviceQueueInterface> ();
240 ndqiB->GetTxQueue (0)->ConnectQueueTraces (queueB);
241 devB->AggregateObject (ndqiB);
242 }
243
245
246 // If MPI is enabled, we need to see if both nodes have the same system id
247 // (rank), and the rank is the same as this instance. If both are true,
248 // use a normal p2p channel, otherwise use a remote channel
249#ifdef NS3_MPI
250 bool useNormalChannel = true;
252 {
253 uint32_t n1SystemId = a->GetSystemId ();
254 uint32_t n2SystemId = b->GetSystemId ();
255 uint32_t currSystemId = MpiInterface::GetSystemId ();
256 if (n1SystemId != currSystemId || n2SystemId != currSystemId)
257 {
258 useNormalChannel = false;
259 }
260 }
261 if (useNormalChannel)
262 {
263 m_channelFactory.SetTypeId ("ns3::PointToPointChannel");
265 }
266 else
267 {
268 m_channelFactory.SetTypeId ("ns3::PointToPointRemoteChannel");
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#else
279#endif
280
281 devA->Attach (channel);
282 devB->Attach (channel);
283 container.Add (devA);
284 container.Add (devB);
285
286 return container;
287}
288
291{
292 Ptr<Node> b = Names::Find<Node> (bName);
293 return Install (a, b);
294}
295
298{
299 Ptr<Node> a = Names::Find<Node> (aName);
300 return Install (a, b);
301}
302
304PointToPointHelper::Install (std::string aName, std::string bName)
305{
306 Ptr<Node> a = Names::Find<Node> (aName);
307 Ptr<Node> b = Names::Find<Node> (bName);
308 return Install (a, b);
309}
310
311} // 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.
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 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.
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 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.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
#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:206
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:282
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs... bargs)
Make Callbacks with varying number of bound arguments.
Definition: callback.h:714
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:661
channel
Definition: third.py:83