UDP Packet Loss Rate Calculation
Topology
N0(UDP Sender)----P2Plink(5Mbps,2ms,
Loss Rate is set to 0.01)---N1(UDP Sink)
loss-udp.cc (put this file under scratch)
#include <fstream> #include <string.h> #include "ns3/core-module.h" #include "ns3/internet-module.h" #include "ns3/point-to-point-module.h" #include "ns3/packet-sink.h" #include "ns3/packet-sink-helper.h" #include "ns3/on-off-helper.h" #include "ns3/error-model.h" #include "ns3/flow-monitor-module.h" using namespace ns3; NS_LOG_COMPONENT_DEFINE ("LossUDP");
uint64_t TotalRx = 0; uint64_t TotalTx = 0; uint64_t TotalLoss = 0; int main (int argc, char *argv[]) { NS_LOG_INFO ("Create nodes."); NodeContainer n; n.Create (2); InternetStackHelper internet; internet.Install (n); NS_LOG_INFO ("Create channels."); PointToPointHelper pointToPoint; pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); pointToPoint.SetDeviceAttribute("Mtu", UintegerValue(1400)); NetDeviceContainer p2pDevices; p2pDevices = pointToPoint.Install (n);
Ptr<RateErrorModel> em = CreateObject<RateErrorModel> (); em->SetAttribute ("ErrorRate", DoubleValue (0.01)); em->SetAttribute ("ErrorUnit", StringValue ("ERROR_UNIT_PACKET")); p2pDevices.Get (1)->SetAttribute ("ReceiveErrorModel", PointerValue (em)); Ipv4AddressHelper ipv4; NS_LOG_INFO ("Assign IP Addresses."); ipv4.SetBase ("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer i = ipv4.Assign (p2pDevices); NS_LOG_INFO ("Create Applications."); PacketSinkHelper sinkHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), 9)); ApplicationContainer sinkApp = sinkHelper.Install (n.Get(1)); OnOffHelper server ("ns3::UdpSocketFactory", (InetSocketAddress (i.GetAddress (1), 9))); server.SetAttribute ("PacketSize", UintegerValue (172)); // G.711 encoding, 20ms delay, 160bytes +12bytes(rtp) server.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]")); server.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]")); server.SetAttribute ("DataRate", DataRateValue (DataRate ("64Kbps"))); ApplicationContainer serverApp = server.Install (n.Get(0)); sinkApp.Start (Seconds (0.0)); serverApp.Start (Seconds (1.0)); FlowMonitorHelper flowmon; Ptr<FlowMonitor> monitor = flowmon.InstallAll (); Simulator::Stop (Seconds (100.0)); NS_LOG_INFO ("Run Simulation."); Simulator::Run (); Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier>(flowmon.GetClassifier()); std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats(); for(std::map<FlowId, FlowMonitor::FlowStats>::const_iterator set = stats.begin(); set != stats.end(); set++) { Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow(set->first); std::cout << "Flow " << set->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")" << std::endl; std::cout << "Tx packets:" << set->second.txPackets << std::endl; std::cout << "Rx packets:" << set->second.rxPackets << std::endl; std::cout << "Loss Rate:" << (double)(set->second.txPackets-set->second.rxPackets)/set->second.txPackets << std::endl; }
Simulator::Destroy (); NS_LOG_INFO ("Done."); } |
Execution
Last Modified: 2022/2/5 done
[Author]
Dr. Chih-Heng Ke
Department
of Computer Science and Information Engineering, National Quemoy
University, Kinmen, Taiwan
Email:
smallko@gmail.com