How to Start Spanning Tree Protocol Projects Using NS2
To start a Spanning Tree Protocol (STP) project in Network Simulator 2 (NS2), we will replicate the STP behavior or execute the custom logic while NS2 doesn’t directly STP support. STP is normally utilised within Layer 2 Ethernet networks for avoiding loops and making sure that single dynamic path among the network nodes.
Below is a general approach to get started:
Steps to Start Spanning Tree Protocol Projects in NS2
Step 1: Set Up NS2 Environment
- Install NS2:
- We should download and install the new version of NS2 on the system.
- Confirm installation by using below code:
ns -version
- Understand NS2 Basics:
- Study on how to specify the nodes, links, and traffic to utilise Tcl scripts in NS2.
- Search examples through ns-allinone-2.x/examples/.
Step 2: Understand Spanning Tree Protocol
- What is STP?
- A Layer 2 protocol, making a loop-free spanning tree topology which avoids loops within Ethernet networks.
- It inactivates the redundant links whereas maintaining one dynamic path to all network segments.
- Core Components:
- Root Bridge: The central node of the spanning tree which is lowest bridge ID.
- Designated Bridge: This node responds for sending packets to a segment.
- Non-Designated Ports: Ports which is designed for avoiding loops.
- Key Steps in STP:
- Root Election: Choose the root bridge according to the lowest bridge ID.
- Path Selection: It finds the shortest route to the root bridge.
- Port States: Then, allocate the ports like root, which is chosen, or obstructed.
Step 3: Design Network Topology
- Define Nodes and Links:
- Make a network topology including potential loops.
set ns [new Simulator]
# Create nodes
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
# Define links
$ns duplex-link $n1 $n2 10Mb 10ms DropTail
$ns duplex-link $n2 $n3 10Mb 10ms DropTail
$ns duplex-link $n3 $n4 10Mb 10ms DropTail
$ns duplex-link $n4 $n1 10Mb 10ms DropTail
$ns duplex-link $n1 $n3 10Mb 10ms DropTail
- Simulate Loops:
- The network must contain several paths among the nodes for analysing the loop-prevention method of STP.
Step 4: Implement Spanning Tree Logic
Option 1: Simulate STP in Tcl
- Root Bridge Election:
- We need to replicate the root bridge selection by way of allocating the bridge IDs and designating the lowest.
set bridges {n1 n2 n3 n4}
set rootBridge {}
set minID inf
foreach bridge $bridges {
set bridgeID [expr rand() * 1000] ;# Assign random bridge IDs
if {$bridgeID < $minID} {
set minID $bridgeID
set rootBridge $bridge
}
}
puts “Root Bridge: $rootBridge with ID $minID”
- Port State Assignment:
- Depends on the shortest route to the root, discover the root, designated, and blocked ports.
proc assignPortStates {bridges root} {
foreach bridge $bridges {
if {$bridge != $root} {
# Determine port roles based on shortest path
puts “Bridge $bridge: Assign port roles”
}
}
}
assignPortStates $bridges $rootBridge
Option 2: Custom STP Implementation in C++
- Create a Spanning Tree Protocol Agent:
- In NS2, prolong the Agent class for executing STP logic.
- Sustain a bridge ID, port states, and a topology database.
Example:
class STPAgent : public Agent {
public:
STPAgent();
void recv(Packet* p, Handler* h);
void calculateSpanningTree();
private:
int bridgeID;
std::map<int, std::string> portStates; // Port ID -> State (Root, Designated, Blocked)
};
- Implement Core Functions:
- Root Election: Transmit bridge IDs and find the root.
- Port State Assignment: Allocate the roles to ports rely on route cost.
- Compile and Link:
- Change the NS2 Makefile with the custom STP agent.
- Recompile NS2 to utilise below command line:
make clean && make
Step 5: Add Traffic Sources
- Define Traffic Flows:
- Connect agents to nodes and make a traffic flows.
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set sink [new Agent/Null]
$ns attach-agent $n4 $sink
$ns connect $udp $sink
# Add CBR traffic
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set packetSize_ 512
$cbr set interval_ 0.05
$ns at 1.0 “$cbr start”
- Monitor Traffic Flow:
- Monitor how STP removes loops and makes sure a single dynamic path.
Step 6: Simulate and Analyze
- Run the Simulation:
- We need to store the script like stp_simulation.tcl and then run the simulation:
ns stp_simulation.tcl
- Visualize in NAM:
- In NAM, monitor the spanning tree topology and traffic flow:
nam stp_simulation.nam
- Analyze Trace File:
- Crucial performance parameters to estimate:
- Duration to meet to a spanning tree.
- Packet delivery ratio.
- Redundant link deactivation.
- Crucial performance parameters to estimate:
Step 7: Enhance the Project
- Dynamic Topology Changes:
- To replicate the link failures or additions depends on dynamic topology modifications and monitor how to STP meeting again.
$ns at 5.0 “$ns duplex-link-delete $n1 $n3”
- Performance Comparison:
- We have to equate the STP including other loop-prevention approaches or spanning tree differences such as RSTP.
- Scalability Testing:
- Experiment the scalability of STP as volume of nodes and links maximizes.
Example Script Outline
Below is an example NS2 Tcl script for an STP simulation:
set ns [new Simulator]
set udp [new Agent/UDP]
set sink [new Agent/Null]
# Node and traffic configuration
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
# Links
$ns duplex-link $n1 $n2 10Mb 10ms DropTail
$ns duplex-link $n2 $n3 10Mb 10ms DropTail
$ns duplex-link $n3 $n4 10Mb 10ms DropTail
$ns duplex-link $n4 $n1 10Mb 10ms DropTail
# Traffic
$ns attach-agent $n1 $udp
$ns attach-agent $n4 $sink
$ns connect $udp $sink
$ns at 1.0 “$udp send”
$ns run
As illustrated above, you will execute these steps with sample snippets that support you for simulating and analysing the Spanning Tree Protocol projects using OMNeT++ environment. Should there be any more questions, we can address in upcoming guide.