How to Calculate Packet delivery ratio in ns3

To calculate the Packet Delivery Ratio (PDR) in ns3, we need to compute how much number of packets sent by source node and number of packets received by destination node is measured. The PDR is defined as the ratio of the number of packets received to the number of packets sent.

Here are the procedures on how to calculate the PDR in ns3:

  1. Set up Your Simulation Environment: Generate a network topology; configure nodes, links, and protocols.
  2. Install Applications: Set up applications on the nodes to generate and receive traffic.
  3. Trace the Packets: To record the number of packets sent and received by use of ns3.
  4. Calculate PDR: Calculate the ratio of the number of packets received to the number of packets sent.

Example

In this direction, now we are going to see the instance on how to simulate the path delivery ratio in ns3:

Step 1: Set Up Your Simulation Environment

First, create a basic network topology. This example will use a point-to-point link between two nodes.

#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”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“NetworkPdrExample”);

int main (int argc, char *argv[])

{

// Create two nodes

NodeContainer nodes;

nodes.Create (2);

// Set up the point-to-point link

PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));

pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));

// Install the link devices on the nodes

NetDeviceContainer devices;

devices = pointToPoint.Install (nodes);

// Install the internet stack

InternetStackHelper stack;

stack.Install (nodes);

// Assign IP addresses

Ipv4AddressHelper address;

address.SetBase (“10.1.1.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces = address.Assign (devices);

// Set up the UDP echo server and client

uint16_t port = 9; // well-known echo port number

UdpEchoServerHelper echoServer (port);

ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (interfaces.GetAddress (1), port);

echoClient.SetAttribute (“MaxPackets”, UintegerValue (100));

echoClient.SetAttribute (“Interval”, TimeValue (Seconds (0.1))); // 10 packets per second

echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));

ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));

clientApps.Start (Seconds (2.0));

clientApps.Stop (Seconds (10.0));

// Enable tracing

AsciiTraceHelper ascii;

pointToPoint.EnableAsciiAll (ascii.CreateFileStream (“network-pdr.tr”));

// Run the simulation

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

Step 2: Install Applications

In this example, a UDP echo server is installed on one node and a UDP echo client on the other node. The client sends 100 packets, each 1024 bytes in size, at an interval of 0.1 seconds.

Step 3: Trace the Packets

NS-3 provides several ways to trace packets. You can use ASCII or PCAP tracing. In the example above, ASCII tracing is enabled.

Step 4: Calculate PDR

After running the simulation, parse the trace file to extract the number of packets sent and received. Here is a simple way to process the trace file:

Example Python Script to Calculate PDR

Here is a sample Python snippet to calculate the PDR:

sent_packets = 0

received_packets = 0

with open(“network-pdr.tr”, “r”) as trace_file:

for line in trace_file:

if “UdpEchoClient” in line and “Sent” in line:

sent_packets += 1

elif “UdpEchoServer” in line and “Received” in line:

received_packets += 1

pdr = (received_packets / sent_packets) * 100 if sent_packets > 0 else 0

print(f”Sent Packets: {sent_packets}”)

print(f”Received Packets: {received_packets}”)

print(f”Packet Delivery Ratio: {pdr} %”)

Explanation

  • Set up Your Simulation Environment: Create two nodes and connect them with a point-to-point link.
  • Install Applications: Install a UDP echo server on one node and a UDP echo client on the other node. Configure the client to send a specified number of packets at a specified interval.
  • Trace the Packets: Enable ASCII tracing to record packet events.
  • Calculate PDR: Parse the trace file to extract the number of packets sent and received. Calculate the PDR by dividing the number of packets received by the number of packets sent and multiplying by 100 to get the percentage.

In the end, we learned to how the packet delivery ratio measured the number of packets sent and received by use of ns3.if you want further details, we will elaborate and deliver how to compute the packet delivery ratio in different tools.

Share with us all your parameter details we will guide you Packet delivery ratio in ns3tool for a perfect comparative analysis.