SHARE
TWEET

JOwS_2

hyper98 May 26th, 2019 (edited) 133 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
  2. /*
  3.  * Copyright (c) 2016 AGH University of Science and Technology
  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.  * Author: Lukasz Prasnal <prasnal@kt.agh.edu.pl>
  19.  */
  20.  
  21. #include "ns3/core-module.h"
  22. #include "ns3/internet-module.h"
  23. #include "ns3/network-module.h"
  24. #include "ns3/applications-module.h"
  25. #include "ns3/wifi-module.h"
  26. #include "ns3/propagation-module.h"
  27. #include "ns3/mobility-module.h"
  28. #include "ns3/rng-seed-manager.h"
  29. #include "ns3/flow-monitor-helper.h"
  30. #include "ns3/ipv4-flow-classifier.h"
  31.  
  32. using namespace ns3;
  33.  
  34. NS_LOG_COMPONENT_DEFINE ("wifi-qos-test");
  35.  
  36. class SimulationHelper
  37. {
  38. public:
  39.     SimulationHelper ();
  40.     static void PopulateArpCache ();
  41. };
  42.  
  43. SimulationHelper::SimulationHelper ()
  44. {
  45. }
  46.  
  47. //fullfil the ARP cache prior to simulation run
  48. void
  49. SimulationHelper::PopulateArpCache ()
  50. {
  51.   Ptr<ArpCache> arp = CreateObject<ArpCache> ();
  52.   arp->SetAliveTimeout (Seconds (3600 * 24 * 365) );
  53.  
  54.   for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i)
  55.     {
  56.       Ptr<Ipv4L3Protocol> ip = (*i)->GetObject<Ipv4L3Protocol> ();
  57.       NS_ASSERT (ip != 0);
  58.       ObjectVectorValue interfaces;
  59.       ip->GetAttribute ("InterfaceList", interfaces);
  60.  
  61.       for (ObjectVectorValue::Iterator j = interfaces.Begin (); j != interfaces.End (); j++)
  62.         {
  63.           Ptr<Ipv4Interface> ipIface = (*j).second->GetObject<Ipv4Interface> ();
  64.           NS_ASSERT (ipIface != 0);
  65.           Ptr<NetDevice> device = ipIface->GetDevice ();
  66.           NS_ASSERT (device != 0);
  67.           Mac48Address addr = Mac48Address::ConvertFrom (device->GetAddress () );
  68.  
  69.           for (uint32_t k = 0; k < ipIface->GetNAddresses (); k++)
  70.             {
  71.               Ipv4Address ipAddr = ipIface->GetAddress (k).GetLocal();
  72.               if (ipAddr == Ipv4Address::GetLoopback ())
  73.                 continue;
  74.  
  75.               ArpCache::Entry *entry = arp->Add (ipAddr);
  76.               Ipv4Header ipv4Hdr;
  77.               ipv4Hdr.SetDestination (ipAddr);
  78.               Ptr<Packet> p = Create<Packet> (100);
  79.               entry->MarkWaitReply (ArpCache::Ipv4PayloadHeaderPair (p, ipv4Hdr) );
  80.               entry->MarkAlive (addr);
  81.             }
  82.         }
  83.     }
  84.  
  85.     for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i)
  86.       {
  87.         Ptr<Ipv4L3Protocol> ip = (*i)->GetObject<Ipv4L3Protocol> ();
  88.         NS_ASSERT (ip != 0);
  89.         ObjectVectorValue interfaces;
  90.         ip->GetAttribute ("InterfaceList", interfaces);
  91.  
  92.         for (ObjectVectorValue::Iterator j = interfaces.Begin (); j != interfaces.End (); j ++)
  93.           {
  94.             Ptr<Ipv4Interface> ipIface = (*j).second->GetObject<Ipv4Interface> ();
  95.             ipIface->SetAttribute ("ArpCache", PointerValue (arp) );
  96.           }
  97.       }
  98. }
  99.  
  100.  
  101.  
  102. /* ===== main function ===== */
  103.  
  104. int main (int argc, char *argv[])
  105. {
  106.   uint32_t nSTA = 5;
  107.   uint32_t packetSize = 1472;
  108.   Time appsStart = Seconds (0);
  109.   float simTime = 5;
  110.   float calcStart = 0;
  111.   double Mbps = 54;
  112.   uint32_t seed = 1;
  113.  
  114.  
  115. /* ===== Command Line parameters ===== */
  116.  
  117.   CommandLine cmd;
  118.   cmd.AddValue ("nSTA",      "Number of stations",                 nSTA);
  119.   cmd.AddValue ("pSize",     "Packet size [B]",                    packetSize);
  120.   cmd.AddValue ("end",       "simulation time [s]",                simTime);
  121.   cmd.AddValue ("calcStart", "start of results analysis [s]",      calcStart);
  122.   cmd.AddValue ("Mbps",      "traffic generated per queue [Mbps]", Mbps);
  123.   cmd.AddValue ("seed",      "Seed",                               seed);
  124.   cmd.Parse (argc, argv);
  125.  
  126.   Time simulationTime = Seconds (simTime);
  127.   ns3::RngSeedManager::SetSeed (seed);
  128.  
  129.   Packet::EnablePrinting ();
  130.  
  131.   NodeContainer sta;
  132.   sta.Create (nSTA+1);
  133.  
  134.  
  135.  
  136. /* ======== Positioning / Mobility ======= */
  137.  
  138.   Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
  139.   positionAlloc->Add (Vector (0.0, 0.0, 0.0));
  140.   for (uint32_t i = 0; i < nSTA; i++){
  141.         if (i==0)
  142.               positionAlloc->Add (Vector (0.0, 0.0, 0.0));
  143.         else
  144.                 positionAlloc->Add (Vector (0.0, 0.0, 0.0));
  145.     }
  146.  
  147.  
  148.   MobilityHelper mobility;
  149.   mobility.SetPositionAllocator (positionAlloc);
  150.   mobility.SetMobilityModel ("ns3::ConstantVelocityMobilityModel");
  151.  
  152.   mobility.Install (sta);
  153.  
  154.  
  155.  
  156. /* ===== Propagation Model configuration ===== */
  157.  
  158.   YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
  159.  
  160.  
  161.  
  162. /* ===== MAC and PHY configuration ===== */
  163.  
  164.   YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
  165.   phy.SetChannel (channel.Create ());
  166.  
  167.   WifiHelper wifi;
  168.   QosWifiMacHelper mac = QosWifiMacHelper::Default (); //802.11a
  169.   wifi.SetStandard (WIFI_PHY_STANDARD_80211a);
  170.  
  171.   //MAC parameters
  172.   //for complete list of available parameters - see Attributes on https://www.nsnam.org/doxygen/classns3_1_1_adhoc_wifi_mac.html#pri-methods
  173.   mac.SetType ("ns3::AdhocWifiMac",
  174.                "Ssid", SsidValue (Ssid ("TEST")) );
  175.   mac.SetQueueControllerForTid (7, "ns3::StrictPriorityQueueController");
  176.     mac.SetQueueControllerForTid (6, "ns3::StrictPriorityQueueController");
  177.   mac.SetQueueControllerForTid (5, "ns3::StrictPriorityQueueController");
  178.   mac.SetQueueControllerForTid (4, "ns3::StrictPriorityQueueController");
  179.  
  180.   //WiFi Remote Station Manager parameters
  181.  
  182.   //Constant Rate setting - see Attributes on https://www.nsnam.org/doxygen/classns3_1_1_constant_rate_wifi_manager.html#pri-attribs
  183.   wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
  184.                                 "DataMode", StringValue ("OfdmRate54Mbps") );
  185.  
  186.  
  187.   NetDeviceContainer staDevices = wifi.Install (phy, mac, sta);
  188.  
  189.  
  190.  
  191.  
  192. /* ===== Internet stack ===== */
  193.  
  194.   InternetStackHelper stack;
  195.   stack.Install (sta);
  196.  
  197.   Ipv4AddressHelper address;
  198.  
  199.   address.SetBase ("192.168.1.0", "255.255.255.0");
  200.   Ipv4InterfaceContainer staIf;
  201.   staIf = address.Assign (staDevices);
  202.  
  203.  
  204.  
  205. /* ===== Setting applications ===== */
  206.  
  207.   //Configure traffic destination (sink)
  208.   uint32_t destinationSTANumber = nSTA; //for one common traffic destination
  209.   Ipv4Address destination = staIf.GetAddress (destinationSTANumber);
  210.   Ptr<Node> dest = sta.Get (destinationSTANumber);
  211.  
  212.   PacketSinkHelper sink ("ns3::TcpSocketFactory", InetSocketAddress (destination, 1000) );
  213.   sink.Install (dest);
  214.  
  215.  
  216.   //Configure CBR traffic sources
  217.   DataRate dataRate = DataRate (1000000 * Mbps);
  218.  
  219.   for (uint32_t i = 0; i < nSTA; i++)
  220.     {
  221.       Ptr<Node> node = sta.Get(i);
  222.  
  223.       OnOffHelper cbr  ("ns3::TcpSocketFactory", InetSocketAddress (destination, 1000) );
  224.       cbr.SetConstantRate (dataRate, packetSize);
  225.       cbr.SetAttribute ("StartTime",  TimeValue (appsStart) );
  226.       cbr.SetAttribute ("StopTime",   TimeValue (simulationTime) );
  227.  
  228.       cbr.Install (node);
  229.  
  230.     }
  231.  
  232.  
  233.  
  234. /* ===== tracing configuration ====== */
  235.  
  236.   //phy.EnablePcap ("out", nSTA-1, 0); // sniffing to PCAP file
  237.  
  238.   //AsciiTraceHelper ascii;
  239.   //phy.EnableAsciiAll (ascii.CreateFileStream ("out.tr"));
  240.   //phy.EnableAscii (ascii.CreateFileStream ("out.tr"), sta.Get (0)->GetDevice (0));
  241.  
  242.   FlowMonitorHelper flowmon_helper;
  243.   Ptr<FlowMonitor> monitor = flowmon_helper.InstallAll ();
  244.   monitor->SetAttribute ("StartTime", TimeValue (Seconds (calcStart) ) ); //Time from which flowmonitor statistics are gathered.
  245.   monitor->SetAttribute ("DelayBinWidth", DoubleValue (0.001));
  246.   monitor->SetAttribute ("JitterBinWidth", DoubleValue (0.001));
  247.   monitor->SetAttribute ("PacketSizeBinWidth", DoubleValue (20));
  248.  
  249.  
  250. /* ===== running simulation ========= */
  251.  
  252.   SimulationHelper::PopulateArpCache ();
  253.   Simulator::Stop (simulationTime);
  254.   Simulator::Run ();
  255.   Simulator::Destroy ();
  256.  
  257.  
  258.  
  259. /* ===== printing results ===== */
  260.  
  261.   monitor->CheckForLostPackets ();
  262.  
  263.   //monitor->SerializeToXmlFile ("out.xml", true, true); // sniffing to XML file
  264.  
  265.   std::string proto;
  266.   //initialize variables for overall results calculation
  267.   uint64_t txBytes = 0, rxBytes = 0, txPackets = 0, rxPackets = 0, lostPackets = 0;
  268.   double throughput;
  269.   Time delaySum = Seconds (0), jitterSum = Seconds (0);
  270.  
  271.   Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon_helper.GetClassifier ());
  272.   //iterate over traffic flows
  273.   std::map< FlowId, FlowMonitor::FlowStats > stats = monitor->GetFlowStats ();
  274.   for (std::map<FlowId, FlowMonitor::FlowStats>::iterator flow = stats.begin (); flow != stats.end (); flow++)
  275.     {
  276.       Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (flow->first);
  277.  
  278.       //print results for given traffic flow
  279.       switch (t.protocol)
  280.         {
  281.           case (6):
  282.             proto = "TCP";
  283.             break;
  284.           case (17):
  285.             proto = "UDP";
  286.             break;
  287.           default:
  288.             exit (1);
  289.         }
  290.       std::cout << "FlowID: " << flow->first << "(" << proto << " "
  291.                 << t.sourceAddress << "/" << t.sourcePort << " --> "
  292.                 << t.destinationAddress << "/" << t.destinationPort << ")" <<
  293.       std::endl;
  294.  
  295.       std::cout << "  Tx bytes:\t"     << flow->second.txBytes << std::endl;
  296.       std::cout << "  Rx bytes:\t"     << flow->second.rxBytes << std::endl;
  297.       std::cout << "  Tx packets:\t"   << flow->second.txPackets << std::endl;
  298.       std::cout << "  Rx packets:\t"   << flow->second.rxPackets << std::endl;
  299.       std::cout << "  Lost packets:\t" << flow->second.lostPackets << std::endl;
  300.       if (flow->second.rxPackets > 0)
  301.         {
  302.           //std::cout << "  Throughput:\t"   << flow->second.rxBytes * 8.0 / (flow->second.timeLastRxPacket.GetSeconds ()-flow->second.timeFirstTxPacket.GetSeconds ()) / 1000000  << " Mb/s" << std::endl;
  303.           std::cout << "  Throughput:\t"   << flow->second.rxBytes * 8.0 / (simulationTime - Seconds (calcStart)).GetMicroSeconds ()  << " Mb/s" << std::endl;
  304.           std::cout << "  Mean delay:\t"   << (double)(flow->second.delaySum / (flow->second.rxPackets)).GetMicroSeconds () / 1000 << " ms" << std::endl;
  305.           if (flow->second.rxPackets > 1)
  306.             std::cout << "  Mean jitter:\t"  << (double)(flow->second.jitterSum / (flow->second.rxPackets - 1)).GetMicroSeconds () / 1000 << " ms" << std::endl;
  307.           else
  308.             std::cout << "  Mean jitter:\t---"   << std::endl;
  309.         }
  310.       else
  311.         {
  312.           std::cout << "  Throughput:\t0 Mb/s" << std::endl;
  313.           std::cout << "  Mean delay:\t---"    << std::endl;
  314.           std::cout << "  Mean jitter:\t---"   << std::endl;
  315.         }
  316.  
  317.       //increase variables for overall results calculation
  318.       txBytes     += flow->second.txBytes;
  319.       rxBytes     += flow->second.rxBytes;
  320.       txPackets   += flow->second.txPackets;
  321.       rxPackets   += flow->second.rxPackets;
  322.       lostPackets += flow->second.lostPackets;
  323.       //throughput  += (flow->second.rxPackets > 0 ? flow->second.rxBytes * 8.0 / (flow->second.timeLastRxPacket.GetSeconds ()-flow->second.timeFirstTxPacket.GetSeconds ()) / 1000000 : 0);
  324.       throughput  += (flow->second.rxPackets > 0 ? flow->second.rxBytes * 8.0 / (simulationTime - Seconds (calcStart)).GetMicroSeconds () : 0);
  325.       delaySum    += flow->second.delaySum;
  326.       jitterSum   += flow->second.jitterSum;
  327.     }
  328.  
  329.  
  330.   //print overall results
  331.   std::cout << "=======================Total: =====================================" << std::endl;
  332.  
  333.   std::cout << "  Tx bytes:\t"     << txBytes     << std::endl;
  334.   std::cout << "  Rx bytes:\t"     << rxBytes     << std::endl;
  335.   std::cout << "  Tx packets:\t"   << txPackets   << std::endl;
  336.   std::cout << "  Rx packets:\t"   << rxPackets   << std::endl;
  337.   std::cout << "  Lost packets:\t" << lostPackets << std::endl;
  338.   std::cout << "  Throughput:\t"   << throughput  << " Mb/s" << std::endl;
  339.   if (rxPackets > 0)
  340.     {
  341.       std::cout << "  Mean delay:\t"   << (double)(delaySum / (rxPackets)).GetMicroSeconds () / 1000 << " ms" << std::endl;
  342.       if (rxPackets > 1)
  343.         std::cout << "  Mean jitter:\t"  << (double)(jitterSum / (rxPackets - 1)).GetMicroSeconds () / 1000  << " ms" << std::endl;
  344.       else
  345.         std::cout << "  Mean jitter:\t---"   << std::endl;
  346.     }
  347.   else
  348.     {
  349.       std::cout << "  Mean delay:\t---"    << std::endl;
  350.       std::cout << "  Mean jitter:\t---"   << std::endl;
  351.     }
  352.  
  353.  
  354.   return 0;
  355. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top