To implement the network structure in ns3 needs to setup the nodes, links, and their configurations. This is usually contains to setup simple network topologies such as star, mesh, tree, and custom topologies. Below are the detailed procedures to implement the numerous network structures in ns3 tool:
Step-by-Step Implementation:
Step 1: Set Up the ns3 Environment
- Install ns3: Make sure ns3 is installed in the computer.
sudo apt-get update
sudo apt-get install ns3
Create a New ns3 Project: Create a directory for your new project within the ns3 workspace.
Step 2: Define Basic Network Structures
- Star Topology: In a star topology, all nodes are connected to a central node (hub).
// star-topology.cc
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“StarTopologyExample”);
int main (int argc, char *argv[]) {
CommandLine cmd;
cmd.Parse (argc, argv);
uint32_t numNodes = 5; // Number of peripheral nodes
NodeContainer hubNode;
hubNode.Create (1);
NodeContainer peripheralNodes;
peripheralNodes.Create (numNodes);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices;
for (uint32_t i = 0; i < numNodes; ++i) {
NodeContainer pair (hubNode.Get (0), peripheralNodes.Get (i));
devices.Add (pointToPoint.Install (pair));
}
InternetStackHelper stack;
stack.Install (hubNode);
stack.Install (peripheralNodes);
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (hubNode.Get (0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (0), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps;
for (uint32_t i = 0; i < numNodes; ++i) {
clientApps.Add (echoClient.Install (peripheralNodes.Get (i)));
}
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Mesh Topology: In a mesh topology, each node is connected to every other node.
// mesh-topology.cc
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“MeshTopologyExample”);
int main (int argc, char *argv[]) {
CommandLine cmd;
cmd.Parse (argc, argv);
uint32_t numNodes = 5;
NodeContainer nodes;
nodes.Create (numNodes);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices;
for (uint32_t i = 0; i < numNodes; ++i) {
for (uint32_t j = i + 1; j < numNodes; ++j) {
NodeContainer pair (nodes.Get (i), nodes.Get (j));
devices.Add (pointToPoint.Install (pair));
}
}
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (0), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps;
for (uint32_t i = 1; i < numNodes; ++i) {
clientApps.Add (echoClient.Install (nodes.Get (i)));
}
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Tree Topology: In a tree topology, nodes are arranged in a hierarchical structure.
// tree-topology.cc
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“TreeTopologyExample”);
void CreateTree (NodeContainer &parent, NodeContainer &children, uint32_t depth, uint32_t fanout, PointToPointHelper &pointToPoint) {
if (depth == 0) return;
NetDeviceContainer devices;
for (uint32_t i = 0; i < parent.GetN (); ++i) {
for (uint32_t j = 0; j < fanout; ++j) {
Ptr<Node> child = CreateObject<Node> ();
children.Add (child);
devices.Add (pointToPoint.Install (parent.Get (i), child));
}
}
NodeContainer nextLevel;
CreateTree (children, nextLevel, depth – 1, fanout, pointToPoint);
}
int main (int argc, char *argv[]) {
CommandLine cmd;
cmd.Parse (argc, argv);
uint32_t depth = 3;
uint32_t fanout = 2;
NodeContainer root;
root.Create (1);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NodeContainer children;
CreateTree (root, children, depth, fanout, pointToPoint);
InternetStackHelper stack;
stack.Install (root);
stack.Install (children);
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (pointToPoint.Install (root.Get (0), children.Get (0))));
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (root.Get (0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (0), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps;
for (uint32_t i = 0; i < children.GetN (); ++i) {
clientApps.Add (echoClient.Install (children.Get (i))));
}
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Step 3: Compile and Run the Simulations
- Compile the Scripts: Compile your scripts using the waf build system.
./waf build
Run the Simulations: Run your simulation scripts and observe the results.
./waf –run scratch/star-topology
./waf –run scratch/mesh-topology
./waf –run scratch/tree-topology
Step 4: Enable Tracing and Analyze Results
- Enable Tracing: Add tracing to collect data for analysis.
AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll (ascii.CreateFileStream (“star-topology.tr”));
Run the Simulation: Set the simulation stop time and run it.
Simulator::Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
Step 5: Analyse the Results
After running the simulations, analyse the generated trace files (star-topology.tr, mesh-topology.tr, tree-topology.tr) to study the network behaviour and performance.
In the whole, we clearly learned to implement the network structure that has created the topology by using the nodes, links, and their configurations with the help of ns3. We will give elaborate additional information on how the network structure will analyse the output in other tools.
Obtain project suggestions and performance evaluations concerning Network Structure within the ns3 tool. We provide a range of project ideas and conduct in-depth comparative analyses based on your concepts. Our team implements network topologies like star, mesh, tree, and custom configurations specific to your projects. Share your research details with us for further guidance.