From 3af7a788550458a6238e93073d66ebcdb231424e Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 29 Mar 2007 23:24:10 +0200 Subject: [PATCH] put together a PcapTrace and test it --- SConstruct | 2 + examples/simple-p2p.cc | 6 +++ src/common/pcap-writer.cc | 4 +- src/common/pcap-writer.h | 2 +- src/node/pcap-trace.cc | 95 +++++++++++++++++++++++++++++++++++++++ src/node/pcap-trace.h | 54 ++++++++++++++++++++++ 6 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 src/node/pcap-trace.cc create mode 100644 src/node/pcap-trace.h diff --git a/SConstruct b/SConstruct index ff05f4afc..28ccff183 100644 --- a/SConstruct +++ b/SConstruct @@ -219,6 +219,7 @@ node.add_sources ([ 'channel.cc', 'node-list.cc', 'ascii-trace.cc', + 'pcap-trace.cc', ]) node.add_headers ([ 'ipv4-header.h', @@ -266,6 +267,7 @@ node.add_inst_headers ([ 'application-list.h', 'onoff-application.h', 'ascii-trace.h', + 'pcap-trace.h', ]) p2p = build.Ns3Module ('p2p', 'src/devices/p2p') diff --git a/examples/simple-p2p.cc b/examples/simple-p2p.cc index d005b12e8..ae17252a5 100644 --- a/examples/simple-p2p.cc +++ b/examples/simple-p2p.cc @@ -49,6 +49,7 @@ #include "ns3/data-rate.h" #include "ns3/ascii-trace.h" +#include "ns3/pcap-trace.h" #include "ns3/internet-node.h" #include "ns3/p2p-channel.h" #include "ns3/p2p-net-device.h" @@ -154,9 +155,14 @@ int main (int argc, char *argv[]) // Configure tracing of all enqueue, dequeue, and NetDevice receive events // Trace output will be sent to the simple-p2p.tr file +#if 0 AsciiTrace trace ("simple-p2p.tr"); trace.TraceAllQueues (); trace.TraceAllNetDeviceRx (); +#else + PcapTrace trace ("simple-p2p.tr"); + trace.TraceAllIp (); +#endif Simulator::StopAt (Seconds(10.0)); diff --git a/src/common/pcap-writer.cc b/src/common/pcap-writer.cc index c95c019cb..fcfee3ffe 100644 --- a/src/common/pcap-writer.cc +++ b/src/common/pcap-writer.cc @@ -48,10 +48,10 @@ PcapWriter::~PcapWriter () } void -PcapWriter::Open (char const *name) +PcapWriter::Open (std::string const &name) { m_writer = new SystemFile (); - m_writer->Open (name); + m_writer->Open (name.c_str ()); } void diff --git a/src/common/pcap-writer.h b/src/common/pcap-writer.h index a605b4b8e..91eaf090b 100644 --- a/src/common/pcap-writer.h +++ b/src/common/pcap-writer.h @@ -46,7 +46,7 @@ public: * This method creates the file if it does not exist. If it * exists, the file is emptied. */ - void Open (char const *name); + void Open (std::string const &name); /** * Write a pcap header in the output file which specifies diff --git a/src/node/pcap-trace.cc b/src/node/pcap-trace.cc new file mode 100644 index 000000000..7dca7bb8c --- /dev/null +++ b/src/node/pcap-trace.cc @@ -0,0 +1,95 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mathieu Lacage + */ +#include "pcap-trace.h" + +#include + +#include "ns3/trace-root.h" +#include "ns3/trace-context.h" +#include "ns3/callback.h" +#include "ns3/pcap-writer.h" + +#include "ipv4.h" +#include "node-list.h" +#include "node.h" + + +namespace ns3 { + + +PcapTrace::PcapTrace (std::string filename) + : m_filename (filename) +{} +PcapTrace::~PcapTrace () +{ + for (std::vector::iterator i = m_traces.begin (); + i != m_traces.end (); i++) + { + delete i->writer; + } +} + +void +PcapTrace::TraceAllIp (void) +{ + TraceRoot::Connect ("/nodes/*/ipv4/interfaces/*/(tx|rx)", + MakeCallback (&PcapTrace::LogIp, this)); +} + +PcapWriter * +PcapTrace::GetStream (uint32_t nodeId, uint32_t interfaceId) +{ + for (std::vector::iterator i = m_traces.begin (); + i != m_traces.end (); i++) + { + if (i->nodeId == nodeId && + i->interfaceId == interfaceId) + { + return i->writer; + } + } + PcapTrace::Trace trace; + trace.nodeId = nodeId; + trace.interfaceId = interfaceId; + trace.writer = new PcapWriter (); + std::ostringstream oss; + oss << m_filename << "-" << nodeId << "-" << interfaceId; + std::string filename = oss.str (); + trace.writer->Open (filename); + trace.writer->WriteIpHeader (); + m_traces.push_back (trace); + return trace.writer; +} + +void +PcapTrace::LogIp (TraceContext const &context, Packet &p) +{ + NodeList::NodeIndex nodeIndex; + context.Get (nodeIndex); + uint32_t nodeId = NodeList::GetNode (nodeIndex)->GetId (); + Ipv4::InterfaceIndex interfaceIndex; + context.Get (interfaceIndex); + PcapWriter *writer = GetStream (nodeId, interfaceIndex); + writer->WritePacket (p); +} + + +}//namespace ns3 diff --git a/src/node/pcap-trace.h b/src/node/pcap-trace.h new file mode 100644 index 000000000..fbd81bcaa --- /dev/null +++ b/src/node/pcap-trace.h @@ -0,0 +1,54 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mathieu Lacage + */ +#ifndef PCAP_TRACE_H +#define PCAP_TRACE_H + +#include +#include + +namespace ns3 { + +class Packet; +class TraceContext; +class PcapWriter; + +class PcapTrace +{ +public: + PcapTrace (std::string filename); + ~PcapTrace (); + + void TraceAllIp (void); +private: + PcapWriter *GetStream (uint32_t nodeId, uint32_t interfaceId); + void LogIp (TraceContext const &context, Packet &p); + std::string m_filename; + struct Trace { + uint32_t nodeId; + uint32_t interfaceId; + PcapWriter *writer; + }; + std::vector m_traces; +}; + +}//namespace ns3 + +#endif /* PCAP_TRACE_H */