A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
trace-helper.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7#include "trace-helper.h"
8
9#include "ns3/abort.h"
10#include "ns3/assert.h"
11#include "ns3/log.h"
12#include "ns3/names.h"
13#include "ns3/net-device.h"
14#include "ns3/node.h"
15#include "ns3/pcap-file-wrapper.h"
16#include "ns3/ptr.h"
17
18#include <cstdint>
19#include <fstream>
20#include <string>
21
22namespace ns3
23{
24
25NS_LOG_COMPONENT_DEFINE("TraceHelper");
26
31
36
38PcapHelper::CreateFile(std::string filename,
39 std::ios::openmode filemode,
40 uint16_t dataLinkType,
41 uint32_t snapLen,
42 int32_t tzCorrection)
43{
44 NS_LOG_FUNCTION(filename << filemode << dataLinkType << snapLen << tzCorrection);
45
46 // If another interface was already given this filename, share its
47 // wrapper instead of reopening (and truncating) the file, which would
48 // corrupt it by clobbering the first interface's records (see @issueid{1150}).
50 {
51 NS_LOG_DEBUG("Reusing already-open pcap file " << filename);
52 return existing;
53 }
54
56 file->Open(filename, filemode);
57 NS_ABORT_MSG_IF(file->Fail(), "Unable to Open " << filename << " for mode " << filemode);
58
59 file->Init(dataLinkType, snapLen, tzCorrection);
60 NS_ABORT_MSG_IF(file->Fail(), "Unable to Init " << filename);
61
62 //
63 // Note that the pcap helper promptly forgets all about the pcap file. We
64 // rely on the reference count of the file object which will soon be owned
65 // by the caller to keep the object alive. If the caller uses the file
66 // object to hook a trace source, ownership of the file object will be
67 // implicitly transferred to the callback which keeps the object alive.
68 // When the callback is destroyed (when either the trace is disconnected or
69 // the object with the trace source is deleted) the callback will be destroyed
70 // and the file object will be destroyed, releasing the pointer and closing
71 // the file.
72 //
73 return file;
74}
75
76std::string
77PcapHelper::GetFilenameFromDevice(std::string prefix, Ptr<NetDevice> device, bool useObjectNames)
78{
79 NS_LOG_FUNCTION(prefix << device << useObjectNames);
80 NS_ABORT_MSG_UNLESS(!prefix.empty(), "Empty prefix string");
81
82 std::ostringstream oss;
83 oss << prefix << "-";
84
85 std::string nodename;
86 std::string devicename;
87
88 Ptr<Node> node = device->GetNode();
89
90 if (useObjectNames)
91 {
92 nodename = Names::FindName(node);
93 devicename = Names::FindName(device);
94 }
95
96 if (!nodename.empty())
97 {
98 oss << nodename;
99 }
100 else
101 {
102 oss << node->GetId();
103 }
104
105 oss << "-";
106
107 if (!devicename.empty())
108 {
109 oss << devicename;
110 }
111 else
112 {
113 oss << device->GetIfIndex();
114 }
115
116 oss << ".pcap";
117
118 return oss.str();
119}
120
121std::string
123 Ptr<Object> object,
124 uint32_t interface,
125 bool useObjectNames)
126{
127 NS_LOG_FUNCTION(prefix << object << interface << useObjectNames);
128 NS_ABORT_MSG_UNLESS(!prefix.empty(), "Empty prefix string");
129
130 std::ostringstream oss;
131 oss << prefix << "-";
132
133 std::string objname;
134 std::string nodename;
135
136 Ptr<Node> node = object->GetObject<Node>();
137
138 if (useObjectNames)
139 {
140 objname = Names::FindName(object);
141 nodename = Names::FindName(node);
142 }
143
144 if (!objname.empty())
145 {
146 oss << objname;
147 }
148 else if (!nodename.empty())
149 {
150 oss << nodename;
151 }
152 else
153 {
154 oss << "n" << node->GetId();
155 }
156
157 oss << "-i" << interface << ".pcap";
158
159 return oss.str();
160}
161
162//
163// The basic default trace sink. This one just writes the packet to the pcap
164// file which is good enough for most kinds of captures.
165//
166void
167PcapHelper::DefaultSink(Ptr<PcapFileWrapper> file, Ptr<const Packet> p)
168{
169 NS_LOG_FUNCTION(file << p);
170 file->Write(Simulator::Now(), p);
171}
172
173void
174PcapHelper::SinkWithHeader(Ptr<PcapFileWrapper> file, const Header& header, Ptr<const Packet> p)
175{
176 NS_LOG_FUNCTION(file << p);
177 file->Write(Simulator::Now(), header, p);
178}
179
180CallbackBase
181PcapHelper::MakeSinkCallback(Ptr<PcapFileWrapper> file)
182{
183 return MakeBoundCallback(&DefaultSink, file);
184}
185
186AsciiTraceHelper::AsciiTraceHelper()
187{
188 NS_LOG_FUNCTION_NOARGS();
189}
190
191AsciiTraceHelper::~AsciiTraceHelper()
192{
193 NS_LOG_FUNCTION_NOARGS();
194}
195
196Ptr<OutputStreamWrapper>
197AsciiTraceHelper::CreateFileStream(std::string filename, std::ios::openmode filemode)
198{
199 NS_LOG_FUNCTION(filename << filemode);
200
201 Ptr<OutputStreamWrapper> StreamWrapper = Create<OutputStreamWrapper>(filename, filemode);
202
203 //
204 // Note that the ascii trace helper promptly forgets all about the trace file.
205 // We rely on the reference count of the file object which will soon be owned
206 // by the caller to keep the object alive. If the caller uses the stream
207 // object to hook a trace source, ownership of the stream object will be
208 // implicitly transferred to the callback which keeps the object alive.
209 // When the callback is destroyed (when either the trace is disconnected or
210 // the object with the trace source is deleted) the callback will be destroyed
211 // and the stream object will be destroyed, releasing the pointer and closing
212 // the underlying file.
213 //
214 return StreamWrapper;
215}
216
217std::string
218AsciiTraceHelper::GetFilenameFromDevice(std::string prefix,
219 Ptr<NetDevice> device,
220 bool useObjectNames)
221{
222 NS_LOG_FUNCTION(prefix << device << useObjectNames);
223 NS_ABORT_MSG_UNLESS(!prefix.empty(), "Empty prefix string");
224
225 std::ostringstream oss;
226 oss << prefix << "-";
227
228 std::string nodename;
229 std::string devicename;
230
231 Ptr<Node> node = device->GetNode();
232
233 if (useObjectNames)
234 {
235 nodename = Names::FindName(node);
236 devicename = Names::FindName(device);
237 }
238
239 if (!nodename.empty())
240 {
241 oss << nodename;
242 }
243 else
244 {
245 oss << node->GetId();
246 }
247
248 oss << "-";
249
250 if (!devicename.empty())
251 {
252 oss << devicename;
253 }
254 else
255 {
256 oss << device->GetIfIndex();
257 }
258
259 oss << ".tr";
260
261 return oss.str();
262}
263
264std::string
265AsciiTraceHelper::GetFilenameFromInterfacePair(std::string prefix,
266 Ptr<Object> object,
267 uint32_t interface,
268 bool useObjectNames)
269{
270 NS_LOG_FUNCTION(prefix << object << interface << useObjectNames);
271 NS_ABORT_MSG_UNLESS(!prefix.empty(), "Empty prefix string");
272
273 std::ostringstream oss;
274 oss << prefix << "-";
275
276 std::string objname;
277 std::string nodename;
278
279 Ptr<Node> node = object->GetObject<Node>();
280
281 if (useObjectNames)
282 {
283 objname = Names::FindName(object);
284 nodename = Names::FindName(node);
285 }
286
287 if (!objname.empty())
288 {
289 oss << objname;
290 }
291 else if (!nodename.empty())
292 {
293 oss << nodename;
294 }
295 else
296 {
297 oss << "n" << node->GetId();
298 }
299
300 oss << "-i" << interface << ".tr";
301
302 return oss.str();
303}
304
305//
306// One of the basic default trace sink sets. Enqueue:
307//
308// When a packet has been sent to a device for transmission, the device is
309// expected to place the packet onto a transmit queue even if it does not
310// have to delay the packet at all, if only to trigger this event. This
311// event will eventually translate into a '+' operation in the trace file.
312//
313// This is typically implemented by hooking the "TxQueue/Enqueue" trace hook
314// in the device (actually the Queue in the device).
315//
316void
317AsciiTraceHelper::DefaultEnqueueSinkWithoutContext(Ptr<OutputStreamWrapper> stream,
318 Ptr<const Packet> p)
319{
320 NS_LOG_FUNCTION(stream << p);
321 *stream->GetStream() << "+ " << Simulator::Now().GetSeconds() << " " << *p << std::endl;
322}
323
324void
325AsciiTraceHelper::DefaultEnqueueSinkWithContext(Ptr<OutputStreamWrapper> stream,
326 std::string context,
327 Ptr<const Packet> p)
328{
329 NS_LOG_FUNCTION(stream << p);
330 *stream->GetStream() << "+ " << Simulator::Now().GetSeconds() << " " << context << " " << *p
331 << std::endl;
332}
333
334//
335// One of the basic default trace sink sets. Drop:
336//
337// When a packet has been sent to a device for transmission, the device is
338// expected to place the packet onto a transmit queue. If this queue is
339// full the packet will be dropped. The device is expected to trigger an
340// event to indicate that an outbound packet is being dropped. This event
341// will eventually translate into a 'd' operation in the trace file.
342//
343// This is typically implemented by hooking the "TxQueue/Drop" trace hook
344// in the device (actually the Queue in the device).
345//
346void
347AsciiTraceHelper::DefaultDropSinkWithoutContext(Ptr<OutputStreamWrapper> stream,
348 Ptr<const Packet> p)
349{
350 NS_LOG_FUNCTION(stream << p);
351 *stream->GetStream() << "d " << Simulator::Now().GetSeconds() << " " << *p << std::endl;
352}
353
354void
355AsciiTraceHelper::DefaultDropSinkWithContext(Ptr<OutputStreamWrapper> stream,
356 std::string context,
357 Ptr<const Packet> p)
358{
359 NS_LOG_FUNCTION(stream << p);
360 *stream->GetStream() << "d " << Simulator::Now().GetSeconds() << " " << context << " " << *p
361 << std::endl;
362}
363
364//
365// One of the basic default trace sink sets. Dequeue:
366//
367// When a packet has been sent to a device for transmission, the device is
368// expected to place the packet onto a transmit queue even if it does not
369// have to delay the packet at all. The device removes the packet from the
370// transmit queue when the packet is ready to send, and this dequeue will
371// fire a corresponding event. This event will eventually translate into a
372// '-' operation in the trace file.
373//
374// This is typically implemented by hooking the "TxQueue/Dequeue" trace hook
375// in the device (actually the Queue in the device).
376//
377void
378AsciiTraceHelper::DefaultDequeueSinkWithoutContext(Ptr<OutputStreamWrapper> stream,
379 Ptr<const Packet> p)
380{
381 NS_LOG_FUNCTION(stream << p);
382 *stream->GetStream() << "- " << Simulator::Now().GetSeconds() << " " << *p << std::endl;
383}
384
385void
386AsciiTraceHelper::DefaultDequeueSinkWithContext(Ptr<OutputStreamWrapper> stream,
387 std::string context,
388 Ptr<const Packet> p)
389{
390 NS_LOG_FUNCTION(stream << p);
391 *stream->GetStream() << "- " << Simulator::Now().GetSeconds() << " " << context << " " << *p
392 << std::endl;
393}
394
395//
396// One of the basic default trace sink sets. Receive:
397//
398// When a packet is received by a device for transmission, the device is
399// expected to trigger this event to indicate the reception has occurred.
400// This event will eventually translate into an 'r' operation in the trace
401// file.
402//
403// This is typically implemented by hooking the "MacRx" trace hook in the
404// device.
405void
406AsciiTraceHelper::DefaultReceiveSinkWithoutContext(Ptr<OutputStreamWrapper> stream,
407 Ptr<const Packet> p)
408{
409 NS_LOG_FUNCTION(stream << p);
410 *stream->GetStream() << "r " << Simulator::Now().GetSeconds() << " " << *p << std::endl;
411}
412
413void
414AsciiTraceHelper::DefaultReceiveSinkWithContext(Ptr<OutputStreamWrapper> stream,
415 std::string context,
416 Ptr<const Packet> p)
417{
418 NS_LOG_FUNCTION(stream << p);
419 *stream->GetStream() << "r " << Simulator::Now().GetSeconds() << " " << context << " " << *p
420 << std::endl;
421}
422
423CallbackBase
424AsciiTraceHelper::MakeSinkWithoutContextCallback(void (*sink)(Ptr<OutputStreamWrapper>,
425 Ptr<const Packet>),
426 Ptr<OutputStreamWrapper> stream)
427{
428 return MakeBoundCallback(sink, stream);
429}
430
431CallbackBase
432AsciiTraceHelper::MakeSinkWithContextCallback(void (*sink)(Ptr<OutputStreamWrapper>,
433 std::string,
434 Ptr<const Packet>),
435 Ptr<OutputStreamWrapper> stream)
436{
437 return MakeBoundCallback(sink, stream);
438}
439
440void
441PcapHelperForDevice::EnablePcap(std::string prefix,
442 Ptr<NetDevice> nd,
443 bool promiscuous,
444 bool explicitFilename)
445{
446 EnablePcapInternal(prefix, nd, promiscuous, explicitFilename);
447}
448
449void
450PcapHelperForDevice::EnablePcap(std::string prefix,
451 std::string ndName,
452 bool promiscuous,
453 bool explicitFilename)
454{
455 Ptr<NetDevice> nd = Names::Find<NetDevice>(ndName);
456 EnablePcap(prefix, nd, promiscuous, explicitFilename);
457}
458
459void
460PcapHelperForDevice::EnablePcap(std::string prefix, NetDeviceContainer d, bool promiscuous)
461{
462 for (auto i = d.Begin(); i != d.End(); ++i)
463 {
464 Ptr<NetDevice> dev = *i;
465 EnablePcap(prefix, dev, promiscuous);
466 }
467}
468
469void
470PcapHelperForDevice::EnablePcap(std::string prefix, NodeContainer n, bool promiscuous)
471{
472 NetDeviceContainer devs;
473 for (auto i = n.Begin(); i != n.End(); ++i)
474 {
475 Ptr<Node> node = *i;
476 for (uint32_t j = 0; j < node->GetNDevices(); ++j)
477 {
478 devs.Add(node->GetDevice(j));
479 }
480 }
481 EnablePcap(prefix, devs, promiscuous);
482}
483
484void
485PcapHelperForDevice::EnablePcapAll(std::string prefix, bool promiscuous)
486{
487 EnablePcap(prefix, NodeContainer::GetGlobal(), promiscuous);
488}
489
490void
491PcapHelperForDevice::EnablePcap(std::string prefix,
492 uint32_t nodeid,
493 uint32_t deviceid,
494 bool promiscuous)
495{
496 NodeContainer n = NodeContainer::GetGlobal();
497
498 for (auto i = n.Begin(); i != n.End(); ++i)
499 {
500 Ptr<Node> node = *i;
501 if (node->GetId() != nodeid)
502 {
503 continue;
504 }
505
506 NS_ABORT_MSG_IF(deviceid >= node->GetNDevices(),
507 "PcapHelperForDevice::EnablePcap(): Unknown deviceid = " << deviceid);
508 Ptr<NetDevice> nd = node->GetDevice(deviceid);
509 EnablePcap(prefix, nd, promiscuous);
510 return;
511 }
512}
513
514//
515// Public API
516//
517void
518AsciiTraceHelperForDevice::EnableAscii(std::string prefix, Ptr<NetDevice> nd, bool explicitFilename)
519{
520 EnableAsciiInternal(Ptr<OutputStreamWrapper>(), prefix, nd, explicitFilename);
521}
522
523//
524// Public API
525//
526void
531
532//
533// Public API
534//
535void
537 std::string ndName,
538 bool explicitFilename)
539{
540 EnableAsciiImpl(Ptr<OutputStreamWrapper>(), prefix, ndName, explicitFilename);
541}
542
543//
544// Public API
545//
546void
548{
549 EnableAsciiImpl(stream, std::string(), ndName, false);
550}
551
552//
553// Private API
554//
555void
557 std::string prefix,
558 std::string ndName,
559 bool explicitFilename)
560{
562 EnableAsciiInternal(stream, prefix, nd, explicitFilename);
563}
564
565//
566// Public API
567//
568void
573
574//
575// Public API
576//
577void
582
583//
584// Private API
585//
586void
588 std::string prefix,
590{
591 for (auto i = d.Begin(); i != d.End(); ++i)
592 {
593 Ptr<NetDevice> dev = *i;
594 EnableAsciiInternal(stream, prefix, dev, false);
595 }
596}
597
598//
599// Public API
600//
601void
606
607//
608// Public API
609//
610void
615
616//
617// Private API
618//
619void
621 std::string prefix,
623{
625 for (auto i = n.Begin(); i != n.End(); ++i)
626 {
627 Ptr<Node> node = *i;
628 for (uint32_t j = 0; j < node->GetNDevices(); ++j)
629 {
630 devs.Add(node->GetDevice(j));
631 }
632 }
633 EnableAsciiImpl(stream, prefix, devs);
634}
635
636//
637// Public API
638//
639void
644
645//
646// Public API
647//
648void
653
654//
655// Public API
656//
657void
659 uint32_t nodeid,
660 uint32_t deviceid)
661{
662 EnableAsciiImpl(stream, std::string(), nodeid, deviceid, false);
663}
664
665//
666// Public API
667//
668void
670 uint32_t nodeid,
671 uint32_t deviceid,
672 bool explicitFilename)
673{
674 EnableAsciiImpl(Ptr<OutputStreamWrapper>(), prefix, nodeid, deviceid, explicitFilename);
675}
676
677//
678// Private API
679//
680void
682 std::string prefix,
683 uint32_t nodeid,
684 uint32_t deviceid,
685 bool explicitFilename)
686{
688
689 for (auto i = n.Begin(); i != n.End(); ++i)
690 {
691 Ptr<Node> node = *i;
692 if (node->GetId() != nodeid)
693 {
694 continue;
695 }
696
698 deviceid >= node->GetNDevices(),
699 "AsciiTraceHelperForDevice::EnableAscii(): Unknown deviceid = " << deviceid);
700
701 Ptr<NetDevice> nd = node->GetDevice(deviceid);
702
703 EnableAsciiInternal(stream, prefix, nd, explicitFilename);
704 return;
705 }
706}
707
708} // namespace ns3
uint32_t r
void EnableAsciiImpl(Ptr< OutputStreamWrapper > stream, std::string prefix, uint32_t nodeid, uint32_t deviceid, bool explicitFilename)
Enable ascii trace output on the device specified by a global node-id (of a previously created node) ...
void EnableAscii(std::string prefix, Ptr< NetDevice > nd, bool explicitFilename=false)
Enable ascii trace output on the indicated net device.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
virtual void EnableAsciiInternal(Ptr< OutputStreamWrapper > stream, std::string prefix, Ptr< NetDevice > nd, bool explicitFilename)=0
Enable ascii trace output on the indicated net device.
Empty class, used as a default parent class for SimpleRefCount.
static Ptr< T > Find(std::string path)
Given a name path string, look to see if there's an object in the system with that associated to it.
Definition names.h:445
static std::string FindName(Ptr< Object > object)
Given a pointer to an object, look to see if that object has a name associated with it and,...
Definition names.cc:818
holds a vector of ns3::NetDevice pointers
Iterator Begin() const
Get an iterator which refers to the first NetDevice in the container.
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Iterator End() const
Get an iterator which indicates past-the-last NetDevice in the container.
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
static NodeContainer GetGlobal()
Create a NodeContainer that contains a list of all nodes created through NodeContainer::Create() and ...
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
A network Node.
Definition node.h:46
static Ptr< PcapFileWrapper > FindOpenFile(const std::string &filename)
Find a wrapper that currently has the given file open.
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
PcapHelperForDevice()
Construct a PcapHelperForDevice.
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.
std::string GetFilenameFromInterfacePair(std::string prefix, Ptr< Object > object, uint32_t interface, bool useObjectNames=true)
Let the pcap helper figure out a reasonable filename to use for the pcap file associated with a node.
Ptr< PcapFileWrapper > CreateFile(std::string filename, std::ios::openmode filemode, uint16_t dataLinkType, uint32_t snapLen=std::numeric_limits< uint32_t >::max(), int32_t tzCorrection=0)
Create and initialize a pcap file.
~PcapHelper()
Destroy a pcap helper.
PcapHelper()
Create a pcap helper.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition abort.h:136
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:99
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:267
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Every class exported by the ns3 library is enclosed in the ns3 namespace.