A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
point-to-point-helper.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19
20#include "ns3/abort.h"
21#include "ns3/config.h"
22#include "ns3/log.h"
23#include "ns3/names.h"
24#include "ns3/net-device-queue-interface.h"
25#include "ns3/packet.h"
26#include "ns3/point-to-point-channel.h"
27#include "ns3/point-to-point-net-device.h"
28#include "ns3/simulator.h"
29
30#ifdef NS3_MPI
31#include "ns3/mpi-interface.h"
32#include "ns3/mpi-receiver.h"
33#include "ns3/point-to-point-remote-channel.h"
34#endif
35
37
38#include "ns3/trace-helper.h"
39
40namespace ns3
41{
42
43NS_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");
51}
52
53void
55{
56 m_deviceFactory.Set(n1, v1);
57}
58
59void
61{
62 m_channelFactory.Set(n1, v1);
63}
64
65void
67{
68 m_enableFlowControl = false;
69}
70
71void
74 bool promiscuous,
75 bool explicitFilename)
76{
77 //
78 // All of the Pcap enable functions vector through here including the ones
79 // that are wandering through all of devices on perhaps all of the nodes in
80 // the system. We can only deal with devices of type PointToPointNetDevice.
81 //
82 Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice>();
83 if (!device)
84 {
85 NS_LOG_INFO("Device " << device << " not of type ns3::PointToPointNetDevice");
86 return;
87 }
88
89 PcapHelper pcapHelper;
90
91 std::string filename;
92 if (explicitFilename)
93 {
94 filename = prefix;
95 }
96 else
97 {
98 filename = pcapHelper.GetFilenameFromDevice(prefix, device);
99 }
100
101 Ptr<PcapFileWrapper> file = pcapHelper.CreateFile(filename, std::ios::out, PcapHelper::DLT_PPP);
102 pcapHelper.HookDefaultSink<PointToPointNetDevice>(device, "PromiscSniffer", file);
103}
104
105void
107 std::string prefix,
109 bool explicitFilename)
110{
111 //
112 // All of the ascii enable functions vector through here including the ones
113 // that are wandering through all of devices on perhaps all of the nodes in
114 // the system. We can only deal with devices of type PointToPointNetDevice.
115 //
116 Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice>();
117 if (!device)
118 {
119 NS_LOG_INFO("Device " << device << " not of type ns3::PointToPointNetDevice");
120 return;
121 }
122
123 //
124 // Our default trace sinks are going to use packet printing, so we have to
125 // make sure that is turned on.
126 //
128
129 //
130 // If we are not provided an OutputStreamWrapper, we are expected to create
131 // one using the usual trace filename conventions and do a Hook*WithoutContext
132 // since there will be one file per context and therefore the context would
133 // be redundant.
134 //
135 if (!stream)
136 {
137 //
138 // Set up an output stream object to deal with private ofstream copy
139 // constructor and lifetime issues. Let the helper decide the actual
140 // name of the file given the prefix.
141 //
142 AsciiTraceHelper asciiTraceHelper;
143
144 std::string filename;
145 if (explicitFilename)
146 {
147 filename = prefix;
148 }
149 else
150 {
151 filename = asciiTraceHelper.GetFilenameFromDevice(prefix, device);
152 }
153
154 Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream(filename);
155
156 //
157 // The MacRx trace source provides our "r" event.
158 //
160 "MacRx",
161 theStream);
162
163 //
164 // The "+", '-', and 'd' events are driven by trace sources actually in the
165 // transmit queue.
166 //
167 Ptr<Queue<Packet>> queue = device->GetQueue();
169 "Enqueue",
170 theStream);
171 asciiTraceHelper.HookDefaultDropSinkWithoutContext<Queue<Packet>>(queue, "Drop", theStream);
173 "Dequeue",
174 theStream);
175
176 // PhyRxDrop trace source for "d" event
178 "PhyRxDrop",
179 theStream);
180
181 return;
182 }
183
184 //
185 // If we are provided an OutputStreamWrapper, we are expected to use it, and
186 // to providd a context. We are free to come up with our own context if we
187 // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
188 // compatibility and simplicity, we just use Config::Connect and let it deal
189 // with the context.
190 //
191 // Note that we are going to use the default trace sinks provided by the
192 // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
193 // but the default trace sinks are actually publicly available static
194 // functions that are always there waiting for just such a case.
195 //
196 uint32_t nodeid = nd->GetNode()->GetId();
197 uint32_t deviceid = nd->GetIfIndex();
198 std::ostringstream oss;
199
200 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid
201 << "/$ns3::PointToPointNetDevice/MacRx";
202 Config::Connect(oss.str(),
204
205 oss.str("");
206 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid
207 << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue";
208 Config::Connect(oss.str(),
210
211 oss.str("");
212 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid
213 << "/$ns3::PointToPointNetDevice/TxQueue/Dequeue";
214 Config::Connect(oss.str(),
216
217 oss.str("");
218 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid
219 << "/$ns3::PointToPointNetDevice/TxQueue/Drop";
220 Config::Connect(oss.str(),
222
223 oss.str("");
224 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid
225 << "/$ns3::PointToPointNetDevice/PhyRxDrop";
226 Config::Connect(oss.str(),
228}
229
232{
233 NS_ASSERT(c.GetN() == 2);
234 return Install(c.Get(0), c.Get(1));
235}
236
239{
240 NetDeviceContainer container;
241
243 devA->SetAddress(Mac48Address::Allocate());
244 a->AddDevice(devA);
246 devA->SetQueue(queueA);
248 devB->SetAddress(Mac48Address::Allocate());
249 b->AddDevice(devB);
251 devB->SetQueue(queueB);
253 {
254 // Aggregate NetDeviceQueueInterface objects
255 Ptr<NetDeviceQueueInterface> ndqiA = CreateObject<NetDeviceQueueInterface>();
256 ndqiA->GetTxQueue(0)->ConnectQueueTraces(queueA);
257 devA->AggregateObject(ndqiA);
258 Ptr<NetDeviceQueueInterface> ndqiB = CreateObject<NetDeviceQueueInterface>();
259 ndqiB->GetTxQueue(0)->ConnectQueueTraces(queueB);
260 devB->AggregateObject(ndqiB);
261 }
262
263 Ptr<PointToPointChannel> channel = nullptr;
264
265 // If MPI is enabled, we need to see if both nodes have the same system id
266 // (rank), and the rank is the same as this instance. If both are true,
267 // use a normal p2p channel, otherwise use a remote channel
268#ifdef NS3_MPI
269 bool useNormalChannel = true;
271 {
272 uint32_t n1SystemId = a->GetSystemId();
273 uint32_t n2SystemId = b->GetSystemId();
274 uint32_t currSystemId = MpiInterface::GetSystemId();
275 if (n1SystemId != currSystemId || n2SystemId != currSystemId)
276 {
277 useNormalChannel = false;
278 }
279 }
280 if (useNormalChannel)
281 {
282 m_channelFactory.SetTypeId("ns3::PointToPointChannel");
284 }
285 else
286 {
287 m_channelFactory.SetTypeId("ns3::PointToPointRemoteChannel");
289 Ptr<MpiReceiver> mpiRecA = CreateObject<MpiReceiver>();
290 Ptr<MpiReceiver> mpiRecB = CreateObject<MpiReceiver>();
291 mpiRecA->SetReceiveCallback(MakeCallback(&PointToPointNetDevice::Receive, devA));
292 mpiRecB->SetReceiveCallback(MakeCallback(&PointToPointNetDevice::Receive, devB));
293 devA->AggregateObject(mpiRecA);
294 devB->AggregateObject(mpiRecB);
295 }
296#else
298#endif
299
300 devA->Attach(channel);
301 devB->Attach(channel);
302 container.Add(devA);
303 container.Add(devB);
304
305 return container;
306}
307
310{
311 Ptr<Node> b = Names::Find<Node>(bName);
312 return Install(a, b);
313}
314
317{
318 Ptr<Node> a = Names::Find<Node>(aName);
319 return Install(a, b);
320}
321
323PointToPointHelper::Install(std::string aName, std::string bName)
324{
325 Ptr<Node> a = Names::Find<Node>(aName);
326 Ptr<Node> b = Names::Find<Node>(bName);
327 return Install(a, b);
328}
329
330} // namespace ns3
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
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:534
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:505
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:592
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:563
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:70
static Mac48Address Allocate()
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
keep track of a set of node pointers.
uint32_t GetN() 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.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
static void EnablePrinting()
Enable printing packets metadata.
Definition: packet.cc:596
Manage pcap files for device models.
Definition: trace-helper.h:40
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:79
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:158
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.
void DisableFlowControl()
Disable flow control only if you know what you are doing.
void EnablePcapInternal(std::string prefix, Ptr< NetDevice > nd, bool promiscuous, bool explicitFilename) override
Enable pcap output 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)
void EnableAsciiInternal(Ptr< OutputStreamWrapper > stream, std::string prefix, Ptr< NetDevice > nd, bool explicitFilename) override
Enable ascii trace output on the indicated net device.
A Device for a Point to Point Network Link.
Ptr< Queue< Packet > > GetQueue() 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:77
Template class for packet Queues.
Definition: queue.h:268
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:978
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition: callback.h:767
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:706