21#include "ns3/assert.h"
24#include "ns3/net-device.h"
26#include "ns3/pcap-file-wrapper.h"
50 std::ios::openmode filemode,
55 NS_LOG_FUNCTION(filename << filemode << dataLinkType << snapLen << tzCorrection);
58 file->Open(filename, filemode);
59 NS_ABORT_MSG_IF(file->Fail(),
"Unable to Open " << filename <<
" for mode " << filemode);
61 file->Init(dataLinkType, snapLen, tzCorrection);
84 std::ostringstream oss;
88 std::string devicename;
98 if (!nodename.empty())
104 oss << node->GetId();
109 if (!devicename.empty())
115 oss << device->GetIfIndex();
132 std::ostringstream oss;
133 oss << prefix <<
"-";
136 std::string nodename;
146 if (!objname.empty())
150 else if (!nodename.empty())
156 oss <<
"n" << node->GetId();
159 oss <<
"-i" <<
interface << ".pcap
";
165// The basic default trace sink. This one just writes the packet to the pcap
166// file which is good enough for most kinds of captures.
169PcapHelper::DefaultSink(Ptr<PcapFileWrapper> file, Ptr<const Packet> p)
171 NS_LOG_FUNCTION(file << p);
172 file->Write(Simulator::Now(), p);
176PcapHelper::SinkWithHeader(Ptr<PcapFileWrapper> file, const Header& header, Ptr<const Packet> p)
178 NS_LOG_FUNCTION(file << p);
179 file->Write(Simulator::Now(), header, p);
182AsciiTraceHelper::AsciiTraceHelper()
184 NS_LOG_FUNCTION_NOARGS();
187AsciiTraceHelper::~AsciiTraceHelper()
189 NS_LOG_FUNCTION_NOARGS();
192Ptr<OutputStreamWrapper>
193AsciiTraceHelper::CreateFileStream(std::string filename, std::ios::openmode filemode)
195 NS_LOG_FUNCTION(filename << filemode);
197 Ptr<OutputStreamWrapper> StreamWrapper = Create<OutputStreamWrapper>(filename, filemode);
200 // Note that the ascii trace helper promptly forgets all about the trace file.
201 // We rely on the reference count of the file object which will soon be owned
202 // by the caller to keep the object alive. If the caller uses the stream
203 // object to hook a trace source, ownership of the stream object will be
204 // implicitly transferred to the callback which keeps the object alive.
205 // When the callback is destroyed (when either the trace is disconnected or
206 // the object with the trace source is deleted) the callback will be destroyed
207 // and the stream object will be destroyed, releasing the pointer and closing
208 // the underlying file.
210 return StreamWrapper;
214AsciiTraceHelper::GetFilenameFromDevice(std::string prefix,
215 Ptr<NetDevice> device,
218 NS_LOG_FUNCTION(prefix << device << useObjectNames);
219 NS_ABORT_MSG_UNLESS(!prefix.empty(), "Empty prefix string
");
221 std::ostringstream oss;
222 oss << prefix << "-
";
224 std::string nodename;
225 std::string devicename;
227 Ptr<Node> node = device->GetNode();
231 nodename = Names::FindName(node);
232 devicename = Names::FindName(device);
235 if (!nodename.empty())
241 oss << node->GetId();
246 if (!devicename.empty())
252 oss << device->GetIfIndex();
261AsciiTraceHelper::GetFilenameFromInterfacePair(std::string prefix,
266 NS_LOG_FUNCTION(prefix << object << interface << useObjectNames);
267 NS_ABORT_MSG_UNLESS(!prefix.empty(), "Empty prefix string
");
269 std::ostringstream oss;
270 oss << prefix << "-
";
273 std::string nodename;
275 Ptr<Node> node = object->GetObject<Node>();
279 objname = Names::FindName(object);
280 nodename = Names::FindName(node);
283 if (!objname.empty())
287 else if (!nodename.empty())
293 oss << "n
" << node->GetId();
296 oss << "-i
" << interface << ".tr
";
302// One of the basic default trace sink sets. Enqueue:
304// When a packet has been sent to a device for transmission, the device is
305// expected to place the packet onto a transmit queue even if it does not
306// have to delay the packet at all, if only to trigger this event. This
307// event will eventually translate into a '+' operation in the trace file.
309// This is typically implemented by hooking the "TxQueue/Enqueue
" trace hook
310// in the device (actually the Queue in the device).
313AsciiTraceHelper::DefaultEnqueueSinkWithoutContext(Ptr<OutputStreamWrapper> stream,
316 NS_LOG_FUNCTION(stream << p);
317 *stream->GetStream() << "+
" << Simulator::Now().GetSeconds() << " " << *p << std::endl;
321AsciiTraceHelper::DefaultEnqueueSinkWithContext(Ptr<OutputStreamWrapper> stream,
325 NS_LOG_FUNCTION(stream << p);
326 *stream->GetStream() << "+
" << Simulator::Now().GetSeconds() << " " << context << " " << *p
331// One of the basic default trace sink sets. Drop:
333// When a packet has been sent to a device for transmission, the device is
334// expected to place the packet onto a transmit queue. If this queue is
335// full the packet will be dropped. The device is expected to trigger an
336// event to indicate that an outbound packet is being dropped. This event
337// will eventually translate into a 'd' operation in the trace file.
339// This is typically implemented by hooking the "TxQueue/Drop
" trace hook
340// in the device (actually the Queue in the device).
343AsciiTraceHelper::DefaultDropSinkWithoutContext(Ptr<OutputStreamWrapper> stream,
346 NS_LOG_FUNCTION(stream << p);
347 *stream->GetStream() << "d
" << Simulator::Now().GetSeconds() << " " << *p << std::endl;
351AsciiTraceHelper::DefaultDropSinkWithContext(Ptr<OutputStreamWrapper> stream,
355 NS_LOG_FUNCTION(stream << p);
356 *stream->GetStream() << "d
" << Simulator::Now().GetSeconds() << " " << context << " " << *p
361// One of the basic default trace sink sets. Dequeue:
363// When a packet has been sent to a device for transmission, the device is
364// expected to place the packet onto a transmit queue even if it does not
365// have to delay the packet at all. The device removes the packet from the
366// transmit queue when the packet is ready to send, and this dequeue will
367// fire a corresponding event. This event will eventually translate into a
368// '-' operation in the trace file.
370// This is typically implemented by hooking the "TxQueue/Dequeue
" trace hook
371// in the device (actually the Queue in the device).
374AsciiTraceHelper::DefaultDequeueSinkWithoutContext(Ptr<OutputStreamWrapper> stream,
377 NS_LOG_FUNCTION(stream << p);
378 *stream->GetStream() << "-
" << Simulator::Now().GetSeconds() << " " << *p << std::endl;
382AsciiTraceHelper::DefaultDequeueSinkWithContext(Ptr<OutputStreamWrapper> stream,
386 NS_LOG_FUNCTION(stream << p);
387 *stream->GetStream() << "-
" << Simulator::Now().GetSeconds() << " " << context << " " << *p
392// One of the basic default trace sink sets. Receive:
394// When a packet is received by a device for transmission, the device is
395// expected to trigger this event to indicate the reception has occurred.
396// This event will eventually translate into an 'r' operation in the trace
399// This is typically implemented by hooking the "MacRx
" trace hook in the
402AsciiTraceHelper::DefaultReceiveSinkWithoutContext(Ptr<OutputStreamWrapper> stream,
405 NS_LOG_FUNCTION(stream << p);
406 *stream->GetStream() << "r
" << Simulator::Now().GetSeconds() << " " << *p << std::endl;
410AsciiTraceHelper::DefaultReceiveSinkWithContext(Ptr<OutputStreamWrapper> stream,
414 NS_LOG_FUNCTION(stream << p);
415 *stream->GetStream() << "r
" << Simulator::Now().GetSeconds() << " " << context << " " << *p
420PcapHelperForDevice::EnablePcap(std::string prefix,
423 bool explicitFilename)
425 EnablePcapInternal(prefix, nd, promiscuous, explicitFilename);
429PcapHelperForDevice::EnablePcap(std::string prefix,
432 bool explicitFilename)
434 Ptr<NetDevice> nd = Names::Find<NetDevice>(ndName);
435 EnablePcap(prefix, nd, promiscuous, explicitFilename);
439PcapHelperForDevice::EnablePcap(std::string prefix, NetDeviceContainer d, bool promiscuous)
441 for (NetDeviceContainer::Iterator i = d.Begin(); i != d.End(); ++i)
443 Ptr<NetDevice> dev = *i;
444 EnablePcap(prefix, dev, promiscuous);
449PcapHelperForDevice::EnablePcap(std::string prefix, NodeContainer n, bool promiscuous)
451 NetDeviceContainer devs;
452 for (NodeContainer::Iterator i = n.Begin(); i != n.End(); ++i)
455 for (uint32_t j = 0; j < node->GetNDevices(); ++j)
457 devs.Add(node->GetDevice(j));
460 EnablePcap(prefix, devs, promiscuous);
464PcapHelperForDevice::EnablePcapAll(std::string prefix, bool promiscuous)
466 EnablePcap(prefix, NodeContainer::GetGlobal(), promiscuous);
470PcapHelperForDevice::EnablePcap(std::string prefix,
475 NodeContainer n = NodeContainer::GetGlobal();
477 for (NodeContainer::Iterator i = n.Begin(); i != n.End(); ++i)
480 if (node->GetId() != nodeid)
485 NS_ABORT_MSG_IF(deviceid >= node->GetNDevices(),
487 Ptr<NetDevice> nd = node->GetDevice(deviceid);
488 EnablePcap(prefix, nd, promiscuous);
508 EnableAsciiInternal(stream, std::string(), nd,
false);
517 bool explicitFilename)
528 EnableAsciiImpl(stream, std::string(), ndName,
false);
538 bool explicitFilename)
541 EnableAsciiInternal(stream, prefix, nd, explicitFilename);
559 EnableAsciiImpl(stream, std::string(), d);
573 EnableAsciiInternal(stream, prefix, dev,
false);
592 EnableAsciiImpl(stream, std::string(), n);
607 for (
uint32_t j = 0; j < node->GetNDevices(); ++j)
609 devs.
Add(node->GetDevice(j));
612 EnableAsciiImpl(stream, prefix, devs);
641 EnableAsciiImpl(stream, std::string(), nodeid, deviceid,
false);
651 bool explicitFilename)
664 bool explicitFilename)
671 if (node->GetId() != nodeid)
677 deviceid >= node->GetNDevices(),
678 "AsciiTraceHelperForDevice::EnableAscii(): Unknown deviceid = " << deviceid);
682 EnableAsciiInternal(stream, prefix, nd, explicitFilename);
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...
Empty class, used as a default parent class for SimpleRefCount.
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
std::vector< Ptr< NetDevice > >::const_iterator Iterator
NetDevice container iterator.
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.
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
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.
Base class providing common user-level pcap operations for helpers representing net devices.
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.
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.
DataLinkType
This enumeration holds the data link types that will be written to the pcap file.
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.
~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_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 ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.