How to Calculate Network Error Rate in NS2

To calculate the network error rate using NS2 that refers to the percentage of packets or bits, which were lost or corrupted during transmission across the network. It is an essential performance metric for measuring the reliability of a network, particularly in situations in which the interference, noise, or other factors can lead to errors.

There are various paths to compute the network error rate based on whether we require to calculate:

  • Packet error rate (PER): The ratio of lost or corrupted packets to the total number of sent packets.
  • Bit error rate (BER): The ratio of erroneous bits to the total number of transferred bits.

Steps to Calculate Packet Error Rate (PER) in NS2

  1. Generate a Trace File

Initially, make sure that NS2 simulation is set up to generate a trace file, which records all packet transmission, reception, and drop events. This file will be supported to estimate the packet losses, drops, and errors.

set tracefile [open out.tr w]

$ns trace-all $tracefile

  1. Identify Packet Transmission and Drop Events

In the trace file, packet events are recorded with particular markers:

  • +: Packet sent.
  • r: Packet received successfully.
  • d: Packet dropped, which could indicate a transmission error or congestion.

A usual trace file line looks like this:

+ 0.12345 0 1 tcp 1040 ——- [0 0 1 0] ——- [1:0 0:0 32 0] [0] 0 0

r 0.23456 1 0 tcp 1040 ——- [0 1 0 0] ——- [0:0 1:0 32 0] [0] 0 0

d 0.34567 1 0 tcp 1040 ——- [0 1 0 0] ——- [0:0 1:0 32 0] [0] 0 0

  • + indicates packet sent.
  • r indicates packet received.
  • d indicates packet dropped (which may include errors due to collisions, interference, or buffer overflows).
  1. Calculate Packet Error Rate (PER)

To compute the Packet Error Rate (PER), we want to discover how many packets were transmitted and how many were either dropped or not received.

AWK Script to Calculate PER:

awk ‘{

if ($1 == “+” && $4 == “tcp”) {

sent_packets++;

}

if ($1 == “r” && $4 == “tcp”) {

received_packets++;

}

if ($1 == “d” && $4 == “tcp”) {

dropped_packets++;

}

} END {

if (sent_packets > 0) {

packet_error_rate = (dropped_packets / sent_packets) * 100;

print “Packet Error Rate (PER): ” packet_error_rate “%”;

} else {

print “No packets were sent.”;

}

}’ out.tr

This script computes:

  • sent_packets: Total number of TCP packets are transmitted.
  • received_packets: Total number of TCP packets effectively received.
  • dropped_packets: Total number of TCP packets dropped that may indicate errors.

To end, the Packet Error Rate (PER) is computed as:

PER=(Dropped PacketsSent Packets)×100\text{PER} = \left( \frac{\text{Dropped Packets}}{\text{Sent Packets}} \right) \times 100PER=(Sent PacketsDropped Packets​)×100

  1. Simulate Errors in NS2

If we require to openly replicate the network errors then we can be launched the packet loss or errors using NS2’s error models.

Now, how we can describe a uniform error model to introduce random packet losses/errors:

# Create a uniform error model with a packet drop probability of 5%

set loss_module [new ErrorModel]

$loss_module set rate_ 0.05  ;# 5% error rate

$loss_module unit pkt

$loss_module ranvar [new RandomVariable/Uniform]

# Attach the error model to a link

$ns lossmodel $loss_module $node(0) $node(1)

This model will randomly drop 5% of the packets sent among the node (0) and node (1), replicating a packet error rate.

Steps to Calculate Bit Error Rate (BER) in NS2

The Bit Error Rate (BER) estimates how frequently bits are transferred incorrectly because of noise or other transmission impairments. Although NS2 is primarily packet-focused, we can be evaluated BER rely on the packet size and the number of packets dropped.

  1. Estimate BER Based on PER

If we understand the packet size then we can be approximated the Bit Error Rate (BER) from the Packet Error Rate (PER). For a packet with N bits, if the packet is dropped then it is supposed that at least one bit was erroneous.

The relationship among PER and BER for a packet with N bits is approximately:

BER≈PERN\text{BER} \approx \frac{\text{PER}}{N}BER≈NPER​

Where:

  • PER is the Packet Error Rate (as calculated earlier).
  • N is the number of bits in each packet.

For instance, if each packet is 1040 bytes (8320 bits), and the PER is 5%, the BER can be assessed as:

BER=0.058320≈6.01×10−6\text{BER} = \frac{0.05}{8320} \approx 6.01 \times 10^{-6}BER=83200.05​≈6.01×10−6

  1. AWK Script to Calculate BER from Trace File

If we have the PER and understand the packet size in bits then we can be calculated the BER using an AWK script similar to the PER calculation.

For example:

awk ‘{

if ($1 == “+” && $4 == “tcp”) {

sent_packets++;

}

if ($1 == “d” && $4 == “tcp”) {

dropped_packets++;

}

} END {

if (sent_packets > 0) {

per = dropped_packets / sent_packets;

packet_size_bits = 1040 * 8;  # Assuming 1040 bytes per packet

ber = per / packet_size_bits;

print “Estimated Bit Error Rate (BER): ” ber;

} else {

print “No packets were sent.”;

}

}’ out.tr

In this script:

  • per is the computed Packet Error Rate.
  • packet_size_bits is the size of each packet in bits (e.g., 1040 bytes = 8320 bits).
  1. Simulate Bit Errors in NS2

We can be replicated the bit errors explicitly using an ErrorModel within NS2 that can mimic errors at the bit level.

For instance, to mimic a BER of 1e-5, we can be described an error model that launches random bit errors:

# Create a uniform error model with a bit error rate of 1e-5

set loss_module [new ErrorModel]

$loss_module set rate_ 1e-5  ;# Bit error rate of 1e-5

$loss_module unit bit         ;# Error applied at the bit level

$loss_module ranvar [new RandomVariable/Uniform]

# Attach the error model to the link between node 0 and node 1

$ns lossmodel $loss_module $node(0) $node(1)

This error model will launch the random bit errors during transmission, also we can estimate the resulting packet drops or corruptions by analysing the trace file.

  1. Analyse the Impact of BER on Packet Delivery

If we are replicating the bit errors then we can be measured their effect on packet delivery by observing the packet drop rate, packet delivery ratio (PDR), or latency in the network. Higher BER values will normally outcome in more dropped packets, decreasing the overall reliability of the network.

In conclusion, we shown the various paths to calculate the Network Error Rate using NS2 tool that containing the procedure on how to compute the packet error rate (PER) and how to estimate the bit error rate (BER). More informations will be provided according to your needs.

If you have any inquiries regarding the Network Error Rate in the NS2 tool project, don’t hesitate to reach out to us. We are fully equipped with the necessary resources to assist you.