How to Implement Spanning Tree Protocol in NS2

 

To implement Spanning Tree Protocol (STP) in Network Simulator 2 (NS2) has needed to mimic its behaviour manually by the way NS2 does not have a built-in support for execution of STP. STP is used in Layer 2 switches to avoid the loops in a network topology by generating a loop-free tree structure. Here’s we can approach this implementation in NS2:

Overview of Spanning Tree Protocol (STP)

  • Spanning Tree Protocol (STP) avoids the network loops by blocking the particular links in a switched network to form a loop-free tree structure.
  • Bridge Protocol Data Units (BPDUs) are interchanged among switches to build the spanning tree.
  • STP elects a root bridge; choose the root ports, and blocks non-vital links to prevent loops.

Steps to Implement STP in NS2

  1. Understand the Components of STP:
    • Root Bridge Election: The switch with the lowest bridge ID becomes the root bridge.
    • Root Ports: Each switch chooses a single root port (the one with the least cost path to the root bridge).
    • Blocking Ports: Any redundant links not used in the shortest path to the root are blocked to stop loops.
  2. Simulating STP in NS2:
    • Since NS2 primarily performs at Layer 3, mimic STP that contains manually blocking or unblocking links according to the protocol’s behaviour.
  3. Manual STP Simulation Logic:
    • Execute a custom logic in TCL script that implement STP by intermittently interchange the messages among switches and blocking redundant links.
  4. Example TCL Script to Simulate STP Behavior: Here’s an sample of how we can physically mimic the behavior of STP in NS2 by blocking and unblocking links to form a spanning tree:

# Create a new NS2 simulator instance

set ns [new Simulator]

# Create a trace file for output

set tracefile [open “stp_trace.tr” w]

$ns trace-all $tracefile

# Create a NAM file for visualization

set namfile [open “stp.nam” w]

$ns namtrace-all $namfile

# Define the nodes (representing switches)

set s0 [$ns node]

set s1 [$ns node]

set s2 [$ns node]

set s3 [$ns node]

set s4 [$ns node]

# Create links between the nodes to form a mesh (loop-prone) network

$ns duplex-link $s0 $s1 10Mb 10ms DropTail

$ns duplex-link $s1 $s2 10Mb 10ms DropTail

$ns duplex-link $s2 $s3 10Mb 10ms DropTail

$ns duplex-link $s3 $s4 10Mb 10ms DropTail

$ns duplex-link $s4 $s0 10Mb 10ms DropTail

$ns duplex-link $s1 $s3 10Mb 10ms DropTail

# Define a procedure to simulate the root bridge election and link blocking

proc simulate_spanning_tree {} {

global ns s0 s1 s2 s3 s4

# In this simple simulation, assume s0 is the root bridge

puts “Node s0 elected as the Root Bridge”

# Block redundant links to avoid loops based on STP rules

# Block link between s1 and s3 to prevent a loop

$ns rtmodel-at 1.0 down $s1 $s3

puts “Link between s1 and s3 is blocked to prevent a loop”

# Other links are left active, forming a spanning tree topology

# Only the shortest paths to the root bridge s0 are left active

}

# Schedule the spanning tree simulation at 0.5 seconds

$ns at 0.5 “simulate_spanning_tree”

# Define the end of the simulation

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam stp.nam &

exit 0

}

# Schedule simulation end

$ns at 5.0 “finish”

# Run the simulation

$ns run

Key Elements of the Script:

  1. Mesh Network Topology:
    • The script generates a mesh network among 5 nodes that are treated as switches in the simulation.
  2. Root Bridge Election:
    • The script manually allocates node s0 as the root bridge (this is simplified in this simulation; real STP would dynamically elect the root bridge).
  3. Blocking Redundant Links:
    • The script blocks the link among s1 and s3 to avoid a loop, mimic STP’s behaviour of preventing redundant paths.
  4. Spanning Tree Simulation:
    • The simulate_spanning_tree procedure implements the STP behaviour by blocking specific links to avoid loops and form a loop-free spanning tree.
  5. End of Simulation:
    • The simulation stops at 5 seconds, and the outcome is saved in the trace and NAM files.

Running the Simulation:

  1. Save the TCL Script: Save the TCL script as stp_simulation.tcl.
  2. Run the Simulation: Execute the TCL script using the following command:

ns stp_simulation.tcl

  1. Visualize the Output: After the simulation executes open the NAM file to visualize the network:

nam stp.nam

In NAM, we should be able to see the network topology and the blocked link that demonstrated by the absence of traffic flow via that link.

  1. Enhancing the Simulation:
  • Dynamic Root Bridge Election: Execute logic for dynamically electing the root bridge according to node IDs or bridge priorities.
  • BPDU Exchange: Execute a more complex simulation in which the nodes exchange Bridge Protocol Data Units (BPDU) to regulate that links to block.
  • Port Costs and Path Selection: Add logic for computing port costs and choosing the best path to the root bridge according to these costs.
  1. Advanced Implementation (C++ Modifications):

For more advanced STP implementation, we need to adapt the C++ source code in NS2 to manage the BPDU exchange and dynamic link blocking/unblocking.

  • Bridge ID and BPDU Handling: We can generate a new protocol class or expand an existing one such as the Link Layer to establish BPDU exchange and root bridge election logic.
  • Link Blocking Logic: Add functions to manage the blocking and unblocking of ports according to the BPDU messages received.

Example:

  1. Generate a new protocol class in NS2 like stp.cc and stp.h.
  2. Execute the methods for:
    • BPDU exchange.
    • Root bridge election.
    • Link blocking based on path costs.

Once changes are made, recompile NS2:

cd ns-allinone-2.35/ns-2.35/

./configure

make clean

make

In the end, we had explored the basic implementation process on how to execute the Spanning Tree Protocol in the network using ns2 tool. If you need additional information regarding the Spanning Tree Protocol we will provide it too.

ns2project.com team is here to inspire you with creative project ideas and unique topics to delve into. We also provide tailored assistance for successfully implementing the Spanning Tree Protocol in NS2.