How to Calculate Network Congestion in NS2

To calculate the Network Congestion in Network Simulator 2 (NS2), it represents a scenario where the requirement for network resources (like bandwidth or buffer space) surpasses the existed capacity, causing packet loss, delays and decreased throughput. It typically happens at routers or network nodes as their buffers become full, leading to packet drops. Follow the below guide to estimate the network congestion in the ns2 tool:

Key Metrics to Measure Network Congestion:

To calculate network congestion in NS2, you can evaluate multiple key metrics:

  1. Packet Drop Rate: The percentage of packets dropped because of buffer overflows or excessive queuing delays.
  2. Queue Length: The average or extreme queue size at network nodes or routers.
  3. End-to-End Delay: The time taken for packets to move from the source to the destination, which maximizes during congestion.
  4. Throughput: The rate at which data is efficiently sends over the network. A drop in throughput can represent congestion.
  5. Packet Loss: The amount of packets lost because of congestion (buffer overflow or other causes).

Steps to Calculate Network Congestion in NS2:

  1. Set Up the Simulation: Develop a network topology where congestion may happen. This can contain several nodes, routers, or links with restricted bandwidth. You can mimic traffic using TCP or UDP protocols, and overloading the network can leads to congestion.
  2. Configure Queues: The queuing mechanism (like DropTail or RED) at routers or intermediate nodes will impact how congestion is managed. It commonly occurs when the queue becomes full, and packets are dropped.
  3. Generate and Analyze the Trace File: During the simulation, NS2 produces a trace file that logs all packet transmissions, receptions, and drops. By evaluating the trace file, you can estimate packet drops, queue lengths, and delays to assess network congestion.
  4. Calculate Congestion Metrics:
    • Packet Drop Rate: Total the amount of dropped packets at network nodes.
    • Queue Length: Track the extreme or average queue length at routers.
    • End-to-End Delay: Compute the time it takes for packets to reach their destination.
    • Throughput: Estimate the data successfully delivered to the destination.

Example Tcl Script for NS2 Simulation:

Here is a sample of NS2 script that replicates a simple network where congestion might occur.

# Create a new simulator instance

set ns [new Simulator]

# Define the network topology

set node0 [$ns node]

set node1 [$ns node]

set node2 [$ns node]

# Set up links between nodes (with limited bandwidth)

$ns duplex-link $node0 $node1 1Mb 10ms DropTail

$ns duplex-link $node1 $node2 500Kb 20ms DropTail

# Attach UDP agents for traffic

set udp0 [new Agent/UDP]

set null0 [new Agent/Null]

$ns attach-agent $node0 $udp0

$ns attach-agent $node2 $null0

$ns connect $udp0 $null0

# Create CBR traffic source to generate packets

set cbr0 [new Application/Traffic/CBR]

$cbr0 attach-agent $udp0

$cbr0 set packetSize_ 512

$cbr0 set rate_ 1Mb   ;# Set a high rate to induce congestion

# Start traffic at 1.0s and stop at 5.0s

$ns at 1.0 “$cbr0 start”

$ns at 5.0 “$cbr0 stop”

# Open trace file to log events

set tracefile [open trace.tr w]

$ns trace-all $tracefile

# Define the finish procedure to close trace file and stop the simulation

proc finish {} {

global ns tracefile

$ns flush-trace

close $tracefile

exit 0

}

# Schedule the end of the simulation

$ns at 6.0 “finish”

# Run the simulation

$ns run

In this example:

  • Nodes node0, node1, and node2 form a basic topology.
  • Node0 delivers UDP traffic (using CBR) to Node2 through Node1.
  • The link amongst Node1 and Node2 has limited bandwidth (500 Kbps), which may leads to congestion if the traffic rate surpasses the link’s capacity.
  1. Trace File Analysis for Congestion:

The trace file has the logs of packet transmissions, receptions, and drops. Congestion can be identified by inspecting:

  • Dropped Packets: Look for dropped packets because of buffer overflow.
  • Queue Lengths: Track the queue size at intermediate nodes to see if it overdoes the buffer limit.
  • Delays: Calculate the packet delay amongst the source and destination, as maximized delays are a symptom of congestion.

Example Trace File Output:

s 1.0 _0_ AGT — 512 [0 0 0 0] ——- [1:0 2:0 32 0]

d 1.1 _1_ AGT — 512 [0 0 0 0] ——- [1:0 2:0 32 0]

r 1.2 _2_ AGT — 512 [0 0 0 0] ——- [1:0 2:0 32 0]

  • s: Packet sent.
  • r: Packet received.
  • d: Packet dropped (due to congestion).
  1. Calculate Packet Drop Rate:

The packet drop rate is a crucial metric to estimate congestion. It can be measured as the ratio of dropped packets to the total number of packets sent.

Bash Script to Calculate Packet Drop Rate:

# Count the total number of packets sent

sent_packets=$(grep “^s” trace.tr | wc -l)

# Count the total number of packets dropped

dropped_packets=$(grep “^d” trace.tr | wc -l)

# Calculate the packet drop rate

if [ $sent_packets -gt 0 ]; then

drop_rate=$(echo “scale=2; $dropped_packets / $sent_packets * 100” | bc)

echo “Packet Drop Rate: $drop_rate%”

else

echo “No packets sent.”

fi

  1. Measure Queue Length:

Queue length can be observed to state how congested a node is. In NS2, queue length can be tracked by allowing queue monitoring.

Enable Queue Monitoring in NS2:

Observe the queue length in NS2 by using the QueueMonitor function and include it to the links:

# Attach a queue monitor to the link between node1 and node2

set qmon [$ns monitor-queue $node1 $node2 0.1]

You can then access the average or maximum queue length during the simulation.

  1. Calculate End-to-End Delay:

The end-to-end delay is the time it takes for a packet to move from the source to the destination. You can compute it by quantifying the difference amongst the time a packet is sent and when it is obtained.

Bash Script to Calculate End-to-End Delay:

# Extract send and receive events from the trace file

grep “^s” trace.tr > sent_packets.txt

grep “^r” trace.tr > received_packets.txt

# Calculate the end-to-end delay for each packet

awk ‘FNR==NR{sent[$6]=$2; next} {if ($6 in sent) print $2 – sent[$6]}’ sent_packets.txt received_packets.txt > delays.txt

# Calculate average delay

awk ‘{sum+=$1; count+=1} END {if (count > 0) print “Average End-to-End Delay: “, sum/count, ” seconds”; else print “No packets received.”}’ delays.txt

  1. Calculate Throughput:

Throughput is the rate at which data is efficiently delivered across the network. It can be calculated as the total data acquired divided by the simulation time.

Bash Script to Calculate Throughput:

# Count the total number of packets received

received_packets=$(grep “^r” trace.tr | wc -l)

# Define packet size in bits (512 bytes = 4096 bits)

packet_size=4096

# Calculate total data received in bits

total_data_received=$(echo “$received_packets * $packet_size” | bc)

# Define simulation time (example: 5 seconds)

simulation_time=5

# Calculate throughput in bits per second (bps)

throughput=$(echo “scale=2; $total_data_received / $simulation_time” | bc)

echo “Throughput: $throughput bps”

Summary of Steps:

  1. Run the simulation in NS2 with traffic load and restricted bandwidth to imitate congestion.
  2. Analyze the trace file to sum packet drops, calculate queue lengths, and compute delays.
  3. Calculate key congestion metrics:
    • Packet Drop Rate: Ratio of dropped packets to total packets sent.
    • Queue Length: Maximum or average queue size at blocked nodes.
    • End-to-End Delay: Time taken for packets to reach the destination.
    • Throughput: Sum up of data successfully delivered.

Through the above manual, we have successfully delivered the computational process of the required metrics like packet drop rate, queue length, end-to-end delay, throughput and packet loss to calculate the network congestion in the simulation environment called ns2.

To Calculate Network Congestion in NS2 tool you can approach ns2project.com where we provide you with best research ideas and topics.