Advertisement
DaLuppo

Untitled

Apr 7th, 2021
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.13 KB | None | 0 0
  1. #include "ns3/core-module.h"
  2. #include "ns3/network-module.h"
  3. #include "ns3/internet-module.h"
  4. #include "ns3/mobility-module.h"
  5. #include "ns3/lte-module.h"
  6. #include "ns3/lte-module.h"
  7. #include "ns3/flow-monitor-module.h"
  8. #include "ns3/applications-module.h"
  9. #include "ns3/point-to-point-module.h"
  10. #include "ns3/config-store-module.h"
  11. #include "ns3/netanim-module.h"
  12. #include "ns3/gnuplot.h"
  13. #include <math.h>
  14.  
  15. using namespace ns3;
  16.  
  17. NS_LOG_COMPONENT_DEFINE ("lte_handover_scenario_manual_handover");
  18.  
  19. void
  20. NotifyConnectionEstablishedUe (std::string context,
  21. uint64_t imsi, //IMSI -> International Mobile Subscriber Identity
  22. uint16_t cellid,
  23. uint16_t rnti) //RNTI -> Radio Network Temporary Identifier
  24. {
  25. std::cout << Simulator::Now ().GetSeconds () << " " << context
  26. << " UE IMSI " << imsi
  27. << ": connected to CellId " << cellid
  28. << " with RNTI " << rnti
  29. << std::endl;
  30. }
  31.  
  32. void
  33. NotifyHandoverStartUe (std::string context,
  34. uint64_t imsi,
  35. uint16_t cellid,
  36. uint16_t rnti,
  37. uint16_t targetCellId)
  38. {
  39. std::cout << Simulator::Now ().GetSeconds () << " " << context
  40. << " UE IMSI " << imsi
  41. << ": previously connected to CellId " << cellid
  42. << " with RNTI " << rnti
  43. << ", doing handover to CellId " << targetCellId
  44. << std::endl;
  45. }
  46.  
  47. void
  48. NotifyHandoverEndOkUe (std::string context,
  49. uint64_t imsi,
  50. uint16_t cellid,
  51. uint16_t rnti)
  52. {
  53. std::cout << Simulator::Now ().GetSeconds () << " " << context
  54. << " UE IMSI " << imsi
  55. << ": successful handover to CellId " << cellid
  56. << " with RNTI " << rnti
  57. << std::endl;
  58. }
  59.  
  60. void
  61. NotifyConnectionEstablishedEnb (std::string context,
  62. uint64_t imsi,
  63. uint16_t cellid,
  64. uint16_t rnti)
  65. {
  66. std::cout << Simulator::Now ().GetSeconds () << " " << context
  67. << " eNB CellId " << cellid
  68. << ": successful connection of UE with IMSI " << imsi
  69. << " RNTI " << rnti
  70. << std::endl;
  71. }
  72.  
  73. void
  74. NotifyHandoverStartEnb (std::string context,
  75. uint64_t imsi,
  76. uint16_t cellid,
  77. uint16_t rnti,
  78. uint16_t targetCellId)
  79. {
  80. std::cout << Simulator::Now ().GetSeconds () << " " << context
  81. << " eNB CellId " << cellid
  82. << ": start handover of UE with IMSI " << imsi
  83. << " RNTI " << rnti
  84. << " to CellId " << targetCellId
  85. << std::endl;
  86. }
  87.  
  88. void
  89. NotifyHandoverEndOkEnb (std::string context,
  90. uint64_t imsi,
  91. uint16_t cellid,
  92. uint16_t rnti)
  93. {
  94. std::cout << Simulator::Now ().GetSeconds () << " " << context
  95. << " eNB CellId " << cellid
  96. << ": completed handover of UE with IMSI " << imsi
  97. << " RNTI " << rnti
  98. << std::endl;
  99. }
  100.  
  101. void ThroughputMonitor (FlowMonitorHelper *fmhelper, Ptr<FlowMonitor> flowMon,Gnuplot2dDataset DataSet)
  102. {
  103. double localThrou=0;
  104. std::map<FlowId, FlowMonitor::FlowStats> flowStats = flowMon->GetFlowStats();
  105. Ptr<Ipv4FlowClassifier> classing = DynamicCast<Ipv4FlowClassifier> (fmhelper->GetClassifier());
  106. for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator stats = flowStats.begin (); stats != flowStats.end (); ++stats)
  107. {
  108. Ipv4FlowClassifier::FiveTuple fiveTuple = classing->FindFlow (stats->first);
  109. if (fiveTuple.sourceAddress == Ipv4Address("1.0.0.2") /*|| fiveTuple.sourceAddress == Ipv4Address("7.0.0.2") || fiveTuple.sourceAddress == Ipv4Address("7.0.0.3")*/)
  110. {
  111.  
  112. std::cout<<"Flow ID : " << stats->first <<" ; "<< fiveTuple.sourceAddress <<" -----> "<<fiveTuple.destinationAddress<<std::endl;
  113. std::cout<<"Tx Packets : " << stats->second.txPackets<<std::endl;
  114. std::cout<<"Rx Packets : " << stats->second.rxPackets<<std::endl;
  115. std::cout<<"Duration : "<<(stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())<<std::endl;
  116. std::cout<<"Last Received Packet : "<< stats->second.timeLastRxPacket.GetSeconds()<<" Seconds"<<std::endl;
  117. std::cout<<"Throughput: " << stats->second.rxBytes * 8.0 / (stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())/1024 << " Kbps"<<std::endl;
  118. std::cout<< "Mean{Delay}: " << (stats->second.delaySum.GetSeconds()/stats->second.rxPackets) << "\n";
  119. std::cout<< "Mean{Jitter}: " << (stats->second.jitterSum.GetSeconds()/(stats->second.rxPackets)) << "\n";
  120. std::cout<< "Total{Delay}: " << (stats->second.delaySum.GetSeconds()) << "\n";
  121. std::cout<< "Total{Jitter}: " << (stats->second.jitterSum.GetSeconds()) << "\n";
  122. std::cout<< "Lost Packets: " << (stats->second.lostPackets) << "\n";
  123. std::cout<< "Dropped Packets: " << (stats->second.packetsDropped.size()) << "\n";
  124. localThrou=(stats->second.rxBytes * 8.0 / (stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())/1024);
  125. // updata gnuplot data
  126. DataSet.Add((double)Simulator::Now().GetSeconds(),(double) localThrou);
  127. std::cout<<"---------------------------------------------------------------------------"<<std::endl;
  128. }
  129. }
  130. Simulator::Schedule(Seconds(0.2 ),&ThroughputMonitor, fmhelper, flowMon,DataSet);
  131. //if(flowToXml)
  132. {
  133. flowMon->SerializeToXmlFile ("ThroughputMonitor.xml", true, true);
  134. }
  135.  
  136. }
  137.  
  138.  
  139. /**
  140. * Sample simulation script for a X2-based handover.
  141. * It instantiates two eNodeB, attaches one UE to the 'source' eNB and
  142. * triggers a handover of the UE towards the 'target' eNB.
  143. */
  144. int
  145. main (int argc, char *argv[])
  146. {
  147. uint16_t numberOfUes = 20;
  148. uint16_t numberOfEnbs = 1;
  149. //uint16_t numBearersPerUe = 2;
  150. double simTime = 1.0;
  151. //double distance = 100.0;
  152.  
  153. // change some default attributes so that they are reasonable for
  154. // this scenario, but do this before processing command line
  155. // arguments, so that the user is allowed to override these settings
  156. Config::SetDefault ("ns3::UdpClient::Interval", TimeValue (MilliSeconds (20)));
  157. Config::SetDefault ("ns3::UdpClient::MaxPackets", UintegerValue (1000000));
  158. Config::SetDefault ("ns3::LteEnbRrc::SrsPeriodicity", UintegerValue(320));
  159. Config::SetDefault ("ns3::LteHelper::UseIdealRrc", BooleanValue (false));
  160.  
  161. // Command line arguments
  162. CommandLine cmd;
  163. cmd.AddValue ("numberOfUes", "Number of UEs", numberOfUes);
  164. cmd.AddValue ("numberOfEnbs", "Number of eNodeBs", numberOfEnbs);
  165. cmd.AddValue ("simTime", "Total duration of the simulation (in seconds)", simTime);
  166. cmd.Parse (argc, argv);
  167.  
  168.  
  169. Ptr<LteHelper> lteHelper = CreateObject<LteHelper> ();
  170. Ptr<PointToPointEpcHelper> epcHelper = CreateObject<PointToPointEpcHelper> ();
  171. lteHelper->SetEpcHelper (epcHelper);
  172. lteHelper->SetSchedulerType ("ns3::RrFfMacScheduler");
  173. lteHelper->SetHandoverAlgorithmType ("ns3::NoOpHandoverAlgorithm"); // disable automatic handover
  174.  
  175. Ptr<Node> pgw = epcHelper->GetPgwNode ();
  176.  
  177. // Create a single RemoteHost
  178. NodeContainer remoteHostContainer;
  179. remoteHostContainer.Create (1);
  180. Ptr<Node> remoteHost = remoteHostContainer.Get (0);
  181. InternetStackHelper internet;
  182. internet.Install (remoteHostContainer);
  183.  
  184. // Create the Internet
  185. PointToPointHelper p2ph;
  186. p2ph.SetDeviceAttribute ("DataRate", DataRateValue (DataRate ("100Gb/s")));
  187. p2ph.SetDeviceAttribute ("Mtu", UintegerValue (1500));
  188. p2ph.SetChannelAttribute ("Delay", TimeValue (Seconds (0.010)));
  189. NetDeviceContainer internetDevices = p2ph.Install (pgw, remoteHost);
  190. Ipv4AddressHelper ipv4h;
  191. ipv4h.SetBase ("1.0.0.0", "255.0.0.0");
  192. Ipv4InterfaceContainer internetIpIfaces = ipv4h.Assign (internetDevices);
  193. Ipv4Address remoteHostAddr = internetIpIfaces.GetAddress (1);
  194.  
  195.  
  196. // Routing of the Internet Host (towards the LTE network)
  197. Ipv4StaticRoutingHelper ipv4RoutingHelper;
  198. Ptr<Ipv4StaticRouting> remoteHostStaticRouting = ipv4RoutingHelper.GetStaticRouting (remoteHost->GetObject<Ipv4> ());
  199. // interface 0 is localhost, 1 is the p2p device
  200. remoteHostStaticRouting->AddNetworkRouteTo (Ipv4Address ("7.0.0.0"), Ipv4Mask ("255.0.0.0"), 1);
  201.  
  202. NodeContainer ueNodes;
  203. NodeContainer enbNodes;
  204. enbNodes.Create (numberOfEnbs);
  205. ueNodes.Create (numberOfUes);
  206.  
  207. // Install Mobility Model
  208. // Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
  209. // for (uint16_t i = 0; i < numberOfEnbs; i++)
  210. // {
  211. // positionAlloc->Add (Vector (distance * 2 * i - distance, 0, 0));
  212. // }
  213. // for (uint16_t i = 0; i < numberOfUes; i++)
  214. // {
  215. // positionAlloc->Add (Vector (0, 0, 0));
  216. // }
  217. MobilityHelper mobility;
  218. /*mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
  219. "MinX", DoubleValue (10.0),
  220. "MinY", DoubleValue (10.0),
  221. "DeltaX", DoubleValue (12.0),
  222. "DeltaY", DoubleValue (10.0),
  223. "GridWidth", UintegerValue (5),
  224. "LayoutType", StringValue ("RowFirst"));
  225. mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
  226. "Bounds", RectangleValue (Rectangle (-500, 500, -250, 500)));*/
  227. mobility.Install (ueNodes);
  228. mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  229. mobility.Install (enbNodes);
  230.  
  231.  
  232.  
  233. // Install LTE Devices in eNB and UEs
  234. NetDeviceContainer enbLteDevs = lteHelper->InstallEnbDevice (enbNodes);
  235. NetDeviceContainer ueLteDevs = lteHelper->InstallUeDevice (ueNodes);
  236.  
  237. // Install the IP stack on the UEs
  238. internet.Install (ueNodes);
  239. Ipv4InterfaceContainer ueIpIfaces;
  240. ueIpIfaces = epcHelper->AssignUeIpv4Address (NetDeviceContainer (ueLteDevs));
  241.  
  242.  
  243. // Attach all UEs to the first eNodeB
  244. // for (uint16_t i = 0; i < numberOfUes; i++)
  245. // {
  246. // lteHelper->Attach (ueLteDevs.Get (i), enbLteDevs.Get (0));
  247. // }
  248.  
  249. lteHelper->AttachToClosestEnb (ueLteDevs, enbLteDevs);
  250.  
  251.  
  252. NS_LOG_LOGIC ("setting up applications");
  253.  
  254. // Install and start applications on UEs and remote host
  255. uint16_t dlPort = 10000;
  256. uint16_t ulPort = 20000;
  257.  
  258. // randomize a bit start times to avoid simulation artifacts
  259. // (e.g., buffer overflows due to packet transmissions happening
  260. // exactly at the same time)
  261. Ptr<UniformRandomVariable> startTimeSeconds = CreateObject<UniformRandomVariable> ();
  262. startTimeSeconds->SetAttribute ("Min", DoubleValue (0));
  263. startTimeSeconds->SetAttribute ("Max", DoubleValue (0.010));
  264.  
  265.  
  266. for (uint32_t u = 0; u < numberOfUes; ++u)
  267. {
  268. Ptr<Node> ue = ueNodes.Get (u);
  269. // Set the default gateway for the UE
  270. Ptr<Ipv4StaticRouting> ueStaticRouting = ipv4RoutingHelper.GetStaticRouting (ue->GetObject<Ipv4> ());
  271. ueStaticRouting->SetDefaultRoute (epcHelper->GetUeDefaultGatewayAddress (), 1);
  272.  
  273. //for (uint32_t b = 0; b < numBearersPerUe; ++b)
  274. {
  275. ++dlPort;
  276. ++ulPort;
  277.  
  278. ApplicationContainer clientApps;
  279. ApplicationContainer serverApps;
  280.  
  281. NS_LOG_LOGIC ("installing UDP DL app for UE " << u);
  282. UdpClientHelper dlClientHelper (ueIpIfaces.GetAddress (u), dlPort);
  283. clientApps.Add (dlClientHelper.Install (remoteHost));
  284. PacketSinkHelper dlPacketSinkHelper ("ns3::UdpSocketFactory",
  285. InetSocketAddress (Ipv4Address::GetAny (), dlPort));
  286. serverApps.Add (dlPacketSinkHelper.Install (ue));
  287.  
  288. NS_LOG_LOGIC ("installing UDP UL app for UE " << u);
  289. UdpClientHelper ulClientHelper (remoteHostAddr, ulPort);
  290. clientApps.Add (ulClientHelper.Install (ue));
  291. PacketSinkHelper ulPacketSinkHelper ("ns3::UdpSocketFactory",
  292. InetSocketAddress (Ipv4Address::GetAny (), ulPort));
  293. serverApps.Add (ulPacketSinkHelper.Install (remoteHost));
  294.  
  295. Ptr<EpcTft> tft = Create<EpcTft> ();
  296. EpcTft::PacketFilter dlpf;
  297. dlpf.localPortStart = dlPort;
  298. dlpf.localPortEnd = dlPort;
  299. tft->Add (dlpf);
  300. EpcTft::PacketFilter ulpf;
  301. ulpf.remotePortStart = ulPort;
  302. ulpf.remotePortEnd = ulPort;
  303. tft->Add (ulpf);
  304. EpsBearer bearer (EpsBearer::GBR_CONV_VOICE);
  305. lteHelper->ActivateDedicatedEpsBearer (ueLteDevs.Get (u), bearer, tft);
  306.  
  307. Time startTime = Seconds (startTimeSeconds->GetValue ());
  308. serverApps.Start (startTime);
  309. clientApps.Start (startTime);
  310.  
  311. }
  312. }
  313.  
  314.  
  315. // Add X2 inteface
  316. lteHelper->AddX2Interface (enbNodes);
  317.  
  318. lteHelper->EnablePhyTraces ();
  319. lteHelper->EnableMacTraces ();
  320. lteHelper->EnableRlcTraces ();
  321. lteHelper->EnablePdcpTraces ();
  322. Ptr<RadioBearerStatsCalculator> rlcStats = lteHelper->GetRlcStats ();
  323. rlcStats->SetAttribute ("EpochDuration", TimeValue (Seconds (0.02)));
  324. Ptr<RadioBearerStatsCalculator> pdcpStats = lteHelper->GetPdcpStats ();
  325. pdcpStats->SetAttribute ("EpochDuration", TimeValue (Seconds (0.02)));
  326.  
  327. //pointToPoint.EnablePcapAll ("blabla");
  328.  
  329.  
  330. // connect custom trace sinks for RRC connection establishment and handover notification
  331. Config::Connect ("/NodeList/*/DeviceList/*/LteEnbRrc/ConnectionEstablished",
  332. MakeCallback (&NotifyConnectionEstablishedEnb));
  333. Config::Connect ("/NodeList/*/DeviceList/*/LteUeRrc/ConnectionEstablished",
  334. MakeCallback (&NotifyConnectionEstablishedUe));
  335. Config::Connect ("/NodeList/*/DeviceList/*/LteEnbRrc/HandoverStart",
  336. MakeCallback (&NotifyHandoverStartEnb));
  337. Config::Connect ("/NodeList/*/DeviceList/*/LteUeRrc/HandoverStart",
  338. MakeCallback (&NotifyHandoverStartUe));
  339. Config::Connect ("/NodeList/*/DeviceList/*/LteEnbRrc/HandoverEndOk",
  340. MakeCallback (&NotifyHandoverEndOkEnb));
  341. Config::Connect ("/NodeList/*/DeviceList/*/LteUeRrc/HandoverEndOk",
  342. MakeCallback (&NotifyHandoverEndOkUe));
  343.  
  344.  
  345. Simulator::Stop (Seconds (simTime));
  346. AnimationInterface anim ("ltealaa.xml");
  347. anim.SetBackgroundImage("/home/alaa/repos/ns-3-allinone/ns-3-dev/scratch/map.png",-1,1,1,1,1);
  348. //anim.EnablePacketMetadata ();
  349. anim.SetMaxPktsPerTraceFile (100000000000);
  350. anim.SetMobilityPollInterval(Seconds(1));
  351. //anim.EnablePacketMetadata(true);
  352. std::string fileNameWithNoExtension = "FlowVSThroughput_";
  353. std::string graphicsFileName = fileNameWithNoExtension + ".png";
  354. std::string plotFileName = fileNameWithNoExtension + ".plt";
  355. std::string plotTitle = "Flow vs Throughput";
  356. std::string dataTitle = "Throughput";
  357.  
  358. // Instantiate the plot and set its title.
  359. Gnuplot gnuplot (graphicsFileName);
  360. gnuplot.SetTitle (plotTitle);
  361.  
  362. // Make the graphics file, which the plot file will be when it
  363. // is used with Gnuplot, be a PNG file.
  364. gnuplot.SetTerminal ("png");
  365.  
  366. // Set the labels for each axis.
  367. gnuplot.SetLegend ("Flow", "Throughput");
  368.  
  369.  
  370. Gnuplot2dDataset dataset;
  371. dataset.SetTitle (dataTitle);
  372. dataset.SetStyle (Gnuplot2dDataset::LINES_POINTS);
  373.  
  374. //flowMonitor declaration
  375. FlowMonitorHelper fmHelper;
  376. Ptr<FlowMonitor> allMon = fmHelper.InstallAll();
  377. allMon->CheckForLostPackets ();
  378. // call the flow monitor function
  379. ThroughputMonitor(&fmHelper, allMon, dataset);
  380.  
  381. Simulator::Run ();
  382.  
  383. //Gnuplot ...continued
  384. gnuplot.AddDataset (dataset);
  385. // Open the plot file.
  386. std::ofstream plotFile (plotFileName.c_str());
  387. // Write the plot file.
  388. gnuplot.GenerateOutput (plotFile);
  389. // Close the plot file.
  390. plotFile.close ();
  391.  
  392. // GtkConfigStore config;
  393. // config.ConfigureAttributes ();
  394.  
  395. Simulator::Destroy ();
  396. return 0;
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement