10#include "ns3/assert.h"
13#include "ns3/net-device.h"
15#include "ns3/pcap-file-wrapper.h"
39 std::ios::openmode filemode,
40 uint16_t dataLinkType,
44 NS_LOG_FUNCTION(filename << filemode << dataLinkType << snapLen << tzCorrection);
51 NS_LOG_DEBUG(
"Reusing already-open pcap file " << filename);
56 file->Open(filename, filemode);
57 NS_ABORT_MSG_IF(file->Fail(),
"Unable to Open " << filename <<
" for mode " << filemode);
59 file->Init(dataLinkType, snapLen, tzCorrection);
82 std::ostringstream oss;
86 std::string devicename;
96 if (!nodename.empty())
102 oss << node->GetId();
107 if (!devicename.empty())
113 oss << device->GetIfIndex();
130 std::ostringstream oss;
131 oss << prefix <<
"-";
134 std::string nodename;
144 if (!objname.empty())
148 else if (!nodename.empty())
154 oss <<
"n" << node->GetId();
157 oss <<
"-i" <<
interface << ".pcap
";
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.
167PcapHelper::DefaultSink(Ptr<PcapFileWrapper> file, Ptr<const Packet> p)
169 NS_LOG_FUNCTION(file << p);
170 file->Write(Simulator::Now(), p);
174PcapHelper::SinkWithHeader(Ptr<PcapFileWrapper> file, const Header& header, Ptr<const Packet> p)
176 NS_LOG_FUNCTION(file << p);
177 file->Write(Simulator::Now(), header, p);
181PcapHelper::MakeSinkCallback(Ptr<PcapFileWrapper> file)
183 return MakeBoundCallback(&DefaultSink, file);
186AsciiTraceHelper::AsciiTraceHelper()
188 NS_LOG_FUNCTION_NOARGS();
191AsciiTraceHelper::~AsciiTraceHelper()
193 NS_LOG_FUNCTION_NOARGS();
196Ptr<OutputStreamWrapper>
197AsciiTraceHelper::CreateFileStream(std::string filename, std::ios::openmode filemode)
199 NS_LOG_FUNCTION(filename << filemode);
201 Ptr<OutputStreamWrapper> StreamWrapper = Create<OutputStreamWrapper>(filename, filemode);
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.
214 return StreamWrapper;
218AsciiTraceHelper::GetFilenameFromDevice(std::string prefix,
219 Ptr<NetDevice> device,
222 NS_LOG_FUNCTION(prefix << device << useObjectNames);
223 NS_ABORT_MSG_UNLESS(!prefix.empty(), "Empty prefix string
");
225 std::ostringstream oss;
226 oss << prefix << "-
";
228 std::string nodename;
229 std::string devicename;
231 Ptr<Node> node = device->GetNode();
235 nodename = Names::FindName(node);
236 devicename = Names::FindName(device);
239 if (!nodename.empty())
245 oss << node->GetId();
250 if (!devicename.empty())
256 oss << device->GetIfIndex();
265AsciiTraceHelper::GetFilenameFromInterfacePair(std::string prefix,
270 NS_LOG_FUNCTION(prefix << object << interface << useObjectNames);
271 NS_ABORT_MSG_UNLESS(!prefix.empty(), "Empty prefix string
");
273 std::ostringstream oss;
274 oss << prefix << "-
";
277 std::string nodename;
279 Ptr<Node> node = object->GetObject<Node>();
283 objname = Names::FindName(object);
284 nodename = Names::FindName(node);
287 if (!objname.empty())
291 else if (!nodename.empty())
297 oss << "n
" << node->GetId();
300 oss << "-i
" << interface << ".tr
";
306// One of the basic default trace sink sets. Enqueue:
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.
313// This is typically implemented by hooking the "TxQueue/Enqueue
" trace hook
314// in the device (actually the Queue in the device).
317AsciiTraceHelper::DefaultEnqueueSinkWithoutContext(Ptr<OutputStreamWrapper> stream,
320 NS_LOG_FUNCTION(stream << p);
321 *stream->GetStream() << "+
" << Simulator::Now().GetSeconds() << " " << *p << std::endl;
325AsciiTraceHelper::DefaultEnqueueSinkWithContext(Ptr<OutputStreamWrapper> stream,
329 NS_LOG_FUNCTION(stream << p);
330 *stream->GetStream() << "+
" << Simulator::Now().GetSeconds() << " " << context << " " << *p
335// One of the basic default trace sink sets. Drop:
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.
343// This is typically implemented by hooking the "TxQueue/Drop
" trace hook
344// in the device (actually the Queue in the device).
347AsciiTraceHelper::DefaultDropSinkWithoutContext(Ptr<OutputStreamWrapper> stream,
350 NS_LOG_FUNCTION(stream << p);
351 *stream->GetStream() << "d
" << Simulator::Now().GetSeconds() << " " << *p << std::endl;
355AsciiTraceHelper::DefaultDropSinkWithContext(Ptr<OutputStreamWrapper> stream,
359 NS_LOG_FUNCTION(stream << p);
360 *stream->GetStream() << "d
" << Simulator::Now().GetSeconds() << " " << context << " " << *p
365// One of the basic default trace sink sets. Dequeue:
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.
374// This is typically implemented by hooking the "TxQueue/Dequeue
" trace hook
375// in the device (actually the Queue in the device).
378AsciiTraceHelper::DefaultDequeueSinkWithoutContext(Ptr<OutputStreamWrapper> stream,
381 NS_LOG_FUNCTION(stream << p);
382 *stream->GetStream() << "-
" << Simulator::Now().GetSeconds() << " " << *p << std::endl;
386AsciiTraceHelper::DefaultDequeueSinkWithContext(Ptr<OutputStreamWrapper> stream,
390 NS_LOG_FUNCTION(stream << p);
391 *stream->GetStream() << "-
" << Simulator::Now().GetSeconds() << " " << context << " " << *p
396// One of the basic default trace sink sets. Receive:
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
403// This is typically implemented by hooking the "MacRx
" trace hook in the
406AsciiTraceHelper::DefaultReceiveSinkWithoutContext(Ptr<OutputStreamWrapper> stream,
409 NS_LOG_FUNCTION(stream << p);
410 *stream->GetStream() << "r " << Simulator::Now().GetSeconds() << " " << *p << std::endl;
414AsciiTraceHelper::DefaultReceiveSinkWithContext(Ptr<OutputStreamWrapper> stream,
418 NS_LOG_FUNCTION(stream << p);
419 *stream->GetStream() << "r " << Simulator::Now().GetSeconds() << " " << context << " " << *p
424AsciiTraceHelper::MakeSinkWithoutContextCallback(void (*sink)(Ptr<OutputStreamWrapper>,
426 Ptr<OutputStreamWrapper> stream)
428 return MakeBoundCallback(sink, stream);
432AsciiTraceHelper::MakeSinkWithContextCallback(void (*sink)(Ptr<OutputStreamWrapper>,
435 Ptr<OutputStreamWrapper> stream)
437 return MakeBoundCallback(sink, stream);
441PcapHelperForDevice::EnablePcap(std::string prefix,
444 bool explicitFilename)
446 EnablePcapInternal(prefix, nd, promiscuous, explicitFilename);
450PcapHelperForDevice::EnablePcap(std::string prefix,
453 bool explicitFilename)
455 Ptr<NetDevice> nd = Names::Find<NetDevice>(ndName);
456 EnablePcap(prefix, nd, promiscuous, explicitFilename);
460PcapHelperForDevice::EnablePcap(std::string prefix, NetDeviceContainer d, bool promiscuous)
462 for (auto i = d.Begin(); i != d.End(); ++i)
464 Ptr<NetDevice> dev = *i;
465 EnablePcap(prefix, dev, promiscuous);
470PcapHelperForDevice::EnablePcap(std::string prefix, NodeContainer n, bool promiscuous)
472 NetDeviceContainer devs;
473 for (auto i = n.Begin(); i != n.End(); ++i)
476 for (uint32_t j = 0; j < node->GetNDevices(); ++j)
478 devs.Add(node->GetDevice(j));
481 EnablePcap(prefix, devs, promiscuous);
485PcapHelperForDevice::EnablePcapAll(std::string prefix, bool promiscuous)
487 EnablePcap(prefix, NodeContainer::GetGlobal(), promiscuous);
491PcapHelperForDevice::EnablePcap(std::string prefix,
496 NodeContainer n = NodeContainer::GetGlobal();
498 for (auto i = n.Begin(); i != n.End(); ++i)
501 if (node->GetId() != nodeid)
506 NS_ABORT_MSG_IF(deviceid >= node->GetNDevices(),
508 Ptr<NetDevice> nd = node->GetDevice(deviceid);
538 bool explicitFilename)
559 bool explicitFilename)
591 for (
auto i = d.
Begin(); i != d.
End(); ++i)
625 for (
auto i = n.
Begin(); i != n.
End(); ++i)
628 for (
uint32_t j = 0; j < node->GetNDevices(); ++j)
630 devs.
Add(node->GetDevice(j));
672 bool explicitFilename)
685 bool explicitFilename)
689 for (
auto i = n.
Begin(); i != n.
End(); ++i)
692 if (node->GetId() != nodeid)
698 deviceid >= node->GetNDevices(),
699 "AsciiTraceHelperForDevice::EnableAscii(): Unknown deviceid = " << deviceid);
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.
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,...
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.
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.
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#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.
Every class exported by the ns3 library is enclosed in the ns3 namespace.