Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ns3/core-module.h"
- #include "ns3/network-module.h"
- #include "ns3/internet-module.h"
- #include "ns3/mobility-module.h"
- #include "ns3/lte-module.h"
- #include "ns3/lte-module.h"
- #include "ns3/flow-monitor-module.h"
- #include "ns3/applications-module.h"
- #include "ns3/point-to-point-module.h"
- #include "ns3/config-store-module.h"
- #include "ns3/netanim-module.h"
- #include "ns3/gnuplot.h"
- #include <math.h>
- using namespace ns3;
- NS_LOG_COMPONENT_DEFINE ("lte_handover_scenario_manual_handover");
- void
- NotifyConnectionEstablishedUe (std::string context,
- uint64_t imsi, //IMSI -> International Mobile Subscriber Identity
- uint16_t cellid,
- uint16_t rnti) //RNTI -> Radio Network Temporary Identifier
- {
- std::cout << Simulator::Now ().GetSeconds () << " " << context
- << " UE IMSI " << imsi
- << ": connected to CellId " << cellid
- << " with RNTI " << rnti
- << std::endl;
- }
- void
- NotifyHandoverStartUe (std::string context,
- uint64_t imsi,
- uint16_t cellid,
- uint16_t rnti,
- uint16_t targetCellId)
- {
- std::cout << Simulator::Now ().GetSeconds () << " " << context
- << " UE IMSI " << imsi
- << ": previously connected to CellId " << cellid
- << " with RNTI " << rnti
- << ", doing handover to CellId " << targetCellId
- << std::endl;
- }
- void
- NotifyHandoverEndOkUe (std::string context,
- uint64_t imsi,
- uint16_t cellid,
- uint16_t rnti)
- {
- std::cout << Simulator::Now ().GetSeconds () << " " << context
- << " UE IMSI " << imsi
- << ": successful handover to CellId " << cellid
- << " with RNTI " << rnti
- << std::endl;
- }
- void
- NotifyConnectionEstablishedEnb (std::string context,
- uint64_t imsi,
- uint16_t cellid,
- uint16_t rnti)
- {
- std::cout << Simulator::Now ().GetSeconds () << " " << context
- << " eNB CellId " << cellid
- << ": successful connection of UE with IMSI " << imsi
- << " RNTI " << rnti
- << std::endl;
- }
- void
- NotifyHandoverStartEnb (std::string context,
- uint64_t imsi,
- uint16_t cellid,
- uint16_t rnti,
- uint16_t targetCellId)
- {
- std::cout << Simulator::Now ().GetSeconds () << " " << context
- << " eNB CellId " << cellid
- << ": start handover of UE with IMSI " << imsi
- << " RNTI " << rnti
- << " to CellId " << targetCellId
- << std::endl;
- }
- void
- NotifyHandoverEndOkEnb (std::string context,
- uint64_t imsi,
- uint16_t cellid,
- uint16_t rnti)
- {
- std::cout << Simulator::Now ().GetSeconds () << " " << context
- << " eNB CellId " << cellid
- << ": completed handover of UE with IMSI " << imsi
- << " RNTI " << rnti
- << std::endl;
- }
- void ThroughputMonitor (FlowMonitorHelper *fmhelper, Ptr<FlowMonitor> flowMon,Gnuplot2dDataset DataSet)
- {
- double localThrou=0;
- std::map<FlowId, FlowMonitor::FlowStats> flowStats = flowMon->GetFlowStats();
- Ptr<Ipv4FlowClassifier> classing = DynamicCast<Ipv4FlowClassifier> (fmhelper->GetClassifier());
- for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator stats = flowStats.begin (); stats != flowStats.end (); ++stats)
- {
- Ipv4FlowClassifier::FiveTuple fiveTuple = classing->FindFlow (stats->first);
- if (fiveTuple.sourceAddress == Ipv4Address("1.0.0.2") /*|| fiveTuple.sourceAddress == Ipv4Address("7.0.0.2") || fiveTuple.sourceAddress == Ipv4Address("7.0.0.3")*/)
- {
- std::cout<<"Flow ID : " << stats->first <<" ; "<< fiveTuple.sourceAddress <<" -----> "<<fiveTuple.destinationAddress<<std::endl;
- std::cout<<"Tx Packets : " << stats->second.txPackets<<std::endl;
- std::cout<<"Rx Packets : " << stats->second.rxPackets<<std::endl;
- std::cout<<"Duration : "<<(stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())<<std::endl;
- std::cout<<"Last Received Packet : "<< stats->second.timeLastRxPacket.GetSeconds()<<" Seconds"<<std::endl;
- std::cout<<"Throughput: " << stats->second.rxBytes * 8.0 / (stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())/1024 << " Kbps"<<std::endl;
- std::cout<< "Mean{Delay}: " << (stats->second.delaySum.GetSeconds()/stats->second.rxPackets) << "\n";
- std::cout<< "Mean{Jitter}: " << (stats->second.jitterSum.GetSeconds()/(stats->second.rxPackets)) << "\n";
- std::cout<< "Total{Delay}: " << (stats->second.delaySum.GetSeconds()) << "\n";
- std::cout<< "Total{Jitter}: " << (stats->second.jitterSum.GetSeconds()) << "\n";
- std::cout<< "Lost Packets: " << (stats->second.lostPackets) << "\n";
- std::cout<< "Dropped Packets: " << (stats->second.packetsDropped.size()) << "\n";
- localThrou=(stats->second.rxBytes * 8.0 / (stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())/1024);
- // updata gnuplot data
- DataSet.Add((double)Simulator::Now().GetSeconds(),(double) localThrou);
- std::cout<<"---------------------------------------------------------------------------"<<std::endl;
- }
- }
- Simulator::Schedule(Seconds(0.2 ),&ThroughputMonitor, fmhelper, flowMon,DataSet);
- //if(flowToXml)
- {
- flowMon->SerializeToXmlFile ("ThroughputMonitor.xml", true, true);
- }
- }
- /**
- * Sample simulation script for a X2-based handover.
- * It instantiates two eNodeB, attaches one UE to the 'source' eNB and
- * triggers a handover of the UE towards the 'target' eNB.
- */
- int
- main (int argc, char *argv[])
- {
- uint16_t numberOfUes = 20;
- uint16_t numberOfEnbs = 1;
- //uint16_t numBearersPerUe = 2;
- double simTime = 1.0;
- //double distance = 100.0;
- // change some default attributes so that they are reasonable for
- // this scenario, but do this before processing command line
- // arguments, so that the user is allowed to override these settings
- Config::SetDefault ("ns3::UdpClient::Interval", TimeValue (MilliSeconds (20)));
- Config::SetDefault ("ns3::UdpClient::MaxPackets", UintegerValue (1000000));
- Config::SetDefault ("ns3::LteEnbRrc::SrsPeriodicity", UintegerValue(320));
- Config::SetDefault ("ns3::LteHelper::UseIdealRrc", BooleanValue (false));
- // Command line arguments
- CommandLine cmd;
- cmd.AddValue ("numberOfUes", "Number of UEs", numberOfUes);
- cmd.AddValue ("numberOfEnbs", "Number of eNodeBs", numberOfEnbs);
- cmd.AddValue ("simTime", "Total duration of the simulation (in seconds)", simTime);
- cmd.Parse (argc, argv);
- Ptr<LteHelper> lteHelper = CreateObject<LteHelper> ();
- Ptr<PointToPointEpcHelper> epcHelper = CreateObject<PointToPointEpcHelper> ();
- lteHelper->SetEpcHelper (epcHelper);
- lteHelper->SetSchedulerType ("ns3::RrFfMacScheduler");
- lteHelper->SetHandoverAlgorithmType ("ns3::NoOpHandoverAlgorithm"); // disable automatic handover
- Ptr<Node> pgw = epcHelper->GetPgwNode ();
- // Create a single RemoteHost
- NodeContainer remoteHostContainer;
- remoteHostContainer.Create (1);
- Ptr<Node> remoteHost = remoteHostContainer.Get (0);
- InternetStackHelper internet;
- internet.Install (remoteHostContainer);
- // Create the Internet
- PointToPointHelper p2ph;
- p2ph.SetDeviceAttribute ("DataRate", DataRateValue (DataRate ("100Gb/s")));
- p2ph.SetDeviceAttribute ("Mtu", UintegerValue (1500));
- p2ph.SetChannelAttribute ("Delay", TimeValue (Seconds (0.010)));
- NetDeviceContainer internetDevices = p2ph.Install (pgw, remoteHost);
- Ipv4AddressHelper ipv4h;
- ipv4h.SetBase ("1.0.0.0", "255.0.0.0");
- Ipv4InterfaceContainer internetIpIfaces = ipv4h.Assign (internetDevices);
- Ipv4Address remoteHostAddr = internetIpIfaces.GetAddress (1);
- // Routing of the Internet Host (towards the LTE network)
- Ipv4StaticRoutingHelper ipv4RoutingHelper;
- Ptr<Ipv4StaticRouting> remoteHostStaticRouting = ipv4RoutingHelper.GetStaticRouting (remoteHost->GetObject<Ipv4> ());
- // interface 0 is localhost, 1 is the p2p device
- remoteHostStaticRouting->AddNetworkRouteTo (Ipv4Address ("7.0.0.0"), Ipv4Mask ("255.0.0.0"), 1);
- NodeContainer ueNodes;
- NodeContainer enbNodes;
- enbNodes.Create (numberOfEnbs);
- ueNodes.Create (numberOfUes);
- // Install Mobility Model
- // Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
- // for (uint16_t i = 0; i < numberOfEnbs; i++)
- // {
- // positionAlloc->Add (Vector (distance * 2 * i - distance, 0, 0));
- // }
- // for (uint16_t i = 0; i < numberOfUes; i++)
- // {
- // positionAlloc->Add (Vector (0, 0, 0));
- // }
- MobilityHelper mobility;
- /*mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
- "MinX", DoubleValue (10.0),
- "MinY", DoubleValue (10.0),
- "DeltaX", DoubleValue (12.0),
- "DeltaY", DoubleValue (10.0),
- "GridWidth", UintegerValue (5),
- "LayoutType", StringValue ("RowFirst"));
- mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
- "Bounds", RectangleValue (Rectangle (-500, 500, -250, 500)));*/
- mobility.Install (ueNodes);
- mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
- mobility.Install (enbNodes);
- // Install LTE Devices in eNB and UEs
- NetDeviceContainer enbLteDevs = lteHelper->InstallEnbDevice (enbNodes);
- NetDeviceContainer ueLteDevs = lteHelper->InstallUeDevice (ueNodes);
- // Install the IP stack on the UEs
- internet.Install (ueNodes);
- Ipv4InterfaceContainer ueIpIfaces;
- ueIpIfaces = epcHelper->AssignUeIpv4Address (NetDeviceContainer (ueLteDevs));
- // Attach all UEs to the first eNodeB
- // for (uint16_t i = 0; i < numberOfUes; i++)
- // {
- // lteHelper->Attach (ueLteDevs.Get (i), enbLteDevs.Get (0));
- // }
- lteHelper->AttachToClosestEnb (ueLteDevs, enbLteDevs);
- NS_LOG_LOGIC ("setting up applications");
- // Install and start applications on UEs and remote host
- uint16_t dlPort = 10000;
- uint16_t ulPort = 20000;
- // randomize a bit start times to avoid simulation artifacts
- // (e.g., buffer overflows due to packet transmissions happening
- // exactly at the same time)
- Ptr<UniformRandomVariable> startTimeSeconds = CreateObject<UniformRandomVariable> ();
- startTimeSeconds->SetAttribute ("Min", DoubleValue (0));
- startTimeSeconds->SetAttribute ("Max", DoubleValue (0.010));
- for (uint32_t u = 0; u < numberOfUes; ++u)
- {
- Ptr<Node> ue = ueNodes.Get (u);
- // Set the default gateway for the UE
- Ptr<Ipv4StaticRouting> ueStaticRouting = ipv4RoutingHelper.GetStaticRouting (ue->GetObject<Ipv4> ());
- ueStaticRouting->SetDefaultRoute (epcHelper->GetUeDefaultGatewayAddress (), 1);
- //for (uint32_t b = 0; b < numBearersPerUe; ++b)
- {
- ++dlPort;
- ++ulPort;
- ApplicationContainer clientApps;
- ApplicationContainer serverApps;
- NS_LOG_LOGIC ("installing UDP DL app for UE " << u);
- UdpClientHelper dlClientHelper (ueIpIfaces.GetAddress (u), dlPort);
- clientApps.Add (dlClientHelper.Install (remoteHost));
- PacketSinkHelper dlPacketSinkHelper ("ns3::UdpSocketFactory",
- InetSocketAddress (Ipv4Address::GetAny (), dlPort));
- serverApps.Add (dlPacketSinkHelper.Install (ue));
- NS_LOG_LOGIC ("installing UDP UL app for UE " << u);
- UdpClientHelper ulClientHelper (remoteHostAddr, ulPort);
- clientApps.Add (ulClientHelper.Install (ue));
- PacketSinkHelper ulPacketSinkHelper ("ns3::UdpSocketFactory",
- InetSocketAddress (Ipv4Address::GetAny (), ulPort));
- serverApps.Add (ulPacketSinkHelper.Install (remoteHost));
- Ptr<EpcTft> tft = Create<EpcTft> ();
- EpcTft::PacketFilter dlpf;
- dlpf.localPortStart = dlPort;
- dlpf.localPortEnd = dlPort;
- tft->Add (dlpf);
- EpcTft::PacketFilter ulpf;
- ulpf.remotePortStart = ulPort;
- ulpf.remotePortEnd = ulPort;
- tft->Add (ulpf);
- EpsBearer bearer (EpsBearer::GBR_CONV_VOICE);
- lteHelper->ActivateDedicatedEpsBearer (ueLteDevs.Get (u), bearer, tft);
- Time startTime = Seconds (startTimeSeconds->GetValue ());
- serverApps.Start (startTime);
- clientApps.Start (startTime);
- }
- }
- // Add X2 inteface
- lteHelper->AddX2Interface (enbNodes);
- lteHelper->EnablePhyTraces ();
- lteHelper->EnableMacTraces ();
- lteHelper->EnableRlcTraces ();
- lteHelper->EnablePdcpTraces ();
- Ptr<RadioBearerStatsCalculator> rlcStats = lteHelper->GetRlcStats ();
- rlcStats->SetAttribute ("EpochDuration", TimeValue (Seconds (0.02)));
- Ptr<RadioBearerStatsCalculator> pdcpStats = lteHelper->GetPdcpStats ();
- pdcpStats->SetAttribute ("EpochDuration", TimeValue (Seconds (0.02)));
- //pointToPoint.EnablePcapAll ("blabla");
- // connect custom trace sinks for RRC connection establishment and handover notification
- Config::Connect ("/NodeList/*/DeviceList/*/LteEnbRrc/ConnectionEstablished",
- MakeCallback (&NotifyConnectionEstablishedEnb));
- Config::Connect ("/NodeList/*/DeviceList/*/LteUeRrc/ConnectionEstablished",
- MakeCallback (&NotifyConnectionEstablishedUe));
- Config::Connect ("/NodeList/*/DeviceList/*/LteEnbRrc/HandoverStart",
- MakeCallback (&NotifyHandoverStartEnb));
- Config::Connect ("/NodeList/*/DeviceList/*/LteUeRrc/HandoverStart",
- MakeCallback (&NotifyHandoverStartUe));
- Config::Connect ("/NodeList/*/DeviceList/*/LteEnbRrc/HandoverEndOk",
- MakeCallback (&NotifyHandoverEndOkEnb));
- Config::Connect ("/NodeList/*/DeviceList/*/LteUeRrc/HandoverEndOk",
- MakeCallback (&NotifyHandoverEndOkUe));
- Simulator::Stop (Seconds (simTime));
- AnimationInterface anim ("ltealaa.xml");
- anim.SetBackgroundImage("/home/alaa/repos/ns-3-allinone/ns-3-dev/scratch/map.png",-1,1,1,1,1);
- //anim.EnablePacketMetadata ();
- anim.SetMaxPktsPerTraceFile (100000000000);
- anim.SetMobilityPollInterval(Seconds(1));
- //anim.EnablePacketMetadata(true);
- std::string fileNameWithNoExtension = "FlowVSThroughput_";
- std::string graphicsFileName = fileNameWithNoExtension + ".png";
- std::string plotFileName = fileNameWithNoExtension + ".plt";
- std::string plotTitle = "Flow vs Throughput";
- std::string dataTitle = "Throughput";
- // Instantiate the plot and set its title.
- Gnuplot gnuplot (graphicsFileName);
- gnuplot.SetTitle (plotTitle);
- // Make the graphics file, which the plot file will be when it
- // is used with Gnuplot, be a PNG file.
- gnuplot.SetTerminal ("png");
- // Set the labels for each axis.
- gnuplot.SetLegend ("Flow", "Throughput");
- Gnuplot2dDataset dataset;
- dataset.SetTitle (dataTitle);
- dataset.SetStyle (Gnuplot2dDataset::LINES_POINTS);
- //flowMonitor declaration
- FlowMonitorHelper fmHelper;
- Ptr<FlowMonitor> allMon = fmHelper.InstallAll();
- allMon->CheckForLostPackets ();
- // call the flow monitor function
- ThroughputMonitor(&fmHelper, allMon, dataset);
- Simulator::Run ();
- //Gnuplot ...continued
- gnuplot.AddDataset (dataset);
- // Open the plot file.
- std::ofstream plotFile (plotFileName.c_str());
- // Write the plot file.
- gnuplot.GenerateOutput (plotFile);
- // Close the plot file.
- plotFile.close ();
- // GtkConfigStore config;
- // config.ConfigureAttributes ();
- Simulator::Destroy ();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement