How to Implement Network Traffic Congestion in NS2
To implement network traffic congestion in NS2, we need to mimic a scenario in which network traffic exceeds the available bandwidth that leads latency, packet loss, or performance degradation and it includes generating a high-traffic environment with multiple sources transmitting data concurrently to limited resources like bandwidth or buffers, prominent to congestion in the network.
Here is a step-by-step guide on how to implement network traffic congestion in NS2:
Step-by-Step Implementation:
- Set up the Simulation Environment
Initially configure the simple network environment that has generated the network topology and describing the simulation parameters. The topology should contain nodes, links, and appropriate traffic sources that can produce a high volume of traffic.
Example TCL Script for Basic Setup:
# Create a simulator instance
set ns [new Simulator]
# Define network parameters (e.g., a wired or wireless network)
set val(chan) Channel/WirelessChannel ;# Wireless channel type
set val(prop) Propagation/TwoRayGround ;# Propagation model
set val(netif) Phy/WirelessPhy ;# Network interface type
set val(mac) Mac/802_11 ;# MAC protocol
set val(ifq) Queue/DropTail/PriQueue ;# Interface queue type
set val(ll) LL ;# Link layer type
set val(ant) Antenna/OmniAntenna ;# Antenna model
set val(bw) 1Mb ;# Bandwidth for the link
set val(delay) 10ms ;# Link delay
# Create the topography (number of nodes)
create-god 10
This configures a simple network environment in which we will establish congestion by creating traffic that exceeds the available bandwidth.
- Define the Network Topology
In a congested network scenario, we should describe a topology in which multiple nodes are transmitting data to a usual destination, creating a bottleneck. Characteristically, this bottleneck occurs when multiple flows attempt to distribute the same link with limited bandwidth.
Example of a Simple Star Topology:
# Create nodes (e.g., node 0 is the destination, and others are sources)
for {set i 0} {$i < 6} {incr i} {
set node_($i) [$ns node]
}
# Create links (bottleneck link with limited bandwidth and high delay)
$ns duplex-link $node_(0) $node_(1) $val(bw) $val(delay) DropTail
$ns duplex-link $node_(0) $node_(2) $val(bw) $val(delay) DropTail
$ns duplex-link $node_(0) $node_(3) $val(bw) $val(delay) DropTail
$ns duplex-link $node_(0) $node_(4) $val(bw) $val(delay) DropTail
$ns duplex-link $node_(0) $node_(5) $val(bw) $val(delay) DropTail
In this topology, node 0 behaves as a common destination, although nodes 1-5 are traffic sources. All the links have limited bandwidth (1 Mbps), and multiple sources will be creating the traffic towards node 0, creating congestion.
- Generate Traffic to Create Congestion
Next, we need to create traffic from multiple sources simultaneously to overload the network and generate congestion. Use UDP or TCP agents alongside with CBR (Constant Bit Rate) traffic to replicate constant data flow.
Example of Generating Traffic Using UDP and CBR:
# Create UDP agents and attach them to source nodes (nodes 1-5)
for {set i 1} {$i <= 5} {incr i} {
set udp($i) [new Agent/UDP]
$ns attach-agent $node_($i) $udp($i)
}
# Create CBR traffic to simulate heavy data transmission
for {set i 1} {$i <= 5} {incr i} {
set cbr($i) [new Application/Traffic/CBR]
$cbr($i) set packet_size_ 512 ;# Packet size in bytes
$cbr($i) set rate_ 2Mb ;# 2 Mbps rate (higher than link bandwidth to create congestion)
$cbr($i) attach-agent $udp($i)
}
# Create UDP sink (null agent) at the destination (node 0)
set null [new Agent/Null]
$ns attach-agent $node_(0) $null
# Connect each source to the destination
for {set i 1} {$i <= 5} {incr i} {
$ns connect $udp($i) $null
}
# Start traffic at different times to simulate congestion build-up
$ns at 1.0 “$cbr(1) start”
$ns at 2.0 “$cbr(2) start”
$ns at 3.0 “$cbr(3) start”
$ns at 4.0 “$cbr(4) start”
$ns at 5.0 “$cbr(5) start”
In this configuration, each node (from node 1 to node 5) creates traffic at a rate of 2 Mbps that exceeds the link bandwidth of 1 Mbps, leads to congestion. The traffic initiate at different times to replicate on how congestion builds up gradually as more sources start transmitting data.
- Introduce Congestion Control (Optional)
We can optionally establish TCP instead of UDP to mimic the impacts of congestion control mechanisms such as AIMD (Additive Increase Multiplicative Decrease) that is built into TCP. TCP agents will systematically adapt the sending rate in response to congestion (packet loss or delay).
Example of Using TCP for Congestion Control:
# Create TCP agents instead of UDP
for {set i 1} {$i <= 5} {incr i} {
set tcp($i) [new Agent/TCP]
$ns attach-agent $node_($i) $tcp($i)
}
# Create FTP traffic to generate continuous data
for {set i 1} {$i <= 5} {incr i} {
set ftp($i) [new Application/FTP]
$ftp($i) attach-agent $tcp($i)
}
# Create TCP sink at the destination
set tcpSink [new Agent/TCPSink]
$ns attach-agent $node_(0) $tcpSink
# Connect each source to the destination using TCP
for {set i 1} {$i <= 5} {incr i} {
$ns connect $tcp($i) $tcpSink
}
# Start traffic
$ns at 1.0 “$ftp(1) start”
$ns at 2.0 “$ftp(2) start”
$ns at 3.0 “$ftp(3) start”
$ns at 4.0 “$ftp(4) start”
$ns at 5.0 “$ftp(5) start”
Using TCP, congestion control mechanisms will diminish the sending rate when congestion is detected, helping to prevent the impacts of congestion. We can compare the outcomes of congestion with and without congestion control.
- Monitor and Trace the Simulation
Allow trace files in NS2 to observe the network’s behaviour during congestion. The trace file will support you to evaluate packet loss, delay, throughput, and queue sizes during periods of congestion.
Enable Trace Files for Congestion Analysis:
# Enable tracing for the simulation
set tracefile [open “congestion_trace.tr” w]
$ns trace-all $tracefile
# Define a finish procedure to end the simulation and close the trace file
proc finish {} {
global ns tracefile
$ns flush-trace
close $tracefile
exit 0
}
# Set simulation end time
$ns at 20.0 “finish”
The trace file will contain data about every event in the network, like packet transmissions, receptions, and drops due to congestion.
- Run the Simulation
Finally, execute the simulation to monitor on how traffic congestion impacts the network. Once the simulation is done, we can measure the trace file for performance metrics such as packet loss, delay, throughput, and queue utilization.
# Run the simulation
$ns run
- Post-Simulation Analysis
After the simulation, we can evaluate the trace file to monitor the impacts of congestion on the network. we can extract significant parameters such as:
- Packet loss: The number of packets dropped due to congestion (queue overflow).
- End-to-end delay: The delay experienced by packets in reaching their destination.
- Throughput: The rate at which data is successfully transmitted across the network.
- Queue length: The size of the queue at congested links that designates the severity of congestion.
We can use NS2’s trace analysis tools such as AWK scripts or post-process the trace file using external tools like Python or Excel to create graphs and evaluate congestion behaviour.
Example Complete TCL Script for Traffic Congestion Simulation
# Create a simulator instance
set ns [new Simulator]
# Define network parameters
set val(chan) Channel/WirelessChannel
set val(prop) Propagation/TwoRayGround
set val(netif) Phy/WirelessPhy
set val(mac) Mac/802_11
set val(ifq) Queue/DropTail/PriQueue
set val(ll) LL
set val(ant) Antenna/OmniAntenna
set val(bw) 1Mb
set val(delay) 10ms
# Create nodes
for {set i 0} {$i < 6} {incr i} {
set node_($i) [$ns node]
}
# Create links with limited bandwidth
for {set i 1} {$i < 6} {incr i} {
$ns duplex-link $node_($i) $node_(0) $val(bw) $val(delay) DropTail
}
# Create UDP agents and attach them to source nodes
for {set i 1} {$i <= 5} {incr i} {
set udp($i) [new Agent/UDP]
$ns attach-agent $node_($i) $udp($i)
}
# Create CBR traffic for each source
for {set i 1} {$i <= 5} {incr i} {
set cbr($i) [new Application/Traffic/CBR]
$cbr($i) set packet_size_ 512
$cbr($i) set rate_ 2Mb
$cbr($i) attach-agent $udp($i)
}
# Create a null agent (sink) at the destination
set null [new Agent/Null]
$ns attach-agent $node_(0) $null
# Connect the sources to the destination
for {set i 1} {$i <= 5} {incr i} {
$ns connect $udp($i) $null
}
# Start traffic at different times
for {set i 1} {$i <= 5} {incr i} {
$ns at [expr $i + 1] “$cbr($i) start”
}
# Enable tracing
set tracefile [open “congestion_trace.tr” w]
$ns trace-all $tracefile
# End simulation
$ns at 20.0 “finish”
proc finish {} {
global ns tracefile
$ns flush-trace
close $tracefile
exit 0
}
# Run the simulation
$ns run
In the end of the manual, we had utterly deliver the comprehensive details about how to setup and how to execute the network traffic congestion in ns2 simulator. More information will be shared about how the network traffic congestion will perform in other simulation tool. Our team of specialists is prepared to assist you in generating the most suitable thesis ideas and topics that align with your interests. For optimal implementation outcomes, you may contact the ns2project.com team.