Advertisement
siddharth-gangadhar

cwnd_issue

Jun 7th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.43 KB | None | 0 0
  1. /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
  2. /*
  3.  * Copyright (c) 2012 NICT
  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: Hajime Tazaki <tazaki@nict.go.jp>
  19.  */
  20.  
  21. #include<iostream>
  22. #include<fstream>
  23. #include <vector>
  24. #include <sstream>
  25. #include <string>
  26. #include "ns3/log.h"
  27. #include "ns3/core-module.h"
  28. #include "ns3/network-module.h"
  29. #include "ns3/internet-module.h"
  30. #include "ns3/point-to-point-module.h"
  31. #include "ns3/applications-module.h"
  32. #include "ns3/flow-monitor-module.h"
  33. #include "ns3/config-store-module.h"
  34. #include "ns3/packet-sink.h"
  35.  
  36.  
  37. using namespace ns3;
  38.  
  39. NS_LOG_COMPONENT_DEFINE ("DceNs3Dumbbell");
  40.  
  41. std::string sock_factory = "ns3::TcpSocketFactory";
  42. int m_seed = 1;
  43. double error_p = 0;
  44. double startTime = 4.0;
  45. double stopTime = 34.0;
  46. int m_nNodes = 2;
  47. bool enablePcap = true;
  48. std::string pcapFile = "dce-ns3-comparison";
  49. uint32_t m_pktSize = 1500;
  50. bool m_frag = false;
  51. bool logging = false;
  52. bool pktDebug = true;
  53. std::vector<std::string> tcpVector;
  54. //std::string transProt = "ns3::TcpReno,ns3::TcpReno";
  55. std::string transProt = "TcpReno";
  56. uint32_t countDrops = 0;
  57. uint32_t countDropsDce = 0;
  58. uint32_t countTx = 0;
  59. uint32_t appcountDrops = 0;
  60. std::string accessLeftSpeed = "10Mbps";
  61. std::string accessRightSpeed = "10Mbps";
  62. std::string accessDelay = "0.1ms";
  63. std::string bottleneckSpeed = "2Mbps";
  64. std::string changingDelay = "0.1ms";
  65. std::string bottleneckDelay = "100ms";
  66. uint32_t queue_size = 65535;
  67. bool qMonitoring = false;
  68. std::string qSizeFileName = "routerQueueSize.txt";
  69. std::string cwnd_tr_file = "test";
  70. std::string ssthresh_tr_file = "test";
  71. std::string error_model_type = "rate";
  72. std::string instTput_file = "test";
  73. double old_time = 0.0;
  74. EventId output;
  75. Time current = Time::FromInteger(3, Time::S);  //Only record cwnd and ssthresh values every 3 seconds
  76. bool first = true;
  77. std::string node_addr_file_name = "node_addresses.csv";
  78. uint16_t flowStepTime = 10;
  79. uint16_t pcapStartTime = 10;
  80.  
  81. static void
  82. PcapScheduler (PointToPointHelper accessLeftLink, std::string pcapFile, NodeContainer lefts)
  83. {
  84.   accessLeftLink.EnablePcap (pcapFile, lefts);
  85. }
  86.  
  87. static void
  88. CwndTracer (Ptr<OutputStreamWrapper> stream, uint32_t oldval, uint32_t newval)
  89. {
  90.   *stream->GetStream() << Simulator::Now().GetSeconds() << "," << oldval/1500 << "," << newval/1500 << std::endl;
  91. }
  92. static void
  93. TraceCwnd (std::string cwnd_tr_file_name)
  94. {
  95.   AsciiTraceHelper ascii;
  96.   Ptr<OutputStreamWrapper>stream = ascii.CreateFileStream(cwnd_tr_file_name.c_str());
  97.     Config::ConnectWithoutContext ("/NodeList/0/$ns3::TcpL4Protocol/SocketList/0/CongestionWindow",MakeBoundCallback (&CwndTracer, stream));
  98.  
  99. }
  100.  
  101.  
  102. static void
  103. SendAppTx (Ptr<const Packet> p)
  104. {
  105.   appcountDrops++;
  106. }
  107.  
  108. static void
  109. RxDrop (Ptr<const Packet> p)
  110. {
  111.   countDrops++;
  112. }
  113.  
  114. static void
  115. SenderTx (Ptr<const Packet> p)
  116. {
  117.   countTx++;
  118. }
  119.  
  120. static void
  121. routerQSize(Ptr<Queue> queue, Ptr<OutputStreamWrapper> stream)
  122. {
  123.  
  124.   if (!Simulator::IsFinished())
  125.     {
  126.     *stream->GetStream() << Simulator::Now().GetSeconds() << "," << queue->GetNPackets() << std::endl;
  127.     Simulator::Schedule (Seconds(0.001), &routerQSize, queue, stream);
  128.     }
  129. }
  130.  
  131. static void
  132. instTput(Ptr<PacketSink> pkt_sink, Ptr<OutputStreamWrapper> stream, double startTime, double stopTime)
  133. {
  134.   if (!Simulator::IsFinished())
  135.     {
  136.       *stream->GetStream() << Simulator::Now().GetSeconds() << "," << (pkt_sink->GetTotalRx() * 8) / (Simulator::Now().GetSeconds() - startTime) << std::endl;
  137.       Simulator::Schedule (Seconds(0.001), &instTput, pkt_sink, stream, startTime, stopTime);
  138.     }
  139. }
  140.  
  141. static void
  142. instTputCaller (std::string instTput_file_name, Ptr<PacketSink> pkt_sink, double startTime, double stopTime){
  143.   AsciiTraceHelper ascii;
  144.   Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream(instTput_file_name.c_str());
  145.   instTput(pkt_sink, stream, startTime, stopTime);
  146. }
  147.  
  148. static void
  149. SimStatus()
  150. {
  151.   if (!Simulator::IsFinished())
  152.     {
  153.       std::cout << "The time elapsed is " << Simulator::Now().GetSeconds() << std::endl;
  154.       Simulator::Schedule(Seconds(30), &SimStatus);
  155.     }
  156. }
  157.  
  158. int
  159. main (int argc, char *argv[])
  160. {
  161.  
  162.   if (logging){
  163.     LogComponentEnable ("OnOffApplication", LOG_LEVEL_ALL);
  164.     LogComponentEnable ("BulkSendApplication", LOG_LEVEL_ALL);
  165.     LogComponentEnable ("PacketSink", LOG_LEVEL_ALL);
  166.     LogComponentEnable ("Socket", LOG_LEVEL_ALL);
  167.     LogComponentEnable ("TcpSocketBase", LOG_LEVEL_ALL);
  168.     LogComponentEnable ("TcpL4Protocol", LOG_LEVEL_ALL);
  169.     LogComponentEnable ("TcpSocket", LOG_LEVEL_ALL);
  170.   }
  171.  
  172.   CommandLine cmd;
  173.   cmd.AddValue("cwnd_tr_name", "Name of output trace file", cwnd_tr_file);
  174.   cmd.AddValue("ssthresh_tr_name", "Name of output trace file", ssthresh_tr_file);
  175.   cmd.AddValue ("seed", "randomize seed", m_seed);
  176.   cmd.AddValue ("nNodes", "the number of nodes in left side", m_nNodes);
  177.   cmd.AddValue ("stopTime", "duration", stopTime);
  178.   cmd.AddValue ("enablePcap", "pcap", enablePcap);
  179.   cmd.AddValue ("pktSize", "packet size", m_pktSize);
  180.   cmd.AddValue ("frag", "fragment", m_frag);
  181.   cmd.AddValue ("error", "Packet error rate", error_p);
  182.   cmd.AddValue ("transProt", "Transport Protocol to use", transProt);
  183.   cmd.AddValue ("pktDebug", "Packet Path Debugging", pktDebug);
  184.   cmd.AddValue ("accessDelay", "Access Delay", accessDelay);
  185.   cmd.AddValue ("changingDelay", "Access Changing Delay", changingDelay);
  186.   cmd.AddValue ("bottleneckSpeed", "Bottleneck Speed", bottleneckSpeed);
  187.   cmd.AddValue ("bottleneckDelay", "Bottleneck Delay", bottleneckDelay);
  188.   cmd.AddValue ("queue_size", "Bottleneck Queue Size", queue_size);
  189.   cmd.AddValue ("pcapFile", "pcap file name", pcapFile);
  190.   cmd.AddValue ("qMonitoring", "Monitor queue", qMonitoring);
  191.   cmd.AddValue ("qSizeFileName", "File name of queue size", qSizeFileName);
  192.   cmd.AddValue ("error_model_type", "Error model type", error_model_type);
  193.   cmd.AddValue ("accessLeftSpeed", "Access left speed", accessLeftSpeed);
  194.   cmd.AddValue ("accessRightSpeed", "Access right speed", accessRightSpeed);
  195.   cmd.AddValue ("flowStepTime", "Start of following flows", flowStepTime);
  196.   cmd.AddValue ("pcapStartTime", "pcap measurement start time", pcapStartTime);
  197.   cmd.AddValue ("instTput_file", "instantaneous throughput file name", instTput_file);
  198.  
  199.   cmd.Parse (argc, argv);
  200.  
  201.   SeedManager::SetSeed (m_seed);
  202.  
  203.   m_nNodes = 1;
  204.  
  205.   //NodeContainer lefts, routers, rights, nodes;
  206.   NodeContainer lefts;
  207.   lefts.Create (m_nNodes);
  208.   NodeContainer routers;
  209.   routers.Create (2);
  210.   NodeContainer rights;
  211.   rights.Create (m_nNodes);
  212.   NodeContainer nodes;
  213.   nodes = NodeContainer (lefts, routers, rights);
  214.  
  215.   InternetStackHelper internetStack;
  216.  
  217.   GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
  218.  
  219.   Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (1448));
  220.  
  221.  
  222.   internetStack.Install (nodes);
  223.  
  224.   std::stringstream leftNodeId, rightNodeId;
  225.   for (int i =0; i<m_nNodes; i++)
  226.     {
  227.     leftNodeId << lefts.Get(i)->GetId ();
  228.     rightNodeId << rights.Get(i)->GetId ();
  229.     std::string specificLeftNode = "/NodeList/" + leftNodeId.str () + "/$ns3::TcpL4Protocol/SocketType";
  230.     std::string specificRightNode = "/NodeList/" + rightNodeId.str () + "/$ns3::TcpL4Protocol/SocketType";
  231.     std::cout << "specific left node is " << leftNodeId << std::endl;
  232.     std::cout << "specific right node is " << rightNodeId << std::endl;
  233.     if (transProt == "TcpTahoe")
  234.       {
  235.         Config::Set(specificLeftNode, TypeIdValue (TcpTahoe::GetTypeId()));
  236.         Config::Set(specificRightNode, TypeIdValue (TcpTahoe::GetTypeId()));
  237.       }
  238.     else if (transProt == "TcpReno")
  239.       {
  240.         Config::Set(specificLeftNode, TypeIdValue (TcpReno::GetTypeId()));
  241.         Config::Set(specificRightNode, TypeIdValue (TcpReno::GetTypeId()));
  242.       }
  243.     else if (transProt == "TcpNewReno")
  244.       {
  245.         Config::Set(specificLeftNode, TypeIdValue (TcpNewReno::GetTypeId()));
  246.         Config::Set(specificRightNode, TypeIdValue (TcpNewReno::GetTypeId()));
  247.       }
  248.     else if (transProt == "TcpWestwood")
  249.       {
  250.         Config::Set(specificLeftNode, TypeIdValue (TcpWestwood::GetTypeId()));
  251.         Config::Set(specificRightNode, TypeIdValue (TcpWestwood::GetTypeId()));
  252.         Config::SetDefault("ns3::TcpWestwood::FilterType", EnumValue(TcpWestwood::TUSTIN));
  253.       }
  254.     else if (transProt == "TcpWestwoodPlus")
  255.       {
  256.         Config::Set(specificLeftNode, TypeIdValue (TcpWestwood::GetTypeId()));
  257.         Config::Set(specificRightNode, TypeIdValue (TcpWestwood::GetTypeId()));
  258.         Config::SetDefault("ns3::TcpWestwood::ProtocolType", EnumValue(TcpWestwood::WESTWOODPLUS));
  259.         Config::SetDefault("ns3::TcpWestwood::FilterType", EnumValue(TcpWestwood::TUSTIN));
  260.       }
  261.     else
  262.       {
  263.         NS_LOG_DEBUG ("Invalid TCP version");
  264.         exit (1);
  265.       }
  266.    }
  267.  
  268.   PointToPointHelper accessLeftLink;
  269.   PointToPointHelper changingRTTLink;
  270.  
  271.  
  272.   Ipv4AddressHelper address;
  273.  
  274.   RateErrorModel rate_error_model;
  275.   Ptr<BurstErrorModel> burst_error_model = CreateObject<BurstErrorModel> ();
  276.  
  277.   if (error_model_type.compare("rate") == 0)
  278.     {
  279.       Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable>();
  280.       uv->SetStream (50);
  281.       rate_error_model.SetRandomVariable(uv);
  282.       rate_error_model.SetUnit(RateErrorModel::ERROR_UNIT_PACKET);
  283.       rate_error_model.SetRate(error_p);
  284.     }
  285.   else if (error_model_type.compare("burst") == 0)
  286.     {
  287.     Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable>();
  288.       uv->SetStream (50);
  289.     burst_error_model->SetRandomVariable(uv);
  290.     burst_error_model->SetAttribute("ErrorRate", DoubleValue (error_p));
  291.     }
  292.   else
  293.     {
  294.       NS_LOG_DEBUG ("Invalid error model");
  295.       exit (1);
  296.     }
  297.  
  298.  
  299.   NetDeviceContainer dev0;
  300.   for (int i = 0; i < m_nNodes; i++)
  301.     {
  302.     std::ostringstream oss;
  303.     oss << "10.0." << i << ".0";
  304.     address.SetBase (oss.str ().c_str (), "255.255.255.0");
  305.     if (i==1){
  306.       accessLeftLink.SetChannelAttribute ("Delay", StringValue (accessDelay));
  307.       accessLeftLink.SetDeviceAttribute ("DataRate", StringValue (accessLeftSpeed));
  308.       dev0 = accessLeftLink.Install (NodeContainer (lefts.Get (i), routers.Get (0)));
  309.     }
  310.     else{
  311.       changingRTTLink.SetChannelAttribute ("Delay", StringValue (changingDelay));
  312.       changingRTTLink.SetDeviceAttribute ("DataRate", StringValue (accessLeftSpeed));
  313.       dev0 = changingRTTLink.Install (NodeContainer (lefts.Get (i), routers.Get (0)));
  314.  
  315.     }
  316.     address.Assign (dev0);
  317.  
  318.     }
  319.  
  320.  
  321.   PointToPointHelper bottleneckLink;
  322.   bottleneckLink.SetDeviceAttribute ("DataRate", StringValue (bottleneckSpeed));
  323.   bottleneckLink.SetChannelAttribute ("Delay", StringValue (bottleneckDelay));
  324.   bottleneckLink.SetQueue ("ns3::DropTailQueue",
  325.       "Mode", StringValue ("QUEUE_MODE_BYTES"),
  326.       "MaxBytes", UintegerValue (queue_size));
  327.  
  328.   if (error_model_type.compare("rate") == 0)
  329.     {
  330.       bottleneckLink.SetDeviceAttribute ("ReceiveErrorModel", PointerValue (&rate_error_model));
  331.     }
  332.   else if (error_model_type.compare("burst") == 0)
  333.     {
  334.       bottleneckLink.SetDeviceAttribute ("ReceiveErrorModel", PointerValue (burst_error_model));
  335.     }
  336.   else
  337.     {
  338.       NS_LOG_DEBUG ("Invalid error model");
  339.       exit (1);
  340.     }
  341.  
  342.   NetDeviceContainer dev1;
  343.   std::ostringstream oss;
  344.   oss << "10.1.0.0";
  345.   address.SetBase (oss.str ().c_str (), "255.255.255.0");
  346.   dev1 = bottleneckLink.Install (NodeContainer (routers.Get (0), routers.Get (1)));
  347.   address.Assign (dev1);
  348.  
  349.   PointToPointHelper accessRightLink;
  350.   accessRightLink.SetDeviceAttribute ("DataRate", StringValue (accessRightSpeed));
  351.   accessRightLink.SetChannelAttribute ("Delay", StringValue (accessDelay));
  352.  
  353.   NetDeviceContainer dev2;
  354.   for (int i = 0; i < m_nNodes; i++)
  355.     {
  356.     std::ostringstream oss;
  357.     oss << "10.2." << i << ".0";
  358.     address.SetBase (oss.str ().c_str (), "255.255.255.0");
  359.     if (i==1){
  360.       accessRightLink.SetChannelAttribute ("Delay", StringValue (accessDelay));
  361.       accessRightLink.SetDeviceAttribute ("DataRate", StringValue (accessRightSpeed));
  362.       dev2 = accessRightLink.Install (NodeContainer (routers.Get (1), rights.Get (i)));
  363.     }
  364.     else{
  365.       changingRTTLink.SetChannelAttribute ("Delay", StringValue (changingDelay));
  366.       changingRTTLink.SetDeviceAttribute ("DataRate", StringValue (accessRightSpeed));
  367.       dev2 = changingRTTLink.Install (NodeContainer (routers.Get (1), rights.Get (i)));
  368.  
  369.     }
  370.     address.Assign (dev2);
  371.  
  372.     }
  373.  
  374.   Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
  375.  
  376.   ApplicationContainer apps;
  377.   BulkSendHelper ftp(sock_factory, InetSocketAddress (Ipv4Address ("10.2.0.2"), 2000));
  378.   //BulkSendHelper ftp(sock_factory, Address());
  379.   for(int i=0; i<m_nNodes; i++)
  380.     {
  381.     std::ostringstream oss;
  382.     oss << "10.2." << i << ".2";
  383.     ftp.SetAttribute ("Remote", AddressValue (InetSocketAddress (Ipv4Address (oss.str ().c_str ()), 2000)));
  384.     ftp.SetAttribute ("SendSize", UintegerValue (m_pktSize));
  385.     ftp.SetAttribute ("MaxBytes", UintegerValue (0));
  386.  
  387.     apps = ftp.Install (lefts.Get(i));
  388.     apps.Start (Seconds (startTime + flowStepTime*i));
  389.     }
  390.  
  391.   Ptr<BulkSendApplication> senderNode = apps.Get(0)->GetObject<BulkSendApplication> ();
  392.   senderNode->TraceConnectWithoutContext("Tx", MakeCallback (&SendAppTx));
  393.  
  394.   ApplicationContainer sinkApp;
  395.   PacketSinkHelper sink = PacketSinkHelper (sock_factory, InetSocketAddress (Ipv4Address::GetAny (), 2000));
  396.   sinkApp = sink.Install (rights);
  397.   sinkApp.Start (Seconds (3.9999));
  398.  
  399.   Ptr<NetDevice> netQ = dev1.Get(0);
  400.   PointerValue val;
  401.   netQ->GetAttribute("TxQueue", val);
  402.   Ptr<Queue> queue = val.Get<Queue>();
  403.   Ptr<DropTailQueue> droptail = DynamicCast<DropTailQueue> (queue);
  404.   AsciiTraceHelper ascii;
  405.   Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream(qSizeFileName);
  406.   Simulator::Schedule(Seconds(0.0001), &routerQSize, queue, stream);
  407.  
  408.   Ptr<NetDevice> netDrop = dev1.Get(1);
  409.   netDrop->TraceConnectWithoutContext ("PhyRxDrop", MakeCallback(&RxDrop));
  410.  
  411.   Ptr<NetDevice> senderND = dev0.Get(0);
  412.   senderND->TraceConnectWithoutContext("PhyTxBegin", MakeCallback (&SenderTx));
  413.  
  414.   if (enablePcap){
  415.     Simulator::Schedule(Seconds(pcapStartTime), &PcapScheduler, accessLeftLink, pcapFile, lefts);
  416.  }
  417.  
  418.  
  419.   Ptr<PacketSink> pkt_sink;
  420.   for (int i = 0;i<m_nNodes-1;i++){
  421.     Simulator::Schedule(Seconds(0.00001), &TraceCwnd, cwnd_tr_file);
  422.     pkt_sink = sinkApp.Get(i)->GetObject<PacketSink>();
  423.     Simulator::Schedule(Seconds(0.00001), &instTputCaller, instTput_file, pkt_sink, startTime, stopTime);
  424.     }
  425.  
  426.  
  427.   Simulator::Schedule (Seconds(0.001), &SimStatus);
  428.   Simulator::Stop (Seconds (stopTime));
  429.   Simulator::Run ();
  430.  
  431.   Ptr<PacketSink> pktsink;
  432.   uint64_t total = 0;
  433.   uint32_t queueDrops = droptail->GetTotalDroppedPackets();
  434.   uint32_t queueRecvs = droptail->GetTotalReceivedPackets();
  435.  
  436.   for (int i = 0; i < m_nNodes; i++)
  437.     {
  438.     pktsink = sinkApp.Get (i)->GetObject<PacketSink> ();
  439.     total += pktsink->GetTotalRx () ;
  440.     }
  441.  
  442.   if (pktDebug){
  443.     std::cout << "Total Sent packets by sender Application " << appcountDrops << std::endl;
  444.     std::cout << "Total Sent packets by sender net device " << countTx << std::endl;
  445.     std::cout << "Total Dropped Packets by router queue " << queueDrops << std::endl;
  446.     std::cout << "Total Received Packets by router queue " << queueRecvs << std::endl;
  447.     std::cout << "Total Corrupt Packets dropped at receiver " << countDrops << std::endl;
  448.     std::cout << "Total packets received by receiver application " << total/m_pktSize << std::endl;
  449.     for (int i = 0; i < m_nNodes; i++)
  450.       {
  451.       uint64_t total = 0;
  452.       pktsink = sinkApp.Get (i)->GetObject<PacketSink> ();
  453.       total = pktsink->GetTotalRx();
  454.       std::cout << "Goodput of node " <<  i << transProt << " = " << total * 8 / (stopTime - startTime) << " bps" << std::endl;
  455.       }
  456.   }
  457.  
  458.   Simulator::Destroy ();
  459.  
  460.   return 0;
  461. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement