Advertisement
MdSadmanSiraj

ns3_ece540_project_v3

Dec 3rd, 2022 (edited)
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.26 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <fstream>
  4.  
  5. #include <sstream>
  6.  
  7. #include <string>
  8.  
  9. #include <vector>
  10.  
  11. #include <cstdlib>
  12.  
  13.  
  14.  
  15. #include "ns3/core-module.h"
  16.  
  17. #include "ns3/network-module.h"
  18.  
  19. #include "ns3/csma-module.h"
  20.  
  21. #include "ns3/internet-module.h"
  22.  
  23. #include "ns3/point-to-point-module.h"
  24.  
  25. #include "ns3/applications-module.h"
  26.  
  27. #include "ns3/ipv4-global-routing-helper.h"
  28.  
  29. #include "ns3/bridge-helper.h"
  30.  
  31. #include "ns3/netanim-module.h"
  32.  
  33. #include "ns3/global-route-manager.h"
  34.  
  35. #include "ns3/mobility-module.h"
  36.  
  37. #include "ns3/assert.h"
  38.  
  39.  
  40.  
  41. #include "ns3/flow-monitor-module.h"
  42.  
  43.  
  44.  
  45. using namespace ns3;
  46.  
  47.  
  48.  
  49. //For colorful console printing
  50.  
  51. /*
  52.  
  53.  * Usage example :
  54.  
  55.  *    std::cout << BOLD_CODE << "some bold text << END_CODE << std::endl;
  56.  
  57.  *
  58.  
  59.  *    std::cout << YELLOW_CODE << BOLD_CODE << "some bold yellow text << END_CODE << std::endl;
  60.  
  61.  *
  62.  
  63.  */
  64.  
  65.  
  66.  
  67. NS_LOG_COMPONENT_DEFINE ("Project");
  68.  
  69.  
  70.  
  71. #define YELLOW_CODE "\033[33m"
  72.  
  73. #define TEAL_CODE "\033[36m"
  74.  
  75. #define BOLD_CODE "\033[1m"
  76.  
  77. #define RED_CODE "\033[91m"
  78.  
  79. #define END_CODE "\033[0m"
  80.  
  81.  
  82.  
  83. double AppStartTime   = 2.0001;
  84.  
  85. double AppStopTime    = 19.80001;
  86.  
  87. std::string AppPacketRate ("40Kbps");
  88.  
  89. uint16_t port = 9;
  90.  
  91.  
  92.  
  93. void setFlow(NodeContainer a, NodeContainer b, int i, int j)
  94.  
  95. {
  96.  
  97.   Ptr<UniformRandomVariable> x = CreateObject<UniformRandomVariable> ();
  98.  
  99.   x->SetAttribute ("Min", DoubleValue (0));
  100.  
  101.   x->SetAttribute ("Max", DoubleValue (1));
  102.  
  103.   double rn = x->GetValue ();
  104.  
  105.   Ptr<Node> n = b.Get (j);
  106.  
  107.   Ptr<Ipv4> ipv4 = n->GetObject<Ipv4> ();
  108.  
  109.   Ipv4InterfaceAddress ipv4_int_addr = ipv4->GetAddress (1, 0);
  110.  
  111.   Ipv4Address ip_addr = ipv4_int_addr.GetLocal ();
  112.  
  113.   OnOffHelper onoff ("ns3::UdpSocketFactory", InetSocketAddress (ip_addr, port)); // traffic flows from node[i] to node[j]
  114.  
  115.   onoff.SetConstantRate (DataRate (AppPacketRate));
  116.  
  117.   ApplicationContainer apps = onoff.Install (a.Get (i));  // traffic sources are installed on all nodes
  118.  
  119.   apps.Start (Seconds (AppStartTime + rn));
  120.  
  121.   apps.Stop (Seconds (AppStopTime));
  122.  
  123. }
  124.  
  125.  
  126.  
  127. uint32_t SentPackets = 0;
  128.  
  129. uint32_t ReceivedPackets = 0;
  130.  
  131. uint32_t LostPackets = 0;
  132.  
  133.  
  134.  
  135. int
  136.  
  137. main (int argc, char *argv[])
  138.  
  139. {
  140.  
  141.   CommandLine cmd;
  142.  
  143.  
  144.  
  145.   uint32_t n1 = 2;
  146.  
  147.   uint32_t n2 = 2;
  148.  
  149.   uint32_t n3 = 2;
  150.  
  151.  
  152.  
  153.   double SimTime        = 20.00;
  154.  
  155.   double SinkStartTime  = 1.0001;
  156.  
  157.   double SinkStopTime   = 19.90001;
  158.  
  159.  
  160.  
  161.   cmd.AddValue ("n1", "Number of LAN 1 nodes", n1);
  162.  
  163.   cmd.AddValue ("n2", "Number of LAN 2 nodes", n2);
  164.  
  165.   cmd.AddValue ("n3", "Number of LAN 3 nodes", n3);
  166.  
  167.  
  168.  
  169.   cmd.Parse (argc, argv);
  170.  
  171.  
  172.  
  173.   //LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
  174.  
  175.   //LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
  176.  
  177.  
  178.  
  179.   //For the first network
  180.  
  181.   NodeContainer lan1_nodes;
  182.  
  183.   NodeContainer switch1_nodes;
  184.  
  185.  
  186.  
  187.   //For the second network
  188.  
  189.   NodeContainer lan2_nodes;
  190.  
  191.   NodeContainer switch2_nodes;
  192.  
  193.  
  194.  
  195.   //For the third network
  196.  
  197.   NodeContainer lan3_nodes;
  198.  
  199.   NodeContainer switch3_nodes;
  200.  
  201.  
  202.  
  203.   //for the nodes in the middle.
  204.  
  205.   NodeContainer router_nodes;
  206.  
  207.  
  208.  
  209.   lan1_nodes.Create (n1);
  210.  
  211.   lan2_nodes.Create (n2);
  212.  
  213.   lan3_nodes.Create (n3);
  214.  
  215.   switch1_nodes.Create (1);
  216.  
  217.   switch2_nodes.Create (1);
  218.  
  219.   switch3_nodes.Create (1);
  220.  
  221.   router_nodes.Create (2);
  222.  
  223.  
  224.  
  225.   //Let's create LAN 1 by attaching a CsmaNetDevice to all the nodes on the LAN
  226.  
  227.   CsmaHelper csma1;
  228.  
  229.   csma1.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
  230.  
  231.   csma1.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
  232.  
  233.   //Actually attaching CsmaNetDevice to all LAN 1 nodes.
  234.  
  235.   lan1_nodes.Add (router_nodes.Get (0));
  236.  
  237.   NetDeviceContainer lan1Devices;
  238.  
  239.   NetDeviceContainer switch1Devices;
  240.  
  241.   for (uint32_t i = 0; i < n1+1; i++)
  242.  
  243.   {
  244.  
  245.     NetDeviceContainer link = csma1.Install(NodeContainer(lan1_nodes.Get (i), switch1_nodes));
  246.  
  247.     lan1Devices.Add(link.Get (0)); switch1Devices.Add(link.Get (1));
  248.  
  249.   }
  250.  
  251.  
  252.  
  253.   //Let's create LAN 2 by attaching a CsmaNetDevice to all the nodes on the LAN
  254.  
  255.   CsmaHelper csma2;
  256.  
  257.   csma2.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
  258.  
  259.   csma2.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
  260.  
  261.   //Actually attaching CsmaNetDevice to all LAN 2 nodes.
  262.  
  263.   lan2_nodes.Add (router_nodes.Get (0));
  264.  
  265.   NetDeviceContainer lan2Devices;
  266.  
  267.   NetDeviceContainer switch2Devices;
  268.  
  269.   for (uint32_t i = 0; i < n2+1; i++)
  270.  
  271.   {
  272.  
  273.     NetDeviceContainer link = csma2.Install(NodeContainer(lan2_nodes.Get (i), switch2_nodes));
  274.  
  275.     lan2Devices.Add(link.Get (0)); switch2Devices.Add(link.Get (1));
  276.  
  277.   }
  278.  
  279.  
  280.  
  281.   //Let's create LAN 3 by attaching a CsmaNetDevice to all the nodes on the LAN
  282.  
  283.   CsmaHelper csma3;
  284.  
  285.   csma3.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
  286.  
  287.   csma3.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
  288.  
  289.   //Actually attaching CsmaNetDevice to all LAN 3 nodes.
  290.  
  291.   lan3_nodes.Add (router_nodes.Get (1));
  292.  
  293.   NetDeviceContainer lan3Devices;
  294.  
  295.   NetDeviceContainer switch3Devices;
  296.  
  297.   for (uint32_t i = 0; i < n3+1; i++)
  298.  
  299.   {
  300.  
  301.     NetDeviceContainer link = csma3.Install(NodeContainer(lan3_nodes.Get (i), switch3_nodes));
  302.  
  303.     lan3Devices.Add(link.Get (0)); switch3Devices.Add(link.Get (1));
  304.  
  305.   }
  306.  
  307.  
  308.  
  309.   /* So far our two LANs are disjoint, r1 and r2 need to be connected */
  310.  
  311.   //A PointToPoint connection between the two routers
  312.  
  313.   PointToPointHelper pointToPoint;
  314.  
  315.   pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
  316.  
  317.   pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
  318.  
  319.  
  320.  
  321.   NetDeviceContainer routerDevices;
  322.  
  323.   routerDevices = pointToPoint.Install (router_nodes);
  324.  
  325.  
  326.  
  327.   //Setting IP addresses. Notice that router 1 & 2 are in LAN 1 & 2 respectively.
  328.  
  329.   InternetStackHelper stack;
  330.  
  331.   stack.Install (lan1_nodes);
  332.  
  333.   stack.Install (lan2_nodes.Get(0));
  334.  
  335.   stack.Install (lan2_nodes.Get(1));
  336.  
  337.   stack.Install (lan3_nodes);
  338.  
  339.   stack.Install (switch1_nodes);
  340.  
  341.   stack.Install (switch2_nodes);
  342.  
  343.   stack.Install (switch3_nodes);
  344.  
  345.  
  346.  
  347.   Ipv4AddressHelper address;
  348.  
  349.   //For LAN 1
  350.  
  351.   address.SetBase ("10.1.1.0", "255.255.255.0");
  352.  
  353.   Ipv4InterfaceContainer lan1interfaces;
  354.  
  355.   lan1interfaces = address.Assign (lan1Devices);
  356.  
  357.   //For LAN 2
  358.  
  359.   address.SetBase ("10.1.2.0", "255.255.255.0");
  360.  
  361.   Ipv4InterfaceContainer lan2interfaces;
  362.  
  363.   lan2interfaces = address.Assign (lan2Devices);
  364.  
  365.   //For LAN 3
  366.  
  367.   address.SetBase ("10.1.3.0", "255.255.255.0");
  368.  
  369.   Ipv4InterfaceContainer lan3interfaces;
  370.  
  371.   lan3interfaces = address.Assign (lan3Devices);
  372.  
  373.   //For PointToPoint
  374.  
  375.   address.SetBase ("10.1.100.0", "255.255.255.0");
  376.  
  377.   Ipv4InterfaceContainer routerInterfaces;
  378.  
  379.   routerInterfaces = address.Assign (routerDevices);
  380.  
  381.  
  382.  
  383.   BridgeHelper bridge1;
  384.  
  385.   bridge1.Install(switch1_nodes.Get(0), switch1Devices);
  386.  
  387.   BridgeHelper bridge2;
  388.  
  389.   bridge2.Install(switch2_nodes.Get(0), switch2Devices);
  390.  
  391.   BridgeHelper bridge3;
  392.  
  393.   bridge3.Install(switch3_nodes.Get(0), switch3Devices);
  394.  
  395.  
  396.  
  397.   for (int i = 0; i < int(n1); i++)
  398.  
  399.   {
  400.  
  401.     PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
  402.  
  403.     ApplicationContainer apps_sink = sink.Install (lan1_nodes.Get (i));   // sink is installed on all nodes
  404.  
  405.     apps_sink.Start (Seconds (SinkStartTime));
  406.  
  407.     apps_sink.Stop (Seconds (SinkStopTime));
  408.  
  409.   }
  410.  
  411.   for (int i = 0; i < int(n2); i++)
  412.  
  413.   {
  414.  
  415.     PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
  416.  
  417.     ApplicationContainer apps_sink = sink.Install (lan2_nodes.Get (i));   // sink is installed on all nodes
  418.  
  419.     apps_sink.Start (Seconds (SinkStartTime));
  420.  
  421.     apps_sink.Stop (Seconds (SinkStopTime));
  422.  
  423.   }
  424.  
  425.   for (int i = 0; i < int(n3); i++)
  426.  
  427.   {
  428.  
  429.     PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
  430.  
  431.     ApplicationContainer apps_sink = sink.Install (lan3_nodes.Get (i));   // sink is installed on all nodes
  432.  
  433.     apps_sink.Start (Seconds (SinkStartTime));
  434.  
  435.     apps_sink.Stop (Seconds (SinkStopTime));
  436.  
  437.   }
  438.  
  439.  
  440.  
  441.   for (int i = 0; i < int(n1); i++)
  442.  
  443.   {
  444.  
  445.     for (int j = 0; j < int(n1); j++)
  446.  
  447.     {
  448.  
  449.       if (i != j)
  450.  
  451.         setFlow(lan1_nodes, lan1_nodes, i, j);
  452.  
  453.     }
  454.  
  455.     for (int j = 0; j < int(n2); j++)
  456.  
  457.     {
  458.  
  459.       setFlow(lan1_nodes, lan2_nodes, i, j);
  460.  
  461.     }
  462.  
  463.     for (int j = 0; j < int(n3); j++)
  464.  
  465.     {
  466.  
  467.       setFlow(lan1_nodes, lan3_nodes, i, j);
  468.  
  469.     }
  470.  
  471.   }
  472.  
  473.   for (int i = 0; i < int(n2); i++)
  474.  
  475.   {
  476.  
  477.     for (int j = 0; j < int(n1); j++)
  478.  
  479.     {
  480.  
  481.       setFlow(lan2_nodes, lan1_nodes, i, j);
  482.  
  483.     }
  484.  
  485.     for (int j = 0; j < int(n2); j++)
  486.  
  487.     {
  488.  
  489.       if (i != j)
  490.  
  491.         setFlow(lan2_nodes, lan2_nodes, i, j);
  492.  
  493.     }
  494.  
  495.     for (int j = 0; j < int(n3); j++)
  496.  
  497.     {
  498.  
  499.       setFlow(lan2_nodes, lan3_nodes, i, j);
  500.  
  501.     }
  502.  
  503.   }
  504.  
  505.   for (int i = 0; i < int(n3); i++)
  506.  
  507.   {
  508.  
  509.     for (int j = 0; j < int(n1); j++)
  510.  
  511.     {
  512.  
  513.       setFlow(lan3_nodes, lan1_nodes, i, j);
  514.  
  515.     }
  516.  
  517.     for (int j = 0; j < int(n2); j++)
  518.  
  519.     {
  520.  
  521.       setFlow(lan3_nodes, lan2_nodes, i, j);
  522.  
  523.     }
  524.  
  525.     for (int j = 0; j < int(n3); j++)
  526.  
  527.     {
  528.  
  529.       if (i != j)
  530.  
  531.         setFlow(lan3_nodes, lan3_nodes, i, j);
  532.  
  533.     }
  534.  
  535.   }
  536.  
  537.  
  538.  
  539.   //For routers to be able to forward packets, they need to have routing rules.
  540.  
  541.   Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
  542.  
  543.  
  544.  
  545.   csma1.EnablePcap("lan1", lan1Devices);
  546.  
  547.   csma2.EnablePcap("lan2", lan2Devices);
  548.  
  549.   csma3.EnablePcap("lan3", lan3Devices);
  550.  
  551.   pointToPoint.EnablePcapAll("routers");
  552.  
  553.   pointToPoint.EnableAscii("ascii-p2p", router_nodes);
  554.  
  555.  
  556.  
  557.   AnimationInterface anim ("project_v3.xml");
  558.  
  559.  
  560.  
  561.   anim.SetConstantPosition(router_nodes.Get(0), 60.0, 45.0);
  562.  
  563.   anim.SetConstantPosition(router_nodes.Get(1), 60.0, 55.0);
  564.  
  565.  
  566.  
  567.   anim.SetConstantPosition(lan1_nodes.Get(0), 30.0, 25.0);
  568.  
  569.   anim.SetConstantPosition(lan1_nodes.Get(1), 50.0, 25.0);
  570.  
  571.   anim.SetConstantPosition(switch1_nodes.Get(0), 40.0, 35.0);
  572.  
  573.  
  574.  
  575.   anim.SetConstantPosition(lan2_nodes.Get(0), 70.0, 25.0);
  576.  
  577.   anim.SetConstantPosition(lan2_nodes.Get(1), 90.0, 25.0);
  578.  
  579.   anim.SetConstantPosition(switch2_nodes.Get(0), 80.0, 35.0);
  580.  
  581.  
  582.  
  583.     anim.SetConstantPosition(lan3_nodes.Get(0), 30.0, 75.0);
  584.  
  585.   anim.SetConstantPosition(lan3_nodes.Get(1), 50.0, 75.0);
  586.  
  587.   anim.SetConstantPosition(switch3_nodes.Get(0), 40.0, 65.0);
  588.  
  589.  
  590.  
  591.   FlowMonitorHelper flowmon;
  592.  
  593.   Ptr<FlowMonitor> monitor = flowmon.InstallAll();
  594.  
  595.  
  596.  
  597.   NS_LOG_INFO ("Running Simulation...");
  598.  
  599.  
  600.  
  601.   Simulator::Stop (Seconds (SimTime));
  602.  
  603.   Simulator::Run ();
  604.  
  605.  
  606.   // Performance Metric Calculations
  607.  
  608.  
  609.   int j=0;
  610.  
  611.   float AvgThroughput = 0;
  612.  
  613.   Time Jitter;
  614.  
  615.   Time Delay;
  616.  
  617.  
  618.  
  619.   Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
  620.  
  621.         std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
  622.  
  623.  
  624.  
  625.   for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator iter = stats.begin (); iter != stats.end (); ++iter)
  626.  
  627.     {
  628.  
  629.       Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (iter->first);
  630.  
  631.  
  632.  
  633.             NS_LOG_UNCOND("\n----- Flow ID: " << iter->first << " -----");
  634.  
  635.             NS_LOG_UNCOND("Src Addr: " << t.sourceAddress << " | Dst Addr: "<< t.destinationAddress);
  636.  
  637.             NS_LOG_UNCOND("Sent Packets = " << iter->second.txPackets);
  638.  
  639.             NS_LOG_UNCOND("Received Packets = " << iter->second.rxPackets);
  640.  
  641.             NS_LOG_UNCOND("Lost Packets = " << iter->second.txPackets-iter->second.rxPackets);
  642.  
  643.             NS_LOG_UNCOND("Packet delivery ratio = " << iter->second.rxPackets*100/iter->second.txPackets << " %");
  644.  
  645.             NS_LOG_UNCOND("Packet loss ratio = " << (iter->second.txPackets-iter->second.rxPackets)*100/iter->second.txPackets << " %");
  646.  
  647.             NS_LOG_UNCOND("Delay = " << iter->second.delaySum);
  648.  
  649.             NS_LOG_UNCOND("Jitter = " << iter->second.jitterSum);
  650.  
  651.             NS_LOG_UNCOND("Throughput = " << iter->second.rxBytes * 8.0/(iter->second.timeLastRxPacket.GetSeconds()-iter->second.timeFirstTxPacket.GetSeconds())/1024<<" Kbps");
  652.  
  653.  
  654.  
  655.             SentPackets = SentPackets +(iter->second.txPackets);
  656.  
  657.             ReceivedPackets = ReceivedPackets + (iter->second.rxPackets);
  658.  
  659.             LostPackets = LostPackets + (iter->second.txPackets-iter->second.rxPackets);
  660.  
  661.             AvgThroughput = AvgThroughput + (iter->second.rxBytes * 8.0/(iter->second.timeLastRxPacket.GetSeconds()-iter->second.timeFirstTxPacket.GetSeconds())/1024);
  662.  
  663.             Delay = Delay + (iter->second.delaySum);
  664.  
  665.             Jitter = Jitter + (iter->second.jitterSum);
  666.  
  667.  
  668.  
  669.             j = j + 1;
  670.  
  671.  
  672.  
  673.     }
  674.  
  675.  
  676.  
  677.     AvgThroughput = AvgThroughput/j;
  678.  
  679.     NS_LOG_UNCOND("\n-------- Total Results of the simulation --------" << std::endl);
  680.  
  681.     NS_LOG_UNCOND("Total sent packets = " << SentPackets);
  682.  
  683.     NS_LOG_UNCOND("Total Received Packets = " << ReceivedPackets);
  684.  
  685.     NS_LOG_UNCOND("Total Lost Packets = " << LostPackets);
  686.  
  687.     NS_LOG_UNCOND("Packet Loss ratio = " << ((LostPackets*100)/SentPackets)<< " %");
  688.  
  689.     NS_LOG_UNCOND("Packet delivery ratio = " << ((ReceivedPackets*100)/SentPackets)<< " %");
  690.  
  691.     NS_LOG_UNCOND("Average Throughput = " << AvgThroughput<< " Kbps");
  692.  
  693.     NS_LOG_UNCOND("End to End Delay = " << Delay);
  694.  
  695.     NS_LOG_UNCOND("End to End Jitter delay = " << Jitter);
  696.  
  697.     NS_LOG_UNCOND("Total Flow id = " << j);
  698.  
  699.     monitor->SerializeToXmlFile("project_v3_flow.xml", true, true);
  700.  
  701.    
  702.  
  703.   Simulator::Destroy ();
  704.  
  705.   return 0;
  706.  
  707.  
  708.  
  709. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement