Advertisement
MdSadmanSiraj

ECE540_v6

Dec 5th, 2022
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. #include <cstdlib>
  7.  
  8. #include "ns3/core-module.h"
  9. #include "ns3/network-module.h"
  10. #include "ns3/csma-module.h"
  11. #include "ns3/internet-module.h"
  12. #include "ns3/point-to-point-module.h"
  13. #include "ns3/applications-module.h"
  14. #include "ns3/ipv4-global-routing-helper.h"
  15. #include "ns3/bridge-helper.h"
  16. #include "ns3/netanim-module.h"
  17. #include "ns3/global-route-manager.h"
  18. #include "ns3/mobility-module.h"
  19. #include "ns3/assert.h"
  20. #include "ns3/flow-monitor-module.h"
  21.  
  22. using namespace ns3;
  23.  
  24. //For colorful console printing
  25. /*
  26. * Usage example :
  27. * std::cout << BOLD_CODE << "some bold text << END_CODE << std::endl;
  28. *
  29. * std::cout << YELLOW_CODE << BOLD_CODE << "some bold yellow text << END_CODE << std::endl;
  30. *
  31. */
  32.  
  33. NS_LOG_COMPONENT_DEFINE ("Project");
  34.  
  35. #define YELLOW_CODE "\033[33m"
  36. #define TEAL_CODE "\033[36m"
  37. #define BOLD_CODE "\033[1m"
  38. #define RED_CODE "\033[91m"
  39. #define END_CODE "\033[0m"
  40.  
  41.  
  42. double SimTime = 200.00;
  43. double AppStartTime = 2.0001;
  44. double AppStopTime = SimTime - 1.0 + 0.80001;
  45. std::string AppPacketRate ("8Kbps");
  46. uint16_t sPIAM = 0;
  47. uint16_t port = 9;
  48.  
  49. void setFlow(NodeContainer a, NodeContainer b, int i, int j)
  50. {
  51. Ptr<UniformRandomVariable> x = CreateObject<UniformRandomVariable> ();
  52. x->SetAttribute ("Min", DoubleValue (0));
  53. x->SetAttribute ("Max", DoubleValue (1));
  54. double rn = x->GetValue ();
  55. Ptr<Node> n = b.Get (j);
  56. Ptr<Ipv4> ipv4 = n->GetObject<Ipv4> ();
  57. Ipv4InterfaceAddress ipv4_int_addr = ipv4->GetAddress (1, 0);
  58. Ipv4Address ip_addr = ipv4_int_addr.GetLocal ();
  59. OnOffHelper onoff ("ns3::UdpSocketFactory", InetSocketAddress (ip_addr, port)); // traffic flows from node[i] to node[j]
  60. onoff.SetAttribute("PacketSize", UintegerValue(1024));
  61. if (sPIAM == 0){
  62. onoff.SetAttribute("OffTime", StringValue ("ns3::ExponentialRandomVariable[Mean=5]"));
  63. onoff.SetAttribute("OnTime", StringValue ("ns3::ExponentialRandomVariable[Mean=5]"));
  64. }
  65. else if (sPIAM == 1){
  66. onoff.SetAttribute("OffTime", StringValue ("ns3::UniformRandomVariable[Min=4|Max=6]"));
  67. onoff.SetAttribute("OnTime", StringValue ("ns3::UniformRandomVariable[Min=4|Max=6]"));
  68. }
  69. else if (sPIAM == 2){
  70. onoff.SetAttribute("OffTime", StringValue ("ns3::UniformRandomVariable[Min=0|Max=10]"));
  71. onoff.SetAttribute("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0|Max=10]"));
  72. }
  73. else if (sPIAM == 3){
  74. onoff.SetAttribute("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=5]"));
  75. onoff.SetAttribute("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=5]"));
  76. }
  77. onoff.SetAttribute("DataRate", StringValue (AppPacketRate));
  78. ApplicationContainer apps = onoff.Install (a.Get (i)); // traffic sources are installed on all nodes
  79. apps.Start (Seconds (AppStartTime + rn));
  80. apps.Stop (Seconds (AppStopTime));
  81. }
  82.  
  83. int
  84. main (int argc, char *argv[])
  85. {
  86. CommandLine cmd;
  87.  
  88. uint32_t n1 = 2;
  89. uint32_t n2 = 2;
  90. uint32_t n3 = 2;
  91. uint32_t SentPackets = 0;
  92. uint32_t ReceivedPackets = 0;
  93. uint32_t LostPackets = 0;
  94. std::string P2PRate ("10Mbps");
  95. std::string CSMARate ("100Mbps");
  96. std::string CSVfileName ("default.csv");
  97.  
  98. Config::SetDefault("ns3::ArpCache::PendingQueueSize", UintegerValue(10));
  99.  
  100. cmd.AddValue ("AppPacketRate", "Application packet generation rate", AppPacketRate);
  101. cmd.AddValue ("SimTime", "Total simulation time", SimTime);
  102. cmd.AddValue ("sPIAM", "Select packet inter arrival model", sPIAM);
  103. cmd.AddValue ("CSVfileName", "Save file name", CSVfileName);
  104. cmd.AddValue ("P2PRate", "P2P data rate", P2PRate);
  105. cmd.AddValue ("CSMARate", "CSMA data rate", CSMARate);
  106.  
  107. cmd.Parse (argc, argv);
  108.  
  109. double SinkStartTime = 1.0001;
  110. double SinkStopTime = SimTime - 1.0 + 0.90001;
  111.  
  112. //LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
  113. //LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
  114.  
  115. //For the first network
  116. NodeContainer lan1_nodes;
  117. NodeContainer switch1_nodes;
  118.  
  119. //For the second network
  120. NodeContainer lan2_nodes;
  121. NodeContainer switch2_nodes;
  122.  
  123. //For the third network
  124. NodeContainer lan3_nodes;
  125. NodeContainer switch3_nodes;
  126.  
  127. //for the nodes in the middle.
  128. NodeContainer router12_nodes;
  129. NodeContainer router23_nodes;
  130.  
  131. lan1_nodes.Create (n1);
  132. lan2_nodes.Create (n2);
  133. lan3_nodes.Create (n3);
  134. switch1_nodes.Create (1);
  135. switch2_nodes.Create (1);
  136. switch3_nodes.Create (1);
  137.  
  138. router12_nodes.Create (1);
  139. lan1_nodes.Add (router12_nodes.Get (0));
  140. router23_nodes.Create (2);
  141. lan2_nodes.Add (router23_nodes.Get (0));
  142. lan3_nodes.Add (router23_nodes.Get (1));
  143.  
  144. router12_nodes.Add (router23_nodes.Get (0));
  145.  
  146. //Let's create LAN 1 by attaching a CsmaNetDevice to all the nodes on the LAN
  147. CsmaHelper csma1;
  148. csma1.SetChannelAttribute ("DataRate", StringValue (CSMARate));
  149. csma1.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
  150. //Actually attaching CsmaNetDevice to all LAN 1 nodes.
  151. NetDeviceContainer lan1Devices;
  152. NetDeviceContainer switch1Devices;
  153. for (uint32_t i = 0; i < n1+1; i++)
  154. {
  155. NetDeviceContainer link = csma1.Install(NodeContainer(lan1_nodes.Get (i), switch1_nodes));
  156. lan1Devices.Add(link.Get (0)); switch1Devices.Add(link.Get (1));
  157. }
  158.  
  159. //Let's create LAN 2 by attaching a CsmaNetDevice to all the nodes on the LAN
  160. CsmaHelper csma2;
  161. csma2.SetChannelAttribute ("DataRate", StringValue (CSMARate));
  162. csma2.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
  163. //Actually attaching CsmaNetDevice to all LAN 2 nodes.
  164. NetDeviceContainer lan2Devices;
  165. NetDeviceContainer switch2Devices;
  166. for (uint32_t i = 0; i < n2+1; i++)
  167. {
  168. NetDeviceContainer link = csma2.Install(NodeContainer(lan2_nodes.Get (i), switch2_nodes));
  169. lan2Devices.Add(link.Get (0)); switch2Devices.Add(link.Get (1));
  170. }
  171.  
  172. //Let's create LAN 3 by attaching a CsmaNetDevice to all the nodes on the LAN
  173. CsmaHelper csma3;
  174. csma3.SetChannelAttribute ("DataRate", StringValue (CSMARate));
  175. csma3.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
  176. //Actually attaching CsmaNetDevice to all LAN 3 nodes.
  177. NetDeviceContainer lan3Devices;
  178. NetDeviceContainer switch3Devices;
  179. for (uint32_t i = 0; i < n3+1; i++)
  180. {
  181. NetDeviceContainer link = csma3.Install(NodeContainer(lan3_nodes.Get (i), switch3_nodes));
  182. lan3Devices.Add(link.Get (0)); switch3Devices.Add(link.Get (1));
  183. }
  184.  
  185. /* So far our two LANs are disjoint, r1 and r2 need to be connected */
  186. //A PointToPoint connection between the two routers
  187. PointToPointHelper pointToPoint12;
  188. pointToPoint12.SetDeviceAttribute ("DataRate", StringValue (P2PRate));
  189. pointToPoint12.SetChannelAttribute ("Delay", StringValue ("2ms"));
  190.  
  191. NetDeviceContainer router12Devices;
  192. router12Devices = pointToPoint12.Install (router12_nodes);
  193.  
  194. PointToPointHelper pointToPoint23;
  195. pointToPoint23.SetDeviceAttribute ("DataRate", StringValue (P2PRate));
  196. pointToPoint23.SetChannelAttribute ("Delay", StringValue ("2ms"));
  197.  
  198. NetDeviceContainer router23Devices;
  199. router23Devices = pointToPoint23.Install (router23_nodes);
  200.  
  201. //Setting IP addresses. Notice that router 1 & 2 are in LAN 1 & 2 respectively.
  202. InternetStackHelper stack;
  203. stack.Install (lan1_nodes);
  204. stack.Install (lan2_nodes);
  205. stack.Install (lan3_nodes);
  206. stack.Install (switch1_nodes);
  207. stack.Install (switch2_nodes);
  208. stack.Install (switch3_nodes);
  209.  
  210. Ipv4AddressHelper address;
  211. //For LAN 1
  212. address.SetBase ("10.1.1.0", "255.255.255.0");
  213. Ipv4InterfaceContainer lan1interfaces;
  214. lan1interfaces = address.Assign (lan1Devices);
  215. //For LAN 2
  216. address.SetBase ("10.1.2.0", "255.255.255.0");
  217. Ipv4InterfaceContainer lan2interfaces;
  218. lan2interfaces = address.Assign (lan2Devices);
  219. //For LAN 3
  220. address.SetBase ("10.1.3.0", "255.255.255.0");
  221. Ipv4InterfaceContainer lan3interfaces;
  222. lan3interfaces = address.Assign (lan3Devices);
  223.  
  224. //For PointToPoint12
  225. address.SetBase ("10.1.10.0", "255.255.255.0");
  226. Ipv4InterfaceContainer router12Interfaces;
  227. router12Interfaces = address.Assign (router12Devices);
  228.  
  229. //For PointToPoint23
  230. address.SetBase ("10.1.20.0", "255.255.255.0");
  231. Ipv4InterfaceContainer router23Interfaces;
  232. router23Interfaces = address.Assign (router23Devices);
  233.  
  234. BridgeHelper bridge1;
  235. bridge1.Install(switch1_nodes.Get(0), switch1Devices);
  236. BridgeHelper bridge2;
  237. bridge2.Install(switch2_nodes.Get(0), switch2Devices);
  238. BridgeHelper bridge3;
  239. bridge3.Install(switch3_nodes.Get(0), switch3Devices);
  240.  
  241. for (int i = 0; i < int(n1); i++)
  242. {
  243. PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
  244. ApplicationContainer apps_sink = sink.Install (lan1_nodes.Get (i)); // sink is installed on all nodes
  245. apps_sink.Start (Seconds (SinkStartTime));
  246. apps_sink.Stop (Seconds (SinkStopTime));
  247. }
  248. for (int i = 0; i < int(n2); i++)
  249. {
  250. PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
  251. ApplicationContainer apps_sink = sink.Install (lan2_nodes.Get (i)); // sink is installed on all nodes
  252. apps_sink.Start (Seconds (SinkStartTime));
  253. apps_sink.Stop (Seconds (SinkStopTime));
  254. }
  255. for (int i = 0; i < int(n3); i++)
  256. {
  257. PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
  258. ApplicationContainer apps_sink = sink.Install (lan3_nodes.Get (i)); // sink is installed on all nodes
  259. apps_sink.Start (Seconds (SinkStartTime));
  260. apps_sink.Stop (Seconds (SinkStopTime));
  261. }
  262.  
  263. for (int i = 0; i < int(n1); i++)
  264. {
  265. for (int j = 0; j < int(n1); j++)
  266. {
  267. if (i != j)
  268. setFlow(lan1_nodes, lan1_nodes, i, j);
  269. }
  270. for (int j = 0; j < int(n2); j++)
  271. {
  272. setFlow(lan1_nodes, lan2_nodes, i, j);
  273. }
  274. for (int j = 0; j < int(n3); j++)
  275. {
  276. setFlow(lan1_nodes, lan3_nodes, i, j);
  277. }
  278. }
  279. for (int i = 0; i < int(n2); i++)
  280. {
  281. for (int j = 0; j < int(n1); j++)
  282. {
  283. setFlow(lan2_nodes, lan1_nodes, i, j);
  284. }
  285. for (int j = 0; j < int(n2); j++)
  286. {
  287. if (i != j)
  288. setFlow(lan2_nodes, lan2_nodes, i, j);
  289. }
  290. for (int j = 0; j < int(n3); j++)
  291. {
  292. setFlow(lan2_nodes, lan3_nodes, i, j);
  293. }
  294. }
  295. for (int i = 0; i < int(n3); i++)
  296. {
  297. for (int j = 0; j < int(n1); j++)
  298. {
  299. setFlow(lan3_nodes, lan1_nodes, i, j);
  300. }
  301. for (int j = 0; j < int(n2); j++)
  302. {
  303. setFlow(lan3_nodes, lan2_nodes, i, j);
  304. }
  305. for (int j = 0; j < int(n3); j++)
  306. {
  307. if (i != j)
  308. setFlow(lan3_nodes, lan3_nodes, i, j);
  309. }
  310. }
  311.  
  312. //For routers to be able to forward packets, they need to have routing rules.
  313. Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
  314.  
  315. csma1.EnablePcap("lan1", lan1Devices);
  316. csma2.EnablePcap("lan2", lan2Devices);
  317. csma3.EnablePcap("lan3", lan3Devices);
  318. pointToPoint12.EnablePcap("routers12", router12Devices);
  319. pointToPoint23.EnablePcap("routers23", router23Devices);
  320.  
  321. AnimationInterface anim ("a_project_v6.1_bus.xml");
  322.  
  323. anim.SetConstantPosition(router12_nodes.Get(0), 40.0, 45.0);
  324. anim.SetConstantPosition(router23_nodes.Get(0), 80.0, 45.0);
  325. anim.SetConstantPosition(router23_nodes.Get(1), 40.0, 55.0);
  326.  
  327. anim.SetConstantPosition(lan1_nodes.Get(0), 30.0, 25.0);
  328. anim.SetConstantPosition(lan1_nodes.Get(1), 50.0, 25.0);
  329. anim.SetConstantPosition(switch1_nodes.Get(0), 40.0, 35.0);
  330.  
  331. anim.SetConstantPosition(lan2_nodes.Get(0), 70.0, 25.0);
  332. anim.SetConstantPosition(lan2_nodes.Get(1), 90.0, 25.0);
  333. anim.SetConstantPosition(switch2_nodes.Get(0), 80.0, 35.0);
  334.  
  335. anim.SetConstantPosition(lan3_nodes.Get(0), 30.0, 75.0);
  336. anim.SetConstantPosition(lan3_nodes.Get(1), 50.0, 75.0);
  337. anim.SetConstantPosition(switch3_nodes.Get(0), 40.0, 65.0);
  338.  
  339. FlowMonitorHelper flowmon;
  340. Ptr<FlowMonitor> monitor = flowmon.InstallAll();
  341.  
  342. NS_LOG_INFO ("Running Simulation...");
  343.  
  344. Simulator::Stop (Seconds (SimTime));
  345. Simulator::Run ();
  346.  
  347. // Performance Metric Calculations
  348. int j=0;
  349. float AvgThroughput = 0;
  350. float AvgGeneration = 0;
  351. float Throughput = 0;
  352. float Generation = 0;
  353. float AvgThroughputPercentage = 0;
  354. Time AvgDelay;
  355. Time AvgJitter;
  356. Time Jitter;
  357. Time Delay;
  358.  
  359. //for output generation
  360. std::ofstream out (CSVfileName.c_str ());
  361.  
  362. out << "Flow ID," << "Source Address," << "Destination Address," << "Sent Packets," << "Received Packets," << "Lost Packets," << "Packet Delivery Ratio (%)," << "Packet Loss (%)," << "End to End Delay (ns)," << "End to End Jitter (ns)," << "Throughput (Kbps)," << "Generation Rate (Kbps)," << "Throughput (%)," << std::endl;
  363.  
  364. Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
  365. std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
  366.  
  367. for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator iter = stats.begin (); iter != stats.end (); ++iter)
  368. {
  369. Throughput = iter->second.rxBytes * 8.0/(iter->second.timeLastRxPacket.GetSeconds()-iter->second.timeFirstTxPacket.GetSeconds())/1024;
  370. Generation = iter->second.txBytes * 8.0/(iter->second.timeLastTxPacket.GetSeconds()-iter->second.timeFirstTxPacket.GetSeconds())/1024;
  371. Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (iter->first);
  372. NS_LOG_UNCOND("\n----- Flow ID: " << iter->first << " -----");
  373. NS_LOG_UNCOND("Src Addr: " << t.sourceAddress << " | Dst Addr: "<< t.destinationAddress);
  374. NS_LOG_UNCOND("Sent Packets = " << iter->second.txPackets);
  375. NS_LOG_UNCOND("Received Packets = " << iter->second.rxPackets);
  376. NS_LOG_UNCOND("Lost Packets = " << iter->second.txPackets-iter->second.rxPackets);
  377. NS_LOG_UNCOND("Packet delivery ratio = " << iter->second.rxPackets*100.0/iter->second.txPackets << " %");
  378. NS_LOG_UNCOND("Packet loss ratio = " << (iter->second.txPackets-iter->second.rxPackets)*100.0/iter->second.txPackets << " %");
  379. NS_LOG_UNCOND("Delay = " << iter->second.delaySum);
  380. NS_LOG_UNCOND("Jitter = " << iter->second.jitterSum);
  381. NS_LOG_UNCOND("Throughput = " << Throughput<<"
  382. NS_LOG_UNCOND("Generation = " << Generation<<" Kbps");
  383.  
  384. SentPackets = SentPackets +(iter->second.txPackets);
  385. ReceivedPackets = ReceivedPackets + (iter->second.rxPackets);
  386. LostPackets = LostPackets + (iter->second.txPackets-iter->second.rxPackets);
  387. AvgThroughput = AvgThroughput + Throughput;
  388. AvgGeneration = AvgGeneration + Generation;
  389. Delay = Delay + (iter->second.delaySum);
  390. Jitter = Jitter + (iter->second.jitterSum);
  391.  
  392. out << iter->first << "," << t.sourceAddress << "," << t.destinationAddress << "," << iter->second.txPackets << "," << iter->second.rxPackets << "," << iter->second.txPackets-iter->second.rxPackets << "," << iter->second.rxPackets*100.0/iter->second.txPackets << "," << (iter->second.txPackets-iter->second.rxPackets)*100.0/iter->second.txPackets << "," << iter->second.delaySum << "," << iter->second.jitterSum << "," << Throughput << "," << Generation << std::endl;
  393.  
  394. j = j + 1;
  395. }
  396.  
  397. AvgThroughput = AvgThroughput/j;
  398. AvgDelay = Delay/j;
  399. AvgJitter = Jitter/j;
  400. NS_LOG_UNCOND("\n-------- Total Results of the simulation --------" << std::endl);
  401. NS_LOG_UNCOND("Total sent packets = " << SentPackets);
  402. NS_LOG_UNCOND("Total Received Packets = " << ReceivedPackets);
  403. NS_LOG_UNCOND("Total Lost Packets = " << LostPackets);
  404. NS_LOG_UNCOND("Packet Loss ratio = " << ((LostPackets*100.0)/SentPackets)<< " %");
  405. NS_LOG_UNCOND("Packet delivery ratio = " << ((ReceivedPackets*100.0)/SentPackets)<< " %");
  406. NS_LOG_UNCOND("Average Throughput = " << AvgThroughput<< " Kbps");
  407. NS_LOG_UNCOND("Average End to End Delay = " << AvgDelay);
  408. NS_LOG_UNCOND("Average End to End Jitter delay = " << AvgJitter);
  409. NS_LOG_UNCOND("Total Flow id = " << j);
  410. //monitor->SerializeToXmlFile("project_v3_flow.xml", true, true);
  411.  
  412. out << "Total Results of the Simulation" << std::endl;
  413. out << j << "," << "N/A," << "N/A," << SentPackets << "," << ReceivedPackets << "," << LostPackets << "," << ((ReceivedPackets*100.0)/SentPackets) << "," << ((LostPackets*100.0)/SentPackets) << "," << AvgDelay << "," << AvgJitter << "," << AvgThroughput << std::endl;
  414.  
  415. out.close ();
  416.  
  417. Simulator::Destroy ();
  418. return 0;
  419.  
  420. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement