To simulate Spanning Tree Protocol (STP) projects in NS2, we want to manually set up a network that prevents the loops, same as to how STP operates by building a loop-free topology from a specified network. While NS2 doesn’t have built-in support for STP, we can physically model the behaviour of STP by preventing redundant paths and making sure only one path remains active among any two nodes.
The main aim of STP is to remove the loops in Ethernet networks since make sure redundancy that permits for automatic reconfiguration in case of link failures. In NS2, we can replicate this by set up a mesh network and executing a spanning tree-like behaviour using static routing.
Here’s a guide to simulate the basic behaviour of STP using NS2.
Steps to Simulate Spanning Tree Protocol in NS2
- Set up NS2 Environment
Ensure NS2 is installed and working. We need to validate this by typing:
ns
This will begins the NS2 shell if NS2 is properly installed.
- Understanding Spanning Tree Protocol
STP performs by disabling redundant links in a network to form a spanning tree in which there is exactly one path among any pair of nodes. It classifies a “root bridge” and estimates the shortest paths from the root to all other nodes while disabling links that can cause loops.
- Write a TCL Script to Simulate a Network with STP-like Behavior
Here’s a sample script that configures a mesh network and replicate the simple behaviour of STP by physically disabling redundant paths.
# Create a simulator object
set ns [new Simulator]
# Open trace and NAM output files
set tracefile [open out.tr w]
set namfile [open out.nam w]
$ns trace-all $tracefile
$ns namtrace-all $namfile
# Define the network topology (mesh network)
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
# Create duplex links between the nodes (full mesh)
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n0 $n3 1Mb 10ms DropTail
$ns duplex-link $n0 $n4 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n1 $n3 1Mb 10ms DropTail
$ns duplex-link $n1 $n4 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
$ns duplex-link $n2 $n4 1Mb 10ms DropTail
$ns duplex-link $n3 $n4 1Mb 10ms DropTail
# Spanning Tree Protocol (STP) simulation by disabling loops
# Manually disable links that would cause loops
$ns rtmodel-at 0.5 down $n0 $n4 ;# Disable link n0 -> n4
$ns rtmodel-at 0.5 down $n1 $n3 ;# Disable link n1 -> n3
$ns rtmodel-at 0.5 down $n2 $n4 ;# Disable link n2 -> n4
# After STP-like behavior, traffic will only use the remaining tree structure
# Create a UDP agent and attach it to node n0 (source)
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# Create a Null agent and attach it to node n3 (destination)
set null0 [new Agent/Null]
$ns attach-agent $n3 $null0
# Connect the source (n0) to the destination (n3)
$ns connect $udp0 $null0
# Create a CBR traffic generator and attach it to the UDP agent
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 512
$cbr0 set interval_ 0.01
$cbr0 attach-agent $udp0
# Schedule the traffic to start and stop
$ns at 1.0 “$cbr0 start”
$ns at 4.5 “$cbr0 stop”
$ns at 5.0 “finish”
# Define the finish procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam out.nam &
exit 0
}
# Run the simulation
$ns run
- Explanation of the Script
- Mesh Topology: The network initiate as a fully connected mesh in which all nodes are associated to each other.
- Spanning Tree Simulation: Links that would origin loops are physically disabled using the rtmodel-at down command. In this case, three links (n0 to n4, n1 to n3, and n2 to n4) are disabled to replicate the behaviour of a spanning tree, in which redundant paths are trimmed.
- Traffic: A Constant Bit Rate (CBR) traffic source is configures at node n0 to transmit traffic to node n3.
- Running the Simulation
Save the script as stp_simulation.tcl and execute it in the terminal:
ns stp_simulation.tcl
This will create a trace file (out.tr) and a NAM file (out.nam) for network visualization.
- Visualizing the Simulation
To envision the network using NAM, run:
nam out.nam
We will monitor the initial full mesh network, however after the simulated Spanning Tree Protocol disables the redundant links, traffic will flow via the reduced, loop-free topology.
- Modifying the Traffic and Network
We can adapt the network or traffic in numerous ways:
- Additional Traffic Flows: Incorporate more traffic flows among different nodes to replicate network load.
- Dynamic Topology Changes: Mimic link failures and track on how the network reconfigures itself by ingress or egress links.
For instance, incorporate a second traffic flow from n2 to n4:
# Second UDP agent for additional traffic
set udp1 [new Agent/UDP]
$ns attach-agent $n2 $udp1
# Null agent at node n4
set null1 [new Agent/Null]
$ns attach-agent $n4 $null1
# Connect the second traffic flow
$ns connect $udp1 $null1
# Create a second CBR traffic generator
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 512
$cbr1 set interval_ 0.02
$cbr1 attach-agent $udp1
$ns at 1.5 “$cbr1 start”
$ns at 4.5 “$cbr1 stop”
- Analysing the Trace File
The trace file (out.tr) encompasses detailed information about packet transmissions and routing decisions. We need to evaluate the file to extract key parameters like:
- Packet Delivery Ratio (PDR): The percentage of successfully delivered packets.
- End-to-End Delay: Estimate the time taken for packets to travel from source to destination.
- Congestion: Evaluate how congestion impacts traffic flow and delivery.
For instance, to count the number of packets received at node n3:
grep “^r” out.tr | wc -l
- Simulating Link Failures and Recovery
We can replicate link failures and recoveries to see how STP might reconfigure the network. For instance, to replicate the failure of a link among n0 and n1:
# Simulate link failure between n0 and n1 at 2.0 seconds
$ns rtmodel-at 2.0 down $n0 $n1
To restore the link at a later time:
# Restore the link between n0 and n1 at 4.0 seconds
$ns rtmodel-at 4.0 up $n0 $n1
In this process, we had detailed covered the information about Spanning Tree Protocol simulation procedures and evaluation process of Spanning Tree Protocol outcomes across the ns2 tool. Further details regarding the Spanning Tree Protocol will be provided in upcoming manuals.
To simulate Spanning Tree Protocol (STP) projects in NS2 tool we at phdprime.com holds a group of experts in this area. Share with us all your project details we will guide you at every step. Get best project ideas in this area from phdprime.com experts.