How to Begin Implement Spanning Tree Protocol in NS3
To implement the Spanning Tree Protocol (STP) is a network protocol for model to avoid the loops in Ethernet networks through build a spanning tree topology. Apply the STP in NS-3 environment has includes their pattern a protocol which dynamically are measures and handle the loop-free topology according to their IDs bridge and port costs.
Here’s a step-by-step guide to implementing the Spanning Tree Protocol (STP) in NS-3:
Steps to Begin Implement Spanning Tree Protocol in NS3
Step 1: Understand Spanning Tree Protocol
- Key Features:
- Avoid the loops in Ethernet networks.
- Select the Root Bridge according to their IDs Bridge.
- Defines the minimum path to the root bridge.
- Deactivate the terminated connection to prevent their loops.
- Message Types:
- Bridge Protocol Data Unit (BPDU): Encompasses the following fields:
- Root Bridge ID.
- Sender Bridge ID
- Path Cost of the root.
- Bridge Protocol Data Unit (BPDU): Encompasses the following fields:
- Roles:
- Root Port: Port by the minimum path cost for the root bridge.
- Designated Port: Port on the part of network through path cost for minimum in the root bridge.
- Blocked Port: Port which is not amount of the spanning tree.
Step 2: Set Up NS-3 Environment
- Create a Protocol Directory:
- Build a directory below src/, e.g., src/spanning-tree-protocol/.
- Add Necessary Files:
- spanning-tree-protocol.h: Header file aimed at STP.
- spanning-tree-protocol.cc: Execution file.
- spanning-tree-helper.h and spanning-tree-helper.cc: Helper classes.
- Update Build System:
- Alter for involves the STP component such as wscript in src/.
Step 3: Design STP
Define the Protocol Class
Encompass the Ipv4RoutingProtocol class or generate a new custom protocol class to estimate of STP logic.
Header File (spanning-tree-protocol.h)
#include “ns3/application.h”
#include <map>
#include <vector>
class SpanningTreeProtocol : public ns3::Application {
public:
static ns3::TypeId GetTypeId (void);
SpanningTreeProtocol ();
virtual ~SpanningTreeProtocol ();
void StartApplication () override;
void StopApplication () override;
void SendBpdu ();
void ReceiveBpdu (ns3::Ptr<ns3::Packet> packet);
private:
struct BridgeInfo {
uint32_t bridgeId;
uint32_t rootBridgeId;
uint32_t pathCost;
uint32_t port;
};
uint32_t m_bridgeId; // ID of this bridge
uint32_t m_rootBridgeId; // Current root bridge ID
uint32_t m_pathCost; // Path cost to the root bridge
std::map<uint32_t, BridgeInfo> m_ports; // Port states and costs
ns3::Ptr<ns3::Socket> m_socket; // Communication socket
};
Implement Core Functions
Start and Stop the Protocol
Begin and clean up the protocol for during the replication.
void SpanningTreeProtocol::StartApplication () {
m_socket = ns3::Socket::CreateSocket(GetNode(), ns3::TypeId::LookupByName(“ns3::UdpSocketFactory”));
m_socket->Bind();
m_socket->SetRecvCallback(MakeCallback(&SpanningTreeProtocol::ReceiveBpdu, this));
SendBpdu(); // Start sending BPDUs
}
void SpanningTreeProtocol::StopApplication () {
if (m_socket) {
m_socket->Close();
}
}
Send BPDU
Propagate the BPDUs enclosing the bridge information.
void SpanningTreeProtocol::SendBpdu () {
ns3::Ptr<ns3::Packet> packet = ns3::Create<ns3::Packet> ();
// Serialize BPDU (Root Bridge ID, Sender Bridge ID, Path Cost)
// …
// Broadcast the BPDU
m_socket->SendTo(packet, 0, ns3::InetSocketAddress(ns3::Ipv4Address::GetBroadcast(), 9999));
// Schedule the next BPDU
ns3::Simulator::Schedule(ns3::Seconds(2.0), &SpanningTreeProtocol::SendBpdu, this);
}
Receive BPDU
Development the receive BPDUs for bring up-to-date the bridge information.
void SpanningTreeProtocol::ReceiveBpdu (ns3::Ptr<ns3::Packet> packet) {
// Deserialize BPDU
uint32_t receivedRootBridgeId = …; // Extract root bridge ID
uint32_t receivedSenderBridgeId = …; // Extract sender bridge ID
uint32_t receivedPathCost = …; // Extract path cost
// Update root bridge and path cost if a better path is found
if (receivedRootBridgeId < m_rootBridgeId ||
(receivedRootBridgeId == m_rootBridgeId && receivedPathCost + 1 < m_pathCost)) {
m_rootBridgeId = receivedRootBridgeId;
m_pathCost = receivedPathCost + 1;
// Update port states and roles
}
}
Update Port States
Introduce the roles for every port such as Root, Designated, and Blocked.
void SpanningTreeProtocol::UpdatePortStates () {
for (auto &port : m_ports) {
if (port.second.rootBridgeId == m_rootBridgeId && port.second.pathCost < m_pathCost) {
port.second.port = 1; // Root port
} else {
port.second.port = 0; // Blocked port
}
}
}
Step 4: Write a Helper Class
Replications a script through enable the incorporate.
#include “spanning-tree-protocol.h”
class SpanningTreeHelper {
public:
void Install (ns3::NodeContainer nodes, uint32_t bridgeIdStart) {
uint32_t bridgeId = bridgeIdStart;
for (auto it = nodes.Begin(); it != nodes.End(); ++it, ++bridgeId) {
ns3::Ptr<SpanningTreeProtocol> stp = ns3::CreateObject<SpanningTreeProtocol>();
stp->SetBridgeId(bridgeId);
(*it)->AddApplication(stp);
}
}
};
Step 5: Write a Simulation Script
Example Simulation Script
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “spanning-tree-helper.h”
using namespace ns3;
int main (int argc, char *argv[]) {
NodeContainer nodes;
nodes.Create(5);
PointToPointHelper p2p;
p2p.SetDeviceAttribute(“DataRate”, StringValue(“100Mbps”));
p2p.SetChannelAttribute(“Delay”, StringValue(“2ms”));
for (uint32_t i = 0; i < nodes.GetN() – 1; ++i) {
p2p.Install(nodes.Get(i), nodes.Get(i + 1));
}
SpanningTreeHelper stpHelper;
stpHelper.Install(nodes, 1);
Simulator::Run();
Simulator::Destroy();
return 0;
}
Step 6: Compile and Run
- Build the Protocol:
./waf configure
./waf build
- Run the Simulation:
./waf –run your-script
Step 7: Analyze and Extend
- Tracing:
- Use AsciiTraceHelper or PcapHelper for follow the BPDU messages and validate the topology.
- Enhancements:
- It helps for enhance the Rapid Spanning Tree Protocol (RSTP).
- Validate below the dynamic surroundings for sample link failures and recoveries.
In the conclusion, you can simulate Spanning Tree Protocol and evaluate their performance using ns3 analysis tool. Any queries related to this project will be clarified in a different manual.