Add IP layer tracing helpers to InternetStackHelper
authorRaj Bhattacharjea <raj.b@gatech.edu>
Wed Apr 02 14:09:36 2008 -0400 (22 months ago)
changeset 28452398826af6b4
parent 2844 6e2d19edddae
child 2865 d40eb18a4da0
Add IP layer tracing helpers to InternetStackHelper
examples/tcp-large-transfer.cc
src/helper/internet-stack-helper.cc
src/helper/internet-stack-helper.h
     1.1 --- a/examples/tcp-large-transfer.cc	Mon Mar 31 15:39:50 2008 -0700
     1.2 +++ b/examples/tcp-large-transfer.cc	Wed Apr 02 14:09:36 2008 -0400
     1.3 @@ -182,9 +182,9 @@
     1.4  
     1.5    std::ofstream ascii;
     1.6    ascii.open ("tcp-large-transfer.tr");
     1.7 -  PointToPointHelper::EnablePcap ("tcp-large-transfer");
     1.8    PointToPointHelper::EnableAscii (ascii);
     1.9  
    1.10 +  InternetStackHelper::EnablePcap ("tcp-large-transfer");
    1.11  
    1.12    Simulator::StopAt (Seconds(1000));
    1.13    Simulator::Run ();
     2.1 --- a/src/helper/internet-stack-helper.cc	Mon Mar 31 15:39:50 2008 -0700
     2.2 +++ b/src/helper/internet-stack-helper.cc	Wed Apr 02 14:09:36 2008 -0400
     2.3 @@ -20,9 +20,13 @@
     2.4  #include "internet-stack-helper.h"
     2.5  #include "ns3/internet-stack.h"
     2.6  #include "ns3/packet-socket-factory.h"
     2.7 +#include "ns3/config.h"
     2.8  
     2.9  namespace ns3 {
    2.10  
    2.11 +std::vector<InternetStackHelper::Trace> InternetStackHelper::m_traces;
    2.12 +std::string InternetStackHelper::m_pcapBaseFilename;
    2.13 +
    2.14  void 
    2.15  InternetStackHelper::Build (NodeContainer c)
    2.16  {
    2.17 @@ -35,5 +39,68 @@
    2.18      }
    2.19  }
    2.20  
    2.21 +void
    2.22 +InternetStackHelper::EnablePcap (std::string filename)
    2.23 +{
    2.24 +  InternetStackHelper::m_pcapBaseFilename = filename;
    2.25 +  Config::Connect ("/NodeList/*/$ns3::Ipv4L3Protocol/Tx",
    2.26 +                              MakeCallback (&InternetStackHelper::LogTxIp));
    2.27 +  Config::Connect ("/NodeList/*/$ns3::Ipv4L3Protocol/Rx",
    2.28 +                              MakeCallback (&InternetStackHelper::LogRxIp));
    2.29 +}
    2.30 +
    2.31 +uint32_t
    2.32 +InternetStackHelper::GetNodeIndex (std::string context)
    2.33 +{
    2.34 +  std::string::size_type pos;
    2.35 +  pos = context.find ("/NodeList/");
    2.36 +  NS_ASSERT (pos == 0);
    2.37 +  std::string::size_type afterNodeIndex = context.find ("/", 11);
    2.38 +  NS_ASSERT (afterNodeIndex != std::string::npos);
    2.39 +  std::string index = context.substr (10, afterNodeIndex - 10);
    2.40 +  std::istringstream iss;
    2.41 +  iss.str (index);
    2.42 +  uint32_t nodeIndex;
    2.43 +  iss >> nodeIndex;
    2.44 +  return nodeIndex;
    2.45 +}
    2.46 +
    2.47 +void
    2.48 +InternetStackHelper::LogTxIp (std::string context, Ptr<const Packet> packet, uint32_t interfaceIndex)
    2.49 +{
    2.50 +  Ptr<PcapWriter> writer = InternetStackHelper::GetStream (GetNodeIndex (context), interfaceIndex);
    2.51 +  writer->WritePacket (packet);
    2.52 +}
    2.53 +
    2.54 +void
    2.55 +InternetStackHelper::LogRxIp (std::string context, Ptr<const Packet> packet, uint32_t interfaceIndex)
    2.56 +{
    2.57 +  Ptr<PcapWriter> writer = InternetStackHelper::GetStream (GetNodeIndex (context), interfaceIndex);
    2.58 +  writer->WritePacket (packet);
    2.59 +}
    2.60 +
    2.61 +Ptr<PcapWriter>
    2.62 +InternetStackHelper::GetStream (uint32_t nodeId, uint32_t interfaceId)
    2.63 +{
    2.64 +  for (std::vector<Trace>::iterator i = m_traces.begin ();
    2.65 +       i != m_traces.end (); i++)
    2.66 +  {
    2.67 +    if (i->nodeId == nodeId &&
    2.68 +        i->interfaceId == interfaceId)
    2.69 +    {
    2.70 +      return i->writer;
    2.71 +    }
    2.72 +  }
    2.73 +  InternetStackHelper::Trace trace;
    2.74 +  trace.nodeId = nodeId;
    2.75 +  trace.interfaceId = interfaceId;
    2.76 +  trace.writer = Create<PcapWriter> ();
    2.77 +  std::ostringstream oss;
    2.78 +  oss << m_pcapBaseFilename << ".pcap-" << nodeId << "-" << interfaceId;
    2.79 +  trace.writer->Open (oss.str ());
    2.80 +  trace.writer->WriteIpHeader ();
    2.81 +  m_traces.push_back (trace);
    2.82 +  return trace.writer;
    2.83 +}
    2.84  
    2.85  } // namespace ns3
     3.1 --- a/src/helper/internet-stack-helper.h	Mon Mar 31 15:39:50 2008 -0700
     3.2 +++ b/src/helper/internet-stack-helper.h	Wed Apr 02 14:09:36 2008 -0400
     3.3 @@ -21,6 +21,9 @@
     3.4  #define INTERNET_STACK_HELPER_H
     3.5  
     3.6  #include "node-container.h"
     3.7 +#include "net-device-container.h"
     3.8 +#include "ns3/pcap-writer.h"
     3.9 +#include "ns3/packet.h"
    3.10  
    3.11  namespace ns3 {
    3.12  
    3.13 @@ -37,6 +40,27 @@
    3.14     * of the ns3::Ipv4, ns3::Udp, and, ns3::Tcp classes.
    3.15     */
    3.16    void Build (NodeContainer c);
    3.17 +
    3.18 +  /**
    3.19 +   * \param filename filename prefix to use for pcap files.
    3.20 +   *
    3.21 +   * Enable pcap output on each protocol instance which is of the
    3.22 +   * ns3::Ipv4L3Protocol type.  Both Tx and Rx events will be logged.
    3.23 +   */
    3.24 +  static void EnablePcap (std::string filename);
    3.25 +
    3.26 +private:
    3.27 +  static void LogRxIp (std::string context, Ptr<const Packet> packet, uint32_t deviceId);
    3.28 +  static void LogTxIp (std::string context, Ptr<const Packet> packet, uint32_t deviceId);
    3.29 +  static Ptr<PcapWriter> GetStream (uint32_t nodeId, uint32_t interfaceId);
    3.30 +  struct Trace {
    3.31 +    uint32_t nodeId;
    3.32 +    uint32_t interfaceId;
    3.33 +    Ptr<PcapWriter> writer;
    3.34 +  };
    3.35 +  static std::string m_pcapBaseFilename;
    3.36 +  static uint32_t GetNodeIndex (std::string context);
    3.37 +  static std::vector<Trace> m_traces;
    3.38  };
    3.39  
    3.40  } // namespace ns3