src/helper/emu-helper.cc
author Nicola Baldo <nbaldo@cttc.es>
Thu Aug 13 09:36:53 2009 +0200 (2009-08-13)
changeset 4709 b0743dbc4e55
parent 4264 9d2e96c4e6e4
child 4712 74ed62336c36
permissions -rw-r--r--
bug 639: add configurable capture size to pcap
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2008 University of Washington
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License version 2 as
     7  * published by the Free Software Foundation;
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    17  */
    18 
    19 #include <string>
    20 
    21 #include "ns3/log.h"
    22 #include "ns3/simulator.h"
    23 #include "ns3/object-factory.h"
    24 #include "ns3/names.h"
    25 #include "ns3/queue.h"
    26 #include "ns3/emu-net-device.h"
    27 #include "ns3/pcap-writer.h"
    28 #include "ns3/config.h"
    29 #include "ns3/packet.h"
    30 
    31 #include "emu-helper.h"
    32 
    33 NS_LOG_COMPONENT_DEFINE ("EmuHelper");
    34 
    35 namespace ns3 {
    36 
    37 EmuHelper::EmuHelper ()
    38 {
    39   NS_LOG_FUNCTION_NOARGS ();
    40   m_queueFactory.SetTypeId ("ns3::DropTailQueue");
    41   m_deviceFactory.SetTypeId ("ns3::EmuNetDevice");
    42 }
    43 
    44 void 
    45 EmuHelper::SetQueue (
    46   std::string type,
    47   std::string n1, const AttributeValue &v1,
    48   std::string n2, const AttributeValue &v2,
    49   std::string n3, const AttributeValue &v3,
    50   std::string n4, const AttributeValue &v4)
    51 {
    52   NS_LOG_FUNCTION_NOARGS ();
    53   m_queueFactory.SetTypeId (type);
    54   m_queueFactory.Set (n1, v1);
    55   m_queueFactory.Set (n2, v2);
    56   m_queueFactory.Set (n3, v3);
    57   m_queueFactory.Set (n4, v4);
    58 }
    59 
    60 void 
    61 EmuHelper::SetAttribute (std::string n1, const AttributeValue &v1)
    62 {
    63   NS_LOG_FUNCTION_NOARGS ();
    64   m_deviceFactory.Set (n1, v1);
    65 }
    66 
    67 void 
    68 EmuHelper::EnablePcap (std::string filename, uint32_t nodeid, uint32_t deviceid, bool promiscuous)
    69 {
    70   NS_LOG_FUNCTION (filename << nodeid << deviceid << promiscuous);
    71   std::ostringstream oss;
    72   oss << filename << "-" << nodeid << "-" << deviceid << ".pcap";
    73   Ptr<PcapWriter> pcap = CreateObject<PcapWriter> ();
    74   pcap->Open (oss.str ());
    75   pcap->WriteEthernetHeader ();
    76 
    77   oss.str ("");
    78   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid;
    79   if (promiscuous)
    80     {
    81       oss << "/$ns3::EmuNetDevice/PromiscSniffer";
    82     }
    83   else
    84     {
    85       oss << "/$ns3::EmuNetDevice/Sniffer";
    86     }
    87   Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&EmuHelper::SniffEvent, pcap));
    88 }
    89 
    90 void 
    91 EmuHelper::EnablePcap (std::string filename, Ptr<NetDevice> nd, bool promiscuous)
    92 {
    93   NS_LOG_FUNCTION (filename << &nd << promiscuous);
    94   EnablePcap (filename, nd->GetNode ()->GetId (), nd->GetIfIndex (), promiscuous);
    95 }
    96 
    97 void 
    98 EmuHelper::EnablePcap (std::string filename, std::string ndName, bool promiscuous)
    99 {
   100   NS_LOG_FUNCTION (filename << ndName << promiscuous);
   101   Ptr<NetDevice> nd = Names::Find<NetDevice> (ndName);
   102   EnablePcap (filename, nd->GetNode ()->GetId (), nd->GetIfIndex (), promiscuous);
   103 }
   104 
   105 void 
   106 EmuHelper::EnablePcap (std::string filename, NetDeviceContainer d, bool promiscuous)
   107 {
   108   NS_LOG_FUNCTION (filename << &d << promiscuous);
   109   for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
   110     {
   111       Ptr<NetDevice> dev = *i;
   112       EnablePcap (filename, dev->GetNode ()->GetId (), dev->GetIfIndex (), promiscuous);
   113     }
   114 }
   115 
   116 void
   117 EmuHelper::EnablePcap (std::string filename, NodeContainer n, bool promiscuous)
   118 {
   119   NS_LOG_FUNCTION (filename << &n << promiscuous);
   120   NetDeviceContainer devs;
   121   for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
   122     {
   123       Ptr<Node> node = *i;
   124       for (uint32_t j = 0; j < node->GetNDevices (); ++j)
   125 	{
   126 	  devs.Add (node->GetDevice (j));
   127 	}
   128     }
   129   EnablePcap (filename, devs, promiscuous);
   130 }
   131 
   132 void
   133 EmuHelper::EnablePcapAll (std::string filename, bool promiscuous)
   134 {
   135   NS_LOG_FUNCTION (filename << promiscuous);
   136   EnablePcap (filename, NodeContainer::GetGlobal (), promiscuous);
   137 }
   138 
   139 void 
   140 EmuHelper::EnableAscii (std::ostream &os, uint32_t nodeid, uint32_t deviceid)
   141 {
   142   NS_LOG_FUNCTION (&os << nodeid << deviceid);
   143   Packet::EnablePrinting ();
   144   std::ostringstream oss;
   145 
   146   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::EmuNetDevice/MacRx";
   147   Config::Connect (oss.str (), MakeBoundCallback (&EmuHelper::AsciiRxEvent, &os));
   148 
   149   oss.str ("");
   150   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::EmuNetDevice/TxQueue/Enqueue";
   151   Config::Connect (oss.str (), MakeBoundCallback (&EmuHelper::AsciiEnqueueEvent, &os));
   152 
   153   oss.str ("");
   154   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::EmuNetDevice/TxQueue/Dequeue";
   155   Config::Connect (oss.str (), MakeBoundCallback (&EmuHelper::AsciiDequeueEvent, &os));
   156 
   157   oss.str ("");
   158   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::EmuNetDevice/TxQueue/Drop";
   159   Config::Connect (oss.str (), MakeBoundCallback (&EmuHelper::AsciiDropEvent, &os));
   160 }
   161 
   162 void 
   163 EmuHelper::EnableAscii (std::ostream &os, NetDeviceContainer d)
   164 {
   165   NS_LOG_FUNCTION (&os << &d);
   166   for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
   167     {
   168       Ptr<NetDevice> dev = *i;
   169       EnableAscii (os, dev->GetNode ()->GetId (), dev->GetIfIndex ());
   170     }
   171 }
   172 
   173 void
   174 EmuHelper::EnableAscii (std::ostream &os, NodeContainer n)
   175 {
   176   NS_LOG_FUNCTION (&os << &n);
   177   NetDeviceContainer devs;
   178   for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
   179     {
   180       Ptr<Node> node = *i;
   181       for (uint32_t j = 0; j < node->GetNDevices (); ++j)
   182 	{
   183 	  devs.Add (node->GetDevice (j));
   184 	}
   185     }
   186   EnableAscii (os, devs);
   187 }
   188 
   189 void
   190 EmuHelper::EnableAsciiAll (std::ostream &os)
   191 {
   192   NS_LOG_FUNCTION (&os);
   193   EnableAscii (os, NodeContainer::GetGlobal ());
   194 }
   195 
   196 NetDeviceContainer
   197 EmuHelper::Install (Ptr<Node> node) const
   198 {
   199   return NetDeviceContainer (InstallPriv (node));
   200 }
   201 
   202 NetDeviceContainer
   203 EmuHelper::Install (std::string nodeName) const
   204 {
   205   Ptr<Node> node = Names::Find<Node> (nodeName);
   206   return NetDeviceContainer (InstallPriv (node));
   207 }
   208 
   209 NetDeviceContainer 
   210 EmuHelper::Install (const NodeContainer &c) const
   211 {
   212   NetDeviceContainer devs;
   213 
   214   for (NodeContainer::Iterator i = c.Begin (); i != c.End (); i++)
   215     {
   216       devs.Add (InstallPriv (*i));
   217     }
   218 
   219   return devs;
   220 }
   221 
   222 Ptr<NetDevice>
   223 EmuHelper::InstallPriv (Ptr<Node> node) const
   224 {
   225   Ptr<EmuNetDevice> device = m_deviceFactory.Create<EmuNetDevice> ();
   226   device->SetAddress (Mac48Address::Allocate ());
   227   node->AddDevice (device);
   228   Ptr<Queue> queue = m_queueFactory.Create<Queue> ();
   229   device->SetQueue (queue);
   230 
   231   return device;
   232 }
   233 
   234 void 
   235 EmuHelper::SniffEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet)
   236 {
   237   NS_LOG_FUNCTION (writer << packet);
   238   writer->WritePacket (packet);
   239 }
   240 
   241 void 
   242 EmuHelper::AsciiEnqueueEvent (
   243   std::ostream *os, 
   244   std::string path, 
   245   Ptr<const Packet> packet)
   246 {
   247   NS_LOG_FUNCTION (&os << path << packet);
   248   *os << "+ " << Simulator::Now ().GetSeconds () << " ";
   249   *os << path << " " << *packet << std::endl;
   250 }
   251 
   252 void 
   253 EmuHelper::AsciiDequeueEvent (
   254   std::ostream *os, 
   255   std::string path, 
   256   Ptr<const Packet> packet)
   257 {
   258   NS_LOG_FUNCTION (&os << path << packet);
   259   *os << "- " << Simulator::Now ().GetSeconds () << " ";
   260   *os << path << " " << *packet << std::endl;
   261 }
   262 
   263 void 
   264 EmuHelper::AsciiDropEvent (
   265   std::ostream *os, 
   266   std::string path, 
   267   Ptr<const Packet> packet)
   268 {
   269   NS_LOG_FUNCTION (&os << path << packet);
   270   *os << "d " << Simulator::Now ().GetSeconds () << " ";
   271   *os << path << " " << *packet << std::endl;
   272 }
   273 
   274 void 
   275 EmuHelper::AsciiRxEvent (
   276   std::ostream *os, 
   277   std::string path, 
   278   Ptr<const Packet> packet)
   279 {
   280   NS_LOG_FUNCTION (&os << path << packet);
   281   *os << "r " << Simulator::Now ().GetSeconds () << " ";
   282   *os << path << " " << *packet << std::endl;
   283 }
   284 
   285 } // namespace ns3