How to Develop Spanning Tree Protocol Projects Using NS3

To develop the Spanning Tree Protocol (STP) has contains the NS3. The Spanning Tree Protocol (STP) is used for Layer 2 networks to avoid the loops in Ethernet networks through blocking redundant connections while handle the network connectivity. STP is generally used for enterprise networks, data centers, and Software-Defined Networking (SDN).

Steps to Develop Spanning Tree Protocol Projects Using NS3

  1. Install NS3

Assure the NS3 tool is installed before proceeding:

sudo apt update

sudo apt install git g++ python3 cmake make

git clone

cd ns-3-dev

./ns3 configure –enable-examples –enable-tests

./ns3 build

  1. Define Network Topology for STP

We execute the STP; we generate a layer-2 Ethernet network in which the switches are altered the Bridge Protocol Data Units (BPDU) to form a spanning tree.

Example: Implementing STP in NS3

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/bridge-module.h”

#include “ns3/point-to-point-module.h”

#include “ns3/csma-module.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE(“STPRoutingExample”);

int main(int argc, char *argv[])

{

uint32_t numSwitches = 3;

uint32_t numHosts = 6;

double simulationTime = 10.0;

NodeContainer switches, hosts;

switches.Create(numSwitches);

hosts.Create(numHosts);

// Create CSMA Links between Switches

CsmaHelper csma;

csma.SetChannelAttribute(“DataRate”, StringValue(“100Mbps”));

csma.SetChannelAttribute(“Delay”, TimeValue(MilliSeconds(1)));

NetDeviceContainer switchDevices[numSwitches];

 

for (uint32_t i = 0; i < numSwitches – 1; i++)

{

switchDevices[i] = csma.Install(NodeContainer(switches.Get(i), switches.Get(i + 1)));

}

// Create CSMA Links between Switches and Hosts

NetDeviceContainer hostDevices[numHosts];

for (uint32_t i = 0; i < numHosts; i++)

{

hostDevices[i] = csma.Install(NodeContainer(hosts.Get(i), switches.Get(i % numSwitches)));

}

// Install BridgeNetDevice on Switches

BridgeHelper bridge;

NetDeviceContainer bridgeDevices[numSwitches];

for (uint32_t i = 0; i < numSwitches; i++)

{

bridgeDevices[i] = bridge.Install(switches.Get(i), switchDevices[i]);

}

// Enable STP on switches

for (uint32_t i = 0; i < numSwitches; i++)

{

Ptr<BridgeNetDevice> bridgeNetDevice = bridgeDevices[i].Get(0)->GetObject<BridgeNetDevice>();

bridgeNetDevice->EnableStp(true);

}

// Assign IP Addresses to Hosts

InternetStackHelper internet;

internet.Install(hosts);

Ipv4AddressHelper address;

address.SetBase(“10.1.1.0”, “255.255.255.0”);

for (uint32_t i = 0; i < numHosts; i++)

{

address.Assign(hostDevices[i]);

}

// Run simulation

Simulator::Stop(Seconds(simulationTime));

Simulator::Run();

Simulator::Destroy();

return 0;

}

  1. Explanation of the Code
  • Network Setup
    • 3 switches linked in loop.
    • 6 hosts linked to switches for using CSMA connections.
  • STP Implementation
    • Bridge devices are downloading the switches.
    • STP is allowed for using:

Ptr<BridgeNetDevice> bridgeNetDevice = bridgeDevices[i].Get(0)->GetObject<BridgeNetDevice>();

bridgeNetDevice->EnableStp(true);

    • Selects the STP root switch and blocks the redundant connection to prevent the loops.
  • Traffic Generation
    • Hosts interconnect over Layer 2 network by permitted the STP.
  1. Run the Simulation

Compile and execute:

./ns3 run stp-routing

  1. Performance Analysis

We investigate the network stability and connection redundancy, use FlowMonitor.

Enable FlowMonitor for STP Analysis

#include “ns3/flow-monitor-module.h”

FlowMonitorHelper flowmon;

Ptr<FlowMonitor> monitor = flowmon.InstallAll();

Simulator::Stop(Seconds(10.0));

Simulator::Run();

monitor->SerializeToXmlFile(“stp_results.xml”, true, true);

Simulator::Destroy();

  1. Extend with RSTP (Rapid Spanning Tree Protocol)

Intended for faster convergence, change the STP set-ups to RSTP.

Enable RSTP in NS3

bridgeNetDevice->EnableStp(true, “RSTP”); // Enable RSTP

  1. Advantages of STP Routing

Loop Prevention: Assure the no Layer 2 loops in Ethernet networks.

Redundancy Handling: We Blocks the redundant connections while keeping an active path.

Improved Network Stability: Decreases the program storms in large networks.

In the end, we had explored the basic implementation process on how to execute the Spanning Tree Protocol in the network using ns3 tool. We will give another manual to address your queries about this Spanning Tree Protocol