To implement switched mesh topology in ns3, wee need to set up a network where each node is connected to a switch, and the switches are interconnected to form a mesh. Here is a detailed guide to achieve this using ns3.
Steps for implementation
Step 1: Install ns-3
Make sure that ns3 is installed in the computer. If not, install it.
Step 2: Create a New Simulation Script
For your simulation, create a new C++ script.
Step 3: Include Necessary Headers
include the necessary ns3 headers in your script.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/csma-module.h”
#include “ns3/applications-module.h”
#include “ns3/mobility-module.h”
Step 4: Set Up the Switched Mesh Topology
below is an example to set up a switched mesh topology with four nodes and four switches.
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“SwitchedMeshTopology”);
int main (int argc, char *argv[])
{
// Configure command line parameters
CommandLine cmd;
cmd.Parse (argc, argv);
// Create nodes
NodeContainer hosts;
hosts.Create (4); // Create 4 host nodes
NodeContainer switches;
switches.Create (4); // Create 4 switch nodes
// Create point-to-point links between each host and its respective switch
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“1Gbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer hostDevices, switchDevices;
for (uint32_t i = 0; i < hosts.GetN (); ++i)
{
NodeContainer pair (hosts.Get (i), switches.Get (i));
NetDeviceContainer devicePair = pointToPoint.Install (pair);
hostDevices.Add (devicePair.Get (0));
switchDevices.Add (devicePair.Get (1));
}
// Create CSMA links to interconnect the switches in a mesh topology
CsmaHelper csma;
csma.SetChannelAttribute (“DataRate”, StringValue (“1Gbps”));
csma.SetChannelAttribute (“Delay”, TimeValue (NanoSeconds (6560)));
NetDeviceContainer switchMeshDevices;
Ipv4AddressHelper address;
Ipv4InterfaceContainer switchInterfaces;
for (uint32_t i = 0; i < switches.GetN (); ++i)
{
for (uint32_t j = i + 1; j < switches.GetN (); ++j)
{
NodeContainer pair (switches.Get (i), switches.Get (j));
NetDeviceContainer devicePair = csma.Install (pair);
switchMeshDevices.Add (devicePair);
std::ostringstream subnet;
subnet << “10.1.” << i << “.” << j << “.0”;
address.SetBase (subnet.str ().c_str (), “255.255.255.0”);
switchInterfaces.Add (address.Assign (devicePair));
}
}
// Install the internet stack
InternetStackHelper internet;
internet.Install (hosts);
internet.Install (switches);
// Assign IP addresses to host devices
Ipv4AddressHelper hostAddress;
Ipv4InterfaceContainer hostInterfaces;
for (uint32_t i = 0; i < hosts.GetN (); ++i)
{
std::ostringstream subnet;
subnet << “192.168.1.” << i + 1 << “.0”;
hostAddress.SetBase (subnet.str ().c_str (), “255.255.255.0”);
hostInterfaces.Add (hostAddress.Assign (hostDevices.Get (i)));
}
// Set up mobility model (optional)
MobilityHelper mobility;
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
positionAlloc->Add (Vector (50.0, 50.0, 0.0)); // Host node 1 position
positionAlloc->Add (Vector (100.0, 50.0, 0.0)); // Host node 2 position
positionAlloc->Add (Vector (50.0, 100.0, 0.0)); // Host node 3 position
positionAlloc->Add (Vector (100.0, 100.0, 0.0)); // Host node 4 position
mobility.SetPositionAllocator (positionAlloc);
mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);
mobility.Install (hosts);
mobility.Install (switches);
// Set up applications (e.g., UDP echo server and client)
uint16_t port = 9; // Port number for applications
// Install UDP Echo Server on host 0
UdpEchoServerHelper echoServer (port);
ApplicationContainer serverApps = echoServer.Install (hosts.Get (0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
// Install UDP Echo Clients on other hosts to communicate with host 0
for (uint32_t i = 1; i < hosts.GetN (); ++i)
{
UdpEchoClientHelper echoClient (hostInterfaces.GetAddress (0), port);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (hosts.Get (i));
clientApps.Start (Seconds (2.0 + i)); // Stagger start times
clientApps.Stop (Seconds (10.0));
}
// Run simulation
Simulator::Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Step 5: Build and Run the Simulation
Save the script as switched-mesh -topology.cc and build the script using waf, then run the simulation.
./waf configure –enable-examples
./waf build
./waf –run scratch/ switched-mesh -topology
Explanation of the script
- Node Creation: For host and switch nodes, eight nodes are created.
- Point-to-Point Links: Configures point-to-point links between each host and its respective switch.
- CSMA links: To interconnect the switches in a mesh topology, configured CSMA links.
- Internet Stack: On all hosts and switches, installs the internet stack.
- IP Addressing: To host devices and the switch mesh links, assigns IP addresses.
- Mobility Model: Sets the position of nodes using ListPositionAllocator and ConstantPositionMobilityModel.
- Applications: On host 0, sets up a UDP echo server and a UDP echo client on other hosts to demonstrate communication.
On the whole, we had a performance analysis on the implementation of switched mesh topology by setting up a network where each node is connected to a switch, and the switches are interconnected to form a mesh. Also, we provide a detailed explanation on Switched Mesh Topology.
Even after reading it, if you are having trouble implementing the switched mesh topology in ns3, please contact us at ns3simulation.com, and our developers will assist you.