Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Implement Network Topology in ns3

To implement the network topology in ns3 that needs to generate the nodes, relates them with network devices, assign the IP addresses and then setup the applications to create the traffic. The given is the procedure on how to make a basic network topology in ns3:

Step-by-Step Implementation:

Step 1: Set Up the Simulation Environment

  • Make certain ns3 is installed in the computer.

Step 2: Create the Network Topology

  • Generate the simple network topology using ns3. The given below is the sample script to setup a basic network with four nodes connected by point-to-point links.

Step 3: Write the Script

  • The given below is the complete sample script on how to generate and configure a network topology in ns3:

#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”

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

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“NetworkTopologyExample”);

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

{

CommandLine cmd;

cmd.Parse (argc, argv);

// Create nodes

NodeContainer nodes;

nodes.Create (4);

// Create point-to-point links and set attributes

PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));

pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));

// Install devices and links

NetDeviceContainer devices01;

devices01 = pointToPoint.Install (nodes.Get(0), nodes.Get(1));

NetDeviceContainer devices12;

devices12 = pointToPoint.Install (nodes.Get(1), nodes.Get(2));

NetDeviceContainer devices23;

devices23 = pointToPoint.Install (nodes.Get(2), nodes.Get(3));

// Install the Internet stack on the nodes

InternetStackHelper stack;

stack.Install (nodes);

// Assign IP addresses to the devices

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces01 = address.Assign (devices01);

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

Ipv4InterfaceContainer interfaces12 = address.Assign (devices12);

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

Ipv4InterfaceContainer interfaces23 = address.Assign (devices23);

// Create a UDP server application on node 3

UdpEchoServerHelper echoServer (9);

ApplicationContainer serverApps = echoServer.Install (nodes.Get (3));

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

// Create a UDP client application on node 0

UdpEchoClientHelper echoClient (interfaces23.GetAddress (1), 9);

echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));

echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));

echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));

ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));

clientApps.Start (Seconds (2.0));

clientApps.Stop (Seconds (10.0));

// Enable routing

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

// Enable Flow Monitor to collect performance metrics

FlowMonitorHelper flowmonHelper;

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

// Run the simulation

Simulator::Run ();

// Output Flow Monitor statistics

monitor->CheckForLostPackets ();

Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmonHelper.GetClassifier ());

std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();

for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)

{

Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);

std::cout << “Flow ID: ” << i->first << ” Source Address: ” << t.sourceAddress << ” Destination Address: ” << t.destinationAddress << std::endl;

std::cout << “Tx Packets = ” << i->second.txPackets << std::endl;

std::cout << “Rx Packets = ” << i->second.rxPackets << std::endl;

std::cout << “Throughput: ” << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds () – i->second.timeFirstTxPacket.GetSeconds ()) / 1024 / 1024 << ” Mbps” << std::endl;

}

// Clean up the simulation

Simulator::Destroy ();

return 0;

}

Step 4: Compile and Run the Script

  1. Save the script as network-topology-example.cc in the scratch directory of your ns-3 installation.
  2. Compile the script using the following commands:

./waf configure

./waf build

./waf –run network-topology-example

Step 5: Analyse the Results

After running the simulation, the script will output performance metrics such as the number of transmitted and received packets, and throughput. We can further evaluate this information or modify the script to collect additional metrics.

As we discussed earlier, we had knowledge on how to simulate and analyse the outcomes in the Network Topology in ns3 tool. We will give the elaborate details on how the network topology will implement and analyse the outcome in other simulation framework.

Setting up the Network Topology in ns3 involves assigning IP addresses and configuring applications to generate traffic. Feel free to reach out to ns3simulation.com for the best solutions provided by our developers. We are here to assist you in analyzing performance and achieving optimal outcomes.