Study the Effect of Buffer Size on Packet Loss in a Point-to-Point Router with UDP CBR Traffic
Problem Statement:
Study the effect of buffer size on packet loss in a point-to-point router with UDP CBR traffic.
Source Code:
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/netanim-module.h"
#include <fstream> // Required for writing data to the text file
using namespace ns3;
int main (int argc, char *argv[])
{
// 1. Default Variables and Command Line Parsing
uint32_t bufferSize = 10; // Default buffer size in packets
CommandLine cmd;
cmd.AddValue ("bufferSize", "Max number of packets in Queue", bufferSize);
cmd.Parse (argc, argv);
// 2. Node Creation
NodeContainer nodes;
nodes.Create (2);
// 3. Point-to-Point Link Setup
PointToPointHelper p2p;
p2p.SetDeviceAttribute ("DataRate", StringValue ("2Mbps"));
p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
// Applying the Buffer Size (Queue Limit)
p2p.SetQueue ("ns3::DropTailQueue", "MaxSize", QueueSizeValue (QueueSize (QueueSizeUnit::PACKETS, bufferSize)));
NetDeviceContainer devices = p2p.Install (nodes);
// 4. Internet Stack and IP Assignment
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// 5. Application Setup (UDP CBR Traffic at 5Mbps to force drops)
uint16_t port = 9;
OnOffHelper onoff ("ns3::UdpSocketFactory", Address (InetSocketAddress (interfaces.GetAddress (1), port)));
onoff.SetConstantRate (DataRate ("5Mbps"));
onoff.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer apps = onoff.Install (nodes.Get (0));
apps.Start (Seconds (1.0));
apps.Stop (Seconds (10.0));
// 6. NetAnim Configuration
AnimationInterface anim ("24BPS1020.xml"); // Your XML file name
anim.SetConstantPosition (nodes.Get (0), 10.0, 50.0);
anim.SetConstantPosition (nodes.Get (1), 90.0, 50.0);
// 7. Flow Monitor Setup
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll ();
// 8. Run Simulation
Simulator::Stop (Seconds (11.0));
Simulator::Run ();
// 9. Results Extraction and File Writing
monitor->CheckForLostPackets ();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
// Open a file to append the data for your graph
std::ofstream dataset;
dataset.open ("loss_data.txt", std::ios_base::app);
// Loop through the flows to print and save stats
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
{
double lossRatio = (double)i->second.lostPackets / i->second.txPackets * 100;
// Print Output to Terminal
std::cout << "Buffer Size: " << bufferSize << " Packets" << std::endl;
std::cout << "Packets Transmitted: " << i->second.txPackets << std::endl;
std::cout << "Packets Received: " << i->second.rxPackets << std::endl;
std::cout << "Packets Lost: " << i->second.lostPackets << std::endl;
std::cout << "Packet Loss Ratio: " << lossRatio << "%" << std::endl;
std::cout << "-----------------------------------" << std::endl;
// Write directly to the text file (Format: BufferSize LossPercentage)
dataset << bufferSize << " " << lossRatio << std::endl;
}
dataset.close (); // Close the text file
// 10. Cleanup
Simulator::Destroy ();
return 0;
}
Output:
Commands & Execution Runs:
./ns3 run "scratch/24BPS1020.cc --bufferSize=5"
./ns3 run "scratch/24BPS1020.cc --bufferSize=10"
./ns3 run "scratch/24BPS1020.cc --bufferSize=25"
./ns3 run "scratch/24BPS1020.cc --bufferSize=50"
NetAnim Visualization:
NetAnim simulation showing point-to-point traffic flow. The NetAnim visualizer depicts the fundamental network topology and runtime environment generated by the ns-3 simulation script.
GNU Plot:
The generated graph plots the Packet Loss Ratio (represented as a percentage on the Y-axis) against the Router's Buffer Size (represented as the number of packets in the DropTailQueue on the X-axis) for a point-to-point network link.
LLM Information:
LLM Used: Gemini (Google AI)
Prompt Provided:
Study the effect of buffer size on packet loss in a point-to-point router with UDP CBR traffic. explain what has to be. You need to design a network as per the Question given in the following Excel sheet. The exercises were allotted to students based on randomisation within an Excel sheet. You need to submit the following: ... these are the instruction provided and the topic is already given. description of the output graph. similarly, for netanim.
Comments
Post a Comment